from vedo import *
# Load and cut mouse limb mesh
limb = Mesh("https://vedo.embl.es/examples/data/270_flank.vtk")
limb.alpha(0.3).c("purple9").lighting("off")
# Make a wall in the cut limb to show gradient
grid = Grid(sx=2000, sy=2000, resx=50, resy=50).rotateX(90)
grid.cutWithMesh(limb).wireframe(False)
# scaling things makes no difference
#limb.scale(0.001)
#grid.scale(0.001)
values = []
for p in grid.points():
q = limb.closestPoint(p)
values.append(mag(p-q))
grid.cmap("viridis_r", values).addScalarBar()
show(limb, grid, viewup='z', axes=1)
The range is also very different.
The name of the two classes is literally the only thing I change between the two… Any ideas?
I stumbled upon the same issue, when replacing the vtkCellLocator of vtkImplicitPolyDataDistance with the vtkStaticCellLocator to get some extra performance. Is there a reason why the vtkStaticCellLocator is not used as the default in vtkImplicitPolyDataDistance. My experience is that neither vtkCellLocator or vtkStaticCellLocator are thread-safe. Would it be an idea to introduce thread_local into the hierarchy of class for vtkStaticCellLocator to make it thread-safe? Making it thread-safe would make it possible to use vtkSMPTools are bit more widely, e.g. for the vtkPolyDataDistanceFilter?
Is there a reason why the vtkStaticCellLocator is not used as the default in vtkImplicitPolyDataDistance
Probably because vtkStaticCellLocator was added after vtkImplicitPolyDataDistance, and vtkImplicitPolyDataDistance was never updated.
vtkStaticCellLocator should be thread safe (once built). There is an outstanding issue for a potential race condition that needs tracking down, maybe this is biting you?
Thanks. I get some weird indices but I am also not waiting for the locators to build. I build a separate locator for each thread in the Initialize function using vtkSMPTools. But I guess from the name, vtkStaticCellLocator, this obviously goes wrong.
I will wait for a single locator to build and use it across multiple threads. I am sure this should work.
If I track down and locate a data race, I will ensure to communicate it such that errors are fixed. Thanks for replying
Generally what I’ve done is build the locator prior to the parallel for. Mostly because the locators can chew up a lot of memory, and can take some time to build, so I minimize these by using just the one instance.
The race is reported by Sean who is (rightfully) bugging me
i have just replace vtkCellLocator with vtkStaticCellLocator in vtkImplicitPolyDataDistance, with nothing else.
but the process is as slow as vtkCellLocator,
i`m sure smp is supported since other classes with smp is quite fast
am i missing something? what other change should i take?
thanks
I have replaced vtkCellLocator and introduced vtkSMP. It made it 2 orders of magnitude faster. Below is a snippet from a new vtkDisplacementFilter, where both distance and directions are computed in parallell.
vtkImplicitPolyDataDistance* imp = vtkImplicitPolyDataDistance::New();
imp->SetInput(src);
// Calculate distance from points.
int numPts = mesh->GetNumberOfPoints();
vtkDoubleArray* directionArray = vtkDoubleArray::New();
directionArray->SetName("Direction");
directionArray->SetNumberOfComponents(3);
directionArray->SetNumberOfTuples(numPts);
vtkDoubleArray* distanceArray = vtkDoubleArray::New();
distanceArray->SetName("Distance");
distanceArray->SetNumberOfComponents(1);
distanceArray->SetNumberOfTuples(numPts);
// The sign of the functions are the dot product between pseudo normal and the vector
// x-p. The point p is on the target polydata.
DisplacementOp pointFunctor(this, imp, mesh, distanceArray, directionArray);
vtkSMPTools::For(0, numPts, pointFunctor);
Hi Jens
thanks for your reply, i have changed vtkDistancePolyDataFilter like yours and it works.
though the time is just reduced about 75%(i will continue optimize it).
with vtk 9.1, i have some error result in vtkStaticCellLoacator, i’ll have some check
I got a factor of around 70 but in addition to the commit you mentioned, I replaced the vtkCellLocator with vtkStaticCellLocator. Also remember to enable vtkSMPTools. I usually use TBB.