AGX Dynamics  2.37.3.2
TriangleExtractor.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGXOSG_TRIANGLEEXTRACTOR_H
18 #define AGXOSG_TRIANGLEEXTRACTOR_H
19 
20 
21 #include <agxOSG/export.h>
22 
23 #include <agx/PushDisableWarnings.h> // Disabling warnings. Include agx/PopDisableWarnings.h below!
24 #include <osg/NodeVisitor>
25 #include <osg/Matrix>
26 #include <osg/Referenced>
27 #include <osg/TriangleFunctor>
28 #include <osg/Version>
29 #include <agx/PopDisableWarnings.h> // End of disabled warnings.
30 
31 #include <agx/Vector.h>
32 #include <agx/agx_vector_types.h>
33 
34 
35 #ifdef _MSC_VER
36 // Disable warning about assignment operator could not be generated due to reference members.
37 # pragma warning( push )
38 # pragma warning( disable: 4512 )
39 #endif
40 
42 
43 
44 namespace osg {
45  class Geometry;
46  class Geode;
47  class MatrixTransform;
48 }
50 
51 namespace agxOSG
52 {
53 
54  class TriangleExtractor;
55 
57 
64  {
65 
66  public:
67  friend class TriangleExtractor;
68 
70  TriangleExtractOperatorBase() : m_num_triangles( 0 ) { }
71 
73 
74 #if OSG_VERSION_GREATER_OR_EQUAL(3,6,4)
75  void operator () (const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3);
76 #else
77  void operator () (const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3, bool);
78 #endif
79 
80  protected:
81 
82 
84  void setMatrix( const osg::Matrix& matrix ) {
85  m_matrix = matrix;
86  }
87  void setMatrix() {
88  m_matrix.identity();
89  }
90 
92  unsigned int numTriangles() {
93  return m_num_triangles;
94  }
95 
97  void reset() {
98  m_num_triangles = 0;
99  }
100 
106  virtual void triangle( const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3 ) = 0;
107 
108  private:
109  unsigned int m_num_triangles;
110  osg::Matrix m_matrix;
111  };
112 
113  //
114  inline void TriangleExtractOperatorBase::operator () ( const osg::Vec3& v1,
115  const osg::Vec3& v2,
116  const osg::Vec3& v3
117 #if OSG_VERSION_GREATER_OR_EQUAL(3,6,4)
118 #else
119  , bool
120 #endif
121  )
122  {
123  if ( v1 == v2 || v2 == v3 || v1 == v3 ) return;
124 
125  // Calculate the vertices world coordinates.
126  osg::Vec3 v1n, v2n , v3n;
127  v1n = m_matrix.preMult( v1 );
128  v2n = m_matrix.preMult( v2 );
129  v3n = m_matrix.preMult( v3 );
130 
131  // Execute the inherited method to process the vertices
132  triangle( v1n, v2n, v3n );
133  m_num_triangles++;
134  }
135 
136 
138  typedef osg::TriangleFunctor<TriangleExtractOperatorBase> TriangleExtractOperator;
139 
140 
142 
148  class AGXOSG_EXPORT TriangleExtractor : public osg::NodeVisitor
149  {
150  public:
151 
156  TriangleExtractor( TriangleExtractOperator &op );
157 
159  virtual ~TriangleExtractor() {}
160 
164  void extract( osg::Node& node );
165 
169  void extract( osg::Geode& node ) {
170  apply( node );
171  }
172 
176  void extract( osg::MatrixTransform& node ) {
177  apply( node );
178  }
179  protected:
180 
181  virtual void apply( osg::Node& );
182  virtual void apply( osg::Geode& node );
183  virtual void apply( osg::Billboard& node );
184 
185  virtual void apply( osg::Group& node );
186  virtual void apply( osg::MatrixTransform& node );
187  virtual void apply( osg::Switch& node );
188  virtual void apply( osg::LOD& node );
189 
190  void apply( osg::Geometry& geom );
191  using osg::NodeVisitor::apply;
192 
193  void pushMatrix( const osg::Matrix& matrix );
194  void popMatrix();
195 
196  TriangleExtractOperator & m_tri_op;
197 
198  private:
200 
201  MatrixStack m_matrix_stack;
202  };
203 
208  {
209  public:
210 
212  virtual void triangle( const osg::Vec3& v0, const osg::Vec3& v1, const osg::Vec3& v2 );
213 
214  agx::Vec3Vector* getVertices() {return &m_vertices;}
215  agx::UInt32Vector* getIndices() {return &m_indices;}
216 
217  virtual ~MeshExtractor( ) {}
218 
219  private:
220  agx::Vec3Vector m_vertices;
221  agx::UInt32Vector m_indices;
222  };
223 
228  {
229  public:
230 
231  MeshExtractorReference(agx::Vec3Vector& vertices, agx::UInt32Vector& indices) : m_vertices(vertices), m_indices(indices)
232  {}
233 
234  virtual void triangle(const osg::Vec3& v0, const osg::Vec3& v1, const osg::Vec3& v2);
235 
236  agx::Vec3Vector* getVertices() { return &m_vertices; }
237  agx::UInt32Vector* getIndices() { return &m_indices; }
238 
240 
241  private:
242  agx::Vec3Vector& m_vertices;
243  agx::UInt32Vector& m_indices;
244  };
245 
246  inline void MeshExtractor::triangle( const osg::Vec3& v0, const osg::Vec3& v1, const osg::Vec3& v2 )
247  {
248  m_vertices.push_back( OSG_VEC3_TO_AGX(v0) );
249  m_vertices.push_back( OSG_VEC3_TO_AGX(v1) );
250  m_vertices.push_back( OSG_VEC3_TO_AGX(v2) );
251  m_indices.push_back((agx::UInt32) m_indices.size());
252  m_indices.push_back((agx::UInt32) m_indices.size());
253  m_indices.push_back((agx::UInt32)m_indices.size());
254  }
255 
256  inline void MeshExtractorReference::triangle(const osg::Vec3& v0, const osg::Vec3& v1, const osg::Vec3& v2)
257  {
258  m_vertices.push_back(OSG_VEC3_TO_AGX(v0));
259  m_vertices.push_back(OSG_VEC3_TO_AGX(v1));
260  m_vertices.push_back(OSG_VEC3_TO_AGX(v2));
261  m_indices.push_back((agx::UInt32)m_indices.size());
262  m_indices.push_back((agx::UInt32)m_indices.size());
263  m_indices.push_back((agx::UInt32)m_indices.size());
264  }
265 
266 } // namespace agxOSG
267 
268 #ifdef _MSC_VER
269 # pragma warning(pop)
270 #endif
271 
272 #endif
agx::UInt32Vector * getIndices()
void push_back(const T &value)
Definition: Vector.h:1362
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3.h:36
agx::UInt32Vector * getIndices()
Store Vertices and indices as a vector in this class instance.
#define AGXOSG_EXPORT
unsigned int numTriangles()
Returns the number of triangles processed.
This class traverses a subgraph, accumulates transformations and execute a functor (TriangleExtractOp...
#define OSG_VEC3_TO_AGX(X)
MeshExtractorReference(agx::Vec3Vector &vertices, agx::UInt32Vector &indices)
This class is a base class that implements a functor, with a method triangle that is executed for eac...
void setMatrix(const osg::Matrix &matrix)
Stores the accumulated matrix.
Templated vector class.
Definition: Vector.h:52
The agxOSG namespace provides functionality for visualizing AGX simulations with OpenSceneGraph.
void extract(osg::MatrixTransform &node)
Store vertices and indices in vectors by reference.
virtual ~TriangleExtractor()
Destructor.
void extract(osg::Geode &node)
agx::Vec3Vector * getVertices()
uint32_t UInt32
Definition: Integer.h:40
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97
void reset()
Resets the number of triangles back to zero.
TriangleExtractOperator & m_tri_op
osg::TriangleFunctor< TriangleExtractOperatorBase > TriangleExtractOperator
TriangleExtractOperator is the method inherit to create a operator that will be executed per triangle...