Commit a167122e980405f749a7826c93060379eec310cb

Authored by Thiago Franco de Moraes
1 parent 04381202
Exists in threshold_history

Starting to support undo and redo threshold changing

invesalius/data/mask.py
@@ -31,6 +31,16 @@ import imagedata_utils as iu @@ -31,6 +31,16 @@ import imagedata_utils as iu
31 31
32 from wx.lib.pubsub import pub as Publisher 32 from wx.lib.pubsub import pub as Publisher
33 33
  34 +class ThresholdHistoryNode(object):
  35 + def __init__(self, threshold):
  36 + self.threshold = threshold
  37 +
  38 + def commit_history(self, mask):
  39 + #Publisher.sendMessage('Changing threshold values', self.threshold)
  40 + mask.threshold_range = self.threshold
  41 + Publisher.sendMessage('Set threshold values no history', self.threshold)
  42 + Publisher.sendMessage('Reload actual slice')
  43 +
34 44
35 class EditionHistoryNode(object): 45 class EditionHistoryNode(object):
36 def __init__(self, index, orientation, array, clean=False): 46 def __init__(self, index, orientation, array, clean=False):
@@ -45,7 +55,8 @@ class EditionHistoryNode(object): @@ -45,7 +55,8 @@ class EditionHistoryNode(object):
45 numpy.save(self.filename, array) 55 numpy.save(self.filename, array)
46 print "Saving history", self.index, self.orientation, self.filename, self.clean 56 print "Saving history", self.index, self.orientation, self.filename, self.clean
47 57
48 - def commit_history(self, mvolume): 58 + def commit_history(self, mask):
  59 + mvolume = mask.matrix
49 array = numpy.load(self.filename) 60 array = numpy.load(self.filename)
50 if self.orientation == 'AXIAL': 61 if self.orientation == 'AXIAL':
51 mvolume[self.index+1,1:,1:] = array 62 mvolume[self.index+1,1:,1:] = array
@@ -84,6 +95,10 @@ class EditionHistory(object): @@ -84,6 +95,10 @@ class EditionHistory(object):
84 node = EditionHistoryNode(index, orientation, array, clean) 95 node = EditionHistoryNode(index, orientation, array, clean)
85 self.add(node) 96 self.add(node)
86 97
  98 + def new_theshold_node(self, threshold):
  99 + node = ThresholdHistoryNode(threshold)
  100 + self.add(node)
  101 +
87 def add(self, node): 102 def add(self, node):
88 if self.index == self.size: 103 if self.index == self.size:
89 self.history.pop(0) 104 self.history.pop(0)
@@ -98,50 +113,60 @@ class EditionHistory(object): @@ -98,50 +113,60 @@ class EditionHistory(object):
98 Publisher.sendMessage("Enable undo", True) 113 Publisher.sendMessage("Enable undo", True)
99 Publisher.sendMessage("Enable redo", False) 114 Publisher.sendMessage("Enable redo", False)
100 115
101 - def undo(self, mvolume, actual_slices=None): 116 + def undo(self, mask, actual_slices=None):
102 h = self.history 117 h = self.history
103 if self.index > 0: 118 if self.index > 0:
104 - #if self.index > 0 and h[self.index].clean:  
105 - ##self.index -= 1  
106 - ##h[self.index].commit_history(mvolume)  
107 - #self._reload_slice(self.index - 1)  
108 - if actual_slices and actual_slices[h[self.index - 1].orientation] != h[self.index - 1].index:  
109 - self._reload_slice(self.index - 1) 119 + if isinstance(h[self.index], EditionHistoryNode):
  120 + #if self.index > 0 and h[self.index].clean:
  121 + ##self.index -= 1
  122 + ##h[self.index].commit_history(mvolume)
  123 + #self._reload_slice(self.index - 1)
  124 + if actual_slices and actual_slices[h[self.index - 1].orientation] != h[self.index - 1].index:
  125 + self._reload_slice(self.index - 1)
  126 + else:
  127 + self.index -= 1
  128 + h[self.index].commit_history(mask)
  129 + if actual_slices and self.index and actual_slices[h[self.index - 1].orientation] == h[self.index - 1].index:
  130 + self.index -= 1
  131 + h[self.index].commit_history(mask)
  132 + self._reload_slice(self.index)
  133 + Publisher.sendMessage("Enable redo", True)
110 else: 134 else:
111 self.index -= 1 135 self.index -= 1
112 - h[self.index].commit_history(mvolume)  
113 - if actual_slices and self.index and actual_slices[h[self.index - 1].orientation] == h[self.index - 1].index:  
114 - self.index -= 1  
115 - h[self.index].commit_history(mvolume)  
116 - self._reload_slice(self.index) 136 + h[self.index].commit_history(mask)
117 Publisher.sendMessage("Enable redo", True) 137 Publisher.sendMessage("Enable redo", True)
118 138
119 if self.index == 0: 139 if self.index == 0:
120 Publisher.sendMessage("Enable undo", False) 140 Publisher.sendMessage("Enable undo", False)
121 - print "AT", self.index, len(self.history), self.history[self.index].filename 141 + #print "AT", self.index, len(self.history), self.history[self.index].filename
122 142
123 - def redo(self, mvolume, actual_slices=None): 143 + def redo(self, mask, actual_slices=None):
124 h = self.history 144 h = self.history
125 if self.index < len(h) - 1: 145 if self.index < len(h) - 1:
126 - #if self.index < len(h) - 1 and h[self.index].clean:  
127 - ##self.index += 1  
128 - ##h[self.index].commit_history(mvolume)  
129 - #self._reload_slice(self.index + 1)  
130 -  
131 - if actual_slices and actual_slices[h[self.index + 1].orientation] != h[self.index + 1].index:  
132 - self._reload_slice(self.index + 1) 146 + if isinstance(h[self.index], EditionHistoryNode):
  147 + #if self.index < len(h) - 1 and h[self.index].clean:
  148 + ##self.index += 1
  149 + ##h[self.index].commit_history(mvolume)
  150 + #self._reload_slice(self.index + 1)
  151 +
  152 + if actual_slices and actual_slices[h[self.index + 1].orientation] != h[self.index + 1].index:
  153 + self._reload_slice(self.index + 1)
  154 + else:
  155 + self.index += 1
  156 + h[self.index].commit_history(mask)
  157 + if actual_slices and self.index < len(h) - 1 and actual_slices[h[self.index + 1].orientation] == h[self.index + 1].index:
  158 + self.index += 1
  159 + h[self.index].commit_history(mask)
  160 + self._reload_slice(self.index)
  161 + Publisher.sendMessage("Enable undo", True)
133 else: 162 else:
134 self.index += 1 163 self.index += 1
135 - h[self.index].commit_history(mvolume)  
136 - if actual_slices and self.index < len(h) - 1 and actual_slices[h[self.index + 1].orientation] == h[self.index + 1].index:  
137 - self.index += 1  
138 - h[self.index].commit_history(mvolume)  
139 - self._reload_slice(self.index) 164 + h[self.index].commit_history(mask)
140 Publisher.sendMessage("Enable undo", True) 165 Publisher.sendMessage("Enable undo", True)
141 166
142 if self.index == len(h) - 1: 167 if self.index == len(h) - 1:
143 Publisher.sendMessage("Enable redo", False) 168 Publisher.sendMessage("Enable redo", False)
144 - print "AT", self.index, len(h), h[self.index].filename 169 + #print "AT", self.index, len(h), h[self.index].filename
145 170
146 def _reload_slice(self, index): 171 def _reload_slice(self, index):
147 Publisher.sendMessage(('Set scroll position', self.history[index].orientation), 172 Publisher.sendMessage(('Set scroll position', self.history[index].orientation),
@@ -191,14 +216,17 @@ class Mask(): @@ -191,14 +216,17 @@ class Mask():
191 Publisher.subscribe(self.OnFlipVolume, 'Flip volume') 216 Publisher.subscribe(self.OnFlipVolume, 'Flip volume')
192 Publisher.subscribe(self.OnSwapVolumeAxes, 'Swap volume axes') 217 Publisher.subscribe(self.OnSwapVolumeAxes, 'Swap volume axes')
193 218
194 - def save_history(self, index, orientation, array, p_array, clean=False): 219 + def save_edition_history(self, index, orientation, array, p_array, clean=False):
195 self.history.new_node(index, orientation, array, p_array, clean) 220 self.history.new_node(index, orientation, array, p_array, clean)
196 221
  222 + def save_threshold_history(self, threshold):
  223 + self.history.new_theshold_node(threshold)
  224 +
197 def undo_history(self, actual_slices): 225 def undo_history(self, actual_slices):
198 - self.history.undo(self.matrix, actual_slices) 226 + self.history.undo(self, actual_slices)
199 227
200 def redo_history(self, actual_slices): 228 def redo_history(self, actual_slices):
201 - self.history.redo(self.matrix, actual_slices) 229 + self.history.redo(self, actual_slices)
202 230
203 def on_show(self): 231 def on_show(self):
204 self.history._config_undo_redo(self.is_shown) 232 self.history._config_undo_redo(self.is_shown)
invesalius/data/slice_.py
@@ -258,7 +258,7 @@ class Slice(object): @@ -258,7 +258,7 @@ class Slice(object):
258 index = self.current_mask.index 258 index = self.current_mask.index
259 self.num_gradient += 1 259 self.num_gradient += 1
260 self.current_mask.matrix[:] = 0 260 self.current_mask.matrix[:] = 0
261 - self.current_mask.clear_history() 261 + #self.current_mask.clear_history()
262 262
263 # TODO: merge this code with apply_slice_buffer_to_mask 263 # TODO: merge this code with apply_slice_buffer_to_mask
264 b_mask = self.buffer_slices["AXIAL"].mask 264 b_mask = self.buffer_slices["AXIAL"].mask
@@ -276,6 +276,8 @@ class Slice(object): @@ -276,6 +276,8 @@ class Slice(object):
276 self.current_mask.matrix[1:, 1:, n] = b_mask 276 self.current_mask.matrix[1:, 1:, n] = b_mask
277 self.current_mask.matrix[0, 0, n] = 1 277 self.current_mask.matrix[0, 0, n] = 1
278 278
  279 + self.current_mask.save_threshold_history(threshold_range)
  280 +
279 def __set_current_mask_threshold_actual_slice(self, evt_pubsub): 281 def __set_current_mask_threshold_actual_slice(self, evt_pubsub):
280 threshold_range = evt_pubsub.data 282 threshold_range = evt_pubsub.data
281 index = self.current_mask.index 283 index = self.current_mask.index
@@ -908,7 +910,7 @@ class Slice(object): @@ -908,7 +910,7 @@ class Slice(object):
908 # TODO: Voltar a usar marcacao na mascara 910 # TODO: Voltar a usar marcacao na mascara
909 if orientation == 'AXIAL': 911 if orientation == 'AXIAL':
910 #if self.current_mask.matrix[index+1, 0, 0] != 2: 912 #if self.current_mask.matrix[index+1, 0, 0] != 2:
911 - #self.current_mask.save_history(index, orientation, 913 + #self.current_mask.save_edition_history(index, orientation,
912 #self.current_mask.matrix[index+1,1:,1:], 914 #self.current_mask.matrix[index+1,1:,1:],
913 #clean=True) 915 #clean=True)
914 p_mask = self.current_mask.matrix[index+1,1:,1:].copy() 916 p_mask = self.current_mask.matrix[index+1,1:,1:].copy()
@@ -917,7 +919,7 @@ class Slice(object): @@ -917,7 +919,7 @@ class Slice(object):
917 919
918 elif orientation == 'CORONAL': 920 elif orientation == 'CORONAL':
919 #if self.current_mask.matrix[0, index+1, 0] != 2: 921 #if self.current_mask.matrix[0, index+1, 0] != 2:
920 - #self.current_mask.save_history(index, orientation, 922 + #self.current_mask.save_edition_history(index, orientation,
921 #self.current_mask.matrix[1:, index+1, 1:], 923 #self.current_mask.matrix[1:, index+1, 1:],
922 #clean=True) 924 #clean=True)
923 p_mask = self.current_mask.matrix[1:, index+1, 1:].copy() 925 p_mask = self.current_mask.matrix[1:, index+1, 1:].copy()
@@ -926,14 +928,14 @@ class Slice(object): @@ -926,14 +928,14 @@ class Slice(object):
926 928
927 elif orientation == 'SAGITAL': 929 elif orientation == 'SAGITAL':
928 #if self.current_mask.matrix[0, 0, index+1] != 2: 930 #if self.current_mask.matrix[0, 0, index+1] != 2:
929 - #self.current_mask.save_history(index, orientation, 931 + #self.current_mask.save_edition_history(index, orientation,
930 #self.current_mask.matrix[1:, 1:, index+1], 932 #self.current_mask.matrix[1:, 1:, index+1],
931 #clean=True) 933 #clean=True)
932 p_mask = self.current_mask.matrix[1:, 1:, index+1].copy() 934 p_mask = self.current_mask.matrix[1:, 1:, index+1].copy()
933 self.current_mask.matrix[1:, 1:, index+1] = b_mask 935 self.current_mask.matrix[1:, 1:, index+1] = b_mask
934 self.current_mask.matrix[0, 0, index+1] = 2 936 self.current_mask.matrix[0, 0, index+1] = 2
935 937
936 - self.current_mask.save_history(index, orientation, b_mask, p_mask) 938 + self.current_mask.save_edition_history(index, orientation, b_mask, p_mask)
937 self.current_mask.was_edited = True 939 self.current_mask.was_edited = True
938 940
939 for o in self.buffer_slices: 941 for o in self.buffer_slices: