7
mirror of https://gitlab.com/kicad/code/kicad.git synced 2025-04-21 00:21:25 +00:00

Replace custom Clamp with std::clamp

Fixes bug in parser that had the elements in the wrong order due to our
custom version not matching the standard order
This commit is contained in:
Seth Hillbrand 2024-07-25 19:17:17 +02:00
parent 3f3505570a
commit 69849ba3ca
15 changed files with 48 additions and 65 deletions

View File

@ -31,7 +31,7 @@
#include <gr_basic.h>
#include <kiface_base.h>
#include <macros.h>
#include <math/util.h> // for KiROUND, Clamp
#include <math/util.h> // for KiROUND
#include <project.h>
#include <tool/actions.h>
#include <tool/tool_manager.h>
@ -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 );
}

View File

@ -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;

View File

@ -37,7 +37,7 @@ int32_t ALTIUM_PROPS_UTILS::ConvertToKicadUnit( const double aValue )
{
constexpr double int_limit = ( std::numeric_limits<int>::max() - 10 ) / 2.54;
int32_t iu = KiROUND( Clamp<double>( -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

View File

@ -27,10 +27,9 @@
#include <cassert>
#include <cstdarg>
#include <iostream> // for string, endl, basic_ost...
#include <stddef.h> // for size_t
#include <cstddef> // for size_t
#include <core/arraydim.h>
#include <math/util.h> // for Clamp
#include <layer_ids.h> // for PCB_LAYER_ID
#include <lseq.h>
#include <macros.h> // 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 );

View File

@ -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();

View File

@ -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

View File

@ -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<double>( -int_limit, ( dbase + dfrac ) / 10.0, int_limit ) ) * 10;
return KiROUND( std::clamp( ( dbase + dfrac ) / 10.0, -int_limit, int_limit ) ) * 10;
}

View File

@ -38,7 +38,7 @@
#include <base_units.h>
#include <lib_id.h>
#include <sch_pin.h>
#include <math/util.h> // KiROUND, Clamp
#include <math/util.h> // KiROUND
#include <font/font.h>
#include <font/fontconfig.h>
#include <pgm_base.h>
@ -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<int>::max() * 0.7071; // 0.7071 = roughly 1/sqrt(2)
return KiROUND( Clamp<double>( -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<int>::max() * 0.7071;
return KiROUND( Clamp<double>( -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();

View File

@ -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:
* <p>
* result is: lower <= value <= upper
*</p>
*/
template <typename T> 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

View File

@ -28,6 +28,7 @@
#ifndef VECTOR2D_H_
#define VECTOR2D_H_
#include <algorithm>
#include <limits>
#include <iostream>
#include <sstream>
@ -97,16 +98,16 @@ public:
CastingType minI = static_cast<CastingType>( std::numeric_limits<T>::min() );
CastingType maxI = static_cast<CastingType>( std::numeric_limits<T>::max() );
x = static_cast<T>( Clamp( minI, aVec.x, maxI ) );
y = static_cast<T>( Clamp( minI, aVec.y, maxI ) );
x = static_cast<T>( std::clamp( aVec.x, minI, maxI ) );
y = static_cast<T>( std::clamp( aVec.y, minI, maxI ) );
}
else if( std::is_integral<T>() && std::is_integral<CastingType>() )
{
int64_t minI = static_cast<int64_t>( std::numeric_limits<T>::min() );
int64_t maxI = static_cast<int64_t>( std::numeric_limits<T>::max() );
x = static_cast<T>( Clamp( minI, static_cast<int64_t>( aVec.x ), maxI ) );
y = static_cast<T>( Clamp( minI, static_cast<int64_t>(aVec.y), maxI ) );
x = static_cast<T>( std::clamp( static_cast<int64_t>( aVec.x ), minI, maxI ) );
y = static_cast<T>( std::clamp( static_cast<int64_t>( aVec.y ), minI, maxI ) );
}
else
{
@ -134,17 +135,19 @@ public:
{
T minI = static_cast<T>( std::numeric_limits<U>::min() );
T maxI = static_cast<T>( std::numeric_limits<U>::max() );
return VECTOR2<U>( static_cast<U>( Clamp( minI, x, maxI ) ),
static_cast<U>( Clamp( minI, y, maxI ) ) );
return VECTOR2<U>( static_cast<U>( std::clamp( x, minI, maxI ) ),
static_cast<U>( std::clamp( y, minI, maxI ) ) );
}
else if( std::is_integral<T>() && std::is_integral<U>() )
{
int64_t minI = static_cast<int64_t>( std::numeric_limits<U>::min() );
int64_t maxI = static_cast<int64_t>( std::numeric_limits<U>::max() );
int64_t x64 = static_cast<int64_t>( x );
int64_t y64 = static_cast<int64_t>( y );
return VECTOR2<U>(
static_cast<U>( Clamp( minI, static_cast<int64_t>( x ), maxI ) ),
static_cast<U>( Clamp( minI, static_cast<int64_t>( y ), maxI ) ) );
static_cast<U>( std::clamp( x64, minI, maxI ) ),
static_cast<U>( std::clamp( y64, minI, maxI ) ) );
}
else
{

View File

@ -27,8 +27,9 @@
* @brief Trigonometric and geometric basic functions.
*/
#include <algorithm> // for std::clamp
#include <limits> // for numeric_limits
#include <stdlib.h> // for abs
#include <cstdlib> // for abs
#include <type_traits> // for swap
#include <geometry/seg.h>
@ -526,13 +527,13 @@ const VECTOR2I CalcArcCenter( const VECTOR2I& aStart, const VECTOR2I& aMid, cons
VECTOR2I iCenter;
iCenter.x = KiROUND( Clamp<double>( double( std::numeric_limits<int>::min() + 100 ),
dCenter.x,
double( std::numeric_limits<int>::max() - 100 ) ) );
iCenter.x = KiROUND( std::clamp( dCenter.x,
double( std::numeric_limits<int>::min() + 100 ),
double( std::numeric_limits<int>::max() - 100 ) ) );
iCenter.y = KiROUND( Clamp<double>( double( std::numeric_limits<int>::min() + 100 ),
dCenter.y,
double( std::numeric_limits<int>::max() - 100 ) ) );
iCenter.y = KiROUND( std::clamp( dCenter.y,
double( std::numeric_limits<int>::min() + 100 ),
double( std::numeric_limits<int>::max() - 100 ) ) );
return iCenter;
}

View File

@ -21,6 +21,8 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <algorithm>
#include <board_design_settings.h>
#include <board.h>
#include <math/util.h>
@ -143,9 +145,9 @@ bool PANEL_SETUP_CONSTRAINTS::TransferDataFromWindow()
m_BrdSettings->m_UseHeightForLengthCalcs = m_useHeightForLengthCalcs->GetValue();
m_BrdSettings->m_MaxError = Clamp<int>( 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<double>( 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();

View File

@ -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<double>( -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<double>( -INT_LIMIT, retval, INT_LIMIT ) );
return KiROUND( std::clamp( retval, -INT_LIMIT, INT_LIMIT ) );
}

View File

@ -26,7 +26,6 @@
#include <layer_ids.h>
#include <lset.h>
#include <string_utils.h>
#include <math/util.h> // for KiROUND
#include <pcb_plot_params.h>
#include <pcb_plot_params_parser.h>
#include <plotters/plotter.h>
@ -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 );
}

View File

@ -32,7 +32,7 @@
#include <geometry/shape_segment.h>
#include <string_utils.h>
#include <macros.h>
#include <math/util.h> // for KiROUND, Clamp
#include <math/util.h> // for KiROUND
#include <math/vector2d.h> // for VECTOR2I
#include <plotters/plotter_gerber.h>
#include <trigo.h>
@ -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 );
}