From 5f86d7f5a870d283521b2d94cfa9db98d4a643e7 Mon Sep 17 00:00:00 2001 From: Ajinkya Dahale Date: Sun, 16 Jun 2024 00:42:51 +0530 Subject: [PATCH] [Sketcher] Refactor `SketchObject::getPoint()` ...to reduce cognitive complexity. Use templates to break longer functions. Also makes it possible to avoid repetition of type-checking logic. --- src/Mod/Sketcher/App/SketchObject.cpp | 178 +++++++++++++++++--------- src/Mod/Sketcher/App/SketchObject.h | 8 ++ 2 files changed, 127 insertions(+), 59 deletions(-) diff --git a/src/Mod/Sketcher/App/SketchObject.cpp b/src/Mod/Sketcher/App/SketchObject.cpp index 67b1682ec8..6962824016 100644 --- a/src/Mod/Sketcher/App/SketchObject.cpp +++ b/src/Mod/Sketcher/App/SketchObject.cpp @@ -1389,68 +1389,128 @@ int SketchObject::movePoint(int GeoId, PointPos PosId, const Base::Vector3d& toP return lastSolverStatus; } +template <> +Base::Vector3d SketchObject::getPointForGeometry<>(const Part::GeomPoint *geomPoint, PointPos PosId) +{ + if (PosId == PointPos::start || PosId == PointPos::mid || PosId == PointPos::end) + return geomPoint->getPoint(); + return Base::Vector3d(); +} + +template <> +Base::Vector3d SketchObject::getPointForGeometry<>(const Part::GeomLineSegment *lineSeg, PointPos PosId) +{ + if (PosId == PointPos::start) + return lineSeg->getStartPoint(); + else if (PosId == PointPos::end) + return lineSeg->getEndPoint(); + return Base::Vector3d(); +} + +template <> +Base::Vector3d SketchObject::getPointForGeometry<>(const Part::GeomCircle *circle, PointPos PosId) +{ + auto pt = circle->getCenter(); + if(PosId != PointPos::mid) + pt.x += circle->getRadius(); + return pt; +} + +template <> +Base::Vector3d SketchObject::getPointForGeometry<>(const Part::GeomEllipse *ellipse, PointPos PosId) +{ + auto pt = ellipse->getCenter(); + if(PosId != PointPos::mid) + pt += ellipse->getMajorAxisDir()*ellipse->getMajorRadius(); + return pt; +} + +template <> +Base::Vector3d SketchObject::getPointForGeometry<>(const Part::GeomArcOfCircle *aoc, PointPos PosId) +{ + if (PosId == PointPos::start) + return aoc->getStartPoint(/*emulateCCW=*/true); + else if (PosId == PointPos::end) + return aoc->getEndPoint(/*emulateCCW=*/true); + else if (PosId == PointPos::mid) + return aoc->getCenter(); + return Base::Vector3d(); +} + +template <> +Base::Vector3d SketchObject::getPointForGeometry<>(const Part::GeomArcOfEllipse *aoe, PointPos PosId) +{ + if (PosId == PointPos::start) + return aoe->getStartPoint(/*emulateCCW=*/true); + else if (PosId == PointPos::end) + return aoe->getEndPoint(/*emulateCCW=*/true); + else if (PosId == PointPos::mid) + return aoe->getCenter(); + return Base::Vector3d(); +} + +template <> +Base::Vector3d SketchObject::getPointForGeometry<>(const Part::GeomArcOfHyperbola *aoh, PointPos PosId) +{ + if (PosId == PointPos::start) + return aoh->getStartPoint(); + else if (PosId == PointPos::end) + return aoh->getEndPoint(); + else if (PosId == PointPos::mid) + return aoh->getCenter(); + return Base::Vector3d(); +} + +template <> +Base::Vector3d SketchObject::getPointForGeometry<>(const Part::GeomArcOfParabola *aop, PointPos PosId) +{ + if (PosId == PointPos::start) + return aop->getStartPoint(); + else if (PosId == PointPos::end) + return aop->getEndPoint(); + else if (PosId == PointPos::mid) + return aop->getCenter(); + return Base::Vector3d(); +} + +template <> +Base::Vector3d SketchObject::getPointForGeometry<>(const Part::GeomBSplineCurve *bsp, PointPos PosId) +{ + if (PosId == PointPos::start) + return bsp->getStartPoint(); + else if (PosId == PointPos::end) + return bsp->getEndPoint(); + return Base::Vector3d(); +} + Base::Vector3d SketchObject::getPoint(const Part::Geometry *geo, PointPos PosId) { if (geo->is()) { - const Part::GeomPoint *p = static_cast(geo); - if (PosId == PointPos::start || PosId == PointPos::mid || PosId == PointPos::end) - return p->getPoint(); - } else if (geo->is()) { - const Part::GeomLineSegment *lineSeg = static_cast(geo); - if (PosId == PointPos::start) - return lineSeg->getStartPoint(); - else if (PosId == PointPos::end) - return lineSeg->getEndPoint(); - } else if (geo->is()) { - const Part::GeomCircle *circle = static_cast(geo); - auto pt = circle->getCenter(); - if(PosId != PointPos::mid) - pt.x += circle->getRadius(); - return pt; - } else if (geo->is()) { - const Part::GeomEllipse *ellipse = static_cast(geo); - auto pt = ellipse->getCenter(); - if(PosId != PointPos::mid) - pt += ellipse->getMajorAxisDir()*ellipse->getMajorRadius(); - return pt; - } else if (geo->is()) { - const Part::GeomArcOfCircle *aoc = static_cast(geo); - if (PosId == PointPos::start) - return aoc->getStartPoint(/*emulateCCW=*/true); - else if (PosId == PointPos::end) - return aoc->getEndPoint(/*emulateCCW=*/true); - else if (PosId == PointPos::mid) - return aoc->getCenter(); - } else if (geo->is()) { - const Part::GeomArcOfEllipse *aoc = static_cast(geo); - if (PosId == PointPos::start) - return aoc->getStartPoint(/*emulateCCW=*/true); - else if (PosId == PointPos::end) - return aoc->getEndPoint(/*emulateCCW=*/true); - else if (PosId == PointPos::mid) - return aoc->getCenter(); - } else if (geo->is()) { - const Part::GeomArcOfHyperbola *aoh = static_cast(geo); - if (PosId == PointPos::start) - return aoh->getStartPoint(); - else if (PosId == PointPos::end) - return aoh->getEndPoint(); - else if (PosId == PointPos::mid) - return aoh->getCenter(); - } else if (geo->is()) { - const Part::GeomArcOfParabola *aop = static_cast(geo); - if (PosId == PointPos::start) - return aop->getStartPoint(); - else if (PosId == PointPos::end) - return aop->getEndPoint(); - else if (PosId == PointPos::mid) - return aop->getCenter(); - } else if (geo->is()) { - const Part::GeomBSplineCurve *bsp = static_cast(geo); - if (PosId == PointPos::start) - return bsp->getStartPoint(); - else if (PosId == PointPos::end) - return bsp->getEndPoint(); + return getPointForGeometry(static_cast(geo), PosId); + } + else if (geo->is()) { + return getPointForGeometry(static_cast(geo), PosId); + } + else if (geo->is()) { + return getPointForGeometry(static_cast(geo), PosId); + } + else if (geo->is()) { + return getPointForGeometry(static_cast(geo), PosId); + } + else if (geo->is()) { + return getPointForGeometry(static_cast(geo), PosId); + } + else if (geo->is()) { + return getPointForGeometry(static_cast(geo), PosId); + } + else if (geo->is()) { + return getPointForGeometry(static_cast(geo), PosId); + } + else if (geo->is()) { + return getPointForGeometry(static_cast(geo), PosId); + } + else if (geo->is()) { + return getPointForGeometry(static_cast(geo), PosId); } return Base::Vector3d(); } diff --git a/src/Mod/Sketcher/App/SketchObject.h b/src/Mod/Sketcher/App/SketchObject.h index 7ad241928b..9d72c010ee 100644 --- a/src/Mod/Sketcher/App/SketchObject.h +++ b/src/Mod/Sketcher/App/SketchObject.h @@ -218,6 +218,9 @@ public: /// Sync frozen external geometries int syncGeometry(const std::vector& geoIds); + template + returnType performActionByGeomType(const Part::Geometry* geo); + /** returns a pointer to a given Geometry index, possible indexes are: * id>=0 for user defined geometries, * id==-1 for the horizontal sketch axis, @@ -357,6 +360,11 @@ public: /// retrieves the coordinates of a point static Base::Vector3d getPoint(const Part::Geometry* geo, PointPos PosId); Base::Vector3d getPoint(int GeoId, PointPos PosId) const; + template + static Base::Vector3d getPointForGeometry(const geomType* geo, PointPos PosId) + { + return Base::Vector3d(); + } /// toggle geometry to draft line int toggleConstruction(int GeoId);