Exemple #1
0
        public Graph(Mesh mesh)
        {
            Vertices = mesh.Vertices.ToPoint3dArray().ToList();

            int numberOfCurves = mesh.TopologyEdges.Count * 2;

            Edges         = new Curve[numberOfCurves];
            EdgesWeights  = new double[numberOfCurves];
            GraphArray    = new int[2, numberOfCurves];
            AdjacencyList = new AdjacencyList(Vertices.Count);

            MeshTopologyEdgeList topologyEdgeList = mesh.TopologyEdges;

            int currentEdgeCount = 0;

            for (int i = 0; i < topologyEdgeList.Count; i++)
            {
                int firstVertexIndex  = topologyEdgeList.GetTopologyVertices(i).I;
                int secondVertexIndex = topologyEdgeList.GetTopologyVertices(i).J;

                double currentEdgeWeight = topologyEdgeList.EdgeLine(i).Length;

                Curve currentEdge = topologyEdgeList.EdgeLine(i).ToNurbsCurve();
                Edges[currentEdgeCount]         = currentEdge;
                EdgesWeights[currentEdgeCount]  = currentEdgeWeight;
                GraphArray[0, currentEdgeCount] = firstVertexIndex;
                GraphArray[1, currentEdgeCount] = secondVertexIndex;

                AdjacencyList.Vertices[firstVertexIndex].Add(secondVertexIndex);
                AdjacencyList.Edges[firstVertexIndex].Add(currentEdgeCount);
                currentEdgeCount += 1;

                Curve duplicateReversedEdge = currentEdge.DuplicateCurve();
                duplicateReversedEdge.Reverse();
                Edges[currentEdgeCount]         = duplicateReversedEdge;
                EdgesWeights[currentEdgeCount]  = currentEdgeWeight;
                GraphArray[0, currentEdgeCount] = secondVertexIndex;
                GraphArray[1, currentEdgeCount] = firstVertexIndex;

                AdjacencyList.Vertices[secondVertexIndex].Add(firstVertexIndex);
                AdjacencyList.Edges[secondVertexIndex].Add(currentEdgeCount);
                currentEdgeCount += 1;
            }
        }
Exemple #2
0
        public Graph(List <GraphPart> graphParts, double tolerance)
        {
            GraphParts = graphParts;

            Vertices = new List <Point3d>();

            int numberOfCurves = 0;

            foreach (var graphPart in graphParts)
            {
                if (graphPart.IsDirected)
                {
                    numberOfCurves += 1;
                }
                else
                {
                    numberOfCurves += 2;
                }
            }

            Edges        = new Curve[numberOfCurves];
            EdgesWeights = new double[numberOfCurves];
            GraphArray   = new int[2, numberOfCurves];

            int edgesCount = 0;

            foreach (var graphPart in graphParts)
            {
                bool foundDuplicateStartVertex = false;
                bool foundDuplicateEndVertex   = false;

                int firstVertexIndex;
                int secondVertexIndex;

                for (int j = 0; j < Vertices.Count; j++)
                {
                    if (foundDuplicateStartVertex && foundDuplicateEndVertex)
                    {
                        break;
                    }
                    if (!foundDuplicateStartVertex && Tools.CheckIfPointsAreSame(graphPart.StartVertex, Vertices[j], tolerance))
                    {
                        firstVertexIndex          = j;
                        GraphArray[0, edgesCount] = firstVertexIndex;
                        foundDuplicateStartVertex = true;
                    }
                    if (!foundDuplicateEndVertex && Tools.CheckIfPointsAreSame(graphPart.EndVertex, Vertices[j], tolerance))
                    {
                        secondVertexIndex         = j;
                        GraphArray[1, edgesCount] = secondVertexIndex;
                        foundDuplicateEndVertex   = true;
                    }
                }

                if (!foundDuplicateStartVertex)
                {
                    Vertices.Add(graphPart.StartVertex);
                    firstVertexIndex          = Vertices.Count - 1;
                    GraphArray[0, edgesCount] = firstVertexIndex;
                }
                if (!foundDuplicateEndVertex)
                {
                    Vertices.Add(graphPart.EndVertex);
                    secondVertexIndex         = Vertices.Count - 1;
                    GraphArray[1, edgesCount] = secondVertexIndex;
                }

                Edges[edgesCount]        = graphPart.Edge;
                EdgesWeights[edgesCount] = graphPart.EdgeWeight;
                edgesCount += 1;

                if (!graphPart.IsDirected)
                {
                    Curve reversedEdge = graphPart.Edge.DuplicateCurve();
                    reversedEdge.Reverse();
                    Edges[edgesCount]         = reversedEdge;
                    EdgesWeights[edgesCount]  = graphPart.EdgeWeight;
                    GraphArray[0, edgesCount] = GraphArray[1, edgesCount - 1];
                    GraphArray[1, edgesCount] = GraphArray[0, edgesCount - 1];

                    edgesCount += 1;
                }
            }

            AdjacencyList = new AdjacencyList(Vertices.Count, GraphArray);
        }