Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
MathOSG.h
1 //
2 // A handy set of inline functions to convert between the VTP and OSG
3 // math classes. v2s() converts from VTP to OSG, s2v() goes the other way.
4 //
5 // Copyright (c) 2001-2007 Virtual Terrain Project
6 // Free for all uses, see license.txt for details.
7 //
8 
9 #ifndef VTOSG_MATHH
10 #define VTOSG_MATHH
11 
12 // Before OSG 2.6, we assume that OSG was built with float matrices. OSG did
13 // not expose whether it was built with OSG_USE_FLOAT_MATRIX or not, so we
14 // must assume. From OSG 2.6 onwards, OSG exposes it in include/osg/Config.
15 #include <osg/Version>
16 #if (OSG_VERSION_MAJOR==2 && OSG_VERSION_MINOR<6) || OSG_VERSION_MAJOR<2
17 #define OSG_USE_FLOAT_MATRIX
18 #endif
19 
20 #include <osg/Vec2>
21 #include <osg/Vec4>
22 #include <osg/BoundingBox>
23 #include <osg/BoundingSphere>
24 #include <osg/Matrix>
25 
26 #ifdef OSG_USE_FLOAT_MATRIX
27 typedef float osg_matrix_value;
28 #else
29 typedef double osg_matrix_value;
30 #endif
31 
33 // math helpers
34 
35 inline void v2s(const FPoint2 &f, osg::Vec2 &s) { s[0] = f.x; s[1] = f.y; }
36 inline void v2s(const FPoint3 &f, osg::Vec3 &s) { s[0] = f.x; s[1] = f.y; s[2] = f.z; }
37 inline void v2s(const RGBf &f, osg::Vec3 &s) { s[0] = f.r; s[1] = f.g; s[2] = f.b; }
38 inline void v2s(const RGBAf &f, osg::Vec4 &s) { s[0] = f.r; s[1] = f.g; s[2] = f.b; s[3] = f.a; }
39 
40 inline osg::Vec3 v2s(const FPoint3 &f)
41 {
42  osg::Vec3 s;
43  s[0] = f.x; s[1] = f.y; s[2] = f.z;
44  return s;
45 }
46 
47 inline osg::Vec4 v2s(const RGBf &f)
48 {
49  osg::Vec4 s;
50  s[0] = f.r; s[1] = f.g; s[2] = f.b; s[3] = 1.0f;
51  return s;
52 }
53 
54 inline osg::Vec4 v2s(const RGBAf &f)
55 {
56  osg::Vec4 s;
57  s[0] = f.r; s[1] = f.g; s[2] = f.b; s[3] = f.a;
58  return s;
59 }
60 
61 inline void v2s(const FSphere &sph, osg::BoundingSphere &bs)
62 {
63  v2s(sph.center, bs._center);
64  bs._radius = sph.radius;
65 }
66 
67 inline void s2v(const osg::Vec3 &s, FPoint3 &f) { f.x = s[0]; f.y = s[1]; f.z = s[2]; }
68 inline void s2v(const osg::Vec2 &s, FPoint2 &f) { f.x = s[0]; f.y = s[1]; }
69 inline void s2v(const osg::Vec3 &s, RGBf &f) { f.r = s[0]; f.g = s[1]; f.b = s[2]; }
70 inline void s2v(const osg::Vec4 &s, RGBAf &f) { f.r = s[0]; f.g = s[1]; f.b = s[2]; f.a = s[3]; }
71 
72 inline void s2v(const osg::BoundingSphere &bs, FSphere &sph)
73 {
74  s2v(bs._center, sph.center);
75  sph.radius = bs._radius;
76 }
77 
78 inline void s2v(const osg::BoundingBox &bs, FBox3 &box)
79 {
80  box.min.x = bs._min[0];
81  box.min.y = bs._min[1];
82  box.min.z = bs._min[2];
83 
84  box.max.x = bs._max[0];
85  box.max.y = bs._max[1];
86  box.max.z = bs._max[2];
87 }
88 
89 inline FPoint3 s2v(const osg::Vec3 &s)
90 {
91  FPoint3 f;
92  f.x = s[0]; f.y = s[1]; f.z = s[2];
93  return f;
94 }
95 
96 inline RGBf s2v(const osg::Vec4 &s)
97 {
98  RGBf f;
99  f.r = s[0]; f.g = s[1]; f.b = s[2];
100  return f;
101 }
102 
103 inline void ConvertMatrix4(const osg::Matrix *mat_osg, FMatrix4 *mat)
104 {
105  const osg_matrix_value *ptr = mat_osg->ptr();
106  int i, j;
107  for (i = 0; i < 4; i++)
108  for (j = 0; j < 4; j++)
109  {
110  mat->Set(j, i, ptr[(i<<2)+j]);
111  }
112 }
113 
114 inline void ConvertMatrix4(const FMatrix4 *mat, osg::Matrix *mat_osg)
115 {
116  osg_matrix_value *ptr = mat_osg->ptr();
117  int i, j;
118  for (i = 0; i < 4; i++)
119  for (j = 0; j < 4; j++)
120  {
121  ptr[(i<<2)+j] = mat->Get(j, i);
122  }
123 }
124 
125 class vtVec3 : public osg::Vec3
126 {
127 public:
128  vtVec3() {}
129  vtVec3(const FPoint3& v) : osg::Vec3(v.x, v.y, v.z) {}
130  vtVec3(const osg::Vec3& v) : osg::Vec3(v) {}
131 };
132 
133 class vtVec2 : public osg::Vec2
134 {
135 public:
136  vtVec2() {}
137  vtVec2(const FPoint2& v) : osg::Vec2(v.x, v.y) {}
138  vtVec2(const osg::Vec2& v) : osg::Vec2(v) {}
139  inline vtVec2& operator /= (const vtVec2& rhs)
140  {
141  _v[0] /= rhs._v[0];
142  _v[1]/= rhs._v[1];
143  return *this;
144  }
145 };
146 
147 #endif // VTOSG_MATHH
148