0
\$\begingroup\$

This question already has an answer here:

I'm in a class where we are creating a ray tracer from the ground up in C++. I'm at a point where I can't seem to wrap my head around the math that is required to calculate the point at which a ray intersects a polygon.

We have some direction for how to do this, but all of the class materials as well as my Googling of the subject have not helped.

These are the functions we must complete:

bool polygon::Intersect(const ray &R, intersection &inter)
{
    // To Do: If you have implemented the "CalculateNormal" method 
    // below, should already be calculated and placed in the member 
    // "n".
    //
    // Now, using n, you need to calculate the intersection of the
    // ray with the plane containing the polygon.  Then, once you 
    // have that point, you need to loop through each edge in the
    // polygon and make sure the point is to the left of that edge.
    //
    // If it is to the left of every edge, then fill the structure
    // inter with all of the data for the intersection and return 
    // true, if not, return 0. 
    // 
    // Don't forget to check to see if the t you calculate for the
    // ray is > 0.

    return false;
}

void polygon::CalculateNormal(void)
{
    // To Do: Use the edges of the polygon to calculate the normal
    // of the polygon. You should be careful to take care of the 
    // case where two edges give you a zero normal.  Place the 
    // normal into the member "n" so that the intersection method
    // can use it when called.
    // 
}

Could someone please explain this in a bit further detail?

Note: I am not asking for anyone to write the code or do my homework for me, but instead help me understand what exactly is going on here.

Thank you.

\$\endgroup\$

marked as duplicate by Seth Battin, Kromster says support Monica, ClassicThunder, congusbongus, Vaughan Hilts Oct 30 '14 at 0:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ That is similar, but @usm's answer is definitely the direction I needed \$\endgroup\$ – Parad0xD9 Oct 22 '14 at 15:04
  • 1
    \$\begingroup\$ When you have trouble with homework, ask the professor, that's exactly what you're paying them for. \$\endgroup\$ – MichaelHouse Oct 22 '14 at 15:52
  • \$\begingroup\$ I did, that's how I finally understood ray intersection with a sphere. However my professor's explanation for ray polygon intersection led me to ask here. :) \$\endgroup\$ – Parad0xD9 Oct 22 '14 at 16:46
2
\$\begingroup\$

To find the normal, you can use the cross product of three of the points in the polygon. Create two vectors from those three points and find the cross product of those.

To find the intersection of the ray with the polygon, you will first need to ensure it intersects with the plane of the polygon. To do this, you will need to do some algebraic manipulation of the equation of the plane as seen here.

Your ray should be defined as R0 + tV = P, where R0 is the origin of the ray, V is the direction of the ray, P is an arbitrary point on the ray and t is an arbitrary parameter.

The plane has the equation P . N + d = 0, where P is an arbitrary point on the plane, N is the normal of the plane (calculated earlier) and d is the vector offset from the origin.

We want to substitute the ray equation into the plane equation (replacing P) to get: (P0 + tV) . N + d = 0 and find the value of t:

t = -(P0 . N + d) / (V . N)

then you substitute that value of t back into your ray equation to get the value of P:

R0 + tV = P.

Finally, you want to go around each adjacent pair of points in the polygon checking that P is inside the polygon, which is done by checking that P is to the same side of each line made by the points.

When the instructions say to check that the point "to the left of every edge", imagine that you rotating the polygon and testing the edge that is the right-most. That might help you understand why we test that the point is always on the left of the edge.

\$\endgroup\$
  • 1
    \$\begingroup\$ Thank you, this is a great explanation! The instructions were, to me, quite vague and your help has cleared things up. I'll attempt to implement this in code soon. \$\endgroup\$ – Parad0xD9 Oct 22 '14 at 15:05
  • \$\begingroup\$ This doesn't take into account concave polygons. \$\endgroup\$ – rodolphito Aug 7 at 10:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.