08_extrusionloop.t 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use List::Util qw(sum);
  5. use Slic3r::XS;
  6. use Test::More tests => 47;
  7. {
  8. my $square = [
  9. [100, 100],
  10. [200, 100],
  11. [200, 200],
  12. [100, 200],
  13. ];
  14. my $square_p = Slic3r::Polygon->new(@$square);
  15. my $loop = Slic3r::ExtrusionLoop->new;
  16. $loop->append(Slic3r::ExtrusionPath->new(
  17. polyline => $square_p->split_at_first_point,
  18. role => Slic3r::ExtrusionPath::EXTR_ROLE_EXTERNAL_PERIMETER,
  19. mm3_per_mm => 1,
  20. ));
  21. isa_ok $loop, 'Slic3r::ExtrusionLoop';
  22. isa_ok $loop->polygon, 'Slic3r::Polygon', 'loop polygon';
  23. is $loop->polygon->area, $square_p->area, 'polygon area';
  24. is $loop->length, $square_p->length(), 'loop length';
  25. $loop = $loop->clone;
  26. is scalar(@$loop), 1, 'loop contains one path';
  27. {
  28. my $path = $loop->[0];
  29. isa_ok $path, 'Slic3r::ExtrusionPath::Ref';
  30. is $path->role, Slic3r::ExtrusionPath::EXTR_ROLE_EXTERNAL_PERIMETER, 'role';
  31. }
  32. $loop->split_at_vertex($square_p->[2]);
  33. is scalar(@$loop), 1, 'splitting a single-path loop results in a single path';
  34. is scalar(@{$loop->[0]->polyline}), 5, 'path has correct number of points';
  35. ok $loop->[0]->polyline->[0]->coincides_with($square_p->[2]), 'expected point order';
  36. ok $loop->[0]->polyline->[1]->coincides_with($square_p->[3]), 'expected point order';
  37. ok $loop->[0]->polyline->[2]->coincides_with($square_p->[0]), 'expected point order';
  38. ok $loop->[0]->polyline->[3]->coincides_with($square_p->[1]), 'expected point order';
  39. ok $loop->[0]->polyline->[4]->coincides_with($square_p->[2]), 'expected point order';
  40. }
  41. {
  42. my $polyline1 = Slic3r::Polyline->new([100,100], [200,100], [200,200]);
  43. my $polyline2 = Slic3r::Polyline->new([200,200], [100,200], [100,100]);
  44. my $loop = Slic3r::ExtrusionLoop->new_from_paths(
  45. Slic3r::ExtrusionPath->new(
  46. polyline => $polyline1,
  47. role => Slic3r::ExtrusionPath::EXTR_ROLE_EXTERNAL_PERIMETER,
  48. mm3_per_mm => 1,
  49. ),
  50. Slic3r::ExtrusionPath->new(
  51. polyline => $polyline2,
  52. role => Slic3r::ExtrusionPath::EXTR_ROLE_OVERHANG_PERIMETER,
  53. mm3_per_mm => 1,
  54. ),
  55. );
  56. my $tot_len = sum($polyline1->length, $polyline2->length);
  57. is $loop->length, $tot_len, 'length';
  58. is scalar(@$loop), 2, 'loop contains two paths';
  59. {
  60. # check splitting at intermediate point
  61. my $loop2 = $loop->clone;
  62. isa_ok $loop2, 'Slic3r::ExtrusionLoop';
  63. $loop2->split_at_vertex($polyline1->[1]);
  64. is $loop2->length, $tot_len, 'length after splitting is unchanged';
  65. is scalar(@$loop2), 3, 'loop contains three paths after splitting';
  66. ok $loop2->[0]->polyline->[0]->coincides_with($polyline1->[1]), 'expected starting point';
  67. ok $loop2->[-1]->polyline->[-1]->coincides_with($polyline1->[1]), 'expected ending point';
  68. ok $loop2->[0]->polyline->[-1]->coincides_with($loop2->[1]->polyline->[0]), 'paths have common point';
  69. ok $loop2->[1]->polyline->[-1]->coincides_with($loop2->[2]->polyline->[0]), 'paths have common point';
  70. is $loop2->[0]->role, Slic3r::ExtrusionPath::EXTR_ROLE_EXTERNAL_PERIMETER, 'expected order after splitting';
  71. is $loop2->[1]->role, Slic3r::ExtrusionPath::EXTR_ROLE_OVERHANG_PERIMETER, 'expected order after splitting';
  72. is $loop2->[2]->role, Slic3r::ExtrusionPath::EXTR_ROLE_EXTERNAL_PERIMETER, 'expected order after splitting';
  73. is scalar(@{$loop2->[0]->polyline}), 2, 'path has correct number of points';
  74. is scalar(@{$loop2->[1]->polyline}), 3, 'path has correct number of points';
  75. is scalar(@{$loop2->[2]->polyline}), 2, 'path has correct number of points';
  76. my @paths = @{$loop2->clip_end(3)};
  77. is sum(map $_->length, @paths), $loop2->length - 3, 'returned paths have expected length';
  78. }
  79. {
  80. # check splitting at endpoint
  81. my $loop2 = $loop->clone;
  82. $loop2->split_at_vertex($polyline2->[0]);
  83. is $loop2->length, $tot_len, 'length after splitting is unchanged';
  84. is scalar(@$loop2), 2, 'loop contains two paths after splitting';
  85. ok $loop2->[0]->polyline->[0]->coincides_with($polyline2->[0]), 'expected starting point';
  86. ok $loop2->[-1]->polyline->[-1]->coincides_with($polyline2->[0]), 'expected ending point';
  87. ok $loop2->[0]->polyline->[-1]->coincides_with($loop2->[1]->polyline->[0]), 'paths have common point';
  88. ok $loop2->[1]->polyline->[-1]->coincides_with($loop2->[0]->polyline->[0]), 'paths have common point';
  89. is $loop2->[0]->role, Slic3r::ExtrusionPath::EXTR_ROLE_OVERHANG_PERIMETER, 'expected order after splitting';
  90. is $loop2->[1]->role, Slic3r::ExtrusionPath::EXTR_ROLE_EXTERNAL_PERIMETER, 'expected order after splitting';
  91. is scalar(@{$loop2->[0]->polyline}), 3, 'path has correct number of points';
  92. is scalar(@{$loop2->[1]->polyline}), 3, 'path has correct number of points';
  93. }
  94. {
  95. my $loop2 = $loop->clone;
  96. my $point = Slic3r::Point->new(250,150);
  97. $loop2->split_at($point);
  98. is $loop2->length, $tot_len, 'length after splitting is unchanged';
  99. is scalar(@$loop2), 3, 'loop contains three paths after splitting';
  100. my $expected_start_point = Slic3r::Point->new(200,150);
  101. ok $loop2->[0]->polyline->[0]->coincides_with($expected_start_point), 'expected starting point';
  102. ok $loop2->[-1]->polyline->[-1]->coincides_with($expected_start_point), 'expected ending point';
  103. }
  104. }
  105. {
  106. my @polylines = (
  107. Slic3r::Polyline->new([59312736,4821067],[64321068,4821067],[64321068,4821067],[64321068,9321068],[59312736,9321068]),
  108. Slic3r::Polyline->new([59312736,9321068],[9829401,9321068]),
  109. Slic3r::Polyline->new([9829401,9321068],[4821067,9321068],[4821067,4821067],[9829401,4821067]),
  110. Slic3r::Polyline->new([9829401,4821067],[59312736,4821067]),
  111. );
  112. my $loop = Slic3r::ExtrusionLoop->new;
  113. $loop->append($_) for (
  114. Slic3r::ExtrusionPath->new(polyline => $polylines[0], role => Slic3r::ExtrusionPath::EXTR_ROLE_EXTERNAL_PERIMETER, mm3_per_mm => 1),
  115. Slic3r::ExtrusionPath->new(polyline => $polylines[1], role => Slic3r::ExtrusionPath::EXTR_ROLE_OVERHANG_PERIMETER, mm3_per_mm => 1),
  116. Slic3r::ExtrusionPath->new(polyline => $polylines[2], role => Slic3r::ExtrusionPath::EXTR_ROLE_EXTERNAL_PERIMETER, mm3_per_mm => 1),
  117. Slic3r::ExtrusionPath->new(polyline => $polylines[3], role => Slic3r::ExtrusionPath::EXTR_ROLE_OVERHANG_PERIMETER, mm3_per_mm => 1),
  118. );
  119. my $len = $loop->length;
  120. my $point = Slic3r::Point->new(4821067,9321068);
  121. $loop->split_at_vertex($point) or $loop->split_at($point);
  122. is $loop->length, $len, 'total length is preserved after splitting';
  123. is_deeply [ map $_->role, @$loop ], [
  124. Slic3r::ExtrusionPath::EXTR_ROLE_EXTERNAL_PERIMETER,
  125. Slic3r::ExtrusionPath::EXTR_ROLE_OVERHANG_PERIMETER,
  126. Slic3r::ExtrusionPath::EXTR_ROLE_EXTERNAL_PERIMETER,
  127. Slic3r::ExtrusionPath::EXTR_ROLE_OVERHANG_PERIMETER,
  128. Slic3r::ExtrusionPath::EXTR_ROLE_EXTERNAL_PERIMETER,
  129. ], 'order is correctly preserved after splitting';
  130. }
  131. {
  132. my $loop = Slic3r::ExtrusionLoop->new;
  133. $loop->append(Slic3r::ExtrusionPath->new(
  134. polyline => Slic3r::Polyline->new([15896783,15868739],[24842049,12117558],[33853238,15801279],[37591780,24780128],[37591780,24844970],[33853231,33825297],[24842049,37509013],[15896798,33757841],[12211841,24812544],[15896783,15868739]),
  135. role => Slic3r::ExtrusionPath::EXTR_ROLE_EXTERNAL_PERIMETER, mm3_per_mm => 1
  136. ));
  137. my $len = $loop->length;
  138. $loop->split_at(Slic3r::Point->new(15896783,15868739));
  139. is $loop->length, $len, 'split_at() preserves total length';
  140. }
  141. __END__