Skip to content

Commit

Permalink
Added face angle based segmentation functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin-Mattheus-Moerman committed Aug 28, 2024
1 parent c174712 commit 37dba60
Show file tree
Hide file tree
Showing 7 changed files with 322 additions and 24 deletions.
Binary file added assets/stl/spur_gear_01.stl
Binary file not shown.
Binary file added assets/stl/spur_gear_02.stl
Binary file not shown.
59 changes: 59 additions & 0 deletions examples/demo_edgefaceangles.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Comodo
using GLMakie
using GeometryBasics
using FileIO
using LinearAlgebra

testCase = 1

if testCase==1
# A "zig-zag" curve with known angles for testing
r = 2.0
B = [170.0,150.0,135.0,90.0,75.0,60.0,45.0,-45.0,-60.0,-75,-90.0,-135.0,-150.0,-170.0]
V = Vector{Point{3, Float64}}()
push!(V,[0.0, 0.0, 0.0])
for b in B
push!(V,V[end]+[r, 0.0, 0.0])
push!(V,V[end]+[r*cosd(b),r*sind(b), 0.0])
end
push!(V,V[end]+[r, 0.0, 0.0])

F,V = extrudecurve(V; extent=1, direction=:positive, n=Vec{3, Float64}(0.0,0.0,-1.0), close_loop=false,face_type=:quad)
elseif testCase==2
# An imported STL based geometry of an engineering part with various flat faces and some known angles (e.g. 0, 45, and 90 degrees)
fileName_mesh = joinpath(comododir(),"assets","stl","spur_gear_01.stl")
M = load(fileName_mesh)
F = tofaces(faces(M))
V = topoints(coordinates(M))
F,V,_,_ = mergevertices(F,V)
end

# E = meshedges(F)
# E_uni,_,indReverse = gunique(E; return_unique=true, return_index=true, return_inverse=true, sort_entries=true)
# con_E2F = con_edge_face(F,E_uni)
# con_F2E = con_face_edge(F,E_uni,indReverse)

A,E,con_E2F = edgefaceangles(F,V; deg=true)


## Visualization
linewidth = 3

Lp = .~isnan.(A) .&& abs.(A).>30
Ap = A[Lp]
Ep = E[Lp]

En,Vn = separate_vertices(Ep,V)
An = simplex2vertexdata(En,Ap,Vn)

fig = Figure(size=(800,800))

ax1 = Axis3(fig[1, 1], aspect = :data, xlabel = "X", ylabel = "Y", zlabel = "Z", title = "Edge angles")
hp1 = poly!(ax1,GeometryBasics.Mesh(V,F), color=:white, shading = FastShading, transparency=false)#,strokecolor=:black,strokewidth=0.25)
# normalplot(ax1,F,V)

hp_A = wireframe!(ax1,GeometryBasics.Mesh(Vn,En),linewidth=linewidth, transparency=false, color=An,colormap=:Spectral,colorrange = (-180, 180))

Colorbar(fig[1, 2],hp_A, label = "Angles",ticks =-180:20:180)

fig
83 changes: 83 additions & 0 deletions examples/demo_faceanglesegment.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Comodo
using GLMakie
using GeometryBasics
using FileIO
using LinearAlgebra

testCase = 2

if testCase==1
# A "zig-zag" curve with known angles for testing
r = 2.0
B = [170.0,150.0,135.0,90.0,75.0,60.0,45.0,-45.0,-60.0,-75,-90.0,-135.0,-150.0,-170.0]
V = Vector{Point{3, Float64}}()
push!(V,[0.0, 0.0, 0.0])
for b in B
push!(V,V[end]+[r, 0.0, 0.0])
push!(V,V[end]+[r*cosd(b),r*sind(b), 0.0])
end
push!(V,V[end]+[r, 0.0, 0.0])

F,V = extrudecurve(V; extent=1, direction=:positive, n=Vec{3, Float64}(0.0,0.0,-1.0), close_loop=false,face_type=:quad)
elseif testCase==2
# An imported STL based geometry of an engineering part with various flat faces and some known angles (e.g. 0, 45, and 90 degrees)
# This model is relatively evenly sampled and of a relatively low resolution
fileName_mesh = joinpath(comododir(),"assets","stl","spur_gear_01.stl")
M = load(fileName_mesh)
F = tofaces(faces(M))
V = topoints(coordinates(M))
F,V,_,_ = mergevertices(F,V)
elseif testCase==3 # Same as 2 but higher angular/general resolution
# An imported STL based geometry of an engineering part with various flat faces and some known angles (e.g. 0, 45, and 90 degrees)
# This model features a non-homogeneous mesh (includes sharp triangles) and is of a relatively high resolution
fileName_mesh = joinpath(comododir(),"assets","stl","spur_gear_02.stl")
M = load(fileName_mesh)
F = tofaces(faces(M))
V = topoints(coordinates(M))
F,V,_,_ = mergevertices(F,V)
end

# E = meshedges(F)
# E_uni,_,indReverse = gunique(E; return_unique=true, return_index=true, return_inverse=true, sort_entries=true)
# con_E2F = con_edge_face(F,E_uni)
# con_F2E = con_face_edge(F,E_uni,indReverse)

A,E,con_E2F = edgefaceangles(F,V; deg=true)


G = faceanglesegment(F,V; deg=true, angleThreshold = 22.5, indStart = 1)

## Visualization
linewidth = 3
c = cgrad(:Spectral,maximum(G),categorical = true)
A,E,con_E2F = edgefaceangles(F,V; deg=true)

Lp = .~isnan.(A) .&& abs.(A).>30
Ap = A[Lp]
Ep = E[Lp]

En,Vn_E = separate_vertices(Ep,V)
An = simplex2vertexdata(En,Ap,Vn_E)


Fn,Vn = separate_vertices(F,V)
Gn = simplex2vertexdata(Fn,G,Vn)## Visualization
linewidth = 3

fig = Figure(size=(800,800))

ax1 = Axis3(fig[1, 1], aspect = :data, xlabel = "X", ylabel = "Y", zlabel = "Z", title = "Edge angles")
hp1 = poly!(ax1,GeometryBasics.Mesh(Vn,Fn), color=:white, shading = FastShading, transparency=false,strokecolor=:black,strokewidth=0.25)
# normalplot(ax1,F,V)
# hp_A = wireframe!(ax1,GeometryBasics.Mesh(Vn_E,En),linewidth=linewidth, transparency=false, color=An,colormap=:Spectral,colorrange = (-120, 120))
# Colorbar(fig[1, 2],hp_A, label = "Angles")

ax2 = Axis3(fig[1, 2], aspect = :data, xlabel = "X", ylabel = "Y", zlabel = "Z", title = "Groups")
hp2 = poly!(ax2,GeometryBasics.Mesh(Vn,Fn), color=Gn, shading = FastShading, transparency=false,colormap=c)#,strokecolor=:black,strokewidth=0.25)
# normalplot(ax1,F,V)

hp3 = wireframe!(ax2,GeometryBasics.Mesh(Vn_E,En),linewidth=linewidth, transparency=false, color=:black)

Colorbar(fig[1, 3],hp2, label = "Groups")

fig
23 changes: 13 additions & 10 deletions examples/demo_meshgroup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using GLMakie
using GeometryBasics

# Example geometry
testCase = 3
testCase = 4
if testCase == 1
s=1.0
V=Vector{GeometryBasics.Point{3, Float64}}(undef,5)
Expand All @@ -30,6 +30,9 @@ elseif testCase==2
elseif testCase==3
r = 1.0
F,V = geosphere(2,r)
elseif testCase==4
r = 1.0
F,V = geosphere(2,r)
for i = 0:1:2
for j = 0:1:2
for k = 0:1:2
Expand All @@ -43,7 +46,7 @@ elseif testCase==3
end
end
end
elseif testCase==4
elseif testCase==5
r = 1.0
F,V = quadsphere(2,r)
for i = 0:1:2
Expand All @@ -59,7 +62,7 @@ elseif testCase==4
end
end
end
elseif testCase==5 # Unmerged STL, each triangle is seperate group
elseif testCase==6 # Unmerged STL, each triangle is seperate group
# Loading a mesh
using FileIO
fileName_mesh = joinpath(comododir(),"assets","stl","stanford_bunny_low.stl")
Expand All @@ -68,7 +71,7 @@ elseif testCase==5 # Unmerged STL, each triangle is seperate group
# Obtain mesh faces and vertices
F = tofaces(faces(M))
V = topoints(coordinates(M))
elseif testCase==6 # Merged STL for single object
elseif testCase==7 # Merged STL for single object
# Loading a mesh
using FileIO
fileName_mesh = joinpath(comododir(),"assets","stl","stanford_bunny_low.stl")
Expand All @@ -78,7 +81,7 @@ elseif testCase==6 # Merged STL for single object
F = tofaces(faces(M))
V = topoints(coordinates(M))
F,V,_ = mergevertices(F,V)
elseif testCase==7 # Merged STL for single object
elseif testCase==8 # Merged STL for single object
# Loading a mesh
using FileIO
fileName_mesh = joinpath(comododir(),"assets","stl","david.stl")
Expand All @@ -105,16 +108,16 @@ Cn = simplex2vertexdata(Fn,C,Vn)

# Visualization

fig = Figure(size=(800,800))
fig = Figure(size=(1200,1200))

ax1 = Axis3(fig[1, 1], aspect = :data, xlabel = "X", ylabel = "Y", zlabel = "Z", title = "A multi-object mesh")
hp1 = poly!(ax1,GeometryBasics.Mesh(V,F), strokewidth=1,color=:white, strokecolor=:black, shading = FastShading, transparency=false)

ax2 = Axis3(fig[2, 1], aspect = :data, xlabel = "X", ylabel = "Y", zlabel = "Z", title = "Grouping")
hp2 = poly!(ax2,GeometryBasics.Mesh(Vn,Fn), strokewidth=1,color=Cn, strokecolor=:black, shading = FastShading, transparency=false,colormap=Makie.Categorical(:viridis))
ax2 = Axis3(fig[1, 2], aspect = :data, xlabel = "X", ylabel = "Y", zlabel = "Z", title = "Grouping")
hp2 = poly!(ax2,GeometryBasics.Mesh(Vn,Fn), strokewidth=1,color=Cn, strokecolor=:black, shading = FastShading, transparency=false,colormap=c)

Legend(fig[1, 2],[hp1,hp2],["Mesh object","Grouped mesh"])
Colorbar(fig[2, 2],hp2, label = "Group labelling")
Legend(fig[1, 3],[hp1,hp2],["Mesh object","Grouped mesh"])
Colorbar(fig[1, 4],hp2, label = "Group labelling")

fig

Expand Down
2 changes: 1 addition & 1 deletion src/Comodo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export invert_faces, kabsch_rot, sweeploft, loftpoints2surf, revolvecurve
export batman, tridisc, triplate, regiontrimesh, scalesimplex, subcurve, dualclad
export tet2hex, element2faces, subhex, rhombicdodecahedron, tri2quad
export tetgenmesh, surfacevolume, tetvolume, extrudefaces, filletcurve
export squircle, circlerange
export squircle, circlerange, edgefaceangles,faceanglesegment

# Export functions: Visualization related
export slidercontrol, slider2anim, dirplot, normalplot
Expand Down
Loading

0 comments on commit 37dba60

Please sign in to comment.