public override byte[] CreateSlicePixelData(Vector3D imagePositionPatient)
		{
			var rowOrientation = RowOrientationPatient;
			var columnOrientation = ColumnOrientationPatient;
			var stackOrientation = rowOrientation.Cross(columnOrientation).Normalize();

			// VTK reslice axes are in the volume coordinate system and should be positioned at the center of the desired output image
			var reslicePosition = imagePositionPatient + Columns*ColumnSpacing/2f*rowOrientation + Rows*RowSpacing/2f*columnOrientation;
			reslicePosition = VolumeReference.ConvertToVolume(reslicePosition);

			var resliceAxes = new Matrix(new[,]
			                             	{
			                             		{rowOrientation.X, rowOrientation.Y, rowOrientation.Z, 0},
			                             		{columnOrientation.X, columnOrientation.Y, columnOrientation.Z, 0},
			                             		{stackOrientation.X, stackOrientation.Y, stackOrientation.Z, 0},
			                             		{0, 0, 0, 1}
			                             	});
			resliceAxes = VolumeReference.RotateToVolumeOrientation(resliceAxes).Transpose();
			resliceAxes.SetColumn(3, reslicePosition.X, reslicePosition.Y, reslicePosition.Z, 1);

			if (Subsamples > 1)
			{
				// if subsampling is required, extract the thick slice ("slab") from the volume and then aggregate it using the specified projection method
				return GetSlabPixelData(VolumeReference, resliceAxes, stackOrientation, Rows, Columns, Subsamples, RowSpacing, ColumnSpacing, SliceThickness, Interpolation, Projection);
			}
			else
			{
				// extract the (thin) slice from the volume
				return GetSlicePixelData(VolumeReference, resliceAxes, Rows, Columns, RowSpacing, ColumnSpacing, Interpolation);
			}
		}
		private Matrix GetImageToPatientTransform()
		{
			if (_pixelToPatientTransform == null)
			{
				_pixelToPatientTransform = new Matrix(4, 4);

				_pixelToPatientTransform.SetColumn(0, (float) (ImageOrientationPatient.RowX*PixelSpacing.Column),
				                                   (float) (ImageOrientationPatient.RowY*PixelSpacing.Column),
				                                   (float) (ImageOrientationPatient.RowZ*PixelSpacing.Column), 0F);

				_pixelToPatientTransform.SetColumn(1, (float) (ImageOrientationPatient.ColumnX*PixelSpacing.Row),
				                                   (float) (ImageOrientationPatient.ColumnY*PixelSpacing.Row),
				                                   (float) (ImageOrientationPatient.ColumnZ*PixelSpacing.Row), 0F);

				_pixelToPatientTransform.SetColumn(3, ImagePositionPatientVector.X, ImagePositionPatientVector.Y, ImagePositionPatientVector.Z, 1F);
			}
			return _pixelToPatientTransform;
		}
		/// <summary>
		/// Converts the input image position (expressed in pixels) to the patient coordinate system.
		/// </summary>
		/// <returns>A position vector, or null if the <see cref="Frame"/>'s position information is invalid.</returns>
		public Vector3D ConvertToPatient(PointF positionPixels)
		{
			if (ImageOrientationPatient.IsNull || PixelSpacing.IsNull)
				return null;

			// A shortcut for when the pixel position is exactly (0, 0).
			if (positionPixels.IsEmpty)
				return ImagePositionPatientVector;

			// Calculation of position in patient coordinates using 
			// the matrix method described in Dicom PS 3.3 C.7.6.2.1.1.
			var imageToPatientTransform = GetImageToPatientTransform();

			Matrix columnMatrix = new Matrix(4, 1);
			columnMatrix.SetColumn(0, positionPixels.X, positionPixels.Y, 0F, 1F);
			Matrix result = imageToPatientTransform*columnMatrix;

			return new Vector3D(result[0, 0], result[1, 0], result[2, 0]);
		}
Example #4
0
        /// <summary>
		/// Converts the input image position (expressed in pixels) to the patient coordinate system.
		/// </summary>
		/// <returns>A position vector, or null if the <see cref="Frame"/>'s position information is invalid.</returns>
		public Vector3D ConvertToPatient(PointF positionPixels)
		{
            if (ImageOrientationPatient.IsNull || PixelSpacing.IsNull)
				return null;

			// A shortcut for when the pixel position is (0, 0).
            if (positionPixels.X == 0F && positionPixels.Y == 0F)
                return ImagePositionPatientVector;

			// Calculation of position in patient coordinates using 
			// the matrix method described in Dicom PS 3.3 C.7.6.2.1.1.

			if (_pixelToPatientTransform == null)
			{
				_pixelToPatientTransform = new Matrix(4, 4);

                _pixelToPatientTransform.SetColumn(0, (float)(ImageOrientationPatient.RowX * PixelSpacing.Column),
                                     (float)(ImageOrientationPatient.RowY * PixelSpacing.Column),
                                     (float)(ImageOrientationPatient.RowZ * PixelSpacing.Column), 0F);

                _pixelToPatientTransform.SetColumn(1, (float)(ImageOrientationPatient.ColumnX * PixelSpacing.Row),
                                     (float)(ImageOrientationPatient.ColumnY * PixelSpacing.Row),
                                     (float)(ImageOrientationPatient.ColumnZ * PixelSpacing.Row), 0F);

                _pixelToPatientTransform.SetColumn(3, ImagePositionPatientVector.X, ImagePositionPatientVector.Y, ImagePositionPatientVector.Z, 1F);
			}

			Matrix columnMatrix = new Matrix(4, 1);
			columnMatrix.SetColumn(0, positionPixels.X, positionPixels.Y, 0F, 1F);
			Matrix result = _pixelToPatientTransform * columnMatrix;

			return new Vector3D(result[0, 0], result[1, 0], result[2, 0]);
		}
Example #5
0
		/// <summary>
		/// Converts the input <paramref name="positionPatient">position vector</paramref> to the coordinate
		/// system of the image plane, moving the origin to <paramref name="originPatient"/>.
		/// </summary>
		/// <remarks>
		/// Note that the resultant position vector remains in units of mm and the z-coordinate is valid.
		/// </remarks>
		/// <param name="positionPatient">The position vector, in patient coordinates,
		/// to be converted to the coordinate system of the image plane.</param>
		/// <param name="originPatient">The new origin, in patient coordinates.</param>
		/// <returns>A position vector, or null if the <see cref="Frame"/>'s position information is invalid.</returns>
		public Vector3D ConvertToImagePlane(Vector3D positionPatient, Vector3D originPatient)
		{
			Platform.CheckForNullReference(positionPatient, "positionPatient");

			Vector3D translated = positionPatient;
			if (originPatient != null)
				translated -= originPatient;

			Matrix rotationMatrix = GetRotationMatrix();
			if (rotationMatrix == null)
				return null;

			Matrix translatedMatrix = new Matrix(3, 1);
			translatedMatrix.SetColumn(0, translated.X, translated.Y, translated.Z);

			// Rotate coordinate system to match that of the image plane.
			Matrix rotated = rotationMatrix * translatedMatrix;
			return new Vector3D(rotated[0, 0], rotated[1, 0], rotated[2, 0]);
		}