polyclip.t 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. use Test::More;
  2. use strict;
  3. use warnings;
  4. plan tests => 18;
  5. BEGIN {
  6. use FindBin;
  7. use lib "$FindBin::Bin/../lib";
  8. use local::lib "$FindBin::Bin/../local-lib";
  9. }
  10. use Slic3r;
  11. use Slic3r::Geometry::Clipper qw(intersection_pl);
  12. #==========================================================
  13. is Slic3r::Geometry::point_in_segment(Slic3r::Point->new(10, 10), Slic3r::Line->new([5, 10], [20, 10])), 1, 'point in horizontal segment';
  14. is Slic3r::Geometry::point_in_segment(Slic3r::Point->new(30, 10), Slic3r::Line->new([5, 10], [20, 10])), 0, 'point not in horizontal segment';
  15. is Slic3r::Geometry::point_in_segment(Slic3r::Point->new(10, 10), Slic3r::Line->new([10, 5], [10, 20])), 1, 'point in vertical segment';
  16. is Slic3r::Geometry::point_in_segment(Slic3r::Point->new(10, 30), Slic3r::Line->new([10, 5], [10, 20])), 0, 'point not in vertical segment';
  17. is Slic3r::Geometry::point_in_segment(Slic3r::Point->new(15, 15), Slic3r::Line->new([10, 10], [20, 20])), 1, 'point in diagonal segment';
  18. is Slic3r::Geometry::point_in_segment(Slic3r::Point->new(20, 15), Slic3r::Line->new([10, 10], [20, 20])), 0, 'point not in diagonal segment';
  19. #==========================================================
  20. my $square = Slic3r::Polygon->new( # ccw
  21. [100, 100],
  22. [200, 100],
  23. [200, 200],
  24. [100, 200],
  25. );
  26. #==========================================================
  27. {
  28. my $hole_in_square = [ # cw
  29. [140, 140],
  30. [140, 160],
  31. [160, 160],
  32. [160, 140],
  33. ];
  34. my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
  35. #is $expolygon->contains_point(Slic3r::Point->new(100, 100)), 1, 'corner point is recognized';
  36. #is $expolygon->contains_point(Slic3r::Point->new(100, 180)), 1, 'point on contour is recognized';
  37. #is $expolygon->contains_point(Slic3r::Point->new(140, 150)), 1, 'point on hole contour is recognized';
  38. #is $expolygon->contains_point(Slic3r::Point->new(140, 140)), 1, 'point on hole corner is recognized';
  39. {
  40. my $intersection = intersection_pl([Slic3r::Polyline->new([150,180], [150,150])], \@$expolygon);
  41. is $intersection->[0]->length, Slic3r::Line->new([150, 180], [150, 160])->length,
  42. 'line is clipped to square with hole';
  43. }
  44. {
  45. my $intersection = intersection_pl([Slic3r::Polyline->new([150,150], [150,120])], \@$expolygon);
  46. is $intersection->[0]->length, Slic3r::Line->new([150, 140], [150, 120])->length,
  47. 'line is clipped to square with hole';
  48. }
  49. {
  50. my $intersection = intersection_pl([Slic3r::Polyline->new([120,180], [180,180])], \@$expolygon);
  51. is $intersection->[0]->length, Slic3r::Line->new([120,180], [180,180])->length,
  52. 'line is clipped to square with hole';
  53. }
  54. {
  55. my $intersection = intersection_pl([Slic3r::Polyline->new([50, 150], [300, 150])], \@$expolygon);
  56. is $intersection->[0]->length, Slic3r::Line->new([100, 150], [140, 150])->length,
  57. 'line is clipped to square with hole';
  58. is $intersection->[1]->length, Slic3r::Line->new([160, 150], [200, 150])->length,
  59. 'line is clipped to square with hole';
  60. }
  61. {
  62. my $intersection = intersection_pl([Slic3r::Polyline->new([300, 150], [50, 150])], \@$expolygon);
  63. is $intersection->[0]->length, Slic3r::Line->new([200, 150], [160, 150])->length,
  64. 'reverse line is clipped to square with hole';
  65. is $intersection->[1]->length, Slic3r::Line->new([140, 150], [100, 150])->length,
  66. 'reverse line is clipped to square with hole';
  67. }
  68. {
  69. my $intersection = intersection_pl([Slic3r::Polyline->new([100,180], [200,180])], \@$expolygon);
  70. is $intersection->[0]->length, Slic3r::Line->new([100,180], [200,180])->length,
  71. 'tangent line is clipped to square with hole';
  72. }
  73. }
  74. #==========================================================
  75. {
  76. my $large_circle = Slic3r::Polygon->new_scale( # ccw
  77. [151.8639,288.1192], [133.2778,284.6011], [115.0091,279.6997], [98.2859,270.8606], [82.2734,260.7933],
  78. [68.8974,247.4181], [56.5622,233.0777], [47.7228,216.3558], [40.1617,199.0172], [36.6431,180.4328],
  79. [34.932,165.2312], [37.5567,165.1101], [41.0547,142.9903], [36.9056,141.4295], [40.199,124.1277],
  80. [47.7776,106.7972], [56.6335,90.084], [68.9831,75.7557], [82.3712,62.3948], [98.395,52.3429],
  81. [115.1281,43.5199], [133.4004,38.6374], [151.9884,35.1378], [170.8905,35.8571], [189.6847,37.991],
  82. [207.5349,44.2488], [224.8662,51.8273], [240.0786,63.067], [254.407,75.4169], [265.6311,90.6406],
  83. [275.6832,106.6636], [281.9225,124.52], [286.8064,142.795], [287.5061,161.696], [286.7874,180.5972],
  84. [281.8856,198.8664], [275.6283,216.7169], [265.5604,232.7294], [254.3211,247.942], [239.9802,260.2776],
  85. [224.757,271.5022], [207.4179,279.0635], [189.5605,285.3035], [170.7649,287.4188],
  86. );
  87. ok $large_circle->is_counter_clockwise, "contour is counter-clockwise";
  88. my $small_circle = Slic3r::Polygon->new_scale( # cw
  89. [158.227,215.9007], [164.5136,215.9007], [175.15,214.5007], [184.5576,210.6044], [190.2268,207.8743],
  90. [199.1462,201.0306], [209.0146,188.346], [213.5135,177.4829], [214.6979,168.4866], [216.1025,162.3325],
  91. [214.6463,151.2703], [213.2471,145.1399], [209.0146,134.9203], [199.1462,122.2357], [189.8944,115.1366],
  92. [181.2504,111.5567], [175.5684,108.8205], [164.5136,107.3655], [158.2269,107.3655], [147.5907,108.7656],
  93. [138.183,112.6616], [132.5135,115.3919], [123.5943,122.2357], [113.7259,134.92], [109.2269,145.7834],
  94. [108.0426,154.7799], [106.638,160.9339], [108.0941,171.9957], [109.4933,178.1264], [113.7259,188.3463],
  95. [123.5943,201.0306], [132.8461,208.1296], [141.4901,211.7094], [147.172,214.4458],
  96. );
  97. ok $small_circle->is_clockwise, "hole is clockwise";
  98. my $expolygon = Slic3r::ExPolygon->new($large_circle, $small_circle);
  99. my $line = Slic3r::Polyline->new_scale([152.742,288.086671142818], [152.742,34.166466971035]);
  100. my $intersection = intersection_pl([$line], \@$expolygon);
  101. is $intersection->[0]->length, Slic3r::Line->new([152742000, 288086661], [152742000, 215178843])->length,
  102. 'line is clipped to square with hole';
  103. is $intersection->[1]->length, Slic3r::Line->new([152742000, 108087507], [152742000, 35166477])->length,
  104. 'line is clipped to square with hole';
  105. }
  106. #==========================================================