KiCad PCB EDA Suite
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-2022 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
84template <typename fp_type, typename ret_type = int>
85constexpr ret_type KiROUND( fp_type v )
86{
87 using max_ret = long long int;
88 fp_type ret = v < 0 ? v - 0.5 : v + 0.5;
89
90 if( ret > std::numeric_limits<ret_type>::max() )
91 {
92 kimathLogOverflow( double( v ), typeid( ret_type ).name() );
93
94 return std::numeric_limits<ret_type>::max() - 1;
95 }
96 else if( ret < std::numeric_limits<ret_type>::lowest() )
97 {
98 kimathLogOverflow( double( v ), typeid( ret_type ).name() );
99
100 if( std::numeric_limits<ret_type>::is_signed )
101 return std::numeric_limits<ret_type>::lowest() + 1;
102 else
103 return 0;
104 }
105
106 return ret_type( max_ret( ret ) );
107}
108
109#ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
110 _Pragma( "GCC diagnostic pop" )
111#endif
112
117template <typename T>
118T rescale( T aNumerator, T aValue, T aDenominator )
119{
120 return aNumerator * aValue / aDenominator;
121}
122
123template <typename T>
124int sign( T val )
125{
126 return ( T( 0 ) < val) - ( val < T( 0 ) );
127}
128
129// explicit specializations for integer types, taking care of overflow.
130template <>
131int rescale( int aNumerator, int aValue, int aDenominator );
132
133template <>
134int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator );
135
136
145template <class T>
146typename std::enable_if<std::is_floating_point<T>::value, bool>::type
147equals( T aFirst, T aSecond, T aEpsilon = std::numeric_limits<T>::epsilon() )
148{
149 T diff = std::abs( aFirst - aSecond );
150
151 if( diff < aEpsilon )
152 {
153 return true;
154 }
155
156 aFirst = std::abs( aFirst );
157 aSecond = std::abs( aSecond );
158 T largest = aFirst > aSecond ? aFirst : aSecond;
159
160 if( diff <= largest * aEpsilon )
161 {
162 return true;
163 }
164
165 return false;
166}
167
168
169#endif // UTIL_H
const char * name
Definition: DXF_plotter.cpp:56
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:412
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:147
int sign(T val)
Definition: util.h:124
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:85
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition: util.h:118
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