]> Creatis software - gdcm.git/blob - src/gdcmOrientation.cxx
ENH: Minor tweek
[gdcm.git] / src / gdcmOrientation.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmOrientation.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/24 02:49:25 $
7   Version:   $Revision: 1.2 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmOrientation.h"
20 #include "gdcmFile.h"
21 #include "gdcmDebug.h"
22
23 namespace gdcm 
24 {
25 //--------------------------------------------------------------------
26 //  THERALYS Algorithm to determine the most similar basic orientation
27 //
28 //  Transliterated from original Python code.
29 //  Kept as close as possible to the original code
30 //  in order to speed up any further modif of Python code :-(
31 //-----------------------------------------------------------------------
32
33 /**
34  * \brief  THERALYS' Algorithm to determine the most similar basic orientation
35  *           (Axial, Coronal, Sagital) of the image
36  * \note Should be run on the first gdcm::File of a 'coherent' Serie
37  * @return orientation code
38  *   #   0 :   Not Applicable (neither 0020,0037 Image Orientation Patient 
39  *   #                         nor     0020,0032Image Position     found )
40  *   #   1 :   Axial
41  *   #  -1 :   Axial invert
42  *   #   2 :   Coronal
43  *   #  -2 :   Coronal invert
44  *   #   3 :   Sagital
45  *   #  -3 :   Sagital invert
46  *   #   4 :   Heart Axial
47  *   #  -4 :   Heart Axial invert
48  *   #   5 :   Heart Coronal
49  *   #  -5 :   Heart Coronal invert
50  *   #   6 :   Heart Sagital
51  *   #  -6 :   Heart Sagital invert
52  */
53 double Orientation::TypeOrientation( File *f )
54 {
55    float iop[6];
56    bool succ = f->GetImageOrientationPatient( iop );
57    if ( !succ )
58    {
59       gdcmErrorMacro( "No Image Orientation (0020,0037) was found in the file, cannot proceed." )
60       return 0;
61    }
62
63    vector3D ori1;
64    vector3D ori2;
65
66    ori1.x = iop[0]; ori1.y = iop[1]; ori1.z = iop[2]; 
67    ori1.x = iop[3]; ori2.y = iop[4]; ori2.z = iop[5];
68
69    // two perpendicular vectors describe one plane
70    double dicPlane[6][2][3] =
71    { {  {1,    0,    0   },{0,       1,     0     }  }, // Axial
72      {  {1,    0,    0   },{0,       0,    -1     }  }, // Coronal
73      {  {0,    1,    0   },{0,       0,    -1     }  }, // Sagittal
74      {  { 0.8, 0.5,  0.0 },{-0.1,    0.1 , -0.95  }  }, // Axial - HEART
75      {  { 0.8, 0.5,  0.0 },{-0.6674, 0.687, 0.1794}  }, // Coronal - HEART
76      {  {-0.1, 0.1, -0.95},{-0.6674, 0.687, 0.1794}  }  // Sagittal - HEART
77    };
78
79    vector3D refA;
80    vector3D refB;
81    int i = 0;
82    Res res;   // [ <result> , <memory of the last succes calcule> ]
83    res.first = 0;
84    res.second = 99999;
85    for (int numDicPlane=0; numDicPlane<6; numDicPlane++)
86    {
87        ++i;
88        // refA=plane[0]
89        refA.x = dicPlane[numDicPlane][0][0]; 
90        refA.y = dicPlane[numDicPlane][0][1]; 
91        refA.z = dicPlane[numDicPlane][0][2];
92        // refB=plane[1]
93        refB.x = dicPlane[numDicPlane][1][0]; 
94        refB.y = dicPlane[numDicPlane][1][1]; 
95        refB.z = dicPlane[numDicPlane][1][2];
96        res=VerfCriterion(  i, CalculLikelyhood2Vec(refA,refB,ori1,ori2), res );
97        res=VerfCriterion( -i, CalculLikelyhood2Vec(refB,refA,ori1,ori2), res );
98    }
99    return res.first;
100 /*
101 //             i=0
102 //             res=[0,99999]  ## [ <result> , <memory of the last succes calculus> ]
103 //             for plane in dicPlane:
104 //                 i=i+1
105 //                 refA=plane[0]
106 //                 refB=plane[1]
107 //                 res=self.VerfCriterion(  i , self.CalculLikelyhood2Vec(refA,refB,ori1,ori2) , res )
108 //                 res=self.VerfCriterion( -i , self.CalculLikelyhood2Vec(refB,refA,ori1,ori2) , res )
109 //             return res[0]
110 */
111
112 }
113
114 Res 
115 Orientation::VerfCriterion(int typeCriterion, double criterionNew, Res const & in)
116 {
117    Res res;
118    double criterion = in.second;
119    if (criterionNew < criterion)
120    {
121       res.first  = criterionNew;
122       res.second = typeCriterion;
123    }
124 /*
125 //   type = res[0]
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 ]
132 */
133    return res;
134
135
136 inline double square_dist(vector3D const &v1, vector3D const & v2)
137 {
138   double res;
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);
142   return res;
143 }
144
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
158 //            the criterion.
159 //------------------------- Other : -------------------------------------
160 // The calculus is based with vectors normalice
161 double
162 Orientation::CalculLikelyhood2Vec(vector3D const & refA, vector3D const & refB, 
163                                   vector3D const & ori1, vector3D const & ori2 )
164 {
165
166    vector3D ori3 = ProductVectorial(ori1,ori2);
167    vector3D refC = ProductVectorial(refA,refB);
168    double res = square_dist(refC, ori3);
169
170    return sqrt(res);
171 }
172
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 : -------------------------------------
181 vector3D
182 Orientation::ProductVectorial(vector3D const & vec1, vector3D const & vec2)
183 {
184    vector3D vec3;
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;
188
189    return vec3;
190 }
191
192 } // end namespace gdcm
193