1 /*=========================================================================
4 Module: $RCSfile: gdcmOrientation.cxx,v $
6 Date: $Date: 2005/09/16 16:47:56 $
7 Version: $Revision: 1.7 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
19 #include "gdcmOrientation.h"
21 #include "gdcmDebug.h"
22 #include <math.h> // for sqrt
26 //--------------------------------------------------------------------
27 // THERALYS Algorithm to determine the most similar basic orientation
29 // Transliterated from original Python code.
30 // Kept as close as possible to the original code
31 // in order to speed up any further modif of Python code :-(
32 //-----------------------------------------------------------------------
35 * \brief THERALYS' Algorithm to determine the most similar basic orientation
36 * (Axial, Coronal, Sagital) of the image
37 * \note Should be run on the first gdcm::File of a 'coherent' Serie
38 * @return orientation code
39 * # 0 : Not Applicable (neither 0020,0037 Image Orientation Patient
40 * # nor 0020,0032 Image Position found)
44 * # -2 : Coronal invert
46 * # -3 : Sagital invert
48 * # -4 : Heart Axial invert
50 * # -5 : Heart Coronal invert
52 * # -6 : Heart Sagital invert
54 double Orientation::TypeOrientation( File *f )
57 bool succ = f->GetImageOrientationPatient( iop );
60 gdcmErrorMacro( "No Image Orientation (0020,0037) found in the file, cannot proceed." )
67 ori1.x = iop[0]; ori1.y = iop[1]; ori1.z = iop[2];
68 ori2.x = iop[3]; ori2.y = iop[4]; ori2.z = iop[5];
70 // two perpendicular vectors describe one plane
71 double dicPlane[6][2][3] =
72 { { {1, 0, 0 },{0, 1, 0 } }, // Axial
73 { {1, 0, 0 },{0, 0, -1 } }, // Coronal
74 { {0, 1, 0 },{0, 0, -1 } }, // Sagittal
75 { { 0.8, 0.5, 0.0 },{-0.1, 0.1 , -0.95 } }, // Axial - HEART
76 { { 0.8, 0.5, 0.0 },{-0.6674, 0.687, 0.1794} }, // Coronal - HEART
77 { {-0.1, 0.1, -0.95},{-0.6674, 0.687, 0.1794} } // Sagittal - HEART
83 Res res; // [ <result> , <memory of the last succes calcule> ]
86 for (int numDicPlane=0; numDicPlane<6; numDicPlane++)
90 refA.x = dicPlane[numDicPlane][0][0];
91 refA.y = dicPlane[numDicPlane][0][1];
92 refA.z = dicPlane[numDicPlane][0][2];
94 refB.x = dicPlane[numDicPlane][1][0];
95 refB.y = dicPlane[numDicPlane][1][1];
96 refB.z = dicPlane[numDicPlane][1][2];
97 res=VerfCriterion( i, CalculLikelyhood2Vec(refA,refB,ori1,ori2), res );
98 res=VerfCriterion( -i, CalculLikelyhood2Vec(refB,refA,ori1,ori2), res );
103 // res=[0,99999] ## [ <result> , <memory of the last succes calculus> ]
104 // for plane in dicPlane:
108 // res=self.VerfCriterion( i , self.CalculLikelyhood2Vec(refA,refB,ori1,ori2) , res )
109 // res=self.VerfCriterion( -i , self.CalculLikelyhood2Vec(refB,refA,ori1,ori2) , res )
115 Orientation::VerfCriterion(int typeCriterion, double criterionNew, Res const &in)
118 double criterion = in.second;
119 if (criterionNew < criterion)
121 res.first = criterionNew;
122 res.second = typeCriterion;
126 // criterion = res[1]
127 // # if criterionNew<0.1 and criterionNew<criterion:
128 // if criterionNew<criterion:
129 // criterion=criterionNew
130 // type=typeCriterion
131 // return [ type , criterion ]
136 inline double square_dist(vector3D const &v1, vector3D const &v2)
139 res = (v1.x - v2.x)*(v1.x - v2.x) +
140 (v1.y - v2.y)*(v1.y - v2.y) +
141 (v1.z - v2.z)*(v1.z - v2.z);
145 //------------------------- Purpose : -----------------------------------
146 //- This function determines the orientation similarity of two planes.
147 // Each plane is described by two vectors.
148 //------------------------- Parameters : --------------------------------
149 //- <refA> : - type : vector 3D (double)
150 //- <refB> : - type : vector 3D (double)
151 // - Description of the first plane
152 //- <ori1> : - type : vector 3D (double)
153 //- <ori2> : - type : vector 3D (double)
154 // - Description of the second plane
155 //------------------------- Return : ------------------------------------
156 // double : 0 if the planes are perpendicular. While the difference of
157 // the orientation between the planes are big more enlarge is
159 //------------------------- Other : -------------------------------------
160 // The calculus is based with vectors normalice
162 Orientation::CalculLikelyhood2Vec(vector3D const &refA, vector3D const &refB,
163 vector3D const &ori1, vector3D const &ori2 )
166 vector3D ori3 = ProductVectorial(ori1,ori2);
167 vector3D refC = ProductVectorial(refA,refB);
168 double res = square_dist(refC, ori3);
173 //------------------------- Purpose : -----------------------------------
174 //- Calculus of the poduct vectorial between two vectors 3D
175 //------------------------- Parameters : --------------------------------
176 //- <vec1> : - type : vector 3D (double)
177 //- <vec2> : - type : vector 3D (double)
178 //------------------------- Return : ------------------------------------
179 // (vec) : - Vector 3D
180 //------------------------- Other : -------------------------------------
182 Orientation::ProductVectorial(vector3D const & vec1, vector3D const & vec2)
185 vec3.x = vec1.y*vec2.z - vec1.z*vec2.y;
186 vec3.y = -( vec1.x*vec2.z - vec1.z*vec2.x);
187 vec3.z = vec1.x*vec2.y - vec1.y*vec2.x;
192 } // end namespace gdcm
197 // ---------------------------------------------------------------------------
198 // Here is the original code, in Python, kindly supllied by THERALYS
200 // C++ code doesn't give good results
205 def TypeOrientation(self,file0):
207 # ------------------------- Purpose : -----------------------------------
208 # - This function compare the orientation of the given image and the
209 # basics orientations (Axial, Cornal, Sagital)
210 # ------------------------- Parameters : --------------------------------
211 # - <file0> : - type : string
212 # - The name of the first image file of the serie
213 # ------------------------- Return : ------------------------------------
217 # -2 : Coronal invert
219 # -3 : Sagital invert
221 # -4 : Heart Axial invert
223 # -5 : Heart Coronal invert
225 # -6 : Heart Sagital invert
227 # ------------------------- Other : -------------------------------------
228 # This method finds the most similar basic orientation.
231 toRead = gdcm.File(file0)
232 ValDict = GetValuesDict(toRead)
234 imageOrientation=ValDict["Image Orientation (Patient)"]
236 imageOrientation=ValDict["Image Orientation"]
238 ori1=[float(split(imageOrientation,"\\")[0]),\
239 float(split(imageOrientation,"\\")[1]),\
240 float(split(imageOrientation,"\\")[2])]
241 ori2=[float(split(imageOrientation,"\\")[3]),\
242 float(split(imageOrientation,"\\")[4]),\
243 float(split(imageOrientation,"\\")[5])]
245 ## two vectors perpendicular describe one plane
246 dicPlane=[ [ [1,0,0],[0,1,0] ], ## Axial
247 [ [1,0,0],[0,0,-1] ], ## Coronal
248 [ [0,1,0],[0,0,-1] ], ## Sagittal
249 [ [ 0.8 , 0.5 , 0.0 ],[-0.1 , 0.1 , -0.95] ],## Axial - HEART
250 [ [ 0.8 , 0.5 , 0.0 ],[-0.6674 , 0.687 , 0.1794] ],## Coronal - HEART
251 [ [-0.1 , 0.1 , -0.95],[-0.6674 , 0.687 , 0.1794] ] ] ## Sagittal - HEART
254 res=[0,99999] ## [ <result> , <memory of the last succes calcule> ]
255 for plane in dicPlane:
259 res=self.VerfCriterion( i , self.CalculLikelyhood2Vec(refA,refB,ori1,ori2) , res )
260 res=self.VerfCriterion( -i , self.CalculLikelyhood2Vec(refB,refA,ori1,ori2) , res )
267 def VerfCriterion(self,typeCriterion,criterionNew,res):
270 # if criterionNew<0.1 and criterionNew<criterion:
271 if criterionNew<criterion:
272 criterion=criterionNew
274 return [ type , criterion ]
277 def CalculLikelyhood2Vec(self,refA,refB,ori1,ori2):
279 # ------------------------- Purpose : -----------------------------------
280 # - This function determine the orientation similarity of two planes.
281 # Each plane is described by two vector.
282 # ------------------------- Parameters : --------------------------------
283 # - <refA> : - type : vector 3D (float)
284 # - <refB> : - type : vector 3D (float)
285 # - Description of the first plane
286 # - <ori1> : - type : vector 3D (float)
287 # - <ori2> : - type : vector 3D (float)
288 # - Description of the second plane
289 # ------------------------- Return : ------------------------------------
290 # float : 0 if the planes are perpendicular.
291 # While the difference of the orientation between the planes
292 # are big more enlarge is
294 # ------------------------- Other : -------------------------------------
295 # The calculus is based with vectors normalice
298 ori3=self.ProductVectorial(ori1,ori2)
299 refC=self.ProductVectorial(refA,refB)
300 res=math.pow(refC[0]-ori3[0],2) + math.pow(refC[1]-ori3[1],2) + math.pow(refC[2]-ori3[2],2)
301 return math.sqrt(res)
303 def ProductVectorial(self,vec1,vec2):
305 # ------------------------- Purpose : -----------------------------------
306 # - Calculus of the poduct vectorial between two vectors 3D
307 # ------------------------- Parameters : --------------------------------
308 # - <vec1> : - type : vector 3D (float)
309 # - <vec2> : - type : vector 3D (float)
310 # ------------------------- Return : ------------------------------------
311 # (vec) : - Vector 3D
312 # ------------------------- Other : -------------------------------------
315 vec3[0]=vec1[1]*vec2[2] - vec1[2]*vec2[1]
316 vec3[1]=-( vec1[0]*vec2[2] - vec1[2]*vec2[0])
317 vec3[2]=vec1[0]*vec2[1] - vec1[1]*vec2[0]
320 def GetValuesDict(image):
322 Returns a dictionnary containing values associated with Field Names
323 dict["Dicom Field Name"]="Dicom field value"
325 val=image.GetFirstEntry()
328 if isinstance(val,gdcm.ValEntryPtr):
329 dic[val.GetName()]=val.GetValue()
330 val=image.GetNextEntry()