X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fmath%2FSGVec4.hxx;h=6030c48ba03e2ea331b43442cebfd2df509fdf1b;hb=3cd4c5566f8c418bc9bbb6026072eb0192027993;hp=335393fbeb6f94591d94e435eb4d61334030579e;hpb=ce54997be569ebcac4222321e7ae3fecccd01324;p=simgear.git diff --git a/simgear/math/SGVec4.hxx b/simgear/math/SGVec4.hxx index 335393fb..6030c48b 100644 --- a/simgear/math/SGVec4.hxx +++ b/simgear/math/SGVec4.hxx @@ -53,12 +53,6 @@ public: { 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]; } -#endif /// Access by index, the index is unchecked const T& operator()(unsigned i) const @@ -106,11 +100,6 @@ public: T (&data(void))[4] { return _data; } -#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) { data()[0]+=v(0);data()[1]+=v(1);data()[2]+=v(2);data()[3]+=v(3);return *this; } @@ -299,7 +288,12 @@ template inline SGVec4 normalize(const SGVec4& v) -{ return (1/norm(v))*v; } +{ + T normv = norm(v); + if (normv <= SGLimits::min()) + return SGVec4::zeros(); + return (1/normv)*v; +} /// Return true if exactly the same template @@ -394,6 +388,18 @@ T distSqr(const SGVec4& v1, const SGVec4& v2) { SGVec4 tmp = v1 - v2; return dot(tmp, tmp); } +// calculate the projection of u along the direction of d. +template +inline +SGVec4 +projection(const SGVec4& u, const SGVec4& 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