Skip to content

Commit

Permalink
First example to actualy use smesh
Browse files Browse the repository at this point in the history
  • Loading branch information
sloede committed Jan 25, 2024
1 parent c457554 commit dc706c8
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Manifest.toml
LocalPreferences.toml
run/
12 changes: 12 additions & 0 deletions examples/build_delaunay_triangulation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Smesh

# Create data points
# Note: the transpose + collect is just such that we can write the matrix in human readable
# form here
data_points = collect([0.0 0.0
1.0 0.0
1.0 1.0
0.0 1.0]')

# Create triangulation
ve = build_delaunay_triangulation(data_points; verbose = true)
3 changes: 0 additions & 3 deletions examples/dummy.jl

This file was deleted.

45 changes: 45 additions & 0 deletions src/Smesh.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
module Smesh

using Preferences: @load_preference, @has_preference

export build_delaunay_triangulation

if !@has_preference("libsmesh")
error("""
Missing preference `libsmesh` for package Smesh.jl. Please add a
`LocalPreferences.toml` file to your current Julia project with the following
content, where `path/to/libsmesh.{ext}` is the path to your local build of
libsmesh and `{ext}` is the appropriate extension for shared libraries on your
system (e.g., `so` on Linux, `dylib` on macOS, `dll` on Windows). Afterwards,
you need to restart Julia.
Content of `LocalPreferences.toml` (between the '```' marks):
```
[Smesh]
libsmesh = "path/to/libsmesh.{ext}"
```
""")
end
const libsmesh = @load_preference("libsmesh")


function build_delaunay_triangulation(data_points; shuffle = false, verbose = false)
# Pre-allocate output array
npoints = size(data_points, 2)
ve_max = @ccall libsmesh.delaunay_triangulation_temparray_size_c(npoints::Cint)::Cint
ve_internal = Matrix{Cint}(undef, 3, ve_max)

# Perform triangulation
ntriangles = @ccall libsmesh.build_delaunay_triangulation_c(ve_internal::Ref{Cint},
data_points::Ref{Float64},
npoints::Cint,
ve_max::Cint,
shuffle::Cint,
verbose::Cint)::Cint

# Copy to array of appropriate size and convert to Julia `Int`s for convenience
ve_out = convert(Matrix{Int}, ve_internal[:, 1:ntriangles])

return ve_out
end


"""
greet()
Expand Down
4 changes: 2 additions & 2 deletions test/test_examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ using Smesh

@testset verbose=true showtiming=true "test_examples.jl" begin

@testset verbose=true showtiming=true "examples/dummy.jl" begin
@test_nowarn include("../examples/dummy.jl")
@testset verbose=true showtiming=true "examples/build_delaunay_triangulation.jl" begin
@test_nowarn include("../examples/build_delaunay_triangulation.jl")
end

end # @testset "test_examples.jl"
Expand Down
9 changes: 7 additions & 2 deletions test/test_unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ using Smesh

@testset verbose=true showtiming=true "test_unit.jl" begin

@testset verbose=true showtiming=true "greet" begin
@test_nowarn Smesh.greet()
@testset verbose=true showtiming=true "build_delaunay_triangulation" begin
data_points = collect([0.0 0.0
1.0 0.0
1.0 1.0
0.0 1.0]')

@test build_delaunay_triangulation(data_points) == [3 1; 1 3; 2 4]
end

end # @testset "test_unit.jl"
Expand Down

0 comments on commit dc706c8

Please sign in to comment.