WarpTwin
Documentation for WarpTwin models and classes.
Loading...
Searching...
No Matches
CartesianVector.hpp
Go to the documentation of this file.
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/*
17Cartesian Vector header file
18
19Author: Alex Reynolds
20*/
21#ifndef CORE_CARTESIAN_VECTOR_HPP
22#define CORE_CARTESIAN_VECTOR_HPP
23
24#include <cmath>
25
26#include "Matrix.hpp"
27#include "safemath.h"
28
29namespace clockwerk {
30
38 template<uint32 L>
39 class CartesianVector : public Matrix<L, 1> {
40 public:
45 CartesianVector() : Matrix<L, 1>() {};
46
48 CartesianVector(floating_point elements) : Matrix<L, 1>(elements) {};
49
52 CartesianVector(const floating_point(&initial)[L]);
53
56 CartesianVector(const CartesianVector<L> &initial);
57
60 CartesianVector(const Matrix<L, 1> &initial);
61
63 CartesianVector(const std::array<floating_point, L> &initial);
64
68
74 floating_point& operator [](uint32 idx);
75
80 int set(uint32 idx, floating_point value) {return Matrix<L, 1>::set(idx, 0, value);}
81
86 int get(uint32 idx, floating_point &result) const {return Matrix<L, 1>::get(idx, 0, result);}
87
92 floating_point get(uint32 idx) const {return Matrix<L, 1>::get(idx, 0);}
93
96 int16 norm(floating_point &result) const;
97
100 floating_point norm() const {floating_point tmp; norm(tmp); return tmp;}
101
104 int16 normSquared(floating_point &result) const;
105
108 floating_point normSquared() const {floating_point tmp; normSquared(tmp); return tmp;}
109
113 int16 unit(CartesianVector<L> &result) const;
114
117 int16 unitize();
118
121 int16 normalize();
122 };
123
124 template <uint32 L>
125 CartesianVector<L>::CartesianVector(const floating_point(&initial)[L]) {
130
132 for(uint32 i = 0; i < L; i++) {
133 Matrix<L, 1>::values[i][0] = initial[i];
134 }
135 }
136
137 template <uint32 L>
138 CartesianVector<L>::CartesianVector(const std::array<floating_point, L> &initial) {
139 for(uint32 i = 0; i < L; i++) {
140 Matrix<L, 1>::values[i][0] = initial[i];
141 }
142 }
143
144 template <uint32 L>
146 : Matrix<L, 1>(initial) {}
147
148 template <uint32 L>
150 : Matrix<L, 1>(initial) {}
151
152 template <uint32 L>
153 floating_point& CartesianVector<L>::operator [](uint32 idx) {
154 return Matrix<L, 1>::values[idx][0];
155 }
156
157 template <uint32 L>
158 int16 CartesianVector<L>::norm(floating_point &result) const {
159 uint32 i;
161 result = 0.0;
162 for(i = 0; i < L; i++) {
163 result += Matrix<L, 1>::values[i][0]*Matrix<L, 1>::values[i][0];
164 }
165
167 int err = safeSqrt(result, result);
168
169 return err;
170 }
171
172 template <uint32 L>
173 int16 CartesianVector<L>::normSquared(floating_point &result) const {
174 uint32 i;
176 result = 0.0;
177 for(i = 0; i < L; i++) {
178 result += Matrix<L, 1>::values[i][0]*Matrix<L, 1>::values[i][0];
179 }
180
181 return NO_ERROR;
182 }
183
184 template <uint32 L>
187 floating_point mag = 0.0;
188 norm(mag);
189
191 if(mag < SAFE_MATH_ZERO_TOLERANCE) {
193 }
194
195 for(uint32 i = 0; i < L; i++) {
196 result.values[i][0] = Matrix<L, 1>::values[i][0]/mag;
197 }
198 return NO_ERROR;
199 }
200
201 template <uint32 L>
203 int err = unit(*this);
204 if(err) {
205 return err;
206 } else {
207 return NO_ERROR;
208 }
209 }
210
211 template <uint32 L>
213 return unitize();
214 }
215}
216
217#endif
Standard vector class derived from Matrix.
Definition CartesianVector.hpp:39
int16 norm(floating_point &result) const
Function to take the norm of a vector.
Definition CartesianVector.hpp:158
int16 normalize()
Function to unitize the current vector.
Definition CartesianVector.hpp:212
~CartesianVector()
Definition CartesianVector.hpp:67
floating_point get(uint32 idx) const
Getter specific to the vector class.
Definition CartesianVector.hpp:92
int16 unitize()
Function to unitize the current vector.
Definition CartesianVector.hpp:202
floating_point normSquared() const
Function to take the squared norm of a vector.
Definition CartesianVector.hpp:108
int get(uint32 idx, floating_point &result) const
Getter specific to the vector class.
Definition CartesianVector.hpp:86
int16 normSquared(floating_point &result) const
Function to take the squared norm of a vector.
Definition CartesianVector.hpp:173
floating_point norm() const
Function to take the norm of a vector.
Definition CartesianVector.hpp:100
int set(uint32 idx, floating_point value)
Setter specific to the vector class.
Definition CartesianVector.hpp:80
floating_point & operator[](uint32 idx)
Function to return a vector value.
Definition CartesianVector.hpp:153
CartesianVector(floating_point elements)
Constructor to initialize a matrix with all the same element.
Definition CartesianVector.hpp:48
int16 unit(CartesianVector< L > &result) const
Function to return the unitized version of the vector.
Definition CartesianVector.hpp:185
CartesianVector()
Definition CartesianVector.hpp:45
Matrix math implementation.
Definition Matrix.hpp:55
std::array< std::array< floating_point, C >, R > values
Definition Matrix.hpp:213
int16 get(uint32 row, uint32 col, floating_point &result) const
Function to get a single value in the matrix.
Definition Matrix.hpp:394
Matrix()
Definition Matrix.hpp:233
int16 set(uint32 row, uint32 col, const floating_point &value)
Function to set a single value in the matrix.
Definition Matrix.hpp:381
#define NO_ERROR
Error code in the case where matrix math executed successfully.
Definition clockwerkerrors.h:34
#define ERROR_DIVIDE_BY_ZERO
Error code in the case where some fool tried to divide by zero.
Definition clockwerkerrors.h:49
Definition CircularBuffer.hpp:28
int16 safeSqrt(float input, float &result)
Overloaded functions to perform safe square root.
Definition safemath.cpp:57
#define SAFE_MATH_ZERO_TOLERANCE
Definition safemath.h:32