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 270 of file ray.cpp.

References m_Dir, m_DOT_End_minus_start, m_End, m_End_minus_start, m_InvDir, m_Length, m_Start, and NextFloatDown().

271 {
272  m_Start = s;
273  m_End = e;
274  m_End_minus_start = e - s;
275  m_Length = glm::length( m_End_minus_start );
276  m_Dir = glm::normalize( m_End_minus_start );
277  m_InvDir = (1.0f / m_Dir);
278 
279  if( fabs(m_Dir.x) < FLT_EPSILON )
280  m_InvDir.x = NextFloatDown(FLT_MAX);
281 
282  if( fabs(m_Dir.y) < FLT_EPSILON )
283  m_InvDir.y = NextFloatDown(FLT_MAX);
284 
286 }
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.

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

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 323 of file ray.cpp.

References m_DOT_End_minus_start, m_End, m_End_minus_start, and m_Start.

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

324 {
325  SFVEC2F p = aPoint - m_Start;
326 
327  const float c1 = glm::dot( p, m_End_minus_start );
328 
329  if( c1 < FLT_EPSILON )
330  return glm::dot( p, p );
331 
332  if( m_DOT_End_minus_start <= c1 )
333  p = aPoint - m_End;
334  else
335  {
336  const float b = c1 / m_DOT_End_minus_start;
337  const SFVEC2F pb = m_Start + m_End_minus_start * b;
338 
339  p = aPoint - pb;
340  }
341 
342  return glm::dot( p, p );
343 }
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 346 of file ray.cpp.

References at(), m_Dir, m_Length, and m_Start.

Referenced by CROUNDSEGMENT2D::Intersect().

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

References m_End_minus_start, and m_Start.

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

292 {
293  float rxs = m_End_minus_start.x *
294  aEnd_minus_start.y - m_End_minus_start.y *
295  aEnd_minus_start.x;
296 
297  if( fabs( rxs ) > glm::epsilon<float>() )
298  {
299  const float inv_rxs = 1.0f / rxs;
300 
301  const SFVEC2F pq = aStart - m_Start;
302 
303  const float t = (pq.x * aEnd_minus_start.y - pq.y * aEnd_minus_start.x) * inv_rxs;
304 
305  if( (t < 0.0f) || (t > 1.0f) )
306  return false;
307 
308  float u = (pq.x * m_End_minus_start.y - pq.y * m_End_minus_start.x) * inv_rxs;
309 
310  if( (u < 0.0f) || (u > 1.0f) )
311  return false;
312 
313  *aOutT = t;
314 
315  return true;
316  }
317 
318  return false;
319 }
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.

Referenced by DistanceToPointSquared(), and RAYSEG2D().

SFVEC2F RAYSEG2D::m_End_minus_start
SFVEC2F RAYSEG2D::m_InvDir

Definition at line 92 of file ray.h.

Referenced by CBBOX2D::Intersect(), and RAYSEG2D().


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