# Find the intersection of two models

Hi there,

How can i find the points in which two models (a sort of cylinder and a bar) intersect one another?
i’ve tried messing up with numpy trying to search for intersections in data arrays but that doesn’t seem the right way.

Thanks!

Hi there,

A bit convoluted but here are some code examples that might get youon the right track useing the modeltomodel distance extension:

``````vtkOutput = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLModelNode")

parameters = {}
parameters["vtkFile1"] = preprocessedSurface
parameters["vtkFile2"] = modelNode1
parameters['distanceType'] = "absolute_closest_point"
parameters["vtkOutput"] = vtkOutput

cliNode = slicer.cli.runSync(slicer.modules.modeltomodeldistance, None, parameters)

VTKFieldData = vtkOutput.GetMesh().GetAttributesAsFieldData(0)
mesh = vtkOutput.GetMesh()

for j in range(0,VTKFieldData.GetNumberOfArrays()):
#print(VTKFieldData.)

tableStorageNode = resultTableNode.CreateDefaultStorageNode()
tableStorageNode.SetFileName("TEST.csv")
tableStorageNode.WriteData(resultTableNode)

``````

Haven’t tested the code in a while but this should generate a table of the output node, where you can look at the distances and see which are below a certain threshold and assume they are touching at those points.

Happy to answer FU quesions, let me know.

Thanks for the answer. I figured it out by myself just five minutes ago.

I’ll post the code, i used using ray-casting if anyone needs it.
Is it accurate enough or should i implement it your way? @nrex45.

``````#loading the model mesh
mesh = getNode(model_name).GetMesh()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(mesh)

#defining the coords of the tube (could be any sort of line-like obj i think)
pSource = points
pTarget = points

#evalutaing the actual intersections
obbTree = vtk.vtkOBBTree()
obbTree.SetDataSet(mesh)
obbTree.BuildLocator()
pointsVTKintersection = vtk.vtkPoints()
code = obbTree.IntersectWithLine(pSource, pTarget, pointsVTKintersection, None)
pointsVTKIntersectionData = pointsVTKintersection.GetData()
noPointsVTKIntersection = pointsVTKIntersectionData.GetNumberOfTuples()

#dealing with the intersection points
pointsIntersection = [] #where the intersections will be stored for future usage
for index in range(noPointsVTKIntersection):
point_of_intersection = pointsVTKIntersectionData.GetTuple3(index)
pointsIntersection.append(point_of_intersection)

return pointsIntersection #note that this is present only because in my code is part of a function that returns the coords, the actual drawing of the markups happens even if you decide to don't store the point in any variable
``````
1 Like

This method seems to find the intersection also.

``````# Expect console erors.
slicer.mrmlScene.RemoveNode(sphereModel)
slicer.mrmlScene.RemoveNode(cylinderModel)
slicer.mrmlScene.RemoveNode(intersectionModel_0)
slicer.mrmlScene.RemoveNode(intersectionModel_1)

cylinder = vtk.vtkCylinderSource()
cylinder.SetHeight(100.0)
cylinder.SetResolution(45)
cylinder.Update()
cylinderTriangulator = vtk.vtkTriangleFilter()
cylinderTriangulator.SetInputConnection(cylinder.GetOutputPort())
cylinderTriangulator.Update()
cylinderModel.SetName("cylinderModel")
cylinderModel.GetDisplayNode().SetOpacity(0.2)
cylinderModel.GetDisplayNode().SetColor(1.0, 0.0, 0.0)

sphere = vtk.vtkSphereSource()
sphere.SetPhiResolution(45)
sphere.SetThetaResolution(45)
sphere.Update()
sphereTriangulator = vtk.vtkTriangleFilter()
sphereTriangulator.SetInputConnection(sphere.GetOutputPort())
sphereTriangulator.Update()
sphereModel.SetName("sphereModel")
sphereModel.GetDisplayNode().SetOpacity(0.2)
sphereModel.GetDisplayNode().SetColor(0.0, 1.0, 0.0)

intersector = vtk.vtkIntersectionPolyDataFilter()
intersector.SetInputConnection(0, cylinderTriangulator.GetOutputPort())
intersector.SetInputConnection(1, sphereTriangulator.GetOutputPort())
intersector.Update()
{"ft_lang_label":"__label__en","ft_lang_prob":0.5736538,"math_prob":0.93654346,"size":5096,"snap":"2022-40-2023-06","text_gpt3_token_len":1251,"char_repetition_ratio":0.17046347,"word_repetition_ratio":0.18584071,"special_character_ratio":0.19642857,"punctuation_ratio":0.21456437,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9538904,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-01-28T12:19:24Z\",\"WARC-Record-ID\":\"<urn:uuid:23e9c77a-5a9f-4232-b88a-ed4fd10a9626>\",\"Content-Length\":\"28741\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b8b5e766-f321-450f-b50f-ff01fcca86b6>\",\"WARC-Concurrent-To\":\"<urn:uuid:4c92a6d3-c071-4a90-9d49-122a7670f051>\",\"WARC-IP-Address\":\"64.62.250.111\",\"WARC-Target-URI\":\"https://discourse.slicer.org/t/find-the-intersection-of-two-models/26338\",\"WARC-Payload-Digest\":\"sha1:VJEOBVYISHCUBQ5QGP6F43LK552JQ5K6\",\"WARC-Block-Digest\":\"sha1:TJKYZOBVTZY6JF3CNNHSAUYXTO6TM445\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764499634.11_warc_CC-MAIN-20230128121809-20230128151809-00467.warc.gz\"}"}