Exemple #1
0
        /// <summary>
        /// reads the OBJ file ONLY with the special format used also in the write_OBJ method
        /// </summary>
        /// <param name="path"></param>
        /// <param name="fileNameShort"></param>
        /// <param name="depthData"></param>
        /// <returns></returns>
        public static PointCloudVertices FromObjFile(string path, string fileNameShort)
        {
            string fileName = path + "\\" + fileNameShort;

            if (!System.IO.File.Exists(fileName))
            {
                System.Diagnostics.Debug.WriteLine("File does not exist: ");
                return(null);
            }
            double[] p3D = new double[3];
            System.Collections.ArrayList lineList = new System.Collections.ArrayList();


            PointCloudVertices pointCloud = new PointCloudVertices();


            //int numberOfDepthPointsNonZero = -1;
            int i          = 0;
            int startIndex = 0;

            try
            {
                string[] lines = System.IO.File.ReadAllLines(fileName);

                //ignore the comment lines

                for (i = 0; i < lines.Length; i++)
                {
                    if (!lines[i].Contains('#'))
                    {
                        startIndex = i;
                        break;
                    }
                }

                for (i = startIndex; i < lines.GetLength(0); i++)
                //for (i = lines.Length -1 ; i >= startIndex + 1; i--)
                {
                    string[] arrStr1 = lines[i].Split(new Char[] { ' ' });
                    if (lines[i].Contains('#') || arrStr1.Length < 4)
                    {
                        //ignore empty and comment lines
                        continue;
                    }

                    if (arrStr1[0] == "vn")
                    {
                        //ignore vector normals for now
                    }
                    if (arrStr1[0] == "v")
                    {
                        if (arrStr1.Length < 7)
                        {
                            System.Windows.Forms.MessageBox.Show("Error reading file " + fileNameShort + " in line i");
                        }
                        else
                        {
                            for (int j = 0; j < 3; j++)
                            {
                                p3D[j] = Convert.ToSingle(arrStr1[j + 1], CultureInfo);
                            }
                            Vector3d vec   = new Vector3d(p3D[0], p3D[1], p3D[2]);
                            float[]  color = new float[4] {
                                Convert.ToSingle(arrStr1[4], CultureInfo), Convert.ToSingle(arrStr1[5], CultureInfo), Convert.ToSingle(arrStr1[6], CultureInfo), 1f
                            };
                            System.Drawing.Color c = System.Drawing.Color.White;
                            c = c.FromFloatsARGB(color[3], color[0], color[1], color[2]);

                            Vertex v = new Vertex(vec, c);
                            pointCloud.Add(v);


                            //if (vec.Z > 0)
                            //{
                            //    numberOfDepthPointsNonZero++;

                            //}
                        }
                    }
                }
            }
            catch (Exception err)
            {
                System.Windows.Forms.MessageBox.Show("Read_OBJ: read error in file :  " + fileNameShort + " : at line: " + i.ToString() + " ; " + err.Message);
            }

            return(pointCloud);
        }