Skip to content

Commit

Permalink
Add function to update a point cloud (#3468)
Browse files Browse the repository at this point in the history
* Add function to update a point cloud

* code review

---------

Co-authored-by: Andreas Voigt <andreas.voigt@optonic.com>
Co-authored-by: Falko Schindler <falko@zauberzeug.com>
  • Loading branch information
3 people authored Aug 12, 2024
1 parent 1a044b2 commit 245c202
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions nicegui/elements/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,11 @@ export default {
if (!this.objects.has(object_id)) return;
this.objects.get(object_id).geometry = texture_geometry(coords);
},
set_points(object_id, position, color) {
const geometry = this.objects.get(object_id).geometry;
geometry.setAttribute("position", new THREE.Float32BufferAttribute(position.flat(), 3));
geometry.setAttribute("color", new THREE.Float32BufferAttribute(color.flat(), 3));
},
move_camera(x, y, z, look_at_x, look_at_y, look_at_z, up_x, up_y, up_z, duration) {
if (this.camera_tween) this.camera_tween.stop();
this.camera_tween = new TWEEN.Tween([
Expand Down
6 changes: 6 additions & 0 deletions nicegui/elements/scene_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,9 @@ def __init__(self,
:param point_size: size of the points (default: 1.0)
"""
super().__init__('point_cloud', points, colors, point_size)

def set_points(self, points: List[List[float]], colors: List[List[float]]) -> None:
"""Change the points and colors of the point cloud."""
self.args[0] = points
self.args[1] = colors
self.scene.run_method('set_points', self.id, points, colors)
15 changes: 12 additions & 3 deletions website/documentation/content/scene_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,24 @@ def handle_drag(e: events.GenericEventArguments) -> None:
@doc.demo('Rendering point clouds', '''
You can render point clouds using the `point_cloud` method.
The `points` argument is a list of point coordinates, and the `colors` argument is a list of RGB colors (0..1).
You can update the cloud using its `set_points()` method.
''')
def point_clouds() -> None:
import numpy as np

with ui.scene().classes('w-full h-64') as scene:
def generate_data(frequency: float = 1.0):
x, y = np.meshgrid(np.linspace(-3, 3), np.linspace(-3, 3))
z = np.sin(x) * np.cos(y) + 1
z = np.sin(x * frequency) * np.cos(y * frequency) + 1
points = np.dstack([x, y, z]).reshape(-1, 3)
scene.point_cloud(points=points, colors=points, point_size=0.1)
colors = points / [6, 6, 2] + [0.5, 0.5, 0]
return points, colors

with ui.scene().classes('w-full h-64') as scene:
points, colors = generate_data()
point_cloud = scene.point_cloud(points, colors, point_size=0.1)

ui.slider(min=0.1, max=3, step=0.1, value=1) \
.on_value_change(lambda e: point_cloud.set_points(*generate_data(e.value)))


@doc.demo('Wait for Initialization', '''
Expand Down

0 comments on commit 245c202

Please sign in to comment.