Пример #1
0
        /// <summary>
        /// Writes the whole B-rep scene to a given text stream (uses text variant of Stanford PLY format).
        /// </summary>
        /// <param name="writer">Already open text writer</param>
        /// <param name="scene">Scene to write</param>
        public void WriteBrepSelection(StreamWriter writer, SceneBrep scene, Selection selection,
                                       IProgress <string> progress, CancellationToken cancel, out List <int> trianglePointers, out List <int> vertexPointers)
        {
            DoNormals   = true;
            DoTxtCoords = true;
            DoColors    = true;

            vertexPointers   = new List <int>();
            trianglePointers = new List <int>();

            if (scene == null || selection.GetCount() < 3)
            {
                return;
            }

            vertexPointers = selection.SelectionPointers;

            Debug.Assert(TextFormat);
            if (!NativeNewLine)
            {
                writer.NewLine = "\r";     // CR only
            }
            bool writeNormals   = DoNormals && scene.HasNormals();
            bool writeTxtCoords = DoTxtCoords && scene.HasTxtCoords();
            bool writeColors    = DoColors && scene.HasColors();

            writer.WriteLine(HEADER);
            writer.WriteLine(FORMAT_TEXT);

            // vertex-header:
            writer.WriteLine("{0} {1} {2}", ELEMENT, VERTEX, selection.GetCount());
            writer.WriteLine("{0} float x", PROPERTY);
            writer.WriteLine("{0} float {1}", PROPERTY, Orientation ? 'z' : 'y');
            writer.WriteLine("{0} float {1}", PROPERTY, Orientation ? 'y' : 'z');
            if (writeNormals)
            {
                writer.WriteLine("{0} float {1}", PROPERTY, NORMAL_X);
                writer.WriteLine("{0} float {1}", PROPERTY, Orientation ? NORMAL_Z : NORMAL_Y);
                writer.WriteLine("{0} float {1}", PROPERTY, Orientation ? NORMAL_Y : NORMAL_Z);
            }
            if (writeTxtCoords)
            {
                writer.WriteLine("{0} float {1}", PROPERTY, TEXTURE_S);
                writer.WriteLine("{0} float {1}", PROPERTY, TEXTURE_T);
            }
            if (writeColors)
            {
                writer.WriteLine("{0} float {1}", PROPERTY, COLOR_R);
                writer.WriteLine("{0} float {1}", PROPERTY, COLOR_G);
                writer.WriteLine("{0} float {1}", PROPERTY, COLOR_B);
            }

            var triangles = selection.GetSelectedTriangles();
            var list      = new List <int>();

            int A, B, C;

            for (int j = 0; j < triangles.Count; j++)
            {
                scene.GetTriangleVertices(triangles[j], out A, out B, out C);
                int Anew = selection.SelectionPointers.IndexOf(A);
                int Bnew = selection.SelectionPointers.IndexOf(B);
                int Cnew = selection.SelectionPointers.IndexOf(C);

                // if this vertex was not selected, do not export the triangle containing it
                if (Anew == -1 || Bnew == -1 || Cnew == -1)
                {
                    continue;
                }

                // save vertex indices of triangles that were selected
                // this supports bach export
                trianglePointers.Add(triangles[j]);

                // otherwise add vertices with their new indices to list
                list.Add(Anew);
                list.Add(Bnew);
                list.Add(Cnew);

                cancel.ThrowIfCancellationRequested();
            }

            // face-header:
            writer.WriteLine("{0} {1} {2}", ELEMENT, FACE, list.Count / 3);
            writer.WriteLine("{0} list uchar int vertex_indices", PROPERTY);

            writer.WriteLine(END_HEADER);

            // vertex-data:
            int           i;
            Vector3       v3;
            Vector2       v2;
            StringBuilder sb            = new StringBuilder();
            var           selectionEnum = selection.GetEnumerator();

            int totalCount = selection.GetCount() + list.Count / 3;

            for (i = 0; i < selection.GetCount(); i++)
            {
                cancel.ThrowIfCancellationRequested();

                selectionEnum.MoveNext();
                v3 = scene.GetVertex(selectionEnum.Current);

                sb.Clear();
                sb.AppendFormat(CultureInfo.InvariantCulture, "{0} {1} {2}", v3.X, v3.Y, v3.Z);
                if (writeNormals)
                {
                    v3 = scene.GetNormal(selectionEnum.Current);
                    sb.AppendFormat(CultureInfo.InvariantCulture, " {0} {1} {2}", v3.X, v3.Y, v3.Z);
                }
                if (writeTxtCoords)
                {
                    v2 = scene.GetTxtCoord(selectionEnum.Current);
                    sb.AppendFormat(CultureInfo.InvariantCulture, " {0} {1}", v2.X, v2.Y);
                }
                if (writeColors)
                {
                    v3 = scene.GetColor(selectionEnum.Current);
                    sb.AppendFormat(CultureInfo.InvariantCulture, " {0} {1} {2}", v3.X, v3.Y, v3.Z);
                }
                writer.WriteLine(sb.ToString());

                if (progress != null)
                {
                    progress.Report((i * 100 / totalCount).ToString());
                }
            }

            // face-data:
            for (i = 0; i < list.Count; i += 3)
            {
                writer.WriteLine("3 {0} {1} {2}", list[i], Orientation ? list[i + 2] : list[i + 1], Orientation ? list[i + 1] : list[i + 2]);

                if (progress != null)
                {
                    progress.Report(((i / 3 + selection.GetCount()) * 100 / totalCount).ToString());
                }

                cancel.ThrowIfCancellationRequested();
            }
        }
Пример #2
0
        /// <summary>
        /// Writes the whole B-rep scene to a given text stream (uses text variant of Stanford PLY format).
        /// </summary>
        /// <param name="writer">Already open text writer</param>
        /// <param name="scene">Scene to write</param>
        public void WriteBrep(StreamWriter writer, SceneBrep scene)
        {
            DoNormals   = true;
            DoTxtCoords = true;
            DoColors    = true;

            if (scene == null ||
                scene.Triangles < 1)
            {
                return;
            }

            Debug.Assert(TextFormat);
            if (!NativeNewLine)
            {
                writer.NewLine = "\r";     // CR only
            }
            bool writeNormals   = DoNormals && scene.HasNormals();
            bool writeTxtCoords = DoTxtCoords && scene.HasTxtCoords();
            bool writeColors    = DoColors && scene.HasColors();

            writer.WriteLine(HEADER);
            writer.WriteLine(FORMAT_TEXT);

            // vertex-header:
            writer.WriteLine("{0} {1} {2}", ELEMENT, VERTEX, scene.Vertices);
            writer.WriteLine("{0} float x", PROPERTY);
            writer.WriteLine("{0} float {1}", PROPERTY, Orientation ? 'z' : 'y');
            writer.WriteLine("{0} float {1}", PROPERTY, Orientation ? 'y' : 'z');
            if (writeNormals)
            {
                writer.WriteLine("{0} float {1}", PROPERTY, NORMAL_X);
                writer.WriteLine("{0} float {1}", PROPERTY, Orientation ? NORMAL_Z : NORMAL_Y);
                writer.WriteLine("{0} float {1}", PROPERTY, Orientation ? NORMAL_Y : NORMAL_Z);
            }
            if (writeTxtCoords)
            {
                writer.WriteLine("{0} float {1}", PROPERTY, TEXTURE_S);
                writer.WriteLine("{0} float {1}", PROPERTY, TEXTURE_T);
            }
            if (writeColors)
            {
                writer.WriteLine("{0} float {1}", PROPERTY, COLOR_R);
                writer.WriteLine("{0} float {1}", PROPERTY, COLOR_G);
                writer.WriteLine("{0} float {1}", PROPERTY, COLOR_B);
            }

            // face-header:
            writer.WriteLine("{0} {1} {2}", ELEMENT, FACE, scene.Triangles);
            writer.WriteLine("{0} list uchar int vertex_indices", PROPERTY);

            writer.WriteLine(END_HEADER);

            // vertex-data:
            int           i;
            Vector3       v3;
            Vector2       v2;
            StringBuilder sb = new StringBuilder();

            for (i = 0; i < scene.Vertices; i++)
            {
                v3 = scene.GetVertex(i);
                sb.Clear();
                sb.AppendFormat(CultureInfo.InvariantCulture, "{0} {1} {2}", v3.X, v3.Y, v3.Z);
                if (writeNormals)
                {
                    v3 = scene.GetNormal(i);
                    sb.AppendFormat(CultureInfo.InvariantCulture, " {0} {1} {2}", v3.X, v3.Y, v3.Z);
                }
                if (writeTxtCoords)
                {
                    v2 = scene.GetTxtCoord(i);
                    sb.AppendFormat(CultureInfo.InvariantCulture, " {0} {1}", v2.X, v2.Y);
                }
                if (writeColors)
                {
                    v3 = scene.GetColor(i);
                    sb.AppendFormat(CultureInfo.InvariantCulture, " {0} {1} {2}", v3.X, v3.Y, v3.Z);
                }
                writer.WriteLine(sb.ToString());
            }

            // face-data:
            int A, B, C;

            for (i = 0; i < scene.Triangles; i++)
            {
                scene.GetTriangleVertices(i, out A, out B, out C);
                writer.WriteLine("3 {0} {1} {2}", A, Orientation ? C : B, Orientation ? B : C);
            }
        }