KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
vec3f.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 dc/vec3f.h
4 Copyright (C) 2013, 2014 Josh "PH3NOM" Pearson
5
6*/
7
8/** \file dc/vec3f.h
9 \brief Basic matrix operations.
10 \ingroup math_matrices
11
12 This file contains various basic vector math functionality for using the
13 SH4's vector instructions. Higher level functionality in KGL is built off
14 of these.
15
16 \author Josh "PH3NOM" Pearson
17 \see dc/matrix.h
18*/
19
20#ifndef __DC_VEC3F_H
21#define __DC_VEC3F_H
22
23#include <sys/cdefs.h>
24__BEGIN_DECLS
25
26#include <math.h>
27
28#include <dc/fmath.h>
29
30/** \addtogroup math_matrices
31 @{
32*/
33
34/** \brief 3D floating-point vector */
35typedef struct vec3f {
36 float x, y, z;
37} vec3f_t;
38
39/** \cond */
40#define R_DEG 182.04444443623349541909523793743f
41#define R_RAD 10430.37835f
42/* \endcond */
43
44static inline float vec_fipr(vec3f_t vec) {
45 return fipr_magnitude_sqr(vec.x, vec.y, vec.z, 0.0f);
46}
47
48/** \brief Function to return the scalar dot product of two 3d vectors.
49
50 This macro is an inline assembly operation using the SH4's fast
51 (approximate) math instructions, and returns a single-precision
52 floating-point value.
53
54 \param vec1 The first vector.
55 \param vec2 The second vector.
56 \return The result of the calculation.
57*/
58static inline float vec_dot(vec3f_t vec1, vec3f_t vec2) {
59 return fipr(vec1.x, vec1.y, vec1.z, 0.0f,
60 vec2.x, vec2.y, vec2.z, 0.0f);
61}
62
63/** \brief Macro to return scalar Euclidean length of a 3d vector.
64
65 This macro is an inline assembly operation using the SH4's fast
66 (approximate) math instructions, and returns a single-precision
67 floating-point value.
68
69 \param vec The vector.
70 \return The result of the calculation.
71*/
72static inline float vec_length(vec3f_t vec) {
73 return sqrtf(vec_fipr(vec));
74}
75
76/** \brief Function to return the Euclidean distance between two 3d vectors.
77
78 This macro is an inline assembly operation using the SH4's fast
79 (approximate) math instructions, and returns a single-precision
80 floating-point value.
81
82 \param vec1 The first vector.
83 \param vec2 The second vector.
84 \return The result of the calculation.
85*/
86static inline float vec_distance(vec3f_t vec1, vec3f_t vec2) {
87 vec3f_t vec = (vec3f_t){ vec2.x - vec1.x, vec2.y - vec1.y, vec2.z - vec1.z };
88 return vec_length(vec);
89}
90
91/** \brief Function to return the normalized version of a vector.
92
93 This macro is an inline assembly operation using the SH4's fast
94 (approximate) math instructions to calculate a vector that is in the same
95 direction as the input vector but with a Euclidean length of one.
96
97 \param vec The source vector.
98 \return The normalized vector.
99*/
100static inline vec3f_t vec_normalize(vec3f_t vec) {
101 float factor = 1.0f / vec_length(vec);
102 return (vec3f_t){ vec.x * factor, vec.y * factor, vec.z * factor };
103}
104
105/** \brief Function to return the normalized version of a vector minus another
106 vector.
107
108 This macro is an inline assembly operation using the SH4's fast
109 (approximate) math instructions.
110
111 \param vec1 The first vector.
112 \param vec2 The second vector.
113 \return The normalized vector.
114*/
115static inline vec3f_t vec_sub_normalize(vec3f_t vec1, vec3f_t vec2) {
116 vec3f_t vec = (vec3f_t){ vec1.x - vec2.x, vec1.y - vec2.y, vec1.z - vec2.z };
117
118 return vec_normalize(vec);
119}
120
121/** \brief Macro to rotate a vector about its origin on the x, y plane.
122
123 This macro is an inline assembly operation using the SH4's fast
124 (approximate) math instructions.
125
126 \param vec The source vector.
127 \param origin The origin vector.
128 \param angle The angle (in radians) of rotation.
129*/
130static inline vec3f_t vec_rotr_xy(vec3f_t vec, vec3f_t origin, float angle) {
131 float c = cosf(angle);
132 float s = sinf(angle);
133
134 return (vec3f_t) {
135 origin.x + (vec.x - origin.x) * c - (vec.y - origin.y) * s,
136 origin.y + (vec.x - origin.x) * s + (vec.y - origin.y) * c,
137 vec.z,
138 };
139}
140
141/** \brief Macro to rotate a vector about its origin on the x, z plane.
142
143 This macro is an inline assembly operation using the SH4's fast
144 (approximate) math instructions.
145
146 \param vec The source vector.
147 \param origin The origin vector.
148 \param angle The angle (in radians) of rotation.
149*/
150static inline vec3f_t vec_rotr_xz(vec3f_t vec, vec3f_t origin, float angle) {
151 float c = cosf(angle);
152 float s = sinf(angle);
153
154 return (vec3f_t) {
155 origin.x + (vec.x - origin.x) * c - (vec.z - origin.z) * s,
156 vec.y,
157 origin.z + (vec.x - origin.x) * s + (vec.z - origin.z) * c,
158 };
159}
160
161/** \brief Macro to rotate a vector about its origin on the y, z plane.
162
163 This macro is an inline assembly operation using the SH4's fast
164 (approximate) math instructions.
165
166 \param vec The source vector.
167 \param origin The origin vector.
168 \param angle The angle (in radians) of rotation.
169*/
170static inline vec3f_t vec_rotr_yz(vec3f_t vec, vec3f_t origin, float angle) {
171 float c = cosf(angle);
172 float s = sinf(angle);
173
174 return (vec3f_t) {
175 vec.x,
176 origin.y + (vec.y - origin.y) * c + (vec.z - origin.z) * s,
177 origin.z + (vec.y - origin.y) * s + (vec.z - origin.z) * c,
178 };
179}
180
181/** \brief Macro to rotate a vector about its origin on the x, y plane.
182
183 This macro is an inline assembly operation using the SH4's fast
184 (approximate) math instructions.
185
186 \param vec The source vector.
187 \param origin The origin vector.
188 \param angle The angle (in degrees) of rotation.
189*/
190static inline vec3f_t vec_rotd_xy(vec3f_t vec, vec3f_t origin, float angle) {
191 return vec_rotr_xy(vec, origin, angle * R_DEG / R_RAD);
192}
193
194/** \brief Macro to rotate a vector about its origin on the x, z plane.
195
196 This macro is an inline assembly operation using the SH4's fast
197 (approximate) math instructions.
198
199 \param vec The source vector.
200 \param origin The origin vector.
201 \param angle The angle (in degrees) of rotation.
202*/
203static inline vec3f_t vec_rotd_xz(vec3f_t vec, vec3f_t origin, float angle) {
204 return vec_rotr_xz(vec, origin, angle * R_DEG / R_RAD);
205}
206
207/** \brief Macro to rotate a vector about its origin on the y, z plane.
208
209 This macro is an inline assembly operation using the SH4's fast
210 (approximate) math instructions.
211
212 \param vec The source vector.
213 \param origin The origin vector.
214 \param angle The angle (in degrees) of rotation.
215*/
216static inline vec3f_t vec_rotd_yz(vec3f_t vec, vec3f_t origin, float angle) {
217 return vec_rotr_yz(vec, origin, angle * R_DEG / R_RAD);
218}
219
220/** \cond */
221/* Compatibility macros */
222#define vec3f_dot(x1, y1, z1, x2, y2, z2, w) \
223 w = vec_dot((vec3f_t){ x1, y1, z1 }, (vec3f_t){ x2, y2, z2 })
224
225#define vec3f_length(x, y, z, w) \
226 w = vec_length((vec3f_t){ x, y, z })
227
228#define vec3f_distance(x1, y1, z1, x2, y2, z2, w) \
229 w = vec_distance((vec3f_t){ x1, y1, z1 }, (vec3f_t){ x2, y2, z2 })
230
231#define vec3f_normalize(__x, __y, __z) { \
232 vec3f_t vec = vec_normalize((vec3f_t){ __x, __y, __z }); \
233 __x = vec.x; __y = vec.y; __z = vec.z; \
234}
235
236#define vec3f_sub_normalize(x1, y1, z1, x2, y2, z2, x3, y3, z3) { \
237 vec3f_t vec = vec_sub_normalize((vec3f_t){ x1, y1, z1 }, (vec3f_t){ x2, y2, z2 }); \
238 x3 = vec.x; y3 = vec.y; z3 = vec.z; \
239}
240
241#define vec3f_rotr_xy(px, py, pz, cx, cy, cz, r) { \
242 vec3f_t vec = vec_rotr_xy((vec3f_t){ px, py, pz }, (vec3f_t){ cx, cy, cz }, r); \
243 px = vec.x; py = vec.y; pz = vec.z; \
244}
245
246#define vec3f_rotr_xz(px, py, pz, cx, cy, cz, r) { \
247 vec3f_t vec = vec_rotr_xz((vec3f_t){ px, py, pz }, (vec3f_t){ cx, cy, cz }, r); \
248 px = vec.x; py = vec.y; pz = vec.z; \
249}
250
251#define vec3f_rotr_yz(px, py, pz, cx, cy, cz, r) { \
252 vec3f_t vec = vec_rotr_yz((vec3f_t){ px, py, pz }, (vec3f_t){ cx, cy, cz }, r); \
253 px = vec.x; py = vec.y; pz = vec.z; \
254}
255
256#define vec3f_rotd_xy(px, py, pz, cx, cy, cz, r) \
257 vec3f_rotr_xy(px, py, pz, cx, cy, cz, (r) * R_DEG / R_RAD)
258
259#define vec3f_rotd_xz(px, py, pz, cx, cy, cz, r) \
260 vec3f_rotr_xz(px, py, pz, cx, cy, cz, (r) * R_DEG / R_RAD)
261
262#define vec3f_rotd_yz(px, py, pz, cx, cy, cz, r) \
263 vec3f_rotr_yz(px, py, pz, cx, cy, cz, (r) * R_DEG / R_RAD)
264/* \endcond */
265
266/** @} */
267
268__END_DECLS
269
270#endif /* !__DC_VEC3F_H */
Inline functions for the DC's special math instructions.
__FMINLINE float fipr_magnitude_sqr(float x, float y, float z, float w)
Floating point inner product w/self (square of vector magnitude)
Definition fmath.h:58
__FMINLINE float fipr(float x, float y, float z, float w, float a, float b, float c, float d)
Floating point inner product.
Definition fmath.h:49
static float vec_fipr(vec3f_t vec)
Definition vec3f.h:44
static float vec_length(vec3f_t vec)
Macro to return scalar Euclidean length of a 3d vector.
Definition vec3f.h:72
static vec3f_t vec_rotd_xz(vec3f_t vec, vec3f_t origin, float angle)
Macro to rotate a vector about its origin on the x, z plane.
Definition vec3f.h:203
static vec3f_t vec_rotr_yz(vec3f_t vec, vec3f_t origin, float angle)
Macro to rotate a vector about its origin on the y, z plane.
Definition vec3f.h:170
static vec3f_t vec_rotr_xy(vec3f_t vec, vec3f_t origin, float angle)
Macro to rotate a vector about its origin on the x, y plane.
Definition vec3f.h:130
static float vec_dot(vec3f_t vec1, vec3f_t vec2)
Function to return the scalar dot product of two 3d vectors.
Definition vec3f.h:58
static vec3f_t vec_normalize(vec3f_t vec)
Function to return the normalized version of a vector.
Definition vec3f.h:100
static vec3f_t vec_sub_normalize(vec3f_t vec1, vec3f_t vec2)
Function to return the normalized version of a vector minus another vector.
Definition vec3f.h:115
static vec3f_t vec_rotd_xy(vec3f_t vec, vec3f_t origin, float angle)
Macro to rotate a vector about its origin on the x, y plane.
Definition vec3f.h:190
static float vec_distance(vec3f_t vec1, vec3f_t vec2)
Function to return the Euclidean distance between two 3d vectors.
Definition vec3f.h:86
static vec3f_t vec_rotr_xz(vec3f_t vec, vec3f_t origin, float angle)
Macro to rotate a vector about its origin on the x, z plane.
Definition vec3f.h:150
static vec3f_t vec_rotd_yz(vec3f_t vec, vec3f_t origin, float angle)
Macro to rotate a vector about its origin on the y, z plane.
Definition vec3f.h:216
Prototypes for optimized math functions written in ASM.
3D floating-point vector
Definition vec3f.h:35
float y
Definition vec3f.h:36
float x
Definition vec3f.h:36
float z
Definition vec3f.h:36