KiCad PCB EDA Suite
RAYSEG2D Struct Reference

#include <ray.h>

Public Member Functions

 RAYSEG2D (const SFVEC2F &s, const SFVEC2F &e)
 
bool IntersectSegment (const SFVEC2F &aStart, const SFVEC2F &aEnd_minus_start, float *aOutT) const
 
bool IntersectCircle (const SFVEC2F &aCenter, float aRadius, float *aOutT0, float *aOutT1, SFVEC2F *aOutNormalT0, SFVEC2F *aOutNormalT1) const
 
float DistanceToPointSquared (const SFVEC2F &aPoint) const
 
SFVEC2F atNormalized (float t) const
 Function atNormalized - returns the position at t t - value 0.0 ... More...
 
SFVEC2F at (float t) const
 

Public Attributes

SFVEC2F m_Start
 
SFVEC2F m_End
 
SFVEC2F m_End_minus_start
 
SFVEC2F m_Dir
 
SFVEC2F m_InvDir
 
float m_Length
 
float m_DOT_End_minus_start
 dot( m_End_minus_start, m_End_minus_start) More...
 

Detailed Description

Definition at line 86 of file ray.h.

Constructor & Destructor Documentation

RAYSEG2D::RAYSEG2D ( const SFVEC2F s,
const SFVEC2F e 
)

Definition at line 272 of file ray.cpp.

References RAY::m_Dir, RAY::m_InvDir, and NextFloatDown().

273 {
274  m_Start = s;
275  m_End = e;
276  m_End_minus_start = e - s;
277  m_Length = glm::length( m_End_minus_start );
278  m_Dir = glm::normalize( m_End_minus_start );
279  m_InvDir = (1.0f / m_Dir);
280 
281  if( fabs(m_Dir.x) < FLT_EPSILON )
282  m_InvDir.x = NextFloatDown(FLT_MAX);
283 
284  if( fabs(m_Dir.y) < FLT_EPSILON )
285  m_InvDir.y = NextFloatDown(FLT_MAX);
286 
288 }
SFVEC2F m_Dir
Definition: ray.h:91
float NextFloatDown(float v)
Definition: 3d_fastmath.h:157
float m_DOT_End_minus_start
dot( m_End_minus_start, m_End_minus_start)
Definition: ray.h:94
float m_Length
Definition: ray.h:93
SFVEC2F m_End
Definition: ray.h:89
SFVEC2F m_InvDir
Definition: ray.h:92
SFVEC2F m_Start
Definition: ray.h:88
SFVEC2F m_End_minus_start
Definition: ray.h:90

Member Function Documentation

SFVEC2F RAYSEG2D::at ( float  t) const
inline

Definition at line 117 of file ray.h.

References IntersectSegment().

Referenced by CFILLEDCIRCLE2D::Intersect(), and CRING2D::Intersect().

117 { return m_Start + m_Dir * t; }
SFVEC2F m_Dir
Definition: ray.h:91
SFVEC2F m_Start
Definition: ray.h:88
SFVEC2F RAYSEG2D::atNormalized ( float  t) const
inline

Function atNormalized - returns the position at t t - value 0.0 ...

1.0

Definition at line 115 of file ray.h.

Referenced by CITEMLAYERCSG2D::Intersect().

115 { return m_Start + m_End_minus_start * t; }
SFVEC2F m_Start
Definition: ray.h:88
SFVEC2F m_End_minus_start
Definition: ray.h:90
float RAYSEG2D::DistanceToPointSquared ( const SFVEC2F aPoint) const

Definition at line 325 of file ray.cpp.

Referenced by CROUNDSEG::Intersect(), CROUNDSEG::IntersectP(), and CROUNDSEGMENT2D::IsPointInside().

326 {
327  SFVEC2F p = aPoint - m_Start;
328 
329  const float c1 = glm::dot( p, m_End_minus_start );
330 
331  if( c1 < FLT_EPSILON )
332  return glm::dot( p, p );
333 
334  if( m_DOT_End_minus_start <= c1 )
335  p = aPoint - m_End;
336  else
337  {
338  const float b = c1 / m_DOT_End_minus_start;
339  const SFVEC2F pb = m_Start + m_End_minus_start * b;
340 
341  p = aPoint - pb;
342  }
343 
344  return glm::dot( p, p );
345 }
glm::vec2 SFVEC2F
Definition: xv3d_types.h:45
float m_DOT_End_minus_start
dot( m_End_minus_start, m_End_minus_start)
Definition: ray.h:94
SFVEC2F m_End
Definition: ray.h:89
SFVEC2F m_Start
Definition: ray.h:88
SFVEC2F m_End_minus_start
Definition: ray.h:90
bool RAYSEG2D::IntersectCircle ( const SFVEC2F aCenter,
float  aRadius,
float *  aOutT0,
float *  aOutT1,
SFVEC2F aOutNormalT0,
SFVEC2F aOutNormalT1 
) const

Definition at line 348 of file ray.cpp.

References RAY::at(), and RAY::m_Dir.

Referenced by CROUNDSEGMENT2D::Intersect().

354 {
355  // This code used directly from Steve Marschner's CS667 framework
356  // http://cs665pd.googlecode.com/svn/trunk/photon/sphere.cpp
357 
358  // Compute some factors used in computation
359  const float qx = m_Start.x - aCenter.x;
360  const float qy = m_Start.y - aCenter.y;
361 
362  const float qd = qx * m_Dir.x + qy * m_Dir.y;
363  const float qq = qx * qx + qy * qy;
364 
365  // solving the quadratic equation for t at the pts of intersection
366  // dd*t^2 + (2*qd)*t + (qq-r^2) = 0
367  const float discriminantsqr = (qd * qd - (qq - aRadius * aRadius));
368 
369  // If the discriminant is less than zero, there is no intersection
370  if( discriminantsqr < FLT_EPSILON )
371  return false;
372 
373 
374  // Otherwise check and make sure that the intersections occur on the ray (t
375  // > 0) and return the closer one
376  const float discriminant = std::sqrt( discriminantsqr );
377  const float t1 = (-qd - discriminant);
378  const float t2 = (-qd + discriminant);
379 
380  if( (( t1 < 0.0f ) || ( t1 > m_Length ) ) &&
381  (( t2 < 0.0f ) || ( t2 > m_Length ) ) )
382  return false;// Neither intersection was in the ray's half line.
383 
384  // Convert the intersection to a normalized
385  *aOutT0 = t1 / m_Length;
386  *aOutT1 = t2 / m_Length;
387 
388  SFVEC2F hitPointT1 = at( t1 );
389  SFVEC2F hitPointT2 = at( t2 );
390 
391  *aOutNormalT0 = (hitPointT1 - aCenter) / aRadius;
392  *aOutNormalT1 = (hitPointT2 - aCenter) / aRadius;
393 
394  return true;
395 }
SFVEC2F m_Dir
Definition: ray.h:91
SFVEC2F at(float t) const
Definition: ray.h:117
glm::vec2 SFVEC2F
Definition: xv3d_types.h:45
float m_Length
Definition: ray.h:93
SFVEC2F m_Start
Definition: ray.h:88
bool RAYSEG2D::IntersectSegment ( const SFVEC2F aStart,
const SFVEC2F aEnd_minus_start,
float *  aOutT 
) const

Definition at line 291 of file ray.cpp.

References abs.

Referenced by CPOLYGON4PTS2D::Intersect(), and CROUNDSEGMENT2D::Intersect().

294 {
295  float rxs = m_End_minus_start.x *
296  aEnd_minus_start.y - m_End_minus_start.y *
297  aEnd_minus_start.x;
298 
299  if( std::abs( rxs ) > glm::epsilon<float>() )
300  {
301  const float inv_rxs = 1.0f / rxs;
302 
303  const SFVEC2F pq = aStart - m_Start;
304 
305  const float t = (pq.x * aEnd_minus_start.y - pq.y * aEnd_minus_start.x) * inv_rxs;
306 
307  if( (t < 0.0f) || (t > 1.0f) )
308  return false;
309 
310  float u = (pq.x * m_End_minus_start.y - pq.y * m_End_minus_start.x) * inv_rxs;
311 
312  if( (u < 0.0f) || (u > 1.0f) )
313  return false;
314 
315  *aOutT = t;
316 
317  return true;
318  }
319 
320  return false;
321 }
#define abs(a)
Definition: auxiliary.h:84
glm::vec2 SFVEC2F
Definition: xv3d_types.h:45
SFVEC2F m_Start
Definition: ray.h:88
SFVEC2F m_End_minus_start
Definition: ray.h:90

Member Data Documentation

float RAYSEG2D::m_DOT_End_minus_start

dot( m_End_minus_start, m_End_minus_start)

Definition at line 94 of file ray.h.

SFVEC2F RAYSEG2D::m_End_minus_start

Definition at line 90 of file ray.h.

Referenced by CROUNDSEGMENT2D::GetEnd_minus_Start(), and CPOLYGONBLOCK2D::Intersect().

SFVEC2F RAYSEG2D::m_InvDir

Definition at line 92 of file ray.h.

Referenced by CBBOX2D::Intersect().

float RAYSEG2D::m_Length

The documentation for this struct was generated from the following files: