//check if points of the triangle is counterclockwise
bool cclock( float x0, float y0,
             float x1, float y1,
             float x2, float y2 ) {
  Matrix3x3 m;
  m(0,0) = x0; m(0,1) = x1; m(0,2) = x2;
  m(1,0) = y0; m(1,1) = y1; m(1,2) = y2;
  m(2,0) = 1 ; m(2,1) = 1 ; m(2,2) = 1 ;
  return m.det() >= 0.0;
  /*
  return lineSide(x2 - x0, y2 - y0, x0, y0, x1, y1);
  */
}
예제 #2
0
bool Triangle::intersect(const Ray& r) const {
  
  // TODO: implement ray-triangle intersection
    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;
    }
    
    double u, v, t;
    double ede = 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{
                return true;
            }
        }
    }
}
예제 #3
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;

}