bool itkDataTensorImageReaderBase::read (const QString &path)
{
    if (this->io.IsNull())
        return false;
	
    this->readInformation ( path );
	
    qDebug() << "Read with: " << this->identifier();

    if (medAbstractData *medData = dynamic_cast<medAbstractData*>(this->data()) ) {
      
        if (medData->identifier()=="itkDataTensorImageDouble3") {

	  if (this->io->GetNumberOfComponents()==6) {

	    typedef itk::Tensor<double, 3>    TensorType;
	    typedef itk::Image<TensorType, 3> TensorImageType;

	    typedef itk::Vector<double, 6>    VectorType;
	    typedef itk::Image<VectorType, 3> VectorImageType;

	    typedef itk::ImageFileReader<VectorImageType> ReaderType;
	    
	    VectorImageType::Pointer image = 0;
	    {
	      ReaderType::Pointer reader = ReaderType::New();
	      reader->SetImageIO (this->io);
	      reader->SetFileName ( path.toAscii().constData() );
	      try {
		reader->Update();
	      }
	      catch (itk::ExceptionObject &e) {
		qDebug() << e.GetDescription();
		return false;
	      }
	      image = reader->GetOutput(); 
	    }

	    TensorImageType::Pointer tensors = TensorImageType::New();
	    TensorImageType::RegionType region = image->GetLargestPossibleRegion();
	    tensors->SetRegions   (region);
	    tensors->SetSpacing   (image->GetSpacing());
	    tensors->SetOrigin    (image->GetOrigin());
	    tensors->SetDirection (image->GetDirection());

	    try {
	        tensors->Allocate();
	    }
	    catch (itk::ExceptionObject &e) {
	        qDebug() << e.GetDescription();
		return false;
	    }

	    itk::ImageRegionConstIteratorWithIndex<VectorImageType>  itIn (image,
									   image->GetLargestPossibleRegion());
	    itk::ImageRegionIteratorWithIndex<TensorImageType> itOut(tensors,
								     tensors->GetLargestPossibleRegion());

	    while(!itOut.IsAtEnd()) {
      
	      VectorType vec = itIn.Get();
	      TensorType tensor;
	      
	      for( unsigned int j=0; j<6; j++) {
		tensor[j] = vec[j];
	      }
      
	      itOut.Set (tensor);
	      
	      ++itOut;
	      ++itIn;
	    }

	    medData->setData (tensors);
	    
	  }
	  else if (this->io->GetNumberOfComponents()==9) {

	    typedef itk::Tensor<double, 3>    TensorType;
	    typedef itk::Image<TensorType, 3> TensorImageType;

	    typedef itk::Vector<double, 9>    VectorType;
	    typedef itk::Image<VectorType, 3> VectorImageType;

	    typedef itk::ImageFileReader<VectorImageType> ReaderType;
	    
	    VectorImageType::Pointer image = 0;
	    {
	      ReaderType::Pointer reader = ReaderType::New();
	      reader->SetImageIO (this->io);
	      reader->SetFileName ( path.toAscii().constData() );
	      try {
		reader->Update();
	      }
	      catch (itk::ExceptionObject &e) {
		qDebug() << e.GetDescription();
		return false;
	      }
	      image = reader->GetOutput(); 
	    }

	    TensorImageType::Pointer tensors = TensorImageType::New();
	    TensorImageType::RegionType region = image->GetLargestPossibleRegion();
	    tensors->SetRegions   (region);
	    tensors->SetSpacing   (image->GetSpacing());
	    tensors->SetOrigin    (image->GetOrigin());
	    tensors->SetDirection (image->GetDirection());

	    try {
	        tensors->Allocate();
	    }
	    catch (itk::ExceptionObject &e) {
	        qDebug() << e.GetDescription();
		return false;
	    }

	    itk::ImageRegionConstIteratorWithIndex<VectorImageType>  itIn (image,
									   image->GetLargestPossibleRegion());
	    itk::ImageRegionIteratorWithIndex<TensorImageType> itOut(tensors,
								     tensors->GetLargestPossibleRegion());

	    while(!itOut.IsAtEnd()) {
      
	      VectorType vec = itIn.Get();
	      TensorType tensor;
	      
	      for (unsigned int i=0; i<3; i++)
		for (unsigned int j=0; j<3; j++)
		  tensor.SetComponent (i, j, vec[i*3+j]);
      
	      itOut.Set (tensor);
	      
	      ++itOut;
	      ++itIn;
	    }

	    medData->setData (tensors);
	  }
	  else {
	      qDebug() << "Unsupported number of components";
	      return false;
	  } 
	}

	else if (medData->identifier()=="itkDataTensorImageFloat3") {

	  if (this->io->GetNumberOfComponents()==6) {

	    typedef itk::Tensor<float, 3>     TensorType;
	    typedef itk::Image<TensorType, 3> TensorImageType;

	    typedef itk::Vector<float, 6>     VectorType;
	    typedef itk::Image<VectorType, 3> VectorImageType;

	    typedef itk::ImageFileReader<VectorImageType> ReaderType;
	    
	    VectorImageType::Pointer image = 0;
	    {
	      ReaderType::Pointer reader = ReaderType::New();
	      reader->SetImageIO (this->io);
	      reader->SetFileName ( path.toAscii().constData() );
	      try {
		reader->Update();
	      }
	      catch (itk::ExceptionObject &e) {
		qDebug() << e.GetDescription();
		return false;
	      }
	      image = reader->GetOutput(); 
	    }

	    TensorImageType::Pointer tensors = TensorImageType::New();
	    TensorImageType::RegionType region = image->GetLargestPossibleRegion();
	    tensors->SetRegions   (region);
	    tensors->SetSpacing   (image->GetSpacing());
	    tensors->SetOrigin    (image->GetOrigin());
	    tensors->SetDirection (image->GetDirection());

	    try {
	        tensors->Allocate();
	    }
	    catch (itk::ExceptionObject &e) {
	        qDebug() << e.GetDescription();
		return false;
	    }

	    itk::ImageRegionConstIteratorWithIndex<VectorImageType>  itIn (image,
									   image->GetLargestPossibleRegion());
	    itk::ImageRegionIteratorWithIndex<TensorImageType> itOut(tensors,
								     tensors->GetLargestPossibleRegion());

	    while(!itOut.IsAtEnd()) {
      
	      VectorType vec = itIn.Get();
	      TensorType tensor;
	      
	      for( unsigned int j=0; j<6; j++) {
		tensor[j] = vec[j];
	      }
      
	      itOut.Set (tensor);
	      
	      ++itOut;
	      ++itIn;
	    }
	    
	    medData->setData (tensors);
	  }
	  else if (this->io->GetNumberOfComponents()==9) {

	    typedef itk::Tensor<float, 3>     TensorType;
	    typedef itk::Image<TensorType, 3> TensorImageType;

	    typedef itk::Vector<float, 9>     VectorType;
	    typedef itk::Image<VectorType, 3> VectorImageType;

	    typedef itk::ImageFileReader<VectorImageType> ReaderType;
	    
	    VectorImageType::Pointer image = 0;
	    {
	      ReaderType::Pointer reader = ReaderType::New();
	      reader->SetImageIO (this->io);
	      reader->SetFileName ( path.toAscii().constData() );
	      try {
		reader->Update();
	      }
	      catch (itk::ExceptionObject &e) {
		qDebug() << e.GetDescription();
		return false;
	      }
	      image = reader->GetOutput(); 
	    }

	    TensorImageType::Pointer tensors = TensorImageType::New();
	    TensorImageType::RegionType region = image->GetLargestPossibleRegion();
	    tensors->SetRegions   (region);
	    tensors->SetSpacing   (image->GetSpacing());
	    tensors->SetOrigin    (image->GetOrigin());
	    tensors->SetDirection (image->GetDirection());

	    try {
	        tensors->Allocate();
	    }
	    catch (itk::ExceptionObject &e) {
	        qDebug() << e.GetDescription();
		return false;
	    }

	    itk::ImageRegionConstIteratorWithIndex<VectorImageType>  itIn (image,
									   image->GetLargestPossibleRegion());
	    itk::ImageRegionIteratorWithIndex<TensorImageType> itOut(tensors,
								     tensors->GetLargestPossibleRegion());

	    while(!itOut.IsAtEnd()) {
      
	      VectorType vec = itIn.Get();
	      TensorType tensor;
	      
	      for (unsigned int i=0; i<3; i++)
		for (unsigned int j=0; j<3; j++)
		  tensor.SetComponent (i, j, vec[i*3+j]);
      
	      itOut.Set (tensor);
	      
	      ++itOut;
	      ++itIn;
	    }

	    medData->setData (tensors);
	  }
	  else {
	      qDebug() << "Unsupported number of components";
	      return false;
	  } 
	}
	else {
	  qDebug() << "Unsupported data type";
	  return false;
	}
    }
    else {
      qDebug() << "No data set or could not create one";
      return false;
    }

    return true;
    
}
bool itkDataTensorImageWriterBase::write(const QString& path, PixelType dummyArgument)
{
    typedef typename itk::Vector<PixelType, 6>     VectorType;
    typedef typename itk::Image<VectorType, 3>     VectorImageType;
    typedef typename itk::Tensor<PixelType, 3>     TensorType;
    typedef typename itk::Image<TensorType, 3>     TensorImageType;

    typedef typename VectorImageType::Pointer VectorImageTypePointer;
    VectorImageTypePointer myTensorImage = VectorImageType::New();

    typedef typename TensorImageType::Pointer TensorImageTypePointer;
    TensorImageTypePointer image = dynamic_cast< TensorImageType* >( (itk::Object*)(this->data()->output()) );

    typedef typename TensorImageType::RegionType TensorImageTypeRegionType;
    TensorImageTypeRegionType region = image->GetLargestPossibleRegion();

    myTensorImage->SetRegions (region);
    myTensorImage->SetSpacing (image->GetSpacing());
    myTensorImage->SetOrigin (image->GetOrigin());
    myTensorImage->SetDirection (image->GetDirection());
    try {
        myTensorImage->Allocate();
    }
    catch (itk::ExceptionObject &e) {
        std::cerr << e;
        throw itk::ExceptionObject (__FILE__,__LINE__,"Error during memory allocation.");
    }

    typedef itk::ImageRegionConstIterator<TensorImageType> IteratorType;
    IteratorType it (image, image->GetLargestPossibleRegion());

    itk::ImageRegionIteratorWithIndex<VectorImageType> itOut(myTensorImage, myTensorImage->GetLargestPossibleRegion());

    while( !it.IsAtEnd() )
    {
        TensorType tensor = it.Get();
        VectorType vec;

        for( unsigned int i=0; i<6; i++) {
            vec[i] = static_cast<float>(tensor[i]);
        }
        itOut.Set (vec);

        ++it;
        ++itOut;
    }

    typedef typename itk::ImageFileWriter<VectorImageType>::Pointer ImageFileWriterPointer;
    ImageFileWriterPointer myWriter = itk::ImageFileWriter<VectorImageType>::New();
    myWriter->SetFileName(path.toAscii().constData());
    myWriter->SetInput(myTensorImage);
    try {
        myWriter->Write();
    }
    catch(itk::ExceptionObject &e) {
        qDebug() << e.GetDescription();
        return false;
    }

    return true;
}
Ejemplo n.º 3
0
      // Prepare set of current unknowns and list of current equations
   VariableSet EquationSystem::prepareCurrentUnknownsAndEquations(
                                                         gnssDataMap& gdsMap )
   {

         // Let's clear the current equations list
      currentEquationsList.clear();

         // Let's create 'currentUnkSet' set
      VariableSet currentUnkSet;

         // Get "currentSatSet" and "currentSourceSet"
         // and stored in currentSourceSet and currentSatSet
      prepareCurrentSourceSat( gdsMap );


         // Visit each "Equation" in "equationDescriptionList"
      for( std::list<Equation>::const_iterator itEq =
                                                equationDescriptionList.begin();
           itEq != equationDescriptionList.end();
           ++itEq )
      {

            // First, get the SourceID set for this equation description
         SourceIDSet equSourceSet;

            // Check if current equation description is valid for all sources
         if ( (*itEq).getEquationSource() == Variable::allSources )
         {
            equSourceSet = currentSourceSet;
         }
         else
         {

               // Check if equation description is valid for some sources
            if ( (*itEq).getEquationSource() == Variable::someSources )
            {

                  // We have to find the intersection between equation
                  // description SourceID's and available SourceID's.
               SourceIDSet tempSourceSet( (*itEq).getSourceSet() );

                  // Declare an 'insert_iterator' to be used by
                  // 'set_intersection' algorithm (provided by STL)
               std::insert_iterator< SourceIDSet >
                                 itOut( equSourceSet, equSourceSet.begin() );

                  // Let's intersect both sets
               set_intersection( tempSourceSet.begin(), tempSourceSet.end(),
                              currentSourceSet.begin(), currentSourceSet.end(),
                              itOut );

            }
            else
            {
                  // In this case, we take directly the source into the
                  // equation source set
               equSourceSet.insert( (*itEq).getEquationSource() );
            }

         }  // End of 'if ( (*itEq).getEquationSource() == ...'
         
            // Second, get the SatID set for this equation description
         SatIDSet equSatSet = (*itEq).getSatSet();
         

            // We have the SourceID set that is applicable to this
            // equation description.

            // Now we must get the satellites visible from each
            // particular SourceID
         for( SourceIDSet::const_iterator itSource = equSourceSet.begin();
              itSource != equSourceSet.end();
              ++itSource )
         {

               // Get visible satellites from this SourceID
            SatIDSet visibleSatSet;

               // Iterate through all items in the gnssDataMap
            for( gnssDataMap::const_iterator it = gdsMap.begin();
                 it != gdsMap.end();
                 ++it )
            {

                  // Look for current SourceID
               sourceDataMap::const_iterator sdmIter(
                                             (*it).second.find( (*itSource) ) );

                  // If SourceID was found, then look for satellites
               if( sdmIter != (*it).second.end() )
               {

                     // Iterate through corresponding 'satTypeValueMap'
                  for( satTypeValueMap::const_iterator stvmIter =
                                                      (*sdmIter).second.begin();
                       stvmIter != (*sdmIter).second.end();
                       stvmIter++ )
                  {
                        // for some sat   
                     if((equSatSet.size() > 0)                           &&
                        (equSatSet.find((*stvmIter).first) == equSatSet.end()))
                     {
                        continue;
                     }

                        // Add current SatID to 'visibleSatSet'
                     visibleSatSet.insert( (*stvmIter).first );

                  }  // End of 'for( satTypeValueMap::const_iterator ...'

               }  // End of 'for( sourceDataMap::const_iterator sdmIter = ...'

            }  // End of 'for( gnssDataMap::const_iterator it = ...'

               // We have the satellites visible from this SourceID

               
               // We need a copy of current Equation object description
            Equation tempEquation( (*itEq) );

               // Remove all variables from current equation
            tempEquation.clear();

               // Update equation independent term with SourceID information
            tempEquation.header.equationSource = (*itSource);

               // Now, let's visit all Variables in this equation description
            for( VariableSet::const_iterator itVar = (*itEq).body.begin();
                 itVar != (*itEq).body.end();
                 ++itVar )
            {

                  // We will work with a copy of current Variable
               Variable var( (*itVar) );

                  // Check what type of variable we are working on

                  // If variable is source-indexed, set SourceID
               if( var.getSourceIndexed() )
               {
                  var.setSource( (*itSource) );
               }

                  // Add this variable to current equation description. Please
                  // be aware that satellite-indexed variables inside current
                  // equations will be handled later
               tempEquation.addVariable(var);

                  // If variable is not satellite-indexed, we just need to
                  // add it to "currentUnkSet
               if( !var.getSatIndexed() )
               {
                     // Insert the result in "currentUnkSet" and
                     // current equation
                  currentUnkSet.insert(var);
                  //tempEquation.addVariable(var);
               }
               else
               {
                     // If variable IS satellite-indexed, we have to visit all
                     // visible satellites (from current SourceID) and set the
                     // satellite before adding variable to "currentUnkSet
                  for( SatIDSet::const_iterator itSat = visibleSatSet.begin();
                       itSat != visibleSatSet.end();
                       ++itSat )
                  {

                        // Set satellite
                     var.setSatellite( (*itSat) );

                        // Insert the result in "currentUnkSet" and
                        // current equation
                     currentUnkSet.insert(var);
                  }

               }  // End of 'if( !var.getSatIndexed() )...'

            }  // End of 'for( VariableSet::const_iterator itVar = ...'


               // Let's generate the current equations starting from this
               // equation description. Therefore, we update equation
               // independent term with SatID information and add each instance
               // to 'currentEquationsList'.
            for( SatIDSet::const_iterator itSat = visibleSatSet.begin();
                 itSat != visibleSatSet.end();
                 ++itSat )
            {
               tempEquation.header.equationSat = (*itSat);

                  // New equation is complete: Add it to 'currentEquationsList'
               currentEquationsList.push_back( tempEquation );
            }

         }  // End of 'for( SourceIDSet::const_iterator itSource = ...'

      }  // End of 'for( std::list<Equation>::const_iterator itEq = ...'


         // Now we will take care of satellite-indexed variables inside each
         // specific "Equation" in "currentEquationsList"
      const size_t eqListSize( currentEquationsList.size() );
      for( size_t i = 0; i < eqListSize; ++i )
      {

            // Get a copy of first equation on 'currentEquationsList'
         Equation tempEqu( currentEquationsList.front() );

            // Remove the original equation at the beginning of the list.
         currentEquationsList.pop_front();

            // Get a copy of variables inside this equation
         VariableSet varSet( tempEqu.body );

            // Clear the variables from this equation
         tempEqu.clear();

            // Visit each variable inside 'varSet', check if it is
            // satellite-indexed, and add it to equation
         for( VariableSet::iterator itVar = varSet.begin();
              itVar != varSet.end();
              ++itVar )
         {

               // Check if it is satellite-indexed
            if( !(*itVar).getSatIndexed() )
            {
                  // If not satellite-indexed, just add it back
               tempEqu.addVariable( (*itVar) );
            }
            else
            {
               // If 'itVar' is satellite-indexed, let's index a copy of it
               // and add it to equation
               Variable var( (*itVar) );
               var.setSatellite( tempEqu.header.equationSat );

               tempEqu.addVariable( var );
            }

         }  // End of 'for( VariableSet::iterator itVar = varSet.begin(); ...'

            // Our equation is ready, let's add it to the end of the list
         currentEquationsList.push_back( tempEqu );

      }  // End of 'for( int i = 0; i < eqListSize; ++i ) ...'


         // Return set of current unknowns
      return currentUnkSet;

   }  // End of method 'EquationSystem::prepareCurrentUnknownsAndEquations()'