1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 ///////////////////////////////////////////////////////////////////////// 18 /* 19 * This module contains vector math utilities for the following datatypes: 20 * -) Vec3 structures for 3-dimensional vectors 21 * -) Vec4 structures for 4-dimensional vectors 22 * -) floating point arrays for N-dimensional vectors. 23 * 24 * Note that the Vec3 and Vec4 utilties were ported from the Android 25 * repository and maintain dependenices in that separate codebase. As a 26 * result, the function signatures were left untouched for compatibility with 27 * this legacy code, despite certain style violations. In particular, for this 28 * module the function argument ordering is outputs before inputs. This style 29 * violation will be addressed once the full set of dependencies in Android 30 * have been brought into this repository. 31 */ 32 #ifndef LOCATION_LBS_CONTEXTHUB_NANOAPPS_COMMON_MATH_VEC_H_ 33 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_COMMON_MATH_VEC_H_ 34 35 #ifdef NANOHUB_NON_CHRE_API 36 #include <nanohub_math.h> 37 #else 38 #include <math.h> 39 #endif // NANOHUB_NON_CHRE_API 40 41 #include <stddef.h> 42 #include "util/nano_assert.h" 43 44 #ifdef __cplusplus 45 extern "C" { 46 #endif 47 48 struct Vec3 { 49 float x, y, z; 50 }; 51 52 struct Vec4 { 53 float x, y, z, w; 54 }; 55 56 #define NANO_PI (3.14159265359f) 57 58 #define NANO_ABS(x) ((x) > 0 ? (x) : -(x)) 59 60 #define NANO_MAX(a, b) ((a) > (b)) ? (a) : (b) 61 62 #define NANO_MIN(a, b) ((a) < (b)) ? (a) : (b) 63 64 // 3-DIMENSIONAL VECTOR MATH /////////////////////////////////////////// 65 static inline void initVec3(struct Vec3 *v, float x, float y, float z) { 66 ASSERT_NOT_NULL(v); 67 v->x = x; 68 v->y = y; 69 v->z = z; 70 } 71 72 // Updates v as the sum of v and w. 73 static inline void vec3Add(struct Vec3 *v, const struct Vec3 *w) { 74 ASSERT_NOT_NULL(v); 75 ASSERT_NOT_NULL(w); 76 v->x += w->x; 77 v->y += w->y; 78 v->z += w->z; 79 } 80 81 // Updates v as the subtraction of w from v. 82 static inline void vec3Sub(struct Vec3 *v, const struct Vec3 *w) { 83 ASSERT_NOT_NULL(v); 84 ASSERT_NOT_NULL(w); 85 v->x -= w->x; 86 v->y -= w->y; 87 v->z -= w->z; 88 } 89 90 // Scales v by the scalar c, i.e. v = c * v. 91 static inline void vec3ScalarMul(struct Vec3 *v, float c) { 92 ASSERT_NOT_NULL(v); 93 v->x *= c; 94 v->y *= c; 95 v->z *= c; 96 } 97 98 // Returns the dot product of v and w. 99 static inline float vec3Dot(const struct Vec3 *v, const struct Vec3 *w) { 100 ASSERT_NOT_NULL(v); 101 ASSERT_NOT_NULL(w); 102 return v->x * w->x + v->y * w->y + v->z * w->z; 103 } 104 105 // Returns the square of the L2-norm of the given vector. 106 static inline float vec3NormSquared(const struct Vec3 *v) { 107 ASSERT_NOT_NULL(v); 108 return vec3Dot(v, v); 109 } 110 111 // Returns the L2-norm of the given vector. 112 static inline float vec3Norm(const struct Vec3 *v) { 113 ASSERT_NOT_NULL(v); 114 return sqrtf(vec3NormSquared(v)); 115 } 116 117 // Normalizes the provided vector to unit norm. If the provided vector has a 118 // norm of zero, the vector will be unchanged. 119 static inline void vec3Normalize(struct Vec3 *v) { 120 ASSERT_NOT_NULL(v); 121 float norm = vec3Norm(v); 122 ASSERT(norm > 0); 123 // Only normalize if norm is non-zero. 124 if (norm > 0) { 125 float invNorm = 1.0f / norm; 126 v->x *= invNorm; 127 v->y *= invNorm; 128 v->z *= invNorm; 129 } 130 } 131 132 // Updates u as the cross product of v and w. 133 static inline void vec3Cross(struct Vec3 *u, const struct Vec3 *v, 134 const struct Vec3 *w) { 135 ASSERT_NOT_NULL(u); 136 ASSERT_NOT_NULL(v); 137 ASSERT_NOT_NULL(w); 138 u->x = v->y * w->z - v->z * w->y; 139 u->y = v->z * w->x - v->x * w->z; 140 u->z = v->x * w->y - v->y * w->x; 141 } 142 143 // Finds a vector orthogonal to the vector [inX, inY, inZ] and returns 144 // this in the components [outX, outY, outZ]. The vector is chosen such 145 // that the smallest component of [inX, inY, inZ] is set to zero in the 146 // output vector. For example, for the in vector [0.01, 4.0, 5.0], this 147 // function will return [0, 5.0, -4.0]. 148 void findOrthogonalVector(float inX, float inY, float inZ, float *outX, 149 float *outY, float *outZ); 150 151 152 // 4-DIMENSIONAL VECTOR MATH /////////////////////////////////////////// 153 // Initialize the Vec4 structure with the provided component values. 154 static inline void initVec4(struct Vec4 *v, float x, float y, float z, 155 float w) { 156 ASSERT_NOT_NULL(v); 157 v->x = x; 158 v->y = y; 159 v->z = z; 160 v->w = w; 161 } 162 163 // N-DIMENSIONAL VECTOR MATH /////////////////////////////////////////// 164 // Dimension specified by the last argument in all functions below. 165 166 // Adds two vectors and returns the sum in the provided vector, i.e. 167 // u = v + w. 168 void vecAdd(float *u, const float *v, const float *w, int dim); 169 170 // Adds two vectors and returns the sum in the first vector, i.e. 171 // v = v + w. 172 void vecAddInPlace(float *v, const float *w, int dim); 173 174 // Subtracts two vectors and returns in the provided vector, i.e. 175 // u = v - w. 176 void vecSub(float *u, const float *v, const float *w, int dim); 177 178 // Scales vector by a scalar and returns in the provided vector, i.e. 179 // u = c * v. 180 void vecScalarMul(float *u, const float *v, float c, int dim); 181 182 // Scales vector by a scalar and returns in the same vector, i.e. 183 // v = c * v. 184 void vecScalarMulInPlace(float *v, float c, int dim); 185 186 // Returns the L2-norm of the given vector. 187 float vecNorm(const float *v, int dim); 188 189 // Returns the square of the L2-norm of the given vector. 190 float vecNormSquared(const float *v, int dim); 191 192 // Returns the dot product of v and w. 193 float vecDot(const float *v, const float *w, int dim); 194 195 // Returns the maximum absolute value in vector. 196 float vecMaxAbsoluteValue(const float *v, int dim); 197 198 #ifdef __cplusplus 199 } 200 #endif 201 202 #endif // LOCATION_LBS_CONTEXTHUB_NANOAPPS_COMMON_MATH_VEC_H_ 203