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 #ifndef VEC_H_ 18 #define VEC_H_ 19 20 #include <nanohub_math.h> 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 26 struct Vec3 { 27 float x, y, z; 28 }; 29 30 struct Vec4 { 31 float x, y, z, w; 32 }; 33 34 35 static inline void initVec3(struct Vec3 *v, float x, float y, float z) { 36 v->x = x; 37 v->y = y; 38 v->z = z; 39 } 40 41 static inline void vec3Add(struct Vec3 *v, const struct Vec3 *w) { 42 v->x += w->x; 43 v->y += w->y; 44 v->z += w->z; 45 } 46 47 static inline void vec3Sub(struct Vec3 *v, const struct Vec3 *w) { 48 v->x -= w->x; 49 v->y -= w->y; 50 v->z -= w->z; 51 } 52 53 static inline void vec3ScalarMul(struct Vec3 *v, float c) { 54 v->x *= c; 55 v->y *= c; 56 v->z *= c; 57 } 58 59 static inline float vec3Dot(const struct Vec3 *v, const struct Vec3 *w) { 60 return v->x * w->x + v->y * w->y + v->z * w->z; 61 } 62 63 static inline float vec3NormSquared(const struct Vec3 *v) { 64 return vec3Dot(v, v); 65 } 66 67 static inline float vec3Norm(const struct Vec3 *v) { 68 return sqrtf(vec3NormSquared(v)); 69 } 70 71 static inline void vec3Normalize(struct Vec3 *v) { 72 float invNorm = 1.0f / vec3Norm(v); 73 v->x *= invNorm; 74 v->y *= invNorm; 75 v->z *= invNorm; 76 } 77 78 static inline void vec3Cross(struct Vec3 *u, const struct Vec3 *v, const struct Vec3 *w) { 79 u->x = v->y * w->z - v->z * w->y; 80 u->y = v->z * w->x - v->x * w->z; 81 u->z = v->x * w->y - v->y * w->x; 82 } 83 84 static inline void initVec4(struct Vec4 *v, float x, float y, float z, float w) { 85 v->x = x; 86 v->y = y; 87 v->z = z; 88 v->w = w; 89 } 90 91 92 void findOrthogonalVector( float inX, float inY, float inZ, 93 float *outX, float *outY, float *outZ); 94 95 96 #ifdef __cplusplus 97 } 98 #endif 99 100 #endif // VEC_H_ 101