1 // Copyright (C) 2006-2009  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
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 GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17 
18 #ifndef SIMGEAR_SCENE_UTILS_OSGMATH_HXX
19 #define SIMGEAR_SCENE_UTILS_OSGMATH_HXX
20 
21 #include <osg/Vec2f>
22 #include <osg/Vec2d>
23 #include <osg/Vec3f>
24 #include <osg/Vec3d>
25 #include <osg/Vec4f>
26 #include <osg/Vec4d>
27 #include <osg/Quat>
28 #include <osg/Matrix>
29 
30 #include <simgear/math/SGMath.hxx>
31 
32 inline
33 SGVec2d
toSG(const osg::Vec2d & v)34 toSG(const osg::Vec2d& v)
35 { return SGVec2d(v[0], v[1]); }
36 
37 inline
38 SGVec2f
toSG(const osg::Vec2f & v)39 toSG(const osg::Vec2f& v)
40 { return SGVec2f(v[0], v[1]); }
41 
42 inline
43 osg::Vec2d
toOsg(const SGVec2d & v)44 toOsg(const SGVec2d& v)
45 { return osg::Vec2d(v[0], v[1]); }
46 
47 inline
48 osg::Vec2f
toOsg(const SGVec2f & v)49 toOsg(const SGVec2f& v)
50 { return osg::Vec2f(v[0], v[1]); }
51 
52 inline
53 SGVec3d
toSG(const osg::Vec3d & v)54 toSG(const osg::Vec3d& v)
55 { return SGVec3d(v[0], v[1], v[2]); }
56 
57 inline
58 SGVec3f
toSG(const osg::Vec3f & v)59 toSG(const osg::Vec3f& v)
60 { return SGVec3f(v[0], v[1], v[2]); }
61 
62 inline
63 osg::Vec3d
toOsg(const SGVec3d & v)64 toOsg(const SGVec3d& v)
65 { return osg::Vec3d(v[0], v[1], v[2]); }
66 
67 inline
68 osg::Vec3f
toOsg(const SGVec3f & v)69 toOsg(const SGVec3f& v)
70 { return osg::Vec3f(v[0], v[1], v[2]); }
71 
72 inline
73 SGVec4d
toSG(const osg::Vec4d & v)74 toSG(const osg::Vec4d& v)
75 { return SGVec4d(v[0], v[1], v[2], v[3]); }
76 
77 inline
78 SGVec4f
toSG(const osg::Vec4f & v)79 toSG(const osg::Vec4f& v)
80 { return SGVec4f(v[0], v[1], v[2], v[3]); }
81 
82 inline
83 osg::Vec4d
toOsg(const SGVec4d & v)84 toOsg(const SGVec4d& v)
85 { return osg::Vec4d(v[0], v[1], v[2], v[3]); }
86 
87 inline
88 osg::Vec4f
toOsg(const SGVec4f & v)89 toOsg(const SGVec4f& v)
90 { return osg::Vec4f(v[0], v[1], v[2], v[3]); }
91 
92 inline
93 SGQuatd
toSG(const osg::Quat & q)94 toSG(const osg::Quat& q)
95 { return SGQuatd(q[0], q[1], q[2], q[3]); }
96 
97 inline
98 osg::Quat
toOsg(const SGQuatd & q)99 toOsg(const SGQuatd& q)
100 { return osg::Quat(q[0], q[1], q[2], q[3]); }
101 
102 // Create a local coordinate frame in the earth-centered frame of
103 // reference. X points north, Z points down.
104 // makeSimulationFrameRelative() only includes rotation.
105 inline
106 osg::Matrix
makeSimulationFrameRelative(const SGGeod & geod)107 makeSimulationFrameRelative(const SGGeod& geod)
108 { return osg::Matrix(toOsg(SGQuatd::fromLonLat(geod))); }
109 
110 inline
111 osg::Matrix
makeSimulationFrame(const SGGeod & geod)112 makeSimulationFrame(const SGGeod& geod)
113 {
114     osg::Matrix result(makeSimulationFrameRelative(geod));
115     SGVec3d coord;
116     SGGeodesy::SGGeodToCart(geod, coord);
117     result.setTrans(toOsg(coord));
118     return result;
119 }
120 
121 // Create a Z-up local coordinate frame in the earth-centered frame
122 // of reference. This is what scenery models, etc. expect.
123 // makeZUpFrameRelative() only includes rotation.
124 inline
125 osg::Matrix
makeZUpFrameRelative(const SGGeod & geod)126 makeZUpFrameRelative(const SGGeod& geod)
127 {
128     osg::Matrix result(makeSimulationFrameRelative(geod));
129     // 180 degree rotation around Y axis
130     result.preMultRotate(osg::Quat(0.0, 1.0, 0.0, 0.0));
131     return result;
132 }
133 
134 inline
135 osg::Matrix
makeZUpFrame(const SGGeod & geod)136 makeZUpFrame(const SGGeod& geod)
137 {
138     osg::Matrix result(makeZUpFrameRelative(geod));
139     SGVec3d coord;
140     SGGeodesy::SGGeodToCart(geod, coord);
141     result.setTrans(toOsg(coord));
142     return result;
143 }
144 
145 #endif
146