Skip to content

Commit

Permalink
Added testing for togeometrybasics_faces
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin-Mattheus-Moerman committed Mar 15, 2024
1 parent 0a0854e commit 6f13391
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 29 deletions.
62 changes: 37 additions & 25 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,35 @@ sliders position will "wrap" back to the start when advancing beyond the end
position, and vice versa.
"""
function slidercontrol(hSlider::Slider,ax::Union{Axis3, Figure})
sliderRange = hSlider.range[] # Get slider range
rangeLength = length(sliderRange) # Number of possible steps
sliderIndex = hSlider.selected_index[] # Current slider index
on(events(ax).keyboardbutton) do event
if event.action == Keyboard.press || event.action == Keyboard.repeat # Pressed or held for instance
if event.key == Keyboard.up
sliderValue = hSlider.value.val
if sliderValue ==hSlider.range.val[end]
set_close_to!(hSlider, hSlider.range.val[1])
else
set_close_to!(hSlider, hSlider.value.val+1)
if event.action == Keyboard.press || event.action == Keyboard.repeat # Pressed or held for instance
if event.key == Keyboard.up
if sliderIndex == rangeLength
sliderIndex = 1
else
sliderIndex += 1
end
elseif event.key == Keyboard.down
sliderValue = hSlider.value.val
if sliderValue ==hSlider.range.val[1]
set_close_to!(hSlider, hSlider.range.val[end])
elseif event.key == Keyboard.down
if sliderIndex == 1
sliderIndex = rangeLength
else
set_close_to!(hSlider, hSlider.value.val-1)
sliderIndex -= 1
end
end
if event.key == Keyboard.right
set_close_to!(hSlider, hSlider.value.val+1)
end
if event.key == Keyboard.right
if sliderIndex < rangeLength
sliderIndex += 1
end
elseif event.key == Keyboard.left
set_close_to!(hSlider, hSlider.value.val-1)
if sliderIndex > 1
sliderIndex -= 1
end
end
println(sliderIndex)
set_close_to!(hSlider, sliderRange[sliderIndex])
end
end
end
Expand Down Expand Up @@ -865,7 +872,7 @@ function tetrahedron(r=1.0)
return GeometryBasics.Mesh(V,F)
end

function togeometrybasics_faces(FM)
function togeometrybasics_faces(FM::Union{Vector{Vector{Int64}},Vector{NgonFace{N, OffsetInteger{-1, UInt32}}}}) where N
# Loop over face matrix and convert to GeometryBasics vector of Faces (e.g. QuadFace, or TriangleFace)
n = length(FM)
m = length(FM[1])
Expand All @@ -874,7 +881,7 @@ function togeometrybasics_faces(FM)
elseif m ==4 # Quads
F = [QuadFace{Int64}(FM[q]) for q eachindex(FM)]
else # Other mesh type
F = [NgonFace{Int64}(FM[q]) for q eachindex(FM)]
F = [NgonFace{m,Int64}(FM[q]) for q eachindex(FM)]
end
return F
end
Expand All @@ -893,7 +900,7 @@ function togeometrybasics_faces(FM::Matrix{Int64})
F[q] = QuadFace{Int64}(FM[q,:])
end
else # Other mesh type
F = Vector{m,NgonFace{Int64}}(undef, n)
F = Vector{NgonFace{m,Int64}}(undef, n)
for q 1:n
F[q] = NgonFace{m,Int64}(FM[q,:])
end
Expand Down Expand Up @@ -1908,12 +1915,17 @@ function quad2tri(F,V; convert_method = :angle)::Vector{TriangleFace{Int64}}
end

function remove_unused_vertices(F,V)::Tuple
T = eltype(F)
indUsed = elements2indices(F)
Vc = V[indUsed] # Remove unused points
indFix = zeros(length(V))
indFix[indUsed].=1:length(indUsed)
Fc = [(T)(indFix[f]) for f F] # Fix indices in F
if isempty(F) # If the face set is empty, return all emtpy outputs
Fc = F
Vc = Vector{eltype(V)}(undef,0)
indFix = Vector{Int64}(undef,0)
else # Faces not empty to check which indices are used and shorten V if needed
indUsed = elements2indices(F) # Indices used
Vc = V[indUsed] # Remove unused points
indFix = zeros(length(V))
indFix[indUsed] .= 1:length(indUsed)
Fc = [(eltype(F))(indFix[f]) for f F] # Fix indices in F
end
return Fc, Vc, indFix
end

Expand Down
66 changes: 62 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,64 @@ end
end
end

@testset "togeometrybasics_faces" verbose = true begin
# Triangles
Ftm = [1 2 3; 4 5 6]
Ftv = [[1,2,3],[4,5,6]]

# Quads
Fqm = [1 2 3 4; 5 6 7 8]
Fqv = [[1,2,3,4],[5,6,7,8]]

# Ngons (pentagons)
Fnm = [1 2 3 4 5; 6 7 8 9 10]
Fnv = [[1,2,3,4,5],[6,7,8,9,10]]

@testset "Matrix input" begin
Ftm_G = togeometrybasics_faces([Ftm[1,:]])
@testset "1 TriangleFace" begin
@test isa(Ftm_G,Vector{GeometryBasics.TriangleFace{Int64}})
end

Ftm_G = togeometrybasics_faces(Ftm)
@testset "TriangleFace" begin
@test isa(Ftm_G,Vector{GeometryBasics.TriangleFace{Int64}})
end

Fqm_G = togeometrybasics_faces(Fqm)
@testset "QuadFace" begin
@test isa(Fqm_G,Vector{GeometryBasics.QuadFace{Int64}})
end

Fnm_G = togeometrybasics_faces(Fnm)
@testset "NgonFace" begin
@test isa(Fnm_G,Vector{GeometryBasics.NgonFace{5,Int64}})
end
end

@testset "vector input" begin
Ftv_G = togeometrybasics_faces([Ftv[1]])
@testset "1 TriangleFace" begin
@test isa(Ftv_G,Vector{GeometryBasics.TriangleFace{Int64}})
end

Ftv_G = togeometrybasics_faces(Ftv)
@testset "TriangleFace" begin
@test isa(Ftv_G,Vector{GeometryBasics.TriangleFace{Int64}})
end

Fqv_G = togeometrybasics_faces(Fqv)
@testset "QuadFace" begin
@test isa(Fqv_G,Vector{GeometryBasics.QuadFace{Int64}})
end

Fnv_G = togeometrybasics_faces(Fnv)
@testset "NgonFace" begin
@test isa(Fnv_G,Vector{GeometryBasics.NgonFace{5,Int64}})
end
end
end

@testset "elements2indices" verbose = true begin
@testset "Tri. faces" begin
F = Vector{TriangleFace{Int64}}(undef, 3)
Expand Down Expand Up @@ -598,8 +656,8 @@ end
r = 2 * sqrt(3) / 2
M = cube(r)

F = togeometrybasics_faces(faces(M))
V = togeometrybasics_points(coordinates(M))
F = faces(M)
V = coordinates(M)
F, V, _ = mergevertices(F, V)

@test V isa Vector{Point3{Float64}}
Expand All @@ -617,8 +675,8 @@ end
r = 2 * sqrt(3) / 2
M = cube(r)

F = togeometrybasics_faces(faces(M))
V = togeometrybasics_points(coordinates(M))
F = faces(M)
V = coordinates(M)
F, V, _ = mergevertices(F, V)

Fn, Vn = seperate_vertices(F, V)
Expand Down

0 comments on commit 6f13391

Please sign in to comment.