My intension was to do Slicing an 3d object which is inside the stl file. Please help with below code
using FileIO
using GeometryBasics
using GLMakie
rotate_x(point::GeometryBasics.Point{3, Float32}, angle_deg::Float64) = GeometryBasics.Point3f(
point[1],
cos(deg2rad(angle_deg)) * point[2] - sin(deg2rad(angle_deg)) * point[3],
sin(deg2rad(angle_deg)) * point[2] + cos(deg2rad(angle_deg)) * point[3]
)
function clip_mesh(mesh::GeometryBasics.Mesh, lower_clip::Float64, upper_clip::Float64)
vertices = GeometryBasics.coordinates(mesh)
faces = GeometryBasics.faces(mesh)
valid_vertex_indices = findall(v -> lower_clip < v[3] < upper_clip, vertices)
valid_indices_set = Set(valid_vertex_indices)
new_faces = []
for face in faces
face_indices = [Int(i) for i in face]
if all(idx -> idx in valid_indices_set, face_indices)
new_face_indices = map(idx -> findfirst(==(idx), valid_vertex_indices), face_indices)
push!(new_faces, Face(new_face_indices...))
end
end
new_vertices = vertices[valid_vertex_indices]
return GeometryBasics.Mesh(new_vertices, new_faces)
end
function process_and_display_stl(input_file::String, rotation_angle_x::Float64, lower_clip::Float64, upper_clip::Float64)
println("Processing STL file: $input_file")
mesh = load(input_file)
rotated_vertices = [rotate_x(v.position, rotation_angle_x) for v in GeometryBasics.coordinates(mesh)]
rotated_mesh = GeometryBasics.Mesh(rotated_vertices, GeometryBasics.faces(mesh))
clipped_mesh = clip_mesh(rotated_mesh, lower_clip, upper_clip)
fig = Figure(resolution = (800, 800))
ax = Axis3(fig[1, 1], title = "Processed Mesh Visualization")
mesh!(ax, clipped_mesh, color = :gray, transparency = true)
display(fig)
end
input_stl = "LowerJaw.stl"
rotation_angle = 90.0
lower_clip = -20.0
upper_clip = 10.0
process_and_display_stl(input_stl, rotation_angle, lower_clip, upper_clip)
please find below output which i got as soon as i executed above code
Please help
thanks and regards