Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to deform a thin plane to accommodate for a sphere on top of it? #2340

Open
amdshameer opened this issue Jan 15, 2025 · 0 comments
Open

Comments

@amdshameer
Copy link

Consider, we have a thin rectangular shaped plane and a sphere. The sphere's pose is above the plane such that it intersects the plane. We remove the part of the sphere that lies above the plane.

Now, how do we deform the plane to show the bulge or "curved effect" caused by the sphere? I have managed to do the following(please see the figure and code). Since I am using convex hull, the plane is not bent like a smooth curved fashion. How can I achieve the curved effect?

Unfortunately, I cannot use python3 version of trimesh.

@mikedh It would be such a huge help, if you have any ideas on how to handle this. Thank you.

trimesh_question

#!/usr/bin/python
# trimesh version: 3.23.5 python2.7
import numpy as np
import matplotlib.pyplot as plt # version 2.2.5
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
import trimesh

def display_mesh_local(ax, mesh=trimesh.Trimesh(), facecolor='yellow', mesh_border_color='black', alpha=0.5):
    poly3dcollection = [mesh.vertices[triangle] for triangle in mesh.faces]
    polygon_3D = Poly3DCollection(poly3dcollection, alpha=alpha, facecolor=facecolor, edgecolor='none', zorder=-1)
    
    lines = []
    for face in mesh.facets_boundary:
        lines.extend(np.array(mesh.vertices[face]))
    polygon_only_border_patch = Line3DCollection(lines, color=mesh_border_color, linewidth=1.0, linestyle='-', alpha=alpha, zorder=1)

    triangle_edges = []
    for face in mesh.faces:
        triangle = mesh.vertices[face]
        edges = [
            [triangle[0], triangle[1]],
            [triangle[1], triangle[2]],
            [triangle[2], triangle[0]]
        ]
        triangle_edges.extend(edges)
    triangle_edges_collection = Line3DCollection(triangle_edges, color=mesh_border_color, linewidth=0.5, alpha=alpha, linestyle='-', zorder=2)

    ax.add_collection3d(polygon_3D)
    ax.add_collection3d(polygon_only_border_patch)
    ax.add_collection3d(triangle_edges_collection)

azimuth_angle = -90.0
elevation_angle = 4.0

fig = plt.figure()
ax = Axes3D(fig=fig)
cfm = plt.get_current_fig_manager()
cfm.resize(*cfm.window.maxsize())
ax.set_xlim(-0.08, 0.08)
ax.set_ylim(-0.08, 0.08)
ax.set_zlim(0.0, 0.07)
ax.view_init(elev=elevation_angle, azim=azimuth_angle)

# 1) Create thin plane
plane_center = np.array([0.0, 0.0, 0.03])
plane_extents = [0.06, 0.04, 0.0001]
mesh_position = np.eye(4)
mesh_position[:3, 3] = plane_center.tolist()
thin_deformable_rectangle_mesh = trimesh.primitives.Box(extents=plane_extents, transform=mesh_position, mutable=True)

# 2) Create sphere
sphere_center = np.array([0.0, 0.0, 0.03])
sphere_radius = 0.005
sphere = trimesh.primitives.Sphere(radius=sphere_radius, center=sphere_center)

# 3) Union of plane and sphere
before_deformation_of_plane_and_sphere = thin_deformable_rectangle_mesh.union(sphere)

plane_z_lower_boundary = plane_center[2] - plane_extents[2] / 2.0

# 4) Filter the sphere vertices that are below the thin plane
sphere_filtered_vertices = []
for vertex, normal in zip(sphere.vertices, sphere.vertex_normals):
    if vertex[2] < plane_z_lower_boundary:
        sphere_filtered_vertices.append(vertex)

sphere_filtered_vertices = np.array(sphere_filtered_vertices)

# 5) Merge the plane vertices with the sphere_filtered_vertices
merged_vertices = np.vstack((thin_deformable_rectangle_mesh.vertices, sphere_filtered_vertices))

# 6) Convex hull from merged vertices
hull = trimesh.convex.convex_hull(merged_vertices)

# 7) Convert the convex hull to a trimesh object
mesh_from_hull = trimesh.Trimesh(vertices=hull.vertices, faces=hull.faces)

# 8) Deformed plane
deformed_plane = mesh_from_hull.difference(before_deformation_of_plane_and_sphere)
display_mesh_local(ax=ax, mesh=deformed_plane, facecolor='none')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant