11module Contour
22
3- using StaticArrays
4-
53include (" interpolate.jl" )
64
75export
@@ -17,17 +15,17 @@ export
1715import Base: push!, length, eltype, show
1816
1917struct Curve2{T}
20- vertices:: Vector{NTuple{2,T} }
18+ vertices:: Vector{T }
2119end
22- Curve2 (:: Type{T} ) where {T} = Curve2 (SVector{ 2 ,T} [])
20+ Curve2 (:: Type{T} ) where {T} = Curve2 (T [])
2321show (io:: IO , :: MIME"text/plain" , c2:: Curve2 ) = write (io, " $(typeof (c2)) \n with $(length (c2. vertices)- 1 ) vertices" )
2422show (io:: IO , :: MIME"text/plain" , c2s:: Vector{Curve2{T}} ) where {T} = write (io, " $(typeof (c2s)) \n $(length (c2s)) contour line(s)" )
2523
26- struct ContourLevel{T}
27- level:: T
24+ struct ContourLevel{T, L }
25+ level:: L
2826 lines:: Vector{Curve2{T}}
2927end
30- ContourLevel (h:: T ) where {T <: AbstractFloat } = ContourLevel (h, Curve2{T }[])
28+ ContourLevel (h:: T ) where {T <: AbstractFloat } = ContourLevel (h, Curve2{NTuple{ 2 ,T} }[])
3129ContourLevel (h:: T ) where {T} = ContourLevel (Float64 (h))
3230show (io:: IO , :: MIME"text/plain" , cl:: ContourLevel ) = write (io, " $(typeof (cl)) \n at $(level (cl)) with $(length (lines (cl))) line(s)" )
3331show (io:: IO , :: MIME"text/plain" , cls:: Vector{ContourLevel{T}} ) where {T} = write (io, " $(typeof (cls)) \n $(length (cls)) contour level(s)" )
@@ -62,11 +60,12 @@ argument `level`.
6260You'll usually call [`lines`](@ref) on the output of `contour`, and then iterate
6361over the result.
6462"""
65- function contour (x, y, z, level:: Number )
63+ function contour (x, y, z, level:: Number ; VT = nothing )
6664 if ! (axes (x) == (axes (z,1 ),) && axes (y) == (axes (z,2 ),) || axes (x) == axes (y) == axes (z))
6765 throw (ArgumentError (" Incompatible input axes in `Contour.contour`." ))
6866 end
69- trace_contour (x, y, z, level, get_level_cells (z, level))
67+ VT = VT === nothing ? NTuple{2 ,promote_type (map (eltype, (x, y, z))... )} : VT
68+ trace_contour (x, y, z, level, get_level_cells (z, level), VT)
7069end
7170
7271"""
@@ -111,8 +110,9 @@ a tuple of lists.
111110"""
112111function coordinates (c:: Curve2{T} ) where {T}
113112 N = length (c. vertices)
114- xlist = Vector {T} (undef, N)
115- ylist = Vector {T} (undef, N)
113+ E = eltype (T)
114+ xlist = Vector {E} (undef, N)
115+ ylist = Vector {E} (undef, N)
116116
117117 for (i, v) in enumerate (c. vertices)
118118 xlist[i] = v[1 ]
@@ -264,17 +264,15 @@ function chase!(cells, curve, x, y, z, h, start, entry_edge, xi_range, yi_range,
264264end
265265
266266
267- function trace_contour (x, y, z, h:: Number , cells:: Dict )
267+ function trace_contour (x, y, z, h:: Number , cells:: Dict , VT )
268268
269- contours = ContourLevel (h)
269+ contours = ContourLevel (h, Curve2{VT}[] )
270270
271271 x_ax, y_ax = axes (z)
272272 xi_range = first (x_ax): last (x_ax)- 1
273273 yi_range = first (y_ax): last (y_ax)- 1
274274
275275
276- VT = NTuple{2 ,promote_type (map (eltype, (x, y, z))... )}
277-
278276 # When tracing out contours, this algorithm picks an arbitrary
279277 # starting cell, then first follows the contour in one direction
280278 # until it either ends up where it started # or at one of the boundaries.
0 commit comments