2013-12-29 22:24:33 -04: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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-02-17 21:25:33 -04:00
|
|
|
#pragma once
|
2013-12-29 22:24:33 -04:00
|
|
|
|
2016-03-31 18:43:36 -03:00
|
|
|
#include <cmath>
|
2013-12-29 22:24:33 -04:00
|
|
|
#include <string.h>
|
2017-02-26 19:24:35 -04:00
|
|
|
#include "matrixN.h"
|
|
|
|
|
|
|
|
#ifndef MATH_CHECK_INDEXES
|
|
|
|
# define MATH_CHECK_INDEXES 0
|
|
|
|
#endif
|
|
|
|
|
2016-02-14 18:22:23 -04:00
|
|
|
#if MATH_CHECK_INDEXES
|
2013-12-29 22:24:33 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#endif
|
2013-12-29 22:24:33 -04:00
|
|
|
|
2017-02-26 19:24:35 -04:00
|
|
|
template <typename T, uint8_t N>
|
|
|
|
class MatrixN;
|
|
|
|
|
|
|
|
|
2013-12-29 22:24:33 -04:00
|
|
|
template <typename T, uint8_t N>
|
|
|
|
class VectorN
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// trivial ctor
|
2024-07-09 13:33:55 -03:00
|
|
|
inline VectorN() {
|
2021-08-25 07:44:51 -03:00
|
|
|
for (auto i = 0; i < N; i++) {
|
|
|
|
_v[i] = T{};
|
|
|
|
}
|
2013-12-29 22:24:33 -04:00
|
|
|
}
|
|
|
|
|
2017-02-26 19:24:35 -04:00
|
|
|
// vector ctor
|
2024-07-09 13:33:55 -03:00
|
|
|
inline VectorN(const T *v) {
|
2017-02-26 19:24:35 -04:00
|
|
|
memcpy(_v, v, sizeof(T)*N);
|
|
|
|
}
|
|
|
|
|
2013-12-29 22:24:33 -04:00
|
|
|
inline T & operator[](uint8_t i) {
|
2016-02-14 18:22:23 -04:00
|
|
|
#if MATH_CHECK_INDEXES
|
2013-12-29 22:24:33 -04:00
|
|
|
assert(i >= 0 && i < N);
|
|
|
|
#endif
|
2013-12-29 22:24:33 -04:00
|
|
|
return _v[i];
|
|
|
|
}
|
|
|
|
|
2013-12-29 22:24:33 -04:00
|
|
|
inline const T & operator[](uint8_t i) const {
|
2016-02-14 18:22:23 -04:00
|
|
|
#if MATH_CHECK_INDEXES
|
2013-12-29 22:24:33 -04:00
|
|
|
assert(i >= 0 && i < N);
|
|
|
|
#endif
|
2013-12-29 22:24:33 -04:00
|
|
|
return _v[i];
|
|
|
|
}
|
|
|
|
|
2014-01-23 19:37:08 -04:00
|
|
|
// test for equality
|
|
|
|
bool operator ==(const VectorN<T,N> &v) const {
|
|
|
|
for (uint8_t i=0; i<N; i++) {
|
|
|
|
if (_v[i] != v[i]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-29 22:24:33 -04:00
|
|
|
// zero the vector
|
2013-12-29 22:24:33 -04:00
|
|
|
inline void zero()
|
2013-12-29 22:24:33 -04:00
|
|
|
{
|
|
|
|
memset(_v, 0, sizeof(T)*N);
|
|
|
|
}
|
|
|
|
|
2014-02-18 18:21:59 -04:00
|
|
|
// negation
|
|
|
|
VectorN<T,N> operator -(void) const {
|
|
|
|
VectorN<T,N> v2;
|
|
|
|
for (uint8_t i=0; i<N; i++) {
|
|
|
|
v2[i] = - _v[i];
|
2016-05-02 18:53:58 -03:00
|
|
|
}
|
2014-02-18 18:21:59 -04:00
|
|
|
return v2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// addition
|
|
|
|
VectorN<T,N> operator +(const VectorN<T,N> &v) const {
|
|
|
|
VectorN<T,N> v2;
|
|
|
|
for (uint8_t i=0; i<N; i++) {
|
|
|
|
v2[i] = _v[i] + v[i];
|
2016-05-02 18:53:58 -03:00
|
|
|
}
|
2014-02-18 18:21:59 -04:00
|
|
|
return v2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// subtraction
|
|
|
|
VectorN<T,N> operator -(const VectorN<T,N> &v) const {
|
|
|
|
VectorN<T,N> v2;
|
|
|
|
for (uint8_t i=0; i<N; i++) {
|
|
|
|
v2[i] = _v[i] - v[i];
|
2016-05-02 18:53:58 -03:00
|
|
|
}
|
2014-02-18 18:21:59 -04:00
|
|
|
return v2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// uniform scaling
|
|
|
|
VectorN<T,N> operator *(const T num) const {
|
|
|
|
VectorN<T,N> v2;
|
|
|
|
for (uint8_t i=0; i<N; i++) {
|
|
|
|
v2[i] = _v[i] * num;
|
2016-05-02 18:53:58 -03:00
|
|
|
}
|
2014-02-18 18:21:59 -04:00
|
|
|
return v2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// uniform scaling
|
|
|
|
VectorN<T,N> operator /(const T num) const {
|
|
|
|
VectorN<T,N> v2;
|
|
|
|
for (uint8_t i=0; i<N; i++) {
|
|
|
|
v2[i] = _v[i] / num;
|
2016-05-02 18:53:58 -03:00
|
|
|
}
|
2014-02-18 18:21:59 -04:00
|
|
|
return v2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// addition
|
|
|
|
VectorN<T,N> &operator +=(const VectorN<T,N> &v) {
|
|
|
|
for (uint8_t i=0; i<N; i++) {
|
|
|
|
_v[i] += v[i];
|
2016-05-02 18:53:58 -03:00
|
|
|
}
|
2014-02-18 18:21:59 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// subtraction
|
|
|
|
VectorN<T,N> &operator -=(const VectorN<T,N> &v) {
|
|
|
|
for (uint8_t i=0; i<N; i++) {
|
|
|
|
_v[i] -= v[i];
|
2016-05-02 18:53:58 -03:00
|
|
|
}
|
2014-02-18 18:21:59 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// uniform scaling
|
|
|
|
VectorN<T,N> &operator *=(const T num) {
|
|
|
|
for (uint8_t i=0; i<N; i++) {
|
|
|
|
_v[i] *= num;
|
2016-05-02 18:53:58 -03:00
|
|
|
}
|
2014-02-18 18:21:59 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// uniform scaling
|
|
|
|
VectorN<T,N> &operator /=(const T num) {
|
|
|
|
for (uint8_t i=0; i<N; i++) {
|
|
|
|
_v[i] /= num;
|
2016-05-02 18:53:58 -03:00
|
|
|
}
|
2014-02-18 18:21:59 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-02-26 19:24:35 -04:00
|
|
|
// dot product
|
|
|
|
T operator *(const VectorN<T,N> &v) const {
|
|
|
|
float ret = 0;
|
|
|
|
for (uint8_t i=0; i<N; i++) {
|
|
|
|
ret += _v[i] * v._v[i];
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// multiplication of a matrix by a vector, in-place
|
|
|
|
// C = A * B
|
|
|
|
void mult(const MatrixN<T,N> &A, const VectorN<T,N> &B) {
|
|
|
|
for (uint8_t i = 0; i < N; i++) {
|
|
|
|
_v[i] = 0;
|
|
|
|
for (uint8_t k = 0; k < N; k++) {
|
|
|
|
_v[i] += A.v[i][k] * B[k];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 07:56:31 -04:00
|
|
|
protected:
|
2013-12-29 22:24:33 -04:00
|
|
|
T _v[N];
|
|
|
|
};
|