MeVisLab Toolbox Reference
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 //--------------------------------------------------------------------------------------------------------------
53 class SoSceneWriter : public SoEngine
54 {
55  SO_ENGINE_HEADER(SoSceneWriter);
56 
57 public:
59 #ifdef TGSOIV
60  enum Format {
61  INVENTOR_ASCII = 0,
63  VRML_1,
64  VRML_2,
65  STL_ASCII,
66  STL_BINARY,
67  Z3D
68  };
69 #else
70  enum Format {
75  VRML_97
76  };
77 
78  enum SoPrimitive {
79  LINE = 2,
80  TRIANGLE = 3
81  };
82 #endif
83 
85 
87  struct COLORED_POINT{
88  SbVec3f coord;
89  SbVec3f normal; // normal is only used if collecting / writing triangle information
90  SbColor color;
91  float shininess;
92  float transparency;
93  bool operator ==(const COLORED_POINT& cp) const
94  {
95  return (cp.coord == coord) &&
96  (cp.color == color) &&
97  (cp.normal == normal) &&
98  (cp.shininess == shininess) &&
99  (cp.transparency == transparency);
100  }
101  };
102 
103 
106  {
107  inline size_t operator()(const COLORED_POINT& cp) const
108  {
109  size_t seed = 0;
110 
111  for (int i = 0; i < 3; ++i) {
112  boost::hash_combine(seed, cp.coord[i]);
113  boost::hash_combine(seed, cp.normal[i]);
114  boost::hash_combine(seed, cp.color[i]);
115  }
116  boost::hash_combine(seed, cp.shininess);
117  boost::hash_combine(seed, cp.transparency);
118 
119  return seed;
120  }
121  };
122 
123 
124 
126 
128  SoSFNode sceneGraph;
130  SoSFTrigger save;
132  SoSFString filename;
134  SoSFEnum format;
135 
136 #ifndef TGSOIV
138 
140  SoSFBool bColorPerVertex;
141 
143  SoSFBool bUseGlobalColor;
144 
147 
149  SoSFBool bSaveNormals;
150 #endif
151 
153 
155  static void initClass();
156 
158  void saveChanged();
160  static void saveChangedCB(void *data, SoSensor* a);
161 
163  void inputChanged(SoField * /*whichField*/) override {};
165  void evaluate() override {};
166 
167 protected:
168  ~SoSceneWriter() override;
169 
172 
173 #ifndef TGSOIV
176 #endif
177 
178 
180 
182  void writeVecToStlFile (SoMFVec3f *pMVec3f);
183 
184 
186  SbVec3f const getNormal (const SbVec3f *pVec, const int nIdx);
187 
188 public:
190  static void getTriIn(void *pData, SoCallbackAction *pAct, const SoPrimitiveVertex *pV1, const SoPrimitiveVertex *pV2,const SoPrimitiveVertex *pV3);
191 
193 
194 private:
196  SoFieldSensor* _saveSensor;
197 
198 
199 #ifndef TGSOIV
201  SbMatrix _wTransMat;
203  SbMatrix _wInverseTransposedMat;
205  unsigned int _currentDrawStyle;
207  unsigned int _currentVertexOrdering;
208 
209 
211  void _writeVRML2Header(std::ofstream& outFile);
213  void _writeVRML2PointSet(std::ofstream& outFile);
215  void _writeVRML2LineSet(std::ofstream& outFile);
217  void _writeVRML2FaceSet(std::ofstream& outFile);
218 
220  int _vecToStr(const SbVec3f& vec, char* buf, size_t numEntries=0, size_t currNum=0);
222  int _vecToStr(const SbColor& col, char* buf, size_t numEntries=0, size_t currNum=0);
225  void _writeIndices(std::ofstream& outFile, size_t numIndices, size_t posDelim);
226  void _writeIndices(std::ofstream& outFile, std::vector<size_t> & indexList, size_t posDelim);
227 
229  SoCallbackAction* _callbackAction;
230 
232  static SoCallbackAction::Response preCB(void *userData,
233  SoCallbackAction *action,
234  const SoNode *node);
235 
237  static void pointCB (void* userData, SoCallbackAction* action,
238  const SoPrimitiveVertex* v);
240  static void lineCB (void* userData, SoCallbackAction* action,
241  const SoPrimitiveVertex* v1,
242  const SoPrimitiveVertex* v2);
244  static void triangleCB(void* userData, SoCallbackAction* action,
245  const SoPrimitiveVertex* v1,
246  const SoPrimitiveVertex* v2,
247  const SoPrimitiveVertex* v3);
248 
250  typedef std::vector<COLORED_POINT> pointsVec;
252  typedef std::vector<COLORED_POINT> linesVec;
254  typedef std::vector<COLORED_POINT> trianglesVec;
255 
256 
258  std::vector<pointsVec> _points;
260  std::vector<linesVec> _lines;
262  std::vector<trianglesVec> _triangles;
263 
266  boost::unordered_map < COLORED_POINT, int, hashColoredPoint > _distinctColoredVertices;
267 #endif
268 
269 };
270 
271 #endif
Open Inventor engine to write scene representation to file.
Definition: SoSceneWriter.h:54
SoSFEnum format
format that should be written
SoSFBool bSaveNormals
save or suppress saving of normals
Format
Save formats.
Definition: SoSceneWriter.h:70
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
struct holds point information when VRML_2 output is applied on SGI Inventor scenes
Definition: SoSceneWriter.h:87
bool operator==(const COLORED_POINT &cp) const
Definition: SoSceneWriter.h:93
For computing a hash value for a colored point.
size_t operator()(const COLORED_POINT &cp) const