From 2ef9d69288fde1ab455ab4af670ea6bf9d0214be Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Tue, 4 Oct 2022 12:57:04 -0700 Subject: [PATCH] Match and link CVector3d, add CVector3i.s Former-commit-id: cdbbfba34eba8b5278a91921de2b14ab7e8e59f5 --- asm/Kyoto/Math/CVector3i.s | 10 ++++++ configure.py | 1 + include/Kyoto/Math/CMath.hpp | 2 +- include/Kyoto/Math/CVector3d.hpp | 33 +++++++++++++++++ obj_files.mk | 2 +- src/Kyoto/Math/CVector3d.cpp | 62 ++++++++++++++++++++++++++++++++ 6 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 asm/Kyoto/Math/CVector3i.s create mode 100644 include/Kyoto/Math/CVector3d.hpp create mode 100644 src/Kyoto/Math/CVector3d.cpp diff --git a/asm/Kyoto/Math/CVector3i.s b/asm/Kyoto/Math/CVector3i.s new file mode 100644 index 00000000..7bf62958 --- /dev/null +++ b/asm/Kyoto/Math/CVector3i.s @@ -0,0 +1,10 @@ +.include "macros.inc" + +.section .text, "ax" + +.global __ct__9CVector3iFiii +__ct__9CVector3iFiii: +/* 80314C48 00311BA8 90 83 00 00 */ stw r4, 0(r3) +/* 80314C4C 00311BAC 90 A3 00 04 */ stw r5, 4(r3) +/* 80314C50 00311BB0 90 C3 00 08 */ stw r6, 8(r3) +/* 80314C54 00311BB4 4E 80 00 20 */ blr diff --git a/configure.py b/configure.py index a0e22f07..640bcb9c 100755 --- a/configure.py +++ b/configure.py @@ -33,6 +33,7 @@ COMPLETE_OBJECTS = [ "Kyoto/Graphics/CGX", "Kyoto/Particles/CWarp", "Kyoto/Math/CPlane", + "Kyoto/Math/CVector3d", "Kyoto/Math/CVector3i", "Kyoto/CRandom16", "Kyoto/CCrc32", diff --git a/include/Kyoto/Math/CMath.hpp b/include/Kyoto/Math/CMath.hpp index 1d21d084..3268e2a9 100644 --- a/include/Kyoto/Math/CMath.hpp +++ b/include/Kyoto/Math/CMath.hpp @@ -50,7 +50,7 @@ public: // BaryToWorld__5CMathFRC9CVector3fRC9CVector3fRC9CVector3fRC9CVector3f global // GetCatmullRomSplinePoint__5CMathFRC9CVector3fRC9CVector3fRC9CVector3fRC9CVector3ff global // FastSqrtF__5CMathFf weak - // SqrtD__5CMathFd global + static double SqrtD(double x); // AbsD__5CMathFd weak // IsEpsilon__5CMathFfff global // FastMin__5CMathFff weak diff --git a/include/Kyoto/Math/CVector3d.hpp b/include/Kyoto/Math/CVector3d.hpp new file mode 100644 index 00000000..b1625ef6 --- /dev/null +++ b/include/Kyoto/Math/CVector3d.hpp @@ -0,0 +1,33 @@ +#ifndef __CVECTOR3D_HPP__ +#define __CVECTOR3D_HPP__ + + +#include "Kyoto/Math/CVector3f.hpp" + +class CVector3d { +public: + CVector3d(double x, double y, double z); + CVector3d(const CVector3f& other); + double Magnitude() const; + double MagSquared() const; + + CVector3d AsNormalized() const; + CVector3f AsCVector3f() const; + + double GetX() const { return mX; } + double GetY() const { return mY; } + double GetZ() const { return mZ; } + + static double Dot(const CVector3d& a, const CVector3d& b); + static CVector3d Cross(const CVector3d& a, const CVector3d& b); + + +private: + double mX; + double mY; + double mZ; +}; + +CVector3d operator+(const CVector3d& other); + +#endif // __CVECTOR3D_HPP__ diff --git a/obj_files.mk b/obj_files.mk index 1b232f8b..a5986131 100644 --- a/obj_files.mk +++ b/obj_files.mk @@ -522,7 +522,7 @@ KYOTO_1 :=\ $(BUILD_DIR)/asm/Kyoto/Math/CUnitVector3f.o\ $(BUILD_DIR)/asm/Kyoto/Math/CVector2f.o\ $(BUILD_DIR)/asm/Kyoto/Math/CVector2i.o\ - $(BUILD_DIR)/asm/Kyoto/Math/CVector3d.o\ + $(BUILD_DIR)/src/Kyoto/Math/CVector3d.o\ $(BUILD_DIR)/asm/Kyoto/Math/CVector3f.o\ $(BUILD_DIR)/src/Kyoto/Math/CVector3i.o\ $(BUILD_DIR)/asm/Kyoto/Math/RMathUtils.o\ diff --git a/src/Kyoto/Math/CVector3d.cpp b/src/Kyoto/Math/CVector3d.cpp new file mode 100644 index 00000000..2d1a5a8e --- /dev/null +++ b/src/Kyoto/Math/CVector3d.cpp @@ -0,0 +1,62 @@ +#include "Kyoto/Math/CVector3d.hpp" +#include "Kyoto/Math/CMath.hpp" + +CVector3d::CVector3d(double x, double y, double z) : mX(x), mY(y), mZ(z) {} + +CVector3d::CVector3d(const CVector3f& other) +: mX(other.GetX()), mY(other.GetY()), mZ(other.GetZ()) {} + +double CVector3d::Magnitude() const { + double ret = mX * mX; + ret += mY * mY; + ret += mZ * mZ; + return CMath::SqrtD(ret); +} + +double CVector3d::MagSquared() const { + double ret = mX * mX; + ret += mY * mY; + ret += mZ * mZ; + return ret; +} + +CVector3d CVector3d::AsNormalized() const { + const double mag = 1.f / Magnitude(); + return CVector3d(mX * mag, mY * mag, mZ * mag); +} + +CVector3f CVector3d::AsCVector3f() const { + float z = mZ; + float y = mY; + float x = mX; + return CVector3f(x, y, z); +} + +double CVector3d::Dot(const CVector3d& a, const CVector3d& b) { + double ret = a.GetX() * b.GetX(); + ret += a.GetY() * b.GetY(); + ret += a.GetZ() * b.GetZ(); + return ret; +} + +CVector3d CVector3d::Cross(const CVector3d& lhs, const CVector3d& rhs) { + double x1 = lhs.GetX(); + double y1 = lhs.GetY(); + double z1 = lhs.GetZ(); + double x2 = rhs.GetX(); + double y2 = rhs.GetY(); + double z2 = rhs.GetZ(); + return CVector3d((y1 * z2) - (y2 * z1), (z1 * x2) - (z2 * x1), (x1 * y2) - (x2 * y1)); +} + +CVector3d operator+(const CVector3d& lhs, const CVector3d& rhs) { + return CVector3d(lhs.GetX() + rhs.GetX(), lhs.GetY() + rhs.GetY(), lhs.GetZ() + rhs.GetZ()); +} + +CVector3d operator-(const CVector3d& lhs, const CVector3d& rhs) { + return CVector3d(lhs.GetX() - rhs.GetX(), lhs.GetY() - rhs.GetY(), lhs.GetZ() - rhs.GetZ()); +} + +CVector3d operator*(double lhs, const CVector3d& rhs) { + return CVector3d(lhs * rhs.GetX(), lhs * rhs.GetY(), lhs * rhs.GetZ()); +}