From dc706c85143c22c54cc6c03676e86dff8f2fbb6e Mon Sep 17 00:00:00 2001 From: Michael Schlottke-Lakemper Date: Thu, 25 Jan 2024 17:35:42 +0100 Subject: [PATCH] First example to actualy use smesh --- .gitignore | 1 + examples/build_delaunay_triangulation.jl | 12 +++++++ examples/dummy.jl | 3 -- src/Smesh.jl | 45 ++++++++++++++++++++++++ test/test_examples.jl | 4 +-- test/test_unit.jl | 9 +++-- 6 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 examples/build_delaunay_triangulation.jl delete mode 100644 examples/dummy.jl diff --git a/.gitignore b/.gitignore index 359e63b..4c7705c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ Manifest.toml +LocalPreferences.toml run/ diff --git a/examples/build_delaunay_triangulation.jl b/examples/build_delaunay_triangulation.jl new file mode 100644 index 0000000..bda809e --- /dev/null +++ b/examples/build_delaunay_triangulation.jl @@ -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) diff --git a/examples/dummy.jl b/examples/dummy.jl deleted file mode 100644 index 065499d..0000000 --- a/examples/dummy.jl +++ /dev/null @@ -1,3 +0,0 @@ -using Smesh - -Smesh.greet() diff --git a/src/Smesh.jl b/src/Smesh.jl index e9fb662..6390bff 100644 --- a/src/Smesh.jl +++ b/src/Smesh.jl @@ -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() diff --git a/test/test_examples.jl b/test/test_examples.jl index bca07d6..4ad85fe 100644 --- a/test/test_examples.jl +++ b/test/test_examples.jl @@ -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" diff --git a/test/test_unit.jl b/test/test_unit.jl index 79cd771..021b37b 100644 --- a/test/test_unit.jl +++ b/test/test_unit.jl @@ -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"