@@ -2,7 +2,7 @@ __precompile__()
2
2
3
3
module Contour
4
4
5
- using Compat, StaticArrays
5
+ using StaticArrays
6
6
7
7
export
8
8
ContourLevel,
@@ -16,21 +16,21 @@ export
16
16
17
17
import Base: push!, start, next, done, length, eltype, show
18
18
19
- type Curve2{T}
19
+ struct Curve2{T}
20
20
vertices:: Vector{SVector{2,T}}
21
21
end
22
- Curve2 {T} (:: Type{T} ) = Curve2 (SVector{2 , T}[])
22
+ Curve2 (:: Type{T} ) where {T} = Curve2 (SVector{2 ,T}[])
23
23
show (io:: IO , :: MIME"text/plain" , c2:: Curve2 ) = write (io, " $(typeof (c2)) \n with $(length (c2. vertices)- 1 ) vertices" )
24
- show {TC<:Curve2} (io:: IO , :: MIME"text/plain" , c2s:: Vector{TC} ) = write (io, " $(typeof (c2s)) \n $(length (c2s)) contour line(s)" )
24
+ show (io:: IO , :: MIME"text/plain" , c2s:: Vector{TC} ) where {TC <: Curve2 } = write (io, " $(typeof (c2s)) \n $(length (c2s)) contour line(s)" )
25
25
26
- type ContourLevel{T}
26
+ struct ContourLevel{T}
27
27
level:: T
28
28
lines:: Vector{Curve2{T}}
29
29
end
30
- ContourLevel {T<:AbstractFloat} (h:: T ) = ContourLevel (h, Curve2{T}[])
31
- ContourLevel {T} (h:: T ) = ContourLevel (Float64 (h))
30
+ ContourLevel (h:: T ) where {T <: AbstractFloat } = ContourLevel (h, Curve2{T}[])
31
+ ContourLevel (h:: T ) where {T} = ContourLevel (Float64 (h))
32
32
show (io:: IO , :: MIME"text/plain" , cl:: ContourLevel ) = write (io, " $(typeof (cl)) \n at $(level (cl)) with $(length (lines (cl))) line(s)" )
33
- show {CL<:ContourLevel} (io:: IO , :: MIME"text/plain" , cls:: Vector{CL} ) = write (io, " $(typeof (cls)) \n $(length (cls)) contour level(s)" )
33
+ show (io:: IO , :: MIME"text/plain" , cls:: Vector{CL} ) where {CL <: ContourLevel } = write (io, " $(typeof (cls)) \n $(length (cls)) contour level(s)" )
34
34
"""
35
35
`lines(c)` Extracts an iterable collection of isolines from a contour level.
36
36
Use [`coordinates`](@ref) to get the coordinates of a line.
@@ -41,11 +41,11 @@ lines(cl::ContourLevel) = cl.lines
41
41
"""
42
42
level (cl:: ContourLevel ) = cl. level
43
43
44
- immutable ContourCollection{Tlevel<: ContourLevel }
44
+ struct ContourCollection{Tlevel<: ContourLevel }
45
45
contours:: Vector{Tlevel}
46
46
end
47
47
ContourCollection () = ContourCollection (Float64)
48
- ContourCollection {Tlevel} (:: Type{Tlevel} ) = ContourCollection (ContourLevel{Tlevel}[])
48
+ ContourCollection (:: Type{Tlevel} ) where {Tlevel} = ContourCollection (ContourLevel{Tlevel}[])
49
49
show (io:: IO , :: MIME"text/plain" , cc:: ContourCollection ) = write (io, " $(typeof (cc)) \n with $(length (levels (cc))) level(s)." )
50
50
51
51
"""
@@ -64,7 +64,7 @@ over the result.
64
64
"""
65
65
function contour (x, y, z, level:: Number )
66
66
# Todo: size checking on x,y,z
67
- trace_contour (x, y, z,level,get_level_cells (z,level))
67
+ trace_contour (x, y, z, level, get_level_cells (z, level))
68
68
end
69
69
70
70
"""
@@ -78,41 +78,41 @@ contours(::Any...) = error("This method exists only for documentation purposes")
78
78
`contours(x,y,z,levels)` Trace the contour levels indicated by the `levels`
79
79
argument.
80
80
"""
81
- contours (x,y,z, levels) = ContourCollection ([contour (x,y,z, l) for l in levels])
81
+ contours (x, y, z, levels) = ContourCollection ([contour (x, y, z, l) for l in levels])
82
82
83
83
"""
84
84
`contours(x,y,z,Nlevels::Integer)` Trace `Nlevels` contour levels at heights
85
85
chosen by the library (using the [`contourlevels`](@ref) function).
86
86
"""
87
- function contours (x,y,z, Nlevels:: Integer )
88
- contours (x,y,z, contourlevels (z,Nlevels))
87
+ function contours (x, y, z, Nlevels:: Integer )
88
+ contours (x, y, z, contourlevels (z, Nlevels))
89
89
end
90
90
91
91
"""
92
92
`contours(x,y,z)` Trace 10 automatically chosen contour levels.
93
93
"""
94
- contours (x,y, z) = contours (x,y,z, 10 )
94
+ contours (x, y, z) = contours (x, y, z, 10 )
95
95
96
96
"""
97
97
`contourlevels(z,n)` Examines the values of `z` and chooses `n` evenly spaced
98
98
levels to trace.
99
99
"""
100
- function contourlevels (z,n)
101
- zmin,zmax = extrema (z)
102
- dz = (zmax- zmin) / (n+ 1 )
103
- range (zmin+ dz,dz,n)
100
+ function contourlevels (z, n)
101
+ zmin, zmax = extrema (z)
102
+ dz = (zmax - zmin) / (n + 1 )
103
+ range (zmin + dz, dz, n)
104
104
end
105
105
106
106
"""
107
107
`coordinates(c)` Returns the coordinates of the vertices of the contour line as
108
108
a tuple of lists.
109
109
"""
110
- function coordinates {T} (c:: Curve2{T} )
110
+ function coordinates (c:: Curve2{T} ) where {T}
111
111
N = length (c. vertices)
112
112
xlist = Vector {T} (N)
113
113
ylist = Vector {T} (N)
114
114
115
- for (i,v) in enumerate (c. vertices)
115
+ for (i, v) in enumerate (c. vertices)
116
116
xlist[i] = v[1 ]
117
117
ylist[i] = v[2 ]
118
118
end
@@ -153,14 +153,14 @@ const dirStr = ["N", "S", "NS", "E", "NE", "NS", "Invalid crossing",
153
153
# the type of crossing that a cell contains. While most
154
154
# cells will have only one crossing, cell type 5 and 10 will
155
155
# have two crossings.
156
- type Cell
156
+ struct Cell
157
157
crossings:: Vector{UInt8}
158
158
end
159
159
160
160
function get_next_edge! (cell:: Cell , entry_edge:: UInt8 )
161
- for (i,edge) in enumerate (cell. crossings)
161
+ for (i, edge) in enumerate (cell. crossings)
162
162
if edge & entry_edge != 0
163
- @compat next_edge = edge ⊻ entry_edge
163
+ next_edge = edge ⊻ entry_edge
164
164
deleteat! (cell. crossings, i)
165
165
166
166
return next_edge
@@ -178,12 +178,12 @@ function get_level_cells(z, h::Number)
178
178
179
179
local case:: Int8
180
180
181
- for xi in 1 : xi_max- 1
182
- for yi in 1 : yi_max- 1
183
- case = 1 (z[xi,yi] > h) |
184
- 2 (z[xi+ 1 , yi] > h) |
185
- 4 (z[xi+ 1 ,yi + 1 ] > h) |
186
- 8 (z[xi,yi + 1 ] > h)
181
+ for xi in 1 : xi_max - 1
182
+ for yi in 1 : yi_max - 1
183
+ case = 1 (z[xi, yi] > h) |
184
+ 2 (z[xi + 1 , yi] > h) |
185
+ 4 (z[xi + 1 , yi + 1 ] > h) |
186
+ 8 (z[xi, yi + 1 ] > h)
187
187
188
188
# Contour does not go through these cells
189
189
if case == 0 || case == 15
@@ -193,19 +193,19 @@ function get_level_cells(z, h::Number)
193
193
# Process ambiguous cells (case 5 and 10) using
194
194
# a bilinear interpolation of the cell-center value.
195
195
if case == 5
196
- if 0.25 (z[xi,yi] + z[xi,yi + 1 ] + z[xi+ 1 , yi] + z[xi+ 1 ,yi + 1 ]) >= h
197
- cells[(xi,yi)] = Cell ([NW, SE])
196
+ if 0.25 (z[xi, yi] + z[xi, yi + 1 ] + z[xi + 1 , yi] + z[xi + 1 , yi + 1 ]) >= h
197
+ cells[(xi, yi)] = Cell ([NW, SE])
198
198
else
199
- cells[(xi,yi)] = Cell ([NE, SW])
199
+ cells[(xi, yi)] = Cell ([NE, SW])
200
200
end
201
201
elseif case == 10
202
- if 0.25 (z[xi,yi] + z[xi,yi + 1 ] + z[xi+ 1 , yi] + z[xi+ 1 ,yi + 1 ]) >= h
203
- cells[(xi,yi)] = Cell ([NE, SW])
202
+ if 0.25 (z[xi, yi] + z[xi, yi + 1 ] + z[xi + 1 , yi] + z[xi + 1 , yi + 1 ]) >= h
203
+ cells[(xi, yi)] = Cell ([NE, SW])
204
204
else
205
- cells[(xi,yi)] = Cell ([NW, SE])
205
+ cells[(xi, yi)] = Cell ([NW, SE])
206
206
end
207
207
else
208
- cells[(xi,yi)] = Cell ([edge_LUT[case]])
208
+ cells[(xi, yi)] = Cell ([edge_LUT[case]])
209
209
end
210
210
end
211
211
end
217
217
218
218
const fwd, rev = (UInt8 (0 )), (UInt8 (1 ))
219
219
220
- function add_vertex! {T} (curve:: Curve2{T} , pos:: (Tuple{T,T}) , dir:: UInt8 )
220
+ function add_vertex! (curve:: Curve2{T} , pos:: (Tuple{T,T}) , dir:: UInt8 ) where {T}
221
221
if dir == fwd
222
222
push! (curve. vertices, SVector {2,T} (pos... ))
223
223
else
@@ -228,19 +228,19 @@ end
228
228
# Given the row and column indices of the lower left
229
229
# vertex, add the location where the contour level
230
230
# crosses the specified edge.
231
- function interpolate {T<:AbstractFloat} (x, y, z:: Matrix{T} , h:: Number , xi:: Int , yi:: Int , edge:: UInt8 )
231
+ function interpolate (x, y, z:: Matrix{T} , h:: Number , xi:: Int , yi:: Int , edge:: UInt8 ) where {T <: AbstractFloat }
232
232
if edge == W
233
- y_interp = y[yi] + (y[yi+ 1 ] - y[yi])* (h - z[xi,yi])/ (z[xi,yi + 1 ] - z[xi,yi])
233
+ y_interp = y[yi] + (y[yi + 1 ] - y[yi]) * (h - z[xi, yi]) / (z[xi, yi + 1 ] - z[xi, yi])
234
234
x_interp = x[xi]
235
235
elseif edge == E
236
- y_interp = y[yi] + (y[yi+ 1 ] - y[yi])* (h - z[xi+ 1 , yi])/ (z[xi+ 1 ,yi + 1 ] - z[xi+ 1 , yi])
236
+ y_interp = y[yi] + (y[yi + 1 ] - y[yi]) * (h - z[xi + 1 , yi]) / (z[xi + 1 , yi + 1 ] - z[xi + 1 , yi])
237
237
x_interp = x[xi + 1 ]
238
238
elseif edge == N
239
239
y_interp = y[yi + 1 ]
240
- x_interp = x[xi] + (x[xi+ 1 ] - x[xi])* (h - z[xi,yi + 1 ])/ (z[xi+ 1 ,yi + 1 ] - z[xi,yi + 1 ])
240
+ x_interp = x[xi] + (x[xi + 1 ] - x[xi]) * (h - z[xi, yi + 1 ]) / (z[xi + 1 , yi + 1 ] - z[xi, yi + 1 ])
241
241
elseif edge == S
242
242
y_interp = y[yi]
243
- x_interp = x[xi] + (x[xi+ 1 ] - x[xi])* (h - z[xi,yi])/ (z[xi+ 1 , yi] - z[xi,yi])
243
+ x_interp = x[xi] + (x[xi + 1 ] - x[xi]) * (h - z[xi, yi]) / (z[xi + 1 , yi] - z[xi, yi])
244
244
end
245
245
246
246
return x_interp, y_interp
@@ -259,10 +259,10 @@ function chase!(cells, curve, x, y, z, h, xi_start, yi_start, entry_edge, xi_max
259
259
loopback_edge = entry_edge
260
260
261
261
while true
262
- cell = cells[(xi,yi)]
262
+ cell = cells[(xi, yi)]
263
263
exit_edge = get_next_edge! (cell, entry_edge)
264
264
if length (cell. crossings) == 0
265
- delete! (cells, (xi,yi))
265
+ delete! (cells, (xi, yi))
266
266
end
267
267
268
268
add_vertex! (curve, interpolate (x, y, z, h, xi, yi, exit_edge), dir)
@@ -280,7 +280,7 @@ function chase!(cells, curve, x, y, z, h, xi_start, yi_start, entry_edge, xi_max
280
280
xi -= 1
281
281
entry_edge = E
282
282
end
283
- ! ((xi,yi,entry_edge) != (xi_start,yi_start, loopback_edge) &&
283
+ ! ((xi, yi, entry_edge) != (xi_start, yi_start, loopback_edge) &&
284
284
0 < yi < yi_max && 0 < xi < xi_max) && break
285
285
end
286
286
@@ -308,11 +308,11 @@ function trace_contour(x, y, z, h::Number, cells::Dict{(Tuple{Int,Int}),Cell})
308
308
# It then tries to trace the contour in the opposite direction.
309
309
310
310
while length (cells) > 0
311
- contour = Curve2 (promote_type (map (eltype, (x,y, z))... ))
311
+ contour = Curve2 (promote_type (map (eltype, (x, y, z))... ))
312
312
313
313
# Pick initial box
314
314
(xi_0, yi_0), cell = first (cells)
315
- (xi,yi) = (xi_0,yi_0)
315
+ (xi, yi) = (xi_0, yi_0)
316
316
317
317
# Pick a starting edge
318
318
crossing = first (cell. crossings)
0 commit comments