vec_types.h revision 65953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#ifndef ANDROID_FILTERFW_FILTERPACKS_BASE_VEC_TYPES_H 18#define ANDROID_FILTERFW_FILTERPACKS_BASE_VEC_TYPES_H 19 20namespace android { 21namespace filterfw { 22 23template < class T, int dim> 24class VecBase { 25 public: 26 T data[dim]; 27 VecBase() {} 28 VecBase<T,dim>& operator = (const VecBase<T, dim> &x) { 29 memcpy(data, x.data, sizeof(T)*dim); 30 return *this; 31 } 32 T & operator [] (int i) { 33 // out of boundary not checked 34 return data[i]; 35 } 36 const T & operator [] (int i) const { 37 // out of boundary not checked 38 return data[i]; 39 } 40 T Length() { 41 double sum = 0; 42 for (int i = 0; i < dim; ++i) 43 sum += static_cast<double> (data[i] * data[i]); 44 return static_cast<T>(sqrt(sum)); 45 } 46}; 47 48template < class T, int dim> 49class Vec : public VecBase<T,dim> { 50 public: 51 Vec() {} 52 Vec<T,dim>& operator = (const Vec<T, dim> &x) { 53 memcpy(this->data, x.data, sizeof(T)*dim); 54 return *this; 55 } 56}; 57 58template <class T, int dim> 59Vec<T, dim> operator + (const Vec<T,dim> &x, const Vec<T,dim> &y) { 60 Vec<T, dim> out; 61 for (int i = 0; i < dim; i++) 62 out.data[i] = x.data[i] + y.data[i]; 63 return out; 64} 65 66template <class T, int dim> 67Vec<T, dim> operator - (const Vec<T,dim> &x, const Vec<T,dim> &y) { 68 Vec<T, dim> out; 69 for (int i = 0; i < dim; i++) 70 out.data[i] = x.data[i] - y.data[i]; 71 return out; 72} 73 74template <class T, int dim> 75Vec<T, dim> operator * (const Vec<T,dim> &x, const Vec<T,dim> &y) { 76 Vec<T, dim> out; 77 for (int i = 0; i < dim; i++) 78 out.data[i] = x.data[i] * y.data[i]; 79 return out; 80} 81 82template <class T, int dim> 83Vec<T, dim> operator / (const Vec<T,dim> &x, const Vec<T,dim> &y) { 84 Vec<T, dim> out; 85 for (int i = 0; i < dim; i++) 86 out.data[i] = x.data[i] / y.data[i]; 87 return out; 88} 89 90template <class T, int dim> 91T dot(const Vec<T,dim> &x, const Vec<T,dim> &y) { 92 T out = 0; 93 for (int i = 0; i < dim; i++) 94 out += x.data[i] * y.data[i]; 95 return out; 96} 97 98template <class T, int dim> 99Vec<T, dim> operator * (const Vec<T,dim> &x, T scale) { 100 Vec<T, dim> out; 101 for (int i = 0; i < dim; i++) 102 out.data[i] = x.data[i] * scale; 103 return out; 104} 105 106template <class T, int dim> 107Vec<T, dim> operator / (const Vec<T,dim> &x, T scale) { 108 Vec<T, dim> out; 109 for (int i = 0; i < dim; i++) 110 out.data[i] = x.data[i] / scale; 111 return out; 112} 113 114template <class T, int dim> 115Vec<T, dim> operator + (const Vec<T,dim> &x, T val) { 116 Vec<T, dim> out; 117 for (int i = 0; i < dim; i++) 118 out.data[i] = x.data[i] + val; 119 return out; 120} 121 122// specialization for vec2, vec3, vec4 float 123template<> 124class Vec<float, 2> : public VecBase<float, 2> { 125public: 126 Vec() {} 127 Vec(float x, float y) { 128 data[0] = x; 129 data[1] = y; 130 } 131 Vec<float, 2>& operator = (const Vec<float, 2> &x) { 132 memcpy(data, x.data, sizeof(float)*2); 133 return *this; 134 } 135}; 136 137template<> 138class Vec<float, 3> { 139public: 140 float data[3]; 141 Vec() {} 142 Vec(float x, float y, float z) { 143 data[0] = x; 144 data[1] = y; 145 data[2] = z; 146 } 147 Vec<float, 3>& operator = (const Vec<float, 3> &x) { 148 memcpy(data, x.data, sizeof(float)*3); 149 return *this; 150 } 151}; 152 153template<> 154class Vec<float, 4> { 155public: 156 float data[4]; 157 Vec() {} 158 Vec(float x, float y, float z, float w) { 159 data[0] = x; 160 data[1] = y; 161 data[2] = z; 162 data[3] = w; 163 } 164 Vec<float, 4>& operator = (const Vec<float, 4> &x) { 165 memcpy(data, x.data, sizeof(float)*4); 166 return *this; 167 } 168}; 169 170typedef Vec<float,2> Vec2f; 171typedef Vec<float,3> Vec3f; 172typedef Vec<float,4> Vec4f; 173 174} // namespace filterfw 175} // namespace android 176 177#endif // ANDROID_FILTERFW_FILTERPACKS_BASE_VEC_TYPES_H 178