Skip to content

Commit

Permalink
4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomusy committed Jul 17, 2019
1 parent 0596ec4 commit fbed73d
Show file tree
Hide file tree
Showing 85 changed files with 150,879 additions and 1,858 deletions.
13 changes: 7 additions & 6 deletions bin/vtkconvert
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ from vtkplotter.utils import humansort
from vtkplotter import printc
import sys, argparse

allowedexts = ['vtk', 'vtp', 'vtu', 'vts', 'ply', 'stl', 'byu', 'xml', 'vti','tif','mhd','xml']
allowedexts = ['vtk', 'vtp', 'vtu', 'vts', 'npy', 'ply', 'stl',
'byu', 'xml', 'vti','tif','mhd','xml']

pr = argparse.ArgumentParser(description="Allowed targets: "+str(allowedexts))
pr.add_argument('files', nargs='*', help="Input filename(s)")
pr.add_argument("-n", "--normalize", help="normalize target size", action="store_true")
pr.add_argument("-c", "--clean", help="remove coincident points", action="store_true")
pr.add_argument("-m", "--mirror", help="mirror along the x-axis", action="store_true")
pr.add_argument("-b", "--binary", help="whether output is binary or not", action="store_true")
pr.add_argument("-n", "--normalize",help="normalize target size", action="store_true")
pr.add_argument("-c", "--clean", help="remove coincident points", action="store_true")
pr.add_argument("-m", "--mirror", help="mirror along the x-axis", action="store_true")
pr.add_argument("-b", "--binary", help="whether output is binary or not", action="store_true")
pr.add_argument("-t", "--triangle", help="Convert polygons to triangles", action="store_true")
pr.add_argument("-to", type=str, help="target format [vtk]", default='vtk', metavar='')
pr.add_argument("-to", type=str, help="target format [vtk]", default='vtk', metavar='')
args = pr.parse_args()

humansort(args.files)
Expand Down
137 changes: 56 additions & 81 deletions bin/vtkplotter
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
#
from __future__ import division, print_function
from vtkplotter import Actor, Plotter, ProgressBar, printc, humansort, __version__
from vtkplotter import Actor, Volume, Plotter, ProgressBar, printc, humansort, __version__
from vtkplotter import vtkio
import vtk
import sys, argparse, os
Expand Down Expand Up @@ -214,10 +214,12 @@ def _showVoxelImage():

def keyfuncJitter(key): #toggle jittering
if key != 'j': return
s = volume.jittering()
snew = (s+1)%2
volume.jittering(snew)
vp.interactor.Render()
if volume.jittering() is not None:
s = int(volume.jittering())
snew = (s+1)%2
volume.jittering(snew)
vp.interactor.Render()

volume.jittering(True)
vp.keyPressFunction = keyfuncJitter # make it known to Plotter class

Expand Down Expand Up @@ -252,6 +254,7 @@ def _showVoxelImage():
w2.SetEnabled(0)



##########################################################
# special case of SLC/TIFF volumes with -g option
if args.ray_cast_mode or args.z_spacing or args.y_spacing:
Expand Down Expand Up @@ -336,19 +339,18 @@ elif nfiles == 1 and (
or ".dem" in args.files[0]
):
#print('DEBUG normal mode for single VOXEL file with Isosurface Slider or LEGO mode')
from vtkplotter import legosurface

image = vtkio.loadImageData(args.files[0])
scrange = image.GetScalarRange()
threshold = (scrange[1] - scrange[0]) / 3.0 + scrange[0]
#printHistogram(image, minbin=3, height=8, bins=40, logscale=1)
vol = Volume(image)

if args.lego:
sliderpos = ((0.79, 0.035), (0.975, 0.035))
slidertitle = ""
showval = False
mbg = "white"
act = legosurface(image, vmin=threshold, cmap=args.cmap)
act = vol.legosurface(vmin=threshold, cmap=args.cmap)
act.addScalarBar(act, horizontal=1, vmin=scrange[0], vmax=scrange[1])
else:
sliderpos = 4
Expand All @@ -368,18 +370,18 @@ elif nfiles == 1 and (

cf.SetValue(0, threshold)
cf.Update()
act = Actor(cf.GetOutput(), c=ic, alpha=args.alpha).wire(args.wireframe)
act = Actor(cf.GetOutput(), c=ic, alpha=args.alpha).wireframe(args.wireframe)
act.phong()

############################## threshold slider
def sliderThres(widget, event):
if args.lego:
a = legosurface(image, vmin=widget.GetRepresentation().GetValue(), cmap=args.cmap)
a = vol.legosurface(vmin=widget.GetRepresentation().GetValue(), cmap=args.cmap)
else:
cf.SetValue(0, widget.GetRepresentation().GetValue())
cf.Update()
poly = cf.GetOutput()
a = Actor(poly, ic, alpha=act.alpha()).wire(args.wireframe)
a = Actor(poly, ic, alpha=act.alpha()).wireframe(args.wireframe)
a.phong()
vp.actors = []
vp.renderer.RemoveActor(vp.getActors()[0])
Expand Down Expand Up @@ -408,10 +410,21 @@ elif nfiles == 1 and (


########################################################################
# NORMAL mode for single or multiple files, or multiren mode
# NORMAL mode for single or multiple files, or multiren mode, or numpy scene
elif nfiles == 1 or (not args.scrolling_mode):
#print('DEBUG NORMAL mode for single or multiple files, or multiren mode')

##########################################################
# loading a full scene
if ".npy" in args.files[0] and nfiles == 1:
import numpy
data = numpy.load(args.files[0], allow_pickle=True)[0]
if 'objects' in data.keys():
vp = vtkio.importWindow(args.files[0])
vp.show()
exit()
##########################################################

actors = []
for i in range(N):
f = args.files[i]
Expand Down Expand Up @@ -459,32 +472,26 @@ else:
n = len(args.files)
pb = ProgressBar(0, n)

actors = []
# load files first
for i, f in enumerate(args.files):
pb.print("..loading")
ic = "gold"

ic = None
if args.color is not None:
if args.color.isdigit():
ic = int(args.color)
else:
ic = args.color

actor = vp.load(f, c=ic, alpha=args.alpha) # can be Image
if hasattr(actor, 'wireframe'):
actor.wire(wire)
actor.legend(leg)
actors.append(actor)
if args.point_size > 0:
try:
ps = actor.GetProperty().GetPointSize()
actor.GetProperty().SetPointSize(args.point_size)
actor.GetProperty().SetRepresentationToPoints()
except AttributeError:
pass
actor = vp.load(f, c=ic, alpha=args.alpha).legend(leg)
if hasattr(actor, 'wireframe'): # can be Image or volume
actor.wireframe(wire)
if args.point_size > 0:
actor.pointSize(args.point_size)

# calculate max actors bounds
bns = []
for a in actors:
for a in vp.actors:
if a and a.GetPickable():
b = a.GetBounds()
if b:
Expand All @@ -494,66 +501,34 @@ else:
min_bns = numpy.min(bns, axis=0)
vbb = (min_bns[0], max_bns[1], min_bns[2], max_bns[3], min_bns[4], max_bns[5])

def _scroll(iren, event): # observer
global _kact
actors[_kact].off()

key = iren.GetKeySym()

n = len(actors)
if key == "Right" and _kact < n - 1:
_kact += 1
elif key == "Left" and _kact > 0:
_kact -= 1

step = int(n / 5)
if key == "Up":
_kact += step
if _kact > n - 1:
_kact = n - 1
elif key == "Down":
_kact -= step
if _kact < 0:
_kact = 0
sliderRep.SetValue(_kact)
if 0 <= _kact < n:
vp.clickedActor = actors[_kact]

actors[_kact].on()
fn = args.files[_kact].split("/")[-1]
printc("showing file #", _kact, "\t", fn, "\r", c="y", bold=0, end="")

vp.interactor.AddObserver("KeyPressEvent", _scroll)

vp.show(actors[0], interactive=False, zoom=args.zoom)
# show the first
saveacts = vp.actors
vp.show(vp.actors[0], interactive=False, zoom=args.zoom)
vp.actors = saveacts

if isinstance(vp.axes_instances[0], vtk.vtkCubeAxesActor):
vp.axes_instances[0].SetBounds(vbb)

cb = (1, 1, 1)
if numpy.sum(vp.renderer.GetBackground()) > 1.5:
cb = (0.1, 0.1, 0.1)

def sliderf(widget, event):
global _kact
actors[_kact].off()
_kact = int(widget.GetRepresentation().GetValue())
actors[_kact].on()
fn = args.files[_kact].split("/")[-1]
printc("showing file #", _kact, "\t", fn, "\r", c="y", bold=0, end="")

wid = vp.addSlider2D(sliderf, 0, n - 1, pos=4, c=cb, showValue=False)
wid.SetAnimationModeToJump()
sliderRep = wid.GetRepresentation()

_kact = 0
for a in actors:
# define the slider
kact = 0
for a in vp.actors:
a.off()
actors[0].on()

printc("Scrolling Mode", c="y", invert=1, end="")
printc(", press:", c="y")
printc("- Right and Left keys to move through files", c="y")
printc("- Up and Down keys to fast forward and backward", c="y")
vp.show(actors, interactive=True, zoom=args.zoom)
vp.actors[0].on()
def sliderf(widget, event):
global kact
kactnew = int(widget.GetRepresentation().GetValue())
if kactnew == kact: return
vp.actors[kact].off()
vp.actors[kactnew].on()
kact = kactnew
printc("Scrolling Mode:", c="y", invert=1, end="")
printc(" showing file nr.", kact, args.files[kact].split("/")[-1],
"\r", c="y", bold=0, end="")

vp.addSlider2D(sliderf, 0, n-1, pos=4, c=cb, showValue=False)

vp.show(interactive=True, zoom=args.zoom)
print()
2 changes: 1 addition & 1 deletion examples/advanced/cutAndCap.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

p1 = Paraboloid().rotateX(90)

cutmesh = Hyperboloid().scale(0.4).wire(True).alpha(0.1)
cutmesh = Hyperboloid().scale(0.4).wireframe(True).alpha(0.1)

show(p1, cutmesh, at=0, N=2, axes=1, viewup="z")

Expand Down
14 changes: 6 additions & 8 deletions examples/advanced/cutWithMesh.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
"""
Cut a mesh with another mesh.
Try command line
> vtkplotter data/embryo.tif
to see the threshold range.
Cut a mesh with another mesh
"""
from vtkplotter import *

embryo = load(datadir+"embryo.tif", threshold=30).normalize()
txt = Text(__doc__, c='w', bg='lb')

# mesh used to cut:
msh = Ellipsoid().pos(0.8, 0.1, -0.3).scale(0.5).wire()
msh = Ellipsoid().pos(0.8, 0.1, -0.3).scale(0.5).wireframe()

# make a working copy and cut it with the ellipsoid
cutembryo = embryo.clone().cutWithMesh(msh).backColor("t").phong()
cutembryo = embryo.clone().cutWithMesh(msh).backColor("t")

show(embryo, msh, Text(__doc__), at=0, N=2, axes=1, viewup="z")
show(cutembryo, at=1, interactive=1)
show(embryo, msh, at=0, N=2, axes=1, viewup="z")
show(cutembryo, txt, at=1, interactive=1)
2 changes: 1 addition & 1 deletion examples/advanced/fatlimb.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@

# refresh actor, so polydata normals are recalculated
s = s.clone()
vp += s.alpha(0.1).color("gold").wire(True) #add into Plotter
vp += s.alpha(0.1).color("gold").wireframe(True) #add into Plotter

vp.show()
2 changes: 1 addition & 1 deletion examples/advanced/geodesic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""
from vtkplotter import *

s = Sphere(r=1.05, res=200).clean(0.005).wire().alpha(0.05)
s = Sphere(r=1.05, res=200).clean(0.005).wireframe().alpha(0.05)

paths = []
for i in range(20):
Expand Down
27 changes: 12 additions & 15 deletions examples/advanced/moving_least_squares1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
Moving Least Squares (MLS) algorithm to project a cloud
of unordered points to become a smooth line.
The parameter f controls the size of the local regression.
The input actor's polydata is modified by the method
so more than one pass is possible.
If showNLines>0 an actor is built demonstrating the
details of the regression for some random points
"""
Expand All @@ -14,29 +12,28 @@
from vtkplotter import *
import numpy as np

N = 9 # nr. of iterations
N = 6 # nr. of iterations

# build some initial cloud of noisy points along a line
# pts = [ (sin(6*x), sin(2*x)/(x+1), cos(9*x)) for x in arange(0,1, .001)]
# pts = [ (0, sin(x), cos(x)) for x in arange(0,6, .002) ]
pts = [(sqrt(x), sin(x), x / 10) for x in arange(0, 16, 0.01)]
pts = [ (sin(6*x), sin(2*x)/(x+1), cos(9*x)) for x in arange(0,1, .001)]
#pts = [ (0, sin(x), cos(x)) for x in arange(0,6, .002) ]
#pts = [(sqrt(x), sin(x), x/10) for x in arange(0, 16, 0.01)]

pts += np.random.randn(len(pts), 3) / 15 # add noise
pts += np.random.randn(len(pts), 3) /20 # add noise
np.random.shuffle(pts) # make sure points are not ordered

vp = Plotter(N=N, axes=5)
a = Points(pts).legend("cloud")
vp.show(a, at=0)

show(a, at=0, N=N, axes=5)

for i in range(1, N):
a = a.clone().color(i).legend("iter #" + str(i))
smoothMLS1D(a, f=0.2)
a = smoothMLS1D(a.clone(), f=0.2).color(i).legend("iter #" + str(i))

# at last iteration make sure points are separated by tol
if i == N - 1:
# at last iteration make sure points are separated by tol (in % of bbox)
if i == N-1:
a.clean(tol=0.01)

print("iteration", i, "#points:", len(a.coordinates()))
vp.show(a, at=i)
show(a, at=i)

vp.show(interactive=1)
interactive()
Loading

0 comments on commit fbed73d

Please sign in to comment.