Ejemplo n.º 1
0
bool Triangle::intersect(const Ray& r, Intersection *i) const {

  // TODO:
  // Implement ray - triangle intersection.
  // When an intersection takes place, the Intersection data should
  // be updated correspondingly.

    Vector3D e1 = mesh->positions[v2]-mesh->positions[v1];
    Vector3D e2 = mesh->positions[v3]-mesh->positions[v1];
    Vector3D s = r.o - mesh->positions[v1];
    
//    Matrix3x3 M;
//    M.column(0) = e1;
//    M.column(1) = e2;
//    M.column(2) = -r.d;
//    
//    Vector3D x = M.inv()*s;
//    double u = x[0];
//    double v = x[1];
//    double t = x[2];
    double f = dot(cross(e1,r.d),e2);
    if (f == 0) {
        return false;
    }
    
    double u = dot(cross(s,r.d),e2)/f;
    double v = dot(cross(e1,r.d),s)/f;
    double t = dot(cross(e1,-s),e2)/f;
    
    if (!(u >= 0 && v >= 0 && u+v <= 1 && t > r.min_t && t < r.max_t && t < i->t)) {
        return false;
    }
    
    //cout << u <<" "<< v << " " << t << endl;
    r.max_t = t;
    

    i->bsdf = get_bsdf();
    i->t = t;
    i->primitive = this;
    Vector3D n = (1-u-v)*mesh->normals[v1]+ u*mesh->normals[v2]+ v*mesh->normals[v3];
    if (dot(r.d,n) > 0) {
        n = -n;
    }
    i->n = n;
    
    
  return true;
  
}
Ejemplo n.º 2
0
bool Triangle::intersect(const Ray& r, Intersection *isect) const {
  
  // TODO: 
  // implement ray-triangle intersection. When an intersection takes
  // place, the Intersection data should be updated accordingly
    Vector3D p0 = mesh->positions[v1];
    Vector3D p1 = mesh->positions[v2];
    Vector3D p2 = mesh->positions[v3];
    
    Vector3D e1 = p1 - p0;
    Vector3D e2 = p2 - p0;
    Vector3D s = r.o - p0;
    Matrix3x3 A;
    for (size_t i = 0; i < 3; i++) {
        A(i,0) = e1[i];
        A(i,1) = e2[i];
        A(i,2) = -r.d[i];
    }
    if (A.det() == 0) {
        return false;
    }
    
    bool tri_intersect = false;
    Vector3D uvt = A.inv() * s;
    if (uvt.z < r.min_t || uvt.z > r.max_t) {
        return false;
    }else if (uvt.x < 0 || uvt.x > 1) {
        return false;
    }else if (uvt.y < 0 || uvt.y > 1) {
        return false;
    }else if (uvt.x + uvt.y > 1) {
        return false;
    }else {
        tri_intersect = true;
    }
    isect->t = uvt.z;
    isect->bsdf = get_bsdf();
    isect->primitive = this;
    isect->n = (1 - uvt.x - uvt.y) * mesh->normals[v1] + uvt.x * mesh->normals[v2] + uvt.y * mesh->normals[v3];
    r.max_t = uvt.z;
    isect->n.normalize();
    
    
//    bool tri_intersect = false;
//    double u, v, t;
//    double ede = 1 / dot(cross(e1, r.d), e2);
//    t = - (dot(cross(s, e2), e1)) * ede;
//    if (t < r.min_t || t > r.max_t) {
//        return false;
//    }else {
//        u = - (dot(cross(s, e2), r.d)) * ede;
//        if (u < 0 || u > 1) {
//            return false;
//        }else {
//            v = (dot(cross(e1, r.d), s)) * ede;
//            if (v < 0 || v > 1) {
//                return false;
//            }else{
//                if (u + v > 1) {
//                    return false;
//                }else{
//                    tri_intersect = true;
//                }
//            }
//        }
//    }
//    isect->t = t;
//    isect->bsdf = get_bsdf();
//    isect->primitive = this;
//    isect->n = (1 - u - v) * mesh->normals[v1] + u * mesh->normals[v2] + v * mesh->normals[v3];
//    r.max_t = t;
//    isect->n.normalize();
    return tri_intersect;

}