summaryrefslogtreecommitdiff
path: root/firmware/os/algos/common/math/vec.h
blob: cb5f08dda66c615975bf647b600e46104c33bff1 (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
#ifndef LOCATION_LBS_CONTEXTHUB_NANOAPPS_COMMON_MATH_VEC_H_
#define LOCATION_LBS_CONTEXTHUB_NANOAPPS_COMMON_MATH_VEC_H_

/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <nanohub_math.h>

#ifdef __cplusplus
extern "C" {
#endif

struct Vec3 {
  float x, y, z;
};

struct Vec4 {
  float x, y, z, w;
};

static inline void initVec3(struct Vec3 *v, float x, float y, float z) {
  v->x = x;
  v->y = y;
  v->z = z;
}

static inline void vec3Add(struct Vec3 *v, const struct Vec3 *w) {
  v->x += w->x;
  v->y += w->y;
  v->z += w->z;
}

static inline void vec3Sub(struct Vec3 *v, const struct Vec3 *w) {
  v->x -= w->x;
  v->y -= w->y;
  v->z -= w->z;
}

static inline void vec3ScalarMul(struct Vec3 *v, float c) {
  v->x *= c;
  v->y *= c;
  v->z *= c;
}

static inline float vec3Dot(const struct Vec3 *v, const struct Vec3 *w) {
  return v->x * w->x + v->y * w->y + v->z * w->z;
}

static inline float vec3NormSquared(const struct Vec3 *v) {
  return vec3Dot(v, v);
}

static inline float vec3Norm(const struct Vec3 *v) {
  return sqrtf(vec3NormSquared(v));
}

static inline void vec3Normalize(struct Vec3 *v) {
  float invNorm = 1.0f / vec3Norm(v);
  v->x *= invNorm;
  v->y *= invNorm;
  v->z *= invNorm;
}

static inline void vec3Cross(struct Vec3 *u, const struct Vec3 *v,
                             const struct Vec3 *w) {
  u->x = v->y * w->z - v->z * w->y;
  u->y = v->z * w->x - v->x * w->z;
  u->z = v->x * w->y - v->y * w->x;
}

static inline void initVec4(struct Vec4 *v, float x, float y, float z,
                            float w) {
  v->x = x;
  v->y = y;
  v->z = z;
  v->w = w;
}

void findOrthogonalVector(float inX, float inY, float inZ, float *outX,
                          float *outY, float *outZ);

#ifdef __cplusplus
}
#endif

#endif  // LOCATION_LBS_CONTEXTHUB_NANOAPPS_COMMON_MATH_VEC_H_