Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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