Change color of poly inside an imperfect surface.

I’m trying to find the best way of changing color of polydata that has penetrated the surface of an (unperfectly closed) object.

for example, if I have a foam football (with holes in it/imperefectly closed surface), and i were to push a pencil half way into the foam ball. I would like the pencil outside of the ball to be blue, and the pencil the inside of the ball to be red.

I have tried doing this with: https://vtk.org/Wiki/VTK/Examples/Cxx/PolyData/PointInsideObject.

However I do not have a closed surface, and the results are not brilliant.

I am trying to do it with a distance filter, however i do not know exactly how, or how to change the scalars to just red and blue .

The best way I can think fo doing it is somehow using the suface normals of the object, to detect whether a point is inside an object.

My question is, is there a good, prefered way of doing things like/similar this that is relativelty simple?
I’m quite stuck on this at the momemnt, so any help is very much appreciated.

vtkSmartPointer sphereSource =
vtkSmartPointer::New();
sphereSource->SetCenter(0.0, 0.0, 0.0);
sphereSource->SetRadius(1.0f);
sphereSource->Update();
vtkSmartPointer sphereMapper =
vtkSmartPointer::New();
sphereMapper->SetInputConnection( sphereSource->GetOutputPort() );
sphereMapper->ScalarVisibilityOff();
vtkSmartPointer sphereActor =
vtkSmartPointer::New();
sphereActor->SetMapper( sphereMapper );
sphereActor->GetProperty()->SetOpacity(.3);
sphereActor->GetProperty()->SetColor(1,1,1);

vtkSmartPointer implicitPolyDataDistance =
vtkSmartPointer::New();
implicitPolyDataDistance->SetInput(sphereSource->GetOutput());

// Setup a grid
vtkSmartPointer points =
vtkSmartPointer::New();
float step = 0.1;
for(float x = - 2.0; x <= 2.0; x += step)
{
for(float y = - 2.0; y <= 2.0; y += step)
{
for(float z = - 2.0; z <= 2.0; z += step)
{
points->InsertNextPoint(x,y,z);
}
}
}

// Add distances to each point
vtkSmartPointer signedDistances =
vtkSmartPointer::New();
signedDistances->SetNumberOfComponents(1);
signedDistances->SetName(“SignedDistances”);

unsigned char red[3] = { 255, 0, 0 };
unsigned char blue[3] = { 0, 0, 255 };

vtkSmartPointer colors =
vtkSmartPointer::New();
colors->SetNumberOfComponents(3);
colors->SetName(“Colors”);

vtkSmartPointer points2 =
vtkSmartPointer::New();

cout << "get number of points " << points->GetNumberOfPoints() << endl;
// Evaluate the signed distance function at all of the grid points
for(vtkIdType pointId = 0; pointId < points->GetNumberOfPoints(); ++pointId)
{
double p[3];
points->GetPoint(pointId, p);

float signedDistance = implicitPolyDataDistance->EvaluateFunction(p);
//signedDistances->InsertNextValue(signedDistance);
//cout << "signed distance " << signedDistance << endl; 
if (signedDistance < 0)
{

	cout << "point number" << pointId << "   " << signedDistance << endl;
	colors->InsertNextTypedTuple(red);

}
else
{
	colors->InsertNextTypedTuple(blue);
}

}

vtkSmartPointer polyData = vtkSmartPointer::New();
polyData->SetPoints(points);
polyData->GetPointData()->SetScalars(colors);

vtkSmartPointer vertexGlyphFilter =
vtkSmartPointer::New();
vertexGlyphFilter->SetInputData(polyData);
vertexGlyphFilter->Update();

vtkSmartPointer signedDistanceMapper =
vtkSmartPointer::New();
signedDistanceMapper->SetInputConnection(vertexGlyphFilter->GetOutputPort());
signedDistanceMapper->ScalarVisibilityOn();

vtkSmartPointer signedDistanceActor =
vtkSmartPointer::New();
signedDistanceActor->SetMapper( signedDistanceMapper );

vtkSmartPointer renderer =
vtkSmartPointer::New();
renderer->AddViewProp(sphereActor);
renderer->AddViewProp(signedDistanceActor);

vtkSmartPointer renderWindow =
vtkSmartPointer::New();
renderWindow->AddRenderer( renderer );

vtkSmartPointer renWinInteractor =
vtkSmartPointer::New();
renWinInteractor->SetRenderWindow( renderWindow );

renderWindow->Render();
renWinInteractor->Start();