openscenegraph
io_utils
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef OSG_IO_UTILS
15#define OSG_IO_UTILS 1
16
17#include <ostream>
18#include <istream>
19#include <sstream>
20
21#include <osg/Vec4d>
22#include <osg/Vec4ub>
23#include <osg/Vec2b>
24#include <osg/Vec3b>
25#include <osg/Vec4b>
26#include <osg/Vec2s>
27#include <osg/Vec3s>
28#include <osg/Vec4s>
29#include <osg/Vec2i>
30#include <osg/Vec3i>
31#include <osg/Vec4i>
32#include <osg/Matrixf>
33#include <osg/Matrixd>
34#include <osg/Plane>
35
36namespace osg {
37
38
39/** Convinience class for building std::string using stringstream.
40 * Usage:
41 * MakeString str;
42 * std::string s = str<<"Mix strings with numbers "<<0" ;
43 * std::string s2 = str.clear()<<"and other classes such as ("<<osg::Vec3(0.0,1.0,3.0)<<)" ; */
44class MakeString
45{
46 public:
47 MakeString() {}
48
49 std::stringstream sstream;
50
51 template<typename T>
52 MakeString& operator << (const T& t)
53 {
54 sstream << t;
55 return *this;
56 }
57
58 MakeString& operator << (std::ostream& (*fun)(std::ostream&))
59 {
60 sstream << fun;
61 return *this;
62 }
63
64 inline MakeString& clear() { sstream.str("") ; return *this; }
65
66 inline operator std::string () const { return sstream.str(); }
67
68 inline std::string str() const { return sstream.str(); }
69};
70
71
72
73inline std::ostream& operator << (std::ostream& output, const MakeString& str) { output << str.str(); return output; }
74
75//////////////////////////////////////////////////////////////////////////
76// Vec2f streaming operators
77inline std::ostream& operator << (std::ostream& output, const Vec2f& vec)
78{
79 output << vec._v[0] << " " << vec._v[1];
80 return output; // to enable cascading
81}
82
83inline std::istream& operator >> (std::istream& input, Vec2f& vec)
84{
85 input >> vec._v[0] >> std::ws >> vec._v[1];
86 return input;
87}
88
89//////////////////////////////////////////////////////////////////////////
90// Vec2d steaming operators.
91inline std::ostream& operator << (std::ostream& output, const Vec2d& vec)
92{
93 output << vec._v[0] << " " << vec._v[1];
94 return output; // to enable cascading
95}
96
97inline std::istream& operator >> (std::istream& input, Vec2d& vec)
98{
99 input >> vec._v[0] >> std::ws >> vec._v[1];
100 return input;
101}
102
103//////////////////////////////////////////////////////////////////////////
104// Vec3f steaming operators.
105inline std::ostream& operator << (std::ostream& output, const Vec3f& vec)
106{
107 output << vec._v[0] << " "
108 << vec._v[1] << " "
109 << vec._v[2];
110 return output; // to enable cascading
111}
112
113inline std::istream& operator >> (std::istream& input, Vec3f& vec)
114{
115 input >> vec._v[0] >> std::ws >> vec._v[1] >> std::ws >> vec._v[2];
116 return input;
117}
118
119
120//////////////////////////////////////////////////////////////////////////
121// Vec3d steaming operators.
122inline std::ostream& operator << (std::ostream& output, const Vec3d& vec)
123{
124 output << vec._v[0] << " "
125 << vec._v[1] << " "
126 << vec._v[2];
127 return output; // to enable cascading
128}
129
130inline std::istream& operator >> (std::istream& input, Vec3d& vec)
131{
132 input >> vec._v[0] >> std::ws >> vec._v[1] >> std::ws >> vec._v[2];
133 return input;
134}
135
136
137//////////////////////////////////////////////////////////////////////////
138// Vec3f steaming operators.
139inline std::ostream& operator << (std::ostream& output, const Vec4f& vec)
140{
141 output << vec._v[0] << " "
142 << vec._v[1] << " "
143 << vec._v[2] << " "
144 << vec._v[3];
145 return output; // to enable cascading
146}
147
148inline std::istream& operator >> (std::istream& input, Vec4f& vec)
149{
150 input >> vec._v[0] >> std::ws
151 >> vec._v[1] >> std::ws
152 >> vec._v[2] >> std::ws
153 >> vec._v[3];
154 return input;
155}
156
157
158
159//////////////////////////////////////////////////////////////////////////
160// Vec4d steaming operators.
161inline std::ostream& operator << (std::ostream& output, const Vec4d& vec)
162{
163 output << vec._v[0] << " "
164 << vec._v[1] << " "
165 << vec._v[2] << " "
166 << vec._v[3];
167 return output; // to enable cascading
168}
169inline std::istream& operator >> (std::istream& input, Vec4d& vec)
170{
171 input >> vec._v[0] >> std::ws
172 >> vec._v[1] >> std::ws
173 >> vec._v[2] >> std::ws
174 >> vec._v[3];
175 return input;
176}
177
178
179//////////////////////////////////////////////////////////////////////////
180// Vec2b steaming operators.
181inline std::ostream& operator << (std::ostream& output, const Vec2b& vec)
182{
183 output << (int)vec._v[0] << " "
184 << (int)vec._v[1];
185 return output; // to enable cascading
186}
187
188inline std::istream& operator >> (std::istream& input, Vec2b& vec)
189{
190 input >> vec._v[0] >> std::ws >> vec._v[1];
191 return input;
192}
193
194//////////////////////////////////////////////////////////////////////////
195// Vec3b steaming operators.
196inline std::ostream& operator << (std::ostream& output, const Vec3b& vec)
197{
198 output << (int)vec._v[0] << " "
199 << (int)vec._v[1] << " "
200 << (int)vec._v[2];
201 return output; // to enable cascading
202}
203
204inline std::istream& operator >> (std::istream& input, Vec3b& vec)
205{
206 input >> vec._v[0] >> std::ws >> vec._v[1] >> std::ws >> vec._v[2];
207 return input;
208}
209
210//////////////////////////////////////////////////////////////////////////
211// Vec4b steaming operators.
212inline std::ostream& operator << (std::ostream& output, const Vec4b& vec)
213{
214 output << (int)vec._v[0] << " "
215 << (int)vec._v[1] << " "
216 << (int)vec._v[2] << " "
217 << (int)vec._v[3];
218 return output; // to enable cascading
219}
220
221inline std::istream& operator >> (std::istream& input, Vec4b& vec)
222{
223 input >> vec._v[0] >> std::ws
224 >> vec._v[1] >> std::ws
225 >> vec._v[2] >> std::ws
226 >> vec._v[3];
227 return input;
228}
229
230
231//////////////////////////////////////////////////////////////////////////
232// Vec2s steaming operators.
233inline std::ostream& operator << (std::ostream& output, const Vec2s& vec)
234{
235 output << (int)vec._v[0] << " "
236 << (int)vec._v[1];
237 return output; // to enable cascading
238}
239
240inline std::istream& operator >> (std::istream& input, Vec2s& vec)
241{
242 input >> vec._v[0] >> std::ws >> vec._v[1];
243 return input;
244}
245
246//////////////////////////////////////////////////////////////////////////
247// Vec3s steaming operators.
248inline std::ostream& operator << (std::ostream& output, const Vec3s& vec)
249{
250 output << (int)vec._v[0] << " "
251 << (int)vec._v[1] << " "
252 << (int)vec._v[2];
253 return output; // to enable cascading
254}
255
256inline std::istream& operator >> (std::istream& input, Vec3s& vec)
257{
258 input >> vec._v[0] >> std::ws >> vec._v[1] >> std::ws >> vec._v[2];
259 return input;
260}
261
262//////////////////////////////////////////////////////////////////////////
263// Vec4s steaming operators.
264inline std::ostream& operator << (std::ostream& output, const Vec4s& vec)
265{
266 output << (int)vec._v[0] << " "
267 << (int)vec._v[1] << " "
268 << (int)vec._v[2] << " "
269 << (int)vec._v[3];
270 return output; // to enable cascading
271}
272
273inline std::istream& operator >> (std::istream& input, Vec4s& vec)
274{
275 input >> vec._v[0] >> std::ws
276 >> vec._v[1] >> std::ws
277 >> vec._v[2] >> std::ws
278 >> vec._v[3];
279 return input;
280}
281
282
283
284//////////////////////////////////////////////////////////////////////////
285// Vec2i steaming operators.
286inline std::ostream& operator << (std::ostream& output, const Vec2i& vec)
287{
288 output << vec._v[0] << " "
289 << vec._v[1];
290 return output; // to enable cascading
291}
292
293inline std::istream& operator >> (std::istream& input, Vec2i& vec)
294{
295 input >> vec._v[0] >> std::ws >> vec._v[1];
296 return input;
297}
298
299//////////////////////////////////////////////////////////////////////////
300// Vec3i steaming operators.
301inline std::ostream& operator << (std::ostream& output, const Vec3i& vec)
302{
303 output << vec._v[0] << " "
304 << vec._v[1] << " "
305 << vec._v[2];
306 return output; // to enable cascading
307}
308
309inline std::istream& operator >> (std::istream& input, Vec3i& vec)
310{
311 input >> vec._v[0] >> std::ws >> vec._v[1] >> std::ws >> vec._v[2];
312 return input;
313}
314
315//////////////////////////////////////////////////////////////////////////
316// Vec4i steaming operators.
317inline std::ostream& operator << (std::ostream& output, const Vec4i& vec)
318{
319 output << vec._v[0] << " "
320 << vec._v[1] << " "
321 << vec._v[2] << " "
322 << vec._v[3];
323 return output; // to enable cascading
324}
325
326inline std::istream& operator >> (std::istream& input, Vec4i& vec)
327{
328 input >> vec._v[0] >> std::ws
329 >> vec._v[1] >> std::ws
330 >> vec._v[2] >> std::ws
331 >> vec._v[3];
332 return input;
333}
334
335
336//////////////////////////////////////////////////////////////////////////
337// Matrixf steaming operators.
338inline std::ostream& operator<< (std::ostream& os, const Matrixf& m )
339{
340 os << "{"<<std::endl;
341 for(int row=0; row<4; ++row) {
342 os << "\t";
343 for(int col=0; col<4; ++col)
344 os << m(row,col) << " ";
345 os << std::endl;
346 }
347 os << "}" << std::endl;
348 return os;
349}
350
351
352//////////////////////////////////////////////////////////////////////////
353// Matrixd steaming operators.
354inline std::ostream& operator<< (std::ostream& os, const Matrixd& m )
355{
356 os << "{"<<std::endl;
357 for(int row=0; row<4; ++row) {
358 os << "\t";
359 for(int col=0; col<4; ++col)
360 os << m(row,col) << " ";
361 os << std::endl;
362 }
363 os << "}" << std::endl;
364 return os;
365}
366
367//////////////////////////////////////////////////////////////////////////
368// Vec4ub steaming operators.
369inline std::ostream& operator << (std::ostream& output, const Vec4ub& vec)
370{
371 output << (int)vec._v[0] << " "
372 << (int)vec._v[1] << " "
373 << (int)vec._v[2] << " "
374 << (int)vec._v[3];
375 return output; // to enable cascading
376}
377
378inline std::istream& operator >> (std::istream& input, Vec4ub& vec)
379{
380 input >> vec._v[0] >> std::ws
381 >> vec._v[1] >> std::ws
382 >> vec._v[2] >> std::ws
383 >> vec._v[3];
384 return input;
385}
386
387
388//////////////////////////////////////////////////////////////////////////
389// Quat steaming operators.
390inline std::ostream& operator << (std::ostream& output, const Quat& vec)
391{
392 output << vec._v[0] << " "
393 << vec._v[1] << " "
394 << vec._v[2] << " "
395 << vec._v[3];
396 return output; // to enable cascading
397}
398
399inline std::istream& operator >> (std::istream& input, Quat& vec)
400{
401 input >> vec._v[0] >> std::ws
402 >> vec._v[1] >> std::ws
403 >> vec._v[2] >> std::ws
404 >> vec._v[3];
405 return input;
406}
407
408
409
410//////////////////////////////////////////////////////////////////////////
411// Plane steaming operators.
412inline std::ostream& operator << (std::ostream& output, const Plane& pl)
413{
414 output << pl[0] << " "
415 << pl[1] << " "
416 << pl[2] << " "
417 << pl[3];
418 return output; // to enable cascading
419}
420
421inline std::istream& operator >> (std::istream& input, Plane& vec)
422{
423 input >> vec[0] >> std::ws
424 >> vec[1] >> std::ws
425 >> vec[2] >> std::ws
426 >> vec[3];
427 return input;
428}
429
430} // end of namespace osg
431#endif