Choreonoid  1.5
VRMLWriter.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_UTIL_VRML_WRITER_H
7 #define CNOID_UTIL_VRML_WRITER_H
8 
9 #include "VRML.h"
10 #include <string>
11 #include <ostream>
12 #include "exportdecl.h"
13 
14 namespace cnoid {
15 
16 class VRMLWriter;
17 
18 struct TIndent {
19  void clear() { n = 0; spaces.resize(n); }
20  inline TIndent& operator++() { n += 2; spaces.resize(n, ' '); return *this; }
21  inline TIndent& operator--() {
22  n -= 2;
23  if(n < 0) { n = 0; }
24  spaces.resize(n, ' '); return *this;
25  }
26  std::string spaces;
27  int n;
28 };
29 
30 inline std::ostream& operator<<(std::ostream& out, TIndent& indent)
31 {
32  return out << indent.spaces;
33 }
34 
35 inline const char* boolstr(bool v)
36 {
37  if(v){
38  return "TRUE";
39  } else {
40  return "FALSE";
41  }
42 }
43 
44 inline std::ostream& operator<<(std::ostream& out, const SFVec2f& v)
45 {
46  return out << v[0] << " " << v[1];
47 }
48 
49 inline std::ostream& operator<<(std::ostream& out, const SFVec3f& v)
50 {
51  return out << v[0] << " " << v[1] << " " << v[2];
52 }
53 
54 inline std::ostream& operator<<(std::ostream& out, const SFColor& v)
55 {
56  return out << v[0] << " " << v[1] << " " << v[2];
57 }
58 
59 inline std::ostream& operator<<(std::ostream& out, const SFRotation& v)
60 {
61  const SFRotation::Vector3& a = v.axis();
62  return out << a[0] << " " << a[1] << " " << a[2] << " " << v.angle();
63 }
64 
66 
68 {
69 public:
70  VRMLWriter(std::ostream& out);
71 
72  void setOutFileName(const std::string& ofname) {
73  this->ofname = ofname;
74  };
75  void writeHeader();
76  bool writeNode(VRMLNodePtr node);
77 
78 protected:
79  std::ostream& out;
80  std::string ofname;
81 
83 
84  void registerNodeMethodMap();
85  void registerNodeMethod(const std::type_info& t, VRMLWriterNodeMethod method);
86  VRMLWriterNodeMethod getNodeMethod(VRMLNodePtr node);
87 
88  template <class MFValues> void writeMFValues(MFValues values, int numColumn) {
89  out << ++indent << "[\n";
90  ++indent;
91  out << indent;
92  int col = 0;
93  int n = values.size();
94  for(int i=0; i < n; i++){
95  out << values[i] << " ";
96  col++;
97  if(col == numColumn){
98  col = 0;
99  out << "\n";
100  if(i < n-1){
101  out << indent;
102  }
103  }
104  }
105  out << --indent << "]\n";
106  --indent;
107  };
108  void writeMFInt32SeparatedByMinusValue(MFInt32& values);
109  void writeNodeIter(VRMLNodePtr node);
110  void beginNode(const char* nodename, VRMLNodePtr node);
111  void endNode();
112  void writeGroupNode(VRMLNodePtr node);
113  void writeGroupFields(VRMLGroupPtr group);
114  void writeTransformNode(VRMLNodePtr node);
115 
116 private:
117  std::string abstorel(std::string& fname);
118  void writeInlineNode(VRMLNodePtr node);
119  void writeShapeNode(VRMLNodePtr node);
120  void writeAppearanceNode(VRMLAppearancePtr appearance);
121  void writeMaterialNode(VRMLMaterialPtr material);
122  void writeBoxNode(VRMLNodePtr node);
123  void writeConeNode(VRMLNodePtr node);
124  void writeCylinderNode(VRMLNodePtr node);
125  void writeSphereNode(VRMLNodePtr node);
126  void writeIndexedFaceSetNode(VRMLNodePtr node);
127  void writeCoordinateNode(VRMLCoordinatePtr coord);
128 
129 };
130 
131 };
132 
133 
134 #endif
Eigen::Vector3d SFVec3f
Definition: VRML.h:36
CNOID_EXPORT std::ostream & operator<<(std::ostream &os, const BoundingBox &bb)
Definition: BoundingBox.cpp:153
Eigen::Vector2d SFVec2f
Definition: VRML.h:34
std::string spaces
Definition: VRMLWriter.h:26
void setOutFileName(const std::string &ofname)
Definition: VRMLWriter.h:72
boost::intrusive_ptr< VRMLAppearance > VRMLAppearancePtr
Definition: VRML.h:268
int n
Definition: VRMLWriter.h:27
Eigen::Vector3f SFColor
Definition: VRML.h:38
std::vector< SFInt32 > MFInt32
Definition: VRML.h:48
Definition: VRMLWriter.h:67
Eigen::AngleAxisd SFRotation
Definition: VRML.h:39
TIndent indent
Definition: VRMLWriter.h:82
void(VRMLWriter::* VRMLWriterNodeMethod)(VRMLNodePtr node)
Definition: VRMLWriter.h:65
boost::intrusive_ptr< VRMLNode > VRMLNodePtr
Definition: VRML.h:132
TIndent & operator--()
Definition: VRMLWriter.h:21
void clear()
Definition: VRMLWriter.h:19
boost::intrusive_ptr< VRMLGroup > VRMLGroupPtr
Definition: VRML.h:229
Defines the minimum processing for performing pasing file for STL.
Definition: AbstractSceneLoader.h:9
const char * boolstr(bool v)
Definition: VRMLWriter.h:35
Definition: VRMLWriter.h:18
TIndent & operator++()
Definition: VRMLWriter.h:20
std::ostream & out
Definition: VRMLWriter.h:79
boost::intrusive_ptr< VRMLCoordinate > VRMLCoordinatePtr
Definition: VRML.h:449
Eigen::Vector3d Vector3
Definition: EigenTypes.h:58
#define CNOID_EXPORT
Definition: Util/exportdecl.h:37
boost::intrusive_ptr< VRMLMaterial > VRMLMaterialPtr
Definition: VRML.h:286
void writeMFValues(MFValues values, int numColumn)
Definition: VRMLWriter.h:88
std::string ofname
Definition: VRMLWriter.h:80