Beispiel #1
0
        private static void WritePointsToFile(string filePath, CVMesh mesh, bool exportColors)
        {
            var    indeces  = mesh.TriangleIndeces;
            var    vertices = mesh.Vertices;
            var    colors   = mesh.Colors;
            string header   = GetHeader(vertices.Count, indeces.Count / 3, exportColors);

            using (StreamWriter file = new StreamWriter(filePath))
            {
                file.WriteLine(header);                  //header
                for (int i = 0; i < vertices.Count; i++) //vertices + colors
                {
                    string line = GetPointString(vertices[i]);
                    if (exportColors)
                    {
                        line += " " + GetColorString(colors[i]);
                    }
                    file.WriteLine(line);
                }
                for (int i = 0; i < indeces.Count; i = i + 3)
                {
                    string baseIndex0 = indeces[i].ToString(CultureInfo.InvariantCulture);
                    string baseIndex1 = indeces[i + 1].ToString(CultureInfo.InvariantCulture);
                    string baseIndex2 = indeces[i + 2].ToString(CultureInfo.InvariantCulture);
                    string faceString = "3 " + baseIndex0 + " " + baseIndex1 + " " + baseIndex2;
                    file.WriteLine(faceString);
                }
            }
        }
        public static CVMesh Clone(CVMesh original)
        {
            CVMesh cloned = new CVMesh();

            cloned.Vertices = CloneVectors(original.Vertices);
            cloned.Normals  = CloneVectors(original.Normals);
            cloned.Colors.AddRange(original.Colors); //TODO: Clone? Or does the list get closed over automatically in AddRange
            cloned.TriangleIndeces.AddRange(original.TriangleIndeces);
            return(cloned);
        }
        [ExcludeFromCodeCoverage]                              //Cannot test this as it requires a Kinect ColorMesh which is dependent on the Kinect API:
        //you can't construct this yourself
        public static CVMesh ConvertToMesh(ColorMesh mesh)
        {
            CVMesh m = new CVMesh();

            m.Colors.AddRange(mesh.GetColors());
            m.Normals.AddRange(mesh.GetNormals());
            m.TriangleIndeces.AddRange(mesh.GetTriangleIndexes());
            m.Vertices.AddRange(mesh.GetVertices());
            return(m);
        }
Beispiel #4
0
        public static bool Output(CVMesh mesh, ref string location, bool exportColors = true)
        {
            string fileName = string.Format("pointcloud_mesh-{0:yyyy-MM-dd_hh-mm-ss-tt}.ply", DateTime.Now);

            location = location + "/" + fileName;
            var file = File.Create(location);

            file.Close();
            try
            {
                WritePointsToFile(location, mesh, exportColors);
                return(true);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error in PLYExporter: " + ex.Message);
                return(false);
            }
        }
Beispiel #5
0
        public static CVMesh ReadMesh(string location)
        {
            CVMesh         mesh             = new CVMesh();
            List <Vector3> vertices         = new List <Vector3>();
            List <int>     triangles        = new List <int>();
            int            vertexStartIndex = 10; //the lines before these is the header

            using (StreamReader sr = File.OpenText(location))
            {
                string s           = string.Empty;
                int    index       = 0;
                int    vertexIndex = 0;
                int    vertexCount = 0;
                bool   headerDone  = false;
                while ((s = sr.ReadLine()) != null)
                {
                    if (index == 3)
                    {
                        vertexCount = FetchIndexCount(s);
                    }
                    if (headerDone)
                    {
                        if (vertexIndex < vertexCount)
                        {
                            string[] xyz = s.Split(' ');
                            decimal  dx, dy, dz;
                            decimal.TryParse(xyz[0], NumberStyles.Float, CultureInfo.InvariantCulture, out dx);
                            decimal.TryParse(xyz[1], NumberStyles.Float, CultureInfo.InvariantCulture, out dy);
                            decimal.TryParse(xyz[2], NumberStyles.Float, CultureInfo.InvariantCulture, out dz);
                            float   x      = (float)dx;
                            float   y      = (float)dy;
                            float   z      = (float)dz;
                            Vector3 vector = new Vector3()
                            {
                                X = x, Y = y, Z = z
                            };
                            vertices.Add(vector);
                            vertexIndex++;
                        }
                        else
                        {
                            string[] threeABC = s.Split(' ');
                            int      a        = int.Parse(threeABC[1]);
                            int      b        = int.Parse(threeABC[2]);
                            int      c        = int.Parse(threeABC[3]);
                            triangles.Add(a);
                            triangles.Add(b);
                            triangles.Add(c);
                        }
                    }
                    if (s.Equals("end_header"))
                    {
                        headerDone = true;
                    }
                    index++;
                }
            }
            mesh.Vertices        = vertices;
            mesh.TriangleIndeces = triangles;
            return(mesh);
        }