Skip to content

Commit

Permalink
Add PointCloud support
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou committed Dec 13, 2019
1 parent 7264d2a commit 620a898
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
7 changes: 6 additions & 1 deletion ipygany/__init__.py
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
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
28 changes: 27 additions & 1 deletion src/widget.ts
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 620a898

Please sign in to comment.