OpenFrames
TrajectoryFollower.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_TRAJECTORYFOLLOWER_
22 #define _OF_TRAJECTORYFOLLOWER_
23 
24 #include <OpenFrames/Export.h>
26 #include <osg/Callback>
27 #include <osg/ref_ptr>
28 #include <osg/observer_ptr>
29 #include <osg/Quat>
30 #include <osg/Vec3d>
31 #include <OpenThreads/Mutex>
32 
33 namespace OpenFrames
34 {
52  class OF_EXPORT TrajectoryFollower : public osg::Callback, public OpenFrames::TrajectorySubscriber
53  {
54  public:
60  enum FollowMode
61  {
62  LOOP = 0, // Loop around repeatedly
63  LIMIT // Limit the frame to the ends of the followed trajectory
64  };
65 
68  {
69  POSITION = 1,
70  ATTITUDE = 2
71  };
72 
73  TrajectoryFollower(Trajectory *traj = NULL);
74 
75  // Don't allow copying from another TrajectoryFollower
76  TrajectoryFollower(const TrajectoryFollower &tf, const osg::CopyOp &copyop) {}
77 
78  META_Object(OpenFrames, TrajectoryFollower);
79 
80  // Follow ONLY the specified trajectory, and unfollow any others
81  // More efficient than unfollowTrajectory(NULL) -> followTrajectory(traj)
82  // NULL input: stop following all trajectories (same as unfollowTrajectory(NULL))
83  void setTrajectory(Trajectory *traj);
84 
85  // Add trajectory to be followed
86  // Has no effect if trajectory is already being followed
87  void addTrajectory(Trajectory *traj);
88 
89  // Stop following a trajectory
90  // NULL input: stop following all trajectories
91  void removeTrajectory(Trajectory *traj);
92 
93  // Set how trajectories are followed
94  inline void setFollowType(unsigned int data, FollowMode mode)
95  {
96  _data = data;
97  _mode = mode;
98  }
99  inline void getFollowType(unsigned int &data, FollowMode &mode) const
100  {
101  data = _data;
102  mode = _mode;
103  }
104 
105  // Set source for each each position component
106  bool setXData(const Trajectory::DataSource &src);
107  bool setYData(const Trajectory::DataSource &src);
108  bool setZData(const Trajectory::DataSource &src);
109  const Trajectory::DataSource* getDataSource() const { return _dataSource; }
110 
111  // Set default sources for all position components
112  // This means standard X/Y/Z components for position
113  // If following multiple trajectories, then use info for
114  // first followed trajectory to determine data availability
115  void setDefaultData();
116  bool getUsingDefaultData() { return _usingDefaultData; }
117 
118  // Time managment functions allow for two types of times
119  // - Offset from the global simulation time (set by WindowProxy)
120  // - Custom simulation time
121  void setTime(double time); // Custom simulation time
122  void setOffsetTime(double offsetTime); // Offset from global simulation time
123  inline bool isFollowingTime() const
124  { return _followTime; } // True for global sim time, false for custom sim time
125 
126  double getLastTime() const { return _lastAdjustedTime; }
127  Trajectory* getLastTrajectory() const { return _follow.get(); }
128 
130  virtual bool run(osg::Object* object, osg::Object* data);
131 
134  virtual void dataCleared(Trajectory* traj) { _needsUpdate = true; }
135  virtual void dataAdded(Trajectory* traj) { _needsUpdate = true; }
136 
137  protected:
138  virtual ~TrajectoryFollower();
139 
140  // Compute adjusted time based on follow mode
141  double _computeTime(double time);
142 
143  // Choose trajectory to follow based on adjusted time
144  Trajectory* _chooseTrajectory(double time);
145 
146  // Update position & orientation based on adjusted time and chosen trajectory
147  bool _updateState(double time, FollowData data);
148 
149  // Check if all followed trajectories support necessary data sources
150  bool _verifyDataSources() const
151  {
152  // Data sources are valid if all followed trajectories support them
153  for(auto traj : _trajList)
154  {
155  if(!traj->verifyData(_dataSource)) return false;
156  }
157  return true;
158  }
159 
160  typedef std::vector<osg::ref_ptr<Trajectory> > TrajList;
161 
162  TrajList _trajList; // All followed trajectories
163  osg::observer_ptr<Trajectory> _follow; // Currently followed trajectory
164  FollowMode _mode; // Mode in which to follow trajectory
165  unsigned int _data; // Whether to follow position and/or attitude
166 
167  // Specifies which data to follow in the trajectory
168  Trajectory::DataSource _dataSource[3];
169  bool _dataValid; // Test if Trajectory supports needed data
170  bool _usingDefaultData; // Whether to use default data sources
171 
172  // Time control variables
173  bool _needsUpdate, _followTime;
174  double _timeVal; // Time value to use (offset if following time, constant otherwise)
175  double _lastSimTime; // Simulation time at most recent update
176  double _lastAdjustedTime;
177 
178  OpenThreads::Mutex _mutex; // For adding/removing followed trajectories
179 
180  private:
181  osg::Vec3d _v1, _v2; // Used for position interpolation
182  osg::Quat _a1, _a2; // Used for attitude interpolation
183  };
184 
185 } // !namespace OpenFrames
186 
187 #endif // !define _OF_TRAJECTORYFOLLOWER_
Abstract base class that is informed of changes to Trajectory objects.
Definition: Trajectory.hpp:261
virtual void dataAdded(Trajectory *traj)
Definition: TrajectoryFollower.hpp:135
Definition: CoordinateAxes.hpp:29
FollowMode
Definition: TrajectoryFollower.hpp:60
Holds a collection of data vectors.
Definition: Trajectory.hpp:42
virtual void dataCleared(Trajectory *traj)
Definition: TrajectoryFollower.hpp:134
FollowData
Definition: TrajectoryFollower.hpp:67
Updates FrameTransform objects from Trajectory objects.
Definition: TrajectoryFollower.hpp:52