contrib/brl/bbas/bgui3d/bgui3d_file_io.cxx
Go to the documentation of this file.
00001 // This is brl/bbas/bgui3d/bgui3d_file_io.cxx
00002 #include "bgui3d_file_io.h"
00003 //:
00004 // \file
00005 
00006 #include <vcl_iostream.h>
00007 
00008 #include <Inventor/nodes/SoNode.h>
00009 #include <Inventor/nodes/SoSeparator.h>
00010 #include <Inventor/SoOutput.h>
00011 #include <Inventor/SoInput.h>
00012 #include <Inventor/actions/SoWriteAction.h>
00013 #include <Inventor/actions/SoToVRMLAction.h>
00014 #include <Inventor/actions/SoToVRML2Action.h>
00015 #include <Inventor/VRMLnodes/SoVRMLGroup.h>
00016 
00017 
00018 //: Export the scene as IV
00019 void
00020 bgui3d_export_iv( SoNode* scene_root, const vcl_string& filename )
00021 {
00022   SoOutput out;
00023   out.openFile(filename.c_str());
00024 
00025   SoWriteAction wra(&out);
00026   wra.apply(scene_root);
00027 
00028   out.closeFile();
00029 }
00030 
00031 
00032 //: Export the scene as VRML
00033 void
00034 bgui3d_export_vrml(SoNode* scene_root, const vcl_string& filename)
00035 {
00036   SoOutput out;
00037   out.openFile(filename.c_str());
00038   out.setHeaderString("#VRML V1.0 utf8");
00039 
00040   SoToVRMLAction to_vrml;
00041   to_vrml.apply(scene_root);
00042   SoNode *vrml_root = to_vrml.getVRMLSceneGraph();
00043   vrml_root->ref();
00044 
00045   SoWriteAction wra(&out);
00046   wra.apply(vrml_root);
00047 
00048   out.closeFile();
00049   vrml_root->unref();
00050 }
00051 
00052 
00053 //: Export the scene as VRML 2.0
00054 void
00055 bgui3d_export_vrml2(SoNode* scene_root, const vcl_string& filename)
00056 {
00057   SoOutput out;
00058   out.openFile(filename.c_str());
00059   out.setHeaderString("#VRML V2.0 utf8");
00060 
00061   SoToVRML2Action to_vrml2;
00062   to_vrml2.apply(scene_root);
00063   SoNode *vrml2_root = to_vrml2.getVRML2SceneGraph();
00064   vrml2_root->ref();
00065 
00066   SoWriteAction wra(&out);
00067   wra.apply(vrml2_root);
00068 
00069   out.closeFile();
00070   vrml2_root->unref();
00071 }
00072 
00073 
00074 SoNode* bgui3d_import_file(const vcl_string& filename, vcl_ostream& os)
00075 {
00076   // Open the input file
00077   SoInput mySceneInput;
00078   if (!mySceneInput.openFile(filename.c_str())) {
00079     os << "Cannot open file "<<filename<<'\n';
00080     return NULL;
00081   }
00082 
00083   // Read the whole file into a scenegraph
00084   SoSeparator *myScene = SoDB::readAll(&mySceneInput);
00085   if (myScene == NULL) {
00086     os << "Problem reading file "<< filename << '\n';
00087     return NULL;
00088   }
00089   if (mySceneInput.isFileVRML1())
00090     os << "Read "<< filename << " as a VRML 1.0 File.\n";
00091   else if (mySceneInput.isFileVRML2())
00092     os << "Read "<< filename << " as a VRML 2.0 (VRML97) File.\n";
00093   else
00094     os << "Read "<< filename << " as an IV File.\n";
00095 
00096   mySceneInput.closeFile();
00097   return myScene;
00098 }
00099 
00100 
00101 #if 0 // Ming: temp test file.
00102   #include <Inventor/SoDB.h>
00103   #include <Inventor/SoInteraction.h>
00104   #include <Inventor/SoInput.h>
00105   #include <Inventor/SoOutput.h>
00106   #include <Inventor/actions/SoWriteAction.h>
00107   #include <Inventor/actions/SoToVRML2Action.h>
00108   #include <Inventor/nodes/SoSeparator.h>
00109   #include <Inventor/VRMLnodes/SoVRMLGroup.h>
00110 
00111   int testVRML (int argc, char *argv[])
00112   {
00113     SoDB::init();
00114     SoInteraction::init();
00115     SoInput in;
00116     in.openFile(argv[1]);
00117     vcl_cout << "Reading...\n"
00118     SoSeparator *root = SoDB::readAll(&in);
00119 
00120     if (root) {
00121       root->ref();
00122       SbString hdr = in.getHeader();
00123       in.closeFile();
00124 
00125       vcl_cout << "Converting...\n"
00126       SoToVRML2Action tovrml2;
00127       tovrml2.apply(root);
00128       SoVRMLGroup *newroot = tovrml2.getVRML2SceneGraph();
00129       newroot->ref();
00130       root->unref();
00131 
00132       vcl_cout << "Writing...\n"
00133 
00134       SoOutput out;
00135       out.openFile("out.wrl");
00136       out.setHeaderString("#VRML V2.0 utf8");
00137       SoWriteAction wra(&out);
00138       wra.apply(newroot);
00139       out.closeFile();
00140 
00141       newroot->unref();
00142     }
00143 
00144     return 0;
00145   }
00146 #endif // 0