Exemplo n.º 1
0
        public bool ProjectPoint(RHVector3 p, out double lambda, RHVector3 pProjected)
        {
            RHVector3 u = v2.pos.Subtract(v1.pos);

            lambda       = p.Subtract(v1.pos).ScalarProduct(u) / u.ScalarProduct(u);
            pProjected.x = v1.pos.x + lambda * u.x;
            pProjected.y = v1.pos.y + lambda * u.y;
            pProjected.z = v1.pos.z + lambda * u.z;
            return(lambda >= 0 && lambda <= 1);
        }
Exemplo n.º 2
0
        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;
        }
Exemplo n.º 3
0
        //--- 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);
        }