+//------------------------------------------------------------------------------
+// This function ensures that we sample the slices of a vtkImageReslice filter
+// in the direction of the slicer (SliceOrientation) similarly as mImageReslice.
+// In other words, we change the grid of the reslice in the same way as the grid
+// of the displayed image in the slicing direction.
+void vvSlicer::AdjustResliceToSliceOrientation(vtkImageReslice *reslice)
+{
+ // Reset autocrop and update output information
+ reslice->SetOutputOriginToDefault();
+ reslice->SetOutputSpacingToDefault();
+#if VTK_MAJOR_VERSION <= 5
+ reslice->GetOutput()->UpdateInformation();
+#else
+ reslice->UpdateInformation();
+#endif
+
+ // Ge new origin / spacing
+ double origin[3];
+ double spacing[3];
+ reslice->GetOutput()->GetOrigin(origin);
+ reslice->GetOutput()->GetSpacing(spacing);
+
+ // Use similar spacing as the image in the direction SliceOrientation
+ spacing[this->SliceOrientation] = mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
+
+ // Modify origin to be on the image grid in the direction SliceOrientation in 3 steps
+ // Step 1: from world coordinates to image coordinates
+ origin[this->SliceOrientation] -= mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
+ origin[this->SliceOrientation] /= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
+
+ // Step 2: round to nearest grid positionInc. This has been validated as the only
+ // way to have something consistent with the thickness of a 2D slice visible on the
+ // other slices. The thickness is accounted for so if the 2D slice is to thin and
+ // between two slices, one will never be able to see this 2D slice (bug #1883).
+ origin[this->SliceOrientation] = itk::Math::Round<double>(origin[this->SliceOrientation]);
+
+ // Step 3: back to world coordinates
+ origin[this->SliceOrientation] *= mImageReslice->GetOutput()->GetSpacing()[this->SliceOrientation];
+ origin[this->SliceOrientation] += mImageReslice->GetOutput()->GetOrigin()[this->SliceOrientation];
+
+ // Set new spacing and origin
+ reslice->SetOutputOrigin(origin);
+ reslice->SetOutputSpacing(spacing);
+ reslice->UpdateInformation();
+}
+//------------------------------------------------------------------------------