]> Creatis software - creaVtk.git/blob - lib/creaVtk/creaVtkHeartAngles.cpp
cceaa643963d809df8312c2a1a8e1cc48986a0de
[creaVtk.git] / lib / creaVtk / creaVtkHeartAngles.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 #                        pour la Sante)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and
11 #  abiding by the rules of distribution of free software. You can  use,
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B
13 #  license as circulated by CEA, CNRS and INRIA at the following URL
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability.
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26 */
27
28 #include "creaVtkHeartAngles.h"
29 #include <math.h>
30 #include <iostream>
31
32 creaVtkHeartAngles::creaVtkHeartAngles()
33 {
34 }
35
36 creaVtkHeartAngles::~creaVtkHeartAngles()
37 {
38 }
39
40 vtkImageData* creaVtkHeartAngles::getAlphaImage ()
41 {
42         return alphaImage;
43 }
44
45 vtkImageData* creaVtkHeartAngles::getBetaImage ()
46 {
47         return betaImage;
48 };
49
50 double creaVtkHeartAngles::alpha (double P0a, double P0b,double P0c, double vx, double vy, double vz, double vxp, double vyp, double vzp)
51 {
52         double a=P0a,b=P0b,c=P0c;
53         double x=vx,y=vy,z=vz;
54         double xp=vxp,yp=vyp,zp=vzp;
55         double alpha;
56
57         //Calcular el ángulo que forman las rectas, sabiendo sus vectores directores.
58         double cosAlpha = ( ( (x-a)*(xp-a) )+( (y-b)*(yp-b) )+( (z-c)*(zp-c) ) )/sqrt((( (pow(x,2.0)-pow(a,2.0)) + (pow(y,2.0)-pow(b,2.0)) + (pow(z,2.0)-pow(c,2.0)) )*( (pow(xp,2.0)-pow(a,2.0)) + (pow(yp,2.0)-pow(b,2.0)) + (pow(zp,2.0)-pow(c,2.0)) ) ));
59         
60         alpha = (180 * acos(fabs(cosAlpha)))/M_PI;
61         //std::cout << "Angulo Alpha: " << alpha << std::endl; 
62         double ent = floor(alpha);
63         double al = alpha - ent;
64         double min = al * 60;
65         //std::cout << "minutos: " << min << std::endl;
66         std::cout << "Grados: "<< ent <<" minutos: " << floor(min) << std::endl;
67         return alpha;
68 }
69
70 double creaVtkHeartAngles::beta (double P0a, double P0b,double P0c, double P3x, double P3y, double P3z, double P4x, double P4y, double P4z)
71 {
72         double a=P0a,b=P0b,c=P0c;
73         double x=P3x,y=P3y,z=P3z;
74         double xp=P4x,yp=P4y,zp=P4z;
75         double beta;
76
77         //Calcular el ángulo que forman las rectas, sabiendo sus vectores directores.
78         double cosBeta = ( ( (x-a)*(xp-a) )+( (y-b)*(yp-b) )+( (z-c)*(zp-c) ) )/sqrt((( (pow(x,2.0)-pow(a,2.0)) + (pow(y,2.0)-pow(b,2.0)) + (pow(z,2.0)-pow(c,2.0)) )*( (pow(xp,2.0)-pow(a,2.0)) + (pow(yp,2.0)-pow(b,2.0)) + (pow(zp,2.0)-pow(c,2.0)) ) ));
79         
80         beta = (180 * acos(fabs(cosBeta)))/M_PI;
81         //std::cout << "Angulo Beta: " << beta << std::endl; 
82         double ent = floor(beta);
83         double al = beta - ent;
84         double min = al * 60;
85         //std::cout << "minutos: " << min << std::endl;
86         std::cout << "Grados: "<< ent <<" minutos: " << floor(min) << std::endl;
87         return beta;
88 }
89
90 double * creaVtkHeartAngles::vectorProjection (double plX, double plY, double plZ, double nX, double nY, double nZ, double vX, double vY, double vZ)
91 {
92         //punto del plano
93         double plx=plX, ply=plY, plz=plZ;
94         //normalPlano
95         double nx=nX, ny=nY, nz=nZ;
96         //vector V
97         double vx=vX, vy=vY, vz=vZ;
98
99         //distancia del punto al plano
100         double dist;
101         //vector proyectado     
102         double vpx, vpy, vpz;
103         double proj [3];
104         
105         dist = ply - vy;
106         //std::cout << dist << std
107         vpx = vx - (dist * nx);
108         vpy = vy - (dist * ny);
109         vpz = vz - (dist * nz);
110         
111         proj[0]=vpx;
112         proj[1]=vpy;
113         proj[2]=vpz;
114
115         return proj;
116 }
117
118 double *creaVtkHeartAngles::intersectionPlaneLine(double plx1, double ply1, double plz1, double plx2, double ply2, double plz2, double plx3, double ply3, double plz3, double px1, double py1, double pz1,double px2, double py2, double pz2)
119 {
120         double puntoCorte[3];
121
122         double Plx1 = plx1, Ply1 = ply1, Plz1 = plz1, Plx2 = plx2, Ply2 = ply2, Plz2 = plz2, Plx3 = plx3, Ply3 = ply3, Plz3 = plz3;
123         double Px1 = px1, Py1 = py1, Pz1 = pz1, Px2 = px2, Py2 = py2, Pz2 = pz2;
124
125         //Calculo del plano
126
127         double aX = ((Ply2-Ply1)*(Plz3-Plz1)-(Ply3-Ply1)*(Plz2-Plz1));
128         double a = (Plx1)*((Ply2-Ply1)*(Plz3-Plz1)-(Ply3-Ply1)*(Plz2-Plz1));
129
130         double bX = ((Plx2-Plx1)*(Plz3-Plz1)-(Plx3-Plx1)*(Plz2-Plz1));
131         double b = (Ply1)*((Plx2-Plx1)*(Plz3-Plz1)-(Plx3-Plx1)*(Plz2-Plz1));
132
133         double cX = ((Plx2-Plx1)*(Ply3-Ply1)-(Plx3-Plx1)*(Ply2-Ply1));
134         double c = (Plz1)*((Plx2-Plx1)*(Ply3-Ply1)-(Plx3-Plx1)*(Ply2-Ply1));
135
136         double r = a + b + c;
137         std::cout << "ecuacion: " << aX << "," << a << "," << bX << "," << b << "," << cX << "," << c << std::endl;
138         std::cout << "r: " << r << std::endl;
139
140         //Calculo del vector director
141         
142         double vdirx = Px2 - Px1;
143         double vdiry = Py2 - Py1;
144         double vdirz = Pz2 - Pz1;
145         
146         std::cout << "vdirx: " << vdirx << " vdiry: " << vdiry << " vdirz: " << vdirz << std::endl;     
147
148         //Se igualan las formulas del plano y la recta
149         //x=Px1+vdirx, y=Py1+vdiry, z=Pz1+vdirz
150         
151         double eRes = aX*Px1 + bX*Py1 + cX*Pz1;
152         double eResT = aX*vdirx + bX*vdiry + cX*vdirz;
153         std::cout << "eRes: " << eRes << " eResT: " << eResT << std::endl;
154         //Calculo del punto solucion
155         if(eResT == 0){
156                 std::cout << "Soluciones infinitas... " << std::endl;
157         }
158         else {
159                 double t = (((-1)*eRes)-r)/eResT;
160
161                 double Px = Px1 + (t * vdirx);
162                 double Py = Py1 + (t * vdiry);
163                 double Pz = Pz1 + (t * vdirz);  
164
165                 std::cout << "px: " << Px << " py: " << Py << " pz: " << Pz << std::endl;       
166         
167                 puntoCorte[0] = Px;
168                 puntoCorte[1] = Py;
169                 puntoCorte[2] = Pz;
170         }
171         return puntoCorte;
172 }
173
174 void creaVtkHeartAngles::calculateImages (vtkImageData* image, double Px, double Py, double Pz, double Vx, double Vy, double Vz, double Nx, double Ny, double Nz, double p2x, double p2y, double p2z)
175 {
176         alphaImage = image;
177   alphaImage->SetExtent( image->GetExtent() );
178   alphaImage->SetOrigin( image->GetOrigin() );
179   alphaImage->SetSpacing( image->GetSpacing() );
180   alphaImage->SetScalarTypeToUnsignedChar();
181   alphaImage->SetNumberOfScalarComponents( image->GetNumberOfScalarComponents() );
182   alphaImage->AllocateScalars();
183
184         betaImage = image;
185   betaImage->SetExtent( image->GetExtent() );
186   betaImage->SetOrigin( image->GetOrigin() );
187   betaImage->SetSpacing( image->GetSpacing() );
188   betaImage->SetScalarTypeToUnsignedChar();
189   betaImage->SetNumberOfScalarComponents( image->GetNumberOfScalarComponents() );
190   betaImage->AllocateScalars();
191
192         unsigned char* pPix;
193         pPix = (unsigned char*)image->GetScalarPointer();
194         double a;
195         double b;
196
197         for( int i = 0 ; i < image->GetExtent()[1] ; i++ )
198   {
199     for( int j = 0 ; j < image->GetExtent()[3] ; j++ )
200         {
201                         for( int k = 0 ; k < image->GetExtent()[5] ; k++ )
202                 {
203                         unsigned char* pPix = (unsigned char *)image->GetScalarPointer( i , j , k );
204                         int p1 = pPix[0];
205                         int p2 = pPix[1];
206                         int p3 = pPix[2];
207                         
208                         double* vecProj;
209                         vecProj = vectorProjection (Px, Py, Pz,
210                                                                                                                                         Nx, Ny, Nz, 
211                                                                                                                                         p1, p2, p3);
212                         a = alpha (Px, Py, Pz, 
213                                                            p1, p2, p3, 
214                                                            vecProj[0], vecProj[1], vecProj[2]);
215
216                         b = beta  (Px, Py, Pz, 
217                                                            p2x, p2y, p2z, 
218                                                            vecProj[0], vecProj[1], vecProj[2]);
219                         
220                         unsigned char *zPtr1 = (unsigned char *) alphaImage->GetScalarPointer( i , j , k );
221                         *zPtr1 = a;
222                         unsigned char *zPtr2 = (unsigned char *) betaImage->GetScalarPointer( i , j , k );
223                         *zPtr2 = b; 
224                         }
225         } 
226   }
227 }
228
229
230