|
| 1 | +using Test |
| 2 | +import GeometryOps as GO |
| 3 | +import GeoInterface as GI |
| 4 | + |
| 5 | +@testset "ConvexConvexSutherlandHodgman" begin |
| 6 | + @testset "Basic intersection" begin |
| 7 | + # Two overlapping squares - intersection is 1x1 square |
| 8 | + square1 = GI.Polygon([[(0.0, 0.0), (2.0, 0.0), (2.0, 2.0), (0.0, 2.0), (0.0, 0.0)]]) |
| 9 | + square2 = GI.Polygon([[(1.0, 1.0), (3.0, 1.0), (3.0, 3.0), (1.0, 3.0), (1.0, 1.0)]]) |
| 10 | + |
| 11 | + result = GO.intersection(GO.ConvexConvexSutherlandHodgman(), square1, square2) |
| 12 | + @test GI.trait(result) isa GI.PolygonTrait |
| 13 | + @test GO.area(result) ≈ 1.0 atol=1e-10 |
| 14 | + end |
| 15 | + |
| 16 | + @testset "No intersection" begin |
| 17 | + # Disjoint squares |
| 18 | + square1 = GI.Polygon([[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0)]]) |
| 19 | + square2 = GI.Polygon([[(5.0, 5.0), (6.0, 5.0), (6.0, 6.0), (5.0, 6.0), (5.0, 5.0)]]) |
| 20 | + |
| 21 | + result = GO.intersection(GO.ConvexConvexSutherlandHodgman(), square1, square2) |
| 22 | + @test GI.trait(result) isa GI.PolygonTrait |
| 23 | + @test GO.area(result) ≈ 0.0 atol=1e-10 |
| 24 | + end |
| 25 | + |
| 26 | + @testset "One contains other" begin |
| 27 | + # Large square contains small square |
| 28 | + large = GI.Polygon([[(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0), (0.0, 0.0)]]) |
| 29 | + small = GI.Polygon([[(2.0, 2.0), (4.0, 2.0), (4.0, 4.0), (2.0, 4.0), (2.0, 2.0)]]) |
| 30 | + |
| 31 | + result = GO.intersection(GO.ConvexConvexSutherlandHodgman(), large, small) |
| 32 | + @test GI.trait(result) isa GI.PolygonTrait |
| 33 | + @test GO.area(result) ≈ 4.0 atol=1e-10 |
| 34 | + |
| 35 | + # Reverse order should give same result |
| 36 | + result2 = GO.intersection(GO.ConvexConvexSutherlandHodgman(), small, large) |
| 37 | + @test GO.area(result2) ≈ 4.0 atol=1e-10 |
| 38 | + end |
| 39 | + |
| 40 | + @testset "Triangles" begin |
| 41 | + # Two overlapping triangles (both CCW winding) |
| 42 | + tri1 = GI.Polygon([[(0.0, 0.0), (4.0, 0.0), (2.0, 4.0), (0.0, 0.0)]]) |
| 43 | + tri2 = GI.Polygon([[(0.0, 2.0), (2.0, -2.0), (4.0, 2.0), (0.0, 2.0)]]) |
| 44 | + |
| 45 | + result = GO.intersection(GO.ConvexConvexSutherlandHodgman(), tri1, tri2) |
| 46 | + @test GI.trait(result) isa GI.PolygonTrait |
| 47 | + @test GO.area(result) > 0 |
| 48 | + end |
| 49 | + |
| 50 | + @testset "Identical polygons" begin |
| 51 | + # Same polygon should return itself |
| 52 | + square = GI.Polygon([[(0.0, 0.0), (2.0, 0.0), (2.0, 2.0), (0.0, 2.0), (0.0, 0.0)]]) |
| 53 | + |
| 54 | + result = GO.intersection(GO.ConvexConvexSutherlandHodgman(), square, square) |
| 55 | + @test GI.trait(result) isa GI.PolygonTrait |
| 56 | + @test GO.area(result) ≈ 4.0 atol=1e-10 |
| 57 | + end |
| 58 | + |
| 59 | + @testset "Shared edge" begin |
| 60 | + # Two squares sharing an edge |
| 61 | + square1 = GI.Polygon([[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0)]]) |
| 62 | + square2 = GI.Polygon([[(1.0, 0.0), (2.0, 0.0), (2.0, 1.0), (1.0, 1.0), (1.0, 0.0)]]) |
| 63 | + |
| 64 | + result = GO.intersection(GO.ConvexConvexSutherlandHodgman(), square1, square2) |
| 65 | + @test GI.trait(result) isa GI.PolygonTrait |
| 66 | + # Shared edge only - area should be 0 or near 0 |
| 67 | + @test GO.area(result) ≈ 0.0 atol=1e-10 |
| 68 | + end |
| 69 | + |
| 70 | + @testset "Unsupported geometry types" begin |
| 71 | + square = GI.Polygon([[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0)]]) |
| 72 | + point = GI.Point(0.5, 0.5) |
| 73 | + |
| 74 | + @test_throws ArgumentError GO.intersection(GO.ConvexConvexSutherlandHodgman(), square, point) |
| 75 | + @test_throws ArgumentError GO.intersection(GO.ConvexConvexSutherlandHodgman(), point, square) |
| 76 | + end |
| 77 | + |
| 78 | + @testset "Pentagons" begin |
| 79 | + # Helper to create regular polygon (CCW winding) |
| 80 | + function regular_polygon(n, radius, center_x, center_y) |
| 81 | + coords = Tuple{Float64,Float64}[] |
| 82 | + for i in 0:n-1 |
| 83 | + θ = 2π * i / n |
| 84 | + push!(coords, (center_x + radius * cos(θ), center_y + radius * sin(θ))) |
| 85 | + end |
| 86 | + push!(coords, coords[1]) # close the ring |
| 87 | + return GI.Polygon([coords]) |
| 88 | + end |
| 89 | + |
| 90 | + # Two overlapping pentagons |
| 91 | + pent1 = regular_polygon(5, 2.0, 0.0, 0.0) |
| 92 | + pent2 = regular_polygon(5, 2.0, 1.5, 0.0) |
| 93 | + |
| 94 | + result = GO.intersection(GO.ConvexConvexSutherlandHodgman(), pent1, pent2) |
| 95 | + @test GI.trait(result) isa GI.PolygonTrait |
| 96 | + @test GO.area(result) > 0 |
| 97 | + # Intersection should be smaller than either pentagon |
| 98 | + @test GO.area(result) < GO.area(pent1) |
| 99 | + end |
| 100 | + |
| 101 | + @testset "Hexagons" begin |
| 102 | + function regular_polygon(n, radius, center_x, center_y) |
| 103 | + coords = Tuple{Float64,Float64}[] |
| 104 | + for i in 0:n-1 |
| 105 | + θ = 2π * i / n |
| 106 | + push!(coords, (center_x + radius * cos(θ), center_y + radius * sin(θ))) |
| 107 | + end |
| 108 | + push!(coords, coords[1]) |
| 109 | + return GI.Polygon([coords]) |
| 110 | + end |
| 111 | + |
| 112 | + # Two overlapping hexagons |
| 113 | + hex1 = regular_polygon(6, 2.0, 0.0, 0.0) |
| 114 | + hex2 = regular_polygon(6, 2.0, 2.0, 0.0) |
| 115 | + |
| 116 | + result = GO.intersection(GO.ConvexConvexSutherlandHodgman(), hex1, hex2) |
| 117 | + @test GI.trait(result) isa GI.PolygonTrait |
| 118 | + @test GO.area(result) > 0 |
| 119 | + @test GO.area(result) < GO.area(hex1) |
| 120 | + |
| 121 | + # Hexagon containing smaller hexagon |
| 122 | + hex_large = regular_polygon(6, 3.0, 0.0, 0.0) |
| 123 | + hex_small = regular_polygon(6, 1.0, 0.0, 0.0) |
| 124 | + |
| 125 | + result2 = GO.intersection(GO.ConvexConvexSutherlandHodgman(), hex_large, hex_small) |
| 126 | + @test GO.area(result2) ≈ GO.area(hex_small) atol=1e-10 |
| 127 | + end |
| 128 | + |
| 129 | + @testset "Octagons" begin |
| 130 | + function regular_polygon(n, radius, center_x, center_y) |
| 131 | + coords = Tuple{Float64,Float64}[] |
| 132 | + for i in 0:n-1 |
| 133 | + θ = 2π * i / n |
| 134 | + push!(coords, (center_x + radius * cos(θ), center_y + radius * sin(θ))) |
| 135 | + end |
| 136 | + push!(coords, coords[1]) |
| 137 | + return GI.Polygon([coords]) |
| 138 | + end |
| 139 | + |
| 140 | + # Two overlapping octagons |
| 141 | + oct1 = regular_polygon(8, 2.0, 0.0, 0.0) |
| 142 | + oct2 = regular_polygon(8, 2.0, 1.0, 1.0) |
| 143 | + |
| 144 | + result = GO.intersection(GO.ConvexConvexSutherlandHodgman(), oct1, oct2) |
| 145 | + @test GI.trait(result) isa GI.PolygonTrait |
| 146 | + @test GO.area(result) > 0 |
| 147 | + @test GO.area(result) < GO.area(oct1) |
| 148 | + |
| 149 | + # Identical octagons should return same area |
| 150 | + result2 = GO.intersection(GO.ConvexConvexSutherlandHodgman(), oct1, oct1) |
| 151 | + @test GO.area(result2) ≈ GO.area(oct1) atol=1e-10 |
| 152 | + end |
| 153 | + |
| 154 | + @testset "Bordering rectangles with offset" begin |
| 155 | + # Two rectangles sharing an edge but offset vertically |
| 156 | + # rect1: [0,2] x [0,2], rect2: [2,4] x [0.5,2.5] |
| 157 | + # They share the edge at x=2 but are offset by 0.5 in y |
| 158 | + # Intersection should be zero area (just a line segment) |
| 159 | + rect1 = GI.Polygon([[(0.0, 0.0), (2.0, 0.0), (2.0, 2.0), (0.0, 2.0), (0.0, 0.0)]]) |
| 160 | + rect2 = GI.Polygon([[(2.0, 0.5), (4.0, 0.5), (4.0, 2.5), (2.0, 2.5), (2.0, 0.5)]]) |
| 161 | + |
| 162 | + result = GO.intersection(GO.ConvexConvexSutherlandHodgman(), rect1, rect2) |
| 163 | + @test GI.trait(result) isa GI.PolygonTrait |
| 164 | + @test GO.area(result) ≈ 0.0 atol=1e-10 |
| 165 | + |
| 166 | + # Two rectangles sharing an edge but offset horizontally |
| 167 | + # rect3: [0,2] x [0,2], rect4: [0.5,2.5] x [2,4] |
| 168 | + rect3 = GI.Polygon([[(0.0, 0.0), (2.0, 0.0), (2.0, 2.0), (0.0, 2.0), (0.0, 0.0)]]) |
| 169 | + rect4 = GI.Polygon([[(0.5, 2.0), (2.5, 2.0), (2.5, 4.0), (0.5, 4.0), (0.5, 2.0)]]) |
| 170 | + |
| 171 | + result2 = GO.intersection(GO.ConvexConvexSutherlandHodgman(), rect3, rect4) |
| 172 | + @test GI.trait(result2) isa GI.PolygonTrait |
| 173 | + @test GO.area(result2) ≈ 0.0 atol=1e-10 |
| 174 | + |
| 175 | + # Rectangles that just touch at a corner |
| 176 | + rect5 = GI.Polygon([[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0)]]) |
| 177 | + rect6 = GI.Polygon([[(1.0, 1.0), (2.0, 1.0), (2.0, 2.0), (1.0, 2.0), (1.0, 1.0)]]) |
| 178 | + |
| 179 | + result3 = GO.intersection(GO.ConvexConvexSutherlandHodgman(), rect5, rect6) |
| 180 | + @test GI.trait(result3) isa GI.PolygonTrait |
| 181 | + @test GO.area(result3) ≈ 0.0 atol=1e-10 |
| 182 | + end |
| 183 | +end |
0 commit comments