From c6bcfd84ce26e51b817db89e282b979bef1eccfa Mon Sep 17 00:00:00 2001 From: Ian Rees Date: Fri, 6 May 2016 23:34:42 +1200 Subject: [PATCH] chainGeoms() and nextGeom() are now static methods --- src/Mod/TechDraw/App/Geometry.cpp | 99 +++++++++++++++------------- src/Mod/TechDraw/App/Geometry.h | 46 ++++++++----- src/Mod/TechDraw/Gui/QGIViewPart.cpp | 2 +- 3 files changed, 82 insertions(+), 65 deletions(-) diff --git a/src/Mod/TechDraw/App/Geometry.cpp b/src/Mod/TechDraw/App/Geometry.cpp index dde027eff..f9bb06eb6 100644 --- a/src/Mod/TechDraw/App/Geometry.cpp +++ b/src/Mod/TechDraw/App/Geometry.cpp @@ -84,40 +84,36 @@ Wire::Wire() { } + Wire::Wire(const TopoDS_Wire &w) { TopExp_Explorer edges(w, TopAbs_EDGE); for (; edges.More(); edges.Next()) { - const TopoDS_Edge& edge = TopoDS::Edge(edges.Current()); - TechDrawGeometry::BaseGeom* base = TechDrawGeometry::BaseGeom::baseFactory(edge); - geoms.push_back(base); + const auto edge( TopoDS::Edge(edges.Current()) ); + geoms.push_back( BaseGeom::baseFactory(edge) ); } } + Wire::~Wire() { - for(std::vector::iterator it = geoms.begin(); it != geoms.end(); ++it) { - delete (*it); - *it = 0; + for(auto it : geoms) { + delete it; } geoms.clear(); } -Face::Face() -{ - -} Face::~Face() { - for(std::vector::iterator it = wires.begin(); it != wires.end(); ++it) { - delete (*it); - *it = 0; + for(auto it : wires) { + delete it; } wires.clear(); } + BaseGeom::BaseGeom() : geomType(NOTDEF), extractType(Plain), @@ -128,31 +124,35 @@ BaseGeom::BaseGeom() : { } + std::vector BaseGeom::findEndPoints() { std::vector result; - //if (occEdge) { - gp_Pnt p = BRep_Tool::Pnt(TopExp::FirstVertex(occEdge)); - result.push_back(Base::Vector2D(p.X(),p.Y())); - p = BRep_Tool::Pnt(TopExp::LastVertex(occEdge)); - result.push_back(Base::Vector2D(p.X(),p.Y())); - //} + + gp_Pnt p = BRep_Tool::Pnt(TopExp::FirstVertex(occEdge)); + result.push_back(Base::Vector2D(p.X(),p.Y())); + p = BRep_Tool::Pnt(TopExp::LastVertex(occEdge)); + result.push_back(Base::Vector2D(p.X(),p.Y())); + return result; } + Base::Vector2D BaseGeom::getStartPoint() { std::vector verts = findEndPoints(); return verts[0]; } + Base::Vector2D BaseGeom::getEndPoint() { std::vector verts = findEndPoints(); return verts[1]; } -//!convert 1 OCC edge into 1 BaseGeom (static factory method) + +//! Convert 1 OCC edge into 1 BaseGeom (static factory method) BaseGeom* BaseGeom::baseFactory(TopoDS_Edge edge) { BaseGeom* result = NULL; @@ -222,6 +222,7 @@ BaseGeom* BaseGeom::baseFactory(TopoDS_Edge edge) return result; } + Ellipse::Ellipse(const TopoDS_Edge &e) { geomType = ELLIPSE; @@ -266,6 +267,7 @@ AOE::AOE(const TopoDS_Edge &e) : Ellipse(e) midPnt = Base::Vector2D(m.X(), m.Y()); } + Circle::Circle(const TopoDS_Edge &e) { geomType = CIRCLE; @@ -279,6 +281,7 @@ Circle::Circle(const TopoDS_Edge &e) center = Base::Vector2D(p.X(), p.Y()); } + AOC::AOC(const TopoDS_Edge &e) : Circle(e) { geomType = ARCOFCIRCLE; @@ -306,7 +309,7 @@ AOC::AOC(const TopoDS_Edge &e) : Circle(e) } -//!Generic is a multiline +//! Generic is a multiline Generic::Generic(const TopoDS_Edge &e) { geomType = GENERIC; @@ -331,11 +334,13 @@ Generic::Generic(const TopoDS_Edge &e) } } + Generic::Generic() { geomType = GENERIC; } + BSpline::BSpline(const TopoDS_Edge &e) { geomType = BSPLINE; @@ -379,7 +384,8 @@ BSpline::BSpline(const TopoDS_Edge &e) } } -//! can this BSpline be represented by a straight line? + +//! Can this BSpline be represented by a straight line? bool BSpline::isLine() { bool result = false; @@ -391,6 +397,7 @@ bool BSpline::isLine() return result; } + //**** Vertex bool Vertex::isEqual(Vertex* v, double tol) { @@ -402,30 +409,29 @@ bool Vertex::isEqual(Vertex* v, double tol) return result; } -//**** TechDrawGeometry utility funtions -extern "C" { -//! return a vector of BaseGeom*'s in tail to nose order -//could/should this be replaced by DVP::connectEdges? -std::vector TechDrawExport chainGeoms(std::vector geoms) +/*static*/ +BaseGeomPtrVector GeometryUtils::chainGeoms(BaseGeomPtrVector geoms) { - std::vector result; + BaseGeomPtrVector result; std::vector used(geoms.size(),false); if (geoms.empty()) { return result; } - if (geoms.size() == 1) { //don't bother for single geom (circles, ellipses,etc) + if (geoms.size() == 1) { + //don't bother for single geom (circles, ellipses,etc) result.push_back(geoms[0]); } else { - result.push_back(geoms[0]); //start with first edge + //start with first edge + result.push_back(geoms[0]); Base::Vector2D atPoint = (geoms[0])->getEndPoint(); used[0] = true; - for (unsigned int i = 1; i < geoms.size(); i++) { //do size-1 more edges - getNextReturnVal next = nextGeom(atPoint,geoms,used,Precision::Confusion()); - if (next.index) { //found an unused edge with vertex == atPoint - TechDrawGeometry::BaseGeom* nextEdge = geoms.at(next.index); + for (unsigned int i = 1; i < geoms.size(); i++) { //do size-1 more edges + auto next( nextGeom(atPoint, geoms, used, Precision::Confusion()) ); + if (next.index) { //found an unused edge with vertex == atPoint + BaseGeom* nextEdge = geoms.at(next.index); used[next.index] = true; nextEdge->reversed = next.reversed; result.push_back(nextEdge); @@ -443,30 +449,31 @@ std::vector TechDrawExport chainGeoms(std::vector geoms, - std::vector used, - double tolerance) + +/*static*/ GeometryUtils::ReturnType GeometryUtils::nextGeom( + Base::Vector2D atPoint, + BaseGeomPtrVector geoms, + std::vector used, + double tolerance ) { - getNextReturnVal result(0,false); - std::vector::iterator itGeom = geoms.begin(); - for (; itGeom != geoms.end(); itGeom++) { - unsigned int index = itGeom - geoms.begin(); + ReturnType result(0, false); + auto index(0); + for (auto itGeom : geoms) { if (used[index]) { + ++index; continue; } - if ((atPoint - (*itGeom)->getStartPoint()).Length() < tolerance) { + if ((atPoint - itGeom->getStartPoint()).Length() < tolerance) { result.index = index; result.reversed = false; break; - } else if ((atPoint - (*itGeom)->getEndPoint()).Length() < tolerance) { + } else if ((atPoint - itGeom->getEndPoint()).Length() < tolerance) { result.index = index; result.reversed = true; break; } + ++index; } return result; } -} //end extern C diff --git a/src/Mod/TechDraw/App/Geometry.h b/src/Mod/TechDraw/App/Geometry.h index 34983e1f8..2b0276f94 100644 --- a/src/Mod/TechDraw/App/Geometry.h +++ b/src/Mod/TechDraw/App/Geometry.h @@ -40,7 +40,7 @@ enum ExtractionType { //obs sb vis/hid + hard/smooth/seam/out(edge }; enum edgeClass { - ecNONE, //not used, OCC index starts at 1 + ecNONE, // Not used, OCC index starts at 1 ecUVISO, ecOUTLINE, ecSMOOTH, @@ -79,6 +79,8 @@ class TechDrawExport BaseGeom static BaseGeom* baseFactory(TopoDS_Edge edge); }; +typedef std::vector BaseGeomPtrVector; + class TechDrawExport Circle: public BaseGeom { public: @@ -205,7 +207,7 @@ class TechDrawExport Wire class TechDrawExport Face { public: - Face(); + Face() = default; ~Face(); std::vector wires; @@ -227,26 +229,34 @@ class TechDrawExport Vertex bool isEqual(Vertex* v, double tol); }; +/// Encapsulates some useful static methods +class TechDrawExport GeometryUtils +{ + public: + /// Used by nextGeom() + struct TechDrawExport ReturnType { + unsigned int index; + bool reversed; + explicit ReturnType(int i = 0, bool r = false) : + index(i), + reversed(r) + {} + }; -//*** utility functions -extern "C" { + /// Find an unused geom starts or ends at atPoint. + /*! + * returns index[1:geoms.size()),reversed [true,false] + */ + static ReturnType nextGeom( Base::Vector2D atPoint, + std::vector geoms, + std::vector used, + double tolerance ); -struct TechDrawExport getNextReturnVal { - unsigned int index; - bool reversed; - explicit getNextReturnVal(int i = 0, bool r = false) : - index(i), - reversed(r) - {} + //! return a vector of BaseGeom*'s in tail to nose order + //could/should this be replaced by DVP::connectEdges? + static std::vector chainGeoms(std::vector geoms); }; -std::vector chainGeoms(std::vector geoms); -getNextReturnVal nextGeom(Base::Vector2D atPoint, - std::vector geoms, - std::vector used, - double tolerance); - -} //end extern "C" } //end namespace TechDrawGeometry #endif //TECHDRAW_GEOMETRY_H diff --git a/src/Mod/TechDraw/Gui/QGIViewPart.cpp b/src/Mod/TechDraw/Gui/QGIViewPart.cpp index 4b3093026..a2107d48e 100644 --- a/src/Mod/TechDraw/Gui/QGIViewPart.cpp +++ b/src/Mod/TechDraw/Gui/QGIViewPart.cpp @@ -329,7 +329,7 @@ void QGIViewPart::drawViewPart() } //chain edges tail to nose into a closed region - std::vector chained = TechDrawGeometry::chainGeoms(unChained); + auto chained( TechDrawGeometry::GeometryUtils::chainGeoms(unChained) ); //iterate through the chain to make QPainterPath std::vector::iterator itChain = chained.begin();