1 #include "wxSphereView.h"
4 wxSphereView::wxSphereView( wxWindow *parent, vtkMPRBaseData *vtkmprbasedata, vtkImageData *imageData )
5 : wxVtk2DBaseView(parent)
8 _vtkmprbasedata = vtkmprbasedata;
9 _imageDataOriginal = imageData;
11 _imageSphere = vtkImageData::New();
12 _imageSphere->SetDimensions (150,150,500);
13 _imageSphere->SetScalarTypeToUnsignedShort();
14 _imageSphere->AllocateScalars();
15 _imageSphere->Update();
18 vtkBaseData *vtkbasedata = new vtkBaseData();
19 vtkbasedata->SetMarImageData( new marImageData(_imageSphere) );
20 this->SetVtkBaseData(vtkbasedata);
22 _transform = vtkTransform::New();
23 _transform1 = vtkTransform::New();
24 _transform2 = vtkTransform::New();
25 _transform ->Identity();
26 _transform1->Identity();
27 _transform2->Identity();
32 //-------------------------------------------------------------------
34 wxSphereView::~wxSphereView()
36 _transform -> Delete();
37 _transform1 -> Delete();
38 _transform2 -> Delete();
42 //----------------------------------------------------------------------------
44 double wxSphereView::GetRadio()
49 //----------------------------------------------------------------------------
51 void wxSphereView::SetRadio(double radio)
60 //----------------------------------------------------------------------------
62 void wxSphereView::Configure()
64 wxVtk2DBaseView::Configure();
66 _vtkinteractorstylesphere = new vtkInteractorStyleSphere();
67 ((vtkInteractorStyleBaseView*)GetInteractorStyleBaseView())->AddInteractorStyleMaracas( _vtkinteractorstylesphere );
70 // EED purify 12/sep/2006
84 //----------------------------------------------------------------------------
86 void wxSphereView::RefreshPoint()
88 double x = _vtkmprbasedata->GetX() - _centerX;
89 double y = _vtkmprbasedata->GetY() - _centerY;
90 double z = _vtkmprbasedata->GetZ() - _centerZ;
91 double alpha= atan2(x,z);
92 double beta = atan2( y , sqrt(z*z+x*x) );
94 alpha = alpha*180/3.1416;
95 beta = beta*180/3.1416;
97 _transform1->Identity();
98 _transform1->RotateY(alpha);
99 _transform1->RotateX(-beta);
101 _radio= sqrt(x*x + y*y +z*z);
106 //----------------------------------------------------------------------------
108 void wxSphereView::RefreshView()
111 wxVtk2DBaseView::Refresh();
114 //----------------------------------------------------------------------------
116 void wxSphereView::RotationEnd()
118 _transform1->RotateWXYZ(_ang,_vxb,_vyb,0);
119 _transform2->Identity();
124 //----------------------------------------------------------------------------
126 void wxSphereView::RotationStart(double vx, double vy, bool ok_v, bool ok_ang)
130 _ang = -sqrt( vx*vx + vy*vy ) / 1.0;
138 _transform2->Identity();
139 _transform2->RotateWXYZ(_ang,_vxb,_vyb,0);
144 //----------------------------------------------------------------------------
146 void wxSphereView::GetPointSphere(double p[3],double r1,double angA,double angB)
152 vtkTransform *transform = vtkTransform::New();
153 transform->Identity();
154 transform->RotateX(angB);
155 transform->RotateZ(angA);
156 transform->TransformPoint(in,out);
163 //----------------------------------------------------------------------------
165 void wxSphereView::RotatePointOverTheSphere( double pp[3], double p[3],double cc[3])
169 _transform->TransformPoint(p,out);
170 pp[0] = out[0] + cc[0];
171 pp[1] = out[1] + cc[1];
172 pp[2] = out[2] + cc[2];
176 //----------------------------------------------------------------------------
178 void wxSphereView::TransferePoints(double pp1[3],double pp2[3],double AngX,double AngY,vtkImageData *image)
181 double difX = pp2[0]-pp1[0];
182 double difY = pp2[1]-pp1[1];
183 double difZ = pp2[2]-pp1[2];
190 _imageDataOriginal->GetDimensions(dimOrg);
191 image->GetDimensions(dimRes);
197 int xx=-1,yy=-1,zz=-1;
202 xx = (int) (x1+t*difX);
203 yy = (int) (y1+t*difY);
204 zz = (int) (z1+t*difZ);
207 if ((xx>=0) && (xx<dimOrg[0]) && (yy>=0) && (yy<dimOrg[1]) && (zz>=0) && (zz<dimOrg[2]) &&
208 (AngX>=0) && (AngX<dimRes[0]) && (AngY>=0) && (AngY<dimRes[1]) && (z>=0) && (z<dimRes[2]) )
210 unsigned short *pOrg=(unsigned short*)_imageDataOriginal->GetScalarPointer (xx,yy,zz);
211 unsigned short *pRes=(unsigned short*)image->GetScalarPointer( (int)AngX , (int)AngY , z );
217 //----------------------------------------------------------------------------
219 void wxSphereView::ResetlstId()
221 int i,size=_lstId.size();
222 for (i=size-1;i>=0;i--)
229 //----------------------------------------------------------------------------
231 int wxSphereView::GetIdOfImage(double radio)
235 _imageSphere->GetDimensions(dim);
236 int sizeMaxList = dim[2];
237 // Search in list >> alpha beta radio
238 int i,size=_lstId.size();
239 for (i=0; i<size;i++)
241 //idAlBeRa *tmp=_lstId[i]; // JPRx
242 if ((_lstId[i]->_radio==radio) && (_lstId[i]->_deltavoxel==_delta))
244 return _lstId[i]->_id;
247 if (size>sizeMaxList)
249 delete _lstId[size-1];
254 id = id % sizeMaxList;
259 FiltreImage(id,radio);
260 _lstId.insert(_lstId.begin(),1,new idAlBeRa(id,radio,_delta) );
265 //----------------------------------------------------------------------------
267 void wxSphereView::DefineImageSphere()
270 id=GetIdOfImage( _radio );
271 GetVtkBaseData()->SetZ( id );
275 //----------------------------------------------------------------------------
276 void wxSphereView::SetDeltaVoxel(int delta)
281 //----------------------------------------------------------------------------
282 void wxSphereView::SetVoxel(double i, double j, int delta,double id, unsigned short gris)
285 unsigned short *pRes;
287 _imageSphere->GetDimensions(dimRes);
290 for ( ii=(int)(i-delta2) ; ii<=(int)(i+delta2) ; ii++ )
292 for ( jj=(int)(j-delta2) ; jj<=(int)(j+delta2) ; jj++ )
294 if ( (ii>=0)&&(ii<dimRes[0]) &&
295 (jj>=0)&&(jj<dimRes[1]) )
297 pRes = (unsigned short*)_imageSphere->GetScalarPointer( ii , jj , (int)id );
305 //----------------------------------------------------------------------------
307 void wxSphereView::SetXYZtoParent(double i, double j)
310 double factor = 0.75;
311 double radio2 = _radio*_radio;
312 double pxx,pyy,d2x,d2y;
313 double cc[3],p[3],pp[3];
318 int dimRes[3],dimOrig[3];
319 _imageSphere->GetDimensions(dimRes);
322 _imageDataOriginal->GetDimensions(dimOrig);
324 p[0] = (i - d2x)*factor;
326 p[1] = (j - d2y)*factor;
332 RotatePointOverTheSphere( pp, p,cc);
333 if ( (pp[0]>=0) && (pp[0]<dimOrig[0]) &&
334 (pp[1]>=0) && (pp[1]<dimOrig[1]) &&
335 (pp[2]>=0) && (pp[2]<dimOrig[2]) )
337 if (_vtkmprbasedata){
338 _vtkmprbasedata->SetX(pp[0]);
339 _vtkmprbasedata->SetY(pp[1]);
340 _vtkmprbasedata->SetZ(pp[2]);
347 //----------------------------------------------------------------------------
349 void wxSphereView::FiltreImageB(int id, double radio, bool ok,int deltaTMP)
351 double factor = 0.75;
352 double radioB = radio/3;
353 double radio2 = radio*radio;
354 double pxx,pyy,d2x,d2y;
355 double cc[3],p[3],pp[3];
360 unsigned short *pOrig;
361 int dimRes[3],dimOrig[3];
364 _imageSphere->GetExtent(ext);
365 _imageSphere->GetDimensions(dimRes);
367 //_imageSphere->SetExtent(0,dimRes[0]-1,0,dimRes[1]-1,0,dimRes[2]-1);
368 _imageSphere->SetExtent(ext);
372 // double deltaTMP=_delta;
373 _imageDataOriginal->GetDimensions(dimOrig);
377 limitA = (int) ( (-radioB/factor)+d2x );
378 limitB = (int) ( (radioB/factor)+d2x );
387 for ( i=start ; i<end ; i=i+deltaTMP )
389 p[0] = (i - d2x)*factor;
391 for (j=start;j<end;j=j+deltaTMP)
393 p[1] = (j - d2y)*factor;
397 if (( ((ok==false) && (!((i>limitA) && (i<limitB) && (j>limitA) && (j<limitB)))) )
404 RotatePointOverTheSphere( pp, p,cc);
405 if ( (pp[0]>=0) && (pp[0]<dimOrig[0]) &&
406 (pp[1]>=0) && (pp[1]<dimOrig[1]) &&
407 (pp[2]>=0) && (pp[2]<dimOrig[2]) )
409 pOrig=(unsigned short*)_imageDataOriginal->GetScalarPointer( (int)(pp[0]) , (int)(pp[1]) , (int)(pp[2]) );
410 SetVoxel(i,j,deltaTMP,id,*pOrig);
412 SetVoxel(i,j,deltaTMP,id,2000);
415 SetVoxel(i,j,deltaTMP,id,0);
421 _imageSphere->Modified();
422 _imageSphere->Update();
428 //----------------------------------------------------------------------------
430 void wxSphereView::FiltreImage(int id, double radio)
433 _transform -> Identity();
434 _transform -> Concatenate(_transform1);
435 _transform -> Concatenate(_transform2);
437 FiltreImageB(id,radio,false, _delta);
438 FiltreImageB(id,radio,true, 1);
442 //----------------------------------------------------------------------------
445 void wxSphereView::FiltreImage(int id, double radio)
447 double radio2 = radio*radio;
448 double radio2TMP= (radio/2)*(radio/2);
449 double pxx,pyy,d2x,d2y;
450 double cc[3],p[3],pp[3];
455 unsigned short *pOrig;
456 int dimRes[3],dimOrig[3];
458 _imageSphere->GetDimensions(dimRes);
459 _imageSphere->SetExtent(0,dimRes[0]-1,0,dimRes[1]-1,0,dimRes[2]-1);
462 double deltaTMP=_delta;
463 _imageDataOriginal->GetDimensions(dimOrig);
465 for ( i=0 ; i<dimRes[0] ; i=i+deltaTMP )
467 p[0] = (i - d2x)*0.75;
469 for (j=0;j<dimRes[1];j=j+deltaTMP)
471 p[1] = (j - d2y)*0.75;
480 RotatePointOverTheSphere( pp, p,cc);
481 if ( (pp[0]>=0) && (pp[0]<dimOrig[0]) &&
482 (pp[1]>=0) && (pp[1]<dimOrig[1]) &&
483 (pp[2]>=0) && (pp[2]<dimOrig[2]) )
485 pOrig=(unsigned short*)_imageDataOriginal->GetScalarPointer( pp[0] , pp[1] , pp[2] );
486 SetVoxel(i,j,deltaTMP,id,*pOrig);
488 SetVoxel(i,j,deltaTMP,id,2000);
491 SetVoxel(i,j,deltaTMP,id,0);
499 for ( i=0 ; i<dimRes[0] ; i=i+deltaTMP )
501 p[0] = (i - d2x)*0.75;
503 for (j=0;j<dimRes[1];j=j+deltaTMP)
505 p[1] = (j - d2y)*0.75;
513 RotatePointOverTheSphere( pp, p,cc);
514 if ( (pp[0]>=0) && (pp[0]<dimOrig[0]) &&
515 (pp[1]>=0) && (pp[1]<dimOrig[1]) &&
516 (pp[2]>=0) && (pp[2]<dimOrig[2]) )
518 pOrig=(unsigned short*)_imageDataOriginal->GetScalarPointer( pp[0] , pp[1] , pp[2] );
519 SetVoxel(i,j,deltaTMP,id,*pOrig);
521 SetVoxel(i,j,deltaTMP,id,2000);
524 SetVoxel(i,j,deltaTMP,id,0);
530 _imageSphere->Modified();
531 _imageSphere->Update();
535 void wxSphereView::FiltreImage(vtkImageData *imageSphere)
538 imageSphere->GetDimensions(dim);
539 for (i=0;i<dim[0];i++)
541 for (j=0;j<dim[1];j++)
543 for (k=0;k<dim[2];k++)
545 unsigned short *pRes=(unsigned short*)imageSphere->GetScalarPointer (i,j,k);
552 double cc[3],p1[3],p2[3],pp1[3],pp2[3];
556 double r1 = _sl_radio->GetValue() - _sl_thickness->GetValue()/2;
557 double r2 = _sl_radio->GetValue() + _sl_thickness->GetValue()/2;
562 double alpha= _sl_alpha->GetValue();
563 double beta = _sl_beta->GetValue();
566 for (angA=-deltaA;angA<deltaA;angA++)
568 for (angB=-deltaA;angB<deltaA;angB++)
570 GetPointSphere(p1,r1,angA,angB);
571 GetPointSphere(p2,r2,angA,angB);
572 RotatePointOverTheSphere( pp1, alpha, beta, p1 ,cc );
573 RotatePointOverTheSphere( pp2, alpha, beta, p2 ,cc );
574 TransferePoints(pp1,pp2,angA+alpha+180,angB+beta+90,imageSphere);
581 //----------------------------------------------------------------------------
583 void wxSphereView::InitSphere(double points[4][3])
586 double r = SphereFindCenter(points,cc); // 4-points , center
589 _centerX = (int)(cc[0]);
590 _centerY = (int)(cc[1]);
591 _centerZ = (int)(cc[2]);
594 _imageDataOriginal->GetDimensions(dim);
595 _centerX = (int)(dim[0]/2);
596 _centerY = (int)(dim[1]/2);
597 _centerZ = (int)(dim[2]/2);
601 //----------------------------------------------------------------------------
603 // Calculate center and radius of sphere given four points
604 // http://home.att.net/~srschmitt/script_sphere_solver.html
605 // source code HTML <script language=JavaScript>
606 double wxSphereView::SphereFindCenter(double P[4][3], double cc[3])
609 double r, m11, m12, m13, m14, m15;
612 for (i = 0; i < 4; i++) // find minor 11
619 m11 = determinant( a, 4 );
621 for (i = 0; i < 4; i++) // find minor 12
623 a[i][0] = P[i][0]*P[i][0] + P[i][1]*P[i][1] + P[i][2]*P[i][2];
628 m12 = determinant( a, 4 );
630 for (i = 0; i < 4; i++) // find minor 13
632 a[i][0] = P[i][0]*P[i][0] + P[i][1]*P[i][1] + P[i][2]*P[i][2];
637 m13 = determinant( a, 4 );
639 for (i = 0; i < 4; i++) // find minor 14
641 a[i][0] = P[i][0]*P[i][0] + P[i][1]*P[i][1] + P[i][2]*P[i][2];
646 m14 = determinant( a, 4 );
649 for (i = 0; i < 4; i++) // find minor 15
651 a[i][0] = P[i][0]*P[i][0] + P[i][1]*P[i][1] + P[i][2]*P[i][2];
656 m15 = determinant( a, 4 );
665 cc[0] = 0.5*m12/m11; //cx
666 cc[1] = -0.5*m13/m11; //cy
667 cc[2] = 0.5*m14/m11; //cz
669 r = sqrt( cc[0]*cc[0] + cc[1]*cc[1] + cc[2]*cc[2] - m15/m11 );
672 return r; // the radius
674 //----------------------------------------------------------------------------
676 // Recursive definition of determinate using expansion by minors.
677 double wxSphereView::determinant(double a[4][4], int n)
691 if (n == 2) // terminate recursion
693 d = a[0][0]*a[1][1] - a[1][0]*a[0][1];
698 for (j1 = 0; j1 < n; j1++ ) // do each column
700 for (i = 1; i < n; i++) // create minor
703 for (j = 0; j < n; j++)
705 if (j == j1) continue;
706 m[i-1][j2] = a[i][j];
711 // sum (+/-)cofactor * minor
712 d = d + pow(-1.0, j1)*a[0][j1]*determinant( m, n-1 );