Commit c5c9a4728a6275b102029cf907d6eb19d008f954

Authored by tatiana
1 parent 4303f718

ADD: Surface duplication support on the way

Showing 1 changed file with 53 additions and 8 deletions   Show diff stats
invesalius/data/surface.py
@@ -33,6 +33,7 @@ import polydata_utils as pu @@ -33,6 +33,7 @@ import polydata_utils as pu
33 import project as prj 33 import project as prj
34 import session as ses 34 import session as ses
35 import surface_process 35 import surface_process
  36 +import utils as utl
36 import vtk_utils as vu 37 import vtk_utils as vu
37 38
38 class Surface(): 39 class Surface():
@@ -125,17 +126,47 @@ class SurfaceManager(): @@ -125,17 +126,47 @@ class SurfaceManager():
125 'Create surface from largest region') 126 'Create surface from largest region')
126 ps.Publisher().subscribe(self.OnSeedSurface, "Create surface from seeds") 127 ps.Publisher().subscribe(self.OnSeedSurface, "Create surface from seeds")
127 128
128 - 129 + ps.Publisher().subscribe(self.OnDuplicate, "Duplicate surfaces")
129 ps.Publisher().subscribe(self.OnRemove,"Remove surfaces") 130 ps.Publisher().subscribe(self.OnRemove,"Remove surfaces")
  131 + ps.Publisher().subscribe(self.OnDuplicate, "Duplicate surfaces")
  132 +
  133 +
  134 + def OnDuplicate(self, pubsub_evt):
  135 +
  136 + selected_items = pubsub_evt.data
  137 + proj = prj.Project()
  138 + surface_dict = proj.surface_dict
  139 + for index in selected_items:
  140 + original_surface = surface_dict[index]
  141 + # compute copy name
  142 + name = original_surface.name
  143 + names_list = [surface_dict[i].name for i in surface_dict.keys()]
  144 + new_name = utl.next_copy_name(name, names_list)
  145 + # create new mask
  146 + self.CreateSurfaceFromPolydata(polydata = original_surface.polydata,
  147 + overwrite = False,
  148 + name = new_name,
  149 + colour = original_surface.colour,
  150 + transparency = original_surface.transparency,
  151 + volume = original_surface.volume)
  152 +
130 153
131 154
132 def OnRemove(self, pubsub_evt): 155 def OnRemove(self, pubsub_evt):
  156 + print "OnRemove"
133 selected_items = pubsub_evt.data 157 selected_items = pubsub_evt.data
134 proj = prj.Project() 158 proj = prj.Project()
135 for item in selected_items: 159 for item in selected_items:
  160 + print "before"
  161 + print "1:", proj.surface_dict
  162 + print "2:", self.actors_dict
136 proj.RemoveSurface(item) 163 proj.RemoveSurface(item)
  164 + print "after"
  165 + print "1", proj.surface_dict
137 actor = self.actors_dict[item] 166 actor = self.actors_dict[item]
138 self.actors_dict.pop(item) 167 self.actors_dict.pop(item)
  168 + print "2", self.actors_dict
  169 + print "\n"
139 ps.Publisher().sendMessage('Remove surface actor from viewer', actor) 170 ps.Publisher().sendMessage('Remove surface actor from viewer', actor)
140 171
141 def OnSeedSurface(self, pubsub_evt): 172 def OnSeedSurface(self, pubsub_evt):
@@ -187,7 +218,9 @@ class SurfaceManager(): @@ -187,7 +218,9 @@ class SurfaceManager():
187 new_index = self.CreateSurfaceFromPolydata(new_polydata) 218 new_index = self.CreateSurfaceFromPolydata(new_polydata)
188 ps.Publisher().sendMessage('Show single surface', (new_index, True)) 219 ps.Publisher().sendMessage('Show single surface', (new_index, True))
189 220
190 - def CreateSurfaceFromPolydata(self, polydata, overwrite=False): 221 + def CreateSurfaceFromPolydata(self, polydata, overwrite=False,
  222 + name=None, colour=None,
  223 + transparency=None, volume=None):
191 224
192 normals = vtk.vtkPolyDataNormals() 225 normals = vtk.vtkPolyDataNormals()
193 normals.SetInput(polydata) 226 normals.SetInput(polydata)
@@ -206,9 +239,18 @@ class SurfaceManager(): @@ -206,9 +239,18 @@ class SurfaceManager():
206 else: 239 else:
207 surface = Surface() 240 surface = Surface()
208 241
209 - surface.colour = random.choice(const.SURFACE_COLOUR) 242 + if not colour:
  243 + surface.colour = random.choice(const.SURFACE_COLOUR)
  244 + else:
  245 + surface.colour = colour
210 surface.polydata = polydata 246 surface.polydata = polydata
211 247
  248 + if transparency:
  249 + surface.transparency = transparency
  250 +
  251 + if name:
  252 + surface.name = name
  253 +
212 # Set actor colour and transparency 254 # Set actor colour and transparency
213 actor.GetProperty().SetColor(surface.colour) 255 actor.GetProperty().SetColor(surface.colour)
214 actor.GetProperty().SetOpacity(1-surface.transparency) 256 actor.GetProperty().SetOpacity(1-surface.transparency)
@@ -227,10 +269,13 @@ class SurfaceManager(): @@ -227,10 +269,13 @@ class SurfaceManager():
227 session.ChangeProject() 269 session.ChangeProject()
228 270
229 # The following lines have to be here, otherwise all volumes disappear 271 # The following lines have to be here, otherwise all volumes disappear
230 - measured_polydata = vtk.vtkMassProperties()  
231 - measured_polydata.SetInput(polydata)  
232 - volume = measured_polydata.GetVolume()  
233 - surface.volume = volume 272 + if not volume:
  273 + measured_polydata = vtk.vtkMassProperties()
  274 + measured_polydata.SetInput(polydata)
  275 + volume = measured_polydata.GetVolume()
  276 + surface.volume = volume
  277 + else:
  278 + surface.volume = volume
234 self.last_surface_index = surface.index 279 self.last_surface_index = surface.index
235 280
236 ps.Publisher().sendMessage('Load surface actor into viewer', actor) 281 ps.Publisher().sendMessage('Load surface actor into viewer', actor)
@@ -568,7 +613,7 @@ class SurfaceManager(): @@ -568,7 +613,7 @@ class SurfaceManager():
568 polydata_list.append(surface.polydata) 613 polydata_list.append(surface.polydata)
569 614
570 if len(polydata_list) == 0: 615 if len(polydata_list) == 0:
571 - print "oops - no polydata" 616 + utl.debug("oops - no polydata")
572 return 617 return
573 elif len(polydata_list) == 1: 618 elif len(polydata_list) == 1:
574 polydata = polydata_list[0] 619 polydata = polydata_list[0]