# VTK Enclosed points. Wrong results

**URL:** https://discourse.vtk.org/t/vtk-enclosed-points-wrong-results/5231
**Category:** Support
**Created:** [February 22, 2021, 8:29pm UTC](https://discourse.vtk.org/t/vtk-enclosed-points-wrong-results/5231 "2021-02-22T20:29:24Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![IVarha](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/i/9e8a1a/32.png) [@IVarha](https://discourse.vtk.org/u/IVarha)
#### Post date: [February 22, 2021, 8:29pm UTC](https://discourse.vtk.org/t/vtk-enclosed-points-wrong-results/5231/1 "2021-02-22T20:29:24Z")

</div>

Dear VTK community, I am trying check which points of my data located inside of the mesh. I use VTKEnclosedPoints to perform this check however it produce wrong results for my data (All points are off the dataset while visually a lot of points are inside  
I attach all my points and mesh.

```
auto poly_data = vtkSmartPointer<vtkPolyData>::New();
poly_data->SetPoints(pts);
auto encl_points = vtkSmartPointer<vtkSelectEnclosedPoints>::New();

encl_points->SetInputData(poly_data);
encl_points->SetSurfaceData(this->mesh->getMesh());
encl_points->Update();

```

[31.obj](https://discourse.vtk.org/uploads/short-url/iVVEM8HFbuNS79csII99Vt91SGD.obj) (16.0 KB) [points.fcsv](https://discourse.vtk.org/uploads/short-url/qKQBmK232FgbS0CnLt473FqWrp9.fcsv) (116.0 KB) ![Screenshot 2021-02-22 212622](https://discourse.vtk.org/uploads/default/original/2X/5/58f11b292204944d18e6f97c3db4c074a05c5763.png)

---

<div class="post-metadata">

### Author: ![Andrew](https://discourse.vtk.org/user_avatar/discourse.vtk.org/andrew/32/3861_2.png) [@Andrew](https://discourse.vtk.org/u/Andrew)
#### Post date: [February 22, 2021, 9:31pm UTC](https://discourse.vtk.org/t/vtk-enclosed-points-wrong-results/5231/2 "2021-02-22T21:31:05Z")

</div>

Can you run vtkCleanPolyData on your data before doing vtkSelectEnclosedPoints?

---

<div class="post-metadata">

### Author: ![Andrew](https://discourse.vtk.org/user_avatar/discourse.vtk.org/andrew/32/3861_2.png) [@Andrew](https://discourse.vtk.org/u/Andrew)
#### Post date: [February 22, 2021, 9:44pm UTC](https://discourse.vtk.org/t/vtk-enclosed-points-wrong-results/5231/3 "2021-02-22T21:44:32Z")

</div>

Also for a regular grid its often faster to use a stencil which will scanline them into a binary mask.

```
// Allocate a new white image
vtkSmartPointer<vtkImageData> whiteImage = vtkSmartPointer<vtkImageData>::New();
whiteImage->SetSpacing(spacing);
whiteImage->SetExtent(extent);
whiteImage->SetOrigin(origin);
whiteImage->AllocateScalars(VTK_FLOAT, 1);
int* dim = whiteImage->GetDimensions();
std::fill_n(static_cast<float*>(whiteImage->GetScalarPointer()), dim[0] * dim[1] * dim[2], 1.0f);

vtkSmartPointer<vtkPolyDataToImageStencil> poly2Stencil = vtkSmartPointer<vtkPolyDataToImageStencil>::New();
poly2Stencil->SetInputData(polyToRasterize);
poly2Stencil->SetOutputOrigin(origin);
poly2Stencil->SetOutputSpacing(spacing);
poly2Stencil->SetOutputWholeExtent(extent);
poly2Stencil->Update();
vtkSmartPointer<vtkImageStencil> imgStencil = vtkSmartPointer<vtkImageStencil>::New();
imgStencil->SetInputData(whiteImage);
imgStencil->SetStencilData(poly2Stencil->GetOutput());
imgStencil->ReverseStencilOff();
imgStencil->SetBackgroundValue(0.0);
imgStencil->Update();
```

---

<div class="post-metadata">

### Author: ![IVarha](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/i/9e8a1a/32.png) [@IVarha](https://discourse.vtk.org/u/IVarha)
#### Post date: [February 22, 2021, 9:51pm UTC](https://discourse.vtk.org/t/vtk-enclosed-points-wrong-results/5231/4 "2021-02-22T21:51:01Z")

</div>

I am totally sure that all vertices of the mesh are connected to faces because I never change faces and modifying only points.

---

<div class="post-metadata">

### Author: ![IVarha](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/i/9e8a1a/32.png) [@IVarha](https://discourse.vtk.org/u/IVarha)
#### Post date: [February 22, 2021, 10:08pm UTC](https://discourse.vtk.org/t/vtk-enclosed-points-wrong-results/5231/5 "2021-02-22T22:08:58Z")

</div>

It seems like the problem occurs because I modify points of mesh on each iteration

```
for (int i = 0;i< this->points->GetNumberOfPoints();i++){
    this->points->SetPoint(i,set_of_points[3*i],set_of_points[3*i +1],set_of_points[3*i +2]);
}
this->mesh->Initialize();
this->mesh->SetPoints(this->points);
this->mesh->SetPolys(this->triangles);
```
