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 110 of file ray.h.

Constructor & Destructor Documentation

◆ RAYSEG2D()

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

Definition at line 274 of file ray.cpp.

275 {
276  m_Start = s;
277  m_End = e;
278  m_End_minus_start = e - s;
279  m_Length = glm::length( m_End_minus_start );
280  m_Dir = glm::normalize( m_End_minus_start );
281  m_InvDir = (1.0f / m_Dir);
282 
283  if( fabs(m_Dir.x) < FLT_EPSILON )
284  m_InvDir.x = NextFloatDown(FLT_MAX);
285 
286  if( fabs(m_Dir.y) < FLT_EPSILON )
287  m_InvDir.y = NextFloatDown(FLT_MAX);
288 
290 }
SFVEC2F m_Dir
Definition: ray.h:115
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:118
float m_Length
Definition: ray.h:117
SFVEC2F m_End
Definition: ray.h:113
SFVEC2F m_InvDir
Definition: ray.h:116
SFVEC2F m_Start
Definition: ray.h:112
SFVEC2F m_End_minus_start
Definition: ray.h:114

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

Member Function Documentation

◆ at()

SFVEC2F RAYSEG2D::at ( float  t) const
inline

Definition at line 141 of file ray.h.

141 { return m_Start + m_Dir * t; }
SFVEC2F m_Dir
Definition: ray.h:115
SFVEC2F m_Start
Definition: ray.h:112

References m_Dir, and m_Start.

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

◆ atNormalized()

SFVEC2F RAYSEG2D::atNormalized ( float  t) const
inline

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

1.0

Definition at line 139 of file ray.h.

139 { return m_Start + m_End_minus_start * t; }
SFVEC2F m_Start
Definition: ray.h:112
SFVEC2F m_End_minus_start
Definition: ray.h:114

References m_End_minus_start, and m_Start.

Referenced by CITEMLAYERCSG2D::Intersect().

◆ DistanceToPointSquared()

float RAYSEG2D::DistanceToPointSquared ( const SFVEC2F aPoint) const

Definition at line 327 of file ray.cpp.

328 {
329  SFVEC2F p = aPoint - m_Start;
330 
331  const float c1 = glm::dot( p, m_End_minus_start );
332 
333  if( c1 < FLT_EPSILON )
334  return glm::dot( p, p );
335 
336  if( m_DOT_End_minus_start <= c1 )
337  p = aPoint - m_End;
338  else
339  {
340  const float b = c1 / m_DOT_End_minus_start;
341  const SFVEC2F pb = m_Start + m_End_minus_start * b;
342 
343  p = aPoint - pb;
344  }
345 
346  return glm::dot( p, p );
347 }
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:118
SFVEC2F m_End
Definition: ray.h:113
SFVEC2F m_Start
Definition: ray.h:112
SFVEC2F m_End_minus_start
Definition: ray.h:114

References m_DOT_End_minus_start, m_End, m_End_minus_start, and m_Start.

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

◆ IntersectCircle()

bool RAYSEG2D::IntersectCircle ( const SFVEC2F aCenter,
float  aRadius,
float *  aOutT0,
float *  aOutT1,
SFVEC2F aOutNormalT0,
SFVEC2F aOutNormalT1 
) const

Definition at line 350 of file ray.cpp.

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

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

Referenced by CROUNDSEGMENT2D::Intersect().

◆ IntersectSegment()

bool RAYSEG2D::IntersectSegment ( const SFVEC2F aStart,
const SFVEC2F aEnd_minus_start,
float *  aOutT 
) const

Definition at line 293 of file ray.cpp.

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

References m_End_minus_start, and m_Start.

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

Member Data Documentation

◆ m_Dir

◆ m_DOT_End_minus_start

float RAYSEG2D::m_DOT_End_minus_start

dot( m_End_minus_start, m_End_minus_start)

Definition at line 118 of file ray.h.

Referenced by DistanceToPointSquared(), and RAYSEG2D().

◆ m_End

◆ m_End_minus_start

◆ m_InvDir

SFVEC2F RAYSEG2D::m_InvDir

Definition at line 116 of file ray.h.

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

◆ m_Length

◆ m_Start


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