Skip to content

Commit 3f6d7dd

Browse files
authored
Merge pull request #21 from martinRenou/pointcloud_support
Add PointCloud support
2 parents 7264d2a + 1ec70a9 commit 3f6d7dd

File tree

5 files changed

+92
-6
lines changed

5 files changed

+92
-6
lines changed

ipygany/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
# Copyright (c) Martin Renou.
55
# Distributed under the terms of the Modified BSD License.
66

7-
from .ipygany import PolyMesh, TetraMesh, Scene, Data, Component, Alpha, IsoColor, Threshold, IsoSurface # noqa
7+
from .ipygany import ( # noqa
8+
PolyMesh, TetraMesh, PointCloud,
9+
Scene,
10+
Data, Component,
11+
Alpha, IsoColor, Threshold, IsoSurface
12+
)
813
from ._version import __version__, version_info # noqa
914

1015
from .nbextension import _jupyter_nbextension_paths # noqa

ipygany/ipygany.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,61 @@ def reload(self, path, reload_vertices=False, reload_triangles=False, reload_dat
301301
_update_data_widget(get_ugrid_data(grid), self)
302302

303303

304+
class PointCloud(Block):
305+
"""A 3-D point-cloud widget."""
306+
307+
_model_name = Unicode('PointCloudModel').tag(sync=True)
308+
309+
def __init__(self, vertices=[], data=[], **kwargs):
310+
"""Construct a PointCloud."""
311+
super(PointCloud, self).__init__(vertices=vertices, data=data, **kwargs)
312+
313+
@staticmethod
314+
def from_vtk(path, **kwargs):
315+
"""Pass a path to a VTK Unstructured Grid file (``.vtu``) or pass a ``vtkUnstructuredGrid`` object to use.
316+
317+
Parameters
318+
----------
319+
path : str or vtk.vtkUnstructuredGrid
320+
The path to the VTK file or an unstructured grid in memory.
321+
"""
322+
import vtk
323+
324+
from .vtk_loader import (
325+
load_vtk, get_ugrid_vertices, get_ugrid_data
326+
)
327+
328+
if isinstance(path, str):
329+
grid = load_vtk(path)
330+
elif isinstance(path, vtk.vtkUnstructuredGrid):
331+
grid = path
332+
elif hasattr(path, "cast_to_unstructured_grid"):
333+
# Allows support for any PyVista mesh
334+
grid = path.cast_to_unstructured_grid()
335+
else:
336+
raise TypeError("Only unstructured grids supported at this time.")
337+
338+
return PointCloud(
339+
vertices=get_ugrid_vertices(grid),
340+
data=_grid_data_to_data_widget(get_ugrid_data(grid)),
341+
**kwargs
342+
)
343+
344+
def reload(self, path, reload_vertices=False, reload_data=True):
345+
"""Reload a vtk file, entirely or partially."""
346+
from .vtk_loader import (
347+
load_vtk, get_ugrid_vertices, get_ugrid_data
348+
)
349+
350+
grid = load_vtk(path)
351+
352+
with self.hold_sync():
353+
if reload_vertices:
354+
self.vertices = get_ugrid_vertices(grid)
355+
if reload_data:
356+
_update_data_widget(get_ugrid_data(grid), self)
357+
358+
304359
class Effect(Block):
305360
"""An effect applied to another block.
306361

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"@jupyter-widgets/base": "^1.1.10 || ^2",
5353
"backbone": "^1.4.0",
5454
"binary-search-tree": "^0.2.6",
55-
"ganyjs": "^0.1.8",
55+
"ganyjs": "^0.1.9",
5656
"three": "^0.110.0",
5757
"uuid": "^3.3.3"
5858
},

src/widget.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
Scene, Renderer,
2323
Data, Component,
2424
Block, Effect,
25-
PolyMesh, TetraMesh,
25+
PolyMesh, TetraMesh, PointCloud,
2626
Alpha, IsoColor, IsoSurface, Threshold
2727
} from 'ganyjs';
2828

@@ -279,6 +279,32 @@ class TetraMeshModel extends PolyMeshModel {
279279
}
280280

281281

282+
export
283+
class PointCloudModel extends BlockModel {
284+
285+
defaults() {
286+
return {...super.defaults(),
287+
_model_name: PointCloudModel.model_name,
288+
};
289+
}
290+
291+
createBlock () {
292+
return new PointCloud(this.vertices, this.data, {environmentMeshes: this.environmentMeshes});
293+
}
294+
295+
initEventListeners () : void {
296+
super.initEventListeners();
297+
298+
this.on('change:vertices', () => { this.block.vertices = this.vertices; });
299+
}
300+
301+
block: PointCloud;
302+
303+
static model_name = 'PointCloudModel';
304+
305+
}
306+
307+
282308
abstract class EffectModel extends BlockModel {
283309

284310
defaults() {

0 commit comments

Comments
 (0)