Skip to content

Commit 67f95b9

Browse files
committed
fix buffer geometry indexing bug
1 parent 67fc758 commit 67f95b9

2 files changed

Lines changed: 26 additions & 12 deletions

File tree

scripts/buffer.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
from compas_viewer import Viewer
44
from compas_viewer.scene import BufferGeometry
55

6+
# One vertex per point
67
points = np.random.rand(1000, 3) * 10
78
pointcolor = np.random.rand(1000, 4)
89

9-
lines = np.random.rand(1000 * 6, 3) * 10
10-
linecolor = np.random.rand(1000 * 6, 4)
10+
# Two vertices per line
11+
lines = np.random.rand(1000 * 2, 3) * 10
12+
linecolor = np.random.rand(1000 * 2, 4)
1113

12-
faces = np.random.rand(1000 * 9, 3) * 10
13-
facecolor = np.random.rand(1000 * 9, 4)
14+
# Three vertices per face
15+
faces = np.random.rand(1000 * 3, 3) * 10
16+
facecolor = np.random.rand(1000 * 3, 4)
1417

1518
geometry = BufferGeometry(points=points, pointcolor=pointcolor, lines=lines, linecolor=linecolor, faces=faces, facecolor=facecolor)
1619

@@ -23,10 +26,10 @@ def update(frame):
2326

2427
geometry.points = np.random.rand(1000, 3) * 10
2528
geometry.pointcolor = np.random.rand(1000, 4)
26-
geometry.lines = np.random.rand(1000 * 6, 3) * 10
27-
geometry.linecolor = np.random.rand(1000 * 6, 4)
28-
geometry.faces = np.random.rand(1000 * 9, 3) * 10
29-
geometry.facecolor = np.random.rand(1000 * 9, 4)
29+
geometry.lines = np.random.rand(1000 * 2, 3) * 10
30+
geometry.linecolor = np.random.rand(1000 * 2, 4)
31+
geometry.faces = np.random.rand(1000 * 3, 3) * 10
32+
geometry.facecolor = np.random.rand(1000 * 3, 4)
3033

3134
obj.update(update_data=True)
3235

src/compas_viewer/scene/bufferobject.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,24 @@ def __init__(self, *args, show_points: bool = True, show_lines: bool = True, sho
122122
def buffergeometry(self) -> BufferGeometry:
123123
return self.item
124124

125+
def _get_num_vertices(self, positions: NDArray) -> int:
126+
"""Get the number of vertices from positions array.
127+
128+
Handles both flat 1D arrays [x1, y1, z1, x2, y2, z2, ...] and
129+
2D arrays with shape (N, 3).
130+
"""
131+
if positions.ndim == 1:
132+
return positions.shape[0] // 3
133+
else:
134+
return positions.shape[0]
135+
125136
def _read_points_data(self):
126137
"""Read points data from the object."""
127138
if self.buffergeometry.points is None:
128139
return None
129140
positions = self.buffergeometry.points
130141
colors = self.buffergeometry.pointcolor
131-
elements = np.arange(positions.shape[0] // 3, dtype=int)
142+
elements = np.arange(self._get_num_vertices(positions), dtype=int)
132143
return positions, colors, elements
133144

134145
def _read_lines_data(self):
@@ -137,7 +148,7 @@ def _read_lines_data(self):
137148
return None
138149
positions = self.buffergeometry.lines
139150
colors = self.buffergeometry.linecolor
140-
elements = np.arange(positions.shape[0] // 3, dtype=int)
151+
elements = np.arange(self._get_num_vertices(positions), dtype=int)
141152
return positions, colors, elements
142153

143154
def _read_frontfaces_data(self):
@@ -146,7 +157,7 @@ def _read_frontfaces_data(self):
146157
return None
147158
positions = self.buffergeometry.faces
148159
colors = self.buffergeometry.facecolor
149-
elements = np.arange(positions.shape[0] // 3, dtype=int)
160+
elements = np.arange(self._get_num_vertices(positions), dtype=int)
150161
return positions, colors, elements
151162

152163
def _read_backfaces_data(self):
@@ -155,5 +166,5 @@ def _read_backfaces_data(self):
155166
return None
156167
positions = self.buffergeometry.faces
157168
colors = self.buffergeometry.facecolor
158-
elements = np.flip(np.arange(positions.shape[0] // 3, dtype=int))
169+
elements = np.flip(np.arange(self._get_num_vertices(positions), dtype=int))
159170
return positions, colors, elements

0 commit comments

Comments
 (0)