]> git.mxchange.org Git - simgear.git/blobdiff - simgear/math/SGVec2.hxx
cmake: add option to disable building tests
[simgear.git] / simgear / math / SGVec2.hxx
index 1e683b49aceb7674ca32b00856b65cc4df1c88f5..2a40564145c39e68ad13426a4b556f92a0ad6506 100644 (file)
 #ifndef SGVec2_H
 #define SGVec2_H
 
-#if defined ( __CYGWIN__ )
-#include <ieeefp.h>
-#endif
-
 #ifndef NO_OPENSCENEGRAPH_INTERFACE
 #include <osg/Vec2f>
 #include <osg/Vec2d>
@@ -55,12 +51,6 @@ public:
   template<typename S>
   explicit SGVec2(const SGVec2<S>& 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 +86,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 +240,12 @@ template<typename T>
 inline
 SGVec2<T>
 normalize(const SGVec2<T>& v)
-{ return (1/norm(v))*v; }
+{
+  T normv = norm(v);
+  if (normv <= SGLimits<T>::min())
+    return SGVec2<T>::zeros();
+  return (1/normv)*v;
+}
 
 /// Return true if exactly the same
 template<typename T>
@@ -342,6 +332,18 @@ T
 distSqr(const SGVec2<T>& v1, const SGVec2<T>& v2)
 { SGVec2<T> tmp = v1 - v2; return dot(tmp, tmp); }
 
+// calculate the projection of u along the direction of d.
+template<typename T>
+inline
+SGVec2<T>
+projection(const SGVec2<T>& u, const SGVec2<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