FillRectilinear2.cpp 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <limits>
  6. #include <boost/static_assert.hpp>
  7. #include "../ExtrusionEntityCollection.hpp"
  8. #include "../ClipperUtils.hpp"
  9. #include "../ExPolygon.hpp"
  10. #include "../Geometry.hpp"
  11. #include "../Surface.hpp"
  12. #include "FillRectilinear2.hpp"
  13. // #define SLIC3R_DEBUG
  14. // Make assert active if SLIC3R_DEBUG
  15. #ifdef SLIC3R_DEBUG
  16. #undef NDEBUG
  17. #include "SVG.hpp"
  18. #endif
  19. #include <cassert>
  20. // We want our version of assert.
  21. #include "../libslic3r.h"
  22. namespace Slic3r {
  23. // Having a segment of a closed polygon, calculate its Euclidian length.
  24. // The segment indices seg1 and seg2 signify an end point of an edge in the forward direction of the loop,
  25. // therefore the point p1 lies on poly.points[seg1-1], poly.points[seg1] etc.
  26. static inline coordf_t segment_length(const Polygon &poly, size_t seg1, const Point &p1, size_t seg2, const Point &p2)
  27. {
  28. #ifdef SLIC3R_DEBUG
  29. // Verify that p1 lies on seg1. This is difficult to verify precisely,
  30. // but at least verify, that p1 lies in the bounding box of seg1.
  31. for (size_t i = 0; i < 2; ++ i) {
  32. size_t seg = (i == 0) ? seg1 : seg2;
  33. Point px = (i == 0) ? p1 : p2;
  34. Point pa = poly.points[((seg == 0) ? poly.points.size() : seg) - 1];
  35. Point pb = poly.points[seg];
  36. if (pa.x > pb.x)
  37. std::swap(pa.x, pb.x);
  38. if (pa.y > pb.y)
  39. std::swap(pa.y, pb.y);
  40. assert(px.x >= pa.x && px.x <= pb.x);
  41. assert(px.y >= pa.y && px.y <= pb.y);
  42. }
  43. #endif /* SLIC3R_DEBUG */
  44. const Point *pPrev = &p1;
  45. const Point *pThis = NULL;
  46. coordf_t len = 0;
  47. if (seg1 <= seg2) {
  48. for (size_t i = seg1; i < seg2; ++ i, pPrev = pThis)
  49. len += pPrev->distance_to(*(pThis = &poly.points[i]));
  50. } else {
  51. for (size_t i = seg1; i < poly.points.size(); ++ i, pPrev = pThis)
  52. len += pPrev->distance_to(*(pThis = &poly.points[i]));
  53. for (size_t i = 0; i < seg2; ++ i, pPrev = pThis)
  54. len += pPrev->distance_to(*(pThis = &poly.points[i]));
  55. }
  56. len += pPrev->distance_to(p2);
  57. return len;
  58. }
  59. // Append a segment of a closed polygon to a polyline.
  60. // The segment indices seg1 and seg2 signify an end point of an edge in the forward direction of the loop.
  61. // Only insert intermediate points between seg1 and seg2.
  62. static inline void polygon_segment_append(Points &out, const Polygon &polygon, size_t seg1, size_t seg2)
  63. {
  64. if (seg1 == seg2) {
  65. // Nothing to append from this segment.
  66. } else if (seg1 < seg2) {
  67. // Do not append a point pointed to by seg2.
  68. out.insert(out.end(), polygon.points.begin() + seg1, polygon.points.begin() + seg2);
  69. } else {
  70. out.reserve(out.size() + seg2 + polygon.points.size() - seg1);
  71. out.insert(out.end(), polygon.points.begin() + seg1, polygon.points.end());
  72. // Do not append a point pointed to by seg2.
  73. out.insert(out.end(), polygon.points.begin(), polygon.points.begin() + seg2);
  74. }
  75. }
  76. // Append a segment of a closed polygon to a polyline.
  77. // The segment indices seg1 and seg2 signify an end point of an edge in the forward direction of the loop,
  78. // but this time the segment is traversed backward.
  79. // Only insert intermediate points between seg1 and seg2.
  80. static inline void polygon_segment_append_reversed(Points &out, const Polygon &polygon, size_t seg1, size_t seg2)
  81. {
  82. if (seg1 >= seg2) {
  83. out.reserve(seg1 - seg2);
  84. for (size_t i = seg1; i > seg2; -- i)
  85. out.push_back(polygon.points[i - 1]);
  86. } else {
  87. // it could be, that seg1 == seg2. In that case, append the complete loop.
  88. out.reserve(out.size() + seg2 + polygon.points.size() - seg1);
  89. for (size_t i = seg1; i > 0; -- i)
  90. out.push_back(polygon.points[i - 1]);
  91. for (size_t i = polygon.points.size(); i > seg2; -- i)
  92. out.push_back(polygon.points[i - 1]);
  93. }
  94. }
  95. // Intersection point of a vertical line with a polygon segment.
  96. class SegmentIntersection
  97. {
  98. public:
  99. SegmentIntersection() :
  100. iContour(0),
  101. iSegment(0),
  102. pos_p(0),
  103. pos_q(1),
  104. type(UNKNOWN),
  105. consumed_vertical_up(false),
  106. consumed_perimeter_right(false)
  107. {}
  108. // Index of a contour in ExPolygonWithOffset, with which this vertical line intersects.
  109. size_t iContour;
  110. // Index of a segment in iContour, with which this vertical line intersects.
  111. size_t iSegment;
  112. // y position of the intersection, ratinal number.
  113. int64_t pos_p;
  114. uint32_t pos_q;
  115. coord_t pos() const {
  116. // Division rounds both positive and negative down to zero.
  117. // Add half of q for an arithmetic rounding effect.
  118. int64_t p = pos_p;
  119. if (p < 0)
  120. p -= int64_t(pos_q>>1);
  121. else
  122. p += int64_t(pos_q>>1);
  123. return coord_t(p / int64_t(pos_q));
  124. }
  125. // Kind of intersection. With the original contour, or with the inner offestted contour?
  126. // A vertical segment will be at least intersected by OUTER_LOW, OUTER_HIGH,
  127. // but it could be intersected with OUTER_LOW, INNER_LOW, INNER_HIGH, OUTER_HIGH,
  128. // and there may be more than one pair of INNER_LOW, INNER_HIGH between OUTER_LOW, OUTER_HIGH.
  129. enum SegmentIntersectionType {
  130. OUTER_LOW = 0,
  131. OUTER_HIGH = 1,
  132. INNER_LOW = 2,
  133. INNER_HIGH = 3,
  134. UNKNOWN = -1
  135. };
  136. SegmentIntersectionType type;
  137. // Was this segment along the y axis consumed?
  138. // Up means up along the vertical segment.
  139. bool consumed_vertical_up;
  140. // Was a segment of the inner perimeter contour consumed?
  141. // Right means right from the vertical segment.
  142. bool consumed_perimeter_right;
  143. // For the INNER_LOW type, this point may be connected to another INNER_LOW point following a perimeter contour.
  144. // For the INNER_HIGH type, this point may be connected to another INNER_HIGH point following a perimeter contour.
  145. // If INNER_LOW is connected to INNER_HIGH or vice versa,
  146. // one has to make sure the vertical infill line does not overlap with the connecting perimeter line.
  147. bool is_inner() const { return type == INNER_LOW || type == INNER_HIGH; }
  148. bool is_outer() const { return type == OUTER_LOW || type == OUTER_HIGH; }
  149. bool is_low () const { return type == INNER_LOW || type == OUTER_LOW; }
  150. bool is_high () const { return type == INNER_HIGH || type == OUTER_HIGH; }
  151. // Compare two y intersection points given by rational numbers.
  152. // Note that the rational number is given as pos_p/pos_q, where pos_p is int64 and pos_q is uint32.
  153. // This function calculates pos_p * other.pos_q < other.pos_p * pos_q as a 48bit number.
  154. // We don't use 128bit intrinsic data types as these are usually not supported by 32bit compilers and
  155. // we don't need the full 128bit precision anyway.
  156. bool operator<(const SegmentIntersection &other) const
  157. {
  158. assert(pos_q > 0);
  159. assert(other.pos_q > 0);
  160. if (pos_p == 0 || other.pos_p == 0) {
  161. // Because the denominators are positive and one of the nominators is zero,
  162. // following simple statement holds.
  163. return pos_p < other.pos_p;
  164. } else {
  165. // None of the nominators is zero.
  166. int sign1 = (pos_p > 0) ? 1 : -1;
  167. int sign2 = (other.pos_p > 0) ? 1 : -1;
  168. int signs = sign1 * sign2;
  169. assert(signs == 1 || signs == -1);
  170. if (signs < 0) {
  171. // The nominators have different signs.
  172. return sign1 < 0;
  173. } else {
  174. // The nominators have the same sign.
  175. // Absolute values
  176. uint64_t p1, p2;
  177. if (sign1 > 0) {
  178. p1 = uint64_t(pos_p);
  179. p2 = uint64_t(other.pos_p);
  180. } else {
  181. p1 = uint64_t(- pos_p);
  182. p2 = uint64_t(- other.pos_p);
  183. };
  184. // Multiply low and high 32bit words of p1 by other_pos.q
  185. // 32bit x 32bit => 64bit
  186. // l_hi and l_lo overlap by 32 bits.
  187. uint64_t l_hi = (p1 >> 32) * uint64_t(other.pos_q);
  188. uint64_t l_lo = (p1 & 0xffffffffll) * uint64_t(other.pos_q);
  189. l_hi += (l_lo >> 32);
  190. uint64_t r_hi = (p2 >> 32) * uint64_t(pos_q);
  191. uint64_t r_lo = (p2 & 0xffffffffll) * uint64_t(pos_q);
  192. r_hi += (r_lo >> 32);
  193. // Compare the high 64 bits.
  194. if (l_hi == r_hi) {
  195. // Compare the low 32 bits.
  196. l_lo &= 0xffffffffll;
  197. r_lo &= 0xffffffffll;
  198. return (sign1 < 0) ? (l_lo > r_lo) : (l_lo < r_lo);
  199. }
  200. return (sign1 < 0) ? (l_hi > r_hi) : (l_hi < r_hi);
  201. }
  202. }
  203. }
  204. bool operator==(const SegmentIntersection &other) const
  205. {
  206. assert(pos_q > 0);
  207. assert(other.pos_q > 0);
  208. if (pos_p == 0 || other.pos_p == 0) {
  209. // Because the denominators are positive and one of the nominators is zero,
  210. // following simple statement holds.
  211. return pos_p == other.pos_p;
  212. }
  213. // None of the nominators is zero, none of the denominators is zero.
  214. bool positive = pos_p > 0;
  215. if (positive != (other.pos_p > 0))
  216. return false;
  217. // The nominators have the same sign.
  218. // Absolute values
  219. uint64_t p1 = positive ? uint64_t(pos_p) : uint64_t(- pos_p);
  220. uint64_t p2 = positive ? uint64_t(other.pos_p) : uint64_t(- other.pos_p);
  221. // Multiply low and high 32bit words of p1 by other_pos.q
  222. // 32bit x 32bit => 64bit
  223. // l_hi and l_lo overlap by 32 bits.
  224. uint64_t l_lo = (p1 & 0xffffffffll) * uint64_t(other.pos_q);
  225. uint64_t r_lo = (p2 & 0xffffffffll) * uint64_t(pos_q);
  226. if (l_lo != r_lo)
  227. return false;
  228. uint64_t l_hi = (p1 >> 32) * uint64_t(other.pos_q);
  229. uint64_t r_hi = (p2 >> 32) * uint64_t(pos_q);
  230. return l_hi + (l_lo >> 32) == r_hi + (r_lo >> 32);
  231. }
  232. };
  233. // A vertical line with intersection points with polygons.
  234. class SegmentedIntersectionLine
  235. {
  236. public:
  237. // Index of this vertical intersection line.
  238. size_t idx;
  239. // x position of this vertical intersection line.
  240. coord_t pos;
  241. // List of intersection points with polygons, sorted increasingly by the y axis.
  242. std::vector<SegmentIntersection> intersections;
  243. };
  244. // A container maintaining an expolygon with its inner offsetted polygon.
  245. // The purpose of the inner offsetted polygon is to provide segments to connect the infill lines.
  246. struct ExPolygonWithOffset
  247. {
  248. public:
  249. ExPolygonWithOffset(
  250. const ExPolygon &expolygon,
  251. float angle,
  252. coord_t aoffset1,
  253. coord_t aoffset2)
  254. {
  255. // Copy and rotate the source polygons.
  256. polygons_src = expolygon;
  257. polygons_src.contour.rotate(angle);
  258. for (Polygons::iterator it = polygons_src.holes.begin(); it != polygons_src.holes.end(); ++ it)
  259. it->rotate(angle);
  260. double mitterLimit = 3.;
  261. // for the infill pattern, don't cut the corners.
  262. // default miterLimt = 3
  263. //double mitterLimit = 10.;
  264. assert(aoffset1 < 0);
  265. assert(aoffset2 < 0);
  266. assert(aoffset2 < aoffset1);
  267. bool sticks_removed = remove_sticks(polygons_src);
  268. // if (sticks_removed) printf("Sticks removed!\n");
  269. polygons_outer = offset(polygons_src, aoffset1,
  270. ClipperLib::jtMiter,
  271. mitterLimit);
  272. polygons_inner = offset(polygons_outer, aoffset2 - aoffset1,
  273. ClipperLib::jtMiter,
  274. mitterLimit);
  275. // Filter out contours with zero area or small area, contours with 2 points only.
  276. const double min_area_threshold = 0.01 * aoffset2 * aoffset2;
  277. remove_small(polygons_outer, min_area_threshold);
  278. remove_small(polygons_inner, min_area_threshold);
  279. remove_sticks(polygons_outer);
  280. remove_sticks(polygons_inner);
  281. n_contours_outer = polygons_outer.size();
  282. n_contours_inner = polygons_inner.size();
  283. n_contours = n_contours_outer + n_contours_inner;
  284. polygons_ccw.assign(n_contours, false);
  285. for (size_t i = 0; i < n_contours; ++ i) {
  286. contour(i).remove_duplicate_points();
  287. assert(! contour(i).has_duplicate_points());
  288. polygons_ccw[i] = Slic3r::Geometry::is_ccw(contour(i));
  289. }
  290. }
  291. // Any contour with offset1
  292. bool is_contour_outer(size_t idx) const { return idx < n_contours_outer; }
  293. // Any contour with offset2
  294. bool is_contour_inner(size_t idx) const { return idx >= n_contours_outer; }
  295. const Polygon& contour(size_t idx) const
  296. { return is_contour_outer(idx) ? polygons_outer[idx] : polygons_inner[idx - n_contours_outer]; }
  297. Polygon& contour(size_t idx)
  298. { return is_contour_outer(idx) ? polygons_outer[idx] : polygons_inner[idx - n_contours_outer]; }
  299. bool is_contour_ccw(size_t idx) const { return polygons_ccw[idx]; }
  300. BoundingBox bounding_box_src() const
  301. { return get_extents(polygons_src); }
  302. BoundingBox bounding_box_outer() const
  303. { return get_extents(polygons_outer); }
  304. BoundingBox bounding_box_inner() const
  305. { return get_extents(polygons_inner); }
  306. #ifdef SLIC3R_DEBUG
  307. void export_to_svg(Slic3r::SVG &svg) {
  308. svg.draw_outline(polygons_src, "black");
  309. svg.draw_outline(polygons_outer, "green");
  310. svg.draw_outline(polygons_inner, "brown");
  311. }
  312. #endif /* SLIC3R_DEBUG */
  313. ExPolygon polygons_src;
  314. Polygons polygons_outer;
  315. Polygons polygons_inner;
  316. size_t n_contours_outer;
  317. size_t n_contours_inner;
  318. size_t n_contours;
  319. protected:
  320. // For each polygon of polygons_inner, remember its orientation.
  321. std::vector<unsigned char> polygons_ccw;
  322. };
  323. static inline int distance_of_segmens(const Polygon &poly, size_t seg1, size_t seg2, bool forward)
  324. {
  325. int d = int(seg2) - int(seg1);
  326. if (! forward)
  327. d = - d;
  328. if (d < 0)
  329. d += int(poly.points.size());
  330. return d;
  331. }
  332. // For a vertical line, an inner contour and an intersection point,
  333. // find an intersection point on the previous resp. next vertical line.
  334. // The intersection point is connected with the prev resp. next intersection point with iInnerContour.
  335. // Return -1 if there is no such point on the previous resp. next vertical line.
  336. static inline int intersection_on_prev_next_vertical_line(
  337. const ExPolygonWithOffset &poly_with_offset,
  338. const std::vector<SegmentedIntersectionLine> &segs,
  339. size_t iVerticalLine,
  340. size_t iInnerContour,
  341. size_t iIntersection,
  342. bool dir_is_next)
  343. {
  344. size_t iVerticalLineOther = iVerticalLine;
  345. if (dir_is_next) {
  346. if (++ iVerticalLineOther == segs.size())
  347. // No successive vertical line.
  348. return -1;
  349. } else if (iVerticalLineOther -- == 0) {
  350. // No preceding vertical line.
  351. return -1;
  352. }
  353. const SegmentedIntersectionLine &il = segs[iVerticalLine];
  354. const SegmentIntersection &itsct = il.intersections[iIntersection];
  355. const SegmentedIntersectionLine &il2 = segs[iVerticalLineOther];
  356. const Polygon &poly = poly_with_offset.contour(iInnerContour);
  357. // const bool ccw = poly_with_offset.is_contour_ccw(iInnerContour);
  358. const bool forward = itsct.is_low() == dir_is_next;
  359. // Resulting index of an intersection point on il2.
  360. int out = -1;
  361. // Find an intersection point on iVerticalLineOther, intersecting iInnerContour
  362. // at the same orientation as iIntersection, and being closest to iIntersection
  363. // in the number of contour segments, when following the direction of the contour.
  364. int dmin = std::numeric_limits<int>::max();
  365. for (size_t i = 0; i < il2.intersections.size(); ++ i) {
  366. const SegmentIntersection &itsct2 = il2.intersections[i];
  367. if (itsct.iContour == itsct2.iContour && itsct.type == itsct2.type) {
  368. /*
  369. if (itsct.is_low()) {
  370. assert(itsct.type == SegmentIntersection::INNER_LOW);
  371. assert(iIntersection > 0);
  372. assert(il.intersections[iIntersection-1].type == SegmentIntersection::OUTER_LOW);
  373. assert(i > 0);
  374. if (il2.intersections[i-1].is_inner())
  375. // Take only the lowest inner intersection point.
  376. continue;
  377. assert(il2.intersections[i-1].type == SegmentIntersection::OUTER_LOW);
  378. } else {
  379. assert(itsct.type == SegmentIntersection::INNER_HIGH);
  380. assert(iIntersection+1 < il.intersections.size());
  381. assert(il.intersections[iIntersection+1].type == SegmentIntersection::OUTER_HIGH);
  382. assert(i+1 < il2.intersections.size());
  383. if (il2.intersections[i+1].is_inner())
  384. // Take only the highest inner intersection point.
  385. continue;
  386. assert(il2.intersections[i+1].type == SegmentIntersection::OUTER_HIGH);
  387. }
  388. */
  389. // The intersection points lie on the same contour and have the same orientation.
  390. // Find the intersection point with a shortest path in the direction of the contour.
  391. int d = distance_of_segmens(poly, itsct.iSegment, itsct2.iSegment, forward);
  392. if (d < dmin) {
  393. out = i;
  394. dmin = d;
  395. }
  396. }
  397. }
  398. //FIXME this routine is not asymptotic optimal, it will be slow if there are many intersection points along the line.
  399. return out;
  400. }
  401. static inline int intersection_on_prev_vertical_line(
  402. const ExPolygonWithOffset &poly_with_offset,
  403. const std::vector<SegmentedIntersectionLine> &segs,
  404. size_t iVerticalLine,
  405. size_t iInnerContour,
  406. size_t iIntersection)
  407. {
  408. return intersection_on_prev_next_vertical_line(poly_with_offset, segs, iVerticalLine, iInnerContour, iIntersection, false);
  409. }
  410. static inline int intersection_on_next_vertical_line(
  411. const ExPolygonWithOffset &poly_with_offset,
  412. const std::vector<SegmentedIntersectionLine> &segs,
  413. size_t iVerticalLine,
  414. size_t iInnerContour,
  415. size_t iIntersection)
  416. {
  417. return intersection_on_prev_next_vertical_line(poly_with_offset, segs, iVerticalLine, iInnerContour, iIntersection, true);
  418. }
  419. enum IntersectionTypeOtherVLine {
  420. // There is no connection point on the other vertical line.
  421. INTERSECTION_TYPE_OTHER_VLINE_UNDEFINED = -1,
  422. // Connection point on the other vertical segment was found
  423. // and it could be followed.
  424. INTERSECTION_TYPE_OTHER_VLINE_OK = 0,
  425. // The connection segment connects to a middle of a vertical segment.
  426. // Cannot follow.
  427. INTERSECTION_TYPE_OTHER_VLINE_INNER,
  428. // Cannot extend the contor to this intersection point as either the connection segment
  429. // or the succeeding vertical segment were already consumed.
  430. INTERSECTION_TYPE_OTHER_VLINE_CONSUMED,
  431. // Not the first intersection along the contor. This intersection point
  432. // has been preceded by an intersection point along the vertical line.
  433. INTERSECTION_TYPE_OTHER_VLINE_NOT_FIRST,
  434. };
  435. // Find an intersection on a previous line, but return -1, if the connecting segment of a perimeter was already extruded.
  436. static inline IntersectionTypeOtherVLine intersection_type_on_prev_next_vertical_line(
  437. const std::vector<SegmentedIntersectionLine> &segs,
  438. size_t iVerticalLine,
  439. size_t iIntersection,
  440. size_t iIntersectionOther,
  441. bool dir_is_next)
  442. {
  443. // This routine will propose a connecting line even if the connecting perimeter segment intersects
  444. // iVertical line multiple times before reaching iIntersectionOther.
  445. if (iIntersectionOther == -1)
  446. return INTERSECTION_TYPE_OTHER_VLINE_UNDEFINED;
  447. assert(dir_is_next ? (iVerticalLine + 1 < segs.size()) : (iVerticalLine > 0));
  448. const SegmentedIntersectionLine &il_this = segs[iVerticalLine];
  449. const SegmentIntersection &itsct_this = il_this.intersections[iIntersection];
  450. const SegmentedIntersectionLine &il_other = segs[dir_is_next ? (iVerticalLine+1) : (iVerticalLine-1)];
  451. const SegmentIntersection &itsct_other = il_other.intersections[iIntersectionOther];
  452. assert(itsct_other.is_inner());
  453. assert(iIntersectionOther > 0);
  454. assert(iIntersectionOther + 1 < il_other.intersections.size());
  455. // Is iIntersectionOther at the boundary of a vertical segment?
  456. const SegmentIntersection &itsct_other2 = il_other.intersections[itsct_other.is_low() ? iIntersectionOther - 1 : iIntersectionOther + 1];
  457. if (itsct_other2.is_inner())
  458. // Cannot follow a perimeter segment into the middle of another vertical segment.
  459. // Only perimeter segments connecting to the end of a vertical segment are followed.
  460. return INTERSECTION_TYPE_OTHER_VLINE_INNER;
  461. assert(itsct_other.is_low() == itsct_other2.is_low());
  462. if (dir_is_next ? itsct_this.consumed_perimeter_right : itsct_other.consumed_perimeter_right)
  463. // This perimeter segment was already consumed.
  464. return INTERSECTION_TYPE_OTHER_VLINE_CONSUMED;
  465. if (itsct_other.is_low() ? itsct_other.consumed_vertical_up : il_other.intersections[iIntersectionOther-1].consumed_vertical_up)
  466. // This vertical segment was already consumed.
  467. return INTERSECTION_TYPE_OTHER_VLINE_CONSUMED;
  468. return INTERSECTION_TYPE_OTHER_VLINE_OK;
  469. }
  470. static inline IntersectionTypeOtherVLine intersection_type_on_prev_vertical_line(
  471. const std::vector<SegmentedIntersectionLine> &segs,
  472. size_t iVerticalLine,
  473. size_t iIntersection,
  474. size_t iIntersectionPrev)
  475. {
  476. return intersection_type_on_prev_next_vertical_line(segs, iVerticalLine, iIntersection, iIntersectionPrev, false);
  477. }
  478. static inline IntersectionTypeOtherVLine intersection_type_on_next_vertical_line(
  479. const std::vector<SegmentedIntersectionLine> &segs,
  480. size_t iVerticalLine,
  481. size_t iIntersection,
  482. size_t iIntersectionNext)
  483. {
  484. return intersection_type_on_prev_next_vertical_line(segs, iVerticalLine, iIntersection, iIntersectionNext, true);
  485. }
  486. // Measure an Euclidian length of a perimeter segment when going from iIntersection to iIntersection2.
  487. static inline coordf_t measure_perimeter_prev_next_segment_length(
  488. const ExPolygonWithOffset &poly_with_offset,
  489. const std::vector<SegmentedIntersectionLine> &segs,
  490. size_t iVerticalLine,
  491. size_t iInnerContour,
  492. size_t iIntersection,
  493. size_t iIntersection2,
  494. bool dir_is_next)
  495. {
  496. size_t iVerticalLineOther = iVerticalLine;
  497. if (dir_is_next) {
  498. if (++ iVerticalLineOther == segs.size())
  499. // No successive vertical line.
  500. return coordf_t(-1);
  501. } else if (iVerticalLineOther -- == 0) {
  502. // No preceding vertical line.
  503. return coordf_t(-1);
  504. }
  505. const SegmentedIntersectionLine &il = segs[iVerticalLine];
  506. const SegmentIntersection &itsct = il.intersections[iIntersection];
  507. const SegmentedIntersectionLine &il2 = segs[iVerticalLineOther];
  508. const SegmentIntersection &itsct2 = il2.intersections[iIntersection2];
  509. const Polygon &poly = poly_with_offset.contour(iInnerContour);
  510. // const bool ccw = poly_with_offset.is_contour_ccw(iInnerContour);
  511. assert(itsct.type == itsct2.type);
  512. assert(itsct.iContour == itsct2.iContour);
  513. assert(itsct.is_inner());
  514. const bool forward = itsct.is_low() == dir_is_next;
  515. Point p1(il.pos, itsct.pos());
  516. Point p2(il2.pos, itsct2.pos());
  517. return forward ?
  518. segment_length(poly, itsct .iSegment, p1, itsct2.iSegment, p2) :
  519. segment_length(poly, itsct2.iSegment, p2, itsct .iSegment, p1);
  520. }
  521. static inline coordf_t measure_perimeter_prev_segment_length(
  522. const ExPolygonWithOffset &poly_with_offset,
  523. const std::vector<SegmentedIntersectionLine> &segs,
  524. size_t iVerticalLine,
  525. size_t iInnerContour,
  526. size_t iIntersection,
  527. size_t iIntersection2)
  528. {
  529. return measure_perimeter_prev_next_segment_length(poly_with_offset, segs, iVerticalLine, iInnerContour, iIntersection, iIntersection2, false);
  530. }
  531. static inline coordf_t measure_perimeter_next_segment_length(
  532. const ExPolygonWithOffset &poly_with_offset,
  533. const std::vector<SegmentedIntersectionLine> &segs,
  534. size_t iVerticalLine,
  535. size_t iInnerContour,
  536. size_t iIntersection,
  537. size_t iIntersection2)
  538. {
  539. return measure_perimeter_prev_next_segment_length(poly_with_offset, segs, iVerticalLine, iInnerContour, iIntersection, iIntersection2, true);
  540. }
  541. // Append the points of a perimeter segment when going from iIntersection to iIntersection2.
  542. // The first point (the point of iIntersection) will not be inserted,
  543. // the last point will be inserted.
  544. static inline void emit_perimeter_prev_next_segment(
  545. const ExPolygonWithOffset &poly_with_offset,
  546. const std::vector<SegmentedIntersectionLine> &segs,
  547. size_t iVerticalLine,
  548. size_t iInnerContour,
  549. size_t iIntersection,
  550. size_t iIntersection2,
  551. Polyline &out,
  552. bool dir_is_next)
  553. {
  554. size_t iVerticalLineOther = iVerticalLine;
  555. if (dir_is_next) {
  556. ++ iVerticalLineOther;
  557. assert(iVerticalLineOther < segs.size());
  558. } else {
  559. assert(iVerticalLineOther > 0);
  560. -- iVerticalLineOther;
  561. }
  562. const SegmentedIntersectionLine &il = segs[iVerticalLine];
  563. const SegmentIntersection &itsct = il.intersections[iIntersection];
  564. const SegmentedIntersectionLine &il2 = segs[iVerticalLineOther];
  565. const SegmentIntersection &itsct2 = il2.intersections[iIntersection2];
  566. const Polygon &poly = poly_with_offset.contour(iInnerContour);
  567. // const bool ccw = poly_with_offset.is_contour_ccw(iInnerContour);
  568. assert(itsct.type == itsct2.type);
  569. assert(itsct.iContour == itsct2.iContour);
  570. assert(itsct.is_inner());
  571. const bool forward = itsct.is_low() == dir_is_next;
  572. // Do not append the first point.
  573. // out.points.push_back(Point(il.pos, itsct.pos));
  574. if (forward)
  575. polygon_segment_append(out.points, poly, itsct.iSegment, itsct2.iSegment);
  576. else
  577. polygon_segment_append_reversed(out.points, poly, itsct.iSegment, itsct2.iSegment);
  578. // Append the last point.
  579. out.points.push_back(Point(il2.pos, itsct2.pos()));
  580. }
  581. static inline coordf_t measure_perimeter_segment_on_vertical_line_length(
  582. const ExPolygonWithOffset &poly_with_offset,
  583. const std::vector<SegmentedIntersectionLine> &segs,
  584. size_t iVerticalLine,
  585. size_t iInnerContour,
  586. size_t iIntersection,
  587. size_t iIntersection2,
  588. bool forward)
  589. {
  590. const SegmentedIntersectionLine &il = segs[iVerticalLine];
  591. const SegmentIntersection &itsct = il.intersections[iIntersection];
  592. const SegmentIntersection &itsct2 = il.intersections[iIntersection2];
  593. const Polygon &poly = poly_with_offset.contour(iInnerContour);
  594. assert(itsct.is_inner());
  595. assert(itsct2.is_inner());
  596. assert(itsct.type != itsct2.type);
  597. assert(itsct.iContour == iInnerContour);
  598. assert(itsct.iContour == itsct2.iContour);
  599. Point p1(il.pos, itsct.pos());
  600. Point p2(il.pos, itsct2.pos());
  601. return forward ?
  602. segment_length(poly, itsct .iSegment, p1, itsct2.iSegment, p2) :
  603. segment_length(poly, itsct2.iSegment, p2, itsct .iSegment, p1);
  604. }
  605. // Append the points of a perimeter segment when going from iIntersection to iIntersection2.
  606. // The first point (the point of iIntersection) will not be inserted,
  607. // the last point will be inserted.
  608. static inline void emit_perimeter_segment_on_vertical_line(
  609. const ExPolygonWithOffset &poly_with_offset,
  610. const std::vector<SegmentedIntersectionLine> &segs,
  611. size_t iVerticalLine,
  612. size_t iInnerContour,
  613. size_t iIntersection,
  614. size_t iIntersection2,
  615. Polyline &out,
  616. bool forward)
  617. {
  618. const SegmentedIntersectionLine &il = segs[iVerticalLine];
  619. const SegmentIntersection &itsct = il.intersections[iIntersection];
  620. const SegmentIntersection &itsct2 = il.intersections[iIntersection2];
  621. const Polygon &poly = poly_with_offset.contour(iInnerContour);
  622. assert(itsct.is_inner());
  623. assert(itsct2.is_inner());
  624. assert(itsct.type != itsct2.type);
  625. assert(itsct.iContour == iInnerContour);
  626. assert(itsct.iContour == itsct2.iContour);
  627. // Do not append the first point.
  628. // out.points.push_back(Point(il.pos, itsct.pos));
  629. if (forward)
  630. polygon_segment_append(out.points, poly, itsct.iSegment, itsct2.iSegment);
  631. else
  632. polygon_segment_append_reversed(out.points, poly, itsct.iSegment, itsct2.iSegment);
  633. // Append the last point.
  634. out.points.push_back(Point(il.pos, itsct2.pos()));
  635. }
  636. //TBD: For precise infill, measure the area of a slab spanned by an infill line.
  637. /*
  638. static inline float measure_outer_contour_slab(
  639. const ExPolygonWithOffset &poly_with_offset,
  640. const std::vector<SegmentedIntersectionLine> &segs,
  641. size_t i_vline,
  642. size_t iIntersection)
  643. {
  644. const SegmentedIntersectionLine &il = segs[i_vline];
  645. const SegmentIntersection &itsct = il.intersections[i_vline];
  646. const SegmentIntersection &itsct2 = il.intersections[iIntersection2];
  647. const Polygon &poly = poly_with_offset.contour((itsct.iContour);
  648. assert(itsct.is_outer());
  649. assert(itsct2.is_outer());
  650. assert(itsct.type != itsct2.type);
  651. assert(itsct.iContour == itsct2.iContour);
  652. if (! itsct.is_outer() || ! itsct2.is_outer() || itsct.type == itsct2.type || itsct.iContour != itsct2.iContour)
  653. // Error, return zero area.
  654. return 0.f;
  655. // Find possible connection points on the previous / next vertical line.
  656. int iPrev = intersection_on_prev_vertical_line(poly_with_offset, segs, i_vline, itsct.iContour, i_intersection);
  657. int iNext = intersection_on_next_vertical_line(poly_with_offset, segs, i_vline, itsct.iContour, i_intersection);
  658. // Find possible connection points on the same vertical line.
  659. int iAbove = iBelow = -1;
  660. // Does the perimeter intersect the current vertical line above intrsctn?
  661. for (size_t i = i_intersection + 1; i + 1 < seg.intersections.size(); ++ i)
  662. if (seg.intersections[i].iContour == itsct.iContour)
  663. { iAbove = i; break; }
  664. // Does the perimeter intersect the current vertical line below intrsctn?
  665. for (int i = int(i_intersection) - 1; i > 0; -- i)
  666. if (seg.intersections[i].iContour == itsct.iContour)
  667. { iBelow = i; break; }
  668. if (iSegAbove != -1 && seg.intersections[iAbove].type == SegmentIntersection::OUTER_HIGH) {
  669. // Invalidate iPrev resp. iNext, if the perimeter crosses the current vertical line earlier than iPrev resp. iNext.
  670. // The perimeter contour orientation.
  671. const Polygon &poly = poly_with_offset.contour(itsct.iContour);
  672. {
  673. int d_horiz = (iPrev == -1) ? std::numeric_limits<int>::max() :
  674. distance_of_segmens(poly, segs[i_vline-1].intersections[iPrev].iSegment, itsct.iSegment, true);
  675. int d_down = (iBelow == -1) ? std::numeric_limits<int>::max() :
  676. distance_of_segmens(poly, iSegBelow, itsct.iSegment, true);
  677. int d_up = (iAbove == -1) ? std::numeric_limits<int>::max() :
  678. distance_of_segmens(poly, iSegAbove, itsct.iSegment, true);
  679. if (intrsctn_type_prev == INTERSECTION_TYPE_OTHER_VLINE_OK && d_horiz > std::min(d_down, d_up))
  680. // The vertical crossing comes eralier than the prev crossing.
  681. // Disable the perimeter going back.
  682. intrsctn_type_prev = INTERSECTION_TYPE_OTHER_VLINE_NOT_FIRST;
  683. if (d_up > std::min(d_horiz, d_down))
  684. // The horizontal crossing comes earlier than the vertical crossing.
  685. vert_seg_dir_valid_mask &= ~DIR_BACKWARD;
  686. }
  687. {
  688. int d_horiz = (iNext == -1) ? std::numeric_limits<int>::max() :
  689. distance_of_segmens(poly, itsct.iSegment, segs[i_vline+1].intersections[iNext].iSegment, true);
  690. int d_down = (iSegBelow == -1) ? std::numeric_limits<int>::max() :
  691. distance_of_segmens(poly, itsct.iSegment, iSegBelow, true);
  692. int d_up = (iSegAbove == -1) ? std::numeric_limits<int>::max() :
  693. distance_of_segmens(poly, itsct.iSegment, iSegAbove, true);
  694. if (d_up > std::min(d_horiz, d_down))
  695. // The horizontal crossing comes earlier than the vertical crossing.
  696. vert_seg_dir_valid_mask &= ~DIR_FORWARD;
  697. }
  698. }
  699. }
  700. */
  701. enum DirectionMask
  702. {
  703. DIR_FORWARD = 1,
  704. DIR_BACKWARD = 2
  705. };
  706. bool FillRectilinear2::fill_surface_by_lines(const Surface *surface, const FillParams &params, float angleBase, float pattern_shift, Polylines &polylines_out)
  707. {
  708. // At the end, only the new polylines will be rotated back.
  709. size_t n_polylines_out_initial = polylines_out.size();
  710. // Shrink the input polygon a bit first to not push the infill lines out of the perimeters.
  711. // const float INFILL_OVERLAP_OVER_SPACING = 0.3f;
  712. const float INFILL_OVERLAP_OVER_SPACING = 0.45f;
  713. assert(INFILL_OVERLAP_OVER_SPACING > 0 && INFILL_OVERLAP_OVER_SPACING < 0.5f);
  714. // Rotate polygons so that we can work with vertical lines here
  715. std::pair<float, Point> rotate_vector = this->_infill_direction(surface);
  716. rotate_vector.first += angleBase;
  717. assert(params.density > 0.0001f && params.density <= 1.f);
  718. coord_t line_spacing = coord_t(scale_(this->spacing) / params.density);
  719. // On the polygons of poly_with_offset, the infill lines will be connected.
  720. ExPolygonWithOffset poly_with_offset(
  721. surface->expolygon,
  722. - rotate_vector.first,
  723. scale_(0 - (0.5 - INFILL_OVERLAP_OVER_SPACING) * this->spacing),
  724. scale_(0 - 0.5 * this->spacing));
  725. if (poly_with_offset.n_contours_inner == 0) {
  726. // Not a single infill line fits.
  727. //FIXME maybe one shall trigger the gap fill here?
  728. return true;
  729. }
  730. BoundingBox bounding_box = poly_with_offset.bounding_box_src();
  731. // define flow spacing according to requested density
  732. if (params.full_infill() && !params.dont_adjust) {
  733. line_spacing = this->_adjust_solid_spacing(bounding_box.size().x, line_spacing);
  734. this->spacing = unscale(line_spacing);
  735. } else {
  736. // extend bounding box so that our pattern will be aligned with other layers
  737. // Transform the reference point to the rotated coordinate system.
  738. Point refpt = rotate_vector.second.rotated(- rotate_vector.first);
  739. // _align_to_grid will not work correctly with positive pattern_shift.
  740. coord_t pattern_shift_scaled = coord_t(scale_(pattern_shift)) % line_spacing;
  741. refpt.x -= (pattern_shift_scaled >= 0) ? pattern_shift_scaled : (line_spacing + pattern_shift_scaled);
  742. bounding_box.merge(_align_to_grid(
  743. bounding_box.min,
  744. Point(line_spacing, line_spacing),
  745. refpt));
  746. }
  747. // Intersect a set of euqally spaced vertical lines wiht expolygon.
  748. // n_vlines = ceil(bbox_width / line_spacing)
  749. size_t n_vlines = (bounding_box.max.x - bounding_box.min.x + line_spacing - 1) / line_spacing;
  750. coord_t x0 = bounding_box.min.x;
  751. if (params.full_infill())
  752. x0 += (line_spacing + SCALED_EPSILON) / 2;
  753. #ifdef SLIC3R_DEBUG
  754. static int iRun = 0;
  755. BoundingBox bbox_svg = poly_with_offset.bounding_box_outer();
  756. ::Slic3r::SVG svg(debug_out_path("FillRectilinear2-%d.svg", iRun), bbox_svg); // , scale_(1.));
  757. poly_with_offset.export_to_svg(svg);
  758. {
  759. ::Slic3r::SVG svg(debug_out_path("FillRectilinear2-initial-%d.svg", iRun), bbox_svg); // , scale_(1.));
  760. poly_with_offset.export_to_svg(svg);
  761. }
  762. iRun ++;
  763. #endif /* SLIC3R_DEBUG */
  764. // For each contour
  765. // Allocate storage for the segments.
  766. std::vector<SegmentedIntersectionLine> segs(n_vlines, SegmentedIntersectionLine());
  767. for (size_t i = 0; i < n_vlines; ++ i) {
  768. segs[i].idx = i;
  769. segs[i].pos = x0 + i * line_spacing;
  770. }
  771. for (size_t iContour = 0; iContour < poly_with_offset.n_contours; ++ iContour) {
  772. const Points &contour = poly_with_offset.contour(iContour).points;
  773. if (contour.size() < 2)
  774. continue;
  775. // For each segment
  776. for (size_t iSegment = 0; iSegment < contour.size(); ++ iSegment) {
  777. size_t iPrev = ((iSegment == 0) ? contour.size() : iSegment) - 1;
  778. const Point &p1 = contour[iPrev];
  779. const Point &p2 = contour[iSegment];
  780. // Which of the equally spaced vertical lines is intersected by this segment?
  781. coord_t l = p1.x;
  782. coord_t r = p2.x;
  783. if (l > r)
  784. std::swap(l, r);
  785. // il, ir are the left / right indices of vertical lines intersecting a segment
  786. int il = (l - x0) / line_spacing;
  787. while (il * line_spacing + x0 < l)
  788. ++ il;
  789. il = std::max(int(0), il);
  790. int ir = (r - x0 + line_spacing) / line_spacing;
  791. while (ir * line_spacing + x0 > r)
  792. -- ir;
  793. ir = std::min(int(segs.size()) - 1, ir);
  794. if (il > ir)
  795. // No vertical line intersects this segment.
  796. continue;
  797. assert(il >= 0 && il < segs.size());
  798. assert(ir >= 0 && ir < segs.size());
  799. for (int i = il; i <= ir; ++ i) {
  800. coord_t this_x = segs[i].pos;
  801. assert(this_x == i * line_spacing + x0);
  802. SegmentIntersection is;
  803. is.iContour = iContour;
  804. is.iSegment = iSegment;
  805. assert(l <= this_x);
  806. assert(r >= this_x);
  807. // Calculate the intersection position in y axis. x is known.
  808. if (p1.x == this_x) {
  809. if (p2.x == this_x) {
  810. // Ignore strictly vertical segments.
  811. continue;
  812. }
  813. is.pos_p = p1.y;
  814. is.pos_q = 1;
  815. } else if (p2.x == this_x) {
  816. is.pos_p = p2.y;
  817. is.pos_q = 1;
  818. } else {
  819. // First calculate the intersection parameter 't' as a rational number with non negative denominator.
  820. if (p2.x > p1.x) {
  821. is.pos_p = this_x - p1.x;
  822. is.pos_q = p2.x - p1.x;
  823. } else {
  824. is.pos_p = p1.x - this_x;
  825. is.pos_q = p1.x - p2.x;
  826. }
  827. assert(is.pos_p >= 0 && is.pos_p <= is.pos_q);
  828. // Make an intersection point from the 't'.
  829. is.pos_p *= int64_t(p2.y - p1.y);
  830. is.pos_p += p1.y * int64_t(is.pos_q);
  831. }
  832. // +-1 to take rounding into account.
  833. assert(is.pos() + 1 >= std::min(p1.y, p2.y));
  834. assert(is.pos() <= std::max(p1.y, p2.y) + 1);
  835. segs[i].intersections.push_back(is);
  836. }
  837. }
  838. }
  839. // Sort the intersections along their segments, specify the intersection types.
  840. for (size_t i_seg = 0; i_seg < segs.size(); ++ i_seg) {
  841. SegmentedIntersectionLine &sil = segs[i_seg];
  842. // Sort the intersection points using exact rational arithmetic.
  843. std::sort(sil.intersections.begin(), sil.intersections.end());
  844. // Assign the intersection types, remove duplicate or overlapping intersection points.
  845. // When a loop vertex touches a vertical line, intersection point is generated for both segments.
  846. // If such two segments are oriented equally, then one of them is removed.
  847. // Otherwise the vertex is tangential to the vertical line and both segments are removed.
  848. // The same rule applies, if the loop is pinched into a single point and this point touches the vertical line:
  849. // The loop has a zero vertical size at the vertical line, therefore the intersection point is removed.
  850. size_t j = 0;
  851. for (size_t i = 0; i < sil.intersections.size(); ++ i) {
  852. // What is the orientation of the segment at the intersection point?
  853. size_t iContour = sil.intersections[i].iContour;
  854. const Points &contour = poly_with_offset.contour(iContour).points;
  855. size_t iSegment = sil.intersections[i].iSegment;
  856. size_t iPrev = ((iSegment == 0) ? contour.size() : iSegment) - 1;
  857. coord_t dir = contour[iSegment].x - contour[iPrev].x;
  858. bool low = dir > 0;
  859. sil.intersections[i].type = poly_with_offset.is_contour_outer(iContour) ?
  860. (low ? SegmentIntersection::OUTER_LOW : SegmentIntersection::OUTER_HIGH) :
  861. (low ? SegmentIntersection::INNER_LOW : SegmentIntersection::INNER_HIGH);
  862. if (j > 0 && sil.intersections[i].iContour == sil.intersections[j-1].iContour) {
  863. // Two successive intersection points on a vertical line with the same contour. This may be a special case.
  864. if (sil.intersections[i].pos() == sil.intersections[j-1].pos()) {
  865. // Two successive segments meet exactly at the vertical line.
  866. #ifdef SLIC3R_DEBUG
  867. // Verify that the segments of sil.intersections[i] and sil.intersections[j-1] are adjoint.
  868. size_t iSegment2 = sil.intersections[j-1].iSegment;
  869. size_t iPrev2 = ((iSegment2 == 0) ? contour.size() : iSegment2) - 1;
  870. assert(iSegment == iPrev2 || iSegment2 == iPrev);
  871. #endif /* SLIC3R_DEBUG */
  872. if (sil.intersections[i].type == sil.intersections[j-1].type) {
  873. // Two successive segments of the same direction (both to the right or both to the left)
  874. // meet exactly at the vertical line.
  875. // Remove the second intersection point.
  876. } else {
  877. // This is a loop returning to the same point.
  878. // It may as well be a vertex of a loop touching this vertical line.
  879. // Remove both the lines.
  880. -- j;
  881. }
  882. } else if (sil.intersections[i].type == sil.intersections[j-1].type) {
  883. // Two non successive segments of the same direction (both to the right or both to the left)
  884. // meet exactly at the vertical line. That means there is a Z shaped path, where the center segment
  885. // of the Z shaped path is aligned with this vertical line.
  886. // Remove one of the intersection points while maximizing the vertical segment length.
  887. if (low) {
  888. // Remove the second intersection point, keep the first intersection point.
  889. } else {
  890. // Remove the first intersection point, keep the second intersection point.
  891. sil.intersections[j-1] = sil.intersections[i];
  892. }
  893. } else {
  894. // Vertical line intersects a contour segment at a general position (not at one of its end points).
  895. // or the contour just touches this vertical line with a vertical segment or a sequence of vertical segments.
  896. // Keep both intersection points.
  897. if (j < i)
  898. sil.intersections[j] = sil.intersections[i];
  899. ++ j;
  900. }
  901. } else {
  902. // Vertical line intersects a contour segment at a general position (not at one of its end points).
  903. if (j < i)
  904. sil.intersections[j] = sil.intersections[i];
  905. ++ j;
  906. }
  907. }
  908. // Shrink the list of intersections, if any of the intersection was removed during the classification.
  909. if (j < sil.intersections.size())
  910. sil.intersections.erase(sil.intersections.begin() + j, sil.intersections.end());
  911. }
  912. // Verify the segments. If something is wrong, give up.
  913. #define ASSERT_OR_RETURN(CONDITION) do { assert(CONDITION); if (! (CONDITION)) return false; } while (0)
  914. for (size_t i_seg = 0; i_seg < segs.size(); ++ i_seg) {
  915. SegmentedIntersectionLine &sil = segs[i_seg];
  916. // The intersection points have to be even.
  917. ASSERT_OR_RETURN((sil.intersections.size() & 1) == 0);
  918. for (size_t i = 0; i < sil.intersections.size();) {
  919. // An intersection segment crossing the bigger contour may cross the inner offsetted contour even number of times.
  920. ASSERT_OR_RETURN(sil.intersections[i].type == SegmentIntersection::OUTER_LOW);
  921. size_t j = i + 1;
  922. ASSERT_OR_RETURN(j < sil.intersections.size());
  923. ASSERT_OR_RETURN(sil.intersections[j].type == SegmentIntersection::INNER_LOW || sil.intersections[j].type == SegmentIntersection::OUTER_HIGH);
  924. for (; j < sil.intersections.size() && sil.intersections[j].is_inner(); ++ j) ;
  925. ASSERT_OR_RETURN(j < sil.intersections.size());
  926. ASSERT_OR_RETURN((j & 1) == 1);
  927. ASSERT_OR_RETURN(sil.intersections[j].type == SegmentIntersection::OUTER_HIGH);
  928. ASSERT_OR_RETURN(i + 1 == j || sil.intersections[j - 1].type == SegmentIntersection::INNER_HIGH);
  929. i = j + 1;
  930. }
  931. }
  932. #undef ASSERT_OR_RETURN
  933. #ifdef SLIC3R_DEBUG
  934. // Paint the segments and finalize the SVG file.
  935. for (size_t i_seg = 0; i_seg < segs.size(); ++ i_seg) {
  936. SegmentedIntersectionLine &sil = segs[i_seg];
  937. for (size_t i = 0; i < sil.intersections.size();) {
  938. size_t j = i + 1;
  939. for (; j < sil.intersections.size() && sil.intersections[j].is_inner(); ++ j) ;
  940. if (i + 1 == j) {
  941. svg.draw(Line(Point(sil.pos, sil.intersections[i].pos()), Point(sil.pos, sil.intersections[j].pos())), "blue");
  942. } else {
  943. svg.draw(Line(Point(sil.pos, sil.intersections[i].pos()), Point(sil.pos, sil.intersections[i+1].pos())), "green");
  944. svg.draw(Line(Point(sil.pos, sil.intersections[i+1].pos()), Point(sil.pos, sil.intersections[j-1].pos())), (j - i + 1 > 4) ? "yellow" : "magenta");
  945. svg.draw(Line(Point(sil.pos, sil.intersections[j-1].pos()), Point(sil.pos, sil.intersections[j].pos())), "green");
  946. }
  947. i = j + 1;
  948. }
  949. }
  950. svg.Close();
  951. #endif /* SLIC3R_DEBUG */
  952. // For each outer only chords, measure their maximum distance to the bow of the outer contour.
  953. // Mark an outer only chord as consumed, if the distance is low.
  954. for (size_t i_vline = 0; i_vline < segs.size(); ++ i_vline) {
  955. SegmentedIntersectionLine &seg = segs[i_vline];
  956. for (size_t i_intersection = 0; i_intersection + 1 < seg.intersections.size(); ++ i_intersection) {
  957. if (seg.intersections[i_intersection].type == SegmentIntersection::OUTER_LOW &&
  958. seg.intersections[i_intersection+1].type == SegmentIntersection::OUTER_HIGH) {
  959. bool consumed = false;
  960. // if (params.full_infill()) {
  961. // measure_outer_contour_slab(poly_with_offset, segs, i_vline, i_ntersection);
  962. // } else
  963. consumed = true;
  964. seg.intersections[i_intersection].consumed_vertical_up = consumed;
  965. }
  966. }
  967. }
  968. // Now construct a graph.
  969. // Find the first point.
  970. // Naively one would expect to achieve best results by chaining the paths by the shortest distance,
  971. // but that procedure does not create the longest continuous paths.
  972. // A simple "sweep left to right" procedure achieves better results.
  973. size_t i_vline = 0;
  974. size_t i_intersection = size_t(-1);
  975. // Follow the line, connect the lines into a graph.
  976. // Until no new line could be added to the output path:
  977. Point pointLast;
  978. Polyline *polyline_current = NULL;
  979. if (! polylines_out.empty())
  980. pointLast = polylines_out.back().points.back();
  981. for (;;) {
  982. if (i_intersection == size_t(-1)) {
  983. // The path has been interrupted. Find a next starting point, closest to the previous extruder position.
  984. coordf_t dist2min = std::numeric_limits<coordf_t>().max();
  985. for (size_t i_vline2 = 0; i_vline2 < segs.size(); ++ i_vline2) {
  986. const SegmentedIntersectionLine &seg = segs[i_vline2];
  987. if (! seg.intersections.empty()) {
  988. assert(seg.intersections.size() > 1);
  989. // Even number of intersections with the loops.
  990. assert((seg.intersections.size() & 1) == 0);
  991. assert(seg.intersections.front().type == SegmentIntersection::OUTER_LOW);
  992. for (size_t i = 0; i < seg.intersections.size(); ++ i) {
  993. const SegmentIntersection &intrsctn = seg.intersections[i];
  994. if (intrsctn.is_outer()) {
  995. assert(intrsctn.is_low() || i > 0);
  996. bool consumed = intrsctn.is_low() ?
  997. intrsctn.consumed_vertical_up :
  998. seg.intersections[i-1].consumed_vertical_up;
  999. if (! consumed) {
  1000. coordf_t dist2 = sqr(coordf_t(pointLast.x - seg.pos)) + sqr(coordf_t(pointLast.y - intrsctn.pos()));
  1001. if (dist2 < dist2min) {
  1002. dist2min = dist2;
  1003. i_vline = i_vline2;
  1004. i_intersection = i;
  1005. //FIXME We are taking the first left point always. Verify, that the caller chains the paths
  1006. // by a shortest distance, while reversing the paths if needed.
  1007. //if (polylines_out.empty())
  1008. // Initial state, take the first line, which is the first from the left.
  1009. goto found;
  1010. }
  1011. }
  1012. }
  1013. }
  1014. }
  1015. }
  1016. if (i_intersection == size_t(-1))
  1017. // We are finished.
  1018. break;
  1019. found:
  1020. // Start a new path.
  1021. polylines_out.push_back(Polyline());
  1022. polyline_current = &polylines_out.back();
  1023. // Emit the first point of a path.
  1024. pointLast = Point(segs[i_vline].pos, segs[i_vline].intersections[i_intersection].pos());
  1025. polyline_current->points.push_back(pointLast);
  1026. }
  1027. // From the initial point (i_vline, i_intersection), follow a path.
  1028. SegmentedIntersectionLine &seg = segs[i_vline];
  1029. SegmentIntersection *intrsctn = &seg.intersections[i_intersection];
  1030. bool going_up = intrsctn->is_low();
  1031. bool try_connect = false;
  1032. if (going_up) {
  1033. assert(! intrsctn->consumed_vertical_up);
  1034. assert(i_intersection + 1 < seg.intersections.size());
  1035. // Step back to the beginning of the vertical segment to mark it as consumed.
  1036. if (intrsctn->is_inner()) {
  1037. assert(i_intersection > 0);
  1038. -- intrsctn;
  1039. -- i_intersection;
  1040. }
  1041. // Consume the complete vertical segment up to the outer contour.
  1042. do {
  1043. intrsctn->consumed_vertical_up = true;
  1044. ++ intrsctn;
  1045. ++ i_intersection;
  1046. assert(i_intersection < seg.intersections.size());
  1047. } while (intrsctn->type != SegmentIntersection::OUTER_HIGH);
  1048. if ((intrsctn - 1)->is_inner()) {
  1049. // Step back.
  1050. -- intrsctn;
  1051. -- i_intersection;
  1052. assert(intrsctn->type == SegmentIntersection::INNER_HIGH);
  1053. try_connect = true;
  1054. }
  1055. } else {
  1056. // Going down.
  1057. assert(intrsctn->is_high());
  1058. assert(i_intersection > 0);
  1059. assert(! (intrsctn - 1)->consumed_vertical_up);
  1060. // Consume the complete vertical segment up to the outer contour.
  1061. if (intrsctn->is_inner())
  1062. intrsctn->consumed_vertical_up = true;
  1063. do {
  1064. assert(i_intersection > 0);
  1065. -- intrsctn;
  1066. -- i_intersection;
  1067. intrsctn->consumed_vertical_up = true;
  1068. } while (intrsctn->type != SegmentIntersection::OUTER_LOW);
  1069. if ((intrsctn + 1)->is_inner()) {
  1070. // Step back.
  1071. ++ intrsctn;
  1072. ++ i_intersection;
  1073. assert(intrsctn->type == SegmentIntersection::INNER_LOW);
  1074. try_connect = true;
  1075. }
  1076. }
  1077. if (try_connect) {
  1078. // Decide, whether to finish the segment, or whether to follow the perimeter.
  1079. // 1) Find possible connection points on the previous / next vertical line.
  1080. int iPrev = intersection_on_prev_vertical_line(poly_with_offset, segs, i_vline, intrsctn->iContour, i_intersection);
  1081. int iNext = intersection_on_next_vertical_line(poly_with_offset, segs, i_vline, intrsctn->iContour, i_intersection);
  1082. IntersectionTypeOtherVLine intrsctn_type_prev = intersection_type_on_prev_vertical_line(segs, i_vline, i_intersection, iPrev);
  1083. IntersectionTypeOtherVLine intrsctn_type_next = intersection_type_on_next_vertical_line(segs, i_vline, i_intersection, iNext);
  1084. // 2) Find possible connection points on the same vertical line.
  1085. int iAbove = -1;
  1086. int iBelow = -1;
  1087. int iSegAbove = -1;
  1088. int iSegBelow = -1;
  1089. {
  1090. SegmentIntersection::SegmentIntersectionType type_crossing = (intrsctn->type == SegmentIntersection::INNER_LOW) ?
  1091. SegmentIntersection::INNER_HIGH : SegmentIntersection::INNER_LOW;
  1092. // Does the perimeter intersect the current vertical line above intrsctn?
  1093. for (size_t i = i_intersection + 1; i + 1 < seg.intersections.size(); ++ i)
  1094. // if (seg.intersections[i].iContour == intrsctn->iContour && seg.intersections[i].type == type_crossing) {
  1095. if (seg.intersections[i].iContour == intrsctn->iContour) {
  1096. iAbove = i;
  1097. iSegAbove = seg.intersections[i].iSegment;
  1098. break;
  1099. }
  1100. // Does the perimeter intersect the current vertical line below intrsctn?
  1101. for (size_t i = i_intersection - 1; i > 0; -- i)
  1102. // if (seg.intersections[i].iContour == intrsctn->iContour && seg.intersections[i].type == type_crossing) {
  1103. if (seg.intersections[i].iContour == intrsctn->iContour) {
  1104. iBelow = i;
  1105. iSegBelow = seg.intersections[i].iSegment;
  1106. break;
  1107. }
  1108. }
  1109. // 3) Sort the intersection points, clear iPrev / iNext / iSegBelow / iSegAbove,
  1110. // if it is preceded by any other intersection point along the contour.
  1111. unsigned int vert_seg_dir_valid_mask =
  1112. (going_up ?
  1113. (iSegAbove != -1 && seg.intersections[iAbove].type == SegmentIntersection::INNER_LOW) :
  1114. (iSegBelow != -1 && seg.intersections[iBelow].type == SegmentIntersection::INNER_HIGH)) ?
  1115. (DIR_FORWARD | DIR_BACKWARD) :
  1116. 0;
  1117. {
  1118. // Invalidate iPrev resp. iNext, if the perimeter crosses the current vertical line earlier than iPrev resp. iNext.
  1119. // The perimeter contour orientation.
  1120. const bool forward = intrsctn->is_low(); // == poly_with_offset.is_contour_ccw(intrsctn->iContour);
  1121. const Polygon &poly = poly_with_offset.contour(intrsctn->iContour);
  1122. {
  1123. int d_horiz = (iPrev == -1) ? std::numeric_limits<int>::max() :
  1124. distance_of_segmens(poly, segs[i_vline-1].intersections[iPrev].iSegment, intrsctn->iSegment, forward);
  1125. int d_down = (iSegBelow == -1) ? std::numeric_limits<int>::max() :
  1126. distance_of_segmens(poly, iSegBelow, intrsctn->iSegment, forward);
  1127. int d_up = (iSegAbove == -1) ? std::numeric_limits<int>::max() :
  1128. distance_of_segmens(poly, iSegAbove, intrsctn->iSegment, forward);
  1129. if (intrsctn_type_prev == INTERSECTION_TYPE_OTHER_VLINE_OK && d_horiz > std::min(d_down, d_up))
  1130. // The vertical crossing comes eralier than the prev crossing.
  1131. // Disable the perimeter going back.
  1132. intrsctn_type_prev = INTERSECTION_TYPE_OTHER_VLINE_NOT_FIRST;
  1133. if (going_up ? (d_up > std::min(d_horiz, d_down)) : (d_down > std::min(d_horiz, d_up)))
  1134. // The horizontal crossing comes earlier than the vertical crossing.
  1135. vert_seg_dir_valid_mask &= ~(forward ? DIR_BACKWARD : DIR_FORWARD);
  1136. }
  1137. {
  1138. int d_horiz = (iNext == -1) ? std::numeric_limits<int>::max() :
  1139. distance_of_segmens(poly, intrsctn->iSegment, segs[i_vline+1].intersections[iNext].iSegment, forward);
  1140. int d_down = (iSegBelow == -1) ? std::numeric_limits<int>::max() :
  1141. distance_of_segmens(poly, intrsctn->iSegment, iSegBelow, forward);
  1142. int d_up = (iSegAbove == -1) ? std::numeric_limits<int>::max() :
  1143. distance_of_segmens(poly, intrsctn->iSegment, iSegAbove, forward);
  1144. if (intrsctn_type_next == INTERSECTION_TYPE_OTHER_VLINE_OK && d_horiz > std::min(d_down, d_up))
  1145. // The vertical crossing comes eralier than the prev crossing.
  1146. // Disable the perimeter going forward.
  1147. intrsctn_type_next = INTERSECTION_TYPE_OTHER_VLINE_NOT_FIRST;
  1148. if (going_up ? (d_up > std::min(d_horiz, d_down)) : (d_down > std::min(d_horiz, d_up)))
  1149. // The horizontal crossing comes earlier than the vertical crossing.
  1150. vert_seg_dir_valid_mask &= ~(forward ? DIR_FORWARD : DIR_BACKWARD);
  1151. }
  1152. }
  1153. // 4) Try to connect to a previous or next vertical line, making a zig-zag pattern.
  1154. if (intrsctn_type_prev == INTERSECTION_TYPE_OTHER_VLINE_OK || intrsctn_type_next == INTERSECTION_TYPE_OTHER_VLINE_OK) {
  1155. coordf_t distPrev = (intrsctn_type_prev != INTERSECTION_TYPE_OTHER_VLINE_OK) ? std::numeric_limits<coord_t>::max() :
  1156. measure_perimeter_prev_segment_length(poly_with_offset, segs, i_vline, intrsctn->iContour, i_intersection, iPrev);
  1157. coordf_t distNext = (intrsctn_type_next != INTERSECTION_TYPE_OTHER_VLINE_OK) ? std::numeric_limits<coord_t>::max() :
  1158. measure_perimeter_next_segment_length(poly_with_offset, segs, i_vline, intrsctn->iContour, i_intersection, iNext);
  1159. // Take the shorter path.
  1160. //FIXME this may not be always the best strategy to take the shortest connection line now.
  1161. bool take_next = (intrsctn_type_prev == INTERSECTION_TYPE_OTHER_VLINE_OK && intrsctn_type_next == INTERSECTION_TYPE_OTHER_VLINE_OK) ?
  1162. (distNext < distPrev) :
  1163. intrsctn_type_next == INTERSECTION_TYPE_OTHER_VLINE_OK;
  1164. assert(intrsctn->is_inner());
  1165. bool skip = params.dont_connect || (link_max_length > 0 && (take_next ? distNext : distPrev) > link_max_length);
  1166. if (skip) {
  1167. // Just skip the connecting contour and start a new path.
  1168. goto dont_connect;
  1169. polyline_current->points.push_back(Point(seg.pos, intrsctn->pos()));
  1170. polylines_out.push_back(Polyline());
  1171. polyline_current = &polylines_out.back();
  1172. const SegmentedIntersectionLine &il2 = segs[take_next ? (i_vline + 1) : (i_vline - 1)];
  1173. polyline_current->points.push_back(Point(il2.pos, il2.intersections[take_next ? iNext : iPrev].pos()));
  1174. } else {
  1175. polyline_current->points.push_back(Point(seg.pos, intrsctn->pos()));
  1176. emit_perimeter_prev_next_segment(poly_with_offset, segs, i_vline, intrsctn->iContour, i_intersection, take_next ? iNext : iPrev, *polyline_current, take_next);
  1177. }
  1178. // Mark both the left and right connecting segment as consumed, because one cannot go to this intersection point as it has been consumed.
  1179. if (iPrev != -1)
  1180. segs[i_vline-1].intersections[iPrev].consumed_perimeter_right = true;
  1181. if (iNext != -1)
  1182. intrsctn->consumed_perimeter_right = true;
  1183. //FIXME consume the left / right connecting segments at the other end of this line? Currently it is not critical because a perimeter segment is not followed if the vertical segment at the other side has already been consumed.
  1184. // Advance to the neighbor line.
  1185. if (take_next) {
  1186. ++ i_vline;
  1187. i_intersection = iNext;
  1188. } else {
  1189. -- i_vline;
  1190. i_intersection = iPrev;
  1191. }
  1192. continue;
  1193. }
  1194. // 5) Try to connect to a previous or next point on the same vertical line.
  1195. if (vert_seg_dir_valid_mask) {
  1196. bool valid = true;
  1197. // Verify, that there is no intersection with the inner contour up to the end of the contour segment.
  1198. // Verify, that the successive segment has not been consumed yet.
  1199. if (going_up) {
  1200. if (seg.intersections[iAbove].consumed_vertical_up) {
  1201. valid = false;
  1202. } else {
  1203. for (int i = (int)i_intersection + 1; i < iAbove && valid; ++i)
  1204. if (seg.intersections[i].is_inner())
  1205. valid = false;
  1206. }
  1207. } else {
  1208. if (seg.intersections[iBelow-1].consumed_vertical_up) {
  1209. valid = false;
  1210. } else {
  1211. for (int i = iBelow + 1; i < (int)i_intersection && valid; ++i)
  1212. if (seg.intersections[i].is_inner())
  1213. valid = false;
  1214. }
  1215. }
  1216. if (valid) {
  1217. const Polygon &poly = poly_with_offset.contour(intrsctn->iContour);
  1218. int iNext = going_up ? iAbove : iBelow;
  1219. int iSegNext = going_up ? iSegAbove : iSegBelow;
  1220. bool dir_forward = (vert_seg_dir_valid_mask == (DIR_FORWARD | DIR_BACKWARD)) ?
  1221. // Take the shorter length between the current and the next intersection point.
  1222. (distance_of_segmens(poly, intrsctn->iSegment, iSegNext, true) <
  1223. distance_of_segmens(poly, intrsctn->iSegment, iSegNext, false)) :
  1224. (vert_seg_dir_valid_mask == DIR_FORWARD);
  1225. // Skip this perimeter line?
  1226. bool skip = params.dont_connect;
  1227. if (! skip && link_max_length > 0) {
  1228. coordf_t link_length = measure_perimeter_segment_on_vertical_line_length(
  1229. poly_with_offset, segs, i_vline, intrsctn->iContour, i_intersection, iNext, dir_forward);
  1230. skip = link_length > link_max_length;
  1231. }
  1232. polyline_current->points.push_back(Point(seg.pos, intrsctn->pos()));
  1233. if (skip) {
  1234. // Just skip the connecting contour and start a new path.
  1235. polylines_out.push_back(Polyline());
  1236. polyline_current = &polylines_out.back();
  1237. polyline_current->points.push_back(Point(seg.pos, seg.intersections[iNext].pos()));
  1238. } else {
  1239. // Consume the connecting contour and the next segment.
  1240. emit_perimeter_segment_on_vertical_line(poly_with_offset, segs, i_vline, intrsctn->iContour, i_intersection, iNext, *polyline_current, dir_forward);
  1241. }
  1242. // Mark both the left and right connecting segment as consumed, because one cannot go to this intersection point as it has been consumed.
  1243. // If there are any outer intersection points skipped (bypassed) by the contour,
  1244. // mark them as processed.
  1245. if (going_up) {
  1246. for (int i = (int)i_intersection; i < iAbove; ++ i)
  1247. seg.intersections[i].consumed_vertical_up = true;
  1248. } else {
  1249. for (int i = iBelow; i < (int)i_intersection; ++ i)
  1250. seg.intersections[i].consumed_vertical_up = true;
  1251. }
  1252. // seg.intersections[going_up ? i_intersection : i_intersection - 1].consumed_vertical_up = true;
  1253. intrsctn->consumed_perimeter_right = true;
  1254. i_intersection = iNext;
  1255. if (going_up)
  1256. ++ intrsctn;
  1257. else
  1258. -- intrsctn;
  1259. intrsctn->consumed_perimeter_right = true;
  1260. continue;
  1261. }
  1262. }
  1263. dont_connect:
  1264. // No way to continue the current polyline. Take the rest of the line up to the outer contour.
  1265. // This will finish the polyline, starting another polyline at a new point.
  1266. if (going_up)
  1267. ++ intrsctn;
  1268. else
  1269. -- intrsctn;
  1270. }
  1271. // Finish the current vertical line,
  1272. // reset the current vertical line to pick a new starting point in the next round.
  1273. assert(intrsctn->is_outer());
  1274. assert(intrsctn->is_high() == going_up);
  1275. pointLast = Point(seg.pos, intrsctn->pos());
  1276. polyline_current->points.push_back(pointLast);
  1277. // Handle duplicate points and zero length segments.
  1278. polyline_current->remove_duplicate_points();
  1279. assert(! polyline_current->has_duplicate_points());
  1280. // Handle nearly zero length edges.
  1281. if (polyline_current->points.size() <= 1 ||
  1282. (polyline_current->points.size() == 2 &&
  1283. std::abs(polyline_current->points.front().x - polyline_current->points.back().x) < SCALED_EPSILON &&
  1284. std::abs(polyline_current->points.front().y - polyline_current->points.back().y) < SCALED_EPSILON))
  1285. polylines_out.pop_back();
  1286. intrsctn = NULL;
  1287. i_intersection = -1;
  1288. polyline_current = NULL;
  1289. }
  1290. #ifdef SLIC3R_DEBUG
  1291. {
  1292. {
  1293. ::Slic3r::SVG svg(debug_out_path("FillRectilinear2-final-%03d.svg", iRun), bbox_svg); // , scale_(1.));
  1294. poly_with_offset.export_to_svg(svg);
  1295. for (size_t i = n_polylines_out_initial; i < polylines_out.size(); ++ i)
  1296. svg.draw(polylines_out[i].lines(), "black");
  1297. }
  1298. // Paint a picture per polyline. This makes it easier to discover the order of the polylines and their overlap.
  1299. for (size_t i_polyline = n_polylines_out_initial; i_polyline < polylines_out.size(); ++ i_polyline) {
  1300. ::Slic3r::SVG svg(debug_out_path("FillRectilinear2-final-%03d-%03d.svg", iRun, i_polyline), bbox_svg); // , scale_(1.));
  1301. svg.draw(polylines_out[i_polyline].lines(), "black");
  1302. }
  1303. }
  1304. #endif /* SLIC3R_DEBUG */
  1305. // paths must be rotated back
  1306. for (Polylines::iterator it = polylines_out.begin() + n_polylines_out_initial; it != polylines_out.end(); ++ it) {
  1307. // No need to translate, the absolute position is irrelevant.
  1308. // it->translate(- rotate_vector.second.x, - rotate_vector.second.y);
  1309. assert(! it->has_duplicate_points());
  1310. it->rotate(rotate_vector.first);
  1311. //FIXME rather simplify the paths to avoid very short edges?
  1312. //assert(! it->has_duplicate_points());
  1313. it->remove_duplicate_points();
  1314. }
  1315. #ifdef SLIC3R_DEBUG
  1316. // Verify, that there are no duplicate points in the sequence.
  1317. for (Polyline &polyline : polylines_out)
  1318. assert(! polyline.has_duplicate_points());
  1319. #endif /* SLIC3R_DEBUG */
  1320. return true;
  1321. }
  1322. Polylines FillRectilinear2::fill_surface(const Surface *surface, const FillParams &params)
  1323. {
  1324. Polylines polylines_out;
  1325. if (! fill_surface_by_lines(surface, params, 0.f, 0.f, polylines_out)) {
  1326. printf("FillRectilinear2::fill_surface() failed to fill a region.\n");
  1327. }
  1328. return polylines_out;
  1329. }
  1330. Polylines FillGrid2::fill_surface(const Surface *surface, const FillParams &params)
  1331. {
  1332. // Each linear fill covers half of the target coverage.
  1333. FillParams params2 = params;
  1334. params2.density *= 0.5f;
  1335. Polylines polylines_out;
  1336. if (! fill_surface_by_lines(surface, params2, 0.f, 0.f, polylines_out) ||
  1337. ! fill_surface_by_lines(surface, params2, float(M_PI / 2.), 0.f, polylines_out)) {
  1338. printf("FillGrid2::fill_surface() failed to fill a region.\n");
  1339. }
  1340. return polylines_out;
  1341. }
  1342. Polylines FillTriangles::fill_surface(const Surface *surface, const FillParams &params)
  1343. {
  1344. // Each linear fill covers 1/3 of the target coverage.
  1345. FillParams params2 = params;
  1346. params2.density *= 0.333333333f;
  1347. FillParams params3 = params2;
  1348. params3.dont_connect = true;
  1349. Polylines polylines_out;
  1350. if (! fill_surface_by_lines(surface, params2, 0.f, 0., polylines_out) ||
  1351. ! fill_surface_by_lines(surface, params2, float(M_PI / 3.), 0., polylines_out) ||
  1352. ! fill_surface_by_lines(surface, params3, float(2. * M_PI / 3.), 0., polylines_out)) {
  1353. printf("FillTriangles::fill_surface() failed to fill a region.\n");
  1354. }
  1355. return polylines_out;
  1356. }
  1357. Polylines FillStars::fill_surface(const Surface *surface, const FillParams &params)
  1358. {
  1359. // Each linear fill covers 1/3 of the target coverage.
  1360. FillParams params2 = params;
  1361. params2.density *= 0.333333333f;
  1362. FillParams params3 = params2;
  1363. params3.dont_connect = true;
  1364. Polylines polylines_out;
  1365. if (! fill_surface_by_lines(surface, params2, 0.f, 0., polylines_out) ||
  1366. ! fill_surface_by_lines(surface, params2, float(M_PI / 3.), 0., polylines_out) ||
  1367. ! fill_surface_by_lines(surface, params3, float(2. * M_PI / 3.), 0.5 * this->spacing / params2.density, polylines_out)) {
  1368. printf("FillStars::fill_surface() failed to fill a region.\n");
  1369. }
  1370. return polylines_out;
  1371. }
  1372. Polylines FillCubic::fill_surface(const Surface *surface, const FillParams &params)
  1373. {
  1374. // Each linear fill covers 1/3 of the target coverage.
  1375. FillParams params2 = params;
  1376. params2.density *= 0.333333333f;
  1377. FillParams params3 = params2;
  1378. params3.dont_connect = true;
  1379. Polylines polylines_out;
  1380. coordf_t dx = sqrt(0.5) * z;
  1381. if (! fill_surface_by_lines(surface, params2, 0.f, dx, polylines_out) ||
  1382. ! fill_surface_by_lines(surface, params2, float(M_PI / 3.), - dx, polylines_out) ||
  1383. // Rotated by PI*2/3 + PI to achieve reverse sloping wall.
  1384. ! fill_surface_by_lines(surface, params3, float(M_PI * 2. / 3.), dx, polylines_out)) {
  1385. printf("FillCubic::fill_surface() failed to fill a region.\n");
  1386. }
  1387. return polylines_out;
  1388. }
  1389. //Polylines FillRectilinear2Peri::fill_surface(const Surface *surface, const FillParams &params) {
  1390. void FillRectilinear2Peri::fill_surface_extrusion(const Surface *surface, const FillParams &params, const Flow &flow, ExtrusionEntityCollection &out) {
  1391. ExtrusionEntityCollection *eecroot = new ExtrusionEntityCollection();
  1392. //you don't want to sort the extrusions: big infill first, small second
  1393. eecroot->no_sort = true;
  1394. Polylines polylines_1;
  1395. //generate perimeter:
  1396. //TODO: better optimize start/end point?
  1397. ExPolygons path_perimeter = offset_ex(surface->expolygon, scale_(-this->spacing/2));
  1398. for (ExPolygon &expolygon : path_perimeter) {
  1399. expolygon.contour.make_counter_clockwise();
  1400. polylines_1.push_back(expolygon.contour.split_at_index(0));
  1401. for (Polygon hole : expolygon.holes) {
  1402. hole.make_clockwise();
  1403. polylines_1.push_back(hole.split_at_index(0));
  1404. }
  1405. }
  1406. // Save into layer.
  1407. auto *eec = new ExtrusionEntityCollection();
  1408. /// pass the no_sort attribute to the extrusion path
  1409. eec->no_sort = this->no_sort();
  1410. /// add it into the collection
  1411. eecroot->entities.push_back(eec);
  1412. /// push the path
  1413. extrusion_entities_append_paths(
  1414. eec->entities, STDMOVE(polylines_1),
  1415. flow.bridge ?
  1416. erBridgeInfill :
  1417. (surface->is_solid() ?
  1418. ((surface->is_top()) ? erTopSolidInfill : erSolidInfill) :
  1419. erInternalInfill),
  1420. flow.mm3_per_mm() * params.flow_mult, flow.width * params.flow_mult, flow.height);
  1421. Polylines polylines_2;
  1422. //50% overlap with the new perimeter
  1423. ExPolygons path_inner = offset2_ex(surface->expolygon, scale_(-this->spacing * 1.5), scale_(this->spacing));
  1424. for (ExPolygon &expolygon : path_inner) {
  1425. Surface surfInner(*surface, expolygon);
  1426. if (!fill_surface_by_lines(&surfInner, params, 0.f, 0.f, polylines_2)) {
  1427. printf("FillRectilinear2::fill_surface() failed to fill a region.\n");
  1428. }
  1429. }
  1430. // Save into layer.
  1431. eec = new ExtrusionEntityCollection();
  1432. /// pass the no_sort attribute to the extrusion path
  1433. eec->no_sort = this->no_sort();
  1434. /// add it into the collection
  1435. eecroot->entities.push_back(eec);
  1436. /// push the path
  1437. extrusion_entities_append_paths(
  1438. eec->entities, STDMOVE(polylines_2),
  1439. flow.bridge ?
  1440. erBridgeInfill :
  1441. (surface->is_solid() ?
  1442. ((surface->is_top()) ? erTopSolidInfill : erSolidInfill) :
  1443. erInternalInfill),
  1444. flow.mm3_per_mm() * params.flow_mult, flow.width * params.flow_mult, flow.height);
  1445. out.entities.push_back(eecroot);
  1446. }
  1447. } // namespace Slic3r