Commit a167122e980405f749a7826c93060379eec310cb
1 parent
04381202
Exists in
threshold_history
Starting to support undo and redo threshold changing
Showing
2 changed files
with
66 additions
and
36 deletions
Show diff stats
invesalius/data/mask.py
| ... | ... | @@ -31,6 +31,16 @@ import imagedata_utils as iu |
| 31 | 31 | |
| 32 | 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 | 45 | class EditionHistoryNode(object): |
| 36 | 46 | def __init__(self, index, orientation, array, clean=False): |
| ... | ... | @@ -45,7 +55,8 @@ class EditionHistoryNode(object): |
| 45 | 55 | numpy.save(self.filename, array) |
| 46 | 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 | 60 | array = numpy.load(self.filename) |
| 50 | 61 | if self.orientation == 'AXIAL': |
| 51 | 62 | mvolume[self.index+1,1:,1:] = array |
| ... | ... | @@ -84,6 +95,10 @@ class EditionHistory(object): |
| 84 | 95 | node = EditionHistoryNode(index, orientation, array, clean) |
| 85 | 96 | self.add(node) |
| 86 | 97 | |
| 98 | + def new_theshold_node(self, threshold): | |
| 99 | + node = ThresholdHistoryNode(threshold) | |
| 100 | + self.add(node) | |
| 101 | + | |
| 87 | 102 | def add(self, node): |
| 88 | 103 | if self.index == self.size: |
| 89 | 104 | self.history.pop(0) |
| ... | ... | @@ -98,50 +113,60 @@ class EditionHistory(object): |
| 98 | 113 | Publisher.sendMessage("Enable undo", True) |
| 99 | 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 | 117 | h = self.history |
| 103 | 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 | 134 | else: |
| 111 | 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 | 137 | Publisher.sendMessage("Enable redo", True) |
| 118 | 138 | |
| 119 | 139 | if self.index == 0: |
| 120 | 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 | 144 | h = self.history |
| 125 | 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 | 162 | else: |
| 134 | 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 | 165 | Publisher.sendMessage("Enable undo", True) |
| 141 | 166 | |
| 142 | 167 | if self.index == len(h) - 1: |
| 143 | 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 | 171 | def _reload_slice(self, index): |
| 147 | 172 | Publisher.sendMessage(('Set scroll position', self.history[index].orientation), |
| ... | ... | @@ -191,14 +216,17 @@ class Mask(): |
| 191 | 216 | Publisher.subscribe(self.OnFlipVolume, 'Flip volume') |
| 192 | 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 | 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 | 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 | 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 | 231 | def on_show(self): |
| 204 | 232 | self.history._config_undo_redo(self.is_shown) | ... | ... |
invesalius/data/slice_.py
| ... | ... | @@ -258,7 +258,7 @@ class Slice(object): |
| 258 | 258 | index = self.current_mask.index |
| 259 | 259 | self.num_gradient += 1 |
| 260 | 260 | self.current_mask.matrix[:] = 0 |
| 261 | - self.current_mask.clear_history() | |
| 261 | + #self.current_mask.clear_history() | |
| 262 | 262 | |
| 263 | 263 | # TODO: merge this code with apply_slice_buffer_to_mask |
| 264 | 264 | b_mask = self.buffer_slices["AXIAL"].mask |
| ... | ... | @@ -276,6 +276,8 @@ class Slice(object): |
| 276 | 276 | self.current_mask.matrix[1:, 1:, n] = b_mask |
| 277 | 277 | self.current_mask.matrix[0, 0, n] = 1 |
| 278 | 278 | |
| 279 | + self.current_mask.save_threshold_history(threshold_range) | |
| 280 | + | |
| 279 | 281 | def __set_current_mask_threshold_actual_slice(self, evt_pubsub): |
| 280 | 282 | threshold_range = evt_pubsub.data |
| 281 | 283 | index = self.current_mask.index |
| ... | ... | @@ -908,7 +910,7 @@ class Slice(object): |
| 908 | 910 | # TODO: Voltar a usar marcacao na mascara |
| 909 | 911 | if orientation == 'AXIAL': |
| 910 | 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 | 914 | #self.current_mask.matrix[index+1,1:,1:], |
| 913 | 915 | #clean=True) |
| 914 | 916 | p_mask = self.current_mask.matrix[index+1,1:,1:].copy() |
| ... | ... | @@ -917,7 +919,7 @@ class Slice(object): |
| 917 | 919 | |
| 918 | 920 | elif orientation == 'CORONAL': |
| 919 | 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 | 923 | #self.current_mask.matrix[1:, index+1, 1:], |
| 922 | 924 | #clean=True) |
| 923 | 925 | p_mask = self.current_mask.matrix[1:, index+1, 1:].copy() |
| ... | ... | @@ -926,14 +928,14 @@ class Slice(object): |
| 926 | 928 | |
| 927 | 929 | elif orientation == 'SAGITAL': |
| 928 | 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 | 932 | #self.current_mask.matrix[1:, 1:, index+1], |
| 931 | 933 | #clean=True) |
| 932 | 934 | p_mask = self.current_mask.matrix[1:, 1:, index+1].copy() |
| 933 | 935 | self.current_mask.matrix[1:, 1:, index+1] = b_mask |
| 934 | 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 | 939 | self.current_mask.was_edited = True |
| 938 | 940 | |
| 939 | 941 | for o in self.buffer_slices: | ... | ... |