-// Copyright (C) 2006 Mathias Froehlich - Mathias.Froehlich@web.de
+// Copyright (C) 2006-2009 Mathias Froehlich - Mathias.Froehlich@web.de
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
#ifndef SGVec2_H
#define SGVec2_H
+#ifndef NO_OPENSCENEGRAPH_INTERFACE
#include <osg/Vec2f>
#include <osg/Vec2d>
-
-template<typename T>
-struct SGVec2Storage {
- /// Readonly raw storage interface
- const T (&data(void) const)[2]
- { return _data; }
- /// Readonly raw storage interface
- T (&data(void))[2]
- { return _data; }
-
- void osg() const
- { }
-
-private:
- T _data[2];
-};
-
-template<>
-struct SGVec2Storage<float> : public osg::Vec2f {
- /// Access raw data by index, the index is unchecked
- const float (&data(void) const)[2]
- { return osg::Vec2f::_v; }
- /// Access raw data by index, the index is unchecked
- float (&data(void))[2]
- { return osg::Vec2f::_v; }
-
- const osg::Vec2f& osg() const
- { return *this; }
- osg::Vec2f& osg()
- { return *this; }
-};
-
-template<>
-struct SGVec2Storage<double> : public osg::Vec2d {
- /// Access raw data by index, the index is unchecked
- const double (&data(void) const)[2]
- { return osg::Vec2d::_v; }
- /// Access raw data by index, the index is unchecked
- double (&data(void))[2]
- { return osg::Vec2d::_v; }
-
- const osg::Vec2d& osg() const
- { return *this; }
- osg::Vec2d& osg()
- { return *this; }
-};
+#endif
/// 2D Vector Class
template<typename T>
-class SGVec2 : protected SGVec2Storage<T> {
+class SGVec2 {
public:
typedef T value_type;
/// make sure it has at least 2 elements
explicit SGVec2(const T* d)
{ data()[0] = d[0]; data()[1] = d[1]; }
- explicit SGVec2(const osg::Vec2f& d)
- { data()[0] = d[0]; data()[1] = d[1]; }
- explicit SGVec2(const osg::Vec2d& d)
+ template<typename S>
+ explicit SGVec2(const SGVec2<S>& d)
{ data()[0] = d[0]; data()[1] = d[1]; }
/// Access by index, the index is unchecked
T& y(void)
{ return data()[1]; }
- /// Get the data pointer
- using SGVec2Storage<T>::data;
-
- /// Readonly interface function to ssg's sgVec2/sgdVec2
- const T (&sg(void) const)[2]
- { return data(); }
- /// Interface function to ssg's sgVec2/sgdVec2
- T (&sg(void))[2]
- { return data(); }
-
- /// Interface function to osg's Vec2*
- using SGVec2Storage<T>::osg;
+ /// Access raw data
+ const T (&data(void) const)[2]
+ { return _data; }
+ /// Access raw data
+ T (&data(void))[2]
+ { return _data; }
/// Inplace addition
SGVec2& operator+=(const SGVec2& v)
{ return SGVec2(1, 0); }
static SGVec2 e2(void)
{ return SGVec2(0, 1); }
+
+private:
+ T _data[2];
};
/// Unary +, do nothing ...
operator*(const SGVec2<T>& v, S s)
{ return SGVec2<T>(s*v(0), s*v(1)); }
+/// multiplication as a multiplicator, that is assume that the first vector
+/// represents a 2x2 diagonal matrix with the diagonal elements in the vector.
+/// Then the result is the product of that matrix times the second vector.
+template<typename T>
+inline
+SGVec2<T>
+mult(const SGVec2<T>& v1, const SGVec2<T>& v2)
+{ return SGVec2<T>(v1(0)*v2(0), v1(1)*v2(1)); }
+
+/// component wise min
+template<typename T>
+inline
+SGVec2<T>
+min(const SGVec2<T>& v1, const SGVec2<T>& v2)
+{return SGVec2<T>(SGMisc<T>::min(v1(0), v2(0)), SGMisc<T>::min(v1(1), v2(1)));}
+template<typename S, typename T>
+inline
+SGVec2<T>
+min(const SGVec2<T>& v, S s)
+{ return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
+template<typename S, typename T>
+inline
+SGVec2<T>
+min(S s, const SGVec2<T>& v)
+{ return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
+
+/// component wise max
+template<typename T>
+inline
+SGVec2<T>
+max(const SGVec2<T>& v1, const SGVec2<T>& v2)
+{return SGVec2<T>(SGMisc<T>::max(v1(0), v2(0)), SGMisc<T>::max(v1(1), v2(1)));}
+template<typename S, typename T>
+inline
+SGVec2<T>
+max(const SGVec2<T>& v, S s)
+{ return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
+template<typename S, typename T>
+inline
+SGVec2<T>
+max(S s, const SGVec2<T>& v)
+{ return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
+
/// Scalar dot product
template<typename T>
inline
norm1(const SGVec2<T>& v)
{ return fabs(v(0)) + fabs(v(1)); }
+/// The inf-norm of the vector
+template<typename T>
+inline
+T
+normI(const SGVec2<T>& v)
+{ return SGMisc<T>::max(fabs(v(0)), fabs(v(1))); }
+
/// The euclidean norm of the vector, that is what most people call length
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>
operator!=(const SGVec2<T>& v1, const SGVec2<T>& v2)
{ return ! (v1 == v2); }
+/// Return true if smaller, good for putting that into a std::map
+template<typename T>
+inline
+bool
+operator<(const SGVec2<T>& v1, const SGVec2<T>& v2)
+{
+ if (v1(0) < v2(0)) return true;
+ else if (v2(0) < v1(0)) return false;
+ else return (v1(1) < v2(1));
+}
+
+template<typename T>
+inline
+bool
+operator<=(const SGVec2<T>& v1, const SGVec2<T>& v2)
+{
+ if (v1(0) < v2(0)) return true;
+ else if (v2(0) < v1(0)) return false;
+ else return (v1(1) <= v2(1));
+}
+
+template<typename T>
+inline
+bool
+operator>(const SGVec2<T>& v1, const SGVec2<T>& v2)
+{ return operator<(v2, v1); }
+
+template<typename T>
+inline
+bool
+operator>=(const SGVec2<T>& v1, const SGVec2<T>& v2)
+{ return operator<=(v2, v1); }
+
/// Return true if equal to the relative tolerance tol
template<typename T>
inline
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
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