00001 #include "X3DBaseTypes.h" 00002 #include <stdio.h> 00003 #include <sstream> 00004 #include <cmath> 00005 00006 using namespace X3DTK; 00007 using namespace std; 00008 00009 class SFStringStream 00010 { 00011 public: 00012 SFStringStream(const SFString &s); 00013 float getNextFloat(); 00014 int getNextInt(); 00015 00016 inline bool end() {return isEnd;}; 00017 inline float toFloat(char c) 00018 { 00019 switch(c) 00020 { 00021 case '0': 00022 return 0.0f; 00023 case '1': 00024 return 1.0f; 00025 case '2': 00026 return 2.0f; 00027 case '3': 00028 return 3.0f; 00029 case '4': 00030 return 4.0f; 00031 case '5': 00032 return 5.0f; 00033 case '6': 00034 return 6.0f; 00035 case '7': 00036 return 7.0f; 00037 case '8': 00038 return 8.0f; 00039 case '9': 00040 return 9.0f; 00041 default: 00042 return 0.0f; 00043 } 00044 } 00045 00046 inline int toInt(char c) 00047 { 00048 switch(c) 00049 { 00050 case '0': 00051 return 0; 00052 case '1': 00053 return 1; 00054 case '2': 00055 return 2; 00056 case '3': 00057 return 3; 00058 case '4': 00059 return 4; 00060 case '5': 00061 return 5; 00062 case '6': 00063 return 6; 00064 case '7': 00065 return 7; 00066 case '8': 00067 return 8; 00068 case '9': 00069 return 9; 00070 default: 00071 return 0; 00072 } 00073 } 00074 00075 private: 00076 const SFString &s; 00077 string::const_iterator itChar; 00078 bool isEnd; 00079 }; 00080 00081 SFStringStream::SFStringStream(const SFString &ss) 00082 : s(ss), isEnd(false) 00083 { 00084 itChar = s.begin(); 00085 00086 while ((itChar != s.end()) 00087 && ((*itChar == ' ') || (*itChar == ','))) 00088 ++itChar; 00089 00090 isEnd = (itChar == s.end()); 00091 } 00092 00093 float SFStringStream::getNextFloat() 00094 { 00095 bool sign; 00096 if (*itChar == '-') 00097 { 00098 sign = false; 00099 ++itChar; 00100 } 00101 else 00102 { 00103 sign = true; 00104 if (*itChar == '+') 00105 ++itChar; 00106 } 00107 00108 float f = 0.0f; 00109 00110 while ((*itChar != ' ') 00111 && (*itChar != ',') 00112 && (*itChar != '.') 00113 && (*itChar != 'e') 00114 && (*itChar != 'E') 00115 && (itChar != s.end())) 00116 { 00117 f = 10.0f*f + toFloat(*itChar); 00118 ++itChar; 00119 } 00120 00121 float n = 0.1f; 00122 if (*itChar == '.') 00123 { 00124 ++itChar; 00125 while ((*itChar != ' ') 00126 && (*itChar != ',') 00127 && (*itChar != 'e') 00128 && (*itChar != 'E') 00129 && (itChar != s.end())) 00130 { 00131 f += n*toFloat(*itChar); 00132 n *= 0.1f; 00133 ++itChar; 00134 } 00135 } 00136 00137 if ((*itChar == 'e') 00138 || (*itChar == 'E')) 00139 { 00140 ++itChar; 00141 bool esign; 00142 if (*itChar == '-') 00143 { 00144 esign = false; 00145 ++itChar; 00146 } 00147 else 00148 { 00149 esign = true; 00150 if (*itChar == '+') 00151 ++itChar; 00152 } 00153 00154 int exp = 0; 00155 while ((*itChar != ' ') 00156 && (*itChar != ',') 00157 && (itChar != s.end())) 00158 { 00159 exp = 10*exp + toInt(*itChar); 00160 ++itChar; 00161 } 00162 00163 if (esign) 00164 f = f*pow(10.0f, exp); 00165 else 00166 f = f*pow(10.0f, -exp); 00167 } 00168 00169 if (!sign) 00170 f = -f; 00171 00172 while (((*itChar == ' ') || (*itChar == ',')) 00173 && (itChar != s.end())) 00174 ++itChar; 00175 00176 isEnd = (itChar == s.end()); 00177 00178 return f; 00179 } 00180 00181 int SFStringStream::getNextInt() 00182 { 00183 bool sign; 00184 if (*itChar == '-') 00185 { 00186 sign = false; 00187 ++itChar; 00188 } 00189 else 00190 { 00191 sign = true; 00192 if (*itChar == '+') 00193 ++itChar; 00194 } 00195 00196 int i = 0; 00197 00198 while ((*itChar != ' ') 00199 && (*itChar != ',') 00200 && (*itChar != '.') 00201 && (*itChar != 'e') 00202 && (*itChar != 'E') 00203 && (itChar != s.end())) 00204 { 00205 i = 10*i + toInt(*itChar); 00206 ++itChar; 00207 } 00208 00209 if ((*itChar == 'e') 00210 || (*itChar == 'E')) 00211 { 00212 ++itChar; 00213 if (*itChar == '+') 00214 ++itChar; 00215 00216 int exp = 0; 00217 while ((*itChar != ' ') 00218 && (*itChar != ',') 00219 && (itChar != s.end())) 00220 { 00221 exp = 10*exp + toInt(*itChar); 00222 ++itChar; 00223 } 00224 00225 i = i*static_cast<int>(pow(10.0f, exp)); 00226 } 00227 00228 if (!sign) 00229 i = -i; 00230 00231 while (((*itChar == ' ') || (*itChar == ',')) 00232 && (itChar != s.end())) 00233 ++itChar; 00234 00235 isEnd = (itChar == s.end()); 00236 00237 return i; 00238 } 00239 00240 MFString::MFString(const SFString &s) 00241 { 00242 string token; 00243 string::const_iterator itChar = s.begin(); 00244 size_t counter = 0; 00245 size_t begin = 0; 00246 size_t size = 0; 00247 00248 while (itChar != s.end()) 00249 { 00250 if (*itChar == '\"') 00251 { 00252 ++itChar; 00253 ++counter; 00254 begin = counter; 00255 size = 0; 00256 00257 while ((*itChar != '\"') && (itChar != s.end())) 00258 { 00259 ++itChar; 00260 ++counter; 00261 ++size; 00262 } 00263 token = s.substr(begin, size); 00264 this->push_back(token); 00265 } 00266 ++itChar; 00267 ++counter; 00268 } 00269 00270 } 00271 00272 MFBool::MFBool(const SFString &s) 00273 { 00274 } 00275 00276 MFInt32::MFInt32(size_type n, const SFInt32 &V) 00277 : vector<SFInt32>(n, V) 00278 { 00279 } 00280 00281 MFInt32::MFInt32(const SFString &s) 00282 { 00283 SFStringStream ss(s); 00284 00285 while (!ss.end()) 00286 push_back(ss.getNextInt()); 00287 } 00288 00289 MFFloat::MFFloat(size_type n, const SFFloat &V) 00290 : vector<SFFloat>(n, V) 00291 { 00292 } 00293 00294 MFFloat::MFFloat(const SFString &s) 00295 { 00296 SFStringStream ss(s); 00297 00298 while (!ss.end()) 00299 push_back(ss.getNextFloat()); 00300 00301 } 00302 00303 MFDouble::MFDouble(const SFString &s) 00304 { 00305 } 00306 00307 SFColorRGBA::SFColorRGBA(SFFloat r, SFFloat g, SFFloat b, SFFloat a) 00308 { 00309 this->r = r; 00310 this->g = g; 00311 this->b = b; 00312 this->a = a; 00313 } 00314 00315 bool X3DTK::operator== (const SFColorRGBA &r1, const SFColorRGBA &r2) 00316 { 00317 return ((r1.r == r2.r) && (r1.g == r2.g) && (r1.b == r2.b) && (r1.a == r2.a)); 00318 } 00319 00320 bool X3DTK::operator!= (const SFColorRGBA &r1, const SFColorRGBA &r2) 00321 { 00322 return ((r1.r != r2.r) || (r1.g != r2.g) || (r1.b != r2.b) || (r1.a != r2.a)); 00323 } 00324 00325 00326 SFColorRGBA::SFColorRGBA(const SFString &s) 00327 { 00328 SFStringStream ss(s); 00329 00330 r = ss.getNextFloat(); 00331 g = ss.getNextFloat(); 00332 b = ss.getNextFloat(); 00333 a = ss.getNextFloat(); 00334 } 00335 00336 SFString toSFString(const SFColorRGBA c) 00337 { 00338 return SFString::number(c.r) + SFString(" ") 00339 + SFString::number(c.g) + SFString(" ") 00340 + SFString::number(c.b) + SFString(" ") 00341 + SFString::number(c.a); 00342 } 00343 00344 MFColorRGBA::MFColorRGBA(size_type n, const SFColorRGBA &C) 00345 : vector<SFColorRGBA>(n, C) 00346 { 00347 } 00348 00349 MFColorRGBA::MFColorRGBA(const SFString &s) 00350 { 00351 SFStringStream ss(s); 00352 00353 while (!ss.end()) 00354 { 00355 SFColorRGBA c; 00356 c.r = ss.getNextFloat(); 00357 c.g = ss.getNextFloat(); 00358 c.b = ss.getNextFloat(); 00359 c.a = ss.getNextFloat(); 00360 00361 push_back(c); 00362 } 00363 } 00364 00365 SFColor::SFColor(SFFloat r, SFFloat g, SFFloat b) 00366 { 00367 this->r = r; 00368 this->g = g; 00369 this->b = b; 00370 }; 00371 00372 bool X3DTK::operator== (const SFColor &r1, const SFColor &r2) 00373 { 00374 return ((r1.r == r2.r) && (r1.g == r2.g) && (r1.b == r2.b)); 00375 } 00376 00377 bool X3DTK::operator!= (const SFColor &r1, const SFColor &r2) 00378 { 00379 return ((r1.r != r2.r) || (r1.g != r2.g) || (r1.b != r2.b)); 00380 } 00381 00382 SFColor::SFColor(const SFString &s) 00383 { 00384 SFStringStream ss(s); 00385 00386 r = ss.getNextFloat(); 00387 g = ss.getNextFloat(); 00388 b = ss.getNextFloat(); 00389 } 00390 00391 SFColor::operator const SFColorRGBA() const 00392 { 00393 return SFColorRGBA(r, g, b, 1.0f); 00394 } 00395 00396 MFColor::MFColor(const SFString &s) 00397 { 00398 SFStringStream ss(s); 00399 00400 while (!ss.end()) 00401 { 00402 SFColor c; 00403 c.r = ss.getNextFloat(); 00404 c.g = ss.getNextFloat(); 00405 c.b = ss.getNextFloat(); 00406 00407 push_back(c); 00408 } 00409 } 00410 00411 MFColor::operator const MFColorRGBA() const 00412 { 00413 MFColorRGBA color = MFColorRGBA(this->size()); 00414 MFColorRGBA::iterator itColor = color.begin(); 00415 for (const_iterator it = this->begin(); it != this->end(); ++it) 00416 { 00417 *itColor = *it; 00418 ++itColor; 00419 } 00420 00421 return color; 00422 } 00423 00424 SFVec2d::SFVec2d() 00425 { 00426 } 00427 00428 SFVec2d::SFVec2d(SFDouble x, SFDouble y) 00429 { 00430 this->x = x; 00431 this->y = y; 00432 } 00433 00434 SFVec2d::SFVec2d(const SFString &s) 00435 { 00436 } 00437 00438 MFVec2d::MFVec2d(const SFString &s) 00439 { 00440 } 00441 00442 SFVec2f::SFVec2f() 00443 { 00444 } 00445 00446 SFVec2f::SFVec2f(SFFloat x, SFFloat y) 00447 { 00448 this->x = x; 00449 this->y = y; 00450 } 00451 00452 SFVec2f::SFVec2f(const SFString &s) 00453 { 00454 SFStringStream ss(s); 00455 00456 x = ss.getNextFloat(); 00457 y = ss.getNextFloat(); 00458 } 00459 00460 MFVec2f::MFVec2f(const SFString &s) 00461 { 00462 SFStringStream ss(s); 00463 00464 while (!ss.end()) 00465 { 00466 SFVec2f v; 00467 v.x = ss.getNextFloat(); 00468 v.y = ss.getNextFloat(); 00469 00470 push_back(v); 00471 } 00472 } 00473 00474 SFVec3d::SFVec3d() 00475 { 00476 } 00477 00478 00479 SFVec3d::SFVec3d(SFDouble x, SFDouble y, SFDouble z) 00480 { 00481 this->x = x; 00482 this->y = y; 00483 this->z = z; 00484 } 00485 00486 SFVec3d::SFVec3d(const SFString &s) 00487 { 00488 } 00489 00490 MFVec3d::MFVec3d(const SFString &s) 00491 { 00492 } 00493 00494 MFVec3f::MFVec3f(size_type n, const SFVec3f &V) 00495 : vector<SFVec3f>(n, V) 00496 { 00497 } 00498 00499 MFVec3f::MFVec3f(const SFString &s) 00500 : vector<SFVec3f>() 00501 { 00502 SFStringStream ss(s); 00503 00504 while (!ss.end()) 00505 { 00506 SFVec3f v; 00507 v.x = ss.getNextFloat(); 00508 v.y = ss.getNextFloat(); 00509 v.z = ss.getNextFloat(); 00510 00511 push_back(v); 00512 } 00513 } 00514 00515 SFRotation::SFRotation(SFFloat x, SFFloat y, SFFloat z, SFFloat angle) 00516 { 00517 this->x = x; 00518 this->y = y; 00519 this->z = z; 00520 this->angle = angle; 00521 } 00522 00523 SFRotation::SFRotation(const SFString &s) 00524 { 00525 SFStringStream ss(s); 00526 00527 x = ss.getNextFloat(); 00528 y = ss.getNextFloat(); 00529 z = ss.getNextFloat(); 00530 angle = ss.getNextFloat(); 00531 } 00532 00533 bool X3DTK::operator== (const SFRotation &r1, const SFRotation &r2) 00534 { 00535 return ((r1.x == r2.x) && (r1.y == r2.y) && (r1.z == r2.z)); 00536 } 00537 00538 bool X3DTK::operator!= (const SFRotation &r1, const SFRotation &r2) 00539 { 00540 return ((r1.x != r2.x) || (r1.y != r2.y) || (r1.z != r2.z)); 00541 } 00542 00543 MFRotation::MFRotation(const SFString &s) 00544 { 00545 SFStringStream ss(s); 00546 00547 while (!ss.end()) 00548 { 00549 SFRotation r; 00550 r.x = ss.getNextFloat(); 00551 r.y = ss.getNextFloat(); 00552 r.z = ss.getNextFloat(); 00553 r.angle = ss.getNextFloat(); 00554 00555 push_back(r); 00556 } 00557 } 00558 00559 std::ostream& X3DTK::operator<<(std::ostream& o, const MFString &ms) 00560 { 00561 if (!ms.empty()) 00562 { 00563 for (unsigned int i = 0; i < ms.size() - 1; ++i) 00564 o << ms[i] << " "; 00565 00566 o << ms.back(); 00567 } 00568 return o; 00569 } 00570 00571 std::ostream& X3DTK::operator<<(std::ostream& o, const MFBool &mb) 00572 { 00573 for (unsigned int i = 0; i < mb.size() - 1; ++i) 00574 { 00575 if (mb[i]) 00576 o << "TRUE, "; 00577 else 00578 o << "FALSE, "; 00579 } 00580 00581 if (!mb.empty()) 00582 { 00583 if (mb.back()) 00584 o << "TRUE"; 00585 else 00586 o << "FALSE"; 00587 } 00588 return o; 00589 } 00590 00591 std::ostream& X3DTK::operator<<(std::ostream& o, const MFInt32 &mi) 00592 { 00593 if (!mi.empty()) 00594 { 00595 for (unsigned int i = 0; i < mi.size() - 1; ++i) 00596 o << mi[i] << " "; 00597 00598 o << mi.back(); 00599 } 00600 return o; 00601 } 00602 00603 std::ostream& X3DTK::operator<<(std::ostream& o, const MFFloat &mf) 00604 { 00605 if (!mf.empty()) 00606 { 00607 for (unsigned int i = 0; i < mf.size() - 1; ++i) 00608 o << mf[i] << " "; 00609 00610 o << mf.back(); 00611 } 00612 return o; 00613 } 00614 00615 std::ostream& X3DTK::operator<<(std::ostream& o, const MFDouble &md) 00616 { 00617 if (!md.empty()) 00618 { 00619 for (unsigned int i = 0; i < md.size() - 1; ++i) 00620 o << md[i] << " "; 00621 00622 o << md.back(); 00623 } 00624 return o; 00625 } 00626 00627 std::ostream& X3DTK::operator<<(std::ostream& o, const SFColor &c) 00628 { 00629 return o << c.r << " " << c.g << " " << c.b; 00630 } 00631 00632 std::ostream& X3DTK::operator<<(std::ostream& o, const MFColor &mc) 00633 { 00634 if (!mc.empty()) 00635 { 00636 for (unsigned int i = 0; i < mc.size() - 1; ++i) 00637 o << mc[i] << ", "; 00638 00639 o << mc.back(); 00640 } 00641 return o; 00642 } 00643 00644 std::ostream& X3DTK::operator<<(std::ostream& o, const SFColorRGBA &c) 00645 { 00646 return o << c.r << " " << c.g << " " << c.b << " " << c.a; 00647 } 00648 00649 std::ostream& X3DTK::operator<<(std::ostream& o, const MFColorRGBA &mc) 00650 { 00651 if (!mc.empty()) 00652 { 00653 for (unsigned int i = 0; i < mc.size() - 1; ++i) 00654 o << mc[i] << ", "; 00655 00656 o << mc.back(); 00657 } 00658 return o; 00659 } 00660 00661 std::ostream& X3DTK::operator<<(std::ostream& o, const SFVec2d &v) 00662 { 00663 return o << v.x << " " << v.y ; 00664 } 00665 00666 std::ostream& X3DTK::operator<<(std::ostream& o, const MFVec2d &mv) 00667 { 00668 if (!mv.empty()) 00669 { 00670 for (unsigned int i = 0; i < mv.size() - 1; ++i) 00671 o << mv[i] << ", "; 00672 00673 o << mv.back(); 00674 } 00675 return o; 00676 } 00677 00678 std::ostream& X3DTK::operator<<(std::ostream& o, const SFVec2f &v) 00679 { 00680 return o << v.x << " " << v.y; 00681 } 00682 00683 std::ostream& X3DTK::operator<<(std::ostream& o, const MFVec2f &mv) 00684 { 00685 if (!mv.empty()) 00686 { 00687 for (unsigned int i = 0; i < mv.size() - 1; ++i) 00688 o << mv[i] << ", "; 00689 00690 o << mv.back(); 00691 } 00692 return o; 00693 } 00694 00695 std::ostream& X3DTK::operator<<(std::ostream& o, const SFVec3d &v) 00696 { 00697 return o << v.x << " " << v.y << " " << v.z; 00698 } 00699 00700 std::ostream& X3DTK::operator<<(std::ostream& o, const MFVec3d &mv) 00701 { 00702 if (!mv.empty()) 00703 { 00704 for (unsigned int i = 0; i < mv.size() - 1; ++i) 00705 o << mv[i] << ", "; 00706 00707 o << mv.back(); 00708 } 00709 return o; 00710 } 00711 00712 std::ostream& X3DTK::operator<<(std::ostream& o, const SFVec3f &v) 00713 { 00714 return o << v.x << " " << v.y << " " << v.z; 00715 } 00716 00717 std::ostream& X3DTK::operator<<(std::ostream& o, const MFVec3f &mv) 00718 { 00719 if (!mv.empty()) 00720 { 00721 for (unsigned int i = 0; i < mv.size() - 1; ++i) 00722 o << mv[i] << ", "; 00723 00724 o << mv.back(); 00725 } 00726 return o; 00727 } 00728 00729 std::ostream& X3DTK::operator<<(std::ostream& o, const SFPoint3f &v) 00730 { 00731 return o << v.x << " " << v.y << " " << v.z; 00732 } 00733 00734 std::ostream& X3DTK::operator<<(std::ostream& o, const SFRotation &r) 00735 { 00736 return o << r.x << " " << r.y << " " << r.z << " " << r.angle; 00737 } 00738 00739 std::ostream& X3DTK::operator<<(std::ostream& o, const MFRotation &mr) 00740 { 00741 if (!mr.empty()) 00742 { 00743 for (unsigned int i = 0; i < mr.size() - 1; ++i) 00744 o << mr[i] << ", "; 00745 00746 o << mr.back(); 00747 } 00748 return o; 00749 }