A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
Vector.cc
1#include "Vector.hh"
2
3namespace EGG {
4
6f32 Vector2f::normalise() {
7 f32 len = length();
8 if (len != 0.0f) {
9 *this = *this * (1.0f / len);
10 }
11
12 return len;
13}
14
16void Vector2f::normalise2() {
17 f32 sqLen = dot();
18 if (sqLen > std::numeric_limits<f32>::epsilon()) {
19 *this *= EGG::Mathf::frsqrt(sqLen);
20 }
21}
22
24void Vector2f::read(Stream &stream) {
25 x = stream.read_f32();
26 y = stream.read_f32();
27}
28
30f32 Vector3f::ps_dot() const {
31 return ps_dot(*this);
32}
33
36f32 Vector3f::ps_dot(const Vector3f &rhs) const {
37 f32 y_ = y * rhs.y;
38 f32 xy = Mathf::fma(x, rhs.x, y_);
39 return xy + z * rhs.z;
40}
41
44 f32 x_ = x * x;
45 f32 zx = Mathf::fma(z, z, x_);
46 return zx + y * y;
47}
48
53 f32 len = 0.0f;
54
55 if (squaredLength() > std::numeric_limits<f32>::epsilon()) {
56 len = length();
57 *this *= (1.0f / len);
58 }
59
60 return len;
61}
62
64EGG::Vector3f Vector3f::ps_normalize() const {
65 f32 dot = Mathf::fma(z, z, x * x) + y * y;
66 return *this * EGG::Mathf::frsqrt(dot);
67}
68
70std::pair<f32, EGG::Vector3f> Vector3f::ps_normalized() {
71 f32 mag = ps_length();
72 if (mag <= 0.0f) {
73 return std::make_pair(mag, EGG::Vector3f::zero);
74 }
75
76 return std::make_pair(mag, *this * (1.0f / mag));
77}
78
80void Vector3f::normalise2() {
81 f32 sqLen = squaredLength();
82 if (sqLen > std::numeric_limits<f32>::epsilon()) {
83 *this *= Mathf::frsqrt(sqLen);
84 }
85}
86
90 Vector3f out;
91
92 out.x = x > rhs.x ? x : rhs.x;
93 out.y = y > rhs.y ? y : rhs.y;
94 out.z = z > rhs.z ? z : rhs.z;
95
96 return out;
97}
98
102 Vector3f out;
103
104 out.x = x < rhs.x ? x : rhs.x;
105 out.y = y < rhs.y ? y : rhs.y;
106 out.z = z < rhs.z ? z : rhs.z;
107
108 return out;
109}
110
113f32 Vector3f::ps_sqDistance(const Vector3f &rhs) const {
114 const EGG::Vector3f diff = *this - rhs;
115 return diff.ps_dot();
116}
117
120Vector3f Vector3f::perpInPlane(const EGG::Vector3f &rhs, bool normalise) const {
121 if (Mathf::abs(dot(rhs)) == 1.0f) {
122 return EGG::Vector3f::zero;
123 }
124
125 f32 _x = (rhs.z * x - rhs.x * z) * rhs.z - (rhs.x * y - rhs.y * x) * rhs.y;
126 f32 _y = (rhs.x * y - rhs.y * x) * rhs.x - (rhs.y * z - rhs.z * y) * rhs.z;
127 f32 _z = (rhs.y * z - rhs.z * y) * rhs.y - (rhs.z * x - rhs.x * z) * rhs.x;
128
129 EGG::Vector3f ret(_x, _y, _z);
130
131 if (normalise) {
132 ret.normalise();
133 }
134
135 return ret;
136}
137
139void Vector3f::read(Stream &stream) {
140 x = stream.read_f32();
141 y = stream.read_f32();
142 z = stream.read_f32();
143}
144
145} // namespace EGG
A stream of data, abstracted to allow for continuous seeking.
Definition Stream.hh:10
static f32 fma(f32 x, f32 y, f32 z)
Fused multiply-add operation.
Definition Math.hh:77
f32 frsqrt(f32 x)
Definition Math.cc:315
EGG core library.
Definition Archive.cc:6
void read(Stream &stream)
Initializes a Vector2f by reading 8 bytes from the stream.
Definition Vector.cc:24
A 3D float vector.
Definition Vector.hh:88
f32 normalise()
Normalizes the vector and returns the original length.
Definition Vector.cc:52
f32 dot(const Vector3f &rhs) const
The dot product between two vectors.
Definition Vector.hh:187
f32 length() const
The square root of the vector's dot product.
Definition Vector.hh:192
f32 squaredLength() const
The dot product between the vector and itself.
Definition Vector.hh:182
f32 ps_squareMag() const
Differs from ps_dot due to variation in which operands are fused.
Definition Vector.cc:43
void read(Stream &stream)
Initializes a Vector3f by reading 12 bytes from the stream.
Definition Vector.cc:139
Vector3f perpInPlane(const EGG::Vector3f &rhs, bool normalise) const
Calculates the orthogonal vector, based on the plane defined by this vector and rhs.
Definition Vector.cc:120
Vector3f maximize(const Vector3f &rhs) const
Returns a vector whose elements are the max of the elements of both vectors.
Definition Vector.cc:89
f32 ps_sqDistance(const Vector3f &rhs) const
Paired-singles impl. of sqDistance.
Definition Vector.cc:113
f32 ps_dot() const
Paired-singles dot product implementation.
Definition Vector.cc:30
Vector3f minimize(const Vector3f &rhs) const
Returns a vector whose elements are the min of the elements of both vectors.
Definition Vector.cc:101