OpenFrames
Trajectory.hpp
Go to the documentation of this file.
1 /***********************************
2  Copyright 2018 Ravishankar Mathur
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 ***********************************/
16 
21 #ifndef _OF_TRAJECTORY_
22 #define _OF_TRAJECTORY_
23 
24 #include <OpenFrames/Export.h>
25 #include <osg/Referenced>
26 #include <OpenThreads/ReadWriteMutex>
27 #include <vector>
28 
29 namespace OpenFrames
30 {
31  class TrajectorySubscriber; // Forward-declare trajectory subscriber
32 
42  class OF_EXPORT Trajectory : public osg::Referenced
43  {
44  public:
47  typedef double DataType;
48  typedef std::vector<DataType> DataArray;
49  typedef std::vector<TrajectorySubscriber*> SubscriberArray;
50 
54  {
55  ZERO = 0, // Use 0 for the component's value
56  TIME, // Use the time value
57  POSOPT, // Use a position/optional value
58  ATTITUDE // Use an attitude value
59  };
60 
69  struct DataSource
70  {
71  DataSource()
72  {
73  _src = ZERO;
74  _element = _opt = 0;
75  _scale = 1.0;
76  }
77 
78  bool operator == (const DataSource &rhs)
79  {
80  if(_src == rhs._src && _element == rhs._element
81  && _opt == rhs._opt && _scale == rhs._scale) return true;
82  else return false;
83  }
84 
85  SourceType _src;
86  unsigned int _element;
87  unsigned int _opt;
88  double _scale;
89  };
90 
93  Trajectory(unsigned int dof = 3, unsigned int nopt = 0 );
94 
100  void reserveMemory(unsigned int numPoints, bool usePos = true, bool useAtt = true);
101 
103  inline const DataArray& getTimeList() const { return _time; }
104  inline const DataArray& getPosOptList() const { return _posopt; }
105  inline const DataArray& getAttList() const { return _att; }
106 
109  inline unsigned int getNumPos() const { return _numPos; }
110 
113  void setNumOptionals(unsigned int nopt);
114  inline unsigned int getNumOptionals() const { return _nopt; }
115 
118  inline unsigned int getNumAtt() const { return _numAtt; }
119 
122  void setDOF(unsigned int dof);
123  inline unsigned int getDOF() const { return _dof; }
124 
127  inline unsigned int getBase() const { return _base; }
128 
143  virtual int getTimeIndex( const DataType &t, int &index ) const;
144 
145  inline bool isEmpty() const { return _time.empty(); }
146  inline unsigned int getNumTimes() const { return _time.size(); }
147  virtual bool getTimeRange( DataType &begin, DataType &end ) const;
148 
156  double getTimeDistance(const DataType &t) const;
157 
159  virtual bool addTime( const DataType &t );
160 
163  virtual bool addPosition( const DataType &x, const DataType &y,
164  const DataType z = 0.0 );
165  virtual bool addPosition( const DataType* const pos );
166  virtual bool getPosition( unsigned int n, DataType &x, DataType &y ) const;
167  virtual bool getPosition( unsigned int n, DataType &x, DataType &y, DataType &z ) const;
168 
171  virtual bool addAttitude( const DataType &x, const DataType &y,
172  const DataType &z, const DataType &w );
173  virtual bool addAttitude( const DataType* const att );
174  virtual bool getAttitude( unsigned int n, DataType &x, DataType &y,
175  DataType &z, DataType &w ) const;
176 
180  virtual bool setOptional( unsigned int index, const DataType &x,
181  const DataType &y, const DataType z = 0.0 );
182  virtual bool setOptional( unsigned int index, const DataType* const opt );
183  virtual bool getOptional( unsigned int n, unsigned int index, DataType &x,
184  DataType &y ) const;
185  virtual bool getOptional( unsigned int n, unsigned int index, DataType &x,
186  DataType &y, DataType &z ) const;
187 
189  virtual void clear();
190 
197  virtual void getPoint(unsigned int i, const DataSource source[], DataType val[]) const;
198 
201  virtual bool verifyData(const DataSource source[]) const;
202 
206  virtual unsigned int getNumPoints(const DataSource source[]) const;
207 
210  virtual void addSubscriber(TrajectorySubscriber* subscriber) const;
211  virtual void removeSubscriber(TrajectorySubscriber* subscriber) const;
212  virtual void informSubscribers();
213  inline void autoInformSubscribers(bool autoinform) { _autoInformSubscribers = autoinform; }
214 
215  enum DataLockType
216  {
217  READ_LOCK,
218  WRITE_LOCK
219  };
220 
223  virtual void lockData(DataLockType lockType = READ_LOCK) const; // Block the data from being changed
224  virtual void unlockData(DataLockType lockType = READ_LOCK) const; // Allow the data to be changed
225 
226  protected:
227  virtual ~Trajectory();
228 
229  DataArray _time; // Times
230  DataArray _posopt; // Positions & optionals
231  DataArray _att; // Attitudes
232 
233  mutable SubscriberArray _subscribers; // Subscribers of this Trajectory
234  bool _autoInformSubscribers;
235 
236  unsigned int _nopt; // Number of optionals for each time.
237  unsigned int _base; // Number of data elements taken up by each
238  // position/optionals group. Each data element
239  // is of type DataType, so _base=_dof*(1+_nopt)
240 
241  unsigned int _numPos; // Current number of pos/opt groups
242  unsigned int _numAtt; // Current number of attitudes
243 
244  unsigned int _dof; // Degrees of freedom for position and optionals
245 
246  // Synchronization variables
247 #define USERWMUTEX
248 
249 #ifdef USERWMUTEX
250  mutable OpenThreads::ReadWriteMutex _readWriteMutex;
251 #else
252  mutable OpenThreads::Mutex _mutex;
253 #endif
254  };
255 
262  {
263  public:
266  virtual void dataCleared(Trajectory *traj) = 0;
267 
270  virtual void dataAdded(Trajectory *traj) = 0;
271  };
272 
273 } // !namespace OpenFrames
274 
275 #endif // !define _OF_TRAJECTORY_
Abstract base class that is informed of changes to Trajectory objects.
Definition: Trajectory.hpp:261
virtual void dataCleared(Trajectory *traj)=0
SourceType
Definition: Trajectory.hpp:53
double DataType
Definition: Trajectory.hpp:47
virtual void dataAdded(Trajectory *traj)=0
Definition: CoordinateAxes.hpp:29
Definition: Trajectory.hpp:69
Holds a collection of data vectors.
Definition: Trajectory.hpp:42
unsigned int getNumAtt() const
Definition: Trajectory.hpp:118
const DataArray & getTimeList() const
Definition: Trajectory.hpp:103
unsigned int getBase() const
Definition: Trajectory.hpp:127
unsigned int getNumPos() const
Definition: Trajectory.hpp:109