void mitk::PlanesPerpendicularToLinesFilter::CreatePlane(const mitk::Point3D& curr)
{
  int j;
  for(j=0;j<3;++j)
    normal[j] = last[j]-curr[j]; //@todo globally define normal direction of display xxx
  normal.normalize();

  down = vnl_cross_3d(normal, targetRight);
  down.normalize();
  right = vnl_cross_3d(down, normal);
  right.normalize();

  itk2vtk(last.GetVnlVector()-right*halfWidthInMM-down*halfHeightInMM, origin);
  right  *= targetSpacing[0];
  down   *= targetSpacing[1];
  normal *= targetSpacing[2];

  mitk::Matrix3D matrix;
  matrix.GetVnlMatrix().set_column(0, right);
  matrix.GetVnlMatrix().set_column(1, down);
  matrix.GetVnlMatrix().set_column(2, normal);

  PlaneGeometry::Pointer plane = PlaneGeometry::New();
  plane->GetIndexToWorldTransform()->SetMatrix(matrix);
  plane->SetOrigin(origin);
  plane->SetBounds(bounds);

  planes.push_back(plane);

  last = curr;
}
void mitk::PlanesPerpendicularToLinesFilter::GenerateData()
{
  mitk::Mesh::ConstPointer input  = this->GetInput();
  mitk::GeometryData::Pointer output = this->GetOutput();

  if(m_Plane.IsNotNull())
  {
    targetRight   = m_Plane->GetMatrixColumn(0);
    targetSpacing = m_Plane->GetSpacing();
    bounds = m_Plane->GetBoundingBox()->GetBounds();
    halfWidthInMM  = m_Plane->GetExtentInMM(0)*0.5;
    halfHeightInMM = m_Plane->GetExtentInMM(1)*0.5;
  }
  else
  {
    FillVector3D(targetRight, 1.0, 0.0, 0.0);
    targetSpacing.Fill(1.0);
    halfWidthInMM=halfHeightInMM=100.0;
    ScalarType stdBounds[6] = {0.0, 2.0*halfWidthInMM, 0.0, 2.0*halfHeightInMM, 0.0, 0.0};
    bounds = stdBounds;
  }

  if(m_UseAllPoints==false)
  {
    int i, size;
    //iterate through all cells and build planes
    Mesh::ConstCellIterator cellIt, cellEnd;
    cellEnd = input->GetMesh()->GetCells()->End();
    for( cellIt = input->GetMesh()->GetCells()->Begin(); cellIt != cellEnd; ++cellIt )
    {
      Mesh::CellType& cell = *cellIt->Value();

      Mesh::PointIdIterator ptIt, ptEnd;
      ptEnd = cell.PointIdsEnd();

      size=cell.GetNumberOfPoints();
      if(size<=1)
        continue;

      ptIt = cell.PointIdsBegin();
      last = input->GetPoint(*ptIt);
      ++ptIt;
      for(i=1;i<size;++i, ++ptIt)
      {
        CreatePlane(input->GetPoint(*ptIt));
      }
    }
  }
  else //m_UseAllPoints==true
  {
    //iterate through all points and build planes
    mitk::PointSet::PointsConstIterator it, pend = input->GetPointSet()->GetPoints()->End();
    it=input->GetPointSet()->GetPoints()->Begin();
    last = it.Value();
    ++it;
    for(;it!=pend;++it)
    {
        CreatePlane(it.Value());
    }
  }

  if(planes.size()>0)
  {
    //initialize sliced-geometry for the number of created planes
    m_CreatedGeometries->InitializeSlicedGeometry(planes.size()+1);

    //set last plane at last point with same normal as the one before the last
    PlaneGeometry::Pointer plane = static_cast<PlaneGeometry*>((*planes.rbegin())->Clone().GetPointer());
    itk2vtk(last.GetVnlVector()-right*halfWidthInMM-down*halfHeightInMM, origin);
    plane->SetOrigin(origin);
    m_CreatedGeometries->SetGeometry2D(plane, planes.size());

    //add all planes to sliced-geometry
    int s;
    for(s=0; planes.empty()==false; planes.pop_front(), ++s)
    {
      m_CreatedGeometries->SetGeometry2D(planes.front(), s);
    }

    m_CreatedGeometries->SetEvenlySpaced(false);

    if(m_FrameGeometry.IsNotNull())
    {
      m_CreatedGeometries->SetIndexToWorldTransform(m_FrameGeometry->GetIndexToWorldTransform());
      m_CreatedGeometries->SetBounds(m_FrameGeometry->GetBounds());
      m_CreatedGeometries->SetReferenceGeometry(m_FrameGeometry);
    }
  }

  output->SetGeometry(m_CreatedGeometries);
}