SMG-Decomp
A decompilation of Super Mario Galaxy 1
Loading...
Searching...
No Matches
MathUtil.hpp
1#pragma once
2
3#include <cmath>
4#include <revolution.h>
5#include "JSystem/JGeometry.hpp"
6
7static f32 minDegree = 0.0f;
8static f32 maxDegree = 360.0f;
9
10static f32 flt_8060FC80[1816];
11
12namespace MR {
13
14 void initAcosTable();
15
16 template<typename T>
17 T sqrt(T val);
18
19 f32 getRandom();
20 f32 getRandom(f32, f32);
21 s32 getRandom(s32, s32);
22 f32 getRandomDegree();
23 void calcRandomVec(TVec3f *, f32, f32);
24 u8 isHalfProbability();
25 f32 getSignHalfProbability();
26 void getRandomVector(TVec3f *, f32);
27 void addRandomVector(TVec3f *, const TVec3f &, f32);
28 void turnRandomVector(TVec3f *, const TVec3f &, f32);
29 f32 getInterpolateValue(f32, f32, f32);
30 f32 getLinerValue(f32, f32, f32, f32);
31 f32 getLinerValueFromMinMax(f32, f32, f32, f32, f32);
32 f32 getEaseInValue(f32, f32, f32, f32);
33 f32 getEaseOutValue(f32, f32, f32, f32);
34 f32 getEaseInOutValue(f32, f32, f32, f32);
35 f32 getScaleWithReactionValueZeroToOne(f32, f32, f32);
36 // MR::getConvergeVibrationValue
37 // MR::getReduceVibrationValue
38 void separateScalarAndDirection(f32 *pScalar, TVec3f *pDirection, const TVec3f &rSrc);
39
40 void makeAxisFrontUp(TVec3f *, TVec3f *, const TVec3f &, const TVec3f &);
41 void makeAxisFrontSide(TVec3f *, TVec3f *, const TVec3f &, const TVec3f &);
42 void makeAxisUpFront(TVec3f *, TVec3f *, const TVec3f &, const TVec3f &);
43 void makeAxisUpSide(TVec3f *, TVec3f *, const TVec3f &, const TVec3f &);
44 void makeAxisVerticalZX(TVec3f *, const TVec3f &);
45 void makeAxisCrossPlane(TVec3f *, TVec3f *, const TVec3f &);
46 bool makeAxisAndCosignVecToVec(TVec3f *, f32 *, const TVec3f &, const TVec3f &);
47 f32 calcPerpendicFootToLine(TVec3f *, const TVec3f &, const TVec3f &, const TVec3f &);
48 f32 calcPerpendicFootToLineInside(TVec3f *, const TVec3f &, const TVec3f &, const TVec3f &);
49
50 void blendQuatUpFront(TQuat4f *, const TVec3f &, const TVec3f &, float, float);
51
52 void clampLength(TVec3f *, const TVec3f &, f32);
53 f32 convergeRadian(f32, f32, f32);
54 bool isInRange(f32, f32, f32);
55
56 f32 calcRotateY(f32, f32);
57
58 f32 calcDistanceXY(const TVec3f &, const TVec3f &);
59
60 void rotateVecDegree(TVec3f *, const TVec3f &, f32);
61
62 bool isNearZero(f32, f32);
63 bool isNearZero(const TVec3f &, f32);
64
65 bool isSameDirection(const TVec3f &, const TVec3f &, float);
66 bool isOppositeDirection(const TVec3f &, const TVec3f &, f32);
67
68 f32 diffAngleAbs(f32, f32);
69 f32 diffAngleAbs(const TVec3f &, const TVec3f &);
70 f32 diffAngleAbsHorizontal(const TVec3f &, const TVec3f &, const TVec3f &);
71 f32 diffAngleSignedHorizontal(const TVec3f &, const TVec3f &, const TVec3f &);
72 f32 normalizeAngleAbs(f32);
73 bool isAngleBetween(f32, f32, f32);
74 f32 blendAngle(f32, f32, f32);
75 u8 lerp(u8, u8, f32);
76 _GXColor lerp(_GXColor, _GXColor, f32);
77
78 // MR::sortSmall(long, float *, long *)
79 // MR::sortSmall(long, unsigned long *, long *)
80
81 f32 vecKillElement(const TVec3f &, const TVec3f &, TVec3f *);
82 void vecScaleAdd(const TVec3f *, const TVec3f *, f32);
83 void PSvecBlend(const TVec3f *, const TVec3f *, TVec3f *, f32, f32);
84 void vecBlend(const TVec3f &, const TVec3f &, TVec3f *, f32);
85
86 s32 vecBlendSphere(const TVec3f &, const TVec3f &, TVec3f *, f32);
87
88 void blendColor(_GXColor *, const _GXColor &, const _GXColor &, f32);
89 void blendVec(Vec *, const Vec &, const Vec &, f32);
90
91 int getMinAbsElementIndex(const TVec3f &);
92 f32 getMaxElement(const TVec3f &);
93 f32 getMaxAbsElement(const TVec3f &);
94 int getMaxElementIndex(const TVec3f &);
95 int getMaxAbsElementIndex(const TVec3f &);
96 f32 diffAngleAbsFast(const TVec3f &, const TVec3f &);
97
98 bool isNearAngleRadian(const TVec3f &, const TVec3f &, float);
99 bool isNearAngleDegree(const TVec3f &, const TVec3f &, float);
100 bool isNearAngleRadianHV(const TVec3f &, const TVec3f &, const TVec3f &, float, float);
101 bool isNearAngleDegreeHV(const TVec3f &, const TVec3f &, const TVec3f &, float, float);
102
103 f32 mod(f32, f32);
104
105 void normalize(TVec2f *);
106 void normalize(TVec3f *);
107 void normalize(const TVec3f &, TVec3f *);
108
109 f32 normalize(f32, f32, f32);
110
111 bool normalizeOrZero(TVec2f *);
112 bool normalizeOrZero(TVec3f *);
113 bool normalizeOrZero(const TVec3f &, TVec3f *);
114
115 bool isNan(const TVec3f &);
116
117 template<typename T>
118 T converge(T, T, T);
119
120 f32 clamp(f32, f32, f32);
121
122 f32 getScaleWithReactionValueZeroToOne(f32, f32, f32);
123
124 void calcReboundVelocity(TVec3f *, const TVec3f &, f32, f32);
125
126 void getRotatedAxisZ(TVec3f *, const TVec3f &);
127 void getRotatedAxisY(TVec3f *, const TVec3f &);
128
129 void turnVecToVecCosOnPlane(TVec3f *, const TVec3f &, const TVec3f &, f32);
130
131 void turnVecToPlane(TVec3f *, const TVec3f &, const TVec3f &);
132
133 f32 cosDegree(f32);
134 f32 sinDegree(f32);
135
136 // this must not be declared as inline. some callers inline it and some do not
137 static f32 max(f32 x, f32 y) {
138 return x >= y ? x : y;
139 }
140
141 /* there's a couple of issues with stack ordering when it comes to vectors being created and scaled
142 * this function automates this and resolves most issues
143 */
144 inline TVec3f createVecAndScale(const TVec3f &rSrc, f32 scalar) {
145 TVec3f vec(rSrc);
146 vec.scale(scalar);
147 return vec;
148 }
149
150 inline TVec3f createNegatedVecAndScale(const TVec3f &rSrc, f32 scalar) {
151 return rSrc.negateInline() % scalar;
152 //vec.scale(scalar);
153 //return vec;
154 }
155
156 inline TVec3f createVecAndScaleByAndAdd(const TVec3f &rSrc, const TVec3f &rOtherVec) {
157 TVec3f vec(rSrc);
158 vec.scaleInline(120.0f);
159
160 TVec3f other_vec(rOtherVec);
161 other_vec.addInline3(vec);
162 return other_vec;
163 }
164
165 inline TVec3f createVecAndScaleByAndAdd_2(const TVec3f &rSrc, const TVec3f &rOtherVec) {
166 TVec3f vec(rSrc);
167 vec.scaleInline(120.0f);
168
169 TVec3f other_vec(rOtherVec);
170 other_vec.addInline_5(vec);
171 return other_vec;
172 }
173
174 inline TVec3f negateVector(TVec3f &rVec) {
175 return -rVec;
176 }
177
178 inline void createVecScaleAndApply(const TVec3f &rSrc, TVec3f &rDest, f32 scalar) {
179 rDest += createVecAndScale(rSrc, scalar);
180 }
181
182 inline TVec3f addTwoScalarVecs(const TVec3f &rSrcVector_One, const TVec3f &rSrcVector_Two, f32 scalar_one, f32 scalar_two) {
183 TVec3f vec_one(rSrcVector_One);
184 vec_one.scale(scalar_one);
185 f32 scale_2 = scalar_two;
186 TVec3f vec_two(rSrcVector_Two);
187 vec_two.scale(scale_2);
188 TVec3f res(vec_two);
189 res += vec_one;
190 return res;
191 }
192
193 inline TVec3f multVec(const TVec3f &rSrc, f32 mult, f32 mult_2) {
194 TVec3f vec_one(rSrc);
195 vec_one *= mult;
196 TVec3f vec_two(vec_one);
197 vec_two *= mult_2;
198 return vec_two;
199 }
200
201 inline TVec3f multVec(const TVec3f &rSrc, f32 mult) {
202 TVec3f vec(rSrc);
203 vec *= mult;
204 return vec;
205 }
206
207 inline TVec3f multAndAddVec(const TVec3f &rSrc, const TVec3f &rSrc_2, f32 mult, f32 mult_2) {
208 TVec3f vec_one(rSrc);
209 vec_one *= mult;
210 TVec3f vec_two(rSrc_2);
211 vec_two *= mult_2;
212 TVec3f ret(vec_two);
213 ret += vec_one;
214 return ret;
215 }
216
217 inline TVec3f multVecNoCtor(const TVec3f& rSrc, f32 mult) {
218 return rSrc * mult;
219 }
220
221 inline TVec3f addVec(const TVec3f& rSrc, const TVec3f& rBase) {
222 TVec3f hurr(rBase);
223 hurr.addInline6(rSrc);
224 return hurr;
225 }
226
227 inline f32 subtractFromSum(f32 lhs, f32 rhs, f32 sub) {
228 return (rhs + lhs) - sub;
229 }
230
231 inline f32 divideFromDiff(f32 lhs, f32 rhs, f32 div) {
232 return (lhs - rhs) / div;
233 }
234
235 inline f32 modAndAdd(f32 a1, f32 a2) {
236 return a1 + (f32)fmod(360.0f + a2, 360.0f);
237 }
238
239 inline f32 modAndAdd(f32 a1, f32 a2, f32 a3) {
240 return a1 + (f32)fmod(a3 + a2, a3);
241 }
242
243 inline f32 modAndSubtract(f32 a1, f32 a3, f32 a4) {
244 f32 mod = fmod((a3 + (a1 - a4)), a3);
245 return a4 + mod;
246 }
247
248 inline f32 add(f32 lhs, f32 rhs) {
249 return lhs + rhs;
250 }
251
252 inline f32 negFloat(f32 val) {
253 return (val < 0.0f) ? -val : val;
254 }
255
256 inline s32 mod(s32 val, s32 mod) {
257 return val % mod;
258 }
259
260 f32 repeat(f32 a1, f32 a2, f32 a3) {
261 f64 mod = fmod(a3 + (a1 - a2), a3);
262 return mod + a2;
263 }
264
265 inline void subtractAndSet(TVec3f &a1, const TVec3f &a2, const TVec3f *a3) {
266 a1.set(a2 - *a3);
267 }
268
269 inline void multAndSet(TVec3f *a1, const TVec3f &a2, f32 a3) {
270 a1->set<f32>(a2 * a3);
271 }
272
273 inline int getRemainder(int ra, int rb) {
274 int quotient = ra / rb;
275 quotient *= rb;
276 int remainder = ra - quotient;
277 return remainder;
278 }
279
280 f32 cos(f32);
281 f32 sin(f32);
282};
283
284f32 PSVECKillElement(const Vec *, const Vec *, const Vec *);