From 6344e0723c28c9ab7aa8e54faf92142cbeb9d5ce Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 23 Apr 2017 15:22:35 -0600 Subject: [PATCH] common: add some stuff to qvec --- include/common/qvec.hh | 78 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 15 deletions(-) diff --git a/include/common/qvec.hh b/include/common/qvec.hh index 1f847508..7b4f11dd 100644 --- a/include/common/qvec.hh +++ b/include/common/qvec.hh @@ -22,6 +22,7 @@ #include #include +#include #ifndef qmax // FIXME: Remove this ifdef #define qmax(a,b) (((a)>(b)) ? (a) : (b)) @@ -74,6 +75,15 @@ public: v[i] = 0; } + /** + * Casting from another vector type of the same length + */ + template + qvec(const qvec &other) { + for (int i=0; i(other[i]); + } + template qvec(const qvec &other) { const int minSize = qmin(N,N2); @@ -146,21 +156,57 @@ public: } }; -template -qvec<3,T> cross(const qvec<3,T> &v1, const qvec<3,T> &v2) { - return qvec<3,T>(v1[1] * v2[2] - v1[2] * v2[1], - v1[2] * v2[0] - v1[0] * v2[2], - v1[0] * v2[1] - v1[1] * v2[0]); -} - -template -T dot(const qvec &v1, const qvec &v2) { - T result = 0; - for (int i=0; i + qvec<3,T> cross(const qvec<3,T> &v1, const qvec<3,T> &v2) { + return qvec<3,T>(v1[1] * v2[2] - v1[2] * v2[1], + v1[2] * v2[0] - v1[0] * v2[2], + v1[0] * v2[1] - v1[1] * v2[0]); } - return result; -} + + template + T dot(const qvec &v1, const qvec &v2) { + T result = 0; + for (int i=0; i + qvec floor(const qvec &v1) { + qvec res; + for (int i=0; i + T length2(const qvec &v1) { + T len2 = 0; + for (int i=0; i + T length(const qvec &v1) { + return std::sqrt(length2(v1)); + } + + template + qvec normalize(const qvec &v1) { + return v1 / length(v1); + } + + template + T distance(const qvec &v1, const qvec &v2) { + return length(v2 - v1); + } +}; + using qvec2f = qvec<2, float>; using qvec3f = qvec<3, float>; @@ -170,6 +216,8 @@ using qvec2d = qvec<2, double>; using qvec3d = qvec<3, double>; using qvec4d = qvec<4, double>; +using qvec2i = qvec<2, int>; + template class qplane3 { private: @@ -181,7 +229,7 @@ public: : m_normal(normal), m_dist(dist) {} - T distAbove(const qvec<3, T> &pt) const { return dot(pt, m_normal) - m_dist; } + T distAbove(const qvec<3, T> &pt) const { return qv::dot(pt, m_normal) - m_dist; } const qvec<3, T> &normal() const { return m_normal; } const T dist() const { return m_dist; } };