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

Constructor & Destructor Documentation

◆ RAYSEG2D()

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

Definition at line 249 of file ray.cpp.

250 {
251  m_Start = s;
252  m_End = e;
253  m_End_minus_start = e - s;
254  m_Length = glm::length( m_End_minus_start );
255  m_Dir = glm::normalize( m_End_minus_start );
256  m_InvDir = ( 1.0f / m_Dir );
257 
258  if( fabs( m_Dir.x ) < FLT_EPSILON )
259  m_InvDir.x = NextFloatDown( FLT_MAX );
260 
261  if( fabs( m_Dir.y ) < FLT_EPSILON )
262  m_InvDir.y = NextFloatDown( FLT_MAX );
263 
265 }
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 142 of file ray.h.

142 { 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 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 140 of file ray.h.

140 { 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 LAYER_ITEM_2D::Intersect().

◆ DistanceToPointSquared()

float RAYSEG2D::DistanceToPointSquared ( const SFVEC2F aPoint) const

Definition at line 300 of file ray.cpp.

301 {
302  SFVEC2F p = aPoint - m_Start;
303 
304  const float c1 = glm::dot( p, m_End_minus_start );
305 
306  if( c1 < FLT_EPSILON )
307  return glm::dot( p, p );
308 
309  if( m_DOT_End_minus_start <= c1 )
310  {
311  p = aPoint - m_End;
312  }
313  else
314  {
315  const float b = c1 / m_DOT_End_minus_start;
316  const SFVEC2F pb = m_Start + m_End_minus_start * b;
317 
318  p = aPoint - pb;
319  }
320 
321  return glm::dot( p, p );
322 }
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: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 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 325 of file ray.cpp.

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

◆ IntersectSegment()

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

Definition at line 268 of file ray.cpp.

270 {
271  float rxs = m_End_minus_start.x * aEnd_minus_start.y - m_End_minus_start.y *
272  aEnd_minus_start.x;
273 
274  if( std::abs( rxs ) > glm::epsilon<float>() )
275  {
276  const float inv_rxs = 1.0f / rxs;
277 
278  const SFVEC2F pq = aStart - m_Start;
279 
280  const float t = ( pq.x * aEnd_minus_start.y - pq.y * aEnd_minus_start.x ) * inv_rxs;
281 
282  if( ( t < 0.0f ) || ( t > 1.0f ) )
283  return false;
284 
285  float u = ( pq.x * m_End_minus_start.y - pq.y * m_End_minus_start.x ) * inv_rxs;
286 
287  if( ( u < 0.0f ) || ( u > 1.0f ) )
288  return false;
289 
290  *aOutT = t;
291 
292  return true;
293  }
294 
295  return false;
296 }
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42
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 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 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 BBOX_2D::Intersect(), and RAYSEG2D().

◆ m_Length

◆ m_Start


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