@@ -29,28 +29,27 @@ abstract type Exiting <: VertexType end
2929# of the clipped rings.
3030mutable struct RingVertex{VT<: VertexType ,M<: Manifold ,C<: CRS }
3131 point:: Point{M,C}
32- left:: RingVertex{VT ,M,C} where {VT <: VertexType }
33- right:: RingVertex{VT ,M,C} where {VT <: VertexType }
32+ left:: RingVertex{<:VertexType ,M,C}
33+ right:: RingVertex{<:VertexType ,M,C}
3434
35- function RingVertex {VT,M,C} (point:: Point{M,C} ) where {VT<: VertexType ,M<: Manifold ,C<: CRS }
35+ function RingVertex {VT,M,C} (point) where {VT<: VertexType ,M<: Manifold ,C<: CRS }
3636 v = new (point)
3737 v. left = v
3838 v. right = v
39- return v
39+ v
4040 end
4141end
4242
43- RingVertex {VT} (point:: Point{M,C} ) where {VT<: VertexType ,M<: Manifold ,C<: CRS } =
44- RingVertex {VT,manifold(point),typeof(point.coords)} (point)
43+ RingVertex {VT} (point:: Point{M,C} ) where {VT<: VertexType ,M<: Manifold ,C<: CRS } = RingVertex {VT,M,C} (point)
4544
4645isnormal (:: RingVertex{Normal} ) = true
4746isnormal (:: RingVertex ) = false
4847
49- function appendvertices! (v1 :: RingVertex{Normal} , v2 :: RingVertex{Normal} )
50- v2 . left = v1 . left
51- v1 . left = v2
52- v2 . right = v1 . right
53- v1 . right = v2
48+ function appendvertices! (v₁ :: RingVertex{Normal} , v₂ :: RingVertex{Normal} )
49+ v₂ . left = v₁ . left
50+ v₁ . left = v₂
51+ v₂ . right = v₁ . right
52+ v₁ . right = v₂
5453end
5554
5655function clip (poly:: Polygon , ring:: Ring , :: WeilerAthertonClipping )
@@ -143,7 +142,7 @@ function clip(poly::Polygon, ring::Ring, ::WeilerAthertonClipping)
143142
144143 if point ≈ clipped. left. point
145144 clippingprev = Segment (clipping. left. point, point)
146- if measure (clippingprev) ≈ 0.0 * u " m"
145+ if measure (clippingprev) ≈ 0.0 u " m"
147146 clippingprev = Segment (clipping. point, point)
148147 end
149148
@@ -163,7 +162,7 @@ function clip(poly::Polygon, ring::Ring, ::WeilerAthertonClipping)
163162
164163 if point ≈ clipping. left. point
165164 clippedprev = Segment (clipped. left. point, point)
166- if measure (clippedprev) ≈ 0.0 * u " m"
165+ if measure (clippedprev) ≈ 0.0 u " m"
167166 clippedprev = Segment (clipped. point, point)
168167 end
169168
@@ -200,26 +199,26 @@ function clip(poly::Polygon, ring::Ring, ::WeilerAthertonClipping)
200199
201200 # When no interesction have been registered with any of the rings, the clipping ring either
202201 # encircles everything or is completely contained in the clipped polygon.
203- if all ( isequal ( false ), intersected)
202+ if ! any ( intersected)
204203 o = orientation (ring)
205- contained = [ v ∈ PolyArea (ring) for v in vertices (polyrings[1 ])]
206- if (o == CCW && all (contained) )
204+ allcontained = all ( v ∈ PolyArea (ring) for v in vertices (polyrings[1 ]))
205+ if (o == CCW && allcontained )
207206 return poly
208207 end
209- if (o == CW && ! all (contained) )
208+ if (o == CW && ! allcontained )
210209 push! (collectedrings, polyrings[1 ])
211210 intersected[1 ] = true
212211 for polyring in polyrings[2 : end ]
213- if ! all ([ v ∈ PolyArea (ring) for v in vertices (polyring)] )
212+ if ! all (v ∈ PolyArea (ring) for v in vertices (polyring))
214213 push! (collectedrings, polyring)
215214 end
216215 end
217- if any ([ v ∈ PolyArea (polyrings[1 ]) for v in vertices (ring)] )
216+ if any (v ∈ PolyArea (polyrings[1 ]) for v in vertices (ring))
218217 push! (collectedrings, ring)
219218 end
220219 return PolyArea (collectedrings... )
221220 end
222- if all ([ v ∈ PolyArea (polyrings[1 ]) for v in vertices (ring)] )
221+ if all (v ∈ PolyArea (polyrings[1 ]) for v in vertices (ring))
223222 push! (collectedrings, ring)
224223 end
225224 end
@@ -231,9 +230,9 @@ function clip(poly::Polygon, ring::Ring, ::WeilerAthertonClipping)
231230 valid = true
232231 for k in eachindex (intersected)
233232 if ! intersected[k]
233+ # Check if majority of vertices are inside the clipping ring.
234+ ins = count (v ∈ PolyArea (ring) for v in vertices (polyrings[k]))
234235 if orientation (ring) == CCW
235- # Check if majority of vertices are inside the clipping ring.
236- ins = count (isequal (true ), [v ∈ PolyArea (ring) for v in vertices (polyrings[k])])
237236 if ins > (length (vertices (polyrings[k])) - ins)
238237 if orientation (polyrings[k]) == CCW
239238 pushfirst! (newpolyrings, polyrings[k])
@@ -246,8 +245,6 @@ function clip(poly::Polygon, ring::Ring, ::WeilerAthertonClipping)
246245 valid = false
247246 end
248247 else
249- # Check if majority of vertices are outside the clipping ring.
250- ins = count (isequal (true ), [v ∈ PolyArea (ring) for v in vertices (polyrings[k])])
251248 if ins < (length (vertices (polyrings[k])) - ins)
252249 if orientation (polyrings[k]) == CCW
253250 pushfirst! (newpolyrings, polyrings[k])
@@ -265,29 +262,24 @@ function clip(poly::Polygon, ring::Ring, ::WeilerAthertonClipping)
265262 end
266263
267264 n = length (polys)
268- out = n == 0 ? nothing : (n == 1 ? polys[1 ] : GeometrySet (polys))
269- return out
265+ n == 0 ? nothing : (n == 1 ? polys[1 ] : GeometrySet (polys))
270266end
271267
272- function clip (poly:: Polygon , other:: Geometry , method:: WeilerAthertonClipping )
273- return _clip (poly, boundary (other), method)
274- end
268+ clip (poly:: Polygon , other:: Geometry , method:: WeilerAthertonClipping ) = clip (poly, boundary (other), method)
275269
276- function _clip (poly, multi:: Multi , method:: WeilerAthertonClipping )
270+ function clip (poly:: Polygon , multi:: Multi , method:: WeilerAthertonClipping )
277271 for r in parent (multi)
278272 poly = clip (poly, r, method)
279273 if isnothing (poly)
280274 return nothing
281275 end
282276 end
283- return poly
277+ poly
284278end
285279
286- _clip (poly, other, method) = clip (poly, other, method:: WeilerAthertonClipping )
287-
288280function clip (dom:: Domain , other:: Geometry , method:: WeilerAthertonClipping )
289281 clipped = filter (! isnothing, [clip (geom, other, method) for geom in dom])
290- return isempty (clipped) ? nothing : (length (clipped) == 1 ? clipped[1 ] : GeometrySet (clipped))
282+ isempty (clipped) ? nothing : (length (clipped) == 1 ? clipped[1 ] : GeometrySet (clipped))
291283end
292284
293285# Inserts the intersection in the ring.
@@ -330,14 +322,14 @@ nextvertex(v::RingVertex{Exiting}) = v.left
330322
331323# Takes a list of entering vertices and returns all rings that contain those vertices.
332324function collectclipped (entering:: Vector{RingVertex{Entering}} )
333- rings:: Vector{Ring} = []
334- visited:: Vector{RingVertex} = []
325+ rings = Ring []
326+ visited = RingVertex []
335327 for i in eachindex (entering)
336328 if entering[i] in visited
337329 continue
338330 end
339331
340- ring:: Vector{RingVertex} = []
332+ ring = RingVertex []
341333 vertex = entering[i]
342334 while ! (vertex in ring)
343335 if vertex in visited
@@ -350,7 +342,7 @@ function collectclipped(entering::Vector{RingVertex{Entering}})
350342 end
351343
352344 # Remove duplicates.
353- newring:: Vector{RingVertex} = [ring[1 ]]
345+ newring = RingVertex [ring[1 ]]
354346 for i in 2 : length (ring)
355347 if ! (ring[i]. point ≈ newring[end ]. point)
356348 push! (newring, ring[i])
@@ -369,52 +361,59 @@ function collectclipped(entering::Vector{RingVertex{Entering}})
369361 end
370362
371363 if length (ring) > 2
372- push! (rings, Ring ([r. point for r in ring]. .. ))
364+ push! (rings, Ring ([r. point for r in ring]))
373365 end
374366 end
375- return rings
367+ rings
376368end
377369
378370# Used to figure out the type of the vertex to add when intersection is other than the crossing
379371# type. For input it takes four segments which all overlap on a single central vertex.
380372# Function measures the angles formed between the segments and checks wether the two clipped segments
381373# start and end in the same region or different regions, as separated by the clipping segments.
382374function decidedirection (clipped₁, clipped₂, clipping₁, clipping₂)
375+ T = numtype (lentype (clipped₁))
376+
383377 # The input segments should all share one common vertex.
384378 # Form vectors from the common vertex to other vertices.
385379 a = minimum (clipped₁) - maximum (clipped₁)
386380 b = maximum (clipped₂) - minimum (clipped₂)
387381 c = minimum (clipping₁) - maximum (clipping₁)
388382 d = maximum (clipping₂) - minimum (clipping₂)
389383
390- if any (norm .([ a, b, c, d]) .≈ 0.0 * u " m " )
384+ if any (norm (v) ≈ 0.0 u " m " for v in ( a, b, c, d) )
391385 # An zero length input segment found, no need to calculate.
392386 return nothing
393387 end
394388
395- β = mod (∠ (a, b), 2pi * u " rad" )
396- γ = mod (∠ (a, c), 2pi * u " rad" )
397- δ = mod (∠ (a, d), 2pi * u " rad" )
389+ tol = atol (0.0 ) * u " rad"
390+ twoπ = 2 * T (π) * u " rad"
398391
399- if isapprox (γ, 0.0 , atol= atol (0.0 )) || isapprox (γ, 2pi , atol= atol (0.0 ))
400- if δ < β < 2pi
392+ β = mod (∠ (a, b), twoπ)
393+ γ = mod (∠ (a, c), twoπ)
394+ δ = mod (∠ (a, d), twoπ)
395+
396+ if isapprox (γ, zero (γ), atol= tol) || isapprox (γ, twoπ, atol= tol)
397+ if δ < β < twoπ
401398 return Entering
402399 else
403400 return Exiting
404401 end
405- elseif isapprox (δ, 0.0 , atol= atol ( 0.0 )) || isapprox (δ, 2pi , atol= atol ( 0.0 ) )
406- if γ < β < 2pi
402+ elseif isapprox (δ, zero (δ) , atol= tol) || isapprox (δ, twoπ , atol= tol )
403+ if γ < β < twoπ
407404 return Exiting
408405 else
409406 return Entering
410407 end
411408 end
412- if γ < δ && (γ < β || isapprox (β, γ, atol= atol (0.0 ))) && (isapprox (β, δ, atol= atol (0.0 )) || β < δ)
409+
410+ if γ < δ && (γ < β || isapprox (β, γ, atol= tol)) && (isapprox (β, δ, atol= tol) || β < δ)
413411 return Exiting
414- elseif δ < γ && (δ < β || isapprox (β, δ, atol= atol ( 0.0 ))) && (isapprox (β, γ, atol= atol ( 0.0 ) ) || β < γ)
412+ elseif δ < γ && (δ < β || isapprox (β, δ, atol= tol)) && (isapprox (β, γ, atol= tol ) || β < γ)
415413 return Entering
416414 end
417- return nothing
415+
416+ nothing
418417end
419418
420419# Converts a regular Meshes.Ring into a ring formed with RingVertex data type.
0 commit comments