OpenFrames
View.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_VIEW_
22 #define _OF_VIEW_
23 
24 #include <OpenFrames/Export.h>
26 #include <osg/Matrixd>
27 #include <osg/Referenced>
28 #include <osg/ref_ptr>
29 #include <osgGA/TrackballManipulator>
30 
31 namespace OpenFrames
32 {
33  class FollowingTrackball;
45  class OF_EXPORT View : public osg::Referenced
46  {
47  public:
50  {
51  ORTHOGRAPHIC=0, // Object size is independent of distance from viewer
52  PERSPECTIVE, // Objects get smaller as they get farther away
53  };
54 
59  {
60  // View frame initialized to the scene's global reference frame.
61  // This is equivalent to an "inertial-fixed" view that follows
62  // the body as it translates.
63  // If viewing in from-to mode, then the rotation starts in the
64  // scene's global reference frame.
65  ABSOLUTE_FRAME=0,
66 
67  // View frame initialized to the frame being viewed. This is
68  // equivalent to a "body-fixed" view.
69  // If viewing in from-to mode, then the rotation starts in the
70  // body-fixed reference frame.
71  RELATIVE_FRAME
72  };
73 
79  {
80  // Single direct rotation, using shortest angle
81  // This may result in upside-down view if the rotation angle
82  // is greater than 90 degrees out-of-plane
83  DIRECT=0,
84 
85  // First rotate along X-Y (azimuth), then up Z (elevation)
86  // This preserves "Z-up" for the view base frame
87  AZEL
88  };
89 
90  View();
91  View(ReferenceFrame *root, ReferenceFrame *viewFrame, ViewFrameType baseframe = RELATIVE_FRAME);
92  View(ReferenceFrame *root, ReferenceFrame *viewFrame, ReferenceFrame *lookatFrame, ViewFrameType frameType = RELATIVE_FRAME, ViewRotationType rotationType = AZEL);
93 
95  inline ProjectionType getProjectionType() { return _projType; }
96 
98  inline void setPerspective(const double fovy, const double ratio)
99  {
100  _projType = PERSPECTIVE;
101  _projection.makePerspective(fovy, ratio, 1, 10000);
102  }
103 
106  inline void getPerspective(double &fovy, double &ratio) const
107  {
108  if(_projType == PERSPECTIVE)
109  {
110  double zNear, zFar;
111  _projection.getPerspective(fovy, ratio, zNear, zFar);
112  }
113  }
114 
116  inline void setOrthographic(const double left, const double right,
117  const double bottom, const double top)
118  {
119  _projType = ORTHOGRAPHIC;
120  _projection.makeOrtho(left, right, bottom, top, 1, 10000);
121  }
122 
125  inline void getOrthographic(double &left, double &right,
126  double &bottom, double &top) const
127  {
128  if(_projType == ORTHOGRAPHIC)
129  {
130  double zNear, zFar;
131  _projection.getOrtho(left, right, bottom, top, zNear, zFar);
132  }
133  }
134 
136  inline osg::Matrixd getProjectionMatrix() { return _projection; }
137  osg::Matrixd getViewMatrix();
138 
142  void setDefaultViewDistance(double distance) { _defaultViewDistance = distance; }
143  double getDefaultViewDistance() const { return _defaultViewDistance; }
144 
146  FollowingTrackball* getTrackball() const { return _trackball.get(); }
147  void setTrackball(FollowingTrackball *trackball);
148 
151  void resetView();
152 
159  void saveView();
160  void restoreView();
161 
168  void setViewFrame( ReferenceFrame* root, ReferenceFrame* viewFrame, ViewFrameType frameType = RELATIVE_FRAME );
169 
174  void setViewBetweenFrames( ReferenceFrame* root, ReferenceFrame* viewFrame, ReferenceFrame* lookatFrame, ViewFrameType frameType = RELATIVE_FRAME, ViewRotationType rotationType = AZEL );
175 
177  ReferenceFrame* getViewRoot() { return _xform->getRoot(); }
178  ReferenceFrame* getViewFrame() { return _xform->getOrigin(); }
179  ReferenceFrame* getLookAtFrame() { return _xform_lookat->getOrigin(); }
180  ViewFrameType getViewFrameType() { return _frameType; }
181  ViewRotationType getViewRotationType() { return _rotationType; }
182 
183  inline bool isValid() {return _xform->isValid();}
184 
185  protected:
186  virtual ~View();
187  void _init();
188 
190  osg::ref_ptr<TransformAccumulator> _xform;
191  ViewFrameType _frameType;
192 
194  osg::ref_ptr<TransformAccumulator> _xform_lookat;
195  ViewRotationType _rotationType;
196 
199 
201  osg::Matrixd _projection;
202 
205 
207  osg::ref_ptr<FollowingTrackball> _trackball;
208  };
209 
220  class OF_EXPORT FollowingTrackball : public osgGA::TrackballManipulator
221  {
222  public:
224 
225  virtual const char* className() const { return "FollowingTrackball"; }
226 
227  // Set the frame transform sources
228  void setTransformSources(TransformAccumulator *xform, TransformAccumulator *xform_lookat, View::ViewFrameType frameType, View::ViewRotationType rotationType);
229 
230  // Inherited from TrackballManipulator
231  virtual osg::Matrixd getMatrix() const; // Get local to world matrix
232  virtual osg::Matrixd getInverseMatrix() const; // Get world to local matrix
233  virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us); // Handle event
234 
235  protected:
236  virtual ~FollowingTrackball();
237 
238  // Save, restore, and reset trackball state
239  friend class View;
240  virtual void saveState();
241  virtual void restoreState();
242  virtual void resetState();
243 
244  // Compute matrix that incorporates custom view transformations
245  void computeWorldToViewMatrix(osg::Matrixd &matrix) const;
246 
247  // The ReferenceFrame being followed.
248  osg::observer_ptr<TransformAccumulator> _xform, _xform_lookat;
249  View::ViewFrameType _frameType;
250  View::ViewRotationType _rotationType;
251  };
252 
253 } // !namespace OpenFrames
254 
255 #endif
osg::Matrixd getProjectionMatrix()
Definition: View.hpp:136
ViewRotationType
Definition: View.hpp:78
ProjectionType getProjectionType()
Definition: View.hpp:95
ViewFrameType
Definition: View.hpp:58
Defines transforms that can perform complex view transformations.
Definition: View.hpp:220
void setPerspective(const double fovy, const double ratio)
Definition: View.hpp:98
ReferenceFrame * getViewRoot()
Definition: View.hpp:177
Definition: CoordinateAxes.hpp:29
void setDefaultViewDistance(double distance)
Definition: View.hpp:142
osg::ref_ptr< FollowingTrackball > _trackball
Definition: View.hpp:207
ProjectionType
Definition: View.hpp:49
double _defaultViewDistance
Definition: View.hpp:204
osg::ref_ptr< TransformAccumulator > _xform
Definition: View.hpp:190
Computes the transformation of a ReferenceFrame wrt an ancestor.
Definition: TransformAccumulator.hpp:77
osg::ref_ptr< TransformAccumulator > _xform_lookat
Definition: View.hpp:194
void getPerspective(double &fovy, double &ratio) const
Definition: View.hpp:106
osg::Matrixd _projection
Definition: View.hpp:201
void getOrthographic(double &left, double &right, double &bottom, double &top) const
Definition: View.hpp:125
FollowingTrackball * getTrackball() const
Definition: View.hpp:146
Definition: ReferenceFrame.hpp:54
void setOrthographic(const double left, const double right, const double bottom, const double top)
Definition: View.hpp:116
Encapsulates variables needed for a view.
Definition: View.hpp:45
ProjectionType _projType
Definition: View.hpp:198