hrplib/hrpUtil/VrmlWriter.cpp
Go to the documentation of this file.
1 
5 #include "VrmlWriter.h"
6 
7 
8 using namespace std;
9 using namespace boost;
10 using namespace hrp;
11 
12 
13 
14 VrmlWriter::TNodeMethodMap VrmlWriter::nodeMethodMap;
15 
16 
17 std::ostream& operator<<(std::ostream& out, VrmlWriter::TIndent& indent)
18 {
19  return out << indent.spaces;
20 }
21 
22 inline const char* boolstr(bool v)
23 {
24  if(v){
25  return "TRUE";
26  } else {
27  return "FALSE";
28  }
29 }
30 
31 ostream& operator<<(std::ostream& out, SFVec3f& v)
32 {
33  return out << v[0] << " " << v[1] << " " << v[2];
34 }
35 
36 
37 ostream& operator<<(std::ostream& out, SFVec4f& v)
38 {
39  return out << v[0] << " " << v[1] << " " << v[2] << " " << v[3];
40 }
41 
42 
43 template <class MFValues> void VrmlWriter::writeMFValues(MFValues values, int numColumn)
44 {
45  out << ++indent << "[\n";
46  ++indent;
47 
48  out << indent;
49  int col = 0;
50  int n = values.size();
51  for(int i=0; i < n; i++){
52  out << values[i] << " ";
53  col++;
54  if(col == numColumn){
55  col = 0;
56  out << "\n";
57  if(i < n-1){
58  out << indent;
59  }
60  }
61  }
62 
63  out << --indent << "]\n";
64  --indent;
65 }
66 
67 
68 void VrmlWriter::writeMFInt32SeparatedByMinusValue(MFInt32& values)
69 {
70  out << ++indent << "[\n";
71  ++indent;
72 
73  out << indent;
74  int n = values.size();
75  for(int i=0; i < n; i++){
76  out << values[i] << " ";
77  if(values[i] < 0){
78  out << "\n";
79  if(i < n-1){
80  out << indent;
81  }
82  }
83  }
84 
85  out << --indent << "]\n";
86  --indent;
87 }
88 
89 
90 VrmlWriter::VrmlWriter(std::ostream& out) : out(out)
91 {
92  if(nodeMethodMap.empty()){
94  }
95 
96 }
97 
98 
100 {
105 }
106 
107 
109 {
110  TNodeMethodMap::iterator p = nodeMethodMap.find(typeid(*node).name());
111  if(p != nodeMethodMap.end()){
112  return p->second;
113  } else {
114  return 0;
115  }
116 }
117 
119 {
120  out << "#VRML V2.0 utf8\n";
121 }
122 
123 
125 {
126  indent.clear();
127  out << "\n";
128  writeNodeIter(node);
129  return true;
130 }
131 
132 
134 {
136  if(method){
137  (this->*method)(node);
138  }
139 }
140 
141 
142 void VrmlWriter::beginNode(const char* nodename, VrmlNodePtr node)
143 {
144  out << indent;
145  if(node->defName.empty()){
146  out << nodename << " {\n";
147  } else {
148  out << "DEF " << node->defName << " " << nodename << " {\n";
149  }
150  ++indent;
151 }
152 
153 
155 {
156  out << --indent << "}\n";
157 }
158 
159 
161 {
162  VrmlGroupPtr group = static_pointer_cast<VrmlGroup>(node);
163 
164  beginNode("Group", group);
165  writeGroupFields(group);
166  endNode();
167 }
168 
169 
171 {
172  if(group->bboxSize[0] >= 0){
173  out << indent << "bboxCenter " << group->bboxCenter << "\n";
174  out << indent << "bboxSize " << group->bboxSize << "\n";
175  }
176 
177  if(!group->children.empty()){
178  out << indent << "children [\n";
179  ++indent;
180  for(size_t i=0; i < group->children.size(); i++){
181  writeNodeIter(group->children[i]);
182  }
183  out << --indent << "]\n";
184  }
185 }
186 
187 
189 {
190  VrmlTransformPtr trans = static_pointer_cast<VrmlTransform>(node);
191 
192  beginNode("Transform", trans);
193 
194  out << indent << "center " << trans->center << "\n";
195  out << indent << "rotation " << trans->rotation << "\n";
196  out << indent << "scale " << trans->scale << "\n";
197  out << indent << "scaleOrientation " << trans->scaleOrientation << "\n";
198  out << indent << "translation " << trans->translation << "\n";
199 
200  writeGroupFields(trans);
201 
202  endNode();
203 }
204 
205 
207 {
208  VrmlShapePtr shape = static_pointer_cast<VrmlShape>(node);
209 
210  beginNode("Shape", shape);
211 
212  if(shape->appearance){
213  out << indent << "appearance\n";
214  ++indent;
215  writeAppearanceNode(shape->appearance);
216  --indent;
217  }
218  if(shape->geometry){
219  out << indent << "geometry\n";
220  VrmlWriterNodeMethod method = getNodeMethod(shape->geometry);
221  if(method){
222  ++indent;
223  (this->*method)(shape->geometry);
224  --indent;
225  }
226  }
227 
228  endNode();
229 }
230 
231 
233 {
234  beginNode("Appearance", appearance);
235 
236  if(appearance->material){
237  out << indent << "material\n";
238  ++indent;
239  writeMaterialNode(appearance->material);
240  --indent;
241  }
242 
243  endNode();
244 }
245 
246 
248 {
249  beginNode("Material", material);
250 
251  out << indent << "ambientIntensity " << material->ambientIntensity << "\n";
252  out << indent << "diffuseColor " << material->diffuseColor << "\n";
253  out << indent << "emissiveColor " << material->emissiveColor << "\n";
254  out << indent << "shininess " << material->shininess << "\n";
255  out << indent << "specularColor " << material->specularColor << "\n";
256  out << indent << "transparency " << material->transparency << "\n";
257 
258  endNode();
259 }
260 
261 
263 {
264  VrmlIndexedFaceSetPtr faceset = static_pointer_cast<VrmlIndexedFaceSet>(node);
265 
266  beginNode("IndexedFaceSet", faceset);
267 
268  if(faceset->coord){
269  out << indent << "coord\n";
270  ++indent;
271  writeCoordinateNode(faceset->coord);
272  --indent;
273  }
274  if(!faceset->coordIndex.empty()){
275  out << indent << "coordIndex\n";
276  writeMFInt32SeparatedByMinusValue(faceset->coordIndex);
277  }
278 
279  out << indent << "ccw " << boolstr(faceset->ccw) << "\n";
280  out << indent << "convex " << boolstr(faceset->convex) << "\n";
281  out << indent << "creaseAngle " << faceset->creaseAngle << "\n";
282  out << indent << "solid " << boolstr(faceset->solid) << "\n";
283 
284  endNode();
285 }
286 
287 
289 {
290  beginNode("Coordinate", coord);
291 
292  if(!coord->point.empty()){
293  out << indent << "point\n";
294  writeMFValues(coord->point, 1);
295  }
296 
297  endNode();
298 }
Matrix33 trans(const Matrix33 &m)
Definition: Tvmet2Eigen.h:18
Modifications controlling boost library behavior.
Definition: ColladaUtil.h:306
void writeGroupNode(VrmlNodePtr node)
boost::intrusive_ptr< VrmlTransform > VrmlTransformPtr
Definition: VrmlNodes.h:265
boost::intrusive_ptr< VrmlGroup > VrmlGroupPtr
Definition: VrmlNodes.h:250
VRML Shape node.
Definition: VrmlNodes.h:285
void beginNode(const char *nodename, VrmlNodePtr node)
bool writeNode(VrmlNodePtr node)
static TNodeMethodMap nodeMethodMap
VRML Group node.
Definition: VrmlNodes.h:236
VRML IndexedFaseSet node.
Definition: VrmlNodes.h:483
png_uint_32 i
Definition: png.h:2735
void writeNodeIter(VrmlNodePtr node)
boost::intrusive_ptr< VrmlShape > VrmlShapePtr
Definition: VrmlNodes.h:292
void writeShapeNode(VrmlNodePtr node)
void writeCoordinateNode(VrmlCoordinatePtr coord)
void writeIndexedFaceSetNode(VrmlNodePtr node)
void(VrmlWriter::* VrmlWriterNodeMethod)(VrmlNodePtr node)
int method
Definition: png.h:1847
void writeAppearanceNode(VrmlAppearancePtr appearance)
static void registerNodeMethodMap()
boost::intrusive_ptr< VrmlAppearance > VrmlAppearancePtr
Definition: VrmlNodes.h:277
const char * boolstr(bool v)
void writeMFInt32SeparatedByMinusValue(MFInt32 &values)
std::ostream & operator<<(std::ostream &out, VrmlWriter::TIndent &indent)
boost::intrusive_ptr< VrmlNode > VrmlNodePtr
Definition: VrmlNodes.h:155
void writeMFValues(MFValues values, int numColumn)
std::string indent(int index)
boost::array< SFFloat, 3 > SFVec3f
Definition: VrmlNodes.h:55
boost::intrusive_ptr< VrmlIndexedFaceSet > VrmlIndexedFaceSetPtr
Definition: VrmlNodes.h:498
static void registNodeMethod(const std::type_info &t, VrmlWriterNodeMethod method)
static VrmlWriterNodeMethod getNodeMethod(VrmlNodePtr node)
boost::intrusive_ptr< VrmlCoordinate > VrmlCoordinatePtr
Definition: VrmlNodes.h:457
VRML Transform node.
Definition: VrmlNodes.h:254
void writeTransformNode(VrmlNodePtr node)
void writeMaterialNode(VrmlMaterialPtr material)
std::vector< SFInt32 > MFInt32
Definition: VrmlNodes.h:69
void writeGroupFields(VrmlGroupPtr group)
boost::array< SFFloat, 4 > SFVec4f
Definition: VrmlNodes.h:56
boost::intrusive_ptr< VrmlMaterial > VrmlMaterialPtr
Definition: VrmlNodes.h:295


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat Apr 13 2019 02:14:26