public static void TriangleLineRelations(
     Vector2d origin, Vector2d direction,
     Triangle2d tri, ref Vector3d dist, ref Vector3i sign,
     ref int positive, ref int negative, ref int zero)
 {
     positive = 0;
     negative = 0;
     zero     = 0;
     for (int i = 0; i < 3; ++i)
     {
         Vector2d diff = tri[i] - origin;
         dist[i] = diff.DotPerp(direction);
         if (dist[i] > MathUtil.ZeroTolerance)
         {
             sign[i] = 1;
             ++positive;
         }
         else if (dist[i] < -MathUtil.ZeroTolerance)
         {
             sign[i] = -1;
             ++negative;
         }
         else
         {
             dist[i] = 0.0;
             sign[i] = 0;
             ++zero;
         }
     }
 }
        public static int WhichSide(Triangle2d V, Vector2d P, Vector2d D)
        {
            // Vertices are projected to the form P+t*D.  Return value is +1 if all
            // t > 0, -1 if all t < 0, 0 otherwise, in which case the line splits the
            // triangle.

            int positive = 0, negative = 0, zero = 0;

            for (int i = 0; i < 3; ++i)
            {
                double t = D.Dot(V[i] - P);
                if (t > (double)0)
                {
                    ++positive;
                }
                else if (t < (double)0)
                {
                    ++negative;
                }
                else
                {
                    ++zero;
                }

                if (positive > 0 && negative > 0)
                {
                    return(0);
                }
            }
            return(zero == 0 ? (positive > 0 ? 1 : -1) : 0);
        }
 public IntrLine2Triangle2(Line2d l, Triangle2d t)
 {
     line = l; triangle = t;
 }
        public static void GetInterval(Vector2d origin, Vector2d direction, Triangle2d tri,
                                       Vector3d dist, Vector3i sign, ref Vector2d param)
        {
            // Project triangle onto line.
            Vector3d proj = Vector3d.Zero;
            int      i;

            for (i = 0; i < 3; ++i)
            {
                Vector2d diff = tri[i] - origin;
                proj[i] = direction.Dot(diff);
            }

            // Compute transverse intersections of triangle edges with line.
            double numer, denom;
            int    i0, i1, i2;
            int    quantity = 0;

            for (i0 = 2, i1 = 0; i1 < 3; i0 = i1++)
            {
                if (sign[i0] * sign[i1] < 0)
                {
                    if (quantity >= 2)
                    {
                        throw new Exception("IntrLine2Triangle2.GetInterval: too many intersections!");
                    }

                    numer             = dist[i0] * proj[i1] - dist[i1] * proj[i0];
                    denom             = dist[i0] - dist[i1];
                    param[quantity++] = numer / denom;
                }
            }

            // Check for grazing contact.
            if (quantity < 2)
            {
                for (i0 = 1, i1 = 2, i2 = 0; i2 < 3; i0 = i1, i1 = i2++)
                {
                    if (sign[i2] == 0)
                    {
                        if (quantity >= 2)
                        {
                            throw new Exception("IntrLine2Triangle2.GetInterval: too many intersections!");
                        }

                        param[quantity++] = proj[i2];
                    }
                }
            }

            // Sort.
            if (quantity < 1)
            {
                throw new Exception("IntrLine2Triangle2.GetInterval: need at least one intersection");
            }

            if (quantity == 2)
            {
                if (param[0] > param[1])
                {
                    double save = param[0];
                    param[0] = param[1];
                    param[1] = save;
                }
            }
            else
            {
                param[1] = param[0];
            }
        }
 public IntrTriangle2Triangle2(Triangle2d t0, Triangle2d t1)
 {
     triangle0 = t0;
     triangle1 = t1;
     Points    = null;
 }
        bool GetCoplanarIntersection(ref Plane3d plane, ref Triangle3d tri0, ref Triangle3d tri1)
        {
            // Project triangles onto coordinate plane most aligned with plane
            // normal.
            int    maxNormal = 0;
            double fmax      = Math.Abs(plane.Normal.x);
            double absMax    = Math.Abs(plane.Normal.y);

            if (absMax > fmax)
            {
                maxNormal = 1;
                fmax      = absMax;
            }
            absMax = Math.Abs(plane.Normal.z);
            if (absMax > fmax)
            {
                maxNormal = 2;
            }

            Triangle2d projTri0 = new Triangle2d(), projTri1 = new Triangle2d();
            int        i;

            if (maxNormal == 0)
            {
                // Project onto yz-plane.
                for (i = 0; i < 3; ++i)
                {
                    projTri0[i] = tri0[i].yz;
                    projTri1[i] = tri1[i].yz;
                }
            }
            else if (maxNormal == 1)
            {
                // Project onto xz-plane.
                for (i = 0; i < 3; ++i)
                {
                    projTri0[i] = tri0[i].xz;
                    projTri1[i] = tri1[i].xz;
                }
            }
            else
            {
                // Project onto xy-plane.
                for (i = 0; i < 3; ++i)
                {
                    projTri0[i] = tri0[i].xy;
                    projTri1[i] = tri1[i].xy;
                }
            }

            // 2D triangle intersection routines require counterclockwise ordering.
            Vector2d save;
            Vector2d edge0 = projTri0[1] - projTri0[0];
            Vector2d edge1 = projTri0[2] - projTri0[0];

            if (edge0.DotPerp(edge1) < (double)0)
            {
                // Triangle is clockwise, reorder it.
                save        = projTri0[1];
                projTri0[1] = projTri0[2];
                projTri0[2] = save;
            }

            edge0 = projTri1[1] - projTri1[0];
            edge1 = projTri1[2] - projTri1[0];
            if (edge0.DotPerp(edge1) < (double)0)
            {
                // Triangle is clockwise, reorder it.
                save        = projTri1[1];
                projTri1[1] = projTri1[2];
                projTri1[2] = save;
            }

            IntrTriangle2Triangle2 intr = new IntrTriangle2Triangle2(projTri0, projTri1);

            if (!intr.Find())
            {
                return(false);
            }

            PolygonPoints = new Vector3d[intr.Quantity];

            // Map 2D intersections back to the 3D triangle space.
            Quantity = intr.Quantity;
            if (maxNormal == 0)
            {
                double invNX = ((double)1) / plane.Normal.x;
                for (i = 0; i < Quantity; i++)
                {
                    double y = intr.Points[i].x;
                    double z = intr.Points[i].y;
                    double x = invNX * (plane.Constant - plane.Normal.y * y - plane.Normal.z * z);
                    PolygonPoints[i] = new Vector3d(x, y, z);
                }
            }
            else if (maxNormal == 1)
            {
                double invNY = ((double)1) / plane.Normal.y;
                for (i = 0; i < Quantity; i++)
                {
                    double x = intr.Points[i].x;
                    double z = intr.Points[i].y;
                    double y = invNY * (plane.Constant - plane.Normal.x * x - plane.Normal.z * z);
                    PolygonPoints[i] = new Vector3d(x, y, z);
                }
            }
            else
            {
                double invNZ = ((double)1) / plane.Normal.z;
                for (i = 0; i < Quantity; i++)
                {
                    double x = intr.Points[i].x;
                    double y = intr.Points[i].y;
                    double z = invNZ * (plane.Constant - plane.Normal.x * x - plane.Normal.y * y);
                    PolygonPoints[i] = new Vector3d(x, y, z);
                }
            }

            Result = IntersectionResult.Intersects;
            Type   = IntersectionType.Polygon;
            return(true);
        }
        bool IntersectsSegment(ref Plane3d plane, ref Triangle3d triangle, Vector3d end0, Vector3d end1)
        {
            // Compute the 2D representations of the triangle vertices and the
            // segment endpoints relative to the plane of the triangle.  Then
            // compute the intersection in the 2D space.

            // Project the triangle and segment onto the coordinate plane most
            // aligned with the plane normal.
            int    maxNormal = 0;
            double fmax      = Math.Abs(plane.Normal.x);
            double absMax    = Math.Abs(plane.Normal.y);

            if (absMax > fmax)
            {
                maxNormal = 1;
                fmax      = absMax;
            }
            absMax = Math.Abs(plane.Normal.z);
            if (absMax > fmax)
            {
                maxNormal = 2;
            }

            Triangle2d projTri = new Triangle2d();
            Vector2d   projEnd0 = Vector2d.Zero, projEnd1 = Vector2d.Zero;
            int        i;

            if (maxNormal == 0)
            {
                // Project onto yz-plane.
                for (i = 0; i < 3; ++i)
                {
                    projTri[i] = triangle[i].yz;
                    projEnd0.x = end0.y;
                    projEnd0.y = end0.z;
                    projEnd1.x = end1.y;
                    projEnd1.y = end1.z;
                }
            }
            else if (maxNormal == 1)
            {
                // Project onto xz-plane.
                for (i = 0; i < 3; ++i)
                {
                    projTri[i] = triangle[i].xz;
                    projEnd0.x = end0.x;
                    projEnd0.y = end0.z;
                    projEnd1.x = end1.x;
                    projEnd1.y = end1.z;
                }
            }
            else
            {
                // Project onto xy-plane.
                for (i = 0; i < 3; ++i)
                {
                    projTri[i] = triangle[i].xy;
                    projEnd0.x = end0.x;
                    projEnd0.y = end0.y;
                    projEnd1.x = end1.x;
                    projEnd1.y = end1.y;
                }
            }

            Segment2d             projSeg = new Segment2d(projEnd0, projEnd1);
            IntrSegment2Triangle2 calc    = new IntrSegment2Triangle2(projSeg, projTri);

            if (!calc.Find())
            {
                return(false);
            }

            Vector2dTuple2 intr = new Vector2dTuple2();

            if (calc.Type == IntersectionType.Segment)
            {
                Result   = IntersectionResult.Intersects;
                Type     = IntersectionType.Segment;
                Quantity = 2;
                intr.V0  = calc.Point0;
                intr.V1  = calc.Point1;
            }
            else
            {
                Debug.Assert(calc.Type == IntersectionType.Point);
                //"Intersection must be a point\n";
                Result   = IntersectionResult.Intersects;
                Type     = IntersectionType.Point;
                Quantity = 1;
                intr.V0  = calc.Point0;
            }

            // Unproject the segment of intersection.
            if (maxNormal == 0)
            {
                double invNX = ((double)1) / plane.Normal.x;
                for (i = 0; i < Quantity; ++i)
                {
                    double y = intr[i].x;
                    double z = intr[i].y;
                    double x = invNX * (plane.Constant - plane.Normal.y * y - plane.Normal.z * z);
                    Points[i] = new Vector3d(x, y, z);
                }
            }
            else if (maxNormal == 1)
            {
                double invNY = ((double)1) / plane.Normal.y;
                for (i = 0; i < Quantity; ++i)
                {
                    double x = intr[i].x;
                    double z = intr[i].y;
                    double y = invNY * (plane.Constant - plane.Normal.x * x - plane.Normal.z * z);
                    Points[i] = new Vector3d(x, y, z);
                }
            }
            else
            {
                double invNZ = ((double)1) / plane.Normal.z;
                for (i = 0; i < Quantity; ++i)
                {
                    double x = intr[i].x;
                    double y = intr[i].y;
                    double z = invNZ * (plane.Constant - plane.Normal.x * x - plane.Normal.y * y);
                    Points[i] = new Vector3d(x, y, z);
                }
            }

            return(true);
        }
 public IntrSegment2Triangle2(Segment2d s, Triangle2d t)
 {
     segment = s; triangle = t;
 }