Exemple #1
0
 public Triangle(int[] points, Plane plane, BlockingDirection direction)
 {
     this.points    = points;
     this.plane     = plane;
     this.direction = direction;
 }
Exemple #2
0
    private static BlockingDirection GetBlockingDir(List <Vector3> points, List <Triangle> trianglesList, List <int[]> edges, int i, BlockingDirection blockedFaces, Triangle t)
    {
        Plane          trianglePlane  = new Plane(points[trianglesList[i].points[0]], points[trianglesList[i].points[1]], points[trianglesList[i].points[2]]);
        List <Vector2> mappedTriangle = trianglePlane.GetMappedPoints(new List <Vector3>()
        {
            points[trianglesList[i].points[0]], points[trianglesList[i].points[1]], points[trianglesList[i].points[2]]
        });

        //if this triangle is already blocked in both directions, ignore this triangle.
        if (blockedFaces == BlockingDirection.BOTH)
        {
            return(blockedFaces);
        }
        for (int i1 = 0; i1 < edges.Count; i1++)
        {
            // if any of the indexes match, ignore this edge
            bool indexesMatch = false;
            for (int i2 = 0; i2 < 3; i2++)
            {
                for (int i3 = 0; i3 < 2; i3++)
                {
                    if (t.points[i2] == edges[i1][i3])
                    {
                        indexesMatch = true;
                        break;
                    }
                }
                if (indexesMatch)
                {
                    break;
                }
            }
            if (indexesMatch)
            {
                continue;
            }

            // if this line doesn't intersect with the triangle, ignore this edge.
            Vector2 mappedPointA = trianglePlane.GetMappedPoint(points[edges[i1][0]]);
            Vector2 mappedPointB = trianglePlane.GetMappedPoint(points[edges[i1][1]]);


            bool lineIntersectsTriangle = false;
            for (int i2 = 0; i2 < 3; i2++)
            {
                Vector2 triangleA = trianglePlane.GetMappedPoint(points[t.points[i2]]);
                Vector2 triangleB = trianglePlane.GetMappedPoint(points[t.points[(i2 + 1) % 3]]);


                if (AreLinesIntersecting(mappedPointA, mappedPointB, triangleA, triangleB, false))
                {
                    lineIntersectsTriangle = true;
                    break;
                }
            }
            if (!lineIntersectsTriangle)
            {
                continue;
            }

            int signA = Math.Sign(Math.Round(trianglePlane.GetDistToPlane(points[edges[i1][0]]), 2));
            int signB = Math.Sign(Math.Round(trianglePlane.GetDistToPlane(points[edges[i1][1]]), 2));

            // if sign A is not the same as sign B, or if sign A or sign B is 0, ignore this edge.
            if (signA != signB || signA == 0 || signB == 0)
            {
                continue;
            }

            if (signA == 1)
            {
                if (blockedFaces == BlockingDirection.AWAY_FROM_NORMAL)
                {
                    blockedFaces = BlockingDirection.BOTH;
                    break;
                }
                else
                {
                    blockedFaces = BlockingDirection.TOWARDS_NORMAL;
                }
            }
            else
            {
                if (blockedFaces == BlockingDirection.TOWARDS_NORMAL)
                {
                    blockedFaces = BlockingDirection.BOTH;
                    break;
                }
                else
                {
                    blockedFaces = BlockingDirection.AWAY_FROM_NORMAL;
                }
            }
        }
        drawTriangles--;
        return(blockedFaces);
    }
Exemple #3
0
    /* Parameters:
     * points: points of the mesh
     * faces: indexes of points that make up the triangles of the 3D mesh
     *
     * Returns:
     * List of Triangles with the direction of their 'normals'/ the direction that the triangles should be rendered in. If a direction can't be determined, then it's left as 0.
     */
    private static List <Triangle> GetKnownTriangles(List <Vector3> points, List <int[]> triangles)
    {
        List <Triangle> trianglesList = new List <Triangle>();

        for (int i = 0; i < triangles.Count; i++)
        {
            Array.Sort(triangles[i]);
        }
        List <int[]> edges = new List <int[]>();

        // go through all the points for each triangle, find points that are either in front of or behind those triangles.
        for (int i = 0; i < triangles.Count; i++)
        {
            edges.Add(new int[] { triangles[i][0], triangles[i][1] });
            edges.Add(new int[] { triangles[i][0], triangles[i][2] });
            edges.Add(new int[] { triangles[i][1], triangles[i][2] });

            Plane          trianglePlane  = new Plane(points[triangles[i][0]], points[triangles[i][1]], points[triangles[i][2]]);
            List <Vector2> mappedTriangle = trianglePlane.GetMappedPoints(new List <Vector3>()
            {
                points[triangles[i][0]], points[triangles[i][1]], points[triangles[i][2]]
            });
            BlockingDirection blockedFaces = BlockingDirection.NONE;
            for (int i1 = 0; i1 < points.Count; i1++)
            {
                if (i1 == triangles[i][0] || i1 == triangles[i][1] || i1 == triangles[i][2])
                {
                    //ignore this point if it's one of the triangle's points
                    continue;
                }
                Vector2 mappedPoint = trianglePlane.GetMappedPoint(points[i1]);
                if (!PointInTriangle(mappedPoint, mappedTriangle[0], mappedTriangle[1], mappedTriangle[2]))
                {
                    //ignore this point if it's not behind the triangle
                    continue;
                }
                //rounding accuracy to hundredths place to
                int pointSign = Math.Sign(Math.Round(trianglePlane.GetDistToPlane(points[i1]), 2));
                if (pointSign == 0)
                {
                    //ignore this point if it's on the same plane
                    continue;
                }

                if (pointSign == 1)
                {
                    if (blockedFaces == BlockingDirection.AWAY_FROM_NORMAL)
                    {
                        blockedFaces = BlockingDirection.BOTH;
                        break;
                    }
                    else
                    {
                        if (drawTriangles > 0)
                        {
                        }
                        blockedFaces = BlockingDirection.TOWARDS_NORMAL;
                    }
                }
                else
                {
                    if (blockedFaces == BlockingDirection.TOWARDS_NORMAL)
                    {
                        blockedFaces = BlockingDirection.BOTH;
                        break;
                    }
                    else
                    {
                        if (drawTriangles > 0)
                        {
                        }
                        blockedFaces = BlockingDirection.AWAY_FROM_NORMAL;
                    }
                }
            }
            trianglesList.Add(new Triangle(triangles[i], trianglePlane, blockedFaces));
        }

        drawTriangles = 1;
        // go through all the connections for each triangle, find lines that are either in front of or behind those triangles.
        for (int i = 0; i < trianglesList.Count; i++)
        {
            Plane          trianglePlane  = new Plane(points[triangles[i][0]], points[triangles[i][1]], points[triangles[i][2]]);
            List <Vector2> mappedTriangle = trianglePlane.GetMappedPoints(new List <Vector3>()
            {
                points[triangles[i][0]], points[triangles[i][1]], points[triangles[i][2]]
            });
            BlockingDirection blockedFaces = trianglesList[i].direction;

            //if this triangle is already blocked in both directions, ignore this triangle.
            if (blockedFaces == BlockingDirection.BOTH)
            {
                continue;
            }
            for (int i1 = 0; i1 < edges.Count; i1++)
            {
                // if any of the indexes match, ignore this edge
                bool indexesMatch = false;
                for (int i2 = 0; i2 < 3; i2++)
                {
                    for (int i3 = 0; i3 < 2; i3++)
                    {
                        if (trianglesList[i].points[i2] == edges[i1][i3])
                        {
                            indexesMatch = true;
                            break;
                        }
                    }
                    if (indexesMatch)
                    {
                        break;
                    }
                }
                if (indexesMatch)
                {
                    continue;
                }

                // if this line doesn't intersect with the triangle, ignore this edge.
                Vector2 mappedPointA = trianglePlane.GetMappedPoint(points[edges[i1][0]]);
                Vector2 mappedPointB = trianglePlane.GetMappedPoint(points[edges[i1][1]]);


                bool lineIntersectsTriangle = false;
                for (int i2 = 0; i2 < 3; i2++)
                {
                    Vector2 triangleA = trianglePlane.GetMappedPoint(points[trianglesList[i].points[i2]]);
                    Vector2 triangleB = trianglePlane.GetMappedPoint(points[trianglesList[i].points[(i2 + 1) % 3]]);


                    if (AreLinesIntersecting(mappedPointA, mappedPointB, triangleA, triangleB, false))
                    {
                        lineIntersectsTriangle = true;
                        break;
                    }
                }
                if (!lineIntersectsTriangle)
                {
                    continue;
                }

                int signA = Math.Sign(Math.Round(trianglePlane.GetDistToPlane(points[edges[i1][0]]), 2));
                int signB = Math.Sign(Math.Round(trianglePlane.GetDistToPlane(points[edges[i1][1]]), 2));

                // if sign A is not the same as sign B, or if sign A or sign B is 0, ignore this edge.
                if (signA != signB || signA == 0 || signB == 0)
                {
                    continue;
                }

                if (signA == 1)
                {
                    if (blockedFaces == BlockingDirection.AWAY_FROM_NORMAL)
                    {
                        blockedFaces = BlockingDirection.BOTH;
                        break;
                    }
                    else
                    {
                        blockedFaces = BlockingDirection.TOWARDS_NORMAL;
                    }
                }
                else
                {
                    if (blockedFaces == BlockingDirection.TOWARDS_NORMAL)
                    {
                        blockedFaces = BlockingDirection.BOTH;
                        break;
                    }
                    else
                    {
                        blockedFaces = BlockingDirection.AWAY_FROM_NORMAL;
                    }
                }
            }
            drawTriangles--;
            trianglesList[i].direction = blockedFaces;
        }

        return(trianglesList);
    }
Exemple #4
0
    private static int GetFirstKnownTriangle(List <Vector3> points, List <Triangle> trianglesList, out BlockingDirection dir)
    {
        List <int[]> edges = new List <int[]>();

        for (int i = 0; i < trianglesList.Count; i++)
        {
            edges.Add(new int[] { trianglesList[i].points[0], trianglesList[i].points[1] });
            edges.Add(new int[] { trianglesList[i].points[0], trianglesList[i].points[2] });
            edges.Add(new int[] { trianglesList[i].points[1], trianglesList[i].points[2] });
        }

        for (int i = 0; i < trianglesList.Count; i++)
        {
            Plane          trianglePlane  = new Plane(points[trianglesList[i].points[0]], points[trianglesList[i].points[1]], points[trianglesList[i].points[2]]);
            List <Vector2> mappedTriangle = trianglePlane.GetMappedPoints(new List <Vector3>()
            {
                points[trianglesList[i].points[0]], points[trianglesList[i].points[1]], points[trianglesList[i].points[2]]
            });
            BlockingDirection blockedFaces = BlockingDirection.NONE;
            for (int i1 = 0; i1 < points.Count; i1++)
            {
                if (i1 == trianglesList[i].points[0] || i1 == trianglesList[i].points[1] || i1 == trianglesList[i].points[2])
                {
                    //ignore this point if it's one of the triangle's points
                    continue;
                }
                Vector2 mappedPoint = trianglePlane.GetMappedPoint(points[i1]);
                if (!PointInTriangle(mappedPoint, mappedTriangle[0], mappedTriangle[1], mappedTriangle[2]))
                {
                    //ignore this point if it's not behind the triangle
                    continue;
                }
                //rounding accuracy to hundredths place to
                int pointSign = Math.Sign(Math.Round(trianglePlane.GetDistToPlane(points[i1]), 2));
                if (pointSign == 0)
                {
                    //ignore this point if it's on the same plane
                    continue;
                }

                if (pointSign == 1)
                {
                    if (blockedFaces == BlockingDirection.AWAY_FROM_NORMAL)
                    {
                        blockedFaces = BlockingDirection.BOTH;
                        break;
                    }
                    else
                    {
                        if (drawTriangles > 0)
                        {
                        }
                        blockedFaces = BlockingDirection.TOWARDS_NORMAL;
                    }
                }
                else
                {
                    if (blockedFaces == BlockingDirection.TOWARDS_NORMAL)
                    {
                        blockedFaces = BlockingDirection.BOTH;
                        break;
                    }
                    else
                    {
                        if (drawTriangles > 0)
                        {
                        }
                        blockedFaces = BlockingDirection.AWAY_FROM_NORMAL;
                    }
                }
            }
            Triangle t = new Triangle(trianglesList[i].points, trianglePlane, blockedFaces);
            t.direction = GetBlockingDir(points, trianglesList, edges, i, t.direction, t);
            if (t.GetDirInt() != 0)
            {
                dir = t.direction;
                return(i);
            }
        }
        dir = BlockingDirection.NONE;
        return(-1);
    }