void TractsToDWIImageFilter::GenerateParticleGrid()
  {
    MITK_INFO << "Generating particle grid from fiber bundle";
    m_ParticleGrid = mitk::ParticleGrid::New();

    float* bounds = m_FiberBundle->GetBounds();
    int size[] = {(int)bounds[0],(int)bounds[1],(int)bounds[2]};
    m_ParticleGrid->SetSize(size);

    typedef itk::Point<float,3>                                                   ContainerPointType; //no need to init, is no smartpointer
    typedef itk::VectorContainer<unsigned int, ContainerPointType>                ContainerTractType;
    typedef itk::VectorContainer< unsigned int, ContainerTractType::Pointer >     ContainerType; //init via smartpointer

    ContainerType::Pointer tractContainer = m_FiberBundle->GetTractContainer();

    for (int i=0; i<tractContainer->Size(); i++)
    {
      ContainerTractType::Pointer tract = tractContainer->GetElement(i);
      for (int j=0; j<tract->Size(); j++)
      {
        vnl_vector_fixed<float,3> pos = tract->GetElement(j).GetVnlVector();
        vnl_vector_fixed<float,3> next;
        vnl_vector_fixed<float,3> dir;

        if (j<tract->Size()-1)
          next = tract->GetElement(j+1).GetVnlVector();
        else
          next = tract->GetElement(j-1).GetVnlVector();

        dir = next-pos;
        dir.normalize();
        mitk::Particle* p = new mitk::Particle();
        p->SetPosition(pos);
        p->SetDirection(dir);
        m_ParticleGrid->AddParticle(p);
      }
    }
  }
Esempio n. 2
0
  void TractsToFiberEndingsImageFilter< TInputImage, TOutputPixelType >
      ::GenerateData()
  {
    MITK_INFO << "Generating 2D fiber endings image";
    if(&typeid(TOutputPixelType) != &typeid(unsigned char))
    {
      MITK_INFO << "Only 'unsigned char' and 'itk::RGBAPixel<unsigned char> supported as OutputPixelType";
      return;
    }
    mitk::Geometry3D::Pointer geometry = m_FiberBundle->GetGeometry();

    typename OutputImageType::Pointer outImage =
        static_cast< OutputImageType * >(this->ProcessObject::GetOutput(0));

    outImage->SetSpacing( geometry->GetSpacing()/m_UpsamplingFactor );   // Set the image spacing

    mitk::Point3D origin = geometry->GetOrigin();
    mitk::Point3D indexOrigin;
    geometry->WorldToIndex(origin, indexOrigin);
    indexOrigin[0] = indexOrigin[0] - .5 * (1.0-1.0/m_UpsamplingFactor);
    indexOrigin[1] = indexOrigin[1] - .5 * (1.0-1.0/m_UpsamplingFactor);
    indexOrigin[2] = indexOrigin[2] - .5 * (1.0-1.0/m_UpsamplingFactor);
    mitk::Point3D newOrigin;
    geometry->IndexToWorld(indexOrigin, newOrigin);

    outImage->SetOrigin( newOrigin );     // Set the image origin

    itk::Matrix<double, 3, 3> matrix;
    for (int i=0; i<3; i++)
      for (int j=0; j<3; j++)
        matrix[j][i] = geometry->GetMatrixColumn(i)[j]/geometry->GetSpacing().GetElement(i);
    outImage->SetDirection( matrix );  // Set the image direction

    float* bounds = m_FiberBundle->GetBounds();
    ImageRegion<3> upsampledRegion;
    upsampledRegion.SetSize(0, bounds[0]);
    upsampledRegion.SetSize(1, bounds[1]);
    upsampledRegion.SetSize(2, bounds[2]);

    typename InputImageType::RegionType::SizeType upsampledSize = upsampledRegion.GetSize();
    for (unsigned int n = 0; n < 3; n++)
    {
      upsampledSize[n] = upsampledSize[n] * m_UpsamplingFactor;
    }
    upsampledRegion.SetSize( upsampledSize );
    outImage->SetRegions( upsampledRegion );

    outImage->Allocate();

    int w = upsampledSize[0];
    int h = upsampledSize[1];
    int d = upsampledSize[2];


    unsigned char* accuout;
    accuout = reinterpret_cast<unsigned char*>(outImage->GetBufferPointer());
    for (int i=0; i<w*h*d; i++) accuout[i] = 0;

    typedef mitk::FiberBundle::ContainerTractType   ContainerTractType;
    typedef mitk::FiberBundle::ContainerType        ContainerType;
    typedef mitk::FiberBundle::ContainerPointType   ContainerPointType;
    ContainerType::Pointer tractContainer = m_FiberBundle->GetTractContainer();

    for (int i=0; i<tractContainer->Size(); i++)
    {
      ContainerTractType::Pointer tract = tractContainer->GetElement(i);
      int tractsize = tract->Size();

      if (tractsize>1)
      {
        ContainerPointType start = tract->GetElement(0);
        ContainerPointType end = tract->GetElement(tractsize-1);

        start[0] = (start[0]+0.5) * m_UpsamplingFactor;
        start[1] = (start[1]+0.5) * m_UpsamplingFactor;
        start[2] = (start[2]+0.5) * m_UpsamplingFactor;

        // int coordinates inside image?
        int px = (int) (start[0]);
        if (px < 0 || px >= w)
          continue;
        int py = (int) (start[1]);
        if (py < 0 || py >= h)
          continue;
        int pz = (int) (start[2]);
        if (pz < 0 || pz >= d)
          continue;

        accuout[( px   + w*(py  + h*pz  ))] += 1;


        end[0] = (end[0]+0.5) * m_UpsamplingFactor;
        end[1] = (end[1]+0.5) * m_UpsamplingFactor;
        end[2] = (end[2]+0.5) * m_UpsamplingFactor;

        // int coordinates inside image?
        px = (int) (end[0]);
        if (px < 0 || px >= w)
          continue;
        py = (int) (end[1]);
        if (py < 0 || py >= h)
          continue;
        pz = (int) (end[2]);
        if (pz < 0 || pz >= d)
          continue;

        accuout[( px   + w*(py  + h*pz  ))] += 1;
      }
    }

    MITK_INFO << "2D fiber endings image generated";
  }
  void FiberBundleReader::GenerateOutputInformation()
  {
    m_OutputCache = OutputType::New();

    std::string ext = itksys::SystemTools::GetFilenameLastExtension(m_FileName);
    ext = itksys::SystemTools::LowerCase(ext);

    if ( m_FileName == "")
    {

    }
    else if (ext == ".fib")
    {
      try
      {
        TiXmlDocument doc( m_FileName );
        doc.LoadFile();

        TiXmlHandle hDoc(&doc);
        TiXmlElement* pElem;
        TiXmlHandle hRoot(0);

        pElem = hDoc.FirstChildElement().Element();

        // save this for later
        hRoot = TiXmlHandle(pElem);

        pElem = hRoot.FirstChildElement(FiberBundleReader::XML_GEOMETRY).Element();

        // read geometry
        mitk::Geometry3D::Pointer geometry = mitk::Geometry3D::New();

        // read origin
        mitk::Point3D origin;
        double temp = 0;
        pElem->Attribute(FiberBundleReader::XML_ORIGIN_X, &temp);
        origin[0] = temp;
        pElem->Attribute(FiberBundleReader::XML_ORIGIN_Y, &temp);
        origin[1] = temp;
        pElem->Attribute(FiberBundleReader::XML_ORIGIN_Z, &temp);
        origin[2] = temp;
        geometry->SetOrigin(origin);

        // read spacing
        float spacing[3];
        pElem->Attribute(FiberBundleReader::XML_SPACING_X, &temp);
        spacing[0] = temp;
        pElem->Attribute(FiberBundleReader::XML_SPACING_Y, &temp);
        spacing[1] = temp;
        pElem->Attribute(FiberBundleReader::XML_SPACING_Z, &temp);
        spacing[2] = temp;
        geometry->SetSpacing(spacing);

        // read transform
        vtkMatrix4x4* m = vtkMatrix4x4::New();
        pElem->Attribute(FiberBundleReader::XML_MATRIX_XX, &temp);
        m->SetElement(0,0,temp);
        pElem->Attribute(FiberBundleReader::XML_MATRIX_XY, &temp);
        m->SetElement(1,0,temp);
        pElem->Attribute(FiberBundleReader::XML_MATRIX_XZ, &temp);
        m->SetElement(2,0,temp);
        pElem->Attribute(FiberBundleReader::XML_MATRIX_YX, &temp);
        m->SetElement(0,1,temp);
        pElem->Attribute(FiberBundleReader::XML_MATRIX_YY, &temp);
        m->SetElement(1,1,temp);
        pElem->Attribute(FiberBundleReader::XML_MATRIX_YZ, &temp);
        m->SetElement(2,1,temp);
        pElem->Attribute(FiberBundleReader::XML_MATRIX_ZX, &temp);
        m->SetElement(0,2,temp);
        pElem->Attribute(FiberBundleReader::XML_MATRIX_ZY, &temp);
        m->SetElement(1,2,temp);
        pElem->Attribute(FiberBundleReader::XML_MATRIX_ZZ, &temp);
        m->SetElement(2,2,temp);

        m->SetElement(0,3,origin[0]);
        m->SetElement(1,3,origin[1]);
        m->SetElement(2,3,origin[2]);
        m->SetElement(3,3,1);
        geometry->SetIndexToWorldTransformByVtkMatrix(m);

        // read bounds
        float bounds[] = {0, 0, 0, 0, 0, 0};
        pElem->Attribute(FiberBundleReader::XML_SIZE_X, &temp);
        bounds[1] = temp;
        pElem->Attribute(FiberBundleReader::XML_SIZE_Y, &temp);
        bounds[3] = temp;
        pElem->Attribute(FiberBundleReader::XML_SIZE_Z, &temp);
        bounds[5] = temp;
        geometry->SetFloatBounds(bounds);

        // read bounds
        float bounds2[] = {0, 0, 0};
        bounds2[0] = bounds[1];
        bounds2[1] = bounds[3];
        bounds2[2] = bounds[5];
        m_OutputCache->SetBounds(bounds2);

        geometry->SetImageGeometry(true);
        m_OutputCache->SetGeometry(geometry);

        // generate tract container
        ContainerType::Pointer tractContainer = ContainerType::New();

        int fiberID = 0;
        pElem = hRoot.FirstChildElement(FiberBundleReader::XML_FIBER_BUNDLE).FirstChild().Element();
        for( pElem; pElem; pElem=pElem->NextSiblingElement())
        {
          TiXmlElement* pElem2 = pElem->FirstChildElement();
          ContainerTractType::Pointer tract = ContainerTractType::New();
          for( pElem2; pElem2; pElem2=pElem2->NextSiblingElement())
          {
            ContainerPointType point;
            pElem2->Attribute(FiberBundleReader::XML_POS_X, &temp);
            point[0] = temp;
            pElem2->Attribute(FiberBundleReader::XML_POS_Y, &temp);
            point[1] = temp;
            pElem2->Attribute(FiberBundleReader::XML_POS_Z, &temp);
            point[2] = temp;

            tract->InsertElement(tract->Size(), point);

          }
          pElem->Attribute(FiberBundleReader::XML_ID, &fiberID);
          tractContainer->CreateIndex(fiberID);
          tractContainer->SetElement(fiberID, tract);

        }
        m_OutputCache->addTractContainer(tractContainer);
        m_OutputCache->initFiberGroup();

        MITK_INFO << "Fiber bundle read";
      }
      catch(...)
      {
        MITK_INFO << "Could not read file ";
      }
    }
    else if (ext == ".vfib")
    {
      // generate tract container
      ContainerType::Pointer tractContainer = ContainerType::New();
      mitk::Geometry3D::Pointer geometry = mitk::Geometry3D::New();

      ///We create a Generic Reader to test de .vtk/
      vtkDataReader *chooser=vtkDataReader::New();
      chooser->SetFileName(m_FileName.c_str() );
      if( chooser->IsFilePolyData())
      {
        vtkPolyDataReader *reader = vtkPolyDataReader::New();
        reader->SetFileName( m_FileName.c_str() );
        reader->Update();

        if ( reader->GetOutput() != NULL )
        {
          vtkPolyData* output = reader->GetOutput();
          output->ComputeBounds();
          double bounds[3];
          output->GetBounds(bounds);
          double center[3];
          output->GetCenter(center);
          Point3D origin;
          origin.SetElement(0, -center[0]);
          origin.SetElement(1, -center[1]);
          origin.SetElement(2, -center[2]);
          MITK_INFO << origin;

          mitk::Surface::Pointer surf = mitk::Surface::New();
          surf->SetVtkPolyData(output);
          mitk::Geometry3D* geom  = surf->GetGeometry();
          //geom->SetOrigin(origin);
          geom->SetImageGeometry(true);
          m_OutputCache->SetBounds(bounds);
          m_OutputCache->SetGeometry(geom);
          vtkCellArray* cells = output->GetLines();

          cells->InitTraversal();

          for (int i=0; i<output->GetNumberOfCells(); i++)
          {
            ContainerTractType::Pointer tract = ContainerTractType::New();
            vtkCell* cell = output->GetCell(i);
            int p = cell->GetNumberOfPoints();
            vtkPoints* points = cell->GetPoints();
            for (int j=0; j<p; j++)
            {
              double p[3];
              points->GetPoint(j, p);
              ContainerPointType point;
              point[0] = p[0];
              point[1] = p[1];
              point[2] = p[2];
              tract->InsertElement(tract->Size(), point);
            }
            tractContainer->InsertElement(i, tract);
          }
        }
        reader->Delete();
      }
      chooser->Delete();

      m_OutputCache->addTractContainer(tractContainer);
      m_OutputCache->initFiberGroup();
      MITK_INFO << "Fiber bundle read";
    }
  }