Radium Engine  1.5.20
Loading...
Searching...
No Matches
Curve2D.hpp
1#pragma once
2
3#include <Core/Containers/VectorArray.hpp>
4#include <Core/Geometry/Spline.hpp>
5#include <Core/RaCore.hpp>
6#include <vector>
7
8namespace Ra {
9namespace Core {
10namespace Geometry {
11class Curve2D
12{
13 public:
14 enum CurveType { LINE, CUBICBEZIER, SPLINE, SIZE };
15
16 using Vector = Eigen::Matrix<Scalar, 2, 1>;
17
18 virtual void addPoint( const Vector p ) = 0;
19 virtual ~Curve2D() = default;
20
21 virtual Vector f( Scalar u ) const = 0;
22 virtual Vector df( Scalar u ) const = 0;
23 virtual Vector fdf( Scalar t, Vector& grad ) const = 0;
24
25 protected:
26 int size;
27};
28
29class QuadraSpline : public Curve2D
30{
31 public:
32 QuadraSpline() { this->size = 0; }
33 QuadraSpline( const Curve2D::Vector& p0,
34 const Curve2D::Vector& p1,
35 const Curve2D::Vector& p2 ) :
36 m_points { p0, p1, p2 } {
37 this->size = 3;
38 }
39 ~QuadraSpline() override = default;
40
41 inline void addPoint( const Vector p ) override;
42
43 inline Vector f( Scalar u ) const override;
44 inline Vector df( Scalar u ) const override;
45 inline Vector fdf( Scalar u, Vector& grad ) const override;
46
47 private:
48 Core::VectorArray<Vector> m_points;
49};
50
51class CubicBezier : public Curve2D
52{
53 public:
54 CubicBezier() { this->size = 0; }
55 CubicBezier( const Curve2D::Vector& p0,
56 const Curve2D::Vector& p1,
57 const Curve2D::Vector& p2,
58 const Curve2D::Vector& p3 ) :
59 m_points { p0, p1, p2, p3 } {
60 this->size = 4;
61 }
62 ~CubicBezier() override = default;
63
64 inline void addPoint( const Vector p ) override;
65
66 inline Vector f( Scalar u ) const override;
67 inline Vector df( Scalar u ) const override;
68 inline Vector fdf( Scalar t, Vector& grad ) const override;
69
70 private:
71 Vector m_points[4];
72};
73
74class Line : public Curve2D
75{
76 public:
77 Line() { this->size = 0; }
78 Line( const Vector& p0, const Vector& p1 ) : m_points { p0, p1 } { this->size = 2; }
79 ~Line() override = default;
80
81 inline void addPoint( const Vector p ) override;
82
83 inline Vector f( Scalar u ) const override;
84 inline Vector df( Scalar u ) const override;
85 inline Vector fdf( Scalar t, Vector& grad ) const override;
86
87 private:
88 Vector m_points[2];
89};
90
91class SplineCurve : public Curve2D
92{
93 public:
94 SplineCurve() { this->size = 0; }
95 explicit SplineCurve( Core::VectorArray<Vector> points ) : m_points( points ) {
96 this->size = points.size();
97 }
98 ~SplineCurve() override = default;
99
100 inline void addPoint( const Vector p ) override;
101
102 inline Vector f( Scalar u ) const override;
103 inline Vector df( Scalar u ) const override;
104 inline Vector fdf( Scalar t, Vector& grad ) const override;
105
106 private:
107 Core::VectorArray<Vector> m_points;
108};
109
110/*--------------------------------------------------*/
111
112void CubicBezier::addPoint( const Curve2D::Vector p ) {
113 if ( size < 4 ) { m_points[size++] = p; }
114}
115
116Curve2D::Vector CubicBezier::f( Scalar u ) const {
117 Vector grad;
118 return fdf( u, grad );
119}
120
121Curve2D::Vector CubicBezier::df( Scalar u ) const {
122 Vector grad;
123 fdf( u, grad );
124 return grad;
125}
126
127Curve2D::Vector CubicBezier::fdf( Scalar t, Vector& grad ) const {
128 float t2 = t * t;
129 float t3 = t2 * t;
130 float oneMinusT = 1.0 - t;
131 float oneMinusT2 = oneMinusT * oneMinusT;
132 float oneMinusT3 = oneMinusT2 * oneMinusT;
133
134 grad = 3.0 * oneMinusT2 * ( m_points[1] - m_points[0] ) +
135 6.0 * oneMinusT * t * ( m_points[2] - m_points[1] ) +
136 3.0 * t2 * ( m_points[3] - m_points[2] );
137 return oneMinusT3 * m_points[0] + 3.0 * oneMinusT2 * t * m_points[1] +
138 3.0 * oneMinusT * t2 * m_points[2] + t3 * m_points[3];
139}
140
141/*--------------------------------------------------*/
142
143void Line::addPoint( const Curve2D::Vector p ) {
144 if ( size < 2 ) { m_points[size++] = p; }
145}
146
147Curve2D::Vector Line::f( Scalar u ) const {
148 return ( 1.0 - u ) * m_points[0] + u * m_points[1];
149}
150
151Curve2D::Vector Line::df( Scalar /*u*/ ) const {
152 return m_points[1] - m_points[0];
153}
154
155Curve2D::Vector Line::fdf( Scalar t, Vector& grad ) const {
156 grad = m_points[1] - m_points[0];
157 return ( 1.0 - t ) * m_points[0] + t * m_points[1];
158}
159
160/*--------------------------------------------------*/
161
162void SplineCurve::addPoint( const Curve2D::Vector p ) {
163 m_points.push_back( p );
164 ++size;
165}
166
167Curve2D::Vector SplineCurve::f( Scalar u ) const {
168 Spline<2, 3> spline;
169 spline.setCtrlPoints( m_points );
170
171 return spline.f( u );
172}
173
174Curve2D::Vector SplineCurve::df( Scalar u ) const {
175 Spline<2, 3> spline;
176 spline.setCtrlPoints( m_points );
177
178 return spline.df( u );
179}
180
181Curve2D::Vector SplineCurve::fdf( Scalar u, Curve2D::Vector& grad ) const {
182 Spline<2, 3> spline;
183 spline.setCtrlPoints( m_points );
184
185 grad = spline.df( u );
186 return spline.f( u );
187}
188
189/*--------------------------------------------------*/
190
191void QuadraSpline::addPoint( const Curve2D::Vector p ) {
192 m_points.push_back( p );
193 ++size;
194}
195
196Curve2D::Vector QuadraSpline::f( Scalar u ) const {
197 Spline<2, 2> spline;
198 spline.setCtrlPoints( m_points );
199
200 return spline.f( u );
201}
202
203Curve2D::Vector QuadraSpline::df( Scalar u ) const {
204 Spline<2, 2> spline;
205 spline.setCtrlPoints( m_points );
206
207 return spline.df( u );
208}
209
210Curve2D::Vector QuadraSpline::fdf( Scalar u, Vector& grad ) const {
211 Spline<2, 2> spline;
212 spline.setCtrlPoints( m_points );
213
214 grad = spline.df( u );
215 return spline.f( u );
216}
217
218} // namespace Geometry
219} // namespace Core
220} // namespace Ra
LineMeshPtr Line(const Core::Vector3 &a, const Core::Vector3 &b, const Core::Utils::Color &color)
Displays given line.
@ 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