MeVisLabToolboxReference
MeVisLab/Standard/Sources/Inventor/SoUtils/SoSceneWriter.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //--------------------------------------------------------------------------------------
00005 //--------------------------------------------------------------------------------------
00006 
00007 #ifndef __SoSceneWriter_H
00008 #define __SoSceneWriter_H
00009 
00010 #ifndef __SoUtilsSystem_H
00011 #include "SoUtilsSystem.h"
00012 #endif
00013 
00014 #include <Inventor/SoPrimitiveVertex.h>
00015 #include <Inventor/nodes/SoNode.h>
00016 #include <Inventor/nodes/SoShape.h>
00017 #include <Inventor/fields/SoFields.h>
00018 #include <Inventor/fields/SoSFEnum.h>
00019 #include <Inventor/engines/SoSubEngine.h>
00020 #include <Inventor/sensors/SoFieldSensor.h>
00021 #include <Inventor/sensors/SoNodeSensor.h>
00022 
00023 
00024 //--------------------------------------------------------------------------------------
00026 //--------------------------------------------------------------------------------------
00027 #ifdef WIN32
00028 
00029 #pragma warning(disable : 4018 )
00030 
00031 #endif
00032 
00033 
00034 #include <vector>
00035 #include <algorithm>
00036 
00037 
00038 #ifdef TGSOIV
00039 static const char* szFileExtension[] = {".iv", ".iv", ".wrl", ".wrl", ".stl", ".stl", ".z3d"};
00040 static const char* szFileHeader   [] = {"#Inventor V2.1 ascii",
00041                                         "#Inventor V2.1 binary",
00042                                         "#VRML V1.0 ascii",
00043                                         "#VRML V2.0 utf8",
00044                                         "STL-file created by MeVisLab",
00045                                         "STL-file created by MeVisLab",
00046                                         ""};
00047 #else
00048 static const char* szFileExtension[] = {".iv", ".iv", ".stl", ".stl", ".wrl"};
00049 static const char* szFileHeader   [] = {"#Inventor V2.1 ascii",
00050                                         "#Inventor V2.1 binary",
00051                                         "STL-file created by MeVisLab",
00052                                         "STL-file created by MeVisLab",
00053                                         "#VRML V2.0 utf8"
00054                                         };
00055 #endif
00056 
00057 
00058 //--------------------------------------------------------------------------------------------------------------
00060 
00065 //--------------------------------------------------------------------------------------------------------------
00066 class SoSceneWriter : public SoEngine
00067 {
00068   SO_ENGINE_HEADER(SoSceneWriter);
00069   
00070 public:
00072 #ifdef TGSOIV
00073   enum Format {
00074       INVENTOR_ASCII = 0,
00075       INVENTOR_BINARY,
00076       VRML_1,
00077       VRML_2,
00078       STL_ASCII,
00079       STL_BINARY,
00080       Z3D
00081   };
00082 #else
00083   enum Format {
00084       INVENTOR_ASCII = 0,
00085       INVENTOR_BINARY,
00086       STL_ASCII,
00087       STL_BINARY,
00088       VRML_97
00089   };
00090 
00091   enum SoPrimitive {
00092       LINE     = 2,
00093       TRIANGLE = 3
00094   };
00095 #endif
00096   
00097   SoSceneWriter();
00098 
00100   struct COLORED_POINT{
00101     SbVec3f coord;
00102     SbVec3f normal; // normal is only used if collecting / writing triangle information
00103     SbColor color;
00104     float   shininess;
00105     float   transparency;
00106   };
00107 
00108 
00110   
00112   SoSFNode     sceneGraph;
00114   SoSFTrigger  save;
00116   SoSFString   filename;
00118   SoSFEnum     format;
00119 
00120 #ifndef TGSOIV
00121 
00122   
00124   SoSFBool      bColorPerVertex;
00125 
00127   SoSFBool      bUseGlobalColor;
00128 
00130   SoSFBool      bNormalPerVertex;
00131 
00133   SoSFBool      bSaveNormals;
00134 #endif 
00135   
00137   
00139   static void initClass();
00140   
00142   void saveChanged();
00144   static void saveChangedCB(void *data, SoSensor* a);
00145   
00147   virtual void inputChanged(SoField * /*whichField*/) {};
00149   virtual void evaluate() {};
00150   
00151 protected:
00152   virtual ~SoSceneWriter();
00153   
00155   void formatFileExtension();
00156 
00157 #ifndef TGSOIV
00158 
00159   void writeSimpleVRML2();
00160 #endif 
00161 
00162 
00164 
00166   void writeVecToStlFile    (SoMFVec3f *pMVec3f);
00167 
00168 
00170   SbVec3f const getNormal   (const SbVec3f *pVec, const int nIdx);
00171   
00172 public:
00174   static void getTriIn(void *pData, SoCallbackAction *pAct, const SoPrimitiveVertex *pV1, const SoPrimitiveVertex *pV2,const SoPrimitiveVertex *pV3);
00175 
00177 
00178 private:
00180     SoFieldSensor*  _saveSensor;
00181 
00182 
00183 #ifndef TGSOIV
00184 
00185     SbMatrix _wTransMat;
00187     SbMatrix _wInverseTransposedMat;
00189     unsigned int _currentDrawStyle;
00191     unsigned int _currentVertexOrdering;
00192 
00193 
00195     void _writeVRML2Header(std::ofstream& outFile);
00197     void _writeVRML2PointSet(std::ofstream& outFile);
00199     void _writeVRML2LineSet(std::ofstream& outFile);
00201     void _writeVRML2FaceSet(std::ofstream& outFile);
00202 
00204     int _vecToStr(const SbVec3f& vec, char* buf, size_t numEntries=0, size_t currNum=0);
00206     int _vecToStr(const SbColor& col, char* buf, size_t numEntries=0, size_t currNum=0);
00209     void _writeIndices(std::ofstream& outFile, size_t numIndices, size_t posDelim);
00210     void _writeIndices(std::ofstream& outFile, std::vector<size_t> & indexList, size_t posDelim);
00211 
00213     SoCallbackAction* _callbackAction;
00214     
00216     static SoCallbackAction::Response preCB(void *userData, 
00217                                             SoCallbackAction *action, 
00218                                             const SoNode *node);
00219 
00221     static void pointCB   (void* userData, SoCallbackAction* action,
00222                            const SoPrimitiveVertex* v);
00224     static void lineCB    (void* userData, SoCallbackAction* action,
00225                            const SoPrimitiveVertex* v1,
00226                            const SoPrimitiveVertex* v2);
00228     static void triangleCB(void* userData, SoCallbackAction* action,
00229                            const SoPrimitiveVertex* v1,
00230                            const SoPrimitiveVertex* v2,
00231                            const SoPrimitiveVertex* v3);
00232 
00234     typedef std::vector<COLORED_POINT> pointsVec;
00236     typedef std::vector<COLORED_POINT> linesVec;
00238     typedef std::vector<COLORED_POINT> trianglesVec;
00239 
00240 
00242     std::vector<pointsVec> _points;
00244     std::vector<linesVec> _lines;
00246     std::vector<trianglesVec> _triangles;
00247 #endif
00248 
00249 };
00250 
00251 #endif