Basic implementation of convex hull for 2d polyhedra.

The code was provided to me by Len Trigg via email.
stl_dim
Giles Bathgate 2011-04-09 10:27:50 +01:00
parent 18a11b5c3c
commit 9cc441025c
5 changed files with 77 additions and 16 deletions

View File

@ -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

View File

@ -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
{

View File

@ -30,11 +30,11 @@
#include "builtin.h"
#include "printutils.h"
#include "cgal.h"
#include <CGAL/ch_graham_andrew.h>
#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(CGAL_Nef_polyhedron2 a, CGAL_Nef_polyhedron2 b);
#endif
enum cgaladv_type_e {
@ -197,7 +197,7 @@ CGAL_Nef_polyhedron CgaladvNode::render_cgal_nef_polyhedron() const
}
if (N.dim == 2 && tmp.dim == 2) {
N.p2 = convexhull2(N.p2, tmp.p2);
}
}
v->progress_report();

View File

@ -0,0 +1,59 @@
/*
* 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(CGAL_Nef_polyhedron2 a, CGAL_Nef_polyhedron2 b);
extern CGAL_Poly2 nef2p2(CGAL_Nef_polyhedron2 p);
static std::list<CGAL_Nef_polyhedron2::Point> p2points(CGAL_Poly2 p2)
{
std::list<CGAL_Nef_polyhedron2::Point> points;
for (int j = 0; j < p2.size(); j++) {
double x = to_double(p2[j].x()), y = to_double(p2[j].y());
CGAL_Nef_polyhedron2::Point p =
CGAL_Nef_polyhedron2::Point(x, y);
points.push_back(p);
}
return points;
}
CGAL_Nef_polyhedron2 convexhull2(CGAL_Nef_polyhedron2 a, CGAL_Nef_polyhedron2 b)
{
CGAL_Poly2 ap = nef2p2(a);
std::list<CGAL_Nef_polyhedron2::Point> points = p2points(ap), result;
CGAL::convex_hull_2(points.begin(), points.end(),
std::back_inserter(result));
std::cout << result.size() << " points on the convex hull" << std::endl;
return CGAL_Nef_polyhedron2(result.begin(),
result.end(), CGAL_Nef_polyhedron2::INCLUDED);
}
#endif

View File

@ -33,14 +33,10 @@
#if 1
#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.
@ -80,9 +76,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;
@ -110,14 +106,14 @@ static Poly2 nef2p2(CGAL_Nef_polyhedron2 p)
double x = to_double(ep.x()), y = to_double(ep.y());
std::cout << "point " << ep << std::endl;
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 (int j = 0; j < p2.size(); j++) {
double x = to_double(p2[j].x());
@ -130,7 +126,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);
std::cout << "ap = "; print_polygon(ap);
std::cout << "bp = "; print_polygon(bp);
@ -141,7 +137,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);
std::cout << "result = "; print_polygon_with_holes(x);
// Make a CGAL_Nef_polyhedron2 out of just the boundary for starters