Beispiel #1
0
 private static bool IsNeighbors(Vector3i tri1, Vector3i tri2)
 {
     if (tri1.ContainsValue(tri2.X) && (tri1.ContainsValue(tri2.Y) || tri1.ContainsValue(tri2.Z)))
     {
         return(true);
     }
     if (tri1.ContainsValue(tri2.Y) && tri1.ContainsValue(tri2.Z))
     {
         return(true);
     }
     return(false);
 }
Beispiel #2
0
            private bool GenerateOutsider(int index, out Outside outsider)
            {
                Vector3i triangle      = m_triangles[index];
                int      neighborCount = 0;
                bool     xy            = true;
                bool     yz            = true;
                bool     zx            = true;

                outsider = default;
                foreach (int checkIndex in m_indexes)
                {
                    if (checkIndex == index)
                    {
                        continue;
                    }
                    Vector3i check = m_triangles[checkIndex];
                    if (check.ContainsValue(triangle.X))
                    {
                        if (check.ContainsValue(triangle.Y))
                        {
                            xy = false;
                            if (++neighborCount == 3)
                            {
                                return(false);
                            }
                        }
                        else if (check.ContainsValue(triangle.Z))
                        {
                            zx = false;
                            if (++neighborCount == 3)
                            {
                                return(false);
                            }
                        }
                    }
                    else if (check.ContainsValue(triangle.Y) && (check.ContainsValue(triangle.Z)))
                    {
                        yz = false;
                        if (++neighborCount == 3)
                        {
                            return(false);
                        }
                    }
                }
                int vertex = xy ? (zx ? 2 : 0) : (yz ? 1 : 2);

                outsider = new Outside(index, vertex);
                return(true);
            }
Beispiel #3
0
        private static void FindOutsideVertex(IReadOnlyList <Vector3i> vertices, out int outsideVertexIndex, out int member)
        {
            for (outsideVertexIndex = 0; outsideVertexIndex < vertices.Count; outsideVertexIndex++)
            {
                Vector3i vertex = vertices[outsideVertexIndex];
                int      xCount = 0;
                int      yCount = 0;
                int      zCount = 0;
                for (int j = 0; j < vertices.Count; j++)
                {
                    Vector3i next = vertices[j];
                    if (next.ContainsValue(vertex.X))
                    {
                        xCount++;
                    }
                    if (next.ContainsValue(vertex.Y))
                    {
                        yCount++;
                    }
                    if (next.ContainsValue(vertex.Z))
                    {
                        zCount++;
                    }
                    if (xCount >= 3 && yCount >= 3 && yCount >= 3)
                    {
                        break;
                    }
                }

                if (xCount < 3)
                {
                    member = yCount <= zCount ? 0 : 2;
                    return;
                }
                if (yCount < 3)
                {
                    member = zCount <= xCount ? 1 : 0;
                    return;
                }
                if (zCount < 3)
                {
                    member = xCount <= yCount ? 2 : 1;
                    return;
                }
            }
            throw new Exception("Outside index wasn't found");
        }
Beispiel #4
0
        private void FindOutsideTriangle(out int outsideTriangle, out int outsideMember)
        {
            for (outsideTriangle = 0; outsideTriangle < m_triangles.Count; outsideTriangle++)
            {
                Vector3i vertex = m_triangles[outsideTriangle];
                int      xCount = 0;
                int      yCount = 0;
                int      zCount = 0;
                for (int j = 0; j < m_triangles.Count; j++)
                {
                    Vector3i next = m_triangles[j];
                    if (next.ContainsValue(vertex.X))
                    {
                        xCount++;
                    }
                    if (next.ContainsValue(vertex.Y))
                    {
                        yCount++;
                    }
                    if (next.ContainsValue(vertex.Z))
                    {
                        zCount++;
                    }
                    if (xCount >= 3 && yCount >= 3 && zCount >= 3)
                    {
                        break;
                    }
                }

                if (xCount < 3)
                {
                    outsideMember = yCount <= zCount ? 0 : 2;
                    return;
                }
                if (yCount < 3)
                {
                    outsideMember = zCount <= xCount ? 1 : 0;
                    return;
                }
                if (zCount < 3)
                {
                    outsideMember = xCount <= yCount ? 2 : 1;
                    return;
                }
            }
            throw new Exception("Outside triangle hasn't been found");
        }
Beispiel #5
0
            private Vector3i GetNextNeighbor(Vector3i tri, int vertex)
            {
                int member     = tri.GetMemberByValue(vertex);
                int nextVertex = tri.GetValueByMember(member + 1);

                foreach (int index in m_indexes)
                {
                    Vector3i check = m_triangles[index];
                    if (check.ContainsValue(vertex) && check.ContainsValue(nextVertex))
                    {
                        if (check != tri)
                        {
                            return(check);
                        }
                    }
                }
                return(default);
Beispiel #6
0
            public void SearchNext()
            {
                NextVertex   = -1;
                NextTriangle = -1;
                float lowestAngle = 360.0f;

                for (int i = 0; i < m_triangles.Count; i++)
                {
                    if (i == CurrentTriangle)
                    {
                        continue;
                    }

                    Vector3i triangle = m_triangles[i];
                    if (!triangle.ContainsValue(CurrentVertex))
                    {
                        continue;
                    }

                    int checkVertexMember = triangle.GetMemberByValue(CurrentVertex);
                    int checkVertex       = triangle.GetValueByMember(checkVertexMember + 1);
                    if (m_line.Contains(checkVertex))
                    {
                        continue;
                    }

                    Vector2f checkVertexValue   = m_vertices[checkVertex].ToVector2();
                    int      checkTriangleIndex = i;
                    if (checkVertexValue == CurrentVertexValue)
                    {
                        // recursivly find next non matching vertex
                        m_line.Add(checkVertex);
                        OutsideVertexSearcher deepSearcher = new OutsideVertexSearcher(this);
                        deepSearcher.CurrentTriangle = i;
                        deepSearcher.CurrentVertex   = checkVertex;
                        deepSearcher.SearchNext();
                        m_line.RemoveAt(m_line.Count - 1);

                        if (deepSearcher.NextVertex == -1)
                        {
                            continue;
                        }

                        checkVertex        = deepSearcher.NextVertex;
                        checkVertexValue   = m_vertices[deepSearcher.NextVertex].ToVector2();
                        checkTriangleIndex = deepSearcher.NextTriangle;
                    }

                    float angle = Vector2f.AngleFrom3Points(PreviousVertexValue, CurrentVertexValue, checkVertexValue);
                    if (angle <= lowestAngle)
                    {
                        NextVertex   = checkVertex;
                        NextTriangle = checkTriangleIndex;
                        lowestAngle  = angle;
                    }
                }
            }
            private bool GetNextNeighbor(Vector3i tri, int vertex, out Vector3i result)
            {
                int member     = tri.GetMemberByValue(vertex);
                int nextVertex = tri.GetValueByMember(member + 1);

                foreach (int index in m_indexes)
                {
                    Vector3i check = m_triangles[index];
                    if (check.ContainsValue(vertex) && check.ContainsValue(nextVertex))
                    {
                        if (check != tri)
                        {
                            result = check;
                            return(true);
                        }
                    }
                }
                result = default;
                return(false);
            }
Beispiel #8
0
        private static Vector2f[] VerticesToOutline(IReadOnlyList <Vector3f> vertexValues, List <Vector3i> vertices)
        {
            FindOutsideVertex(vertices, out int outsideVertexIndex, out int member);
            Vector3i   outsideVertex = vertices[outsideVertexIndex];
            List <int> line          = new List <int>();
            int        point1        = outsideVertex.GetValueByMember(member);
            int        point2        = outsideVertex.GetValueByMember(member + 1);

            line.Add(point1);
            line.Add(point2);

            int      currentPoint   = point2;
            int      currentIndex   = outsideVertexIndex;
            Vector2f prevPointValue = vertexValues[point1].ToVector2();
            Vector2f curPointValue  = vertexValues[point2].ToVector2();

            while (true)
            {
                int   nextPoint       = -1;
                int   nextVertexIndex = -1;
                float lowestAngle     = 360.0f;
                for (int i = 0; i < vertices.Count; i++)
                {
                    if (i == currentIndex)
                    {
                        continue;
                    }

                    Vector3i vertex = vertices[i];
                    if (!vertex.ContainsValue(currentPoint))
                    {
                        continue;
                    }

                    int checkPointMember = vertex.GetMemberByValue(currentPoint);
                    int checkPoint       = vertex.GetValueByMember(checkPointMember + 1);
                    if (line.Contains(checkPoint))
                    {
                        continue;
                    }

                    Vector2f nextPointValue = vertexValues[checkPoint].ToVector2();
                    float    angle          = Vector2f.AngleFrom3Points(prevPointValue, curPointValue, nextPointValue);
                    if (angle < lowestAngle)
                    {
                        nextPoint       = checkPoint;
                        nextVertexIndex = i;
                        lowestAngle     = angle;
                    }
                }

                if (nextVertexIndex == -1)
                {
                    Vector3i currentVertex   = vertices[currentIndex];
                    int      nextPointMember = currentVertex.GetMemberByValue(currentPoint);
                    nextPoint       = currentVertex.GetValueByMember(nextPointMember + 1);
                    nextVertexIndex = currentIndex;
                    if (line.Contains(nextPoint))
                    {
                        break;
                    }
                }

                prevPointValue = curPointValue;
                curPointValue  = vertexValues[nextPoint].ToVector2();

                currentPoint = nextPoint;
                line.Add(nextPoint);

                currentIndex = nextVertexIndex;
            }

            Vector2f[] outline = new Vector2f[line.Count];
            for (int i = 0; i < line.Count; i++)
            {
                outline[i] = vertexValues[line[i]].ToVector2();
            }

            HashSet <int> deletePoints = new HashSet <int>();

            foreach (int point in line)
            {
                deletePoints.Add(point);
            }
            while (true)
            {
                bool isNew = false;
                for (int i = 0; i < vertices.Count; i++)
                {
                    Vector3i vertex = vertices[i];
                    if (deletePoints.Contains(vertex.X))
                    {
                        isNew |= deletePoints.Add(vertex.Y);
                        isNew |= deletePoints.Add(vertex.Z);
                        vertices.RemoveAt(i--);
                    }
                    else if (deletePoints.Contains(vertex.Y))
                    {
                        isNew |= deletePoints.Add(vertex.Z);
                        isNew |= deletePoints.Add(vertex.X);
                        vertices.RemoveAt(i--);
                    }
                    else if (deletePoints.Contains(vertex.Z))
                    {
                        isNew |= deletePoints.Add(vertex.X);
                        isNew |= deletePoints.Add(vertex.Y);
                        vertices.RemoveAt(i--);
                    }
                }
                if (!isNew)
                {
                    break;
                }
            }

            return(outline);
        }