[vtkusers] Iterative Closest Point Transformation

Fani Deligianni fd301 at doc.ic.ac.uk
Sat Apr 29 13:31:06 EDT 2006


Hello!

I'm a new user of vtk and I am trying to use the 
vtkIterativeClosestPointTransform class to rigidly register one cloud of 
3D points on an other. The ICPT doesn't function. It returns zero 
iterations and zero MeanDistance and the transformation matrix is 
Identity. I followed some indications from previous emails posted on the 
list but I cannot find what is wrong.
It seems to me that this wouldn't be a big problem for someone that 
knows vtk well.

I use the vtkPolyData class to define the 3D points and their 
connectivity (I don't know if the later is necessary for the ICPT 
algorithm but I got confused whether I need to use the vtkCellLocator or 
not-Anyway in either way it doesn't work).

Any suggestions, please?

F.

--
    //points and connectivity from mesh-structure
    long N1 = left->numvertices;
    long N2 = right->numvertices;

    //Set the source and target point sets
    vtkPoints* source = vtkPoints::New();
    source->SetNumberOfPoints(N1);

    vtkPoints* target = vtkPoints::New();
    target->SetNumberOfPoints(N2);

    float point[3];
    for (long i=0; i<N1; i++)
    {
        point[0]=right->vertices[3*i];
        point[1]=right->vertices[3*i+1];
        point[2]=right->vertices[3*i+2];
        source->SetPoint(i, point);
    }

    for (long i=0; i<N2; i++)
    {
        point[0]=left->vertices[3*i];
        point[1]=left->vertices[3*i+1];
        point[2]=left->vertices[3*i+2];
        target->SetPoint(i, point);
    }

    vtkPolyData *datasetSource = vtkPolyData::New();
    datasetSource->SetPoints(source);
    vtkPolyData *datasetTarget = vtkPolyData::New();
    datasetTarget->SetPoints(target);

    //set connectivity
    vtkCellArray *connectSource = vtkCellArray::New();
    vtkCellArray *connectTarget = vtkCellArray::New();
    long NT1 = right->numtriangles;
    long NT2 = left->numtriangles;
    for( long i=0; i<NT1; i++ )
    {
       
        vtkIdType pts[3] = {right->triangles[i].vindices[0],
                        right->triangles[i].vindices[1],
                        right->triangles[i].vindices[2]};
        connectSource->InsertNextCell( VTK_TRIANGLE, pts );
    }
    datasetSource->SetPolys( connectSource );

    for( long i=0; i<NT2; i++ )
    {
       
        vtkIdType pts[3] = {left->triangles[i].vindices[0],
                        left->triangles[i].vindices[1],
                        left->triangles[i].vindices[2]};
        connectTarget->InsertNextCell( VTK_TRIANGLE, pts );
    }
    datasetTarget->SetPolys( connectTarget );

    //initialise ICPT
    vtkIterativeClosestPointTransform *ICPT = 
vtkIterativeClosestPointTransform::New();
    ICPT->SetSource( datasetTarget );
    ICPT->SetSource( datasetSource );

    //vtkCellLocator *locator = vtkCellLocator::New();
    //locator->SetDataSet(datasetTarget);
    //locator->BuildLocator(); //---->'HERE IT CRASHES WHEN UNCOMMENT'
    //ICPT->SetLocator( locator );

    //set parameters of ICPT
    if( N1>N2 )
        ICPT->SetMaximumNumberOfLandmarks(N2);
    else
        ICPT->SetMaximumNumberOfLandmarks(N1);

    //Set ICPT to rigid mode
    vtkLandmarkTransform* LT = ICPT->GetLandmarkTransform();
    LT->SetModeToRigidBody();

    ICPT->SetMaximumNumberOfIterations(10);
    ICPT->StartByMatchingCentroidsOn();

    ICPT->SetMeanDistanceModeToAbsoluteValue();
    ICPT->Update();

    //check ICPT progress
    double tmp = ICPT->GetMaximumMeanDistance();
    tmp = ICPT->GetMeanDistance();

    int temp = ICPT->GetMaximumNumberOfLandmarks();
    temp = ICPT->GetNumberOfIterations();

    //Get the homogenious transformation matrix
    vtkMatrix4x4* m = vtkMatrix4x4::New();
    LT->GetMatrix(m);

    //Set the rotation matrix
    for (i=0;i<3;i++)
        for (int j=0;j<3;j++)
            R[i][j] = m->GetElement(i,j);

    //Set the translation matrix
    for (i=0;i<3;i++)
        T[i] = m->GetElement(i,3);


    //Clean-up
    source->Delete();
    target->Delete();
    connectSource->Delete();
    connectTarget->Delete();
    m->Delete();
    ICPT->Delete();
    datasetSource->Delete();
    datasetTarget->Delete();




More information about the vtkusers mailing list