From 28166e620094b359ccb98696fa9bda3785ce24eb Mon Sep 17 00:00:00 2001 From: whitequark Date: Fri, 27 Mar 2015 18:43:28 +0300 Subject: [PATCH] Use C++ std::{min,max,swap} instead of custom ones. The main benefit is that std::swap will ensure that the type of arguments is copy-constructible and move-constructible. It is more concise as well. When min and max are defined as macros, they will conflict with STL header files included by other C++ libraries; in this case STL will #undef any other definition. --- src/bsp.cpp | 2 +- src/confscreen.cpp | 6 +++--- src/constraint.cpp | 10 +++++----- src/constrainteq.cpp | 4 ++-- src/drawconstraint.cpp | 2 +- src/dsc.h | 2 +- src/export.cpp | 12 ++++++------ src/exportvector.cpp | 2 +- src/graphicswin.cpp | 2 +- src/group.cpp | 4 ++-- src/groupmesh.cpp | 2 +- src/mesh.cpp | 4 ++-- src/modify.cpp | 4 ++-- src/polygon.cpp | 4 ++-- src/solvespace.h | 13 +++++-------- src/srf/boolean.cpp | 2 +- src/srf/curve.cpp | 4 ++-- src/srf/raycast.cpp | 6 +++--- src/srf/surface.cpp | 12 ++++++------ src/srf/surfinter.cpp | 4 ++-- src/style.cpp | 2 +- src/system.cpp | 4 ++-- src/textwin.cpp | 6 +++--- src/ttf.cpp | 2 +- 24 files changed, 56 insertions(+), 59 deletions(-) diff --git a/src/bsp.cpp b/src/bsp.cpp index 8b944ec4..80c359ae 100644 --- a/src/bsp.cpp +++ b/src/bsp.cpp @@ -25,7 +25,7 @@ SBsp3 *SBsp3::FromMesh(SMesh *m) { while(n > 1) { int k = rand() % n; n--; - SWAP(STriangle, mc.l.elem[k], mc.l.elem[n]); + swap(mc.l.elem[k], mc.l.elem[n]); } for(i = 0; i < mc.l.n; i++) { diff --git a/src/confscreen.cpp b/src/confscreen.cpp index 1cc2d4fe..9aceb3f4 100644 --- a/src/confscreen.cpp +++ b/src/confscreen.cpp @@ -316,7 +316,7 @@ void TextWindow::ShowConfiguration(void) { bool TextWindow::EditControlDoneForConfiguration(const char *s) { switch(edit.meaning) { case EDIT_LIGHT_INTENSITY: - SS.lightIntensity[edit.i] = min(1, max(0, atof(s))); + SS.lightIntensity[edit.i] = min(1.0, max(0.0, atof(s))); InvalidateGraphics(); break; @@ -341,7 +341,7 @@ bool TextWindow::EditControlDoneForConfiguration(const char *s) { break; } case EDIT_CHORD_TOLERANCE: { - SS.chordTol = min(10, max(0.1, atof(s))); + SS.chordTol = min(10.0, max(0.1, atof(s))); SS.GenerateAll(0, INT_MAX); break; } @@ -351,7 +351,7 @@ bool TextWindow::EditControlDoneForConfiguration(const char *s) { break; } case EDIT_CAMERA_TANGENT: { - SS.cameraTangent = (min(2, max(0, atof(s))))/1000.0; + SS.cameraTangent = (min(2.0, max(0.0, atof(s))))/1000.0; if(!SS.usePerspectiveProj) { Message("The perspective factor will have no effect until you " "enable View -> Use Perspective Projection."); diff --git a/src/constraint.cpp b/src/constraint.cpp index 37a28b1c..933f52e1 100644 --- a/src/constraint.cpp +++ b/src/constraint.cpp @@ -369,7 +369,7 @@ void Constraint::MenuConstrain(int id) { if((l1->group.v != SS.GW.activeGroup.v) || (l1->construction && !(l0->construction))) { - SWAP(Entity *, l0, l1); + swap(l0, l1); } c.ptA = l1->point[0]; c.ptB = l1->point[1]; @@ -476,7 +476,7 @@ void Constraint::MenuConstrain(int id) { Entity *nfree = SK.GetEntity(c.entityA); Entity *nref = SK.GetEntity(c.entityB); if(nref->group.v == SS.GW.activeGroup.v) { - SWAP(Entity *, nref, nfree); + swap(nref, nfree); } if(nfree->group.v == SS.GW.activeGroup.v && nref ->group.v != SS.GW.activeGroup.v) @@ -491,7 +491,7 @@ void Constraint::MenuConstrain(int id) { // There might be an odd*90 degree rotation about the // normal vector; allow that, since the numerical // constraint does - SWAP(Vector, ru, rv); + swap(ru, rv); } fu = fu.Dot(ru) > 0 ? ru : ru.ScaledBy(-1); fv = fv.Dot(rv) > 0 ? rv : rv.ScaledBy(-1); @@ -582,7 +582,7 @@ void Constraint::MenuConstrain(int id) { Entity *line = SK.GetEntity(gs.entity[0]); Entity *arc = SK.GetEntity(gs.entity[1]); if(line->type == Entity::ARC_OF_CIRCLE) { - SWAP(Entity *, line, arc); + swap(line, arc); } Vector l0 = SK.GetEntity(line->point[0])->PointGetNum(), l1 = SK.GetEntity(line->point[1])->PointGetNum(); @@ -606,7 +606,7 @@ void Constraint::MenuConstrain(int id) { Entity *line = SK.GetEntity(gs.entity[0]); Entity *cubic = SK.GetEntity(gs.entity[1]); if(line->type == Entity::CUBIC) { - SWAP(Entity *, line, cubic); + swap(line, cubic); } Vector l0 = SK.GetEntity(line->point[0])->PointGetNum(), l1 = SK.GetEntity(line->point[1])->PointGetNum(); diff --git a/src/constrainteq.cpp b/src/constrainteq.cpp index 964fdf9e..1c6beb1d 100644 --- a/src/constrainteq.cpp +++ b/src/constrainteq.cpp @@ -592,7 +592,7 @@ void ConstraintBase::GenerateReal(IdList *l) { EntityBase *a = SK.GetEntity(entityA); EntityBase *b = SK.GetEntity(entityB); if(b->group.v != group.v) { - SWAP(EntityBase *, a, b); + swap(a, b); } ExprVector au = a->NormalExprsU(), @@ -744,7 +744,7 @@ void ConstraintBase::GenerateReal(IdList *l) { case PARALLEL: { EntityBase *ea = SK.GetEntity(entityA), *eb = SK.GetEntity(entityB); if(eb->group.v != group.v) { - SWAP(EntityBase *, ea, eb); + swap(ea, eb); } ExprVector a = ea->VectorGetExprs(); ExprVector b = eb->VectorGetExprs(); diff --git a/src/drawconstraint.cpp b/src/drawconstraint.cpp index e2d85179..391ae5cc 100644 --- a/src/drawconstraint.cpp +++ b/src/drawconstraint.cpp @@ -727,7 +727,7 @@ void Constraint::DrawOrGetDistance(Vector *labelPos) { // a bit of bias to stop it from flickering between the // two possibilities if(fabs(u.Dot(ru)) < fabs(v.Dot(ru)) + LENGTH_EPS) { - SWAP(Vector, u, v); + swap(u, v); } if(u.Dot(ru) < 0) u = u.ScaledBy(-1); } diff --git a/src/dsc.h b/src/dsc.h index e2765477..3c527e16 100644 --- a/src/dsc.h +++ b/src/dsc.h @@ -216,7 +216,7 @@ public: void Reverse(void) { int i; for(i = 0; i < (n/2); i++) { - SWAP(T, elem[i], elem[(n-1)-i]); + swap(elem[i], elem[(n-1)-i]); } } }; diff --git a/src/export.cpp b/src/export.cpp index 37a4e884..cc458ba1 100644 --- a/src/export.cpp +++ b/src/export.cpp @@ -48,7 +48,7 @@ void SolveSpaceUI::ExportSectionTo(const char *filename) { vt = vt.WithMagnitude(1); if(fabs(SS.GW.projUp.Dot(vt)) < fabs(SS.GW.projUp.Dot(ut))) { - SWAP(Vector, ut, vt); + swap(ut, vt); } if(SS.GW.projRight.Dot(ut) < 0) ut = ut.ScaledBy(-1); if(SS.GW.projUp. Dot(vt) < 0) vt = vt.ScaledBy(-1); @@ -280,11 +280,11 @@ void SolveSpaceUI::ExportLinesAndMesh(SEdgeList *sel, SBezierList *sbl, SMesh *s // And calculate lighting for the triangle Vector n = tt.Normal().WithMagnitude(1); double lighting = SS.ambientIntensity + - max(0, (SS.lightIntensity[0])*(n.Dot(l0))) + - max(0, (SS.lightIntensity[1])*(n.Dot(l1))); - double r = min(1, tt.meta.color.redF() *lighting), - g = min(1, tt.meta.color.greenF()*lighting), - b = min(1, tt.meta.color.blueF() *lighting); + max(0.0, (SS.lightIntensity[0])*(n.Dot(l0))) + + max(0.0, (SS.lightIntensity[1])*(n.Dot(l1))); + double r = min(1.0, tt.meta.color.redF() * lighting), + g = min(1.0, tt.meta.color.greenF() * lighting), + b = min(1.0, tt.meta.color.blueF() * lighting); tt.meta.color = RGBf(r, g, b); smp.AddTriangle(&tt); } diff --git a/src/exportvector.cpp b/src/exportvector.cpp index c92fa4c5..efa5f9dd 100644 --- a/src/exportvector.cpp +++ b/src/exportvector.cpp @@ -112,7 +112,7 @@ void DxfFileWriter::Bezier(SBezier *sb) { theta1 = atan2(sb->ctrl[2].y - c.y, sb->ctrl[2].x - c.x), dtheta = WRAP_SYMMETRIC(theta1 - theta0, 2*PI); if(dtheta < 0) { - SWAP(double, theta0, theta1); + swap(theta0, theta1); } fprintf(f, diff --git a/src/graphicswin.cpp b/src/graphicswin.cpp index ac44d8bc..3ea03ccc 100644 --- a/src/graphicswin.cpp +++ b/src/graphicswin.cpp @@ -384,7 +384,7 @@ void GraphicsWindow::ZoomToFit(bool includingInvisibles) { if(EXACT(dy != 0)) scaley = 0.9*height/dy; scale = min(scalex, scaley); - scale = min(300, scale); + scale = min(300.0, scale); scale = max(0.003, scale); } diff --git a/src/group.cpp b/src/group.cpp index a0ef4b56..873e6b45 100644 --- a/src/group.cpp +++ b/src/group.cpp @@ -97,7 +97,7 @@ void Group::MenuGroup(int id) { vt = vt.WithMagnitude(1); if(fabs(SS.GW.projUp.Dot(vt)) < fabs(SS.GW.projUp.Dot(ut))) { - SWAP(Vector, ut, vt); + swap(ut, vt); g.predef.swapUV = true; } if(SS.GW.projRight.Dot(ut) < 0) g.predef.negateU = true; @@ -317,7 +317,7 @@ void Group::Generate(IdList *entity, Vector n = u.Cross(v); v = (n.Cross(u)).WithMagnitude(1); - if(predef.swapUV) SWAP(Vector, u, v); + if(predef.swapUV) swap(u, v); if(predef.negateU) u = u.ScaledBy(-1); if(predef.negateV) v = v.ScaledBy(-1); q = Quaternion::From(u, v); diff --git a/src/groupmesh.cpp b/src/groupmesh.cpp index ad404fa2..317a59ab 100644 --- a/src/groupmesh.cpp +++ b/src/groupmesh.cpp @@ -149,7 +149,7 @@ void Group::GenerateForStepAndRepeat(T *steps, T *outs) { scratch->MakeFromUnionOf(soFar, &transd); } - SWAP(T *, scratch, soFar); + swap(scratch, soFar); scratch->Clear(); transd.Clear(); } diff --git a/src/mesh.cpp b/src/mesh.cpp index 81f31a5c..78150517 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -312,7 +312,7 @@ void SMesh::MakeFromTransformationOf(SMesh *a, tt.c = (tt.c).ScaledBy(scale); if(scale < 0) { // The mirroring would otherwise turn a closed mesh inside out. - SWAP(Vector, tt.a, tt.b); + swap(tt.a, tt.b); } tt.a = (q.Rotate(tt.a)).Plus(trans); tt.b = (q.Rotate(tt.b)).Plus(trans); @@ -373,7 +373,7 @@ SKdNode *SKdNode::From(SMesh *m) { while(n > 1) { int k = rand() % n; n--; - SWAP(STriangle, tra[k], tra[n]); + swap(tra[k], tra[n]); } STriangleLl *tll = NULL; diff --git a/src/modify.cpp b/src/modify.cpp index 0a1847f0..1917e5f7 100644 --- a/src/modify.cpp +++ b/src/modify.cpp @@ -96,7 +96,7 @@ void GraphicsWindow::ParametricCurve::MakeFromEntity(hEntity he, bool reverse) { p0 = e->EndpointStart(), p1 = e->EndpointFinish(); if(reverse) { - SWAP(Vector, p0, p1); + swap(p0, p1); } } else if(e->type == Entity::ARC_OF_CIRCLE) { isLine = false; @@ -105,7 +105,7 @@ void GraphicsWindow::ParametricCurve::MakeFromEntity(hEntity he, bool reverse) { r = (pe.Minus(p0)).Magnitude(); e->ArcGetAngles(&theta0, &theta1, &dtheta); if(reverse) { - SWAP(double, theta0, theta1); + swap(theta0, theta1); dtheta = -dtheta; } EntityBase *wrkpln = SK.GetEntity(e->workplane)->Normal(); diff --git a/src/polygon.cpp b/src/polygon.cpp index e4ebcd52..c012a6dc 100644 --- a/src/polygon.cpp +++ b/src/polygon.cpp @@ -43,8 +43,8 @@ bool STriangle::ContainsPointProjd(Vector n, Vector p) { } void STriangle::FlipNormal(void) { - SWAP(Vector, a, b); - SWAP(Vector, an, bn); + swap(a, b); + swap(an, bn); } STriangle STriangle::From(STriMeta meta, Vector a, Vector b, Vector c) { diff --git a/src/solvespace.h b/src/solvespace.h index 4a2664cd..b4a91088 100644 --- a/src/solvespace.h +++ b/src/solvespace.h @@ -16,6 +16,7 @@ #include #include #include +#include #ifdef HAVE_STDINT_H # include #endif @@ -51,19 +52,16 @@ abort(); } while(0) #endif -#ifndef min -# define min(x, y) ((x) < (y) ? (x) : (y)) -#endif -#ifndef max -# define max(x, y) ((x) > (y) ? (x) : (y)) -#endif - #ifndef isnan # define isnan(x) (((x) != (x)) || (x > 1e11) || (x < -1e11)) #endif namespace SolveSpace { +using std::min; +using std::max; +using std::swap; + inline int WRAP(int v, int n) { // Clamp it to the range [0, n) while(v >= n) v -= n; @@ -86,7 +84,6 @@ inline double WRAP_SYMMETRIC(double v, double n) { // Why is this faster than the library function? inline double ffabs(double v) { return (v > 0) ? v : (-v); } -#define SWAP(T, a, b) do { T temp = (a); (a) = (b); (b) = temp; } while(0) #define ZERO(v) memset((v), 0, sizeof(*(v))) #define CO(v) (v).x, (v).y, (v).z diff --git a/src/srf/boolean.cpp b/src/srf/boolean.cpp index 15811cb4..4ae61c7f 100644 --- a/src/srf/boolean.cpp +++ b/src/srf/boolean.cpp @@ -277,7 +277,7 @@ static void DEBUGEDGELIST(SEdgeList *sel, SSurface *surf) { for(se = sel->l.First(); se; se = sel->l.NextAfter(se)) { Vector mid = (se->a).Plus(se->b).ScaledBy(0.5); Vector arrow = (se->b).Minus(se->a); - SWAP(double, arrow.x, arrow.y); + swap(arrow.x, arrow.y); arrow.x *= -1; arrow = arrow.WithMagnitude(0.01); arrow = arrow.Plus(mid); diff --git a/src/srf/curve.cpp b/src/srf/curve.cpp index c5eb59aa..e05bb75f 100644 --- a/src/srf/curve.cpp +++ b/src/srf/curve.cpp @@ -74,8 +74,8 @@ Vector SBezier::Finish(void) { void SBezier::Reverse(void) { int i; for(i = 0; i < (deg+1)/2; i++) { - SWAP(Vector, ctrl[i], ctrl[deg-i]); - SWAP(double, weight[i], weight[deg-i]); + swap(ctrl[i], ctrl[deg-i]); + swap(weight[i], weight[deg-i]); } } diff --git a/src/srf/raycast.cpp b/src/srf/raycast.cpp index 4eee7e6e..97c1706c 100644 --- a/src/srf/raycast.cpp +++ b/src/srf/raycast.cpp @@ -471,9 +471,9 @@ bool SShell::ClassifyEdge(int *indir, int *outdir, } if(fabs(dotp[1]) < DOTP_TOL) { - SWAP(double, dotp[0], dotp[1]); - SWAP(Vector, inter_surf_n[0], inter_surf_n[1]); - SWAP(Vector, inter_edge_n[0], inter_edge_n[1]); + swap(dotp[0], dotp[1]); + swap(inter_surf_n[0], inter_surf_n[1]); + swap(inter_edge_n[0], inter_edge_n[1]); } int coinc = (surf_n.Dot(inter_surf_n[0])) > 0 ? COINC_SAME : COINC_OPP; diff --git a/src/srf/surface.cpp b/src/srf/surface.cpp index 0cbacf2e..b8d1e7c6 100644 --- a/src/srf/surface.cpp +++ b/src/srf/surface.cpp @@ -459,15 +459,15 @@ void SSurface::Reverse(void) { int i, j; for(i = 0; i < (degm+1)/2; i++) { for(j = 0; j <= degn; j++) { - SWAP(Vector, ctrl[i][j], ctrl[degm-i][j]); - SWAP(double, weight[i][j], weight[degm-i][j]); + swap(ctrl[i][j], ctrl[degm-i][j]); + swap(weight[i][j], weight[degm-i][j]); } } STrimBy *stb; for(stb = trim.First(); stb; stb = trim.NextAfter(stb)) { stb->backwards = !stb->backwards; - SWAP(Vector, stb->start, stb->finish); + swap(stb->start, stb->finish); } } @@ -494,7 +494,7 @@ void SShell::MakeFromExtrusionOf(SBezierLoopSet *sbls, Vector t0, Vector t1, Rgb // Make the extrusion direction consistent with respect to the normal // of the sketch we're extruding. if((t0.Minus(t1)).Dot(sbls->normal) < 0) { - SWAP(Vector, t0, t1); + swap(t0, t1); } // Define a coordinate system to contain the original sketch, and get @@ -780,8 +780,8 @@ void SShell::MakeFromRevolutionOf(SBezierLoopSet *sbls, Vector pt, Vector axis, srf->weight[1][1] = 1; if(oldn.Dot(srf->NormalAt(0.5, 0.5)) < 0) { - SWAP(Vector, srf->ctrl[0][0], srf->ctrl[1][0]); - SWAP(Vector, srf->ctrl[0][1], srf->ctrl[1][1]); + swap(srf->ctrl[0][0], srf->ctrl[1][0]); + swap(srf->ctrl[0][1], srf->ctrl[1][1]); } continue; } diff --git a/src/srf/surfinter.cpp b/src/srf/surfinter.cpp index 876c0e3f..95b07030 100644 --- a/src/srf/surfinter.cpp +++ b/src/srf/surfinter.cpp @@ -262,8 +262,8 @@ void SSurface::IntersectAgainst(SSurface *b, SShell *agnstA, SShell *agnstB, b_axis0 = (b->ctrl[0][0]).Dot(axis), b_axis1 = (b->ctrl[0][1]).Dot(axis); - if(a_axis0 > a_axis1) SWAP(double, a_axis0, a_axis1); - if(b_axis0 > b_axis1) SWAP(double, b_axis0, b_axis1); + if(a_axis0 > a_axis1) swap(a_axis0, a_axis1); + if(b_axis0 > b_axis1) swap(b_axis0, b_axis1); double ab_axis0 = max(a_axis0, b_axis0), ab_axis1 = min(a_axis1, b_axis1); diff --git a/src/style.cpp b/src/style.cpp index e03b2731..1d0563d6 100644 --- a/src/style.cpp +++ b/src/style.cpp @@ -659,7 +659,7 @@ bool TextWindow::EditControlDoneForStyles(const char *str) { } else { v = atof(str); } - v = max(0, v); + v = max(0.0, v); if(edit.meaning == EDIT_STYLE_TEXT_HEIGHT) { s->textHeight = v; } else { diff --git a/src/system.cpp b/src/system.cpp index 286e4a65..dec1fa5c 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -199,9 +199,9 @@ bool System::SolveLinearSystem(double X[], double A[][MAX_UNKNOWNS], // Swap row imax with row i for(jp = 0; jp < n; jp++) { - SWAP(double, A[i][jp], A[imax][jp]); + swap(A[i][jp], A[imax][jp]); } - SWAP(double, B[i], B[imax]); + swap(B[i], B[imax]); // For rows i+1 and greater, eliminate the term in column i. for(ip = i+1; ip < n; ip++) { diff --git a/src/textwin.cpp b/src/textwin.cpp index c474bb6c..c0b3bfdc 100644 --- a/src/textwin.cpp +++ b/src/textwin.cpp @@ -442,8 +442,8 @@ void TextWindow::DrawOrHitTestIcons(int how, double mx, double my) ox += 3; oy -= 3; int tw = ((int)strlen(str) + 1)*CHAR_WIDTH; - ox = min(ox, (width - 25) - tw); - oy = max(oy, 5); + ox = min(ox, (double) (width - 25) - tw); + oy = max(oy, 5.0); ssglCreateBitmapFont(); glLineWidth(1); @@ -838,7 +838,7 @@ void TextWindow::Paint(void) { if(ltop < (scrollPos-1)) continue; if(ltop > scrollPos+halfRows) break; - for(c = 0; c < min((width/CHAR_WIDTH)+1, MAX_COLS); c++) { + for(c = 0; c < min((width/CHAR_WIDTH)+1, (int) MAX_COLS); c++) { int x = LEFT_MARGIN + c*CHAR_WIDTH; int y = (ltop-scrollPos)*(LINE_HEIGHT/2) + 4; diff --git a/src/ttf.cpp b/src/ttf.cpp index 2862e8c7..0a7ad1b4 100644 --- a/src/ttf.cpp +++ b/src/ttf.cpp @@ -442,7 +442,7 @@ bool TtfFont::LoadFontFromFile(bool nameOnly) { uint16_t hmtxAdvanceWidth = 0; int16_t hmtxLsb = 0; - for(i = 0; i < min(glyphs, hheaNumberOfMetrics); i++) { + for(i = 0; i < min(glyphs, (int)hheaNumberOfMetrics); i++) { hmtxAdvanceWidth = GetUSHORT(); hmtxLsb = (int16_t)GetUSHORT();