[vtkusers] Line/triangle intersection
David Doria
daviddoria at gmail.com
Tue Jun 2 07:30:16 EDT 2009
On Mon, Jun 1, 2009 at 12:57 PM, David Doria <daviddoria at gmail.com> wrote:
> 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
>
Update:
I also tried this:
vtkOBBTree* Tree = vtkOBBTree::New();
Tree->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};
vtkPoints* IntersectPoints = vtkPoints::New();
Tree->IntersectWithLine(LineP0, LineP1, IntersectPoints, NULL);
std::cout << "NumPoints: " << IntersectPoints->GetNumberOfPoints() <<
std::endl;
and it just segfaults on the IntersectWithLine call.
Dave
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20090602/2f74bc67/attachment.htm>
More information about the vtkusers
mailing list