isle-portable/LEGO1/realtime/vector.h

454 lines
13 KiB
C
Raw Normal View History

#ifndef VECTOR_H
#define VECTOR_H
2023-10-24 19:38:27 -04:00
2024-01-12 19:34:38 -05:00
#include "compat.h"
2024-01-12 19:34:38 -05:00
#include <math.h>
#include <memory.h>
// Note: Many functions most likely take const references/pointers instead of non-const.
// The class needs to undergo a very careful refactoring to fix that (no matches should break).
2024-01-12 19:34:38 -05:00
// VTABLE: LEGO1 0x100d4288
// VTABLE: BETA10 0x101b8440
2024-01-12 19:34:38 -05:00
// SIZE 0x08
class Vector2 {
public:
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x1000c0f0
// FUNCTION: BETA10 0x100116a0
Vector2(float* p_data) { SetData(p_data); }
2024-01-12 19:34:38 -05:00
// Note: virtual function overloads appear in the virtual table
// in reverse order of appearance.
// FUNCTION: LEGO1 0x10001f80
virtual void AddImpl(float* p_value)
{
2024-01-12 19:34:38 -05:00
m_data[0] += p_value[0];
m_data[1] += p_value[1];
} // vtable+0x04
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10001fa0
virtual void AddImpl(float p_value)
{
2024-01-12 19:34:38 -05:00
m_data[0] += p_value;
m_data[1] += p_value;
} // vtable+0x00
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10001fc0
virtual void SubImpl(float* p_value)
{
m_data[0] -= p_value[0];
m_data[1] -= p_value[1];
} // vtable+0x08
2024-01-12 19:34:38 -05:00
// Those are also overloads in all likelihood,
// but we need a type to do that.
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10002000
virtual void MulScalarImpl(float* p_value)
{
2024-01-12 19:34:38 -05:00
m_data[0] *= *p_value;
m_data[1] *= *p_value;
} // vtable+0x0c
// FUNCTION: LEGO1 0x10001fe0
virtual void MulVectorImpl(float* p_value)
{
2024-01-12 19:34:38 -05:00
m_data[0] *= p_value[0];
m_data[1] *= p_value[1];
} // vtable+0x10
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10002020
virtual void DivScalarImpl(float* p_value)
{
m_data[0] /= *p_value;
m_data[1] /= *p_value;
} // vtable+0x14
// FUNCTION: LEGO1 0x10002040
virtual float DotImpl(float* p_a, float* p_b) const { return p_b[0] * p_a[0] + p_b[1] * p_a[1]; } // vtable+0x18
// FUNCTION: LEGO1 0x10002060
// FUNCTION: BETA10 0x10010c90
2024-01-12 19:34:38 -05:00
virtual void SetData(float* p_data) { m_data = p_data; } // vtable+0x1c
// FUNCTION: LEGO1 0x10002070
virtual void EqualsImpl(float* p_data) { memcpy(m_data, p_data, sizeof(float) * 2); } // vtable+0x20
// FUNCTION: LEGO1 0x10002090
virtual float* GetData() { return m_data; } // vtable+0x28
// FUNCTION: LEGO1 0x100020a0
virtual const float* GetData() const { return m_data; } // vtable+0x24
// FUNCTION: LEGO1 0x100020b0
virtual void Clear() { memset(m_data, 0, sizeof(float) * 2); } // vtable+0x2c
// FUNCTION: LEGO1 0x100020d0
virtual float Dot(float* p_a, float* p_b) const { return DotImpl(p_a, p_b); } // vtable+0x3c
// FUNCTION: LEGO1 0x100020f0
// FUNCTION: BETA10 0x100108c0
2024-01-12 19:34:38 -05:00
virtual float Dot(Vector2* p_a, Vector2* p_b) const { return DotImpl(p_a->m_data, p_b->m_data); } // vtable+0x38
// FUNCTION: LEGO1 0x10002110
virtual float Dot(float* p_a, Vector2* p_b) const { return DotImpl(p_a, p_b->m_data); } // vtable+0x34
// FUNCTION: LEGO1 0x10002130
virtual float Dot(Vector2* p_a, float* p_b) const { return DotImpl(p_a->m_data, p_b); } // vtable+0x30
// FUNCTION: LEGO1 0x10002150
virtual float LenSquared() const { return m_data[0] * m_data[0] + m_data[1] * m_data[1]; } // vtable+0x40
// FUNCTION: LEGO1 0x10002160
// FUNCTION: BETA10 0x10010900
2024-01-12 19:34:38 -05:00
virtual int Unitize()
{
float sq = LenSquared();
2024-01-12 19:34:38 -05:00
if (sq > 0.0f) {
float root = sqrt(sq);
if (root > 0.0f) {
2024-01-12 19:34:38 -05:00
DivScalarImpl(&root);
return 0;
}
}
2024-01-12 19:34:38 -05:00
return -1;
} // vtable+0x44
// FUNCTION: LEGO1 0x100021c0
virtual void Add(float p_value) { AddImpl(p_value); } // vtable+0x50
// FUNCTION: LEGO1 0x100021d0
virtual void Add(float* p_other) { AddImpl(p_other); } // vtable+0x4c
// FUNCTION: LEGO1 0x100021e0
virtual void Add(const Vector2& p_other) { AddImpl((float*) p_other.m_data); } // vtable+0x48
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x100021f0
virtual void Sub(const float* p_other) { SubImpl((float*) p_other); } // vtable+0x58
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10002200
virtual void Sub(const Vector2& p_other) { SubImpl((float*) p_other.m_data); } // vtable+0x54
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10002210
virtual void Mul(float* p_other) { MulVectorImpl(p_other); } // vtable+0x64
// FUNCTION: LEGO1 0x10002220
virtual void Mul(Vector2* p_other) { MulVectorImpl(p_other->m_data); } // vtable+0x60
// FUNCTION: LEGO1 0x10002230
virtual void Mul(const float& p_value) { MulScalarImpl((float*) &p_value); } // vtable+0x5c
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10002240
2024-06-13 16:09:14 -04:00
virtual void Div(const float& p_value) { DivScalarImpl((float*) &p_value); } // vtable+0x68
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10002250
virtual void SetVector(float* p_other) { EqualsImpl(p_other); } // vtable+0x70
// FUNCTION: LEGO1 0x10002260
virtual void SetVector(const Vector2* p_other) { EqualsImpl(p_other->m_data); } // vtable+0x6c
2023-10-24 19:38:27 -04:00
// SYNTHETIC: LEGO1 0x10010be0
// Vector3::operator=
// SYNTHETIC: BETA10 0x1004af40
// Vector4::operator=
Vector2& operator=(const Vector2& p_other)
{
Vector2::SetVector(&p_other);
return *this;
}
// SYNTHETIC: BETA10 0x10013460
// Vector3::operator[]
// FUNCTION: BETA10 0x10010890
float& operator[](int idx) { return m_data[idx]; }
// FUNCTION: BETA10 0x1001d140
const float& operator[](int idx) const { return m_data[idx]; }
2023-10-24 19:38:27 -04:00
protected:
float* m_data; // 0x04
};
(Proposal) Adjustments to "decomp" language (#308) * Adjustments to "decomp" language * Fix a comment * Fix accidental clang-formatting * Fix order * Fix order * Remove junk * Fix OFFSET * Adjustments based on new suggestions * Annotate globals * Globals in ISLE * More globals * Merge from parser2 branch * Allow prepending space for exact marker match * To eliminate noise, require the 0x prefix on offset for marker match * fix test from previous * Count tab stops for indented functions to reduce MISSED_END_OF_FUNCTION noise * FUNCTION to SYNTHETIC where needed * Missed marker conversion on SetAtomId * pylint cleanup, remove unused code * Fix unexpected function end, add more unit tests * Be more strict about synthetic name syntax * Revert "Missed marker conversion on SetAtomId" This reverts commit d87d665127fae7dd6e5bd48d9af14a0a829bf9e2. * Revert "FUNCTION to SYNTHETIC where needed" This reverts commit 8c815418d261ba8c5f67a9a2cae349fe4ac92db8. * Implicit lookup by name for functions * Fix VTABLE SYNTHETIC and other decomp markers * Get vtable class name * Vtable marker should identify struct * No colon for SIZE comment * Update README.md * Update README.md * Update CONTRIBUTING.md * Update README.md * Update README.md * Update CONTRIBUTING.md * Update README.md * Update CONTRIBUTING.md * Fix destructor/annotation * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md --------- Co-authored-by: disinvite <disinvite@users.noreply.github.com>
2023-12-06 07:10:45 -05:00
// VTABLE: LEGO1 0x100d4518
// VTABLE: BETA10 0x101b8398
2024-01-12 19:34:38 -05:00
// SIZE 0x08
class Vector3 : public Vector2 {
public:
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x1001d150
// FUNCTION: BETA10 0x10011660
Vector3(float* p_data) : Vector2(p_data) {}
2024-01-12 19:34:38 -05:00
// Hack: Some code initializes a Vector3 from a (most likely) const float* source.
// Example: LegoCameraController::GetWorldUp
// Vector3 however is a class that can mutate its underlying source, making
// initialization with a const source fundamentally incompatible.
Vector3(const float* p_data) : Vector2((float*) p_data) {}
2024-01-12 19:34:38 -05:00
// Note: virtual function overloads appear in the virtual table
// in reverse order of appearance.
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10002270
// FUNCTION: BETA10 0x10011350
virtual void EqualsCrossImpl(float* p_a, float* p_b)
2024-01-12 19:34:38 -05:00
{
m_data[0] = p_a[1] * p_b[2] - p_a[2] * p_b[1];
m_data[1] = p_a[2] * p_b[0] - p_a[0] * p_b[2];
m_data[2] = p_a[0] * p_b[1] - p_a[1] * p_b[0];
} // vtable+0x74
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x100022c0
// FUNCTION: BETA10 0x10011430
2024-01-12 19:34:38 -05:00
virtual void EqualsCross(Vector3* p_a, Vector3* p_b) { EqualsCrossImpl(p_a->m_data, p_b->m_data); } // vtable+0x80
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x100022e0
virtual void EqualsCross(Vector3* p_a, float* p_b) { EqualsCrossImpl(p_a->m_data, p_b); } // vtable+0x7c
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10002300
virtual void EqualsCross(float* p_a, Vector3* p_b) { EqualsCrossImpl(p_a, p_b->m_data); } // vtable+0x78
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10003bf0
virtual void Fill(const float& p_value)
2024-01-12 19:34:38 -05:00
{
m_data[0] = p_value;
m_data[1] = p_value;
m_data[2] = p_value;
2024-01-12 19:34:38 -05:00
} // vtable+0x84
2024-01-12 19:34:38 -05:00
// Vector2 overrides
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10003a60
void AddImpl(float* p_value) override
2024-01-12 19:34:38 -05:00
{
m_data[0] += p_value[0];
m_data[1] += p_value[1];
m_data[2] += p_value[2];
} // vtable+0x04
// FUNCTION: LEGO1 0x10003a90
void AddImpl(float p_value) override
2024-01-12 19:34:38 -05:00
{
m_data[0] += p_value;
m_data[1] += p_value;
m_data[2] += p_value;
} // vtable+0x00
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10003ac0
void SubImpl(float* p_value) override
2024-01-12 19:34:38 -05:00
{
m_data[0] -= p_value[0];
m_data[1] -= p_value[1];
m_data[2] -= p_value[2];
} // vtable+0x08
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10003b20
void MulScalarImpl(float* p_value) override
2024-01-12 19:34:38 -05:00
{
m_data[0] *= *p_value;
m_data[1] *= *p_value;
m_data[2] *= *p_value;
} // vtable+0x0c
// FUNCTION: LEGO1 0x10003af0
void MulVectorImpl(float* p_value) override
2024-01-12 19:34:38 -05:00
{
m_data[0] *= p_value[0];
m_data[1] *= p_value[1];
m_data[2] *= p_value[2];
} // vtable+0x10
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10003b50
void DivScalarImpl(float* p_value) override
2024-01-12 19:34:38 -05:00
{
m_data[0] /= *p_value;
m_data[1] /= *p_value;
m_data[2] /= *p_value;
} // vtable+0x14
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10003b80
float DotImpl(float* p_a, float* p_b) const override
2024-01-12 19:34:38 -05:00
{
return p_a[0] * p_b[0] + p_a[2] * p_b[2] + p_a[1] * p_b[1];
} // vtable+0x18
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10003ba0
// FUNCTION: BETA10 0x100113f0
void EqualsImpl(float* p_data) override { memcpy(m_data, p_data, sizeof(float) * 3); } // vtable+0x20
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10003bc0
// FUNCTION: BETA10 0x100114f0
void Clear() override { memset(m_data, 0, sizeof(float) * 3); } // vtable+0x2c
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10003bd0
float LenSquared() const override
2024-01-12 19:34:38 -05:00
{
return m_data[0] * m_data[0] + m_data[1] * m_data[1] + m_data[2] * m_data[2];
2024-01-12 19:34:38 -05:00
} // vtable+0x40
2024-01-12 19:34:38 -05:00
friend class Mx3DPointFloat;
};
2024-01-12 19:34:38 -05:00
// VTABLE: LEGO1 0x100d45a0
// VTABLE: BETA10 0x101bac38
// SIZE 0x08
2024-01-12 19:34:38 -05:00
class Vector4 : public Vector3 {
public:
// FUNCTION: BETA10 0x10048780
Vector4(float* p_data) : Vector3(p_data) {}
2024-01-12 19:34:38 -05:00
// Hack: Some code initializes a Vector4 from a (most likely) const float* source.
// Example: LegoCarBuild::VTable0x6c
// Vector4 however is a class that can mutate its underlying source, making
// initialization with a const source fundamentally incompatible.
Vector4(const float* p_data) : Vector3((float*) p_data) {}
2024-01-12 19:34:38 -05:00
// Note: virtual function overloads appear in the virtual table
// in reverse order of appearance.
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10002a40
virtual void SetMatrixProduct(float* p_vec, float* p_mat)
2023-10-24 19:38:27 -04:00
{
2024-01-12 19:34:38 -05:00
m_data[0] = p_vec[0] * p_mat[0] + p_vec[1] * p_mat[4] + p_vec[2] * p_mat[8] + p_vec[3] * p_mat[12];
m_data[1] = p_vec[0] * p_mat[1] + p_vec[1] * p_mat[5] + p_vec[2] * p_mat[9] + p_vec[4] * p_mat[13];
m_data[2] = p_vec[0] * p_mat[2] + p_vec[1] * p_mat[6] + p_vec[2] * p_mat[10] + p_vec[4] * p_mat[14];
m_data[3] = p_vec[0] * p_mat[3] + p_vec[1] * p_mat[7] + p_vec[2] * p_mat[11] + p_vec[4] * p_mat[15];
} // vtable+0x8c
2023-10-24 19:38:27 -04:00
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10002ae0
virtual void SetMatrixProduct(Vector4* p_a, float* p_b) { SetMatrixProduct(p_a->m_data, p_b); } // vtable+0x88
inline virtual int NormalizeQuaternion(); // vtable+0x90
inline virtual int EqualsHamiltonProduct(Vector4* p_a, Vector4* p_b); // vtable+0x94
2024-01-12 19:34:38 -05:00
// Vector3 overrides
// FUNCTION: LEGO1 0x10002870
void AddImpl(float* p_value) override
2024-01-12 19:34:38 -05:00
{
m_data[0] += p_value[0];
m_data[1] += p_value[1];
m_data[2] += p_value[2];
m_data[3] += p_value[3];
} // vtable+0x04
// FUNCTION: LEGO1 0x100028b0
void AddImpl(float p_value) override
2024-01-12 19:34:38 -05:00
{
m_data[0] += p_value;
m_data[1] += p_value;
m_data[2] += p_value;
m_data[3] += p_value;
} // vtable+0x00
// FUNCTION: LEGO1 0x100028f0
void SubImpl(float* p_value) override
2024-01-12 19:34:38 -05:00
{
m_data[0] -= p_value[0];
m_data[1] -= p_value[1];
m_data[2] -= p_value[2];
m_data[3] -= p_value[3];
} // vtable+0x08
// FUNCTION: LEGO1 0x10002970
void MulScalarImpl(float* p_value) override
2024-01-12 19:34:38 -05:00
{
m_data[0] *= *p_value;
m_data[1] *= *p_value;
m_data[2] *= *p_value;
m_data[3] *= *p_value;
} // vtable+0x0c
// FUNCTION: LEGO1 0x10002930
void MulVectorImpl(float* p_value) override
2024-01-12 19:34:38 -05:00
{
m_data[0] *= p_value[0];
m_data[1] *= p_value[1];
m_data[2] *= p_value[2];
m_data[3] *= p_value[3];
} // vtable+0x10
// FUNCTION: LEGO1 0x100029b0
void DivScalarImpl(float* p_value) override
2024-01-12 19:34:38 -05:00
{
m_data[0] /= *p_value;
m_data[1] /= *p_value;
m_data[2] /= *p_value;
m_data[3] /= *p_value;
} // vtable+0x14
// FUNCTION: LEGO1 0x100029f0
float DotImpl(float* p_a, float* p_b) const override
2024-01-12 19:34:38 -05:00
{
return p_a[0] * p_b[0] + p_a[2] * p_b[2] + (p_a[1] * p_b[1] + p_a[3] * p_b[3]);
} // vtable+0x18
// FUNCTION: LEGO1 0x10002a20
void EqualsImpl(float* p_data) override { memcpy(m_data, p_data, sizeof(float) * 4); } // vtable+0x20
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10002b00
void Clear() override { memset(m_data, 0, sizeof(float) * 4); } // vtable+0x2c
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10002b20
float LenSquared() const override
2024-01-12 19:34:38 -05:00
{
return m_data[1] * m_data[1] + m_data[0] * m_data[0] + m_data[2] * m_data[2] + m_data[3] * m_data[3];
} // vtable+0x40
// FUNCTION: LEGO1 0x10002b40
void Fill(const float& p_value) override
2024-01-12 19:34:38 -05:00
{
m_data[0] = p_value;
m_data[1] = p_value;
m_data[2] = p_value;
m_data[3] = p_value;
2024-01-12 19:34:38 -05:00
} // vtable+0x84
friend class Mx4DPointFloat;
};
// FUNCTION: LEGO1 0x10002b70
// FUNCTION: BETA10 0x10048ad0
2024-01-12 19:34:38 -05:00
inline int Vector4::NormalizeQuaternion()
{
float* v = m_data;
float magnitude = v[0] * v[0] + v[2] * v[2] + v[1] * v[1];
2024-01-12 19:34:38 -05:00
if (magnitude > 0.0f) {
float theta = v[3] * 0.5f;
v[3] = cos(theta);
magnitude = sin(theta) / sqrt(magnitude);
Vector3::MulScalarImpl(&magnitude);
2024-01-12 19:34:38 -05:00
return 0;
}
return -1;
}
inline static float QuaternionProductScalarPart(float* bDat, float* aDat)
{
// We have no indication from the beta that this function exists,
// but it helps with the stack layout of Vector4::EqualsHamiltonProduct()
return aDat[3] * bDat[3] - (aDat[0] * bDat[0] + aDat[2] * bDat[2] + aDat[1] * bDat[1]);
}
2024-01-12 19:34:38 -05:00
// FUNCTION: LEGO1 0x10002bf0
// FUNCTION: BETA10 0x10048c20
inline int Vector4::EqualsHamiltonProduct(Vector4* p_a, Vector4* p_b)
2024-01-12 19:34:38 -05:00
{
m_data[3] = QuaternionProductScalarPart(p_a->m_data, p_b->m_data);
2024-01-12 19:34:38 -05:00
Vector3::EqualsCrossImpl(p_a->m_data, p_b->m_data);
2024-01-12 19:34:38 -05:00
m_data[0] = p_b->m_data[3] * p_a->m_data[0] + p_a->m_data[3] * p_b->m_data[0] + m_data[0];
m_data[1] = p_b->m_data[1] * p_a->m_data[3] + p_a->m_data[1] * p_b->m_data[3] + m_data[1];
m_data[2] = p_b->m_data[2] * p_a->m_data[3] + p_a->m_data[2] * p_b->m_data[3] + m_data[2];
return 0;
2024-01-12 19:34:38 -05:00
}
#endif // VECTOR_H