00001 /* 00002 * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc. 00003 * All rights reserved. This program is made available under the terms of the 00004 * Eclipse Public License v1.0 which accompanies this distribution, and is 00005 * available at http://www.eclipse.org/legal/epl-v10.html 00006 * Contributors: 00007 * National Institute of Advanced Industrial Science and Technology (AIST) 00008 */ 00009 00014 #ifndef OPENHRP_UTIL_VRML_WRITER_INCLUDED 00015 #define OPENHRP_UTIL_VRML_WRITER_INCLUDED 00016 00017 #include "config.h" 00018 #include "VrmlNodes.h" 00019 00020 #include <map> 00021 #include <string> 00022 #include <ostream> 00023 00024 00025 namespace hrp { 00026 00027 class VrmlWriter; 00028 00029 typedef void (VrmlWriter::*VrmlWriterNodeMethod)(VrmlNodePtr node); 00030 00031 00032 class HRP_UTIL_EXPORT VrmlWriter 00033 { 00034 public: 00035 VrmlWriter(std::ostream& out); 00036 00037 void writeHeader(); 00038 bool writeNode(VrmlNodePtr node); 00039 00040 struct TIndent { 00041 void clear() { n = 0; spaces.resize(n); } 00042 TIndent& operator++() { n += 2; spaces.resize(n, ' '); return *this; } 00043 TIndent& operator--() { 00044 n -= 2; 00045 if(n < 0) { n = 0; } 00046 spaces.resize(n, ' '); return *this; 00047 } 00048 std::string spaces; 00049 int n; 00050 }; 00051 00052 private: 00053 std::ostream& out; 00054 00055 TIndent indent; 00056 00057 typedef std::map<std::string, VrmlWriterNodeMethod> TNodeMethodMap; 00058 typedef std::pair<std::string, VrmlWriterNodeMethod> TNodeMethodPair; 00059 00060 static TNodeMethodMap nodeMethodMap; 00061 00062 static void registNodeMethod(const std::type_info& t, VrmlWriterNodeMethod method) { 00063 nodeMethodMap.insert(TNodeMethodPair(t.name(), method)); 00064 } 00065 static VrmlWriterNodeMethod getNodeMethod(VrmlNodePtr node); 00066 00067 static void registerNodeMethodMap(); 00068 00069 template <class MFValues> void writeMFValues(MFValues values, int numColumn); 00070 void writeMFInt32SeparatedByMinusValue(MFInt32& values); 00071 00072 void writeNodeIter(VrmlNodePtr node); 00073 void beginNode(const char* nodename, VrmlNodePtr node); 00074 void endNode(); 00075 void writeGroupNode(VrmlNodePtr node); 00076 void writeGroupFields(VrmlGroupPtr group); 00077 void writeTransformNode(VrmlNodePtr node); 00078 void writeShapeNode(VrmlNodePtr node); 00079 void writeAppearanceNode(VrmlAppearancePtr appearance); 00080 void writeMaterialNode(VrmlMaterialPtr material); 00081 void writeIndexedFaceSetNode(VrmlNodePtr node); 00082 void writeCoordinateNode(VrmlCoordinatePtr coord); 00083 00084 }; 00085 00086 }; 00087 00088 00089 #endif