Skip to content

Commit e8d0b01

Browse files
committed
simplify buffer generation and docs stubs
1 parent e2729f5 commit e8d0b01

File tree

6 files changed

+174
-194
lines changed

6 files changed

+174
-194
lines changed

docs/api/compas_dem.algorithms.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
********************************************************************************
2+
algorithms
3+
********************************************************************************
4+
5+
.. currentmodule:: compas_dem.algorithms
6+
7+
8+
.. toctree::
9+
:maxdepth: 1

docs/api/compas_dem.analysis.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
********************************************************************************
2+
analysis
3+
********************************************************************************
4+
5+
.. currentmodule:: compas_dem.analysis
6+
7+
8+
.. toctree::
9+
:maxdepth: 1

docs/api/compas_dem.elements.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
********************************************************************************
2+
elements
3+
********************************************************************************
4+
5+
.. currentmodule:: compas_dem.elements
6+
7+
8+
.. toctree::
9+
:maxdepth: 1

notebooks/dem_crossvault.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
{
1818
"cell_type": "code",
19-
"execution_count": 5,
19+
"execution_count": 1,
2020
"metadata": {},
2121
"outputs": [],
2222
"source": [
@@ -39,7 +39,7 @@
3939
},
4040
{
4141
"cell_type": "code",
42-
"execution_count": 6,
42+
"execution_count": 2,
4343
"metadata": {},
4444
"outputs": [],
4545
"source": [
@@ -63,7 +63,7 @@
6363
},
6464
{
6565
"cell_type": "code",
66-
"execution_count": 7,
66+
"execution_count": 3,
6767
"metadata": {},
6868
"outputs": [],
6969
"source": [
@@ -81,7 +81,7 @@
8181
},
8282
{
8383
"cell_type": "code",
84-
"execution_count": 8,
84+
"execution_count": 4,
8585
"metadata": {},
8686
"outputs": [],
8787
"source": [
@@ -97,7 +97,7 @@
9797
},
9898
{
9999
"cell_type": "code",
100-
"execution_count": 9,
100+
"execution_count": 5,
101101
"metadata": {},
102102
"outputs": [],
103103
"source": [

src/compas_dem/notebook/buffers.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
from typing import Optional
2+
3+
import numpy as np
4+
import pythreejs as three
5+
from compas.colors import Color
6+
from compas.datastructures import Mesh
7+
from compas.geometry import Polygon
8+
from compas.geometry import earclip_polygon
9+
10+
11+
def mesh_to_edgesbuffer(mesh: Mesh, color: Color) -> tuple[list[list[float]], list[Color]]:
12+
positions = []
13+
colors = []
14+
15+
for u, v in mesh.edges():
16+
positions.append(mesh.vertex_coordinates(u))
17+
positions.append(mesh.vertex_coordinates(v))
18+
colors.append(color)
19+
colors.append(color)
20+
21+
return positions, colors
22+
23+
24+
def meshes_to_edgesbuffer(
25+
meshes: list[Mesh],
26+
color: Color,
27+
material: Optional[three.LineBasicMaterial] = None,
28+
) -> three.LineSegments:
29+
positions = []
30+
colors = []
31+
32+
for mesh in meshes:
33+
buffer = mesh_to_edgesbuffer(mesh, color)
34+
positions += buffer[0]
35+
colors += buffer[1]
36+
37+
positions = np.array(positions, dtype=np.float32)
38+
colors = np.array(colors, dtype=np.float32)
39+
40+
geometry = three.BufferGeometry(
41+
attributes={
42+
"position": three.BufferAttribute(positions, normalized=False),
43+
"color": three.BufferAttribute(colors, normalized=False, itemSize=3),
44+
}
45+
)
46+
47+
if not material:
48+
material = three.LineBasicMaterial(vertexColors="VertexColors")
49+
50+
return three.LineSegments(geometry, material)
51+
52+
53+
def mesh_to_facesbuffer(mesh: Mesh, color: Color) -> tuple[list[list[float]], list[Color]]:
54+
positions = []
55+
colors = []
56+
57+
for face in mesh.faces():
58+
vertices = mesh.face_vertices(face)
59+
60+
if len(vertices) == 3:
61+
positions.append(mesh.vertex_coordinates(vertices[0]))
62+
positions.append(mesh.vertex_coordinates(vertices[1]))
63+
positions.append(mesh.vertex_coordinates(vertices[2]))
64+
colors.append(color)
65+
colors.append(color)
66+
colors.append(color)
67+
68+
elif len(vertices) == 4:
69+
positions.append(mesh.vertex_coordinates(vertices[0]))
70+
positions.append(mesh.vertex_coordinates(vertices[1]))
71+
positions.append(mesh.vertex_coordinates(vertices[2]))
72+
colors.append(color)
73+
colors.append(color)
74+
colors.append(color)
75+
positions.append(mesh.vertex_coordinates(vertices[0]))
76+
positions.append(mesh.vertex_coordinates(vertices[2]))
77+
positions.append(mesh.vertex_coordinates(vertices[3]))
78+
colors.append(color)
79+
colors.append(color)
80+
colors.append(color)
81+
82+
else:
83+
ears = earclip_polygon(Polygon([mesh.vertex_coordinates(v) for v in vertices]))
84+
for ear in ears:
85+
positions.append(mesh.vertex_coordinates(vertices[ear[0]]))
86+
positions.append(mesh.vertex_coordinates(vertices[ear[1]]))
87+
positions.append(mesh.vertex_coordinates(vertices[ear[2]]))
88+
colors.append(color)
89+
colors.append(color)
90+
colors.append(color)
91+
92+
return positions, colors
93+
94+
95+
def meshes_to_facesbuffer(
96+
meshes: list[Mesh],
97+
color: Color,
98+
material: Optional[three.MeshBasicMaterial] = None,
99+
) -> three.Mesh:
100+
positions = []
101+
colors = []
102+
103+
for mesh in meshes:
104+
buffer = mesh_to_facesbuffer(mesh, color)
105+
positions += buffer[0]
106+
colors += buffer[1]
107+
108+
positions = np.array(positions, dtype=np.float32)
109+
colors = np.array(colors, dtype=np.float32)
110+
111+
geometry = three.BufferGeometry(
112+
attributes={
113+
"position": three.BufferAttribute(positions, normalized=False),
114+
"color": three.BufferAttribute(colors, normalized=False, itemSize=3),
115+
}
116+
)
117+
118+
if not material:
119+
material = three.MeshBasicMaterial(
120+
side="DoubleSide",
121+
vertexColors="VertexColors",
122+
)
123+
124+
return three.Mesh(geometry, material)

0 commit comments

Comments
 (0)