IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
DICOM.cs
1 using System;
2 using System.Runtime.InteropServices;
3 using UnityEngine;
4 using itk.simple;
5 using System.Collections.Generic;
6 
8 public class DICOM {
9 
13  public DICOMSeries seriesInfo { private set; get; }
18  public int dimensions { protected set; get; }
19  public int texWidth { protected set; get; }
20  public int texHeight { protected set; get; }
21  public int texDepth { protected set; get; }
22 
24  public int origTexWidth { protected set; get; }
26  public int origTexHeight { protected set; get; }
28  public int origTexDepth { protected set; get; }
29 
31  public int texPaddingWidth { protected set; get; }
33  public int texPaddingHeight { protected set; get; }
35  public int texPaddingDepth { protected set; get; }
36 
42  public Image image { protected set; get; }
43 
45  public Vector3 origin { protected set; get; }
47  public Vector2 pixelSpacing { protected set; get; }
48 
50  public Vector3 sliceOffset { get {return seriesInfo.sliceOffset;} }
51 
57  public Vector3 directionCosineX { protected set; get; }
63  public Vector3 directionCosineY { protected set; get; }
64 
66  public Vector3 sliceNormal { protected set; get; }
67 
70  public Matrix4x4 pixelToPatient { protected set; get; }
71 
75  public Matrix4x4 patientToPixel { protected set; get; }
76 
85 
86  // Remember, we will need it later:
87  this.seriesInfo = seriesInfo;
88  }
89 
90  // ===============================================================
91  // Transformations:
92 
99  {
100  // Set up the transformation matrix:
101  Matrix4x4 transformMatrix = new Matrix4x4 ();
102  // Column 1:
103  transformMatrix [0, 0] = directionCosineX.x * pixelSpacing.x;
104  transformMatrix [1, 0] = directionCosineX.y * pixelSpacing.x;
105  transformMatrix [2, 0] = directionCosineX.z * pixelSpacing.x;
106  transformMatrix [3, 0] = 0f;
107  // Column 2:
108  transformMatrix [0, 1] = directionCosineY.x * pixelSpacing.y;
109  transformMatrix [1, 1] = directionCosineY.y * pixelSpacing.y;
110  transformMatrix [2, 1] = directionCosineY.z * pixelSpacing.y;
111  transformMatrix [3, 1] = 0f;
112  // Column 3:
113  transformMatrix [0, 2] = sliceOffset.x;
114  transformMatrix [1, 2] = sliceOffset.y;
115  transformMatrix [2, 2] = sliceOffset.z;
116  transformMatrix [3, 2] = 0f;
117  // Column 4:
118  transformMatrix [0, 3] = origin.x;
119  transformMatrix [1, 3] = origin.y;
120  transformMatrix [2, 3] = origin.z;
121  transformMatrix [3, 3] = 1f;
122 
123  // Convert to a the left-hand-side coordinate system which Unity uses:
124  Matrix4x4 rightHandToLeftHand = new Matrix4x4 ();
125  rightHandToLeftHand [0, 0] = 1f;
126  rightHandToLeftHand [1, 1] = 1f;
127  rightHandToLeftHand [2, 2] = -1f;
128  rightHandToLeftHand [3, 3] = 1f;
129 
130  pixelToPatient = rightHandToLeftHand*transformMatrix;
131 
132  // Inverse transformation:
133  patientToPixel = pixelToPatient.inverse;
134  }
138  public Vector3 transformPixelToPatientPos( Vector2 pixel, float layer = 0 )
139  {
140  Vector4 p = new Vector4 (pixel.x, pixel.y, layer, 1f);
141  Vector4 pos = pixelToPatient * p;
142  return new Vector3 (pos.x, pos.y, pos.z);
143  }
144 
151  public Vector3 transformPatientPosToPixel( Vector3 pos )
152  {
153  Vector4 p = new Vector4 (pos.x, pos.y, pos.z, 1f);
154  Vector4 pixel = patientToPixel * p;
155  return new Vector3 (pixel.x, pixel.y, pixel.z);
156  }
157 
163  public Vector3 transformPatientPosToDiscretePixel( Vector3 pos )
164  {
165  Vector3 pixel = transformPatientPosToPixel( pos );
166  Vector3 rounded = new Vector3 (Mathf.Round (pixel.x),
167  Mathf.Round (pixel.y),
168  Mathf.Round (pixel.z));
169  return rounded;
170  }
171 
173  public static Color32 F2C(UInt32 value)
174  {
175  Color32 c = new Color32 ();
176 
177  /*c.r = (byte)(value % 256); value /= 256;
178  c.g = (byte)(value % 256); value /= 256;
179  c.b = (byte)(value % 256); value /= 256;
180  c.a = (byte)(value % 256);*/
181 
182  /*c.r = (byte)(value % 256); value = value >> 8;
183  c.g = (byte)(value % 256); value = value >> 8;
184  c.b = (byte)(value % 256); value = value >> 8;
185  c.a = (byte)(value % 256);*/
186  c.a = (byte)(value >> 24);
187  c.b = (byte)(value >> 16);
188  c.g = (byte)(value >> 8);
189  c.r = (byte)(value);
190  //Debug.LogWarning ("c: " + c.r + " " + c.g + " " + c.b + " " + c.a);
191 
192  return c;
193  }
195  /*public static Color F2C(Int16 value)
196  {
197  UInt32 valueUInt = (UInt32)((int)value + 32768);
198  return F2C (valueUInt);
199  }*/
200  /*public static Color F2C(UInt16 value)
201  {
202  UInt32 valueUInt = (UInt32)(value);
203  return F2C (valueUInt);
204  }*/
205 }
Vector3 sliceOffset
Definition: DICOM.cs:50
int texPaddingDepth
Definition: DICOM.cs:35
Vector2 pixelSpacing
Definition: DICOM.cs:47
Vector3 directionCosineY
Definition: DICOM.cs:63
Matrix4x4 pixelToPatient
Definition: DICOM.cs:70
Vector3 directionCosineX
Definition: DICOM.cs:57
Vector3 origin
Definition: DICOM.cs:45
DICOM(DICOMSeries seriesInfo)
Definition: DICOM.cs:84
int origTexDepth
Definition: DICOM.cs:28
Matrix4x4 patientToPixel
Definition: DICOM.cs:75
int origTexWidth
Definition: DICOM.cs:24
Image image
Definition: DICOM.cs:42
Vector3 sliceNormal
Definition: DICOM.cs:66
Definition: DICOM.cs:8
Vector3 transformPatientPosToPixel(Vector3 pos)
Definition: DICOM.cs:151
int texPaddingWidth
Definition: DICOM.cs:31
Vector3 transformPatientPosToDiscretePixel(Vector3 pos)
Definition: DICOM.cs:163
int texPaddingHeight
Definition: DICOM.cs:33
int origTexHeight
Definition: DICOM.cs:26
void setupTransformationMatrices()
Definition: DICOM.cs:98
static Color32 F2C(UInt32 value)
Definition: DICOM.cs:173
int dimensions
Definition: DICOM.cs:18
Vector3 transformPixelToPatientPos(Vector2 pixel, float layer=0)
Definition: DICOM.cs:138
DICOMSeries seriesInfo
Definition: DICOM.cs:13