X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fmath%2FSGVec2.hxx;h=2a40564145c39e68ad13426a4b556f92a0ad6506;hb=7a52c2fa71355631a559d74e0860fa6c5efa424e;hp=f910499133f8fdd24e02c8055dda186867e0fc98;hpb=006f90997a8eef6704de2511e38fcc786672308d;p=simgear.git diff --git a/simgear/math/SGVec2.hxx b/simgear/math/SGVec2.hxx index f9104991..2a405641 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 @@ -18,62 +18,14 @@ #ifndef SGVec2_H #define SGVec2_H -#if defined ( __CYGWIN__ ) -#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 +51,6 @@ public: template explicit SGVec2(const SGVec2& d) { data()[0] = d[0]; data()[1] = d[1]; } - 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]; } /// Access by index, the index is unchecked const T& operator()(unsigned i) const @@ -131,18 +79,12 @@ 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(); } - - /// Interface function to osg's Vec2* - using SGVec2Storage::osg; + /// Access raw data + const T (&data(void) const)[2] + { return _data; } + /// Access raw data + T (&data(void))[2] + { return _data; } /// Inplace addition SGVec2& operator+=(const SGVec2& v) @@ -167,6 +109,9 @@ public: { return SGVec2(1, 0); } static SGVec2 e2(void) { return SGVec2(0, 1); } + +private: + T _data[2]; }; /// Unary +, do nothing ... @@ -295,7 +240,12 @@ template inline SGVec2 normalize(const SGVec2& v) -{ return (1/norm(v))*v; } +{ + T normv = norm(v); + if (normv <= SGLimits::min()) + return SGVec2::zeros(); + return (1/normv)*v; +} /// Return true if exactly the same template @@ -382,6 +332,18 @@ T distSqr(const SGVec2& v1, const SGVec2& v2) { SGVec2 tmp = v1 - v2; return dot(tmp, tmp); } +// calculate the projection of u along the direction of d. +template +inline +SGVec2 +projection(const SGVec2& u, const SGVec2& d) +{ + T denom = dot(d, d); + T ud = dot(u, d); + if (SGLimits::min() < denom) return u; + else return d * (dot(u, d) / denom); +} + #ifndef NDEBUG template inline @@ -409,4 +371,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