Geogram Version 1.9.9
A programming library of geometric algorithms
Loading...
Searching...
No Matches
numeric.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40#ifndef GEOGRAM_BASIC_NUMERIC
41#define GEOGRAM_BASIC_NUMERIC
42
44#include <cmath>
45#include <float.h>
46#include <limits.h>
47#include <algorithm> // for std::min / std::max
48#include <stdint.h>
49#include <limits>
50#include <type_traits>
51
52#ifndef M_PI
56#define M_PI 3.14159265358979323846
57#endif
58
64namespace GEO {
65
69 enum Sign {
73 ZERO = 0,
75 POSITIVE = 1
76 };
77
78
88 template <class T>
89 inline Sign geo_cmp(const T& a, const T& b) {
90 return Sign((a > b) - (a < b));
91 }
92
93
106 template <class T>
107 inline Sign geo_sgn(const T& x) {
108 return geo_cmp(x, T(0));
109 }
110
118 namespace Numeric {
119
121 typedef void* pointer;
122
124 typedef int8_t int8;
125
127 typedef int16_t int16;
128
130 typedef int32_t int32;
131
133 typedef int64_t int64;
134
136 typedef uint8_t uint8;
137
139 typedef uint16_t uint16;
140
142 typedef uint32_t uint32;
143
145 typedef uint64_t uint64;
146
148 typedef float float32;
149
151 typedef double float64;
152
157 return std::numeric_limits<float32>::max();
158 }
159
164 // Note: numeric_limits<>::min() is not
165 // what we want (it returns the smallest
166 // positive non-denormal).
167 return -max_float32();
168 }
169
174 return std::numeric_limits<float64>::max();
175 }
176
181 // Note: numeric_limits<>::min() is not
182 // what we want (it returns the smallest
183 // positive non-denormal).
184 return -max_float64();
185 }
186
190 bool GEOGRAM_API is_nan(float32 x);
191
195 bool GEOGRAM_API is_nan(float64 x);
196
200 void GEOGRAM_API random_reset();
201
205 int32 GEOGRAM_API random_int32();
206
210 float32 GEOGRAM_API random_float32();
211
215 float64 GEOGRAM_API random_float64();
216
231 template <class T, bool is_numeric>
232 struct LimitsHelper : std::numeric_limits<T> {
233 };
234
242 template <class T>
243 struct LimitsHelper<T, true> : std::numeric_limits<T> {
245 static const size_t size = sizeof(T);
247 static const size_t numbits = 8 * sizeof(T);
248 };
249
259 template <class T>
260 struct Limits :
261 LimitsHelper<T, std::numeric_limits<T>::is_specialized> {
262 };
263
268 template <class T> inline void optimize_number_representation(T& x) {
269 geo_argused(x);
270 }
271
279 template <class T> inline Sign ratio_compare(
280 const T& a_num, const T& a_denom, const T& b_num, const T& b_denom
281 ) {
282 if(a_denom == b_denom) {
283 return Sign(geo_cmp(a_num,b_num)*geo_sgn(a_denom));
284 }
285 return Sign(
286 geo_cmp(a_num*b_denom, b_num*a_denom) *
287 geo_sgn(a_denom) * geo_sgn(b_denom)
288 );
289 }
290 }
291
292 /************************************************************************/
293
294
301 template <class T>
302 inline T geo_sqr(T x) {
303 return x * x;
304 }
305
314 template <class T>
315 inline void geo_clamp(T& x, T min, T max) {
316 if(x < min) {
317 x = min;
318 } else if(x > max) {
319 x = max;
320 }
321 }
322
331
336 return std::numeric_limits<index_t>::max();
337 }
338
345
350 return std::numeric_limits<signed_index_t>::max();
351 }
352
357 return std::numeric_limits<signed_index_t>::min();
358 }
359
365
369 inline double round(double x) {
370 return ((x - floor(x)) > 0.5 ? ceil(x) : floor(x));
371 }
372
373 /************************************************************************/
374
380 static constexpr index_t NO_INDEX = index_t(-1);
381
382 /************************************************************************/
383
390 template <class T> struct is_scalar {
391 typedef typename std::is_arithmetic<T>::type type;
392 static constexpr bool value = std::is_arithmetic<T>::value;
393 };
394
395 /************************************************************************/
396}
397
398#endif
unsigned char geo_coord_index_t
Represents dimension (e.g. 3 for 3d, 4 for 4d ...).
Definition defs.h:105
unsigned int geo_index_t
Represents indices.
Definition defs.h:133
int geo_signed_index_t
Represents possibly negative indices.
Definition defs.h:139
Common include file, providing basic definitions. Should be included before anything else by all head...
float float32
Definition numeric.h:148
uint8_t uint8
Definition numeric.h:136
uint64_t uint64
Definition numeric.h:145
int8_t int8
Definition numeric.h:124
double float64
Definition numeric.h:151
int32_t int32
Definition numeric.h:130
void * pointer
Definition numeric.h:121
float64 max_float64()
Gets 64 bits float maximum positive value.
Definition numeric.h:173
float32 max_float32()
Gets 32 bits float maximum positive value.
Definition numeric.h:156
uint16_t uint16
Definition numeric.h:139
float32 random_float32()
Returns a 32 bits float between 0 and 1.
uint32_t uint32
Definition numeric.h:142
Sign ratio_compare(const T &a_num, const T &a_denom, const T &b_num, const T &b_denom)
Compares two rational numbers given as separate numerators and denominators.
Definition numeric.h:279
float64 random_float64()
Returns a 64 bits float between 0 and 1.
void optimize_number_representation(T &x)
place holder for optimizing internal number representation
Definition numeric.h:268
void random_reset()
Resets the random number generator.
int32 random_int32()
Returns a 32 bits integer between 0 and RAND_MAX.
float64 min_float64()
Gets 64 bits float minimum negative value.
Definition numeric.h:180
float32 min_float32()
Gets 32 bits float minimum negative value.
Definition numeric.h:163
bool is_nan(float32 x)
Checks whether a 32 bits float is "not a number".
int16_t int16
Definition numeric.h:127
int64_t int64
Definition numeric.h:133
Global Vorpaline namespace.
Definition basic.h:55
signed_index_t min_signed_index_t()
Gets the minimum negative value of type signed_index_t.
Definition numeric.h:356
T geo_sqr(T x)
Gets the square value of a value.
Definition numeric.h:302
index_t max_index_t()
Gets the maximum positive value of type index_t.
Definition numeric.h:335
void geo_argused(const T &)
Suppresses compiler warnings about unused parameters.
Definition argused.h:60
geo_signed_index_t signed_index_t
The type for storing and manipulating indices differences.
Definition numeric.h:344
void geo_clamp(T &x, T min, T max)
Clamps a value to a range.
Definition numeric.h:315
Sign geo_sgn(const T &x)
Gets the sign of a value.
Definition numeric.h:107
geo_index_t index_t
The type for storing and manipulating indices.
Definition numeric.h:330
Sign
Integer constants that represent the sign of a value.
Definition numeric.h:69
@ ZERO
Definition numeric.h:73
@ NEGATIVE
Definition numeric.h:71
@ POSITIVE
Definition numeric.h:75
Sign geo_cmp(const T &a, const T &b)
Compares two values.
Definition numeric.h:89
signed_index_t max_signed_index_t()
Gets the maximum positive value of type signed_index_t.
Definition numeric.h:349
double round(double x)
Definition numeric.h:369
geo_coord_index_t coord_index_t
The type for storing coordinate indices, and iterating on the coordinates of a point.
Definition numeric.h:364
Limits helper class that extends std::numeric_limits.
Definition numeric.h:232
Extends std::numeric_limits with additional information.
Definition numeric.h:261
type traits for scalars
Definition numeric.h:390