marcomusy / vedo

A python module for scientific analysis of 3D data based on VTK and Numpy
https://vedo.embl.es
MIT License
2.05k stars 266 forks source link

intersectWith do not work #734

Open zhang-qiang-github opened 2 years ago

zhang-qiang-github commented 2 years ago

The code to reproduce my problem is:

from vedo import *

tube = Tube([[0, 0, 0], [10, 0, 0]], r=4, res=50).triangulate().alpha(0.3)

polydata = vtk.vtkPolyData()
vps = vtk.vtkPoints()
polygon = vtk.vtkPolygon()
polygon.GetPointIds().SetNumberOfIds(3)
vps.InsertNextPoint(0, 0, 0)
polygon.GetPointIds().SetId(0, 0)
vps.InsertNextPoint(10, 0, 0)
polygon.GetPointIds().SetId(1, 1)
vps.InsertNextPoint(10, 10, 0)
polygon.GetPointIds().SetId(2, 2)

polygons = vtk.vtkCellArray()
polygons.InsertNextCell(polygon)
polydata.SetPoints(vps)
polydata.SetPolys(polygons)
p = Mesh(polydata).triangulate().alpha(0.3)
contour = tube.intersectWith(p).lw(10).c('k')
show(tube, p, contour)

vps.InsertNextPoint(10, 10, 0)
polygon.GetPointIds().SetId(2, 2)

polygons = vtk.vtkCellArray()
polygons.InsertNextCell(polygon)
polydata.SetPoints(vps)
polydata.SetPolys(polygons)
p = Mesh(polydata).triangulate()
contour = tube.intersectWith(p)
show(tube, p, contour)

When I run it, a bug is reported:

Process finished with exit code -1073741819 (0xC0000005)

But, if I modify tube = Tube([[0, 0, 0], [10, 0, 0]], r=4, res=50).triangulate().alpha(0.3) to tube = Tube([[0, 0, 0], [10, 0, 0]], r=4, res=49).triangulate().alpha(0.3), everything is OK:

image

Only res is modified.

I think this bug may be caused by vtk.

Could you please have a look at it?

marcomusy commented 2 years ago

I think this is indeed some problem from upstream vtk, I remember this showed up already in some other context.. although i'm not getting the error with this:

from vedo import *
tube = Tube([[0,0,0], [10,0,0]], r=4, res=50).triangulate().alpha(0.3)
tri = Mesh([[(0,0,0), (10,0,0), (10,10,0)], [(0,1,2)]]).c('r5')
contour = tube.intersect_with(tri).lw(10).c('k')
show(tube, tri, contour, axes=1)

in

vedo 2022.4.1.dev7 | vtk 9.2.2 | python 3.9
zhang-qiang-github commented 2 years ago

Finally, I can repeat the bug:

        vtkSmartPointer<vtkLineSource> lineSource = vtkSmartPointer<vtkLineSource>::New();
    lineSource->SetPoint1(0, 0, 0);
    lineSource->SetPoint2(10, 0, 0);

    vtkSmartPointer<vtkTubeFilter> tubeFilter = vtkSmartPointer<vtkTubeFilter>::New();
    tubeFilter->SetInputConnection(lineSource->GetOutputPort());
    tubeFilter->SetRadius(4);
    /////////////////////////////////////////////////////////////////////////////
    tubeFilter->SetNumberOfSides(50); // key code
    /////////////////////////////////////////////////////////////////////////////
    tubeFilter->SetCapping(1);
    tubeFilter->Update();

    vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
    vtkSmartPointer<vtkPoints> vps = vtkSmartPointer<vtkPoints>::New();
    vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
    polygon->GetPointIds()->SetNumberOfIds(3);
    vps->InsertNextPoint(0, 0, 0);
    polygon->GetPointIds()->SetId(0, 0);
    vps->InsertNextPoint(10, 0, 0);
    polygon->GetPointIds()->SetId(1, 1);
    vps->InsertNextPoint(10, 10, 0);
    polygon->GetPointIds()->SetId(2, 2);

    vtkSmartPointer<vtkCellArray> polygons = vtkSmartPointer<vtkCellArray>::New();
    polygons->InsertNextCell(polygon);
    polydata->SetPoints(vps);
    polydata->SetPolys(polygons);

    vtkSmartPointer<vtkTriangleFilter> tri1 = vtkSmartPointer<vtkTriangleFilter>::New();
    tri1->SetInputData(tubeFilter->GetOutput());
    tri1->Update();

    vtkSmartPointer<vtkTriangleFilter> tri2 = vtkSmartPointer<vtkTriangleFilter>::New();
    tri2->SetInputData(polydata);
    tri2->Update();

    vtkSmartPointer<vtkIntersectionPolyDataFilter> intersection = vtkSmartPointer<vtkIntersectionPolyDataFilter>::New();
    intersection->SetInputData(0, tri1->GetOutput());
    intersection->SetInputData(1, tri2->GetOutput());
    intersection->Update();

It is caused by vtk.

I don't know whether it is a bug. Or there is something wrong with my bug.

Could you please have a look at this code?

marcomusy commented 2 years ago

Hi @zhang-qiang-github thanks for reporting this, I would suggest to submit this report to the vtk experts (on discourse website) as there is not much to do in the downstream vedo...