Skip to content

Commit e3a217b

Browse files
committed
use hot for geometry
1 parent 6e7dc9f commit e3a217b

File tree

8 files changed

+646
-121
lines changed

8 files changed

+646
-121
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import numpy as np
2+
import pyvista as pv
3+
import trimesh
4+
from pydantic import BaseModel, Field
5+
from tesseract_core.runtime import Array, Differentiable, Float32
6+
7+
#
8+
# Schemata
9+
#
10+
11+
12+
class InputSchema(BaseModel):
13+
"""Input schema for bar geometry design and SDF generation."""
14+
15+
differentiable_parameters: Differentiable[
16+
Array[
17+
(None,),
18+
Float32,
19+
]
20+
] = Field(
21+
description=(
22+
"Vertex positions of the bar geometry. "
23+
"The shape is (num_bars, num_vertices, 3), where num_bars is the number of bars "
24+
"and num_vertices is the number of vertices per bar. The last dimension represents "
25+
"the x, y, z coordinates of each vertex."
26+
)
27+
)
28+
29+
non_differentiable_parameters: Array[
30+
(None,),
31+
Float32,
32+
] = Field(description="Flattened array of non-differentiable geometry parameters.")
33+
34+
geometry_ints: list[int] = Field(
35+
description=(
36+
"List of integers used to construct the geometry."
37+
" The first integer is the number of bars, and the second integer is the number of vertices per bar."
38+
)
39+
)
40+
41+
42+
class TriangularMesh(BaseModel):
43+
"""Triangular mesh representation with fixed-size arrays."""
44+
45+
points: Array[(None, 3), Float32] = Field(description="Array of vertex positions.")
46+
faces: Array[(None, 3), Float32] = Field(
47+
description="Array of triangular faces defined by indices into the points array."
48+
)
49+
50+
51+
class OutputSchema(BaseModel):
52+
"""Output schema for generated geometry and SDF field."""
53+
54+
mesh: TriangularMesh = Field(
55+
description="Triangular mesh representation of the geometry"
56+
)
57+
58+
59+
#
60+
# Helper functions
61+
#
62+
63+
64+
def build_geometry(
65+
differentiable_parameters: np.ndarray,
66+
non_differentiable_parameters: np.ndarray,
67+
geometry_ints: list[int],
68+
) -> list[trimesh.Trimesh]:
69+
"""Build a pyvista geometry from the parameters.
70+
71+
The parameters are expected to be of shape (n_chains, n_edges_per_chain + 1, 3),
72+
"""
73+
n_chains = geometry_ints[0]
74+
n_vertices_per_chain = geometry_ints[1]
75+
geometry = []
76+
77+
params = differentiable_parameters.reshape((n_chains, n_vertices_per_chain, 3))
78+
radius = non_differentiable_parameters[0]
79+
80+
for chain in range(n_chains):
81+
tube = pv.Spline(points=params[chain]).tube(
82+
radius=radius, capping=True, n_sides=30
83+
)
84+
tube = tube.triangulate()
85+
tube = pyvista_to_trimesh(tube)
86+
geometry.append(tube)
87+
88+
# convert each geometry in a trimesh style mesh and combine them
89+
mesh = geometry[0]
90+
91+
for geom in geometry[1:]:
92+
mesh = mesh.union(geom)
93+
94+
return mesh
95+
96+
97+
def pyvista_to_trimesh(mesh: pv.PolyData) -> trimesh.Trimesh:
98+
"""Convert a pyvista mesh to a trimesh style polygon mesh."""
99+
points = mesh.points
100+
points_per_face = mesh.faces[0]
101+
n_faces = mesh.faces.shape[0] // (points_per_face + 1)
102+
103+
faces = mesh.faces.reshape(n_faces, (points_per_face + 1))[:, 1:]
104+
105+
return trimesh.Trimesh(vertices=points, faces=faces)
106+
107+
108+
#
109+
# Tesseract endpoints
110+
#
111+
112+
113+
def apply(inputs: InputSchema) -> OutputSchema:
114+
"""Generate mesh and SDF from bar geometry parameters.
115+
116+
Args:
117+
inputs: Input schema containing bar geometry parameters.
118+
119+
Returns:
120+
Output schema with generated mesh and SDF field.
121+
"""
122+
mesh = build_geometry(
123+
differentiable_parameters=inputs.differentiable_parameters,
124+
non_differentiable_parameters=inputs.non_differentiable_parameters,
125+
geometry_ints=inputs.geometry_ints,
126+
)
127+
128+
print(mesh)
129+
130+
return OutputSchema(
131+
mesh=TriangularMesh(
132+
points=mesh.vertices.astype(np.float32),
133+
faces=mesh.faces.astype(np.int32),
134+
),
135+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: design-bars-3d
2+
version: "0.1.0"
3+
description: |
4+
Tesseract that generates 3D bar geometry.
5+
6+
Parameters are expected to define the control points and radii of piecewise linear tubes in 3D space.
7+
8+
build_config:
9+
target_platform: "linux/x86_64"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
numpy==1.26.4
2+
pyvista==0.45.2
3+
trimesh==4.9.0

examples/ansys/demo_2.ipynb

Lines changed: 406 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)