minor rewrite
This commit is contained in:
parent
8639deac1e
commit
20ecd2ab9d
8 changed files with 173 additions and 279 deletions
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue