Loading [MathJax]/extensions/TeX/AMSmath.js
Radium Engine  1.5.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
PolyLine.hpp
1 #pragma once
2 
3 #include <Core/Containers/VectorArray.hpp>
4 #include <Core/Geometry/DistanceQueries.hpp>
5 #include <Core/Math/LinearAlgebra.hpp> // cotan, saturate (from Math.hpp)
6 #include <Core/RaCore.hpp>
7 #include <Core/Types.hpp>
8 
9 namespace Ra {
10 namespace Core {
11 namespace Geometry {
14 class RA_CORE_API PolyLine
15 {
16 
17  public:
19  explicit PolyLine( const Vector3Array& pt );
20 
22  inline const Vector3Array& getPoints() const;
23 
25  inline void getSegment( uint segment, Vector3& aOut, Vector3& abOut ) const;
26 
28  inline const Vector3Array& getSegmentVectors() const;
29 
31  inline Aabb aabb() const;
32 
34  inline Scalar length() const;
35 
37  void setPoints( const Vector3Array& pt );
38 
40  Scalar squaredDistance( const Vector3& p ) const;
41 
43  Scalar distance( const Vector3& p ) const;
44 
47  Scalar projectOnSegment( const Vector3& p, uint segment ) const;
48 
50  uint getNearestSegment( const Vector3& p ) const;
51 
53  inline uint getSegmentIndex( Scalar t ) const;
54 
57  Scalar project( const Vector3& p ) const;
58 
61  Vector3 f( Scalar t ) const;
62 
63  protected:
65  void update();
66 
70  inline Scalar getLineParameter( uint segment, Scalar tSegment ) const;
71 
72  private:
73  // Stores the points Pi
74  Vector3Array m_pts;
75  // Stores the vectors (Pi+1 - Pi)
76  Vector3Array m_ptsDiff;
77  // Length from origin to point Pi+1.
78  std::vector<Scalar> m_lengths;
79 };
80 
81 const Vector3Array& PolyLine::getPoints() const {
82  return m_pts;
83 }
84 
85 Scalar PolyLine::length() const {
86  return m_lengths.back();
87 }
88 
89 Aabb PolyLine::aabb() const {
90  Aabb aabb;
91  for ( const auto& v : m_pts ) {
92  aabb.extend( v );
93  }
94  return aabb;
95 }
96 
97 Scalar PolyLine::getLineParameter( uint segment, Scalar tSegment ) const {
98  CORE_ASSERT( segment < m_ptsDiff.size(), "invalid segment index" );
99  const Scalar lprev = segment > 0 ? m_lengths[segment - 1] : 0;
100  const Scalar lSegment = m_lengths[segment] - lprev;
101  return ( ( lSegment * tSegment ) + lprev ) / length();
102 }
103 
104 void PolyLine::getSegment( uint segment, Vector3& aOut, Vector3& abOut ) const {
105  CORE_ASSERT( segment < m_ptsDiff.size(), "Invalid segment index." );
106  aOut = m_pts[segment];
107  abOut = m_ptsDiff[segment];
108 }
109 
110 const Vector3Array& PolyLine::getSegmentVectors() const {
111  return m_ptsDiff;
112 }
113 
114 uint PolyLine::getSegmentIndex( Scalar t ) const {
115  // This could be optimized by dichotomy
116  Scalar param = length() * Math::saturate( t );
117  uint i = 0;
118  while ( m_lengths[i] < param ) {
119  ++i;
120  }
121  return i;
122 }
123 
124 } // namespace Geometry
125 } // namespace Core
126 } // namespace Ra
void getSegment(uint segment, Vector3 &aOut, Vector3 &abOut) const
Get the ith segment AB as starting point A and vector AB.
Definition: PolyLine.hpp:104
Aabb aabb() const
Get the aabb of the polyline.
Definition: PolyLine.hpp:89
const Vector3Array & getPoints() const
Get the point vector.
Definition: PolyLine.hpp:81
uint getSegmentIndex(Scalar t) const
Returns the index of the segment to which t belons.
Definition: PolyLine.hpp:114
Scalar getLineParameter(uint segment, Scalar tSegment) const
Definition: PolyLine.hpp:97
Scalar length() const
Get the total length of the line .
Definition: PolyLine.hpp:85
const Vector3Array & getSegmentVectors() const
Get the segment vector ( Pi+1 - Pi)
Definition: PolyLine.hpp:110
constexpr T saturate(T v)
Clamps the value between 0 and 1.
Definition: Math.hpp:120
Definition: Cage.cpp:3