public double Area() { RHVector3 d1 = vertices[1].pos.Subtract(vertices[0].pos); RHVector3 d2 = vertices[2].pos.Subtract(vertices[1].pos); return(0.5 * d1.CrossProduct(d2).Length); }
public void RecomputeNormal() { try { RHVector3 d1 = vertices[1].pos.Subtract(vertices[0].pos); RHVector3 d2 = vertices[2].pos.Subtract(vertices[1].pos); normal = d1.CrossProduct(d2); normal.NormalizeSafe(); } catch (System.NullReferenceException) { } }
public double alphaBeta; // Sum of dihedral angles to a virtual shared triangle public TopoEdgePair(TopoEdge _edgeA, TopoEdge _edgeB) { edgeA = _edgeA; edgeB = _edgeB; RHVector3 sharedPoint = null; RHVector3 p1 = null, p2 = null; if (edgeA.v1 == edgeB.v1) { sharedPoint = edgeA.v1.pos; p1 = edgeA.v2.pos; p2 = edgeB.v2.pos; } else if (edgeA.v1 == edgeB.v2) { sharedPoint = edgeA.v1.pos; p1 = edgeA.v2.pos; p2 = edgeB.v1.pos; } else if (edgeA.v2 == edgeB.v1) { sharedPoint = edgeA.v1.pos; p1 = edgeA.v1.pos; p2 = edgeB.v2.pos; } else if (edgeA.v2 == edgeB.v2) { sharedPoint = edgeA.v2.pos; p1 = edgeA.v1.pos; p2 = edgeB.v1.pos; } RHVector3 d1 = p1.Subtract(sharedPoint); RHVector3 d2 = p2.Subtract(sharedPoint); RHVector3 normal = d1.CrossProduct(d2); normal.NormalizeSafe(); //alphaBeta = normal.AngleForNormalizedVectors(edgeA.faces.First.Value.normal) + normal.AngleForNormalizedVectors(edgeB.faces.First.Value.normal); //if (alphaBeta > Math.PI) // normal was wrong direction //alphaBeta = 2 * Math.PI - alphaBeta; }
//--- MODEL_SLA // milton // 實作論文的方法 - Fast, Minimum Storage RayTriangle Intersection // t => delta, 如果有打到物體,t>0,否則t<0 public bool IntersectsLineTest(RHVector3 orig, RHVector3 dir, out double t, out double u, out double v) { t = u = v = 0; //Debug.WriteLine("IntersectsLine orig " + orig + " dir " + dir ); //Debug.WriteLine("IntersectsLine ver1 " + vertices[0].pos + " ver2 " + vertices[1].pos + " ver3 " + vertices[2].pos); RHVector3 vert0 = vertices[0].pos; /* find vectors for two edges sharing vert0 * SUB(edge1, vert1, vert0) * SUB(edge2, vert2, vert0) */ RHVector3 edge1 = vertices[1].pos.Subtract(vert0); RHVector3 edge2 = vertices[2].pos.Subtract(vert0); /* begin calculating determinant - also used to calculate U parameter * CROSS(pvec, dir, edge2)*/ RHVector3 pvec = dir.CrossProduct(edge2); /* if determinant is near zero, ray lies in plane of triangle * det = DOT(edge1, pvec)*/ double det = edge1.ScalarProduct(pvec); //Debug.WriteLine("IntersectsLine det " + det); /* define TEST_CULL if culling is desired * if (det < EPSILON)return 0*/ if (det < 0.000001) { return(false); } /* calculate distance from vert0 to ray origin * SUB(tvec, orig, vert0)*/ RHVector3 tvec = orig.Subtract(vert0); //Debug.WriteLine("IntersectsLine tvec " + tvec); /* calculate U parameter and test bounds * u = DOT(tvec, pvec)*/ u = tvec.ScalarProduct(pvec); /*if (*u < 0.0 || *u > det) return 0;*/ //Debug.WriteLine("IntersectsLine u " + u); if (u < 0 || u > det) { return(false); } /* prepare to test V parameter * CROSS(qvec, tvec, edge1)*/ RHVector3 qvec = tvec.CrossProduct(edge1); /* calculate V parameter and test bounds * v = DOT(dir, qvec)*/ v = dir.ScalarProduct(qvec); //Debug.WriteLine("IntersectsLine v " + v); /*if (*v < 0.0 || *u + *v > det) return 0*/ if (v < 0 || (u + v) > det) { return(false); } /* calculate t, scale parameters, ray intersects triangle * t = DOT(edge2, qvec) * inv_det = 1.0 / det * t *= inv_det * u *= inv_det * v *= inv_det*/ double inv_det = 1.0 / det; t = edge2.ScalarProduct(qvec); t *= inv_det; u *= inv_det; v *= inv_det; return(true); }