66#include " shape/trapezoidation.hpp"
77#include " shape/supports.hpp"
88#include " shape/element_intersections.hpp"
9+ #include " shape/self_intersections_removal.hpp"
910
1011#include < iostream>
1112
@@ -157,9 +158,13 @@ BranchingScheme::BranchingScheme(
157158 border_pos < (DefectId)simplified_bin_type.borders .size ();
158159 ++border_pos) {
159160 const Shape& simplified_inflated_shape = simplified_bin_type.borders [border_pos].shape_inflated ;
160- Shape shape_inflated = convert_shape (simplified_inflated_shape, direction);
161161
162+ Shape shape_inflated = convert_shape (simplified_inflated_shape, direction);
163+ std::vector<Shape> holes_deflated;
162164 shape_inflated = shape::clean_extreme_slopes (shape_inflated, true );
165+ auto res = shape::remove_self_intersections (shape_inflated);
166+ shape_inflated = res.first ;
167+ holes_deflated = res.second ;
163168
164169 // Supports.
165170 shape::ShapeSupports supports = shape::compute_shape_supports (shape_inflated, false );
@@ -174,7 +179,9 @@ BranchingScheme::BranchingScheme(
174179 }
175180
176181 // Trapezoidation.
177- auto trapezoids = trapezoidation (shape_inflated);
182+ auto trapezoids = trapezoidation (
183+ shape_inflated,
184+ holes_deflated);
178185 for (const GeneralizedTrapezoid& trapezoid: trapezoids) {
179186 UncoveredTrapezoid defect (
180187 -1 ,
@@ -192,6 +199,23 @@ BranchingScheme::BranchingScheme(
192199 Shape shape_inflated = convert_shape (simplified_inflated_shape, direction);
193200
194201 shape_inflated = shape::clean_extreme_slopes (shape_inflated, true );
202+ std::vector<Shape> holes_deflated;
203+ auto res = shape::remove_self_intersections (shape_inflated);
204+ shape_inflated = res.first ;
205+ holes_deflated = res.second ;
206+ for (ShapePos hole_pos = 0 ;
207+ hole_pos < (ShapePos)simplified_bin_type.defects [defect_id].holes_deflated .size ();
208+ ++hole_pos) {
209+ const Shape& simplified_deflated_shape = simplified_bin_type.defects [defect_id].holes_deflated [hole_pos];
210+
211+ Shape shape_deflated = convert_shape (simplified_deflated_shape, direction);
212+
213+ shape_deflated = shape::clean_extreme_slopes (shape_deflated, false );
214+
215+ auto res = shape::extract_all_holes_from_self_intersecting_hole (shape_deflated);
216+ for (const auto & hole: res)
217+ holes_deflated.push_back (hole);
218+ }
195219
196220 // Supports.
197221 shape::ShapeSupports supports = shape::compute_shape_supports (shape_inflated, false );
@@ -205,17 +229,10 @@ BranchingScheme::BranchingScheme(
205229 bb_bin_type.supporting_parts .push_back (supporting_part_pos);
206230 }
207231
208- std::vector<Shape> holes_deflated;
209232 for (ShapePos hole_pos = 0 ;
210- hole_pos < (ShapePos)simplified_bin_type. defects [defect_id]. holes_deflated .size ();
233+ hole_pos < (ShapePos)holes_deflated.size ();
211234 ++hole_pos) {
212- const Shape& simplified_deflated_shape = simplified_bin_type.defects [defect_id].holes_deflated [hole_pos];
213- Shape shape_deflated = convert_shape (simplified_deflated_shape, direction);
214-
215- shape_deflated = shape::clean_extreme_slopes (shape_deflated, false );
216-
217- // Update trapezoidation input.
218- holes_deflated.push_back (shape_deflated);
235+ const Shape& shape_deflated = holes_deflated[hole_pos];
219236
220237 // Supports.
221238 shape::ShapeSupports supports = shape::compute_shape_supports (shape_deflated, true );
@@ -275,56 +292,34 @@ BranchingScheme::BranchingScheme(
275292 ++item_shape_pos) {
276293 const ItemShape& item_shape = item_type.shapes [item_shape_pos];
277294 const Shape& simplified_shape = simplified_item_type.shapes [item_shape_pos].shape ;
278- const Shape& simplified_inflated_shape = simplified_item_type.shapes [item_shape_pos].shape_inflated ;
279- // std::cout << "item_type_id " << item_type_id
280- // << " item_shape_pos " << item_shape_pos
281- // << " angle " << angle_range.first
282- // << " mirror " << mirror
283- // << " simplified_shape " << simplified_shape.to_string(2)
284- // << std::endl;
285-
286295 if (write_shapes) {
287296 simplified_shape.write_svg (
288297 " item_type_" + std::to_string (item_type_id)
289298 + " _shape_pos_" + std::to_string (item_shape_pos)
290299 + " _simplified.svg" );
291- simplified_inflated_shape.write_svg (
292- " item_type_" + std::to_string (item_type_id)
293- + " _shape_pos_" + std::to_string (item_shape_pos)
294- + " _inflated_simplified.svg" );
295300 }
296301
297302 Shape shape = convert_shape (simplified_shape, angle_range.first , mirror, direction);
298- Shape shape_inflated = convert_shape (simplified_inflated_shape, angle_range.first , mirror, direction);
299-
303+ std::vector<Shape> holes;
300304 shape = shape::clean_extreme_slopes (shape, true );
301- shape_inflated = shape::clean_extreme_slopes (shape_inflated, true );
305+ auto res = shape::remove_self_intersections (shape);
306+ shape = res.first ;
307+ holes = res.second ;
308+ for (ShapePos hole_pos = 0 ;
309+ hole_pos < (ShapePos)simplified_item_type.shapes [item_shape_pos].holes .size ();
310+ ++hole_pos) {
311+ const Shape& simplified_shape = simplified_item_type.shapes [item_shape_pos].holes [hole_pos];
302312
303- if (write_shapes) {
304- simplified_inflated_shape.write_svg (
305- " item_type_" + std::to_string (item_type_id)
306- + " _shape_pos_" + std::to_string (item_shape_pos)
307- + " _inflated_simplified_cleaned.svg" );
313+ Shape shape = convert_shape (simplified_shape, angle_range.first , mirror, direction);
314+
315+ shape = shape::clean_extreme_slopes (shape, false );
316+
317+ auto res = shape::extract_all_holes_from_self_intersecting_hole (shape);
318+ for (const auto & hole: res)
319+ holes.push_back (hole);
308320 }
309321
310322 // Supports.
311- // Supporting parts are computed on inflated shapes.
312- // Supported parts are computed on (original) non-inflated shapes.
313- std::vector<Shape> supporting_parts
314- = shape::compute_shape_supports (shape_inflated, false ).supporting_parts ;
315- for (const Shape& supporting_part: supporting_parts) {
316- ShapePos supporting_part_pos = direction_data.supporting_parts .size ();
317- Support support;
318- support.trapezoid_set_id = trapezoid_set_id;
319- support.item_shape_pos = item_shape_pos;
320- support.shape = supporting_part;
321- // std::cout << "trapezoid_set_id " << trapezoid_set_id
322- // << " item_shape_pos " << item_shape_pos
323- // << std::endl;
324- // std::cout << support.shape.to_string(2) << std::endl;
325- direction_data.supporting_parts .push_back (support);
326- trapezoid_set.supporting_parts .push_back (supporting_part_pos);
327- }
328323 std::vector<Shape> supported_parts
329324 = shape::compute_shape_supports (shape, false ).supported_parts ;
330325 for (const Shape& supported_part: supported_parts) {
@@ -337,18 +332,10 @@ BranchingScheme::BranchingScheme(
337332 trapezoid_set.supported_parts .push_back (supported_part_pos);
338333 }
339334
340- std::vector<Shape> holes;
341335 for (ShapePos hole_pos = 0 ;
342- hole_pos < (ShapePos)simplified_item_type. shapes [item_shape_pos]. holes .size ();
336+ hole_pos < (ShapePos)holes.size ();
343337 ++hole_pos) {
344- const Shape& simplified_shape = simplified_item_type.shapes [item_shape_pos].holes [hole_pos];
345-
346- Shape shape = convert_shape (simplified_shape, angle_range.first , mirror, direction);
347-
348- shape = shape::clean_extreme_slopes (shape, false );
349-
350- // Update trapezoidation input.
351- holes.push_back (shape);
338+ const Shape& shape = holes[hole_pos];
352339
353340 // Supports.
354341 std::vector<Shape> supported_parts
@@ -365,7 +352,53 @@ BranchingScheme::BranchingScheme(
365352 }
366353 }
367354
355+ // Write rotated item shape.
356+ if (write_shapes) {
357+ std::string name = " item_type_" + std::to_string (item_type_id)
358+ + " _x"
359+ + " _" + std::to_string (item_shape_pos)
360+ + " _mirror_" + std::to_string (mirror)
361+ + " _rotated_" + std::to_string (angle_range.first )
362+ + " .svg" ;
363+ irregular::write_svg (
364+ shape,
365+ holes,
366+ name);
367+ }
368+
369+ // Trapezoidation.
370+ auto trapezoids = trapezoidation (
371+ shape,
372+ holes);
373+ trapezoid_set.shapes .push_back ({});
374+ for (const GeneralizedTrapezoid& trapezoid: trapezoids)
375+ trapezoid_set.shapes .back ().push_back (trapezoid);
376+ }
377+
378+ for (ItemShapePos item_shape_pos = 0 ;
379+ item_shape_pos < (ItemShapePos)item_type.shapes .size ();
380+ ++item_shape_pos) {
381+ const Shape& simplified_inflated_shape = simplified_item_type.shapes [item_shape_pos].shape_inflated ;
382+ // std::cout << "item_type_id " << item_type_id
383+ // << " item_shape_pos " << item_shape_pos
384+ // << " angle " << angle_range.first
385+ // << " mirror " << mirror
386+ // << " simplified_shape " << simplified_shape.to_string(2)
387+ // << std::endl;
388+
389+ if (write_shapes) {
390+ simplified_inflated_shape.write_svg (
391+ " item_type_" + std::to_string (item_type_id)
392+ + " _shape_pos_" + std::to_string (item_shape_pos)
393+ + " _inflated_simplified.svg" );
394+ }
395+
396+ Shape shape_inflated = convert_shape (simplified_inflated_shape, angle_range.first , mirror, direction);
368397 std::vector<Shape> holes_deflated;
398+ shape_inflated = shape::clean_extreme_slopes (shape_inflated, true );
399+ auto res = shape::remove_self_intersections (shape_inflated);
400+ shape_inflated = res.first ;
401+ holes_deflated = res.second ;
369402 for (ShapePos hole_pos = 0 ;
370403 hole_pos < (ShapePos)simplified_item_type.shapes [item_shape_pos].holes_deflated .size ();
371404 ++hole_pos) {
@@ -375,8 +408,41 @@ BranchingScheme::BranchingScheme(
375408
376409 shape_deflated = shape::clean_extreme_slopes (shape_deflated, false );
377410
378- // Update trapezoidation input.
379- holes_deflated.push_back (shape_deflated);
411+ auto res = shape::extract_all_holes_from_self_intersecting_hole (shape_deflated);
412+ for (const auto & hole: res)
413+ holes_deflated.push_back (hole);
414+ }
415+
416+ if (write_shapes) {
417+ simplified_inflated_shape.write_svg (
418+ " item_type_" + std::to_string (item_type_id)
419+ + " _shape_pos_" + std::to_string (item_shape_pos)
420+ + " _inflated_simplified_cleaned.svg" );
421+ }
422+
423+ // Supports.
424+ // Supporting parts are computed on inflated shapes.
425+ // Supported parts are computed on (original) non-inflated shapes.
426+ std::vector<Shape> supporting_parts
427+ = shape::compute_shape_supports (shape_inflated, false ).supporting_parts ;
428+ for (const Shape& supporting_part: supporting_parts) {
429+ ShapePos supporting_part_pos = direction_data.supporting_parts .size ();
430+ Support support;
431+ support.trapezoid_set_id = trapezoid_set_id;
432+ support.item_shape_pos = item_shape_pos;
433+ support.shape = supporting_part;
434+ // std::cout << "trapezoid_set_id " << trapezoid_set_id
435+ // << " item_shape_pos " << item_shape_pos
436+ // << std::endl;
437+ // std::cout << support.shape.to_string(2) << std::endl;
438+ direction_data.supporting_parts .push_back (support);
439+ trapezoid_set.supporting_parts .push_back (supporting_part_pos);
440+ }
441+
442+ for (ShapePos hole_pos = 0 ;
443+ hole_pos < (ShapePos)holes_deflated.size ();
444+ ++hole_pos) {
445+ const Shape& shape_deflated = holes_deflated[hole_pos];
380446
381447 // Supports.
382448 std::vector<Shape> supporting_parts
@@ -393,28 +459,7 @@ BranchingScheme::BranchingScheme(
393459 }
394460 }
395461
396- // Write rotated item shape.
397- if (write_shapes) {
398- std::string name = " item_type_" + std::to_string (item_type_id)
399- + " _x"
400- + " _" + std::to_string (item_shape_pos)
401- + " _mirror_" + std::to_string (mirror)
402- + " _rotated_" + std::to_string (angle_range.first )
403- + " .svg" ;
404- irregular::write_svg (
405- shape,
406- holes,
407- name);
408- }
409-
410462 // Trapezoidation.
411- auto trapezoids = trapezoidation (
412- shape,
413- holes);
414- trapezoid_set.shapes .push_back ({});
415- for (const GeneralizedTrapezoid& trapezoid: trapezoids)
416- trapezoid_set.shapes .back ().push_back (trapezoid);
417-
418463 auto trapezoids_inflated = trapezoidation (
419464 shape_inflated,
420465 holes_deflated);
0 commit comments