We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › 3D intersection: point on polygon
Page Index Toggle Pages: 1
3D intersection: point on polygon (Read 893 times)
3D intersection: point on polygon
Nov 9th, 2009, 7:00am
 
hi there, hoping there's an easy fix for this issue that i've looked over.

i'm using saito's objloader library to define a volume in which a particular interaction can occur. to do this i need to find determine whether a given point is inside an imported, solid, polygon array.

using the rule that a ray from a point within a polygon will have an odd number of intersections, while a point outside will have an even number, i'm using this geometry to find the intersections of a ray and each planar section of the solid.

local.wasp.uwa.edu.au/~pbourke/geometry/planeline/

however, this gives all the intersections on each infinite plane, and i only need the intersections within the planar polygon.

has anyone encountered this issue and found a quick solution? i can't quite fathom an operation with 3D rotations about the normal.

below is the code i've hacked together to define the intersections

Code:

int intersection_count = 0;
//define ray from point/particle
PVector v1 = new PVector (particle.loc.x, particle.loc.y, particle.loc.z-10);

// where j gets model, k gets segment, l gets face, m gets vertex
// this is just getting the normal and vertexes of each face
for (int j = 0; j < models.length; j++) {
 for (int k = 0; k < models[j].getSegmentSize(); k++) {
   for (int l = 0; l < models[j].getIndexCountInSegment(k); l++) {
     int normalIndex = (models[j].getNormalIndexArrayInSegment(k,l)[0]);
     PVector faceNormal = (PVector) models[j].getNormal(normalIndex);
     faceNormal = (PVector) models[j].getNormal(normalIndex);              
     PVector[] faceVertexes = new PVector[models[j].getVertIndexArrayInSegment(k,l).length];
     for (int m = 0; m < models[j].getVertIndexArrayInSegment(k,l).length; m++) {
       int index = (models[j].getVertIndexArrayInSegment(k,l)[m]);
       PVector faceVector = (PVector) models[j].getVertex(index);
       faceVertexes[m] = new PVector(faceVector.x,faceVector.y,faceVector.z);
     }

     // define intersection between ray and plane
     PVector top = new PVector (faceVertexes[0].x - particle.loc.x, faceVertexes[0].y - particle.loc.y, faceVertexes[0].z - particle.loc.z);
     PVector intersectionVector1 = new PVector (v1.x - particle.loc.x, v1.y - particle.loc.y, v1.z - particle.loc.z);                
     float u = (faceNormal.dot(top)/faceNormal.dot(intersectionVector1));
     intersectionVector1.mult(u);
     intersectionVector1.add(particle.loc);

     // this anglesum function only works in 2D, something of a problem
     if (intersectionVector1.x <= particle.loc.x) {
       float interior_check = angleSum(faceVertexes,intersectionVector1);
       if ((interior_check <= 360 + 1) && (interior_check >= 360 - 1)) {
         //println("intcheck " + interior_check + " " + i);
         points.add(intersectionVector1);
         intersection_count++;
       }
     }
   }
 }
}


and the point inside polygon function that only works in 2D:

Code:
float angleSum(PVector[] polySegments, PVector tempPosition) {  
 PVector[] p = polySegments;
 int n = p.length;
 PVector q = tempPosition;
 float anglesum = 0;
 double costhetad = 0;
 PVector v1 = new PVector(0,0,0);
 PVector v2 = new PVector(0,0,0);
 for (int i = 0; i < n; i++) {
   v1 = PVector.sub(p[i], q);
   v2 = PVector.sub(p[(i+1)%n],q);
   costhetad = PVector.angleBetween(v1, v2);
   float costheta = (float)costhetad;
     anglesum += (costheta);
 }
 return(degrees(anglesum));
}


cheers...
Page Index Toggle Pages: 1