Difference between revisions of "VTK/Examples/Python/IterativeClosestPoints"

From KitwarePublic
< VTK‎ | Examples‎ | Python
Jump to navigationJump to search
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
==IterativeClosestPoints.py==
 
<source lang="python">
 
<source lang="python">
 +
from __future__ import print_function
 
import vtk
 
import vtk
 
from vtk import *
 
from vtk import *
import hybrid
 
from hybrid import *
 
  
 
# ============ create source points ==============
 
# ============ create source points ==============
print "Creating source points..."
+
print("Creating source points...")
 
sourcePoints = vtk.vtkPoints()
 
sourcePoints = vtk.vtkPoints()
 
sourceVertices = vtk.vtkCellArray()
 
sourceVertices = vtk.vtkCellArray()
Line 25: Line 25:
 
source.SetPoints(sourcePoints)
 
source.SetPoints(sourcePoints)
 
source.SetVerts(sourceVertices)
 
source.SetVerts(sourceVertices)
source.Update()
+
if vtk.VTK_MAJOR_VERSION <= 5:
 +
    source.Update()
  
print "Displaying source points..."
+
print("Displaying source points...")
 
# ============ display source points ==============
 
# ============ display source points ==============
 
pointCount = 3
 
pointCount = 3
index = 0
+
for index in range(pointCount):
while index < pointCount:
 
 
     point = [0,0,0]
 
     point = [0,0,0]
 
     sourcePoints.GetPoint(index, point)
 
     sourcePoints.GetPoint(index, point)
     print "source point[%s]=%s" % (index,point)
+
     print("source point[%s]=%s" % (index,point))
    index += 1
 
  
 
#============ create target points ==============
 
#============ create target points ==============
print "Creating target points..."
+
print("Creating target points...")
 
targetPoints = vtk.vtkPoints()
 
targetPoints = vtk.vtkPoints()
 
targetVertices = vtk.vtkCellArray()
 
targetVertices = vtk.vtkCellArray()
Line 57: Line 56:
 
target.SetPoints(targetPoints)
 
target.SetPoints(targetPoints)
 
target.SetVerts(targetVertices)
 
target.SetVerts(targetVertices)
target.Update()
+
if vtk.VTK_MAJOR_VERSION <= 5:
 +
    target.Update()
 +
 
  
 
# ============ display target points ==============
 
# ============ display target points ==============
print "Displaying target points..."
+
print("Displaying target points...")
 
pointCount = 3
 
pointCount = 3
index = 0
+
for index in range(pointCount):
while index < pointCount:
 
 
     point = [0,0,0]
 
     point = [0,0,0]
 
     targetPoints.GetPoint(index, point)
 
     targetPoints.GetPoint(index, point)
     print "target point[%s]=%s" % (index,point)
+
     print("target point[%s]=%s" % (index,point))
    index += 1
 
  
print "Running ICP ----------------"
+
print("Running ICP ----------------")
 
# ============ run ICP ==============
 
# ============ run ICP ==============
 
icp = vtk.vtkIterativeClosestPointTransform()
 
icp = vtk.vtkIterativeClosestPointTransform()
Line 82: Line 81:
  
 
icpTransformFilter = vtk.vtkTransformPolyDataFilter()
 
icpTransformFilter = vtk.vtkTransformPolyDataFilter()
icpTransformFilter.SetInput(source)
+
if vtk.VTK_MAJOR_VERSION <= 5:
 +
    icpTransformFilter.SetInput(source)
 +
else:
 +
    icpTransformFilter.SetInputData(source)
 +
 
 
icpTransformFilter.SetTransform(icp)
 
icpTransformFilter.SetTransform(icp)
 
icpTransformFilter.Update()
 
icpTransformFilter.Update()
Line 90: Line 93:
 
# ============ display transformed points ==============
 
# ============ display transformed points ==============
 
pointCount = 3
 
pointCount = 3
index = 0
+
for index in range(pointCount):
while index < pointCount:
 
 
     point = [0,0,0]
 
     point = [0,0,0]
 
     transformedSource.GetPoint(index, point)
 
     transformedSource.GetPoint(index, point)
     print "xformed source point[%s]=%s" % (index,point)
+
     print("transformed source point[%s]=%s" % (index,point))
    index += 1
 
 
 
 
</source>
 
</source>

Latest revision as of 02:53, 22 August 2015

IterativeClosestPoints.py

from __future__ import print_function
import vtk
from vtk import *

# ============ create source points ==============
print("Creating source points...")
sourcePoints = vtk.vtkPoints()
sourceVertices = vtk.vtkCellArray()

id = sourcePoints.InsertNextPoint(1.0, 0.1, 0.0)
sourceVertices.InsertNextCell(1)
sourceVertices.InsertCellPoint(id)

id = sourcePoints.InsertNextPoint(0.1, 1.1, 0.0)
sourceVertices.InsertNextCell(1)
sourceVertices.InsertCellPoint(id)

id = sourcePoints.InsertNextPoint(0.0, 0.1, 1.0)
sourceVertices.InsertNextCell(1)
sourceVertices.InsertCellPoint(id)

source = vtk.vtkPolyData()
source.SetPoints(sourcePoints)
source.SetVerts(sourceVertices)
if vtk.VTK_MAJOR_VERSION <= 5:
    source.Update()

print("Displaying source points...")
# ============ display source points ==============
pointCount = 3
for index in range(pointCount):
    point = [0,0,0]
    sourcePoints.GetPoint(index, point)
    print("source point[%s]=%s" % (index,point))

#============ create target points ==============
print("Creating target points...")
targetPoints = vtk.vtkPoints()
targetVertices = vtk.vtkCellArray()

id = targetPoints.InsertNextPoint(1.0, 0.0, 0.0)
targetVertices.InsertNextCell(1)
targetVertices.InsertCellPoint(id)

id = targetPoints.InsertNextPoint(0.0, 1.0, 0.0)##
targetVertices.InsertNextCell(1)
targetVertices.InsertCellPoint(id)

id = targetPoints.InsertNextPoint(0.0, 0.0, 1.0)
targetVertices.InsertNextCell(1)
targetVertices.InsertCellPoint(id)

target = vtk.vtkPolyData()
target.SetPoints(targetPoints)
target.SetVerts(targetVertices)
if vtk.VTK_MAJOR_VERSION <= 5:
    target.Update()


# ============ display target points ==============
print("Displaying target points...")
pointCount = 3
for index in range(pointCount):
    point = [0,0,0]
    targetPoints.GetPoint(index, point)
    print("target point[%s]=%s" % (index,point))

print("Running ICP ----------------")
# ============ run ICP ==============
icp = vtk.vtkIterativeClosestPointTransform()
icp.SetSource(source)
icp.SetTarget(target)
icp.GetLandmarkTransform().SetModeToRigidBody()
#icp.DebugOn()
icp.SetMaximumNumberOfIterations(20)
icp.StartByMatchingCentroidsOn()
icp.Modified()
icp.Update()

icpTransformFilter = vtk.vtkTransformPolyDataFilter()
if vtk.VTK_MAJOR_VERSION <= 5:
    icpTransformFilter.SetInput(source)
else:
    icpTransformFilter.SetInputData(source)

icpTransformFilter.SetTransform(icp)
icpTransformFilter.Update()

transformedSource = icpTransformFilter.GetOutput()

# ============ display transformed points ==============
pointCount = 3
for index in range(pointCount):
    point = [0,0,0]
    transformedSource.GetPoint(index, point)
    print("transformed source point[%s]=%s" % (index,point))