MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
SoSceneWriter.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2007, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef SO_SCENE_WRITER_H
14#define SO_SCENE_WRITER_H
15
16
18
19#include "SoUtilsSystem.h"
20#include <ThirdPartyWarningsDisable.h>
21#include <Inventor/SoPrimitiveVertex.h>
22#include <Inventor/nodes/SoNode.h>
23#include <Inventor/nodes/SoShape.h>
24#include <Inventor/fields/SoFields.h>
25#include <Inventor/fields/SoSFEnum.h>
26#include <Inventor/engines/SoSubEngine.h>
27#include <Inventor/sensors/SoFieldSensor.h>
28#include <Inventor/sensors/SoNodeSensor.h>
29#include <boost/unordered_map.hpp>
30#include <ThirdPartyWarningsRestore.h>
31//--------------------------------------------------------------------------------------
33//--------------------------------------------------------------------------------------
34#ifdef WIN32
36#pragma warning(disable : 4018)
37
38#endif
40
41#include <vector>
42#include <algorithm>
43
44
45//--------------------------------------------------------------------------------------------------------------
47
52//--------------------------------------------------------------------------------------------------------------
53class SoSceneWriter : public SoEngine
54{
55 SO_ENGINE_HEADER(SoSceneWriter);
56
57public:
59#ifdef TGSOIV
60 enum Format {
63 VRML_1,
64 VRML_2,
67 Z3D
68 };
69#else
77
79 LINE = 2,
80 TRIANGLE = 3
81 };
82#endif
83
85
88 {
90 SbVec3f normal; // normal is only used if collecting / writing triangle information
92 float shininess;
94 bool operator==(const COLORED_POINT& cp) const
95 {
96 return (cp.coord == coord) && (cp.color == color) && (cp.normal == normal) && (cp.shininess == shininess) &&
97 (cp.transparency == transparency);
98 }
99 };
100
101
104 {
105 inline size_t operator()(const COLORED_POINT& cp) const
106 {
107 size_t seed = 0;
108
109 for (int i = 0; i < 3; ++i) {
110 boost::hash_combine(seed, cp.coord[i]);
111 boost::hash_combine(seed, cp.normal[i]);
112 boost::hash_combine(seed, cp.color[i]);
113 }
114 boost::hash_combine(seed, cp.shininess);
115 boost::hash_combine(seed, cp.transparency);
116
117 return seed;
118 }
119 };
120
121
123
132
133#ifndef TGSOIV
135
138
141
144
147#endif
148
150
152 static void initClass();
153
157 static void saveChangedCB(void* data, SoSensor* a);
158
160 void inputChanged(SoField* /*whichField*/) override {};
162 void evaluate() override {};
163
164protected:
165 ~SoSceneWriter() override;
166
169
170#ifndef TGSOIV
173#endif
174
175
177
180
181
183 SbVec3f const getNormal(const SbVec3f* pVec, const int nIdx);
184
185public:
188 const SoPrimitiveVertex* pV3);
189
191
192private:
194 SoFieldSensor* _saveSensor;
195
196
197#ifndef TGSOIV
199 SbMatrix _wTransMat;
201 SbMatrix _wInverseTransposedMat;
203 unsigned int _currentDrawStyle;
205 unsigned int _currentVertexOrdering;
206
207
209 void _writeVRML2Header(std::ofstream& outFile);
211 void _writeVRML2PointSet(std::ofstream& outFile);
213 void _writeVRML2LineSet(std::ofstream& outFile);
215 void _writeVRML2FaceSet(std::ofstream& outFile);
216
218 int _vecToStr(const SbVec3f& vec, char* buf, size_t numEntries = 0, size_t currNum = 0);
220 int _vecToStr(const SbColor& col, char* buf, size_t numEntries = 0, size_t currNum = 0);
223 void _writeIndices(std::ofstream& outFile, size_t numIndices, size_t posDelim);
224 void _writeIndices(std::ofstream& outFile, std::vector<size_t>& indexList, size_t posDelim);
225
227 SoCallbackAction* _callbackAction;
228
230 static SoCallbackAction::Response preCB(void* userData, SoCallbackAction* action, const SoNode* node);
231
233 static void pointCB(void* userData, SoCallbackAction* action, const SoPrimitiveVertex* v);
235 static void lineCB(void* userData, SoCallbackAction* action, const SoPrimitiveVertex* v1,
236 const SoPrimitiveVertex* v2);
238 static void triangleCB(void* userData, SoCallbackAction* action, const SoPrimitiveVertex* v1,
239 const SoPrimitiveVertex* v2, const SoPrimitiveVertex* v3);
240
242 typedef std::vector<COLORED_POINT> pointsVec;
244 typedef std::vector<COLORED_POINT> linesVec;
246 typedef std::vector<COLORED_POINT> trianglesVec;
247
248
250 std::vector<pointsVec> _points;
252 std::vector<linesVec> _lines;
254 std::vector<trianglesVec> _triangles;
255
258 boost::unordered_map<COLORED_POINT, int, hashColoredPoint> _distinctColoredVertices;
259#endif
260};
261
262#endif
Open Inventor engine to write scene representation to file.
SoSFEnum format
format that should be written
SoSFBool bSaveNormals
save or suppress saving of normals
Format
Save formats.
void inputChanged(SoField *) override
Engine dummy implementation.
void writeSimpleVRML2()
write simple VRML 2 format (oko)
SbVec3f const getNormal(const SbVec3f *pVec, const int nIdx)
compute the normal of the given triangle
static void initClass()
initializes this class
static void saveChangedCB(void *data, SoSensor *a)
forward sensor CB to saveChanged()
SoSFNode sceneGraph
Fields.
~SoSceneWriter() override
void formatFileExtension()
set filename field's extension according to format
SoSFString filename
absolute filename of the output file
SoSFBool bNormalPerVertex
save normal per vertex (or only one per facet)
static void getTriIn(void *pData, SoCallbackAction *pAct, const SoPrimitiveVertex *pV1, const SoPrimitiveVertex *pV2, const SoPrimitiveVertex *pV3)
callback function for extracting triangulation from the scene
void writeVecToStlFile(SoMFVec3f *pMVec3f)
STL File format (Stefan Zidowitz)
SoSFBool bUseGlobalColor
save only one color per object
void saveChanged()
save the scene
SoSFBool bColorPerVertex
Format Options for self-coded VRML 2.0.
void evaluate() override
Engine dummy implementation.
SoSFTrigger save
save the scene
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
struct holds point information when VRML_2 output is applied on SGI Inventor scenes
bool operator==(const COLORED_POINT &cp) const
For computing a hash value for a colored point.
size_t operator()(const COLORED_POINT &cp) const