AGX Dynamics  2.35.0.1
Spline.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 #pragma once
17 
18 
19 // Do not parse this file if we are running it through swig for the java bindings
20 #if (defined(__cplusplus) && !defined(SWIG)) || (defined(SWIG) && !defined(SWIGJAVA))
21 
22 #include <agx/agx.h>
23 #include <agx/agxPhysics_export.h>
24 #include <agx/Referenced.h>
25 #include <agxStream/Serializable.h>
26 #include <agx/Vec3.h>
27 #include <agx/AffineMatrix4x4.h>
28 
29 namespace agxUtil {
30 
34  class AGXPHYSICS_EXPORT Spline : public agx::Referenced, public virtual agxStream::Serializable
35  {
39  public:
40  class Tangent : public agx::Vec3
41  {
42  public:
43  Tangent()
44  : agx::Vec3() {}
45  Tangent( const agx::Vec3& v )
46  : agx::Vec3( v ) {}
47  };
48 
52  class Point : public agx::Vec3
53  {
54  public:
55  AGXPHYSICS_EXPORT static const double DEFAULT_TENSION;
56  AGXPHYSICS_EXPORT static const agx::Real DEFAULT_STRETCH;
57  AGXPHYSICS_EXPORT static const agx::Real DEFAULT_CURVATURE;
58 
66  Point(const agx::Vec3& p=agx::Vec3(), agx::Real t= DEFAULT_TENSION, agx::Real stretch=DEFAULT_STRETCH, agx::Real curvature=DEFAULT_CURVATURE)
67  : agx::Vec3(p), m_tension(t), m_stretch(stretch), m_curvature(curvature) {}
68 
70  inline agx::Real getTension() const { return m_tension; }
71 
73  inline void setTension(agx::Real tension) { m_tension = tension; }
74 
76  inline agx::Real getStretch() const { return m_stretch; }
77 
79  inline agx::Real getCurvature() const { return m_curvature; }
80 
81  private:
82  agx::Real m_tension;
83  agx::Real m_stretch;
84  agx::Real m_curvature;
85  };
86 
87  typedef agx::Vector< Tangent > TangentVector;
88  typedef agx::Vector< Point > PointVector;
89 
90  public:
91 
93  Spline();
94 
102  void add(const agx::Vec3& point, agx::Real tension = Point::DEFAULT_TENSION, agx::Real stretch = Point::DEFAULT_STRETCH, agx::Real curvature = Point::DEFAULT_CURVATURE);
103 
108  virtual Point evaluate( size_t index, agx::Real t ) const = 0;
109 
113  inline size_t getNumPoints() const { return m_points.size(); }
114 
118  inline PointVector& getPoints() { return m_points; }
119 
120 #ifndef SWIG
124  inline const PointVector& getPoints() const { return m_points; }
125 #endif
126 
130  virtual Tangent getTangent(size_t /*index*/, agx::Real /*t*/) const { return Tangent(); }
131 
136  virtual inline agx::Real getTension(size_t index, agx::Real time) const;
137 
141  virtual void updateTangents() = 0;
142 
146  virtual void clear();
147 
149 
150  void store(agxStream::OutputArchive& out) const override;
151  void restore(agxStream::InputArchive& in) override;
152 
153  protected:
154  virtual ~Spline() {}
155 
156  protected:
157  PointVector m_points;
158  TangentVector m_tangents;
159  };
160 
161  typedef agx::ref_ptr< Spline > SplineRef;
162 
163 
164  inline agx::Real Spline::getTension(size_t index, agx::Real time) const
165  {
166  if (index + 1 == m_points.size())
167  return m_points.back().getTension();
168 
169  time = agx::clamp(time, agx::Real(0), agx::Real(1));
170  if (agx::equalsZero(time))
171  return m_points[index].getTension();
172  else if (agx::equivalent(time, agx::Real(1), agx::RealEpsilon))
173  return m_points[index + 1].getTension();
174  else
175  return (1 - time) * m_points[index].getTension() + time * m_points[index + 1].getTension();
176  }
177 
178 
183  class AGXPHYSICS_EXPORT CubicSpline : public Spline
184  {
185  public:
186 
187  // Default constructor
188  CubicSpline();
189 
193  virtual Point evaluate( size_t index, agx::Real t ) const override;
194 
198  void updateTangents() override;
199 
200  Tangent getTangent(size_t index, agx::Real t) const override;
201 
202  AGXSTREAM_DECLARE_SERIALIZABLE(agxUtil::CubicSpline);
203 
204 
205  protected:
206  virtual ~CubicSpline() {}
207 
208  private:
209  agx::AffineMatrix4x4 m_matrix;
210  };
211 
212  typedef agx::ref_ptr< CubicSpline > CubicSplineRef;
213 
218  class AGXPHYSICS_EXPORT CardinalSpline : public Spline
219  {
220  public:
221  CardinalSpline();
222 
226  virtual Point evaluate( size_t index, agx::Real t ) const override;
227 
231  void updateTangents() override;
232 
233  Tangent getTangent(size_t index, agx::Real t) const override;
234 
235  AGXSTREAM_DECLARE_SERIALIZABLE(agxUtil::CardinalSpline);
236 
237 
238  protected:
239  virtual ~CardinalSpline() {}
240  };
241 
242  typedef agx::ref_ptr< CardinalSpline > CardinalSplineRef;
243 
248  class AGXPHYSICS_EXPORT NonUniformCardinalSpline : public Spline
249  {
250  public:
251  NonUniformCardinalSpline();
252 
256  virtual Point evaluate(size_t index, agx::Real t) const override;
257 
261  void updateTangents() override;
262 
263  Tangent getTangent(size_t index, agx::Real t) const override;
264 
265  AGXSTREAM_DECLARE_SERIALIZABLE(agxUtil::NonUniformCardinalSpline);
266 
267 
268  protected:
269  virtual ~NonUniformCardinalSpline() {}
270  };
271 
272  typedef agx::ref_ptr< NonUniformCardinalSpline > NonUniformCardinalSplineRef;
273 
278  class AGXPHYSICS_EXPORT ParameterizedCatmullRomSpline : public Spline
279  {
280  public:
281  ParameterizedCatmullRomSpline(agx::Real alpha);
282 
286  virtual Point evaluate(size_t index, agx::Real t) const override;
287 
291  void updateTangents() override;
292 
293  Tangent getTangent(size_t index, agx::Real t) const override;
294 
295  AGXSTREAM_DECLARE_SERIALIZABLE(agxUtil::ParameterizedCatmullRomSpline);
296 
297 
298  protected:
299  virtual ~ParameterizedCatmullRomSpline() {}
300 
302  ParameterizedCatmullRomSpline();
303 
304  private:
305  agx::Real m_alpha;
306  };
307 
308  typedef agx::ref_ptr< ParameterizedCatmullRomSpline > ParameterizedCatmullRomSplineRef;
309 
313  class AGXPHYSICS_EXPORT BSpline : public Spline
314  {
315  public:
316  BSpline();
317 
321  virtual Point evaluate(size_t index, agx::Real t) const override;
322 
326  void updateTangents() override;
327 
328  Tangent getTangent(size_t index, agx::Real t) const override;
329 
330  AGXSTREAM_DECLARE_SERIALIZABLE(agxUtil::BSpline);
331 
332 
333  protected:
334  virtual ~BSpline() {}
335  };
336 
337  typedef agx::ref_ptr< BSpline > BSplineRef;
338 
342  class AGXPHYSICS_EXPORT CPCatmullRomBSpline : public Spline
343  {
344  public:
345  CPCatmullRomBSpline();
346 
350  virtual Point evaluate(size_t index, agx::Real t) const override;
351 
355  virtual void updateTangents() override;
356 
357  Tangent getTangent(size_t index, agx::Real t) const override;
358 
359  AGXSTREAM_DECLARE_SERIALIZABLE(agxUtil::CPCatmullRomBSpline);
360 
361 
362  protected:
363  virtual ~CPCatmullRomBSpline() {}
364  ParameterizedCatmullRomSplineRef m_catmull;
365  BSplineRef m_bspline;
366  };
367 
368  typedef agx::ref_ptr< CPCatmullRomBSpline > CPCatmullRomBSplineRef;
369 
373  class AGXPHYSICS_EXPORT HermiteSpline : public Spline
374  {
375  public:
376  HermiteSpline();
377 
381  virtual Point evaluate(size_t index, agx::Real t) const override;
382 
386  void updateTangents() override;
387 
388  Tangent getTangent(size_t index, agx::Real t) const override;
389 
390  AGXSTREAM_DECLARE_SERIALIZABLE(agxUtil::HermiteSpline);
391 
392 
393  protected:
394  virtual ~HermiteSpline() {}
395  ParameterizedCatmullRomSplineRef m_catmull;
396  BSplineRef m_bspline;
397  };
398 
399  typedef agx::ref_ptr< HermiteSpline > HermiteSplineRef;
400 }
401 #endif // SWIGJAVA
#define AGXSTREAM_DECLARE_ABSTRACT_SERIALIZABLE(T)
Use this in a pure abstract Serializable class to add the required methods Important: Use full namesp...
Definition: Serializable.h:221
#define AGXSTREAM_DECLARE_SERIALIZABLE(T)
Use this in a Serializable class to add the required methods Important: Use full namespace in the dec...
Definition: Serializable.h:207
#define AGXPHYSICS_EXPORT
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:52
Class for writing serialized data in binary format to a stream.
Definition: OutputArchive.h:57
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:45
Base class providing referencing counted objects.
Definition: Referenced.h:114
Templated vector class.
Definition: agx/Vector.h:53
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:30
InputRef< T > in(const char *name, T &obj)
Create an object with a name and a reference to the object that should be restored (usually a pointer...
Definition: InputArchive.h:400
OutputRef< Serializable > out(const char *name, const Serializable *obj)
Return an object that contain the name and the object that should be serialized to an archive.
AGXTERRAIN_EXPORT void clear()
The agxUtil namespace contain classes and methods for utility functionality.
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: agx/Vec3.h:36
T1 clamp(T1 v, T2 minimum, T3 maximum)
Definition: Math.h:318
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
AGXPHYSICS_EXPORT agx::Bool equivalent(const agx::AddedMassInteraction::Matrix6x6 &lhs, const agx::AddedMassInteraction::Matrix6x6 &rhs, agx::Real eps=agx::RealEpsilon)