bool Triangle::Intersect(Ray &ray,Point3 &hitPoint) { vector3 e1 = m_Vertex[1] - m_Vertex[0]; vector3 e2 = m_Vertex[2] - m_Vertex[0]; vector3 q = ray.direction.cross(e2); float a = e1.dot(q); if(abs(a)<0.001) { hitPoint.set(Point3(0,0,0)); return false; } float f = 1/a; vector3 s = ray.position - m_Vertex[0]; float u = f * s.dot(q); if(u<0.0) { hitPoint.set(Point3(0,0,0)); return false; } vector3 r = s.cross(e1); float v = f * ray.direction.dot(r); if(v<0.0||u+v > 1.0) { hitPoint.set(Point3(0,0,0)); return false; } float t = f * e2.dot(r); for(int i =0;i<3;i++) { hitPoint.cell[i] = ray.position.cell[i] + t * ray.direction.cell[i]; } return true; }
REAL GarlandMeasurer<THEMesh>::collapse_cost( TEdge* e, Point3<REAL>& v, Vector3<REAL>& u) { assert( vQuad_.count(e->he_->headVtx_) && vQuad_.count(e->he_->tailVtx_) ); _VtxQuadric m = vQuad_[e->he_->headVtx_] + vQuad_[e->he_->tailVtx_]; Matrix3<REAL> invQ; if ( m.Q.inverse(invQ) >= 0 ) { Vector3<REAL> vv = invQ*m.b; v.set(vv.x, vv.y, vv.z); u.zero(); return vv.dot(m.Q*vv) - 2.*m.b.dot(vv) + m.d; } // use head vertex Vector3<REAL> vv = e->he_->headVtx_->pos_; REAL e0 = vv.dot(m.Q*vv) - 2.*m.b.dot(vv); // use tail vertex vv = e->he_->tailVtx_->pos_; REAL e1 = vv.dot(m.Q*vv) - 2.*m.b.dot(vv); if ( e0 > e1 ) { e0 = e1; v = e->he_->tailVtx_->pos_; } else { v = e->he_->headVtx_->pos_; } vv = (e->he_->headVtx_->pos_ + e->he_->tailVtx_->pos_)*0.5; e1 = vv.dot(m.Q*vv) - 2.*m.b.dot(vv); if ( e1 < e0 ) { v = vv; return e1 + m.d; } return e0 + m.d; }