From ce54997be569ebcac4222321e7ae3fecccd01324 Mon Sep 17 00:00:00 2001 From: frohlich Date: Sat, 5 Sep 2009 06:53:52 +0000 Subject: [PATCH] Should be now more easy to make use of SGMath without having osg. Modified Files: simgear/scene/sky/dome.cxx simgear/math/SGGeod.cxx simgear/math/SGGeod.hxx simgear/math/SGQuat.hxx simgear/math/SGVec2.hxx simgear/math/SGVec3.hxx simgear/math/SGVec4.hxx --- simgear/math/SGGeod.cxx | 12 +++-- simgear/math/SGGeod.hxx | 16 +++--- simgear/math/SGQuat.hxx | 75 +++++++++++--------------- simgear/math/SGVec2.hxx | 101 +++++++++++++++-------------------- simgear/math/SGVec3.hxx | 104 +++++++++++++++--------------------- simgear/math/SGVec4.hxx | 105 +++++++++++++++---------------------- simgear/scene/sky/dome.cxx | 4 +- 7 files changed, 179 insertions(+), 238 deletions(-) diff --git a/simgear/math/SGGeod.cxx b/simgear/math/SGGeod.cxx index 01e857a2..370b9725 100644 --- a/simgear/math/SGGeod.cxx +++ b/simgear/math/SGGeod.cxx @@ -17,13 +17,15 @@ #include "SGMath.hxx" -osg::Matrix SGGeod::makeSimulationFrameRelative() +#ifndef NO_OPENSCENEGRAPH_INTERFACE + +osg::Matrix SGGeod::makeSimulationFrameRelative() const { SGQuatd hlOr = SGQuatd::fromLonLat(*this); return osg::Matrix(hlOr.osg()); } -osg::Matrix SGGeod::makeSimulationFrame() +osg::Matrix SGGeod::makeSimulationFrame() const { osg::Matrix result(makeSimulationFrameRelative()); SGVec3d coord; @@ -32,7 +34,7 @@ osg::Matrix SGGeod::makeSimulationFrame() return result; } -osg::Matrix SGGeod::makeZUpFrameRelative() +osg::Matrix SGGeod::makeZUpFrameRelative() const { osg::Matrix result(makeSimulationFrameRelative()); // 180 degree rotation around Y axis @@ -41,7 +43,7 @@ osg::Matrix SGGeod::makeZUpFrameRelative() return result; } -osg::Matrix SGGeod::makeZUpFrame() +osg::Matrix SGGeod::makeZUpFrame() const { osg::Matrix result(makeZUpFrameRelative()); SGVec3d coord; @@ -49,3 +51,5 @@ osg::Matrix SGGeod::makeZUpFrame() result.setTrans(coord.osg()); return result; } + +#endif diff --git a/simgear/math/SGGeod.hxx b/simgear/math/SGGeod.hxx index 144ee347..86e38a1f 100644 --- a/simgear/math/SGGeod.hxx +++ b/simgear/math/SGGeod.hxx @@ -20,7 +20,9 @@ #include +#ifndef NO_OPENSCENEGRAPH_INTERFACE #include +#endif // #define SG_GEOD_NATIVE_DEGREE @@ -84,25 +86,27 @@ public: /// Set the geodetic elevation from the argument given in feet void setElevationFt(double elevation); +#ifndef NO_OPENSCENEGRAPH_INTERFACE // Create a local coordinate frame in the earth-centered frame of // reference. X points north, Z points down. // makeSimulationFrameRelative() only includes rotation. - - osg::Matrix makeSimulationFrameRelative(); - osg::Matrix makeSimulationFrame(); + osg::Matrix makeSimulationFrameRelative() const; + osg::Matrix makeSimulationFrame() const; // Create a Z-up local coordinate frame in the earth-centered frame // of reference. This is what scenery models, etc. expect. // makeZUpFrameRelative() only includes rotation. - osg::Matrix makeZUpFrameRelative(); - osg::Matrix makeZUpFrame(); + osg::Matrix makeZUpFrameRelative() const; + osg::Matrix makeZUpFrame() const; +#endif private: /// This one is private since construction is not unique if you do /// not know the units of the arguments. Use the factory methods for /// that purpose SGGeod(double lon, double lat, double elevation); - /// The actual data, angles in degree, elevation in meters + //// FIXME: wrong comment! + /// The actual data, angles in degrees, elevation in meters /// The rationale for storing the values in degrees is that most code places /// in flightgear/terragear use degrees as a nativ input and output value. /// The places where it makes sense to use radians is when we convert diff --git a/simgear/math/SGQuat.hxx b/simgear/math/SGQuat.hxx index 1f6aaef0..387951da 100644 --- a/simgear/math/SGQuat.hxx +++ b/simgear/math/SGQuat.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2006 Mathias Froehlich - Mathias.Froehlich@web.de +// Copyright (C) 2006-2009 Mathias Froehlich - Mathias.Froehlich@web.de // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public @@ -26,42 +26,13 @@ #undef max #endif +#ifndef NO_OPENSCENEGRAPH_INTERFACE #include +#endif +/// Quaternion Class template -struct SGQuatStorage { - /// Readonly raw storage interface - const T (&data(void) const)[4] - { return _data; } - /// Readonly raw storage interface - T (&data(void))[4] - { return _data; } - - void osg() const - { } - -private: - T _data[4]; -}; - -template<> -struct SGQuatStorage : public osg::Quat { - /// Access raw data by index, the index is unchecked - const double (&data(void) const)[4] - { return osg::Quat::_v; } - /// Access raw data by index, the index is unchecked - double (&data(void))[4] - { return osg::Quat::_v; } - - const osg::Quat& osg() const - { return *this; } - osg::Quat& osg() - { return *this; } -}; - -/// 3D Vector Class -template -class SGQuat : protected SGQuatStorage { +class SGQuat { public: typedef T value_type; @@ -84,8 +55,10 @@ public: /// make sure it has at least 4 elements explicit SGQuat(const T* d) { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; } +#ifndef NO_OPENSCENEGRAPH_INTERFACE explicit SGQuat(const osg::Quat& d) { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; } +#endif /// Return a unit quaternion static SGQuat unit(void) @@ -158,6 +131,7 @@ public: static SGQuat fromLonLat(const SGGeod& geod) { return fromLonLatRad(geod.getLongitudeRad(), geod.getLatitudeRad()); } + /// Return a quaternion rotation from the earth centered to the /// OpenGL/viewer horizontal local frame from given longitude and latitude. /// This frame matches the usual OpenGL axis directions. That is the target @@ -432,17 +406,16 @@ public: { return data()[3]; } /// Get the data pointer - using SGQuatStorage::data; - - /// Readonly interface function to ssg's sgQuat/sgdQuat - const T (&sg(void) const)[4] - { return data(); } - /// Interface function to ssg's sgQuat/sgdQuat - T (&sg(void))[4] - { return data(); } + const T (&data(void) const)[4] + { return _data; } + /// Get the data pointer + T (&data(void))[4] + { return _data; } - /// Interface function to osg's Quat* - using SGQuatStorage::osg; +#ifndef NO_OPENSCENEGRAPH_INTERFACE + osg::Quat osg() const + { return osg::Quat(data()[0], data()[1], data()[2], data()[3]); } +#endif /// Inplace addition SGQuat& operator+=(const SGQuat& v) @@ -561,6 +534,8 @@ private: SGQuat q2 = SGQuat::fromRotateToSmaller90Deg(-cosang, -from, to); return q1*q2; } + + T _data[4]; }; /// Unary +, do nothing ... @@ -790,4 +765,16 @@ SGQuatd toQuatd(const SGQuatf& v) { return SGQuatd(v(0), v(1), v(2), v(3)); } +#ifndef NO_OPENSCENEGRAPH_INTERFACE +inline +SGQuatd +toSG(const osg::Quat& q) +{ return SGQuatd(q[0], q[1], q[2], q[3]); } + +inline +osg::Quat +toOsg(const SGQuatd& q) +{ return osg::Quat(q[0], q[1], q[2], q[3]); } +#endif + #endif diff --git a/simgear/math/SGVec2.hxx b/simgear/math/SGVec2.hxx index f9104991..1e683b49 100644 --- a/simgear/math/SGVec2.hxx +++ b/simgear/math/SGVec2.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2006 Mathias Froehlich - Mathias.Froehlich@web.de +// Copyright (C) 2006-2009 Mathias Froehlich - Mathias.Froehlich@web.de // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public @@ -22,58 +22,14 @@ #include #endif +#ifndef NO_OPENSCENEGRAPH_INTERFACE #include #include - -template -struct SGVec2Storage { - /// Readonly raw storage interface - const T (&data(void) const)[2] - { return _data; } - /// Readonly raw storage interface - T (&data(void))[2] - { return _data; } - - void osg() const - { } - -private: - T _data[2]; -}; - -template<> -struct SGVec2Storage : public osg::Vec2f { - /// Access raw data by index, the index is unchecked - const float (&data(void) const)[2] - { return osg::Vec2f::_v; } - /// Access raw data by index, the index is unchecked - float (&data(void))[2] - { return osg::Vec2f::_v; } - - const osg::Vec2f& osg() const - { return *this; } - osg::Vec2f& osg() - { return *this; } -}; - -template<> -struct SGVec2Storage : public osg::Vec2d { - /// Access raw data by index, the index is unchecked - const double (&data(void) const)[2] - { return osg::Vec2d::_v; } - /// Access raw data by index, the index is unchecked - double (&data(void))[2] - { return osg::Vec2d::_v; } - - const osg::Vec2d& osg() const - { return *this; } - osg::Vec2d& osg() - { return *this; } -}; +#endif /// 2D Vector Class template -class SGVec2 : protected SGVec2Storage { +class SGVec2 { public: typedef T value_type; @@ -99,10 +55,12 @@ public: template explicit SGVec2(const SGVec2& d) { data()[0] = d[0]; data()[1] = d[1]; } +#ifndef NO_OPENSCENEGRAPH_INTERFACE explicit SGVec2(const osg::Vec2f& d) { data()[0] = d[0]; data()[1] = d[1]; } explicit SGVec2(const osg::Vec2d& d) { data()[0] = d[0]; data()[1] = d[1]; } +#endif /// Access by index, the index is unchecked const T& operator()(unsigned i) const @@ -131,18 +89,17 @@ public: T& y(void) { return data()[1]; } - /// Get the data pointer - using SGVec2Storage::data; - - /// Readonly interface function to ssg's sgVec2/sgdVec2 - const T (&sg(void) const)[2] - { return data(); } - /// Interface function to ssg's sgVec2/sgdVec2 - T (&sg(void))[2] - { return data(); } + /// Access raw data + const T (&data(void) const)[2] + { return _data; } + /// Access raw data + T (&data(void))[2] + { return _data; } - /// Interface function to osg's Vec2* - using SGVec2Storage::osg; +#ifndef NO_OPENSCENEGRAPH_INTERFACE + osg::Vec2d osg() const + { return osg::Vec2d(data()[0], data()[1]); } +#endif /// Inplace addition SGVec2& operator+=(const SGVec2& v) @@ -167,6 +124,9 @@ public: { return SGVec2(1, 0); } static SGVec2 e2(void) { return SGVec2(0, 1); } + +private: + T _data[2]; }; /// Unary +, do nothing ... @@ -409,4 +369,27 @@ SGVec2d toVec2d(const SGVec2f& v) { return SGVec2d(v(0), v(1)); } +#ifndef NO_OPENSCENEGRAPH_INTERFACE +inline +SGVec2d +toSG(const osg::Vec2d& v) +{ return SGVec2d(v[0], v[1]); } + +inline +SGVec2f +toSG(const osg::Vec2f& v) +{ return SGVec2f(v[0], v[1]); } + +inline +osg::Vec2d +toOsg(const SGVec2d& v) +{ return osg::Vec2d(v[0], v[1]); } + +inline +osg::Vec2f +toOsg(const SGVec2f& v) +{ return osg::Vec2f(v[0], v[1]); } + +#endif + #endif diff --git a/simgear/math/SGVec3.hxx b/simgear/math/SGVec3.hxx index 4ec4145a..f2b7406a 100644 --- a/simgear/math/SGVec3.hxx +++ b/simgear/math/SGVec3.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2006 Mathias Froehlich - Mathias.Froehlich@web.de +// Copyright (C) 2006-2009 Mathias Froehlich - Mathias.Froehlich@web.de // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public @@ -18,58 +18,14 @@ #ifndef SGVec3_H #define SGVec3_H +#ifndef NO_OPENSCENEGRAPH_INTERFACE #include #include - -template -struct SGVec3Storage { - /// Readonly raw storage interface - const T (&data(void) const)[3] - { return _data; } - /// Readonly raw storage interface - T (&data(void))[3] - { return _data; } - - void osg() const - { } - -private: - T _data[3]; -}; - -template<> -struct SGVec3Storage : public osg::Vec3f { - /// Access raw data by index, the index is unchecked - const float (&data(void) const)[3] - { return osg::Vec3f::_v; } - /// Access raw data by index, the index is unchecked - float (&data(void))[3] - { return osg::Vec3f::_v; } - - const osg::Vec3f& osg() const - { return *this; } - osg::Vec3f& osg() - { return *this; } -}; - -template<> -struct SGVec3Storage : public osg::Vec3d { - /// Access raw data by index, the index is unchecked - const double (&data(void) const)[3] - { return osg::Vec3d::_v; } - /// Access raw data by index, the index is unchecked - double (&data(void))[3] - { return osg::Vec3d::_v; } - - const osg::Vec3d& osg() const - { return *this; } - osg::Vec3d& osg() - { return *this; } -}; +#endif /// 3D Vector Class template -class SGVec3 : protected SGVec3Storage { +class SGVec3 { public: typedef T value_type; @@ -95,12 +51,14 @@ public: template explicit SGVec3(const SGVec3& d) { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; } + explicit SGVec3(const SGVec2& v2, const T& v3 = 0) + { data()[0] = v2[0]; data()[1] = v2[1]; data()[2] = v3; } +#ifndef NO_OPENSCENEGRAPH_INTERFACE explicit SGVec3(const osg::Vec3f& d) { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; } explicit SGVec3(const osg::Vec3d& d) { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; } - explicit SGVec3(const SGVec2& v2, const T& v3 = 0) - { data()[0] = v2[0]; data()[1] = v2[1]; data()[2] = v3; } +#endif /// Access by index, the index is unchecked const T& operator()(unsigned i) const @@ -135,18 +93,17 @@ public: T& z(void) { return data()[2]; } - /// Get the data pointer - using SGVec3Storage::data; - - /// Readonly interface function to ssg's sgVec3/sgdVec3 - const T (&sg(void) const)[3] - { return data(); } - /// Interface function to ssg's sgVec3/sgdVec3 - T (&sg(void))[3] - { return data(); } + /// Readonly raw storage interface + const T (&data(void) const)[3] + { return _data; } + /// Readonly raw storage interface + T (&data(void))[3] + { return _data; } - /// Interface function to osg's Vec3* - using SGVec3Storage::osg; +#ifndef NO_OPENSCENEGRAPH_INTERFACE + osg::Vec3d osg() const + { return osg::Vec3d(data()[0], data()[1], data()[2]); } +#endif /// Inplace addition SGVec3& operator+=(const SGVec3& v) @@ -180,6 +137,9 @@ public: /// Constructor. Initialize by a geocentric coordinate /// Note that this conversion is relatively expensive to compute static SGVec3 fromGeoc(const SGGeoc& geoc); + +private: + T _data[3]; }; template<> @@ -527,4 +487,26 @@ SGVec3d toVec3d(const SGVec3f& v) { return SGVec3d(v(0), v(1), v(2)); } +#ifndef NO_OPENSCENEGRAPH_INTERFACE +inline +SGVec3d +toSG(const osg::Vec3d& v) +{ return SGVec3d(v[0], v[1], v[2]); } + +inline +SGVec3f +toSG(const osg::Vec3f& v) +{ return SGVec3f(v[0], v[1], v[2]); } + +inline +osg::Vec3d +toOsg(const SGVec3d& v) +{ return osg::Vec3d(v[0], v[1], v[2]); } + +inline +osg::Vec3f +toOsg(const SGVec3f& v) +{ return osg::Vec3f(v[0], v[1], v[2]); } +#endif + #endif diff --git a/simgear/math/SGVec4.hxx b/simgear/math/SGVec4.hxx index 63309495..335393fb 100644 --- a/simgear/math/SGVec4.hxx +++ b/simgear/math/SGVec4.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2006 Mathias Froehlich - Mathias.Froehlich@web.de +// Copyright (C) 2006-2009 Mathias Froehlich - Mathias.Froehlich@web.de // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public @@ -18,58 +18,14 @@ #ifndef SGVec4_H #define SGVec4_H +#ifndef NO_OPENSCENEGRAPH_INTERFACE #include #include - -template -struct SGVec4Storage { - /// Readonly raw storage interface - const T (&data(void) const)[4] - { return _data; } - /// Readonly raw storage interface - T (&data(void))[4] - { return _data; } - - void osg() const - { } - -private: - T _data[4]; -}; - -template<> -struct SGVec4Storage : public osg::Vec4f { - /// Access raw data by index, the index is unchecked - const float (&data(void) const)[4] - { return osg::Vec4f::_v; } - /// Access raw data by index, the index is unchecked - float (&data(void))[4] - { return osg::Vec4f::_v; } - - const osg::Vec4f& osg() const - { return *this; } - osg::Vec4f& osg() - { return *this; } -}; - -template<> -struct SGVec4Storage : public osg::Vec4d { - /// Access raw data by index, the index is unchecked - const double (&data(void) const)[4] - { return osg::Vec4d::_v; } - /// Access raw data by index, the index is unchecked - double (&data(void))[4] - { return osg::Vec4d::_v; } - - const osg::Vec4d& osg() const - { return *this; } - osg::Vec4d& osg() - { return *this; } -}; +#endif /// 4D Vector Class template -class SGVec4 : protected SGVec4Storage { +class SGVec4 { public: typedef T value_type; @@ -95,13 +51,14 @@ public: template explicit SGVec4(const SGVec4& d) { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; } + explicit SGVec4(const SGVec3& v3, const T& v4 = 0) + { data()[0] = v3[0]; data()[1] = v3[1]; data()[2] = v3[2]; data()[3] = v4; } +#ifndef NO_OPENSCENEGRAPH_INTERFACE explicit SGVec4(const osg::Vec4f& d) { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; } explicit SGVec4(const osg::Vec4d& d) { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; } - explicit SGVec4(const SGVec3& v3, const T& v4 = 0) - { data()[0] = v3[0]; data()[1] = v3[1]; data()[2] = v3[2]; data()[3] = v4; } - +#endif /// Access by index, the index is unchecked const T& operator()(unsigned i) const @@ -142,18 +99,17 @@ public: T& w(void) { return data()[3]; } - /// Get the data pointer - using SGVec4Storage::data; - - /// Readonly interface function to ssg's sgVec4/sgdVec4 - const T (&sg(void) const)[4] - { return data(); } - /// Interface function to ssg's sgVec4/sgdVec4 - T (&sg(void))[4] - { return data(); } + /// Readonly raw storage interface + const T (&data(void) const)[4] + { return _data; } + /// Readonly raw storage interface + T (&data(void))[4] + { return _data; } - /// Interface function to osg's Vec4* - using SGVec4Storage::osg; +#ifndef NO_OPENSCENEGRAPH_INTERFACE + osg::Vec4d osg() const + { return osg::Vec4d(data()[0], data()[1], data()[2], data()[3]); } +#endif /// Inplace addition SGVec4& operator+=(const SGVec4& v) @@ -182,6 +138,9 @@ public: { return SGVec4(0, 0, 1, 0); } static SGVec4 e4(void) { return SGVec4(0, 0, 0, 1); } + +private: + T _data[4]; }; /// Unary +, do nothing ... @@ -463,4 +422,26 @@ SGVec4d toVec4d(const SGVec4f& v) { return SGVec4d(v(0), v(1), v(2), v(3)); } +#ifndef NO_OPENSCENEGRAPH_INTERFACE +inline +SGVec4d +toSG(const osg::Vec4d& v) +{ return SGVec4d(v[0], v[1], v[2], v[3]); } + +inline +SGVec4f +toSG(const osg::Vec4f& v) +{ return SGVec4f(v[0], v[1], v[2], v[3]); } + +inline +osg::Vec4d +toOsg(const SGVec4d& v) +{ return osg::Vec4d(v[0], v[1], v[2], v[3]); } + +inline +osg::Vec4f +toOsg(const SGVec4f& v) +{ return osg::Vec4f(v[0], v[1], v[2], v[3]); } +#endif + #endif diff --git a/simgear/scene/sky/dome.cxx b/simgear/scene/sky/dome.cxx index b0c0f6c4..3bdb48ab 100644 --- a/simgear/scene/sky/dome.cxx +++ b/simgear/scene/sky/dome.cxx @@ -274,8 +274,8 @@ SGSkyDome::repaint( const SGVec3f& sun_color, const SGVec3f& sky_color, colors(2, i) = (sky_color - upperVisFactor * diff).osg(); colors(3, i) = (sky_color - middleVisFactor * diff + middle_amt).osg(); colors(4, i) = (fog_color + outer_amt).osg(); - colors(0, i) = simgear::math::lerp(sky_color.osg(), colors(2, i), .3942); - colors(1, i) = simgear::math::lerp(sky_color.osg(), colors(2, i), .7885); + colors(0, i) = simgear::math::lerp(toOsg(sky_color), colors(2, i), .3942); + colors(1, i) = simgear::math::lerp(toOsg(sky_color), colors(2, i), .7885); for (int j = 0; j < numRings - 1; ++j) clampColor(colors(j, i)); outer_amt -= outer_diff; -- 2.39.5