Skip to content

Commit

Permalink
More fixes for std::min/std::max template types to prevent potential …
Browse files Browse the repository at this point in the history
…build issue with Microsoft Visual Studio 2022
  • Loading branch information
fspindle committed Jan 5, 2024
1 parent 3f3e3f2 commit 50cb067
Show file tree
Hide file tree
Showing 65 changed files with 878 additions and 754 deletions.
20 changes: 10 additions & 10 deletions modules/core/include/visp3/core/vpImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1629,8 +1629,8 @@ template <class Type> Type vpImage<Type>::getValue(double i, double j) const
double rfrac = 1.0 - rratio;
double cfrac = 1.0 - cratio;

unsigned int iround_1 = (std::min)(height - 1, iround + 1);
unsigned int jround_1 = (std::min)(width - 1, jround + 1);
unsigned int iround_1 = std::min<unsigned int>(height - 1, iround + 1);
unsigned int jround_1 = std::min<unsigned int>(width - 1, jround + 1);

double value =
(static_cast<double>(row[iround][jround]) * rfrac + static_cast<double>(row[iround_1][jround]) * rratio) * cfrac +
Expand Down Expand Up @@ -1661,8 +1661,8 @@ template <> inline double vpImage<double>::getValue(double i, double j) const
double rfrac = 1.0 - rratio;
double cfrac = 1.0 - cratio;

unsigned int iround_1 = (std::min)(height - 1, iround + 1);
unsigned int jround_1 = (std::min)(width - 1, jround + 1);
unsigned int iround_1 = std::min<unsigned int>(height - 1, iround + 1);
unsigned int jround_1 = std::min<unsigned int>(width - 1, jround + 1);

return (row[iround][jround] * rfrac + row[iround_1][jround] * rratio) * cfrac +
(row[iround][jround_1] * rfrac + row[iround_1][jround_1] * rratio) * cratio;
Expand Down Expand Up @@ -1732,8 +1732,8 @@ template <> inline unsigned char vpImage<unsigned char>::getValue(double i, doub
double rfrac = 1.0 - rratio;
double cfrac = 1.0 - cratio;

unsigned int iround_1 = (std::min)(height - 1, iround + 1);
unsigned int jround_1 = (std::min)(width - 1, jround + 1);
unsigned int iround_1 = std::min<unsigned int>(height - 1, iround + 1);
unsigned int jround_1 = std::min<unsigned int>(width - 1, jround + 1);

double value =
(static_cast<double>(row[iround][jround]) * rfrac + static_cast<double>(row[iround_1][jround]) * rratio) * cfrac +
Expand Down Expand Up @@ -1764,8 +1764,8 @@ template <> inline vpRGBa vpImage<vpRGBa>::getValue(double i, double j) const
double rfrac = 1.0 - rratio;
double cfrac = 1.0 - cratio;

unsigned int iround_1 = (std::min)(height - 1, iround + 1);
unsigned int jround_1 = (std::min)(width - 1, jround + 1);
unsigned int iround_1 = std::min<unsigned int>(height - 1, iround + 1);
unsigned int jround_1 = std::min<unsigned int>(width - 1, jround + 1);

double valueR =
(static_cast<double>(row[iround][jround].R) * rfrac + static_cast<double>(row[iround_1][jround].R) * rratio) *
Expand Down Expand Up @@ -1808,8 +1808,8 @@ template <> inline vpRGBf vpImage<vpRGBf>::getValue(double i, double j) const
double rfrac = 1.0 - rratio;
double cfrac = 1.0 - cratio;

unsigned int iround_1 = (std::min)(height - 1, iround + 1);
unsigned int jround_1 = (std::min)(width - 1, jround + 1);
unsigned int iround_1 = std::min<unsigned int>(height - 1, iround + 1);
unsigned int jround_1 = std::min<unsigned int>(width - 1, jround + 1);

double valueR =
(static_cast<double>(row[iround][jround].R) * rfrac + static_cast<double>(row[iround_1][jround].R) * rratio) *
Expand Down
4 changes: 2 additions & 2 deletions modules/core/include/visp3/core/vpImageFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ class VISP_EXPORT vpImageFilter
float dx = static_cast<float>(dIx[r][c]);
float dy = static_cast<float>(dIy[r][c]);
float gradient = std::abs(dx) + std::abs(dy);
float gradientClamped = std::min(gradient, static_cast<float>(std::numeric_limits<unsigned char>::max()));
float gradientClamped = std::min<float>(gradient, static_cast<float>(std::numeric_limits<unsigned char>::max()));
dI[r][c] = static_cast<unsigned char>(gradientClamped);
}
}
Expand All @@ -334,7 +334,7 @@ class VISP_EXPORT vpImageFilter
break;
}
}
float upperThresh = std::max(bon, 1.f);
float upperThresh = std::max<float>(bon, 1.f);
lowerThresh = lowerThresholdRatio * bon;
return upperThresh;
}
Expand Down
28 changes: 14 additions & 14 deletions modules/core/include/visp3/core/vpImageTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ template <class Type>
void vpImageTools::crop(const vpImage<Type> &I, double roi_top, double roi_left, unsigned int roi_height,
unsigned int roi_width, vpImage<Type> &crop, unsigned int v_scale, unsigned int h_scale)
{
int i_min = (std::max)((int)(ceil(roi_top / v_scale)), 0);
int j_min = (std::max)((int)(ceil(roi_left / h_scale)), 0);
int i_max = (std::min)((int)(ceil((roi_top + roi_height)) / v_scale), (int)(I.getHeight() / v_scale));
int j_max = (std::min)((int)(ceil((roi_left + roi_width) / h_scale)), (int)(I.getWidth() / h_scale));
int i_min = std::max<int>((int)(ceil(roi_top / v_scale)), 0);
int j_min = std::max<int>((int)(ceil(roi_left / h_scale)), 0);
int i_max = std::min<int>((int)(ceil((roi_top + roi_height)) / v_scale), (int)(I.getHeight() / v_scale));
int j_max = std::min<int>((int)(ceil((roi_left + roi_width) / h_scale)), (int)(I.getWidth() / h_scale));

unsigned int i_min_u = (unsigned int)i_min;
unsigned int j_min_u = (unsigned int)j_min;
Expand Down Expand Up @@ -409,10 +409,10 @@ template <class Type>
void vpImageTools::crop(const unsigned char *bitmap, unsigned int width, unsigned int height, const vpRect &roi,
vpImage<Type> &crop, unsigned int v_scale, unsigned int h_scale)
{
int i_min = (std::max)((int)(ceil(roi.getTop() / v_scale)), 0);
int j_min = (std::max)((int)(ceil(roi.getLeft() / h_scale)), 0);
int i_max = (std::min)((int)(ceil((roi.getTop() + roi.getHeight()) / v_scale)), (int)(height / v_scale));
int j_max = (std::min)((int)(ceil((roi.getLeft() + roi.getWidth()) / h_scale)), (int)(width / h_scale));
int i_min = std::max<int>((int)(ceil(roi.getTop() / v_scale)), 0);
int j_min = std::max<int>((int)(ceil(roi.getLeft() / h_scale)), 0);
int i_max = std::min<int>((int)(ceil((roi.getTop() + roi.getHeight()) / v_scale)), (int)(height / v_scale));
int j_max = std::min<int>((int)(ceil((roi.getLeft() + roi.getWidth()) / h_scale)), (int)(width / h_scale));

unsigned int i_min_u = (unsigned int)i_min;
unsigned int j_min_u = (unsigned int)j_min;
Expand Down Expand Up @@ -892,8 +892,8 @@ template <class Type> Type vpImageTools::getPixelClamped(const vpImage<Type> &I,
{
int x = vpMath::round(u);
int y = vpMath::round(v);
x = (std::max)(0, (std::min)(x, static_cast<int>(I.getWidth()) - 1));
y = (std::max)(0, (std::min)(y, static_cast<int>(I.getHeight()) - 1));
x = std::max<int>(0, std::min<int>(x, static_cast<int>(I.getWidth()) - 1));
y = std::max<int>(0, std::min<int>(y, static_cast<int>(I.getHeight()) - 1));

return I[y][x];
}
Expand Down Expand Up @@ -994,11 +994,11 @@ void vpImageTools::resizeBilinear(const vpImage<Type> &I, vpImage<Type> &Ires, u
int u0 = static_cast<int>(u);
int v0 = static_cast<int>(v);

int u1 = (std::min)(static_cast<int>(I.getWidth()) - 1, u0 + 1);
int u1 = std::min<int>(static_cast<int>(I.getWidth()) - 1, u0 + 1);
int v1 = v0;

int u2 = u0;
int v2 = (std::min)(static_cast<int>(I.getHeight()) - 1, v0 + 1);
int v2 = std::min<int>(static_cast<int>(I.getHeight()) - 1, v0 + 1);

int u3 = u1;
int v3 = v2;
Expand All @@ -1017,11 +1017,11 @@ inline void vpImageTools::resizeBilinear(const vpImage<vpRGBa> &I, vpImage<vpRGB
int u0 = static_cast<int>(u);
int v0 = static_cast<int>(v);

int u1 = (std::min)(static_cast<int>(I.getWidth()) - 1, u0 + 1);
int u1 = std::min<int>(static_cast<int>(I.getWidth()) - 1, u0 + 1);
int v1 = v0;

int u2 = u0;
int v2 = (std::min)(static_cast<int>(I.getHeight()) - 1, v0 + 1);
int v2 = std::min<int>(static_cast<int>(I.getHeight()) - 1, v0 + 1);

int u3 = u1;
int v3 = v2;
Expand Down
4 changes: 2 additions & 2 deletions modules/core/include/visp3/core/vpMunkres.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ template <typename Type> inline vpMunkres::STEP_T vpMunkres::stepOne(std::vector
for (auto col = 0u; col < costs.size(); ++col) {
auto minval = std::numeric_limits<Type>::max();
for (const auto &cost_row : costs) {
minval = std::min(minval, cost_row.at(col));
minval = std::min<Type>(minval, cost_row.at(col));
}

for (auto &cost_row : costs) {
Expand Down Expand Up @@ -316,7 +316,7 @@ inline std::vector<std::pair<unsigned int, unsigned int> > vpMunkres::run(std::v
{
const auto original_row_size = costs.size();
const auto original_col_size = costs.front().size();
const auto sq_size = std::max(original_row_size, original_col_size);
const auto sq_size = std::max<Type>(original_row_size, original_col_size);

auto mask = std::vector<std::vector<vpMunkres::ZERO_T> >(
sq_size, std::vector<vpMunkres::ZERO_T>(sq_size, vpMunkres::ZERO_T::NA));
Expand Down
4 changes: 2 additions & 2 deletions modules/core/include/visp3/core/vpPixelMeterConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class VISP_EXPORT vpPixelMeterConversion
double scale = 1.0;
double r_d = sqrt(vpMath::sqr(x_d) + vpMath::sqr(y_d));

r_d = std::min(std::max(-M_PI, r_d), M_PI); // FOV restricted to 180degrees.
r_d = std::min<double>(std::max<double>(-M_PI, r_d), M_PI); // FOV restricted to 180degrees.

std::vector<double> k = cam.getKannalaBrandtDistortionCoefficients();

Expand Down Expand Up @@ -327,7 +327,7 @@ class VISP_EXPORT vpPixelMeterConversion
double scale = 1.0;
double r_d = sqrt(vpMath::sqr(x_d) + vpMath::sqr(y_d));

r_d = std::min(std::max(-M_PI, r_d), M_PI); // FOV restricted to 180degrees.
r_d = std::min<double>(std::max<double>(-M_PI, r_d), M_PI); // FOV restricted to 180degrees.

std::vector<double> k = cam.getKannalaBrandtDistortionCoefficients();

Expand Down
8 changes: 4 additions & 4 deletions modules/core/include/visp3/core/vpRect.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,10 @@ class VISP_EXPORT vpRect
*/
inline vpRect &operator&=(const vpRect &r)
{
double x1 = (std::max)(left, r.left);
double y1 = (std::max)(top, r.top);
width = (std::min)(left + width, r.left + r.width) - x1;
height = (std::min)(top + height, r.top + r.height) - y1;
double x1 = std::max<double>(left, r.left);
double y1 = std::max<double>(top, r.top);
width = std::min<double>(left + width, r.left + r.width) - x1;
height = std::min<double>(top + height, r.top + r.height) - y1;
left = x1;
top = y1;

Expand Down
6 changes: 3 additions & 3 deletions modules/core/src/display/vpDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ unsigned int vpDisplay::computeAutoScale(unsigned int width, unsigned int height
{
unsigned int screen_width, screen_height;
getScreenSize(screen_width, screen_height);
double wscale = (std::max)(1., ceil(2. * (double)width / (double)screen_width));
double hscale = (std::max)(1., ceil(2. * (double)height / (double)screen_height));
unsigned int scale = (unsigned int)(std::max)(1u, (std::max)((unsigned int)wscale, (unsigned int)hscale));
double wscale = std::max<double>(1., ceil(2. * (double)width / (double)screen_width));
double hscale = std::max<double>(1., ceil(2. * (double)height / (double)screen_height));
unsigned int scale = std::max<unsigned int>(1u, std::max<unsigned int>((unsigned int)wscale, (unsigned int)hscale));
return scale;
}

Expand Down
54 changes: 25 additions & 29 deletions modules/core/src/image/private/Font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace Font
{
template <typename T> struct Point {
template <typename T> struct Point
{
typedef T Type; /*!< Type definition. */

T x; /*!< \brief Specifies the x-coordinate of a point. */
Expand Down Expand Up @@ -110,19 +111,17 @@ template <> inline int Convert<int, double>(double src) { return Round(src); }

template <> inline int Convert<int, float>(float src) { return Round(src); }

template <typename T> inline Point<T>::Point() : x(0), y(0) {}
template <typename T> inline Point<T>::Point() : x(0), y(0) { }

template <typename T>
template <typename TX, typename TY>
inline Point<T>::Point(TX tx, TY ty) : x(Convert<T, TX>(tx)), y(Convert<T, TY>(ty))
{
}
{ }

template <typename T>
template <class TP, template <class> class TPoint>
inline Point<T>::Point(const TPoint<TP> &p) : x(Convert<T, TP>(p.x)), y(Convert<T, TP>(p.y))
{
}
{ }

template <typename T> inline Point<T> Point<T>::operator<<(int shift) const { return Point<T>(x << shift, y << shift); }

Expand All @@ -140,7 +139,8 @@ template <typename TP, typename TA> inline Point<TP> operator*(const TA &a, cons
return Point<TP>(p.x * a, p.y * a);
}

template <typename T> struct Rectangle {
template <typename T> struct Rectangle
{
typedef T Type; /*!< Type definition. */

T left; /*!< \brief Specifies the position of left side of a rectangle. */
Expand Down Expand Up @@ -362,36 +362,32 @@ template <typename T> struct Rectangle {

// struct Rectangle<T> implementation:

template <typename T> inline Rectangle<T>::Rectangle() : left(0), top(0), right(0), bottom(0) {}
template <typename T> inline Rectangle<T>::Rectangle() : left(0), top(0), right(0), bottom(0) { }

template <typename T>
template <typename TL, typename TT, typename TR, typename TB>
inline Rectangle<T>::Rectangle(TL l, TT t, TR r, TB b)
: left(Convert<T, TL>(l)), top(Convert<T, TT>(t)), right(Convert<T, TR>(r)), bottom(Convert<T, TB>(b))
{
}
{ }

template <typename T>
template <typename TLT, typename TRB>
inline Rectangle<T>::Rectangle(const Point<TLT> &lt, const Point<TRB> &rb)
: left(Convert<T, TLT>(lt.x)), top(Convert<T, TLT>(lt.y)), right(Convert<T, TRB>(rb.x)), bottom(Convert<T, TRB>(rb.y))
{
}
{ }

template <typename T>
template <typename TRB>
inline Rectangle<T>::Rectangle(const Point<TRB> &rb)
: left(0), top(0), right(Convert<T, TRB>(rb.x)), bottom(Convert<T, TRB>(rb.y))
{
}
{ }

template <typename T>
template <class TR, template <class> class TRectangle>
inline Rectangle<T>::Rectangle(const TRectangle<TR> &r)
: left(Convert<T, TR>(r.left)), top(Convert<T, TR>(r.top)), right(Convert<T, TR>(r.right)),
bottom(Convert<T, TR>(r.bottom))
{
}
bottom(Convert<T, TR>(r.bottom))
{ }

template <typename T>
template <class TR, template <class> class TRectangle>
Expand Down Expand Up @@ -473,10 +469,10 @@ template <typename TR>
inline Rectangle<T> Rectangle<T>::Intersection(const Rectangle<TR> &rect) const
{
Rectangle<T> _rect(rect);
T l = std::max(left, _rect.left);
T t = std::max(top, _rect.top);
T r = std::max(l, std::min(right, _rect.right));
T b = std::max(t, std::min(bottom, _rect.bottom));
T l = std::max<T>(left, _rect.left);
T t = std::max<T>(top, _rect.top);
T r = std::max<T>(l, std::min<T>(right, _rect.right));
T b = std::max<T>(t, std::min<T>(bottom, _rect.bottom));
return Rectangle(l, t, r, b);
}

Expand All @@ -489,13 +485,13 @@ template <typename T> template <typename TR> inline Rectangle<T> &Rectangle<T>::

Rectangle<T> _r(r);
if (left < _r.left)
left = std::min(_r.left, right);
left = std::min<T>(_r.left, right);
if (top < _r.top)
top = std::min(_r.top, bottom);
top = std::min<T>(_r.top, bottom);
if (right > _r.right)
right = std::max(_r.right, left);
right = std::max<T>(_r.right, left);
if (bottom > _r.bottom)
bottom = std::max(_r.bottom, top);
bottom = std::max<T>(_r.bottom, top);
return *this;
}

Expand All @@ -507,10 +503,10 @@ template <typename T> template <typename TR> inline Rectangle<T> &Rectangle<T>::
return *this;

Rectangle<T> _r(r);
left = std::min(left, _r.left);
top = std::min(top, _r.top);
right = std::max(right, _r.right);
bottom = std::max(bottom, _r.bottom);
left = std::min<T>(left, _r.left);
top = std::min<T>(top, _r.top);
right = std::max<T>(right, _r.right);
bottom = std::max<T>(bottom, _r.bottom);
return *this;
}
} // namespace Font
Expand Down
10 changes: 5 additions & 5 deletions modules/core/src/image/vpCannyEdgeDetection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ vpCannyEdgeDetection::detect(const vpImage<unsigned char> &I)
lowerThreshold = m_upperThreshold / 3.f;
}
// To ensure that if lowerThreshold = 0, we reject null gradient points
lowerThreshold = std::max(lowerThreshold, std::numeric_limits<float>::epsilon());
lowerThreshold = std::max<float>(lowerThreshold, std::numeric_limits<float>::epsilon());
performEdgeThinning(lowerThreshold);

// // Step 4: hysteresis thresholding
Expand Down Expand Up @@ -338,8 +338,8 @@ getGradientOrientation(const vpImage<float> &dIx, const vpImage<float> &dIy, con
else {
// -dy because the y-axis of the image is oriented towards the bottom of the screen
// while we later work with a y-axis oriented towards the top when getting the theta quadrant.
gradientOrientation = static_cast<float>(std::atan2(-dy , dx));
if(gradientOrientation < 0.f) {
gradientOrientation = static_cast<float>(std::atan2(-dy, dx));
if (gradientOrientation < 0.f) {
gradientOrientation += M_PIf; // + M_PI in order to be between 0 and M_PIf
}
}
Expand Down Expand Up @@ -427,9 +427,9 @@ vpCannyEdgeDetection::recursiveSearchForStrongEdge(const std::pair<unsigned int,
for (int dr = -1; dr <= 1 && !hasFoundStrongEdge; dr++) {
for (int dc = -1; dc <= 1 && !hasFoundStrongEdge; dc++) {
int idRow = dr + (int)coordinates.first;
idRow = std::max(idRow, 0); // Avoid getting negative pixel ID
idRow = std::max<int>(idRow, 0); // Avoid getting negative pixel ID
int idCol = dc + (int)coordinates.second;
idCol = std::max(idCol, 0); // Avoid getting negative pixel ID
idCol = std::max<int>(idCol, 0); // Avoid getting negative pixel ID

// Checking if we are still looking for an edge in the limit of the image
if ((idRow < 0 || idRow >= nbRows)
Expand Down
Loading

0 comments on commit 50cb067

Please sign in to comment.