Commit 9519edaa1d0ffd159f193aab1ae2cdb8de2b8a84
1 parent
5722493b
Exists in
master
Using Isort and Black to improve code format in geometry
Showing
1 changed file
with
203 additions
and
152 deletions
Show diff stats
invesalius/data/geometry.py
| 1 | -#-------------------------------------------------------------------------- | |
| 1 | +# -------------------------------------------------------------------------- | |
| 2 | 2 | # Software: InVesalius - Software de Reconstrucao 3D de Imagens Medicas |
| 3 | 3 | # Copyright: (C) 2001 Centro de Pesquisas Renato Archer |
| 4 | -# Homepage: http://www.softwarepublico.gov.br / | |
| 4 | +# Homepage: http://www.softwarepublico.gov.br/ | |
| 5 | 5 | # http://www.cti.gov.br/invesalius |
| 6 | 6 | # Contact: invesalius@cti.gov.br |
| 7 | 7 | # License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt) |
| 8 | -#-------------------------------------------------------------------------- | |
| 8 | +# -------------------------------------------------------------------------- | |
| 9 | 9 | # Este programa e software livre; voce pode redistribui-lo e/ou |
| 10 | 10 | # modifica-lo sob os termos da Licenca Publica Geral GNU, conforme |
| 11 | 11 | # publicada pela Free Software Foundation; de acordo com a versao 2 |
| ... | ... | @@ -16,17 +16,17 @@ |
| 16 | 16 | # COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM |
| 17 | 17 | # PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais |
| 18 | 18 | # detalhes. |
| 19 | -#-------------------------------------------------------------------------- | |
| 19 | +# -------------------------------------------------------------------------- | |
| 20 | 20 | |
| 21 | -from six import with_metaclass | |
| 21 | +import math | |
| 22 | 22 | |
| 23 | 23 | import numpy as np |
| 24 | -import math | |
| 25 | 24 | import vtk |
| 25 | +from six import with_metaclass | |
| 26 | 26 | from wx.lib.pubsub import pub as Publisher |
| 27 | 27 | |
| 28 | -import invesalius.utils as utils | |
| 29 | 28 | import invesalius.constants as const |
| 29 | +import invesalius.utils as utils | |
| 30 | 30 | |
| 31 | 31 | |
| 32 | 32 | class Box(with_metaclass(utils.Singleton, object)): |
| ... | ... | @@ -62,7 +62,7 @@ class Box(with_metaclass(utils.Singleton, object)): |
| 62 | 62 | def SetX(self, i, f): |
| 63 | 63 | self.xi = i |
| 64 | 64 | self.xf = f |
| 65 | - self.size_x = f | |
| 65 | + self.size_x = f | |
| 66 | 66 | |
| 67 | 67 | def SetY(self, i, f): |
| 68 | 68 | self.yi = i |
| ... | ... | @@ -75,7 +75,7 @@ class Box(with_metaclass(utils.Singleton, object)): |
| 75 | 75 | self.size_z = f |
| 76 | 76 | |
| 77 | 77 | def SetSpacing(self, x, y, z): |
| 78 | - self.xs = x | |
| 78 | + self.xs = x | |
| 79 | 79 | self.ys = y |
| 80 | 80 | self.zs = z |
| 81 | 81 | |
| ... | ... | @@ -100,63 +100,90 @@ class Box(with_metaclass(utils.Singleton, object)): |
| 100 | 100 | Update values in a matrix to each orientation. |
| 101 | 101 | """ |
| 102 | 102 | |
| 103 | - self.sagital[const.SAGITAL_LEFT] = [[self.xi, self.yi - (self.ys/2), self.zi],\ | |
| 104 | - [self.xi, self.yi - (self.ys/2), self.zf]] | |
| 105 | - | |
| 106 | - self.sagital[const.SAGITAL_RIGHT] = [[self.xi, self.yf + (self.ys/2), self.zi],\ | |
| 107 | - [self.xi, self.yf + (self.ys/2), self.zf]] | |
| 108 | - | |
| 109 | - self.sagital[const.SAGITAL_BOTTOM] = [[self.xi, self.yi, self.zi - (self.zs/2)],\ | |
| 110 | - [self.xi, self.yf, self.zi - (self.zs/2)]] | |
| 111 | - | |
| 112 | - self.sagital[const.SAGITAL_UPPER] = [[self.xi, self.yi, self.zf + (self.zs/2) ],\ | |
| 113 | - [self.xi, self.yf, self.zf + (self.zs/2) ]] | |
| 114 | - | |
| 115 | - self.coronal[const.CORONAL_BOTTOM] = [[self.xi, self.yi, self.zi - (self.zs/2)],\ | |
| 116 | - [self.xf, self.yf, self.zi - (self.zs/2)]] | |
| 117 | - | |
| 118 | - self.coronal[const.CORONAL_UPPER] = [[self.xi, self.yi, self.zf + (self.zs/2)],\ | |
| 119 | - [self.xf, self.yf, self.zf + (self.zs/2)]] | |
| 120 | - | |
| 121 | - self.coronal[const.CORONAL_LEFT] = [[self.xi - (self.xs/2), self.yi, self.zi],\ | |
| 122 | - [self.xi - (self.xs/2), self.yf, self.zf]] | |
| 123 | - | |
| 124 | - self.coronal[const.CORONAL_RIGHT] = [[self.xf + (self.xs/2), self.yi, self.zi],\ | |
| 125 | - [self.xf + (self.xs/2), self.yf, self.zf]] | |
| 126 | - | |
| 127 | - self.axial[const.AXIAL_BOTTOM] = [[self.xi, self.yi - (self.ys/2), self.zi],\ | |
| 128 | - [self.xf, self.yi - (self.ys/2), self.zf]] | |
| 129 | - | |
| 130 | - self.axial[const.AXIAL_UPPER] = [[self.xi, self.yf + (self.ys/2), self.zi],\ | |
| 131 | - [self.xf, self.yf + (self.ys/2), self.zf]] | |
| 132 | - | |
| 133 | - self.axial[const.AXIAL_LEFT] = [[self.xi - (self.xs/2), self.yi, self.zi],\ | |
| 134 | - [self.xi - (self.xs/2), self.yf, self.zf]] | |
| 135 | - | |
| 136 | - self.axial[const.AXIAL_RIGHT] = [[self.xf + (self.xs/2), self.yi, self.zi],\ | |
| 137 | - [self.xf + (self.xs/2), self.yf, self.zf]] | |
| 138 | - | |
| 139 | - Publisher.sendMessage('Update crop limits into gui', limits=self.GetLimits()) | |
| 140 | - | |
| 103 | + self.sagital[const.SAGITAL_LEFT] = [ | |
| 104 | + [self.xi, self.yi - (self.ys / 2), self.zi], | |
| 105 | + [self.xi, self.yi - (self.ys / 2), self.zf], | |
| 106 | + ] | |
| 107 | + | |
| 108 | + self.sagital[const.SAGITAL_RIGHT] = [ | |
| 109 | + [self.xi, self.yf + (self.ys / 2), self.zi], | |
| 110 | + [self.xi, self.yf + (self.ys / 2), self.zf], | |
| 111 | + ] | |
| 112 | + | |
| 113 | + self.sagital[const.SAGITAL_BOTTOM] = [ | |
| 114 | + [self.xi, self.yi, self.zi - (self.zs / 2)], | |
| 115 | + [self.xi, self.yf, self.zi - (self.zs / 2)], | |
| 116 | + ] | |
| 117 | + | |
| 118 | + self.sagital[const.SAGITAL_UPPER] = [ | |
| 119 | + [self.xi, self.yi, self.zf + (self.zs / 2)], | |
| 120 | + [self.xi, self.yf, self.zf + (self.zs / 2)], | |
| 121 | + ] | |
| 122 | + | |
| 123 | + self.coronal[const.CORONAL_BOTTOM] = [ | |
| 124 | + [self.xi, self.yi, self.zi - (self.zs / 2)], | |
| 125 | + [self.xf, self.yf, self.zi - (self.zs / 2)], | |
| 126 | + ] | |
| 127 | + | |
| 128 | + self.coronal[const.CORONAL_UPPER] = [ | |
| 129 | + [self.xi, self.yi, self.zf + (self.zs / 2)], | |
| 130 | + [self.xf, self.yf, self.zf + (self.zs / 2)], | |
| 131 | + ] | |
| 132 | + | |
| 133 | + self.coronal[const.CORONAL_LEFT] = [ | |
| 134 | + [self.xi - (self.xs / 2), self.yi, self.zi], | |
| 135 | + [self.xi - (self.xs / 2), self.yf, self.zf], | |
| 136 | + ] | |
| 137 | + | |
| 138 | + self.coronal[const.CORONAL_RIGHT] = [ | |
| 139 | + [self.xf + (self.xs / 2), self.yi, self.zi], | |
| 140 | + [self.xf + (self.xs / 2), self.yf, self.zf], | |
| 141 | + ] | |
| 142 | + | |
| 143 | + self.axial[const.AXIAL_BOTTOM] = [ | |
| 144 | + [self.xi, self.yi - (self.ys / 2), self.zi], | |
| 145 | + [self.xf, self.yi - (self.ys / 2), self.zf], | |
| 146 | + ] | |
| 147 | + | |
| 148 | + self.axial[const.AXIAL_UPPER] = [ | |
| 149 | + [self.xi, self.yf + (self.ys / 2), self.zi], | |
| 150 | + [self.xf, self.yf + (self.ys / 2), self.zf], | |
| 151 | + ] | |
| 152 | + | |
| 153 | + self.axial[const.AXIAL_LEFT] = [ | |
| 154 | + [self.xi - (self.xs / 2), self.yi, self.zi], | |
| 155 | + [self.xi - (self.xs / 2), self.yf, self.zf], | |
| 156 | + ] | |
| 157 | + | |
| 158 | + self.axial[const.AXIAL_RIGHT] = [ | |
| 159 | + [self.xf + (self.xs / 2), self.yi, self.zi], | |
| 160 | + [self.xf + (self.xs / 2), self.yf, self.zf], | |
| 161 | + ] | |
| 162 | + | |
| 163 | + Publisher.sendMessage("Update crop limits into gui", limits=self.GetLimits()) | |
| 141 | 164 | |
| 142 | 165 | def GetLimits(self): |
| 143 | 166 | """ |
| 144 | 167 | Return the bounding box limits (initial and final) in x, y and z. |
| 145 | 168 | """ |
| 146 | 169 | |
| 147 | - limits = [ int(self.xi / self.xs), int(self.xf / self. xs),\ | |
| 148 | - int(self.yi / self.ys), int(self.yf / self.ys),\ | |
| 149 | - int(self.zi / self.zs), int(self.zf / self.zs)] | |
| 170 | + limits = [ | |
| 171 | + int(self.xi / self.xs), | |
| 172 | + int(self.xf / self.xs), | |
| 173 | + int(self.yi / self.ys), | |
| 174 | + int(self.yf / self.ys), | |
| 175 | + int(self.zi / self.zs), | |
| 176 | + int(self.zf / self.zs), | |
| 177 | + ] | |
| 150 | 178 | |
| 151 | 179 | return limits |
| 152 | - | |
| 153 | - | |
| 180 | + | |
| 154 | 181 | def UpdatePositionBySideBox(self, pc, axis, position): |
| 155 | 182 | """ |
| 156 | 183 | Checks the coordinates are in any side of box and update it. |
| 157 | 184 | Is necessary to move limits of box. |
| 158 | - """ | |
| 159 | - | |
| 185 | + """ | |
| 186 | + | |
| 160 | 187 | if axis == "AXIAL": |
| 161 | 188 | if position == const.AXIAL_UPPER: |
| 162 | 189 | if pc[1] > self.yi and pc[1] > 0 and pc[1] <= self.size_y: |
| ... | ... | @@ -167,15 +194,13 @@ class Box(with_metaclass(utils.Singleton, object)): |
| 167 | 194 | self.yi = pc[1] |
| 168 | 195 | |
| 169 | 196 | if position == const.AXIAL_LEFT: |
| 170 | - if pc[0] < self.xf and pc[0] >= 0: | |
| 197 | + if pc[0] < self.xf and pc[0] >= 0: | |
| 171 | 198 | self.xi = pc[0] |
| 172 | 199 | |
| 173 | - | |
| 174 | 200 | if position == const.AXIAL_RIGHT: |
| 175 | 201 | if pc[0] > self.xi and pc[0] <= self.size_x: |
| 176 | 202 | self.xf = pc[0] |
| 177 | 203 | |
| 178 | - | |
| 179 | 204 | if axis == "SAGITAL": |
| 180 | 205 | if position == const.SAGITAL_UPPER: |
| 181 | 206 | if pc[2] > self.zi and pc[2] > 0 and pc[2] <= self.size_z: |
| ... | ... | @@ -186,14 +211,13 @@ class Box(with_metaclass(utils.Singleton, object)): |
| 186 | 211 | self.zi = pc[2] |
| 187 | 212 | |
| 188 | 213 | if position == const.SAGITAL_LEFT: |
| 189 | - if pc[1] < self.yf and pc[1] >= 0: | |
| 214 | + if pc[1] < self.yf and pc[1] >= 0: | |
| 190 | 215 | self.yi = pc[1] |
| 191 | 216 | |
| 192 | 217 | if position == const.SAGITAL_RIGHT: |
| 193 | 218 | if pc[1] > self.yi and pc[1] <= self.size_y: |
| 194 | 219 | self.yf = pc[1] |
| 195 | 220 | |
| 196 | - | |
| 197 | 221 | if axis == "CORONAL": |
| 198 | 222 | if position == const.CORONAL_UPPER: |
| 199 | 223 | if pc[2] > self.zi and pc[2] > 0 and pc[2] <= self.size_z: |
| ... | ... | @@ -204,16 +228,15 @@ class Box(with_metaclass(utils.Singleton, object)): |
| 204 | 228 | self.zi = pc[2] |
| 205 | 229 | |
| 206 | 230 | if position == const.CORONAL_LEFT: |
| 207 | - if pc[0] < self.xf and pc[0] >= 0: | |
| 231 | + if pc[0] < self.xf and pc[0] >= 0: | |
| 208 | 232 | self.xi = pc[0] |
| 209 | - | |
| 233 | + | |
| 210 | 234 | if position == const.CORONAL_RIGHT: |
| 211 | 235 | if pc[0] > self.yi and pc[0] <= self.size_y: |
| 212 | - self.xf = pc[0] | |
| 236 | + self.xf = pc[0] | |
| 213 | 237 | |
| 214 | 238 | self.MakeMatrix() |
| 215 | 239 | |
| 216 | - | |
| 217 | 240 | def UpdatePositionByInsideBox(self, pc, axis): |
| 218 | 241 | """ |
| 219 | 242 | Checks the coordinates are inside the box and update it. |
| ... | ... | @@ -253,13 +276,13 @@ class Box(with_metaclass(utils.Singleton, object)): |
| 253 | 276 | self.MakeMatrix() |
| 254 | 277 | |
| 255 | 278 | |
| 256 | - | |
| 257 | -class DrawCrop2DRetangle(): | |
| 279 | +class DrawCrop2DRetangle: | |
| 258 | 280 | """ |
| 259 | 281 | This class is responsible for draw and control user |
| 260 | 282 | interactions with the box. Each side of box is displayed in an |
| 261 | 283 | anatomical orientation (axial, sagital or coronal). |
| 262 | 284 | """ |
| 285 | + | |
| 263 | 286 | def __init__(self): |
| 264 | 287 | self.viewer = None |
| 265 | 288 | self.points_in_display = {} |
| ... | ... | @@ -280,66 +303,80 @@ class DrawCrop2DRetangle(): |
| 280 | 303 | x_pos_sl_, y_pos_sl_ = self.viewer.get_slice_pixel_coord_by_screen_pos(x, y) |
| 281 | 304 | slice_spacing = self.viewer.slice_.spacing |
| 282 | 305 | xs, ys, zs = slice_spacing |
| 283 | - | |
| 306 | + | |
| 284 | 307 | x_pos_sl = x_pos_sl_ * xs |
| 285 | 308 | y_pos_sl = y_pos_sl_ * ys |
| 286 | 309 | |
| 287 | 310 | x, y, z = self.viewer.get_voxel_coord_by_screen_pos(x, y) |
| 288 | 311 | |
| 289 | 312 | if self.viewer.orientation == "AXIAL": |
| 290 | - | |
| 291 | - if self.status_move == const.AXIAL_UPPER or\ | |
| 292 | - self.status_move == const.AXIAL_BOTTOM: | |
| 293 | - Publisher.sendMessage('Set interactor resize NS cursor') | |
| 294 | - elif self.status_move == const.AXIAL_LEFT or\ | |
| 295 | - self.status_move == const.AXIAL_RIGHT: | |
| 296 | - Publisher.sendMessage('Set interactor resize WE cursor') | |
| 313 | + | |
| 314 | + if ( | |
| 315 | + self.status_move == const.AXIAL_UPPER | |
| 316 | + or self.status_move == const.AXIAL_BOTTOM | |
| 317 | + ): | |
| 318 | + Publisher.sendMessage("Set interactor resize NS cursor") | |
| 319 | + elif ( | |
| 320 | + self.status_move == const.AXIAL_LEFT | |
| 321 | + or self.status_move == const.AXIAL_RIGHT | |
| 322 | + ): | |
| 323 | + Publisher.sendMessage("Set interactor resize WE cursor") | |
| 297 | 324 | elif self.crop_pan == const.CROP_PAN: |
| 298 | - Publisher.sendMessage('Set interactor resize NSWE cursor') | |
| 325 | + Publisher.sendMessage("Set interactor resize NSWE cursor") | |
| 299 | 326 | else: |
| 300 | - Publisher.sendMessage('Set interactor default cursor') | |
| 327 | + Publisher.sendMessage("Set interactor default cursor") | |
| 301 | 328 | |
| 302 | 329 | if self.viewer.orientation == "SAGITAL": |
| 303 | - if self.status_move == const.SAGITAL_UPPER or\ | |
| 304 | - self.status_move == const.SAGITAL_BOTTOM: | |
| 305 | - Publisher.sendMessage('Set interactor resize NS cursor') | |
| 306 | - elif self.status_move == const.SAGITAL_LEFT or\ | |
| 307 | - self.status_move == const.SAGITAL_RIGHT: | |
| 308 | - Publisher.sendMessage('Set interactor resize WE cursor') | |
| 330 | + if ( | |
| 331 | + self.status_move == const.SAGITAL_UPPER | |
| 332 | + or self.status_move == const.SAGITAL_BOTTOM | |
| 333 | + ): | |
| 334 | + Publisher.sendMessage("Set interactor resize NS cursor") | |
| 335 | + elif ( | |
| 336 | + self.status_move == const.SAGITAL_LEFT | |
| 337 | + or self.status_move == const.SAGITAL_RIGHT | |
| 338 | + ): | |
| 339 | + Publisher.sendMessage("Set interactor resize WE cursor") | |
| 309 | 340 | elif self.crop_pan == const.CROP_PAN: |
| 310 | - Publisher.sendMessage('Set interactor resize NSWE cursor') | |
| 341 | + Publisher.sendMessage("Set interactor resize NSWE cursor") | |
| 311 | 342 | else: |
| 312 | - Publisher.sendMessage('Set interactor default cursor') | |
| 343 | + Publisher.sendMessage("Set interactor default cursor") | |
| 313 | 344 | |
| 314 | 345 | if self.viewer.orientation == "CORONAL": |
| 315 | - if self.status_move == const.CORONAL_UPPER or\ | |
| 316 | - self.status_move == const.CORONAL_BOTTOM: | |
| 317 | - Publisher.sendMessage('Set interactor resize NS cursor') | |
| 318 | - elif self.status_move == const.CORONAL_LEFT or\ | |
| 319 | - self.status_move == const.CORONAL_RIGHT: | |
| 320 | - Publisher.sendMessage('Set interactor resize WE cursor') | |
| 346 | + if ( | |
| 347 | + self.status_move == const.CORONAL_UPPER | |
| 348 | + or self.status_move == const.CORONAL_BOTTOM | |
| 349 | + ): | |
| 350 | + Publisher.sendMessage("Set interactor resize NS cursor") | |
| 351 | + elif ( | |
| 352 | + self.status_move == const.CORONAL_LEFT | |
| 353 | + or self.status_move == const.CORONAL_RIGHT | |
| 354 | + ): | |
| 355 | + Publisher.sendMessage("Set interactor resize WE cursor") | |
| 321 | 356 | elif self.crop_pan == const.CROP_PAN: |
| 322 | - Publisher.sendMessage('Set interactor resize NSWE cursor') | |
| 357 | + Publisher.sendMessage("Set interactor resize NSWE cursor") | |
| 323 | 358 | else: |
| 324 | - Publisher.sendMessage('Set interactor default cursor') | |
| 325 | - | |
| 359 | + Publisher.sendMessage("Set interactor default cursor") | |
| 360 | + | |
| 326 | 361 | if self.mouse_pressed and self.status_move: |
| 327 | - self.box.UpdatePositionBySideBox((x * xs, y * ys, z * zs),\ | |
| 328 | - self.viewer.orientation, self.status_move) | |
| 362 | + self.box.UpdatePositionBySideBox( | |
| 363 | + (x * xs, y * ys, z * zs), self.viewer.orientation, self.status_move | |
| 364 | + ) | |
| 329 | 365 | |
| 330 | 366 | nv_x = x - self.last_x |
| 331 | 367 | nv_y = y - self.last_y |
| 332 | 368 | nv_z = z - self.last_z |
| 333 | 369 | |
| 334 | 370 | if self.mouse_pressed and self.crop_pan: |
| 335 | - self.box.UpdatePositionByInsideBox((nv_x * xs, nv_y * ys, nv_z * zs),\ | |
| 336 | - self.viewer.orientation) | |
| 371 | + self.box.UpdatePositionByInsideBox( | |
| 372 | + (nv_x * xs, nv_y * ys, nv_z * zs), self.viewer.orientation | |
| 373 | + ) | |
| 337 | 374 | |
| 338 | 375 | self.last_x = x |
| 339 | 376 | self.last_y = y |
| 340 | 377 | self.last_z = z |
| 341 | 378 | |
| 342 | - Publisher.sendMessage('Redraw canvas') | |
| 379 | + Publisher.sendMessage("Redraw canvas") | |
| 343 | 380 | |
| 344 | 381 | def ReleaseLeft(self): |
| 345 | 382 | self.status_move = None |
| ... | ... | @@ -352,7 +389,7 @@ class DrawCrop2DRetangle(): |
| 352 | 389 | |
| 353 | 390 | slice_spacing = self.viewer.slice_.spacing |
| 354 | 391 | xs, ys, zs = slice_spacing |
| 355 | - | |
| 392 | + | |
| 356 | 393 | if self.viewer.orientation == "AXIAL": |
| 357 | 394 | x_pos_sl = x_pos_sl_ * xs |
| 358 | 395 | y_pos_sl = y_pos_sl_ * ys |
| ... | ... | @@ -361,19 +398,21 @@ class DrawCrop2DRetangle(): |
| 361 | 398 | p0 = p[0] |
| 362 | 399 | p1 = p[1] |
| 363 | 400 | |
| 364 | - dist = self.distance_from_point_line((p0[0], p0[1]),\ | |
| 365 | - (p1[0], p1[1]),\ | |
| 366 | - (x_pos_sl, y_pos_sl)) | |
| 401 | + dist = self.distance_from_point_line( | |
| 402 | + (p0[0], p0[1]), (p1[0], p1[1]), (x_pos_sl, y_pos_sl) | |
| 403 | + ) | |
| 367 | 404 | |
| 368 | 405 | if dist <= 2: |
| 369 | 406 | if self.point_between_line(p0, p1, (x_pos_sl, y_pos_sl), "AXIAL"): |
| 370 | 407 | self.status_move = k |
| 371 | 408 | break |
| 372 | 409 | |
| 373 | - if self.point_into_box(p0, p1, (x_pos_sl, y_pos_sl), "AXIAL")\ | |
| 374 | - and self.status_move == None: | |
| 410 | + if ( | |
| 411 | + self.point_into_box(p0, p1, (x_pos_sl, y_pos_sl), "AXIAL") | |
| 412 | + and self.status_move == None | |
| 413 | + ): | |
| 375 | 414 | self.crop_pan = const.CROP_PAN |
| 376 | - #break | |
| 415 | + # break | |
| 377 | 416 | else: |
| 378 | 417 | if self.crop_pan: |
| 379 | 418 | self.crop_pan = None |
| ... | ... | @@ -382,27 +421,28 @@ class DrawCrop2DRetangle(): |
| 382 | 421 | if not (self.mouse_pressed) and k != self.status_move: |
| 383 | 422 | self.status_move = None |
| 384 | 423 | |
| 385 | - | |
| 386 | 424 | if self.viewer.orientation == "CORONAL": |
| 387 | 425 | x_pos_sl = x_pos_sl_ * xs |
| 388 | 426 | y_pos_sl = y_pos_sl_ * zs |
| 389 | - | |
| 427 | + | |
| 390 | 428 | for k, p in self.box.coronal.items(): |
| 391 | 429 | p0 = p[0] |
| 392 | 430 | p1 = p[1] |
| 393 | 431 | |
| 394 | - dist = self.distance_from_point_line((p0[0], p0[2]),\ | |
| 395 | - (p1[0], p1[2]),\ | |
| 396 | - (x_pos_sl, y_pos_sl)) | |
| 432 | + dist = self.distance_from_point_line( | |
| 433 | + (p0[0], p0[2]), (p1[0], p1[2]), (x_pos_sl, y_pos_sl) | |
| 434 | + ) | |
| 397 | 435 | if dist <= 2: |
| 398 | 436 | if self.point_between_line(p0, p1, (x_pos_sl, y_pos_sl), "CORONAL"): |
| 399 | 437 | self.status_move = k |
| 400 | 438 | break |
| 401 | 439 | |
| 402 | - if self.point_into_box(p0, p1, (x_pos_sl, y_pos_sl), "CORONAL")\ | |
| 403 | - and self.status_move == None: | |
| 440 | + if ( | |
| 441 | + self.point_into_box(p0, p1, (x_pos_sl, y_pos_sl), "CORONAL") | |
| 442 | + and self.status_move == None | |
| 443 | + ): | |
| 404 | 444 | self.crop_pan = const.CROP_PAN |
| 405 | - #break | |
| 445 | + # break | |
| 406 | 446 | else: |
| 407 | 447 | if self.crop_pan: |
| 408 | 448 | self.crop_pan = None |
| ... | ... | @@ -411,7 +451,6 @@ class DrawCrop2DRetangle(): |
| 411 | 451 | if not (self.mouse_pressed) and k != self.status_move: |
| 412 | 452 | self.status_move = None |
| 413 | 453 | |
| 414 | - | |
| 415 | 454 | if self.viewer.orientation == "SAGITAL": |
| 416 | 455 | x_pos_sl = x_pos_sl_ * ys |
| 417 | 456 | y_pos_sl = y_pos_sl_ * zs |
| ... | ... | @@ -420,19 +459,21 @@ class DrawCrop2DRetangle(): |
| 420 | 459 | p0 = p[0] |
| 421 | 460 | p1 = p[1] |
| 422 | 461 | |
| 423 | - dist = self.distance_from_point_line((p0[1], p0[2]),\ | |
| 424 | - (p1[1], p1[2]),\ | |
| 425 | - (x_pos_sl, y_pos_sl)) | |
| 462 | + dist = self.distance_from_point_line( | |
| 463 | + (p0[1], p0[2]), (p1[1], p1[2]), (x_pos_sl, y_pos_sl) | |
| 464 | + ) | |
| 426 | 465 | |
| 427 | 466 | if dist <= 2: |
| 428 | 467 | if self.point_between_line(p0, p1, (x_pos_sl, y_pos_sl), "SAGITAL"): |
| 429 | 468 | self.status_move = k |
| 430 | 469 | break |
| 431 | 470 | |
| 432 | - if self.point_into_box(p0, p1, (x_pos_sl, y_pos_sl), "SAGITAL")\ | |
| 433 | - and self.status_move == None: | |
| 471 | + if ( | |
| 472 | + self.point_into_box(p0, p1, (x_pos_sl, y_pos_sl), "SAGITAL") | |
| 473 | + and self.status_move == None | |
| 474 | + ): | |
| 434 | 475 | self.crop_pan = const.CROP_PAN |
| 435 | - #break | |
| 476 | + # break | |
| 436 | 477 | else: |
| 437 | 478 | if self.crop_pan: |
| 438 | 479 | self.crop_pan = None |
| ... | ... | @@ -441,8 +482,6 @@ class DrawCrop2DRetangle(): |
| 441 | 482 | if not (self.mouse_pressed) and k != self.status_move: |
| 442 | 483 | self.status_move = None |
| 443 | 484 | |
| 444 | - | |
| 445 | - | |
| 446 | 485 | def draw_to_canvas(self, gc, canvas): |
| 447 | 486 | """ |
| 448 | 487 | Draws to an wx.GraphicsContext. |
| ... | ... | @@ -457,61 +496,71 @@ class DrawCrop2DRetangle(): |
| 457 | 496 | def point_into_box(self, p1, p2, pc, axis): |
| 458 | 497 | |
| 459 | 498 | if axis == "AXIAL": |
| 460 | - if pc[0] > self.box.xi + 10 and pc[0] < self.box.xf - 10\ | |
| 461 | - and pc[1] - 10 > self.box.yi and pc[1] < self.box.yf - 10: | |
| 499 | + if ( | |
| 500 | + pc[0] > self.box.xi + 10 | |
| 501 | + and pc[0] < self.box.xf - 10 | |
| 502 | + and pc[1] - 10 > self.box.yi | |
| 503 | + and pc[1] < self.box.yf - 10 | |
| 504 | + ): | |
| 462 | 505 | return True |
| 463 | 506 | else: |
| 464 | 507 | return False |
| 465 | 508 | |
| 466 | 509 | if axis == "SAGITAL": |
| 467 | - if pc[0] > self.box.yi + 10 and pc[0] < self.box.yf - 10\ | |
| 468 | - and pc[1] - 10 > self.box.zi and pc[1] < self.box.zf - 10: | |
| 510 | + if ( | |
| 511 | + pc[0] > self.box.yi + 10 | |
| 512 | + and pc[0] < self.box.yf - 10 | |
| 513 | + and pc[1] - 10 > self.box.zi | |
| 514 | + and pc[1] < self.box.zf - 10 | |
| 515 | + ): | |
| 469 | 516 | return True |
| 470 | 517 | else: |
| 471 | 518 | return False |
| 472 | 519 | |
| 473 | 520 | if axis == "CORONAL": |
| 474 | - if pc[0] > self.box.xi + 10 and pc[0] < self.box.xf - 10\ | |
| 475 | - and pc[1] - 10 > self.box.zi and pc[1] < self.box.zf - 10: | |
| 521 | + if ( | |
| 522 | + pc[0] > self.box.xi + 10 | |
| 523 | + and pc[0] < self.box.xf - 10 | |
| 524 | + and pc[1] - 10 > self.box.zi | |
| 525 | + and pc[1] < self.box.zf - 10 | |
| 526 | + ): | |
| 476 | 527 | return True |
| 477 | 528 | else: |
| 478 | 529 | return False |
| 479 | 530 | |
| 480 | - | |
| 481 | 531 | def point_between_line(self, p1, p2, pc, axis): |
| 482 | 532 | """ |
| 483 | 533 | Checks whether a point is in the line limits |
| 484 | 534 | """ |
| 485 | 535 | |
| 486 | 536 | if axis == "AXIAL": |
| 487 | - if p1[0] < pc[0] and p2[0] > pc[0]: #x axis | |
| 537 | + if p1[0] < pc[0] and p2[0] > pc[0]: # x axis | |
| 488 | 538 | return True |
| 489 | - elif p1[1] < pc[1] and p2[1] > pc[1]: #y axis | |
| 539 | + elif p1[1] < pc[1] and p2[1] > pc[1]: # y axis | |
| 490 | 540 | return True |
| 491 | 541 | else: |
| 492 | 542 | return False |
| 493 | 543 | elif axis == "SAGITAL": |
| 494 | - if p1[1] < pc[0] and p2[1] > pc[0]: #y axis | |
| 544 | + if p1[1] < pc[0] and p2[1] > pc[0]: # y axis | |
| 495 | 545 | return True |
| 496 | - elif p1[2] < pc[1] and p2[2] > pc[1]: #z axis | |
| 546 | + elif p1[2] < pc[1] and p2[2] > pc[1]: # z axis | |
| 497 | 547 | return True |
| 498 | 548 | else: |
| 499 | 549 | return False |
| 500 | 550 | elif axis == "CORONAL": |
| 501 | - if p1[0] < pc[0] and p2[0] > pc[0]: #x axis | |
| 551 | + if p1[0] < pc[0] and p2[0] > pc[0]: # x axis | |
| 502 | 552 | return True |
| 503 | - elif p1[2] < pc[1] and p2[2] > pc[1]: #z axis | |
| 553 | + elif p1[2] < pc[1] and p2[2] > pc[1]: # z axis | |
| 504 | 554 | return True |
| 505 | 555 | else: |
| 506 | 556 | return False |
| 507 | 557 | |
| 508 | - | |
| 509 | 558 | def distance_from_point_line(self, p1, p2, pc): |
| 510 | 559 | """ |
| 511 | 560 | Calculate the distance from point pc to a line formed by p1 and p2. |
| 512 | 561 | """ |
| 513 | 562 | |
| 514 | - #TODO: Same function into clut_raycasting | |
| 563 | + # TODO: Same function into clut_raycasting | |
| 515 | 564 | # Create a function to organize it. |
| 516 | 565 | |
| 517 | 566 | # Create a vector pc-p1 and p2-p1 |
| ... | ... | @@ -527,20 +576,19 @@ class DrawCrop2DRetangle(): |
| 527 | 576 | distance = math.sin(theta) * len_A |
| 528 | 577 | return distance |
| 529 | 578 | |
| 530 | - | |
| 531 | 579 | def Coord3DtoDisplay(self, x, y, z, canvas): |
| 532 | 580 | |
| 533 | 581 | coord = vtk.vtkCoordinate() |
| 534 | 582 | coord.SetValue(x, y, z) |
| 535 | 583 | cx, cy = coord.GetComputedDisplayValue(canvas.evt_renderer) |
| 536 | - | |
| 584 | + | |
| 537 | 585 | return (cx, cy) |
| 538 | 586 | |
| 539 | 587 | def MakeBox(self): |
| 540 | 588 | |
| 541 | 589 | slice_size = self.viewer.slice_.matrix.shape |
| 542 | 590 | zf, yf, xf = slice_size[0] - 1, slice_size[1] - 1, slice_size[2] - 1 |
| 543 | - | |
| 591 | + | |
| 544 | 592 | slice_spacing = self.viewer.slice_.spacing |
| 545 | 593 | xs, ys, zs = slice_spacing |
| 546 | 594 | |
| ... | ... | @@ -553,55 +601,58 @@ class DrawCrop2DRetangle(): |
| 553 | 601 | box.SetSpacing(xs, ys, zs) |
| 554 | 602 | box.MakeMatrix() |
| 555 | 603 | |
| 556 | - | |
| 557 | 604 | def UpdateValues(self, canvas): |
| 558 | 605 | |
| 559 | 606 | box = self.box |
| 560 | - slice_number = self.viewer.slice_data.number | |
| 607 | + slice_number = self.viewer.slice_data.number | |
| 561 | 608 | |
| 562 | 609 | slice_spacing = self.viewer.slice_.spacing |
| 563 | 610 | xs, ys, zs = slice_spacing |
| 564 | - | |
| 611 | + | |
| 565 | 612 | if canvas.orientation == "AXIAL": |
| 566 | 613 | for points in box.axial.values(): |
| 567 | 614 | pi_x, pi_y, pi_z = points[0] |
| 568 | 615 | pf_x, pf_y, pf_z = points[1] |
| 569 | 616 | |
| 570 | 617 | s_cxi, s_cyi = self.Coord3DtoDisplay(pi_x, pi_y, pi_z, canvas) |
| 571 | - s_cxf, s_cyf = self.Coord3DtoDisplay(pf_x, pf_y, pf_z ,canvas) | |
| 618 | + s_cxf, s_cyf = self.Coord3DtoDisplay(pf_x, pf_y, pf_z, canvas) | |
| 572 | 619 | |
| 573 | 620 | sn = slice_number * zs |
| 574 | 621 | if sn >= box.zi and sn <= box.zf: |
| 575 | - canvas.draw_line((s_cxi, s_cyi),(s_cxf, s_cyf), colour=(255,255,255,255)) | |
| 576 | - | |
| 622 | + canvas.draw_line( | |
| 623 | + (s_cxi, s_cyi), (s_cxf, s_cyf), colour=(255, 255, 255, 255) | |
| 624 | + ) | |
| 625 | + | |
| 577 | 626 | elif canvas.orientation == "CORONAL": |
| 578 | 627 | for points in box.coronal.values(): |
| 579 | 628 | pi_x, pi_y, pi_z = points[0] |
| 580 | 629 | pf_x, pf_y, pf_z = points[1] |
| 581 | 630 | |
| 582 | 631 | s_cxi, s_cyi = self.Coord3DtoDisplay(pi_x, pi_y, pi_z, canvas) |
| 583 | - s_cxf, s_cyf = self.Coord3DtoDisplay(pf_x, pf_y, pf_z ,canvas) | |
| 632 | + s_cxf, s_cyf = self.Coord3DtoDisplay(pf_x, pf_y, pf_z, canvas) | |
| 584 | 633 | |
| 585 | 634 | sn = slice_number * ys |
| 586 | 635 | |
| 587 | 636 | if sn >= box.yi and sn <= box.yf: |
| 588 | - canvas.draw_line((s_cxi, s_cyi),(s_cxf, s_cyf), colour=(255,255,255,255)) | |
| 637 | + canvas.draw_line( | |
| 638 | + (s_cxi, s_cyi), (s_cxf, s_cyf), colour=(255, 255, 255, 255) | |
| 639 | + ) | |
| 589 | 640 | |
| 590 | 641 | elif canvas.orientation == "SAGITAL": |
| 591 | 642 | for points in box.sagital.values(): |
| 592 | - | |
| 643 | + | |
| 593 | 644 | pi_x, pi_y, pi_z = points[0] |
| 594 | 645 | pf_x, pf_y, pf_z = points[1] |
| 595 | 646 | |
| 596 | 647 | s_cxi, s_cyi = self.Coord3DtoDisplay(pi_x, pi_y, pi_z, canvas) |
| 597 | - s_cxf, s_cyf = self.Coord3DtoDisplay(pf_x, pf_y, pf_z ,canvas) | |
| 648 | + s_cxf, s_cyf = self.Coord3DtoDisplay(pf_x, pf_y, pf_z, canvas) | |
| 598 | 649 | |
| 599 | 650 | sn = slice_number * xs |
| 600 | 651 | if sn >= box.xi and sn <= box.xf: |
| 601 | - canvas.draw_line((s_cxi, s_cyi),(s_cxf, s_cyf), colour=(255,255,255,255)) | |
| 602 | - | |
| 652 | + canvas.draw_line( | |
| 653 | + (s_cxi, s_cyi), (s_cxf, s_cyf), colour=(255, 255, 255, 255) | |
| 654 | + ) | |
| 603 | 655 | |
| 604 | 656 | def SetViewer(self, viewer): |
| 605 | 657 | self.viewer = viewer |
| 606 | 658 | self.MakeBox() |
| 607 | - | ... | ... |