This class represents 3D triangulation of random data.
Inheritance: System.Windows.Media.Media3D.ModelVisual3D
Esempio n. 1
0
        void Create()
        {
            if (triangulation != null) viewport.Children.Remove(triangulation);

            int count;
            if (!int.TryParse(numPoints.Text, out count) && count < 10) count = 100;

            triangulation = RandomTriangulation.Create(count, 10);
            viewport.Children.Add(triangulation);

            infoText.Text = string.Format("{0} tetrahedrons", triangulation.Count);
        }
Esempio n. 2
0
        void Create()
        {
            if (triangulation != null)
            {
                viewport.Children.Remove(triangulation);
            }

            int count;

            if (!int.TryParse(numPoints.Text, out count) && count < 10)
            {
                count = 100;
            }

            triangulation = RandomTriangulation.Create(count, 10);
            viewport.Children.Add(triangulation);

            infoText.Text = string.Format("{0} tetrahedrons", triangulation.Count);
        }
Esempio n. 3
0
        void Create(bool uniform = false)
        {
            if (triangulation != null)
            {
                viewport.Children.Remove(triangulation);
            }

            // add file's path
            string path = @"C:\Users\DEVMF\Desktop\File\VS\MIConvexHull-master\MIConvexHull-master\Examples\7DelaunayWPF\input.txt";

            string[] textValue = System.IO.File.ReadAllLines(path);

            int count;

            if (!int.TryParse(numPoints.Text, out count) && count < 10)
            {
                count = 4097;
            }

            triangulation = RandomTriangulation.Create(count, 10, uniform, textValue);
            viewport.Children.Add(triangulation);

            infoText.Text = string.Format("{0} tetrahedrons", triangulation.Count);
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a triangulation of random data.
        /// For larger data sets it might be a good idea to separate the triangulation calculation
        /// from creating the visual so that the triangulation can be computed on a separate threat.
        /// </summary>
        /// <param name="count">Number of vertices to generate</param>
        /// <param name="radius">Radius of the vertices</param>
        /// <returns>Triangulation</returns>
        public static RandomTriangulation Create(int count, double radius)
        {
            Random rnd = new Random();

            // generate some random points
            Func <double> nextRandom = () => 2 * radius * rnd.NextDouble() - radius;
            var           vertices   = Enumerable.Range(0, count)
                                       .Select(_ => new Vertex(nextRandom(), nextRandom(), nextRandom()))
                                       .ToList();

            //var vertices = new List<Vertex>();
            //int d = 4;
            //double cs = 10.0;
            //for (int i = 0; i < d; i++)
            //{
            //    for (int j = 0; j < d; j++)
            //    {
            //        for (int k = 0; k < d; k++)
            //        {
            //            //vertices.Add(new Vertex(10 * i - 20 + rnd.NextDouble(), 10 * j - 20 - rnd.NextDouble(), 10 * k - 20 + rnd.NextDouble()));
            //            vertices.Add(new Vertex(-10 * i, 10 * j, 10 * k));
            //        }
            //    }
            //}

            // calculate the triangulation
            var tetrahedrons = Triangulation.CreateDelaunay <Vertex, Tetrahedron>(vertices).Cells;

            // create a model for each tetrahedron, pick a random color
            Model3DGroup model = new Model3DGroup();

            foreach (var t in tetrahedrons)
            {
                var color = Color.FromArgb((byte)255, (byte)rnd.Next(256), (byte)rnd.Next(256), (byte)rnd.Next(256));
                model.Children.Add(t.CreateModel(color, radius));
            }

            //var redMaterial = new MaterialGroup
            //{
            //    Children = new MaterialCollection
            //    {
            //        new DiffuseMaterial(Brushes.Red),
            //        // give it some shine
            //        new SpecularMaterial(Brushes.LightYellow, 2.0)
            //    }
            //};

            //var greenMaterial = new MaterialGroup
            //{
            //    Children = new MaterialCollection
            //    {
            //        new DiffuseMaterial(Brushes.Green),
            //        // give it some shine
            //        new SpecularMaterial(Brushes.LightYellow, 2.0)
            //    }
            //};

            //var blueMaterial = new MaterialGroup
            //{
            //    Children = new MaterialCollection
            //    {
            //        new DiffuseMaterial(Brushes.Blue),
            //        // give it some shine
            //        new SpecularMaterial(Brushes.LightYellow, 2.0)
            //    }
            //};

            //CylinderMesh c = new CylinderMesh() { Length = 10, Radius = 0.5 };
            //model.Children.Add(new GeometryModel3D { Geometry = c.Geometry, Material = greenMaterial });
            //model.Children.Add(new GeometryModel3D { Geometry = c.Geometry, Material = redMaterial, Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), 90)) });
            //model.Children.Add(new GeometryModel3D { Geometry = c.Geometry, Material = blueMaterial, Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), 90)) });

            var triangulation = new RandomTriangulation();

            triangulation.tetrahedrons = tetrahedrons;

            // assign the Visual3DModel property of the ModelVisual3D class
            triangulation.Visual3DModel = model;

            return(triangulation);
        }
Esempio n. 5
0
        /// <summary>
        /// Creates a triangulation of random data.
        /// For larger data sets it might be a good idea to separate the triangulation calculation
        /// from creating the visual so that the triangulation can be computed on a separate threat.
        /// </summary>
        /// <param name="count">Number of vertices to generate</param>
        /// <param name="radius">Radius of the vertices</param>
        /// <returns>Triangulation</returns>
        public static RandomTriangulation Create(int count, double radius)
        {
            Random rnd = new Random();

            // generate some random points
            Func<double> nextRandom = () => 2 * radius * rnd.NextDouble() - radius;
            var vertices = Enumerable.Range(0, count)
                .Select(_ => new Vertex(nextRandom(), nextRandom(), nextRandom()))
                .ToList();

            //var vertices = new List<Vertex>();
            //int d = 4;
            //double cs = 10.0;
            //for (int i = 0; i < d; i++)
            //{
            //    for (int j = 0; j < d; j++)
            //    {
            //        for (int k = 0; k < d; k++)
            //        {
            //            //vertices.Add(new Vertex(10 * i - 20 + rnd.NextDouble(), 10 * j - 20 - rnd.NextDouble(), 10 * k - 20 + rnd.NextDouble()));
            //            vertices.Add(new Vertex(-10 * i, 10 * j, 10 * k));
            //        }
            //    }
            //}

            // calculate the triangulation
            var tetrahedrons = Triangulation.CreateDelaunay<Vertex, Tetrahedron>(vertices).Cells;

            // create a model for each tetrahedron, pick a random color
            Model3DGroup model = new Model3DGroup();
            foreach (var t in tetrahedrons)            
            {
                var color = Color.FromArgb((byte)255, (byte)rnd.Next(256), (byte)rnd.Next(256), (byte)rnd.Next(256));
                model.Children.Add(t.CreateModel(color, radius));
            }

            //var redMaterial = new MaterialGroup 
            //{ 
            //    Children = new MaterialCollection
            //    {
            //        new DiffuseMaterial(Brushes.Red),
            //        // give it some shine
            //        new SpecularMaterial(Brushes.LightYellow, 2.0) 
            //    } 
            //};

            //var greenMaterial = new MaterialGroup
            //{
            //    Children = new MaterialCollection
            //    {
            //        new DiffuseMaterial(Brushes.Green),
            //        // give it some shine
            //        new SpecularMaterial(Brushes.LightYellow, 2.0) 
            //    }
            //};

            //var blueMaterial = new MaterialGroup
            //{
            //    Children = new MaterialCollection
            //    {
            //        new DiffuseMaterial(Brushes.Blue),
            //        // give it some shine
            //        new SpecularMaterial(Brushes.LightYellow, 2.0) 
            //    }
            //};

            //CylinderMesh c = new CylinderMesh() { Length = 10, Radius = 0.5 };
            //model.Children.Add(new GeometryModel3D { Geometry = c.Geometry, Material = greenMaterial });
            //model.Children.Add(new GeometryModel3D { Geometry = c.Geometry, Material = redMaterial, Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), 90)) });
            //model.Children.Add(new GeometryModel3D { Geometry = c.Geometry, Material = blueMaterial, Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), 90)) });

            var triangulation = new RandomTriangulation();
            triangulation.tetrahedrons = tetrahedrons;

            // assign the Visual3DModel property of the ModelVisual3D class
            triangulation.Visual3DModel = model;

            return triangulation;
        }
Esempio n. 6
0
        /// <summary>
        /// Creates a triangulation of random data.
        /// For larger data sets it might be a good idea to separate the triangulation calculation
        /// from creating the visual so that the triangulation can be computed on a separate threat.
        /// </summary>
        /// <param name="count">Number of vertices to generate</param>
        /// <param name="radius">Radius of the vertices</param>
        /// <param name="uniform"></param>
        /// <returns>Triangulation</returns>
        public static RandomTriangulation Create(int count, double radius, bool uniform)
        {
            Random        rnd = new Random();
            List <Vertex> vertices;

            if (!uniform)
            {
                // generate some random points
                Func <double> nextRandom = () => 2 * radius * rnd.NextDouble() - radius;
                vertices = Enumerable.Range(0, count)
                           .Select(_ => new Vertex(nextRandom(), nextRandom(), nextRandom()))
                           .ToList();
            }
            else
            {
                vertices = new List <Vertex>();
                int    d  = Math.Max((int)Math.Ceiling(Math.Sqrt(count)) / 2, 3);
                double cs = 2 * radius / (d - 1);
                for (int i = 0; i < d; i++)
                {
                    for (int j = 0; j < d; j++)
                    {
                        for (int k = 0; k < d; k++)
                        {
                            vertices.Add(new Vertex(cs * i - cs * (d - 1) / 2, cs * j - cs * (d - 1) / 2, cs * k - cs * (d - 1) / 2));
                        }
                    }
                }
            }

            // calculate the triangulation
            var config = !uniform
               ? new TriangulationComputationConfig()
               : new TriangulationComputationConfig
            {
                PointTranslationType   = PointTranslationType.TranslateInternal,
                PlaneDistanceTolerance = 0.000001,
                // the translation radius should be lower than PlaneDistanceTolerance / 2
                PointTranslationGenerator = TriangulationComputationConfig.RandomShiftByRadius(0.0000001, 0)
            };

            var tetrahedrons = Triangulation.CreateDelaunay <Vertex, Tetrahedron>(vertices, config).Cells;

            // create a model for each tetrahedron, pick a random color
            Model3DGroup model = new Model3DGroup();

            foreach (var t in tetrahedrons)
            {
                var color = Color.FromArgb((byte)255, (byte)rnd.Next(256), (byte)rnd.Next(256), (byte)rnd.Next(256));
                model.Children.Add(t.CreateModel(color, radius));
            }

            //var redMaterial = new MaterialGroup
            //{
            //    Children = new MaterialCollection
            //    {
            //        new DiffuseMaterial(Brushes.Red),
            //        // give it some shine
            //        new SpecularMaterial(Brushes.LightYellow, 2.0)
            //    }
            //};

            //var greenMaterial = new MaterialGroup
            //{
            //    Children = new MaterialCollection
            //    {
            //        new DiffuseMaterial(Brushes.Green),
            //        // give it some shine
            //        new SpecularMaterial(Brushes.LightYellow, 2.0)
            //    }
            //};

            //var blueMaterial = new MaterialGroup
            //{
            //    Children = new MaterialCollection
            //    {
            //        new DiffuseMaterial(Brushes.Blue),
            //        // give it some shine
            //        new SpecularMaterial(Brushes.LightYellow, 2.0)
            //    }
            //};

            //CylinderMesh c = new CylinderMesh() { Length = 10, Radius = 0.5 };
            //model.Children.Add(new GeometryModel3D { Geometry = c.Geometry, Material = greenMaterial });
            //model.Children.Add(new GeometryModel3D { Geometry = c.Geometry, Material = redMaterial, Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), 90)) });
            //model.Children.Add(new GeometryModel3D { Geometry = c.Geometry, Material = blueMaterial, Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), 90)) });

            var triangulation = new RandomTriangulation();

            triangulation.tetrahedrons = tetrahedrons;

            // assign the Visual3DModel property of the ModelVisual3D class
            triangulation.Visual3DModel = model;

            return(triangulation);
        }
        /// <summary>
        /// Creates a triangulation of random data.
        /// For larger data sets it might be a good idea to separate the triangulation calculation
        /// from creating the visual so that the triangulation can be computed on a separate threat.
        /// </summary>
        /// <param name="count">Number of vertices to generate</param>
        /// <param name="radius">Radius of the vertices</param>
        /// <param name="uniform"></param>
        /// <returns>Triangulation</returns>
        public static RandomTriangulation Create(int count, double radius, bool uniform, string[] textValue)
        {
            Random        rnd      = new Random();
            List <Vertex> vertices = new List <Vertex>();

            if (!uniform)
            {
                // generate some random points
                //Func<double> nextRandom = () => 2 * radius * rnd.NextDouble() - radius;
                //vertices = Enumerable.Range(0, count)
                //    .Select(_ => new Vertex(nextRandom(), nextRandom(), nextRandom()))
                //    .ToList();
                for (int i = 0; i < textValue.Length; i += 3)
                {
                    vertices.Add(new Vertex(Convert.ToDouble(textValue[i]), Convert.ToDouble(textValue[i + 1]), Convert.ToDouble(textValue[i + 2])));
                }
            }
            else
            {
                vertices = new List <Vertex>();
                int    d  = Math.Max((int)Math.Ceiling(Math.Sqrt(count)) / 2, 3);
                double cs = 2 * radius / (d - 1);
                for (int i = 0; i < d; i++)
                {
                    for (int j = 0; j < d; j++)
                    {
                        for (int k = 0; k < d; k++)
                        {
                            vertices.Add(new Vertex(cs * i - cs * (d - 1) / 2, cs * j - cs * (d - 1) / 2, cs * k - cs * (d - 1) / 2));
                        }
                    }
                }
            }
            var tetrahedrons = Triangulation.CreateDelaunay <Vertex, Tetrahedron>(vertices).Cells;

            // create a model for each tetrahedron, pick a random color
            Model3DGroup model = new Model3DGroup();

            foreach (var t in tetrahedrons)
            {
                var color = Color.FromArgb((byte)255, (byte)rnd.Next(256), (byte)rnd.Next(256), (byte)rnd.Next(256));
                model.Children.Add(t.CreateModel(color, radius));
            }

            //var redMaterial = new MaterialGroup
            //{
            //    Children = new MaterialCollection
            //    {
            //        new DiffuseMaterial(Brushes.Red),
            //        // give it some shine
            //        new SpecularMaterial(Brushes.LightYellow, 2.0)
            //    }
            //};

            //var greenMaterial = new MaterialGroup
            //{
            //    Children = new MaterialCollection
            //    {
            //        new DiffuseMaterial(Brushes.Green),
            //        // give it some shine
            //        new SpecularMaterial(Brushes.LightYellow, 2.0)
            //    }
            //};

            //var blueMaterial = new MaterialGroup
            //{
            //    Children = new MaterialCollection
            //    {
            //        new DiffuseMaterial(Brushes.Blue),
            //        // give it some shine
            //        new SpecularMaterial(Brushes.LightYellow, 2.0)
            //    }
            //};

            //CylinderMesh c = new CylinderMesh() { Length = 10, Radius = 0.5 };
            //model.Children.Add(new GeometryModel3D { Geometry = c.Geometry, Material = greenMaterial });
            //model.Children.Add(new GeometryModel3D { Geometry = c.Geometry, Material = redMaterial, Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), 90)) });
            //model.Children.Add(new GeometryModel3D { Geometry = c.Geometry, Material = blueMaterial, Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), 90)) });

            var triangulation = new RandomTriangulation();

            triangulation.tetrahedrons = tetrahedrons;

            int OBJIndex = 1, OBJTextureIndex = 1;
            Dictionary <string, OBJNormalVertex> OBJNormalVertexList = new Dictionary <string, OBJNormalVertex>();
            Dictionary <string, string>          OBJTextureKeyList = new Dictionary <string, string>();
            Dictionary <string, int>             OBJTextureList = new Dictionary <string, int>();

            foreach (var Element in vertices)
            {
                string textVertex  = string.Format("v {0} {1} {2}", Element.Position[0], Element.Position[1], Element.Position[2]);
                string textTexture = string.Format("vt {0} {1}", (Element.Position[0] + 50) / 100, (Element.Position[1] + 50) / 100);

                OBJTextureKeyList.Add(textVertex, textTexture);
                OBJNormalVertexList.Add(textVertex, new OBJNormalVertex(OBJIndex++, 0, new double[] { 0, 0, 0 }, ""));

                if (OBJTextureList.ContainsKey(textTexture) == false)
                {
                    OBJTextureList.Add(textTexture, OBJTextureIndex++);
                }
            }

            foreach (var Element in tetrahedrons)
            {
                for (int i = 0; i < 3; i++)
                {
                    string textVertex = string.Format("v {0} {1} {2}", Element.Vertices[i].Position[0], Element.Vertices[i].Position[1], Element.Vertices[i].Position[2]);

                    OBJNormalVertexList[textVertex].count++;
                    OBJNormalVertexList[textVertex].NormalVertex[0] += Element.Normal[0];
                    OBJNormalVertexList[textVertex].NormalVertex[1] += Element.Normal[1];
                    OBJNormalVertexList[textVertex].NormalVertex[2] += Element.Normal[2];
                }
            }

            foreach (var Element in OBJNormalVertexList)
            {
                Element.Value.ArrangeNormalVertex();
            }

            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\DEVMF\Desktop\File\VS\MIConvexHull-master\MIConvexHull-master\Examples\7DelaunayWPF\output.txt"))
            {
                foreach (var Element in OBJNormalVertexList)
                {
                    file.WriteLine(Element.Key);
                }
                file.WriteLine();

                foreach (var Element in OBJNormalVertexList)
                {
                    file.WriteLine(Element.Value.NormalVertexQuery);
                }
                file.WriteLine();

                foreach (var Element in OBJTextureList)
                {
                    file.WriteLine(Element.Key);
                }
                file.WriteLine();

                foreach (var Element in tetrahedrons)
                {
                    string query1 = string.Format("v {0} {1} {2}", Element.Vertices[0].Position[0], Element.Vertices[0].Position[1], Element.Vertices[0].Position[2]);
                    string query2 = string.Format("v {0} {1} {2}", Element.Vertices[1].Position[0], Element.Vertices[1].Position[1], Element.Vertices[1].Position[2]);
                    string query3 = string.Format("v {0} {1} {2}", Element.Vertices[2].Position[0], Element.Vertices[2].Position[1], Element.Vertices[2].Position[2]);

                    string face1 = string.Format("{0}/{1}/{2}", OBJNormalVertexList[query1].index, OBJTextureList[OBJTextureKeyList[query1]], OBJNormalVertexList[query1].index);
                    string face2 = string.Format("{0}/{1}/{2}", OBJNormalVertexList[query2].index, OBJTextureList[OBJTextureKeyList[query2]], OBJNormalVertexList[query2].index);
                    string face3 = string.Format("{0}/{1}/{2}", OBJNormalVertexList[query3].index, OBJTextureList[OBJTextureKeyList[query3]], OBJNormalVertexList[query3].index);

                    string face = string.Format("f {0} {1} {2}", face1, face2, face3);

                    file.WriteLine(face);
                }
            }

            //foreach (var Element in OBJVertexAndVormalVertex)
            //    if (Element.Value.isNegative)
            //        OBJVertexTexture.Add(Element.Value.vertexTexture, VTindex++);

            // assign the Visual3DModel property of the ModelVisual3D class
            triangulation.Visual3DModel = model;

            return(triangulation);
        }
        /// <summary>
        /// Creates a triangulation of random data.
        /// For larger data sets it might be a good idea to separate the triangulation calculation
        /// from creating the visual so that the triangulation can be computed on a separate threat.
        /// </summary>
        /// <param name="count">Number of vertices to generate</param>
        /// <param name="radius">Radius of the vertices</param>
        /// <param name="uniform"></param>
        /// <returns>Triangulation</returns>
        public static RandomTriangulation Create(int count, double radius, bool uniform)
        {
            Random rnd = new Random();
            List<Vertex> vertices;

            if (!uniform)
            {
                // generate some random points
                Func<double> nextRandom = () => 2 * radius * rnd.NextDouble() - radius;
                vertices = Enumerable.Range(0, count)
                    .Select(_ => new Vertex(nextRandom(), nextRandom(), nextRandom()))
                    .ToList();
            }
            else
            {
                vertices = new List<Vertex>();
                int d = Math.Max((int)Math.Ceiling(Math.Sqrt(count)) / 2, 3);
                double cs = 2 * radius / (d - 1);
                for (int i = 0; i < d; i++)
                {
                    for (int j = 0; j < d; j++)
                    {
                        for (int k = 0; k < d; k++)
                        {
                            vertices.Add(new Vertex(cs * i - cs * (d - 1) / 2, cs * j - cs * (d - 1) / 2, cs * k - cs * (d - 1) / 2));
                        }
                    }
                }
            }

            // calculate the triangulation
            var config = !uniform
               ? new TriangulationComputationConfig()
               : new TriangulationComputationConfig
               {
                   PointTranslationType = PointTranslationType.TranslateInternal,
                   PlaneDistanceTolerance = 0.000001,
                   // the translation radius should be lower than PlaneDistanceTolerance / 2
                   PointTranslationGenerator = TriangulationComputationConfig.RandomShiftByRadius(0.0000001, 0)
               };

            var tetrahedrons = Triangulation.CreateDelaunay<Vertex, Tetrahedron>(vertices, config).Cells;

            // create a model for each tetrahedron, pick a random color
            Model3DGroup model = new Model3DGroup();
            foreach (var t in tetrahedrons)
            {
                var color = Color.FromArgb((byte)255, (byte)rnd.Next(256), (byte)rnd.Next(256), (byte)rnd.Next(256));
                model.Children.Add(t.CreateModel(color, radius));
            }

            //var redMaterial = new MaterialGroup
            //{
            //    Children = new MaterialCollection
            //    {
            //        new DiffuseMaterial(Brushes.Red),
            //        // give it some shine
            //        new SpecularMaterial(Brushes.LightYellow, 2.0)
            //    }
            //};

            //var greenMaterial = new MaterialGroup
            //{
            //    Children = new MaterialCollection
            //    {
            //        new DiffuseMaterial(Brushes.Green),
            //        // give it some shine
            //        new SpecularMaterial(Brushes.LightYellow, 2.0)
            //    }
            //};

            //var blueMaterial = new MaterialGroup
            //{
            //    Children = new MaterialCollection
            //    {
            //        new DiffuseMaterial(Brushes.Blue),
            //        // give it some shine
            //        new SpecularMaterial(Brushes.LightYellow, 2.0)
            //    }
            //};

            //CylinderMesh c = new CylinderMesh() { Length = 10, Radius = 0.5 };
            //model.Children.Add(new GeometryModel3D { Geometry = c.Geometry, Material = greenMaterial });
            //model.Children.Add(new GeometryModel3D { Geometry = c.Geometry, Material = redMaterial, Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), 90)) });
            //model.Children.Add(new GeometryModel3D { Geometry = c.Geometry, Material = blueMaterial, Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), 90)) });

            var triangulation = new RandomTriangulation();
            triangulation.tetrahedrons = tetrahedrons;

            // assign the Visual3DModel property of the ModelVisual3D class
            triangulation.Visual3DModel = model;

            return triangulation;
        }