From a7b71335b74b07c6dfe06e4312c94760b5da32db Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 27 Mar 2025 19:00:18 +0100 Subject: [PATCH] CAM: Use std::numeric_limits and std::numbers instead of defines --- src/Mod/CAM/App/Area.cpp | 12 ++++++------ src/Mod/CAM/App/PathSegmentWalker.cpp | 18 +++++------------- src/Mod/CAM/App/Voronoi.cpp | 17 ++++++++--------- src/Mod/CAM/App/Voronoi.h | 10 ++-------- src/Mod/CAM/App/VoronoiEdgePyImp.cpp | 14 ++++++++------ src/Mod/CAM/Gui/ViewProviderPath.cpp | 4 ++-- src/Mod/CAM/PathSimulator/AppGL/linmath.h | 2 +- src/Mod/CAM/libarea/Adaptive.cpp | 17 +++++++++-------- src/Mod/CAM/libarea/Adaptive.hpp | 4 ---- src/Mod/CAM/libarea/Box2D.h | 2 +- src/Mod/CAM/libarea/Curve.h | 2 +- src/Mod/CAM/libarea/kurve/geometry.h | 2 +- 12 files changed, 44 insertions(+), 60 deletions(-) diff --git a/src/Mod/CAM/App/Area.cpp b/src/Mod/CAM/App/Area.cpp index df87a8f527..82892eef4d 100644 --- a/src/Mod/CAM/App/Area.cpp +++ b/src/Mod/CAM/App/Area.cpp @@ -26,7 +26,6 @@ #define BOOST_GEOMETRY_DISABLE_DEPRECATED_03_WARNING #ifndef _PreComp_ -#include #include #include @@ -452,7 +451,7 @@ void Area::addWire(CArea& area, if (reversed) { type = -type; } - if (fabs(first - last) > M_PI) { + if (fabs(first - last) > std::numbers::pi) { // Split arc(circle) larger than half circle. Because gcode // can't handle full circle? gp_Pnt mid = curve.Value((last - first) * 0.5 + first); @@ -1221,7 +1220,8 @@ struct WireJoiner info.iEnd[i] = info.iStart[i] = (int)adjacentList.size(); // populate adjacent list - for (auto vit = vmap.qbegin(bgi::nearest(pt[i], INT_MAX)); vit != vmap.qend(); + constexpr int intMax = std::numeric_limits::max(); + for (auto vit = vmap.qbegin(bgi::nearest(pt[i], intMax)); vit != vmap.qend(); ++vit) { ++rcount; if (vit->pt().SquareDistance(pt[i]) > tol) { @@ -2631,7 +2631,7 @@ TopoDS_Shape Area::makePocket(int index, PARAM_ARGS(PARAM_FARG, AREA_PARAMS_POCK for (int j = 0; j < steps; ++j, offset += stepover) { Point p1(-r, offset), p2(r, offset); if (a > Precision::Confusion()) { - double r = a * M_PI / 180.0; + double r = a * std::numbers::pi / 180.0; p1.Rotate(r); p2.Rotate(r); } @@ -3703,7 +3703,7 @@ std::list Area::sortWires(const std::list& shapes, double max_dist = sort_mode == SortModeGreedy ? threshold * threshold : 0; while (!shape_list.empty()) { AREA_TRACE("sorting " << shape_list.size() << ' ' << AREA_XYZ(pstart)); - double best_d = DBL_MAX; + double best_d = std::numeric_limits::max(); auto best_it = shape_list.begin(); for (auto it = best_it; it != shape_list.end(); ++it) { double d; @@ -4155,7 +4155,7 @@ void Area::toPath(Toolpath& path, } } - if (fabs(first - last) > M_PI) { + if (fabs(first - last) > std::numbers::pi) { // Split arc(circle) larger than half circle. gp_Pnt mid = curve.Value((last - first) * 0.5 + first); addGArc(verbose, diff --git a/src/Mod/CAM/App/PathSegmentWalker.cpp b/src/Mod/CAM/App/PathSegmentWalker.cpp index 2cd883ef09..5b9a54b6cb 100644 --- a/src/Mod/CAM/App/PathSegmentWalker.cpp +++ b/src/Mod/CAM/App/PathSegmentWalker.cpp @@ -31,14 +31,6 @@ #define ARC_MIN_SEGMENTS 20.0 // minimum # segments to interpolate an arc -#ifndef M_PI -#define M_PI 3.14159265358979323846 /* pi */ -#endif - -#ifndef M_PI_2 -#define M_PI_2 1.57079632679489661923 /* pi/2 */ -#endif - namespace Path { @@ -195,7 +187,7 @@ void PathSegmentWalker::walk(PathSegmentVisitor& cb, const Base::Vector3d& start if (nrot != lrot) { double amax = std::max(fmod(fabs(a - A), 360), std::max(fmod(fabs(b - B), 360), fmod(fabs(c - C), 360))); - double angle = amax / 180 * M_PI; + double angle = amax / 180 * std::numbers::pi; int segments = std::max(ARC_MIN_SEGMENTS, 3.0 / (deviation / angle)); double da = (a - A) / segments; @@ -257,16 +249,16 @@ void PathSegmentWalker::walk(PathSegmentVisitor& cb, const Base::Vector3d& start Base::Vector3d anorm = (last0 - center0) % (next0 - center0); if (anorm.*pz < 0) { if (name == "G3" || name == "G03") { - angle = M_PI * 2 - angle; + angle = std::numbers::pi * 2 - angle; } } else if (anorm.*pz > 0) { if (name == "G2" || name == "G02") { - angle = M_PI * 2 - angle; + angle = std::numbers::pi * 2 - angle; } } else if (angle == 0) { - angle = M_PI * 2; + angle = std::numbers::pi * 2; } double amax = std::max(fmod(fabs(a - A), 360), @@ -337,7 +329,7 @@ void PathSegmentWalker::walk(PathSegmentVisitor& cb, const Base::Vector3d& start if (nrot != lrot) { double amax = std::max(fmod(fabs(a - A), 360), std::max(fmod(fabs(b - B), 360), fmod(fabs(c - C), 360))); - double angle = amax / 180 * M_PI; + double angle = amax / 180 * std::numbers::pi; int segments = std::max(ARC_MIN_SEGMENTS, 3.0 / (deviation / angle)); double da = (a - A) / segments; diff --git a/src/Mod/CAM/App/Voronoi.cpp b/src/Mod/CAM/App/Voronoi.cpp index 55aeb601c3..0ccad2e191 100644 --- a/src/Mod/CAM/App/Voronoi.cpp +++ b/src/Mod/CAM/App/Voronoi.cpp @@ -22,8 +22,6 @@ #include "PreCompiled.h" #ifndef _PreComp_ -#define _USE_MATH_DEFINES -#include #endif #include @@ -260,10 +258,10 @@ double Voronoi::diagram_type::angleOfSegment(int i, Voronoi::diagram_type::angle double ang = 0; if (p0.x() == p1.x()) { if (p0.y() < p1.y()) { - ang = M_PI_2; + ang = std::numbers::pi / 2; } else { - ang = -M_PI_2; + ang = -std::numbers::pi / 2; } } else { @@ -292,7 +290,8 @@ bool Voronoi::diagram_type::segmentsAreConnected(int i, int j) const void Voronoi::colorColinear(Voronoi::color_type color, double degree) { - double rad = degree * M_PI / 180; + using std::numbers::pi; + double rad = degree * pi / 180; Voronoi::diagram_type::angle_map_t angle; int psize = vd->points.size(); @@ -306,11 +305,11 @@ void Voronoi::colorColinear(Voronoi::color_type color, double degree) double a0 = vd->angleOfSegment(i0, &angle); double a1 = vd->angleOfSegment(i1, &angle); double a = a0 - a1; - if (a > M_PI_2) { - a -= M_PI; + if (a > pi / 2) { + a -= pi; } - else if (a < -M_PI_2) { - a += M_PI; + else if (a < -pi / 2) { + a += pi; } if (fabs(a) < rad) { it->color(color); diff --git a/src/Mod/CAM/App/Voronoi.h b/src/Mod/CAM/App/Voronoi.h index d7a4ac5c8c..ff77698bfb 100644 --- a/src/Mod/CAM/App/Voronoi.h +++ b/src/Mod/CAM/App/Voronoi.h @@ -22,7 +22,6 @@ #ifndef PATH_VORONOI_H #define PATH_VORONOI_H -#include #include #include #include @@ -33,11 +32,6 @@ #include #include -#if (SIZE_MAX == UINT_MAX) -#define PATH_VORONOI_COLOR_MASK 0x07FFFFFFul -#else -#define PATH_VORONOI_COLOR_MASK 0x07FFFFFFFFFFFFFFul -#endif namespace Path { @@ -51,8 +45,8 @@ public: ~Voronoi() override; using color_type = std::size_t; - static const int InvalidIndex = INT_MAX; - static const color_type ColorMask = PATH_VORONOI_COLOR_MASK; + static const int InvalidIndex = std::numeric_limits::max(); + static const color_type ColorMask = std::numeric_limits::max() >> 5; // types using coordinate_type = double; diff --git a/src/Mod/CAM/App/VoronoiEdgePyImp.cpp b/src/Mod/CAM/App/VoronoiEdgePyImp.cpp index 9015e49d52..bf47258083 100644 --- a/src/Mod/CAM/App/VoronoiEdgePyImp.cpp +++ b/src/Mod/CAM/App/VoronoiEdgePyImp.cpp @@ -466,12 +466,12 @@ PyObject* VoronoiEdgePy::isBorderline(PyObject* args) PyObject* VoronoiEdgePy::toShape(PyObject* args) { double z0 = 0.0; - double z1 = DBL_MAX; + double z1 = std::numeric_limits::max(); int dbg = 0; if (!PyArg_ParseTuple(args, "|ddp", &z0, &z1, &dbg)) { throw Py::RuntimeError("no, one or two arguments of type double accepted"); } - if (z1 == DBL_MAX) { + if (z1 == std::numeric_limits::max()) { z1 = z0; } VoronoiEdge* e = getVoronoiEdgePtr(); @@ -688,6 +688,8 @@ PyObject* VoronoiEdgePy::getDistances(PyObject* args) PyObject* VoronoiEdgePy::getSegmentAngle(PyObject* args) { + using std::numbers::pi; + VoronoiEdge* e = getVoronoiEdgeFromPy(this, args); if (e->ptr->cell()->contains_segment() && e->ptr->twin()->cell()->contains_segment()) { @@ -697,11 +699,11 @@ PyObject* VoronoiEdgePy::getSegmentAngle(PyObject* args) double a0 = e->dia->angleOfSegment(i0); double a1 = e->dia->angleOfSegment(i1); double a = a0 - a1; - if (a > M_PI_2) { - a -= M_PI; + if (a > pi / 2) { + a -= pi; } - else if (a < -M_PI_2) { - a += M_PI; + else if (a < -pi / 2) { + a += pi; } return Py::new_reference_to(Py::Float(a)); } diff --git a/src/Mod/CAM/Gui/ViewProviderPath.cpp b/src/Mod/CAM/Gui/ViewProviderPath.cpp index 25276bdacb..44698df05a 100644 --- a/src/Mod/CAM/Gui/ViewProviderPath.cpp +++ b/src/Mod/CAM/Gui/ViewProviderPath.cpp @@ -183,11 +183,11 @@ ViewProviderPath::ViewProviderPath() ShowCountConstraints.LowerBound = 0; - ShowCountConstraints.UpperBound = INT_MAX; + ShowCountConstraints.UpperBound = std::numeric_limits::max(); ShowCountConstraints.StepSize = 1; ShowCount.setConstraints(&ShowCountConstraints); StartIndexConstraints.LowerBound = 0; - StartIndexConstraints.UpperBound = INT_MAX; + StartIndexConstraints.UpperBound = std::numeric_limits::max(); StartIndexConstraints.StepSize = 1; StartIndex.setConstraints(&StartIndexConstraints); ADD_PROPERTY_TYPE(StartPosition, diff --git a/src/Mod/CAM/PathSimulator/AppGL/linmath.h b/src/Mod/CAM/PathSimulator/AppGL/linmath.h index 7ebdc2cfba..c0feb2caa0 100644 --- a/src/Mod/CAM/PathSimulator/AppGL/linmath.h +++ b/src/Mod/CAM/PathSimulator/AppGL/linmath.h @@ -5,7 +5,7 @@ #define LINMATH_H #include -#include +#include #ifdef LINMATH_NO_INLINE #define LINMATH_H_FUNC static diff --git a/src/Mod/CAM/libarea/Adaptive.cpp b/src/Mod/CAM/libarea/Adaptive.cpp index 3b45c3254a..535d25efe4 100644 --- a/src/Mod/CAM/libarea/Adaptive.cpp +++ b/src/Mod/CAM/libarea/Adaptive.cpp @@ -26,6 +26,7 @@ #include #include #include +#include namespace ClipperLib { @@ -116,7 +117,7 @@ inline double Angle3Points(const DoublePoint& p1, const DoublePoint& p2, const D double t1 = atan2(p2.Y - p1.Y, p2.X - p1.X); double t2 = atan2(p3.Y - p2.Y, p3.X - p2.X); double a = fabs(t2 - t1); - return min(a, 2 * M_PI - a); + return min(a, 2 * std::numbers::pi - a); } inline DoublePoint DirectionV(const IntPoint& pt1, const IntPoint& pt2) @@ -1096,8 +1097,8 @@ private: class Interpolation { public: - const double MIN_ANGLE = -M_PI / 4; - const double MAX_ANGLE = M_PI / 4; + const double MIN_ANGLE = -std::numbers::pi / 4; + const double MAX_ANGLE = std::numbers::pi / 4; void clear() { @@ -1542,7 +1543,7 @@ double Adaptive2d::CalcCutArea(Clipper& clip, double minFi = fi1; double maxFi = fi2; if (maxFi < minFi) { - maxFi += 2 * M_PI; + maxFi += 2 * std::numbers::pi; } if (preventConventional && interPathLen >= RESOLUTION_FACTOR) { @@ -2359,7 +2360,7 @@ bool Adaptive2d::MakeLeadPath(bool leadIn, IntPoint(currentPoint.X + nextDir.X * stepSize, currentPoint.Y + nextDir.Y * stepSize); Path checkPath; double adaptFactor = 0.4; - double alfa = M_PI / 64; + double alfa = std::numbers::pi / 64; double pathLen = 0; checkPath.push_back(nextPoint); for (int i = 0; i < 10000; i++) { @@ -2802,7 +2803,7 @@ void Adaptive2d::ProcessPolyNode(Paths boundPaths, Paths toolBoundPaths) IntPoint clp; // to store closest point vector gyro; // used to average tool direction vector angleHistory; // use to predict deflection angle - double angle = M_PI; + double angle = std::numbers::pi; engagePoint = toolPos; Interpolation interp; // interpolation instance @@ -2846,7 +2847,7 @@ void Adaptive2d::ProcessPolyNode(Paths boundPaths, Paths toolBoundPaths) } } - angle = M_PI / 4; // initial pass angle + angle = std::numbers::pi / 4; // initial pass angle bool recalcArea = false; double cumulativeCutArea = 0; // init gyro @@ -2991,7 +2992,7 @@ void Adaptive2d::ProcessPolyNode(Paths boundPaths, Paths toolBoundPaths) rotateStep++; // if new tool pos. outside boundary rotate until back in recalcArea = true; - newToolDir = rotate(newToolDir, M_PI / 90); + newToolDir = rotate(newToolDir, std::numbers::pi / 90); newToolPos = IntPoint(long(toolPos.X + newToolDir.X * stepScaled), long(toolPos.Y + newToolDir.Y * stepScaled)); } diff --git a/src/Mod/CAM/libarea/Adaptive.hpp b/src/Mod/CAM/libarea/Adaptive.hpp index 497aa2792a..9f57b5407f 100644 --- a/src/Mod/CAM/libarea/Adaptive.hpp +++ b/src/Mod/CAM/libarea/Adaptive.hpp @@ -36,10 +36,6 @@ #define __LONG_MAX__ 2147483647 #endif -#ifndef M_PI -#define M_PI 3.141592653589793238 -#endif - // #define DEV_MODE #define NTOL 1.0e-7 // numeric tolerance diff --git a/src/Mod/CAM/libarea/Box2D.h b/src/Mod/CAM/libarea/Box2D.h index 23ee202ac7..b0c02d9e3f 100644 --- a/src/Mod/CAM/libarea/Box2D.h +++ b/src/Mod/CAM/libarea/Box2D.h @@ -29,7 +29,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #pragma once #include // for memcpy() prototype -#include // for sqrt() prototype +#include // for sqrt() prototype class CBox2D { diff --git a/src/Mod/CAM/libarea/Curve.h b/src/Mod/CAM/libarea/Curve.h index f6f62eee14..35ea485a52 100644 --- a/src/Mod/CAM/libarea/Curve.h +++ b/src/Mod/CAM/libarea/Curve.h @@ -31,7 +31,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include -#include +#include #include "Point.h" #include "Box2D.h" diff --git a/src/Mod/CAM/libarea/kurve/geometry.h b/src/Mod/CAM/libarea/kurve/geometry.h index 88cbbf5b1a..8c8dc5a6ca 100644 --- a/src/Mod/CAM/libarea/kurve/geometry.h +++ b/src/Mod/CAM/libarea/kurve/geometry.h @@ -17,7 +17,7 @@ #endif #endif -#include +#include #include #include #include