Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
NumberOperations.hpp
Go to the documentation of this file.
1
12#ifndef NUMBER_OPERATIONS_HPP
13#define NUMBER_OPERATIONS_HPP
14
16#include <algorithm>
17#include <cmath>
18#include <iostream>
19
21
22
30namespace numops
31{
32
40 template<typename T>
42 {
43 public:
44 // Declare public struct member variables.
45 T tX;
46 T tY;
47 T tZ;
48
49
59 CoordinatePoint(const T tX = 0.0, const T tY = 0.0, const T tZ = 0.0)
60 {
61 // Initialize member variables.
62 this->tX = tX;
63 this->tY = tY;
64 this->tZ = tZ;
65 }
66 };
67
68
80 template<typename T>
81 inline constexpr T Clamp(T tValue, T tMin, T tMax)
82
83 {
84 return std::max(std::min(tMax, tValue), tMin);
85 }
86
87
100 template<typename T>
101 inline bool Bounded(T tValue, T tMin, T tMax, const bool bInclusive = true)
102 {
103 // Check if value is inclusive or not.
104 if (bInclusive)
105 {
106 // Return true if the given value is valid.
107 return (tValue >= tMin) && (tValue <= tMax);
108 }
109 else
110 {
111 // Return true if the given value is valid.
112 return (tValue > tMin) && (tValue < tMax);
113 }
114 }
115
116
130 template<typename T>
131 inline constexpr T MapRange(const T tValue, const T tOldMinimum, const T tOldMaximum, const T tNewMinimum, const T tNewMaximum)
132 {
133 // Check if the ranges are valid.
134 if (tOldMinimum == tOldMaximum || tNewMinimum == tNewMaximum)
135 {
136 // Submit logger message.
137 std::cerr << "MAPRANGE: The old/new range is not valid." << std::endl;
138
139 // Return old, given value.
140 return tValue;
141 }
142
143 // Perform the mapping using linear interpolation.
144 T tOldRange = tOldMaximum - tOldMinimum;
145 T tNewRange = tNewMaximum - tNewMinimum;
146 return (((tValue - tOldMinimum) * tNewRange) / tOldRange) + tNewMinimum;
147 }
148
149
161 template<typename T>
162 inline constexpr T InputAngleModulus(T tValue, T tMinValue, T tMaxValue)
163 {
164 // Determine the correct modulus number.
165 T tModulus = tMaxValue - tMinValue;
166
167 // Wrap input if it's above the maximum input.
168 int nNumMax = (tValue - tMinValue) / tModulus;
169 tValue -= nNumMax * tModulus;
170 // Wrap input if it's below the minimum input.
171 int nNumMin = (tValue - tMaxValue) / tModulus;
172 tValue -= nNumMin * tModulus;
173
174 // Check if the final value is the max value, set to min.
175 if (tValue == tMaxValue)
176 {
177 tValue = tMinValue;
178 }
179
180 // Return wrapped number.
181 return tValue;
182 }
183
184
200 template<typename T>
201 inline constexpr T AngularDifference(T tFirstValue, T tSecondValue)
202 {
203 // Calculate the difference between the two angles.
204 T tDifference = tSecondValue - tFirstValue;
205 // Wrap the difference around the 0-360 degree range.
206 tDifference = InputAngleModulus(tDifference, -180.0, 180.0);
207
208 return tDifference;
209 }
210
211
265 template<typename T>
266 inline constexpr void CoordinateFrameRotate3D(std::vector<CoordinatePoint<T>>& vPointCloud,
267 const double dXRotationDegrees,
268 const double dYRotationDegrees,
269 const double dZRotationDegrees)
270 {
271 // Convert input degrees to radians.
272 double dXRotationRadians = (dXRotationDegrees * M_PI) / 180.0;
273 double dYRotationRadians = (dYRotationDegrees * M_PI) / 180.0;
274 double dZRotationRadians = (dZRotationDegrees * M_PI) / 180.0;
275
276 // Find the cosine and sine for the X-axis rotation matrix.
277 double dCosA = cos(dXRotationRadians);
278 double dSinA = sin(dXRotationRadians);
279 // Find the cosine and sine for the Y-axis rotation matrix.
280 double dCosB = cos(dYRotationRadians);
281 double dSinB = sin(dYRotationRadians);
282 // Find the cosine and sine for the Z-axis rotation matrix.
283 double dCosC = cos(dZRotationRadians);
284 double dSinC = sin(dZRotationRadians);
285
286 // Calculate the 3D rotation matrix using matrix multiplication with proper Euler angles Z, Y, X. A = (Rz * Ry * Rx).
287 // First row of A matrix.
288 double dAXX = dCosC * dCosB;
289 double dAXY = dCosC * dSinB * dSinA - dSinC * dCosA;
290 double dAXZ = dCosC * dSinB * dCosA + dSinC * dSinA;
291 // Second row of A matrix.
292 double dAYX = dSinC * dCosB;
293 double dAYY = dSinC * dSinB * dSinA + dCosC * dCosA;
294 double dAYZ = dSinC * dSinB * dCosA - dCosC * dSinA;
295 // Third row of A matrix.
296 double dAZX = -dSinB;
297 double dAZY = dCosB * dSinA;
298 double dAZZ = dCosB * dCosA;
299
300 // Loop through each point in the given point cloud.
301 for (CoordinatePoint<T>& stPoint : vPointCloud)
302 {
303 // Rotate the points in X, Y, Z order using the rotation matrix A.
304 T tX = static_cast<T>((dAXX * stPoint.tX) + (dAXY * stPoint.tY) + (dAXZ * stPoint.tZ));
305 T tY = static_cast<T>((dAYX * stPoint.tX) + (dAYY * stPoint.tY) + (dAYZ * stPoint.tZ));
306 T tZ = static_cast<T>((dAZX * stPoint.tX) + (dAZY * stPoint.tY) + (dAZZ * stPoint.tZ));
307 // Update point cloud.
308 stPoint.tX = tX;
309 stPoint.tY = tY;
310 stPoint.tZ = tZ;
311 }
312 }
313} // namespace numops
314#endif
__device__ __forceinline__ float1 cos(const uchar1 &a)
__device__ __forceinline__ float4 sin(const uchar4 &a)
Namespace containing functions related to operations on numbers and other datatypes.
Definition NumberOperations.hpp:31
constexpr T Clamp(T tValue, T tMin, T tMax)
Clamps a given value from going above or below a given threshold.
Definition NumberOperations.hpp:81
constexpr T InputAngleModulus(T tValue, T tMinValue, T tMaxValue)
Calculates the modulus of an input angle.
Definition NumberOperations.hpp:162
constexpr void CoordinateFrameRotate3D(std::vector< CoordinatePoint< T > > &vPointCloud, const double dXRotationDegrees, const double dYRotationDegrees, const double dZRotationDegrees)
This method will rotate a list of 3D coordinate points a variable amount of degrees around the X,...
Definition NumberOperations.hpp:266
constexpr T AngularDifference(T tFirstValue, T tSecondValue)
Calculates the distance in degrees between two angles. This function accounts for wrap around so that...
Definition NumberOperations.hpp:201
constexpr T MapRange(const T tValue, const T tOldMinimum, const T tOldMaximum, const T tNewMinimum, const T tNewMaximum)
Maps a value to a new range given the old range.
Definition NumberOperations.hpp:131
bool Bounded(T tValue, T tMin, T tMax, const bool bInclusive=true)
Checks if a given value is between the given maximum and minimum ranges.
Definition NumberOperations.hpp:101
This struct represents a point in a 3D coordinate system.
Definition NumberOperations.hpp:42
CoordinatePoint(const T tX=0.0, const T tY=0.0, const T tZ=0.0)
Construct a new Coordinate Point object.
Definition NumberOperations.hpp:59