Skip to content

Commit

Permalink
2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomusy committed Apr 20, 2020
1 parent 6916bd6 commit 596a197
Show file tree
Hide file tree
Showing 27 changed files with 562 additions and 264 deletions.
160 changes: 36 additions & 124 deletions bin/vtkplotter
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ from vtkplotter import settings, printc, getColor, humansort, __version__
from vtkplotter import vtkio, utils, datadir
import vtk
import sys, argparse, os
import numpy as np

#################################################################################################
class MyArgs:
Expand Down Expand Up @@ -35,6 +36,7 @@ class MyArgs:
self.y_spacing = None
self.x_spacing = None
self.slicer = False
self.slicer2d = False
self.lego = False
self.cmap = "jet"
self.mode = 0
Expand All @@ -44,7 +46,8 @@ vp = None
args = MyArgs()
_alphaslider0, _alphaslider1, _alphaslider2 = 0.33, 0.66, 1 # defaults
kact = 0
cmap = args.cmap
cmap_slicer = None


#################################################################################################
def _setspacing(img):
Expand All @@ -58,8 +61,9 @@ def _setspacing(img):
ispa = img.GetSpacing()
img.SetSpacing(ispa[0], ispa[1], ispa[2] * args.z_spacing)


def draw_scene():
global kact
global kact, cmap_slicer

nfiles = len(args.files)
if nfiles == 0:
Expand Down Expand Up @@ -92,7 +96,7 @@ def draw_scene():
if nfiles < 201:
N = nfiles
if nfiles > 200:
printc("~lightning Warning: option '-n' allows a maximum of 200 files", c=1)
printc("Warning: option '-n' allows a maximum of 200 files", c=1)
printc(" you are trying to load ", nfiles, " files.\n", c=1)
N = 200
vp = Plotter(size=wsize, N=N, bg=args.background, bg2=args.background_grad)
Expand All @@ -118,7 +122,6 @@ def draw_scene():

import vtkplotter.colors as vc
from vtkplotter import Volume
import numpy as np

printc("GPU Ray-casting Mode", c="b", invert=1)
printc("Press j to toggle jittering", c="b", invert=0, bold=0)
Expand Down Expand Up @@ -317,134 +320,38 @@ def draw_scene():
elif args.slicer:
# print('DEBUG special case of SLC/TIFF/DICOM volumes with --slicer option')

from vtkplotter.pyplot import cornerHistogram
from vtkplotter import show, mag

filename = args.files[0]
alpha = args.alpha
from vtkplotter.applications import slicer

useSlider2D = True
useSlider3D = False
if args.axes_type == 4:
args.axes_type=1
elif args.axes_type == 3:
args.axes_type=1
useSlider2D = False

cmaps = [args.cmap, "hot_r", "gist_earth_r", "viridis", "bone_r"]

################################
vol = vtkio.load(filename)#.printInfo()
box = vol.box().wireframe().alpha(0)
useSlider3D = True

vol = vtkio.load(args.files[0])

settings.plotter_instance = None # reset
wsize = (800,700)
if args.full_screen:
wsize = "full"

vp = show(box, axes=args.axes_type, bg='w', bg2='lb', size=wsize,
title=filename, viewup="z", interactive=False)
vp.showInset(vol, pos=(1,1), size=0.3, c='w', draggable=False)

# inits
dims = vol.dimensions()
cmap = cmaps[0]
visibles = [None, None, None]
msh = vol.zSlice(int(dims[2]/2)).alpha(alpha)
msh.pointColors(cmap=cmap)
vp.renderer.AddActor(msh)
visibles[2] = msh

def sliderfunc_x(widget, event):
global cmap
i = int(widget.GetRepresentation().GetValue())
msh = vol.xSlice(i).alpha(alpha).lighting('plastic')
msh.pointColors(cmap=cmap)
vp.renderer.RemoveActor(visibles[0])
if i and i<dims[0]-1: vp.renderer.AddActor(msh)
vp.window.Render()
visibles[0] = msh

def sliderfunc_y(widget, event):
global cmap
i = int(widget.GetRepresentation().GetValue())
msh = vol.ySlice(i).alpha(alpha).lighting('plastic')
msh.pointColors(cmap=cmap)
vp.renderer.RemoveActor(visibles[1])
if i and i<dims[1]-1: vp.renderer.AddActor(msh)
vp.window.Render()
visibles[1] = msh

def sliderfunc_z(widget, event):
global cmap
i = int(widget.GetRepresentation().GetValue())
msh = vol.zSlice(i).alpha(alpha).lighting('plastic')
msh.pointColors(cmap=cmap)
vp.renderer.RemoveActor(visibles[2])
if i and i<dims[2]-1: vp.renderer.AddActor(msh)
vp.window.Render()
visibles[2] = msh

if useSlider2D:
vp.addSlider2D(sliderfunc_x, 0, dims[0],
pos=[(0.8,0.12), (0.95,0.12)], showValue=False, c='dr')
vp.addSlider2D(sliderfunc_y, 0, dims[1],
pos=[(0.8,0.08), (0.95,0.08)], showValue=False, c='dg')
vp.addSlider2D(sliderfunc_z, 0, dims[2], value=int(dims[2]/2),
pos=[(0.8,0.04), (0.95,0.04)], showValue=False, c='db')
bu_pos = (0.9, 0.155)
else: # 3d sliders attached to the axes bounds
bs = box.bounds()
vp.addSlider3D(sliderfunc_x,
pos1=(bs[0], bs[2], bs[4]),
pos2=(bs[1], bs[2], bs[4]),
xmin=0, xmax=dims[0]-1,
t=box.diagonalSize()/mag(box.xbounds())*0.6,
c="dr",
showValue=False,
)
vp.addSlider3D(sliderfunc_y,
pos1=(bs[1], bs[2], bs[4]),
pos2=(bs[1], bs[3], bs[4]),
xmin=0, xmax=dims[1]-1,
t=box.diagonalSize()/mag(box.ybounds())*0.6,
c="dg",
showValue=False,
)
vp.addSlider3D(sliderfunc_z,
pos1=(bs[0], bs[2], bs[4]),
pos2=(bs[0], bs[2], bs[5]),
xmin=0, xmax=dims[2]-1,
value=int(dims[2]/2),
t=box.diagonalSize()/mag(box.zbounds())*0.6,
c="db",
showValue=False,
)
bu_pos = (0.09, 0.22)


#################
def buttonfunc():
global cmap
bu.switch()
cmap = bu.status()
for mesh in visibles:
if mesh:
mesh.pointColors(cmap=cmap)
bu = vp.addButton(buttonfunc,
pos=bu_pos, # x,y fraction from bottom left corner
states=cmaps,
c=["db"]*len(cmaps),
bc=["lb"]*len(cmaps), # colors of states
size=12,
bold=True,
)
plt = slicer(vol,
bg='white', bg2='lb',
useSlider3D=useSlider3D,
cmaps=[args.cmap, "Spectral_r", "hot_r", "bone_r", "gist_ncar_r"],
alpha=args.alpha,
axes=args.axes_type,
clamp=True,
)

#################
hist = cornerHistogram(vol.getPointArray(), s=0.18,
bins=25, logscale=True, pos=(0.03, 0.015),
c=(0.5,0.5,0.5), bg=(0.5,0.5,0.5), alpha=0.6)

vp.show(msh, hist, interactive=True)
plt.show()
return

########################################################################
elif args.slicer2d:
# print('DEBUG special case of SLC/TIFF/DICOM volumes with --slicer2d option')
from vtkplotter.applications import slicer2d
vol = vtkio.load(args.files[0])
settings.plotter_instance = None # reset
slicer2d(vol)
return


Expand Down Expand Up @@ -825,6 +732,7 @@ if len(sys.argv) == 1 or os.name == "nt": # no args are passed, pop up GUI
"maximum proj",
"lego",
"slicer",
"slicer2d",
)
self.modeCB = Combobox(self, state="readonly", values=modevalues, width=20)
self.modeCB.current(0)
Expand Down Expand Up @@ -952,6 +860,7 @@ if len(sys.argv) == 1 or os.name == "nt": # no args are passed, pop up GUI
args.ray_cast_mode = False
args.lego = False
args.slicer = False
args.slicer2d = False
args.lego = False
args.mode = 0
if self.modeCB.get() == "composite":
Expand All @@ -962,6 +871,8 @@ if len(sys.argv) == 1 or os.name == "nt": # no args are passed, pop up GUI
args.mode = 1
elif self.modeCB.get() == "slicer":
args.slicer = True
elif self.modeCB.get() == "slicer2d":
args.slicer2d = True
elif self.modeCB.get() == "lego":
args.lego = True

Expand Down Expand Up @@ -1024,7 +935,8 @@ else: #########################################################################
pr.add_argument("-gx", "--x-spacing", type=float, help="Volume x-spacing factor [1]", default=None, metavar='')
pr.add_argument("-gy", "--y-spacing", type=float, help="Volume y-spacing factor [1]", default=None, metavar='')
pr.add_argument("-gz", "--z-spacing", type=float, help="Volume z-spacing factor [1]", default=None, metavar='')
pr.add_argument("--slicer", help="Slicer Mode for 3D image files", action="store_true")
pr.add_argument("--slicer", help="Slicer Mode for volumetric data", action="store_true")
pr.add_argument("--slicer2d", help="2D Slicer Mode for volumetric data", action="store_true")
pr.add_argument("--lego", help="Voxel rendering for 3D image files", action="store_true")
pr.add_argument("--cmap", help="Voxel rendering color map name", default='jet', metavar='')
pr.add_argument("--mode", help="Voxel rendering composite mode", default=0, metavar='')
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
# cd ~/Projects/vtkplotter/notebooks/
# jupyter notebook > /dev/null 2>&1

# cd ~/Projects/vtkplotter/examples/
# cd ~/Projects/vtkplotter/
# rm -rf examples/*/.ipynb_checkpoints examples/*/*/.ipynb_checkpoints .ipynb_checkpoints/
# rm -rf examples/other/dolfin/navier_stokes_cylinder/ examples/other/dolfin/shuttle.xml
# rm examples/other/trimesh/featuretype.STL examples/other/trimesh/machinist.XAML
Expand Down
14 changes: 11 additions & 3 deletions vtkplotter/addons.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ def addScalarBar(mesh,
titleXOffset=0,
titleYOffset=15,
titleFontSize=12,
nlabels=10,
nlabels=None,
c=None,
horizontal=False,
width=None, height=None,
vmin=None, vmax=None,
):
"""Add a 2D scalar bar for the specified mesh.
Expand Down Expand Up @@ -153,18 +154,25 @@ def addScalarBar(mesh,

if horizontal:
sb.SetOrientationToHorizontal()
sb.SetNumberOfLabels(int((nlabels-1)/2.))
sb.SetNumberOfLabels(3)
sb.SetTextPositionToSucceedScalarBar()
sb.SetPosition(pos)
sb.SetMaximumWidthInPixels(1000)
sb.SetMaximumHeightInPixels(50)
else:
sb.SetNumberOfLabels(nlabels)
sb.SetNumberOfLabels(10)
sb.SetTextPositionToPrecedeScalarBar()
sb.SetPosition(pos[0]+0.07, pos[1])
sb.SetMaximumWidthInPixels(80)
sb.SetMaximumHeightInPixels(500)

if nlabels is not None:
sb.SetNumberOfLabels(nlabels)
# if width is not None:
# sb.SetMaximumWidthInPixels(int(width))
# if height is not None:
# sb.SetMaximumHeightInPixels(int(height))

sctxt = sb.GetLabelTextProperty()
sctxt.SetColor(c)
sctxt.SetShadow(0)
Expand Down
Loading

0 comments on commit 596a197

Please sign in to comment.