]> Creatis software - creaVtk.git/blob - lib/creaVtk/creaVtkHeartAngles.cpp
2187 BBTK Feature New Normal New feature creaVtk
[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 double creaVtkHeartAngles::alpha (double P0a, double P0b,double P0c, double vx, double vy, double vz, double vxp, double vyp, double vzp)
41 {
42         double a=P0a,b=P0b,c=P0c;
43         double x=vx,y=vy,z=vz;
44         double xp=vxp,yp=vyp,zp=vzp;
45         double alpha;
46
47         //Calcular el ángulo que forman las rectas, sabiendo sus vectores directores.
48         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)) ) ));
49         
50         alpha = (180 * acos(fabs(cosAlpha)))/M_PI;
51         //std::cout << "Angulo Alpha: " << alpha << std::endl; 
52         double ent = floor(alpha);
53         double al = alpha - ent;
54         double min = al * 60;
55         //std::cout << "minutos: " << min << std::endl;
56         std::cout << "Grados: "<< ent <<" minutos: " << floor(min) << std::endl;
57         return alpha;
58 }
59
60 double creaVtkHeartAngles::beta (double P0a, double P0b,double P0c, double P3x, double P3y, double P3z, double P4x, double P4y, double P4z)
61 {
62         double a=P0a,b=P0b,c=P0c;
63         double x=P3x,y=P3y,z=P3z;
64         double xp=P4x,yp=P4y,zp=P4z;
65         double beta;
66
67         //Calcular el ángulo que forman las rectas, sabiendo sus vectores directores.
68         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)) ) ));
69         
70         beta = (180 * acos(fabs(cosBeta)))/M_PI;
71         //std::cout << "Angulo Beta: " << beta << std::endl; 
72         double ent = floor(beta);
73         double al = beta - ent;
74         double min = al * 60;
75         //std::cout << "minutos: " << min << std::endl;
76         std::cout << "Grados: "<< ent <<" minutos: " << floor(min) << std::endl;
77         return beta;
78 }
79
80 double * creaVtkHeartAngles::vectorProjection (double plX, double plY, double plZ, double nX, double nY, double nZ, double vX, double vY, double vZ)
81 {
82         //punto del plano
83         double plx=plX, ply=plY, plz=plZ;
84         //normalPlano
85         double nx=nX, ny=nY, nz=nZ;
86         //vector V
87         double vx=vX, vy=vY, vz=vZ;
88
89         //distancia del punto al plano
90         double dist;
91         //vector proyectado     
92         double vpx, vpy, vpz;
93         double proj [3];
94         
95         dist = ply - vy;
96         //std::cout << dist << std
97         vpx = vx - (dist * nx);
98         vpy = vy - (dist * ny);
99         vpz = vz - (dist * nz);
100         
101         proj[0]=vpx;
102         proj[1]=vpy;
103         proj[2]=vpz;
104
105         return proj;
106 }
107
108 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)
109 {
110         double puntoCorte[3];
111
112         double Plx1 = plx1, Ply1 = ply1, Plz1 = plz1, Plx2 = plx2, Ply2 = ply2, Plz2 = plz2, Plx3 = plx3, Ply3 = ply3, Plz3 = plz3;
113         double Px1 = px1, Py1 = py1, Pz1 = pz1, Px2 = px2, Py2 = py2, Pz2 = pz2;
114
115         //Calculo del plano
116
117         double aX = ((Ply2-Ply1)*(Plz3-Plz1)-(Ply3-Ply1)*(Plz2-Plz1));
118         double a = (Plx1)*((Ply2-Ply1)*(Plz3-Plz1)-(Ply3-Ply1)*(Plz2-Plz1));
119
120         double bX = ((Plx2-Plx1)*(Plz3-Plz1)-(Plx3-Plx1)*(Plz2-Plz1));
121         double b = (Ply1)*((Plx2-Plx1)*(Plz3-Plz1)-(Plx3-Plx1)*(Plz2-Plz1));
122
123         double cX = ((Plx2-Plx1)*(Ply3-Ply1)-(Plx3-Plx1)*(Ply2-Ply1));
124         double c = (Plz1)*((Plx2-Plx1)*(Ply3-Ply1)-(Plx3-Plx1)*(Ply2-Ply1));
125
126         double r = a + b + c;
127         std::cout << "ecuacion: " << aX << "," << a << "," << bX << "," << b << "," << cX << "," << c << std::endl;
128         std::cout << "r: " << r << std::endl;
129
130         //Calculo del vector director
131         
132         double vdirx = Px2 - Px1;
133         double vdiry = Py2 - Py1;
134         double vdirz = Pz2 - Pz1;
135         
136         std::cout << "vdirx: " << vdirx << " vdiry: " << vdiry << " vdirz: " << vdirz << std::endl;     
137
138         //Se igualan las formulas del plano y la recta
139         //x=Px1+vdirx, y=Py1+vdiry, z=Pz1+vdirz
140         
141         double eRes = aX*Px1 + bX*Py1 + cX*Pz1;
142         double eResT = aX*vdirx + bX*vdiry + cX*vdirz;
143         std::cout << "eRes: " << eRes << " eResT: " << eResT << std::endl;
144         //Calculo del punto solucion
145         if(eResT == 0){
146                 std::cout << "Soluciones infinitas... " << std::endl;
147         }
148         else {
149                 double t = (((-1)*eRes)-r)/eResT;
150
151                 double Px = Px1 + (t * vdirx);
152                 double Py = Py1 + (t * vdiry);
153                 double Pz = Pz1 + (t * vdirz);  
154
155                 std::cout << "px: " << Px << " py: " << Py << " pz: " << Pz << std::endl;       
156         
157                 puntoCorte[0] = Px;
158                 puntoCorte[1] = Py;
159                 puntoCorte[2] = Pz;
160         }
161         return puntoCorte;
162 }
163
164 vtkImageData* creaVtkHeartAngles::calculateAngleAlpha (vtkImageData* image, double Px, double Py, double Pz, double Vx, double Vy, double Vz, double Nx, double Ny, double Nz /*, double* pPlane*/)
165 {
166         unsigned char* pPix;
167         pPix = (unsigned char*)image->GetScalarPointer();
168
169         for(int i=0;i<1041;i=i+3){
170                 int p1 = pPix[i];
171                 int p2 = pPix[i+1];
172                 int p3 = pPix[i+2];
173
174         double* vecProj;
175         vecProj = vectorProjection (Px, Py, Pz, Nx, Ny, Nz, p1, p2, p3);
176         double a, b;
177
178         //double* cutPoint;
179         //cutPoint = intersectionPlaneLine(pPlane[0], pPlane[1], pPlane[2], pPlane[3], pPlane[4], pPlane[5], pPlane[6], pPlane[7], pPlane[8], px1, py1, pz1, px2, py2, pz2);
180
181         a = alpha (p1, p2, p3, 
182                                                  p1, p2, p3, //?? es el vector?
183                                                  vecProj[0], vecProj[1], vecProj[2]);
184
185         }
186         
187         
188 }
189
190 vtkImageData* creaVtkHeartAngles::calculateAngleBeta (vtkImageData* image, double Px, double Py, double Pz, double Vx, double Vy, double Vz, double Nx, double Ny, double Nz /*, double* pPlane*/)
191 {
192         unsigned char* pPix;
193         pPix = (unsigned char*)image->GetScalarPointer();
194
195         for(int i=0;i<1041;i=i+3){
196                 int p1 = pPix[i];
197                 int p2 = pPix[i+1];
198                 int p3 = pPix[i+2];
199
200         double* vecProj;
201         vecProj = vectorProjection (Px, Py, Pz, Nx, Ny, Nz, p1, p2, p3);
202         double a, b;
203
204         //double* cutPoint;
205         //cutPoint = intersectionPlaneLine(pPlane[0], pPlane[1], pPlane[2], pPlane[3], pPlane[4], pPlane[5], pPlane[6], pPlane[7], pPlane[8], px1, py1, pz1, px2, py2, pz2);
206
207         b = beta (0, 0, 0, 
208                                                 p1, p2, p3, //?? es el vector?
209                                                 vecProj[0], vecProj[1], vecProj[2]);
210                 
211         }
212         
213         
214 }
215