Skip to content

Commit

Permalink
Merge pull request #21 from martinRenou/pointcloud_support
Browse files Browse the repository at this point in the history
Add PointCloud support
  • Loading branch information
martinRenou authored Dec 13, 2019
2 parents 7264d2a + 1ec70a9 commit 3f6d7dd
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 6 deletions.
7 changes: 6 additions & 1 deletion ipygany/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
# Copyright (c) Martin Renou.
# Distributed under the terms of the Modified BSD License.

from .ipygany import PolyMesh, TetraMesh, Scene, Data, Component, Alpha, IsoColor, Threshold, IsoSurface # noqa
from .ipygany import ( # noqa
PolyMesh, TetraMesh, PointCloud,
Scene,
Data, Component,
Alpha, IsoColor, Threshold, IsoSurface
)
from ._version import __version__, version_info # noqa

from .nbextension import _jupyter_nbextension_paths # noqa
55 changes: 55 additions & 0 deletions ipygany/ipygany.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,61 @@ def reload(self, path, reload_vertices=False, reload_triangles=False, reload_dat
_update_data_widget(get_ugrid_data(grid), self)


class PointCloud(Block):
"""A 3-D point-cloud widget."""

_model_name = Unicode('PointCloudModel').tag(sync=True)

def __init__(self, vertices=[], data=[], **kwargs):
"""Construct a PointCloud."""
super(PointCloud, self).__init__(vertices=vertices, data=data, **kwargs)

@staticmethod
def from_vtk(path, **kwargs):
"""Pass a path to a VTK Unstructured Grid file (``.vtu``) or pass a ``vtkUnstructuredGrid`` object to use.
Parameters
----------
path : str or vtk.vtkUnstructuredGrid
The path to the VTK file or an unstructured grid in memory.
"""
import vtk

from .vtk_loader import (
load_vtk, get_ugrid_vertices, get_ugrid_data
)

if isinstance(path, str):
grid = load_vtk(path)
elif isinstance(path, vtk.vtkUnstructuredGrid):
grid = path
elif hasattr(path, "cast_to_unstructured_grid"):
# Allows support for any PyVista mesh
grid = path.cast_to_unstructured_grid()
else:
raise TypeError("Only unstructured grids supported at this time.")

return PointCloud(
vertices=get_ugrid_vertices(grid),
data=_grid_data_to_data_widget(get_ugrid_data(grid)),
**kwargs
)

def reload(self, path, reload_vertices=False, reload_data=True):
"""Reload a vtk file, entirely or partially."""
from .vtk_loader import (
load_vtk, get_ugrid_vertices, get_ugrid_data
)

grid = load_vtk(path)

with self.hold_sync():
if reload_vertices:
self.vertices = get_ugrid_vertices(grid)
if reload_data:
_update_data_widget(get_ugrid_data(grid), self)


class Effect(Block):
"""An effect applied to another block.
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@jupyter-widgets/base": "^1.1.10 || ^2",
"backbone": "^1.4.0",
"binary-search-tree": "^0.2.6",
"ganyjs": "^0.1.8",
"ganyjs": "^0.1.9",
"three": "^0.110.0",
"uuid": "^3.3.3"
},
Expand Down
28 changes: 27 additions & 1 deletion src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
Scene, Renderer,
Data, Component,
Block, Effect,
PolyMesh, TetraMesh,
PolyMesh, TetraMesh, PointCloud,
Alpha, IsoColor, IsoSurface, Threshold
} from 'ganyjs';

Expand Down Expand Up @@ -279,6 +279,32 @@ class TetraMeshModel extends PolyMeshModel {
}


export
class PointCloudModel extends BlockModel {

defaults() {
return {...super.defaults(),
_model_name: PointCloudModel.model_name,
};
}

createBlock () {
return new PointCloud(this.vertices, this.data, {environmentMeshes: this.environmentMeshes});
}

initEventListeners () : void {
super.initEventListeners();

this.on('change:vertices', () => { this.block.vertices = this.vertices; });
}

block: PointCloud;

static model_name = 'PointCloudModel';

}


abstract class EffectModel extends BlockModel {

defaults() {
Expand Down

0 comments on commit 3f6d7dd

Please sign in to comment.