Skip to content

Commit

Permalink
Merge branch 'change_view_actions'
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaty committed Oct 12, 2015
2 parents 966bb04 + a5f761f commit b65ee47
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 104 deletions.
4 changes: 1 addition & 3 deletions tissuelab/gui/vtkviewer/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,6 @@ def LeftButtonPressEvent(self, obj, event):
break
else:
self.selectedLabel = -1
print self.selectedLabel
self.InvokeEvent("CellSelected")
self.refresh()

Expand Down Expand Up @@ -1305,7 +1304,7 @@ def MouseMoveEvent(self, obj, event):
def KeyPressEvent(self, obj, event):
key = self.GetInteractor().GetKeyCode()
if key == 'z':
print "wip"
raise NotImplementedError

def find_point_to_move(self, coord):
if self.select_neighboor_mode == 'propagationSphere':
Expand Down Expand Up @@ -1513,7 +1512,6 @@ def split_considered_cell(self):
if i not in label_in_matrix:
new_label = i
break
print new_label
self.polyList[new_label] = cleaning1.GetOutput()

self.refresh()
Expand Down
163 changes: 82 additions & 81 deletions tissuelab/gui/vtkviewer/point_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
import numpy as np
import vtk

from scipy.cluster.vq import vq
from scipy.cluster.vq import vq
from copy import deepcopy

from openalea.core.world.world import World
from tissuelab.gui.vtkviewer.vtkworldviewer import world_kwargs


class SelectPointInteractorStyle (vtk.vtkInteractorStyleTrackballCamera):

def __init__(self, parent=None, world=None, world_object=None):
Expand All @@ -52,7 +53,7 @@ def __init__(self, parent=None, world=None, world_object=None):
self.selected_axis_polydata_actor = vtk.vtkActor()

self.motion_plane = vtk.vtkPlaneSource()
self.motion_plane.SetResolution(100,100)
self.motion_plane.SetResolution(100, 100)
self.motion_plane.SetCenter(0.0, 0.0, 0.0)
self.motion_plane.SetNormal(0.0, 0.0, 1.0)

Expand All @@ -61,10 +62,10 @@ def __init__(self, parent=None, world=None, world_object=None):
# self.motion_plane.SetPoint1(100.*np.array(self.motion_plane.GetPoint1()))
# self.motion_plane.SetPoint2(np.cross([0,0,1],100.*np.array(self.motion_plane.GetPoint1())))
self.motion_plane.Update()

motion_plane_mapper = vtk.vtkPolyDataMapper()
motion_plane_mapper.SetInputConnection(self.motion_plane.GetOutputPort())

self.motion_plane_actor = vtk.vtkActor()
self.motion_plane_actor.SetMapper(motion_plane_mapper)
self.motion_plane_actor.GetProperty().SetColor(1.0, 0.0, 1.0)
Expand All @@ -73,7 +74,7 @@ def __init__(self, parent=None, world=None, world_object=None):
if world is None:
self.world = World()
else:
self.world = world
self.world = world

def add_selected_cell(self):
self.cell_sphere.SetRadius(2.0)
Expand All @@ -83,28 +84,27 @@ def add_selected_cell(self):

cell_mapper = vtk.vtkPolyDataMapper()
cell_mapper.SetInputConnection(self.cell_sphere.GetOutputPort())

self.cell_actor.SetMapper(cell_mapper)
self.cell_actor.GetProperty().SetColor(1.0, 1.0, 1.0)
self.cell_actor.GetProperty().SetOpacity(0.3)

self.world.add(self.cell_actor,"selected_cell")

self.world.add(self.cell_actor, "selected_cell")

def add_axes(self):
cell_center = np.array(self.cell_sphere.GetCenter())
points = {}
points['x-'] = deepcopy(cell_center )
points['x-'] = deepcopy(cell_center)
points['x-'][0] -= 1000.
points['x+'] = deepcopy(cell_center )
points['x+'] = deepcopy(cell_center)
points['x+'][0] += 1000.
points['y-'] = deepcopy(cell_center )
points['y-'] = deepcopy(cell_center)
points['y-'][1] -= 1000.
points['y+'] = deepcopy(cell_center )
points['y+'] = deepcopy(cell_center)
points['y+'][1] += 1000.
points['z-'] = deepcopy(cell_center )
points['z-'] = deepcopy(cell_center)
points['z-'][2] -= 1000.
points['z+'] = deepcopy(cell_center )
points['z+'] = deepcopy(cell_center)
points['z+'][2] += 1000.

axis_points = vtk.vtkPoints()
Expand All @@ -116,9 +116,9 @@ def add_axes(self):
axis_points.InsertNextPoint(points['z+'])

colors = {}
colors['red'] = (255,0,0)
colors['green'] = (0,255,0)
colors['blue'] = (0,0,255)
colors['red'] = (255, 0, 0)
colors['green'] = (0, 255, 0)
colors['blue'] = (0, 0, 255)

#axis_colors.SetNumberOfComponents(3)
#axis_colors.InsertNextTupleValue(colors['red'])
Expand All @@ -129,20 +129,20 @@ def add_axes(self):
axis_colors.SetNumberOfComponents(3)
axis_lines = vtk.vtkCellArray()
line = vtk.vtkLine()
line.GetPointIds().SetId(0,0)
line.GetPointIds().SetId(1,1)
line.GetPointIds().SetId(0, 0)
line.GetPointIds().SetId(1, 1)
line_id = axis_lines.InsertNextCell(line)
axis_colors.InsertTupleValue(line_id,colors['red'])
axis_colors.InsertTupleValue(line_id, colors['red'])
line = vtk.vtkLine()
line.GetPointIds().SetId(0,2)
line.GetPointIds().SetId(1,3)
line.GetPointIds().SetId(0, 2)
line.GetPointIds().SetId(1, 3)
line_id = axis_lines.InsertNextCell(line)
axis_colors.InsertTupleValue(line_id,colors['green'])
axis_colors.InsertTupleValue(line_id, colors['green'])
line = vtk.vtkLine()
line.GetPointIds().SetId(0,4)
line.GetPointIds().SetId(1,5)
line.GetPointIds().SetId(0, 4)
line.GetPointIds().SetId(1, 5)
line_id = axis_lines.InsertNextCell(line)
axis_colors.InsertTupleValue(line_id,colors['blue'])
axis_colors.InsertTupleValue(line_id, colors['blue'])

self.axis_polydata.SetPoints(axis_points)
self.axis_polydata.SetLines(axis_lines)
Expand All @@ -156,23 +156,23 @@ def add_axes(self):
axis_mapper.SetInputData(self.axis_polydata)

self.axis_polydata_actor.SetMapper(axis_mapper)
self.world.add(self.axis_polydata_actor,"axes")
self.world.add(self.axis_polydata_actor, "axes")
#self.GetCurrentRenderer().AddActor(axis_polydata_actor)

def add_selected_axis(self):
cell_center = np.array(self.cell_sphere.GetCenter())
points = {}
points['x-'] = deepcopy(cell_center )
points['x-'] = deepcopy(cell_center)
points['x-'][0] -= 1000.
points['x+'] = deepcopy(cell_center )
points['x+'] = deepcopy(cell_center)
points['x+'][0] += 1000.
points['y-'] = deepcopy(cell_center )
points['y-'] = deepcopy(cell_center)
points['y-'][1] -= 1000.
points['y+'] = deepcopy(cell_center )
points['y+'] = deepcopy(cell_center)
points['y+'][1] += 1000.
points['z-'] = deepcopy(cell_center )
points['z-'] = deepcopy(cell_center)
points['z-'][2] -= 1000.
points['z+'] = deepcopy(cell_center )
points['z+'] = deepcopy(cell_center)
points['z+'][2] += 1000.

selected_axis_points = vtk.vtkPoints()
Expand All @@ -184,31 +184,31 @@ def add_selected_axis(self):
selected_axis_points.InsertNextPoint(points['z+'])

colors = {}
colors['red'] = (255,0,0)
colors['green'] = (0,255,0)
colors['blue'] = (0,0,255)
colors['red'] = (255, 0, 0)
colors['green'] = (0, 255, 0)
colors['blue'] = (0, 0, 255)

selected_axis_colors = vtk.vtkUnsignedCharArray()
selected_axis_colors.SetNumberOfComponents(3)
selected_axis_lines = vtk.vtkCellArray()
line = vtk.vtkLine()
line.GetPointIds().SetId(0,0)
line.GetPointIds().SetId(1,1)
line.GetPointIds().SetId(0, 0)
line.GetPointIds().SetId(1, 1)
if self.selected_axis == 'x':
line_id = selected_axis_lines.InsertNextCell(line)
selected_axis_colors.InsertTupleValue(line_id,colors['red'])
selected_axis_colors.InsertTupleValue(line_id, colors['red'])
line = vtk.vtkLine()
line.GetPointIds().SetId(0,2)
line.GetPointIds().SetId(1,3)
line.GetPointIds().SetId(0, 2)
line.GetPointIds().SetId(1, 3)
if self.selected_axis == 'y':
line_id = selected_axis_lines.InsertNextCell(line)
selected_axis_colors.InsertTupleValue(line_id,colors['green'])
selected_axis_colors.InsertTupleValue(line_id, colors['green'])
line = vtk.vtkLine()
line.GetPointIds().SetId(0,4)
line.GetPointIds().SetId(1,5)
line.GetPointIds().SetId(0, 4)
line.GetPointIds().SetId(1, 5)
if self.selected_axis == 'z':
line_id = selected_axis_lines.InsertNextCell(line)
selected_axis_colors.InsertTupleValue(line_id,colors['blue'])
selected_axis_colors.InsertTupleValue(line_id, colors['blue'])

self.selected_axis_polydata.SetPoints(selected_axis_points)
self.selected_axis_polydata.SetLines(selected_axis_lines)
Expand All @@ -222,20 +222,19 @@ def add_selected_axis(self):

self.selected_axis_polydata_actor.SetMapper(selected_axis_mapper)
self.selected_axis_polydata_actor.GetProperty().SetLineWidth(3)
self.world.add(self.selected_axis_polydata_actor,"selected_axis")
self.world.add(self.selected_axis_polydata_actor, "selected_axis")

def update_motion_plane(self):
camera_center = np.array(self.GetCurrentRenderer().GetActiveCamera().GetPosition())
camera_focal_point = np.array(self.GetCurrentRenderer().GetActiveCamera().GetFocalPoint())
cell_center = np.array(self.cell_sphere.GetCenter())
self.motion_plane.SetCenter(cell_center)
self.motion_plane.SetNormal((camera_center-cell_center)/np.linalg.norm(camera_center-cell_center))
self.motion_plane.SetNormal((camera_center - cell_center) / np.linalg.norm(camera_center - cell_center))
# self.motion_plane.SetNormal((camera_center-camera_focal_point)/np.linalg.norm(camera_center-camera_focal_point))
self.motion_plane.Update()
# self.world.add(self.motion_plane_actor,"motion_plane")

# self.motion_picker.AddPickList(self.motion_plane_actor)

# self.motion_picker.AddPickList(self.motion_plane_actor)

def LeftButtonPressed(self, obj, event):
if self.GetInteractor().GetShiftKey():
Expand All @@ -247,9 +246,8 @@ def LeftButtonPressed(self, obj, event):
points.GetPoint(0, coord)

if (self.GetInteractor().GetPicker().GetPointId() != -1):
if len(self.data.points)>0:
print coord
pick_cell_matching = vq(np.array([coord]),np.array(self.data.points.values()))
if len(self.data.points) > 0:
pick_cell_matching = vq(np.array([coord]), np.array(self.data.points.values()))
label = self.data.points.keys()[pick_cell_matching[0][0]]
self.selected_cell = label

Expand All @@ -258,7 +256,7 @@ def LeftButtonPressed(self, obj, event):
self.add_axes()
self.grab_mode = False
vtk.vtkInteractorStyleTrackballCamera.OnLeftButtonDown(self)

if self.selected_cell == -1:
self.world.remove('selected_cell')
self.world.remove('axes')
Expand All @@ -267,8 +265,6 @@ def LeftButtonPressed(self, obj, event):
# self.world.remove("motion_plane")
self.selected_cell == -1



def KeyPressed(self, obj, event):
#key = obj.GetKeySym()
#key = self.GetInteractor().GetKeySym()
Expand Down Expand Up @@ -311,7 +307,7 @@ def KeyPressed(self, obj, event):
# self.world.add(self.cell_actor,"selected_cell")
# self.world.add(self.axis_polydata_actor,"axes")
# # self.add_axes()
if key in ['x','y','z']:
if key in ['x', 'y', 'z']:
if self.grab_mode:
if self.selected_axis != key:
self.selected_axis = key
Expand All @@ -332,7 +328,6 @@ def KeyPressed(self, obj, event):
self.grab_mode = True
self.selected_axis = 'x'
self.add_selected_axis()
print "Grab Mode : On!"
self.update_motion_plane()
# self.GetInteractor().GetPicker().InitializePickList()
self.GetInteractor().GetPicker().AddPickList(self.motion_plane_actor)
Expand All @@ -349,7 +344,7 @@ def KeyPressed(self, obj, event):
elif key == 's':
# self.data.points[self.selected_cell] = self.world['selected_cell'].data.points[self.selected_cell]
self.data.points[self.selected_cell] = np.array(self.cell_sphere.GetCenter())
self.world.add(self.data,self.world_object.name)
self.world.add(self.data, self.world_object.name)
self.world.remove('selected_cell')
self.world.remove('axes')
self.grab_mode = False
Expand All @@ -359,9 +354,9 @@ def KeyPressed(self, obj, event):
self.GetInteractor().GetPicker().DeletePickList(self.motion_plane_actor)
self.GetInteractor().GetPicker().PickFromListOff()
elif key == 'd':
if self.data.points.has_key(self.selected_cell):
if self.selected_cell in self.data.points:
del self.data.points[self.selected_cell]
self.world.add(self.data,self.world_object.name,**world_kwargs(self.world_object))
self.world.add(self.data, self.world_object.name, **world_kwargs(self.world_object))
self.world.remove('selected_cell')
self.world.remove('axes')
self.grab_mode = False
Expand All @@ -374,11 +369,10 @@ def KeyPressed(self, obj, event):
def MouseMoved(self, obj, event):
if self.grab_mode:
pos = self.GetInteractor().GetEventPosition()
print pos
self.GetInteractor().GetPicker().Pick(pos[0], pos[1], 0, self.GetCurrentRenderer())
# points = self.GetInteractor().GetPicker().GetPickedPositions()
coord = np.zeros(3)

mouse_position = self.GetInteractor().GetPicker().GetPickPosition()
# points.GetPoint(0, coord)
# print coord
Expand All @@ -393,40 +387,47 @@ def MouseMoved(self, obj, event):

if self.selected_axis is None:
plane_normal = np.array(self.motion_plane.GetNormal())
plane_normal = plane_normal/np.linalg.norm(plane_normal)
plane_normal = plane_normal / np.linalg.norm(plane_normal)
plane_vector_1 = np.array(self.motion_plane.GetPoint1()) - cell_center
plane_vector_1 = plane_vector_1/np.linalg.norm(plane_vector_1)
plane_vector_2 = np.cross(plane_normal,plane_vector_1)
plane_vector_1 = plane_vector_1 / np.linalg.norm(plane_vector_1)
plane_vector_2 = np.cross(plane_normal, plane_vector_1)
new_cell_center = deepcopy(cell_center)
new_cell_center += np.dot(mouse_position-cell_center,plane_vector_1)*plane_vector_1
new_cell_center += np.dot(mouse_position-cell_center,plane_vector_2)*plane_vector_2
new_cell_center += np.dot(mouse_position - cell_center, plane_vector_1) * plane_vector_1
new_cell_center += np.dot(mouse_position - cell_center, plane_vector_2) * plane_vector_2
# new_cell_center = mouse_position
elif self.selected_axis == 'x':
new_cell_center = cell_center + np.dot(mouse_position-cell_center,np.array([1,0,0]))*np.array([1,0,0])
new_cell_center = cell_center + \
np.dot(mouse_position - cell_center, np.array([1, 0, 0])) * np.array([1, 0, 0])
elif self.selected_axis == 'y':
new_cell_center = cell_center + np.dot(mouse_position-cell_center,np.array([0,1,0]))*np.array([0,1,0])
new_cell_center = cell_center + \
np.dot(mouse_position - cell_center, np.array([0, 1, 0])) * np.array([0, 1, 0])
elif self.selected_axis == 'z':
new_cell_center = cell_center + np.dot(mouse_position-cell_center,np.array([0,0,1]))*np.array([0,0,1])
new_cell_center = cell_center + \
np.dot(mouse_position - cell_center, np.array([0, 0, 1])) * np.array([0, 0, 1])
self.cell_sphere.SetCenter(new_cell_center)

for p in xrange(self.axis_polydata.GetNumberOfPoints()):
self.axis_polydata.GetPoints().SetPoint(p,(np.array(self.axis_polydata.GetPoint(p))+new_cell_center-cell_center))
self.axis_polydata.GetPoints().SetPoint(
p,
(np.array(
self.axis_polydata.GetPoint(p)) +
new_cell_center -
cell_center))

self.world.add(self.cell_actor,"selected_cell")
self.world.add(self.axis_polydata_actor,"axes")
self.world.add(self.cell_actor, "selected_cell")
self.world.add(self.axis_polydata_actor, "axes")

if self.selected_axis is not None:
for p in xrange(self.selected_axis_polydata.GetNumberOfPoints()):
self.selected_axis_polydata.GetPoints().SetPoint(p,(np.array(self.selected_axis_polydata.GetPoint(p))+new_cell_center-cell_center))
self.world.add(self.selected_axis_polydata_actor,"selected_axis")
self.selected_axis_polydata.GetPoints().SetPoint(
p,
(np.array(
self.selected_axis_polydata.GetPoint(p)) +
new_cell_center -
cell_center))
self.world.add(self.selected_axis_polydata_actor, "selected_axis")

self.update_motion_plane()
vtk.vtkInteractorStyleTrackballCamera.OnMouseMove(self)
else:
vtk.vtkInteractorStyleTrackballCamera.OnMouseMove(self)






Loading

0 comments on commit b65ee47

Please sign in to comment.