Collision detection between not polyhedron models

i’ve been following this guide and implemented into my code and it doesnt work.

I have a white and pial file, from freesurfer that are models that represent the white and pial surface of the brain.

I have also multiple cylinder vtk aligned in some random direction that goes through the brain to represent the electrodes implanted into the patient’s brain.

My goal is to detect whenever the electrodes is inside the brain and detecting correctly the brain activities or outside the brain, so ignore anything that electrode is recording.
So from the link provided at the beginning of the thread i’ve built this code

for i in range(0, len(listCylinderModel)):
            for j in range(0, len(listBrain)):
                collisionDetection = vtk.vtkCollisionDetectionFilter()
                collisionDetection.SetInputData(0, listCylinderModel[i].GetPolyData())
                collisionDetection.SetInputData(1, listBrain[j])
                matrix1 = vtk.vtkMatrix4x4()
                collisionDetection.SetMatrix(0, matrix1)
                collisionDetection.SetMatrix(1, matrix1)


            if collisionDetection.GetNumberOfContacts() > 0:
                print("collision detected")

listcylinder are the list of the electrodes.
listbrain are the list of the pial and white surfaces and there are only 4 elements there (left pial, left white, right pial, right white).
So each cycle of the outer for take 1 electrodes and check if it collides with any of the 4 elements inside listbrain. If so, the selected electrodes, change its vtk color and update itself.

But it doesnt work. It detect nothing. I’ve read from the doc here that it detects collision only between two polyhedral surfaces. And if i recall correctly, a polyhedral surfaces it’s a surface where, given two random selected point inside the surface x and y, any points z in the direction of x,y and between x,y has to be inside the surface. so if i have a C shape model, that isn’t a polyhedron.

I tried the collision detection between two cylinder, and because a cylinder is a polyhedral surface, the code works.

But still, from the other post that i linked at the start of this thread, when that user was trying to detect a collision for a orthodontical procedure, the model of the teeth doesn’t seems a polyhedral surface.

So. What can you suggest me to do? I’ve search up again and found this, overlapping cell detector. But i have no idea how it works, and there isn’t any example or anything so far

As far as I know, all FreeSurfer surfaces are triangle meshes (but you can run the meshes through a vtkTriangleFilter to ensure). Since triangle is a polyhedron (the simplest one), the vtkCollisionDetectionFilter should work well.

In your code the issue seems to be that you don’t set matrix1 (you leave the default identity matrix, i.e., not transformed). I would not mess with the tolerance values and number of cells either - the defaults are generally a good choice. Instead, I would recommend to experiment with different CollisionMode choices and inspect all the outputs (cells and points in ContactsOutput, ContactCells, etc.).

If none of these help then you can share an example scene saved as a .mrb file (upload to dropbox/onedrive/etc. and post the link) and provide a script that you expect to work and we’ll have a look.