Algebra3D
Algebra for 3D transformations
 All Classes Namespaces Files Functions Variables Typedefs Macros
global.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Jaroslaw Glowacki
3  * jarek (dot) glowacki (at) gmail (dot) com
4  *
5  * This file is part of algebra3d.
6  *
7  * algebra3d is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * algebra3d is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with algebra3d. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
27 #ifndef _ALGEBRA3D_GLOBAL_H_
28 #define _ALGEBRA3D_GLOBAL_H_
29 
30 #include<limits>
31 #include<cmath>
32 #include<ostream>
33 #include<istream>
34 
43 #define _PRECISION(FP_TYPE) ( \
44  (sizeof(FP_TYPE) == sizeof(float)) * 1.0e-5 \
45  + (sizeof(FP_TYPE) > sizeof(float)) * 1.0e-12\
46  )
47 
51 #define PRECISION _PRECISION(fpType)
52 
55 #define PRECISION_D _PRECISION(double)
56 
59 #define PRECISION_F _PRECISION(float)
60 
64 #define _INF(FP_TYPE) (std::numeric_limits<FP_TYPE>::infinity())
65 
69 #define INF _INF(fpType)
70 
73 #define INF_D _INF(double)
74 
77 #define INF_F _INF(float)
78 
83 #define N_A_N (INF - INF)
84 
87 #define N_A_N_D (INF_D - INF_D)
88 
91 #define N_A_N_F (INF_F - INF_F)
92 
97 namespace algebra3d {
98 
107 template<class fpType>
108 inline int sign(const fpType &val) {
109  // A branchless version
110  return (val > 0) - (val < 0);
111 }
112 
121 template<class fpType>
122 inline int signStar(const fpType &val) {
123  // A branchless version
124  return (val >= 0) - (val < 0);
125 }
126 
136 template<class fpType>
137 inline bool isInf(const fpType &number) {
138  return number == std::numeric_limits<fpType>::infinity()
139  || -number == std::numeric_limits<fpType>::infinity();
140 }
141 
151 template<class fpType>
152 inline bool isNaN(const fpType &number) {
153  return number != number;
154 }
155 
174 template<class fpType>
175 inline int compare(const fpType &d1, const fpType&d2, const fpType &precision =
176 PRECISION) {
177  // A branchless version
178  return (isNaN<fpType>(d1) || isNaN<fpType>(d2)) * 2 + (d1 - d2 > precision)
179  - (d2 - d1 > precision);
180 }
181 
193 template<class fpType>
194 fpType normalizeAngle(const fpType &angle);
195 
203 template<class fpType>
204 inline fpType getAngleFromSinCose(fpType sinAlpha, fpType cosAlpha) {
205  return acos(cosAlpha) * ((sinAlpha > 0.0) - (sinAlpha < 0.0));
206 }
207 
208 } /* namespace algebra3d */
209 
210 #endif /* _ALGEBRA3D_GLOBAL_H_ */
fpType normalizeAngle(const fpType &angle)
bool isInf(const fpType &number)
Definition: global.h:137
int compare(const fpType &d1, const fpType &d2, const fpType &precision=PRECISION)
Definition: global.h:175
#define PRECISION
Definition: global.h:51
int signStar(const fpType &val)
Definition: global.h:122
bool isNaN(const fpType &number)
Definition: global.h:152
fpType getAngleFromSinCose(fpType sinAlpha, fpType cosAlpha)
Definition: global.h:204
int sign(const fpType &val)
Definition: global.h:108