X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fmath%2FSGVec4.hxx;h=6030c48ba03e2ea331b43442cebfd2df509fdf1b;hb=11479cd8c386d8bf7e1fee7bed60ab4abefc5fad;hp=baf9137daa2aba31e290c5c81a022f72d2254847;hpb=9cbbe5559844317f44744788ddb308101a1e75e9;p=simgear.git diff --git a/simgear/math/SGVec4.hxx b/simgear/math/SGVec4.hxx index baf9137d..6030c48b 100644 --- a/simgear/math/SGVec4.hxx +++ b/simgear/math/SGVec4.hxx @@ -288,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 @@ -383,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