Skip to content

Commit 04c5034

Browse files
authored
Merge pull request #68 from martinRenou/warpbyscalar
WarpByScalar
2 parents ae8d0f3 + db10cc0 commit 04c5034

File tree

8 files changed

+103
-4
lines changed

8 files changed

+103
-4
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
WarpByScalar
2+
============
3+
4+
The ``WarpByScalar`` widget will modify the mesh geometry.
5+
6+
It is similar to ``Paraview``, ``PyVista`` and ``vtk``'s warp-by-scalar effect, but instead of computing the transformation on the CPU,
7+
it is entirely computed on the GPU. Which means that changing the warp factor does not involve looping over the mesh vertices,
8+
we only send the new ``factor`` value to the GPU.
9+
10+
The ``input`` attribute should be a 1-D data. For example, if your mesh has a 1-D ``Data`` named ``"height"``, your can set the input to be:
11+
12+
.. code::
13+
14+
warped_mesh = WarpByScalar(mesh, input='height') # Warp by 'height' data
15+
16+
17+
Examples
18+
--------
19+
20+
.. code:: Python
21+
22+
from pyvista import examples
23+
import numpy as np
24+
25+
from ipywidgets import VBox, FloatSlider
26+
from ipygany import PolyMesh, Scene, IsoColor, WarpByScalar
27+
28+
pvmesh = examples.download_topo_global()
29+
ugrid = pvmesh.cast_to_unstructured_grid()
30+
31+
from ipygany import PolyMesh, Scene, IsoColor, WarpByScalar
32+
33+
# Turn the PyVista mesh into a PolyMesh
34+
mesh = PolyMesh.from_vtk(ugrid)
35+
36+
colored_mesh = IsoColor(mesh, min=-10421.0, max=6527.0)
37+
warped_mesh = WarpByScalar(colored_mesh, input='altitude', factor=0.5e-5)
38+
39+
# Link a slider to the warp value
40+
warp_slider = FloatSlider(min=0., max=5., value=0.5)
41+
42+
def on_slider_change(change):
43+
warped_mesh.factor = change['new'] * 1e-5
44+
45+
warp_slider.observe(on_slider_change, 'value')
46+
47+
VBox((warp_slider, Scene([warped_mesh])))
48+
49+
.. image:: warpscalar.gif
50+
:alt: warpscalar
9.26 MB
Loading

docs/source/api_reference/water.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ If you have under-water meshes on which you want to cast caustics, you need to c
1717
1818
Then you'll need to pass those under-water meshes to the water effect:
1919

20-
.. code::
20+
.. code:: Python
2121
2222
underwater_mesh1 = UnderWater(mesh1, input='underwater')
2323
underwater_mesh2 = UnderWater(mesh2, input='underwater')

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ ipygany: Scientific visualization in the Jupyter notebook
2626

2727
api_reference/isocolor
2828
api_reference/warp
29+
api_reference/warpbyscalar
2930
api_reference/threshold
3031

3132
.. toctree::

ipygany/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
PolyMesh, TetraMesh, PointCloud,
99
Scene,
1010
Data, Component,
11-
Alpha, RGB, IsoColor, Threshold, IsoSurface, Warp,
11+
Alpha, RGB, IsoColor, Threshold, IsoSurface,
12+
Warp, WarpByScalar,
1213
Water, UnderWater
1314
)
1415
from ._version import __version__, version_info # noqa

ipygany/ipygany.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,19 @@ def input_dim(self):
529529
return 3
530530

531531

532+
class WarpByScalar(Effect):
533+
"""A warp-by-scalar effect to another block."""
534+
535+
_model_name = Unicode('WarpByScalarModel').tag(sync=True)
536+
537+
factor = CFloat(1.).tag(sync=True)
538+
539+
@property
540+
def input_dim(self):
541+
"""Input dimension."""
542+
return 1
543+
544+
532545
class Alpha(Effect):
533546
"""An transparency effect to another block."""
534547

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"@jupyter-widgets/controls": "^2.0.0",
5454
"backbone": "^1.4.0",
5555
"binary-search-tree": "^0.2.6",
56-
"ganyjs": "^0.6.2",
56+
"ganyjs": "^0.6.3",
5757
"three": "^0.118.0",
5858
"uuid": "^3.3.3"
5959
},

src/widget.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
Data, Component,
2828
Block, Effect,
2929
PolyMesh, TetraMesh, PointCloud,
30-
Warp, Alpha, RGB, IsoColor, IsoSurface, Threshold,
30+
Warp, WarpByScalar, Alpha, RGB, IsoColor, IsoSurface, Threshold,
3131
Water, UnderWater,
3232
} from 'ganyjs';
3333

@@ -409,6 +409,40 @@ class WarpModel extends EffectModel {
409409
}
410410

411411

412+
export
413+
class WarpByScalarModel extends EffectModel {
414+
415+
defaults() {
416+
return {...super.defaults(),
417+
_model_name: WarpByScalarModel.model_name,
418+
};
419+
}
420+
421+
get input () {
422+
return this.get('input');
423+
}
424+
425+
get factor () {
426+
return this.get('factor');
427+
}
428+
429+
createBlock () {
430+
return new WarpByScalar(this.parent.block, this.input, this.factor);
431+
}
432+
433+
initEventListeners () : void {
434+
super.initEventListeners();
435+
436+
this.on('change:factor', () => { this.block.factor = this.factor; });
437+
}
438+
439+
block: WarpByScalar;
440+
441+
static model_name = 'WarpByScalarModel';
442+
443+
}
444+
445+
412446
export
413447
class AlphaModel extends EffectModel {
414448

0 commit comments

Comments
 (0)