Ignore:
Timestamp:
09/11/06 00:29:47 (18 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.cpp

    r863 r1328  
    11#include "Triangle3.h" 
     2#include "Ray.h" 
     3#include "AxisAlignedBox3.h" 
     4 
    25 
    36namespace GtpVisibilityPreprocessor { 
    47 
    5 float 
    6 Triangle3::GetSpatialAngle(const Vector3 &point) const 
     8         
     9Triangle3::Triangle3(const Vector3 &a, const Vector3 &b, const Vector3 &c)  
    710{ 
    8   return 0.0f; 
     11        Init(a, b, c); 
    912} 
    1013 
     14 
     15void Triangle3::Init(const Vector3 &a, const Vector3 &b, const Vector3 &c)  
     16{ 
     17        mVertices[0] = a; 
     18        mVertices[1] = b; 
     19        mVertices[2] = c; 
     20 
    1121} 
     22 
     23 
     24float Triangle3::GetSpatialAngle(const Vector3 &point) const 
     25{ 
     26        return 0.0f; 
     27} 
     28 
     29 
     30int Triangle3::CastRay(const Ray &ray, float &t, const float nearestT) const 
     31{ 
     32        // get triangle edge vectors and plane normal 
     33        const Vector3 u = mVertices[1] - mVertices[0]; 
     34    const Vector3 v = mVertices[2] - mVertices[0]; 
     35 
     36    const Vector3 n = u * v;  // cross product 
     37 
     38    const Vector3 dir = ray.GetDir();           // ray direction vector 
     39    const Vector3 w0 = ray.GetLoc() - mVertices[0]; 
     40 
     41        // params to calc ray-plane intersect 
     42    const float a = - DotProd(n, w0); 
     43    const float b = DotProd(n, dir); 
     44 
     45        if (fabs(b) < Limits::Small)  
     46        {    
     47                // ray is parallel to triangle plane 
     48        if (a == 0) // ray lies in triangle plane 
     49                { 
     50            return Ray::INTERSECTION_OUT_OF_LIMITS; 
     51                } 
     52        else 
     53                { 
     54                        return Ray::NO_INTERSECTION; // ray disjoint from plane 
     55                } 
     56    } 
     57 
     58    // get intersect point of ray with triangle plane 
     59    t = a / b; 
     60 
     61    if (t < 0.0) // ray goes away from triangle 
     62        { 
     63        return Ray::NO_INTERSECTION; // => no intersect 
     64        } 
     65 
     66    // for a segment, also test if (r > 1.0) => no intersect 
     67 
     68        // intersect point of ray and plane 
     69    const Vector3 pt = ray.GetLoc() + t * dir;  
     70 
     71 
     72        /////////////////////////////////////////////// 
     73    //-- is point inside triangle? 
     74    
     75        const Vector3 w = pt - mVertices[0]; 
     76 
     77        const float uu = DotProd(u, u); 
     78    const float uv = DotProd(u, v); 
     79    const float vv = DotProd(v, v); 
     80     
     81        const float wu = DotProd(w, u); 
     82    const float wv = DotProd(w, v); 
     83    const float D = uv * uv - uu * vv; 
     84 
     85    // get and test parametric coords 
     86    const float s = (uv * wv - vv * wu) / D; 
     87 
     88    if (s < 0.0 || s > 1.0) // pt is outside triangle 
     89        { 
     90        return Ray::NO_INTERSECTION; 
     91        } 
     92 
     93        const float s2 = (uv * wu - uu * wv) / D; 
     94 
     95    if (s2 < 0.0 || (s + s2) > 1.0) // pt is outside triangle 
     96        { 
     97        return Ray::NO_INTERSECTION; 
     98        } 
     99 
     100        return Ray::INTERSECTION; // I is in T 
     101} 
     102 
     103 
     104AxisAlignedBox3 Triangle3::GetBoundingBox() const 
     105{ 
     106        AxisAlignedBox3 box; 
     107        box.Initialize(); 
     108 
     109        box.Include(mVertices[0]); 
     110        box.Include(mVertices[1]); 
     111        box.Include(mVertices[2]); 
     112 
     113        return box; 
     114} 
     115 
     116 
     117Vector3 Triangle3::GetNormal() const  
     118{ 
     119        const Vector3 v1 = mVertices[0] - mVertices[1]; 
     120        const Vector3 v2 = mVertices[2]-mVertices[1]; 
     121 
     122        return Normalize(CrossProd(v2, v1)); 
     123} 
     124 
     125 
     126Vector3 Triangle3::GetCenter() const  
     127{ 
     128        return (mVertices[0] + mVertices[1] + mVertices[2]) / 3.0f; 
     129} 
     130 
     131 
     132float Triangle3::GetArea() const  
     133{ 
     134        Vector3 v1=mVertices[0]-mVertices[1], v2=mVertices[2]-mVertices[1]; 
     135        return 0.5f * Magnitude(CrossProd(v2, v1)); 
     136} 
     137 
     138 
     139} 
Note: See TracChangeset for help on using the changeset viewer.