I have tried recreating a cylinder that is deformed in a gaussian way. Here’s my code for that:
import pyvista as pv
import numpy as np
# Set higher resolution for a finer mesh
height = 2.0
radius = 1.0
resolution_height = 50 # Number of subdivisions along the height
resolution_circumference = 200 # Number of subdivisions along the circumference
# Create a structured cylinder with finer mesh
cylinder = pv.CylinderStructured(radius=radius, height=height,
theta_resolution=resolution_circumference,
z_resolution=resolution_height)
# Convert the structured grid to PolyData for normal computation
cylinder_polydata = cylinder.extract_surface()
# Compute normals for the surface
cylinder_polydata = cylinder_polydata.compute_normals(cell_normals=False)
# Extract the computed point normals
normals = cylinder_polydata['Normals']
# Define different standard deviations for radial and vertical directions
num_parameters = 5 # Adjust as needed
deformed_points = np.copy(cylinder_polydata.points)
# Generate random sigmas for radial direction
sigmas = np.random.uniform(0.1, 0.5, size=num_parameters) # Example range for sigmas
sgns_scale = np.random.uniform(-3, -2, size=num_parameters)
centers = np.random.uniform(-height, height, size=num_parameters)
for sgn, sigma, center in zip(sgns_scale, sigmas, centers):
for i, point in enumerate(deformed_points):
distance = np.linalg.norm(point - np.array([ center, 0, 0])) # Distance from the center
gaussian_factor = np.exp(-0.5 * (distance / sigma) ** 2) # Gaussian weight
displacement = sgn * gaussian_factor * normals[i] # Displacement vector
deformed_points[i] += displacement
# Update the cylinder's points with the deformed points
deformed_cylinder = pv.PolyData(deformed_points, faces=cylinder_polydata.faces)
# Create a plotter object
plotter = pv.Plotter()
# Add original and merged cylinders to the plotter
plotter.add_mesh(deformed_cylinder, color='blue', label='Deformed Cylinder', show_edges=True)
# Add points (nodes) to visualize the mesh structure
plotter.add_points(deformed_cylinder.points, color='red', point_size=5, render_points_as_spheres=True, label='Deformed Nodes')
# Add a legend to the plotter
plotter.add_legend()
# Show the plot with the mesh
plotter.show()
In doing so, I get a cylinder that is deformed, see image
However, the cylinder isn’t capped, and it only contains the surface.
I would like to create a closed mesh instead, so I tried using the pv.Cylinder() instead of pv.CylinderStructured. But the issue here is that the mesh lines only go vertically, and I need mesh lines that go along the polar direction.
So the result looks a bit off:
I tried creating a triangular mesh but then these gaussian deformations sometimes happen to deform the caps of the cylinder and creates all sorts of weird shapes.
Is it any way to seal off the CylinderStructured mesh by capping the top and bottom? Or is there any other efficient way to do this? I’m relatively new to PyVista and have tried finding some examples on line, but haven’t succeeded in doing so.
Thanks for all the help in advance!