AnimatLab  2
Test
OsgAnimatSim/OsgLinearPath.h
1 #pragma once
2 
3 #include <osg/Referenced>
4 
5 namespace OsgAnimatSim
6 {
7  namespace Visualization
8  {
9 
10  class OsgLinearPath;
11  class OsgScriptedSimulationWindow;
12 
13 class ANIMAT_OSG_PORT ControlPoint : public osg::Referenced, public AnimatSim::AnimatBase
14 {
15 public:
16  // constructor
17  ControlPoint(const double t, const osg::Vec3d pos, const osg::Vec3d v)
18  : AnimatBase(),
19  m_Pos(pos),
20  m_vDist(0, 0, 0),
21  m_dblDist(0),
22  m_V(v),
23  m_T(t),
24  m_Uv(osg::Vec3d(0.0, 0.0, 0.0)),
25  m_IsDirty(true),
26  m_lpParentSpline(NULL),
27  m_Tnext(0)
28  {}
29 
30  ControlPoint()
31  : AnimatBase(),
32  m_Pos(0, 0, 0),
33  m_vDist(0, 0, 0),
34  m_dblDist(0),
35  m_V(0, 0, 0),
36  m_T(0),
37  m_Uv(osg::Vec3d(0.0, 0.0, 0.0)),
38  m_IsDirty(true),
39  m_lpParentSpline(NULL),
40  m_Tnext(0)
41  {}
42 
43 protected:
44  // destructor
45  ~ControlPoint()
46  {}
47 
48 public:
49  // position of the point
50  osg::Vec3d m_Pos;
51  //report position
52  osg::Vec3d m_ReportPos;
53  // Distance vector for this section of the path
54  osg::Vec3d m_vDist;
55  //Linear distance for this section of the path
56  double m_dblDist;
57  // Velocity for this section of the path
58  osg::Vec3d m_V;
59  // unit vector from this waypoint to next.
60  osg::Vec3d m_Uv;
61  // time at the position
62  double m_T;
63  // time at next waypoint.
64  double m_Tnext;
65  // flag if the transformed position is dirty
66  bool m_IsDirty;
67 
68  OsgLinearPath *m_lpParentSpline;
69 
70  virtual osg::Vec3d Position();
71  virtual void Position(CStdFPoint &oPoint, bool bUseScaling = true);
72  virtual void Position(float fltX, float fltY, float fltZ, bool bUseScaling = true);
73  virtual void Position(std::string strXml, bool bUseScaling = true);
74 
75  virtual double Time();
76  virtual void Time(double dblVal);
77 
78  virtual OsgLinearPath *ParentSpline() {return m_lpParentSpline;};
79  virtual void ParentSpline(OsgLinearPath *lpParentSpline) {m_lpParentSpline = lpParentSpline;};
80 
81  virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError = true);
82  virtual void QueryProperties(CStdPtrArray<TypeProperty> &aryProperties);
83  virtual void Load(StdUtils::CStdXml &oXml);
84 };
85 
86 
87 class ANIMAT_OSG_PORT OsgLinearPath : public osg::Referenced, public AnimatSim::AnimatBase
88 {
89 
90 public:
91 
92  class ANIMAT_OSG_PORT ControlPointTransformFunctor
93  {
94  public:
95 
96  // destructor
98  // constructor
100 
101  public:
102 
103  // transform a control point position to user frame position
104  virtual osg::Vec3d Transform(const osg::Vec3d p) { return p; }
105  };
106 
107 
108 public:
109 
110  // list of control points
111  typedef std::vector<osg::ref_ptr<ControlPoint> > PointListType;
112 
113 public:
114  // constructor
115  OsgLinearPath();
116  // destructor
117  ~OsgLinearPath();
118 
119 public:
120 
121  // Control Point methods
122 
123  // add a control point
124  bool AddControlPoint(const osg::Vec3d point, const double time);
125 
126  // add a control point
127  bool AddControlPoint(ControlPoint *p);
128 
129  // clear all control points
130  void ClearAllControlPoints();
131 
132  // return the number of control points in the curve
133  unsigned int GetNumberOfControlPoints() { return m_ControlPoints.size(); }
134 
135  // return control point by index
136  ControlPoint* GetControlPoint(const unsigned int i) { return m_ControlPoints[i].get(); }
137 
138  // Method to create and retrieve output curve
139 
141  virtual osg::Node *CreateTestGeom(const bool showControlPoints = false,
142  const unsigned int numSamples = 3,
143  ControlPointTransformFunctor* xform = NULL);
144 
146  virtual bool Interpolate(const double t);
147 
148  // return last interpolated position (dbu)
149  osg::Vec3d &GetInterpPosition() { return m_Interpolated; }
150 
151  osg::Vec3d GetPositionAtTime(const double t);
152 
153  virtual CStdColor *LineColor();
154  virtual void LineColor(CStdColor &aryColor);
155  virtual void LineColor(float *aryColor);
156  virtual void LineColor(std::string strXml);
157 
158  virtual std::string PartID();
159  virtual void PartID(std::string strID);
160 
161  virtual double StartTime();
162  virtual void StartTime(double dblTime, bool bSortPaths = true);
163 
164  virtual double EndTime();
165  virtual void EndTime(double dblTime, bool bSortPaths = true);
166 
167  virtual bool Visible();
168  virtual void Visible(bool bVal);
169 
170  virtual bool VisibleInSim();
171  virtual void VisibleInSim(bool bVal);
172 
173  virtual bool ShowWaypoints();
174  virtual void ShowWaypoints(bool bVal);
175 
176  virtual BodyPart *TrackBody() {return m_lpTrackBody;};
177 
178  virtual void SimStarting();
179  virtual void ResetSimulation();
180 
181  virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError = true);
182  virtual void QueryProperties(CStdPtrArray<TypeProperty> &aryProperties);
183  virtual bool AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError = true, bool bDoNotInit = false);
184  virtual bool RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError = true);
185  virtual void Initialize();
186  virtual void Load(StdUtils::CStdXml &oXml);
187 
188  virtual void RedrawCurve();
189 
190  virtual bool BeforePathTime(double dblTime);
191  virtual bool WithinPathTime(double dblTime);
192  virtual bool AfterPathTime(double dblTime);
193 
194  virtual OsgScriptedSimulationWindow *ParentWindow() {return m_lpParentWindow;};
195  virtual void ParentWindow(OsgScriptedSimulationWindow *lpWnd) {m_lpParentWindow = lpWnd;};
196 
197 protected:
198 
200  void InvalidateCurve();
203  bool BuildCurve();
204 
205  void RemoveCurve();
206 
207  void FindCurrentWaypoint(const double t);
208 
209  osg::ref_ptr<ControlPoint> LoadWaypoint(CStdXml &oXml);
210 
211  virtual void AddWaypoint(std::string strXml);
212  virtual void RemoveWaypoint(std::string strID, bool bThrowError = true);
213  virtual int FindWaypointPos(std::string strID, bool bThrowError = true);
214  virtual void MakeVisible(bool bVal);
215 
216 protected:
217 
218  // if true, curve holds valid points
219  bool m_CurveIsValid;
220 
221  // list of 3D control points sorted by time
222  PointListType m_ControlPoints;
223 
224  // last interpolated position (dbu)
225  osg::Vec3d m_Interpolated;
226 
227  //ID of the part to look at while this path is playing out.
228  std::string m_strPartID;
229 
230  //Part to look at during camera path playback
231  BodyPart *m_lpTrackBody;
232 
233  //Start time for this path.
234  double m_dblStartTime;
235 
236  //End time for this path
237  double m_dblEndTime;
238 
239  //Line color to use
240  CStdColor m_vLineColor;
241 
242  //Turns visibility of line on/off while not running.
243  bool m_bVisible;
244 
245  //Turns visibility of line on/off while running simulation
246  bool m_bVisibleInSim;
247 
248  //Tells if we should show the waypoints or not.
249  bool m_bShowWaypoints;
250 
251  //The osg spline node
252  osg::ref_ptr<osg::Node> m_osgSpline;
253 
254  OsgScriptedSimulationWindow *m_lpParentWindow;
255 
256  //the current waypoint being evaluated
257  ControlPoint *m_lpCurrentWaypoint;
258 
259  //index of the current waypoint being evaluated.
260  int m_iCurrentWaypointIdx;
261 };
262 
263 
264  } // Visualization
265 //} //OsgAnimatSim
266 
267 }
268 
269 
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
A standard xml manipulation class.
Definition: StdXml.h:19
Animat base class.
Definition: AnimatBase.h:36