summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--openscad.pro4
-rw-r--r--src/cgal.h6
-rw-r--r--src/cgaladv.cc32
-rw-r--r--src/cgaladv_convexhull2.cc55
-rw-r--r--src/cgaladv_minkowski2.cc22
-rw-r--r--testdata/scad/convex_hull.scad5
6 files changed, 108 insertions, 16 deletions
diff --git a/openscad.pro b/openscad.pro
index 5b3a398..eecae9a 100644
--- a/openscad.pro
+++ b/openscad.pro
@@ -161,6 +161,7 @@ SOURCES += src/openscad.cc \
src/primitives.cc \
src/projection.cc \
src/cgaladv.cc \
+ src/cgaladv_convexhull2.cc \
src/cgaladv_minkowski3.cc \
src/cgaladv_minkowski2.cc \
src/surface.cc \
@@ -180,7 +181,7 @@ SOURCES += src/openscad.cc \
src/Preferences.cc \
src/progress.cc \
src/editor.cc \
- src/mathc99.cc
+ src/mathc99.cc
macx {
HEADERS += src/AppleEvents.h \
@@ -198,4 +199,3 @@ INSTALLS += examples
libraries.path = /usr/local/share/openscad/libraries/
libraries.files = libraries/*
INSTALLS += libraries
-
diff --git a/src/cgal.h b/src/cgal.h
index 6cbb251..f9161cc 100644
--- a/src/cgal.h
+++ b/src/cgal.h
@@ -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..6301cd3 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
@@ -103,6 +105,10 @@ AbstractNode *CgaladvModule::evaluate(const Context *ctx, const ModuleInstantiat
level = c.lookup_variable("level", true);
}
+ if (type == HULL) {
+ convexity = c.lookup_variable("convexity", true);
+ }
+
node->convexity = (int)convexity.num;
node->path = path;
node->subdiv_type = subdiv_type.text;
@@ -125,6 +131,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 +181,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);
+ 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();
diff --git a/src/cgaladv_convexhull2.cc b/src/cgaladv_convexhull2.cc
new file mode 100644
index 0000000..e1e466d
--- /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 (int 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 fb8dd4b..8d0bf62 100644
--- a/src/cgaladv_minkowski2.cc
+++ b/src/cgaladv_minkowski2.cc
@@ -31,14 +31,11 @@
#include "grid.h"
#include "cgal.h"
-#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);
-
-struct K2 : public CGAL::Exact_predicates_exact_constructions_kernel {};
-typedef CGAL::Polygon_2<K2> Poly2;
-typedef CGAL::Polygon_with_holes_2<K2> Poly2h;
+extern CGAL_Poly2 nef2p2(CGAL_Nef_polyhedron2 p);
//-----------------------------------------------------------------------------
// Pretty-print a CGAL polygon.
@@ -78,9 +75,9 @@ void print_polygon_with_holes (const CGAL::Polygon_with_holes_2<Kernel, Containe
return;
}
-static Poly2 nef2p2(CGAL_Nef_polyhedron2 p)
+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;
@@ -105,15 +102,14 @@ 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());
}
-
-static CGAL_Nef_polyhedron2 p2nef2(Poly2 p2) {
+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());
@@ -126,7 +122,7 @@ static CGAL_Nef_polyhedron2 p2nef2(Poly2 p2) {
CGAL_Nef_polyhedron2 minkowski2(CGAL_Nef_polyhedron2 a, CGAL_Nef_polyhedron2 b)
{
- Poly2 ap = nef2p2(a), bp = nef2p2(b);
+ CGAL_Poly2 ap = nef2p2(a), bp = nef2p2(b);
if (ap.size() == 0) {
PRINT("WARNING: minkowski() could not get any points from object 1!");
@@ -135,7 +131,7 @@ CGAL_Nef_polyhedron2 minkowski2(CGAL_Nef_polyhedron2 a, CGAL_Nef_polyhedron2 b)
PRINT("WARNING: minkowski() could not get any points from object 2!");
return CGAL_Nef_polyhedron2();
} else {
- Poly2h x = minkowski_sum_2(ap, bp);
+ 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());
diff --git a/testdata/scad/convex_hull.scad b/testdata/scad/convex_hull.scad
new file mode 100644
index 0000000..8255417
--- /dev/null
+++ b/testdata/scad/convex_hull.scad
@@ -0,0 +1,5 @@
+hull() {
+ translate([15,10,0])
+ circle(10);
+ circle(10);
+}
contact: Jan Huwald // Impressum