Skip to content
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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
508 changes: 105 additions & 403 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ classifiers = [
# pycollada is not working in Python 3.9. See
# https://github.com/lace/tri-again/issues/25
python = ">=3.7,<3.9"
lacecore = ">=3.0.0a0"
numpy = "*"
polliwog = ">=2.0.0,<=3.0.0"
polliwog = ">=3.0.0a4"
pycollada = ">=0.7.1,<0.8"
toolz = ">=0.10.0,<0.12.0"
vg = ">=2.0.0"
Expand All @@ -39,7 +40,6 @@ coverage = "5.5"
executor = "23.2"
flake8 = "3.9.2"
flake8-import-order = "0.18.1"
lacecore = "2.0.0"
myst-parser = "0.15.1"
pytest = "6.2.4"
pytest-cov = "2.12.1"
Expand Down
15 changes: 14 additions & 1 deletion tri_again/_collada.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Mesh as InternalMesh,
Point as InternalPoint,
)
from ._shapes import sphere


def create_material(collada, name, color=(1, 1, 1)):
Expand Down Expand Up @@ -144,11 +145,23 @@ def collada_from_scene(scene, name="triagain"):
[
child
for child in scene.children
if isinstance(child, InternalPoint)
if isinstance(child, InternalPoint) and child.radius is None
],
).items()
)
]
+ [
geometry_node_from_mesh(
collada=collada,
mesh=sphere(radius=child.radius, center=child.point),
name=f"point_as_sphere_{i}",
)
for i, child in enumerate(
child
for child in scene.children
if isinstance(child, InternalPoint) and child.radius is not None
)
]
)

scene = Scene(name, [Node("root", children=geometry_nodes)])
Expand Down
11 changes: 11 additions & 0 deletions tri_again/_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ def add_points(self, *points, name=None, color="red"):
self.children.append(Point(point, name=name, color=color))
return self

def add_points_as_spheres(self, *points, radius=None, name=None, color="red"):
if len(points) > 0 and name is not None:
raise ValueError(
"When more than one point is provided, expected `name` to be None"
)
if radius is None:
raise ValueError("radius is required")
for point in points:
self.children.append(Point(point, radius=radius, name=name, color=color))
return self

def write(self, filename):
"""
Write a COLLADA file for this scene.
Expand Down
3 changes: 2 additions & 1 deletion tri_again/_scene_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ def __init__(self, polyline, color):


class Point(Object):
def __init__(self, point, color, name=None):
def __init__(self, point, color, radius=None, name=None):
# Validate.
normalize_color(color)

self.point = point
self.name = name
self.radius = radius
self.color = color
100 changes: 100 additions & 0 deletions tri_again/_shapes.py
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.
Copy link
Member Author

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

]
)
if project_to_unit_sphere:
new_v = vg.normalize(new_v)
Copy link
Member Author

Choose a reason for hiding this comment

The 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):
Copy link
Member Author

Choose a reason for hiding this comment

The 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()
)
6 changes: 6 additions & 0 deletions tri_again/test_collada.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ def test_example():
.add_points(
np.array([0.0, 0.0, 0.6]), np.array([5.0, 0.0, 0.2]), color="SeaGreen"
)
.add_points_as_spheres(
np.array([0.0, 0.0, 0.9]),
np.array([5.0, 0.0, 0.3]),
radius=0.1,
color="blue",
)
)
scene.write("example.dae")

Expand Down
59 changes: 59 additions & 0 deletions tri_again/test_shapes.py
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")