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);
      }
    }
  }
Ejemplo 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";
  }