diff --git a/dlib/geometry/vector.h b/dlib/geometry/vector.h index cd3787d5f..dbea36897 100644 --- a/dlib/geometry/vector.h +++ b/dlib/geometry/vector.h @@ -52,6 +52,23 @@ namespace dlib typedef double type; }; +// ---------------------------------------------------------------------------------------- + + // This insanity here is to work around a bug in visual studio 8. These two rebind + // structures are actually declared at a few points in this file because just having the + // one declaration here isn't enough for visual studio. It takes the three spread around + // to avoid all its bugs. + template + struct vc_rebind + { + typedef vector type; + }; + template + struct vc_rebind_promote + { + typedef vector::type,N> type; + }; + // ---------------------------------------------------------------------------------------- template @@ -175,6 +192,18 @@ namespace dlib !*/ + // This insanity here is to work around a bug in visual studio 8. + template + struct vc_rebind + { + typedef vector type; + }; + template + struct vc_rebind_promote + { + typedef vector::type,N> type; + }; + public: typedef T type; @@ -312,7 +341,7 @@ namespace dlib // --------------------------------------- - vector normalize ( + typename vc_rebind::type normalize ( ) const { const double tmp = std::sqrt((double)(x()*x() + y()*y() + z()*z())); @@ -392,7 +421,7 @@ namespace dlib // --------------------------------------- template - vector::type,3> cross ( + typename vc_rebind_promote::type cross ( const vector& rhs ) const { @@ -456,7 +485,7 @@ namespace dlib // --------------------------------------- template - vector::type,3> operator + ( + typename vc_rebind_promote::type operator + ( const vector& rhs ) const { @@ -476,7 +505,7 @@ namespace dlib // --------------------------------------- template - vector::type,3> operator - ( + typename vc_rebind_promote::type operator - ( const vector& rhs ) const { @@ -496,7 +525,7 @@ namespace dlib // --------------------------------------- template - vector::type,3> operator / ( + typename vc_rebind_promote::type operator / ( const U& val ) const { @@ -555,6 +584,19 @@ namespace dlib - z() == 0 !*/ + // This insanity here is to work around a bug in visual studio 8. + template + struct vc_rebind + { + typedef vector type; + }; + template + struct vc_rebind_promote + { + typedef vector::type,N> type; + }; + + public: typedef T type; @@ -685,13 +727,14 @@ namespace dlib // --------------------------------------- - vector normalize ( + + typename vc_rebind::type normalize ( ) const { const double tmp = std::sqrt((double)(x()*x() + y()*y())); return vector ( x()/tmp, - y()/tmp - ); + y()/tmp + ); } // --------------------------------------- @@ -809,7 +852,7 @@ namespace dlib // --------------------------------------- template - vector::type,N> operator + ( + typename vc_rebind_promote::type operator + ( const vector& rhs ) const { @@ -829,7 +872,7 @@ namespace dlib // --------------------------------------- template - vector::type,N> operator - ( + typename vc_rebind_promote::type operator - ( const vector& rhs ) const { @@ -840,7 +883,7 @@ namespace dlib // --------------------------------------- template - vector::type,2> operator / ( + typename vc_rebind_promote::type operator / ( const U& val ) const { @@ -899,7 +942,7 @@ namespace dlib // --------------------------------------- template - vector::type,3> cross ( + typename vc_rebind_promote::type cross ( const vector& rhs ) const { @@ -921,45 +964,49 @@ namespace dlib template - inline typename disable_if, const vector >::type operator* ( + inline typename disable_if, const typename vc_rebind_promote::type >::type operator* ( const vector& v, const U& s ) { - return vector(v.x()*s, v.y()*s); + typedef typename vc_rebind_promote::type ret_type; + return ret_type(v.x()*s, v.y()*s); } // ---------------------------------------------------------------------------------------- template - inline typename disable_if, const vector >::type operator* ( + inline typename disable_if, const typename vc_rebind_promote::type >::type operator* ( const U& s, const vector& v ) { - return vector(v.x()*s, v.y()*s); + typedef typename vc_rebind_promote::type ret_type; + return ret_type(v.x()*s, v.y()*s); } // ---------------------------------------------------------------------------------------- template - inline typename disable_if, const vector >::type operator* ( + inline typename disable_if, const typename vc_rebind_promote::type >::type operator* ( const vector& v, const U& s ) { - return vector(v.x()*s, v.y()*s, v.z()*s); + typedef typename vc_rebind_promote::type ret_type; + return ret_type(v.x()*s, v.y()*s, v.z()*s); } // ---------------------------------------------------------------------------------------- template - inline typename disable_if, const vector >::type operator* ( + inline typename disable_if, const typename vc_rebind_promote::type >::type operator* ( const U& s, const vector& v ) { - return vector(v.x()*s, v.y()*s, v.z()*s); + typedef typename vc_rebind_promote::type ret_type; + return ret_type(v.x()*s, v.y()*s, v.z()*s); } // ---------------------------------------------------------------------------------------- diff --git a/dlib/test/geometry.cpp b/dlib/test/geometry.cpp index 9f61a6dd6..9bdd8b9d6 100644 --- a/dlib/test/geometry.cpp +++ b/dlib/test/geometry.cpp @@ -202,6 +202,19 @@ namespace DLIB_CASSERT(vd3.y() == 4*2 + 5*3 + 6*4,""); DLIB_CASSERT(vd3.z() == 7*2 + 8*3 + 9*4,""); + (vd3*2).dot(vd3); + (vd2*2).dot(vd3); + (vd3*2).dot(vd2); + (vd2*2).dot(vd2); + (2*vd3*2).dot(vd3); + (2*vd2*2).dot(vd3); + (2*vd3*2).dot(vd2); + (2*vd2*2).dot(vd2); + + (vd2 + vd3).dot(vd2); + (vd2 - vd3).dot(vd2); + (vd2/2).dot(vd2); + (vd3/2).dot(vd2); } }