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?<br><br>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]).<br>
<br>It doesn't seem to give the result of (.5, .5, 0) as I would expect - instead all the values seem to be uninitialized.<br><br>Here is the function declaration:<br>virtual int <a class="el" href="http://www.vtk.org/doc/release/4.0/html/classvtkCellLocator.html#a9">IntersectWithLine</a> (float a0[3], float a1[3], float tol, float &t, float x[3], float pcoords[3], int &subId)<br>
<br>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??<br><br>Here is my example code:<br><br>
#include <vtkCellArray.h><br>#include <vtkPoints.h><br>#include <vtkTriangle.h><br>#include <vtkPolyData.h><br>#include <vtkPointData.h><br>#include <vtkLine.h><br>#include <vtkCellLocator.h><br>
#include <vtkSmartPointer.h><br><br>#include <iostream><br><br><br>int main(int argc, char *argv[])<br>{<br> //create the points<br> vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();<br>
points->InsertNextPoint(1.0, 0.0, 0.0);<br> points->InsertNextPoint(0.0, 0.0, 0.0);<br> points->InsertNextPoint(0.0, 1.0, 0.0);<br> <br> //create the triangles<br> vtkSmartPointer<vtkCellArray> triangles = vtkSmartPointer<vtkCellArray>::New();<br>
<br> vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::New();<br> triangle->GetPointIds()->SetId(0,0);<br> triangle->GetPointIds()->SetId(1,1);<br> triangle->GetPointIds()->SetId(2,2);<br>
triangles->InsertNextCell(triangle);<br> <br> vtkSmartPointer<vtkPolyData> polydata = vtkPolyData::New();<br><br> polydata->SetPoints(points);<br> polydata->SetPolys(triangles);<br> <br> //create the locator<br>
vtkCellLocator* Locator = vtkCellLocator::New();<br> Locator->SetDataSet(polydata);<br><br> //intersect the locator with the line<br> double LineP0[3] = {0.5, 0.5, 1.0};<br> double LineP1[3] = {0.5, 0.5, -1.0};<br>
double t;<br> double x[3];<br> double pcoords[3];<br> int subId;<br> Locator->IntersectWithLine(LineP0, LineP1, 1e-6, t, x, pcoords, subId);<br><br> std::cout << "t: " << t << std::endl;<br>
std::cout << "subId: " << subId << std::endl;<br> std::cout << "x: " << x[0] << " " << x[1] << " " << x[2] << std::endl;<br>
std::cout << "pcoords: " << pcoords[0] << " " << pcoords[1] << " " << pcoords[2] << std::endl;<br> <br> <br> return 0;<br>}<br><br><br>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?<br>
<br clear="all">Thanks,<br><br>David<br>