Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/marcomusy/vtkplotter
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomusy committed Apr 30, 2019
2 parents 683a8d5 + d14c9f2 commit e27e400
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 172 deletions.
4 changes: 3 additions & 1 deletion examples/other/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ python example.py # on mac OSX try 'pythonw' instead
| | |
| [![logosh](https://shtools.oca.eu/shtools/images/company_logo.png)](https://github.com/marcomusy/vtkplotter/blob/master/examples/other/spherical_harmonics2.py)<br/> `spherical_harmonics2.py` | Morph one shape into another using spherical harmonics package [SHTOOLS](https://shtools.oca.eu/shtools/). In this example we morph a sphere into a octahedron and viceversa. |
| | |
| [![makevideo](https://user-images.githubusercontent.com/32848391/50739007-2bfc2b80-11da-11e9-97e6-620a3541a6fa.jpg)](https://github.com/marcomusy/vtkplotter/blob/master/examples/basic/makeVideo.py)<br/> `makeVideo.py` | Make a video of a rotating spider (needs linux `ffmpeg`). <br/>Set `offscreen=True` to only produce the video without any graphical window showing. |
| [![makevideo](https://user-images.githubusercontent.com/32848391/50739007-2bfc2b80-11da-11e9-97e6-620a3541a6fa.jpg)](https://github.com/marcomusy/vtkplotter/blob/master/examples/other/makeVideo.py)<br/> `makeVideo.py` | Make a video of a rotating spider (needs linux `ffmpeg`). <br/>Set `offscreen=True` to only produce the video without any graphical window showing. |
| | |
| [![makevideo](https://upload.wikimedia.org/wikipedia/commons/c/cd/Hanoi_03.gif)](https://github.com/marcomusy/vtkplotter/blob/master/examples/other/hanoi3d.py)<br/> `hanoi3d.py` | Make an animation of the Tower of Hanoi (needs imagemagick `convert`). <br/> Standalone program with possibility to change number of disks |
| | |
| [![printc](https://user-images.githubusercontent.com/32848391/50739010-2bfc2b80-11da-11e9-94de-011e50a86e61.jpg)](https://github.com/marcomusy/vtkplotter/blob/master/examples/other/printc.py)<br/> `printc.py` | Printing in colors to the terminal.<br> Available colors: <br>0-black, 1-red, 2-green, 3-yellow, 4-blue, 5-magenta, 6-cyan, 7-white<br>Available modifiers:<br> c (foreground color), bc (background color), hidden, bold, blink, underline, dim, invert, box |
| | |
Expand Down
4 changes: 2 additions & 2 deletions examples/other/spherical_harmonics1.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
ilat = grid_reco.lats()[i]
for j, value in enumerate(longs):
ilong = grid_reco.lons()[j]
th = (90 - ilat) / 57.3
ph = ilong / 57.3
th = np.deg2rad(90 - ilat)
ph = np.deg2rad(ilong)
r = value + rbias
p = np.array([sin(th) * cos(ph), sin(th) * sin(ph), cos(th)]) * r
pts.append(p)
Expand Down
4 changes: 2 additions & 2 deletions examples/other/spherical_harmonics2.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def morph(clm1, clm2, t, lmax):
ilat = grid_reco.lats()[i]
for j, value in enumerate(longs):
ilong = grid_reco.lons()[j]
th = (90 - ilat) / 57.3
ph = ilong / 57.3
th = np.deg2rad(90 - ilat)
ph = np.deg2rad(ilong)
r = value + rbias
p = np.array([sin(th) * cos(ph), sin(th) * sin(ph), cos(th)]) * r
pts.append(p)
Expand Down
9 changes: 3 additions & 6 deletions examples/run_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ python simulations/gyroscope2.py
echo Running simulations/multiple_pendulum.py
python simulations/multiple_pendulum.py

echo Running simulations/hanoi3d.py
python simulations/hanoi3d.py

echo Running simulations/pendulum.py
python simulations/pendulum.py

Expand All @@ -286,9 +289,6 @@ python simulations/doubleslit.py
echo Running simulations/tunnelling2.py
python simulations/tunnelling2.py

echo Running simulations/hanoi3d.py
python simulations/hanoi3d.py


################################### volumetric
echo Running volumetric/readVolumeAsIsoSurface.py
Expand All @@ -297,9 +297,6 @@ python volumetric/readVolumeAsIsoSurface.py
echo Running volumetric/readVolume.py
python volumetric/readVolume.py

echo Running volumetric/readStructuredPoints.py
python volumetric/readStructuredPoints.py

echo Running volumetric/probePoints.py
python volumetric/probePoints.py

Expand Down
2 changes: 2 additions & 0 deletions examples/simulations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ python example.py # on mac OSX try 'pythonw' instead
| | |
| [![pendulum](https://user-images.githubusercontent.com/32848391/55420020-51e56200-5576-11e9-8513-4a5d93913b17.png)](https://github.com/marcomusy/vtkplotter/blob/master/examples/simulations/pendulum.py)<br/> `pendulum.py` | Visualize the phase space of a simple pendulum (from [3Blue1Brown](https://www.youtube.com/watch?v=p_di4Zn4wz4)). |
| | |
| [![hanoi](https://user-images.githubusercontent.com/32848391/56989284-58c1bd80-6b92-11e9-8f82-1ce95813f846.gif)](https://github.com/marcomusy/vtkplotter/blob/master/examples/simulations/hanoi3d.py)<br/> `hanoi3d.py` | Solve the Tower of Hanoi puzzle (contributed by [G. Jacquenot](https://github.com/Gjacquenot)). |
| | |
| [![ruth](https://user-images.githubusercontent.com/32848391/50738891-db380300-11d8-11e9-84c2-0f55be7228f1.gif)](https://github.com/marcomusy/vtkplotter/blob/master/examples/simulations/particle_simulator.py)<br/> `particle_simulator.py` | Simulates interacting charged particles in 3D space. |
| | |
| [![tunneling1](https://vtkplotter.embl.es/gifs/tunnelling2.gif)](https://github.com/marcomusy/vtkplotter/blob/master/examples/simulations/tunnelling1.py)<br/> `tunnelling1.py` | Quantum Tunnelling effect using 4th order Runge-Kutta method with arbitrary potential shape. <br>The animation shows the evolution of a particle of relatively well defined momentum (hence undefined position) in a box hitting a potential barrier. The wave function is forced to be zero at the box walls. |
Expand Down
2 changes: 1 addition & 1 deletion examples/simulations/gyroscope1.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
gpos = cm - 1 / 2 * Ls * versor(Lrot)

# set orientation along gaxis and rotate it around its axis by omega*t degrees
gyro.orientation(Lrot, rotation=omega * t * 57.3).pos(gpos)
gyro.orientation(Lrot, rotation=omega * t, rad=True).pos(gpos)
spring.stretch(top, gpos)
vp.show()
pb.print()
Expand Down
2 changes: 1 addition & 1 deletion examples/simulations/gyroscope2.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

gaxis = (Lshaft + 0.03) * vector(st * sp, ct, st * cp)
# set orientation along gaxis and rotate it around its axis by psidot*t degrees
gyro.orientation(gaxis, rotation=psidot * t * 57.3)
gyro.orientation(gaxis, rotation=psidot * t, rad=True)
if not i % 200: # add trace and render all, every 200 iterations
vp.add(Point(gaxis, r=3, c="r"))
vp.show()
Expand Down
172 changes: 172 additions & 0 deletions examples/simulations/hanoi3d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
"""
Demo to show how to solve the Tower of Hanoi
"""
from vtkplotter import *


class Hanoi:
"""
Class to solve the Hanoi problem. It is taken from
Geert Jan Bex's website
https://github.com/gjbex/training-material/blob/master/Misc/Notebooks/hanoi.ipynb
with licence
Creative Commons Zero v1.0 Universal
The Creative Commons CC0 Public Domain Dedication
waives copyright interest in a work you've created
and dedicates it to the world-wide public domain.
Use CC0 to opt out of copyright entirely and ensure
your work has the widest reach. As with the Unlicense
and typical software licenses, CC0 disclaims
warranties. CC0 is very similar to the Unlicense.
"""

def __init__(self, nr_disks):
self._nr_disks = nr_disks
self._towers = [list(range(nr_disks, 0, -1)), list(), list()]

@property
def nr_disks(self):
return self._nr_disks

@property
def nr_moves(self):
return 2 ** self.nr_disks - 1

@property
def towers(self):
from copy import deepcopy

return deepcopy(self._towers)

def tower(self, n):
return self._towers[n].copy()

def move_disk(self, from_tower, to_tower):
disk = self._towers[from_tower].pop()
self._towers[to_tower].append(disk)
return disk, from_tower, to_tower

def move_disks(self, n, from_tower, to_tower):
if n == 1:
yield self.move_disk(from_tower, to_tower)
else:
helper = 3 - from_tower - to_tower
yield from self.move_disks(n - 1, from_tower, helper)
yield self.move_disk(from_tower, to_tower)
yield from self.move_disks(n - 1, helper, to_tower)

def solve(self):
for move in self.moves():
print("disk {0} from {1} to {2}".format(*move))

def moves(self):
yield from self.move_disks(self.nr_disks, 0, 1)


def demo3d_hanoi(**kwargs):
nr_disks = kwargs.get("nr_disks", 5)
create_png = kwargs.get("create_png", False)
create_gif = kwargs.get("create_gif", False)
interactive = kwargs.get("interactive", 1)
if create_gif:
create_png = True

hanoi = Hanoi(nr_disks)
tower_states = list([hanoi.towers])
for _ in hanoi.moves():
tower_states.append(hanoi.towers)

vp = Plotter(axes=0, interactive=0, bg="w")
cols = makePalette("red", "blue", hanoi.nr_disks + 1, hsv=True)
disks = {
hanoi.nr_disks
- i: Cylinder(pos=[0, 0, 0.0], r=0.2 * (hanoi.nr_disks - i + 1), c=cols[i])
for i in range(hanoi.nr_disks)
}
for k in disks:
vp.add(disks[k])
vp.add(Box(pos=(3.0, 0, -0.05), length=12.0, width=4.0, height=0.1))
vp.camera.SetPosition([18.5, -20.7, 7.93])
vp.camera.SetFocalPoint([3.0, 0.0, 2.5])
vp.camera.SetViewUp([-0.1, +0.17, 0.977])
vp.camera.SetDistance(26.0)
vp.show()

list_of_images = []
printc("\n Press q to continue, Esc to exit. ", c="y", invert=1)
pb = ProgressBar(0, len(tower_states), 1, c="b", ETA=False)
for t in pb.range():
pb.print()
state = tower_states[t]
for tower_nr in range(3):
for i, disk in enumerate(state[tower_nr]):
disks[disk].pos([3 * tower_nr, 0, i + 0.5])
vp.show(resetcam=0, interactive=interactive)
if create_png:
vp.interactive = 0
output = "Hanoi_{0:02d}.png".format(t)
screenshot(output)
list_of_images.append(output)
if create_gif:
create_animated_gif(
filename="Hanoi_{0:02d}.gif".format(nr_disks), pngs=list_of_images
)


def create_animated_gif(filename="Hanoi.gif", **kwargs):
import os

pngs = kwargs.get("pngs", None)
if pngs is None:
from glob import glob

pngs = glob("*.png")
cmd = "convert -antialias -density 100 -delay 40 "
cmd += " ".join(pngs)
cmd += " " + filename
os.system(cmd.split(" "))


def main(cli=None):
import argparse

parser = argparse.ArgumentParser(
description="Create a 3D images showing how to solve the Tower of Hanoi."
)
pa = parser.add_argument
pa("-n", "--nr_disks", type=int, default=5, help="Number of disks")
pa(
"-i",
"--interactive",
action="store_true",
help="Request user to press keyboard to display next step",
)
pa(
"-r",
"--display_rate",
type=int,
default=2,
help="Change display rate to speed up or slow down animation",
)
pa("-p", "--png", action="store_true", help="Create pngs")
pa(
"-g",
"--gif",
action="store_true",
help="Create an animated gif. Require convert program from imagemagick",
)
args = parser.parse_args(cli)
demo3d_hanoi(
nr_disks=args.nr_disks,
display_rate=args.display_rate,
create_png=args.png,
create_gif=args.gif,
)


if __name__ == "__main__":
main()
5 changes: 3 additions & 2 deletions examples/volumetric/readStructuredPoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
The list of existing arrays can be found by selecting an actor and
pressing i in the rendering window.
"""
from vtkplotter import Plotter, loadStructuredPoints, Text, datadir
from vtkplotter import *

vp = Plotter(axes=4, verbose=0)

actor = loadStructuredPoints(datadir+"hydrogen.vtk")
vtkstr = loadStructuredPoints(datadir+"hydrogen.vtk")
actor = Actor(geometry(vtkstr))

actor.alpha(0.2).pointSize(15)

Expand Down
Loading

0 comments on commit e27e400

Please sign in to comment.