]> git.mxchange.org Git - simgear.git/blobdiff - simgear/math/SGVec4.hxx
use the proper namespace
[simgear.git] / simgear / math / SGVec4.hxx
index 335393fbeb6f94591d94e435eb4d61334030579e..4339dfd9b79879a3fab0da2b59a5cca747cc5578 100644 (file)
 #ifndef SGVec4_H
 #define SGVec4_H
 
-#ifndef NO_OPENSCENEGRAPH_INTERFACE
-#include <osg/Vec4f>
-#include <osg/Vec4d>
-#endif
+#include <iosfwd>
 
 /// 4D Vector Class
 template<typename T>
@@ -53,12 +50,6 @@ public:
   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
   explicit SGVec4(const SGVec3<T>& 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 +97,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; }
@@ -258,6 +244,19 @@ max(S s, const SGVec4<T>& v)
                    SGMisc<T>::max(s, v(3)));
 }
 
+/// Add two vectors taking care of (integer) overflows. The values are limited
+/// to the respective minimum and maximum values.
+template<class T>
+SGVec4<T> addClipOverflow(SGVec4<T> const& lhs, SGVec4<T> const& rhs)
+{
+  return SGVec4<T>(
+    SGMisc<T>::addClipOverflow(lhs.x(), rhs.x()),
+    SGMisc<T>::addClipOverflow(lhs.y(), rhs.y()),
+    SGMisc<T>::addClipOverflow(lhs.z(), rhs.z()),
+    SGMisc<T>::addClipOverflow(lhs.w(), rhs.w())
+  );
+}
+
 /// Scalar dot product
 template<typename T>
 inline
@@ -299,7 +298,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>
@@ -394,6 +398,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
@@ -422,26 +438,4 @@ SGVec4d
 toVec4d(const SGVec4f& v)
 { return SGVec4d(v(0), v(1), v(2), v(3)); }
 
-#ifndef NO_OPENSCENEGRAPH_INTERFACE
-inline
-SGVec4d
-toSG(const osg::Vec4d& v)
-{ return SGVec4d(v[0], v[1], v[2], v[3]); }
-
-inline
-SGVec4f
-toSG(const osg::Vec4f& v)
-{ return SGVec4f(v[0], v[1], v[2], v[3]); }
-
-inline
-osg::Vec4d
-toOsg(const SGVec4d& v)
-{ return osg::Vec4d(v[0], v[1], v[2], v[3]); }
-
-inline
-osg::Vec4f
-toOsg(const SGVec4f& v)
-{ return osg::Vec4f(v[0], v[1], v[2], v[3]); }
-#endif
-
 #endif