]> git.mxchange.org Git - simgear.git/blobdiff - simgear/math/SGVec4.hxx
Merge branch 'ehofman/sound'
[simgear.git] / simgear / math / SGVec4.hxx
index baf9137daa2aba31e290c5c81a022f72d2254847..6030c48ba03e2ea331b43442cebfd2df509fdf1b 100644 (file)
@@ -288,7 +288,12 @@ template<typename T>
 inline
 SGVec4<T>
 normalize(const SGVec4<T>& v)
-{ return (1/norm(v))*v; }
+{
+  T normv = norm(v);
+  if (normv <= SGLimits<T>::min())
+    return SGVec4<T>::zeros();
+  return (1/normv)*v;
+}
 
 /// Return true if exactly the same
 template<typename T>
@@ -383,6 +388,18 @@ T
 distSqr(const SGVec4<T>& v1, const SGVec4<T>& v2)
 { SGVec4<T> tmp = v1 - v2; return dot(tmp, tmp); }
 
+// calculate the projection of u along the direction of d.
+template<typename T>
+inline
+SGVec4<T>
+projection(const SGVec4<T>& u, const SGVec4<T>& d)
+{
+  T denom = dot(d, d);
+  T ud = dot(u, d);
+  if (SGLimits<T>::min() < denom) return u;
+  else return d * (dot(u, d) / denom);
+}
+
 #ifndef NDEBUG
 template<typename T>
 inline