00001 #ifndef __MATH_XML_H__
00002 #define __MATH_XML_H__
00003
00004 #include <tinyxml2.h>
00005
00006 #include <math/quat.hpp>
00007 #include <math/vec4.hpp>
00008 #include <math/transform.hpp>
00009 #include <math/color.hpp>
00010
00011 namespace math {
00012 #ifdef TINYXML
00013 int xml_parse_int(tinyxml2::XMLElement* element, int v) {
00014
00015 if(!element) return v;
00016
00017 char const * buf = element->GetText();
00018
00019 v = atoi(buf);
00020
00021 return v;
00022 }
00023 float math::xml_parse_float(tinyxml2::XMLElement* element, float f) {
00024
00025 if(element == NULL) return f;
00026
00027 char const * buf = element->GetText();
00028
00029 int c = sscanf(buf, "%f", &f);
00030 assert(c==1);
00031
00032 return f;
00033 }
00034 template<typename T> math::quat<T> math::xml_parse_quat(tinyxml2::XMLElement* element, math::quat<T> q) {
00035
00036 if(element == NULL) return q;
00037
00038 math::vec3<double> v = xml_parse_vec3(element->FirstChildElement("v"), math::vec3<double>(1.0,0.0,0.0));
00039 float a = xml_parse_float(element->FirstChildElement("a"), 0.0);
00040
00041 return math::quat<T>(a, v);
00042 }
00043 template<typename T> math::vec3<T> math::xml_parse_vec3(tinyxml2::XMLElement* element, math::vec3<double> v) {
00044
00045 if( !element )
00046 {
00047
00048 return v;
00049 }
00050
00051 float x, y, z;
00052
00053 char const * buf = element->GetText();
00054
00055 int c = sscanf(buf, "%f,%f,%f", &x, &y, &z);
00056 assert(c==3);
00057
00058 return math::vec3<double>(x,y,z);
00059 }
00060 template<typename T> math::transform<T> math::xml_parse_transform(tinyxml2::XMLElement* element, math::transform<T> pose) {
00061
00062 if(element == NULL) return pose;
00063
00064 pose.p = xml_parse_vec3(element->FirstChildElement("p"), math::vec3<T>(0.0,0.0,0.0));
00065 pose.q = xml_parse_quat(element->FirstChildElement("q"), math::quat<T>(0.0, math::vec3<T>(1.0,0.0,0.0)));
00066
00067 return pose;
00068 }
00069 template<typename T> math::color<T> math::xml_parse_color(tinyxml2::XMLElement* element, math::color<T> color) {
00070
00071 if(element == NULL) return color;
00072
00073 float r,g,b,a;
00074
00075 char const * buf = NULL;
00076
00077 buf = element->GetText();
00078 assert(buf);
00079
00080 if(strcmp(buf, "red") == 0)
00081 {
00082 color = math::red;
00083 }
00084 else if(strcmp(buf, "green") == 0)
00085 {
00086 color = math::green;
00087 }
00088 else if(strcmp(buf, "blue") == 0)
00089 {
00090 color = math::blue;
00091 }
00092 else if(strcmp(buf, "cyan") == 0)
00093 {
00094 color = math::cyan;
00095 }
00096 else if(strcmp(buf, "magenta") == 0)
00097 {
00098 color = math::magenta;
00099 }
00100 else if(strcmp(buf, "yellow") == 0)
00101 {
00102 color = math::yellow;
00103 }
00104 else if(strcmp(buf, "white") == 0)
00105 {
00106 color = math::white;
00107 }
00108 else if(strcmp(buf, "black") == 0)
00109 {
00110 color = math::black;
00111 }
00112 else if(strcmp(buf, "rand") == 0) {
00113 color = math::color<T>::rand();
00114 }
00115 else if(strcmp(buf, "sine") == 0) {
00116 printf("sine color\n");
00117 color = math::color<T>(0.25, 0.5, 1.0, 1.0,
00118 math::color<T>::type::e::SINE,
00119 math::color<T>::type::e::SINE,
00120 math::color<T>::type::e::SINE);
00121 }
00122 else if(strcmp(buf, "saw") == 0) {
00123 printf("saw color\n");
00124 color = math::color<T>(0.2, 0.5, 1.0, 1.0,
00125 math::color<T>::type::e::SAW,
00126 math::color<T>::type::e::SAW,
00127 math::color<T>::type::e::SAW);
00128 }
00129 else
00130 {
00131 int c = sscanf(buf, "%f,%f,%f,%f", &r, &g, &b, &a);
00132 if(c != 4)
00133 {
00134 printf("invalid color '%s'\n", buf);
00135 abort();
00136 }
00137 color = math::color<T>(r,g,b,a);
00138 }
00139
00140 return color;
00141 }
00142 template<typename T> math::vec4<T> math::xml_parse_vec4(tinyxml2::XMLElement* element) {
00143
00144 if( !element )
00145 {
00146 printf("element not found\n");
00147 exit(0);
00148 return math::vec4<T>(0,0,0,0);
00149 }
00150
00151 float r,g,b,a;
00152
00153 char const * buf = element->GetText();
00154
00155 int c = sscanf(buf, "%f,%f,%f,%f", &r, &g, &b, &a);
00156 assert(c==4);
00157
00158 return math::vec4<T>(r,g,b,a);
00159 }
00160 #endif
00161
00162
00163 float xml_parse_float(tinyxml2::XMLElement*, float = 0.0);
00164 int xml_parse_int(tinyxml2::XMLElement*, int);
00165 template <typename T> math::quat<T> xml_parse_quat(tinyxml2::XMLElement*, math::quat<T>);
00166 template <typename T> math::vec3<T> xml_parse_vec3(tinyxml2::XMLElement*, math::vec3<T>);
00167 template <typename T> math::vec4<T> xml_parse_vec4(tinyxml2::XMLElement*);
00168 template <typename T> math::transform<T> xml_parse_transform(tinyxml2::XMLElement*, math::transform<T>());
00169 template <typename T> math::color<T> xml_parse_color(tinyxml2::XMLElement* element, math::color<T> = math::black);
00170 }
00171
00172 #endif
00173
00174
00175
00176
00177
00178