summaryrefslogtreecommitdiff
path: root/src/modules/ray/ray_3f.h
blob: 8e852faa045b5616cfb0ac4cac02bc00b93bc36e (plain)
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
145
146
147
148
149
150
151
152
153
154
#ifndef _RAY_3F_H
#define _RAY_3F_H

#include <math.h>

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


/* return the result of (a + b) */
static inline ray_3f_t ray_3f_add(ray_3f_t *a, ray_3f_t *b)
{
	ray_3f_t	res = {
				.x = a->x + b->x,
				.y = a->y + b->y,
				.z = a->z + b->z,
			};

	return res;
}


/* return the result of (a - b) */
static inline ray_3f_t ray_3f_sub(ray_3f_t *a, ray_3f_t *b)
{
	ray_3f_t	res = {
				.x = a->x - b->x,
				.y = a->y - b->y,
				.z = a->z - b->z,
			};

	return res;
}


/* return the result of (-v) */
static inline ray_3f_t ray_3f_negate(ray_3f_t *v)
{
	ray_3f_t	res = {
				.x = -v->x,
				.y = -v->y,
				.z = -v->z,
			};

	return res;
}


/* return the result of (a * b) */
static inline ray_3f_t ray_3f_mult(ray_3f_t *a, ray_3f_t *b)
{
	ray_3f_t	res = {
				.x = a->x * b->x,
				.y = a->y * b->y,
				.z = a->z * b->z,
			};

	return res;
}


/* return the result of (v * scalar) */
static inline ray_3f_t ray_3f_mult_scalar(ray_3f_t *v, float scalar)
{
	ray_3f_t	res = {
				.x = v->x * scalar,
				.y = v->y * scalar,
				.z = v->z * scalar,
			};

	return res;
}


/* return the result of (uv / scalar) */
static inline ray_3f_t ray_3f_div_scalar(ray_3f_t *v, float scalar)
{
	ray_3f_t	res = {
				.x = v->x / scalar,
				.y = v->y / scalar,
				.z = v->z / scalar,
			};

	return res;
}


/* return the result of (a . b) */
static inline float ray_3f_dot(ray_3f_t *a, ray_3f_t *b)
{
	return a->x * b->x + a->y * b->y + a->z * b->z;
}


/* return the length of the supplied vector */
static inline float ray_3f_length(ray_3f_t *v)
{
	return sqrtf(ray_3f_dot(v, v));
}


/* return the normalized form of the supplied vector */
static inline ray_3f_t ray_3f_normalize(ray_3f_t *v)
{
	return ray_3f_mult_scalar(v, 1.0f / ray_3f_length(v));
}


/* return the distance between two arbitrary points */
static inline float ray_3f_distance(ray_3f_t *a, ray_3f_t *b)
{
	ray_3f_t	delta = ray_3f_sub(a, b);

	return ray_3f_length(&delta);
}


/* return the cross product of two unit vectors */
static inline ray_3f_t ray_3f_cross(ray_3f_t *a, ray_3f_t *b)
{
	ray_3f_t	product;

	product.x = a->y * b->z - a->z * b->y;
	product.y = a->z * b->x - a->x * b->z;
	product.z = a->x * b->y - a->y * b->x;

	return product;
}


/* return the linearly interpolated vector between the two vectors at point alpha (0-1.0) */
static inline ray_3f_t ray_3f_lerp(ray_3f_t *a, ray_3f_t *b, float alpha)
{
	ray_3f_t	lerp_a, lerp_b;

	lerp_a = ray_3f_mult_scalar(a, 1.0f - alpha);
	lerp_b = ray_3f_mult_scalar(b, alpha);

	return ray_3f_add(&lerp_a, &lerp_b);
}


/* return the normalized linearly interpolated vector between the two vectors at point alpha (0-1.0) */
static inline ray_3f_t ray_3f_nlerp(ray_3f_t *a, ray_3f_t *b, float alpha)
{
	ray_3f_t	lerp;

	lerp = ray_3f_lerp(a, b, alpha);

	return ray_3f_normalize(&lerp);
}

#endif
© All Rights Reserved