Skip to content

Make Contour.jl compatible with GLVisualize #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
language: julia
sudo: false
os:
- linux
- osx
julia:
- nightly
- release
- 0.4
- 0.3
notifications:
- email: false
script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- julia --inline=no -e 'Pkg.clone(pwd()); Pkg.test("Contour"; coverage=true)'
after_success:
- julia -e 'cd(Pkg.dir("Contour")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(process_folder()); Codecov.submit(process_folder())'
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ and `z` is a matrix arranged such that `z[xi,yi]` correspond to the location


```julia
x = [-3:0.01:3]
y = [-4:0.02:5]
x = -3:0.01:3
y = -4:0.02:5

z = [(xi^2 + yi^2)::Float64 for xi in x, yi in y]
z = [Float64((xi^2 + yi^2)) for xi in x, yi in y]
```

Let's find the contour line corresponding to `z = 4.0`:
Expand All @@ -50,13 +50,13 @@ julia> c.lines
[0.05,1.99937],[0.04,1.9996],[0.03,1.99977],[0.02,1.9999],[0.01,1.99997],
[0.0,2.0]])
```

The format of the output data is intented to give as extensive information as possible about the contour line, in a format that can be generalized in the future, if/when something like a [`Geometry.jl` package](https://groups.google.com/forum/#!topic/julia-dev/vZpZ8NBX_z8) is created. Each contour level is represented by an instance of

```julia
type ContourLevel
level::Float64
lines::Vector{Curve2}
type ContourLevel{T}
level::T
lines::Vector{Curve2{T}}
end
```

Expand All @@ -73,8 +73,8 @@ julia> plot(xs, ys) # using your favorite plotting tool

`Contour.jl` makes sure that the coordinates are ordered correctly, and contours that close on themselves are given cyclically, so that e.g. `xs[1]==xs[end]` - in other words, plotting the contour does not require you to add the first point at the end manually to close the curve.

We can also find the contours at multiple levels using `contours`,
which returns an array of `ContourLevel` types.
We can also find the contours at multiple levels using `contours`,
which returns an array of `ContourLevel` types.

```julia
julia> h = [4.0, 5.0, 6.0];
Expand All @@ -92,7 +92,7 @@ specify the number of levels we want.
julia> N = 3;
julia> c = contours(x, y, z, N)
3-element Array{ContourLevel,1}:
ContourLevel(8.5,[Curve2{Float64}([[0.62,2.84877],…,[0.62,2.84877]])])
ContourLevel(8.5,[Curve2{Float64}([[0.62,2.84877],…,[0.62,2.84877]])])
ContourLevel(17.0,[Curve2{Float64}([[3.0,2.82841],…,[3.0,-2.82841]])])
ContourLevel(25.5,[Curve2{Float64}([[3.0,4.06201],…,[-3.0,4.06201]])])
```
Expand Down
4 changes: 2 additions & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
julia 0.3
julia 0.4
Compat
ImmutableArrays
FixedSizeArrays
28 changes: 14 additions & 14 deletions src/Contour.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
module Contour

using Compat, ImmutableArrays
using Compat, FixedSizeArrays

export ContourLevel, Curve2, contour, contours, coordinates

type Curve2{T}
vertices::Vector{Vector2{T}}
vertices::Vector{Point{2,T}}
end
Curve2{T}(::Type{T}) = Curve2(Vector2{T}[])
Curve2{T}(::Type{T}) = Curve2(Point{2, T}[])

type ContourLevel
level::Float64
lines::Vector{Curve2{Float64}}
type ContourLevel{T}
level::T
lines::Vector{Curve2{T}}
end
ContourLevel(h::Float64) = ContourLevel(h, Curve2{Float64}[])
ContourLevel(h::Real) = ContourLevel(@compat Float64(h))
ContourLevel{T<:AbstractFloat}(h::T) = ContourLevel(h, Curve2{T}[])
ContourLevel{T}(h::T) = ContourLevel(Float64(h))

function contour(x, y, z, level::Number)
# Todo: size checking on x,y,z
Expand All @@ -32,10 +32,10 @@ function contourlevels(z,n)
range(zmin+dz,dz,n)
end

function coordinates(c::Curve2)
function coordinates{T}(c::Curve2{T})
N = length(c.vertices)
xlist = Array(Float64,N)
ylist = Array(Float64,N)
xlist = Array(T,N)
ylist = Array(T,N)

for (i,v) in enumerate(c.vertices)
xlist[i] = v[1]
Expand Down Expand Up @@ -144,9 +144,9 @@ const fwd, rev = (@compat UInt8(0)), (@compat UInt8(1))

function add_vertex!{T}(curve::Curve2{T}, pos::(@compat Tuple{T,T}), dir::UInt8)
if dir == fwd
push!(curve.vertices, Vector2{T}(pos...))
push!(curve.vertices, Point{2,T}(pos...))
else
unshift!(curve.vertices, Vector2{T}(pos...))
unshift!(curve.vertices, Point{2,T}(pos...))
end
end

Expand Down Expand Up @@ -233,7 +233,7 @@ function trace_contour(x, y, z, h::Number, cells::Dict{(@compat Tuple{Int,Int}),
# It then tries to trace the contour in the opposite direction.

while length(cells) > 0
contour = Curve2(Float64)
contour = Curve2(promote_type(map(eltype, (x,y,z))...))

# Pick initial box
(xi_0, yi_0), cell = first(cells)
Expand Down
6 changes: 2 additions & 4 deletions test/verify_vertices.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using ImmutableArrays

# Setup test axes that will be shared among the tests

# Shift the axes so that they do not line up with
Expand Down Expand Up @@ -111,7 +109,7 @@ end

# Issue #12
x = float(collect(1:3));
y = copy(x);
y = copy(x);
z = eye(3,3);
contours(x,y,z)

Expand Down Expand Up @@ -139,7 +137,7 @@ for line in contourlevels.lines
ys .== [v[2] for v in line.vertices]
end

# Test that closed contours are identified correctly
# Test that closed contours are identified correctly
# when ambiguous cells are involved

Z = float([1 1 1 1 1 1
Expand Down