/* * 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_FILTERFW_FILTERPACKS_BASE_VEC_TYPES_H #define ANDROID_FILTERFW_FILTERPACKS_BASE_VEC_TYPES_H namespace android { namespace filterfw { template < class T, int dim> class VecBase { public: T data[dim]; VecBase() {} VecBase& operator = (const VecBase &x) { memcpy(data, x.data, sizeof(T)*dim); return *this; } T & operator [] (int i) { // out of boundary not checked return data[i]; } const T & operator [] (int i) const { // out of boundary not checked return data[i]; } T Length() { double sum = 0; for (int i = 0; i < dim; ++i) sum += static_cast (data[i] * data[i]); return static_cast(sqrt(sum)); } }; template < class T, int dim> class Vec : public VecBase { public: Vec() {} Vec& operator = (const Vec &x) { memcpy(this->data, x.data, sizeof(T)*dim); return *this; } }; template Vec operator + (const Vec &x, const Vec &y) { Vec out; for (int i = 0; i < dim; i++) out.data[i] = x.data[i] + y.data[i]; return out; } template Vec operator - (const Vec &x, const Vec &y) { Vec out; for (int i = 0; i < dim; i++) out.data[i] = x.data[i] - y.data[i]; return out; } template Vec operator * (const Vec &x, const Vec &y) { Vec out; for (int i = 0; i < dim; i++) out.data[i] = x.data[i] * y.data[i]; return out; } template Vec operator / (const Vec &x, const Vec &y) { Vec out; for (int i = 0; i < dim; i++) out.data[i] = x.data[i] / y.data[i]; return out; } template T dot(const Vec &x, const Vec &y) { T out = 0; for (int i = 0; i < dim; i++) out += x.data[i] * y.data[i]; return out; } template Vec operator * (const Vec &x, T scale) { Vec out; for (int i = 0; i < dim; i++) out.data[i] = x.data[i] * scale; return out; } template Vec operator / (const Vec &x, T scale) { Vec out; for (int i = 0; i < dim; i++) out.data[i] = x.data[i] / scale; return out; } template Vec operator + (const Vec &x, T val) { Vec out; for (int i = 0; i < dim; i++) out.data[i] = x.data[i] + val; return out; } // specialization for vec2, vec3, vec4 float template<> class Vec : public VecBase { public: Vec() {} Vec(float x, float y) { data[0] = x; data[1] = y; } Vec& operator = (const Vec &x) { memcpy(data, x.data, sizeof(float)*2); return *this; } }; template<> class Vec { public: float data[3]; Vec() {} Vec(float x, float y, float z) { data[0] = x; data[1] = y; data[2] = z; } Vec& operator = (const Vec &x) { memcpy(data, x.data, sizeof(float)*3); return *this; } }; template<> class Vec { public: float data[4]; Vec() {} Vec(float x, float y, float z, float w) { data[0] = x; data[1] = y; data[2] = z; data[3] = w; } Vec& operator = (const Vec &x) { memcpy(data, x.data, sizeof(float)*4); return *this; } }; typedef Vec Vec2f; typedef Vec Vec3f; typedef Vec Vec4f; } // namespace filterfw } // namespace android #endif // ANDROID_FILTERFW_FILTERPACKS_BASE_VEC_TYPES_H