Hash :
6229f456
Author :
Thomas de Grivel
Date :
2023-12-27T22:05:56
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
/* c3
* Copyright 2022,2023 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <math.h>
#include <libc3/c3.h>
#include "gl_point_3d.h"
s_gl_point_3d * gl_point_3d_init (s_gl_point_3d *p, f64 x, f64 y, f64 z)
{
assert(p);
p->x = x;
p->y = y;
p->z = z;
return p;
}
s_gl_point_3d * gl_point_3d_init_copy (s_gl_point_3d *p,
const s_gl_point_3d *src)
{
assert(p);
assert(src);
p->x = src->x;
p->y = src->y;
p->z = src->z;
return p;
}
s_gl_point_3d * gl_point_3d_init_normalize (s_gl_point_3d *p,
const s_gl_point_3d *src)
{
f64 r;
assert(p);
assert(src);
r = 1.0 / gl_point_3d_norm(src);
p->x = src->x * r;
p->y = src->y * r;
p->z = src->z * r;
return p;
}
s_gl_point_3d * gl_point_3d_init_product (s_gl_point_3d *p,
const s_gl_matrix_4d *m,
const s_gl_point_3d *s)
{
assert(p);
assert(m);
assert(s);
p->x = s->x * m->xx + s->y * m->xy + s->z * m->xz + m->xt;
p->y = s->x * m->yx + s->y * m->yy + s->z * m->yz + m->yt;
p->z = s->x * m->zx + s->y * m->zy + s->z * m->zz + m->zt;
return p;
}
s_gl_point_3d * gl_point_3d_init_zero (s_gl_point_3d *p)
{
assert(p);
p->x = 0.0;
p->y = 0.0;
p->z = 0.0;
return p;
}
void gl_point_3d_delete (s_gl_point_3d *p)
{
free(p);
}
s_gl_point_3d * gl_point_3d_new (f64 x, f64 y, f64 z)
{
s_gl_point_3d *p;
p = calloc(1, sizeof(s_gl_point_3d));
if (! p) {
err_puts("gl_point_3d_new: failed to allocate memory");
return NULL;
}
gl_point_3d_init(p, x, y, z);
return p;
}
s_gl_point_3d * gl_point_3d_new_copy (const s_gl_point_3d *src)
{
s_gl_point_3d *p;
p = calloc(1, sizeof(s_gl_point_3d));
if (! p) {
err_puts("gl_point_3d_new: failed to allocate memory");
return NULL;
}
gl_point_3d_init_copy(p, src);
return p;
}
s_gl_point_3d * gl_point_3d_new_product (const s_gl_matrix_4d *m,
const s_gl_point_3d *s)
{
s_gl_point_3d *p;
assert(m);
assert(s);
p = calloc(1, sizeof(s_gl_point_3d));
if (! p) {
err_puts("gl_point_3d_new: failed to allocate memory");
return NULL;
}
gl_point_3d_init_product(p, m, s);
return p;
}
s_gl_point_3d * gl_point_3d_new_zero (void)
{
s_gl_point_3d *p;
p = calloc(1, sizeof(s_gl_point_3d));
if (! p) {
err_puts("gl_point_3d_new: failed to allocate memory");
return NULL;
}
gl_point_3d_init_zero(p);
return p;
}
f64 gl_point_3d_norm (const s_gl_point_3d *p)
{
assert(p);
return sqrt(p->x * p->x + p->y * p->y + p->z * p->z);
}
void gl_point_3d_normalize (s_gl_point_3d *p)
{
f64 inv_norm;
assert(p);
inv_norm = 1.0 / gl_point_3d_norm(p);
p->x *= inv_norm;
p->y *= inv_norm;
p->z *= inv_norm;
}
void gl_point_3d_transform (s_gl_point_3d *p,
const s_gl_matrix_4d *matrix)
{
s_gl_point_3d tmp;
assert(p);
assert(matrix);
gl_point_3d_init_product(&tmp, matrix, p);
*p = tmp;
}