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
|
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#define VEC_ABS(x) ((x) > 0 ? (x) : -(x))
struct vec2i {
union {
struct {
ssize_t x, y;
};
ssize_t dims[2];
};
};
struct vec3i {
union {
struct {
ssize_t x, y, z;
};
ssize_t dims[3];
};
};
struct vec2f {
union {
struct {
double x, y;
};
double dims[2];
};
};
struct vec3f {
union {
struct {
double x, y, z;
};
double dims[3];
};
};
#define DEFINE_VEC_SET(name, type, dims) \
static inline void name(type *dst, const type *src) { \
memcpy(dst, src, sizeof(type)); \
}
#define DEFINE_VEC_ADD(name, type, n) \
static inline void name(type *dst, const type *a, const type *b) { \
for (int i = 0; i < n; i++) \
dst->dims[i] = a->dims[i] + b->dims[i]; \
}
#define DEFINE_VEC_SUB(name, type, n) \
static inline void name(type *dst, const type *a, const type *b) { \
for (int i = 0; i < n; i++) \
dst->dims[i] = a->dims[i] - b->dims[i]; \
}
#define DEFINE_VEC_MUL(name, type, prim, n) \
static inline void name(type *dst, const type *a, prim b) { \
for (int i = 0; i < n; i++) \
dst->dims[i] = a->dims[i] * b; \
}
#define DEFINE_VEC_EQL(name, type, n) \
static inline bool name(const type *a, const type *b) { \
for (int i = 0; i < n; i++) \
if (a->dims[i] != b->dims[i]) return false; \
return true; \
}
DEFINE_VEC_SET(vec2i_set, struct vec2i, 2);
DEFINE_VEC_ADD(vec2i_add, struct vec2i, 2);
DEFINE_VEC_SUB(vec2i_sub, struct vec2i, 2);
DEFINE_VEC_MUL(vec2i_mul, struct vec2i, ssize_t, 2);
DEFINE_VEC_EQL(vec2i_eql, struct vec2i, 2);
static inline void
vec2i_setv(struct vec2i *dst, ssize_t x, ssize_t y)
{
dst->x = x;
dst->y = y;
}
static inline ssize_t
vec2i_len(struct vec2i *a)
{
return VEC_ABS(a->x) + VEC_ABS(a->y);
}
DEFINE_VEC_SET(vec3i_set, struct vec3i, 3);
DEFINE_VEC_ADD(vec3i_add, struct vec3i, 3);
DEFINE_VEC_SUB(vec3i_sub, struct vec3i, 3);
DEFINE_VEC_MUL(vec3i_mul, struct vec3i, ssize_t, 3);
DEFINE_VEC_EQL(vec3i_eql, struct vec3i, 3);
static inline void
vec3i_setv(struct vec3i *dst, ssize_t x, ssize_t y, ssize_t z)
{
dst->x = x;
dst->y = y;
dst->z = z;
}
DEFINE_VEC_SET(vec2f_set, struct vec2f, 2);
DEFINE_VEC_ADD(vec2f_add, struct vec2f, 2);
DEFINE_VEC_SUB(vec2f_sub, struct vec2f, 2);
DEFINE_VEC_MUL(vec2f_mul, struct vec2f, double, 2);
static inline void
vec2f_setv(struct vec2f *dst, double x, double y)
{
dst->x = x;
dst->y = y;
}
DEFINE_VEC_SET(vec3f_set, struct vec3f, 3);
DEFINE_VEC_ADD(vec3f_add, struct vec3f, 3);
DEFINE_VEC_SUB(vec3f_sub, struct vec3f, 3);
DEFINE_VEC_MUL(vec3f_mul, struct vec3f, double, 3);
static inline void
vec3f_setv(struct vec3f *dst, double x, double y, double z)
{
dst->x = x;
dst->y = y;
dst->z = z;
}
static inline void
vec3i_add_2i(struct vec3i *dst, const struct vec3i *a, const struct vec2i *b)
{
dst->x = a->x + b->x;
dst->y = a->y + b->y;
dst->z = a->z;
}
#undef DEFINE_VEC_SET
#undef DEFINE_VEC_ADD
#undef DEFINE_VEC_SUB
#undef DEFINE_VEC_MUL
|