Skip to content

Commit

Permalink
[geom] Add per_vertex_thickness to to_spline()
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Holl committed Jan 8, 2025
1 parent 1cbfc88 commit c71166b
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions phi/geom/_spline_solid.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,26 +306,34 @@ def __add__(self, other):
return NotImplemented


def to_spline(geo: Geometry, /, variable_attrs=('points', 'thickness', 'fillet'), value_attrs=(), rel_separation=1e-5) -> SplineSolid:
def to_spline(geo: Geometry, /, per_vertex_thickness=True, variable_attrs=('points', 'thickness', 'fillet'), value_attrs=(), rel_separation=1e-5) -> SplineSolid:
assert geo.spatial_rank == 3, f"SplineSolid must be fit to 3D geometry but got {geo}"
if isinstance(geo, Cylinder):
tips = dpack(geo.face_centers['bottom,top'], 'u:s')
right = orthogonal_vector(geo.up)
side_eps = geo.depth * rel_separation * right * wrap([-1, 1], 'v:s')
points = tips + side_eps
return SplineSolid(points, geo.radius * 2, {'u-': wrap(0.), 'u+': wrap(0.), 'v-': wrap(1.), 'v+': wrap(1.)}, {'u': 1, 'v': 1}, variable_attrs, value_attrs)
thickness = geo.radius * 2
if per_vertex_thickness:
thickness = expand(thickness, spatial(points))
return SplineSolid(points, thickness, {'u-': wrap(0.), 'u+': wrap(0.), 'v-': wrap(1.), 'v+': wrap(1.)}, {'u': 1, 'v': 1}, variable_attrs, value_attrs)
elif isinstance(geo, BaseBox):
thickness, th_idx = math.min((geo.size, range), 'vector', key=geo.size)
u_idx, v_idx = (th_idx + 1) % 3, (th_idx + 2) % 3
vu, vv = geo.rotation_matrix[{'~vector': u_idx}], geo.rotation_matrix[{'~vector': v_idx}]
su, sv = geo.size.vector[u_idx], geo.size.vector[v_idx]
u = vu * su * wrap([-.5, .5], 'u:s')
v = vv * sv * wrap([-.5, .5], 'v:s')
return SplineSolid(geo.center + u + v, thickness, {'u-': wrap(0.), 'u+': wrap(0.), 'v-': wrap(0.), 'v+': wrap(0.)}, {'u': 1, 'v': 1}, variable_attrs, value_attrs)
points = geo.center + u + v
if per_vertex_thickness:
thickness = expand(thickness, spatial(points))
return SplineSolid(points, thickness, {'u-': wrap(0.), 'u+': wrap(0.), 'v-': wrap(0.), 'v+': wrap(0.)}, {'u': 1, 'v': 1}, variable_attrs, value_attrs)
elif isinstance(geo, Sphere):
thickness = geo.radius * 2
u, v = (rel_separation * thickness * math.meshgrid(u=2, v=2)).vector
points = geo.center + wrap([u, v, 0], geo.shape['vector'])
if per_vertex_thickness:
thickness = expand(thickness, spatial(points))
return SplineSolid(points, thickness, {'u-': wrap(1.), 'u+': wrap(1.), 'v-': wrap(1.), 'v+': wrap(1.)}, {'u': 1, 'v': 1}, variable_attrs, value_attrs)
else:
raise NotImplementedError(type(geo))
Expand Down

0 comments on commit c71166b

Please sign in to comment.