Ticket #170: ouMathIO.cpp

File ouMathIO.cpp, 2.9 kB (added by marcusl, 2 years ago)

impl

Line 
1 #include "stdafx.h"
2
3 using namespace osg;
4
5 #include <styx/osgutil/ouMathIO.h>
6
7 #pragma optimize("s", on)
8 namespace mtc
9 {
10 namespace styx
11 {
12 namespace osgutil
13 {
14 namespace io
15 {
16 namespace
17 {
18 // one function to parse them all :)
19 template<class V, int S>
20 std::istream &
21 vecRead(std::istream & is, V * obj) {
22     for (int i = 0; i < S; i++) {
23         is >> obj[i];
24
25         // swallow separator (comma/space) & eventual following whitespace
26         if (i != S - 1) {
27             char tmp;
28             is >> tmp;
29             std::ws(is);
30         }
31     }
32
33     return is;
34 }
35
36 std::istream& consumeNewLine(std::istream& is)
37 {
38     while(is) {
39         int c = is.peek();
40         if (c != '\n' && c != '\r') {
41             break;
42         }
43         is.get();
44     }
45
46     return is;
47 }
48
49 // end anoymous namespace
50 }
51
52 template<class V>
53 STYX_DECLSPEC std::istream & operator >>(std::istream & is, Color3<V> &obj) {
54     return vecRead<V, 3> (is, obj.getValuesRGB());
55 }
56
57 template<class V>
58 STYX_DECLSPEC std::istream & operator >>(std::istream & is, Color4<V> &obj) {
59     return vecRead<V, 4> (is, obj.getValuesRGBA());
60 }
61
62 template<class V, class S>
63 STYX_DECLSPEC std::istream & operator >>(std::istream & is, PointInterface<V, S> &obj) {
64     return vecRead<V, S::_iSize> (is, obj.getValues());
65 }
66
67 template<class V, class S>
68 STYX_DECLSPEC std::istream & operator >>(std::istream & is, VectorInterface<V, S> &obj) {
69     return vecRead<V, S::_iSize> (is, obj.getValues());
70 }
71
72
73 template<class V>
74 STYX_DECLSPEC std::istream & operator >>(std::istream & is, TransformationMatrix<V> &obj) {
75     for (int j = 0; j < 4; j++) {
76         for (int i = 0; i < 4; i++) {
77             is >> std::ws >> obj[i][j] >> std::ws;
78
79             if (j < 3 || i < 3) {
80                 is >> consumeNewLine;
81             }
82         }
83     }
84
85     return is;
86 }
87
88 template<class V>
89 STYX_DECLSPEC std::istream & operator >>(std::istream & is, QuaternionBase<V> &obj) {
90     V v[4];
91     vecRead<V, 4> (is, v);
92     if (!is.fail()) {
93         obj.setValueAsQuat(v[0], v[1], v[2], v[3]);
94     }
95
96     return is;
97 }
98 }
99
100 namespace
101 {
102 template<typename T>
103 void instantiate2()
104 {
105     std::istringstream is;
106     is >> VectorInterface<T, VecStorage2<T> > ();
107     is >> VectorInterface<T, VecStorage3<T> > ();
108     is >> VectorInterface<T, VecStorage4<T> > ();
109     is >> PointInterface<T, VecStorage2<T> > ();
110     is >> PointInterface<T, VecStorage3<T> > ();
111     is >> PointInterface<T, VecStorage4<T> > ();
112     is >> Color3<T> ();
113     is >> Color4<T> ();
114     is >> TransformationMatrix<T> ();
115     is >> QuaternionBase<T> ();
116 }
117
118 void instantiate1()
119 {
120     instantiate2<Real32> ();
121     instantiate2<Real64> ();
122     instantiate2<Real128> ();
123     instantiate2<Int8> ();
124     instantiate2<UInt8> ();
125     instantiate2<Int16> ();
126     instantiate2<UInt16> ();
127     instantiate2<Int32> ();
128     instantiate2<UInt32> ();
129 }
130 }
131
132 }
133 }
134 }