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
 Return the position at t. 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 105 of file ray.h.

Constructor & Destructor Documentation

◆ RAYSEG2D()

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

Definition at line 243 of file ray.cpp.

244 {
245  m_Start = s;
246  m_End = e;
247  m_End_minus_start = e - s;
248  m_Length = glm::length( m_End_minus_start );
249  m_Dir = glm::normalize( m_End_minus_start );
250  m_InvDir = ( 1.0f / m_Dir );
251 
252  if( fabs( m_Dir.x ) < FLT_EPSILON )
253  m_InvDir.x = NextFloatDown( FLT_MAX );
254 
255  if( fabs( m_Dir.y ) < FLT_EPSILON )
256  m_InvDir.y = NextFloatDown( FLT_MAX );
257 
259 }
SFVEC2F m_Dir
Definition: ray.h:110
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:113
float m_Length
Definition: ray.h:112
SFVEC2F m_End
Definition: ray.h:108
SFVEC2F m_InvDir
Definition: ray.h:111
SFVEC2F m_Start
Definition: ray.h:107
SFVEC2F m_End_minus_start
Definition: ray.h:109

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

137 { return m_Start + m_Dir * t; }
SFVEC2F m_Dir
Definition: ray.h:110
SFVEC2F m_Start
Definition: ray.h:107

References m_Dir, and m_Start.

Referenced by RING_2D::Intersect(), FILLED_CIRCLE_2D::Intersect(), and IntersectCircle().

◆ atNormalized()

SFVEC2F RAYSEG2D::atNormalized ( float  t) const
inline

Return the position at t.

t - value 0.0 ... 1.0

Definition at line 135 of file ray.h.

135 { return m_Start + m_End_minus_start * t; }
SFVEC2F m_Start
Definition: ray.h:107
SFVEC2F m_End_minus_start
Definition: ray.h:109

References m_End_minus_start, and m_Start.

Referenced by LAYER_ITEM_2D::Intersect().

◆ DistanceToPointSquared()

float RAYSEG2D::DistanceToPointSquared ( const SFVEC2F aPoint) const

Definition at line 294 of file ray.cpp.

295 {
296  SFVEC2F p = aPoint - m_Start;
297 
298  const float c1 = glm::dot( p, m_End_minus_start );
299 
300  if( c1 < FLT_EPSILON )
301  return glm::dot( p, p );
302 
303  if( m_DOT_End_minus_start <= c1 )
304  {
305  p = aPoint - m_End;
306  }
307  else
308  {
309  const float b = c1 / m_DOT_End_minus_start;
310  const SFVEC2F pb = m_Start + m_End_minus_start * b;
311 
312  p = aPoint - pb;
313  }
314 
315  return glm::dot( p, p );
316 }
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42
float m_DOT_End_minus_start
dot( m_End_minus_start, m_End_minus_start)
Definition: ray.h:113
SFVEC2F m_End
Definition: ray.h:108
SFVEC2F m_Start
Definition: ray.h:107
SFVEC2F m_End_minus_start
Definition: ray.h:109

References m_DOT_End_minus_start, m_End, m_End_minus_start, and m_Start.

Referenced by ROUND_SEGMENT::Intersect(), ROUND_SEGMENT::IntersectP(), and ROUND_SEGMENT_2D::IsPointInside().

◆ IntersectCircle()

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

Definition at line 319 of file ray.cpp.

321 {
322  // This code used directly from Steve Marschner's CS667 framework
323  // http://cs665pd.googlecode.com/svn/trunk/photon/sphere.cpp
324 
325  // Compute some factors used in computation
326  const float qx = m_Start.x - aCenter.x;
327  const float qy = m_Start.y - aCenter.y;
328 
329  const float qd = qx * m_Dir.x + qy * m_Dir.y;
330  const float qq = qx * qx + qy * qy;
331 
332  // solving the quadratic equation for t at the pts of intersection
333  // dd*t^2 + (2*qd)*t + (qq-r^2) = 0
334  const float discriminantsqr = (qd * qd - (qq - aRadius * aRadius));
335 
336  // If the discriminant is less than zero, there is no intersection
337  if( discriminantsqr < FLT_EPSILON )
338  return false;
339 
340  // Otherwise check and make sure that the intersections occur on the ray (t
341  // > 0) and return the closer one
342  const float discriminant = std::sqrt( discriminantsqr );
343  const float t1 = ( -qd - discriminant );
344  const float t2 = ( -qd + discriminant );
345 
346  if( ( ( t1 < 0.0f ) || ( t1 > m_Length ) ) && ( ( t2 < 0.0f ) || ( t2 > m_Length ) ) )
347  return false; // Neither intersection was in the ray's half line.
348 
349  // Convert the intersection to a normalized
350  *aOutT0 = t1 / m_Length;
351  *aOutT1 = t2 / m_Length;
352 
353  SFVEC2F hitPointT1 = at( t1 );
354  SFVEC2F hitPointT2 = at( t2 );
355 
356  *aOutNormalT0 = ( hitPointT1 - aCenter ) / aRadius;
357  *aOutNormalT1 = ( hitPointT2 - aCenter ) / aRadius;
358 
359  return true;
360 }
SFVEC2F m_Dir
Definition: ray.h:110
SFVEC2F at(float t) const
Definition: ray.h:137
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42
float m_Length
Definition: ray.h:112
SFVEC2F m_Start
Definition: ray.h:107

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

Referenced by ROUND_SEGMENT_2D::Intersect().

◆ IntersectSegment()

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

Definition at line 262 of file ray.cpp.

264 {
265  float rxs = m_End_minus_start.x * aEnd_minus_start.y - m_End_minus_start.y *
266  aEnd_minus_start.x;
267 
268  if( std::abs( rxs ) > glm::epsilon<float>() )
269  {
270  const float inv_rxs = 1.0f / rxs;
271 
272  const SFVEC2F pq = aStart - m_Start;
273 
274  const float t = ( pq.x * aEnd_minus_start.y - pq.y * aEnd_minus_start.x ) * inv_rxs;
275 
276  if( ( t < 0.0f ) || ( t > 1.0f ) )
277  return false;
278 
279  float u = ( pq.x * m_End_minus_start.y - pq.y * m_End_minus_start.x ) * inv_rxs;
280 
281  if( ( u < 0.0f ) || ( u > 1.0f ) )
282  return false;
283 
284  *aOutT = t;
285 
286  return true;
287  }
288 
289  return false;
290 }
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42
SFVEC2F m_Start
Definition: ray.h:107
SFVEC2F m_End_minus_start
Definition: ray.h:109

References m_End_minus_start, and m_Start.

Referenced by POLYGON_4PT_2D::Intersect(), and ROUND_SEGMENT_2D::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 113 of file ray.h.

Referenced by DistanceToPointSquared(), and RAYSEG2D().

◆ m_End

◆ m_End_minus_start

◆ m_InvDir

SFVEC2F RAYSEG2D::m_InvDir

Definition at line 111 of file ray.h.

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

◆ m_Length

◆ m_Start


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