Added engine
This commit is contained in:
commit
61ddd9fc26
78 changed files with 26595 additions and 0 deletions
195
src/geno/math/linear/GenoMatrix.h
Normal file
195
src/geno/math/linear/GenoMatrix.h
Normal file
|
@ -0,0 +1,195 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* Copyright (c) 2019 Gnarly Narwhal
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files(the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*******************************************************************************/
|
||||
|
||||
#include "../../GenoInts.h"
|
||||
|
||||
#ifndef GNARLY_GENOME_VECTOR_FORWARD
|
||||
#define GNARLY_GENOME_VECTOR_FORWARD
|
||||
|
||||
template <uint32 N, typename T>
|
||||
class GenoVector;
|
||||
|
||||
#endif // GNARLY_GENOME_VECTOR_FORWARD
|
||||
|
||||
#ifndef GNARLY_GENOME_MATRIX
|
||||
#define GNARLY_GENOME_MATRIX
|
||||
|
||||
#include <ostream>
|
||||
#include <initializer_list>
|
||||
|
||||
#include "GenoVector.h"
|
||||
|
||||
template <uint32 N, uint32 M, typename T>
|
||||
class GenoMatrix {
|
||||
|
||||
static_assert(N > 0 && M > 0, "Matrix dimensions must be greater than 0!");
|
||||
|
||||
private:
|
||||
bool owner = true;
|
||||
|
||||
void clean() {
|
||||
if (owner)
|
||||
delete [] m;
|
||||
}
|
||||
public:
|
||||
T * m;
|
||||
|
||||
GenoMatrix() :
|
||||
m(new T[N * M]()) {}
|
||||
|
||||
GenoMatrix(T * m, bool owner = true) noexcept :
|
||||
owner(owner),
|
||||
m(m) {}
|
||||
|
||||
GenoMatrix(std::initializer_list<T> list) :
|
||||
m(new T[N * M]) {
|
||||
auto min = list.size() < N * M ? list.size() : N * M;
|
||||
auto init = list.begin();
|
||||
for (uint32 i = 0; i < min; ++i)
|
||||
m[(i % M) * N + (i / M)] = init[i];
|
||||
}
|
||||
|
||||
template <typename T2>
|
||||
GenoMatrix(const GenoMatrix<N, M, T2> & matrix) :
|
||||
m(new T[N * M]) {
|
||||
for (uint32 i = 0; i < N * M; ++i)
|
||||
m[i] = (T) matrix.m[i];
|
||||
}
|
||||
|
||||
GenoMatrix(const GenoMatrix<N, M, T> & matrix) :
|
||||
m(new T[N * M]) {
|
||||
for (uint32 i = 0; i < N * M; ++i)
|
||||
m[i] = matrix.m[i];
|
||||
}
|
||||
|
||||
GenoMatrix(GenoMatrix<N, M, T> && matrix) noexcept :
|
||||
owner(matrix.owner),
|
||||
m(matrix.m) {
|
||||
matrix.owner = false;
|
||||
}
|
||||
|
||||
GenoMatrix<N, M, T> & operator=(const GenoMatrix<N, M, T> & matrix) {
|
||||
for (uint32 i = 0; i < N * M; ++i)
|
||||
m[i] = matrix.m[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoMatrix<N, M, T> & operator=(GenoMatrix<N, M, T> && matrix) noexcept {
|
||||
clean();
|
||||
owner = matrix.owner;
|
||||
m = matrix.m;
|
||||
matrix.owner = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoMatrix<N, M, T> & operator+=(const GenoMatrix<N, M, T> & matrix) {
|
||||
for (uint32 i = 0; i < N * M; ++i)
|
||||
m[i] += matrix.m[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoMatrix<N, M, T> & operator-=(const GenoMatrix<N, M, T> & matrix) {
|
||||
for (uint32 i = 0; i < N * M; ++i)
|
||||
m[i] -= matrix.m[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoVector<M, T> operator[](uint32 index) const noexcept {
|
||||
return GenoVector<M, T>(index * M, false);
|
||||
}
|
||||
|
||||
~GenoMatrix() {
|
||||
clean();
|
||||
}
|
||||
};
|
||||
|
||||
template <uint32 N, uint32 M, typename T>
|
||||
GenoMatrix<N, M, T> operator+(const GenoMatrix<N, M, T> & left, const GenoMatrix<N, M, T> & right) {
|
||||
auto sum = new T[N * M];
|
||||
for (uint32 i = 0; i < N * M; ++i)
|
||||
sum[i] = left.m[i] + right.m[i];
|
||||
return sum;
|
||||
}
|
||||
|
||||
template <uint32 N, uint32 M, typename T>
|
||||
GenoMatrix<N, M, T> operator-(const GenoMatrix<N, M, T> & left, const GenoMatrix<N, M, T> & right) {
|
||||
auto difference = new T[N * M];
|
||||
for (uint32 i = 0; i < N * M; ++i)
|
||||
difference[i] = left.m[i] - right.m[i];
|
||||
return difference;
|
||||
}
|
||||
|
||||
template <uint32 N, uint32 N2, uint32 M, typename T>
|
||||
GenoMatrix<N2, M, T> operator*(const GenoMatrix<N, M, T> & left, const GenoMatrix<N2, N, T> & right) {
|
||||
auto product = new T[N2 * M];
|
||||
for (uint32 i = 0; i < M; ++i) {
|
||||
for (uint32 j = 0; j < N2; ++j) {
|
||||
product[j * M + i] = 0;
|
||||
for (uint32 k = 0; k < N; ++k)
|
||||
product[j * M + i] += left.m[k * M + i] * right.m[j * N + k];
|
||||
}
|
||||
}
|
||||
return product;
|
||||
}
|
||||
|
||||
template <uint32 N, uint32 M, typename T>
|
||||
GenoVector<M, T> operator*(const GenoMatrix<N, M, T> & left, const GenoVector<N, T> & right) {
|
||||
auto product = new T[M];
|
||||
for (uint32 i = 0; i < M; ++i) {
|
||||
product[i] = 0;
|
||||
for (uint32 j = 0; j < N; ++j)
|
||||
product[i] += left.m[j * M + i] * right.v[j];
|
||||
}
|
||||
return product;
|
||||
}
|
||||
|
||||
template <uint32 N, uint32 M, typename T>
|
||||
std::ostream & operator<<(std::ostream & stream, const GenoMatrix<N, M, T> & matrix) {
|
||||
for (uint32 i = 0; i < M; ++i) {
|
||||
stream << '[';
|
||||
for (uint32 j = 0; j < N; ++j) {
|
||||
stream << matrix.m[j * M + i];
|
||||
if (j < N - 1)
|
||||
stream << ", ";
|
||||
}
|
||||
stream << "]\n";
|
||||
}
|
||||
return stream;
|
||||
}
|
||||
|
||||
template <uint32 N, uint32 M> using GenoMatrixNMb = GenoMatrix<N, M, int8 >;
|
||||
template <uint32 N, uint32 M> using GenoMatrixNMub = GenoMatrix<N, M, uint8 >;
|
||||
template <uint32 N, uint32 M> using GenoMatrixNMs = GenoMatrix<N, M, int16>;
|
||||
template <uint32 N, uint32 M> using GenoMatrixNMus = GenoMatrix<N, M, uint16>;
|
||||
template <uint32 N, uint32 M> using GenoMatrixNMi = GenoMatrix<N, M, int32>;
|
||||
template <uint32 N, uint32 M> using GenoMatrixNMui = GenoMatrix<N, M, uint32>;
|
||||
template <uint32 N, uint32 M> using GenoMatrixNMl = GenoMatrix<N, M, int8 >;
|
||||
template <uint32 N, uint32 M> using GenoMatrixNMul = GenoMatrix<N, M, uint64>;
|
||||
template <uint32 N, uint32 M> using GenoMatrixNMf = GenoMatrix<N, M, float >;
|
||||
template <uint32 N, uint32 M> using GenoMatrixNMd = GenoMatrix<N, M, double>;
|
||||
|
||||
#define GNARLY_GENOME_MATRIX_FORWARD
|
||||
#endif // GNARLY_GENOME_MATRIX
|
8579
src/geno/math/linear/GenoMatrix4.h
Normal file
8579
src/geno/math/linear/GenoMatrix4.h
Normal file
File diff suppressed because it is too large
Load diff
200
src/geno/math/linear/GenoMatrixN.h
Normal file
200
src/geno/math/linear/GenoMatrixN.h
Normal file
|
@ -0,0 +1,200 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* Copyright (c) 2019 Gnarly Narwhal
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files(the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* INPLIED, INCLUDING BUT NOT LINITED TO THE WARRANTIES OF NERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGENENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIN, DANAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FRON,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*******************************************************************************/
|
||||
|
||||
#include "../../GenoInts.h"
|
||||
|
||||
#ifndef GNARLY_GENOME_VECTOR_FORWARD
|
||||
#define GNARLY_GENOME_VECTOR_FORWARD
|
||||
|
||||
template <uint32 N, typename T>
|
||||
class GenoVector<N, T>;
|
||||
|
||||
#endif // GNARLY_GENOME_VECTOR_FORWARD
|
||||
|
||||
#ifndef GNARLY_GENOME_MATRIX_FORWARD
|
||||
#define GNARLY_GENOME_MATRIX_FORWARD
|
||||
|
||||
template <uint32 N, uint32 M, typename T>
|
||||
class GenoMatrix;
|
||||
|
||||
#endif // GNARLY_GENOME_MATRIX_FORWARD
|
||||
|
||||
#ifndef GNARLY_GENOME_MATRIXN
|
||||
#define GNARLY_GENOME_MATRIXN
|
||||
|
||||
#include <ostream>
|
||||
|
||||
#include "GenoVector.h"
|
||||
#include "GenoMatrix.h"
|
||||
|
||||
template <uint32 N, typename T>
|
||||
class GenoMatrix<N, N, T> {
|
||||
|
||||
static_assert(N > 0 && N > 0, "Matrix dimensions must be greater than 0!");
|
||||
|
||||
private:
|
||||
bool owner = true;
|
||||
|
||||
void clean() {
|
||||
if (owner)
|
||||
delete [] m;
|
||||
}
|
||||
public:
|
||||
T * m;
|
||||
|
||||
static GenoMatrix<N, N, T> makeIdentity() {
|
||||
auto identity = new T[N * N];
|
||||
for (uint32 i = 0; i < N * N; ++i) {
|
||||
if (i % N == i / N)
|
||||
identity[i] = 1;
|
||||
else
|
||||
identity[i] = 0;
|
||||
}
|
||||
return identity;
|
||||
}
|
||||
|
||||
GenoMatrix() :
|
||||
m(new T[N * N]()) {}
|
||||
|
||||
GenoMatrix(T * m, bool owner = true) noexcept :
|
||||
owner(owner),
|
||||
m(m) {}
|
||||
|
||||
GenoMatrix(std::initializer_list<T> list) :
|
||||
m(new T[N * N]) {
|
||||
auto min = list.size() < N * N ? list.size() : N * N;
|
||||
auto init = list.begin();
|
||||
for (uint32 i = 0; i < min; ++i)
|
||||
m[(i % N) * N + (i / N)] = init[i];
|
||||
}
|
||||
|
||||
template <typename T2>
|
||||
GenoMatrix(const GenoMatrix<N, N, T2> & matrix) :
|
||||
m(new T[N * N]) {
|
||||
for (uint32 i = 0; i < N * N; ++i)
|
||||
m[i] = (T) matrix.m[i];
|
||||
}
|
||||
|
||||
GenoMatrix(const GenoMatrix<N, N, T> & matrix) :
|
||||
m(new T[N * N]) {
|
||||
for (uint32 i = 0; i < N * N; ++i)
|
||||
m[i] = matrix.m[i];
|
||||
}
|
||||
|
||||
GenoMatrix(GenoMatrix<N, N, T> && matrix) noexcept :
|
||||
owner(matrix.owner),
|
||||
m(matrix.m) {
|
||||
matrix.owner = false;
|
||||
}
|
||||
|
||||
GenoMatrix<N, N, T> & operator=(const GenoMatrix<N, N, T> & matrix) {
|
||||
for (uint32 i = 0; i < N * N; ++i)
|
||||
m[i] = matrix.m[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoMatrix<N, N, T> & operator=(GenoMatrix<N, N, T> && matrix) noexcept {
|
||||
clean();
|
||||
owner = matrix.owner;
|
||||
m = matrix.m;
|
||||
matrix.owner = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoMatrix<N, N, T> & operator+=(const GenoMatrix<N, N, T> & matrix) {
|
||||
for (uint32 i = 0; i < N * N; ++i)
|
||||
m[i] += matrix.m[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoMatrix<N, N, T> & operator-=(const GenoMatrix<N, N, T> & matrix) {
|
||||
for (uint32 i = 0; i < N * N; ++i)
|
||||
m[i] -= matrix.m[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoMatrix<N, N, T> & operator*=(const GenoMatrix<N, N, T> & matrix) {
|
||||
return *this = *this * matrix;
|
||||
}
|
||||
|
||||
GenoVector<N, T> operator[](uint32 index) const noexcept {
|
||||
return GenoVector<N, T>(index * N, false);
|
||||
}
|
||||
|
||||
GenoMatrix<N, N, T> & setIdentity() {
|
||||
for (uint32 i = 0; i < N * N; ++i) {
|
||||
if (i % N == i / N)
|
||||
m[i] = 1;
|
||||
else
|
||||
m[i] = 0;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
~GenoMatrix() {
|
||||
clean();
|
||||
}
|
||||
};
|
||||
|
||||
template <uint32 N, typename T>
|
||||
T det(const GenoMatrix<N, N, T> & matrix) {
|
||||
auto ret = T{ 0 };
|
||||
auto sign = T{ 1 };
|
||||
GenoMatrix<N - 1, N - 1, T> storage;
|
||||
for (uint32 i = 0; i < N; ++i) {
|
||||
auto offset = uint32( -1 );
|
||||
for (uint32 j = 0; j < N * N; ++j)
|
||||
if (j % N != 0 && j / N != i)
|
||||
storage.m[++offset] = matrix.m[j];
|
||||
ret += sign * matrix.m[i * N] * det(storage);
|
||||
sign *= -1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T det(const GenoMatrix<2, 2, T> & matrix) {
|
||||
return matrix.m[0] * matrix.m[3] - matrix.m[2] * matrix.m[1];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T det(const GenoMatrix<1, 1, T> & matrix) {
|
||||
return matrix.m[0];
|
||||
}
|
||||
|
||||
template <uint32 N> using GenoMatrixNb = GenoMatrix<N, N, int8 >;
|
||||
template <uint32 N> using GenoMatrixNub = GenoMatrix<N, N, uint8 >;
|
||||
template <uint32 N> using GenoMatrixNs = GenoMatrix<N, N, int16>;
|
||||
template <uint32 N> using GenoMatrixNus = GenoMatrix<N, N, uint16>;
|
||||
template <uint32 N> using GenoMatrixNi = GenoMatrix<N, N, int32>;
|
||||
template <uint32 N> using GenoMatrixNui = GenoMatrix<N, N, uint32>;
|
||||
template <uint32 N> using GenoMatrixNl = GenoMatrix<N, N, int8 >;
|
||||
template <uint32 N> using GenoMatrixNul = GenoMatrix<N, N, uint64>;
|
||||
template <uint32 N> using GenoMatrixNf = GenoMatrix<N, N, float >;
|
||||
template <uint32 N> using GenoMatrixNd = GenoMatrix<N, N, double>;
|
||||
|
||||
#define GNARLY_GENOME_MATRIXN_FORWARD
|
||||
#endif // GNARLY_GENOME_MATRIXN
|
476
src/geno/math/linear/GenoVector.h
Normal file
476
src/geno/math/linear/GenoVector.h
Normal file
|
@ -0,0 +1,476 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* Copyright (c) 2019 Gnarly Narwhal
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files(the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef GNARLY_GENOME_VECTOR
|
||||
#define GNARLY_GENOME_VECTOR
|
||||
|
||||
#include <ostream>
|
||||
#include <cmath>
|
||||
#include <initializer_list>
|
||||
|
||||
#include "../../GenoInts.h"
|
||||
|
||||
template <uint32 N, typename T>
|
||||
class GenoVector {
|
||||
|
||||
static_assert(N > 0, "Vector dimensions must be greater than 0!");
|
||||
|
||||
private:
|
||||
bool owner = true;
|
||||
|
||||
void clean() {
|
||||
if (owner)
|
||||
delete [] v;
|
||||
}
|
||||
|
||||
public:
|
||||
static GenoVector<N, T> * newArray(uint32 length) {
|
||||
T * v = new T[N * length];
|
||||
GenoVector<N, T> * ret = new GenoVector<N, T>[length];
|
||||
ret[0] = GenoVector<N, T>(v);
|
||||
for (uint32 i = 1; i < length; ++i)
|
||||
ret[i] = GenoVector<N, T>(v + i * N, false);
|
||||
return ret;
|
||||
}
|
||||
|
||||
T * v;
|
||||
|
||||
GenoVector() :
|
||||
v(new T[N]()) {}
|
||||
|
||||
GenoVector(T * v, bool owner = true) noexcept :
|
||||
owner(owner),
|
||||
v(v) {}
|
||||
|
||||
explicit GenoVector(T value) :
|
||||
v(new T[N]) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] = value;
|
||||
}
|
||||
|
||||
GenoVector(std::initializer_list<T> list) :
|
||||
v(new T[N]) {
|
||||
auto min = list.size() < N ? list.size() : N;
|
||||
auto init = list.begin();
|
||||
for (uint32 i = 0; i < min; ++i)
|
||||
v[i] = init[i];
|
||||
}
|
||||
|
||||
template <typename T2>
|
||||
GenoVector(const GenoVector<N, T2> & vector) :
|
||||
v(new T[N]) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] = (T) vector.v[i];
|
||||
}
|
||||
|
||||
GenoVector(const GenoVector<N, T> & vector) :
|
||||
v(new T[N]) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] = vector.v[i];
|
||||
}
|
||||
|
||||
GenoVector(GenoVector<N, T> && vector) noexcept :
|
||||
owner(vector.owner),
|
||||
v(vector.v) {
|
||||
vector.owner = false;
|
||||
}
|
||||
|
||||
GenoVector<N, T> & operator=(const GenoVector<N, T> & vector) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] = vector.v[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoVector<N, T> & operator=(GenoVector<N, T> && vector) noexcept {
|
||||
if (owner) {
|
||||
clean();
|
||||
owner = vector.owner;
|
||||
v = vector.v;
|
||||
vector.owner = false;
|
||||
}
|
||||
else for (uint32 i = 0; i < N; ++i)
|
||||
v[i] = vector.v[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoVector<N, T> & operator+=(const GenoVector<N, T> & vector) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] += vector.v[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoVector<N, T> & operator-=(const GenoVector<N, T> & vector) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] -= vector.v[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoVector<N, T> & operator*=(T scalar) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] *= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoVector<N, T> & operator*=(const GenoVector<N, T> & vector) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] *= vector.v[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoVector<N, T> & operator/=(T scalar) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] /= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoVector<N, T> & operator/=(const GenoVector<N, T> & vector) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] /= vector.v[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
T & operator[](uint32 index) noexcept {
|
||||
return v[index];
|
||||
}
|
||||
|
||||
const T & operator[](uint32 index) const noexcept {
|
||||
return v[index];
|
||||
}
|
||||
|
||||
T getLength() const {
|
||||
auto lengthSquared = 0;
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
lengthSquared += v[i] * v[i];
|
||||
return sqrt(lengthSquared);
|
||||
}
|
||||
|
||||
T getLengthSquared() const {
|
||||
auto lengthSquared = 0;
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
lengthSquared += v[i] * v[i];
|
||||
return lengthSquared;
|
||||
}
|
||||
|
||||
GenoVector<N, T> & setLength(T length) {
|
||||
auto scalar = length / getLength();
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] *= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoVector<N, T> & normalize() {
|
||||
auto scalar = 1 / getLength();
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] *= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoVector<N, T> & negate() {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] = -v[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoVector<N, T> & project(const GenoVector<N, T> & projection) {
|
||||
auto scalar = dot(*this, projection) / projection.getLengthSquared();
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] = scalar * projection.v[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoVector<N, T> & set(const GenoVector<N, T> & set) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] = set.v[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoVector<N, T> & translate(const GenoVector<N, T> & translate) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] += translate.v[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoVector<N, T> & scale(T scale) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] *= scale;
|
||||
return *this;
|
||||
}
|
||||
|
||||
GenoVector<N, T> & scale(const GenoVector<N, T> & scale) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
v[i] *= scale.v[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ~GenoVector() noexcept {
|
||||
clean();
|
||||
}
|
||||
};
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> operator-(const GenoVector<N, T> & vector) {
|
||||
auto newV = new T[N];
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
newV[i] = -vector.v[i];
|
||||
return newV;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> operator+(const GenoVector<N, T> & left, const GenoVector<N, T> & right) {
|
||||
auto newV = new T[N];
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
newV[i] = left.v[i] + right.v[i];
|
||||
return newV;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> operator-(const GenoVector<N, T> & left, const GenoVector<N, T> & right) {
|
||||
auto newV = new T[N];
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
newV[i] = left.v[i] - right.v[i];
|
||||
return newV;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> operator*(const GenoVector<N, T> & left, T right) {
|
||||
auto newV = new T[N];
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
newV[i] = left.v[i] * right;
|
||||
return newV;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> operator*(T left, const GenoVector<N, T> & right) {
|
||||
auto newV = new T[N];
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
newV[i] = left * right.v[i];
|
||||
return newV;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> operator*(const GenoVector<N, T> & left, const GenoVector<N, T> & right) {
|
||||
auto newV = new T[N];
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
newV[i] = left.v[i] * right.v[i];
|
||||
return newV;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> operator/(const GenoVector<N, T> & left, T right) {
|
||||
auto newV = new T[N];
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
newV[i] = left.v[i] / right;
|
||||
return newV;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> operator/(const GenoVector<N, T> & left, const GenoVector<N, T> & right) {
|
||||
auto newV = new T[N];
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
newV[i] = left.v[i] / right.v[i];
|
||||
return newV;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N + 1, T> operator|(const GenoVector<N, T> & left, T right) {
|
||||
auto newV = new T[N + 1];
|
||||
for (uint32 i = 0; i < N + 1; ++i)
|
||||
newV[i] = left.v[i];
|
||||
newV[N] = right;
|
||||
return newV;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N + 1, T> operator|(T left, const GenoVector<N, T> & right) {
|
||||
auto newV = new T[N + 1];
|
||||
newV[0] = left;
|
||||
for (uint32 i = 0; i < N + 1; ++i)
|
||||
newV[i + 1] = right.v[i];
|
||||
return newV;
|
||||
}
|
||||
|
||||
template <uint32 N, uint32 N2, typename T>
|
||||
GenoVector<N + N2, T> operator|(const GenoVector<N, T> & left, const GenoVector<N2, T> & right) {
|
||||
auto newV = new T[N + N2];
|
||||
auto vRight = newV + N;
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
newV[i] = left.v[i];
|
||||
for (uint32 i = 0; i < N2; ++i)
|
||||
vRight[i] = right.v[i];
|
||||
return newV;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> setLength(const GenoVector<N, T> & vector, T length) {
|
||||
auto scalar = length / vector.getLength();
|
||||
auto newV = new T[N];
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
newV[i] = vector.v[i] * scalar;
|
||||
return newV;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> & setLength(const GenoVector<N, T> & vector, T length, const GenoVector<N, T> & target) {
|
||||
auto scalar = length / vector.getLength();
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
target.v[i] = vector.v[i] * scalar;
|
||||
return target;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> normalize(const GenoVector<N, T> & vector) {
|
||||
auto scalar = 1 / vector.getLength();
|
||||
auto newV = new T[N];
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
newV[i] = vector.v[i] * scalar;
|
||||
return newV;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> & normalize(const GenoVector<N, T> & vector, const GenoVector<N, T> & target) {
|
||||
auto scalar = 1 / vector.getLength();
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
target.v[i] = vector.v[i] * scalar;
|
||||
return target;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> negate(const GenoVector<N, T> & vector) {
|
||||
auto newV = new T[N];
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
newV[i] = -vector.v[i];
|
||||
return newV;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> & negate(const GenoVector<N, T> & vector, GenoVector<N, T> & target) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
target.v[i] = -vector.v[i];
|
||||
return target;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
T dot(const GenoVector<N, T> & left, const GenoVector<N, T> & right) {
|
||||
auto product = T();
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
product += left.v[i] * right.v[i];
|
||||
return product;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> project(const GenoVector<N, T> & vector, const GenoVector<N, T> & projection) {
|
||||
auto scalar = dot(vector, projection) / projection.getLengthSquared();
|
||||
auto newV = new T[N];
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
newV[i] = scalar * projection.v[i];
|
||||
return newV;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> & project(const GenoVector<N, T> & vector, const GenoVector<N, T> & projection, GenoVector<N, T> & target) {
|
||||
auto scalar = dot(vector, projection) / projection.getLengthSquared();
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
target.v[i] = scalar * projection.v[i];
|
||||
return target;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
T angleBetween(const GenoVector<N, T> & vector1, const GenoVector<N, T> & vector2) {
|
||||
return acos(dot(vector1, vector2) / (vector1.getLength() * vector2.getLength()));
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> & translate(const GenoVector<N, T> & vector, const GenoVector<N, T> & translate) {
|
||||
auto newV = new T[N];
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
newV[i] = vector.v[i] + translate.v[i];
|
||||
return newV;
|
||||
}
|
||||
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> & translate(const GenoVector<N, T> & vector, const GenoVector<N, T> & translate, GenoVector<N, T> & target) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
target.v[i] = vector.v[i] + translate.v[i];
|
||||
return target;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> & scale(const GenoVector<N, T> & vector, T scale) {
|
||||
auto newV = new T[N];
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
newV[i] = vector.v[i] * scale;
|
||||
return newV;
|
||||
}
|
||||
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> & scale(const GenoVector<N, T> & vector, T scale, GenoVector<N, T> & target) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
target.v[i] = vector.v[i] * scale;
|
||||
return target;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> & scale(const GenoVector<N, T> & vector, const GenoVector<N, T> & scale) {
|
||||
auto newV = new T[N];
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
newV[i] = vector.v[i] * scale.v[i];
|
||||
return newV;
|
||||
}
|
||||
|
||||
|
||||
template <uint32 N, typename T>
|
||||
GenoVector<N, T> & scale(const GenoVector<N, T> & vector, const GenoVector<N, T> & scale, GenoVector<N, T> & target) {
|
||||
for (uint32 i = 0; i < N; ++i)
|
||||
target.v[i] = vector.v[i] * scale.v[i];
|
||||
return target;
|
||||
}
|
||||
|
||||
template <uint32 N, typename T>
|
||||
std::ostream & operator<<(std::ostream & stream, const GenoVector<N, T> & vector) {
|
||||
stream << '<';
|
||||
for (uint32 i = 0; i < N; ++i) {
|
||||
stream << vector.v[i];
|
||||
if (i < N - 1)
|
||||
stream << ", ";
|
||||
}
|
||||
return stream << '>';
|
||||
}
|
||||
|
||||
template <uint32 N> using GenoVectorNb = GenoVector<N, int8 >;
|
||||
template <uint32 N> using GenoVectorNub = GenoVector<N, uint8 >;
|
||||
template <uint32 N> using GenoVectorNs = GenoVector<N, int16>;
|
||||
template <uint32 N> using GenoVectorNus = GenoVector<N, uint16>;
|
||||
template <uint32 N> using GenoVectorNi = GenoVector<N, int32>;
|
||||
template <uint32 N> using GenoVectorNui = GenoVector<N, uint32>;
|
||||
template <uint32 N> using GenoVectorNl = GenoVector<N, int64>;
|
||||
template <uint32 N> using GenoVectorNul = GenoVector<N, uint64>;
|
||||
template <uint32 N> using GenoVectorNf = GenoVector<N, float >;
|
||||
template <uint32 N> using GenoVectorNd = GenoVector<N, double>;
|
||||
|
||||
#define GNARLY_GENOME_VECTOR_FORWARD
|
||||
#endif // GNARLY_GENOME_VECTOR
|
56
src/geno/math/linear/GenoVector1.h
Normal file
56
src/geno/math/linear/GenoVector1.h
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* Copyright (c) 2019 Gnarly Narwhal
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files(the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*******************************************************************************/
|
||||
|
||||
#include "../../GenoInts.h"
|
||||
|
||||
#ifndef GNARLY_GENOME_VECTOR_FORWARD
|
||||
#define GNARLY_GENOME_VECTOR_FORWARD
|
||||
|
||||
template <uint32 N, typename T>
|
||||
class GenoVector;
|
||||
|
||||
#endif // GNARLY_GENOME_VECTOR_FORWARD
|
||||
|
||||
#ifndef GNARLY_GENOME_VECTOR1
|
||||
#define GNARLY_GENOME_VECTOR1
|
||||
|
||||
#include "GenoVector.h"
|
||||
|
||||
template <typename T> using GenoVector1 = GenoVector<1, T>;
|
||||
|
||||
using GenoVector1b = GenoVector1< int8 >;
|
||||
using GenoVector1ub = GenoVector1<uint8 >;
|
||||
using GenoVector1s = GenoVector1< int16>;
|
||||
using GenoVector1us = GenoVector1<uint16>;
|
||||
using GenoVector1i = GenoVector1< int32>;
|
||||
using GenoVector1ui = GenoVector1<uint32>;
|
||||
using GenoVector1l = GenoVector1< int64>;
|
||||
using GenoVector1ul = GenoVector1<uint64>;
|
||||
using GenoVector1f = GenoVector1<float >;
|
||||
using GenoVector1d = GenoVector1<double>;
|
||||
|
||||
#define GNARLY_GENOME_VECTOR1_FORWARD
|
||||
#endif // GNARLY_GENOME_VECTOR1
|
1080
src/geno/math/linear/GenoVector2.h
Normal file
1080
src/geno/math/linear/GenoVector2.h
Normal file
File diff suppressed because it is too large
Load diff
3080
src/geno/math/linear/GenoVector3.h
Normal file
3080
src/geno/math/linear/GenoVector3.h
Normal file
File diff suppressed because it is too large
Load diff
7440
src/geno/math/linear/GenoVector4.h
Normal file
7440
src/geno/math/linear/GenoVector4.h
Normal file
File diff suppressed because it is too large
Load diff
34
src/geno/math/linear/GenoVectorDimensions.cpp
Normal file
34
src/geno/math/linear/GenoVectorDimensions.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* Copyright (c) 2019 Gnarly Narwhal
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files(the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*******************************************************************************/
|
||||
|
||||
#include "GenoVectorDimensions.h"
|
||||
|
||||
namespace GenoVectorDimensions {
|
||||
GenoVectorDimension<0> x;
|
||||
GenoVectorDimension<1> y;
|
||||
GenoVectorDimension<2> z;
|
||||
GenoVectorDimension<3> w;
|
||||
}
|
41
src/geno/math/linear/GenoVectorDimensions.h
Normal file
41
src/geno/math/linear/GenoVectorDimensions.h
Normal file
|
@ -0,0 +1,41 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* Copyright (c) 2019 Gnarly Narwhal
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files(the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef GNARLY_GENOME_VECTOR_DIMENSIONS
|
||||
#define GNARLY_GENOME_VECTOR_DIMENSIONS
|
||||
|
||||
#include "../../GenoInts.h"
|
||||
|
||||
namespace GenoVectorDimensions {
|
||||
|
||||
template <uint32 N>
|
||||
struct GenoVectorDimension {
|
||||
const static uint32 dimension = N;
|
||||
};
|
||||
}
|
||||
|
||||
#define GNARLY_GENOME_VECTOR_DIMENSIONS_FORWARD
|
||||
#endif // GNARLY_GENOME_VECTOR_DIMENSIONS
|
Loading…
Add table
Add a link
Reference in a new issue