Skip to content

Commit

Permalink
make sure subdivisions=0 still refines radius
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedh committed Mar 31, 2024
1 parent 0785d8f commit 58b259e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/test_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,16 @@ def test_sphere_subdivisions(self):

assert a.faces.shape == b.faces.shape

def test_sphere_subdivisions_radius(self):
tf = list(g.random_transforms(4))
for r in [1e-4, 1.1, 3, 1213.22]:
for subd in range(4):
s = g.trimesh.primitives.Sphere(
radius=r, transform=tf[subd], subdivisions=subd
)
rc = g.np.linalg.norm(s.vertices - s.center_mass, axis=1)
assert g.np.isclose(rc.mean(), r)


if __name__ == "__main__":
g.trimesh.util.attach_to_log()
Expand Down
10 changes: 10 additions & 0 deletions trimesh/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,16 +855,26 @@ def icosphere(subdivisions: Integer = 3, radius: Number = 1.0, **kwargs):
ico : trimesh.Trimesh
Meshed sphere
"""
radius = float(radius)
subdivisions = int(subdivisions)

ico = icosahedron()
ico._validate = False

for _ in range(subdivisions):
ico = ico.subdivide()
vectors = ico.vertices
scalar = np.sqrt(np.dot(vectors**2, [1, 1, 1]))
unit = vectors / scalar.reshape((-1, 1))
ico.vertices += unit * (radius - scalar).reshape((-1, 1))

# if we didn't subdivide we still need to refine the radius
if subdivisions <= 0:
vectors = ico.vertices
scalar = np.sqrt(np.dot(vectors**2, [1, 1, 1]))
unit = vectors / scalar.reshape((-1, 1))
ico.vertices += unit * (radius - scalar).reshape((-1, 1))

if "color" in kwargs:
warnings.warn(
"`icosphere(color=...)` is deprecated and will "
Expand Down

0 comments on commit 58b259e

Please sign in to comment.