vec.h (2976B)
1#include <string.h> 2#include <stdbool.h> 3#include <sys/types.h> 4 5#define VEC_ABS(x) ((x) > 0 ? (x) : -(x)) 6 7struct vec2i { 8 union { 9 struct { 10 ssize_t x, y; 11 }; 12 ssize_t dims[2]; 13 }; 14}; 15 16struct vec3i { 17 union { 18 struct { 19 ssize_t x, y, z; 20 }; 21 ssize_t dims[3]; 22 }; 23}; 24 25struct vec2f { 26 union { 27 struct { 28 double x, y; 29 }; 30 double dims[2]; 31 }; 32}; 33 34struct vec3f { 35 union { 36 struct { 37 double x, y, z; 38 }; 39 double dims[3]; 40 }; 41}; 42 43#define DEFINE_VEC_SET(name, type, dims) \ 44 static inline void name(type *dst, const type *src) { \ 45 memcpy(dst, src, sizeof(type)); \ 46 } 47 48#define DEFINE_VEC_ADD(name, type, n) \ 49 static inline void name(type *dst, const type *a, const type *b) { \ 50 for (int i = 0; i < n; i++) \ 51 dst->dims[i] = a->dims[i] + b->dims[i]; \ 52 } 53 54#define DEFINE_VEC_SUB(name, type, n) \ 55 static inline void name(type *dst, const type *a, const type *b) { \ 56 for (int i = 0; i < n; i++) \ 57 dst->dims[i] = a->dims[i] - b->dims[i]; \ 58 } 59 60#define DEFINE_VEC_MUL(name, type, prim, n) \ 61 static inline void name(type *dst, const type *a, prim b) { \ 62 for (int i = 0; i < n; i++) \ 63 dst->dims[i] = a->dims[i] * b; \ 64 } 65 66#define DEFINE_VEC_EQL(name, type, n) \ 67 static inline bool name(const type *a, const type *b) { \ 68 for (int i = 0; i < n; i++) \ 69 if (a->dims[i] != b->dims[i]) return false; \ 70 return true; \ 71 } 72 73DEFINE_VEC_SET(vec2i_set, struct vec2i, 2); 74DEFINE_VEC_ADD(vec2i_add, struct vec2i, 2); 75DEFINE_VEC_SUB(vec2i_sub, struct vec2i, 2); 76DEFINE_VEC_MUL(vec2i_mul, struct vec2i, ssize_t, 2); 77DEFINE_VEC_EQL(vec2i_eql, struct vec2i, 2); 78 79static inline void 80vec2i_setv(struct vec2i *dst, ssize_t x, ssize_t y) 81{ 82 dst->x = x; 83 dst->y = y; 84} 85 86static inline ssize_t 87vec2i_len(struct vec2i *a) 88{ 89 return VEC_ABS(a->x) + VEC_ABS(a->y); 90} 91 92DEFINE_VEC_SET(vec3i_set, struct vec3i, 3); 93DEFINE_VEC_ADD(vec3i_add, struct vec3i, 3); 94DEFINE_VEC_SUB(vec3i_sub, struct vec3i, 3); 95DEFINE_VEC_MUL(vec3i_mul, struct vec3i, ssize_t, 3); 96DEFINE_VEC_EQL(vec3i_eql, struct vec3i, 3); 97 98static inline void 99vec3i_setv(struct vec3i *dst, ssize_t x, ssize_t y, ssize_t z) 100{ 101 dst->x = x; 102 dst->y = y; 103 dst->z = z; 104} 105 106DEFINE_VEC_SET(vec2f_set, struct vec2f, 2); 107DEFINE_VEC_ADD(vec2f_add, struct vec2f, 2); 108DEFINE_VEC_SUB(vec2f_sub, struct vec2f, 2); 109DEFINE_VEC_MUL(vec2f_mul, struct vec2f, double, 2); 110 111static inline void 112vec2f_setv(struct vec2f *dst, double x, double y) 113{ 114 dst->x = x; 115 dst->y = y; 116} 117 118DEFINE_VEC_SET(vec3f_set, struct vec3f, 3); 119DEFINE_VEC_ADD(vec3f_add, struct vec3f, 3); 120DEFINE_VEC_SUB(vec3f_sub, struct vec3f, 3); 121DEFINE_VEC_MUL(vec3f_mul, struct vec3f, double, 3); 122 123static inline void 124vec3f_setv(struct vec3f *dst, double x, double y, double z) 125{ 126 dst->x = x; 127 dst->y = y; 128 dst->z = z; 129} 130 131static inline void 132vec3i_add_2i(struct vec3i *dst, const struct vec3i *a, const struct vec2i *b) 133{ 134 dst->x = a->x + b->x; 135 dst->y = a->y + b->y; 136 dst->z = a->z; 137} 138 139#undef DEFINE_VEC_SET 140#undef DEFINE_VEC_ADD 141#undef DEFINE_VEC_SUB 142#undef DEFINE_VEC_MUL