Geogram Version 1.8.5
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
51#ifndef M_PI
55#define M_PI 3.14159265358979323846
56#endif
57
63namespace GEO {
64
72 namespace Numeric {
73
75 typedef void* pointer;
76
78 typedef int8_t int8;
79
81 typedef int16_t int16;
82
84 typedef int32_t int32;
85
87 typedef int64_t int64;
88
90 typedef uint8_t uint8;
91
93 typedef uint16_t uint16;
94
96 typedef uint32_t uint32;
97
99 typedef uint64_t uint64;
100
102 typedef float float32;
103
105 typedef double float64;
106
111 return std::numeric_limits<float32>::max();
112 }
113
118 // Note: numeric_limits<>::min() is not
119 // what we want (it returns the smallest
120 // positive non-denormal).
121 return -max_float32();
122 }
123
128 return std::numeric_limits<float64>::max();
129 }
130
135 // Note: numeric_limits<>::min() is not
136 // what we want (it returns the smallest
137 // positive non-denormal).
138 return -max_float64();
139 }
140
144 bool GEOGRAM_API is_nan(float32 x);
145
149 bool GEOGRAM_API is_nan(float64 x);
150
154 void GEOGRAM_API random_reset();
155
159 int32 GEOGRAM_API random_int32();
160
164 float32 GEOGRAM_API random_float32();
165
169 float64 GEOGRAM_API random_float64();
170
185 template <class T, bool is_numeric>
186 struct LimitsHelper : std::numeric_limits<T> {
187 };
188
196 template <class T>
197 struct LimitsHelper<T, true> : std::numeric_limits<T> {
199 static const size_t size = sizeof(T);
201 static const size_t numbits = 8 * sizeof(T);
202 };
203
213 template <class T>
214 struct Limits :
215 LimitsHelper<T, std::numeric_limits<T>::is_specialized> {
216 };
217 }
218
219 /************************************************************************/
220
224 enum Sign {
228 ZERO = 0,
230 POSITIVE = 1
231 };
232
245 template <class T>
246 inline Sign geo_sgn(const T& x) {
247 return (x > 0) ? POSITIVE : (
248 (x < 0) ? NEGATIVE : ZERO
249 );
250 }
251
258 template <class T>
259 inline T geo_sqr(T x) {
260 return x * x;
261 }
262
271 template <class T>
272 inline void geo_clamp(T& x, T min, T max) {
273 if(x < min) {
274 x = min;
275 } else if(x > max) {
276 x = max;
277 }
278 }
279
288
293 return std::numeric_limits<index_t>::max();
294 }
295
302
307 return std::numeric_limits<signed_index_t>::max();
308 }
309
314 return std::numeric_limits<signed_index_t>::min();
315 }
316
322
326 inline double round(double x) {
327 return ((x - floor(x)) > 0.5 ? ceil(x) : floor(x));
328 }
329}
330
331#endif
332
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:102
uint8_t uint8
Definition numeric.h:90
uint64_t uint64
Definition numeric.h:99
int8_t int8
Definition numeric.h:78
double float64
Definition numeric.h:105
int32_t int32
Definition numeric.h:84
void * pointer
Definition numeric.h:75
float64 max_float64()
Gets 64 bits float maximum positive value.
Definition numeric.h:127
float32 max_float32()
Gets 32 bits float maximum positive value.
Definition numeric.h:110
uint16_t uint16
Definition numeric.h:93
float32 random_float32()
Returns a 32 bits float between 0 and 1.
uint32_t uint32
Definition numeric.h:96
float64 random_float64()
Returns a 64 bits float between 0 and 1.
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:134
float32 min_float32()
Gets 32 bits float minimum negative value.
Definition numeric.h:117
bool is_nan(float32 x)
Checks whether a 32 bits float is "not a number".
int16_t int16
Definition numeric.h:81
int64_t int64
Definition numeric.h:87
Global Vorpaline namespace.
Definition algorithm.h:64
signed_index_t min_signed_index_t()
Gets the minimum negative value of type signed_index_t.
Definition numeric.h:313
T geo_sqr(T x)
Gets the square value of a value.
Definition numeric.h:259
index_t max_index_t()
Gets the maximum positive value of type index_t.
Definition numeric.h:292
geo_signed_index_t signed_index_t
The type for storing and manipulating indices differences.
Definition numeric.h:301
void geo_clamp(T &x, T min, T max)
Clamps a value to a range.
Definition numeric.h:272
Sign geo_sgn(const T &x)
Gets the sign of a value.
Definition numeric.h:246
geo_index_t index_t
The type for storing and manipulating indices.
Definition numeric.h:287
Sign
Integer constants that represent the sign of a value.
Definition numeric.h:224
@ ZERO
Definition numeric.h:228
@ NEGATIVE
Definition numeric.h:226
@ POSITIVE
Definition numeric.h:230
signed_index_t max_signed_index_t()
Gets the maximum positive value of type signed_index_t.
Definition numeric.h:306
double round(double x)
Definition numeric.h:326
geo_coord_index_t coord_index_t
The type for storing coordinate indices, and iterating on the coordinates of a point.
Definition numeric.h:321
Limits helper class that extends std::numeric_limits.
Definition numeric.h:186
Extends std::numeric_limits with additional information.
Definition numeric.h:215