/* Bullet Continuous Collision Detection and Physics Library Maya Plugin Copyright (c) 2008 Walt Disney Studios This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Written by: Nicola Candussi */ //vec.h #ifndef MVL_VEC_H #define MVL_VEC_H #include #include #include "base.h" #include "traits.h" namespace mvl { template class vec { public: typedef T value_type; typedef T& reference; typedef T const& const_reference; typedef T* iterator; typedef T const* const_iterator; typedef std::reverse_iterator reverse_iterator; typedef std::reverse_iterator const_reverse_iterator; public: enum { Size = Sz, }; public: //constructors explicit vec() {} template vec(vec const& v) { *this = v; } explicit vec(value_type val) { for(int i = 0; i < Size; ++i) { m_data[i] = val; } } explicit vec(value_type x0, value_type x1) { m_data[0] = x0; m_data[1] = x1; } explicit vec(value_type x0, value_type x1, value_type x2) { m_data[0] = x0; m_data[1] = x1; m_data[2] = x2; } explicit vec(value_type x0, value_type x1, value_type x2, value_type x3) { m_data[0] = x0; m_data[1] = x1; m_data[2] = x2; m_data[3] = x3; } explicit vec(value_type x0, value_type x1, value_type x2, value_type x3, value_type x4) { m_data[0] = x0; m_data[1] = x1; m_data[2] = x2; m_data[3] = x3; m_data[4] = x4; } explicit vec(value_type x0, value_type x1, value_type x2, value_type x3, value_type x4, value_type x5) { m_data[0] = x0; m_data[1] = x1; m_data[2] = x2; m_data[3] = x3; m_data[4] = x4; m_data[5] = x5; } public: //data access value_type operator[](std::size_t i) const { return m_data[i]; } reference operator[](std::size_t i) { return m_data[i]; } value_type operator()(std::size_t i) const { return m_data[i]; } reference operator()(std::size_t i) { return m_data[i]; } public: //stl static std::size_t size() { return Size; } static std::size_t max_size() { return Size; } static bool empty() { return false; } iterator begin() { return m_data; } iterator end() { return m_data + Size; } const_iterator begin() const { return m_data; } const_iterator end() const { return m_data + Size; } reverse_iterator rbegin() { return reverse_iterator(end()); } reverse_iterator rend() { return reverse_iterator(begin()); } const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } value_type front() { return m_data[0]; } value_type back() { return m_data[Size - 1]; } const_reference front() const { return m_data[0]; } const_reference back() const { return m_data[Size - 1]; } friend std::ostream& operator << (std::ostream& out, vec const& v) { out << "("; for(size_t i = 0; i < Size - 1; i++) { out << v(i) << ", "; } out << v(Size - 1) << ")"; return out; } public: //assignment vec& operator=(vec const& rhs) { for(int i = 0; i < Size; ++i) { m_data[i] = rhs[i]; } return *this; } template vec& operator=(vec const& rhs) { for(int i = 0; i < Size; ++i) { m_data[i] = rhs[i]; } return *this; } private: T m_data[Size]; }; //assignment operators // OP(vec, vec) // OP(vec, T) #define VEC_IMPLEMENT_MACRO(OP) \ template \ inline \ vec& \ operator OP(vec& lhs, vec const& rhs) { \ for(int i = 0; i < Sz; ++i) { \ lhs[i] OP rhs[i]; \ } \ return lhs; \ } \ \ template \ inline \ vec& \ operator OP(vec& lhs, T const& rhs) { \ for(int i = 0; i < Sz; ++i) { \ lhs[i] OP rhs; \ } \ return lhs; \ } \ VEC_IMPLEMENT_MACRO(+=) VEC_IMPLEMENT_MACRO(-=) VEC_IMPLEMENT_MACRO(*=) VEC_IMPLEMENT_MACRO(/=) #undef VEC_IMPLEMENT_MACRO //operator + (vec, vec) template inline vec::value_type, Sz> operator + (vec const& lhs, vec const& rhs) { vec::value_type, Sz> res; for(int i = 0; i < Sz; ++i) { res[i] = lhs[i] + rhs[i]; } return res; } //operator - (vec, vec) template inline vec::value_type, Sz> operator - (vec const& lhs, vec const& rhs) { vec::value_type, Sz> res; for(int i = 0; i < Sz; ++i) { res[i] = lhs[i] - rhs[i]; } return res; } //operator * (vec, POD) template inline vec::value_type, Sz> operator * (vec const& lhs, T2 const& rhs) { vec::value_type, Sz> res; for(int i = 0; i < Sz; ++i) { res[i] = lhs[i] * rhs; } return res; } //operator * (POD, vec) template inline vec::value_type, Sz> operator * (T1 const& lhs, vec const& rhs) { vec::value_type, Sz> res; for(int i = 0; i < Sz; ++i) { res[i] = lhs * rhs[i]; } return res; } //operator / (vec, POD) template inline vec::value_type, Sz> operator / (vec const& lhs, T2 const& rhs) { vec::value_type, Sz> res; for(int i = 0; i < Sz; ++i) { res[i] = lhs[i] / rhs; } return res; } //element_prod(vec, vec) template inline vec::value_type, Sz> element_prod(vec const& lhs, vec const& rhs) { vec::value_type, Sz> res; for(int i = 0; i < Sz; ++i) { res[i] = lhs[i] * rhs[i]; } return res; } //element_div(vec, vec) template inline vec::value_type, Sz> element_div(vec const& lhs, vec const& rhs) { vec::value_type, Sz> res; for(int i = 0; i < Sz; ++i) { res[i] = lhs[i] / rhs[i]; } return res; } //unary operator -(expr_vec) template inline vec operator -(vec const& rhs) { vec res; for(int i = 0; i < Sz; ++i) { res[i] = -rhs[i]; } return res; } //dot product template inline typename promote_traits::value_type dot(vec const& lhs, vec const& rhs) { typename promote_traits::value_type res(0); for(int i = 0; i < Sz; ++i) { res += rhs[i] * lhs[i]; } return res; } //cross product template inline vec::value_type, 3> cross(vec const& lhs, vec const& rhs) { typedef typename promote_traits::value_type value_type; return vec(lhs(1)*rhs(2) - rhs(1)*lhs(2), rhs(0)*lhs(2) - lhs(0)*rhs(2), lhs(0)*rhs(1) - rhs(0)*lhs(1)); } //length of the vector template inline T norm2(vec const& rhs) { return static_cast(sqrt(dot(rhs, rhs))); } //length of the vector squared template inline T norm_squared(vec const& rhs) { return dot(rhs, rhs); } //normalize the vector template inline vec normalize(vec const& v) { typedef T value_type; T tmp = norm2(v); if(tmp == value_type(0)) { tmp = value_type(0); } else { tmp = value_type(1) / tmp; } vec res; for(int i = 0; i < Sz; ++i) { res[i] = v[i] * tmp; } return res; } } //namespace mvl #endif