AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
TriangleExtractor.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx 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>
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
44namespace osg {
45 class Geometry;
46 class Geode;
47 class MatrixTransform;
48}
50
51namespace 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
157
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
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
#define OSG_VEC3_TO_AGX(X)
#define AGXOSG_EXPORT
Store vertices and indices in vectors by reference.
MeshExtractorReference(agx::Vec3Vector &vertices, agx::UInt32Vector &indices)
Store Vertices and indices as a vector in this class instance.
agx::Vec3Vector * getVertices()
agx::UInt32Vector * getIndices()
This class is a base class that implements a functor, with a method triangle that is executed for eac...
void reset()
Resets the number of triangles back to zero.
virtual void triangle(const osg::Vec3 &v1, const osg::Vec3 &v2, const osg::Vec3 &v3)=0
unsigned int numTriangles()
Returns the number of triangles processed.
void setMatrix(const osg::Matrix &matrix)
Stores the accumulated matrix.
This class traverses a subgraph, accumulates transformations and execute a functor (TriangleExtractOp...
virtual ~TriangleExtractor()
Destructor.
virtual void apply(osg::Switch &node)
virtual void apply(osg::Billboard &node)
TriangleExtractOperator & m_tri_op
virtual void apply(osg::LOD &node)
void extract(osg::Node &node)
virtual void apply(osg::Group &node)
void apply(osg::Geometry &geom)
virtual void apply(osg::Geode &node)
void extract(osg::MatrixTransform &node)
void extract(osg::Geode &node)
virtual void apply(osg::Node &)
void pushMatrix(const osg::Matrix &matrix)
TriangleExtractor(TriangleExtractOperator &op)
virtual void apply(osg::MatrixTransform &node)
void push_back(const T &value)
Templated vector class.
Definition agx/Vector.h:53
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition macros.h:96
The agxOSG namespace provides functionality for visualizing AGX simulations with OpenSceneGraph.
osg::TriangleFunctor< TriangleExtractOperatorBase > TriangleExtractOperator
TriangleExtractOperator is the method inherit to create a operator that will be executed per triangle...
uint32_t UInt32
Definition Integer.h:32