30 #ifndef __X3DFIELDCONVERSION_H__
31 #define __X3DFIELDCONVERSION_H__
73 while( isspace(s[i]) && s[i]!=
'\0' ) {
87 if( s[i] ==
',' || isspace(s[i] ) ) {
100 const char *&rest ) {
106 while( s[i] !=
'\"' && s[i]!=
'\0' ) {
112 char *new_s =
new char[i+1];
113 strncpy( new_s, &s[1], i-1 );
122 H3D_DISABLE_UNUSED_PARAMETER_WARNING()
133 template< class Type >
134 inline Type
getValue( const
char *s, const
char *&rest ) {
147 inline
string getValue<
string>( const
char *s, const
char *&rest ) {
149 rest = &s[ str.length() ];
167 }
else if(s[0] ==
'0') {
174 std::stringstream conv;
179 conv << std::tolower(s[i], loc);
182 if(conv.str() ==
"true") {
188 conv << std::tolower(s[i], loc);
192 if(conv.str() ==
"false") {
211 bool negative =
false;
212 unsigned long integer_part = 0;
213 double fractional_part = 0;
215 while( isspace(s[i]) && s[i]!=
'\0' ) {
220 }
else if( s[i] ==
'-' ) {
224 while( isdigit( s[i] ) && s[i]!=
'\0' ) {
225 integer_part = 10 * integer_part + ( s[i] -
'0' );
231 unsigned int divider = 10;
232 while( isdigit( s[i] ) && s[i]!=
'\0' ) {
233 fractional_part = fractional_part + (double)( s[i] -
'0' ) / divider;
241 bool exponent_negative =
false;
242 if( s[i] ==
'd' || s[i] ==
'D' || s[i] ==
'e' || s[i] ==
'E' ) {
246 }
else if ( s[i] ==
'-' ) {
248 exponent_negative =
true;
251 int before_digit = i;
252 while( isdigit( s[i] ) && s[i]!=
'\0' ) {
253 exponent = 10 * exponent + ( s[i] -
'0' );
258 if( i == before_digit ) {
262 if( exponent_negative ) {
263 exponent = -exponent;
272 double res = integer_part + fractional_part;
273 if( negative ) res = -res;
274 if( exponent != 0 ) res = res *
H3DPow( 10, exponent );
299 while( isspace(s[i]) && s[i]!=
'\0' ) {
302 if( s[0] ==
'0' && s[1] ==
'x' ) {
305 int max_index = i + 8;
306 int return_value = 0;
308 for( ; i < max_index; ++i ) {
311 if (s[i] > 0x29 && s[i] < 0x40 )
312 pos_value = s[i] & 0x0f;
313 else if (s[i] >=
'a' && s[i] <=
'f')
314 pos_value= (s[i] & 0x0f) + 9;
315 else if (s[i] >=
'A' && s[i] <=
'F')
316 pos_value = (s[i] & 0x0f) + 9;
320 return_value = return_value << 4;
321 return_value = return_value | pos_value;
332 bool negative =
false;
336 }
else if ( s[i] ==
'-' ) {
342 while( isdigit( s[i] ) && s[i]!=
'\0' ) {
343 result = 10 * result + ( s[i] -
'0' );
352 if( negative ) result = -result;
367 color.r = getValue<H3DFloat>( s, t1 );
368 if( !isspace(t1[0]) ) {
371 color.g = getValue<H3DFloat>( t1, t2 );
372 if( !isspace(t2[0]) ) {
375 color.b = getValue<H3DFloat>( t2, rest );
388 color.r = getValue<H3DFloat>( s, t1 );
389 if( !isspace(t1[0]) ) {
392 color.g = getValue<H3DFloat>( t1, t2 );
393 if( !isspace(t2[0]) ) {
396 color.b = getValue<H3DFloat>( t2, t1 );
397 if( !isspace(t1[0]) ) {
400 color.a = getValue<H3DFloat>( t1, rest );
411 const char *&rest ) {
414 rot.axis.x = getValue<H3DFloat>( s, t1 );
415 if( !isspace(t1[0]) ) {
418 rot.axis.y = getValue<H3DFloat>( t1, t2 );
419 if( !isspace(t2[0]) ) {
422 rot.axis.z = getValue<H3DFloat>( t2, t1 );
423 if( !isspace(t1[0]) ) {
426 rot.angle = getValue<H3DFloat>( t1, rest );
437 const char *&rest ) {
440 q.v.x = getValue<H3DFloat>( s, t1 );
441 if( !isspace(t1[0]) ) {
444 q.v.y = getValue<H3DFloat>( t1, t2 );
445 if( !isspace(t2[0]) ) {
448 q.v.z = getValue<H3DFloat>( t2, t1 );
449 if( !isspace(t1[0]) ) {
452 q.w = getValue<H3DFloat>( t1, rest );
465 m[0][0] = getValue<H3DFloat>( s, t1 );
466 if( !isspace(t1[0]) ) {
469 m[0][1] = getValue<H3DFloat>( t1, t2 );
470 if( !isspace(t2[0]) ) {
473 m[0][2] = getValue<H3DFloat>( t2, t1 );
474 if( !isspace(t1[0]) ) {
477 m[0][3] = getValue<H3DFloat>( t1, t2 );
478 if( !isspace(t2[0]) ) {
482 m[1][0] = getValue<H3DFloat>( t2, t1 );
483 if( !isspace(t1[0]) ) {
486 m[1][1] = getValue<H3DFloat>( t1, t2 );
487 if( !isspace(t2[0]) ) {
490 m[1][2] = getValue<H3DFloat>( t2, t1 );
491 if( !isspace(t1[0]) ) {
494 m[1][3] = getValue<H3DFloat>( t1, t2 );
495 if( !isspace(t2[0]) ) {
499 m[2][0] = getValue<H3DFloat>( t2, t1 );
500 if( !isspace(t1[0]) ) {
503 m[2][1] = getValue<H3DFloat>( t1, t2 );
504 if( !isspace(t2[0]) ) {
507 m[2][2] = getValue<H3DFloat>( t2, t1 );
508 if( !isspace(t1[0]) ) {
511 m[2][3] = getValue<H3DFloat>( t1, t2 );
512 if( !isspace(t2[0]) ) {
516 m[3][0] = getValue<H3DFloat>( t2, t1 );
517 if( !isspace(t1[0]) ) {
520 m[3][1] = getValue<H3DFloat>( t1, t2 );
521 if( !isspace(t2[0]) ) {
524 m[3][2] = getValue<H3DFloat>( t2, t1 );
525 if( !isspace(t1[0]) ) {
528 m[3][3] = getValue<H3DFloat>( t1, rest );
542 m[0][0] = getValue<H3DFloat>( s, t1 );
543 if( !isspace(t1[0]) ) {
546 m[0][1] = getValue<H3DFloat>( t1, t2 );
547 if( !isspace(t2[0]) ) {
550 m[0][2] = getValue<H3DFloat>( t2, t1 );
551 if( !isspace(t1[0]) ) {
554 m[1][0] = getValue<H3DFloat>( t1, t2 );
555 if( !isspace(t2[0]) ) {
558 m[1][1] = getValue<H3DFloat>( t2, t1 );
559 if( !isspace(t1[0]) ) {
562 m[1][2] = getValue<H3DFloat>( t1, t2 );
563 if( !isspace(t2[0]) ) {
566 m[2][0] = getValue<H3DFloat>( t2, t1 );
567 if( !isspace(t1[0]) ) {
570 m[2][1] = getValue<H3DFloat>( t1, t2 );
571 if( !isspace(t2[0]) ) {
574 m[2][2] = getValue<H3DFloat>( t2, rest );
588 m[0][0] = getValue<H3DDouble>( s, t1 );
589 if( !isspace(t1[0]) ) {
592 m[0][1] = getValue<H3DDouble>( t1, t2 );
593 if( !isspace(t2[0]) ) {
596 m[0][2] = getValue<H3DDouble>( t2, t1 );
597 if( !isspace(t1[0]) ) {
600 m[0][3] = getValue<H3DDouble>( t1, t2 );
601 if( !isspace(t2[0]) ) {
605 m[1][0] = getValue<H3DDouble>( t2, t1 );
606 if( !isspace(t1[0]) ) {
609 m[1][1] = getValue<H3DDouble>( t1, t2 );
610 if( !isspace(t2[0]) ) {
613 m[1][2] = getValue<H3DDouble>( t2, t1 );
614 if( !isspace(t1[0]) ) {
617 m[1][3] = getValue<H3DDouble>( t1, t2 );
618 if( !isspace(t2[0]) ) {
622 m[2][0] = getValue<H3DDouble>( t2, t1 );
623 if( !isspace(t1[0]) ) {
626 m[2][1] = getValue<H3DDouble>( t1, t2 );
627 if( !isspace(t2[0]) ) {
630 m[2][2] = getValue<H3DDouble>( t2, t1 );
631 if( !isspace(t1[0]) ) {
634 m[2][3] = getValue<H3DDouble>( t1, t2 );
635 if( !isspace(t2[0]) ) {
639 m[3][0] = getValue<H3DDouble>( t2, t1 );
640 if( !isspace(t1[0]) ) {
643 m[3][1] = getValue<H3DDouble>( t1, t2 );
644 if( !isspace(t2[0]) ) {
647 m[3][2] = getValue<H3DDouble>( t2, t1 );
648 if( !isspace(t1[0]) ) {
651 m[3][3] = getValue<H3DDouble>( t1, rest );
665 m[0][0] = getValue<H3DDouble>( s, t1 );
666 if( !isspace(t1[0]) ) {
669 m[0][1] = getValue<H3DDouble>( t1, t2 );
670 if( !isspace(t2[0]) ) {
673 m[0][2] = getValue<H3DDouble>( t2, t1 );
674 if( !isspace(t1[0]) ) {
677 m[1][0] = getValue<H3DDouble>( t1, t2 );
678 if( !isspace(t2[0]) ) {
681 m[1][1] = getValue<H3DDouble>( t2, t1 );
682 if( !isspace(t1[0]) ) {
685 m[1][2] = getValue<H3DDouble>( t1, t2 );
686 if( !isspace(t2[0]) ) {
689 m[2][0] = getValue<H3DDouble>( t2, t1 );
690 if( !isspace(t1[0]) ) {
693 m[2][1] = getValue<H3DDouble>( t1, t2 );
694 if( !isspace(t2[0]) ) {
697 m[2][2] = getValue<H3DDouble>( t2, rest );
713 v.x = getValue<H3DFloat>( s, t1 );
714 if( !isspace(t1[0]) ) {
717 v.y = getValue<H3DFloat>( t1, t2 );
718 if( !isspace(t2[0]) ) {
721 v.z = getValue<H3DFloat>( t2, t1 );
722 if( !isspace(t1[0]) ) {
725 v.w = getValue<H3DFloat>( t1, rest );
738 v.x = getValue<H3DDouble>( s, t1 );
739 if( !isspace(t1[0]) ) {
742 v.y = getValue<H3DDouble>( t1, t2 );
743 if( !isspace(t2[0]) ) {
746 v.z = getValue<H3DDouble>( t2, t1 );
747 if( !isspace(t1[0]) ) {
750 v.w = getValue<H3DDouble>( t1, rest );
763 v.x = getValue<H3DFloat>( s, t1 );
764 if( !isspace(t1[0]) ) {
767 v.y = getValue<H3DFloat>( t1, t2 );
768 if( !isspace(t2[0]) ) {
771 v.z = getValue<H3DFloat>( t2, rest );
784 v.x = getValue<H3DDouble>( s, t1 );
785 if( !isspace(t1[0]) ) {
788 v.y = getValue<H3DDouble>( t1, t2 );
789 if( !isspace(t2[0]) ) {
792 v.z = getValue<H3DDouble>( t2, rest );
805 v.x = getValue<H3DFloat>( s, t1 );
806 if( !isspace(t1[0]) ) {
809 v.y = getValue<H3DFloat>( t1, rest );
822 v.x = getValue<H3DDouble>( s, t1 );
823 if( !isspace(t1[0]) ) {
826 v.y = getValue<H3DDouble>( t1, rest );
830 unsigned char *readImageData(
const char *s,
841 inline double atof(
const char * s) {
858 template<
class Type >
861 const char *s = x3d_string.c_str();
866 value = Convert::getValue<Type>( s, t1 );
868 }
catch(
const ConversionError & ) {
869 throw ConversionError(
typeid( Type ).name() );
872 throw ConversionError(
typeid( Type ).name() );
884 template<
class VectorType >
886 VectorType &values ) {
888 const char *s = x3d_string.c_str();
893 while( s[0] !=
'\0' ) {
895 typename VectorType::value_type >
896 ( (
const char*)s, t1 ) );
899 }
catch(
const ConversionError & ) {
901 ss <<
typeid(
typename VectorType::value_type ).name()
903 throw ConversionError( ss.str() );
914 inline void X3DStringToVector< vector< string > >(
915 const string &x3d_string,
916 vector< string > &values ) {
918 const char *s = x3d_string.c_str();
926 while( s[0] !=
'\0' ) {
932 }
catch(
const ConversionError & ) {
934 ss <<
" string vector";
935 throw ConversionError( ss.str() );
Base header file that handles all configuration related settings.
Include this file to include the H3D specific types.
An exception for errors when the conversion from a string to the type that we try convert to is not i...
Definition: X3DFieldConversion.h:64
An exception for errors when string to convert to a field value is invalid for the field type.
Definition: X3DFieldConversion.h:56
H3D_VALUE_EXCEPTION(string, InvalidType)
An exception thrown when a field is of the wrong type when it is checked.
RGB getValue< RGB >(const char *s, const char *&rest)
Template specialization to handle the type 'RGB'.
Definition: X3DFieldConversion.h:364
const char * skipWhitespacesAndCommas(const char *s)
Skip whitespaces and commas at the beginning of a string.
Definition: X3DFieldConversion.h:84
Type getValue(const char *s, const char *&rest)
Function that reads characters from a char * and converts them to a given type.
Definition: X3DFieldConversion.h:134
Rotation getValue< Rotation >(const char *s, const char *&rest)
Template specialization to handle the type 'Rotation'.
Definition: X3DFieldConversion.h:410
Quaternion getValue< Quaternion >(const char *s, const char *&rest)
Template specialization to handle the type 'Quaternion'.
Definition: X3DFieldConversion.h:436
Vec4d getValue< Vec4d >(const char *s, const char *&rest)
Template specialization to handle the type 'Vec4d'.
Definition: X3DFieldConversion.h:735
const char * skipWhitespaces(const char *s)
Skip whitespaces at the beginning of a string.
Definition: X3DFieldConversion.h:71
int getValue< int >(const char *s, const char *&rest)
Template specialization to handle the type 'int'.
Definition: X3DFieldConversion.h:295
string getQuoteEnclosedStringValue(const char *s, const char *&rest)
Works the same way as getValue<string>() but with the exception that the string given is within doubl...
Definition: X3DFieldConversion.h:99
Vec2f getValue< Vec2f >(const char *s, const char *&rest)
Template specialization to handle the type 'Vec2f'.
Definition: X3DFieldConversion.h:802
Vec3d getValue< Vec3d >(const char *s, const char *&rest)
Template specialization to handle the type 'Vec3d'.
Definition: X3DFieldConversion.h:781
Matrix4d getValue< Matrix4d >(const char *s, const char *&rest)
Template specialization to handle the type 'Matrix4d'.
Definition: X3DFieldConversion.h:585
Vec2d getValue< Vec2d >(const char *s, const char *&rest)
Template specialization to handle the type 'Vec2d'.
Definition: X3DFieldConversion.h:819
bool getValue< bool >(const char *s, const char *&rest)
Template specialization to handle the type 'bool'.
Definition: X3DFieldConversion.h:159
string getValue< string >(const char *s, const char *&rest)
Template specialization to handle the type 'string'.
Definition: X3DFieldConversion.h:147
Matrix3d getValue< Matrix3d >(const char *s, const char *&rest)
Template specialization to handle the type 'Matrix3f'.
Definition: X3DFieldConversion.h:662
double getValue< double >(const char *s, const char *&rest)
Template specialization to handle the type 'double'.
Definition: X3DFieldConversion.h:207
RGBA getValue< RGBA >(const char *s, const char *&rest)
Template specialization to handle the type 'RGBA'.
Definition: X3DFieldConversion.h:385
Matrix4f getValue< Matrix4f >(const char *s, const char *&rest)
Template specialization to handle the type 'Matrix4f'.
Definition: X3DFieldConversion.h:462
float getValue< float >(const char *s, const char *&rest)
Template specialization to handle the type 'float'.
Definition: X3DFieldConversion.h:285
Matrix3f getValue< Matrix3f >(const char *s, const char *&rest)
Template specialization to handle the type 'Matrix3f'.
Definition: X3DFieldConversion.h:539
double atof(const char *s)
The same as standard atof function with the difference that the decimal point is always .
Definition: X3DFieldConversion.h:841
Vec3f getValue< Vec3f >(const char *s, const char *&rest)
Template specialization to handle the type 'Vec3f'.
Definition: X3DFieldConversion.h:760
Vec4f getValue< Vec4f >(const char *s, const char *&rest)
Template specialization to handle the type 'Vec4f'.
Definition: X3DFieldConversion.h:710
Type X3DStringToValue(const string &x3d_string)
Convert a string to a specified type according to the X3D/XML field encoding.
Definition: X3DFieldConversion.h:859
void X3DStringToVector(const string &x3d_string, VectorType &values)
Convert a string to a vector of a specified type according to the X3D/XML field encoding.
Definition: X3DFieldConversion.h:885
PixelImage * X3DStringTo3DImage(const string &x3d_string)
Convert a string to a 3D PixelImage according to the X3D spec for Pixel3DTexture.
Definition: X3DFieldConversion.cpp:36
PixelImage * X3DStringTo2DImage(const string &x3d_string)
Convert a string to a PixelImage according to the X3D/XML field encoding for SFImage.
Definition: X3DFieldConversion.cpp:107
H3D API namespace.
Definition: Anchor.h:38