00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "mitkInterpolateLinesFilter.h"
00019 #include "mitkMesh.h"
00020 #include "mitkSurface.h"
00021
00022 #include <vtkPolyData.h>
00023 #include <vtkPoints.h>
00024 #include <vtkCellArray.h>
00025 #include <vtkCardinalSpline.h>
00026
00027 #include <vector>
00028
00029
00030 mitk::InterpolateLinesFilter::InterpolateLinesFilter()
00031 : m_SplineResolution(10), m_GeometryForInterpolation(NULL), m_Length(0.0)
00032 {
00033 m_SpX=vtkCardinalSpline::New();
00034 m_SpY=vtkCardinalSpline::New();
00035 m_SpZ=vtkCardinalSpline::New();
00036
00037 }
00038
00039 mitk::InterpolateLinesFilter::~InterpolateLinesFilter()
00040 {
00041 m_SpX->Delete();
00042 m_SpY->Delete();
00043 m_SpZ->Delete();
00044 }
00045
00046 void mitk::InterpolateLinesFilter::GenerateOutputInformation()
00047 {
00048 mitk::Mesh::ConstPointer input = this->GetInput();
00049 mitk::Surface::Pointer output = this->GetOutput();
00050
00051 itkDebugMacro(<<"GenerateOutputInformation()");
00052
00053 if(input.IsNull()) return;
00054
00055 if(m_GeometryForInterpolation.IsNotNull())
00056 output->SetGeometry(static_cast<Geometry3D*>(m_GeometryForInterpolation->Clone().GetPointer()));
00057 else
00058 output->SetGeometry(static_cast<Geometry3D*>(input->GetGeometry()->Clone().GetPointer()));
00059 }
00060
00061 void mitk::InterpolateLinesFilter::GenerateData()
00062 {
00063 mitk::Mesh::ConstPointer input = this->GetInput();
00064 mitk::Surface::Pointer output = this->GetOutput();
00065
00066 vtkPolyData *polyData = vtkPolyData::New();
00067 vtkPoints *points = vtkPoints::New();
00068 vtkCellArray *cellarray = vtkCellArray::New();
00069
00070 mitk::Mesh::PointType thisPoint;
00071
00072 m_Length = 0.0;
00073
00074
00075 Mesh::ConstCellIterator cellIt, cellEnd;
00076 cellEnd = input->GetMesh()->GetCells()->End();
00077 for( cellIt = input->GetMesh()->GetCells()->Begin(); cellIt != cellEnd; ++cellIt )
00078 {
00079 if(((*cellIt->Value()).GetType()==mitk::Mesh::CellType::POLYGON_CELL)
00080 && ((*cellIt->Value()).GetNumberOfPoints()>=2))
00081 BuildPointAndVectorList(*cellIt->Value(), points, cellarray);
00082 }
00083
00084 polyData->SetPoints( points );
00085 points->Delete();
00086 polyData->SetLines( cellarray );
00087 cellarray->Delete();
00088
00089 output->SetVtkPolyData(polyData);
00090
00091 polyData->Delete();
00092 }
00093
00094 void mitk::InterpolateLinesFilter::BuildPointAndVectorList(mitk::Mesh::CellType& cell, vtkPoints* points, vtkCellArray* cellarray)
00095 {
00096 const mitk::Mesh* input = GetInput();
00097
00098 Mesh::PointIdIterator ptIt;
00099 Mesh::PointIdIterator ptEnd;
00100
00101 ptEnd = cell.PointIdsEnd();
00102
00103 Point3D pt;
00104
00105 int i, size=cell.GetNumberOfPoints();
00106
00107 int closed_loop_pre_load=0;
00108 if(m_GeometryForInterpolation.IsNull())
00109 {
00110
00111 ptIt = ptEnd; ptIt-=closed_loop_pre_load+1;
00112 for(i=0;i<closed_loop_pre_load;++i, ++ptIt)
00113 {
00114 pt = input->GetPoint(*ptIt);
00115 m_SpX->AddPoint(i, pt[0]); m_SpY->AddPoint(i, pt[1]); m_SpZ->AddPoint(i, pt[2]);
00116 }
00117
00118 for(ptIt = cell.PointIdsBegin();i<size+closed_loop_pre_load;++i, ++ptIt)
00119 {
00120 pt = input->GetPoint(*ptIt);
00121 m_SpX->AddPoint(i, pt[0]); m_SpY->AddPoint(i, pt[1]); m_SpZ->AddPoint(i, pt[2]);
00122 }
00123
00124 int j;
00125 for(j=0,ptIt = cell.PointIdsBegin();j<closed_loop_pre_load;++j,++i, ++ptIt)
00126 {
00127 pt = input->GetPoint(*ptIt);
00128 m_SpX->AddPoint(i, pt[0]); m_SpY->AddPoint(i, pt[1]); m_SpZ->AddPoint(i, pt[2]);
00129 }
00130
00131 Point3D lastPt, firstPt;
00132 Vector3D vec;
00133 float t, step=1.0f/m_SplineResolution;
00134 size=(size-1)*m_SplineResolution;
00135 i=closed_loop_pre_load;
00136
00137 cellarray->InsertNextCell(size);
00138 for(t=closed_loop_pre_load;i<size+closed_loop_pre_load;++i, t+=step)
00139 {
00140 float pt[3];
00141 FillVector3D(pt, m_SpX->Evaluate(t), m_SpY->Evaluate(t), m_SpZ->Evaluate(t));
00142 cellarray->InsertCellPoint(points->InsertNextPoint(pt));
00143 }
00144 }
00145 else
00146 {
00147 Point2D pt2d;
00148
00149 ptIt = ptEnd; ptIt-=closed_loop_pre_load+1;
00150 for(i=0;i<closed_loop_pre_load;++i, ++ptIt)
00151 {
00152 m_GeometryForInterpolation->Map(input->GetPoint(*ptIt), pt2d);
00153 m_SpX->AddPoint(i, pt2d[0]); m_SpY->AddPoint(i, pt2d[1]);
00154 }
00155
00156 for(ptIt = cell.PointIdsBegin();i<size+closed_loop_pre_load;++i, ++ptIt)
00157 {
00158 m_GeometryForInterpolation->Map(input->GetPoint(*ptIt), pt2d);
00159 m_SpX->AddPoint(i, pt2d[0]); m_SpY->AddPoint(i, pt2d[1]);
00160 }
00161
00162 int j;
00163 for(j=0,ptIt = cell.PointIdsBegin();j<closed_loop_pre_load;++j,++i, ++ptIt)
00164 {
00165 m_GeometryForInterpolation->Map(input->GetPoint(*ptIt), pt2d);
00166 m_SpX->AddPoint(i, pt2d[0]); m_SpY->AddPoint(i, pt2d[1]);
00167 }
00168 bool first = true;
00169 Point3D lastPt; lastPt.Fill(0);
00170 Vector3D vec;
00171 float t, step=1.0f/m_SplineResolution;
00172 size=(size-1)*m_SplineResolution;
00173 i=closed_loop_pre_load;
00174
00175 cellarray->InsertNextCell(size);
00176 for(t=closed_loop_pre_load;i<size+closed_loop_pre_load;++i, t+=step)
00177 {
00178 pt2d[0] = m_SpX->Evaluate(t); pt2d[1] = m_SpY->Evaluate(t);
00179 m_GeometryForInterpolation->Map(pt2d, pt);
00180
00181 if(first==false)
00182 {
00183 vec=pt-lastPt;
00184 m_Length+=vec.GetNorm();
00185 }
00186 first=false;
00187
00188 float pvtk[3];
00189 itk2vtk(pt, pvtk);
00190 cellarray->InsertCellPoint(points->InsertNextPoint(pvtk));
00191 lastPt = pt;
00192 }
00193 }
00194 }
00195
00196 const mitk::Mesh *mitk::InterpolateLinesFilter::GetInput(void)
00197 {
00198 if (this->GetNumberOfInputs() < 1)
00199 {
00200 return 0;
00201 }
00202
00203 return static_cast<const mitk::Mesh * >
00204 (this->ProcessObject::GetInput(0) );
00205 }
00206
00207 void mitk::InterpolateLinesFilter::SetInput(const mitk::Mesh *input)
00208 {
00209
00210 this->ProcessObject::SetNthInput(0,
00211 const_cast< mitk::Mesh * >( input ) );
00212 }