-
Notifications
You must be signed in to change notification settings - Fork 0
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
Show points as spheres #56
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import math | ||
from lacecore import Mesh | ||
import numpy as np | ||
from vg.compat import v2 as vg | ||
|
||
# Adapted from https://medium.com/@oscarsc/four-ways-to-create-a-mesh-for-a-sphere-d7956b825db4 | ||
|
||
|
||
def subdivide(mesh, project_to_unit_sphere=False): | ||
""" | ||
Args: | ||
project_to_unit_sphere (boolean): When True, project new vertices to the | ||
unit sphere. This is useful for `sphere()` below. | ||
""" | ||
triangles = mesh.v[mesh.f] | ||
new_v = np.concatenate( | ||
[ | ||
np.average(triangles[:, 0:2], axis=1), # Midpoint of AB. | ||
np.average(triangles[:, 1:3], axis=1), # Midpoint of BC. | ||
np.average(triangles[:, 0:3:2], axis=1), # Midpoint of AC. | ||
] | ||
) | ||
if project_to_unit_sphere: | ||
new_v = vg.normalize(new_v) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this. Normalize verts at the end, after repeated calls to subdivide(). |
||
new_v = np.concatenate([mesh.v, new_v]) | ||
|
||
new_f = np.zeros((0, 3), dtype=np.int64) | ||
for i, (a, b, c) in enumerate(mesh.f): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: vectorize |
||
# d: First midpoint of AB. | ||
# e: First midpoint of BC. | ||
# f: First midpoint of AC. | ||
d, e, f = mesh.num_v + np.arange(3) * len(triangles) + i | ||
new_f = np.vstack( | ||
[ | ||
new_f, | ||
[a, d, f], | ||
[d, b, e], | ||
[e, c, f], | ||
[d, e, f], | ||
] | ||
) | ||
return Mesh(v=new_v, f=new_f) | ||
|
||
|
||
def sphere(center, radius): | ||
vg.shape.check(locals(), "center", (3,)) | ||
|
||
t = (1 + math.sqrt(5)) / 2 | ||
vertices = vg.normalize( | ||
np.array( | ||
[ | ||
[-1, t, 0], | ||
[1, t, 0], | ||
[-1, -t, 0], | ||
[1, -t, 0], | ||
[0, -1, t], | ||
[0, 1, t], | ||
[0, -1, -t], | ||
[0, 1, -t], | ||
[t, 0, -1], | ||
[t, 0, 1], | ||
[-t, 0, -1], | ||
[-t, 0, 1], | ||
] | ||
) | ||
) | ||
faces = np.array( | ||
[ | ||
[0, 11, 5], | ||
[0, 5, 1], | ||
[0, 1, 7], | ||
[0, 7, 10], | ||
[0, 10, 11], | ||
[1, 5, 9], | ||
[5, 11, 4], | ||
[11, 10, 2], | ||
[10, 7, 6], | ||
[7, 1, 8], | ||
[3, 9, 4], | ||
[3, 4, 2], | ||
[3, 2, 6], | ||
[3, 6, 8], | ||
[3, 8, 9], | ||
[4, 9, 5], | ||
[2, 4, 11], | ||
[6, 2, 10], | ||
[8, 6, 7], | ||
[9, 8, 1], | ||
] | ||
) | ||
return ( | ||
subdivide( | ||
subdivide(Mesh(v=vertices, f=faces), project_to_unit_sphere=True), | ||
project_to_unit_sphere=True, | ||
) | ||
.transform() | ||
.uniform_scale(radius) | ||
.translate(center) | ||
.end() | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from lacecore import Mesh, shapes | ||
import numpy as np | ||
from ._shapes import sphere as create_sphere, subdivide | ||
from . import Scene | ||
|
||
|
||
def test_subdivide_square(): | ||
cube = shapes.cube(np.zeros(3), 1.0) | ||
just_a_square = Mesh(v=cube.v[:4], f=cube.f[:2]) | ||
subdivided = subdivide(just_a_square) | ||
|
||
Scene().add_meshes(subdivided).write("subdivided_square.dae") | ||
|
||
np.testing.assert_array_equal( | ||
subdivided.v, | ||
np.array( | ||
[ | ||
[0.0, 0.0, 0.0], | ||
[1.0, 0.0, 0.0], | ||
[1.0, 0.0, 1.0], | ||
[0.0, 0.0, 1.0], | ||
[0.5, 0.0, 0.0], | ||
[0.5, 0.0, 0.5], | ||
[1.0, 0.0, 0.5], | ||
[0.5, 0.0, 1.0], | ||
[0.5, 0.0, 0.5], | ||
[0.0, 0.0, 0.5], | ||
] | ||
), | ||
) | ||
np.testing.assert_array_equal( | ||
subdivided.f, | ||
np.array( | ||
[ | ||
[0, 4, 8], | ||
[4, 1, 6], | ||
[6, 2, 8], | ||
[4, 6, 8], | ||
[0, 5, 9], | ||
[5, 2, 7], | ||
[7, 3, 9], | ||
[5, 7, 9], | ||
] | ||
), | ||
) | ||
|
||
|
||
def test_subdivide_cube(): | ||
cube = shapes.cube(np.zeros(3), 1.0) | ||
subdivided = subdivide(cube) | ||
|
||
Scene().add_meshes(subdivided).write("subdivided_cube.dae") | ||
|
||
|
||
def test_sphere(): | ||
sphere = create_sphere(np.zeros(3), 1.0) | ||
subdivided = subdivide(sphere) | ||
|
||
Scene().add_meshes(subdivided).write("sphere.dae") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Todo: avoid duplicating points for repeated edges