VTK/Examples/Cxx/Filtering/IterativeClosestPointsTransform

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Revision as of 09:59, 12 April 2009 by Daviddoria (talk | contribs)

Jump to: navigation, search
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkSmartPointer.h>
#include <vtkIterativeClosestPointTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkLandmarkTransform.h> //to set type to ridgid body

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
	/*
	This demo moves points that have been shifted .1 in +y back to their target positions.
	*/
	
	vtkIdType pid[1];

	vtkSmartPointer<vtkPoints> SourcePoints = vtkSmartPointer<vtkPoints>::New();
	vtkSmartPointer<vtkCellArray> SourceVertices = vtkSmartPointer<vtkCellArray>::New();
	double SourcePoint1[3] = {1.0, 0.1, 0.0};
	pid[0] = SourcePoints->InsertNextPoint(SourcePoint1);
	SourceVertices->InsertNextCell(1,pid);
	double SourcePoint2[3] = {0.1, 1.1, 0.0};
	pid[0] = SourcePoints->InsertNextPoint(SourcePoint2);
	SourceVertices->InsertNextCell(1,pid);
	double SourcePoint3[3] = {0.0, 0.1, 1.0};
	pid[0] = SourcePoints->InsertNextPoint(SourcePoint3);
	SourceVertices->InsertNextCell(1,pid);
		
	vtkSmartPointer<vtkPolyData> Source = vtkPolyData::New();
	Source->SetPoints(SourcePoints);
	Source->SetVerts(SourceVertices);
	
	
	for(unsigned int i = 0; i < 3; i++)
	{
		double origpoint[3];
		SourcePoints->GetPoint(i, origpoint);
		cout << "Original point: (" << origpoint[0] << ", " << origpoint[1] << ", " << origpoint[2] << ")" << endl;
	}
	
	vtkSmartPointer<vtkPoints> TargetPoints = vtkSmartPointer<vtkPoints>::New();
	vtkSmartPointer<vtkCellArray> TargetVertices = vtkSmartPointer<vtkCellArray>::New();
	double TargetPoint1[3] = {1.0, 0.0, 0.0};
	pid[0] = TargetPoints->InsertNextPoint(TargetPoint1);
	TargetVertices->InsertNextCell(1,pid);
	double TargetPoint2[3] = {0.0, 1.0, 0.0};
	pid[0] = TargetPoints->InsertNextPoint(TargetPoint2);
	TargetVertices->InsertNextCell(1,pid);
	double TargetPoint3[3] = {0.0, 0.0, 1.0};
	pid[0] = TargetPoints->InsertNextPoint(TargetPoint3);
	TargetVertices->InsertNextCell(1,pid);
	
	vtkSmartPointer<vtkPolyData> Target = vtkPolyData::New();
	Target->SetPoints(TargetPoints);
	Target->SetVerts(TargetVertices);
	
	//setup ICP transform
	vtkIterativeClosestPointTransform* icp = vtkIterativeClosestPointTransform::New();
	icp->SetSource(Source);
	icp->SetTarget(Target);
	icp->GetLandmarkTransform()->SetModeToRigidBody();
	//icp->DebugOn();
	icp->SetMaximumNumberOfIterations(20);
	icp->StartByMatchingCentroidsOn();
	icp->Modified();
	icp->Update();

	//transform the source points by the ICP solution
	vtkTransformPolyDataFilter* ICPTransFilter = vtkTransformPolyDataFilter::New();
	ICPTransFilter->SetInput(Source);
	ICPTransFilter->SetTransform(icp);
	ICPTransFilter->Update();
	
	vtkSmartPointer<vtkPolyData> Transformed = ICPTransFilter->GetOutput();
		
	for(unsigned int i = 0; i < 3; i++)
	{
		double transpoint[3];
		Transformed->GetPoint(i, transpoint);
		cout << "Transformed point: (" << transpoint[0] << ", " << transpoint[1] << ", " << transpoint[2] << ")" << endl;
	}
	
	return 0;
}