/* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ANDROID_VEC_H #define ANDROID_VEC_H #include #include #include #include "traits.h" // ----------------------------------------------------------------------- #define PURE __attribute__((pure)) namespace android { // ----------------------------------------------------------------------- // non-inline helpers template class vec; template class vbase; namespace helpers { template inline T min(T a, T b) { return a inline T max(T a, T b) { return a>b ? a : b; } template < template class VEC, typename TYPE, size_t SIZE, size_t S> vec& doAssign( vec& lhs, const VEC& rhs) { const size_t minSize = min(SIZE, S); const size_t maxSize = max(SIZE, S); for (size_t i=0 ; i class VLHS, template class VRHS, typename TYPE, size_t SIZE > VLHS PURE doAdd( const VLHS& lhs, const VRHS& rhs) { VLHS r; for (size_t i=0 ; i class VLHS, template class VRHS, typename TYPE, size_t SIZE > VLHS PURE doSub( const VLHS& lhs, const VRHS& rhs) { VLHS r; for (size_t i=0 ; i class VEC, typename TYPE, size_t SIZE > VEC PURE doMulScalar( const VEC& lhs, typename TypeTraits::ParameterType rhs) { VEC r; for (size_t i=0 ; i class VEC, typename TYPE, size_t SIZE > VEC PURE doScalarMul( typename TypeTraits::ParameterType lhs, const VEC& rhs) { VEC r; for (size_t i=0 ; i. Without this, an extra conversion to vec<> would be needed. // // example: // vec4_t a; // vec3_t b; // vec3_t c = a.xyz + b; // // "a.xyz + b" is a mixed-operation between a vbase<> and a vec<>, requiring // a conversion of vbase<> to vec<>. The template gunk below avoids this, // by allowing the addition on these different vector types directly // template < template class VLHS, template class VRHS, typename TYPE, size_t SIZE > inline VLHS PURE operator + ( const VLHS& lhs, const VRHS& rhs) { return helpers::doAdd(lhs, rhs); } template < template class VLHS, template class VRHS, typename TYPE, size_t SIZE > inline VLHS PURE operator - ( const VLHS& lhs, const VRHS& rhs) { return helpers::doSub(lhs, rhs); } template < template class VEC, typename TYPE, size_t SIZE > inline VEC PURE operator * ( const VEC& lhs, typename TypeTraits::ParameterType rhs) { return helpers::doMulScalar(lhs, rhs); } template < template class VEC, typename TYPE, size_t SIZE > inline VEC PURE operator * ( typename TypeTraits::ParameterType lhs, const VEC& rhs) { return helpers::doScalarMul(lhs, rhs); } template < template class VLHS, template class VRHS, typename TYPE, size_t SIZE > TYPE PURE dot_product( const VLHS& lhs, const VRHS& rhs) { TYPE r(0); for (size_t i=0 ; i class V, typename TYPE, size_t SIZE > TYPE PURE length(const V& v) { return sqrt(dot_product(v, v)); } template < template class VLHS, template class VRHS, typename TYPE > VLHS PURE cross_product( const VLHS& u, const VRHS& v) { VLHS r; r.x = u.y*v.z - u.z*v.y; r.y = u.z*v.x - u.x*v.z; r.z = u.x*v.y - u.y*v.x; return r; } template vec PURE operator - (const vec& lhs) { vec r; for (size_t i=0 ; i struct vbase { TYPE v[SIZE]; inline const TYPE& operator[](size_t i) const { return v[i]; } inline TYPE& operator[](size_t i) { return v[i]; } }; template<> struct vbase { union { float v[2]; struct { float x, y; }; struct { float s, t; }; }; inline const float& operator[](size_t i) const { return v[i]; } inline float& operator[](size_t i) { return v[i]; } }; template<> struct vbase { union { float v[3]; struct { float x, y, z; }; struct { float s, t, r; }; vbase xy; vbase st; }; inline const float& operator[](size_t i) const { return v[i]; } inline float& operator[](size_t i) { return v[i]; } }; template<> struct vbase { union { float v[4]; struct { float x, y, z, w; }; struct { float s, t, r, q; }; vbase xyz; vbase str; vbase xy; vbase st; }; inline const float& operator[](size_t i) const { return v[i]; } inline float& operator[](size_t i) { return v[i]; } }; // ----------------------------------------------------------------------- template class vec : public vbase { typedef typename TypeTraits::ParameterType pTYPE; typedef vbase base; public: // STL-like interface. typedef TYPE value_type; typedef TYPE& reference; typedef TYPE const& const_reference; typedef size_t size_type; typedef TYPE* iterator; typedef TYPE const* const_iterator; iterator begin() { return base::v; } iterator end() { return base::v + SIZE; } const_iterator begin() const { return base::v; } const_iterator end() const { return base::v + SIZE; } size_type size() const { return SIZE; } // ----------------------------------------------------------------------- // default constructors vec() { } vec(const vec& rhs) : base(rhs) { } vec(const base& rhs) : base(rhs) { } // ----------------------------------------------------------------------- // conversion constructors vec(pTYPE rhs) { for (size_t i=0 ; i class VEC, size_t S> explicit vec(const VEC& rhs) { helpers::doAssign(*this, rhs); } explicit vec(TYPE const* array) { for (size_t i=0 ; i class VEC, size_t S> vec& operator = (const VEC& rhs) { return helpers::doAssign(*this, rhs); } // ----------------------------------------------------------------------- // operation-assignment vec& operator += (const vec& rhs); vec& operator -= (const vec& rhs); vec& operator *= (pTYPE rhs); // ----------------------------------------------------------------------- // non-member function declaration and definition // NOTE: we declare the non-member function as friend inside the class // so that they are known to the compiler when the class is instantiated. // This helps the compiler doing template argument deduction when the // passed types are not identical. Essentially this helps with // type conversion so that you can multiply a vec by an scalar int // (for instance). friend inline vec PURE operator + (const vec& lhs, const vec& rhs) { return helpers::doAdd(lhs, rhs); } friend inline vec PURE operator - (const vec& lhs, const vec& rhs) { return helpers::doSub(lhs, rhs); } friend inline vec PURE operator * (const vec& lhs, pTYPE v) { return helpers::doMulScalar(lhs, v); } friend inline vec PURE operator * (pTYPE v, const vec& rhs) { return helpers::doScalarMul(v, rhs); } friend inline TYPE PURE dot_product(const vec& lhs, const vec& rhs) { return android::dot_product(lhs, rhs); } }; // ----------------------------------------------------------------------- template vec& vec::operator += (const vec& rhs) { vec& lhs(*this); for (size_t i=0 ; i vec& vec::operator -= (const vec& rhs) { vec& lhs(*this); for (size_t i=0 ; i vec& vec::operator *= (vec::pTYPE rhs) { vec& lhs(*this); for (size_t i=0 ; i vec2_t; typedef vec vec3_t; typedef vec vec4_t; // ----------------------------------------------------------------------- }; // namespace android #endif /* ANDROID_VEC_H */