0

I have a frustum made of six planes, normals all pointing inwards. I can check if a point is inside the frustum with this function:

char ClassifyPoint(float x, float y, float z, char thePlane)
    {
        Vector& aPos=mPlane[thePlane].mPos;
        Vector& aNorm=mPlane[thePlane].mNormal;
        float aD=(aPos.mX-x)*aNorm.mX+(aPos.mY-y)*aNorm.mY+(aPos.mZ-z)*aNorm.mZ;
        if (aD<-0.0005f) return 1;if (aD>0.0005f) return -1;return 0;
    }

Which gives me -1, 0, 1 for if the point is front, on, or behind each plane. This works fine for a single point, but a line segment or a triangle could have ALL points outside the frustum but still be intersecting it.

What's the right way to judge if a line or triangle intersects? Everything I've tried always produces false positives because checking against six planes.

Edit, more info:

So to classify a line, I tried this:

inline char ClassifyLine(Vector theL1, Vector theL2, char thePlane) 
    {
        return ClassifyPoint(theL1,thePlane)+ClassifyPoint(theL2,thePlane);
    }

Which should produce "2" if both points are in front of the plane, 0 if it's straddling the plane, or any negative number if both points are behind the plane-- right?

But then I try this function to see if the frustum contains a line:

inline bool ContainsLine(Vector theLine1, Vector theLine2)
    {
        if (ClassifyLine(theLine1,theLine2,0)<0) return false;
        if (ClassifyLine(theLine1,theLine2,1)<0) return false;
        if (ClassifyLine(theLine1,theLine2,2)<0) return false;
        if (ClassifyLine(theLine1,theLine2,3)<0) return false;
        if (ClassifyLine(theLine1,theLine2,4)<0) return false;
        if (ClassifyLine(theLine1,theLine2,5)<0) return false;
        return true;
    }

And I get both false positives and false negatives with certain lines that straddle. What am I doing wrong here?

KiraHoneybee
  • 495
  • 3
  • 12
  • You could check to see where the line segment intersects the planes. Alternatively, clip the 1D line to the 3D interior of your frustum. – 1201ProgramAlarm Aug 31 '21 at 21:43
  • In your analysis of `ClassifyLine()`, you skipped describing what it means for this function to produce `1`. When you add a `1`, `0`, or `-1` to a `1`, `0`, or `-1`, the result could be any of `2`, `1`, `0`, `-1`, or `-2`, yet your analysis covers only `2`, `0`, and negative numbers. – JaMiT Aug 31 '21 at 23:56
  • Hi Jamit, anything positive would indicate that at least one point was in front of the plane (both return in front: 1+1). Anything negative means NO points are in front of the plane (both return behind: -1+-1) (and 0 means a straddle) (One returned negative, one positive: -1+1) – KiraHoneybee Sep 01 '21 at 00:34
  • @KiraHoneybee Don't worry -- I personally am aware of the significance. However, not everyone who reads the question will necessarily be aware of it. I My comment was not for my benefit, but for other readers; I pointed out a defect in your question that you might want to improve (with an edit). Your question does not cover what a positive 1 would mean for a line (one point returned +1 and the other returned 0). *With regard to your comment, returning 0 for the line does not necessarily mean that one point returned -1 and the other returned +1; it might be that both returned 0.* – JaMiT Sep 01 '21 at 23:04
  • you need to check 6x line plane intersection ... using simple point inside convex mesh test is useless for this task. For starters you can port these: [`line closest(line l0,triangle t0);` and `line closest(line l0,convex_mesh m0);`](https://stackoverflow.com/a/62257945/2521214) – Spektre Sep 02 '21 at 06:22

0 Answers0