generalization for more than one concave hulls.

This commit is contained in:
tamasmeszaros 2018-08-27 15:18:28 +02:00
parent e667203825
commit 4c1bb6888c

View File

@ -38,6 +38,14 @@ inline coord_t x(const Vec3crd& p) { return p(0); }
inline coord_t y(const Vec3crd& p) { return p(1); } inline coord_t y(const Vec3crd& p) { return p(1); }
inline coord_t z(const Vec3crd& p) { return p(2); } inline coord_t z(const Vec3crd& p) { return p(2); }
inline void triangulate(const ExPolygon& expoly, Polygons& triangles) {
expoly.triangulate_p2t(&triangles);
}
inline Polygons triangulate(const ExPolygon& expoly) {
Polygons tri; triangulate(expoly, tri); return tri;
}
using Indices = std::vector<Vec3crd>; using Indices = std::vector<Vec3crd>;
/// Intermediate struct for a 3D mesh /// Intermediate struct for a 3D mesh
@ -81,8 +89,7 @@ inline Contour3D convert(const Polygons& triangles, coord_t z, bool dir) {
/// Only a debug function to generate top and bottom plates from a 2D shape. /// Only a debug function to generate top and bottom plates from a 2D shape.
/// It is not used in the algorithm directly. /// It is not used in the algorithm directly.
inline Contour3D roofs(const ExPolygon& poly, coord_t z_distance) { inline Contour3D roofs(const ExPolygon& poly, coord_t z_distance) {
Polygons triangles; Polygons triangles = triangulate(poly);
poly.triangulate_pp(&triangles);
auto lower = convert(triangles, 0, false); auto lower = convert(triangles, 0, false);
auto upper = convert(triangles, z_distance, true); auto upper = convert(triangles, z_distance, true);
@ -99,8 +106,7 @@ inline Contour3D walls(const ExPolygon& floor_plate, const ExPolygon& ceiling,
poly.holes.emplace_back(ceiling.contour); poly.holes.emplace_back(ceiling.contour);
auto& h = poly.holes.front(); auto& h = poly.holes.front();
std::reverse(h.points.begin(), h.points.end()); std::reverse(h.points.begin(), h.points.end());
Polygons tri; Polygons tri = triangulate(poly);
poly.triangulate_p2t(&tri);
Contour3D ret; Contour3D ret;
ret.points.reserve(tri.size() * 3); ret.points.reserve(tri.size() * 3);
@ -258,8 +264,7 @@ inline Contour3D round_edges(const ExPolygon& base_plate,
inline Contour3D inner_bed(const ExPolygon& poly, double depth_mm, inline Contour3D inner_bed(const ExPolygon& poly, double depth_mm,
double begin_h_mm = 0) { double begin_h_mm = 0) {
Polygons triangles; Polygons triangles = triangulate(poly);
poly.triangulate_p2t(&triangles);
coord_t depth = mm(depth_mm); coord_t depth = mm(depth_mm);
coord_t begin_h = mm(begin_h_mm); coord_t begin_h = mm(begin_h_mm);
@ -367,14 +372,13 @@ inline Point centroid(const ExPolygon& poly) {
/// with explicit bridges. Bridges are generated from each shape's centroid /// with explicit bridges. Bridges are generated from each shape's centroid
/// to the center of the "scene" which is the centroid calculated from the shape /// to the center of the "scene" which is the centroid calculated from the shape
/// centroids (a star is created...) /// centroids (a star is created...)
inline ExPolygon concave_hull(const ExPolygons& polys) { inline ExPolygons concave_hull(const ExPolygons& polys, double max_dist_mm = 0)
if(polys.empty()) return ExPolygon(); {
if(polys.empty()) return ExPolygons();
ExPolygons punion = unify(polys); // could be redundant ExPolygons punion = unify(polys); // could be redundant
ExPolygon ret; if(punion.size() == 1) return punion;
if(punion.size() == 1) return punion.front();
// We get the centroids of all the islands in the 2D slice // We get the centroids of all the islands in the 2D slice
Points centroids; centroids.reserve(punion.size()); Points centroids; centroids.reserve(punion.size());
@ -389,12 +393,14 @@ inline ExPolygon concave_hull(const ExPolygons& polys) {
std::transform(centroids.begin(), centroids.end(), std::transform(centroids.begin(), centroids.end(),
std::back_inserter(punion), std::back_inserter(punion),
[cc](const Point& c) { [cc, max_dist_mm](const Point& c) {
double dx = x(c) - x(cc), dy = y(c) - y(cc); double dx = x(c) - x(cc), dy = y(c) - y(cc);
double l = std::sqrt(dx * dx + dy * dy); double l = std::sqrt(dx * dx + dy * dy);
double nx = dx / l, ny = dy / l; double nx = dx / l, ny = dy / l;
if(l < max_dist_mm) return ExPolygon();
ExPolygon r; ExPolygon r;
auto& ctour = r.contour.points; auto& ctour = r.contour.points;
@ -414,9 +420,7 @@ inline ExPolygon concave_hull(const ExPolygons& polys) {
if(punion.size() != 1) if(punion.size() != 1)
BOOST_LOG_TRIVIAL(error) << "Cannot generate correct SLA base pool!"; BOOST_LOG_TRIVIAL(error) << "Cannot generate correct SLA base pool!";
if(!punion.empty()) ret = punion.front(); return punion;
return ret;
} }
} }
@ -437,127 +441,75 @@ void create_base_pool(const ExPolygons &ground_layer, TriangleMesh& out,
double min_wall_thickness_mm, double min_wall_thickness_mm,
double min_wall_height_mm) double min_wall_height_mm)
{ {
// Contour3D pool; auto concavehs = concave_hull(ground_layer);
for(ExPolygon& concaveh : concavehs) {
if(concaveh.contour.points.empty()) return;
concaveh.holes.clear();
// auto& poly = ground_layer.front(); BoundingBox bb(concaveh);
// auto floor_poly = poly; coord_t w = x(bb.max) - x(bb.min);
// offset(floor_poly, mm(5)); coord_t h = y(bb.max) - y(bb.min);
// Polygons floor_plate; auto wall_thickness = coord_t(std::pow((w+h)*0.1, 0.8));
// floor_poly.triangulate_p2t(&floor_plate);
// auto floor_mesh = convert(floor_plate, mm(20), false);
// pool.merge(floor_mesh);
// Polygons ceil_plate; const coord_t WALL_THICKNESS = mm(min_wall_thickness_mm) +
// poly.triangulate_p2t(&ceil_plate); wall_thickness;
// auto ceil_mesh = convert(ceil_plate, mm(0), true);
// pool.merge(ceil_mesh);
// auto ob = floor_poly; const coord_t WALL_DISTANCE = coord_t(0.3*WALL_THICKNESS);
// auto ob_prev = ob; const coord_t HEIGHT = mm(min_wall_height_mm);
// double wh = 20, wh_prev = wh;
// Contour3D curvedwalls;
// const size_t steps = 6; auto outer_base = concaveh;
// const double radius_mm = 1; offset(outer_base, WALL_THICKNESS+WALL_DISTANCE);
// const double ystep_mm = radius_mm/steps; auto inner_base = outer_base;
// const double ceilheight_mm = 20; offset(inner_base, -WALL_THICKNESS);
// for(int i = 0; i < 1.5*steps; i++) { inner_base.holes.clear(); outer_base.holes.clear();
// ob = floor_poly;
// // The offset is given by the equation: x = sqrt(r^2 - y^2) ExPolygon top_poly;
// // which can be derived from the circle equation. y is the current top_poly.contour = outer_base.contour;
// // height for which the offset is calculated and x is the offset itself top_poly.holes.emplace_back(inner_base.contour);
// // r is the radius of the circle that is used to smooth the edges auto& tph = top_poly.holes.back().points;
std::reverse(tph.begin(), tph.end());
// double r2 = radius_mm * radius_mm; Contour3D pool;
// double y2 = steps*ystep_mm - i*ystep_mm;
// y2 *= y2;
// double x = sqrt(r2 - y2); ExPolygon ob = outer_base; double wh = 0;
// offset(ob, mm(x)); auto curvedwalls = round_edges(ob,
// wh = ceilheight_mm - i*ystep_mm; 1, // radius 1 mm
170, // 170 degrees
0, // z position of the input plane
true,
ob, wh);
pool.merge(curvedwalls);
// Contour3D pwalls; ExPolygon ob_contr = ob;
// pwalls = walls(ob, ob_prev, wh, wh_prev); ob_contr.holes.clear();
// curvedwalls.merge(pwalls); auto pwalls = walls(ob_contr, inner_base, wh, -min_wall_height_mm);
// ob_prev = ob; pool.merge(pwalls);
// wh_prev = wh;
// }
// auto w = walls(ob, poly, wh, 0); Polygons top_triangles, bottom_triangles;
triangulate(top_poly, top_triangles);
triangulate(inner_base, bottom_triangles);
auto top_plate = convert(top_triangles, 0, false);
auto bottom_plate = convert(bottom_triangles, -HEIGHT, true);
// pool.merge(w); ob = inner_base; wh = 0;
// pool.merge(curvedwalls); curvedwalls = round_edges(ob,
// out = mesh(pool);
auto concaveh = concave_hull(ground_layer);
if(concaveh.contour.points.empty()) return;
concaveh.holes.clear();
BoundingBox bb(concaveh);
coord_t w = x(bb.max) - x(bb.min);
coord_t h = y(bb.max) - y(bb.min);
auto wall_thickness = coord_t(std::pow((w+h)*0.1, 0.8));
const coord_t WALL_THICKNESS = mm(min_wall_thickness_mm) + wall_thickness;
const coord_t WALL_DISTANCE = coord_t(0.3*WALL_THICKNESS);
const coord_t HEIGHT = mm(min_wall_height_mm);
auto outer_base = concaveh;
offset(outer_base, WALL_THICKNESS+WALL_DISTANCE);
auto inner_base = outer_base;
offset(inner_base, -WALL_THICKNESS);
inner_base.holes.clear(); outer_base.holes.clear();
ExPolygon top_poly;
top_poly.contour = outer_base.contour;
top_poly.holes.emplace_back(inner_base.contour);
auto& tph = top_poly.holes.back().points;
std::reverse(tph.begin(), tph.end());
Contour3D pool;
ExPolygon ob = outer_base; double wh = 0;
auto curvedwalls = round_edges(ob,
1, // radius 1 mm 1, // radius 1 mm
170, // 170 degrees 90, // 170 degrees
0, // z position of the input plane 0, // z position of the input plane
true, false,
ob, wh); ob, wh);
pool.merge(curvedwalls); pool.merge(curvedwalls);
ExPolygon ob_contr = ob; auto innerbed = inner_bed(ob, min_wall_height_mm/2 + wh, wh);
ob_contr.holes.clear();
auto pwalls = walls(ob_contr, inner_base, wh, -min_wall_height_mm); pool.merge(top_plate);
pool.merge(pwalls); pool.merge(bottom_plate);
pool.merge(innerbed);
Polygons top_triangles, bottom_triangles; out.merge(mesh(pool));
top_poly.triangulate_p2t(&top_triangles); }
inner_base.triangulate_p2t(&bottom_triangles);
auto top_plate = convert(top_triangles, 0, false);
auto bottom_plate = convert(bottom_triangles, -HEIGHT, true);
ob = inner_base; wh = 0;
curvedwalls = round_edges(ob,
1, // radius 1 mm
90, // 170 degrees
0, // z position of the input plane
false,
ob, wh);
pool.merge(curvedwalls);
auto innerbed = inner_bed(ob, min_wall_height_mm/2 + wh, wh);
pool.merge(top_plate);
pool.merge(bottom_plate);
pool.merge(innerbed);
out = mesh(pool);
} }
} }