ModelSpace
All Classes Namespaces Functions Variables Enumerations Pages
CartesianVector.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/*
17Cartesian Vector header file
18
19Author: Alex Reynolds
20*/
21#ifndef CARTESIAN_VECTOR_HPP
22#define CARTESIAN_VECTOR_HPP
23
24#include <cmath>
25
26#include "Matrix.hpp"
27#include "safemath.hpp"
28
29namespace clockwerk {
30
31 /// @brief Standard vector class derived from Matrix
32 ///
33 /// This file defines a simple vector class for cartesian systems
34 /// in any number of dimensions. As an inherited class from the
35 /// Matrix class defined in this directory, it can perform all
36 /// matrix operations and has several operations of its own specific
37 /// to vectors.
38 template<typename T, unsigned int L>
39 class CartesianVector : public Matrix<T, L, 1> {
40 public:
41 /// Redeclare constructors - note here that we're defining these to call
42 /// their matrix counterparts, with the exception of the initializer list
43 /// which we redefine for a vector
44 /// Default constructor to initialize a vector to all zeroes
45 CartesianVector<T, L>() : Matrix<T, L, 1>() {};
46
47 /// Constructor to initialize a matrix with all the same element
48 CartesianVector<T, L>(T elements) : Matrix<T, L, 1>(elements) {};
49
50 /// Constructor for Matrix class with initialization.
51 /// Initializes matrix to values passed in via array
52 CartesianVector<T, L>(const T(&initial)[L]);
53
54 /// Copy constructor for Matrix class. Copies data from matrix
55 /// object to the current instance
56 CartesianVector<T, L>(const CartesianVector<T, L> &initial);
57
58 /// Constructor for vector initialization via array
59 CartesianVector<T, L>(const std::array<T, L> &initial);
60
61 /// Destructor -- doesn't do anything because we don't dynamically
62 /// allocate
63 ~CartesianVector<T, L>() {}
64
65 /// @brief Function to return a vector value
66 /// @param idx The index to return
67 /// @return Reference to the vector element
68 /// @note This vector, much like std::array, does not check index
69 /// range and should be used accordingly
70 T& operator [](unsigned int idx);
71
72 /// @brief Setter specific to the vector class
73 /// @param idx Vector element to set
74 /// @param value The value to set the element to
75 /// @return Integer value corresponding to error codes in clockwerkerrrors.h
76 int set(const unsigned int &idx, const T &value) {return Matrix<T, L, 1>::set(idx, 0, value);}
77
78 /// @brief Getter specific to the vector class
79 /// @param idx The index to return
80 /// @param value PBR return of the value in the vector
81 /// @return Integer value corresponding to error codes in clockwerkerrrors.h
82 int get(const unsigned int &idx, T &result) const {return Matrix<T, L, 1>::get(idx, 0, result);}
83
84 /// @brief Getter specific to the vector class
85 /// @param idx The index to return
86 /// @return Value at index
87 /// @note Does not bounds check index and should be treated safely
88 T get(const unsigned int &idx) const {return Matrix<T, L, 1>::get(idx, 0);}
89
90 /// @brief Function to take the norm of a vector
91 /// @param result PBR return of the norm operation
92 int norm(T &result) const;
93
94 /// @brief Function to take the norm of a vector
95 /// @return return of the norm operation
96 T norm() const {T tmp; norm(tmp); return tmp;}
97
98 /// @brief Function to take the squared norm of a vector
99 /// @param result PBR return of the norm^2 operation
100 int normSquared(T &result) const;
101
102 /// @brief Function to take the squared norm of a vector
103 /// @return return of the norm squared operation
104 T normSquared() const {T tmp; normSquared(tmp); return tmp;}
105
106 /// @brief Function to return the unitized version of the vector
107 /// @param result PBR return of the unit vector
108 /// @return Error code corresponding to those in clockwerkerrors.h
109 int unit(CartesianVector<T, L> &result) const;
110
111 /// @brief Function to unitize the current vector
112 /// @return Error code corresponding to those in clockwerkerrors.h
113 int unitize();
114
115 /// @brief Function to unitize the current vector
116 /// @return Error code corresponding to those in clockwerkerrors.h
117 int normalize();
118
119 /// @brief Function to write out vector as a string
120 /// @return Vector written as a square brace enclosed string
121 std::string str() const;
122 };
123
124 template <typename T, unsigned int L>
125 CartesianVector<T, L>::CartesianVector(const T(&initial)[L]) {
126 /// Note -- one absolutely essential check that's occurring here is that the size
127 /// of the initializer list matches the size of the matrix. This is occurring implicitly
128 /// at compile time via templating and is the reason why an array is used rather than
129 /// a std::initializer_list
130
131 /// Initialize to values from initializer list
132 unsigned int i;
133 for(i = 0; i < L; i++) {
134 Matrix<T, L, 1>::values[i][0] = initial[i];
135 }
136 }
137
138 template <typename T, unsigned int L>
139 CartesianVector<T, L>::CartesianVector(const std::array<T, L> &initial) {
140 unsigned int i;
141 for(i = 0; i < L; i++) {
142 Matrix<T, L, 1>::values[i][0] = initial[i];
143 }
144 }
145
146 template <typename T, unsigned int L>
147 CartesianVector<T, L>::CartesianVector(const CartesianVector<T, L> &initial)
148 : Matrix<T, L, 1>::Matrix(initial) {}
149
150 template <typename T, unsigned int L>
151 T& CartesianVector<T, L>::operator [](unsigned int idx) {
152 return Matrix<T, L, 1>::values[idx][0];
153 }
154
155 template <typename T, unsigned int L>
156 int CartesianVector<T, L>::norm(T &result) const {
157 unsigned int i;
158 /// Loop through vector to get norm squared value
159 result = 0.0;
160 for(i = 0; i < L; i++) {
161 result += Matrix<T, L, 1>::values[i][0]*Matrix<T, L, 1>::values[i][0];
162 }
163
164 /// Take square root to get norm.
165 int err = safeSqrt(result, result);
166
167 return err;
168 }
169
170 template <typename T, unsigned int L>
171 int CartesianVector<T, L>::normSquared(T &result) const {
172 unsigned int i;
173 /// Loop through vector to get norm squared value
174 result = 0.0;
175 for(i = 0; i < L; i++) {
176 result += Matrix<T, L, 1>::values[i][0]*Matrix<T, L, 1>::values[i][0];
177 }
178
179 return NO_ERROR;
180 }
181
182 template <typename T, unsigned int L>
183 int CartesianVector<T, L>::unit(CartesianVector<T, L> &result) const {
184 /// Calculate the norm of our vector
185 T mag = T();
186 norm(mag);
187
188 /// Ensure magnitude is not zero -- if it is, throw error and return
189 if(mag == 0) {
191 }
192
193 unsigned int i;
194 for(i = 0; i < L; i++) {
195 result.values[i][0] = Matrix<T, L, 1>::values[i][0]/mag;
196 }
197 return NO_ERROR;
198 }
199
200 template <typename T, unsigned int L>
201 int CartesianVector<T, L>::unitize() {
202 int err = unit(*this);
203 if(err) {
204 return err;
205 } else {
206 return NO_ERROR;
207 }
208 }
209
210 template <typename T, unsigned int L>
211 int CartesianVector<T, L>::normalize() {
212 return unitize();
213 }
214
215 template <typename T, unsigned int L>
216 std::string CartesianVector<T, L>::str() const {
217 std::string out = "[";
218 /// Loop through vector and write out values
219 for(unsigned int i = 0; i < L; i++) {
220 if(i > 0) {
221 out += ",";
222 }
223 out += std::to_string(Matrix<T, L, 1>::values[i][0]);
224 }
225
226 out += "]";
227 return out;
228 }
229
230}
231
232#endif
Standard vector class derived from Matrix.
Definition CartesianVector.hpp:39
T & operator[](unsigned int idx)
Function to return a vector value.
Definition CartesianVector.hpp:151
int unit(CartesianVector< T, L > &result) const
Function to return the unitized version of the vector.
Definition CartesianVector.hpp:183
T norm() const
Function to take the norm of a vector.
Definition CartesianVector.hpp:96
int norm(T &result) const
Function to take the norm of a vector.
Definition CartesianVector.hpp:156
T normSquared() const
Function to take the squared norm of a vector.
Definition CartesianVector.hpp:104
int normalize()
Function to unitize the current vector.
Definition CartesianVector.hpp:211
T get(const unsigned int &idx) const
Getter specific to the vector class.
Definition CartesianVector.hpp:88
int unitize()
Function to unitize the current vector.
Definition CartesianVector.hpp:201
int get(const unsigned int &idx, T &result) const
Getter specific to the vector class.
Definition CartesianVector.hpp:82
int set(const unsigned int &idx, const T &value)
Setter specific to the vector class.
Definition CartesianVector.hpp:76
int normSquared(T &result) const
Function to take the squared norm of a vector.
Definition CartesianVector.hpp:171
std::string str() const
Function to write out vector as a string.
Definition CartesianVector.hpp:216
Matrix math implementation.
Definition Matrix.hpp:54
#define NO_ERROR
Definition clockwerkerrors.h:31
#define ERROR_DIVIDE_BY_ZERO
Definition clockwerkerrors.h:46