OpenFrames
ReferenceFrame.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_REFERENCEFRAME_
22 #define _OF_REFERENCEFRAME_
23 
24 #include <OpenFrames/Export.h>
26 #include <OpenFrames/Vector.hpp>
27 
28 #include <osg/Geode>
29 #include <osg/LightSource>
30 #include <osgText/Text>
31 #include <osg/Referenced>
32 #include <osg/ref_ptr>
33 
34 #include <vector>
35 #include <string>
36 
40 namespace OpenFrames
41 {
42 
43 class FrameTracker;
44 class Trajectory;
45 
46  /*
47  * \class ReferenceFrame.
48  *
49  * \brief Defines the standard functions of a classical reference frame.
50  *
51  * A reference frame can only contain other reference frames,
52  * so all objects should be derived from this class.
53  */
54  class OF_EXPORT ReferenceFrame : public osg::Referenced
55  {
56  public:
57  typedef std::vector<ReferenceFrame*> ParentList;
58  typedef std::vector<osg::ref_ptr<ReferenceFrame> > ChildList;
59  typedef std::vector<FrameTracker*> TrackerList;
61  /*
62  * \brief Construct a new ReferenceFrame.
63  *
64  * The color of this frame will be white and 90% opaque.
65  *
66  * \param name Name of the frame.
67  */
68  ReferenceFrame(const std::string &name);
69 
70  /*
71  * \brief Construct a new ReferenceFrame.
72  *
73  * The color of this frame is specified by the constructor arguments and is 90% opaque.
74  *
75  * \param name Name of the frame.
76  * \param color Vector of the colors [red, green, blue].
77  */
78  ReferenceFrame(const std::string &name, const osg::Vec3 &color);
79 
80  /*
81  * \brief Construct a new ReferenceFrame.
82  *
83  * The color of this frame is specified by the constructor arguments.
84  *
85  * \param name Name of the frame.
86  * \param color Vector of the colors [red, green, blue, alpha].
87  */
88  ReferenceFrame(const std::string &name, const osg::Vec4 &color);
89 
90  /*
91  * \brief Construct a new ReferenceFrame.
92  *
93  * The color of this frame is specified by the constructor arguments.
94  *
95  * \param name Name of the frame.
96  * \param r Red color component [0-1].
97  * \param g Green color component [0-1].
98  * \param b Blue color component [0-1].
99  * \param a Alpha (transparancy) component [0-1].
100  */
101  ReferenceFrame( const std::string &name , float r, float g, float b, float a = 1.0 );
102 
103  /*
104  * \brief Set the name of the frame that will be displayed.
105  *
106  * \param name Name of the frame.
107  */
108  void setName( const std::string &name );
109 
110  /*
111  * \brief Get the name of the frame.
112  *
113  * \return Name of the frame.
114  */
115  inline const std::string& getName() const { return _name; }
116 
117  /*
118  * \brief Set the color of the frame's decorations (axes, name, ...).
119  *
120  * This method can be overridden by derived classes.
121  *
122  * \param color Vector of color components [0-1] [red, green, blue, alpha].
123  */
124  virtual void setColor( const osg::Vec4 &color );
125 
126  /*
127  * \brief Set the color of the frame's decorations (axes, name, ...).
128  *
129  * This method can be overridden by derived classes.
130  *
131  * \param r Red color component [0-1].
132  * \param g Green color component [0-1].
133  * \param b Blue color component [0-1].
134  * \param a Alpha (transparency) component [0-1].
135  */
136  virtual void setColor( float r, float g, float b, float a = 1.0 );
137 
138  /*
139  * \brief Get the color of the frame's decorations (axes, name, ...).
140  *
141  * This method can be overridden by derived classes.
142  *
143  * \return Vector of the colors [red, green, blue, alpha].
144  */
145  virtual const osg::Vec4& getColor() const;
146 
147  /*
148  * \brief Get the color of the frame's decorations (axes, name, ...).
149  *
150  * This method can be overridden by derived classes.
151  *
152  * \param r Returned red color component [0-1].
153  * \param g Returned green color component [0-1].
154  * \param b Returned blue color component [0-1].
155  * \param a Returned alpha (transparancy) component [0-1].
156  */
157  virtual void getColor( float &r, float &g, float &b, float &a ) const;
158 
159  /*
160  * \brief Get the transform corresponding to this ReferenceFrame.
161  *
162  * \return The transform.
163  */
164  inline FrameTransform* getTransform() const {return _xform.get();}
165 
166  /*
167  * \brief Get the group corresponding to this ReferenceFrame.
168  *
169  * By default, the frame's group is the same as its transform.
170  * However, subclasses can define a separate group if they wish to.
171  * A child frame's group is what is added to a parent frame's
172  * transform in addChild().
173  *
174  * \return The FrameTransform.
175  */
176  virtual osg::Group* getGroup() const;
177 
178  /*
179  * \brief Set the position of this frame.
180  *
181  * This only applies if the frame is not being auto positioned by a TrajectoryFollower.
182  *
183  * \param x X position.
184  * \param y Y position.
185  * \param z Z position.
186  */
187  inline void setPosition( const double &x, const double &y, const double &z )
188  { _xform->setPosition(x, y, z); }
189 
190  inline void setPosition( const osg::Vec3d &pos )
191  { _xform->setPosition(pos); }
192 
193  /*
194  * \brief Get the position of this frame.
195  *
196  * \param x Returned X position.
197  * \param y Returned Y position.
198  * \param z Returned Z position.
199  */
200  inline void getPosition( double &x, double &y, double &z ) const
201  { _xform->getPosition(x, y, z); }
202 
203  inline void getPosition( osg::Vec3d &pos ) const
204  { _xform->getPosition(pos); }
205 
206  /*
207  * \brief Set the orientation of this frame.
208  *
209  * This only applies if the frame is not being auto positioned by a TrajectoryFollower.
210  *
211  * \param rx X component of the rotation quaternion.
212  * \param ry Y component of the rotation quaternion.
213  * \param rz Z component of the rotation quaternion.
214  * \param angle Angle component of the rotation quaternion.
215  */
216  inline void setAttitude( const double &rx, const double &ry, const double &rz, const double &angle )
217  { _xform->setAttitude(rx, ry, rz, angle); }
218 
219  inline void setAttitude( const osg::Quat &att )
220  { _xform->setAttitude(att); }
221 
222  /*
223  * \brief Get the orientation of this frame.
224  *
225  * \param rx Returned X component of the rotation quaternion.
226  * \param ry Returned Y component of the rotation quaternion.
227  * \param rz Returned Z component of the rotation quaternion.
228  * \param angle Returned angle component of the rotation quaternion.
229  */
230  inline void getAttitude( double &rx, double &ry, double &rz, double &angle) const
231  {
232  _xform->getAttitude(rx, ry, rz, angle);
233  }
234 
235  inline void getAttitude( osg::Quat &att ) const
236  { _xform->getAttitude(att); }
237 
238  /*
239  * \brief Get the BoundingSphere encompassing this frame plus all of its decorations.
240  *
241  * Derived classes should override this method and compute their own local BoundingSphere.
242  *
243  * \return The BoundingSphere
244  */
245  virtual const osg::BoundingSphere& getBound() const;
246 
247  enum AxesType
248  {
249  NO_AXES = 0,
250  X_AXIS = 1,
251  Y_AXIS = 2,
252  Z_AXIS = 4
253  };
254 
255  // Show/hide the x, y, z axes vectors and labels; see AxesType
256 
257  /*
258  * \brief Show/hide the x, y, z axes vectors.
259  *
260  * \param axes AxesType indicating which axes are to be shown.
261  */
262  virtual void showAxes(unsigned int axes);
263 
264  /*
265  * \brief Select which axis labels are to be displayed.
266  *
267  * \param labels AxesType indicating which axes labels are to be shown.
268  */
269  virtual void showAxesLabels(unsigned int labels);
270 
271  /*
272  * \brief Show/hide axis name labels.
273  *
274  * \param show True if labels are to be shown.
275  */
276  virtual void showNameLabel(bool namelabel);
277 
278  // Show/hide this frame's contents, e.g. everything a frame shows (excluding axes, labels, and children).
279  // Derived classes should override this.
280  virtual void showContents(bool showContents) {}
281  virtual bool getContentsShown() const { return true; }
282 
283  /*
284  * \brief Place x axis vectors at the given location with given length.
285  *
286  * \param base Position of the base of the axis vector.
287  * \param len Length of the drawn axis vector.
288  * \param headRatio Ratio of the size of the axis vector head compared to the base.
289  * \param bodyRadius Radius of the body of the drawn axis.
290  * \param headRadius Radius of the head of the drawn axis.
291  */
292  void moveXAxis(osg::Vec3d base, double len, double headRatio = 0.3, double bodyRadius = 0.0, double headRadius = 0.0) const;
293 
294  /*
295  * Place y axis vectors at the given location with given length.
296  *
297  * \param base Position of the base of the axis vector.
298  * \param len Length of the drawn axis vector.
299  * \param headRatio Ratio of the size of the axis vector head compared to the base.
300  * \param bodyRadius Radius of the body of the drawn axis.
301  * \param headRadius Radius of the head of the drawn axis.
302  */
303  void moveYAxis(osg::Vec3d base, double len, double headRatio = 0.3, double bodyRadius = 0.0, double headRadius = 0.0) const;
304 
305  /*
306  * Place z axis vectors at the given location with given length.
307  *
308  * \param base Position of the base of the axis vector.
309  * \param len Length of the drawn axis vector.
310  * \param headRatio Ratio of the size of the axis vector head compared to the base.
311  * \param bodyRadius Radius of the body of the drawn axis.
312  * \param headRadius Radius of the head of the drawn axis.
313  */
314  void moveZAxis(osg::Vec3d base, double len, double headRatio = 0.3, double bodyRadius = 0.0, double headRadius = 0.0) const;
315 
316  /*
317  * Set the text displayed for the x-axis label.
318  *
319  * The default axis label is 'X'.
320  *
321  * \param str String to set as the axis label.
322  */
323  inline void setXLabel(const std::string &str) { _xLabel->setText(str); }
324 
325  /*
326  * Set the text displayed for the y-axis label.
327  *
328  * The default axis label is 'Y'.
329  *
330  * \param str String to set as the axis label.
331  */
332  inline void setYLabel(const std::string &str) { _yLabel->setText(str); }
333 
334  /*
335  * Set the text displayed for the z-axis label.
336  *
337  * The default axis label is 'Z'.
338  *
339  * \param str String to set as the axis label.
340  */
341  inline void setZLabel(const std::string &str) { _zLabel->setText(str); }
342 
343  /*
344  * \brief Add a ReferenceFrame as a child to this one.
345  *
346  * This effectively adds the osg structure of that frame as a child to this frame's transform.
347  *
348  * \param child Child to add.
349  *
350  * \return True if successful, false otherwise.
351  */
352  bool addChild(ReferenceFrame* frame);
353 
354  /*
355  * \brief Remove a ReferenceFrame from the children of this one.
356  *
357  * This effectively removes the osg structure of that frame from this frame's transform.
358  *
359  * \param child Child to remove.
360  *
361  * \return True if successful, false otherwise.
362  */
363  bool removeChild( ReferenceFrame* frame );
364 
365  /*
366  * \brief Set whether this frame's light source is enabled.
367  * A light source will be created as needed.
368  * A ReferenceFrame's light source is disabled by default.
369  *
370  * \param enable Whether to enable/disable light source.
371  */
372  void setLightSourceEnabled(bool enable);
373 
374  /*
375  * \brief Check whether this frame's light source is enabled.
376  */
377  bool getLightSourceEnabled() const;
378 
379  /*
380  * \brief Get this frame's light source.
381  *
382  * \return The osg::LightSource, or NULL if it doesn't exist.
383  */
384  osg::LightSource* getLightSource() const;
385 
386  /*
387  * \brief Get the number of children.
388  *
389  * \return The number of children.
390  */
391  inline int getNumChildren() { return _children.size(); }
392 
393  /*
394  * \brief Get a child by its index.
395  *
396  * \param i Index of the child to get.
397  *
398  * \return The child at the index.
399  */
400  inline ReferenceFrame* getChild( int i ) { return _children[i].get(); }
401 
402  /*
403  * \brief Create a formatted string containing names of all child frames.
404  *
405  * \param str Formatted string.
406  * \param prefix Prefix to display in front of child objects.
407  */
408  void createFrameString( std::string& str, std::string prefix = " " ) const;
409 
410  /*
411  * \brief Information about this ReferenceFrame that is included in its
412  * formatted name during a createFrameString() call.
413  *
414  * \return Frame info.
415  */
416  virtual std::string frameInfo() const;
417 
418  /*
419  * \brief Add a parent for this frame.
420  *
421  * This is called automatically by addChild, so should not be called manually.
422  *
423  * \param frame Parent to add.
424  */
425  void addParent( ReferenceFrame* frame );
426 
427  /*
428  * \brief Remove a parent for this frame, if it exists.
429  *
430  * \param frame Parent to remove.
431  */
432  void removeParent( ReferenceFrame* frame );
433 
434  /*
435  * \brief Get the number of parents
436  *
437  * \return The number of parents.
438  */
439  inline int getNumParents() const { return _parents.size(); }
440 
441  /*
442  * \brief Get a parent by its index
443  *
444  * \param i Index of the parent to get
445  *
446  * \return The parent at the index.
447  */
448  inline ReferenceFrame* getParent( int i ) { return _parents[i]; }
449 
450  /*
451  * \brief Add a tracker for this frame.
452  *
453  * \param t Tracker to add.
454  */
455  void addTracker( FrameTracker* t );
456 
457  /*
458  * \brief Remove a tracker for this frame, if it exists.
459  *
460  * \param t Tracker to remove.
461  */
462  void removeTracker( FrameTracker* t );
463 
464  /*
465  * \brief Get the number of trackers.
466  *
467  * \return The number of trackers.
468  */
469  inline int getNumTrackers() const { return _trackers.size(); }
470 
471  /*
472  * \brief Get a tracker by its index.
473  *
474  * \param i Index of the tracker to get.
475  *
476  * \return The tracker at the index.
477  */
478  inline FrameTracker* getTracker( int i ) { return _trackers[i]; }
479 
480  /*
481  * \brief Find the index of the requested child.
482  *
483  * \param frame Child to find the index
484  *
485  * \return Index of the requested child.
486  * If the requested child does not exist, return -1.
487  */
488  int getChildIndex( const ReferenceFrame* frame ) const;
489 
490  /*
491  * \brief Find the index of the requested parent
492  *
493  * \param frame Parent to find the index
494  *
495  * \return Index of the requested parent.
496  * If the requested parent does not exist, return -1.
497  */
498  int getParentIndex( const ReferenceFrame* frame ) const;
499 
500  /*
501  * \brief Find the index of the requested tracker.
502  *
503  * \param frame Tracker to find the index.
504  *
505  * \return Index of the requested tracker.
506  * If the requested tracker does not exist, return -1.
507  */
508  int getTrackerIndex( const FrameTracker* t ) const;
509 
510  protected:
511  virtual ~ReferenceFrame(); // Must be allocated on heap using 'new'
512 
513  std::string _name;
514  mutable osg::ref_ptr<Vector> _xAxis;
515  mutable osg::ref_ptr<Vector> _yAxis;
516  mutable osg::ref_ptr<Vector> _zAxis;
517  mutable osg::ref_ptr<osgText::Text> _xLabel;
518  mutable osg::ref_ptr<osgText::Text> _yLabel;
519  mutable osg::ref_ptr<osgText::Text> _zLabel;
520  mutable osg::ref_ptr<osgText::Text> _nameLabel;
521  osg::ref_ptr<osg::Geode> _axes;
522  osg::ref_ptr<osg::Geode> _labels;
523 
524  mutable osg::BoundingSphere _bound;
525 
526  osg::ref_ptr<FrameTransform> _xform;
527 
528  private:
529  void _init( const std::string &n, const osg::Vec4& c );
530 
531  ParentList _parents;
532  ChildList _children;
533  TrackerList _trackers;
534  };
535 
536 } // !namespace OpenFrames
537 
538 #endif // !define _OF_REFERENCEFRAME_
osg::ref_ptr< osg::Geode > _axes
x,y,z axes together
Definition: ReferenceFrame.hpp:521
osg::BoundingSphere _bound
Frame&#39;s bounding sphere.
Definition: ReferenceFrame.hpp:524
osg::ref_ptr< osgText::Text > _zLabel
Z-Axes label.
Definition: ReferenceFrame.hpp:519
AxesType
Definition: ReferenceFrame.hpp:247
osg::ref_ptr< osgText::Text > _xLabel
X-Axes label.
Definition: ReferenceFrame.hpp:517
Definition: CoordinateAxes.hpp:29
std::vector< ReferenceFrame * > ParentList
Definition: ReferenceFrame.hpp:57
osg::ref_ptr< Vector > _xAxis
Vector of frame&#39;s x-axis.
Definition: ReferenceFrame.hpp:514
osg::ref_ptr< osgText::Text > _nameLabel
Name of reference frame that is displayed.
Definition: ReferenceFrame.hpp:520
std::vector< FrameTracker * > TrackerList
Definition: ReferenceFrame.hpp:59
This class transforms from local to world coordinates or vice versa.
Definition: FrameTransform.hpp:37
std::vector< osg::ref_ptr< ReferenceFrame > > ChildList
Definition: ReferenceFrame.hpp:58
Definition: ReferenceFrame.hpp:54
osg::ref_ptr< osg::Geode > _labels
axes and name labels
Definition: ReferenceFrame.hpp:522
osg::ref_ptr< osgText::Text > _yLabel
Y-Axes label.
Definition: ReferenceFrame.hpp:518
std::string _name
Name of reference frame.
Definition: ReferenceFrame.hpp:513
osg::ref_ptr< Vector > _zAxis
Vector of frame&#39;s z-axis.
Definition: ReferenceFrame.hpp:516
osg::ref_ptr< FrameTransform > _xform
The transform that all contained objects will undergo.
Definition: ReferenceFrame.hpp:526
osg::ref_ptr< Vector > _yAxis
Vector of frame&#39;s y-axis.
Definition: ReferenceFrame.hpp:515