marAxis.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   wxMaracas
00004   Module:    $RCSfile: marAxis.cpp,v $
00005   Language:  C++
00006   Date:      $Date: 2009/05/14 13:55:07 $
00007   Version:   $Revision: 1.1 $
00008 
00009   Copyright: (c) 2002, 2003
00010   License:
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00014      PURPOSE.  See the above copyright notice for more information.
00015 
00016 =========================================================================*/
00017 #include <vtkPlaneSource.h>
00018 #include <vtkKochanekSpline.h>
00019 #include <vtkPolyDataConnectivityFilter.h>         
00020 #include <vtkContourFilter.h>
00021 #include <vtkProbeFilter.h>
00022 
00023 
00024 #include <vtkImageChangeInformation.h>
00025 #include <vtkImageShiftScale.h> 
00026 #include <vtkDoubleArray.h> 
00027 
00028 
00029 #include "marAxis.h"
00030 #include "marVector.h"
00031 
00032 #include "ExtractionAxe.h"
00033 #include "FonctionsGrales.h"
00034 
00035 #include <vtkCellArray.h>
00036 #include <vtkCell.h>
00037 #include <vtkCleanPolyData.h>
00038 #include <vtkIdList.h>
00039 #include <vtkMath.h>
00040 #include <vtkMatrix4x4.h> 
00041 #include <vtkPointData.h>
00042 #include <vtkPolyData.h>
00043 #include <vtkPlane.h>
00044 #include <vtkSetGet.h>
00045 #include <vtkStripper.h>
00046 #include <vtkStructuredPoints.h>
00047 #include <vtkTransform.h> 
00048 
00049 // ----------------------------------------------------------------------------
00050 marAxis::marAxis( marParameters* p ) : 
00051        marObject( p ), 
00052            kCurve( 3, INDX_count - 3 ),
00053       _healthySlice( -1 ), _startQuant( -1 ),
00054       _finishQuant( -1 ), _actualQuant( -1 )
00055 
00056 {
00057         _totalAxisLenght=-1;
00058         _subAxisLenght=-1;
00059         _referenceArea=-1;
00060         _referenceAverDiam=-1;
00061 
00062 
00063     _allData = NULL;
00064 
00065         _healthySliceStart=-1;
00066         _healthySliceEnd=-1;
00067     reset( );
00068         calibration = false;
00069 }
00070 
00071 // ----------------------------------------------------------------------------
00072 void marAxis::addAxisPoint( double* p )
00073 {
00074     double pt[ INDX_count ];
00075 
00076     memcpy( pt, p, sizeof( POINTAXE ) );
00077     pt[ INDX_SIGNALVALUE ] = 0;
00078     addControlPoint( pt, pt + 3 );
00079 }
00080 
00081 // ----------------------------------------------------------------------------
00082 
00083 double* marAxis::getNormal( unsigned int slice ) {
00084 
00085     double* ret = new double[ 3 ];
00086 
00087     if ( slice == 0 ) {
00088       ret[0] = _points[ slice + 1 ][0] - _points[ slice ][0];
00089       ret[1] = _points[ slice + 1 ][1] - _points[ slice ][1];
00090       ret[2] = _points[ slice + 1 ][2] - _points[ slice ][2];
00091     }
00092     else if ( slice == _points.size( ) - 1 ) {
00093       ret[0] = _points[ slice - 1 ][0] - _points[ slice ][0];
00094       ret[1] = _points[ slice - 1 ][1] - _points[ slice ][1];
00095       ret[2] = _points[ slice - 1 ][2] - _points[ slice ][2];
00096     }
00097     else if ( slice > 0 && slice < _points.size( ) - 1 ) {
00098       ret[0] = _points[ slice - 1 ][0] - _points[ slice + 1 ][0];
00099       ret[1] = _points[ slice - 1 ][1] - _points[ slice + 1 ][1];
00100       ret[2] = _points[ slice - 1 ][2] - _points[ slice + 1 ][2];
00101     }
00102 
00103     return( ret );
00104 }
00105 
00106 // ----------------------------------------------------------------------------
00107 void marAxis::changeAxisResolution( )
00108 {/*
00109    int i;
00110    int nOut = ( int )ceil(
00111    getParameters( )->
00112    getDoubleParam( marParameters::e_axis_discret_step )
00113    * ( double )( GetNumberOfPoints( ) )
00114    );
00115 
00116    i = GetNumberOfPoints( );
00117    double d = getParameters( )->
00118    getDoubleParam( marParameters::e_axis_discret_step );
00119 
00120    SetResolution( nOut );
00121    for( i = 0; i < _contours.size( ); i++ )
00122    delete _contours[ i ];
00123    _contours.clear( );
00124    for( i = 0; i < nOut; i++ )
00125    _contours.push_back( new marContour( getParameters( ) ) );
00126  */
00127 }
00128 
00129 // ----------------------------------------------------------------------------
00130 void marAxis::calculateSignal( kVolume* vol )
00131 {
00135     /* PSIGNAL_FLOAT PutSignalValueFloat(PSIGNAL_FLOAT signal, int pos, float value)
00136     {
00137       signal[pos] = value;
00138       return signal;
00139     }*/
00140   ushort maxlocal;
00141   double *p;
00142 
00143   double rayon;
00144   unsigned int i, mask_size = getParameters( )-> getIntParam( marParameters::e_mask_size );
00145 
00146   while( _signal.size( ) > 0 ) _signal.pop_back( );
00147   // Intensity signal
00148 
00149   PSIGNAL_USHORT signal = ( PSIGNAL_USHORT ) IdSigAlloc( _points.size( ), SIG_USHORT );
00150   for( i = 0; i < _points.size( ); i++ ) {
00151     p = _points[i];
00152     rayon = RayonLocal(
00153                       ( int ) ( p[ 0 ] ),
00154                       ( int ) ( p[ 1 ] ),
00155                       ( int ) ( p[ 2 ] ),
00156                       _points_disc, getNumberOfControlPoints( ));
00157 
00158     maxlocal = vol->GetMaxIntSphere2( p, rayon );
00159     PutSignalValue( signal, i, maxlocal );
00160   } // rof
00161 
00162   PSIGNAL_FLOAT mask_signal = ( PSIGNAL_FLOAT )IdSigAlloc( mask_size, SIG_FLOAT );
00163 
00164   for( i = 0; i < mask_size; i++ )
00165     PutSignalValueFloat( mask_signal, i, 1 );
00166 
00167   PSIGNAL_USHORT signalfilt = ( PSIGNAL_USHORT )IdSigAlloc( _points.size( ), SIG_USHORT );
00168   signalfilt = MonSigConvolve( signal, mask_signal, signalfilt, 1.0 / ( double )mask_size, 0 );
00169   for( i = 0; i < _points.size( ); i++ ){
00170     _signal.push_back( GetSignalValue( signalfilt, i ) );
00171   }
00172   IdSigFree( signal );
00173   IdSigFree( mask_signal );
00174   IdSigFree( signalfilt );
00175 }
00176 
00177 // ----------------------------------------------------------------------------
00178 void marAxis::start( )
00179 {
00180     /* TODO
00181        int swap, n = GetNumberOfPoints( );
00182 
00183        swap = GSL_MIN( _startQuant, _finishQuant );
00184        _finishQuant = GSL_MAX( _startQuant, _finishQuant );
00185        _startQuant = swap;
00186 
00187        _startQuant = ( _startQuant >= 0 )? _startQuant: 0;
00188        _finishQuant = ( _finishQuant >= n )? _finishQuant: n - 1;
00189        _actualQuant = _startQuant;
00190     */
00191 }
00192 
00193 // ----------------------------------------------------------------------------
00194 void marAxis::cut( int slice, bool up )
00195 { // TODO
00196 }
00197 
00198 
00199 // ----------------------------------------------------------------------------
00200 void marAxis::doSpline( )
00201 {
00202   unsigned int i;
00203   unsigned int nop, nip;
00204   float length=0, t;
00205   float p1[ 3 ], p2[ 3 ];
00206   double p[marAxis::INDX_count];
00207 
00208   vtkKochanekSpline* spX = vtkKochanekSpline::New( );
00209   vtkKochanekSpline* spY = vtkKochanekSpline::New( );
00210   vtkKochanekSpline* spZ = vtkKochanekSpline::New( );
00211 
00212   for( i = 0, length = 0.0; i < _controlPoints.size( ); i++ ) {
00213     getControlPoint(i, p, p+3);
00214     p2[ 0 ] = (float) p[marAxis::INDX_X];
00215     p2[ 1 ] = (float) p[marAxis::INDX_Y];
00216     p2[ 2 ] = (float) p[marAxis::INDX_Z];
00217     spX->AddPoint( (float) i, p2[ 0 ] );
00218     spY->AddPoint( (float) i, p2[ 1 ] );
00219     spZ->AddPoint( (float) i, p2[ 2 ] );
00220     if( i > 0 )
00221       length += (float) (sqrt( (p2[0]-p1[0])*(p2[0]-p1[0]) +
00222                                (p2[1]-p1[1])*(p2[1]-p1[1]) +
00223                                (p2[2]-p1[2])*(p2[2]-p1[2]) ) );
00224     p1[ 0 ] = p2[ 0 ]; p1[ 1 ] = p2[ 1 ]; p1[ 2 ] = p2[ 2 ];
00225   } // rof
00226 
00227   nop = ( unsigned int ) ( getParameters( )->getDoubleParam(
00228                                            marParameters::e_axis_discret_step ) * length );
00229   nip = _controlPoints.size( );
00230   _healthySlice = -1;
00231 
00232   while( _points.size( ) > 0 ) {
00233     delete _points[ _points.size( ) - 1 ];
00234     _points.pop_back( );
00235   } // fwhile
00236 
00237   while( _contours.size( ) > 0 ) {
00238     delete _contours[ _contours.size( ) - 1 ];
00239     _contours.pop_back( );
00240   } // fwhile
00241 
00242   for( i = 0; i < nop; i++ ) {
00243     t = (float) (( ( double ) nip - 1.0 ) / ( ( double ) nop - 1.0 ) * i);
00244     double* np = new double[ 3 ];
00245     np[ 0 ] =  0;
00246     np[ 1 ] =  0;
00247     np[ 2 ] =  0;
00248     np[ 0 ] = (double) (spX->Evaluate( t ));
00249     np[ 1 ] = (double) (spY->Evaluate( t ));
00250     np[ 2 ] = (double) (spZ->Evaluate( t ));
00251     _points.push_back( np );
00252     _contours.push_back( new marContour( i, getParameters( ) ) );
00253 
00254   } // rof
00255 
00256   spX->Delete( );
00257   spY->Delete( );
00258   spZ->Delete( );
00259 
00260 }
00261 
00262 
00263 
00264 // ----------------------------------------------------------------------------
00265 void marAxis::sliceVolumeAxis( kVolume* vol, bool forceCnt )
00266 {
00267 /*
00268   int nCnts = ( int ) getNumberOfSplinePoints( );
00269   int sizeIma = getParameters( )->getSizeIma( );
00270   double dimIma = getParameters( )->getDimIma( );
00271   double voxSize = getParameters( )->getVoxelSize( );
00272 
00273   if( forceCnt )
00274     for( unsigned int i = 0; i < _contours.size( ); i++ )
00275       delete _contours[ i ];
00276     _contours.clear( );
00277 
00278     for( unsigned int i = 0; i < _slices.size( ); i++ ) {
00279       delete _slices[ i ];
00280       _3Dslices[ i ]->Delete( );
00281       _quantificationImages[ i ]->Delete( );
00282     }
00283 
00284   _slices.clear( );
00285   _3Dslices.clear( );
00286   _quantificationImages.clear( );
00287 
00288 
00289   // "Cutter" initialization
00290   vtkStructuredPoints* stPoints = vtkStructuredPoints::New( );
00291   // Perform "cut"
00292   double *p, *n;
00293   for( int k = 0; k < nCnts; k++ ) {
00294     //s = ( double )k / ( double )( nCnts - 1 );
00295     p = _points[k];
00296     n = getNormal( k );
00297     vtkPlaneSource* pSource = vtkPlaneSource::New( );
00298     pSource->SetOrigin( p[ 0 ], p[ 1 ], p[ 2 ] );
00299     pSource->SetPoint1( p[ 0 ] + dimIma - 1.0, p[ 1 ], p[ 2 ] );
00300     pSource->SetPoint2( p[ 0 ], p[ 1 ], p[ 2 ] + dimIma - 1.0 );
00301     pSource->SetResolution( sizeIma - 1, sizeIma - 1 );
00302     pSource->Update( );
00303     pSource->SetCenter( p[ 0 ], p[ 1 ], p[ 2 ] );
00304     pSource->SetNormal( n[ 0 ], n[ 1 ], n[ 2 ] );
00305     pSource->Update( );
00306 
00307     _3Dslices.push_back( vtkProbeFilter::New( ) );
00308     _3Dslices[ k ]->SetInput( ( vtkDataSet* )pSource->GetOutput( ) );
00309     _3Dslices[ k ]->SetSource( vol->castVtk( ) );
00310     _3Dslices[ k ]->Update( );
00311     pSource->Delete( );
00312 
00313     stPoints->GetPointData( )->
00314                 SetScalars( _3Dslices[ k ]->GetOutput( )->
00315                 GetPointData( )->GetScalars( ) );
00316     stPoints->SetDimensions( sizeIma, sizeIma, 1 );
00317     stPoints->SetScalarType( vol->castVtk( )->GetDataObjectType( ) );
00318     stPoints->Update( );
00319     if( forceCnt )
00320       {
00321                 marContour* tmp_marContour = new marContour( k, getParameters( ) ) ;
00322                 tmp_marContour->calculateVariables();
00323                 _contours.push_back( tmp_marContour );
00324         }
00325     _slices.push_back( new kVolume( kVolume::UCHAR, 1, 1, 1 ) );
00326 
00327     *( _slices[ k ] ) = ( vtkImageData* )stPoints;  //copy
00328     _quantificationImages.push_back( (_slices[ k ])->castVtk( ) );
00329   } // rof
00330 
00331   stPoints->Delete( );
00332 */
00333 }
00334 
00335 
00336 
00337 // ----------------------------------------------------------------------------
00338 int marAxis::getNumberOfContours( )
00339 {
00340     return( _contours.size( ) );
00341 }
00342 
00343 // ----------------------------------------------------------------------------
00344 int marAxis::getNumberOfSplinePoints( )
00345 {
00346     return( _points.size( ) );
00347 }
00348 
00349 /*
00350 // EED Esto toca limpiarlo o reimplementarlo en varias partes o algo
00351 // ----------------------------------------------------------------------------
00352 vtkPolyData* marAxis::setContour( int i, int x, int y,  std::vector< double* >* points, marContour* c )
00353 {
00354         if( c != NULL ) 
00355         {
00356                 c->setS( c->getS( ) );
00357                 _contours[ i ]->copyFrom( *c );
00358         } 
00359         else 
00360         {
00361                 // PS -> //    int numberOfPoints, numberOfCells; // PS
00362                 // PS -> //    int id; // PS
00363                 // PS -> //    gslobj_vector p( 3 ); // PS
00364                 // PS -> //    double new_p[3]; // PS
00365                 // PS -> //    float pf[ 3 ]; // PS
00366                 vtkImageData* imagedata;
00367                 
00368                 // Contour parameters
00369                 // PS ->     int typ = getParameters( )->getIntParam( marParameters::e_algorithm_type );
00370                 double thr = getParameters( )->getDoubleParam( marParameters::e_threshold_isocontour );
00371                 
00372                 // PS ->        double thr = ( typ == marParameters::ISOCONTOURS )?
00373                 // PS ->       getParameters( )->
00374                 // PS ->       getDoubleParam( marParameters::e_threshold_isocontour ):
00375                 // PS ->     ( typ == marParameters::SNAKE ) ?
00376                 // PS ->       getParameters( )->
00377                 // PS ->       getDoubleParam( marParameters::e_threshold_snake_isocontour ):
00378                 // PS ->     30;
00379                 
00380                 
00381                 int sizeIma = getParameters( )->getSizeIma( );
00382                 double localint = getSignal( i );
00383                 double sigma = getParameters( )->getDoubleParam( marParameters::e_sigma );
00384                 double threshold = localint * thr / 100.0;
00385                 int dimIma    = ( int ) ( ( double ) sizeIma /getParameters( )->getDoubleParam( marParameters::e_scale ) );
00386                 int vmin = ( int )threshold;
00387                 int vmax = ( int )threshold;
00388                 
00389                 x = ( x != -1 )? x: sizeIma / 2;
00390                 y = ( y != -1 )? y: sizeIma / 2;
00391                 
00392                 // Isocontour
00393                 imagedata = (vtkImageData*) ((getSlice( i , NULL ))->castVtk( ));
00394                 float *range = imagedata->GetScalarRange();
00395                 
00396                 //vtkKitwareContourFilter* cntVTK = vtkKitwareContourFilter::New( );
00397                 vtkContourFilter* cntVTK = vtkContourFilter::New( );
00398                 cntVTK->SetInput( imagedata );
00399                 cntVTK->SetNumberOfContours( 1 );
00400                 //cntVTK->SetValue( 0, vmin );
00401                 cntVTK->SetValue( 0, (range[1]*thr/100) );
00402                 //cntVTK->SetValue( 1, vmax );
00403                 cntVTK->Update( );
00404                 
00405                 vtkCleanPolyData* cpd = vtkCleanPolyData::New( );
00406                 cpd->SetInput( cntVTK->GetOutput( ) );
00407                 cpd->ConvertLinesToPointsOff( );
00408                 cpd->Update( );
00409                 
00410                 vtkPolyDataConnectivityFilter* conn = vtkPolyDataConnectivityFilter::New( );
00411                 //conn->SetMaxRecursionDepth( 3000 );
00412                 
00413                 // PS ->     //conn->SetInput( cntVTK->GetOutput( ) ); PS
00414                 conn->SetInput( cpd->GetOutput( ) );
00415                 
00416                 conn->SetClosestPoint( x, y, 0 );
00417                 conn->SetExtractionModeToClosestPointRegion( );
00418                 conn->Update( );
00419                 
00420                 vtkCleanPolyData* cpd2 = vtkCleanPolyData::New( );
00421                 cpd2->SetInput( conn->GetOutput( ) );
00422                 cpd2->Update();
00423                 
00424                 vtkPolyData* polyDataResult = cpd2->GetOutput() ;
00425                 
00426                 double ta = _contours[ i ]->getS( );
00427                 
00428                 _contours[ i ]->reset( );
00429                 
00430                 // PS ->        gslobj_vector o( 3 ), n( 3 ), y( 3 ), vr( 3 );
00431                 
00432                 int id;
00433                 int numberOfCells;
00434                 int numberOfPoints,numberOfPoints2;
00435                 vtkCell* cell;
00436                 vtkIdList* pids;
00437                 marVector p( 3 );
00438 // PS ->                double new_p[3];
00439                 float pf[ 3 ];
00440 
00441                 numberOfCells = polyDataResult->GetNumberOfCells( );
00442         cell = polyDataResult->GetCell( 0 );
00443                 pids = cell->GetPointIds( );
00444                 numberOfPoints = pids->GetNumberOfIds( );
00445 
00446                 numberOfPoints2=polyDataResult->GetNumberOfPoints();
00447                 
00448                 marVector o(3),n(3),y(3),vr(3);
00449                 double cost, sint;
00450                 y( 0 ) = y( 2 ) = 0.0;
00451                 y( 1 ) = 1.0;
00452                 getPoint( ( double* )o, ta );
00453                 getTangent( ( double* )n, ta );
00454                 vr = y.cross( n ).normalize( );
00455                 sint = y.cross( n ).norm2( );
00456                 cost = y.dot( n );
00457         
00458                 for( int j = 0; j < numberOfPoints; j++ ) 
00459                 {
00460                         id = pids->GetId( j );
00461                         polyDataResult->GetPoint( id, pf );
00462                         p( 0 ) = pf[ 0 ];
00463                         p( 1 ) = 0.0;
00464                         p( 2 ) = pf[ 1 ];
00465                         
00466                         // 3D transformation
00467                         p = ( p * cost ) + ( vr * ( vr.dot( p ) * ( 1.0 - cost ) ) ) -
00468                                 ( p.cross( vr ) * sint ) + o;
00469                         
00470                 // PS ->                double p_cross_vr[3];
00471                 // PS ->                vtkMath::Cross( p, vr, p_cross_vr);
00472                 // PS ->                for(int k=0; k<3; k++) 
00473                 // PS ->                {
00474                 // PS ->                        new_p[i] = (new_p[k] * cost) + ( vr(k) * ( vtkMath::Dot(vr, p) * ( 1.0 - cost ) ) ) -
00475                 // PS ->                                ( p_cross_vr[k] * sint ) + o(i);
00476                 // PS ->                }
00477                         
00478                         //TODO: check if p and new_p are the same...
00479                         _contours[ i ]->addContourPoint( p );
00480                 } // rof
00481                 
00482                 // PS ->        int nbPoints=polyDataResult->GetNumberOfPoints();
00483                 // PS ->        
00484                 // PS ->        float* vtkContourPoint;
00485                 // PS ->        vtkContourPoint= new float[3];
00486                 // PS ->        double* marContourPoint;
00487                 // PS ->        marContourPoint= new double[3];
00488                 // PS -> 
00489                 // PS ->        for (int j=0;j< nbPoints;j++)
00490                 // PS ->        {
00491                 // PS ->                vtkContourPoint=polyDataResult->GetPoint(j);
00492                 // PS ->                marContourPoint[0]=(double)vtkContourPoint[0];
00493                 // PS ->                marContourPoint[1]=(double)vtkContourPoint[1];
00494                 // PS ->                marContourPoint[2]=(double)vtkContourPoint[2];
00495                 // PS ->                _contours[ i ]->addContourPoint( marContourPoint );
00496                 // PS ->        }
00497                 marContour * tutu=_contours[ i ];
00498                 _contours[ i ]->getArea();
00499                 _contours[ i ]->getPerimeter();
00500                 return( polyDataResult );
00501                 //return( cpd->GetOutput( ) );
00502                 //return( cntVTK->GetOutput( ) );
00503                 
00504                  
00505 //EED1             vtkStripper* isoStrips = vtkStripper::New( );
00506 //EED1             isoStrips->SetInput( cpd2->GetOutput( ) );
00507 //EED1             isoStrips->Update( );
00508 //EED1             
00509 //EED1                   vtkPolyData* data = isoStrips->GetOutput( );
00510 //EED1                   data->Update( );
00511 //EED1                   vtkCell* cell;
00512 //EED1                   vtkIdList* pids;
00513 //EED1                   
00514 //EED1                     numberOfCells = data->GetNumberOfCells( );
00515 //EED1                     cell = data->GetCell( 0 );
00516 //EED1                     pids = cell->GetPointIds( );
00517 //EED1                     numberOfPoints = pids->GetNumberOfIds( );
00518 
00519                 
00520 //EED2          _contours[ i ]->reset( );
00521 //EED2          if( typ == marParameters::ISOCONTOURS ) {
00522 //EED2          gslobj_vector o( 3 ), n( 3 ), y( 3 ), vr( 3 );
00523 //EED2          double cost, sint;
00524 //EED2          y( 0 ) = y( 2 ) = 0.0;
00525 //EED2          y( 1 ) = 1.0;
00526 //EED2          getPoint( ( double* )o, ta );
00527 //EED2          getTangent( ( double* )n, ta );
00528 //EED2          vr = y.cross( n ).normalize( );
00529 //EED2          sint = y.cross( n ).norm2( );
00530 //EED2          cost = y.dot( n );
00531 //EED2          
00532 //EED2            for( int j = 0; j < numberOfPoints; j++ ) {
00533 //EED2            id = pids->GetId( j );
00534 //EED2            data->GetPoint( id, pf );
00535 //EED2            p( 0 ) = pf[ 0 ];
00536 //EED2            p( 1 ) = 0.0;
00537 //EED2            p( 2 ) = pf[ 1 ];
00538 //EED2            
00539 //EED2                  // 3D transformation
00540 //EED2                  p = ( p * cost ) + ( vr * ( vr.dot( p ) * ( 1.0 - cost ) ) ) -
00541 //EED2          ( p.cross( vr ) * sint ) + o;
00542 //EED2                  
00543 //EED2                    double p_cross_vr[3];
00544 //EED2                    vtkMath::Cross( p, vr, p_cross_vr);
00545 //EED2                    for(int ii=0; ii<3; ii++) {
00546 //EED2                    new_p[i] = (new_p[ii] * cost) + ( vr(ii) * ( vtkMath::Dot(vr, p) * ( 1.0 - cost ) ) ) -
00547 //EED2                    ( p_cross_vr[ii] * sint ) + o(i);
00548 //EED2                    }
00549 //EED2                    
00550 //EED2                          //TODO: check if p and new_p are the same...
00551 //EED2                          _contours[ i ]->addContourPoint( p );
00552 //EED2                          } // rof
00553 //EED2                          } else if( typ == marParameters::SNAKE || typ == marParameters::DERICHE ) {
00554 //EED2                          // ONLY Isocontours at the moment.
00555 //EED2                          } // fi
00556 //EED2                          
00557 //EED2                            cntVTK->Delete( );
00558 //EED2                            cpd->Delete( );
00559 //EED2                            conn->Delete( );
00560 //EED2                            cpd2->Delete( );
00561 //EED2          isoStrips->Delete( );
00562 
00563   } // fi
00564 }
00565 
00566 */
00567 
00568 
00569 
00570 
00571 // ----------------------------------------------------------------------------
00572 void marAxis::createContour(int i, kVolume* vol)
00573 {
00574 
00575         int j,id;
00576         int numberOfPoints,numberOfCells;
00577         vtkCell* cell;
00578         vtkIdList* pids;
00579         double p[ 3 ];
00580         vtkPolyData* vtkPolyData_2DContour;
00581         double x,y;
00582 
00583         int sizeIma                             =       getParameters( )->getSizeIma( );
00584         double dimIma                   =       getParameters( )->getDimIma( );
00585 
00586 // EED 24 Dec 2007
00587   dimIma=sizeIma/3;
00588 
00589         _contours[i]                    =       new marContour( i, getParameters( ) ) ;
00590         vtkPolyData_2DContour   =       get2Dcontour(i,vol);
00591 
00592         numberOfCells                   =       vtkPolyData_2DContour->GetNumberOfCells( );
00593     cell                                        =       vtkPolyData_2DContour->GetCell( 0 );
00594         pids                                    =       cell->GetPointIds( );
00595         numberOfPoints                  =       pids->GetNumberOfIds( );
00596 
00597         for( j = 0; j < numberOfPoints; j++ ) 
00598         {
00599                 id = pids->GetId( j );
00600                 vtkPolyData_2DContour->GetPoint( id, p);
00601                 x=p[0]-64.0;
00602                 y=p[1]-64.0;
00603                 x=x * dimIma / ( float ) sizeIma;
00604                 y=y * dimIma / ( float ) sizeIma;
00605                 _contours[ i ]->addContourPoint( x , y );
00606         }
00607 
00608         _contours[i]->do_spline();
00609         _contours[i]->calculateVariables();
00610 
00611 }
00612 
00613 // ----------------------------------------------------------------------------
00614 void marAxis::EraseContour(int i){
00615         if (_3Dcontour[i]!=NULL){
00616                 _3Dcontour[i]->Delete();
00617                 _3Dcontour[i]=NULL;
00618         }
00619 
00620         if (_2DDiameterMax[i]!=NULL){
00621                 _2DDiameterMax[i]->Delete();
00622                 _2DDiameterMax[i]=NULL;
00623         }
00624 
00625         if (_2DDiameterMin[i]!=NULL){
00626                 _2DDiameterMin[i]->Delete();
00627                 _2DDiameterMin[i]=NULL;
00628         }
00629 
00630         if (_2Dcontours[i]!=NULL){
00631                 _2Dcontours[i]->Delete();
00632                 _2Dcontours[i]=NULL;
00633         }
00634 
00635         if (_contours[i]!=NULL){
00636                 delete _contours[i];
00637                 _contours[i]=NULL;
00638         }
00639 }
00640 
00641 // ----------------------------------------------------------------------------
00642 void marAxis::replaceContour2D(int i,int size,double *vx,double *vy){
00643 
00644         EraseContour(i);
00645 
00646 
00647         vtkPoints *_pts = vtkPoints::New();
00648         _pts->SetNumberOfPoints(size);
00649         int j;
00650 
00651         for (j=0 ; j<size ; j++){
00652                 _pts->SetPoint(j,       vx[j]   , vy[j] , 0 );
00653         }
00654 //      _pts->SetPoint(0,       vx[0]   , vy[0] , 0 );
00655 
00656         vtkCellArray *lines = vtkCellArray::New();
00657         lines->InsertNextCell( size );
00658         for ( j=0 ; j<size+1 ; j++ ){
00659                 lines->InsertCellPoint(j % size );
00660         }
00661 
00662         vtkPolyData *_pd = vtkPolyData::New();
00663         _pd->SetPoints( _pts );
00664         _pd->SetLines( lines );
00665         lines->Delete();  //do not delete lines ??
00666         _pts->Delete();  
00667 
00668         _2Dcontours[i]=_pd;
00669         createContour(i,NULL);
00670 }
00671 // ----------------------------------------------------------------------------
00672 void marAxis::createSlice(int i , kVolume* vol){
00673         int k=i;
00674         int sizeIma = getParameters( )->getSizeIma( );
00675 
00676         vtkStructuredPoints* stPoints = vtkStructuredPoints::New( );
00677         double *p, *n;
00678     p = _points[k];
00679     n = getNormal( k );
00680 
00681     stPoints->GetPointData( )->SetScalars(  get3DSlice(k,vol)->GetOutput()->GetPointData()->GetScalars()  );
00682     stPoints->SetDimensions( sizeIma, sizeIma, 1 );
00683 
00684 
00685     stPoints->SetScalarType( vol->castVtk( )->GetDataObjectType( ) );
00686         stPoints->SetScalarTypeToShort ();
00687     stPoints->Update();
00688         
00689     vtkImageChangeInformation* change = vtkImageChangeInformation::New();
00690     change->SetInput( stPoints );  
00691     change->Update();    //important
00692 
00693 
00694     if (_slices[k]!=NULL) { delete _slices[k]; }
00695     _slices[k] = new kVolume( change->GetOutput() ) ;
00696 
00697         stPoints->Delete( );
00698         change->Delete();
00699 }
00700 // ----------------------------------------------------------------------------
00701 void marAxis::create3DSlice(int i , kVolume* vol )
00702 {
00703 
00704   int k=i;
00705   int sizeIma   = getParameters( )->getSizeIma( );
00706   double dimIma = getParameters( )->getDimIma( );
00707 
00708   dimIma=sizeIma/3;
00709 
00710 
00711 //  double voxSize = getParameters( )->getVoxelSize( );
00712 
00713 //  if( forceCnt )
00714 //    for( unsigned int i = 0; i < _contours.size( ); i++ )
00715 //      delete _contours[ i ];
00716 //    _contours.clear( );
00717 
00718 //    for( unsigned int i = 0; i < _slices.size( ); i++ ) {
00719 //      delete _slices[ i ];
00720 //      _3Dslices[ i ]->Delete( );
00721 //      _quantificationImages[ i ]->Delete( );
00722 //    }
00723 
00724 //  _slices.clear( );
00725 // _3Dslices.clear( );
00726 //  _quantificationImages.clear( );
00727 
00728   // "Cutter" initialization
00729 //  vtkStructuredPoints* stPoints = vtkStructuredPoints::New( );
00730 
00731   // Perform "cut"
00732   double *p, *n;
00733 //  for( int k = 0; k < nCnts; k++ ) {
00734     //s = ( double )k / ( double )( nCnts - 1 );
00735     p = _points[k];
00736     n = getNormal( k );
00737 
00738     vtkPlaneSource* pSource = vtkPlaneSource::New( );
00739     pSource->SetOrigin( p[ 0 ], p[ 1 ], p[ 2 ] );
00740     pSource->SetPoint1( p[ 0 ] + dimIma - 1.0, p[ 1 ], p[ 2 ] );
00741     pSource->SetPoint2( p[ 0 ], p[ 1 ], p[ 2 ] + dimIma - 1.0 );
00742     pSource->SetResolution( sizeIma - 1, sizeIma - 1 );
00743     pSource->Update( );
00744     pSource->SetCenter( p[ 0 ], p[ 1 ], p[ 2 ] );
00745     pSource->SetNormal( n[ 0 ], n[ 1 ], n[ 2 ] );
00746     pSource->Update( );
00747 
00748         if (_3Dslices[ k ]!=NULL){ _3Dslices[ k ]->Delete(); }
00749     _3Dslices[ k ] = vtkProbeFilter::New( ) ;
00750     _3Dslices[ k ]->SetInput( ( vtkDataSet* )pSource->GetOutput( ) );
00751     _3Dslices[ k ]->SetSource( vol->castVtk( ) );
00752     _3Dslices[ k ]->Update( );
00753     pSource->Delete( );
00754 
00755 //    stPoints->GetPointData( )->
00756 //              SetScalars( _3Dslices[ k ]->GetOutput( )->
00757 //              GetPointData( )->GetScalars( ) );
00758 //    stPoints->SetDimensions( sizeIma, sizeIma, 1 );
00759 //    stPoints->SetScalarType( vol->castVtk( )->GetDataObjectType( ) );
00760 //    stPoints->Update( );
00761 //
00762 //    if( forceCnt )
00763 //      {
00764 //              marContour* tmp_marContour = new marContour( k, getParameters( ) ) ;
00765 //              tmp_marContour->calculateVariables();
00766 //              _contours.push_back( tmp_marContour );
00767 //      }
00768 //    _slices.push_back( new kVolume( kVolume::UCHAR, 1, 1, 1 ) );
00769 //    *( _slices[ k ] ) = ( vtkImageData* )stPoints;  //copy
00770 //    _quantificationImages.push_back( (_slices[ k ])->castVtk( ) );
00771 //  } // rof
00772 
00773 //  stPoints->Delete( );
00774 
00775 //      _3Dslices[i]=NULL;
00776 }
00777 
00778 
00779 
00780 
00781 // ----------------------------------------------------------------------------
00782 void marAxis::create3Dcontour(int i, kVolume* vol)
00783 {       
00784         vtkPoints *_vtkPoints;
00785         double *o;
00786         double *n;
00787         vtkMatrix4x4* mat;
00788         vtkTransform* trans;
00789         double nn[3];
00790         double c[3],p1[3],p2[3];
00791         double nA,nB,nC;
00792 
00793         _vtkPoints                                              = vtkPoints::New();
00794         o                                                               = _points[i];
00795         n                                                               = getNormal( i );
00796         mat                                                             = vtkMatrix4x4::New();
00797         trans                                                   = vtkTransform::New();
00798 
00799 
00800         nn[0]=n[0];  nn[1]=n[1];  nn[2]=n[2];
00801         nC=sqrt( nn[0]*nn[0] + nn[1]*nn[1] + nn[2]*nn[2] );
00802         nn[0]=nn[0]/nC;  nn[1]=nn[1]/nC;  nn[2]=nn[2]/nC;
00803 
00804     vtkPlaneSource* pSource = vtkPlaneSource::New( );
00805     pSource->SetOrigin( 0, 0    , 0                             );
00806     pSource->SetPoint1( 1, 0    , 0                             );
00807     pSource->SetPoint2( 0, 0    , 1.0   );
00808     pSource->SetCenter( 0,0,0 );
00809     pSource->SetNormal( nn[ 0 ], nn[ 1 ], nn[ 2 ] );
00810     pSource->Update( );
00811 //    pSource->Update( );
00812         pSource->GetOrigin(c);
00813         pSource->GetPoint1(p1);
00814         pSource->GetPoint2(p2);
00815         pSource->Delete();
00816         p1[0]=p1[0]-c[0];  p1[1]=p1[1]-c[1];  p1[2]=p1[2]-c[2];
00817         p2[0]=p2[0]-c[0];  p2[1]=p2[1]-c[1];  p2[2]=p2[2]-c[2];
00818         nA=sqrt( p1[0]*p1[0] + p1[1]*p1[1] + p1[2]*p1[2] );
00819         nB=sqrt( p2[0]*p2[0] + p2[1]*p2[1] + p2[2]*p2[2] );
00820         p1[0]=p1[0]/nA;  p1[1]=p1[1]/nA;  p1[2]=p1[2]/nA;
00821         p2[0]=p2[0]/nB;  p2[1]=p2[1]/nB;  p2[2]=p2[2]/nB;
00822 
00823     mat->SetElement (0,0, nn[0]);
00824     mat->SetElement (1,0, nn[1]);
00825     mat->SetElement (2,0, nn[2]);
00826     mat->SetElement (3,0, 0);
00827     mat->SetElement (0,1, p2[0]);
00828     mat->SetElement (1,1, p2[1]);
00829     mat->SetElement (2,1, p2[2]);
00830     mat->SetElement (3,1, 0);
00831         mat->SetElement (0,2, p1[0]);
00832     mat->SetElement (1,2, p1[1]);
00833     mat->SetElement (2,2, p1[2]);
00834     mat->SetElement (3,2, 0);
00835         mat->SetElement (0,3, 0);
00836     mat->SetElement (1,3, 0);
00837     mat->SetElement (2,3, 0);
00838     mat->SetElement (3,3, 1);
00839 
00840 //      double deter=mat->Determinant(mat);
00841         trans->Identity();
00842         trans->SetMatrix(mat);
00843         float ang;
00844         ang=-90;
00845         trans->RotateWXYZ  ( ang,0,1,0); 
00846 
00847 // EED Borrame
00848 //      double scale = 1; 
00849 //      trans->Scale  ( scale , scale, scale); 
00850 
00851         trans->Update();
00852 
00853         vtkMatrix4x4 *m=vtkMatrix4x4::New();
00854         trans->GetMatrix  (  m  );
00855     
00856 
00857         int j,numberOfPoints;
00858         marContour* contour2D   = getContour(i,vol);
00859         numberOfPoints                  = contour2D->getNumberOfPoints( );
00860         double fpA[3];
00861         double pA[3],ppp[3];
00862 
00863 
00864         for( j = 0; j <= numberOfPoints; j++ ) {
00865                 contour2D->getPoint( fpA,j % numberOfPoints);
00866 
00867                 pA[0] = fpA[0] + 0.2;  // correction EED 
00868                 pA[1] = fpA[1] + 0.2;  // correction EED
00869 
00870                 pA[2] = 0;
00871 
00872 // Why this does not works...(release version)????
00873 //              trans->TransformPoint(pA,ppp); 
00874 // or
00875 //              trans->TransformVector(pA,ppp);
00876 // so..
00877                 ppp[0] = m->GetElement(0,0)*pA[0] + m->GetElement(0,1)*pA[1] + m->GetElement(0,2)*pA[2] + m->GetElement(0,3);
00878                 ppp[1] = m->GetElement(1,0)*pA[0] + m->GetElement(1,1)*pA[1] + m->GetElement(1,2)*pA[2] + m->GetElement(1,3);
00879                 ppp[2] = m->GetElement(2,0)*pA[0] + m->GetElement(2,1)*pA[1] + m->GetElement(2,2)*pA[2] + m->GetElement(2,3);
00880 
00881 //              ppp[0]=ppp[0]+o[0]-c[0]; ppp[1]=ppp[1]+o[1]-c[1]; ppp[2]=ppp[2]+o[2]-c[2];  
00882                 ppp[0]=ppp[0]+o[0]; ppp[1]=ppp[1]+o[1]; ppp[2]=ppp[2]+o[2];  
00883 //              ppp[0]=ppp[0]+o[0]-c[0]*0.781; ppp[1]=ppp[1]+o[1]-c[1]*0.781;; ppp[2]=ppp[2]+o[2]-c[2]*0.781;;  
00884                 _vtkPoints->InsertNextPoint( (float)ppp[0],(float)ppp[1],(float)ppp[2] );
00885         }
00886 
00887         m->Delete();
00888         mat->Delete();
00889         trans->Delete();
00890         if (_3Dcontour[i]!=NULL){ _3Dcontour[i]->Delete(); }
00891         _3Dcontour[i]=_vtkPoints;       
00892 
00893 }
00894 // ----------------------------------------------------------------------------
00895 void marAxis::createSliceImage(int i  , kVolume* vol )
00896 {
00897   _quantificationImages[i] = getSlice(i,vol)->castVtk( ) ;
00898 
00899 }
00900 // ----------------------------------------------------------------------------
00901 void marAxis::create2Dcontour(int i , kVolume* vol )
00902 {
00903         vtkImageData* imagedata;
00904                 
00905         double  thr                     = getParameters( )->getDoubleParam( marParameters::e_threshold_isocontour );
00906         int             sizeIma         = getParameters( )->getSizeIma( );
00907         double  localint        = getSignal( i );
00908         double  sigma           = getParameters( )->getDoubleParam( marParameters::e_sigma );
00909         double  threshold       = localint * thr / 100.0;
00910 
00911         int             dimIma          = ( int ) ( ( double ) sizeIma /getParameters( )->getDoubleParam( marParameters::e_scale ) );
00912         int             vmin            = ( int )threshold;
00913         int             vmax            = ( int )threshold;
00914 
00915         int             x                       = -1;
00916         int             y                       = -1;
00917 
00918         x = ( x != -1 )? x: sizeIma / 2;
00919         y = ( y != -1 )? y: sizeIma / 2;
00920         
00921         // Isocontour
00922         imagedata = (vtkImageData*) ((getSlice( i , vol ))->castVtk( ));
00923         double *range = imagedata->GetScalarRange();    
00924 
00925         //vtkKitwareContourFilter* cntVTK = vtkKitwareContourFilter::New( );
00926         vtkContourFilter* cntVTK = vtkContourFilter::New( );
00927         cntVTK->SetInput( imagedata );
00928         cntVTK->SetNumberOfContours( 1 );
00929         //cntVTK->SetValue( 0, vmin );
00930         cntVTK->SetValue( 0, (range[1]*thr/100) );
00931         //cntVTK->SetValue( 1, vmax );
00932         cntVTK->Update( );
00933                 
00934         vtkCleanPolyData* cpd = vtkCleanPolyData::New( );
00935         cpd->SetInput( cntVTK->GetOutput( ) );
00936         cpd->ConvertLinesToPointsOff( );
00937         cpd->Update( );
00938                 
00939         vtkPolyDataConnectivityFilter* conn = vtkPolyDataConnectivityFilter::New( );
00940         //conn->SetMaxRecursionDepth( 3000 );
00941                 
00942         // PS ->     //conn->SetInput( cntVTK->GetOutput( ) ); PS
00943         conn->SetInput( cpd->GetOutput( ) );
00944                 
00945         conn->SetClosestPoint( x, y, 0 );
00946         conn->SetExtractionModeToClosestPointRegion( );
00947         conn->Update( );
00948                 
00949         vtkCleanPolyData* cpd2 = vtkCleanPolyData::New( );
00950         cpd2->SetInput( conn->GetOutput( ) );
00951         cpd2->Update();
00952 
00953         vtkStripper* vtkstripper = vtkStripper::New( );
00954         vtkstripper->SetInput( cpd2->GetOutput() );
00955         vtkstripper->Update();
00956 
00957         vtkPolyData* polyDataResult =  vtkstripper->GetOutput();
00958     polyDataResult->Update( );
00959         
00960         cntVTK          -> Delete();
00961         cpd2            -> Delete();
00962         cpd                     -> Delete();
00963         conn            -> Delete();
00964 
00965 /*
00966 // EED 27 Oct 2007
00967         double *p;
00968         int ii,size=polyDataResult->GetNumberOfPoints();
00969 printf("EED marAxis::create2Dcontour size %d\n",size);
00970         for (ii=0;ii<size;ii++)
00971         {
00972                 p=polyDataResult->GetPoint(ii);
00973 printf("EED marAxis::create2Dcontour %d >> %f %f %f\n",ii,p[0], p[1], p[2]);
00974                 p[2]=-100;      
00975         }
00976 
00977         for (ii=0;ii<size;ii++)
00978         {
00979                 p=polyDataResult->GetPoint(ii);
00980 printf("EED marAxis::create2Dcontour xxxxx %d >> %f %f %f\n",ii,p[0], p[1], p[2]);
00981         }
00982 */
00983 
00984         if ( _2Dcontours[i]!=NULL ) { _2Dcontours[i]->Delete(); }
00985         _2Dcontours[i]=polyDataResult;
00986 }
00987 // ----------------------------------------------------------------------------
00988 void marAxis::create2DDiameterMin(      int i , kVolume* vol ){
00989         double p1[2],p2[2];
00990         marContour *marcontour=getContour(i,vol);
00991         marcontour->getMinimumLine(p1,p2);
00992         vtkPoints *_vtkPoints = vtkPoints::New();
00993         int sizeIma = getParameters( )->getSizeIma( );
00994 
00995         double dimIma = getParameters( )->getDimIma( );
00996 
00997 // EED 24 Dec 2007
00998   dimIma=sizeIma/3;
00999 
01000         double factor=( float ) sizeIma / dimIma;
01001         p1[0]=p1[0]*factor+sizeIma/2;
01002         p1[1]=p1[1]*factor+sizeIma/2;
01003         p2[0]=p2[0]*factor+sizeIma/2;
01004         p2[1]=p2[1]*factor+sizeIma/2;
01005         _vtkPoints->InsertNextPoint( p1[0] , p1[1] , 1 );
01006         _vtkPoints->InsertNextPoint( p2[0] , p2[1] , 1 );
01007         if ( _2DDiameterMin[i]!=NULL ) { _2DDiameterMin[i]->Delete(); }
01008         _2DDiameterMin[i] = _vtkPoints;
01009 }
01010 // ----------------------------------------------------------------------------
01011 void marAxis::create2DDiameterMax(      int i , kVolume* vol ){
01012         double p1[2],p2[2];
01013         marContour *marcontour=getContour(i,vol);
01014         marcontour->getMaximumLine(p1,p2);
01015         vtkPoints *_vtkPoints = vtkPoints::New();
01016         int sizeIma = getParameters( )->getSizeIma( );
01017 
01018         double dimIma = getParameters( )->getDimIma( );
01019 // EED 24 Dec 2007
01020 dimIma=sizeIma/3;
01021 
01022         double factor=( float ) sizeIma / dimIma;
01023         p1[0]=p1[0]*factor+sizeIma/2;
01024         p1[1]=p1[1]*factor+sizeIma/2;
01025         p2[0]=p2[0]*factor+sizeIma/2;
01026         p2[1]=p2[1]*factor+sizeIma/2;
01027         _vtkPoints->InsertNextPoint( p1[0] , p1[1] , 1 );
01028         _vtkPoints->InsertNextPoint( p2[0] , p2[1] , 1 );
01029         if ( _2DDiameterMax[i]!=NULL ) { _2DDiameterMax[i]->Delete(); }
01030         _2DDiameterMax[i] = _vtkPoints;
01031 }
01032 // ----------------------------------------------------------------------------
01033 void marAxis::createEmptyVectors()
01034 {
01035         int nCnts = ( int ) getNumberOfSplinePoints( );
01036         clearAllVectors();
01037         int i;
01038 //      int xxxx=getNumberOfContours();
01039         for (i=0;i<nCnts;i++){
01040                 _contours.push_back( NULL );
01041                 _slices.push_back( NULL );
01042                 _3Dslices.push_back( NULL );
01043                 _3Dcontour.push_back( NULL );
01044                 _quantificationImages.push_back( NULL );
01045                 _2Dcontours.push_back( NULL );
01046                 _2DDiameterMin.push_back( NULL );
01047                 _2DDiameterMax.push_back( NULL );
01048         }
01049 }
01050 // ----------------------------------------------------------------------------
01051 void marAxis::clearAllVectors()
01052 {
01053         unsigned int i;
01054         
01055         for (i=0;i<_contours.size();i++){
01056                 if (_contours[i]!=NULL) {                                       //  DATA-MODEL-2D
01057                         delete _contours[i];
01058                         _contours[i]=NULL;
01059                 }
01060         }
01061         _contours.clear();
01062 
01063         for (i=0;i<_slices.size();i++){                                 //  DATA-MODEL-Voxel XxYx1
01064                 if (_slices[i]!=NULL) {                         
01065                         delete _slices[i];
01066                         _slices[i]=NULL;
01067                 }
01068         }
01069         _slices.clear();
01070 
01071 
01072         for (i=0;i<_3Dslices.size();i++){                                       //  VISUALISATION_VTK 3D
01073                 if (_3Dslices[i]!=NULL) {
01074                         _3Dslices[i]->Delete();
01075                         _3Dslices[i]=NULL;
01076                 }
01077         }
01078         _3Dslices.clear();
01079 
01080         for (i=0;i<_3Dcontour.size();i++){                              //  VISUALISATION_VTK 3D
01081                 if (_3Dcontour[i]!=NULL) {                              
01082                         _3Dcontour[i]->Delete();
01083                         _3Dcontour[i]=NULL;
01084                 }
01085         }
01086         _3Dcontour.clear();
01087 
01088         for (i=0;i<_quantificationImages.size();i++){   //  VISUALISATION_VTK 2D
01089                 if (_quantificationImages[i]!=NULL) {                           
01090 //                      _quantificationImages[i]->Delete();
01091 //                      _quantificationImages[i]=NULL;
01092                 }
01093         }
01094         _quantificationImages.clear();
01095 
01096         for (i=0;i<_2Dcontours.size();i++){                             //  VISUALISATION_VTK 2D
01097                 if (_2Dcontours[i]!=NULL) {                             
01098 // EED ???  This object was allready erased bye the VTK pipeline
01099 //                      _2Dcontours[i]->Delete();
01100                         _2Dcontours[i]=NULL;
01101                 }
01102         }
01103         _2Dcontours.clear();
01104 
01105         for (i=0;i<_2DDiameterMin.size();i++){                  //  VISUALISATION_VTK 2D
01106                 if (_2DDiameterMin[i]!=NULL) {                          
01107                         _2DDiameterMin[i]->Delete() ;
01108                         _2DDiameterMin[i]=NULL;
01109                 }
01110         }
01111         _2DDiameterMin.clear();
01112 
01113         for (i=0;i<_2DDiameterMax.size();i++){                  //  VISUALISATION_VTK 2D
01114                 if (_2DDiameterMax[i]!=NULL) {                          
01115                         _2DDiameterMax[i]->Delete();
01116                         _2DDiameterMax[i]=NULL;
01117                 }
01118         }
01119         _2DDiameterMax.clear();
01120 
01121 }
01122 // ----------------------------------------------------------------------------
01123 void marAxis::eraseContourVectorsContent()
01124 {
01125         unsigned int i;
01126         for (i=0;i<_contours.size();i++){
01127                 if (_contours[i]!=NULL) {                                       //  DATA-MODEL-2D
01128                         delete _contours[i];
01129                         _contours[i]=NULL;
01130                 }
01131         }
01132         for (i=0;i<_2Dcontours.size();i++){                             //  VISUALISATION_VTK 2D
01133                 if (_2Dcontours[i]!=NULL) {                             
01134                         _2Dcontours[i]->Delete();
01135                         _2Dcontours[i]=NULL;
01136                 }
01137         }
01138         for (i=0;i<_2DDiameterMin.size();i++){                          //  VISUALISATION_VTK 2D
01139                 if (_2DDiameterMin[i]!=NULL) {                          
01140                         _2DDiameterMin[i]->Delete();
01141                         _2DDiameterMin[i]=NULL;
01142                 }
01143         }
01144         for (i=0;i<_2DDiameterMax.size();i++){                          //  VISUALISATION_VTK 2D
01145                 if (_2DDiameterMax[i]!=NULL) {                          
01146                         _2DDiameterMax[i]->Delete();
01147                         _2DDiameterMax[i]=NULL;
01148                 }
01149         }
01150         for (i=0;i<_3Dcontour.size();i++){                              //  VISUALISATION_VTK 3D
01151                 if (_3Dcontour[i]!=NULL) {                              
01152                         _3Dcontour[i]->Delete();
01153                         _3Dcontour[i]=NULL;
01154                 }
01155         }
01156 }
01157 
01158 // ----------------------------------------------------------------------------
01159 marContour* marAxis::getContour(int i, kVolume* vol)                            // DATA-MODEL 2D
01160 {                                                               
01161         if (_contours[i]==NULL){ createContour(i, vol); }
01162         return _contours[i];
01163 } 
01164 // ----------------------------------------------------------------------------
01165 kVolume* marAxis::getSlice(int i, kVolume* vol)                                         // DATA-MODEL-Voxel XxYx1
01166 {
01167         if (_slices[i]==NULL){ createSlice( i , vol ); }
01168         return _slices[i];
01169 }
01170 // ----------------------------------------------------------------------------
01171 
01172 bool marAxis::if3DcontourExist(int i)
01173 {
01174         bool result=true;
01175         if (_3Dcontour[i]==NULL)
01176         {
01177                 result=false;
01178         } 
01179         return result;
01180 }
01181 
01182 // ----------------------------------------------------------------------------
01183 void marAxis::Save3Dcontour(FILE *ff,int id)
01184 {
01185         int i,size;
01186         double point[3];
01187         if (_3Dcontour[id]!=NULL)
01188         {
01189 //Ramiro                fprintf(ff,"contour_id: %d \n", id);
01190                 size = _3Dcontour[id]->GetNumberOfPoints();
01191 //Ramiro                fprintf(ff,"numberOfPoints: %d \n", size);
01192             for ( i=0 ; i<size  ;i++ ){
01193                 _3Dcontour[id]->GetPoint( i , point );
01194                         fprintf(ff,"%f %f %f %d\n", point[0], point[1],point[2],id);
01195             }
01196         }
01197 }
01198 // ----------------------------------------------------------------------------
01199 void marAxis::SaveExisting3DContours(FILE *ff)
01200 {
01201         if (ff!=NULL)
01202         {
01203                 // counting existing 3Dcontours
01204                 int acum=0;
01205                 int i,size=_3Dcontour.size();
01206                 for (i=0; i<size; i++)
01207                 {
01208                         if (_3Dcontour[i]!=NULL)
01209                         {
01210                                 acum++;
01211                         }
01212                 }
01213 //Ramiro                fprintf(ff, "NumberOf3DContours: %d \n", acum);
01214                 // saving existing 3Dcontours
01215                 for (i=0; i<size; i++)
01216                 {
01217                         Save3Dcontour(ff,i);
01218                 }               
01219         }
01220 }
01221 
01222 // ----------------------------------------------------------------------------
01223 vtkProbeFilter* marAxis::get3DSlice(int i, kVolume* vol){                       // VISUALISATION-VTK 3D
01224         if (_3Dslices[i]==NULL){ create3DSlice(i,vol); }
01225         return _3Dslices[i];
01226 }
01227 // ----------------------------------------------------------------------------
01228 vtkPoints* marAxis::get3Dcontour(int i, kVolume* vol){                          // VISUALISATION-VTK 3D
01229         if (_3Dcontour[i]==NULL){ create3Dcontour(i, vol); }
01230         return _3Dcontour[i];
01231 }
01232 // ----------------------------------------------------------------------------
01233 vtkImageData* marAxis::getSliceImage(int i, kVolume* vol){                      // VISUALISATION-VTK 2D
01234         if (_quantificationImages[i]==NULL){ createSliceImage(i,vol); }
01235         return _quantificationImages[i];
01236 }
01237 // ----------------------------------------------------------------------------
01238 vtkPolyData* marAxis::get2Dcontour(int i , kVolume* vol){                       // VISUALISATION-VTK 2D
01239         if (_2Dcontours[i]==NULL){ create2Dcontour(i,vol); }
01240         return _2Dcontours[i];
01241 }
01242 // ----------------------------------------------------------------------------
01243 vtkPoints* marAxis::get2DDiameterMin(int i , kVolume* vol){             // VISUALISATION-VTK 2D
01244         if (_2DDiameterMin[i]==NULL){ create2DDiameterMin(i,vol); }
01245         return _2DDiameterMin[i];
01246 }
01247 // ----------------------------------------------------------------------------
01248 vtkPoints* marAxis::get2DDiameterMax(int i , kVolume* vol){             // VISUALISATION-VTK 2D
01249         if (_2DDiameterMax[i]==NULL){ create2DDiameterMax(i,vol); }
01250         return _2DDiameterMax[i];
01251 }
01252 // ----------------------------------------------------------------------------
01253 double marAxis::getAxisLenght(int pIni,int pEnd){
01254 
01255     if (pIni>pEnd){
01256                 int tmp=pIni;
01257                 pIni=pEnd;
01258                 pEnd=tmp;
01259         }
01260 
01261         marVector* pO = new marVector(2);
01262         marVector* pF = new marVector(2);
01263         double L;
01264     unsigned int j;
01265     for( j = pIni, L = 0.0; j < pEnd-1; j++ ) {
01266                 (*pO)=_points[j];
01267                 (*pF)=_points[j+1];
01268                 (*pF)=(*pF)-(*pO);
01269                 L+=pF->norm2();
01270     } // rof
01271         delete pO;
01272         delete pF;
01273         return L;
01274 }
01275 // ----------------------------------------------------------------------------
01276 void marAxis::calculateTotalAxisLenght(){
01277         _totalAxisLenght = getAxisLenght( 0 , _points.size( ) );
01278 }
01279 // ----------------------------------------------------------------------------
01280 double marAxis::getTotalLength(){
01281         if (_totalAxisLenght==-1){
01282                 calculateTotalAxisLenght();
01283         }
01284         return _totalAxisLenght;
01285 }
01286 
01287 // ----------------------------------------------------------------------------
01288 void marAxis::calculateSubAxisLength(){
01289         _subAxisLenght = getAxisLenght( _startQuant , _finishQuant );
01290 }
01291 // ----------------------------------------------------------------------------
01292 double marAxis::getSubAxisLength(){
01293         if (_subAxisLenght==-1){
01294                 calculateSubAxisLength();
01295         }
01296         return _subAxisLenght;
01297 }
01298 // ----------------------------------------------------------------------------
01299 double marAxis::getAverageArea(int pIni, int pEnd, kVolume* vol){
01300         marContour      *marcontourHealthy;
01301         int                     ihealthySlice,itemp;
01302         double          acumArea        =       0;
01303         for (ihealthySlice = pIni ; ihealthySlice <= pEnd ; ihealthySlice++ ){
01304                 itemp=ihealthySlice;
01305                 if (itemp<0)                            { itemp=0;                                      }
01306                 if (itemp>=_points.size( ))     { itemp=_points.size( )-1;      }
01307                 marcontourHealthy = getContour( itemp , vol );
01308                 acumArea = acumArea + marcontourHealthy->getArea();
01309         }
01310         return acumArea / (pEnd - pIni + 1);
01311 }
01312 // ----------------------------------------------------------------------------
01313 void marAxis::calculateReferenceArea(kVolume* vol){
01314         _referenceArea = getAverageArea(_healthySliceStart,_healthySliceEnd,  vol);
01315 }
01316 // ----------------------------------------------------------------------------
01317 double marAxis::getReferenceArea(kVolume* vol){
01318         if (_referenceArea==-1){
01319                 calculateReferenceArea(vol);
01320         }
01321         return _referenceArea;
01322 }
01323 // ----------------------------------------------------------------------------
01324 void marAxis::calculateReferenceAverDiam(kVolume* vol){
01325         marContour *marcontourHealthy;
01326         int ihealthySlice;
01327         double acumDiam=0;
01328         for (ihealthySlice=_healthySliceStart; ihealthySlice<=_healthySliceEnd; ihealthySlice++){
01329                 marcontourHealthy = getContour( ihealthySlice , vol );
01330                 acumDiam = acumDiam + marcontourHealthy->getAverageDiameter();
01331         }
01332         _referenceAverDiam = acumDiam / (_healthySliceEnd-_healthySliceStart+1);
01333 }
01334 // ----------------------------------------------------------------------------
01335 double marAxis::getReferenceAverDiam(kVolume* vol){
01336         if (_referenceAverDiam==-1){
01337                 calculateReferenceAverDiam(vol);
01338         }
01339         return _referenceAverDiam;
01340 }
01341 
01342 // ----------------------------------------------------------------------------
01343 void marAxis::reset( )
01344 {
01345 //EED Borrame
01346 //    unsigned int i;
01347     kCurve::reset( );
01348 
01349     _description                = "";
01350     _healthySlice               = -1;
01351     _startQuant                 = -1;
01352     _finishQuant                = -1;
01353     _actualQuant                = -1;
01354 
01355         _subAxisLenght          = -1;
01356         _referenceArea          = -1;
01357         _referenceAverDiam      = -1;
01358 
01359         Delete( );
01360 
01361 // EED Borrame  
01362 /*
01363     for( i = 0; i < _contours.size( ); i++ )
01364       delete _contours[ i ];
01365     _contours.clear( );
01366     for( i = 0; i < _slices.size( ); i++ )
01367         delete _slices[ i ];
01368     _slices.clear( );
01369 */
01370 }
01371 
01372 // ----------------------------------------------------------------------------
01373 void marAxis::copyFrom( const marObject& from )
01374 { // TODO
01375 }
01376 
01377 // ----------------------------------------------------------------------------
01378 bool marAxis::save( std::ofstream& os )
01379 {
01380     double p[ INDX_count ];
01381     unsigned int i;
01382 
01383     i = _description.length( );
01384     os.write( ( const char* )&i, sizeof( int ) );
01385     os.write( ( char* )_description.c_str( ), i * sizeof( char ) );
01386 
01387     i = getNumberOfControlPoints( );
01388     os.write( ( const char* )&i, sizeof( int ) );
01389     for( i = 0; i < getNumberOfControlPoints( ); i++ ) {
01390 
01391         memcpy( p, _controlPoints[ i ], 3 * sizeof( double ) );
01392         memcpy( p + 3, _controlState[ i ],
01393                 ( INDX_count - 3 ) * sizeof( double ) );
01394         os.write( ( const char* )p, INDX_count * sizeof( double ) );
01395 
01396     } // rof
01397     i = getNumberOfContours( );
01398     os.write( ( const char* )&i, sizeof( int ) );
01399     for( i = 0; i < getNumberOfContours( ); i++ )
01400       _contours[ i ]->save( os );
01401 
01402     return( true );
01403 }
01404 
01405 // ----------------------------------------------------------------------------
01406 bool marAxis::load( std::ifstream& is )
01407 {
01408     double p[ INDX_count ];
01409     int i, n;
01410 
01411     reset( );
01412 
01413     is.read( ( char* )&n, sizeof( int ) );
01414     _description.resize( n );
01415     is.read( ( char* )_description.c_str( ), n * sizeof( char ) );
01416 
01417     is.read( ( char* )&n, sizeof( int ) );
01418     for( i = 0; i < n; i++ ) {
01419 
01420         is.read( ( char* )p, INDX_count * sizeof( double ) );
01421         addControlPoint( p, p + 3 );
01422 
01423     } // rof
01424     is.read( ( char* )&n, sizeof( int ) );
01425     for( i = 0; i < n; i++ ) {
01426 
01427       _contours.push_back( new marContour( 0, getParameters( ) ) );
01428       _contours[ i ]->load( is );
01429 
01430     } // rof
01431     return( true );
01432 }
01433 // ----------------------------------------------------------------------------
01434 vtkPolyData* marAxis::Draw( )
01435 {
01436   unsigned int i, j;
01437   double *p;
01438 
01439   vtkPoints* allPoints = vtkPoints::New( );
01440   vtkCellArray* allTopology = vtkCellArray::New( );
01441 
01442   allTopology->InsertNextCell( _points.size( ) );
01443   for( i = 0, j=0; i < _points.size( ); i++, j++ ) {
01444     p = _points[i];
01445     allPoints->InsertNextPoint( p[ 0 ],  p[ 1 ], p[ 2 ] );
01446     allTopology->InsertCellPoint( j );
01447   } // rof
01448 
01449   _allData = vtkPolyData::New( );
01450   _allData->SetPoints( allPoints );
01451   _allData->SetLines( allTopology );
01452   allPoints->Delete();
01453   allTopology->Delete();
01454 
01455   return ( _allData );
01456 }
01457 // ----------------------------------------------------------------------------
01458 vtkPolyData *marAxis::GetAxisData( )
01459 {
01460         unsigned int i;
01461         double point[ 3 ];
01462         double radio;
01463         double p[marAxis::INDX_count];
01464 
01465 
01466         vtkPoints                       *allPoints              = vtkPoints::New( );
01467         vtkCellArray            *allTopology    = vtkCellArray::New( );
01468         vtkDoubleArray          *allRadios              = vtkDoubleArray::New( );
01469         allRadios->SetName("radio");
01470 
01471         for( i = 0; i < _controlPoints.size( ); i++ ) {
01472                 getControlPoint(i, p, p+3);
01473                 point[ 0 ]      =  p[marAxis::INDX_X];
01474                 point[ 1 ]      =  p[marAxis::INDX_Y];
01475                 point[ 2 ]      =  p[marAxis::INDX_Z];
01476                 radio           =  p[marAxis::INDX_RAYON]*2.0;
01477                 allPoints    -> InsertPoint( i, point[ 0 ] ,  point[ 1 ] , point[ 2 ]  );  //para saber exactamante el indice que se le asigno
01478 
01479                 allRadios       -> InsertValue(i,radio);
01480  
01481     if(i>0){
01482        allTopology->InsertNextCell(2);
01483        allTopology->InsertCellPoint(i);
01484        allTopology->InsertCellPoint(i-1);
01485       }
01486 
01487         } // rof
01488 
01489         _allData = vtkPolyData::New( );
01490         _allData        -> SetPoints( allPoints );
01491         _allData        -> SetLines( allTopology );
01492         _allData        -> GetPointData()->SetScalars(allRadios);
01493 
01494         
01495         return ( _allData );
01496 }
01497 // ----------------------------------------------------------------------------
01498 void marAxis::Delete( ){
01499         clearAllVectors();
01500 
01501         int i;
01502         for (i=0;i<_points.size();i++){         
01503                 if (_points[i]!=NULL) {                         
01504                         delete _points[i];
01505                         _points[i]=NULL;
01506                 }
01507         }
01508         _points.clear();
01509         _signal.clear();
01510 
01511 
01512         if(_allData) {
01513                 _allData->Delete();
01514                 _allData = NULL;
01515         }
01516 
01517 //  reset();
01518 }
01519 // ----------------------------------------------------------------------------
01520 void marAxis::setHealthySlice( int hsS, int hs, int hsE ){ 
01521           _healthySliceStart    = hsS;
01522           _healthySlice                 = hs;   
01523           _healthySliceEnd              = hsE;
01524           _referenceArea=-1;
01525           _referenceAverDiam=-1;
01526   };
01527 // ----------------------------------------------------------------------------
01528 void marAxis::setStartQuant( int sq ){
01529         _startQuant             =       sq;             
01530         _subAxisLenght  =       -1;
01531 };
01532 // ----------------------------------------------------------------------------
01533 void marAxis::setFinishQuant( int fq ){ 
01534         _finishQuant    =       fq;             
01535         _subAxisLenght  =       -1;
01536 };
01537 
01538 // ----------------------------------------------------------------------------
01539 void marAxis::AddPointToList(double x, double y, double z, int signal)
01540 {
01541         double *p;
01542         p=(double *)malloc(sizeof(double)*3);
01543         p[0]=x;
01544         p[1]=y;
01545         p[2]=z;
01546         _points.push_back( p );
01547         _signal.push_back( signal );
01548 }
01549 
01550 
01551 
01552 // eof - axis.cxx

Generated on 18 Mar 2010 for creaMaracasVisu_lib by  doxygen 1.6.1