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 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
59template <typename T> inline constexpr T Clamp( const T& lower, const T& value, const T& upper )
60{
61 if( value < lower )
62 return lower;
63 else if( upper < value )
64 return upper;
65 return value;
66}
67
68// Suppress an annoying warning that the explicit rounding we do is not precise
69#ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
70 _Pragma( "GCC diagnostic push" ) \
71 _Pragma( "GCC diagnostic ignored \"-Wimplicit-int-float-conversion\"" )
72#endif
73
79template <typename fp_type, typename ret_type = int>
80constexpr ret_type KiROUND( fp_type v )
81{
82 using max_ret = long long int;
83 fp_type ret = v < 0 ? v - 0.5 : v + 0.5;
84
85 if( std::numeric_limits<ret_type>::max() < ret ||
86 std::numeric_limits<ret_type>::lowest() > ret )
87 {
88 kimathLogDebug( "Overflow KiROUND converting value %f to %s", double( v ),
89 typeid( ret_type ).name() );
90 return 0;
91 }
92
93 return ret_type( max_ret( ret ) );
94}
95
96#ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
97 _Pragma( "GCC diagnostic pop" )
98#endif
99
104template <typename T>
105T rescale( T aNumerator, T aValue, T aDenominator )
106{
107 return aNumerator * aValue / aDenominator;
108}
109
110template <typename T>
111int sign( T val )
112{
113 return ( T( 0 ) < val) - ( val < T( 0 ) );
114}
115
116// explicit specializations for integer types, taking care of overflow.
117template <>
118int rescale( int aNumerator, int aValue, int aDenominator );
119
120template <>
121int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator );
122
123
132template <class T>
133typename std::enable_if<std::is_floating_point<T>::value, bool>::type
134equals( T aFirst, T aSecond, T aEpsilon = std::numeric_limits<T>::epsilon() )
135{
136 T diff = std::abs( aFirst - aSecond );
137
138 if( diff < aEpsilon )
139 {
140 return true;
141 }
142
143 aFirst = std::abs( aFirst );
144 aSecond = std::abs( aSecond );
145 T largest = aFirst > aSecond ? aFirst : aSecond;
146
147 if( diff <= largest * aEpsilon )
148 {
149 return true;
150 }
151
152 return false;
153}
154
155
156#endif // UTIL_H
const char * name
Definition: DXF_plotter.cpp:56
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:401
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:134
int sign(T val)
Definition: util.h:111
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:80
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition: util.h:105
void kimathLogDebug(const char *aFormatString,...)
Helper to avoid directly including wx/log.h for the templated functions in kimath.
Definition: util.cpp:43
constexpr T Clamp(const T &lower, const T &value, const T &upper)
Limit value within the range lower <= value <= upper.
Definition: util.h:59