ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

vector.h

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifndef MATH_VECTOR_H
00024 #define MATH_VECTOR_H
00025 
00026 #include "common/stream.h"
00027 
00028 #include "math/matrix.h"
00029 #include "math/utils.h"
00030 
00031 namespace Math {
00032 
00037 template<int dim>
00038 class MatrixType<dim, 1> : public MatrixBase<dim, 1> {
00039 public:
00040     void normalize();
00041     Vector(dim) getNormalized() const;
00042     float getMagnitude() const;
00043     float getSquareMagnitude() const;
00044     float getDistanceTo(const Vector(dim) &point) const;
00045     float dotProduct(const Vector(dim) &v) const;
00046 
00047     inline void setValue(int i, float val) { value(i) = val; }
00048     inline float getValue(int i) const { return value(i); }
00049 
00050     template<int d>
00051     inline static float dotProduct(const Vector(d) &v1, const Vector(d) &v2) {
00052         return v1.dotProduct(v2);
00053     }
00054 
00059     void readFromStream(Common::ReadStream *stream);
00060 
00061 protected:
00062     MatrixType() : MatrixBase<dim, 1>() { }
00063     MatrixType(const float *data) : MatrixBase<dim, 1>(data) { }
00064     MatrixType(const MatrixBase<dim, 1> &m) : MatrixBase<dim, 1>(m) { }
00065 
00066     inline float &value(int i) { return this->operator()(i, 0); }
00067     inline float value(int i) const { return this->operator()(i, 0); }
00068 };
00069 
00070 
00071 template<int dim>
00072 void MatrixType<dim, 1>::normalize() {
00073     float mag = getMagnitude();
00074     if (mag > 0.f) {
00075         for (int i = 0; i < dim; ++i) {
00076             this->operator()(i, 0) /= mag;
00077         }
00078     }
00079 }
00080 
00081 template<int dim>
00082 Vector(dim) MatrixType<dim, 1>::getNormalized() const {
00083     Vector(dim) v(*this);
00084     v.normalize();
00085     return v;
00086 }
00087 
00088 template<int dim>
00089 float MatrixType<dim, 1>::getMagnitude() const {
00090     return sqrt(getSquareMagnitude());
00091 }
00092 
00093 template<int dim>
00094 float MatrixType<dim, 1>::getSquareMagnitude() const {
00095     float mag = 0;
00096     for (int i = 0; i < dim; ++i) {
00097         mag += square(getValue(i));
00098     }
00099     return mag;
00100 }
00101 
00102 template<int dim>
00103 float MatrixType<dim, 1>::getDistanceTo(const Vector(dim) &point) const {
00104     float result = 0;
00105     for (int i = 0; i < dim; ++i) {
00106         result += square(getValue(i) - point.getValue(i));
00107     }
00108     return sqrt(result);
00109 }
00110 
00111 template<int dim>
00112 float MatrixType<dim, 1>::dotProduct(const Vector(dim) &v) const {
00113     float result = 0;
00114     for (int i = 0; i < dim; ++i) {
00115         result += value(i) * v.value(i);
00116     }
00117     return result;
00118 }
00119 
00120 template<int dim>
00121 void MatrixType<dim, 1>::readFromStream(Common::ReadStream *stream) {
00122     for (int i = 0; i < dim; ++i) {
00123         setValue(i, stream->readFloatLE());
00124     }
00125 }
00126 
00127 
00128 template<int dim>
00129 Common::Debug &operator<<(Common::Debug dbg, const Math::Matrix<dim, 1> &v) {
00130     dbg.nospace() << "Vector<" << dim << ">(" << v.getValue(0);
00131     for (int i = 1; i < dim; ++i) {
00132         dbg << ", " << v.getValue(i);
00133     }
00134     dbg << ")";
00135 
00136     return dbg.space();
00137 }
00138 
00139 }
00140 
00141 #endif


Generated on Sat Oct 19 2019 05:01:25 for ResidualVM by doxygen 1.7.1
curved edge   curved edge