Changeset 314


Ignore:
Timestamp:
10/10/05 04:49:10 (19 years ago)
Author:
mattausch
Message:

added ray intersection for polygons

Location:
trunk/VUT/GtpVisibilityPreprocessor/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp

    r312 r314  
    44#include "Mesh.h" 
    55#include "AxisAlignedBox3.h" 
     6#include "Ray.h" 
    67 
    78Polygon3::Polygon3(): mMaterial(NULL), mParent(NULL) 
     
    234235                box.Include(*(*it)); 
    235236} 
     237 
     238// int_lineseg returns 1 if the given line segment intersects a 2D 
     239// ray travelling in the positive X direction.  This is used in the 
     240// Jordan curve computation for polygon intersection. 
     241inline int 
     242int_lineseg(float px, 
     243            float py, 
     244            float u1, 
     245            float v1, 
     246            float u2, 
     247            float v2) 
     248{ 
     249        float t; 
     250        float ydiff; 
     251 
     252        u1 -= px; u2 -= px;       // translate line 
     253        v1 -= py; v2 -= py; 
     254 
     255        if ((v1 > 0 && v2 > 0) || 
     256                (v1 < 0 && v2 < 0) || 
     257                (u1 < 0 && u2 < 0)) 
     258        return 0; 
     259 
     260        if (u1 > 0 && u2 > 0) 
     261        return 1; 
     262 
     263        ydiff = v2 - v1; 
     264 
     265        if (fabs(ydiff) < Limits::Small)  
     266        {         // denominator near 0 
     267                if (((fabs(v1) > Limits::Small) || (u1 > 0) || (u2 > 0))) 
     268                        return 0; 
     269                return 1; 
     270        } 
     271 
     272        t = -v1 / ydiff;                  // Compute parameter 
     273 
     274        return (u1 + t * (u2 - u1)) > 0; 
     275} 
     276 
     277int Polygon3::CastRay(const Ray &ray, float &t, const float nearestT) 
     278{ 
     279        Plane3 plane = GetSupportingPlane(); 
     280        float dot = DotProd(plane.mNormal, ray.GetDir()); 
     281 
     282        // Watch for near-zero denominator 
     283        // ONLY single sided polygons!!!!! 
     284        if (dot > -Limits::Small) 
     285        //  if (fabs(dot) < Limits::Small) 
     286        return Ray::NO_INTERSECTION; 
     287 
     288        t = (-plane.mD - DotProd(plane.mNormal, ray.GetLoc())) / dot; 
     289 
     290        if (t <= Limits::Small) 
     291        return Ray::INTERSECTION_OUT_OF_LIMITS; 
     292 
     293        if (t >= nearestT) { 
     294        return Ray::INTERSECTION_OUT_OF_LIMITS; // no intersection was found 
     295        } 
     296 
     297        int count = 0; 
     298        float u, v, u1, v1, u2, v2; 
     299        int i; 
     300 
     301        int paxis = plane.mNormal.DrivingAxis(); 
     302 
     303        // Project the intersection point onto the coordinate plane 
     304        // specified by which. 
     305        ray.Extrap(t).ExtractVerts(&u, &v, paxis); 
     306 
     307        int size = (int)mVertices.size(); 
     308 
     309        mVertices.back().ExtractVerts(&u1, &v1, paxis ); 
     310 
     311        if (0 && size <= 4)  
     312        { 
     313                // assume a convex face 
     314                for (i = 0; i < size; i++)  
     315                { 
     316                mVertices[i].ExtractVerts(&u2, &v2, paxis); 
     317             
     318                        // line u1, v1, u2, v2 
     319             
     320                        if ((v2 - v1)*(u1 - u) > (u2 - u1)*(v1 - v)) 
     321                                return Ray::NO_INTERSECTION; 
     322 
     323                        u1 = u2; 
     324                        v1 = v2; 
     325        } 
     326 
     327        return Ray::INTERSECTION; 
     328        } 
     329 
     330        // We're stuck with the Jordan curve computation.  Count number 
     331        // of intersections between the line segments the polygon comprises 
     332        // with a ray originating at the point of intersection and 
     333        // travelling in the positive X direction. 
     334        for (i = 0; i < size; i++)  
     335        { 
     336                mVertices[i].ExtractVerts(&u2, &v2, paxis); 
     337 
     338                count += (int_lineseg(u, v, u1, v1, u2, v2) != 0); 
     339 
     340                u1 = u2; 
     341                v1 = v2; 
     342        } 
     343 
     344        // We hit polygon if number of intersections is odd. 
     345        return (count & 1) ? Ray::INTERSECTION : Ray::NO_INTERSECTION; 
     346} 
     347 
  • trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h

    r312 r314  
    1515class Intersectable; 
    1616class AxisAlignedBox3; 
     17class Ray; 
    1718//typedef Vertex3 Vector3; 
    1819 
     
    8182        static void IncludeInBox(const PolygonContainer &polys, AxisAlignedBox3 &box); 
    8283 
     84        /** Casts ray to polygon. 
     85        */ 
     86        int CastRay(const Ray &ray, float &t, const float nearestT); 
     87 
    8388        /// vertices are connected in counterclockwise order. 
    8489        VertexContainer mVertices; 
Note: See TracChangeset for help on using the changeset viewer.