I hope to be as clear as possible. I'm trying to implement a function that, given two tetrahedra, checks if they intersect with each other. I am working with python and the only library I am using is NumPy. To describe a tetrahedron I use its 4 vertices which are each described by coordinates [x, y, z].
vertex = [x, y, z]
tetrahedra = [vertex 1,vertex 2,vertex 3,vertex 4]
This is the reasoning I want to use:
- A tetrahedron is nothing more than a region defined by inequalities.
- These inequalities are described by the planes containing one face of the tetrahedron.
- So given the inequalities of the two tetrahedra, and putting them in a system, if this system admits a solution then there is an intersection.
This is my function:
def IsInterpenetrated(self, tetrahedra):
A= []
B= []
sol= 0
for tr in [self, tetrahedra]:
print("Plane of tetrahedra")
vertexList = tr.vertices
i=0
while i<4:
if handedness(vertexList)>0:
n= numpy.cross(vertexList[1].coords - vertexList[0].coords, vertexList[2].coords - vertexList[0].coords)
else:
n= numpy.cross(vertexList[2].coords - vertexList[0].coords, vertexList[1].coords - vertexList[0].coords)
p0= vertexList[0].coords
d= -(n[0]*p0[0] + n[1]*p0[1] + n[2]*p0[2])
print("normal: ", n , end=" ")
print("termine noto: ",(d))
if len(A) > 3:
j=0
while j<=3:
if numpy.all(-n == A[j]) and -d == B[j]:
sol = 1
j= j+1
A.append(n)
B.append(d)
p0= vertexList[0]
vertexList[0] = vertexList[1]
vertexList[1] = vertexList[2]
vertexList[2] = vertexList[3]
vertexList[3] = p0
i=i+1
A= numpy.array(A)
B= numpy.array(B)
print("\n")
print("Disequazioni:\n")
i=0
for n in A:
print("({0})x + ({1})y + ({2})z + ({3}) > 0".format(n[0],n[1],n[2],B[i]))
i=i+1
print("\n")
x = cvxpy.Variable(3)
prob = cvxpy.Problem(cvxpy.Minimize(0),[A @ x + B >= 0])
prob.solve()
if prob.value == 0 and sol != 1:
return 1
return 0
In this case I have solved the system of inequalities using cvxpy and I have verified the particular case in which the two tetrahedra have a common face. However this code does not cover the cases in which the touch occurs between vertices or between face-vertex, side-vertex, side-side. That is all those situations in which there is no real interpenetration, but there is a touch between the two tetrahedra.
So what I'm asking is if there is a way to verify the interpenetration between two tetrahedra, including this case too? Thanks in advance!