]> git.mxchange.org Git - simgear.git/blobdiff - simgear/math/SGVec2.hxx
Canvas: fix element mouse hit detection with OSG 3.3.2.
[simgear.git] / simgear / math / SGVec2.hxx
index 5024e2c19e56c00fe8d99fec47a160359886d14b..1ed150d82f4428f194359b61f8e6135cc093f414 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>
-#endif
+#include <iosfwd>
 
 /// 2D Vector Class
 template<typename T>
@@ -203,6 +196,17 @@ SGVec2<T>
 max(S s, const SGVec2<T>& v)
 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
 
+/// Add two vectors taking care of (integer) overflows. The values are limited
+/// to the respective minimum and maximum values.
+template<class T>
+SGVec2<T> addClipOverflow(SGVec2<T> const& lhs, SGVec2<T> const& rhs)
+{
+  return SGVec2<T>(
+    SGMisc<T>::addClipOverflow(lhs.x(), rhs.x()),
+    SGMisc<T>::addClipOverflow(lhs.y(), rhs.y())
+  );
+}
+
 /// Scalar dot product
 template<typename T>
 inline
@@ -244,7 +248,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>
@@ -331,6 +340,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
@@ -358,27 +379,4 @@ SGVec2d
 toVec2d(const SGVec2f& v)
 { return SGVec2d(v(0), v(1)); }
 
-#ifndef NO_OPENSCENEGRAPH_INTERFACE
-inline
-SGVec2d
-toSG(const osg::Vec2d& v)
-{ return SGVec2d(v[0], v[1]); }
-
-inline
-SGVec2f
-toSG(const osg::Vec2f& v)
-{ return SGVec2f(v[0], v[1]); }
-
-inline
-osg::Vec2d
-toOsg(const SGVec2d& v)
-{ return osg::Vec2d(v[0], v[1]); }
-
-inline
-osg::Vec2f
-toOsg(const SGVec2f& v)
-{ return osg::Vec2f(v[0], v[1]); }
-
-#endif
-
 #endif