# vtkCelllocator slow in the version VTK 9 than VTK 8

**URL:** https://discourse.vtk.org/t/vtkcelllocator-slow-in-the-version-vtk-9-than-vtk-8/5068
**Category:** Support
**Created:** [January 27, 2021, 8:59am UTC](https://discourse.vtk.org/t/vtkcelllocator-slow-in-the-version-vtk-9-than-vtk-8/5068 "2021-01-27T08:59:36Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Ahmed](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@Ahmed](https://discourse.vtk.org/u/Ahmed)
#### Post date: [January 27, 2021, 8:59am UTC](https://discourse.vtk.org/t/vtkcelllocator-slow-in-the-version-vtk-9-than-vtk-8/5068/1 "2021-01-27T08:59:36Z")

</div>

Hi,

I compare the performance of vtkCelllocator in vtk 9 compared to the VTK 8, I use FindClosestPoint method

I find that the VTK 9 is 2x more slow than VTK 8

Did some people identify the same problem ? What is the solution ?

Thks

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [January 27, 2021, 9:05am UTC](https://discourse.vtk.org/t/vtkcelllocator-slow-in-the-version-vtk-9-than-vtk-8/5068/2 "2021-01-27T09:05:17Z")

</div>

Can you compare against the vtkStaticCellLocator ?

---

<div class="post-metadata">

### Author: ![Ahmed](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@Ahmed](https://discourse.vtk.org/u/Ahmed)
#### Post date: [January 27, 2021, 9:10am UTC](https://discourse.vtk.org/t/vtkcelllocator-slow-in-the-version-vtk-9-than-vtk-8/5068/3 "2021-01-27T09:10:18Z")

</div>

Yes, I tried it and I confirm that the vtkStaticCellLocator is very speed thant vtkCelllocator

But vtkStaticCellLocator not give the same result, see this topic and not solution is given  
[https://gitlab.kitware.com/vtk/vtk/-/issues/17911](https://gitlab.kitware.com/vtk/vtk/-/issues/17911)

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [January 27, 2021, 9:17am UTC](https://discourse.vtk.org/t/vtkcelllocator-slow-in-the-version-vtk-9-than-vtk-8/5068/4 "2021-01-27T09:17:32Z")

</div>

FYI @will.schroeder @Charles_Gueunet

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [January 27, 2021, 11:06am UTC](https://discourse.vtk.org/t/vtkcelllocator-slow-in-the-version-vtk-9-than-vtk-8/5068/5 "2021-01-27T11:06:02Z")

</div>

I’ll look into this if you provide data and a test please. Note the cell locators can and will give different results if there are two or more equivalent results, e.g., given a query point, two cells are within the exact same distance.

---

<div class="post-metadata">

### Author: ![Ahmed](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@Ahmed](https://discourse.vtk.org/u/Ahmed)
#### Post date: [January 27, 2021, 1:16pm UTC](https://discourse.vtk.org/t/vtkcelllocator-slow-in-the-version-vtk-9-than-vtk-8/5068/6 "2021-01-27T13:16:49Z")

</div>

I re-use the case shared in the [https://gitlab.kitware.com/vtk/vtk/-/issues/17911](https://gitlab.kitware.com/vtk/vtk/-/issues/17911) topic

- Bunny mesh: [https://drive.google.com/file/d/1nL8FqaOAN8U8QrhnZFo6-p4-cxI1Ek8a/view?usp=sharing](https://drive.google.com/file/d/1nL8FqaOAN8U8QrhnZFo6-p4-cxI1Ek8a/view?usp=sharing)
- Sphere mesh: [https://drive.google.com/file/d/1Yh6cZcX0CYgYo2KhJIGJ4LMrQOOFi9gd/view?usp=sharing](https://drive.google.com/file/d/1Yh6cZcX0CYgYo2KhJIGJ4LMrQOOFi9gd/view?usp=sharing)

I re-adapt the code, and the sample must be called with Bunny.stl Sphere.stl in this order

VTK 8: Elapsed time (ms) = 63523  
VTK 9: Elapsed time (ms) = 84815  
35% more in VTK 9 than VTK 8.  
My case with 2x time is more complex and I can’t share the data. But this example show this problem.

```
#include <iostream>
#include <string>
#include <chrono>
#include <vtkSTLReader.h>
#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkGenericCell.h>
#include <vtkCellLocator.h>

vtkSmartPointer<vtkPolyData> readSurfaceMeshStl(const std::string& filename)
{
   // Create STL reader
   const auto reader = vtkSmartPointer<vtkSTLReader>::New();
   reader->SetFileName(filename.c_str());
   reader->Update();

   // Get read surface mesh
   vtkSmartPointer<vtkPolyData> surfaceMesh = reader->GetOutput();

   return surfaceMesh;
}

int main(int argc, char** argv)
{
   auto start = std::chrono::system_clock::now();

   if (argc != 3)
   {
      cout << "Usage: ./testCellLocator interiorMeshStl boundingMeshStl" << endl;
      return EXIT_FAILURE;
   }

   std::string interiorMeshFileName = argv[1]; 
   std::string boundingMeshFileName = argv[2];

   std::cout << "Reading interior mesh...\n";
   vtkSmartPointer<vtkPolyData> interiorMesh = readSurfaceMeshStl(interiorMeshFileName);
   std::cout << "\nReading bounding mesh...\n";
   vtkSmartPointer<vtkPolyData> boundingMesh = readSurfaceMeshStl(boundingMeshFileName);

   const vtkIdType interiorMeshNumberOfPoints = interiorMesh->GetNumberOfPoints();

   // Octree
   auto boundingMeshLocator = vtkSmartPointer<vtkCellLocator>::New();
   boundingMeshLocator->SetDataSet(boundingMesh);
   boundingMeshLocator->BuildLocator();

   // Loop variables
   auto boundingMeshClosestCell = vtkSmartPointer<vtkGenericCell>::New();

   ///////////////////////////////////////////////////////////
   // Iterate through every vertex in the interior mesh
   ///////////////////////////////////////////////////////////
   double interiorMeshVertex[3];
   double closestBoundingMeshPoint[3];
   const int idMax(10);
   for (vtkIdType idx = 0; idx < idMax; ++idx)
   {
      for (vtkIdType i = 0; i < interiorMeshNumberOfPoints; ++i)
      {
         // Get point
         interiorMesh->GetPoint(i, interiorMeshVertex);

         // Find closest bounding mesh point
         vtkIdType boundingMeshClosestCellId = -1;
         int subId = -1;
         double dist = -1;
         boundingMeshLocator->FindClosestPoint(interiorMeshVertex, closestBoundingMeshPoint, boundingMeshClosestCell,
            boundingMeshClosestCellId, subId, dist);
      }
   }
   const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start);
   std::cout << "\nNbr of iterations = " << idMax << "\n";
   std::cout << "\nElapsed time (ms) = " << elapsed.count() << "\n";

   return EXIT_SUCCESS;
}
```

---

<div class="post-metadata">

### Author: ![Ahmed](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@Ahmed](https://discourse.vtk.org/u/Ahmed)
#### Post date: [February 3, 2021, 1:41pm UTC](https://discourse.vtk.org/t/vtkcelllocator-slow-in-the-version-vtk-9-than-vtk-8/5068/7 "2021-02-03T13:41:04Z")

</div>

@Charles_Gueunet the sample help you to analyze the problem ?
