0

Before going to the flag and marking this as a dupe, please acknowledge that I have minor c++ experience, and I don't completely understand answers given to some questions regarding matrices or higher up-there maths.

I have triangle defined by 3 points, A(0,0,0), B(1,0,0), C(0,1,1)

I have a line segment, defined by 2 points, D(0,1/2,0), and E(1,1/2,1)

Now, regarding the first link, I understand that if the volumes of tetra ABCD = 0, or tetra ABCE = 0, that there is a point within the triangles face. But when negative volumes are mentioned:

Here is one way to solve your problem. Compute the volume of the tetrahedron Td = (a,b,c,d) and Te = (a,b,c,e). If either volume of Td or Te is zero, then one endpoint of the segment de lies on the plane containing triangle (a,b,c). If the volumes of Td and Te have the same sign, then de lies strictly to one side, and there is no intersection. If Td and Te have opposite signs, then de crosses the plane containing (a,b,c).

How can a volume be negative? Am I missing something?

I have this code for a 2d triangle, but I cannot find anything online for a 3d triangle which is understandable/easily translatable into code,

def areaOfTri(x1, y1, x2, y2, x3, y3): 
    return abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0) 


def pointIn2dTri(x1, y1, x2, y2, x3, y3, x, y): 
    # Calculate area of triangle ABC 
    A = areaOfTri (x1, y1, x2, y2, x3, y3)

    # Calculate area of triangle PBC  
    A1 = areaOfTri (x, y, x2, y2, x3, y3) 

    # Calculate area of triangle PAC
    A2 = areaOfTri (x1, y1, x, y, x3, y3) 

    # Calculate area of triangle PAB  
    A3 = areaOfTri (x1, y1, x2, y2, x, y) 

    # Check if sum of A1, A2 and A3  
    # is same as A 
    if(A == A1 + A2 + A3): 
        return True
    else: 
        return False

Some random links I found: here, here, and here

crazicrafter1
  • 309
  • 5
  • 18

2 Answers2

0

In this case, the line does cross the plane containing (a, b, c).

Proof:

abcd = np.array([[0, 0, 0, 1],
                 [1, 0, 0, 1],
                 [0, 1, 1, 1],
                 [0, 0.5, 0, 1]])

abce = np.array([[0, 0, 0, 1],
                 [1, 0, 0, 1],
                 [0, 1, 1, 1],
                 [1, 0.5, 1, 1]])

print(np.sign(np.linalg.det(abcd)))
print(np.sign(np.linalg.det(abce)))

Output:

1.0
-1.0

The interpretation of negative volume is probably off topic for this site, but see this answer.

Briefly, vectors have a direction representing where you're coming from versus where you're going, as represented by their sign.

We can extend that concept to volumes by recognising that a volume is bounded by certain vectors, which themselves have directions.

gmds
  • 19,325
  • 4
  • 32
  • 58
  • I realize I didn't ask for the intersection point between the triangle and line segment, but are you able to find that? – crazicrafter1 Apr 11 '19 at 00:49
  • Oh and another thing, it appears that the triangle is being treated as a plane that extends infinitely in each direction: https://hastebin.com/tuzuhuxuja.cs – crazicrafter1 Apr 11 '19 at 20:02
  • I see that hastebin is broken a lot, so heres the pastebin, https://pastebin.com/FzCB2i49. I'm trying to find out why the plane is treated as if its boundless; so far i've gotten nowhere. – crazicrafter1 Apr 13 '19 at 22:07
0

Let segment is described by parametric equation

P(t) = D + (E-D)*t

and triangle defines two-parameter coordinate system in it's plane

F(u,v) = A + u*(B-A) + v*(C-A)

Make system of three linear equations for segment point belonging that plane

D.X + (E.X - D.X) * t  = A.X + u*(B.X-A.X) + v*(C.X-A.X)
similar for y,z

solve it for unknowns t,u,v (it should be easy with NumPy).

Segment does intersect triangle if solution exists and

t is in range 0..1
u ~ 0..1
v ~ 0..1
u+v ~ 0..1

In case of success substitute t into the first equation to get intersection point

MBo
  • 77,366
  • 5
  • 53
  • 86