Implemented 2D hull

stl_dim
Marius Kintel 2011-09-06 22:03:52 +02:00
parent a93e644980
commit a653b0c604
3 changed files with 46 additions and 15 deletions

View File

@ -63,12 +63,6 @@ void CGALEvaluator::process(CGAL_Nef_polyhedron &target, const CGAL_Nef_polyhedr
case CGE_MINKOWSKI:
target.minkowski(src);
break;
case CGE_HULL:
//FIXME: Port convex hull to a binary operator or process it all in the end somehow
// target.p2 = convexhull2(target.p2, src.p2);
// target.p2 = convexhull2(polys);
// FIXME: Print warning: hull() not supported in 3D
break;
}
}
@ -100,6 +94,40 @@ void CGALEvaluator::applyToChildren(const AbstractNode &node, CGALEvaluator::Csg
this->cache.insert(cacheid, N);
}
extern CGAL_Nef_polyhedron2 *convexhull2(std::list<CGAL_Nef_polyhedron2*> a);
void CGALEvaluator::applyHull(const CgaladvNode &node)
{
if (this->visitedchildren[node.index()].size() > 0) {
std::list<CGAL_Nef_polyhedron2*> polys;
bool all2d = true;
for (ChildList::const_iterator iter = this->visitedchildren[node.index()].begin();
iter != this->visitedchildren[node.index()].end();
iter++) {
const AbstractNode *chnode = iter->first;
const string &chcacheid = iter->second;
// FIXME: Don't use deep access to modinst members
if (chnode->modinst->tag_background) continue;
assert(isCached(*chnode));
const CGAL_Nef_polyhedron &ch = this->cache[chcacheid];
if (ch.dim == 2) {
polys.push_back(ch.p2);
}
else if (ch.dim == 3) {
PRINT("WARNING: hull() is not implemented yet for 3D objects!");
all2d = false;
}
chnode->progress_report();
}
if (all2d) {
CGAL_Nef_polyhedron N(convexhull2(polys));
const std::string &cacheid = this->tree.getString(node);
this->cache.insert(cacheid, N);
}
}
}
/*
Typical visitor behavior:
o In prefix: Check if we're cached -> prune
@ -246,17 +274,20 @@ Response CGALEvaluator::visit(State &state, const CgaladvNode &node)
switch (node.type) {
case MINKOWSKI:
op = CGE_MINKOWSKI;
applyToChildren(node, op);
break;
case GLIDE:
PRINT("WARNING: glide() is not implemented yet!");
return PruneTraversal;
break;
case SUBDIV:
// FIXME: Not implemented
PRINT("WARNING: subdiv() is not implemented yet!");
return PruneTraversal;
break;
case HULL:
op = CGE_HULL;
applyHull(node);
break;
}
applyToChildren(node, op);
}
addToParent(state, node);
}

View File

@ -19,7 +19,7 @@ using std::pair;
class CGALEvaluator : public Visitor
{
public:
enum CsgOp {CGE_UNION, CGE_INTERSECTION, CGE_DIFFERENCE, CGE_MINKOWSKI, CGE_HULL};
enum CsgOp {CGE_UNION, CGE_INTERSECTION, CGE_DIFFERENCE, CGE_MINKOWSKI};
// FIXME: If a cache is not given, we need to fix this ourselves
CGALEvaluator(QHash<string, CGAL_Nef_polyhedron> &cache, const Tree &tree) : cache(cache), tree(tree), psevaluator(*this) {}
virtual ~CGALEvaluator() {}
@ -41,6 +41,7 @@ private:
bool isCached(const AbstractNode &node) const;
void process(CGAL_Nef_polyhedron &target, const CGAL_Nef_polyhedron &src, CGALEvaluator::CsgOp op);
void applyToChildren(const AbstractNode &node, CGALEvaluator::CsgOp op);
void applyHull(const CgaladvNode &node);
string currindent;
typedef list<pair<const AbstractNode *, string> > ChildList;

View File

@ -29,16 +29,15 @@
#include "cgal.h"
#include <CGAL/convex_hull_2.h>
extern CGAL_Nef_polyhedron2 convexhull2(std::list<CGAL_Nef_polyhedron2> a);
extern CGAL_Poly2 nef2p2(CGAL_Nef_polyhedron2 p);
CGAL_Nef_polyhedron2 convexhull2(std::list<CGAL_Nef_polyhedron2> a)
CGAL_Nef_polyhedron2 *convexhull2(std::list<CGAL_Nef_polyhedron2*> a)
{
std::list<CGAL_Nef_polyhedron2::Point> points;
std::list<CGAL_Nef_polyhedron2>::iterator i;
std::list<CGAL_Nef_polyhedron2*>::iterator i;
for (i=a.begin(); i!=a.end(); i++) {
CGAL_Poly2 ap=nef2p2(*i);
CGAL_Poly2 ap=nef2p2(**i);
for (size_t j=0;j<ap.size();j++) {
double x=to_double(ap[j].x()),y=to_double(ap[j].y());
CGAL_Nef_polyhedron2::Point p=CGAL_Nef_polyhedron2::Point(x,y);
@ -49,7 +48,7 @@ CGAL_Nef_polyhedron2 convexhull2(std::list<CGAL_Nef_polyhedron2> a)
std::list<CGAL_Nef_polyhedron2::Point> result;
CGAL::convex_hull_2(points.begin(),points.end(),std::back_inserter(result));
return CGAL_Nef_polyhedron2(result.begin(),result.end(),CGAL_Nef_polyhedron2::INCLUDED);
return new CGAL_Nef_polyhedron2(result.begin(),result.end(),CGAL_Nef_polyhedron2::INCLUDED);
}
#endif