Skip to content

Commit c3d397e

Browse files
committed
fix up following code review
1 parent 3aff457 commit c3d397e

File tree

4 files changed

+52
-134
lines changed

4 files changed

+52
-134
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
* Added support for `frames, circle, ellipse, vector, plane, curve, analystical surface primitives`
13+
1214
### Changed
1315

1416
### Removed

notebooks/20_geometry.ipynb

Lines changed: 38 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,58 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": null,
5+
"execution_count": 2,
66
"metadata": {},
77
"outputs": [],
8-
"source": "from compas.colors import Color\nfrom compas.geometry import Circle\nfrom compas.geometry import Ellipse\nfrom compas.geometry import Frame\nfrom compas.geometry import Line\nfrom compas.geometry import Plane\nfrom compas.geometry import Point\nfrom compas.geometry import Pointcloud\nfrom compas.geometry import Polyline\nfrom compas.geometry import Vector\nfrom compas.geometry import SphericalSurface\nfrom compas.geometry import CylindricalSurface\nfrom compas_notebook.viewer import Viewer"
8+
"source": [
9+
"from compas.colors import Color\n",
10+
"from compas.geometry import Circle\n",
11+
"from compas.geometry import Ellipse\n",
12+
"from compas.geometry import Frame\n",
13+
"from compas.geometry import Line\n",
14+
"from compas.geometry import Plane\n",
15+
"from compas.geometry import Point\n",
16+
"from compas.geometry import Pointcloud\n",
17+
"from compas.geometry import Polyline\n",
18+
"from compas.geometry import Vector\n",
19+
"from compas.geometry import SphericalSurface\n",
20+
"from compas.geometry import CylindricalSurface\n",
21+
"from compas_notebook.viewer import Viewer"
22+
]
923
},
1024
{
1125
"cell_type": "code",
12-
"execution_count": 2,
26+
"execution_count": 5,
1327
"metadata": {},
1428
"outputs": [],
1529
"source": [
16-
"cloud = Pointcloud.from_bounds(x=8, y=5, z=3, n=13)\n",
17-
"\n",
1830
"point = Point(-1, 2, 3)\n",
31+
"vector = Vector(1, 1, 2)\n",
32+
"plane = Plane([0, 0, -1], [0, 0, 1])\n",
33+
"frame = Frame(point, [1, 0, 0], [0, 1, 0])\n",
34+
"\n",
1935
"line = Line([0, 0, 0], point)\n",
36+
"cloud = Pointcloud.from_bounds(x=8, y=5, z=3, n=13)\n",
2037
"polyline = Polyline(cloud.points)\n",
2138
"\n",
39+
"# quadric curves\n",
40+
"circle = Circle(radius=1.5, frame=Frame([2, 0, 0], [0, 1, 0], [0, 0, 1]))\n",
41+
"ellipse = Ellipse(major=2.0, minor=1.0, frame=Frame([5, 0, 0], [0, 1, 0], [0, 0, 1]))\n",
2242
"\n",
23-
"frame = Frame(point, [1, 0, 0], [0, 1, 0])\n",
24-
"\n",
25-
"# x_line = Line(frame.point, frame.point+frame.xaxis)\n",
26-
"# y_line = Line(frame.point, frame.point+frame.yaxis)\n",
27-
"# z_line = Line(frame.point, frame.point+frame.zaxis)\n",
28-
"\n",
29-
"\n"
43+
"# Create analytical surfaces\n",
44+
"sphere_surface = SphericalSurface(radius=1.5, frame=Frame([0, 0, 0], [1, 0, 0], [0, 1, 0]))\n",
45+
"cylinder_surface = CylindricalSurface(radius=0.8, frame=Frame([3, 0, 0], [1, 0, 0], [0, 1, 0]))"
3046
]
3147
},
3248
{
3349
"cell_type": "code",
34-
"execution_count": 3,
50+
"execution_count": 7,
3551
"metadata": {},
3652
"outputs": [
3753
{
3854
"data": {
3955
"application/vnd.jupyter.widget-view+json": {
40-
"model_id": "0498e1e0a2e3459480997d957fbfc9da",
56+
"model_id": "d32833ac778148c0920dfa0e9b672076",
4157
"version_major": 2,
4258
"version_minor": 0
4359
},
@@ -57,100 +73,20 @@
5773
"viewer.scene.add(frame)\n",
5874
"viewer.scene.add(polyline, color=Color.blue())\n",
5975
"viewer.scene.add(cloud, color=Color.green(), pointsize=0.3)\n",
76+
"viewer.scene.add(circle, color=Color.red())\n",
77+
"viewer.scene.add(ellipse, color=Color.green())\n",
78+
"viewer.scene.add(vector)\n",
79+
"viewer.scene.add(plane)\n",
80+
"viewer.scene.add(sphere_surface, color=Color.cyan())\n",
81+
"viewer.scene.add(cylinder_surface, color=Color.magenta())\n",
6082
"\n",
6183
"viewer.show()"
6284
]
63-
},
64-
{
65-
"cell_type": "code",
66-
"execution_count": null,
67-
"metadata": {},
68-
"outputs": [],
69-
"source": []
70-
},
71-
{
72-
"cell_type": "markdown",
73-
"metadata": {},
74-
"source": [
75-
"# New Primitives\n",
76-
"\n",
77-
"Demonstration of recently added primitive support: Circle, Ellipse, Vector, and Plane."
78-
]
79-
},
80-
{
81-
"cell_type": "code",
82-
"execution_count": 4,
83-
"metadata": {},
84-
"outputs": [],
85-
"source": [
86-
"# Create new primitives\n",
87-
"circle = Circle(radius=1.5, frame=Frame([2, 0, 0], [0, 1, 0], [0, 0, 1]))\n",
88-
"ellipse = Ellipse(major=2.0, minor=1.0, frame=Frame([5, 0, 0], [0, 1, 0], [0, 0, 1]))\n",
89-
"vector = Vector(1, 1, 2)\n",
90-
"plane = Plane([0, 0, -1], [0, 0, 1])"
91-
]
92-
},
93-
{
94-
"cell_type": "code",
95-
"execution_count": 5,
96-
"metadata": {},
97-
"outputs": [
98-
{
99-
"data": {
100-
"application/vnd.jupyter.widget-view+json": {
101-
"model_id": "3cac0c93ba69483fb0534923124dbe15",
102-
"version_major": 2,
103-
"version_minor": 0
104-
},
105-
"text/plain": [
106-
"VBox(children=(HBox(children=(Button(icon='search-plus', layout=Layout(height='32px', width='48px'), style=But…"
107-
]
108-
},
109-
"metadata": {},
110-
"output_type": "display_data"
111-
}
112-
],
113-
"source": [
114-
"viewer2 = Viewer()\n",
115-
"\n",
116-
"viewer2.scene.add(circle, color=Color.red())\n",
117-
"viewer2.scene.add(ellipse, color=Color.green())\n",
118-
"viewer2.scene.add(vector)\n",
119-
"viewer2.scene.add(plane)\n",
120-
"\n",
121-
"viewer2.show()"
122-
]
123-
},
124-
{
125-
"cell_type": "code",
126-
"execution_count": null,
127-
"metadata": {},
128-
"outputs": [],
129-
"source": []
130-
},
131-
{
132-
"cell_type": "markdown",
133-
"source": "# Surfaces\n\nAnalytical surfaces work out of the box.",
134-
"metadata": {}
135-
},
136-
{
137-
"cell_type": "code",
138-
"source": "# Create analytical surfaces\nsphere_surface = SphericalSurface(radius=1.5, frame=Frame([0, 0, 0], [1, 0, 0], [0, 1, 0]))\ncylinder_surface = CylindricalSurface(radius=0.8, frame=Frame([3, 0, 0], [1, 0, 0], [0, 1, 0]))",
139-
"metadata": {},
140-
"execution_count": null,
141-
"outputs": []
142-
},
143-
{
144-
"cell_type": "code",
145-
"source": "viewer3 = Viewer()\n\nviewer3.scene.add(sphere_surface, color=Color.cyan())\nviewer3.scene.add(cylinder_surface, color=Color.magenta())\n\nviewer3.show()",
146-
"metadata": {},
147-
"execution_count": null,
148-
"outputs": []
14985
}
15086
],
15187
"metadata": {
15288
"kernelspec": {
153-
"display_name": "Python 3 (ipykernel)",
89+
"display_name": "compas_opzuid",
15490
"language": "python",
15591
"name": "python3"
15692
},
@@ -169,4 +105,4 @@
169105
},
170106
"nbformat": 4,
171107
"nbformat_minor": 4
172-
}
108+
}

src/compas_notebook/conversions/geometry.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
import math
23
import numpy
34
import pythreejs as three
45
from compas.geometry import Box
@@ -17,6 +18,8 @@
1718
from compas.geometry import Surface
1819
from compas.geometry import Torus
1920
from compas.geometry import Vector
21+
from compas_notebook.conversions.meshes import vertices_and_edges_to_threejs
22+
from compas_notebook.conversions.meshes import vertices_and_faces_to_threejs
2023

2124

2225
def line_to_threejs(line: Line) -> three.BufferGeometry:
@@ -32,7 +35,7 @@ def line_to_threejs(line: Line) -> three.BufferGeometry:
3235
:class:`three.BufferGeometry`
3336
3437
"""
35-
vertices = numpy.array([line.start, line.end], dtype=numpy.float32)
38+
vertices = numpy.array([line.start, line.end], dtype=numpy.float64)
3639
geometry = three.BufferGeometry(attributes={"position": three.BufferAttribute(vertices, normalized=False)})
3740
return geometry
3841

@@ -53,8 +56,8 @@ def frame_to_threejs(frame: Frame) -> list[three.BufferGeometry]:
5356

5457
# create lines for each axis
5558
_x_line = Line(frame.point, frame.point + frame.xaxis)
56-
_y_line = Line(frame.point, frame.point + frame.yaxis)
57-
_z_line = Line(frame.point, frame.point + frame.zaxis)
59+
_y_line = Line.from_point_and_vector(frame.point, frame.yaxis)
60+
_z_line = Line.from_point_and_vector(frame.point, frame.zaxis)
5861

5962
# convert lines to threejs vertex buffers
6063
xline_verts = line_to_threejs(_x_line)
@@ -90,7 +93,7 @@ def point_to_threejs(point: Point) -> three.SphereGeometry:
9093
BufferGeometry(...)
9194
9295
"""
93-
vertices = numpy.array([point], dtype=numpy.float32)
96+
vertices = numpy.array([point], dtype=numpy.float64)
9497
geometry = three.BufferGeometry(attributes={"position": three.BufferAttribute(vertices, normalized=False)})
9598
return geometry
9699

@@ -112,7 +115,7 @@ def pointcloud_to_threejs(pointcloud: Pointcloud) -> three.SphereGeometry:
112115
>>>
113116
114117
"""
115-
vertices = numpy.array(pointcloud.points, dtype=numpy.float32)
118+
vertices = numpy.array(pointcloud.points, dtype=numpy.float64)
116119
geometry = three.BufferGeometry(attributes={"position": three.BufferAttribute(vertices, normalized=False)})
117120
return geometry
118121

@@ -130,7 +133,7 @@ def polyline_to_threejs(polyline: Polyline) -> three.BufferGeometry:
130133
:class:`three.BufferGeometry`
131134
132135
"""
133-
vertices = numpy.array(polyline.points, dtype=numpy.float32)
136+
vertices = numpy.array(polyline.points, dtype=numpy.float64)
134137
geometry = three.BufferGeometry(attributes={"position": three.BufferAttribute(vertices, normalized=False)})
135138
return geometry
136139

@@ -150,11 +153,10 @@ def circle_to_threejs(circle: Circle, max_angle: float = 5.0) -> three.BufferGeo
150153
:class:`three.BufferGeometry`
151154
152155
"""
153-
import math
154156

155157
n = max(8, int(math.ceil(360.0 / max_angle)))
156158
polyline = circle.to_polyline(n=n)
157-
vertices = numpy.array(polyline.points, dtype=numpy.float32)
159+
vertices = numpy.array(polyline.points, dtype=numpy.float64)
158160
geometry = three.BufferGeometry(attributes={"position": three.BufferAttribute(vertices, normalized=False)})
159161
return geometry
160162

@@ -174,11 +176,9 @@ def ellipse_to_threejs(ellipse: Ellipse, max_angle: float = 5.0) -> three.Buffer
174176
:class:`three.BufferGeometry`
175177
176178
"""
177-
import math
178-
179179
n = max(8, int(math.ceil(360.0 / max_angle)))
180180
polyline = ellipse.to_polyline(n=n)
181-
vertices = numpy.array(polyline.points, dtype=numpy.float32)
181+
vertices = numpy.array(polyline.points, dtype=numpy.float64)
182182
geometry = three.BufferGeometry(attributes={"position": three.BufferAttribute(vertices, normalized=False)})
183183
return geometry
184184

@@ -326,7 +326,7 @@ def curve_to_threejs(curve: Curve, resolution: int = 100) -> three.BufferGeometr
326326
327327
"""
328328
polyline = curve.to_polyline(n=resolution)
329-
vertices = numpy.array(polyline.points, dtype=numpy.float32)
329+
vertices = numpy.array(polyline.points, dtype=numpy.float64)
330330
geometry = three.BufferGeometry(attributes={"position": three.BufferAttribute(vertices, normalized=False)})
331331
return geometry
332332

@@ -349,9 +349,6 @@ def surface_to_threejs(surface: Surface, resolution_u: int = 20, resolution_v: i
349349
Mesh and edge visualization.
350350
351351
"""
352-
from compas_notebook.conversions.meshes import vertices_and_edges_to_threejs
353-
from compas_notebook.conversions.meshes import vertices_and_faces_to_threejs
354-
355352
vertices, faces = surface.to_vertices_and_faces(nu=resolution_u, nv=resolution_v)
356353

357354
# Create faces

src/compas_notebook/scene/__init__.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,3 @@ def register_scene_objects():
106106
register(Vector, ThreeVectorObject, context="Notebook")
107107
register(list, ThreeGroupObject, context="Notebook")
108108

109-
# # yuck java
110-
# __all__ = [
111-
# "NotebookScene",
112-
# "ThreeBoxObject",
113-
# "ThreeCapsuleObject",
114-
# "ThreeConeObject",
115-
# "ThreeCylinderObject",
116-
# "ThreeGraphObject",
117-
# "ThreePointObject",
118-
# "ThreePointcloudObject",
119-
# "ThreePolygonObject",
120-
# "ThreePolyhedronObject",
121-
# "ThreePolylineObject",
122-
# "ThreeSceneObject",
123-
# "ThreeSphereObject",
124-
# "ThreeTorusObject",
125-
# ]

0 commit comments

Comments
 (0)