示例#1
0
        public int ShortestPathByLeeBFS(int[,] matrix, Point srcPoint, Point destPoint, Stack <Point> resultPath)
        {
            if (matrix == null || matrix.GetLength(0) == 0 || matrix.GetLength(1) == 0)
            {
                return(-1);
            }

            if (matrix[srcPoint.xPos, srcPoint.yPos] != 1 || matrix[destPoint.xPos, destPoint.yPos] != 1)
            {
                return(-1);
            }

            bool[,] visited = new bool[matrix.GetLength(0), matrix.GetLength(1)];

            // Mark the source cell as visited
            visited[srcPoint.xPos, srcPoint.yPos] = true;

            Queue <VertexM> VertexMQueue = new Queue <VertexM>();

            VertexM s = new VertexM()
            {
                Point = srcPoint, Distance = 0
            };

            VertexMQueue.Enqueue(s);  // Enqueue source cell
            resultPath.Push(srcPoint);

            // These arrays are used to get row and column numbers of 4 neighbours of a given cell
            int[] rowNum = { -1, 0, 0, 1 };
            int[] colNum = { 0, -1, 1, 0 };

            // Do a BFS starting from source cell
            while (VertexMQueue.Count > 0)
            {
                VertexM currVertexM = VertexMQueue.Dequeue();
                Point   currPoint   = currVertexM.Point;

                // Reached the destination cell, we are done
                if (currPoint.xPos == destPoint.xPos && currPoint.yPos == destPoint.yPos)
                {
                    return(currVertexM.Distance);
                }

                for (int lpCnt = 0; lpCnt < 4; lpCnt++)
                {
                    int rowPos = currPoint.xPos + rowNum[lpCnt];
                    int colPos = currPoint.yPos + colNum[lpCnt];

                    // If adjacent cell is valid, has path and not visited yet, enqueue it.
                    if (Point.IsSafePoint(matrix, rowPos, colPos) == true && visited[rowPos, colPos] == false)
                    {
                        visited[rowPos, colPos] = true;
                        Point point = new Point()
                        {
                            xPos = rowPos, yPos = colPos
                        };
                        VertexM Adjcell = new VertexM()
                        {
                            Point    = point,
                            Distance = currVertexM.Distance + 1
                        };
                    }
                }
            }
            return(-1);
        }
示例#2
0
        /// <summary>
        /// _3DModel 변환
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="extension"></param>
        /// <returns></returns>
        public Object3DModel Get3DModel(string filename, string extension)
        {
            Object3DModel stlModel = new Object3DModel();
            Metadata      metadata = new Metadata();

            metadata.formatVersion = "3";
            metadata.generatedBy   = "Mersh";
            stlModel.metadata      = metadata;
            stlModel.scale         = "1.0";
            stlModel.materials     = new string[] { };
            stlModel.morphTargets  = new string[] { };
            stlModel.morphColors   = new string[] { };
            stlModel.normals       = new string[] { };
            stlModel.colors        = new string[] { };
            stlModel.uvs           = new string[, ] {
            };

            int indexF = 0;
            int indexV = 0;

            if (extension == ".stl")
            {
                STLDocument facets = STLDocument.Open(filename);

                int index = 0;

                stlModel.faces    = new int[facets.Count() * 4];
                stlModel.vertices = new float[facets.Count() * 9];
                foreach (var facet in facets)
                {
                    stlModel.faces[index] = 0;

                    index++;
                    foreach (var vertice in facet.Vertices)
                    {
                        VertexM m = new VertexM();
                        stlModel.faces[index] = indexF;

                        stlModel.vertices[indexV] = vertice.X;
                        indexV++;
                        stlModel.vertices[indexV] = vertice.Y;
                        indexV++;
                        stlModel.vertices[indexV] = vertice.Z;
                        indexV++;

                        indexF++;
                        index++;
                    }
                }
            }

            if (extension == ".obj")
            {
                VertexList = new List <QuantumConcepts.Formats.StereoLithography.Vertex>();
                OBJDocument objDoc = new OBJDocument().LoadObj(filename);

                stlModel.faces    = new int[objDoc.FaceList.Count() * 4];
                stlModel.vertices = new float[objDoc.VertexList.Count() * 9];

                for (int i = 0; i < objDoc.FaceList.Count; i++)
                {
                    stlModel.faces[indexF] = 0;
                    indexF++;
                    stlModel.faces[indexF] = objDoc.FaceList[i].VertexIndexList[0] - 1;
                    indexF++;
                    stlModel.faces[indexF] = objDoc.FaceList[i].VertexIndexList[1] - 1;
                    indexF++;
                    stlModel.faces[indexF] = objDoc.FaceList[i].VertexIndexList[2] - 1;
                    indexF++;
                }

                for (int i = 0; i < objDoc.VertexList.Count; i++)
                {
                    VertexM m = new VertexM();
                    stlModel.vertices[indexV] = (float)objDoc.VertexList[i].X;
                    indexV++;
                    stlModel.vertices[indexV] = (float)objDoc.VertexList[i].Y;
                    indexV++;
                    stlModel.vertices[indexV] = (float)objDoc.VertexList[i].Z;
                    indexV++;
                }
            }

            return(stlModel);
        }