Example #1
0
        Polytope GenerateDodeca()
        {
            Polytope icosa  = GenerateIcosa();
            Polytope dodeca = new Polytope();

            foreach (var edge in icosa.polygons)
            {
                dodeca.Add(new Point
                {
                    X = edge.Points(icosa).Sum(p => p.X) / 3, Y = edge.Points(icosa).Sum(p => p.Y) / 3,
                    Z = edge.Points(icosa).Sum(p => p.Z) / 3
                });
            }

            dodeca.Add(new Polygon(new int[] { 16, 12, 8, 4, 0 }));
            for (int i = 0, j = 3; i < dodeca.points.Count; i += 4, j += 4)
            {
                dodeca.Add(new Polygon(new int[] { (i + 4) % 20, (i + 5) % 20, i + 3, i + 1, i }));
                dodeca.Add(new Polygon(new int[] { j, j - 1, (20 + j - 4) % 20 - 1, (20 + j - 4) % 20, i + 1 }));
            }

            dodeca.Add(new Polygon(new int[] { 2, 6, 10, 14, 18 }));

            return(dodeca);
        }
Example #2
0
        Polytope MakeGraph(Node node, double xStart, double xEnd, double xStep, double yStart, double yEnd, double yStep)
        {
            var result = new Polytope();
            int xDelta = 0;
            int yDelta = 0;

            for (double i = xStart; i <= xEnd; i += xStep)
            {
                xDelta += 1;
                yDelta  = 0;
                for (double j = yStart; j <= yEnd; j += yStep)
                {
                    yDelta += 1;
                    result.Add(new Base3D.Point {
                        X = i, Y = j, Z = node.Get(i, j)
                    });
                    result.AddNormal(new Base3D.Point {
                        X = -node.Dx().Get(i, j), Y = -node.Dy().Get(i, j), Z = 1
                    }.Normal());
                    result.AddNormal(new Base3D.Point {
                        X = node.Dx().Get(i, j), Y = node.Dy().Get(i, j), Z = -1
                    }.Normal());
                }
            }

            for (int i = 0; i < xDelta - 1; ++i)
            {
                for (int j = 0; j < yDelta - 1; ++j)
                {
                    result.Add(new Polygon(new int[] {
                        j + i * yDelta,
                        j + (i + 1) * yDelta,
                        (j + 1) + (i + 1) * yDelta,
                        (j + 1) + i * yDelta
                    },
                                           new int[] {
                        2 * (j + i * yDelta),
                        2 * (j + (i + 1) * yDelta),
                        2 * ((j + 1) + (i + 1) * yDelta),
                        2 * ((j + 1) + i * yDelta)
                    }
                                           ));

                    result.Add(new Polygon(new int[] {
                        j + i * yDelta,
                        (j + 1) + i * yDelta,
                        (j + 1) + (i + 1) * yDelta,
                        j + (i + 1) * yDelta
                    },
                                           new int[] {
                        2 * (j + i * yDelta) + 1,
                        2 * ((j + 1) + i * yDelta) + 1,
                        2 * ((j + 1) + (i + 1) * yDelta) + 1,
                        2 * (j + (i + 1) * yDelta) + 1
                    }
                                           ));
                }
            }

            return(result);
        }
Example #3
0
        public static Entity Parse(string file)
        {
            Directory.SetCurrentDirectory(Path.GetDirectoryName(file));
            Polytope current = new Polytope();
            Dictionary <string, BaseMaterial> mtl = new Dictionary <string, BaseMaterial>();

            foreach (var s in File.ReadAllLines(file))
            {
                if (String.IsNullOrEmpty(s))
                {
                    continue;
                }
                var    lines = Regex.Split(s, @"\s").Where(l => !String.IsNullOrEmpty(l)).ToList();
                string flag  = lines[0];
                if (flag == "v")
                {
                    current.Add(new Base3D.Point
                    {
                        X = double.Parse(lines[1]),
                        Y = -double.Parse(lines[3]),
                        Z = double.Parse(lines[2])
                    });
                }
                else
                if (flag == "vn")
                {
                    current.AddNormal(new Base3D.Point
                    {
                        X = double.Parse(lines[1]),
                        Y = -double.Parse(lines[3]),
                        Z = double.Parse(lines[2])
                    });
                }
                else
                if (flag == "vt")
                {
                    current.Addtexture((
                                           double.Parse(lines[1]),
                                           double.Parse(lines[2])
                                           ));
                }
                else
                if (flag == "f")
                {
                    var first    = lines[1].Split('/');
                    var vertexes = lines.Skip(1).Select(l => Int32.Parse(l.Split('/')[0]) - 1).ToArray();
                    var textures = (first.Length > 1 && !String.IsNullOrEmpty(first[1])) ?
                                   lines.Skip(1).Select(l => Int32.Parse(l.Split('/')[1]) - 1).ToArray() : null;
                    var normals = (first.Length > 2 && !String.IsNullOrEmpty(first[2])) ?
                                  lines.Skip(1).Select(l => Int32.Parse(l.Split('/')[2]) - 1).ToArray() : null;

                    current.Add(new Polygon(vertexes, textures, normals));
                }
                else
                if (flag == "mtllib")
                {
                    try
                    {
                        var new_mtl = LoadMtl(Regex.Replace(s, @"\s*mtllib\s*(.*\S)\s*$", "$1"));
                        mtl = new_mtl;
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show($"Не вышло загрузить текстуры: {e.Message}", "Окошко-всплывашка", MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                    }
                }
                else
                if (flag == "usemtl")
                {
                    string matName = Regex.Replace(s, @"\s*usemtl\s*(.*\S)\s*$", "$1");
                    if (mtl.ContainsKey(matName))
                    {
                        current.Matreial = mtl[matName];
                    }
                }
            }
            return(current);
        }