File: math.h

package info (click to toggle)
nihstro 0.0~git20240505.f4d8659-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704 kB
  • sloc: ansic: 2,920; cpp: 2,889; makefile: 97; asm: 12
file content (144 lines) | stat: -rw-r--r-- 3,076 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef MATH_H
#define MATH_H

#include <3ds/types.h>
#include <math.h>

typedef float mtx44[4][4];
typedef float mtx33[3][3];

typedef struct
{
	s32 x, y, z;
}vect3Di_s;

static inline vect3Di_s vect3Di(s32 x, s32 y, s32 z)
{
	return (vect3Di_s){x,y,z};
}

static inline vect3Di_s vaddi(vect3Di_s u, vect3Di_s v)
{
	return (vect3Di_s){u.x+v.x,u.y+v.y,u.z+v.z};
}

static inline vect3Di_s vsubi(vect3Di_s u, vect3Di_s v)
{
	return (vect3Di_s){u.x-v.x,u.y-v.y,u.z-v.z};
}

static inline vect3Di_s vmuli(vect3Di_s v, s32 f)
{
	return (vect3Di_s){v.x*f,v.y*f,v.z*f};
}

typedef struct
{
	float x, y, z;
}vect3Df_s;

static inline vect3Df_s vect3Df(float x, float y, float z)
{
	return (vect3Df_s){x,y,z};
}

static inline vect3Df_s vaddf(vect3Df_s u, vect3Df_s v)
{
	return (vect3Df_s){u.x+v.x,u.y+v.y,u.z+v.z};
}

static inline vect3Df_s vsubf(vect3Df_s u, vect3Df_s v)
{
	return (vect3Df_s){u.x-v.x,u.y-v.y,u.z-v.z};
}

static inline vect3Df_s vmulf(vect3Df_s v, float f)
{
	return (vect3Df_s){v.x*f,v.y*f,v.z*f};
}

static inline vect3Df_s vscalef(vect3Df_s v1, vect3Df_s v2)
{
	return (vect3Df_s){v1.x*v2.x,v1.y*v2.y,v1.z*v2.z};
}

static inline float vmagf(vect3Df_s v)
{
	return sqrtf(v.x*v.x+v.y*v.y+v.z*v.z);
}

static inline float vdistf(vect3Df_s v1, vect3Df_s v2)
{
	return sqrtf((v1.x-v2.x)*(v1.x-v2.x)+(v1.y-v2.y)*(v1.y-v2.y)+(v1.z-v2.z)*(v1.z-v2.z));
}

static inline vect3Df_s vnormf(vect3Df_s v)
{
	const float l=sqrtf(v.x*v.x+v.y*v.y+v.z*v.z);
	return (vect3Df_s){v.x/l,v.y/l,v.z/l};
}

typedef struct
{
	float x, y, z, w;
}vect4Df_s;

static inline vect4Df_s vect4Df(float x, float y, float z, float w)
{
	return (vect4Df_s){x,y,z,w};
}

static inline vect4Df_s vaddf4(vect4Df_s u, vect4Df_s v)
{
	return (vect4Df_s){u.x+v.x,u.y+v.y,u.z+v.z,u.w+v.w};
}

static inline vect4Df_s vsubf4(vect4Df_s u, vect4Df_s v)
{
	return (vect4Df_s){u.x-v.x,u.y-v.y,u.z-v.z,u.w-v.w};
}

static inline vect4Df_s vmulf4(vect4Df_s v, float f)
{
	return (vect4Df_s){v.x*f,v.y*f,v.z*f,v.w*f};
}

static inline float vdotf4(vect4Df_s v1, vect4Df_s v2)
{
	return v1.x*v2.x+v1.y*v2.y+v1.z*v2.z+v1.w*v2.w;
}

static inline vect4Df_s vnormf4(vect4Df_s v)
{
	const float l=sqrtf(v.x*v.x+v.y*v.y+v.z*v.z+v.w*v.w);
	return (vect4Df_s){v.x/l,v.y/l,v.z/l,v.w/l};
}

//interstuff
static inline vect3Di_s vf2i(vect3Df_s v)
{
	return (vect3Di_s){floorf(v.x),floorf(v.y),floorf(v.z)};
}

static inline vect3Df_s vi2f(vect3Di_s v)
{
	return (vect3Df_s){(float)v.x,(float)v.y,(float)v.z};
}

void loadIdentity44(float* m);
void multMatrix44(float* m1, float* m2, float* m);

void translateMatrix(float* tm, float x, float y, float z);
void rotateMatrixX(float* tm, float x, bool r);
void rotateMatrixY(float* tm, float x, bool r);
void rotateMatrixZ(float* tm, float x, bool r);
void scaleMatrix(float* tm, float x, float y, float z);

void initProjectionMatrix(float* m, float fovy, float aspect, float near, float far);

vect3Df_s getMatrixColumn(float* m, u8 i);
vect3Df_s getMatrixRow(float* m, u8 i);
vect4Df_s getMatrixColumn4(float* m, u8 i);
vect4Df_s getMatrixRow4(float* m, u8 i);

#endif