aboutsummaryrefslogtreecommitdiff
path: root/mons_math/src/vec3.c
blob: 725f5b303a95dd79ef929d5b040100b4cdfb7be1 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "mons_math/vec3.h"
#include "mons_math/vec2.h"
#include "mons_math/vec4.h"
#include "mons_math/util.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

mons_vec3 mons_vec3_add(mons_vec3 a, mons_vec3 b) {
    mons_vec3 result = {
        a.x + b.x,
        a.y + b.y,
        a.z + b.z,
    };
    return result;
}

void mons_vec3_add_inplace(mons_vec3 *a, mons_vec3 b) {
    a->x += b.x;
    a->y += b.y;
    a->z += b.z;
}

mons_vec3 mons_vec3_sub(mons_vec3 a, mons_vec3 b) {
    mons_vec3 result = {
        a.x - b.x,
        a.y - b.y,
        a.z - b.z,
    };
    return result;
}

void mons_vec3_sub_inplace(mons_vec3 *a, mons_vec3 b) {
    a->x -= b.x;
    a->y -= b.y;
    a->z -= b.z;
}

mons_vec3 mons_vec3_mul_f(mons_vec3 a, float b) {
    mons_vec3 result = {
        a.x * b,
        a.y * b,
        a.z * b,
    };
    return result;
}

mons_vec3 mons_vec3_mul_i(mons_vec3 a, int b) {
    mons_vec3 result = {
        a.x * b,
        a.y * b,
        a.z * b,
    };
    return result;
}

float mons_vec3_dot(mons_vec3 a, mons_vec3 b) {
    return (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
}

void mons_vec3_mul_f_inplace(mons_vec3 *a, float b) {
    a->x *= b;
    a->y *= b;
    a->z *= b;
}

void mons_vec3_mul_i_inplace(mons_vec3 *a, int b) {
    a->x *= b;
    a->y *= b;
    a->z *= b;
}

float mons_vec3_len(mons_vec3 a) { return sqrtf(mons_vec3_len_squared(a)); }

float mons_vec3_len_squared(mons_vec3 a) {
    return (a.x * a.x) + (a.y * a.y) + (a.z * a.z);
}

mons_vec4 mons_vec3_extend(mons_vec3 a) {
    mons_vec4 result = {
        a.x,
        a.y,
        a.z,
        0.0,
    };
    return result;
}

mons_vec2 mons_vec3_truncate(mons_vec3 a) {
    mons_vec2 result = {
        a.x,
        a.y,
    };
    return result;
}

int mons_vec3_equal(mons_vec3 a, mons_vec3 b) {
    return mons_float_approx_equal(a.x, b.x)
        && mons_float_approx_equal(a.y, b.y)
        && mons_float_approx_equal(a.z, b.z);
}

mons_vec3 mons_vec3_div_f(mons_vec3 a, float b) {
    return (mons_vec3) {
        a.x / b,
        a.y / b,
        a.z / b,
    };
}

void mons_vec3_div_f_inplace(mons_vec3 *a, float b) {
    a->x /= b;
    a->y /= b;
    a->z /= b;
}

mons_vec3 mons_vec3_div_i(mons_vec3 a, int b) {
    return (mons_vec3) {
        a.x / b,
        a.y / b,
        a.z / b,
    };
}

void mons_vec3_div_i_inplace(mons_vec3 *a, int b) {
    a->x /= b;
    a->y /= b;
    a->z /= b;
}

mons_vec3 mons_vec3_negate(mons_vec3 a) {
    return mons_vec3_mul_i(a, -1);
}

void mons_vec3_negate_inplace(mons_vec3 *a) {
    return mons_vec3_mul_i_inplace(a, -1);
}

mons_vec3 mons_vec3_normalize(mons_vec3 a) {
    float length = mons_vec3_len(a);
    return mons_vec3_div_f(a, length);
}

void mons_vec3_normalize_inplace(mons_vec3 *a) {
    float length = mons_vec3_len(*a);
    mons_vec3_div_f_inplace(a, length);
}

mons_vec3 mons_vec3_cross(mons_vec3 a, mons_vec3 b) {
    return (mons_vec3) {
        (a.y * b.z) - (a.z * b.y),
        (a.z * b.x) - (a.x * b.z),
        (a.x * b.y) - (a.y * b.x),
    };
}

mons_vec3 mons_vec3_mul_memberwise(mons_vec3 a, mons_vec3 b) {
    return (mons_vec3) {
        a.x * b.x,
        a.y * b.y,
        a.z * b.z,
    };
}

void mons_vec3_mul_memberwise_inplace(mons_vec3 *a, mons_vec3 b) {
    a->x *= b.x;
    a->y *= b.y;
    a->z *= b.z;
}

void mons_vec3_print(mons_vec3 vec) {
    printf("[%f, %f, %f]", vec.x, vec.y, vec.z);
}