summaryrefslogtreecommitdiff
path: root/common/vec3.c
blob: 4cf26ebf4c1721cdf1a60aa05c431bc91d930ed0 (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
/* Copyright 2015 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "common.h"
#include "math.h"
#include "math_util.h"
#include "util.h"
#include "vec3.h"

static fpv3_t zero_initialized_vector = { FLOAT_TO_FP(0.0f), FLOAT_TO_FP(0.0f),
					  FLOAT_TO_FP(0.0f) };

void fpv3_zero(fpv3_t v)
{
	memcpy(v, zero_initialized_vector, sizeof(fpv3_t));
}

void fpv3_init(fpv3_t v, fp_t x, fp_t y, fp_t z)
{
	v[X] = x;
	v[Y] = y;
	v[Z] = z;
}

void fpv3_scalar_mul(fpv3_t v, fp_t c)
{
	v[X] = fp_mul(v[X], c);
	v[Y] = fp_mul(v[Y], c);
	v[Z] = fp_mul(v[Z], c);
}

void fpv3_sub(fpv3_t out, const fpv3_t a, const fpv3_t b)
{
	out[X] = a[X] - b[X];
	out[Y] = a[Y] - b[Y];
	out[Z] = a[Z] - b[Z];
}

void fpv3_add(fpv3_t out, const fpv3_t a, const fpv3_t b)
{
	out[X] = a[X] + b[X];
	out[Y] = a[Y] + b[Y];
	out[Z] = a[Z] + b[Z];
}

fp_t fpv3_dot(const fpv3_t v, const fpv3_t w)
{
	return fp_mul(v[X], w[X]) + fp_mul(v[Y], w[Y]) + fp_mul(v[Z], w[Z]);
}

fp_t fpv3_norm_squared(const fpv3_t v)
{
	return fpv3_dot(v, v);
}

fp_t fpv3_norm(const fpv3_t v)
{
	return fp_sqrtf(fpv3_norm_squared(v));
}