diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cgal.h | 6 | ||||
-rw-r--r-- | src/cgaladv.cc | 35 | ||||
-rw-r--r-- | src/cgaladv_convexhull2.cc | 55 | ||||
-rw-r--r-- | src/cgaladv_minkowski2.cc | 97 | ||||
-rw-r--r-- | src/openscad.cc | 24 |
5 files changed, 180 insertions, 37 deletions
@@ -10,6 +10,9 @@ #include <CGAL/Polyhedron_3.h> #include <CGAL/Nef_polyhedron_3.h> #include <CGAL/IO/Polyhedron_iostream.h> +#include <CGAL/Exact_predicates_exact_constructions_kernel.h> +#include <CGAL/Polygon_2.h> +#include <CGAL/Polygon_with_holes_2.h> typedef CGAL::Extended_cartesian<CGAL::Gmpq> CGAL_Kernel2; typedef CGAL::Nef_polyhedron_2<CGAL_Kernel2> CGAL_Nef_polyhedron2; @@ -24,6 +27,9 @@ typedef CGAL_Nef_polyhedron3::Aff_transformation_3 CGAL_Aff_transformation; typedef CGAL_Nef_polyhedron3::Vector_3 CGAL_Vector; typedef CGAL_Nef_polyhedron3::Plane_3 CGAL_Plane; typedef CGAL_Nef_polyhedron3::Point_3 CGAL_Point; +typedef CGAL::Exact_predicates_exact_constructions_kernel CGAL_ExactKernel2; +typedef CGAL::Polygon_2<CGAL_ExactKernel2> CGAL_Poly2; +typedef CGAL::Polygon_with_holes_2<CGAL_ExactKernel2> CGAL_Poly2h; struct CGAL_Nef_polyhedron { diff --git a/src/cgaladv.cc b/src/cgaladv.cc index bf2e85a..dd797fd 100644 --- a/src/cgaladv.cc +++ b/src/cgaladv.cc @@ -34,12 +34,14 @@ #ifdef ENABLE_CGAL extern CGAL_Nef_polyhedron3 minkowski3(CGAL_Nef_polyhedron3 a, CGAL_Nef_polyhedron3 b); extern CGAL_Nef_polyhedron2 minkowski2(CGAL_Nef_polyhedron2 a, CGAL_Nef_polyhedron2 b); +extern CGAL_Nef_polyhedron2 convexhull2(std::list<CGAL_Nef_polyhedron2> a); #endif enum cgaladv_type_e { MINKOWSKI, GLIDE, - SUBDIV + SUBDIV, + HULL }; class CgaladvModule : public AbstractModule @@ -125,6 +127,7 @@ void register_builtin_cgaladv() builtin_modules["minkowski"] = new CgaladvModule(MINKOWSKI); builtin_modules["glide"] = new CgaladvModule(GLIDE); builtin_modules["subdiv"] = new CgaladvModule(SUBDIV); + builtin_modules["hull"] = new CgaladvModule(HULL); } #ifdef ENABLE_CGAL @@ -174,6 +177,29 @@ CGAL_Nef_polyhedron CgaladvNode::render_cgal_nef_polyhedron() const PRINT("WARNING: subdiv() is not implemented yet!"); } + if (type == HULL) + { + std::list<CGAL_Nef_polyhedron2> polys; + bool all2d = true; + foreach(AbstractNode * v, children) { + if (v->modinst->tag_background) + continue; + N = v->render_cgal_nef_polyhedron(); + if (N.dim == 3) { + //polys.push_back(tmp.p3); + PRINT("WARNING: hull() is not implemented yet for 3D objects!"); + all2d=false; + } + if (N.dim == 2) { + polys.push_back(N.p2); + } + v->progress_report(); + } + + if (all2d) + N.p2 = convexhull2(polys); + } + cgal_nef_cache.insert(cache_id, new cgal_nef_cache_entry(N), N.weight()); print_messages_pop(); progress_report(); @@ -192,6 +218,9 @@ CSGTerm *CgaladvNode::render_csg_term(double m[20], QVector<CSGTerm*> *highlight if (type == SUBDIV) return render_csg_term_from_nef(m, highlights, background, "subdiv", this->convexity); + if (type == HULL) + return render_csg_term_from_nef(m, highlights, background, "hull", this->convexity); + return NULL; } @@ -199,7 +228,7 @@ CSGTerm *CgaladvNode::render_csg_term(double m[20], QVector<CSGTerm*> *highlight CSGTerm *CgaladvNode::render_csg_term(double m[20], QVector<CSGTerm*> *highlights, QVector<CSGTerm*> *background) const { - PRINT("WARNING: Found minkowski(), glide() or subdiv() statement but compiled without CGAL support!"); + PRINT("WARNING: Found minkowski(), glide(), subdiv() or hull() statement but compiled without CGAL support!"); return NULL; } @@ -217,6 +246,8 @@ QString CgaladvNode::dump(QString indent) const } if (type == SUBDIV) text.sprintf("subdiv(level = %d, convexity = %d) {\n", this->level, this->convexity); + if (type == HULL) + text.sprintf("hull() {\n"); foreach (AbstractNode *v, this->children) text += v->dump(indent + QString("\t")); text += indent + "}\n"; diff --git a/src/cgaladv_convexhull2.cc b/src/cgaladv_convexhull2.cc new file mode 100644 index 0000000..448dd4b --- /dev/null +++ b/src/cgaladv_convexhull2.cc @@ -0,0 +1,55 @@ +/* + * OpenSCAD (www.openscad.org) + * Copyright (C) 2009-2011 Clifford Wolf <clifford@clifford.at> and + * Marius Kintel <marius@kintel.net> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * As a special exception, you have permission to link this program + * with the CGAL library and distribute executables, as long as you + * follow the requirements of the GNU GPL in regard to all of the + * software in the executable aside from CGAL. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifdef ENABLE_CGAL + +#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) +{ + std::list<CGAL_Nef_polyhedron2::Point> points; + + std::list<CGAL_Nef_polyhedron2>::iterator i; + for (i=a.begin(); i!=a.end(); 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); + points.push_back(p); + } + } + + 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); +} + +#endif diff --git a/src/cgaladv_minkowski2.cc b/src/cgaladv_minkowski2.cc index 6a4b31c..8d0bf62 100644 --- a/src/cgaladv_minkowski2.cc +++ b/src/cgaladv_minkowski2.cc @@ -31,20 +31,53 @@ #include "grid.h" #include "cgal.h" -#if 0 -#include <CGAL/Exact_predicates_exact_constructions_kernel.h> #include <CGAL/minkowski_sum_2.h> extern CGAL_Nef_polyhedron2 minkowski2(CGAL_Nef_polyhedron2 a, CGAL_Nef_polyhedron2 b); +extern CGAL_Poly2 nef2p2(CGAL_Nef_polyhedron2 p); -struct K2 : public CGAL::Exact_predicates_exact_constructions_kernel {}; -typedef CGAL::Polygon_2<K2> Poly2; -typedef CGAL::Polygon_with_holes_2<K2> Poly2h; +//----------------------------------------------------------------------------- +// Pretty-print a CGAL polygon. +// +template<class Kernel, class Container> +void print_polygon (const CGAL::Polygon_2<Kernel, Container>& P) +{ + typename CGAL::Polygon_2<Kernel, Container>::Vertex_const_iterator vit; + + std::cout << "[ " << P.size() << " vertices:"; + for (vit = P.vertices_begin(); vit != P.vertices_end(); ++vit) + std::cout << " (" << *vit << ')'; + std::cout << " ]" << std::endl; +} -static Poly2 nef2p2(CGAL_Nef_polyhedron2 p) +//----------------------------------------------------------------------------- +// Pretty-print a polygon with holes. +// +template<class Kernel, class Container> +void print_polygon_with_holes (const CGAL::Polygon_with_holes_2<Kernel, Container>& pwh) { + if (! pwh.is_unbounded()) { + std::cout << "{ Outer boundary = "; + print_polygon (pwh.outer_boundary()); + } else + std::cout << "{ Unbounded polygon." << std::endl; + + typename CGAL::Polygon_with_holes_2<Kernel,Container>::Hole_const_iterator hit; + unsigned int k = 1; + + std::cout << " " << pwh.number_of_holes() << " holes:" << std::endl; + for (hit = pwh.holes_begin(); hit != pwh.holes_end(); ++hit, ++k) { + std::cout << " Hole #" << k << " = "; + print_polygon (*hit); + } + std::cout << " }" << std::endl; + + return; +} + +CGAL_Poly2 nef2p2(CGAL_Nef_polyhedron2 p) { - std::list<K2::Point_2> points; + std::list<CGAL_ExactKernel2::Point_2> points; Grid2d<int> grid(GRID_COARSE); typedef CGAL_Nef_polyhedron2::Explorer Explorer; @@ -54,7 +87,11 @@ static Poly2 nef2p2(CGAL_Nef_polyhedron2 p) for (fci_t fit = E.faces_begin(), fend = E.faces_end(); fit != fend; ++fit) { - if (fit != E.faces_begin()) { + if (!E.mark(fit)) { + continue; + } + //if (fit != E.faces_begin()) { + if (points.size() != 0) { PRINT("WARNING: minkowski() is not implemented for 2d objects with holes!"); break; } @@ -65,33 +102,41 @@ static Poly2 nef2p2(CGAL_Nef_polyhedron2 p) Explorer::Point ep = E.point(E.target(fcirc)); double x = to_double(ep.x()), y = to_double(ep.y()); grid.align(x, y); - points.push_back(K2::Point_2(x, y)); + points.push_back(CGAL_ExactKernel2::Point_2(x, y)); } } } - return Poly2(points.begin(), points.end()); + return CGAL_Poly2(points.begin(), points.end()); } - -CGAL_Nef_polyhedron2 minkowski2(CGAL_Nef_polyhedron2 a, CGAL_Nef_polyhedron2 b) -{ - Poly2 ap = nef2p2(a), bp = nef2p2(b); - Poly2h x = minkowski_sum_2(ap, bp); - /** FIXME **/ - - PRINT("WARNING: minkowski() is not implemented yet for 2d objects!"); - return CGAL_Nef_polyhedron2(); +static CGAL_Nef_polyhedron2 p2nef2(CGAL_Poly2 p2) { + std::list<CGAL_Nef_polyhedron2::Point> points; + for (size_t j = 0; j < p2.size(); j++) { + double x = to_double(p2[j].x()); + double y = to_double(p2[j].y()); + CGAL_Nef_polyhedron2::Point p = CGAL_Nef_polyhedron2::Point(x, y); + points.push_back(p); + } + return CGAL_Nef_polyhedron2(points.begin(), points.end(), CGAL_Nef_polyhedron2::INCLUDED); } -#else - -CGAL_Nef_polyhedron2 minkowski2(CGAL_Nef_polyhedron2, CGAL_Nef_polyhedron2) +CGAL_Nef_polyhedron2 minkowski2(CGAL_Nef_polyhedron2 a, CGAL_Nef_polyhedron2 b) { - PRINT("WARNING: minkowski() is not implemented yet for 2d objects!"); - return CGAL_Nef_polyhedron2(); + CGAL_Poly2 ap = nef2p2(a), bp = nef2p2(b); + + if (ap.size() == 0) { + PRINT("WARNING: minkowski() could not get any points from object 1!"); + return CGAL_Nef_polyhedron2(); + } else if (bp.size() == 0) { + PRINT("WARNING: minkowski() could not get any points from object 2!"); + return CGAL_Nef_polyhedron2(); + } else { + CGAL_Poly2h x = minkowski_sum_2(ap, bp); + + // Make a CGAL_Nef_polyhedron2 out of just the boundary for starters + return p2nef2(x.outer_boundary()); + } } #endif -#endif - diff --git a/src/openscad.cc b/src/openscad.cc index bc1d845..bf22246 100644 --- a/src/openscad.cc +++ b/src/openscad.cc @@ -140,19 +140,25 @@ int main(int argc, char **argv) desc.add_options() ("help,h", "help message") ("version,v", "print the version") - ("s", po::value<string>(), "stl-file") - ("o", po::value<string>(), "off-file") - ("x", po::value<string>(), "dxf-file") - ("d", po::value<string>(), "deps-file") - ("m", po::value<string>(), "make file") - ("D", po::value<vector<string> >(), "var=val") - ; + ("s,s", po::value<string>(), "stl-file") + ("o,o", po::value<string>(), "off-file") + ("x,x", po::value<string>(), "dxf-file") + ("d,d", po::value<string>(), "deps-file") + ("m,m", po::value<string>(), "makefile") + ("D,D", po::value<vector<string> >(), "var=val"); + + po::options_description hidden("Hidden options"); + hidden.add_options() + ("input-file", po::value< vector<string> >(), "input file"); po::positional_options_description p; p.add("input-file", -1); + po::options_description all_options; + all_options.add(desc).add(hidden); + po::variables_map vm; - po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); + po::store(po::command_line_parser(argc, argv).options(all_options).positional(p).run(), vm); // po::notify(vm); if (vm.count("help")) help(argv[0]); @@ -348,7 +354,7 @@ int main(int argc, char **argv) new MainWindow(qfilename); vector<string> inputFiles; if (vm.count("input-file")) { - inputFiles = vm["input-files"].as<vector<string> >(); + inputFiles = vm["input-file"].as<vector<string> >(); for (vector<string>::const_iterator i = inputFiles.begin()+1; i != inputFiles.end(); i++) { new MainWindow(QFileInfo(original_path, i->c_str()).absoluteFilePath()); } |