diff --git a/common/dialogs/dialog_page_settings.cpp b/common/dialogs/dialog_page_settings.cpp index 71db4ad19b..629b663cfa 100644 --- a/common/dialogs/dialog_page_settings.cpp +++ b/common/dialogs/dialog_page_settings.cpp @@ -31,7 +31,7 @@ #include #include #include -#include // for KiROUND, Clamp +#include // for KiROUND #include #include #include @@ -602,8 +602,8 @@ void DIALOG_PAGES_SETTINGS::UpdateDrawingSheetExample() { int lyWidth, lyHeight; - VECTOR2D clamped_layout_size( Clamp( (double)MIN_PAGE_SIZE_MILS, m_layout_size.x, m_maxPageSizeMils.x ), - Clamp( (double)MIN_PAGE_SIZE_MILS, m_layout_size.y, m_maxPageSizeMils.y ) ); + VECTOR2D clamped_layout_size( std::clamp( m_layout_size.x, (double)MIN_PAGE_SIZE_MILS, m_maxPageSizeMils.x ), + std::clamp( m_layout_size.y, (double)MIN_PAGE_SIZE_MILS, m_maxPageSizeMils.y ) ); double lyRatio = clamped_layout_size.x < clamped_layout_size.y ? (double) clamped_layout_size.y / clamped_layout_size.x : @@ -775,8 +775,8 @@ void DIALOG_PAGES_SETTINGS::GetCustomSizeMilsFromDialog() double customSizeY = (double) m_customSizeY.GetDoubleValue() / m_iuPerMils; // Ensure layout size can be converted to int coordinates later - customSizeX = Clamp( double( INT_MIN ), customSizeX, double( INT_MAX ) ); - customSizeY = Clamp( double( INT_MIN ), customSizeY, double( INT_MAX ) ); + customSizeX = std::clamp( customSizeX, double( INT_MIN ), double( INT_MAX ) ); + customSizeY = std::clamp( customSizeY, double( INT_MIN ), double( INT_MAX ) ); m_layout_size = VECTOR2D( customSizeX, customSizeY ); } diff --git a/common/drawing_sheet/drawing_sheet_parser.cpp b/common/drawing_sheet/drawing_sheet_parser.cpp index 03018bc302..b29c70f9fe 100644 --- a/common/drawing_sheet/drawing_sheet_parser.cpp +++ b/common/drawing_sheet/drawing_sheet_parser.cpp @@ -792,7 +792,7 @@ void DRAWING_SHEET_PARSER::parseText( DS_DATA_ITEM_TEXT* aItem ) aItem->m_TextColor.r = parseInt( 0, 255 ) / 255.0; aItem->m_TextColor.g = parseInt( 0, 255 ) / 255.0; aItem->m_TextColor.b = parseInt( 0, 255 ) / 255.0; - aItem->m_TextColor.a = Clamp( parseDouble(), 0.0, 1.0 ); + aItem->m_TextColor.a = std::clamp( parseDouble(), 0.0, 1.0 ); NeedRIGHT(); break; diff --git a/common/io/altium/altium_props_utils.cpp b/common/io/altium/altium_props_utils.cpp index a93c0227d9..43c69fc737 100644 --- a/common/io/altium/altium_props_utils.cpp +++ b/common/io/altium/altium_props_utils.cpp @@ -37,7 +37,7 @@ int32_t ALTIUM_PROPS_UTILS::ConvertToKicadUnit( const double aValue ) { constexpr double int_limit = ( std::numeric_limits::max() - 10 ) / 2.54; - int32_t iu = KiROUND( Clamp( -int_limit, aValue, int_limit ) * 2.54 ); + int32_t iu = KiROUND( std::clamp( aValue, -int_limit, int_limit ) * 2.54 ); // Altium's internal precision is 0.1uinch. KiCad's is 1nm. Round to nearest 10nm to clean // up most rounding errors. This allows lossless conversion of increments of 0.05mils and diff --git a/common/lset.cpp b/common/lset.cpp index 668d5e3f08..dd85eb113b 100644 --- a/common/lset.cpp +++ b/common/lset.cpp @@ -27,10 +27,9 @@ #include #include #include // for string, endl, basic_ost... -#include // for size_t +#include // for size_t #include -#include // for Clamp #include // for PCB_LAYER_ID #include #include // for arrayDim @@ -742,7 +741,7 @@ LSET LSET::AllCuMask( int aCuLayerCount ) LSET ret = all; int clear_count = MAX_CU_LAYERS - aCuLayerCount; - clear_count = Clamp( 0, clear_count, MAX_CU_LAYERS - 2 ); + clear_count = std::clamp( clear_count, 0, MAX_CU_LAYERS - 2 ); for( int elem = In30_Cu; clear_count; --elem, --clear_count ) ret.set( elem, false ); diff --git a/common/stroke_params.cpp b/common/stroke_params.cpp index 45c65c709d..16440a059a 100644 --- a/common/stroke_params.cpp +++ b/common/stroke_params.cpp @@ -311,7 +311,7 @@ void STROKE_PARAMS_PARSER::ParseStroke( STROKE_PARAMS& aStroke ) color.r = parseInt( "red" ) / 255.0; color.g = parseInt( "green" ) / 255.0; color.b = parseInt( "blue" ) / 255.0; - color.a = Clamp( parseDouble( "alpha" ), 0.0, 1.0 ); + color.a = std::clamp( parseDouble( "alpha" ), 0.0, 1.0 ); aStroke.SetColor( color ); NeedRIGHT(); diff --git a/eeschema/dialogs/dialog_symbol_fields_table.cpp b/eeschema/dialogs/dialog_symbol_fields_table.cpp index 1e1667d867..348f5a5340 100644 --- a/eeschema/dialogs/dialog_symbol_fields_table.cpp +++ b/eeschema/dialogs/dialog_symbol_fields_table.cpp @@ -421,7 +421,7 @@ void DIALOG_SYMBOL_FIELDS_TABLE::SetupAllColumnProperties() int textWidth = m_dataModel->GetDataWidth( col ) + COLUMN_MARGIN; int maxWidth = defaultDlgSize.x / 3; - m_grid->SetColSize( col, Clamp( 100, textWidth, maxWidth ) ); + m_grid->SetColSize( col, std::clamp( textWidth, 100, maxWidth ) ); } } else diff --git a/eeschema/sch_io/altium/altium_parser_sch.cpp b/eeschema/sch_io/altium/altium_parser_sch.cpp index 1be8ce3c72..b9737708a2 100644 --- a/eeschema/sch_io/altium/altium_parser_sch.cpp +++ b/eeschema/sch_io/altium/altium_parser_sch.cpp @@ -50,7 +50,7 @@ constexpr int Altium2KiCadUnit( const int val, const int frac ) double dbase = 10 * schIUScale.MilsToIU( val ); double dfrac = schIUScale.MilsToIU( frac ) / 10000.0; - return KiROUND( Clamp( -int_limit, ( dbase + dfrac ) / 10.0, int_limit ) ) * 10; + return KiROUND( std::clamp( ( dbase + dfrac ) / 10.0, -int_limit, int_limit ) ) * 10; } diff --git a/eeschema/sch_io/kicad_sexpr/sch_io_kicad_sexpr_parser.cpp b/eeschema/sch_io/kicad_sexpr/sch_io_kicad_sexpr_parser.cpp index fee2487525..ec0f27a08d 100644 --- a/eeschema/sch_io/kicad_sexpr/sch_io_kicad_sexpr_parser.cpp +++ b/eeschema/sch_io/kicad_sexpr/sch_io_kicad_sexpr_parser.cpp @@ -38,7 +38,7 @@ #include #include #include -#include // KiROUND, Clamp +#include // KiROUND #include #include #include @@ -610,7 +610,7 @@ int SCH_IO_KICAD_SEXPR_PARSER::parseInternalUnits() // the system. Limit values to the largest that can be displayed on the screen. constexpr double int_limit = std::numeric_limits::max() * 0.7071; // 0.7071 = roughly 1/sqrt(2) - return KiROUND( Clamp( -int_limit, retval, int_limit ) ); + return KiROUND( std::clamp( retval, -int_limit, int_limit ) ); } @@ -620,7 +620,7 @@ int SCH_IO_KICAD_SEXPR_PARSER::parseInternalUnits( const char* aExpected ) constexpr double int_limit = std::numeric_limits::max() * 0.7071; - return KiROUND( Clamp( -int_limit, retval, int_limit ) ); + return KiROUND( std::clamp( retval, -int_limit, int_limit ) ); } @@ -676,7 +676,7 @@ void SCH_IO_KICAD_SEXPR_PARSER::parseFill( FILL_PARAMS& aFill ) color.r = parseInt( "red" ) / 255.0; color.g = parseInt( "green" ) / 255.0; color.b = parseInt( "blue" ) / 255.0; - color.a = Clamp( parseDouble( "alpha" ), 0.0, 1.0 ); + color.a = std::clamp( parseDouble( "alpha" ), 0.0, 1.0 ); aFill.m_Color = color; NeedRIGHT(); break; @@ -761,7 +761,7 @@ void SCH_IO_KICAD_SEXPR_PARSER::parseEDA_TEXT( EDA_TEXT* aText, bool aConvertOve color.r = parseInt( "red" ) / 255.0; color.g = parseInt( "green" ) / 255.0; color.b = parseInt( "blue" ) / 255.0; - color.a = Clamp( parseDouble( "alpha" ), 0.0, 1.0 ); + color.a = std::clamp( parseDouble( "alpha" ), 0.0, 1.0 ); aText->SetTextColor( color ); NeedRIGHT(); break; @@ -3592,7 +3592,7 @@ SCH_JUNCTION* SCH_IO_KICAD_SEXPR_PARSER::parseJunction() color.r = parseInt( "red" ) / 255.0; color.g = parseInt( "green" ) / 255.0; color.b = parseInt( "blue" ) / 255.0; - color.a = Clamp( parseDouble( "alpha" ), 0.0, 1.0 ); + color.a = std::clamp( parseDouble( "alpha" ), 0.0, 1.0 ); junction->SetColor( color ); NeedRIGHT(); diff --git a/libs/kimath/include/math/util.h b/libs/kimath/include/math/util.h index 783b6aaf0c..705cc0b389 100644 --- a/libs/kimath/include/math/util.h +++ b/libs/kimath/include/math/util.h @@ -51,27 +51,6 @@ void kimathLogDebug( const char* aFormatString, ... ); */ void kimathLogOverflow( double v, const char* aTypeName ); -/** - * Limit @a value within the range @a lower <= @a value <= @a upper. - * - * It will work on temporary expressions, since they are evaluated only once, and it should - * work on most if not all numeric types, string types, or any type for which "operator < ()" - * is present. The arguments are accepted in this order so you can remember the expression as - * a memory aid: - *

- * result is: lower <= value <= upper - *

- */ -template inline constexpr T Clamp( const T& lower, const T& value, const T& upper ) -{ - assert( upper >= lower ); - - if( value < lower ) - return lower; - else if( upper < value ) - return upper; - return value; -} // Suppress an annoying warning that the explicit rounding we do is not precise #ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION diff --git a/libs/kimath/include/math/vector2d.h b/libs/kimath/include/math/vector2d.h index be2697fce0..7eb604a8b7 100644 --- a/libs/kimath/include/math/vector2d.h +++ b/libs/kimath/include/math/vector2d.h @@ -28,6 +28,7 @@ #ifndef VECTOR2D_H_ #define VECTOR2D_H_ +#include #include #include #include @@ -97,16 +98,16 @@ public: CastingType minI = static_cast( std::numeric_limits::min() ); CastingType maxI = static_cast( std::numeric_limits::max() ); - x = static_cast( Clamp( minI, aVec.x, maxI ) ); - y = static_cast( Clamp( minI, aVec.y, maxI ) ); + x = static_cast( std::clamp( aVec.x, minI, maxI ) ); + y = static_cast( std::clamp( aVec.y, minI, maxI ) ); } else if( std::is_integral() && std::is_integral() ) { int64_t minI = static_cast( std::numeric_limits::min() ); int64_t maxI = static_cast( std::numeric_limits::max() ); - x = static_cast( Clamp( minI, static_cast( aVec.x ), maxI ) ); - y = static_cast( Clamp( minI, static_cast(aVec.y), maxI ) ); + x = static_cast( std::clamp( static_cast( aVec.x ), minI, maxI ) ); + y = static_cast( std::clamp( static_cast( aVec.y ), minI, maxI ) ); } else { @@ -134,17 +135,19 @@ public: { T minI = static_cast( std::numeric_limits::min() ); T maxI = static_cast( std::numeric_limits::max() ); - return VECTOR2( static_cast( Clamp( minI, x, maxI ) ), - static_cast( Clamp( minI, y, maxI ) ) ); + return VECTOR2( static_cast( std::clamp( x, minI, maxI ) ), + static_cast( std::clamp( y, minI, maxI ) ) ); } else if( std::is_integral() && std::is_integral() ) { int64_t minI = static_cast( std::numeric_limits::min() ); int64_t maxI = static_cast( std::numeric_limits::max() ); + int64_t x64 = static_cast( x ); + int64_t y64 = static_cast( y ); return VECTOR2( - static_cast( Clamp( minI, static_cast( x ), maxI ) ), - static_cast( Clamp( minI, static_cast( y ), maxI ) ) ); + static_cast( std::clamp( x64, minI, maxI ) ), + static_cast( std::clamp( y64, minI, maxI ) ) ); } else { diff --git a/libs/kimath/src/trigo.cpp b/libs/kimath/src/trigo.cpp index 3232803c8d..1280658c3e 100644 --- a/libs/kimath/src/trigo.cpp +++ b/libs/kimath/src/trigo.cpp @@ -27,8 +27,9 @@ * @brief Trigonometric and geometric basic functions. */ +#include // for std::clamp #include // for numeric_limits -#include // for abs +#include // for abs #include // for swap #include @@ -526,13 +527,13 @@ const VECTOR2I CalcArcCenter( const VECTOR2I& aStart, const VECTOR2I& aMid, cons VECTOR2I iCenter; - iCenter.x = KiROUND( Clamp( double( std::numeric_limits::min() + 100 ), - dCenter.x, - double( std::numeric_limits::max() - 100 ) ) ); + iCenter.x = KiROUND( std::clamp( dCenter.x, + double( std::numeric_limits::min() + 100 ), + double( std::numeric_limits::max() - 100 ) ) ); - iCenter.y = KiROUND( Clamp( double( std::numeric_limits::min() + 100 ), - dCenter.y, - double( std::numeric_limits::max() - 100 ) ) ); + iCenter.y = KiROUND( std::clamp( dCenter.y, + double( std::numeric_limits::min() + 100 ), + double( std::numeric_limits::max() - 100 ) ) ); return iCenter; } diff --git a/pcbnew/dialogs/panel_setup_constraints.cpp b/pcbnew/dialogs/panel_setup_constraints.cpp index d8451a263d..79ed9a4087 100644 --- a/pcbnew/dialogs/panel_setup_constraints.cpp +++ b/pcbnew/dialogs/panel_setup_constraints.cpp @@ -21,6 +21,8 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ +#include + #include #include #include @@ -143,9 +145,9 @@ bool PANEL_SETUP_CONSTRAINTS::TransferDataFromWindow() m_BrdSettings->m_UseHeightForLengthCalcs = m_useHeightForLengthCalcs->GetValue(); - m_BrdSettings->m_MaxError = Clamp( pcbIUScale.IU_PER_MM * MINIMUM_ERROR_SIZE_MM, - m_maxError.GetValue(), - pcbIUScale.IU_PER_MM * MAXIMUM_ERROR_SIZE_MM ); + m_BrdSettings->m_MaxError = KiROUND( std::clamp( static_cast( m_maxError.GetValue() ), + pcbIUScale.IU_PER_MM * MINIMUM_ERROR_SIZE_MM, + pcbIUScale.IU_PER_MM * MAXIMUM_ERROR_SIZE_MM ) ); m_BrdSettings->m_ZoneKeepExternalFillets = m_allowExternalFilletsOpt->GetValue(); m_BrdSettings->m_MinResolvedSpokes = m_minResolvedSpokeCountCtrl->GetValue(); diff --git a/pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp b/pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp index 8b2614eae3..de06b98af7 100644 --- a/pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp +++ b/pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp @@ -203,7 +203,7 @@ int PCB_IO_KICAD_SEXPR_PARSER::parseBoardUnits() // N.B. we currently represent board units as integers. Any values that are // larger or smaller than those board units represent undefined behavior for // the system. We limit values to the largest that is visible on the screen - return KiROUND( Clamp( -INT_LIMIT, retval, INT_LIMIT ) ); + return KiROUND( std::clamp( retval, -INT_LIMIT, INT_LIMIT ) ); } @@ -214,7 +214,7 @@ int PCB_IO_KICAD_SEXPR_PARSER::parseBoardUnits( const char* aExpected ) // N.B. we currently represent board units as integers. Any values that are // larger or smaller than those board units represent undefined behavior for // the system. We limit values to the largest that is visible on the screen - return KiROUND( Clamp( -INT_LIMIT, retval, INT_LIMIT ) ); + return KiROUND( std::clamp( retval, -INT_LIMIT, INT_LIMIT ) ); } diff --git a/pcbnew/pcb_plot_params.cpp b/pcbnew/pcb_plot_params.cpp index 5100242069..dca742e5cf 100644 --- a/pcbnew/pcb_plot_params.cpp +++ b/pcbnew/pcb_plot_params.cpp @@ -26,7 +26,6 @@ #include #include #include -#include // for KiROUND #include #include #include @@ -167,7 +166,7 @@ void PCB_PLOT_PARAMS::SetGerberPrecision( int aPrecision ) void PCB_PLOT_PARAMS::SetSvgPrecision( unsigned aPrecision ) { - m_svgPrecision = Clamp( SVG_PRECISION_MIN, aPrecision, SVG_PRECISION_MAX ); + m_svgPrecision = std::clamp( aPrecision, SVG_PRECISION_MIN, SVG_PRECISION_MAX ); } diff --git a/pcbnew/plot_brditems_plotter.cpp b/pcbnew/plot_brditems_plotter.cpp index 724dd51829..8dd3eeb534 100644 --- a/pcbnew/plot_brditems_plotter.cpp +++ b/pcbnew/plot_brditems_plotter.cpp @@ -32,7 +32,7 @@ #include #include #include -#include // for KiROUND, Clamp +#include // for KiROUND #include // for VECTOR2I #include #include @@ -1094,12 +1094,12 @@ void BRDITEMS_PLOTTER::plotOneDrillMark( PAD_DRILL_SHAPE aDrillShape, const VECT // Round holes only have x diameter, slots have both drillSize.x -= getFineWidthAdj(); - drillSize.x = Clamp( 1, drillSize.x, aPadSize.x - 1 ); + drillSize.x = std::clamp( drillSize.x, 1, aPadSize.x - 1 ); if( aDrillShape == PAD_DRILL_SHAPE::OBLONG ) { drillSize.y -= getFineWidthAdj(); - drillSize.y = Clamp( 1, drillSize.y, aPadSize.y - 1 ); + drillSize.y = std::clamp( drillSize.y, 1, aPadSize.y - 1 ); m_plotter->FlashPadOval( aDrillPos, drillSize, aOrientation, GetPlotMode(), nullptr ); }