2012-01-06 23:15:48 +01:00
|
|
|
/* ResidualVM - A 3D game interpreter
|
2011-09-14 22:48:24 +02:00
|
|
|
*
|
2012-01-06 23:15:48 +01:00
|
|
|
* ResidualVM is the legal property of its developers, whose names
|
2011-09-14 22:48:24 +02:00
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
2012-12-19 23:15:43 +01:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2014-04-05 18:18:42 +02:00
|
|
|
*
|
2012-12-19 23:15:43 +01:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2011-09-14 22:48:24 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-12-19 23:15:43 +01:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-04-05 18:18:42 +02:00
|
|
|
*
|
2012-12-19 23:15:43 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2011-09-14 22:48:24 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MATH_VECTOR_H
|
|
|
|
#define MATH_VECTOR_H
|
|
|
|
|
2012-01-30 21:57:06 +01:00
|
|
|
#include "common/stream.h"
|
|
|
|
|
2011-09-14 22:48:24 +02:00
|
|
|
#include "math/matrix.h"
|
|
|
|
#include "math/utils.h"
|
|
|
|
|
|
|
|
namespace Math {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \class MatrixType<dim, 1>
|
|
|
|
* This MatrixType specialization defines new methods for the vectors.
|
|
|
|
*/
|
|
|
|
template<int dim>
|
|
|
|
class MatrixType<dim, 1> : public MatrixBase<dim, 1> {
|
|
|
|
public:
|
|
|
|
void normalize();
|
|
|
|
Vector(dim) getNormalized() const;
|
|
|
|
float getMagnitude() const;
|
2012-02-10 14:55:04 +01:00
|
|
|
float getSquareMagnitude() const;
|
2011-09-14 22:48:24 +02:00
|
|
|
float getDistanceTo(const Vector(dim) &point) const;
|
2012-01-30 21:45:22 +01:00
|
|
|
float dotProduct(const Vector(dim) &v) const;
|
2011-09-14 22:48:24 +02:00
|
|
|
|
|
|
|
inline void setValue(int i, float val) { value(i) = val; }
|
|
|
|
inline float getValue(int i) const { return value(i); }
|
|
|
|
|
2011-09-18 19:14:25 +02:00
|
|
|
template<int d>
|
|
|
|
inline static float dotProduct(const Vector(d) &v1, const Vector(d) &v2) {
|
2012-01-30 21:45:22 +01:00
|
|
|
return v1.dotProduct(v2);
|
2011-09-18 19:14:25 +02:00
|
|
|
}
|
|
|
|
|
2012-01-30 21:57:06 +01:00
|
|
|
/**
|
|
|
|
* Reads <i>dim</i> floats from the passed stream, and uses them
|
|
|
|
* as value 0...dim in chronological order.
|
|
|
|
*/
|
|
|
|
void readFromStream(Common::ReadStream *stream);
|
|
|
|
|
2011-09-14 22:48:24 +02:00
|
|
|
protected:
|
|
|
|
MatrixType() : MatrixBase<dim, 1>() { }
|
2011-11-20 15:17:41 +01:00
|
|
|
MatrixType(const float *data) : MatrixBase<dim, 1>(data) { }
|
2011-09-14 22:48:24 +02:00
|
|
|
MatrixType(const MatrixBase<dim, 1> &m) : MatrixBase<dim, 1>(m) { }
|
|
|
|
|
|
|
|
inline float &value(int i) { return this->operator()(i, 0); }
|
|
|
|
inline float value(int i) const { return this->operator()(i, 0); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<int dim>
|
|
|
|
void MatrixType<dim, 1>::normalize() {
|
|
|
|
float mag = getMagnitude();
|
2011-10-09 19:14:38 +02:00
|
|
|
if (mag > 0.f) {
|
|
|
|
for (int i = 0; i < dim; ++i) {
|
|
|
|
this->operator()(i, 0) /= mag;
|
|
|
|
}
|
2011-09-14 22:48:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<int dim>
|
|
|
|
Vector(dim) MatrixType<dim, 1>::getNormalized() const {
|
|
|
|
Vector(dim) v(*this);
|
|
|
|
v.normalize();
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<int dim>
|
|
|
|
float MatrixType<dim, 1>::getMagnitude() const {
|
2012-02-10 14:55:04 +01:00
|
|
|
return sqrt(getSquareMagnitude());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<int dim>
|
|
|
|
float MatrixType<dim, 1>::getSquareMagnitude() const {
|
2011-09-14 22:48:24 +02:00
|
|
|
float mag = 0;
|
|
|
|
for (int i = 0; i < dim; ++i) {
|
|
|
|
mag += square(getValue(i));
|
|
|
|
}
|
2012-02-10 14:55:04 +01:00
|
|
|
return mag;
|
2011-09-14 22:48:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<int dim>
|
|
|
|
float MatrixType<dim, 1>::getDistanceTo(const Vector(dim) &point) const {
|
2011-09-18 18:52:44 +02:00
|
|
|
float result = 0;
|
2011-09-14 22:48:24 +02:00
|
|
|
for (int i = 0; i < dim; ++i) {
|
2011-09-18 18:52:44 +02:00
|
|
|
result += square(getValue(i) - point.getValue(i));
|
2011-09-14 22:48:24 +02:00
|
|
|
}
|
2011-09-18 18:52:44 +02:00
|
|
|
return sqrt(result);
|
2011-09-14 22:48:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<int dim>
|
2012-01-30 21:45:22 +01:00
|
|
|
float MatrixType<dim, 1>::dotProduct(const Vector(dim) &v) const {
|
2011-09-14 22:48:24 +02:00
|
|
|
float result = 0;
|
|
|
|
for (int i = 0; i < dim; ++i) {
|
|
|
|
result += value(i) * v.value(i);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-01-30 21:57:06 +01:00
|
|
|
template<int dim>
|
|
|
|
void MatrixType<dim, 1>::readFromStream(Common::ReadStream *stream) {
|
|
|
|
const int size = dim * sizeof(float);
|
|
|
|
char buf[size];
|
|
|
|
stream->read(buf, size);
|
|
|
|
|
|
|
|
for (int i = 0; i < dim; ++i) {
|
2017-05-04 13:40:37 +00:00
|
|
|
setValue(i, READ_LE_FLOAT(buf + i * sizeof(float)));
|
2012-01-30 21:57:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-14 22:48:24 +02:00
|
|
|
|
|
|
|
template<int dim>
|
|
|
|
Common::Debug &operator<<(Common::Debug dbg, const Math::Matrix<dim, 1> &v) {
|
|
|
|
dbg.nospace() << "Vector<" << dim << ">(" << v.getValue(0);
|
|
|
|
for (int i = 1; i < dim; ++i) {
|
|
|
|
dbg << ", " << v.getValue(i);
|
|
|
|
}
|
|
|
|
dbg << ")";
|
|
|
|
|
|
|
|
return dbg.space();
|
|
|
|
}
|
|
|
|
|
2013-01-09 18:41:50 +01:00
|
|
|
}
|
|
|
|
|
2011-09-14 22:48:24 +02:00
|
|
|
#endif
|