ModelSpace
All Classes Namespaces Functions Variables Enumerations Pages
vectormath.hpp
1/******************************************************************************
2* Copyright (c) ATTX LLC 2024. All Rights Reserved.
3*
4* This software and associated documentation (the "Software") are the
5* proprietary and confidential information of ATTX, LLC. The Software is
6* furnished under a license agreement between ATTX and the user organization
7* and may be used or copied only in accordance with the terms of the agreement.
8* Refer to 'license/attx_license.adoc' for standard license terms.
9*
10* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
11* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION
12* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or
13* transmitted in any form or by any means, for any purpose, without the express
14* written permission of ATTX, LLC.
15******************************************************************************/
16/*
17Vector math header file
18-----------------------
19This file defines basic functions for mathematical operations on
20the vector class
21Rather than raising an error on fail cases (i.e. divide by zero),
22the following libraries are designed to check for errors and return
23corresponding codes.
24
25Author: Alex Reynolds
26*/
27
28#ifndef VECTORMATH_HPP
29#define VECTORMATH_HPP
30
31#include "Matrix.hpp"
34#include "matrixmath.hpp"
35
36namespace clockwerk {
37
38 //// @brief Function to take the dot product of two vectors
39 //// @param a First vector in the dot operation
40 //// @param b Second vector in the dot operation
41 //// @param result PBR return of a dot b result
42 //// @note Overloaded function to return result
43 template<typename T, unsigned int L>
44 void dot(const CartesianVector<T, L> &a, const CartesianVector<T, L> &b, T &result) {
45 result = T();
46 for(unsigned int i = 0; i < L; i++) {
47 result += a.values[i][0]*b.values[i][0];
48 }
49 }
50 template<typename T, unsigned int L>
51 T dot(const CartesianVector<T, L> &a, const CartesianVector<T, L> &b) {
52 T tmp;
53 dot(a, b, tmp);
54 return tmp;
55 }
56
57 //// @brief Function to take the cross product of two vectors with length 3
58 //// @param a First vector in the cross operation
59 //// @param b Second vector in the cross operation
60 //// @param result PBR return of a cross b result
61 //// @note Overloaded function to return result
62 template<typename T>
63 void cross(CartesianVector<T, 3> a, CartesianVector<T, 3> b, CartesianVector<T, 3> &result) {
64 result.values[0][0] = a.values[1][0]*b.values[2][0] - a.values[2][0]*b.values[1][0];
65 result.values[1][0] = a.values[2][0]*b.values[0][0] - a.values[0][0]*b.values[2][0];
66 result.values[2][0] = a.values[0][0]*b.values[1][0] - a.values[1][0]*b.values[0][0];
67 }
68 template<typename T>
69 CartesianVector<T, 3> cross(const CartesianVector<T, 3> &a, const CartesianVector<T, 3> &b) {
70 CartesianVector<T, 3> tmp;
71 cross(a, b, tmp);
72 return tmp;
73 }
74
75 //// @brief Function to take the tilde operator of a vector with length 3
76 //// @param v Vector to take the tilde operator of
77 //// @param t Return of tilde matrix
78 //// @note Overloaded function returns t
79 template <typename T>
80 void tilde(const CartesianVector<T, 3> &v, Matrix<T, 3, 3> &t) {
81 t.set(0, 1, -v.values[2][0]);
82 t.set(0, 2, v.values[1][0]);
83 t.set(1, 0, v.values[2][0]);
84 t.set(1, 2, -v.values[0][0]);
85 t.set(2, 0, -v.values[1][0]);
86 t.set(2, 1, v.values[0][0]);
87 }
88 template <typename T>
89 Matrix<T, 3, 3> tilde(const CartesianVector<T, 3> &v) {
90 Matrix<T, 3, 3> t;
91 tilde(v, t);
92 return t;
93 }
94
95 /// ------------------------------------------------------------------------------------------------------
96 /// Operator overloads -- NOTE: May be less efficient than equivalent PBR function in some cases due to
97 /// return by value. Where overload is less efficient, it is explicitly noted.
98 /// ------------------------------------------------------------------------------------------------------
99 /// @brief Overload of matrix multiplication - Matrix and vector
100 /// @note Returns value -- less efficient in some cases
101 template<typename T, unsigned int R1, unsigned int C1R2>
102 CartesianVector<T, R1> operator*(const Matrix<T, R1, C1R2> &A, const CartesianVector<T, C1R2> &B) {
103 /// Call our function to add two matrices and return the result
104 CartesianVector<T, R1> result;
105 multiply(A, B, result);
106 return result;
107 }
108
109 /// @brief Overload of vector multiplication - vector and scalar
110 /// @note Returns value -- less efficient in some cases
111 template<typename T, unsigned int L>
112 CartesianVector<T, L> operator*(const T &a, const CartesianVector<T, L> &A) {
113 /// Call our function to multiply and return the result
114 CartesianVector<T, L> result;
115 multiply(a, A, result);
116 return result;
117 }
118 template<typename T, unsigned int L>
119 CartesianVector<T, L> operator*(const CartesianVector<T, L> &A, const T &a) {
120 /// Call our function to multiply and return the result
121 CartesianVector<T, L> result;
122 multiply(a, A, result);
123 return result;
124 }
125
126 /// @brief Overload of matrix addition - Two matrices
127 /// @note Returns value -- less efficient in some cases
128 template<typename T, unsigned int L>
129 CartesianVector<T, L> operator+(const CartesianVector<T, L> &A, const CartesianVector<T, L> &B) {
130 /// Call our function to add two matrices and return the result
131 CartesianVector<T, L> result;
132 eAdd(A, B, result);
133 return result;
134 }
135
136 /// @brief Overload of matrix addition - Matrix and scalar
137 /// @note Returns value -- less efficient in some cases
138 template<typename T, unsigned int L>
139 CartesianVector<T, L> operator+(const T &a, const CartesianVector<T, L> &A) {
140 /// Call our function to add two matrices and return the result
141 CartesianVector<T, L> result;
142 add(a, A, result);
143 return result;
144 }
145 template<typename T, unsigned int L>
146 CartesianVector<T, L> operator+(const CartesianVector<T, L> &A, const T &a) {
147 /// Call our function to add two matrices and return the result
148 CartesianVector<T, L> result;
149 add(a, A, result);
150 return result;
151 }
152
153 /// Returns value -- less efficient in some cases
154 template<typename T, unsigned int L>
155 CartesianVector<T, L> operator-(const CartesianVector<T, L> &A, const CartesianVector<T, L> &B) {
156 /// Call our function to add two matrices and return the result
157 CartesianVector<T, L> result;
158 eSubtract(A, B, result);
159 return result;
160 }
161
162}
163
164#endif
Standard vector class derived from Matrix.
Definition CartesianVector.hpp:39
Matrix math implementation.
Definition Matrix.hpp:54