コード例 #1
0
        public static void WriteASCIIFile(Triangle[] triangles, string fileName)
        {
            ReplaceCharInStr ReplaceCommaToDot = (string line) =>
            {
                int commaIndex = line.IndexOf(',');
                if (commaIndex == -1)
                {
                    return(line);
                }
                StringBuilder tempStr = new StringBuilder();
                tempStr.Append(line.Substring(0, commaIndex));
                tempStr.Append('.');
                tempStr.Append(line.Substring(commaIndex + 1));
                return(tempStr.ToString());
            };

            Stream       baseStream = new FileStream(fileName, FileMode.Create);
            StreamWriter writeFile  = new StreamWriter(baseStream);

            using (writeFile)
            {
                writeFile.WriteLine("solid\t" + fileName);
                foreach (Triangle triangl in triangles)
                {
                    string normalXStr = ReplaceCommaToDot(triangl.Normal.X.ToString());
                    string normalYStr = ReplaceCommaToDot(triangl.Normal.Y.ToString());
                    string normalZStr = ReplaceCommaToDot(triangl.Normal.Z.ToString());
                    writeFile.WriteLine("facet normal {0} {1} {2}", normalXStr, normalYStr, normalZStr);
                    writeFile.WriteLine("outer loop");

                    for (int i = 0; i < 3; i++)
                    {
                        string vertexXStr = ReplaceCommaToDot(triangl[i].X.ToString());
                        string vertexYStr = ReplaceCommaToDot(triangl[i].Y.ToString());
                        string vertexZStr = ReplaceCommaToDot(triangl[i].Z.ToString());
                        writeFile.WriteLine("vertex {0} {1} {2}", vertexXStr, vertexYStr, vertexZStr);
                    }

                    writeFile.WriteLine("endloop");
                    writeFile.WriteLine("endfacet");
                }
                writeFile.Write("endfacet");
            }
        }
コード例 #2
0
        private static Triangle[] ReadASCIIFile(string fileName)
        {
            ReplaceCharInStr ReplaceDotToComma = (string line) =>
            {
                int           dotIndex = line.IndexOf('.');
                StringBuilder tempStr  = new StringBuilder();
                tempStr.Append(line.Substring(0, dotIndex));
                tempStr.Append(',');
                tempStr.Append(line.Substring(dotIndex + 1));
                return(tempStr.ToString());
            };

            List <Vertex>   values          = new List <Vertex>();
            List <Triangle> resultTriangles = new List <Triangle>();
            Vertex          normal          = new Vertex();

            try
            {
                StreamReader readFile = new StreamReader(fileName, Encoding.ASCII);
                using (readFile)
                {
                    while (!readFile.EndOfStream)
                    {
                        try
                        {
                            string   nextLine = readFile.ReadLine();
                            string[] line     = nextLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            if (line[0].Equals("vertex"))
                            {
                                //Крапку перероблюємо у кому бо double.Parse її не розпізнає
                                for (int i = 1; i < 4; i++)
                                {
                                    line[i] = ReplaceDotToComma(line[i]);
                                }

                                double x = double.Parse(line[1]);
                                double y = double.Parse(line[2]);
                                double z = double.Parse(line[3]);
                                values.Add(new Vertex(x, y, z));
                            }
                            else if (line[0].Equals("facet"))
                            {
                                for (int i = 2; i < 5; i++)
                                {
                                    line[i] = ReplaceDotToComma(line[i]);
                                }
                                double xNormal = double.Parse(line[2]);
                                double yNormal = double.Parse(line[3]);
                                double zNormal = double.Parse(line[4]);
                                normal = new Vertex(xNormal, yNormal, zNormal);
                            }

                            if (values.Count == 3)
                            {
                                resultTriangles.Add(new Triangle(values, normal));
                                values.Clear();
                            }
                        }
                        catch (FormatException e)
                        {
                            Console.WriteLine("Occured error while reading the file:" + e.Message);
                        }
                    }
                }
            }
            catch (IOException e)
            {
                Console.WriteLine("Виникли проблеми при зчитуванні файлу: " + e.Message);
            }
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine("Вершина містить недостатньо координат:" + e.Message);
            }
            return(resultTriangles.ToArray());
        }