<p>In order to translate a vtkCellLocator, you have only to translate a bounding box of root octant. However this bounding box cannot be publicly accessible. So it is necessary to create a new class inheriting from a vtkCellLocator and provide access functions to get and set it directly. The following is a simple example.</p>

<pre><code>#include &lt;vtkTransform.h&gt;
#include &lt;vtkTransformPolyDataFilter.h&gt;
#include &lt;vtkSmartPointer.h&gt;
#include &lt;vtkSphereSource.h&gt;

#include &lt;vtkCellLocator.h&gt;
#include &lt;vtkObjectFactory.h&gt;

class vtkMyCellLocator : public vtkCellLocator
{
public:
  static vtkMyCellLocator *New();

  vtkSetVectorMacro(Bounds,double,6);
  vtkGetVectorMacro(Bounds,double,6);
};
vtkStandardNewMacro(vtkMyCellLocator);

int main(int, char *[])
{
  auto sphereSource = vtkSmartPointer&lt;vtkSphereSource&gt;::New();
  sphereSource-&gt;Update();

  // Create the tree
  auto cellLocator = vtkSmartPointer&lt;vtkMyCellLocator&gt;::New();
  cellLocator-&gt;SetDataSet(sphereSource-&gt;GetOutput());
  cellLocator-&gt;BuildLocator();

  double translate[3] = {10.0, 0, 0};
  auto transform = vtkSmartPointer&lt;vtkTransform&gt;::New();
  transform-&gt;Translate(translate);

  auto transformFilter = vtkSmartPointer&lt;vtkTransformPolyDataFilter&gt;::New();
  transformFilter-&gt;SetTransform(transform);
  transformFilter-&gt;SetInputConnection(sphereSource-&gt;GetOutputPort());
  transformFilter-&gt;Update();
  cellLocator-&gt;SetDataSet(transformFilter-&gt;GetOutput());

  double bounds[6];
  cellLocator-&gt;GetBounds(bounds);
  for (int i = 0; i &lt; 3; ++i)
  {
    bounds[2*i] += translate[i];
    bounds[2*i+1] += translate[i];
  }
  cellLocator-&gt;SetBounds(bounds);

  double testPoint[3] = {2.0, 0.0, 0.0};

  //Find the closest points to TestPoint
  double closestPoint[3];//the coordinates of the closest point will be returned here
  double closestPointDist2; //the squared distance to the closest point will be returned here
  vtkIdType cellId; //the cell id of the cell containing the closest point will be returned here
  int subId; //this is rarely used (in triangle strips only, I believe)
  cellLocator-&gt;FindClosestPoint(testPoint, closestPoint, cellId, subId, closestPointDist2);

  std::cout &lt;&lt; "Coordinates of closest point: " &lt;&lt; closestPoint[0] &lt;&lt; " " &lt;&lt; closestPoint[1] &lt;&lt; " " &lt;&lt; closestPoint[2] &lt;&lt; std::endl;
  std::cout &lt;&lt; "Squared distance to closest point: " &lt;&lt; closestPointDist2 &lt;&lt; std::endl;
  std::cout &lt;&lt; "CellId: " &lt;&lt; cellId &lt;&lt; std::endl;

  return EXIT_SUCCESS;
}
</code></pre>

CC BY-SA 4.0
2018-09-25T00:34:20.640Z
2018-09-25T00:34:20.640Z
K.Yoshimi
<p>I would need to translate a vtkCellLocator, without rebuilding it entirely. I suppose that it should be possible to apply an offset to all the buckets in the octree. Do you know if is it possible to do this (or to apply a translation to any other cell locator structure) with the tools already provided by vtk?</p>

<space-partitioning><vtk>
Translate vtkCellLocator
Federico
# Translate vtkCellLocator

(Asked by: username_0 on 2018-09-21)

I would need to translate a vtkCellLocator, without rebuilding it entirely. I suppose that it should be possible to apply an offset to all the buckets in the octree. Do you know if is it possible to do this (or to apply a translation to any other cell locator structure) with the tools already provided by vtk?

**Tags:** space-partitioning, vtk


---

## 1 answers

### Answer by username_1 on 2018-09-25. Score: 1 | Accepted answer

In order to translate a vtkCellLocator, you have only to translate a bounding box of root octant. However this bounding box cannot be publicly accessible. So it is necessary to create a new class inheriting from a vtkCellLocator and provide access functions to get and set it directly. The following is a simple example.

```
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>

#include <vtkCellLocator.h>
#include <vtkObjectFactory.h>

class vtkMyCellLocator : public vtkCellLocator
{
public:
  static vtkMyCellLocator *New();

  vtkSetVectorMacro(Bounds,double,6);
  vtkGetVectorMacro(Bounds,double,6);
};
vtkStandardNewMacro(vtkMyCellLocator);

int main(int, char *[])
{
  auto sphereSource = vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->Update();

  // Create the tree
  auto cellLocator = vtkSmartPointer<vtkMyCellLocator>::New();
  cellLocator->SetDataSet(sphereSource->GetOutput());
  cellLocator->BuildLocator();

  double translate[3] = {10.0, 0, 0};
  auto transform = vtkSmartPointer<vtkTransform>::New();
  transform->Translate(translate);

  auto transformFilter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
  transformFilter->SetTransform(transform);
  transformFilter->SetInputConnection(sphereSource->GetOutputPort());
  transformFilter->Update();
  cellLocator->SetDataSet(transformFilter->GetOutput());

  double bounds[6];
  cellLocator->GetBounds(bounds);
  for (int i = 0; i < 3; ++i)
  {
    bounds[2*i] += translate[i];
    bounds[2*i+1] += translate[i];
  }
  cellLocator->SetBounds(bounds);

  double testPoint[3] = {2.0, 0.0, 0.0};

  //Find the closest points to TestPoint
  double closestPoint[3];//the coordinates of the closest point will be returned here
  double closestPointDist2; //the squared distance to the closest point will be returned here
  vtkIdType cellId; //the cell id of the cell containing the closest point will be returned here
  int subId; //this is rarely used (in triangle strips only, I believe)
  cellLocator->FindClosestPoint(testPoint, closestPoint, cellId, subId, closestPointDist2);

  std::cout << "Coordinates of closest point: " << closestPoint[0] << " " << closestPoint[1] << " " << closestPoint[2] << std::endl;
  std::cout << "Squared distance to closest point: " << closestPointDist2 << std::endl;
  std::cout << "CellId: " << cellId << std::endl;

  return EXIT_SUCCESS;
}

```


---

