/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ // Original file Copyright Crytek GMBH or its affiliates, used under license. // Description : vectorn template class declaration and inlined implementation #ifndef CRYINCLUDE_CRYPHYSICS_VECTORN_H #define CRYINCLUDE_CRYPHYSICS_VECTORN_H #pragma once #if ENABLE_CRY_PHYSICS template class matrix_vector_product_tpl { public: matrix_vector_product_tpl(int nrows, int ncols, int _istride, int _jstride, ftype* pmtxdata, ftype* pvecdata) { nRows = nrows; nCols = ncols; istride = _istride; jstride = _jstride; mtxdata = pmtxdata; vecdata = pvecdata; } int nRows, nCols; int istride, jstride; ftype* mtxdata, * vecdata; }; template class vector_scalar_product_tpl { public: vector_scalar_product_tpl(int ncols, ftype* pdata, ftype scalar) { len = ncols; data = pdata; op = scalar; } ftype* data; int len; ftype op; }; template class vectorn_tpl { public: vectorn_tpl(int _len, ftype* pdata = 0) { len = _len; if (pdata) { flags = mtx_foreign_data; data = pdata; } #if defined(USE_MATRIX_POOLS) else if (len < 64) { if (vecn_pool_pos + len > vecn_pool_size) { vecn_pool_pos = 0; } data = vecn_pool + vecn_pool_pos; vecn_pool_pos += len; flags = mtx_foreign_data; } #endif else { data = new ftype[len]; flags = 0; } } #if defined(__GNUC__) && __GNUC__ >= 4 // Copy constructor to make GCC4 happy. The copy constructor is never // really used, but GCC4 won't complile this if it is not there... vectorn_tpl(const vectorn_tpl& src) { abort(); } #endif vectorn_tpl(vectorn_tpl& src) { flags = src.flags & mtx_foreign_data; data = src.data; len = src.len; src.flags |= mtx_foreign_data; } ~vectorn_tpl() { if (!(flags & mtx_foreign_data)) { delete[] data; } } vectorn_tpl& operator=(const vectorn_tpl& src) { if (src.len != len && !(flags & mtx_foreign_data)) { delete[] data; data = new ftype[src.len]; } len = src.len; for (int i = 0; i < len; i++) { data[i] = src.data[i]; } return *this; } template vectorn_tpl& operator=(const vectorn_tpl& src) { if (src.len != len && !(flags & mtx_foreign_data)) { delete[] data; data = new ftype[src.len]; } len = src.len; for (int i = 0; i < len; i++) { data[i] = src.data[i]; } return *this; } vectorn_tpl& operator=(const matrix_vector_product_tpl& src) { int i, j; for (i = 0; i < src.nRows; i++) { for (data[i] = 0, j = 0; j < src.nCols; j++) { data[i] += src.mtxdata[i * src.istride + j * src.jstride] * src.vecdata[j]; } } return *this; } vectorn_tpl& operator+=(const matrix_vector_product_tpl& src) { int i, j; for (i = 0; i < src.nRows; i++) { for (j = 0; j < src.nCols; j++) { data[i] += src.mtxdata[i * src.istride + j * src.jstride] * src.vecdata[j]; } } return *this; } vectorn_tpl& operator-=(const matrix_vector_product_tpl& src) { int i, j; for (i = 0; i < src.nRows; i++) { for (j = 0; j < src.nCols; j++) { data[i] -= src.mtxdata[i * src.istride + j * src.jstride] * src.vecdata[j]; } } return *this; } vectorn_tpl& operator=(const vector_scalar_product_tpl& src) { for (int i = 0; i < src.len; i++) { data[i] = src.data[i]; } return *this; } vectorn_tpl& operator+=(const vector_scalar_product_tpl& src) { for (int i = 0; i < src.len; i++) { data[i] += src.data[i]; } return *this; } vectorn_tpl& operator-=(const vector_scalar_product_tpl& src) { for (int i = 0; i < src.len; i++) { data[i] -= src.data[i]; } return *this; } ftype len2() { ftype res = 0; for (int i = 0; i < len; i++) { res += data[i] * data[i]; } return res; } ftype& operator[](int idx) const { return data[idx]; } operator ftype*() { return data; } vectorn_tpl& zero() { for (int i = 0; i < len; i++) { data[i] = 0; } return *this; } vectorn_tpl& allocate() { if (flags & mtx_foreign_data) { ftype* newdata = new ftype[len]; for (int i = 0; i < len; i++) { newdata[i] = data[i]; } data = newdata; flags &= ~mtx_foreign_data; } return *this; } /*vectorn_tpl operator*(ftype op) const { vectorn_tpl res(len); for(int i=0;i ftype1 operator*(const vectorn_tpl& op1, const vectorn_tpl& op2) { ftype1 res = 0; for (int i = 0; i < op1.len; i++) { res += op1.data[i] * op2.data[i]; } return res; } /*template vectorn_tpl operator+(const vectorn_tpl &op1,const vectorn_tpl &op2) { vectorn_tpl res(len); for(int i=0;i vectorn_tpl operator-(const vectorn_tpl& op1, const vectorn_tpl& op2) { vectorn_tpl res(op1.len); for (int i = 0; i < op1.len; i++) { res.data[i] = op1.data[i] - op2.data[i]; } return res; } template vectorn_tpl& operator+=(vectorn_tpl& op1, const vectorn_tpl& op2) { for (int i = 0; i < op1.len; i++) { op1.data[i] += op2.data[i]; } return op1; } template vectorn_tpl& operator-=(vectorn_tpl& op1, const vectorn_tpl& op2) { for (int i = 0; i < op1.len; i++) { op1.data[i] -= op2.data[i]; } return op1; } /*template vectorn_tpl operator*(const vectorn_tpl &vec, const matrix_tpl &mtx) { int i,j; vectorn_tpl res(mtx.nCols); for(i=0;i vectorn_tpl operator*(const matrix_tpl &mtx, const vectorn_tpl &vec) { int i,j; vectorn_tpl res(mtx.nRows); for(i=0;i matrix_vector_product_tpl operator*(const matrix_tpl& mtx, const vectorn_tpl& vec) { return matrix_vector_product_tpl(mtx.nRows, mtx.nCols, mtx.nCols, 1, mtx.data, vec.data); } template matrix_vector_product_tpl operator*(const vectorn_tpl& vec, const matrix_tpl& mtx) { return matrix_vector_product_tpl(mtx.nCols, mtx.nRows, 1, mtx.nRows, mtx.data, vec.data); } template vector_scalar_product_tpl operator*(const vectorn_tpl& vec, ftype op) { return vector_scalar_product_tpl(vec.len, vec.data, op); } typedef vectorn_tpl vectornf; typedef vectorn_tpl vectorn; #define DECLARE_VECTORN_POOL(ftype, sz) template<> \ ftype vectorn_tpl::vecn_pool[sz] = {}; \ template<> \ int vectorn_tpl::vecn_pool_pos = 0; \ template<> \ int vectorn_tpl::vecn_pool_size = sz; #endif // ENABLE_CRY_PHYSICS #endif // CRYINCLUDE_CRYPHYSICS_VECTORN_H