Skip to content

Commit

Permalink
handle indexing at tile bounds correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhfu committed Jun 14, 2019
1 parent 2145ab2 commit 1ff05dc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
30 changes: 22 additions & 8 deletions volumina/patchAccessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,36 @@ def patchRectF(self, blockNum, overlap=0):
return QRectF(QPointF(startx, starty), QPointF(endx, endy))

def getPatchesForRect(self, startx, starty, endx, endy):
"""returns patchnumbers for patches that are intersecting with the normalized rectangle defined by upper left
corner (staŕtx/y) and lower right corner (endx/y)
"""
if endy < 0 or endx < 0:
# allowing no inverted start/end -positions, there should be no negative end-positions
return []
sx = int(numpy.floor(1.0 * startx / self._blockSize))
ex = int(numpy.ceil(1.0 * endx / self._blockSize))
sy = int(numpy.floor(1.0 * starty / self._blockSize))
ey = int(numpy.ceil(1.0 * endy / self._blockSize))

# Clip to rect bounds
sx = max(sx, 0)
sy = max(sy, 0)
# Clip to rect to upper bounds
ex = min(ex, self._cX)
ey = min(ey, self._cY)

# always return at least one index
if sx==ex:
ex = ex + 1
if sy==ey:
ey = ey + 1
# return an index also, when start and end are equal
if sx == ex:
if ex >= self._cX:
sx = sx - 1
else:
ex = ex + 1
if sy == ey:
if ey >= self._cY:
sy = sy - 1
else:
ey = ey + 1

# Clip to rect to lower bounds
sx = max(sx, 0)
sy = max(sy, 0)

nums = []
for y in range(sy, ey):
Expand Down
21 changes: 17 additions & 4 deletions volumina/sliceSelectorHud.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ def setMouseCoords(self, x, y, z):
coords = [int(val) for val in [x,y,z]]
imgView = self.editor.posModel.activeView
blockSize = self.editor.imageViews[imgView].scene()._tileProvider.tiling.blockSize
sliceShape = self.editor.imageViews[imgView].scene()._tileProvider.tiling.sliceShape
labelSet = False

del coords[imgView]
Expand All @@ -802,14 +803,26 @@ def setMouseCoords(self, x, y, z):
value = None
layer_id = self.editor.imagepumps[imgView].stackedImageSources._layerToIms[layer]
stack_id = self.editor.imageViews[imgView].scene()._tileProvider._current_stack_id
tile_id = self.editor.imageViews[imgView].scene()._tileProvider.tiling.intersected(
QRect(QPoint(x, y), QPoint(x, y)))[
0] # There will be just one tile, since we have just a single point
tile_ids = self.editor.imageViews[imgView].scene()._tileProvider.tiling.intersected(
QRect(QPoint(x, y), QPoint(x, y)))
if tile_ids:
tile_id = tile_ids[0] # There will be just one tile, since we have just a single point
else:
return

with self.editor.imageViews[imgView].scene()._tileProvider._cache:
image = self.editor.imageViews[imgView].scene()._tileProvider._cache.layer(stack_id, layer_id,
tile_id)
if image is not None:
value = image.pixelColor(x % blockSize, y % blockSize)
x_r = x % blockSize
y_r = y % blockSize

if x >= blockSize and x >= int(sliceShape[0]/blockSize) * blockSize:
x_r = x_r + blockSize
if y >= blockSize and y >= int(sliceShape[1]/blockSize) * blockSize:
y_r = y_r + blockSize

value = image.pixelColor(x_r, y_r)

lbl, foreground, background = layer.setValueWidget(value)

Expand Down
2 changes: 1 addition & 1 deletion volumina/tiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def containsF(self, point):
def intersected(self, sceneRect):
if not sceneRect.isValid():
return list(range(len(self.tileRects)))

sceneRect = sceneRect.normalized()
# Patch accessor uses data coordinates
rect = self.data2scene.inverted()[0].mapRect(sceneRect)
patchNumbers = self._patchAccessor.getPatchesForRect(
Expand Down

0 comments on commit 1ff05dc

Please sign in to comment.