X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fmath%2FSGVec2.hxx;h=0695cded19f232e7cc14b698e2f1c6892be2e0fe;hb=914d3e6a2b323cf9f186cbef2aef7865ea07b309;hp=1e683b49aceb7674ca32b00856b65cc4df1c88f5;hpb=ce54997be569ebcac4222321e7ae3fecccd01324;p=simgear.git diff --git a/simgear/math/SGVec2.hxx b/simgear/math/SGVec2.hxx index 1e683b49..0695cded 100644 --- a/simgear/math/SGVec2.hxx +++ b/simgear/math/SGVec2.hxx @@ -18,14 +18,7 @@ #ifndef SGVec2_H #define SGVec2_H -#if defined ( __CYGWIN__ ) -#include -#endif - -#ifndef NO_OPENSCENEGRAPH_INTERFACE -#include -#include -#endif +#include /// 2D Vector Class template @@ -55,12 +48,6 @@ 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 @@ -96,11 +83,6 @@ public: T (&data(void))[2] { return _data; } -#ifndef NO_OPENSCENEGRAPH_INTERFACE - osg::Vec2d osg() const - { return osg::Vec2d(data()[0], data()[1]); } -#endif - /// Inplace addition SGVec2& operator+=(const SGVec2& v) { data()[0] += v(0); data()[1] += v(1); return *this; } @@ -255,7 +237,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 @@ -342,6 +329,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 @@ -369,27 +368,4 @@ 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