KiCad PCB EDA Suite
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KICAD, a free EDA CAD application.
3 *
4 * Copyright (c) 2005 Michael Niedermayer <[email protected]>
5 * Copyright (C) CERN
6 * Copyright (C) 2021-2023 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * @author Tomasz Wlostowski <[email protected]>
9 *
10 * The equals() method to compare two floating point values adapted from
11 * AlmostEqualRelativeAndAbs() on
12 * https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
13 * (C) Bruce Dawson subject to the Apache 2.0 license.
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, you may find one here:
27 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
28 * or you may search the http://www.gnu.org website for the version 2 license,
29 * or you may write to the Free Software Foundation, Inc.,
30 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
31 */
32
33#ifndef UTIL_H
34#define UTIL_H
35
36#include <config.h>
37#include <cmath>
38#include <cstdint>
39#include <limits>
40#include <typeinfo>
41#include <type_traits>
42
46void kimathLogDebug( const char* aFormatString, ... );
47
51void kimathLogOverflow( double v, const char* aTypeName );
52
64template <typename T> inline constexpr T Clamp( const T& lower, const T& value, const T& upper )
65{
66 if( value < lower )
67 return lower;
68 else if( upper < value )
69 return upper;
70 return value;
71}
72
73// Suppress an annoying warning that the explicit rounding we do is not precise
74#ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
75 _Pragma( "GCC diagnostic push" ) \
76 _Pragma( "GCC diagnostic ignored \"-Wimplicit-int-float-conversion\"" )
77#endif
78
79
85template <typename in_type = long long int, typename ret_type = int>
86inline constexpr ret_type KiCheckedCast( in_type v )
87{
88 if constexpr( std::is_same_v<in_type, long long int> && std::is_same_v<ret_type, int> )
89 {
90 if( v > std::numeric_limits<int>::max() )
91 {
92 kimathLogOverflow( double( v ), typeid( int ).name() );
93
94 return std::numeric_limits<int>::max();
95 }
96 else if( v < std::numeric_limits<int>::lowest() )
97 {
98 kimathLogOverflow( double( v ), typeid( int ).name() );
99
100 return std::numeric_limits<int>::lowest();
101 }
102
103 return int( v );
104 }
105 else
106 {
107 return v;
108 }
109}
110
111
117template <typename fp_type, typename ret_type = int>
118constexpr ret_type KiROUND( fp_type v )
119{
120 using max_ret = long long int;
121 fp_type ret = v < 0 ? v - 0.5 : v + 0.5;
122
123 if( ret > std::numeric_limits<ret_type>::max() )
124 {
125 kimathLogOverflow( double( v ), typeid( ret_type ).name() );
126
127 return std::numeric_limits<ret_type>::max() - 1;
128 }
129 else if( ret < std::numeric_limits<ret_type>::lowest() )
130 {
131 kimathLogOverflow( double( v ), typeid( ret_type ).name() );
132
133 if( std::numeric_limits<ret_type>::is_signed )
134 return std::numeric_limits<ret_type>::lowest() + 1;
135 else
136 return 0;
137 }
138#if __cplusplus >= 202302L // isnan is not constexpr until C++23
139 else if constexpr( std::is_floating_point_v<fp_type> )
140 {
141 if( std::isnan( v ) )
142 {
143 kimathLogOverflow( double( v ), typeid( ret_type ).name() );
144
145 return 0;
146 }
147 }
148#endif
149
150 return ret_type( max_ret( ret ) );
151}
152
153#ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
154 _Pragma( "GCC diagnostic pop" )
155#endif
156
161template <typename T>
162T rescale( T aNumerator, T aValue, T aDenominator )
163{
164 return aNumerator * aValue / aDenominator;
165}
166
167template <typename T>
168int sign( T val )
169{
170 return ( T( 0 ) < val) - ( val < T( 0 ) );
171}
172
173// explicit specializations for integer types, taking care of overflow.
174template <>
175int rescale( int aNumerator, int aValue, int aDenominator );
176
177template <>
178int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator );
179
180
189template <class T>
190typename std::enable_if<std::is_floating_point<T>::value, bool>::type
191equals( T aFirst, T aSecond, T aEpsilon = std::numeric_limits<T>::epsilon() )
192{
193 T diff = std::abs( aFirst - aSecond );
194
195 if( diff < aEpsilon )
196 {
197 return true;
198 }
199
200 aFirst = std::abs( aFirst );
201 aSecond = std::abs( aSecond );
202 T largest = aFirst > aSecond ? aFirst : aSecond;
203
204 if( diff <= largest * aEpsilon )
205 {
206 return true;
207 }
208
209 return false;
210}
211
212
213#endif // UTIL_H
const char * name
Definition: DXF_plotter.cpp:57
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:424
std::enable_if< std::is_floating_point< T >::value, bool >::type equals(T aFirst, T aSecond, T aEpsilon=std::numeric_limits< T >::epsilon())
Template to compare two floating point values for equality within a required epsilon.
Definition: util.h:191
int sign(T val)
Definition: util.h:168
void kimathLogOverflow(double v, const char *aTypeName)
Workaround to avoid the empty-string conversion issue in wxWidgets.
Definition: util.cpp:58
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:118
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition: util.h:162
void kimathLogDebug(const char *aFormatString,...)
Helper to avoid directly including wx/log.h for the templated functions in kimath.
Definition: util.cpp:44
constexpr T Clamp(const T &lower, const T &value, const T &upper)
Limit value within the range lower <= value <= upper.
Definition: util.h:64
constexpr ret_type KiCheckedCast(in_type v)
Perform a cast between numerical types.
Definition: util.h:86