API Documentation
Loading...
Searching...
No Matches
BaseFunctions.hpp
Go to the documentation of this file.
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>
35/**--------------------------------------------------------------------------------------------------
36Namespace: Parke
37Namespace that wraps all Logic created by Tyler Parke
38*-----------------------------------------------------------------------------------------------**/
39
40namespace NDEVR
41{
42
43 /**--------------------------------------------------------------------------------------------------
44 Fn: constexpr t_type getMin(const t_type& left, const t_type& right)
45
46 \brief Finds the minimum of the given arguments based on the < operator
47
48 Author: Tyler Parke
49
50 Date: 2017-11-05
51
52 Param:
53 left - A t_type to process.
54 right - A t_type to process.
55
56 The only requirement is that t_type have > operator defined.
57
58 For Vector-based structures a new object is created that, for each dimension stores the max for that
59 dimension.
60
61 Note integers, use same as the templated getMax, however, we can optimize specially for integers using
62 some bit twiddling tricks. This removes a branch statement entirely and is likely faster on a modern CPU
63
64 Returns: The calculated minimum.
65 *-----------------------------------------------------------------------------------------------**/
66 template <typename t_type>
67 constexpr t_type getMin(const t_type& left, const t_type& right) { return left < right ? left : right; }
68 template <typename t_type>
69 constexpr t_type getMin(const t_type& left, const t_type& middle, const t_type& right) { return getMin(getMin(left, middle), right); }
70
71 template<>
72 constexpr sint01 getMin(const sint01& left, const sint01& right) { return left ^ ((right ^ left) & -(right < left)); }
73 template<>
74 constexpr uint01 getMin(const uint01& left, const uint01& right) { return static_cast<uint01>(left ^ ((right ^ left) & -(right < left))); }
75
76 template<>
77 constexpr sint02 getMin(const sint02& left, const sint02& right) { return left ^ ((right ^ left) & -(right < left)); }
78 template<>
79 constexpr uint02 getMin(const uint02& left, const uint02& right) { return static_cast<uint02>(left ^ ((right ^ left) & -(right < left))); }
80
81 template<>
82 constexpr sint04 getMin(const sint04& left, const sint04& right) { return left ^ ((right ^ left) & -(right < left)); }
83 template<>
84 constexpr uint04 getMin(const uint04& left, const uint04& right) { return static_cast<uint04>(left ^ ((right ^ left) & -(right < left))); }
85
86 template<>
87 constexpr sint08 getMin(const sint08& left, const sint08& right) { return left ^ ((right ^ left) & -(right < left)); }
88 template<>
89 constexpr uint08 getMin(const uint08& left, const uint08& right) { return static_cast<uint08>(left ^ ((right ^ left) & -(right < left))); }
90
91 /**--------------------------------------------------------------------------------------------------
92 Fn: constexpr t_type getMax(const t_type& left, const t_type& n)
93
94 \brief Finds the max of the given arguments using the > operator
95
96 Author: Tyler Parke
97
98 Date: 2017-11-05
99
100 Param:
101 left - One of two comparators to determine the max of.
102 right - One of two comparators to determine the max of.
103
104 The only requirement is that t_type have > operator defined.
105
106 For Vector-based structures a new object is created that, for each dimension stores the max for that
107 dimension.
108
109 Note integers, use same as the templated getMax, however, we can optimize specially for integers using
110 some bit twiddling tricks. This removes a branch statement entirely and is likely faster on a modern CPU
111
112 Returns: The calculated maximum.
113 *-----------------------------------------------------------------------------------------------**/
114
115 template <typename t_type>
116 constexpr t_type getMax(const t_type& left, const t_type& right) { return left > right ? left : right; }
117 template <typename t_type>
118 constexpr t_type getMax(const t_type& left, const t_type& middle, const t_type& right) { return getMax(getMax(left, middle), right); }
119
120 template<>
121 constexpr sint01 getMax(const sint01& left, const sint01& right) { return left ^ ((left ^ right) & -(left < right)); }
122 template<>
123 constexpr uint01 getMax(const uint01& left, const uint01& right) { return static_cast<uint01>(left ^ ((left ^ right) & -(left < right))); }
124
125 template<>
126 constexpr sint02 getMax(const sint02& left, const sint02& right) { return left ^ ((left ^ right) & -(left < right)); }
127 template<>
128 constexpr uint02 getMax(const uint02& left, const uint02& right) { return static_cast<uint02>(left ^ ((left ^ right) & -(left < right))); }
129
130 template<>
131 constexpr sint04 getMax(const sint04& left, const sint04& right) { return left ^ ((left ^ right) & -(left < right)); }
132 template<>
133 constexpr uint04 getMax(const uint04& left, const uint04& right) { return static_cast<uint04>(left ^ ((left ^ right) & -(left < right))); }
134
135 template<>
136 constexpr sint08 getMax(const sint08& left, const sint08& right) { return left ^ ((left ^ right) & -(left < right)); }
137 template<>
138 constexpr uint08 getMax(const uint08& left, const uint08& right) { return static_cast<uint08>(left ^ ((left ^ right) & -(left < right))); }
139
140
141
142 /**--------------------------------------------------------------------------------------------------
143 Fn: constexpr t_type quantize(t_type a, t_type d = cast<t_type>(1))
144
145 \brief Rounds the value to the nearest multiple given and returns that value.
146
147 Author: Tyler Parke
148
149 Date: 2017-11-05
150
151 Param:
152 value - A t_type to process.
153 d - (Optional) A t_type to process. Default value is 1.
154
155 Returns: the nearest multiple from the given input. For example quantize(.8, .25) would return .75.
156 quantize(.8, .1) would return .8 and quantize(.8, .5) would return 1.0
157 *-----------------------------------------------------------------------------------------------**/
158 template <typename t_type>
159 constexpr t_type quantize(t_type value, t_type d = cast<t_type>(1))
160 {
161 return cast<t_type>(cast<sint08>(value / d + cast<t_type>(value < 0 ? -0.5 : 0.5))) * d;
162 }
163
164 /**--------------------------------------------------------------------------------------------------
165 Fn: constexpr t_type sign(t_type a)
166
167 \brief A simple function that returns 1 for all values greater than or equal to 0 and -1 for all values less than 0
168
169 Author: Tyler Parke
170
171 Date: 2017-11-05
172
173 Param:
174 value - The value to check the sign of.
175
176 Returns: 1 for all values greater than or equal to 0 and -1 for all values less than 0.
177 *-----------------------------------------------------------------------------------------------**/
178 template <typename t_type>
179 constexpr t_type sign(t_type value)
180 {
181 return value >= 0 ? cast<t_type>(1) : cast<t_type>(-1);
182 }
183
184
185 /**--------------------------------------------------------------------------------------------------
186 Fn: static constexpr bool isNaN(const t_type& value)
187
188 \brief Query if 'value' is valid or invalid.
189
190 Author: Tyler Parke
191
192 Date: 2017-11-05
193
194 Param:
195 value - The value.
196
197 Returns: True if the value is invalid, false if the value is valid.
198 *-----------------------------------------------------------------------------------------------**/
199 template<class t_type>
200 constexpr bool isNaN(const t_type& value)
201 {
202 return value == Constant<t_type>::NaN;
203 }
204 template<>
205 constexpr bool isNaN(const fltp04& value)
206 {
207 // ReSharper disable once CppIdenticalOperandsInBinaryExpression
208 return value != value;
209 }
210 template<>
211 constexpr bool isNaN(const fltp08& value)
212 {
213 // ReSharper disable once CppIdenticalOperandsInBinaryExpression
214 return value != value;
215 }
216
217 /**--------------------------------------------------------------------------------------------------
218 Fn: constexpr t_type clip(const t_type& n, const t_type& lower, const t_type& upper)
219
220 \brief Clips the value given so that that the returned value falls between upper and lower bound.
221
222 For example clip(-1, 0, 10) would return 0, clip(.5, 0, 10) would return .5, and clip(11.5, 0, 10)
223 would return 10.
224
225 Author: Tyler Parke
226
227 Date: 2017-11-05
228
229 Param:
230 value - The value. If this falls between the upper and lower bound it is returned.
231 lower_bound - The lower bound. It is assumed that this will always be a value less than or equal to
232 the upper bound.
233 upper_bound - The upper bound. It is assumed that this will always be a value greater than or equal to
234 the lower bound.
235
236 Returns: the value if it falls between the upper or lower bound, otherwise if less than lower bound.
237 returns lower bound and if greater than upper bound returns upper bound.
238 *-----------------------------------------------------------------------------------------------**/
239 template <typename t_type>
240 constexpr t_type clip(const t_type& value, const t_type& lower_bound, const t_type& upper_bound)
241 {
242 lib_assert(isNaN(lower_bound) || isNaN(upper_bound) || (lower_bound <= upper_bound), "Invalid clip: upper bound is less than lower bound");
243 return getMax(lower_bound, getMin(value, upper_bound));
244 }
245};
246
247
#define lib_assert(expression, message)
Asserts some logic in the code. Disabled in non debug mode by default. Can be re-enabled in release u...
Definition LibAssert.h:70
Definition ACIColor.h:37
int32_t sint04
-Defines an alias representing a 4 byte, signed integer. -Can represent exact integer values -2147483...
Definition BaseValues.hpp:76
constexpr t_type getMax(const t_type &left, const t_type &right)
Finds the max of the given arguments using the > operator.
Definition BaseFunctions.hpp:116
int64_t sint08
-Defines an alias representing an 8 byte, signed integer -Can represent exact integer values -9223372...
Definition BaseValues.hpp:86
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.
Definition BaseFunctions.hpp:240
float fltp04
Defines an alias representing a 4 byte floating-point number.
Definition BaseValues.hpp:157
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))
Definition AngleFunctions.h:955
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:98
int8_t sint01
-Defines an alias representing a 1 byte, signed integer. -Can represent exact integer values -127 thr...
Definition BaseValues.hpp:56
int16_t sint02
-Defines an alias representing a 2 byte, signed integer. -Can represent exact integer values -32767 t...
Definition BaseValues.hpp:66
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer -Can represent exact integer values 0 thro...
Definition BaseValues.hpp:132
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514
uint16_t uint02
-Defines an alias representing a 2 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:109
constexpr bool isNaN(const t_type &value)
Query if 'value' is valid or invalid.
Definition BaseFunctions.hpp:200
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...
Definition BaseFunctions.hpp:179
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:181
constexpr t_type getMin(const t_type &left, const t_type &right)
Finds the minimum of the given arguments based on the < operator.
Definition BaseFunctions.hpp:67
Definition BaseValues.hpp:272