Difference between revisions of "VTK/Examples/Cxx/Filtering/IterativeClosestPointsTransform"

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to: navigation, search
(New page: <source lang="cpp"> #include <vtkPoints.h> #include <vtkPolyData.h> #include <vtkCellArray.h> #include <vtkSmartPointer.h> #include <vtkIterativeClosestPointTransform.h> #include <vtkTrans...)
 
(Replaced content with "= '''See [https://lorensen.github.io/VTKExamples/site/Cxx/Filtering/IterativeClosestPointsTransform IterativeClosestPointsTransform] on the new [https://lorensen.github.io...")
 
(24 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<source lang="cpp">
+
= '''See [https://lorensen.github.io/VTKExamples/site/Cxx/Filtering/IterativeClosestPointsTransform IterativeClosestPointsTransform] on the new [https://lorensen.github.io/VTKExamples/site/ VTKExamples website].''' =
#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();
 
 
for(unsigned int i = 0; i < 3; i++)
 
{
 
double transpoint[3];
 
Source->GetPoint(i, transpoint);
 
cout << "Transformed point: (" << transpoint[0] << ", " << transpoint[1] << ", " << transpoint[2] << ")" << endl;
 
}
 
 
return 0;
 
}
 
</source>
 

Latest revision as of 19:39, 15 April 2019

See IterativeClosestPointsTransform on the new VTKExamples website.