chainGeoms() and nextGeom() are now static methods

This commit is contained in:
Ian Rees 2016-05-06 23:34:42 +12:00 committed by wmayer
parent 88bc3574aa
commit c6bcfd84ce
3 changed files with 82 additions and 65 deletions

View File

@ -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<BaseGeom *>::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<Wire *>::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<Base::Vector2D> BaseGeom::findEndPoints()
{
std::vector<Base::Vector2D> 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<Base::Vector2D> verts = findEndPoints();
return verts[0];
}
Base::Vector2D BaseGeom::getEndPoint()
{
std::vector<Base::Vector2D> 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<TechDrawGeometry::BaseGeom*> TechDrawExport chainGeoms(std::vector<TechDrawGeometry::BaseGeom*> geoms)
/*static*/
BaseGeomPtrVector GeometryUtils::chainGeoms(BaseGeomPtrVector geoms)
{
std::vector<TechDrawGeometry::BaseGeom*> result;
BaseGeomPtrVector result;
std::vector<bool> 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<TechDrawGeometry::BaseGeom*> TechDrawExport chainGeoms(std::vector<T
return result;
}
//! find an unused geom starts or ends at atPoint. returns index[1:geoms.size()),reversed [true,false]
getNextReturnVal TechDrawExport nextGeom(Base::Vector2D atPoint,
std::vector<TechDrawGeometry::BaseGeom*> geoms,
std::vector<bool> used,
double tolerance)
/*static*/ GeometryUtils::ReturnType GeometryUtils::nextGeom(
Base::Vector2D atPoint,
BaseGeomPtrVector geoms,
std::vector<bool> used,
double tolerance )
{
getNextReturnVal result(0,false);
std::vector<TechDrawGeometry::BaseGeom*>::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

View File

@ -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<BaseGeom *> BaseGeomPtrVector;
class TechDrawExport Circle: public BaseGeom
{
public:
@ -205,7 +207,7 @@ class TechDrawExport Wire
class TechDrawExport Face
{
public:
Face();
Face() = default;
~Face();
std::vector<Wire *> 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<TechDrawGeometry::BaseGeom*> geoms,
std::vector<bool> 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<BaseGeom*> chainGeoms(std::vector<BaseGeom*> geoms);
};
std::vector<TechDrawGeometry::BaseGeom*> chainGeoms(std::vector<TechDrawGeometry::BaseGeom*> geoms);
getNextReturnVal nextGeom(Base::Vector2D atPoint,
std::vector<TechDrawGeometry::BaseGeom*> geoms,
std::vector<bool> used,
double tolerance);
} //end extern "C"
} //end namespace TechDrawGeometry
#endif //TECHDRAW_GEOMETRY_H

View File

@ -329,7 +329,7 @@ void QGIViewPart::drawViewPart()
}
//chain edges tail to nose into a closed region
std::vector<TechDrawGeometry::BaseGeom*> chained = TechDrawGeometry::chainGeoms(unChained);
auto chained( TechDrawGeometry::GeometryUtils::chainGeoms(unChained) );
//iterate through the chain to make QPainterPath
std::vector<TechDrawGeometry::BaseGeom*>::iterator itChain = chained.begin();