NDEVR
API Documentation
BaseFunctions.hpp
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: BaseFunctions
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/BaseValues.h>
34#include <NDEVR/LibAssert.h>
35namespace NDEVR
36{
37
55 template <typename t_type>
56 constexpr t_type getMin(const t_type& left, const t_type& right) { return left < right ? left : right; }
57 template <typename t_type>
58 constexpr t_type getMin(const t_type& left, const t_type& middle, const t_type& right) { return getMin(getMin(left, middle), right); }
59
60 template<>
61 constexpr sint01 getMin(const sint01& left, const sint01& right) { return left ^ ((right ^ left) & -(right < left)); }
62 template<>
63 constexpr uint01 getMin(const uint01& left, const uint01& right) { return static_cast<uint01>(left ^ ((right ^ left) & -(right < left))); }
64
65 template<>
66 constexpr sint02 getMin(const sint02& left, const sint02& right) { return left ^ ((right ^ left) & -(right < left)); }
67 template<>
68 constexpr uint02 getMin(const uint02& left, const uint02& right) { return static_cast<uint02>(left ^ ((right ^ left) & -(right < left))); }
69
70 template<>
71 constexpr sint04 getMin(const sint04& left, const sint04& right) { return left ^ ((right ^ left) & -(right < left)); }
72 template<>
73 constexpr uint04 getMin(const uint04& left, const uint04& right) { return static_cast<uint04>(left ^ ((right ^ left) & -(right < left))); }
74
75 template<>
76 constexpr sint08 getMin(const sint08& left, const sint08& right) { return left ^ ((right ^ left) & -(right < left)); }
77 template<>
78 constexpr uint08 getMin(const uint08& left, const uint08& right) { return static_cast<uint08>(left ^ ((right ^ left) & -(right < left))); }
79
93 template <typename t_type>
94 constexpr t_type getMax(const t_type& left, const t_type& right) { return left > right ? left : right; }
95 template <typename t_type>
96 constexpr t_type getMax(const t_type& left, const t_type& middle, const t_type& right) { return getMax(getMax(left, middle), right); }
97
98 template<>
99 constexpr sint01 getMax(const sint01& left, const sint01& right) { return left ^ ((left ^ right) & -(left < right)); }
100 template<>
101 constexpr uint01 getMax(const uint01& left, const uint01& right) { return static_cast<uint01>(left ^ ((left ^ right) & -(left < right))); }
102
103 template<>
104 constexpr sint02 getMax(const sint02& left, const sint02& right) { return left ^ ((left ^ right) & -(left < right)); }
105 template<>
106 constexpr uint02 getMax(const uint02& left, const uint02& right) { return static_cast<uint02>(left ^ ((left ^ right) & -(left < right))); }
107
108 template<>
109 constexpr sint04 getMax(const sint04& left, const sint04& right) { return left ^ ((left ^ right) & -(left < right)); }
110 template<>
111 constexpr uint04 getMax(const uint04& left, const uint04& right) { return static_cast<uint04>(left ^ ((left ^ right) & -(left < right))); }
112
113 template<>
114 constexpr sint08 getMax(const sint08& left, const sint08& right) { return left ^ ((left ^ right) & -(left < right)); }
115 template<>
116 constexpr uint08 getMax(const uint08& left, const uint08& right) { return static_cast<uint08>(left ^ ((left ^ right) & -(left < right))); }
117
118
119
133 template <typename t_type>
134 constexpr t_type quantize(t_type value, t_type d = cast<t_type>(1))
135 {
136 return cast<t_type>(cast<sint08>(value / d + cast<t_type>(value < 0 ? -0.5 : 0.5))) * d;
137 }
138
150 template <typename t_type>
151 constexpr t_type sign(t_type value)
152 {
153 return value >= 0 ? cast<t_type>(1) : cast<t_type>(-1);
154 }
155
156 template<typename t_type>
157 constexpr t_type makeNiceNumberA(t_type value)
158 {
159 t_type magnitude = pow(10, std::floor(std::log10(value)));
160 t_type normalized_step = value / magnitude;
161 if (normalized_step <= 1.5)
162 normalized_step = 1.0;
163 else if (normalized_step <= 3.0)
164 normalized_step = 2.0;
165 else if (normalized_step <= 7.5)
166 normalized_step = 5.0;
167 else
168 normalized_step = 10.0;
169 return normalized_step * magnitude;
170 }
171 template<typename t_type>
172 constexpr t_type makeNiceNumberB(t_type value)
173 {
174 t_type magnitude = pow(10, std::floor(std::log10(value)));
175 t_type normalized_step = value / magnitude;
176 if (normalized_step <= 1.025)
177 normalized_step = 1.0;
178 else if (normalized_step <= 1.075)
179 normalized_step = 1.05;
180 else
181 normalized_step = 0.1 * std::round(10.0 * normalized_step);
182 return normalized_step * magnitude;
183 }
196 template<class t_type>
197 constexpr bool IsInvalid(const t_type& value)
198 {
199 return value == Constant<t_type>::Invalid;
200 }
201 template<>
202 constexpr bool IsInvalid(const fltp04& value)
203 {
204 // ReSharper disable once CppIdenticalOperandsInBinaryExpression
205 return value != value;
206 }
207 template<>
208 constexpr bool IsInvalid(const fltp08& value)
209 {
210 // ReSharper disable once CppIdenticalOperandsInBinaryExpression
211 return value != value;
212 }
213 template<class t_type>
214 constexpr bool IsValid(const t_type& value)
215 {
216 return !IsInvalid<t_type>(value);
217 }
237 template <typename t_type>
238 constexpr t_type clip(const t_type& value, const t_type& lower_bound, const t_type& upper_bound)
239 {
240 lib_assert(IsInvalid(lower_bound) || IsInvalid(upper_bound) || (lower_bound <= upper_bound), "Invalid clip: upper bound is less than lower bound");
241 return getMax(lower_bound, getMin(value, upper_bound));
242 }
243};
244
245
The primary namespace for the NDEVR SDK.
constexpr t_type getMin(const t_type &left, const t_type &right)
Finds the minimum of the given arguments based on the < operator Author: Tyler Parke Date: 2017-11-05...
constexpr t_type sign(t_type value)
A simple function that returns 1 for all values greater than or equal to 0 and -1 for all values less...
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
constexpr t_type getMax(const t_type &left, const t_type &right)
Finds the max of the given arguments using the > operator The only requirement is that t_type have > ...
static constexpr bool IsValid(const Angle< t_type > &value)
Checks whether the given Angle holds a valid value.
Definition Angle.h:398
uint16_t uint02
-Defines an alias representing a 2 byte, unsigned integer -Can represent exact integer values 0 throu...
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
constexpr Vector< t_dims, Angle< t_angle_type > > quantize(const Vector< t_dims, Angle< t_angle_type > > &value, Angle< t_angle_type > d=Angle< t_angle_type >(DEGREES, 1.0))
Quantizes a Vector of Angles to the nearest multiple of a given step size.
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
double fltp08
Defines an alias representing an 8 byte floating-point number.
int16_t sint02
-Defines an alias representing a 2 byte, signed integer.
int32_t sint04
-Defines an alias representing a 4 byte, signed integer.
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
static constexpr bool IsInvalid(const Angle< t_type > &value)
Checks whether the given Angle holds an invalid value.
Definition Angle.h:388
int8_t sint01
-Defines an alias representing a 1 byte, signed integer.
constexpr t_type clip(const t_type &value, const t_type &lower_bound, const t_type &upper_bound)
Clips the value given so that that the returned value falls between upper and lower bound.
int64_t sint08
-Defines an alias representing an 8 byte, signed integer -Can represent exact integer values -9223372...
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Definition Angle.h:408