AGX Dynamics  2.35.0.1
LineSegmentIntersectionFinder.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGXCOLLIDE_LINESEGMENTINTERSECTIONFINDER_H
18 #define AGXCOLLIDE_LINESEGMENTINTERSECTIONFINDER_H
19 
20 
21 #include <agxCollide/agxCollide.h>
22 #include <agx/StackArray.h>
23 #include <agx/agxPhysics_export.h>
24 #include <agx/Interval.h>
25 #include <agx/Vec3.h>
26 
27 
28 namespace agxCollide
29 {
30 
31  namespace agxGeometryQueries {
32 
34 
41  bool AGXPHYSICS_EXPORT pointsAreIdentical(const agx::Vec3& point0, const agx::Vec3& point1);
42 
44 
45 
47  class Line {
48  public:
50  Line(const agx::Vec3& point, const agx::Vec3& dir);
51 
53  agx::Real projectPoint(const agx::Vec3& point) const;
54 
56  bool containsPoint(const agx::Vec3& point, const agx::Real epsilon = agx::RealEpsilon) const;
57 
59  bool isDegeneratedToPoint() const;
60 
62  agx::Interval projectLineSegment(const LineSegment& segment) const;
63 
65  const agx::Vec3& point() const;
66 
68  agx::Vec3 evaluateAt(const agx::Real lineParameter) const;
69 
71  const agx::Vec3& dir() const;
72 
74  agx::Real dirLength2() const;
75 
76  private:
77  agx::Vec3 m_point;
78  agx::Vec3 m_dir;
79  agx::Real m_dirLength2;
80  };
81 
84  public:
86  LineSegment(const agx::Vec3& point0, const agx::Vec3& point1);
87 
89  LineSegment();
90 
92  agx::Real projectPointToInfiniteLine(const agx::Vec3& point) const;
93 
95  bool containsPoint(const agx::Vec3& point, const agx::Real epsilon = agx::RealEpsilon) const;
96 
98  bool isDegeneratedToPoint() const;
99 
101  agxGeometryQueries::Line getLine() const;
102 
104  const agx::Vec3& point0() const;
105 
107  const agx::Vec3& point1() const;
108 
115  agx::Vec3 evaluateAt(const agx::Real lineParameter) const;
116 
118  const agx::Vec3& dir() const;
119 
121  agx::Real dirLength2() const;
122 
123  private:
124  agx::Vec3 m_point0;
125  agx::Vec3 m_point1;
126  agx::Vec3 m_dir;
127  agx::Real m_dirLength2;
128  };
129 
130 
136  {
137  public:
142 
148  SweptLineSegment(const LineSegment& start, const LineSegment& end);
149 
156  agx::Vec3 evaluateAt(const agx::Real time, const agx::Real lineParameter) const;
157 
163  LineSegment evaluateAt(const agx::Real time) const;
164 
166  LineSegment& segment0();
167 
169  const LineSegment& segment0() const;
170 
172  LineSegment& segment1();
173 
175  const LineSegment& segment1() const;
176 
178  bool isDegeneratedToPoint() const;
179 
181  bool isDegeneratedToLine() const;
182 
184  agx::Vec3 computeSweepDirectionPoint0() const;
185 
187  agx::Vec3 computeSweepDirectionPoint1() const;
188 
190  LineSegment computeSweepSegmentPoint0() const;
191 
193  LineSegment computeSweepSegmentPoint1() const;
194 
195  private:
196  LineSegment m_segment0;
197  LineSegment m_segment1;
198  };
199  }
200 
201 
203  {
204  public:
217  const agxGeometryQueries::SweptLineSegment& sweptSegment0,
218  const agxGeometryQueries::SweptLineSegment& sweptSegment1,
219  agx::Real& t,
220  agx::Interval& lineParameters0,
221  agx::Interval& lineParameters1,
223  agx::Vec3& normal,
224  agx::Real& distance
225  );
226  };
227 
228 
229 
230 
232 
233 
234  namespace agxGeometryQueries {
235  // Functions.
236  AGX_FORCE_INLINE bool pointsAreIdentical(const agx::Vec3& point0, const agx::Vec3& point1)
237  {
238  return (point0 - point1).length2() <= agxGeometryQueries::geometricEpsilon * agxGeometryQueries::geometricEpsilon;
239  }
240 
241 
242  // Class Line.
243  AGX_FORCE_INLINE agxGeometryQueries::Line::Line(const agx::Vec3& point, const agx::Vec3& dir) : m_point(point), m_dir(dir), m_dirLength2(dir*dir)
244  {
245  }
246 
247 
249  {
250  if (isDegeneratedToPoint())
251  return 0;
252  else
253  return (m_dir * (point - m_point)) / m_dirLength2;
254  }
255 
256 
258  {
259  const agx::Vec3 projection = m_point + m_dir * projectPoint(point);
260  return pointsAreIdentical(projection, point);
261  }
262 
263 
265  {
266  return agx::equalsZero(m_dirLength2, agx::RealEpsilon * agx::RealEpsilon);
267  }
268 
269 
271  {
272  return agx::Interval(projectPoint(segment.point0()), projectPoint(segment.point1()), false);
273  }
274 
275 
277  {
278  return m_point;
279  }
280 
281 
283  {
284  return m_point + m_dir * lineParameter;
285  }
286 
287 
289  {
290  return m_dir;
291  }
292 
293 
295  {
296  return m_dirLength2;
297  }
298 
299 
300  // Class LineSegment.
301  AGX_FORCE_INLINE agxGeometryQueries::LineSegment::LineSegment(const agx::Vec3& point0, const agx::Vec3& point1) : m_point0(point0), m_point1(point1),
302  m_dir(point1 - point0), m_dirLength2(m_dir * m_dir)
303  {
304  }
305 
306 
308  {
309  }
310 
311 
313  {
314  if (isDegeneratedToPoint())
315  return 0;
316  else
317  return (m_dir * (point - m_point0)) / m_dirLength2;
318  }
319 
320 
322  {
323  const agx::Real projection = projectPointToInfiniteLine(point);
324  if (projection < 0 || projection * projection > m_dirLength2)
325  return false;
326  const agx::Vec3 projectionPoint = m_point0 + m_dir * projection;
327  const agx::Vec3 dist = point - projectionPoint;
328  return dist.length2() < epsilon * epsilon;
329  }
330 
331 
333  {
334  return agx::equalsZero(m_dirLength2, agx::RealEpsilon * agx::RealEpsilon);
335  }
336 
337 
339  {
340  return agxGeometryQueries::Line(point0(), dir());
341  }
342 
343 
345  {
346  return m_point0;
347  }
348 
349 
351  {
352  return m_point1;
353  }
354 
355 
357  {
358  return m_dir;
359  }
360 
361 
363  {
364  return m_dirLength2;
365  }
366 
367 
369  {
370  return m_point0 + m_dir * lineParameter;
371  }
372 
373 
374  // Class SweptLineSegment.
376  {
377  }
378 
380  :m_segment0(start), m_segment1(end)
381  {
382  }
383 
384 
386  {
387  const agx::Vec3 point = m_segment0.evaluateAt(lineParameter) * (agx::Real(1.0) - time) + m_segment1.evaluateAt(lineParameter) * time;
388  return point;
389  }
390 
391 
393  {
394  const LineSegment segment(m_segment0.point0() * (agx::Real(1.0) - time) + m_segment1.point0() * time,
395  m_segment0.point1() * (agx::Real(1.0) - time) + m_segment1.point1() * time);
396  return segment;
397 
398  }
399 
400 
402  {
403  return m_segment0;
404  }
405 
406 
408  {
409  return m_segment0;
410  }
411 
412 
414  {
415  return m_segment1;
416  }
417 
418 
420  {
421  return m_segment1;
422  }
423 
424 
426  {
427  return m_segment0.isDegeneratedToPoint() && m_segment1.isDegeneratedToPoint() && agx::equivalent(m_segment0.point0(), m_segment1.point0());
428 
429  }
430 
431 
433  {
434  return m_segment0.isDegeneratedToPoint() && m_segment1.isDegeneratedToPoint();
435  }
436 
437 
439  {
440  return m_segment1.point0() - m_segment0.point0();
441  }
442 
443 
445  {
446  return m_segment1.point1() - m_segment0.point1();
447  }
448 
449 
451  {
452  return LineSegment(m_segment0.point0(), m_segment1.point0());
453  }
454 
455 
457  {
458  return LineSegment(m_segment0.point1(), m_segment1.point1());
459  }
460 
461 
462  }
463 }
464 
465 #endif // AGXCOLLIDE_LINESEGMENTINTERSECTIONFINDER_H
#define AGXPHYSICS_EXPORT
static bool calculateLineSegmentCollision(const agxGeometryQueries::SweptLineSegment &sweptSegment0, const agxGeometryQueries::SweptLineSegment &sweptSegment1, agx::Real &t, agx::Interval &lineParameters0, agx::Interval &lineParameters1, agx::StackArray< agx::Vec3, 2 > &points, agx::Vec3 &normal, agx::Real &distance)
A line segment. Note that the direction does not have to be normalized.
agx::Vec3 evaluateAt(const agx::Real lineParameter) const
Evaluates the line equation at the parameter.
agxGeometryQueries::Line getLine() const
Gets the line segment's line.
const agx::Vec3 & point1() const
Gets the line segment's second point.
bool isDegeneratedToPoint() const
Is the line segment degenerated to a point?
bool containsPoint(const agx::Vec3 &point, const agx::Real epsilon=agx::RealEpsilon) const
Does the line segment contain the point, given an epsilon threshold?
LineSegment()
Creates an empty line segment, with both end points at origin.
const agx::Vec3 & point0() const
Gets the line segment's first point.
const agx::Vec3 & dir() const
Gets the direction; not necessarily normalized.
agx::Real dirLength2() const
Gets the squared length of the line direction.
agx::Real projectPointToInfiniteLine(const agx::Vec3 &point) const
Computes the scalar value of the projection of a point along the segment's line.
An infinite line, defined by a starting point and a (not necessarily normalized) direction.
const agx::Vec3 & point() const
Gets the starting point of the line.
bool isDegeneratedToPoint() const
Is the line degenerated to a point?
agx::Interval projectLineSegment(const LineSegment &segment) const
Computes the interval of the projection of a line segment onto this line.
bool containsPoint(const agx::Vec3 &point, const agx::Real epsilon=agx::RealEpsilon) const
Does the line contain the point, given an epsilon threshold?
const agx::Vec3 & dir() const
Gets the direction; not necessarily normalized.
agx::Real projectPoint(const agx::Vec3 &point) const
Computes the scalar value of the projection of a point along the line.
agx::Vec3 evaluateAt(const agx::Real lineParameter) const
Note that the line dir does not have to be normalized.
Line(const agx::Vec3 &point, const agx::Vec3 &dir)
Note that the direction does not have to be normalized.
agx::Real dirLength2() const
Gets the squared length of the line direction.
A line segment that is swept in time from t=0 to t=1.
bool isDegeneratedToLine() const
Is the swept line segment degenerated to a line?
LineSegment computeSweepSegmentPoint0() const
Returns the line segment point0 over time.
bool isDegeneratedToPoint() const
Is the swept line segment degenerated to a point?
LineSegment computeSweepSegmentPoint1() const
Returns the line segment point1 over time.
SweptLineSegment()
Creates a SweptLineSegment, with both line segments degenaring to points at the origin.
agx::Vec3 evaluateAt(const agx::Real time, const agx::Real lineParameter) const
Evaluates the swept line at a given time and line parameter and returns a point.
agx::Vec3 computeSweepDirectionPoint1() const
Returns the non-normalized sweep direction of point1 over time.
LineSegment & segment0()
Gets the swept line segment's first line segment, i.e. at t=0.
agx::Vec3 computeSweepDirectionPoint0() const
Returns the non-normalized sweep direction of point0 over time.
LineSegment & segment1()
Gets the swept line segment's second line segment, i.e. at t=1.
Templated stack array class.
Real length2() const
Length squared of the vector = vec .
Definition: Vec3Template.h:695
#define AGX_FORCE_INLINE
Definition: macros.h:58
bool AGXPHYSICS_EXPORT pointsAreIdentical(const agx::Vec3 &point0, const agx::Vec3 &point1)
Tests if two points are identical given agxGeometryQueries::geometricEpsilon.
This namespace consists of a set of classes for handling geometric intersection tests including boole...
AGXCORE_EXPORT const Real RealEpsilon
AGXPHYSICS_EXPORT agx::Bool equalsZero(const agx::AddedMassInteraction::Matrix6x6 &matrix, agx::Real eps=agx::RealEpsilon)
double Real
Definition: Real.h:42
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
LineT< Vec3 > Line
Definition: agx/Line.h:39
AGXPHYSICS_EXPORT agx::Bool equivalent(const agx::AddedMassInteraction::Matrix6x6 &lhs, const agx::AddedMassInteraction::Matrix6x6 &rhs, agx::Real eps=agx::RealEpsilon)