Ticket #170: OSGMathIO.cpp

File OSGMathIO.cpp, 5.7 kB (added by marcusl, 1 year ago)

Source file, compiles with 1.8

Line 
1 /*---------------------------------------------------------------------------*\
2  *                                OpenSG                                     *
3  *                                                                           *
4  *                                                                           *
5  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
6  *                                                                           *
7  *                            www.opensg.org                                 *
8  *                                                                           *
9  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
10  *                                                                           *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13  *                                License                                    *
14  *                                                                           *
15  * This library is free software; you can redistribute it and/or modify it   *
16  * under the terms of the GNU Library General Public License as published    *
17  * by the Free Software Foundation, version 2.                               *
18  *                                                                           *
19  * This library is distributed in the hope that it will be useful, but       *
20  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
22  * Library General Public License for more details.                          *
23  *                                                                           *
24  * You should have received a copy of the GNU Library General Public         *
25  * License along with this library; if not, write to the Free Software       *
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
27  *                                                                           *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30  *                                Changes                                    *
31  *                                                                           *
32  *                                                                           *
33  *                                                                           *
34  *                                                                           *
35  *                                                                           *
36  *                                                                           *
37 \*---------------------------------------------------------------------------*/
38
39 #include <OSGMathIO.h>
40
41 #include <OSGMatrix.h>
42 #include <OSGVector.h>
43 #include <OSGQuaternion.h>
44 #include <OSGColor.h>
45
46 #include <iostream>
47 #include <sstream>
48
49 OSG_BEGIN_NAMESPACE
50
51 namespace
52 {
53 template<int S, class T>
54 inline std::istream& vecRead(std::istream & is, T& obj)
55 {
56     for (int i = 0; i < S; i++) {
57         is >> obj[i];
58
59         // swallow separator (comma/space) & eventual following whitespace
60         if (i != S - 1) {
61             char tmp;
62             is >> tmp;
63             std::ws(is);
64         }
65     }
66
67     return is;
68 }
69
70 inline std::istream& consumeNewLine(std::istream& is)
71 {
72     while(is) {
73         int c = is.peek();
74         if (c != '\n' && c != '\r') {
75             break;
76         }
77         is.get();
78     }
79
80     return is;
81 }
82
83 // end anoymous namespace
84 }
85
86 template<class T>
87 OSG_BASE_DLLMAPPING std::istream & operator >>(std::istream & is, Color3<T> &obj)
88 {
89     return vecRead<3> (is, obj);
90 }
91
92 template<class T>
93 OSG_BASE_DLLMAPPING std::istream & operator >>(std::istream & is, Color4<T> &obj)
94 {
95     return vecRead<4> (is, obj);
96 }
97
98 template<class T, class S>
99 OSG_BASE_DLLMAPPING std::istream & operator >>(std::istream & is, PointInterface<T, S> &obj)
100 {
101     return vecRead<S::_iSize> (is, obj);
102 }
103
104 template<class T, class S>
105 OSG_BASE_DLLMAPPING std::istream & operator >>(std::istream & is, VectorInterface<T, S> &obj)
106 {
107     return vecRead<S::_iSize> (is, obj);
108 }
109
110 template<class T>
111 OSG_BASE_DLLMAPPING std::istream & operator >>(std::istream & is, QuaternionBase<T> &obj)
112 {
113     return vecRead<4>(is, obj);
114 }
115
116 template<class T>
117 OSG_BASE_DLLMAPPING std::istream & operator >>(std::istream & is, TransformationMatrix<T> &obj)
118 {
119     for (int j = 0; j < 4; j++) {
120         for (int i = 0; i < 4; i++) {
121             is >> std::ws >> obj[i][j] >> std::ws;
122
123             if (j < 3 || i < 3) {
124                 is >> consumeNewLine;
125             }
126         }
127     }
128
129     return is;
130 }
131
132 namespace
133 {
134 template<typename T>
135 void instantiate2()
136 {
137     std::istringstream is;
138     is >> VectorInterface<T, VecStorage2<T> > ();
139     is >> VectorInterface<T, VecStorage3<T> > ();
140     is >> VectorInterface<T, VecStorage4<T> > ();
141     is >> PointInterface<T, VecStorage2<T> > ();
142     is >> PointInterface<T, VecStorage3<T> > ();
143     is >> PointInterface<T, VecStorage4<T> > ();
144     is >> Color3<T> ();
145     is >> Color4<T> ();
146     is >> TransformationMatrix<T> ();
147     is >> QuaternionBase<T> ();
148 }
149
150 void instantiate1()
151 {
152     instantiate2<Real32> ();
153     instantiate2<Real64> ();
154     instantiate2<Real128> ();
155     instantiate2<Int8> ();
156     instantiate2<UInt8> ();
157     instantiate2<Int16> ();
158     instantiate2<UInt16> ();
159     instantiate2<Int32> ();
160     instantiate2<UInt32> ();
161 }
162 }
163
164 OSG_END_NAMESPACE