Radium Engine  1.5.20
Loading...
Searching...
No Matches
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
9namespace Ra {
10namespace Core {
11namespace Geometry {
14class 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
81const Vector3Array& PolyLine::getPoints() const {
82 return m_pts;
83}
84
85Scalar PolyLine::length() const {
86 return m_lengths.back();
87}
88
89Aabb PolyLine::aabb() const {
90 Aabb aabb;
91 for ( const auto& v : m_pts ) {
92 aabb.extend( v );
93 }
94 return aabb;
95}
96
97Scalar 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
104void 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
110const Vector3Array& PolyLine::getSegmentVectors() const {
111 return m_ptsDiff;
112}
113
114uint 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
T back(T... args)
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
@ Geometry
"Geometry" render objects are those loaded using Radium::IO and generated by GeometrySystem
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3