minor rewrite
This commit is contained in:
parent
8639deac1e
commit
20ecd2ab9d
8 changed files with 173 additions and 279 deletions
|
|
@ -18,9 +18,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|||
*/
|
||||
|
||||
|
||||
export module Crafter.Math:BasicTypes;
|
||||
export module Crafter.Math:Basic;
|
||||
import std;
|
||||
|
||||
namespace Crafter {
|
||||
|
||||
template<typename T>
|
||||
constexpr T ToRadian(T degrees) {
|
||||
return degrees * (std::numbers::pi / 180);
|
||||
}
|
||||
}
|
||||
|
|
@ -19,9 +19,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|||
|
||||
export module Crafter.Math:MatrixRowMajor;
|
||||
|
||||
import :BasicTypes;
|
||||
import :Basic;
|
||||
import :Vector;
|
||||
import :Misc;
|
||||
import std;
|
||||
|
||||
namespace Crafter {
|
||||
|
|
@ -142,46 +141,6 @@ namespace Crafter {
|
|||
return result;
|
||||
}
|
||||
|
||||
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Perspective(float fovAngleY, float aspectRatio, float nearZ, float farZ) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
||||
MatrixRowMajor<T, CollumSize, RowSize, Repeats> M;
|
||||
float SinFov;
|
||||
float CosFov;
|
||||
XMScalarSinCos(&SinFov, &CosFov, 0.5f * fovAngleY);
|
||||
|
||||
float Height = CosFov / SinFov;
|
||||
float Width = Height / aspectRatio;
|
||||
float fRange = farZ / (nearZ - farZ);
|
||||
|
||||
M.m[0][0] = Width;
|
||||
M.m[0][1] = 0.0f;
|
||||
M.m[0][2] = 0.0f;
|
||||
M.m[0][3] = 0.0f;
|
||||
|
||||
M.m[1][0] = 0.0f;
|
||||
M.m[1][1] = Height;
|
||||
M.m[1][2] = 0.0f;
|
||||
M.m[1][3] = 0.0f;
|
||||
|
||||
M.m[2][0] = 0.0f;
|
||||
M.m[2][1] = 0.0f;
|
||||
M.m[2][2] = fRange;
|
||||
M.m[2][3] = -1.0f;
|
||||
|
||||
M.m[3][0] = 0.0f;
|
||||
M.m[3][1] = 0.0f;
|
||||
M.m[3][2] = fRange * nearZ;
|
||||
M.m[3][3] = 0.0f;
|
||||
return M;
|
||||
}
|
||||
|
||||
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Identity() requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
||||
return MatrixRowMajor<T, CollumSize, RowSize, Repeats>(
|
||||
1, 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1
|
||||
);
|
||||
}
|
||||
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Identity() requires(CollumSize == 4 && RowSize == 3 && Repeats == 1 && std::same_as<T, float>) {
|
||||
return MatrixRowMajor<T, CollumSize, RowSize, Repeats>(
|
||||
1, 0, 0, 0,
|
||||
|
|
@ -190,7 +149,6 @@ namespace Crafter {
|
|||
);
|
||||
}
|
||||
|
||||
|
||||
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Scaling(float x, float y, float z) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
||||
return MatrixRowMajor<T, CollumSize, RowSize, Repeats>(
|
||||
x, 0, 0, 0,
|
||||
|
|
|
|||
|
|
@ -1,84 +0,0 @@
|
|||
/*
|
||||
Crafter®.Math
|
||||
Copyright (C) 2026 Catcrafts®
|
||||
catcrafts.net
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License version 3.0 as published by the Free Software Foundation;
|
||||
|
||||
This library 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
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
export module Crafter.Math:Misc;
|
||||
import std;
|
||||
|
||||
export namespace Crafter {
|
||||
//-------------------------------------------------------------------------------------
|
||||
// DirectXMathMisc.inl -- SIMD C++ Math library
|
||||
//
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
//
|
||||
// http://go.microsoft.com/fwlink/?LinkID=615560
|
||||
//-------------------------------------------------------------------------------------
|
||||
constexpr float XM_PI = 3.141592654f;
|
||||
constexpr float XM_2PI = 6.283185307f;
|
||||
constexpr float XM_1DIVPI = 0.318309886f;
|
||||
constexpr float XM_1DIV2PI = 0.159154943f;
|
||||
constexpr float XM_PIDIV2 = 1.570796327f;
|
||||
constexpr float XM_PIDIV4 = 0.785398163f;
|
||||
|
||||
|
||||
inline void XMScalarSinCos(float* pSin, float* pCos, float Value) noexcept
|
||||
{
|
||||
// Map Value to y in [-pi,pi], x = 2*pi*quotient + remainder.
|
||||
float quotient = XM_1DIV2PI * Value;
|
||||
if (Value >= 0.0f)
|
||||
{
|
||||
quotient = static_cast<float>(static_cast<int>(quotient + 0.5f));
|
||||
}
|
||||
else
|
||||
{
|
||||
quotient = static_cast<float>(static_cast<int>(quotient - 0.5f));
|
||||
}
|
||||
float y = Value - XM_2PI * quotient;
|
||||
|
||||
// Map y to [-pi/2,pi/2] with sin(y) = sin(Value).
|
||||
float sign;
|
||||
if (y > XM_PIDIV2)
|
||||
{
|
||||
y = XM_PI - y;
|
||||
sign = -1.0f;
|
||||
}
|
||||
else if (y < -XM_PIDIV2)
|
||||
{
|
||||
y = -XM_PI - y;
|
||||
sign = -1.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
sign = +1.0f;
|
||||
}
|
||||
|
||||
float y2 = y * y;
|
||||
|
||||
// 11-degree minimax approximation
|
||||
*pSin = (((((-2.3889859e-08f * y2 + 2.7525562e-06f) * y2 - 0.00019840874f) * y2 + 0.0083333310f) * y2 - 0.16666667f) * y2 + 1.0f) * y;
|
||||
|
||||
// 10-degree minimax approximation
|
||||
float p = ((((-2.6051615e-07f * y2 + 2.4760495e-05f) * y2 - 0.0013888378f) * y2 + 0.041666638f) * y2 - 0.5f) * y2 + 1.0f;
|
||||
*pCos = sign * p;
|
||||
}
|
||||
|
||||
constexpr float ToRadian(float degrees) {
|
||||
return degrees * (std::numbers::pi / 180);
|
||||
}
|
||||
}
|
||||
62
interfaces/Crafter.Math-Ray.cppm
Executable file
62
interfaces/Crafter.Math-Ray.cppm
Executable file
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
Crafter®.Math
|
||||
Copyright (C) 2026 Catcrafts®
|
||||
catcrafts.net
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License version 3.0 as published by the Free Software Foundation;
|
||||
|
||||
This library 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
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
export module Crafter.Math:Ray;
|
||||
import :Vector;
|
||||
import std;
|
||||
|
||||
namespace Crafter {
|
||||
export template<typename T>
|
||||
T IntersectionTestRayTriangle(Vector<T, 3, 0> vert0, Vector<T, 3, 0> vert1, Vector<T, 3, 0> vert2, Vector<T, 3, 0> rayOrigin, Vector<T, 3, 0> rayDir) {
|
||||
constexpr float EPSILON = 0.0000001;
|
||||
Vector<T, 3, 0> edge1 = vert1 - vert0;
|
||||
Vector<T, 3, 0> edge2 = vert2 - vert0;
|
||||
|
||||
Vector<T, 3, 0> h = Vector<T, 3, 0>::Cross(rayDir, edge2);
|
||||
float determinant = Vector<T, 3, 0>::Dot(edge1, h);
|
||||
|
||||
|
||||
if(determinant < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (determinant > -EPSILON && determinant < EPSILON) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
float inverse_determinant = 1.0 / determinant;
|
||||
|
||||
Vector<T, 3, 0> origins_diff_vector = rayOrigin - vert0;
|
||||
float u = Vector<T, 3, 0>::Dot(origins_diff_vector, h) * inverse_determinant;
|
||||
|
||||
if (u < 0.0 || u > 1.0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector<T, 3, 0> q = Vector<T, 3, 0>::Cross(origins_diff_vector, edge1);
|
||||
float v = inverse_determinant * Vector<T, 3, 0>::Dot(rayDir, q);
|
||||
|
||||
if (v < 0.0 || u + v > 1.0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return inverse_determinant * Vector<T, 3, 0>::Dot(edge2, q);
|
||||
}
|
||||
}
|
||||
|
|
@ -20,9 +20,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|||
export module Crafter.Math:Vector;
|
||||
import std;
|
||||
|
||||
import :BasicTypes;
|
||||
import :Misc;
|
||||
|
||||
namespace Crafter {
|
||||
template <typename T, std::uint32_t Len, std::uint32_t Aligment>
|
||||
struct __attribute__((packed)) VectorBase {
|
||||
|
|
@ -45,10 +42,10 @@ namespace Crafter {
|
|||
T x, y;
|
||||
};
|
||||
};
|
||||
VectorBase<T, 2, Aligment>(float x, float y): x(x), y(y) {
|
||||
constexpr VectorBase<T, 2, Aligment>(float x, float y): x(x), y(y) {
|
||||
|
||||
}
|
||||
VectorBase<T, 2, Aligment>() = default;
|
||||
constexpr VectorBase<T, 2, Aligment>() = default;
|
||||
};
|
||||
|
||||
template <typename T, std::uint32_t Aligment>
|
||||
|
|
@ -59,10 +56,10 @@ namespace Crafter {
|
|||
T x, y, z;
|
||||
};
|
||||
};
|
||||
VectorBase<T, 3, Aligment>(float x, float y, float z): x(x), y(y), z(z) {
|
||||
constexpr VectorBase<T, 3, Aligment>(float x, float y, float z): x(x), y(y), z(z) {
|
||||
|
||||
}
|
||||
VectorBase<T, 3, Aligment>() = default;
|
||||
constexpr VectorBase<T, 3, Aligment>() = default;
|
||||
};
|
||||
|
||||
template <typename T, std::uint32_t Aligment>
|
||||
|
|
@ -73,10 +70,10 @@ namespace Crafter {
|
|||
T x, y, z, w;
|
||||
};
|
||||
};
|
||||
VectorBase<T, 4, Aligment>(float x, float y, float z, float w): x(x), y(y), z(z), w(w) {
|
||||
constexpr VectorBase<T, 4, Aligment>(float x, float y, float z, float w): x(x), y(y), z(z), w(w) {
|
||||
|
||||
}
|
||||
VectorBase<T, 4, Aligment>() = default;
|
||||
constexpr VectorBase<T, 4, Aligment>() = default;
|
||||
};
|
||||
|
||||
template <typename T, std::uint32_t Len>
|
||||
|
|
@ -100,10 +97,10 @@ namespace Crafter {
|
|||
T x, y;
|
||||
};
|
||||
};
|
||||
VectorBase<T, 2, 0>(float x, float y): x(x), y(y) {
|
||||
constexpr VectorBase<T, 2, 0>(float x, float y): x(x), y(y) {
|
||||
|
||||
}
|
||||
VectorBase<T, 2, 0>() = default;
|
||||
constexpr VectorBase<T, 2, 0>() = default;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
|
|
@ -114,10 +111,10 @@ namespace Crafter {
|
|||
T x, y, z;
|
||||
};
|
||||
};
|
||||
VectorBase<T, 3, 0>(float x, float y, float z): x(x), y(y), z(z) {
|
||||
constexpr VectorBase<T, 3, 0>(float x, float y, float z): x(x), y(y), z(z) {
|
||||
|
||||
}
|
||||
VectorBase<T, 3, 0>() = default;
|
||||
constexpr VectorBase<T, 3, 0>() = default;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
|
|
@ -128,49 +125,28 @@ namespace Crafter {
|
|||
T x, y, z, w;
|
||||
};
|
||||
};
|
||||
VectorBase<T, 4, 0>(float x, float y, float z, float w): x(x), y(y), z(z), w(w) {
|
||||
constexpr VectorBase<T, 4, 0>(float x, float y, float z, float w): x(x), y(y), z(z), w(w) {
|
||||
|
||||
}
|
||||
VectorBase<T, 4, 0>() = default;
|
||||
constexpr VectorBase<T, 4, 0>() = default;
|
||||
};
|
||||
|
||||
export template <typename T, std::uint32_t Len, std::uint32_t Aligment>
|
||||
class Vector : public VectorBase<T, Len, Aligment> {
|
||||
public:
|
||||
Vector(float x, float y, float z, float w ) requires(std::same_as<T, float> && Len == 4) : VectorBase<T, Len, Aligment>(x, y, z, w) {
|
||||
constexpr Vector(float x, float y, float z, float w ) requires(std::same_as<T, float> && Len == 4) : VectorBase<T, Len, Aligment>(x, y, z, w) {
|
||||
|
||||
}
|
||||
Vector(float x, float y, float z) requires(std::same_as<T, float> && Len == 3) : VectorBase<T, Len, Aligment>(x, y, z) {
|
||||
constexpr Vector(float x, float y, float z) requires(std::same_as<T, float> && Len == 3) : VectorBase<T, Len, Aligment>(x, y, z) {
|
||||
|
||||
}
|
||||
Vector(float x, float y) requires(std::same_as<T, float> && Len == 2) : VectorBase<T, Len, Aligment>(x, y) {
|
||||
constexpr Vector(float x, float y) requires(std::same_as<T, float> && Len == 2) : VectorBase<T, Len, Aligment>(x, y) {
|
||||
|
||||
}
|
||||
Vector() = default;
|
||||
|
||||
static Vector<T, Len, Aligment> QuaternionRotationRollPitchYaw(float Pitch, float Yaw, float Roll) requires(Len == 4) {
|
||||
const float halfpitch = Pitch * 0.5f;
|
||||
float cp = std::cosf(halfpitch);
|
||||
float sp = std::sinf(halfpitch);
|
||||
|
||||
const float halfyaw = Yaw * 0.5f;
|
||||
float cy = std::cosf(halfyaw);
|
||||
float sy = std::sinf(halfyaw);
|
||||
|
||||
const float halfroll = Roll * 0.5f;
|
||||
float cr = std::cosf(halfroll);
|
||||
float sr = std::sinf(halfroll);
|
||||
|
||||
return Vector<T, Len, Aligment>(
|
||||
cr * sp * cy + sr * cp * sy,
|
||||
cr * cp * sy - sr * sp * cy,
|
||||
sr * cp * cy - cr * sp * sy,
|
||||
cr * cp * cy + sr * sp * sy
|
||||
);
|
||||
}
|
||||
constexpr Vector() = default;
|
||||
|
||||
template <std::uint32_t Blen, std::uint32_t BAlignment>
|
||||
Vector<T, Len, Aligment> operator+(Vector<T, Blen, BAlignment> b){
|
||||
constexpr Vector<T, Len, Aligment> operator+(Vector<T, Blen, BAlignment> b) const {
|
||||
Vector<T, Len, Aligment> resultVector;
|
||||
for(std::uint32_t i = 0; i < std::min(Len, Blen); i++) {
|
||||
resultVector.v[i] = this->v[i]+b.v[i];
|
||||
|
|
@ -178,14 +154,14 @@ namespace Crafter {
|
|||
return resultVector;
|
||||
}
|
||||
template <std::uint32_t Blen, std::uint32_t BAlignment>
|
||||
Vector<T, Len, Aligment> operator-(Vector<T, Blen, BAlignment> b){
|
||||
constexpr Vector<T, Len, Aligment> operator-(Vector<T, Blen, BAlignment> b) const {
|
||||
Vector<T, Len, Aligment> resultVector;
|
||||
for(std::uint32_t i = 0; i < std::min(Len, Blen); i++) {
|
||||
resultVector.v[i] = this->v[i]-b.v[i];
|
||||
}
|
||||
return resultVector;
|
||||
}
|
||||
Vector<T, Len, Aligment> operator-(){
|
||||
constexpr Vector<T, Len, Aligment> operator-(){
|
||||
Vector<T, Len, Aligment> resultVector;
|
||||
for(std::uint32_t i = 0; i < Len; i++) {
|
||||
resultVector.v[i] = -this->v[i];
|
||||
|
|
@ -193,7 +169,7 @@ namespace Crafter {
|
|||
return resultVector;
|
||||
}
|
||||
template <std::uint32_t Blen, std::uint32_t BAlignment>
|
||||
Vector<T, Len, Aligment> operator*(Vector<T, Blen, BAlignment> b){
|
||||
constexpr Vector<T, Len, Aligment> operator*(Vector<T, Blen, BAlignment> b) const {
|
||||
Vector<T, Len, Aligment> resultVector;
|
||||
for(std::uint32_t i = 0; i < std::min(Len, Blen); i++) {
|
||||
resultVector.v[i] = this->v[i]*b.v[i];
|
||||
|
|
@ -201,141 +177,101 @@ namespace Crafter {
|
|||
return resultVector;
|
||||
}
|
||||
template <std::uint32_t Blen, std::uint32_t BAlignment>
|
||||
Vector<T, Len, Aligment> operator/(Vector<T, Blen, BAlignment> b){
|
||||
constexpr Vector<T, Len, Aligment> operator/(Vector<T, Blen, BAlignment> b) const {
|
||||
Vector<T, Len, Aligment> resultVector;
|
||||
for(std::uint32_t i = 0; i < std::min(Len, Blen); i++) {
|
||||
resultVector.v[i] = this->v[i]/b.v[i];
|
||||
}
|
||||
return resultVector;
|
||||
}
|
||||
|
||||
Vector<T, Len, Aligment> Rotate(Vector<T, 4, Aligment> rotation) requires(Len == 3) {
|
||||
Vector<T, 4, Aligment> q = rotation.QuaternionConjugate();
|
||||
Vector<T, 4, Aligment> result = q.QuaternionMultiply(Vector<T, 4, Aligment>(this->x, this->y, this->z, 0));
|
||||
return Vector<T, Len, Aligment>(result.x, result.y, result.z);
|
||||
template <std::uint32_t Blen, std::uint32_t BAlignment>
|
||||
constexpr void operator+=(Vector<T, Blen, BAlignment> b){
|
||||
for(std::uint32_t i = 0; i < std::min(Len, Blen); i++) {
|
||||
this->v[i]+=b.v[i];
|
||||
}
|
||||
}
|
||||
template <std::uint32_t Blen, std::uint32_t BAlignment>
|
||||
constexpr void operator-=(Vector<T, Blen, BAlignment> b){
|
||||
for(std::uint32_t i = 0; i < std::min(Len, Blen); i++) {
|
||||
this->v[i]-=b.v[i];
|
||||
}
|
||||
}
|
||||
template <std::uint32_t Blen, std::uint32_t BAlignment>
|
||||
constexpr void operator*=(Vector<T, Blen, BAlignment> b){
|
||||
for(std::uint32_t i = 0; i < std::min(Len, Blen); i++) {
|
||||
this->v[i]*=b.v[i];
|
||||
}
|
||||
}
|
||||
template <std::uint32_t Blen, std::uint32_t BAlignment>
|
||||
constexpr void operator/=(Vector<T, Blen, BAlignment> b){
|
||||
for(std::uint32_t i = 0; i < std::min(Len, Blen); i++) {
|
||||
this->v[i]/=b.v[i];
|
||||
}
|
||||
}
|
||||
|
||||
Vector<T, Len, Aligment> Normalize() requires(Len == 3) {
|
||||
constexpr void Normalize() {
|
||||
float fLength = Length();
|
||||
|
||||
// Prevent divide by zero
|
||||
if (fLength > 0)
|
||||
{
|
||||
if (fLength > 0) {
|
||||
fLength = 1.0f / fLength;
|
||||
}
|
||||
|
||||
return Vector<T, Len, Aligment>(this->v[0] * fLength, this->v[1] * fLength, this->v[2] * fLength);
|
||||
}
|
||||
|
||||
Vector<T, Len, Aligment> Normalize() requires(Len == 4) {
|
||||
float fLength = Length();
|
||||
|
||||
// Prevent divide by zero
|
||||
if (fLength > 0)
|
||||
{
|
||||
fLength = 1.0f / fLength;
|
||||
for(std::uint32_t i = 0; i < Len; i++) {
|
||||
this->v[i] *= fLength;
|
||||
}
|
||||
|
||||
return Vector<T, Len, Aligment>(this->v[0] * fLength, this->v[1] * fLength, this->v[2] * fLength, this->v[3] * fLength);
|
||||
}
|
||||
|
||||
float Length()
|
||||
{
|
||||
constexpr float Length() const {
|
||||
float Result = LengthSq();
|
||||
return std::sqrtf(Result);
|
||||
}
|
||||
|
||||
Vector<T, Len, Aligment> ReciprocalLength() requires(Len == 3) {
|
||||
Vector<T, Len, Aligment> Result = LengthSq();
|
||||
Result = ReciprocalSqrt(Result);
|
||||
return Result;
|
||||
constexpr float LengthSq() const {
|
||||
return Dot(*this, *this);
|
||||
}
|
||||
|
||||
Vector<T, Len, Aligment> ReciprocalSqrt() requires(Len == 3)
|
||||
{
|
||||
return Vector<T, Len, Aligment>(
|
||||
1.f / std::sqrtf(this->v[0]),
|
||||
1.f / std::sqrtf(this->v[1]),
|
||||
1.f / std::sqrtf(this->v[2])
|
||||
template <typename AT, std::uint32_t Alen, std::uint32_t AAlignment, typename BT, std::uint32_t Blen, std::uint32_t BAlignment>
|
||||
constexpr static Vector<T, Len, Aligment> Cross(Vector<AT, Alen, AAlignment> a, Vector<BT, Blen, BAlignment> b) requires(Len == 3 && Alen >= 3 && Blen >= 3) {
|
||||
return Vector<T, Len, Aligment>(
|
||||
(a.v[1] * b.v[2]) - (a.v[2] * b.v[1]),
|
||||
(a.v[2] * b.v[0]) - (a.v[0] * b.v[2]),
|
||||
(a.v[0] * b.v[1]) - (a.v[1] * b.v[0])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
float LengthSq()
|
||||
{
|
||||
return Dot(*this);
|
||||
}
|
||||
|
||||
float Dot(Vector<T, Len, Aligment> v2) requires(Len == 3)
|
||||
{
|
||||
return this->v[0] * v2.v[0] + this->v[1] * v2.v[1] + this->v[2] * v2.v[2];
|
||||
}
|
||||
|
||||
float Dot(Vector<T, Len, Aligment> v2) requires(Len == 4)
|
||||
{
|
||||
return this->v[0] * v2.v[0] + this->v[1] * v2.v[1] + this->v[2] * v2.v[2] + this->v[3] * v2.v[3];
|
||||
}
|
||||
|
||||
Vector<T, Len, Aligment> Cross(Vector<T, Len, Aligment> v2) requires(Len == 3) {
|
||||
return Vector<T, Len, Aligment>(
|
||||
(this->v[1] * v2.v[2]) - (this->v[2] * v2.v[1]),
|
||||
(this->v[2] * v2.v[0]) - (this->v[0] * v2.v[2]),
|
||||
(this->v[0] * v2.v[1]) - (this->v[1] * v2.v[0])
|
||||
);
|
||||
};
|
||||
|
||||
Vector<T, Len, Aligment> QuaternionConjugate() requires(Len == 4) {
|
||||
return Vector<T, Len, Aligment>(
|
||||
-this->x,
|
||||
-this->y,
|
||||
-this->z,
|
||||
this->w
|
||||
);
|
||||
}
|
||||
|
||||
Vector<T, Len, Aligment> QuaternionMultiply(Vector<T, Len, Aligment> q2) requires(Len == 4) {
|
||||
return Vector<T, Len, Aligment>(
|
||||
(q2.v[3] * this->v[0]) + (q2.v[0] * this->v[3]) + (q2.v[1] * this->v[2]) - (q2.v[2] * this->v[1]),
|
||||
(q2.v[3] * this->v[1]) - (q2.v[0] * this->v[2]) + (q2.v[1] * this->v[3]) + (q2.v[2] * this->v[0]),
|
||||
(q2.v[3] * this->v[2]) + (q2.v[0] * this->v[1]) - (q2.v[1] * this->v[0]) + (q2.v[2] * this->v[3]),
|
||||
(q2.v[3] * this->v[3]) - (q2.v[0] * this->v[0]) - (q2.v[1] * this->v[1]) - (q2.v[2] * this->v[2])
|
||||
);
|
||||
}
|
||||
|
||||
Vector<T, 4, Aligment> QuaternionRotationAxis(float angle) requires(Len == 3) {
|
||||
Vector<T, 3, Aligment> Normal = Normalize();
|
||||
return Normal.QuaternionRotationNormal(angle);
|
||||
}
|
||||
|
||||
Vector<T, 4, Aligment> QuaternionRotationNormal(float angle) requires(Len == 3) {
|
||||
Vector<T, 4, Aligment> N = Vector<T, 4, Aligment>(this->x, this->y, this->z, 1);
|
||||
|
||||
float SinV, CosV;
|
||||
XMScalarSinCos(&SinV, &CosV, 0.5f * angle);
|
||||
|
||||
Vector<T, 4, Aligment> Scale = Vector<T, 4, Aligment>(SinV, SinV, SinV, CosV);
|
||||
return N * Scale;
|
||||
}
|
||||
|
||||
static Vector<T, 4, Aligment> NegativeMultiplySubtract(Vector<T, 4, Aligment> a, Vector<T, 4, Aligment> b, Vector<T, 4, Aligment> c) requires(Len == 4) {
|
||||
return Vector<T, 4, Aligment>(
|
||||
c.v[0] - (a.v[0] * b.v[0]),
|
||||
c.v[1] - (a.v[1] * b.v[1]),
|
||||
c.v[2] - (a.v[2] * b.v[2]),
|
||||
c.v[3] - (a.v[3] * b.v[3])
|
||||
);
|
||||
template <typename AT, std::uint32_t Alen, std::uint32_t AAlignment, typename BT, std::uint32_t Blen, std::uint32_t BAlignment>
|
||||
constexpr static float Dot(Vector<AT, Alen, AAlignment> a, Vector<BT, Blen, BAlignment> b) requires(Alen >= Len && Blen >= Len) {
|
||||
float accumulate = a.v[0] * b.v[0];
|
||||
for(std::uint32_t i = 1; i < Len; i++) {
|
||||
accumulate += a.v[i] * b.v[i];
|
||||
}
|
||||
return accumulate;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
template <>
|
||||
struct std::formatter<Crafter::Vector<float, 3, 4>> : std::formatter<std::string> {
|
||||
auto format(const Crafter::Vector<float, 3, 4>& obj, format_context& ctx) const {
|
||||
template <typename T, std::uint32_t Aligment>
|
||||
struct std::formatter<Crafter::Vector<T, 2, Aligment>> : std::formatter<std::string> {
|
||||
auto format(const Crafter::Vector<T, 2, Aligment>& obj, format_context& ctx) const {
|
||||
return std::formatter<std::string>::format(std::format("{{{}, {}}}",
|
||||
obj.x, obj.y
|
||||
), ctx);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, std::uint32_t Aligment>
|
||||
struct std::formatter<Crafter::Vector<T, 3, Aligment>> : std::formatter<std::string> {
|
||||
auto format(const Crafter::Vector<T, 3, Aligment>& obj, format_context& ctx) const {
|
||||
return std::formatter<std::string>::format(std::format("{{{}, {}, {}}}",
|
||||
obj.x, obj.y, obj.z
|
||||
), ctx);
|
||||
}
|
||||
auto format(const Crafter::Vector<float, 4, 4>& obj, format_context& ctx) const {
|
||||
};
|
||||
|
||||
template <typename T, std::uint32_t Aligment>
|
||||
struct std::formatter<Crafter::Vector<T, 4, Aligment>> : std::formatter<std::string> {
|
||||
auto format(const Crafter::Vector<T, 4, Aligment>& obj, format_context& ctx) const {
|
||||
return std::formatter<std::string>::format(std::format("{{{}, {}, {}, {}}}",
|
||||
obj.x, obj.y, obj.z, obj.w
|
||||
), ctx);
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|||
*/
|
||||
|
||||
export module Crafter.Math;
|
||||
export import :BasicTypes;
|
||||
export import :Basic;
|
||||
export import :Vector;
|
||||
export import :MatrixRowMajor;
|
||||
export import :Misc;
|
||||
export import :Ray;
|
||||
13
interfaces/main.cpp
Normal file
13
interfaces/main.cpp
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import Crafter.Math;
|
||||
import std;
|
||||
|
||||
using namespace Crafter;
|
||||
|
||||
int main() {
|
||||
Vector<float, 3, 0> test(1,2,3);
|
||||
Vector<float, 3, 0> test2(1,2,3);
|
||||
Vector<float, 3, 0> test3(1,2,3);
|
||||
Vector<float, 3, 0> test4(1,2,3);
|
||||
Vector<float, 3, 0> test5(1,2,3);
|
||||
std::cout << IntersectionTestRayTriangle(test, test2, test3, test4, test5) << std::endl;
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
"configurations": [
|
||||
{
|
||||
"name": "base",
|
||||
"interfaces": ["interfaces/Crafter.Math-Vector", "interfaces/Crafter.Math-BasicTypes", "interfaces/Crafter.Math-MatrixRowMajor", "interfaces/Crafter.Math-Misc", "interfaces/Crafter.Math"],
|
||||
"interfaces": ["interfaces/Crafter.Math-Vector", "interfaces/Crafter.Math-Basic", "interfaces/Crafter.Math-MatrixRowMajor", "interfaces/Crafter.Math", "interfaces/Crafter.Math-Ray"],
|
||||
"implementations": []
|
||||
},
|
||||
{
|
||||
|
|
@ -16,6 +16,12 @@
|
|||
"name": "lib-debug",
|
||||
"extends": ["lib"],
|
||||
"debug": true
|
||||
},
|
||||
{
|
||||
"name": "test",
|
||||
"implementations": ["interfaces/main"],
|
||||
"extends": ["base"],
|
||||
"debug": true
|
||||
}
|
||||
]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue