From 7a7d289541eb976591e45cede4abcf96c787b3dd Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauld.mccarthy@gmail.com>
Date: Mon, 23 Jun 2014 12:03:37 +0100
Subject: [PATCH] Fixed image bounds calculation for pixdim/identity
 transformations - they were not being calculated correctly.

---
 fsl/data/fslimage.py      | 26 +++++++++++++++++++++-----
 fsl/fslview/orthopanel.py |  6 +++---
 2 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/fsl/data/fslimage.py b/fsl/data/fslimage.py
index a8cd635a6..2ea2b34db 100644
--- a/fsl/data/fslimage.py
+++ b/fsl/data/fslimage.py
@@ -176,15 +176,27 @@ class Image(props.HasProperties):
 
         if self.transform == 'affine':
             voxToWorldMat = self.nibImage.get_affine().transpose()
+            pixdims       = self.nibImage.get_header().get_zooms()
+            
         elif self.transform == 'pixdim':
-            pixdims = [self.pixdim[0], self.pixdim[1], self.pixdim[2], 1]
-            voxToWorldMat = np.diag(pixdims)
+            pixdims       = self.nibImage.get_header().get_zooms()
+            voxToWorldMat = np.diag([pixdims[0], pixdims[1], pixdims[2], 1.0])
+            
         elif self.transform == 'id':
             voxToWorldMat = np.identity(4)
+            pixdims       = [1.0, 1.0, 1.0]
 
         self.voxToWorldMat = np.array(voxToWorldMat, dtype=np.float32)
         self.worldToVoxMat = linalg.inv(self.voxToWorldMat)
-
+        self.pixdims       = pixdims
+
+        # for pixdim/identity transformations, we want the world
+        # location (0, 0) to map to voxel location (0, 0)
+        if self.transform in ['pixdim', 'id']:
+            for i in range(3):
+                self.voxToWorldMat[3, i] =  self.pixdims[i] * 0.5
+                self.worldToVoxMat[3, i] = -self.pixdims[i] * 0.5
+                
         log.debug('Image {} transformation matrix changed: {}'.format(
             self.name, self.voxToWorldMat))
 
@@ -197,6 +209,10 @@ class Image(props.HasProperties):
 
         x, y, z = self.shape[:3]
 
+        x = x - 1
+        y = y - 1
+        z = z - 1
+
         points = np.zeros((8, 3), dtype=np.float32)
 
         points[0, :] = [0, 0, 0]
@@ -210,8 +226,8 @@ class Image(props.HasProperties):
 
         tx = self.voxToWorld(points)
 
-        lo = tx[:, axis].min()
-        hi = tx[:, axis].max()
+        lo = tx[:, axis].min() - self.pixdims[axis] * 0.5
+        hi = tx[:, axis].max() + self.pixdims[axis] * 0.5
 
         return (lo, hi)
 
diff --git a/fsl/fslview/orthopanel.py b/fsl/fslview/orthopanel.py
index 34f0aa614..62b0dc734 100644
--- a/fsl/fslview/orthopanel.py
+++ b/fsl/fslview/orthopanel.py
@@ -264,14 +264,14 @@ class OrthoPanel(wx.Panel, props.HasProperties):
         source  = ev.GetEventObject()
         w, h    = source.GetClientSize()
 
-        log.debug('Mouse click on canvas {}: ({}, {})'.format(
-            source.name, mx, my))
-
         my = h - my
 
         xpos = source.canvasToWorldX(mx)
         ypos = source.canvasToWorldY(my)
 
+        log.debug('Mouse click on canvas {}: ({}, {} -> {}, {})'.format(
+            source.name, mx, my, xpos, ypos))
+
         if   source == self.xcanvas: self.imageList.location.yz = [xpos, ypos]
         elif source == self.ycanvas: self.imageList.location.xz = [xpos, ypos]
         elif source == self.zcanvas: self.imageList.location.xy = [xpos, ypos]
-- 
GitLab