Skip to content

Commit 1014b42

Browse files
committed
Adds support for rendering frames in the Notebook Viewer
Introduces functionality to visualize frames as 3D objects in the Notebook Viewer. - Adds a `frame_to_threejs` conversion utility for transforming frames into PyThreeJS objects. - Implements `ThreeFrameObject` for rendering frames in scenes, with axes visualized in red, green, and blue. - Updates the registration process to include frame objects in the Notebook Viewer. - Enhances the example notebook to demonstrate frame rendering capabilities. - Fixes a type annotation issue in `line_to_threejs` and improves the `draw` method's return type annotations for consistency. These changes enhance the visualization capabilities of the Notebook Viewer and improve type safety in the codebase.
1 parent b37388c commit 1014b42

File tree

5 files changed

+100
-25
lines changed

5 files changed

+100
-25
lines changed

notebooks/20_geometry.ipynb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"metadata": {},
77
"outputs": [],
88
"source": [
9-
"%pip install -q compas_notebook"
9+
"# /%pip install -q compas_notebook"
1010
]
1111
},
1212
{
@@ -20,6 +20,7 @@
2020
"from compas.geometry import Point\n",
2121
"from compas.geometry import Pointcloud\n",
2222
"from compas.geometry import Polyline\n",
23+
"from compas.geometry import Frame \n",
2324
"from compas_notebook.viewer import Viewer"
2425
]
2526
},
@@ -33,7 +34,16 @@
3334
"\n",
3435
"point = Point(-1, 2, 3)\n",
3536
"line = Line([0, 0, 0], point)\n",
36-
"polyline = Polyline(cloud.points)"
37+
"polyline = Polyline(cloud.points)\n",
38+
"\n",
39+
"\n",
40+
"frame = Frame(point, [1, 0, 0], [0, 1, 0])\n",
41+
"\n",
42+
"# x_line = Line(frame.point, frame.point+frame.xaxis)\n",
43+
"# y_line = Line(frame.point, frame.point+frame.yaxis)\n",
44+
"# z_line = Line(frame.point, frame.point+frame.zaxis)\n",
45+
"\n",
46+
"\n"
3747
]
3848
},
3949
{
@@ -44,7 +54,7 @@
4454
{
4555
"data": {
4656
"application/vnd.jupyter.widget-view+json": {
47-
"model_id": "a55820df515f4eb0a67494e16869aac2",
57+
"model_id": "60cefc1deea3458c9e1a390cdd0ea3ba",
4858
"version_major": 2,
4959
"version_minor": 0
5060
},
@@ -61,6 +71,7 @@
6171
"\n",
6272
"viewer.scene.add(point, color=Color.red(), pointsize=0.3)\n",
6373
"viewer.scene.add(line)\n",
74+
"viewer.scene.add(frame)\n",
6475
"viewer.scene.add(polyline, color=Color.blue())\n",
6576
"viewer.scene.add(cloud, color=Color.green(), pointsize=0.3)\n",
6677
"\n",
@@ -70,7 +81,7 @@
7081
],
7182
"metadata": {
7283
"kernelspec": {
73-
"display_name": "compas2",
84+
"display_name": "compas_opzuid",
7485
"language": "python",
7586
"name": "python3"
7687
},
@@ -84,7 +95,7 @@
8495
"name": "python",
8596
"nbconvert_exporter": "python",
8697
"pygments_lexer": "ipython3",
87-
"version": "3.12.1"
98+
"version": "3.12.12"
8899
}
89100
},
90101
"nbformat": 4,

src/compas_notebook/conversions/geometry.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import numpy
23
import pythreejs as three
34
from compas.geometry import Box
@@ -8,9 +9,11 @@
89
from compas.geometry import Polyline
910
from compas.geometry import Sphere
1011
from compas.geometry import Torus
12+
from compas.geometry import Frame
13+
from compas.geometry import Line
1114

1215

13-
def line_to_threejs(line: Point) -> three.BufferGeometry:
16+
def line_to_threejs(line: Line) -> three.BufferGeometry:
1417
"""Convert a COMPAS line to PyThreeJS.
1518
1619
Parameters
@@ -28,6 +31,39 @@ def line_to_threejs(line: Point) -> three.BufferGeometry:
2831
return geometry
2932

3033

34+
def frame_to_threejs(frame: Frame) -> list[three.BufferGeometry]:
35+
"""Convert a COMPAS frame to PyThreeJS.
36+
37+
Parameters
38+
----------
39+
frame : :class:`compas.geometry.Frame`
40+
The frame to convert.
41+
42+
Returns
43+
-------
44+
list[three.BufferGeometry]
45+
46+
"""
47+
48+
# create lines for each axis
49+
_x_line = Line(frame.point, frame.point + frame.xaxis)
50+
_y_line = Line(frame.point, frame.point + frame.yaxis)
51+
_z_line = Line(frame.point, frame.point + frame.zaxis)
52+
53+
# convert lines to threejs vertex buffers
54+
xline_verts = line_to_threejs(_x_line)
55+
yline_verts = line_to_threejs(_y_line)
56+
zline_verts = line_to_threejs(_z_line)
57+
58+
# convert from vertex buffers to line objects
59+
xline_lines = three.Line(xline_verts, three.LineBasicMaterial(color="red"))
60+
yline_lines = three.Line(yline_verts, three.LineBasicMaterial(color="green"))
61+
zline_lines = three.Line(zline_verts, three.LineBasicMaterial(color="blue"))
62+
63+
result = [xline_lines, yline_lines, zline_lines]
64+
return result
65+
66+
3167
def point_to_threejs(point: Point) -> three.SphereGeometry:
3268
"""Convert a COMPAS point to PyThreeJS.
3369

src/compas_notebook/scene/__init__.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from compas.geometry import Cone
1414
from compas.geometry import Cylinder
1515
from compas.geometry import Line
16+
from compas.geometry import Frame
1617
from compas.geometry import Point
1718
from compas.geometry import Pointcloud
1819
from compas.geometry import Polygon
@@ -46,6 +47,7 @@
4647
from .meshobject import ThreeMeshObject
4748

4849
from .groupobject import ThreeGroupObject
50+
from .frameobject import ThreeFrameObject
4951

5052

5153
@plugin(category="drawing-utils", pluggable_name="clear", requires=["pythreejs"])
@@ -75,6 +77,7 @@ def register_scene_objects():
7577
register(Cone, ThreeConeObject, context="Notebook")
7678
register(Cylinder, ThreeCylinderObject, context="Notebook")
7779
register(Graph, ThreeGraphObject, context="Notebook")
80+
register(Frame, ThreeFrameObject, context="Notebook")
7881
register(Line, ThreeLineObject, context="Notebook")
7982
register(Point, ThreePointObject, context="Notebook")
8083
register(Pointcloud, ThreePointcloudObject, context="Notebook")
@@ -86,20 +89,20 @@ def register_scene_objects():
8689
register(Mesh, ThreeMeshObject, context="Notebook")
8790
register(list, ThreeGroupObject, context="Notebook")
8891

89-
90-
__all__ = [
91-
"NotebookScene",
92-
"ThreeBoxObject",
93-
"ThreeCapsuleObject",
94-
"ThreeConeObject",
95-
"ThreeCylinderObject",
96-
"ThreeGraphObject",
97-
"ThreePointObject",
98-
"ThreePointcloudObject",
99-
"ThreePolygonObject",
100-
"ThreePolyhedronObject",
101-
"ThreePolylineObject",
102-
"ThreeSceneObject",
103-
"ThreeSphereObject",
104-
"ThreeTorusObject",
105-
]
92+
# # yuck java
93+
# __all__ = [
94+
# "NotebookScene",
95+
# "ThreeBoxObject",
96+
# "ThreeCapsuleObject",
97+
# "ThreeConeObject",
98+
# "ThreeCylinderObject",
99+
# "ThreeGraphObject",
100+
# "ThreePointObject",
101+
# "ThreePointcloudObject",
102+
# "ThreePolygonObject",
103+
# "ThreePolyhedronObject",
104+
# "ThreePolylineObject",
105+
# "ThreeSceneObject",
106+
# "ThreeSphereObject",
107+
# "ThreeTorusObject",
108+
# ]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pythreejs as three
2+
from compas.scene import GeometryObject
3+
4+
from compas_notebook.conversions.geometry import frame_to_threejs
5+
6+
from .sceneobject import ThreeSceneObject
7+
8+
9+
class ThreeFrameObject(ThreeSceneObject, GeometryObject):
10+
"""Scene object for drawing frames"""
11+
12+
def draw(self):
13+
"""Draw the frame associated with the scene object as a set of lines: x-axis in red, y-axis in green, z-axis in blue.
14+
15+
Returns
16+
-------
17+
list[three.Line]
18+
List of pythreejs objects created.
19+
20+
"""
21+
22+
23+
self._guids = frame_to_threejs(self.geometry)
24+
25+
return self.guids

src/compas_notebook/scene/lineobject.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
class ThreeLineObject(ThreeSceneObject, GeometryObject):
1010
"""Scene object for drawing line."""
1111

12-
def draw(self):
13-
"""Draw the line associated with the scene object.
12+
def draw(self)-> list[three.Line]:
13+
"""Draw the frame associated with the scene object.
1414
1515
Returns
1616
-------

0 commit comments

Comments
 (0)