示例#1
0
文件: Mesh.cs 项目: mguzdek/terremesh
        public virtual void Read(out Mesh mesh, IProgressListener listener = null)
        {
            mesh = new Mesh();

            Regex r = new Regex(@" |//");
            NumberStyles numberStyle = NumberStyles.Float;
            IFormatProvider numberFormatProvider = CultureInfo.InvariantCulture;

            if (listener != null)
            {
                listener.OnStarted("Load mesh");
            }

            int indicesCount = 0;

            while (m_Reader.Peek() >= 0)
            {
                string line = m_Reader.ReadLine();

                String[] elements = r.Split(line);

                // List<Vertex> normals;
                switch (elements[0])
                {
                    case "v":
                        {
                            float x, y, z;
                            if (
                                float.TryParse(elements[1], numberStyle, numberFormatProvider, out x) &&
                                float.TryParse(elements[2], numberStyle, numberFormatProvider, out y) &&
                                float.TryParse(elements[3], numberStyle, numberFormatProvider, out z)
                                )
                            {
                                mesh.Vertices.Add(++indicesCount, new Vertex(new Vector3(x, y, z)));
                            }
                            else
                            {
                                Trace.WriteLine(line);
                            }
                            break;
                        }
                    case "f":
                        {
                            int f1, f2, f3;
                            int f1Index = 1;
                            int f2Index = 2;
                            int f3Index = 3;

                            switch (elements.Length)
                            {
                                case 7:
                                    {
                                        //normal indexes in elements[2],elements[4] and elements[6] are ignored;
                                        f1Index = 1;
                                        f2Index = 3;
                                        f3Index = 5;

                                        goto case 4;
                                    }
                                case 4:
                                    {
                                        if (
                                            int.TryParse(elements[f1Index], numberStyle, numberFormatProvider, out f1) &&
                                            int.TryParse(elements[f2Index], numberStyle, numberFormatProvider, out f2) &&
                                            int.TryParse(elements[f3Index], numberStyle, numberFormatProvider, out f3)
                                            )
                                        {
                                            mesh.Faces.Add(new Triangle(f1, f2, f3));
                                        }
                                        else
                                        {
                                            Trace.WriteLine(line);
                                            throw new NotImplementedException("Only triangles are Implemented. Faces in file are not a triangles.That is bad:(");
                                        }
                                    }
                                    break;
                                default:
                                    Trace.WriteLine("Invalid number of components");
                                    break;
                            }
                            break;
                        }
                    case "vn":
                        {
                            break;
                        }
                    default:
                        Trace.WriteLine("Unknown obj specifier.");
                        break;

                }

                if (listener != null)
                {
                    listener.OnStep(
                        m_Reader.BaseStream.Position,
                        m_Reader.BaseStream.Length);
                }
            }

            if (listener != null)
            {
                listener.OnComplete("Mesh loaded successfully");
            }

            mesh.Invalidate(listener, Mesh.InvalidateFlags.Faces);
        }
示例#2
0
文件: Mesh.cs 项目: mguzdek/terremesh
        public void WriteMesh(ref Mesh mesh, IProgressListener listener = null)
        {
            long current = 0;
            long total = mesh.Vertices.Count + mesh.Faces.Count;

            listener.OnStarted("Mesh write");

            foreach (var vertex in mesh.Vertices)
            {
                m_Writer.WriteLine(string.Format(
                    CultureInfo.InvariantCulture,
                    "v {0} {1} {2}",
                    vertex.Value.Position.X.ToString("0.000000", CultureInfo.InvariantCulture),
                    vertex.Value.Position.Y.ToString("0.000000", CultureInfo.InvariantCulture),
                    vertex.Value.Position.Z.ToString("0.000000", CultureInfo.InvariantCulture)));

                ++current;

                if (listener != null)
                {
                    listener.OnStep(current, total);
                }
            }

            int count = 0;
            foreach (var face in mesh.Faces)
            {
                m_Writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "f {0} {1} {2}",
                    face[0], face[1], face[2]));

                ++count;
                ++current;

                if (listener != null)
                {
                    listener.OnStep(current, total);
                }
            }

            if (listener != null)
            {
                listener.OnComplete("Mesh write");
            }

            m_Writer.Close();
        }