SLABasePool.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. #include "SLABasePool.hpp"
  2. #include "SLABoilerPlate.hpp"
  3. #include "boost/log/trivial.hpp"
  4. #include "SLABoostAdapter.hpp"
  5. #include "ClipperUtils.hpp"
  6. #include "Tesselate.hpp"
  7. // For debugging:
  8. //#include <fstream>
  9. //#include <libnest2d/tools/benchmark.h>
  10. //#include "SVG.hpp"
  11. namespace Slic3r { namespace sla {
  12. /// This function will return a triangulation of a sheet connecting an upper
  13. /// and a lower plate given as input polygons. It will not triangulate the
  14. /// plates themselves only the sheet. The caller has to specify the lower and
  15. /// upper z levels in world coordinates as well as the offset difference
  16. /// between the sheets. If the lower_z_mm is higher than upper_z_mm or the
  17. /// offset difference is negative, the resulting triangle orientation will be
  18. /// reversed.
  19. ///
  20. /// IMPORTANT: This is not a universal triangulation algorithm. It assumes
  21. /// that the lower and upper polygons are offsetted versions of the same
  22. /// original polygon. In general, it assumes that one of the polygons is
  23. /// completely inside the other. The offset difference is the reference
  24. /// distance from the inner polygon's perimeter to the outer polygon's
  25. /// perimeter. The real distance will be variable as the clipper offset has
  26. /// different strategies (rounding, etc...). This algorithm should have
  27. /// O(2n + 3m) complexity where n is the number of upper vertices and m is the
  28. /// number of lower vertices.
  29. Contour3D walls(const Polygon& lower, const Polygon& upper,
  30. double lower_z_mm, double upper_z_mm,
  31. double offset_difference_mm, ThrowOnCancel thr)
  32. {
  33. Contour3D ret;
  34. if(upper.points.size() < 3 || lower.size() < 3) return ret;
  35. // The concept of the algorithm is relatively simple. It will try to find
  36. // the closest vertices from the upper and the lower polygon and use those
  37. // as starting points. Then it will create the triangles sequentially using
  38. // an edge from the upper polygon and a vertex from the lower or vice versa,
  39. // depending on the resulting triangle's quality.
  40. // The quality is measured by a scalar value. So far it looks like it is
  41. // enough to derive it from the slope of the triangle's two edges connecting
  42. // the upper and the lower part. A reference slope is calculated from the
  43. // height and the offset difference.
  44. // Offset in the index array for the ceiling
  45. const auto offs = upper.points.size();
  46. // Shorthand for the vertex arrays
  47. auto& upoints = upper.points, &lpoints = lower.points;
  48. auto& rpts = ret.points; auto& ind = ret.indices;
  49. // If the Z levels are flipped, or the offset difference is negative, we
  50. // will interpret that as the triangles normals should be inverted.
  51. bool inverted = upper_z_mm < lower_z_mm || offset_difference_mm < 0;
  52. // Copy the points into the mesh, convert them from 2D to 3D
  53. rpts.reserve(upoints.size() + lpoints.size());
  54. ind.reserve(2 * upoints.size() + 2 * lpoints.size());
  55. for (auto &p : upoints)
  56. rpts.emplace_back(unscaled(p.x()), unscaled(p.y()), upper_z_mm);
  57. for (auto &p : lpoints)
  58. rpts.emplace_back(unscaled(p.x()), unscaled(p.y()), lower_z_mm);
  59. // Create pointing indices into vertex arrays. u-upper, l-lower
  60. size_t uidx = 0, lidx = offs, unextidx = 1, lnextidx = offs + 1;
  61. // Simple squared distance calculation.
  62. auto distfn = [](const Vec3d& p1, const Vec3d& p2) {
  63. auto p = p1 - p2; return p.transpose() * p;
  64. };
  65. // We need to find the closest point on lower polygon to the first point on
  66. // the upper polygon. These will be our starting points.
  67. double distmin = std::numeric_limits<double>::max();
  68. for(size_t l = lidx; l < rpts.size(); ++l) {
  69. thr();
  70. double d = distfn(rpts[l], rpts[uidx]);
  71. if(d < distmin) { lidx = l; distmin = d; }
  72. }
  73. // Set up lnextidx to be ahead of lidx in cyclic mode
  74. lnextidx = lidx + 1;
  75. if(lnextidx == rpts.size()) lnextidx = offs;
  76. // This will be the flip switch to toggle between upper and lower triangle
  77. // creation mode
  78. enum class Proceed {
  79. UPPER, // A segment from the upper polygon and one vertex from the lower
  80. LOWER // A segment from the lower polygon and one vertex from the upper
  81. } proceed = Proceed::UPPER;
  82. // Flags to help evaluating loop termination.
  83. bool ustarted = false, lstarted = false;
  84. // The variables for the fitness values, one for the actual and one for the
  85. // previous.
  86. double current_fit = 0, prev_fit = 0;
  87. // Every triangle of the wall has two edges connecting the upper plate with
  88. // the lower plate. From the length of these two edges and the zdiff we
  89. // can calculate the momentary squared offset distance at a particular
  90. // position on the wall. The average of the differences from the reference
  91. // (squared) offset distance will give us the driving fitness value.
  92. const double offsdiff2 = std::pow(offset_difference_mm, 2);
  93. const double zdiff2 = std::pow(upper_z_mm - lower_z_mm, 2);
  94. // Mark the current vertex iterator positions. If the iterators return to
  95. // the same position, the loop can be terminated.
  96. size_t uendidx = uidx, lendidx = lidx;
  97. do { thr(); // check throw if canceled
  98. prev_fit = current_fit;
  99. switch(proceed) { // proceed depending on the current state
  100. case Proceed::UPPER:
  101. if(!ustarted || uidx != uendidx) { // there are vertices remaining
  102. // Get the 3D vertices in order
  103. const Vec3d& p_up1 = rpts[uidx];
  104. const Vec3d& p_low = rpts[lidx];
  105. const Vec3d& p_up2 = rpts[unextidx];
  106. // Calculate fitness: the average of the two connecting edges
  107. double a = offsdiff2 - (distfn(p_up1, p_low) - zdiff2);
  108. double b = offsdiff2 - (distfn(p_up2, p_low) - zdiff2);
  109. current_fit = (std::abs(a) + std::abs(b)) / 2;
  110. if(current_fit > prev_fit) { // fit is worse than previously
  111. proceed = Proceed::LOWER;
  112. } else { // good to go, create the triangle
  113. inverted
  114. ? ind.emplace_back(int(unextidx), int(lidx), int(uidx))
  115. : ind.emplace_back(int(uidx), int(lidx), int(unextidx));
  116. // Increment the iterators, rotate if necessary
  117. ++uidx; ++unextidx;
  118. if(unextidx == offs) unextidx = 0;
  119. if(uidx == offs) uidx = 0;
  120. ustarted = true; // mark the movement of the iterators
  121. // so that the comparison to uendidx can be made correctly
  122. }
  123. } else proceed = Proceed::LOWER;
  124. break;
  125. case Proceed::LOWER:
  126. // Mode with lower segment, upper vertex. Same structure:
  127. if(!lstarted || lidx != lendidx) {
  128. const Vec3d& p_low1 = rpts[lidx];
  129. const Vec3d& p_low2 = rpts[lnextidx];
  130. const Vec3d& p_up = rpts[uidx];
  131. double a = offsdiff2 - (distfn(p_up, p_low1) - zdiff2);
  132. double b = offsdiff2 - (distfn(p_up, p_low2) - zdiff2);
  133. current_fit = (std::abs(a) + std::abs(b)) / 2;
  134. if(current_fit > prev_fit) {
  135. proceed = Proceed::UPPER;
  136. } else {
  137. inverted
  138. ? ind.emplace_back(int(uidx), int(lnextidx), int(lidx))
  139. : ind.emplace_back(int(lidx), int(lnextidx), int(uidx));
  140. ++lidx; ++lnextidx;
  141. if(lnextidx == rpts.size()) lnextidx = offs;
  142. if(lidx == rpts.size()) lidx = offs;
  143. lstarted = true;
  144. }
  145. } else proceed = Proceed::UPPER;
  146. break;
  147. } // end of switch
  148. } while(!ustarted || !lstarted || uidx != uendidx || lidx != lendidx);
  149. return ret;
  150. }
  151. /// Offsetting with clipper and smoothing the edges into a curvature.
  152. void offset(ExPolygon& sh, coord_t distance) {
  153. using ClipperLib::ClipperOffset;
  154. using ClipperLib::jtRound;
  155. using ClipperLib::etClosedPolygon;
  156. using ClipperLib::Paths;
  157. using ClipperLib::Path;
  158. auto&& ctour = Slic3rMultiPoint_to_ClipperPath(sh.contour);
  159. auto&& holes = Slic3rMultiPoints_to_ClipperPaths(sh.holes);
  160. // If the input is not at least a triangle, we can not do this algorithm
  161. if(ctour.size() < 3 ||
  162. std::any_of(holes.begin(), holes.end(),
  163. [](const Path& p) { return p.size() < 3; })
  164. ) {
  165. BOOST_LOG_TRIVIAL(error) << "Invalid geometry for offsetting!";
  166. return;
  167. }
  168. ClipperOffset offs;
  169. offs.ArcTolerance = 0.01*scaled(1.0);
  170. Paths result;
  171. offs.AddPath(ctour, jtRound, etClosedPolygon);
  172. offs.AddPaths(holes, jtRound, etClosedPolygon);
  173. offs.Execute(result, static_cast<double>(distance));
  174. // Offsetting reverts the orientation and also removes the last vertex
  175. // so boost will not have a closed polygon.
  176. bool found_the_contour = false;
  177. sh.holes.clear();
  178. for(auto& r : result) {
  179. if(ClipperLib::Orientation(r)) {
  180. // We don't like if the offsetting generates more than one contour
  181. // but throwing would be an overkill. Instead, we should warn the
  182. // caller about the inability to create correct geometries
  183. if(!found_the_contour) {
  184. auto rr = ClipperPath_to_Slic3rPolygon(r);
  185. sh.contour.points.swap(rr.points);
  186. found_the_contour = true;
  187. } else {
  188. BOOST_LOG_TRIVIAL(warning)
  189. << "Warning: offsetting result is invalid!";
  190. }
  191. } else {
  192. // TODO If there are multiple contours we can't be sure which hole
  193. // belongs to the first contour. (But in this case the situation is
  194. // bad enough to let it go...)
  195. sh.holes.emplace_back(ClipperPath_to_Slic3rPolygon(r));
  196. }
  197. }
  198. }
  199. /// Unification of polygons (with clipper) preserving holes as well.
  200. ExPolygons unify(const ExPolygons& shapes) {
  201. using ClipperLib::ptSubject;
  202. ExPolygons retv;
  203. bool closed = true;
  204. bool valid = true;
  205. ClipperLib::Clipper clipper;
  206. for(auto& path : shapes) {
  207. auto clipperpath = Slic3rMultiPoint_to_ClipperPath(path.contour);
  208. if(!clipperpath.empty())
  209. valid &= clipper.AddPath(clipperpath, ptSubject, closed);
  210. auto clipperholes = Slic3rMultiPoints_to_ClipperPaths(path.holes);
  211. for(auto& hole : clipperholes) {
  212. if(!hole.empty())
  213. valid &= clipper.AddPath(hole, ptSubject, closed);
  214. }
  215. }
  216. if(!valid) BOOST_LOG_TRIVIAL(warning) << "Unification of invalid shapes!";
  217. ClipperLib::PolyTree result;
  218. clipper.Execute(ClipperLib::ctUnion, result, ClipperLib::pftNonZero);
  219. retv.reserve(static_cast<size_t>(result.Total()));
  220. // Now we will recursively traverse the polygon tree and serialize it
  221. // into an ExPolygon with holes. The polygon tree has the clipper-ish
  222. // PolyTree structure which alternates its nodes as contours and holes
  223. // A "declaration" of function for traversing leafs which are holes
  224. std::function<void(ClipperLib::PolyNode*, ExPolygon&)> processHole;
  225. // Process polygon which calls processHoles which than calls processPoly
  226. // again until no leafs are left.
  227. auto processPoly = [&retv, &processHole](ClipperLib::PolyNode *pptr) {
  228. ExPolygon poly;
  229. poly.contour.points = ClipperPath_to_Slic3rPolygon(pptr->Contour);
  230. for(auto h : pptr->Childs) { processHole(h, poly); }
  231. retv.push_back(poly);
  232. };
  233. // Body of the processHole function
  234. processHole = [&processPoly](ClipperLib::PolyNode *pptr, ExPolygon& poly)
  235. {
  236. poly.holes.emplace_back();
  237. poly.holes.back().points = ClipperPath_to_Slic3rPolygon(pptr->Contour);
  238. for(auto c : pptr->Childs) processPoly(c);
  239. };
  240. // Wrapper for traversing.
  241. auto traverse = [&processPoly] (ClipperLib::PolyNode *node)
  242. {
  243. for(auto ch : node->Childs) {
  244. processPoly(ch);
  245. }
  246. };
  247. // Here is the actual traverse
  248. traverse(&result);
  249. return retv;
  250. }
  251. /// This method will create a rounded edge around a flat polygon in 3d space.
  252. /// 'base_plate' parameter is the target plate.
  253. /// 'radius' is the radius of the edges.
  254. /// 'degrees' is tells how much of a circle should be created as the rounding.
  255. /// It should be in degrees, not radians.
  256. /// 'ceilheight_mm' is the Z coordinate of the flat polygon in 3D space.
  257. /// 'dir' Is the direction of the round edges: inward or outward
  258. /// 'thr' Throws if a cancel signal was received
  259. /// 'last_offset' An auxiliary output variable to save the last offsetted
  260. /// version of 'base_plate'
  261. /// 'last_height' An auxiliary output to save the last z coordinate of the
  262. /// offsetted base_plate. In other words, where the rounded edges end.
  263. Contour3D round_edges(const ExPolygon& base_plate,
  264. double radius_mm,
  265. double degrees,
  266. double ceilheight_mm,
  267. bool dir,
  268. ThrowOnCancel thr,
  269. ExPolygon& last_offset, double& last_height)
  270. {
  271. auto ob = base_plate;
  272. auto ob_prev = ob;
  273. double wh = ceilheight_mm, wh_prev = wh;
  274. Contour3D curvedwalls;
  275. int steps = 30;
  276. double stepx = radius_mm / steps;
  277. coord_t s = dir? 1 : -1;
  278. degrees = std::fmod(degrees, 180);
  279. // we use sin for x distance because we interpret the angle starting from
  280. // PI/2
  281. int tos = degrees < 90?
  282. int(radius_mm*std::cos(degrees * PI / 180 - PI/2) / stepx) : steps;
  283. for(int i = 1; i <= tos; ++i) {
  284. thr();
  285. ob = base_plate;
  286. double r2 = radius_mm * radius_mm;
  287. double xx = i*stepx;
  288. double x2 = xx*xx;
  289. double stepy = std::sqrt(r2 - x2);
  290. offset(ob, s*scaled(xx));
  291. wh = ceilheight_mm - radius_mm + stepy;
  292. Contour3D pwalls;
  293. double prev_x = xx - (i - 1) * stepx;
  294. pwalls = walls(ob.contour, ob_prev.contour, wh, wh_prev, s*prev_x, thr);
  295. curvedwalls.merge(pwalls);
  296. ob_prev = ob;
  297. wh_prev = wh;
  298. }
  299. if(degrees > 90) {
  300. double tox = radius_mm - radius_mm*std::cos(degrees * PI / 180 - PI/2);
  301. int tos = int(tox / stepx);
  302. for(int i = 1; i <= tos; ++i) {
  303. thr();
  304. ob = base_plate;
  305. double r2 = radius_mm * radius_mm;
  306. double xx = radius_mm - i*stepx;
  307. double x2 = xx*xx;
  308. double stepy = std::sqrt(r2 - x2);
  309. offset(ob, s*scaled(xx));
  310. wh = ceilheight_mm - radius_mm - stepy;
  311. Contour3D pwalls;
  312. double prev_x = xx - radius_mm + (i - 1)*stepx;
  313. pwalls =
  314. walls(ob_prev.contour, ob.contour, wh_prev, wh, s*prev_x, thr);
  315. curvedwalls.merge(pwalls);
  316. ob_prev = ob;
  317. wh_prev = wh;
  318. }
  319. }
  320. last_offset = std::move(ob);
  321. last_height = wh;
  322. return curvedwalls;
  323. }
  324. inline Point centroid(Points& pp) {
  325. Point c;
  326. switch(pp.size()) {
  327. case 0: break;
  328. case 1: c = pp.front(); break;
  329. case 2: c = (pp[0] + pp[1]) / 2; break;
  330. default: {
  331. auto MAX = std::numeric_limits<Point::coord_type>::max();
  332. auto MIN = std::numeric_limits<Point::coord_type>::min();
  333. Point min = {MAX, MAX}, max = {MIN, MIN};
  334. for(auto& p : pp) {
  335. if(p(0) < min(0)) min(0) = p(0);
  336. if(p(1) < min(1)) min(1) = p(1);
  337. if(p(0) > max(0)) max(0) = p(0);
  338. if(p(1) > max(1)) max(1) = p(1);
  339. }
  340. c(0) = min(0) + (max(0) - min(0)) / 2;
  341. c(1) = min(1) + (max(1) - min(1)) / 2;
  342. // TODO: fails for non convex cluster
  343. // c = std::accumulate(pp.begin(), pp.end(), Point{0, 0});
  344. // x(c) /= coord_t(pp.size()); y(c) /= coord_t(pp.size());
  345. break;
  346. }
  347. }
  348. return c;
  349. }
  350. inline Point centroid(const ExPolygon& poly) {
  351. return poly.contour.centroid();
  352. }
  353. /// A fake concave hull that is constructed by connecting separate shapes
  354. /// with explicit bridges. Bridges are generated from each shape's centroid
  355. /// to the center of the "scene" which is the centroid calculated from the shape
  356. /// centroids (a star is created...)
  357. ExPolygons concave_hull(const ExPolygons& polys, double max_dist_mm = 50,
  358. ThrowOnCancel throw_on_cancel = [](){})
  359. {
  360. namespace bgi = boost::geometry::index;
  361. using SpatElement = std::pair<BoundingBox, unsigned>;
  362. using SpatIndex = bgi::rtree< SpatElement, bgi::rstar<16, 4> >;
  363. if(polys.empty()) return ExPolygons();
  364. ExPolygons punion = unify(polys); // could be redundant
  365. if(punion.size() == 1) return punion;
  366. // We get the centroids of all the islands in the 2D slice
  367. Points centroids; centroids.reserve(punion.size());
  368. std::transform(punion.begin(), punion.end(), std::back_inserter(centroids),
  369. [](const ExPolygon& poly) { return centroid(poly); });
  370. SpatIndex boxindex; unsigned idx = 0;
  371. std::for_each(punion.begin(), punion.end(),
  372. [&boxindex, &idx](const ExPolygon& expo) {
  373. BoundingBox bb(expo);
  374. boxindex.insert(std::make_pair(bb, idx++));
  375. });
  376. // Centroid of the centroids of islands. This is where the additional
  377. // connector sticks are routed.
  378. Point cc = centroid(centroids);
  379. punion.reserve(punion.size() + centroids.size());
  380. idx = 0;
  381. std::transform(centroids.begin(), centroids.end(),
  382. std::back_inserter(punion),
  383. [&punion, &boxindex, cc, max_dist_mm, &idx, throw_on_cancel]
  384. (const Point& c)
  385. {
  386. throw_on_cancel();
  387. double dx = x(c) - x(cc), dy = y(c) - y(cc);
  388. double l = std::sqrt(dx * dx + dy * dy);
  389. double nx = dx / l, ny = dy / l;
  390. double max_dist = scaled(max_dist_mm);
  391. ExPolygon& expo = punion[idx++];
  392. BoundingBox querybb(expo);
  393. querybb.offset(max_dist);
  394. std::vector<SpatElement> result;
  395. boxindex.query(bgi::intersects(querybb), std::back_inserter(result));
  396. if(result.size() <= 1) return ExPolygon();
  397. ExPolygon r;
  398. auto& ctour = r.contour.points;
  399. ctour.reserve(3);
  400. ctour.emplace_back(cc);
  401. Point d(coord_t(scaled(1.)*nx), coord_t(scaled(1.)*ny));
  402. ctour.emplace_back(c + Point( -y(d), x(d) ));
  403. ctour.emplace_back(c + Point( y(d), -x(d) ));
  404. offset(r, scaled(1.));
  405. return r;
  406. });
  407. // This is unavoidable...
  408. punion = unify(punion);
  409. return punion;
  410. }
  411. void base_plate(const TriangleMesh &mesh, ExPolygons &output, float h,
  412. float layerh, ThrowOnCancel thrfn)
  413. {
  414. TriangleMesh m = mesh;
  415. m.require_shared_vertices(); // TriangleMeshSlicer needs this
  416. TriangleMeshSlicer slicer(&m);
  417. auto bb = mesh.bounding_box();
  418. float gnd = float(bb.min(Z));
  419. std::vector<float> heights = {float(bb.min(Z))};
  420. for(float hi = gnd + layerh; hi <= gnd + h; hi += layerh)
  421. heights.emplace_back(hi);
  422. std::vector<ExPolygons> out; out.reserve(size_t(std::ceil(h/layerh)));
  423. slicer.slice(heights, 0.f, &out, thrfn);
  424. size_t count = 0; for(auto& o : out) count += o.size();
  425. // Now we have to unify all slice layers which can be an expensive operation
  426. // so we will try to simplify the polygons
  427. ExPolygons tmp; tmp.reserve(count);
  428. for(ExPolygons& o : out)
  429. for(ExPolygon& e : o) {
  430. auto&& exss = e.simplify(scaled(0.1));
  431. for(ExPolygon& ep : exss) tmp.emplace_back(std::move(ep));
  432. }
  433. ExPolygons utmp = unify(tmp);
  434. for(auto& o : utmp) {
  435. auto&& smp = o.simplify(scaled(0.1));
  436. output.insert(output.end(), smp.begin(), smp.end());
  437. }
  438. }
  439. Contour3D create_base_pool(const ExPolygons &ground_layer,
  440. const PoolConfig& cfg = PoolConfig())
  441. {
  442. // for debugging:
  443. // Benchmark bench;
  444. // bench.start();
  445. double mergedist = 2*(1.8*cfg.min_wall_thickness_mm + 4*cfg.edge_radius_mm)+
  446. cfg.max_merge_distance_mm;
  447. // Here we get the base polygon from which the pad has to be generated.
  448. // We create an artificial concave hull from this polygon and that will
  449. // serve as the bottom plate of the pad. We will offset this concave hull
  450. // and then offset back the result with clipper with rounding edges ON. This
  451. // trick will create a nice rounded pad shape.
  452. ExPolygons concavehs = concave_hull(ground_layer, mergedist, cfg.throw_on_cancel);
  453. const double thickness = cfg.min_wall_thickness_mm;
  454. const double wingheight = cfg.min_wall_height_mm;
  455. const double fullheight = wingheight + thickness;
  456. const double slope = cfg.wall_slope;
  457. const double wingdist = wingheight / std::tan(slope);
  458. const double bottom_offs = (thickness + wingheight) / std::tan(slope);
  459. // scaled values
  460. const coord_t s_thickness = scaled(thickness);
  461. const coord_t s_eradius = scaled(cfg.edge_radius_mm);
  462. const coord_t s_safety_dist = 2*s_eradius + coord_t(0.8*s_thickness);
  463. const coord_t s_wingdist = scaled(wingdist);
  464. const coord_t s_bottom_offs = scaled(bottom_offs);
  465. auto& thrcl = cfg.throw_on_cancel;
  466. Contour3D pool;
  467. for(ExPolygon& concaveh : concavehs) {
  468. if(concaveh.contour.points.empty()) return pool;
  469. // Get rid of any holes in the concave hull output.
  470. concaveh.holes.clear();
  471. // Here lies the trick that does the smoothing only with clipper offset
  472. // calls. The offset is configured to round edges. Inner edges will
  473. // be rounded because we offset twice: ones to get the outer (top) plate
  474. // and again to get the inner (bottom) plate
  475. auto outer_base = concaveh;
  476. outer_base.holes.clear();
  477. offset(outer_base, s_safety_dist + s_wingdist + s_thickness);
  478. ExPolygon bottom_poly = outer_base;
  479. bottom_poly.holes.clear();
  480. offset(bottom_poly, -s_bottom_offs);
  481. // Punching a hole in the top plate for the cavity
  482. ExPolygon top_poly;
  483. ExPolygon middle_base;
  484. ExPolygon inner_base;
  485. top_poly.contour = outer_base.contour;
  486. if(wingheight > 0) {
  487. inner_base = outer_base;
  488. offset(inner_base, -(s_thickness + s_wingdist + s_eradius));
  489. middle_base = outer_base;
  490. offset(middle_base, -s_thickness);
  491. top_poly.holes.emplace_back(middle_base.contour);
  492. auto& tph = top_poly.holes.back().points;
  493. std::reverse(tph.begin(), tph.end());
  494. }
  495. ExPolygon ob = outer_base; double wh = 0;
  496. // now we will calculate the angle or portion of the circle from
  497. // pi/2 that will connect perfectly with the bottom plate.
  498. // this is a tangent point calculation problem and the equation can
  499. // be found for example here:
  500. // http://www.ambrsoft.com/TrigoCalc/Circles2/CirclePoint/CirclePointDistance.htm
  501. // the y coordinate would be:
  502. // y = cy + (r^2*py - r*px*sqrt(px^2 + py^2 - r^2) / (px^2 + py^2)
  503. // where px and py are the coordinates of the point outside the circle
  504. // cx and cy are the circle center, r is the radius
  505. // We place the circle center to (0, 0) in the calculation the make
  506. // things easier.
  507. // to get the angle we use arcsin function and subtract 90 degrees then
  508. // flip the sign to get the right input to the round_edge function.
  509. double r = cfg.edge_radius_mm;
  510. double cy = 0;
  511. double cx = 0;
  512. double px = thickness + wingdist;
  513. double py = r - fullheight;
  514. double pxcx = px - cx;
  515. double pycy = py - cy;
  516. double b_2 = pxcx*pxcx + pycy*pycy;
  517. double r_2 = r*r;
  518. double D = std::sqrt(b_2 - r_2);
  519. double vy = (r_2*pycy - r*pxcx*D) / b_2;
  520. double phi = -(std::asin(vy/r) * 180 / PI - 90);
  521. // Generate the smoothed edge geometry
  522. if(s_eradius > 0) pool.merge(round_edges(ob,
  523. r,
  524. phi,
  525. 0, // z position of the input plane
  526. true,
  527. thrcl,
  528. ob, wh));
  529. // Now that we have the rounded edge connecting the top plate with
  530. // the outer side walls, we can generate and merge the sidewall geometry
  531. pool.merge(walls(ob.contour, bottom_poly.contour, wh, -fullheight,
  532. bottom_offs, thrcl));
  533. if(wingheight > 0) {
  534. // Generate the smoothed edge geometry
  535. wh = 0;
  536. if(s_eradius) pool.merge(round_edges(middle_base,
  537. r,
  538. phi - 90, // from tangent lines
  539. 0, // z position of the input plane
  540. false,
  541. thrcl,
  542. ob, wh));
  543. // Next is the cavity walls connecting to the top plate's
  544. // artificially created hole.
  545. pool.merge(walls(inner_base.contour, ob.contour, -wingheight,
  546. wh, -wingdist, thrcl));
  547. }
  548. // Now we need to triangulate the top and bottom plates as well as the
  549. // cavity bottom plate which is the same as the bottom plate but it is
  550. // elevated by the thickness.
  551. pool.merge(triangulate_expolygon_3d(top_poly));
  552. pool.merge(triangulate_expolygon_3d(bottom_poly, -fullheight, true));
  553. if(wingheight > 0)
  554. pool.merge(triangulate_expolygon_3d(inner_base, -wingheight));
  555. }
  556. return pool;
  557. }
  558. void create_base_pool(const ExPolygons &ground_layer, TriangleMesh& out,
  559. const PoolConfig& cfg)
  560. {
  561. // For debugging:
  562. // bench.stop();
  563. // std::cout << "Pad creation time: " << bench.getElapsedSec() << std::endl;
  564. // std::fstream fout("pad_debug.obj", std::fstream::out);
  565. // if(fout.good()) pool.to_obj(fout);
  566. out.merge(mesh(create_base_pool(ground_layer, cfg)));
  567. }
  568. }
  569. }