cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
osaOSGBody.h
Go to the documentation of this file.
1 
2 /*
3 
4  Author(s): Simon Leonard
5  Created on: Nov 11 2009
6 
7  (C) Copyright 2008 Johns Hopkins University (JHU), All Rights
8  Reserved.
9 
10 --- begin cisst license - do not edit ---
11 
12 This software is provided "as is" under an open source license, with
13 no warranty. The complete license can be found in license.txt and
14 http://www.cisst.org/cisst/license.txt.
15 
16 --- end cisst license ---
17 */
18 
19 #ifndef _osaOSGBody_h
20 #define _osaOSGBody_h
21 
22 #include <osg/Group>
23 #include <osg/Switch>
24 #include <osg/MatrixTransform>
25 #include <osg/TriangleFunctor>
26 #include <osg/PositionAttitudeTransform>
27 #include <osgUtil/TransformAttributeFunctor>
28 
30 
33 
34 class CISST_EXPORT osaOSGBody : public osg::Group {
35 
36  public:
37 
38  enum Switch{ SWITCH_OFF, SWITCH_ON };
39 
40  protected:
41 
42 
43  class TransparencyVisitor : public osg::NodeVisitor {
44  public:
45  double alpha;
47  virtual void apply( osg::Geode& geode );
48  }; // TransparencyVisitor
49 
50 
51  // This class is used to extract the triangle mesh out of the OSG
52  // classes/structures. It is a geode visitor that traverse the drawable
53  // objects and extract all the triangles from all the drawables
54  class GeodeVisitor : public osg::NodeVisitor {
55 
56  private:
57 
58  // Create a structure to hold a triangle
59  struct Triangle
60  { osg::Vec3 p1, p2, p3; };
61 
62  // For each drawable, a TriangleExtractor object is created. The operator()
63  // is called for each triangle of the drawable
64  struct TriangleExtractor {
65 
66  // The list of triangles for a drawable
67  std::vector< osaOSGBody::GeodeVisitor::Triangle > drawabletriangles;
68 
69  // This method is called for each triangle of the drawable. All it does
70  // is to copy the vertices to the vector
71  inline void operator ()( const osg::Vec3& p1,
72  const osg::Vec3& p2,
73  const osg::Vec3& p3,
74  bool treatVertexDataAsTemporary );
75  };
76 
77  public:
78 
79  // the list of triangles for the geode (composed of several drawables)
80  std::vector< osaOSGBody::GeodeVisitor::Triangle > geodetriangles;
81 
82  // Default constructor
83  GeodeVisitor();
84 
85  // This method is called for each geode. It scans all the drawable of the
86  // geode and extract/copy the triangles to the triangle vector
87  //virtual void apply( osg::Geode& geode );
88  virtual void apply( osg::Geode& geode );
89 
90  }; // GeodeVisitor
91 
92 
93  // Callback stuff
94 
95  // This is to store a pointer to the body
96  class UserData : public osg::Referenced {
97  private:
98  osg::ref_ptr<osaOSGBody> body;
99  public:
100  UserData( osaOSGBody* body ) : body( body ){}
101  osaOSGBody* GetBody() { return body; }
102  };
103  osg::ref_ptr<UserData> userdata;
104 
105  // A scaling factor
106  osg::ref_ptr<osg::PositionAttitudeTransform> osgscale;
107 
108 
109  // This is used to update the position of the body
110  class TransformCallback : public osg::NodeCallback {
111  public:
113  void operator()( osg::Node* node, osg::NodeVisitor* nv );
114  };
115  // The transform callback
116  osg::ref_ptr<TransformCallback> transformcallback;
117 
119  virtual void UpdateTransform();
120 
121  // The vct transform
124 
125  // The osg transform
126  osg::ref_ptr<osg::MatrixTransform> osgtransform;
127 
128 
129 
130  // This is used to update the position of the body
131  class SwitchCallback : public osg::NodeCallback {
132  public:
134  void operator()( osg::Node* node, osg::NodeVisitor* nv );
135  };
136  // The switch callback
137  osg::ref_ptr<SwitchCallback> switchcallback;
138 
140  virtual void UpdateSwitch();
141 
142  // The switch
144 
145  // The switch
146  osg::ref_ptr< osg::Switch> osgswitch;
147 
148  osg::ref_ptr<osg::Geode> osggeode;
149 
150  // I/O
151  void ReadModel( const std::string& fname,
152  const std::string& options,
153  double alpha );
154 
155  void Read3DData( const vctDynamicMatrix<double>& pc,
156  const vctFixedSizeVector<unsigned char,3>& RGB = RGBDEFAULT,
157  float size = 3.0 );
158 
159  public:
160 
162 
164 
167  osaOSGBody(osaOSGWorld* world, const vctFrame4x4<double>& Rt);
168  osaOSGBody(osaOSGBody* body, const vctFrame4x4<double>& Rt);
169 
171 
180  osaOSGBody( const std::string& model,
181  const vctFrame4x4<double>& Rt,
182  double scale = 1.0,
183  double alpha = 1.0,
184  const vctFrame4x4<double>& Rtoffset = vctFrame4x4<double>(),
185  const std::string& option = std::string("") );
186 
188 
198  osaOSGBody( const std::string& model,
199  osaOSGWorld* world,
200  const vctFrame4x4<double>& Rt,
201  double scale = 1.0,
202  double alpha = 1.0,
203  const vctFrame4x4<double>& Rtoffset = vctFrame4x4<double>(),
204  const std::string& option = std::string("") );
205 
206 
208 
218  osaOSGBody( const std::string& model,
219  osaOSGBody* world,
220  const vctFrame4x4<double>& Rt,
221  double scale = 1.0,
222  double alpha = 1.0,
223  const vctFrame4x4<double>& Rtoffset = vctFrame4x4<double>(),
224  const std::string& option = std::string("") );
225 
227 
237  osaOSGBody( const std::string& model,
238  osaOSGWorld* world,
239  const vctFrm3& Rt,
240  double scale = 1.0,
241  double alpha = 1.0,
242  const std::string& option = std::string("") );
243 
244 
246  osaOSGBody( const vctDynamicMatrix<double>& pointcloud,
247  osaOSGWorld* world,
248  const vctFrm3& Rt,
249  const vctFixedSizeVector<unsigned char,3>& rgb=RGBDEFAULT,
250  float size = 3.0 );
251 
252  osaOSGBody();
253 
254  ~osaOSGBody();
255 
256  void Initialize( double scale = 1.0 );
257 
259  virtual void SetTransform( const vctFrame4x4<double>& Rt );
260  virtual void SetTransform( const vctFrm3& Rt );
261  virtual vctFrm3 GetTransform() const;
262 
263  osg::ref_ptr<osg::MatrixTransform> GetMatrixTransform() {
264  return this->osgtransform;
265  }
266 
268  osg::Matrix matrix = osgtransform->getMatrix();
269  vctFrame4x4<double> frame( vctMatrixRotation3<double>( matrix(0,0), matrix(0,1), matrix(0,2),
270  matrix(1,0), matrix(1,1), matrix(1,2),
271  matrix(2,0), matrix(2,1), matrix(2,2)),
272  vctFixedSizeVector<double,3>(matrix(0,3), matrix(1,3), matrix(2,3)));
273  SetTransform(frame);
274  }
275 
277  void SwitchOn();
278  void SwitchOff();
279 
280  void SetModeLine();
281  void SetModePoint();
282  void SetModeFill();
283 
284  void AddTransformCallback(void);
285 
286  virtual vctDynamicMatrix<double> GetVertices();
287 
288 };
289 
290 #endif
static const vctFixedSizeVector< unsigned char, 3 > RGBDEFAULT
Definition: osaOSGBody.h:161
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
osg::ref_ptr< osg::MatrixTransform > osgtransform
Definition: osaOSGBody.h:126
Definition: osaOSGBody.h:110
Definition: osaOSGBody.h:34
osg::ref_ptr< osg::PositionAttitudeTransform > osgscale
Definition: osaOSGBody.h:106
osg::ref_ptr< SwitchCallback > switchcallback
Definition: osaOSGBody.h:137
vctFrame4x4< double > transform
Definition: osaOSGBody.h:122
osg::ref_ptr< osg::Switch > osgswitch
Definition: osaOSGBody.h:146
SwitchCallback()
Definition: osaOSGBody.h:133
osaOSGBody * GetBody()
Definition: osaOSGBody.h:101
void ResetMatrixTransform()
Definition: osaOSGBody.h:267
osg::ref_ptr< osg::MatrixTransform > GetMatrixTransform()
Definition: osaOSGBody.h:263
osg::ref_ptr< TransformCallback > transformcallback
Definition: osaOSGBody.h:116
UserData(osaOSGBody *body)
Definition: osaOSGBody.h:100
Definition: osaOSGBody.h:54
osg::ref_ptr< osg::Geode > osggeode
Definition: osaOSGBody.h:148
Typedef for different transformations.
Definition: osaOSGWorld.h:24
Definition: osaOSGBody.h:43
vctFrame4x4< double > Rtoffset
Definition: osaOSGBody.h:123
std::vector< osaOSGBody::GeodeVisitor::Triangle > geodetriangles
Definition: osaOSGBody.h:80
Definition: osaOSGBody.h:96
double alpha
Definition: osaOSGBody.h:45
Switch
Definition: osaOSGBody.h:38
void CISST_EXPORT Triangle(svlSampleImage *image, unsigned int videoch, svlTriangle &tri, unsigned char value, svlDraw::Internals &internals)
Switch onoff
Definition: osaOSGBody.h:143
osg::ref_ptr< UserData > userdata
Definition: osaOSGBody.h:103
Definition: osaOSGBody.h:131
TransformCallback()
Definition: osaOSGBody.h:112