Ejemplo n.º 1
0
int main(int argc, char* argv[])
{
  PARSE_ARGS;

  if(fiberFile == "")
    {
      std::cerr << "A fiber file has to be specified" << std::endl;
      return EXIT_FAILURE;
    }
 
  
  vtkSmartPointer<vtkPolyDataReader> reader = vtkSmartPointer<vtkPolyDataReader>::New();
  std::cout << "Reading " << fiberFile<< std::endl;
  reader->SetFileName(fiberFile.c_str());
  reader->Update();
 
  // Extract the polydata
  vtkSmartPointer<vtkPolyData> polydata =
    reader->GetOutput();
  
 
  DeformationImageType::Pointer deformationfield(NULL);
  if(hField != "")
    deformationfield = readDeformationField(hField, HField);
  else if(displacementField != "")
    deformationfield = readDeformationField(displacementField, Displacement);
  else
    deformationfield = NULL;
  
  typedef itk::VectorLinearInterpolateImageFunction<DeformationImageType, double> DeformationInterpolateType;
  DeformationInterpolateType::Pointer definterp(NULL);
  if(deformationfield)
  {
    definterp = DeformationInterpolateType::New();
    definterp->SetInputImage(deformationfield);
  } else {
    std::cout << "A deformation field has to be specified" << std::endl;
      return EXIT_FAILURE;
  }
  
    
    typedef DeformationInterpolateType::ContinuousIndexType ContinuousIndexType;
    ContinuousIndexType ci, origci;
    // For each point along the fiber
     vtkIdType numPoints= polydata->GetNumberOfPoints();
     vtkPoints* inPts = polydata->GetPoints();
     vtkPoints    * points = vtkPoints::New();
  

    typedef DTIPointType::PointType PointType;
    PointType fiberpoint;
    double fiberpointtemp[3];

    for(int i=0;i<numPoints;i++)
    {	
        inPts->GetPoint( i, fiberpointtemp );
	//convert RAS to LPS (vtk)
	fiberpoint[0] = - fiberpointtemp[0];
	fiberpoint[1] = - fiberpointtemp[1];
	fiberpoint[2] = + fiberpointtemp[2];	

        deformationfield->TransformPhysicalPointToContinuousIndex(fiberpoint,ci);
	if( !deformationfield->GetLargestPossibleRegion().IsInside( ci ) )
        {
          std::cerr << "Fiber is outside deformation field image. Deformation field has to be in the fiber space. Warning: Original position will be used" << std::endl ;
        }
        DeformationPixelType warp(definterp->EvaluateAtContinuousIndex(ci).GetDataPointer());
        for(unsigned int j =0; j < 3; j++)
          fiberpoint[j] +=warp[j]; 

	//convert LPS to RAS (vtk)
	fiberpoint[0] = - fiberpoint[0];
	fiberpoint[1] = - fiberpoint[1];
	fiberpoint[2] = + fiberpoint[2];

        points->InsertPoint(i,fiberpoint[0],fiberpoint[1],fiberpoint[2]);
	
    }
    polydata->SetPoints(points);
    

  
  if(fiberOutput != "")
  {
  std::cout<<std::endl;
  std::cout<<"Saving fibers...."<<std::endl;
  std::cout<<fiberOutput<<std::endl;
  vtkPolyDataWriter * fiberwriter = vtkPolyDataWriter::New();
  fiberwriter->SetFileName(fiberOutput.c_str());
  fiberwriter->SetInput(polydata);
  fiberwriter->Update();
  try
    {
    fiberwriter->Write();
    std::cout<<"Done!"<<std::endl;
    std::cout<<"Number of fibers saved: "<<polydata->GetNumberOfLines()<<std::endl;
    }
  catch(...)
    {
    std::cout << "Error while saving fiber file." << std::endl;
    throw;
    }
	}

  return EXIT_SUCCESS;
}
int averageDeformationField(parameters list )
{
//read the reference volume
    typedef itk::Image< unsigned char , 3 > ImageType ;
    typedef itk::ImageFileReader< ImageType > ReaderType ;
    typedef typename ReaderType::Pointer ReaderTypePointer ;
    ReaderTypePointer readerReference ;
    readerReference = ReaderType::New() ;
    readerReference->SetFileName( list.referenceVolume.c_str() ) ;
    readerReference->UpdateOutputInformation() ;

      //set the size, origin, spacing,direction of the reference volume
      itk::Point< double , 3 > m_Origin ;
      itk::Vector< double , 3 > m_Spacing ;
      itk::Size< 3 > m_Size ;
      itk::Matrix< double , 3 , 3 > m_Direction;
       m_Spacing = readerReference->GetOutput()->GetSpacing() ;
       m_Size= readerReference->GetOutput()->GetLargestPossibleRegion().GetSize() ;
       m_Origin = readerReference->GetOutput()->GetOrigin() ;
       m_Direction = readerReference->GetOutput()->GetDirection() ;

       int n=list.deffieldtoaverage.size();
	typename DeformationImageType::Pointer fieldPointer1 ;
	DeformationFieldType dftype1 = HField ;
	if( !list.typeOfField[0].compare( "displacement" ) )
		{
			dftype1 = Displacement ;
		}
	//reads deformation field and if it is a h-field, it transforms it to a displacement field
	fieldPointer1 = readDeformationField( list.deffieldtoaverage[0] , dftype1 ) ;
	//Resample the deformation field so that it has the same properties as the reference image
      	ResampleDeformationField( fieldPointer1 ,m_Origin , m_Spacing ,m_Size ,m_Direction) ;
	
	typedef itk::ImageRegionIterator< DeformationImageType > DeformationConstIteratorType;
	DeformationConstIteratorType it1 (fieldPointer1,fieldPointer1->GetLargestPossibleRegion());
	//walk through all other deformation field volumes
	for(int i=1;i<n;i++)
	{
		
		typename DeformationImageType::Pointer fieldPointer2 ;
		//set if the field is a displacement or a H- field
		DeformationFieldType dftype2 = HField ;
		
		if( !list.typeOfField[i].compare( "displacement" ) )
		{
			dftype2 = Displacement ;
		}
		//reads deformation field and if it is a h-field, it transforms it to a displacement field
		fieldPointer2 = readDeformationField( list.deffieldtoaverage[i] , dftype2 ) ;
	 	
        	//Resample the deformation field so that it has the same properties as the reference image
      		ResampleDeformationField( fieldPointer2 ,m_Origin ,m_Spacing , m_Size ,m_Direction) ;
		
		it1.GoToBegin();
		DeformationConstIteratorType it2 (fieldPointer2,fieldPointer2->GetLargestPossibleRegion());
		it2.GoToBegin();
		itk::Vector<double,3> sum;sum[0]=0;sum[1]=0;sum[2]=0;
		while(!it1.IsAtEnd() && !it2.IsAtEnd())
		{
			sum+=(it1.Get())+(it2.Get());//sum displacements together	
			it1.Set(sum);
			sum[0]=0;sum[1]=0;sum[2]=0;
			++it1;++it2;
		}

	}
	it1.GoToBegin();
	itk::Vector<double,3> temp;
	while(!it1.IsAtEnd())
        {
		temp=(it1.Get())/n;//compute the average of displacements
		it1.Set(temp);
		++it1;
	}
	//Save HField
        typedef itk::ImageFileWriter< DeformationImageType > HFieldWriterType ;
        typedef typename HFieldWriterType::Pointer HFieldWriterTypePointer ;
        HFieldWriterTypePointer hfwriterAvg = HFieldWriterType::New() ;

        hfwriterAvg->SetInput( fieldPointer1 ) ;
        hfwriterAvg->SetFileName( list.outputAvg ) ;
	hfwriterAvg->UseCompressionOn();
        try
        {
          hfwriterAvg->Update() ;
        }
        catch( itk::ExceptionObject & Except )
        {
          std::cerr << "Writing output HField: Exception caught!"
                    << std::endl ;
          std::cerr << Except << std::endl ;
          return EXIT_FAILURE ;
        }

return EXIT_SUCCESS ;
}