H3D API  2.4.1
X3DFieldConversion.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
27 //
29 
30 #ifndef __X3DFIELDCONVERSION_H__
31 #define __X3DFIELDCONVERSION_H__
32 
33 #include <sstream>
34 #include <H3D/H3DApi.h>
35 #include <H3DUtil/Exception.h>
36 #include <H3D/H3DTypes.h>
37 #include <H3DUtil/PixelImage.h>
38 
39 #include <typeinfo>
40 
41 //NOTE: Don't include <cctype> because then you'll get name-hiding issues for tolower().
42 #include <locale> // For std::tolower
43 
44 namespace H3D {
45  namespace X3D {
49  namespace Convert {
57 
65 
71  inline const char *skipWhitespaces( const char *s ) {
72  int i = 0;
73  while( isspace(s[i]) && s[i]!='\0' ) {
74  ++i;
75  }
76  return s+i;
77  }
78 
84  inline const char *skipWhitespacesAndCommas( const char *s ) {
85  int i = 0;
86  while( true ) {
87  if( s[i] == ',' || isspace(s[i] ) ) {
88  ++i;
89  } else {
90  break;
91  }
92  }
93  return s+i;
94  }
95 
99  inline string getQuoteEnclosedStringValue( const char *s,
100  const char *&rest ) {
101  int i = 0;
102  if( s[0] != '\"' ) {
103  throw X3DFieldConversionError( "string" );
104  }
105  ++i;
106  while( s[i] !='\"' && s[i]!='\0' ) {
107  ++i;
108  }
109  if( s[i] != '\"' ) {
110  throw X3DFieldConversionError( "string" );
111  }
112  char *new_s = new char[i+1];
113  strncpy( new_s, &s[1], i-1 );
114  new_s[i-1] = '\0';
115  string ret( new_s );
116  delete [] new_s;
117  rest = &s[ i + 1 ];
118  return ret;
119  }
120 
121 H3D_PUSH_WARNINGS()
122 H3D_DISABLE_UNUSED_PARAMETER_WARNING()
133  template< class Type >
134  inline Type getValue( const char *s, const char *&rest ) {
135  throw UnimplementedConversionType( typeid( Type ).name(),
136  "" );
137  }
138 H3D_POP_WARNINGS()
139 
140 
141 
146  template<>
147  inline string getValue<string>( const char *s, const char *&rest ) {
148  string str( s );
149  rest = &s[ str.length() ];
150  return str;
151  }
152 
158  template<>
159  inline bool getValue<bool>( const char *s, const char *&rest ) {
160  // Skip fluff
161  s = skipWhitespaces(s);
162 
163  // First handle the... integer approach to boolean conditions...
164  if(s[0] == '1') {
165  rest = &s[1];
166  return true;
167  } else if(s[0] == '0') {
168  rest = &s[1];
169  return false;
170  }
171 
172  // Convert string to lower for improved reliability
173  std::locale loc;
174  std::stringstream conv;
175 
176  // Add first four to look for "true"
177  size_t i = 0;
178  for(; i < 4; ++i) {
179  conv << std::tolower(s[i], loc);
180  }
181 
182  if(conv.str() == "true") {
183  rest = &s[i];
184  return true;
185  }
186 
187  // If "true" didn't match, add another character
188  conv << std::tolower(s[i], loc);
189  ++i;
190 
191  // And check for "false"
192  if(conv.str() == "false") {
193  rest = &s[i];
194  return false;
195  }
196 
197  // input isn't "true" or "false".
198  throw X3DFieldConversionError("bool");
199  }
200 
206  template<>
207  inline double getValue<double>( const char *s, const char *&rest ) {
208  // make sure the string is on the form
209  // [whitespace][sign][digits][.digits][ {d | D | e | E }[sign]digits]
210  int i = 0;
211  bool negative = false;
212  unsigned long integer_part = 0;
213  double fractional_part = 0;
214  bool valid = false;
215  while( isspace(s[i]) && s[i]!='\0' ) {
216  ++i;
217  }
218  if( s[i] == '+' ) {
219  ++i;
220  } else if( s[i] == '-' ) {
221  ++i;
222  negative = true;
223  }
224  while( isdigit( s[i] ) && s[i]!='\0' ) {
225  integer_part = 10 * integer_part + ( s[i] - '0' );
226  ++i;
227  valid = true;
228  }
229  if( s[i] == '.' ) {
230  ++i;
231  unsigned int divider = 10;
232  while( isdigit( s[i] ) && s[i]!='\0' ) {
233  fractional_part = fractional_part + (double)( s[i] - '0' ) / divider;
234  divider *= 10;
235  valid = true;
236  ++i;
237  }
238  }
239  int saved_i = i;
240  int exponent = 0;
241  bool exponent_negative = false;
242  if( s[i] == 'd' || s[i] == 'D' || s[i] == 'e' || s[i] == 'E' ) {
243  ++i;
244  if( s[i] == '+' ) {
245  ++i;
246  } else if ( s[i] == '-' ) {
247  ++i;
248  exponent_negative = true;
249  }
250 
251  int before_digit = i;
252  while( isdigit( s[i] ) && s[i]!='\0' ) {
253  exponent = 10 * exponent + ( s[i] - '0' );
254  ++i;
255  }
256  // make sure that we have at least one digit, otherwise this
257  // part is invalid and only the previous part will be used.
258  if( i == before_digit ) {
259  i = saved_i;
260  exponent = 0;
261  } else {
262  if( exponent_negative ) {
263  exponent = -exponent;
264  }
265  }
266  }
267 
268  if( !valid ) {
269  throw X3DFieldConversionError( "double" );
270  } else {
271  rest = &s[i];
272  double res = integer_part + fractional_part;
273  if( negative ) res = -res;
274  if( exponent != 0 ) res = res * H3DPow( 10, exponent );
275  return res;
276  }
277  }
278 
284  template<>
285  inline float getValue<float>( const char *s, const char *&rest ) {
286  return (float)getValue<double>( s, rest );
287  }
288 
294  template<>
295  inline int getValue<int>( const char *s, const char *&rest ) {
296  int i = 0;
297  bool valid = false;
298  // remove whitespaces
299  while( isspace(s[i]) && s[i]!='\0' ) {
300  ++i;
301  }
302  if( s[0] == '0' && s[1] == 'x' ) {
303  i+=2;
304  // hexadecimal number 0x????????
305  int max_index = i + 8;
306  int return_value = 0;
307  int pos_value;
308  for( ; i < max_index; ++i ) {
309  if (s[i]=='\0')
310  break;
311  if (s[i] > 0x29 && s[i] < 0x40 ) //if 0 to 9
312  pos_value = s[i] & 0x0f; //convert to int
313  else if (s[i] >='a' && s[i] <= 'f') //if a to f
314  pos_value= (s[i] & 0x0f) + 9; //convert to int
315  else if (s[i] >='A' && s[i] <= 'F') //if A to F
316  pos_value = (s[i] & 0x0f) + 9; //convert to int
317  else break;
318 
319  valid = true;
320  return_value = return_value << 4;
321  return_value = return_value | pos_value;
322  }
323  if( !valid ) {
324  throw X3DFieldConversionError( "int" );
325  } else {
326  rest = &s[i];
327  return return_value;
328  }
329  } else {
330  // decimal number. Make sure it is on the form
331  // [sign]digits
332  bool negative = false;
333 
334  if( s[i] == '+' ) {
335  ++i;
336  } else if ( s[i] == '-' ) {
337  negative = true;
338  ++i;
339  }
340 
341  int result = 0;
342  while( isdigit( s[i] ) && s[i]!='\0' ) {
343  result = 10 * result + ( s[i] - '0' );
344  ++i;
345  valid = true;
346  }
347 
348  if( !valid ) {
349  throw X3DFieldConversionError( "int" );
350  } else {
351  rest = &s[i];
352  if( negative ) result = -result;
353  return result;
354  }
355  }
356  }
357 
363  template<>
364  inline RGB getValue<RGB>( const char *s, const char *&rest ) {
365  RGB color;
366  const char *t1, *t2;
367  color.r = getValue<H3DFloat>( s, t1 );
368  if( !isspace(t1[0]) ) {
369  throw X3DFieldConversionError( "RGB" );
370  }
371  color.g = getValue<H3DFloat>( t1, t2 );
372  if( !isspace(t2[0]) ) {
373  throw X3DFieldConversionError( "RGB" );
374  }
375  color.b = getValue<H3DFloat>( t2, rest );
376  return color;
377  }
378 
384  template<>
385  inline RGBA getValue<RGBA>( const char *s, const char *&rest ) {
386  RGBA color;
387  const char *t1, *t2;
388  color.r = getValue<H3DFloat>( s, t1 );
389  if( !isspace(t1[0]) ) {
390  throw X3DFieldConversionError( "RGBA" );
391  }
392  color.g = getValue<H3DFloat>( t1, t2 );
393  if( !isspace(t2[0]) ) {
394  throw X3DFieldConversionError( "RGBA" );
395  }
396  color.b = getValue<H3DFloat>( t2, t1 );
397  if( !isspace(t1[0]) ) {
398  throw X3DFieldConversionError( "RGBA" );
399  }
400  color.a = getValue<H3DFloat>( t1, rest );
401  return color;
402  }
403 
409  template<>
410  inline Rotation getValue<Rotation>( const char *s,
411  const char *&rest ) {
412  Rotation rot;
413  const char *t1, *t2;
414  rot.axis.x = getValue<H3DFloat>( s, t1 );
415  if( !isspace(t1[0]) ) {
416  throw X3DFieldConversionError( "Rotation" );
417  }
418  rot.axis.y = getValue<H3DFloat>( t1, t2 );
419  if( !isspace(t2[0]) ) {
420  throw X3DFieldConversionError( "Rotation" );
421  }
422  rot.axis.z = getValue<H3DFloat>( t2, t1 );
423  if( !isspace(t1[0]) ) {
424  throw X3DFieldConversionError( "Rotation" );
425  }
426  rot.angle = getValue<H3DFloat>( t1, rest );
427  return rot;
428  }
429 
435  template<>
436  inline Quaternion getValue<Quaternion>( const char *s,
437  const char *&rest ) {
438  Quaternion q;
439  const char *t1, *t2;
440  q.v.x = getValue<H3DFloat>( s, t1 );
441  if( !isspace(t1[0]) ) {
442  throw X3DFieldConversionError( "Quaternion" );
443  }
444  q.v.y = getValue<H3DFloat>( t1, t2 );
445  if( !isspace(t2[0]) ) {
446  throw X3DFieldConversionError( "Quaternion" );
447  }
448  q.v.z = getValue<H3DFloat>( t2, t1 );
449  if( !isspace(t1[0]) ) {
450  throw X3DFieldConversionError( "Quaternion" );
451  }
452  q.w = getValue<H3DFloat>( t1, rest );
453  return q;
454  }
455 
461  template<>
462  inline Matrix4f getValue<Matrix4f>( const char *s, const char *&rest ) {
463  Matrix4f m;
464  const char *t1, *t2;
465  m[0][0] = getValue<H3DFloat>( s, t1 );
466  if( !isspace(t1[0]) ) {
467  throw X3DFieldConversionError( "Matrix4f" );
468  }
469  m[0][1] = getValue<H3DFloat>( t1, t2 );
470  if( !isspace(t2[0]) ) {
471  throw X3DFieldConversionError( "Matrix4f" );
472  }
473  m[0][2] = getValue<H3DFloat>( t2, t1 );
474  if( !isspace(t1[0]) ) {
475  throw X3DFieldConversionError( "Matrix4f" );
476  }
477  m[0][3] = getValue<H3DFloat>( t1, t2 );
478  if( !isspace(t2[0]) ) {
479  throw X3DFieldConversionError( "Matrix4f" );
480  }
481 
482  m[1][0] = getValue<H3DFloat>( t2, t1 );
483  if( !isspace(t1[0]) ) {
484  throw X3DFieldConversionError( "Matrix4f" );
485  }
486  m[1][1] = getValue<H3DFloat>( t1, t2 );
487  if( !isspace(t2[0]) ) {
488  throw X3DFieldConversionError( "Matrix4f" );
489  }
490  m[1][2] = getValue<H3DFloat>( t2, t1 );
491  if( !isspace(t1[0]) ) {
492  throw X3DFieldConversionError( "Matrix4f" );
493  }
494  m[1][3] = getValue<H3DFloat>( t1, t2 );
495  if( !isspace(t2[0]) ) {
496  throw X3DFieldConversionError( "Matrix4f" );
497  }
498 
499  m[2][0] = getValue<H3DFloat>( t2, t1 );
500  if( !isspace(t1[0]) ) {
501  throw X3DFieldConversionError( "Matrix4f" );
502  }
503  m[2][1] = getValue<H3DFloat>( t1, t2 );
504  if( !isspace(t2[0]) ) {
505  throw X3DFieldConversionError( "Matrix4f" );
506  }
507  m[2][2] = getValue<H3DFloat>( t2, t1 );
508  if( !isspace(t1[0]) ) {
509  throw X3DFieldConversionError( "Matrix4f" );
510  }
511  m[2][3] = getValue<H3DFloat>( t1, t2 );
512  if( !isspace(t2[0]) ) {
513  throw X3DFieldConversionError( "Matrix4f" );
514  }
515 
516  m[3][0] = getValue<H3DFloat>( t2, t1 );
517  if( !isspace(t1[0]) ) {
518  throw X3DFieldConversionError( "Matrix4f" );
519  }
520  m[3][1] = getValue<H3DFloat>( t1, t2 );
521  if( !isspace(t2[0]) ) {
522  throw X3DFieldConversionError( "Matrix4f" );
523  }
524  m[3][2] = getValue<H3DFloat>( t2, t1 );
525  if( !isspace(t1[0]) ) {
526  throw X3DFieldConversionError( "Matrix4f" );
527  }
528  m[3][3] = getValue<H3DFloat>( t1, rest );
529 
530  return m;
531  }
532 
538  template<>
539  inline Matrix3f getValue<Matrix3f>( const char *s, const char *&rest ) {
540  Matrix3f m;
541  const char *t1, *t2;
542  m[0][0] = getValue<H3DFloat>( s, t1 );
543  if( !isspace(t1[0]) ) {
544  throw X3DFieldConversionError( "Matrix3f" );
545  }
546  m[0][1] = getValue<H3DFloat>( t1, t2 );
547  if( !isspace(t2[0]) ) {
548  throw X3DFieldConversionError( "Matrix3f" );
549  }
550  m[0][2] = getValue<H3DFloat>( t2, t1 );
551  if( !isspace(t1[0]) ) {
552  throw X3DFieldConversionError( "Matrix3f" );
553  }
554  m[1][0] = getValue<H3DFloat>( t1, t2 );
555  if( !isspace(t2[0]) ) {
556  throw X3DFieldConversionError( "Matrix3f" );
557  }
558  m[1][1] = getValue<H3DFloat>( t2, t1 );
559  if( !isspace(t1[0]) ) {
560  throw X3DFieldConversionError( "Matrix3f" );
561  }
562  m[1][2] = getValue<H3DFloat>( t1, t2 );
563  if( !isspace(t2[0]) ) {
564  throw X3DFieldConversionError( "Matrix3f" );
565  }
566  m[2][0] = getValue<H3DFloat>( t2, t1 );
567  if( !isspace(t1[0]) ) {
568  throw X3DFieldConversionError( "Matrix3f" );
569  }
570  m[2][1] = getValue<H3DFloat>( t1, t2 );
571  if( !isspace(t2[0]) ) {
572  throw X3DFieldConversionError( "Matrix3f" );
573  }
574  m[2][2] = getValue<H3DFloat>( t2, rest );
575 
576  return m;
577  }
578 
584  template<>
585  inline Matrix4d getValue<Matrix4d>( const char *s, const char *&rest ) {
586  Matrix4d m;
587  const char *t1, *t2;
588  m[0][0] = getValue<H3DDouble>( s, t1 );
589  if( !isspace(t1[0]) ) {
590  throw X3DFieldConversionError( "Matrix4d" );
591  }
592  m[0][1] = getValue<H3DDouble>( t1, t2 );
593  if( !isspace(t2[0]) ) {
594  throw X3DFieldConversionError( "Matrix4d" );
595  }
596  m[0][2] = getValue<H3DDouble>( t2, t1 );
597  if( !isspace(t1[0]) ) {
598  throw X3DFieldConversionError( "Matrix4d" );
599  }
600  m[0][3] = getValue<H3DDouble>( t1, t2 );
601  if( !isspace(t2[0]) ) {
602  throw X3DFieldConversionError( "Matrix4d" );
603  }
604 
605  m[1][0] = getValue<H3DDouble>( t2, t1 );
606  if( !isspace(t1[0]) ) {
607  throw X3DFieldConversionError( "Matrix4d" );
608  }
609  m[1][1] = getValue<H3DDouble>( t1, t2 );
610  if( !isspace(t2[0]) ) {
611  throw X3DFieldConversionError( "Matrix4d" );
612  }
613  m[1][2] = getValue<H3DDouble>( t2, t1 );
614  if( !isspace(t1[0]) ) {
615  throw X3DFieldConversionError( "Matrix4d" );
616  }
617  m[1][3] = getValue<H3DDouble>( t1, t2 );
618  if( !isspace(t2[0]) ) {
619  throw X3DFieldConversionError( "Matrix4d" );
620  }
621 
622  m[2][0] = getValue<H3DDouble>( t2, t1 );
623  if( !isspace(t1[0]) ) {
624  throw X3DFieldConversionError( "Matrix4d" );
625  }
626  m[2][1] = getValue<H3DDouble>( t1, t2 );
627  if( !isspace(t2[0]) ) {
628  throw X3DFieldConversionError( "Matrix4d" );
629  }
630  m[2][2] = getValue<H3DDouble>( t2, t1 );
631  if( !isspace(t1[0]) ) {
632  throw X3DFieldConversionError( "Matrix4d" );
633  }
634  m[2][3] = getValue<H3DDouble>( t1, t2 );
635  if( !isspace(t2[0]) ) {
636  throw X3DFieldConversionError( "Matrix4d" );
637  }
638 
639  m[3][0] = getValue<H3DDouble>( t2, t1 );
640  if( !isspace(t1[0]) ) {
641  throw X3DFieldConversionError( "Matrix4d" );
642  }
643  m[3][1] = getValue<H3DDouble>( t1, t2 );
644  if( !isspace(t2[0]) ) {
645  throw X3DFieldConversionError( "Matrix4d" );
646  }
647  m[3][2] = getValue<H3DDouble>( t2, t1 );
648  if( !isspace(t1[0]) ) {
649  throw X3DFieldConversionError( "Matrix4d" );
650  }
651  m[3][3] = getValue<H3DDouble>( t1, rest );
652 
653  return m;
654  }
655 
661  template<>
662  inline Matrix3d getValue<Matrix3d>( const char *s, const char *&rest ) {
663  Matrix3d m;
664  const char *t1, *t2;
665  m[0][0] = getValue<H3DDouble>( s, t1 );
666  if( !isspace(t1[0]) ) {
667  throw X3DFieldConversionError( "Matrix3d" );
668  }
669  m[0][1] = getValue<H3DDouble>( t1, t2 );
670  if( !isspace(t2[0]) ) {
671  throw X3DFieldConversionError( "Matrix3d" );
672  }
673  m[0][2] = getValue<H3DDouble>( t2, t1 );
674  if( !isspace(t1[0]) ) {
675  throw X3DFieldConversionError( "Matrix3d" );
676  }
677  m[1][0] = getValue<H3DDouble>( t1, t2 );
678  if( !isspace(t2[0]) ) {
679  throw X3DFieldConversionError( "Matrix3d" );
680  }
681  m[1][1] = getValue<H3DDouble>( t2, t1 );
682  if( !isspace(t1[0]) ) {
683  throw X3DFieldConversionError( "Matrix3d" );
684  }
685  m[1][2] = getValue<H3DDouble>( t1, t2 );
686  if( !isspace(t2[0]) ) {
687  throw X3DFieldConversionError( "Matrix3d" );
688  }
689  m[2][0] = getValue<H3DDouble>( t2, t1 );
690  if( !isspace(t1[0]) ) {
691  throw X3DFieldConversionError( "Matrix3d" );
692  }
693  m[2][1] = getValue<H3DDouble>( t1, t2 );
694  if( !isspace(t2[0]) ) {
695  throw X3DFieldConversionError( "Matrix3d" );
696  }
697  m[2][2] = getValue<H3DDouble>( t2, rest );
698 
699  return m;
700  }
701 
702 
703 
709  template<>
710  inline Vec4f getValue<Vec4f>( const char *s, const char *&rest ) {
711  Vec4f v;
712  const char *t1, *t2;
713  v.x = getValue<H3DFloat>( s, t1 );
714  if( !isspace(t1[0]) ) {
715  throw X3DFieldConversionError( "Vec4f" );
716  }
717  v.y = getValue<H3DFloat>( t1, t2 );
718  if( !isspace(t2[0]) ) {
719  throw X3DFieldConversionError( "Vec4f" );
720  }
721  v.z = getValue<H3DFloat>( t2, t1 );
722  if( !isspace(t1[0]) ) {
723  throw X3DFieldConversionError( "Vec4f" );
724  }
725  v.w = getValue<H3DFloat>( t1, rest );
726  return v;
727  }
728 
734  template<>
735  inline Vec4d getValue<Vec4d>( const char *s, const char *&rest ) {
736  Vec4d v;
737  const char *t1, *t2;
738  v.x = getValue<H3DDouble>( s, t1 );
739  if( !isspace(t1[0]) ) {
740  throw X3DFieldConversionError( "Vec4d" );
741  }
742  v.y = getValue<H3DDouble>( t1, t2 );
743  if( !isspace(t2[0]) ) {
744  throw X3DFieldConversionError( "Vec4d" );
745  }
746  v.z = getValue<H3DDouble>( t2, t1 );
747  if( !isspace(t1[0]) ) {
748  throw X3DFieldConversionError( "Vec4d" );
749  }
750  v.w = getValue<H3DDouble>( t1, rest );
751  return v;
752  }
753 
759  template<>
760  inline Vec3f getValue<Vec3f>( const char *s, const char *&rest ) {
761  Vec3f v;
762  const char *t1, *t2;
763  v.x = getValue<H3DFloat>( s, t1 );
764  if( !isspace(t1[0]) ) {
765  throw X3DFieldConversionError( "Vec3f" );
766  }
767  v.y = getValue<H3DFloat>( t1, t2 );
768  if( !isspace(t2[0]) ) {
769  throw X3DFieldConversionError( "Vec3f" );
770  }
771  v.z = getValue<H3DFloat>( t2, rest );
772  return v;
773  }
774 
780  template<>
781  inline Vec3d getValue<Vec3d>( const char *s, const char *&rest ) {
782  Vec3d v;
783  const char *t1, *t2;
784  v.x = getValue<H3DDouble>( s, t1 );
785  if( !isspace(t1[0]) ) {
786  throw X3DFieldConversionError( "Vec3d" );
787  }
788  v.y = getValue<H3DDouble>( t1, t2 );
789  if( !isspace(t2[0]) ) {
790  throw X3DFieldConversionError( "Vec3d" );
791  }
792  v.z = getValue<H3DDouble>( t2, rest );
793  return v;
794  }
795 
801  template<>
802  inline Vec2f getValue<Vec2f>( const char *s, const char *&rest ) {
803  Vec2f v;
804  const char *t1;
805  v.x = getValue<H3DFloat>( s, t1 );
806  if( !isspace(t1[0]) ) {
807  throw X3DFieldConversionError( "Vec2f" );
808  }
809  v.y = getValue<H3DFloat>( t1, rest );
810  return v;
811  }
812 
818  template<>
819  inline Vec2d getValue<Vec2d>( const char *s, const char *&rest ) {
820  Vec2d v;
821  const char *t1;
822  v.x = getValue<H3DDouble>( s, t1 );
823  if( !isspace(t1[0]) ) {
824  throw X3DFieldConversionError( "Vec2d" );
825  }
826  v.y = getValue<H3DDouble>( t1, rest );
827  return v;
828  }
829 
830  unsigned char *readImageData( const char *s,
831  int width,
832  int height,
833  int depth,
834  int nr_components );
835 
841  inline double atof( const char * s) {
842  try {
843  const char *pos;
844  return X3D::Convert::getValue< double >( s, pos );
846  return 0.0;
847  }
848  }
849  }
850 
858  template< class Type >
859  Type X3DStringToValue( const string &x3d_string ) {
860  typedef Convert::X3DFieldConversionError ConversionError;
861  const char *s = x3d_string.c_str();
862  const char *t1;
863  Type value;
864  try {
865  s = Convert::skipWhitespaces( s );
866  value = Convert::getValue<Type>( s, t1 );
867  s = Convert::skipWhitespaces( t1 );
868  } catch( const ConversionError & ) {
869  throw ConversionError( typeid( Type ).name() );
870  }
871  if( s[0] != '\0' ) {
872  throw ConversionError( typeid( Type ).name() );
873  }
874  return value;
875  }
876 
884  template< class VectorType >
885  inline void X3DStringToVector( const string &x3d_string,
886  VectorType &values ) {
887  typedef Convert::X3DFieldConversionError ConversionError;
888  const char *s = x3d_string.c_str();
889  const char *t1;
890  values.clear();
891  try {
892  s = Convert::skipWhitespaces( s );
893  while( s[0] != '\0' ) {
894  values.push_back( Convert::getValue<
895  typename VectorType::value_type >
896  ( (const char*)s, t1 ) );
898  }
899  } catch( const ConversionError & ) {
900  stringstream ss;
901  ss << typeid( typename VectorType::value_type ).name()
902  << " vector";
903  throw ConversionError( ss.str() );
904  }
905  }
906 
913  template< >
914  inline void X3DStringToVector< vector< string > >(
915  const string &x3d_string,
916  vector< string > &values ) {
917  typedef Convert::X3DFieldConversionError ConversionError;
918  const char *s = x3d_string.c_str();
919  const char *t1;
920  values.clear();
921  try {
922  s = Convert::skipWhitespaces( s );
923  if( s[0] != '\"' )
924  values.push_back( Convert::getValue< string >( s, t1 ) );
925  else
926  while( s[0] != '\0' ) {
927  values.push_back(
928  Convert::getQuoteEnclosedStringValue( (const char*)s, t1 )
929  );
931  }
932  } catch( const ConversionError & ) {
933  stringstream ss;
934  ss << " string vector";
935  throw ConversionError( ss.str() );
936  }
937  }
938 
945  PixelImage *X3DStringTo2DImage( const string &x3d_string );
946 
953  PixelImage *X3DStringTo3DImage( const string &x3d_string );
954  }
955 }
956 
957 #endif
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
F H3DPow(F f, T t)
Rotation()
Vec2d()
Vec2f()
Vec3d()
Vec4f()
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