]> Creatis software - creaImageIO.git/blob - lib/TransformDicom3D/matrixRotation.cxx
40b1f5272245de384570b918b6ee72f2177fd099
[creaImageIO.git] / lib / TransformDicom3D / matrixRotation.cxx
1 #include"Math.h"
2 #include"matrixRotation.h"
3
4 //-------------------------------------------------------------------------------------------
5 matrixRotation::matrixRotation()
6 {
7         spcX = 1;
8         spcY = 1;
9         spcZ = 1;
10          
11         orgX = 0;
12         orgY = 0;
13         orgZ = 0;
14
15         vec1X = 1;
16         vec1Y = 0;
17         vec1Z = 0;
18         vec2X = 0;
19         vec2Y = 1;
20         vec2Z = 0;
21 }
22
23 //-------------------------------------------------------------------------------------------
24 matrixRotation::~matrixRotation()
25 {
26 }
27
28 void matrixRotation::CrossProduct(double *vecZx, double *vecZy, double *vecZz)
29 {       
30         *vecZx = vec1Y * vec2Z - vec1Z * vec2Y; 
31         *vecZy = vec1Z * vec2X - vec1X * vec2Z; 
32         *vecZz = vec1X * vec2Y - vec1Y * vec2X;         
33 }
34
35 //-------------------------------------------------------------------------------------------
36 void matrixRotation::GetTransformation(vtkTransform *transform)
37 {
38         double dst_nrm_dircos_x;
39         double dst_nrm_dircos_y;
40         double dst_nrm_dircos_z;
41         //http://www.dclunie.com/medical-image-faq/html/part2.html
42         CrossProduct( &dst_nrm_dircos_x , &dst_nrm_dircos_y , &dst_nrm_dircos_z );
43         vtkMatrix4x4 *mat = vtkMatrix4x4::New();
44     
45         mat->SetElement(0,0,vec1X);
46         mat->SetElement(1,0,vec1Y);
47         mat->SetElement(2,0,vec1Z);
48     mat->SetElement(3,0,0);
49     
50         mat->SetElement(0,1,vec2X);
51     mat->SetElement(1,1,vec2Y);
52         mat->SetElement(2,1,vec2Z);
53         mat->SetElement(3,1,0);
54     
55         mat->SetElement(0,2,dst_nrm_dircos_x);
56         mat->SetElement(1,2,dst_nrm_dircos_y);
57         mat->SetElement(2,2,dst_nrm_dircos_z);
58         mat->SetElement(3,2,0);
59
60     mat->SetElement(0,3,0);
61         mat->SetElement(1,3,0);
62         mat->SetElement(2,3,0);
63         mat->SetElement(3,3,1);
64     
65         transform->PostMultiply ();
66         transform->Identity();
67         transform->Scale(spcX,spcY,spcZ);
68         transform->Update();
69         transform->Concatenate(mat);
70         transform->Update();
71         transform->Translate(orgX,orgY,orgZ);   
72         transform->Update();
73 }
74
75
76 //-------------------------------------------------------------------------------------------
77 void matrixRotation::SetSpacing(double spcX,double spcY, double spcZ)
78 {
79         this->spcX = spcX;
80         this->spcY = spcY;
81         this->spcZ = spcZ;
82 }
83
84 //-------------------------------------------------------------------------------------------
85 void matrixRotation::SetOrigin(double orgX,double orgY, double orgZ)
86 {
87         this->orgX = orgX;
88         this->orgY = orgY;
89         this->orgZ = orgZ;
90 }
91
92 //-------------------------------------------------------------------------------------------
93 void matrixRotation::SetVector1(double vX,double vY, double vZ)
94 {
95         double nor=sqrt(vX*vX + vY*vY + vZ*vZ);
96         this->vec1X = vX/nor;
97         this->vec1Y = vY/nor;
98         this->vec1Z = vZ/nor;
99 }
100
101 //-------------------------------------------------------------------------------------------
102 void matrixRotation::SetVector2(double vX,double vY, double vZ)
103 {
104         double nor=sqrt(vX*vX + vY*vY + vZ*vZ);
105         this->vec2X = vX/nor;
106         this->vec2Y = vY/nor;
107         this->vec2Z = vZ/nor;
108 }