Grasshopper

algorithmic modeling for Rhino

Hi all, it might sound a really silly problem but I couldn't manage to solve it so far:

I am writing in python a function that given two boxes should give me as an output

1 if one box is inside the other

1 if one box is intersecting the other

0 if the two are not intersecting 

The third condition should contain the possibility that the two solids are "touching" but not intersecting, but it looks that all the functions are flagging this condition as a proper intersection.

Is there or could you suggest me a (simple) way to solve this?

Thank you very much in advance

 

Here is the code and below an image of what I would like to be NOT flagged as an intersection:

import rhinoscriptsyntax as rs


def checkintersection(obj,brep,tol):

   test = rs.IsObjectInBox(obj, rs.BoundingBox(brep), test_mode=True)
   insec=rs.IntersectBreps(obj,brep,tol)

   if test == True:
      flag = 1
   if insec:
      flag = 1
      rs.DeleteObjects(insec)
   else:
      flag = 0

   return flag

obj1 = rs.GetObject("1")
obj2 = rs.GetObject("2")

flag = checkintersection(obj1,obj2,0.01)
print flag

Views: 1594

Attachments:

Replies to This Discussion

Hi Vincenzo,

In case your two objects do not include each other or intersect, you can check for the minimum distance between them. If it's less then say Model Absolute Tolerance they "touch" (flag = -1), else - they do not (flag = 0).
Check the attached file.

Attachments:

Thank you very much djordje!

I had to make a few amendments to what you sent me in order to take in account that the small cube could "touch" the surface of the big one but from the inside, here it is how it look like:

Thanks again!

import rhinoscriptsyntax as rs

#flag 1 = intersecting, or one inside another
#flag 0 = not intersecting, or just touching each other


def checkintersection(obj,brep,tol):

test = rs.IsObjectInBox(obj, rs.BoundingBox(brep), test_mode=True)

insec=rs.IntersectBreps(obj,brep,tol)

if test == True and not insec:
flag = 1
elif insec:
insec=rs.IntersectBreps(obj,brep,tol) #if one object is inside the other

flag1 = minDistanceTwoBreps(obj1, obj2, tol)
flag2 = minDistanceTwoBreps(obj2, obj1, tol)

rs.DeleteObjects(insec)

if flag1 == 0 and flag2== 0 :
flag = 0
else:
flag = 1

return flag


def minDistanceTwoBreps(obj1, obj2, tol):
if (tol is None) or tol < 0.0:
tol = rs.UnitAbsoluteTolerance()
pt1 = rs.SurfaceVolumeCentroid(obj1)[0]
pt2 = rs.BrepClosestPoint(obj2, pt1)[0]
dist = 100
precision = 3 # the larger the number - the more precise the distance
i = 0
while (dist > tol) and (i < precision):
pt1 = rs.BrepClosestPoint(obj1, pt2)[0]
pt2 = rs.BrepClosestPoint(obj2, pt1)[0]
dist = rs.Distance(pt1,pt2)
i += 1

if dist <= tol:
flag = 0
else:
flag = 1
return flag


obj1 = rs.GetObject("select object 1")
obj2 = rs.GetObject("select object 2")
tol = rs.UnitAbsoluteTolerance()

flag = checkintersection(obj1, obj2, tol)

print flag

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service