BRL-CAD
osg_fb_manipulator.h
Go to the documentation of this file.
1 /* The following is a customization of StandardManipulator for Framebuffer applications */
2 
3 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
4  *
5  * This library is open source and may be redistributed and/or modified under
6  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
7  * (at your option) any later version. The full license is in LICENSE file
8  * included with this distribution, and on the openscenegraph.org website.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * OpenSceneGraph Public License for more details.
14  *
15  * OrbitManipulator code Copyright (C) 2010 PCJohn (Jan Peciva)
16  * while some pieces of code were taken from OSG.
17  * Thanks to company Cadwork (www.cadwork.ch) and
18  * Brno University of Technology (www.fit.vutbr.cz) for open-sourcing this work.
19  */
20 
21 #include "common.h"
22 
23 #include <osgGA/StandardManipulator>
24 
25 using namespace osg;
26 using namespace osgGA;
27 
28 namespace osgGA {
29 
30 
31 /** FrameBufferManipulator is base class for interacting with a framebuffer .*/
32 class FB_EXPORT FrameBufferManipulator : public StandardManipulator
33 {
34  typedef StandardManipulator inherited;
35 
36  public:
37 
38  FrameBufferManipulator( int flags = DEFAULT_SETTINGS );
40  const osg::CopyOp& copyOp = osg::CopyOp::SHALLOW_COPY );
41 
42  META_Object( osgGA, FrameBufferManipulator );
43 
44  virtual void setByMatrix( const osg::Matrixd& matrix );
45  virtual void setByInverseMatrix( const osg::Matrixd& matrix );
46  virtual osg::Matrixd getMatrix() const;
47  virtual osg::Matrixd getInverseMatrix() const;
48 
49  virtual void setTransformation( const osg::Vec3d& eye, const osg::Quat& rotation );
50  virtual void setTransformation( const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up );
51  virtual void getTransformation( osg::Vec3d& eye, osg::Quat& rotation ) const;
52  virtual void getTransformation( osg::Vec3d& eye, osg::Vec3d& center, osg::Vec3d& up ) const;
53 
54  virtual osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const;
55  virtual float getFusionDistanceValue() const;
56 
57  protected:
58 
59  osg::Vec3d _center;
60  osg::Quat _rotation;
61  double _distance;
62 
65 };
66 
67 
68 int FrameBufferManipulator::_minimumDistanceFlagIndex = allocateRelativeFlag();
69 
70 
71 /// Constructor.
72 FrameBufferManipulator::FrameBufferManipulator( int flags )
73  : inherited( flags ), _distance( 1. )
74 {
75 }
76 
77 
78 /// Constructor.
80  : osg::Object(om, copyOp), inherited( om, copyOp ), _center( om._center ),
81  _rotation( om._rotation ), _distance( om._distance ), _minimumDistance( om._minimumDistance )
82 {
83 }
84 
85 
86 /** Set the position of the manipulator using a 4x4 matrix.*/
87 void FrameBufferManipulator::setByMatrix( const osg::Matrixd& matrix )
88 {
89  _center = osg::Vec3d( 0., 0., -_distance ) * matrix;
90  _rotation = matrix.getRotate();
91 
92  // fix current rotation
93  if( getVerticalAxisFixed() )
94  fixVerticalAxis( _center, _rotation, true );
95 }
96 
97 
98 /** Set the position of the manipulator using a 4x4 matrix.*/
99 void FrameBufferManipulator::setByInverseMatrix( const osg::Matrixd& matrix )
100 {
101  setByMatrix( osg::Matrixd::inverse( matrix ) );
102 }
103 
104 
105 /** Get the position of the manipulator as 4x4 matrix.*/
107 {
108  return osg::Matrixd::translate( 0., 0., _distance ) *
109  osg::Matrixd::rotate( _rotation ) *
111 }
112 
113 
114 /** Get the position of the manipulator as a inverse matrix of the manipulator,
115  typically used as a model view matrix.*/
117 {
118  return osg::Matrixd::translate( -_center ) *
119  osg::Matrixd::rotate( _rotation.inverse() ) *
120  osg::Matrixd::translate( 0.0, 0.0, -_distance );
121 }
122 
123 
124 // doc in parent
125 void FrameBufferManipulator::setTransformation( const osg::Vec3d& eye, const osg::Quat& rotation )
126 {
127  _center = eye + rotation * osg::Vec3d( 0., 0., -_distance );
128  _rotation = rotation;
129 
130  // fix current rotation
131  if( getVerticalAxisFixed() )
132  fixVerticalAxis( _center, _rotation, true );
133 }
134 
135 
136 // doc in parent
137 void FrameBufferManipulator::getTransformation( osg::Vec3d& eye, osg::Quat& rotation ) const
138 {
139  eye = _center - _rotation * osg::Vec3d( 0., 0., -_distance );
140  rotation = _rotation;
141 }
142 
143 
144 // doc in parent
145 void FrameBufferManipulator::setTransformation( const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up )
146 {
147  Vec3d lv( center - eye );
148 
149  Vec3d f( lv );
150  f.normalize();
151  Vec3d s( f^up );
152  s.normalize();
153  Vec3d u( s^f );
154  u.normalize();
155 
156  osg::Matrixd rotation_matrix( s[0], u[0], -f[0], 0.0f,
157  s[1], u[1], -f[1], 0.0f,
158  s[2], u[2], -f[2], 0.0f,
159  0.0f, 0.0f, 0.0f, 1.0f );
160 
161  _center = center;
162  _distance = lv.length();
163  _rotation = rotation_matrix.getRotate().inverse();
164 
165  // fix current rotation
166  if( getVerticalAxisFixed() )
167  fixVerticalAxis( _center, _rotation, true );
168 }
169 
170 
171 // doc in parent
172 void FrameBufferManipulator::getTransformation( osg::Vec3d& eye, osg::Vec3d& center, osg::Vec3d& up ) const
173 {
174  center = _center;
175  eye = _center + _rotation * osg::Vec3d( 0., 0., _distance );
176  up = _rotation * osg::Vec3d( 0., 1., 0. );
177 }
178 
179 /** Get the FusionDistanceMode. Used by SceneView for setting up stereo convergence.*/
180 osgUtil::SceneView::FusionDistanceMode FrameBufferManipulator::getFusionDistanceMode() const
181 {
182  return osgUtil::SceneView::USE_FUSION_DISTANCE_VALUE;
183 }
184 
185 /** Get the FusionDistanceValue. Used by SceneView for setting up stereo convergence.*/
187 {
188  return _distance;
189 }
190 
191 } // end osgGA namespace
192 
193 /**************************** End FrameBufferManipulator *************************************/
194 
195 
196 // KeyHandler --
197 // Allow user to do interesting things with an
198 // OcclusionQueryNode-enabled scene graph at run time.
199 class KeyHandler : public osgGA::GUIEventHandler
200 {
201 public:
202  KeyHandler( osg::Node& node )
203  : _node( node ),
204  _enable( true ),
205  _debug( false )
206  {}
207 
208  bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& )
209  {
210  switch( ea.getEventType() )
211  {
212  case(osgGA::GUIEventAdapter::KEYUP):
213  {
214  if (ea.getKey()==osgGA::GUIEventAdapter::KEY_F6)
215  {
216  // F6 -- Toggle osgOQ testing.
217  //std::cout << "F6!";
218  return true;
219  }
220  else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_F7)
221  {
222  // F7 -- Toggle display of OQ test bounding volumes
223  //std::cout << "F7!";
224  return true;
225  }
226  else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_F8)
227  {
228  // F8 -- Gather stats and display
229  //std::cout << "F8!";
230  return true;
231  }
232  else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_F9)
233  {
234  // F9 -- Remove all OcclusionQueryNodes
235  //std::cout << "F9!";
236  return true;
237  }
238  else if (ea.getKey()=='o')
239  {
240  //std::cout << "o!";
241  return true;
242  }
243  return false;
244  }
246  {
247  //std::cout << "PUSH! ";
248  if (osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON & ea.getButton()) {
249  //std::cout << "Left Button\n";
250  }
251  if (osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON & ea.getButton()) {
252  ea.setHandled(true);
253  osgViewer::GraphicsWindow *gw = dynamic_cast<osgViewer::GraphicsWindow*>(OSGL(fbp)->glc);
254  gw->close();
255  break;
256  }
257  if (osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON & ea.getButton()) {
258  int x = ea.getX();
259  int y = ea.getY();
260 
261  if (x < 0 || y < 0) {
262  fb_log("No RGB (outside image viewport)\n");
263  break;
264  }
265  int r = fbp->if_mem[3*(y*fbp->if_width + x)];
266  int g = fbp->if_mem[3*(y*fbp->if_width + x)+1];
267  int b = fbp->if_mem[3*(y*fbp->if_width + x)+2];
268  if (r < 0) r = 255 + r + 1;
269  if (g < 0) g = 255 + g + 1;
270  if (b < 0) b = 255 + b + 1;
271 
272  fb_log("At image (%d, %d), real RGB=(%d %d %d)\n", x, y, r, g, b);
273  }
274  return false;
275  }
276  case(osgGA::GUIEventAdapter::RELEASE):
277  {
278  /*std::cout << "RELEASE! ";
279  if (osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON & ea.getButton()) std::cout << "Left Button\n";
280  if (osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON & ea.getButton()) std::cout << "Middle Button\n";
281  if (osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON & ea.getButton()) std::cout << "Right Button\n";*/
282  return false;
283  }
284  case(osgGA::GUIEventAdapter::DOUBLECLICK):
285  {
286  //std::cout << "DOUBLECLICK!\n";
287  return false;
288  }
289  case(osgGA::GUIEventAdapter::DRAG):
290  {
291  //std::cout << "DRAG!\n";
292  return false;
293  }
295  {
296  //std::cout << "MOVE!\n";
297  return false;
298  }
299  case(osgGA::GUIEventAdapter::SCROLL):
300  {
301  /*std::cout << "SCROLL ";
302  if (osgGA::GUIEventAdapter::SCROLL_UP == ea.getScrollingMotion()) std::cout << "UP!\n";
303  if (osgGA::GUIEventAdapter::SCROLL_DOWN == ea.getScrollingMotion()) std::cout << "DOWN!\n";
304  if (osgGA::GUIEventAdapter::SCROLL_LEFT == ea.getScrollingMotion()) std::cout << "LEFT!\n";
305  if (osgGA::GUIEventAdapter::SCROLL_RIGHT == ea.getScrollingMotion()) std::cout << "RIGHT!\n";*/
306  return false;
307  }
308 
309 
310  default:
311  return false;
312  break;
313  }
314  return false;
315  }
316 
317  osg::Node& _node;
318 
319  fb *fbp;
320 
322 };
323 /*********************************** End KeyHandler ******************************************/
324 
325 /*
326  * Local Variables:
327  * mode: C
328  * tab-width: 8
329  * indent-tabs-mode: t
330  * c-file-style: "stroustrup"
331  * End:
332  * ex: shiftwidth=4 tabstop=8
333  */
virtual float getFusionDistanceValue() const
#define PUSH(ptr)
Definition: fastgen4_read.c:67
void fb_log(const char *fmt,...) _BU_ATTR_PRINTF12
Definition: fb_log.c:42
if lu s
Definition: nmg_mod.c:3860
virtual osg::Matrixd getMatrix() const
Header file for the BRL-CAD common definitions.
KeyHandler(osg::Node &node)
FrameBufferManipulator(int flags=DEFAULT_SETTINGS)
Constructor.
virtual osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const
bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &)
#define MOVE(_pt)
osg::Node & _node
virtual void setTransformation(const osg::Vec3d &eye, const osg::Quat &rotation)
virtual osg::Matrixd getInverseMatrix() const
virtual void setByInverseMatrix(const osg::Matrixd &matrix)
A frame-buffer IO structure.
Definition: fb_private.h:80
int if_width
current values
Definition: fb_private.h:115
virtual void setByMatrix(const osg::Matrixd &matrix)
virtual void getTransformation(osg::Vec3d &eye, osg::Quat &rotation) const
void translate(matp_t matrix, mat_t tm, point_t min, point_t max)
Definition: pull.c:131