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
64std::pair<f32, EGG::Vector3f> Vector3f::ps_normalized() {
65 f32 mag = ps_length();
66 if (mag <= 0.0f) {
67 return std::make_pair(mag, EGG::Vector3f::zero);
68 }
69
70 return std::make_pair(mag, *this * (1.0f / mag));
71}
72
74void Vector3f::normalise2() {
75 f32 sqLen = squaredLength();
76 if (sqLen > std::numeric_limits<f32>::epsilon()) {
77 *this *= Mathf::frsqrt(sqLen);
78 }
79}
80
84 Vector3f out;
85
86 out.x = x > rhs.x ? x : rhs.x;
87 out.y = y > rhs.y ? y : rhs.y;
88 out.z = z > rhs.z ? z : rhs.z;
89
90 return out;
91}
92
96 Vector3f out;
97
98 out.x = x < rhs.x ? x : rhs.x;
99 out.y = y < rhs.y ? y : rhs.y;
100 out.z = z < rhs.z ? z : rhs.z;
101
102 return out;
103}
104
107f32 Vector3f::ps_sqDistance(const Vector3f &rhs) const {
108 const EGG::Vector3f diff = *this - rhs;
109 return diff.ps_dot();
110}
111
114Vector3f Vector3f::perpInPlane(const EGG::Vector3f &rhs, bool normalise) const {
115 if (Mathf::abs(dot(rhs)) == 1.0f) {
116 return EGG::Vector3f::zero;
117 }
118
119 f32 _x = (rhs.z * x - rhs.x * z) * rhs.z - (rhs.x * y - rhs.y * x) * rhs.y;
120 f32 _y = (rhs.x * y - rhs.y * x) * rhs.x - (rhs.y * z - rhs.z * y) * rhs.z;
121 f32 _z = (rhs.y * z - rhs.z * y) * rhs.y - (rhs.z * x - rhs.x * z) * rhs.x;
122
123 EGG::Vector3f ret(_x, _y, _z);
124
125 if (normalise) {
126 ret.normalise();
127 }
128
129 return ret;
130}
131
133void Vector3f::read(Stream &stream) {
134 x = stream.read_f32();
135 y = stream.read_f32();
136 z = stream.read_f32();
137}
138
139} // 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:133
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:114
Vector3f maximize(const Vector3f &rhs) const
Returns a vector whose elements are the max of the elements of both vectors.
Definition Vector.cc:83
f32 ps_sqDistance(const Vector3f &rhs) const
Paired-singles impl. of sqDistance.
Definition Vector.cc:107
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:95