[vtkusers] Line/triangle intersection

David Doria daviddoria at gmail.com
Mon Jun 1 12:57:28 EDT 2009


I am trying to intersect a ray with a triangulated mesh. I see that
vtkOBBTree is derived from vtkCellLocator, but they both have a
IntersectWithLine function. When should I use a vtkOBBTree vs a
vtkCellLocator?

I tried to use a CellLocator to find the intersection with a triangle
(vertices [0,0,0], [1,0,0], [0,1,0]) and a line segment (end points [.5, .5,
1], [.5, .5, -1]).

It doesn't seem to give the result of (.5, .5, 0) as I would expect -
instead all the values seem to be uninitialized.

Here is the function declaration:
virtual int IntersectWithLine<http://www.vtk.org/doc/release/4.0/html/classvtkCellLocator.html#a9>(float
a0[3], float a1[3], float tol, float &t, float x[3], float
pcoords[3], int &subId)

I'm assuming a0 and a1 are the endpoints of the line? tol is the tolerance
for the intersection calculation, x is the point of intersection.  Then what
is pcoords and subId??

Here is my example code:

#include <vtkCellArray.h>
#include <vtkPoints.h>
#include <vtkTriangle.h>
#include <vtkPolyData.h>
#include <vtkPointData.h>
#include <vtkLine.h>
#include <vtkCellLocator.h>
#include <vtkSmartPointer.h>

#include <iostream>


int main(int argc, char *argv[])
{
    //create the points
    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    points->InsertNextPoint(1.0, 0.0, 0.0);
    points->InsertNextPoint(0.0, 0.0, 0.0);
    points->InsertNextPoint(0.0, 1.0, 0.0);

    //create the triangles
    vtkSmartPointer<vtkCellArray> triangles =
vtkSmartPointer<vtkCellArray>::New();

    vtkSmartPointer<vtkTriangle> triangle =
vtkSmartPointer<vtkTriangle>::New();
    triangle->GetPointIds()->SetId(0,0);
    triangle->GetPointIds()->SetId(1,1);
    triangle->GetPointIds()->SetId(2,2);
    triangles->InsertNextCell(triangle);

    vtkSmartPointer<vtkPolyData> polydata = vtkPolyData::New();

    polydata->SetPoints(points);
    polydata->SetPolys(triangles);

    //create the locator
    vtkCellLocator* Locator = vtkCellLocator::New();
    Locator->SetDataSet(polydata);

    //intersect the locator with the line
    double LineP0[3] = {0.5, 0.5, 1.0};
    double LineP1[3] = {0.5, 0.5, -1.0};
    double t;
    double x[3];
    double pcoords[3];
    int subId;
    Locator->IntersectWithLine(LineP0, LineP1, 1e-6, t, x, pcoords, subId);

    std::cout << "t: " << t << std::endl;
    std::cout << "subId: " << subId << std::endl;
    std::cout << "x: " << x[0] << " " << x[1] << " " << x[2] << std::endl;
    std::cout << "pcoords: " << pcoords[0] << " " << pcoords[1] << " " <<
pcoords[2] << std::endl;


    return 0;
}


Anyone see what is wrong with that? I assume it is with my missing
understanding of pcoords because it is not passed by reference and therefore
I am passing in uninitialized values - thoughts?

Thanks,

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20090601/3b416a37/attachment.htm>


More information about the vtkusers mailing list