private void CreateGeometry()
 {
     var builder = new MeshBuilder();
     builder.AddSphere(new Point3D(0, 0, 0), Diameter/2, ThetaDiv, PhiDiv);
     _sphere = builder.ToMesh();
     _sphere.Freeze();
 }
Ejemplo n.º 2
0
        static Predator()
        {
            var egg = new MeshBuilder();
            egg.AddPyramid(new Point3D(0, 0, 0), 0.2, 0.4);
            eggGeometry = egg.ToMesh();

            var child = new MeshBuilder();
            child.AddBox(new Point3D(0, 0, 0.5), 1, 1, 1);
            child.AddSphere(new Point3D(0.2, 0.2, 1), 0.2, 12, 24);
            child.AddSphere(new Point3D(0.2, -0.2, 1), 0.2, 12, 24);
            childGeometry = child.ToMesh();

            var creature = new MeshBuilder();
            creature.AddBox(new Point3D(0, 0, 0.5), 1, 1, 1);
            creature.AddCone(new Point3D(0.2, 0.2, 1), new Vector3D(0, 0, 1), 0.16, 0.04, 0.3, false, true, 12);
            creature.AddCone(new Point3D(0.2, -0.2, 1), new Vector3D(0, 0, 1), 0.16, 0.04, 0.3, false, true, 12);
            creatureGeometry = creature.ToMesh();

            var coffin = new MeshBuilder();
            coffin.AddCylinder(new Point3D(0, 0, 0), new Point3D(0, 0, 0.25), 1, 12);
            deadGeometry = coffin.ToMesh();

            maleMaterial = MaterialHelper.CreateMaterial(Brushes.Navy);
            femaleMaterial = MaterialHelper.CreateMaterial(Brushes.Violet);
            eggMaterial = MaterialHelper.CreateMaterial(Brushes.AntiqueWhite);
            deadMaterial = MaterialHelper.CreateMaterial(Brushes.Black);

        }
Ejemplo n.º 3
0
        private void CreateModel()
        {
            const double dt = 0.1;
            const int nSteps = 100;
            var mb = new MeshBuilder(true, true);
            for (double y0 = -5; y0 <= 5; y0 += 0.25)
            {
                var p0 = new Point(-3, y0);
                Point[] pts = Solve(Velocity, p0, dt, nSteps);
                var vel = new double[pts.Length];
                var diam = new double[pts.Length];
                int i = 0;
                var pts3d = new Point3D[pts.Length];
                double vmax = 0;
                foreach (Point pt in pts)
                {
                    pts3d[i] = new Point3D(pt.X, pt.Y, 0);
                    double v = Velocity(pt.X, pt.Y).Length;
                    if (v > vmax) vmax = v;
                    vel[i++] = v;
                }
                for (int j = 0; j < vel.Length; j++)
                    vel[j] /= vmax;
                for (int j = 0; j < vel.Length; j++)
                    diam[j] = 0.075;

                mb.AddTube(pts3d, vel, diam, 12, false);
            }
            StreamLinesModel = new GeometryModel3D();
            StreamLinesModel.Geometry = mb.ToMesh();
            StreamLinesModel.Material = Materials.Hue;
            StreamLinesModel.BackMaterial = Materials.Hue;
        }
Ejemplo n.º 4
0
        static Creature()
        {
            var egg = new MeshBuilder();
            egg.AddSphere(new Point3D(0, 0, 0.2), 0.2, 24, 12);
            eggGeometry = egg.ToMesh();

            var child = new MeshBuilder();
            child.AddSphere(new Point3D(0, 0, 1), 1, 24, 12);
            child.AddSphere(new Point3D(1, 0, 1), 0.5, 24, 12);
            child.AddSphere(new Point3D(1.5, 0.2, 1), 0.1, 24, 12);
            child.AddSphere(new Point3D(1.5, -0.2, 1), 0.1, 24, 12);
            childGeometry = child.ToMesh();

            var creature = new MeshBuilder();
            creature.AddSphere(new Point3D(0, 0, 1), 1, 24, 12);
            creature.AddSphere(new Point3D(0, 0, 2), 0.5, 24, 12);
            creature.AddSphere(new Point3D(0.5, 0.2, 2), 0.1, 24, 12);
            creature.AddSphere(new Point3D(0.5, -0.2, 2), 0.1, 24, 12);
            creatureGeometry = creature.ToMesh();

            var coffin = new MeshBuilder();
            coffin.AddBox(new Point3D(0, 0, 0.25), 1, 1, 1);
            deadGeometry = coffin.ToMesh();

            maleMaterial = MaterialHelper.CreateMaterial(Brushes.Blue);
            femaleMaterial = MaterialHelper.CreateMaterial(Brushes.IndianRed);
            eggMaterial = MaterialHelper.CreateMaterial(Brushes.AntiqueWhite);
            deadMaterial = MaterialHelper.CreateMaterial(Brushes.Black);

        }
        void CreateGeometry()
        {

            double r = Diameter / 2;
            double l = HeadLength * Diameter;

            // arrowhead
            var pc = new PointCollection();
            pc.Add(new Point(-l, r));
            pc.Add(new Point(-l, r * 2));
            pc.Add(new Point(0, 0));

            var headBuilder = new MeshBuilder();
            headBuilder.AddRevolvedGeometry(pc, new Point3D(0, 0, 0), new Vector3D(0, 0, 1), ThetaDiv);
            _head = headBuilder.ToMesh();
            _head.Freeze(); 
            

            // body
            pc = new PointCollection();
            pc.Add(new Point(0, 0));
            pc.Add(new Point(0, r));
            pc.Add(new Point(1, r));

            var bodyBuilder = new MeshBuilder();
            bodyBuilder.AddRevolvedGeometry(pc, new Point3D(0, 0, 0), new Vector3D(0, 0, 1), ThetaDiv);
            _body = bodyBuilder.ToMesh();
            _body.Freeze();
        }
Ejemplo n.º 6
0
        // Add all cubes to a ModelVisual3D, reuse geometry but create new visual for each cube - this is slow
        /*   GeometryModel3D AddGeometrySeparate(IEnumerable<Point3D> centers, double L)
           {
               var mv = new ModelVisual3D();

               var cubit = new CubeVisual3D { SideLength = L * 0.95, Fill = Brushes.Gold };
               var cuboidGeometry = cubit.Model.Geometry as MeshGeometry3D;
               var r = new Random();

               foreach (var center in centers)
               {
                   var tg = new Transform3DGroup();
                   tg.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), (r.NextDouble() - 0.5) * 10)));
                   tg.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), (r.NextDouble() - 0.5) * 10)));
                   tg.Children.Add(new TranslateTransform3D(center.ToVector3D()));

                   var c = new ModelVisual3D
                               {
                                   Content =
                                       new GeometryModel3D
                                           {
                                               Geometry = cuboidGeometry,
                                               Material = cubit.Material,
                                               Transform = tg
                                           }
                               };
                   mv.Children.Add(c);
               }
              return mv;
           }*/

        // All cubes in one GeometryModel - much faster
        GeometryModel3D AddGeometry(IEnumerable<Point3D> centers, double L)
        {
            var w = new Stopwatch();
            w.Start();
            /*            var geometry = new MeshGeometry3D();
                        foreach (var center in centers)
                        {
                            MeshGeometryHelper.AddBox(geometry,center, L, L, L);
                        }
                        */

            var builder = new MeshBuilder();
            foreach (var center in centers)
            {
                builder.AddBox(center, L, L, L);
            }
            var geometry = builder.ToMesh();
            geometry.Freeze();

            Trace.WriteLine(Level + ": " + w.ElapsedMilliseconds + " ms");

            var mv = new GeometryModel3D
                             {
                                 Geometry = geometry,
                                 Material = MaterialHelper.CreateMaterial(Brushes.Gold)
                             };
            TriangleCount = geometry.TriangleIndices.Count / 3;

            return mv;
        }
Ejemplo n.º 7
0
 public static Geometry3D CreateUnitCubeGeometry()
 {
     var mb = new MeshBuilder();
     mb.AddBox(new Point3D(0, 0, 0), 1, 1, 1);
     var mesh = mb.ToMesh();
     mesh.Freeze();
     return mesh;
 }
Ejemplo n.º 8
0
        private void CreateModel()
        {
            var points = new Point3DCollection();
            var edges = new Int32Collection();
            var triangles = new Int32Collection();
            switch (CurrentModelType)
            {
                case ModelTypes.StellatedOctahedron:
                case ModelTypes.Tetrahedron:
                    points.Add(+1, +1, +1);
                    points.Add(-1, -1, 1);
                    points.Add(-1, +1, -1);
                    points.Add(+1, -1, -1);
                    edges.Add(0, 1, 1, 2, 2, 0, 0, 3, 1, 3, 2, 3);
                    triangles.Add(0, 1, 2, 0, 3, 1, 1, 3, 2, 2, 3, 0);
                    break;
            }
            switch (CurrentModelType)
            {
                case ModelTypes.StellatedOctahedron:
                    // http://en.wikipedia.org/wiki/Compound_of_two_tetrahedra
                    points.Add(-1, +1, +1);
                    points.Add(1, -1, 1);
                    points.Add(1, +1, -1);
                    points.Add(-1, -1, -1);
                    edges.Add(4, 5, 5, 6, 6, 4, 4, 7, 5, 7, 6, 7);
                    triangles.Add(4, 5, 6, 4, 7, 5, 5, 7, 6, 6, 7, 4);
                    break;
            }

            var m = new Model3DGroup();

            // Add the nodes
            var gm = new MeshBuilder();
            foreach (var p in points)
            {
                gm.AddSphere(p, 0.1);
            }
            m.Children.Add(new GeometryModel3D(gm.ToMesh(), Materials.Gold));

            // Add the triangles
            var tm = new MeshBuilder();
            for (int i = 0; i < triangles.Count; i += 3)
            {
                tm.AddTriangle(points[triangles[i]], points[triangles[i + 1]], points[triangles[i + 2]]);
            }
            m.Children.Add(new GeometryModel3D(tm.ToMesh(), Materials.Red) { BackMaterial = Materials.Blue });

            // Add the edges
            var em = new MeshBuilder();
            for (int i = 0; i < edges.Count; i += 2)
            {
                em.AddCylinder(points[edges[i]], points[edges[i + 1]], 0.08, 10);
            }
            m.Children.Add(new GeometryModel3D(em.ToMesh(), Materials.Gray));

            Model = m;
        }
Ejemplo n.º 9
0
        public DemoElement3D()
        {
            var gm = new GeometryModel3D();
            var mb = new MeshBuilder();
            mb.AddSphere(new Point3D(0, 0, 0), 2, 100, 50);
            gm.Geometry = mb.ToMesh();
            gm.Material = Materials.Blue;

            Visual3DModel = gm;
        }
Ejemplo n.º 10
0
 protected override MeshGeometry3D Tessellate()
 {
     var b = new MeshBuilder();
     b.AddCubeFace(Center, new Vector3D(-1, 0, 0), new Vector3D(0, 0, 1), Length, Width, Height);
     b.AddCubeFace(Center, new Vector3D(1, 0, 0), new Vector3D(0, 0, -1), Length, Width, Height);
     b.AddCubeFace(Center, new Vector3D(0, -1, 0), new Vector3D(0, 0, 1), Width, Length, Height);
     b.AddCubeFace(Center, new Vector3D(0, 1, 0), new Vector3D(0, 0, -1), Width, Length, Height);
     if (TopFace)
         b.AddCubeFace(Center, new Vector3D(0, 0, 1), new Vector3D(0, -1, 0), Height, Length, Width);
     if (BottomFace)
         b.AddCubeFace(Center, new Vector3D(0, 0, -1), new Vector3D(0, 1, 0), Height, Length, Width);
     return b.ToMesh();
 }
        void CreateGeometry()
        {

            double r = Diameter / 2;

            var pc = new PointCollection();
            pc.Add(new Point(0, 0));
            pc.Add(new Point(0, r));
            pc.Add(new Point(1, r));
            pc.Add(new Point(1, 0));

            var builder = new MeshBuilder();
            builder.AddRevolvedGeometry(pc, new Point3D(0, 0, 0), new Vector3D(0, 0, 1), ThetaDiv);
            _tube= builder.ToMesh();
            _tube.Freeze();
        }
Ejemplo n.º 12
0
        private ModelVisual3D CreateDice()
        {
            var diceMesh = new MeshBuilder();
            diceMesh.AddBox(new Point3D(0, 0, 0), 1, 1, 1);
            for (int i = 0; i < 2; i++)
                for (int j = 0; j < 2; j++)
                    for (int k = 0; k < 2; k++)
                    {
                        var points = new List<Point3D>();
                        diceMesh.ChamferCorner(new Point3D(i - 0.5, j - 0.5, k - 0.5), 0.1, 1e-6, points);
                        //foreach (var p in points)
                        //    b.ChamferCorner(p, 0.03);
                    }

            return new ModelVisual3D { Content = new GeometryModel3D { Geometry = diceMesh.ToMesh(), Material = Materials.White } };
        }
Ejemplo n.º 13
0
        public override GeometryModel3D Generate()
        {
            var mesh = new MeshBuilder();

            AddBranch(mesh, new Point3D(0,0,0), UpVector, 0);

            var mv = new GeometryModel3D
            {
                Geometry = mesh.ToMesh(),
                Material = mat,
                BackMaterial = mat
            };

            TriangleCount = mesh.TriangleIndices.Count / 3;

            return mv;
        }
Ejemplo n.º 14
0
        public void Initialize(uint xRes, uint yRes)
        {
            _xRes = xRes;
            _yRes = yRes;

            _pointModels = new PointModel[_xRes, _yRes];
            var material = MaterialHelper.CreateMaterial(Colors.MediumPurple);

            Model3DGroup group = new Model3DGroup();
            for (int y = 0; y < _yRes; y++)
            {
                var mb = new MeshBuilder();
                for (int x = 0; x < _xRes; x++)
                {
                    mb.AddBox(new Point3D(x, y, 0), 1, 1, 1);

            /*
                    var model = new PointModel();
                    model.Model = new GeometryModel3D(CreateUnitCubeGeometry(), material);
                    model.TranslateTransform = new TranslateTransform3D();
                    model.ScaleTransform = new ScaleTransform3D();

                    Transform3DGroup tg = new Transform3DGroup();
                    tg.Children.Add(model.TranslateTransform);
                    tg.Children.Add(model.ScaleTransform);
                    model.Model.Transform = tg;

                    group.Children.Add(model.Model);
                    _pointModels[x, y] = model;
            */
                }
                var model = new GeometryModel3D(mb.ToMesh(), material);

                group.Children.Add(model);
            }

            this.Content = group;

            /*
                        X = (u - 320) * depth_md_[k] * pixel_size_ * 0.001 / F_;
                        Y = (v - 240) * depth_md_[k] * pixel_size_ * 0.001 / F_;
                        Z = depth_md_[k] * 0.001; // from mm in meters!
            */
        }
Ejemplo n.º 15
0
        GeometryModel3D AddGeometry(IEnumerable<Point3D> centers, double l)
        {

            var builder = new MeshBuilder();

            foreach (var center in centers)
            {
                builder.AddPyramid(center, l, l);
            }

            var mv = new GeometryModel3D
                             {
                                 Geometry = builder.ToMesh(true),
                                 Material = MaterialHelper.CreateMaterial(Brushes.Gold)
                             };

            TriangleCount = builder.TriangleIndices.Count / 3;

            return mv;
        }
Ejemplo n.º 16
0
        public MainWindow()
        {
            InitializeComponent();

            var c = new ContainerUIElement3D();
            var e = new ModelUIElement3D();
            var gm = new GeometryModel3D();
            var mb = new MeshBuilder();
            mb.AddSphere(new Point3D(0, 0, 0), 2, 100, 50);
            gm.Geometry = mb.ToMesh();
            gm.Material = Materials.Red;
            e.Model = gm;
            e.Transform = new TranslateTransform3D(5, 0, 0);
            e.MouseDown += (sender, args) => gm.Material = Materials.Yellow;
            c.Children.Add(e);
            view1.Children.Add(c);

            //e.Visibility = Visibility.Hidden;
            //this.Dispatcher.BeginInvoke(DispatcherPriority.Send, new ThreadStart(delegate
            //{
            //    e.Visibility = Visibility.Visible;
            //}));
        }
Ejemplo n.º 17
0
        protected override MeshGeometry3D Tessellate()
        {
            double width = Columns*grid - margin*2;
            double length = Rows*grid - margin*2;
            double height = Height*plateThickness;
            var builder = new MeshBuilder(true, true);

            for (int i = 0; i < Columns; i++)
                for (int j = 0; j < Rows; j++)
                {
                    var o = new Point3D((i + 0.5)*grid, (j + 0.5)*grid, height);
                    builder.AddCone(o, new Vector3D(0, 0, 1), knobDiameter/2, knobDiameter/2, knobHeight, false, true,
                                    Divisions);
                    builder.AddPipe(new Point3D(o.X, o.Y, o.Z - wallThickness), new Point3D(o.X, o.Y, wallThickness),
                                    knobDiameter, outerDiameter, Divisions);
                }

            builder.AddBox(new Point3D(Columns * 0.5 * grid, Rows * 0.5 * grid, height - wallThickness / 2), length, width,
                          wallThickness,
                          MeshBuilder.BoxFaces.All);
            builder.AddBox(new Point3D(margin + wallThickness / 2, Rows * 0.5 * grid, height / 2 - wallThickness / 2), length,
                           wallThickness, height - wallThickness,
                           MeshBuilder.BoxFaces.All ^ MeshBuilder.BoxFaces.Top);
            builder.AddBox(
                new Point3D(Columns * grid - margin - wallThickness / 2, Rows * 0.5 * grid, height / 2 - wallThickness / 2), length,
                wallThickness, height - wallThickness,
                MeshBuilder.BoxFaces.All ^ MeshBuilder.BoxFaces.Top);
            builder.AddBox(new Point3D(Columns * 0.5 * grid, margin + wallThickness / 2, height / 2 - wallThickness / 2),
                           wallThickness, width, height - wallThickness,
                           MeshBuilder.BoxFaces.All ^ MeshBuilder.BoxFaces.Top);
            builder.AddBox(
                new Point3D(Columns * 0.5 * grid, Rows * grid - margin - wallThickness / 2, height / 2 - wallThickness / 2),
                wallThickness, width, height - wallThickness,
                MeshBuilder.BoxFaces.All ^ MeshBuilder.BoxFaces.Top);

            return builder.ToMesh();
        }
Ejemplo n.º 18
0
        private void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            // cubit.Model.Freeze();
            // cubit.Material.Freeze();
            var cuboidGeometry = cubit.Model.Geometry as MeshGeometry3D;

            var vis = new ModelVisual3D();

            bool allInOneGeometry = true;

            MeshBuilder builder = null;

            if (allInOneGeometry)
                builder = new MeshBuilder();

            int countAll = 0;
            int countModelled = 0;
            for (int i = 0; i < nvert; i++)
            {
                double m = nside - i * (double)nside / nvert;
                var mx = (int)m;
                Debug.WriteLine(i + ": " + mx);
                for (int j = 0; j <= mx; j++)
                {
                    for (int k = 0; k <= mx; k++)
                    {
                        countAll++;

                        // only adding blocks on the outside...
                        if (j > 0 && j < mx - 1 && i > 0 && i < nvert - 1 && k > 0 && k < mx - 1)
                            continue;

                        countModelled++;

                        var center = new Point3D(a * (j - (double)mx / 2), a * (k - (double)mx / 2), (i + 0.5) * a);

                        if (allInOneGeometry)
                            builder.AddBox(center, a * b, a * b, a * b);
                        else
                        {
                            // add a new visual for each cubit, but reuse the geometry
                            var transform = new TranslateTransform3D(center.ToVector3D());
                            var c = new ModelVisual3D
                                        {
                                            Content =
                                                new GeometryModel3D
                                                    {
                                                        Geometry = cuboidGeometry,
                                                        Material = cubit.Material,
                                                        Transform = transform
                                                    }
                                        };
                            vis.Children.Add(c);
                        }
                    }
                }
            }

            if (allInOneGeometry)
            {
                var c = new ModelVisual3D
                            {
                                Content =
                                    new GeometryModel3D
                                        {
                                            Geometry = builder.ToMesh(),
                                            Material = cubit.Material
                                        }
                            };
                view1.Children.Add(c);
            }
            else
            {
                view1.Children.Add(vis);
            }

            var mg = cubit.Model.Geometry as MeshGeometry3D;
            int ntri = mg.TriangleIndices.Count / 3;
            int ntriTotal = ntri * countModelled;

            if (allInOneGeometry)
                ntriTotal = builder.TriangleIndices.Count / 3;

            Title = "Mini Pyramid, total " + countAll + " cubits, rendered with " + countModelled + " cubits / " + ntriTotal +
                    " triangles.";

            view1.LookAt(new Point3D(0, 0, a * nvert / 2), new Vector3D(-70, -60, -100), 0);
        }
Ejemplo n.º 19
0
        private void AddBranch(MeshBuilder mesh, Point3D p0, Vector3D direction, int p)
        {
            double angle = GetAngleBetween(direction, UpVector);
            bool isStem = angle < 10;


            double h = isStem ? 2.5 : 2;
            double r = (Level+1-p)*0.1;

            mesh.AddCone(p0, direction, r, r * 0.8, h, false, false, 12);
            var p1 = p0 + direction*h;

            if (p == Level)
                return;

            if (isStem)
            {
                var rightVector=direction.FindAnyPerpendicular();
                var t0 = new RotateTransform3D(new AxisAngleRotation3D(rightVector, GetRandom(3)));
                AddBranch(mesh, p1, t0.Transform(direction), p + 1);
                
                var t1 = new RotateTransform3D(new AxisAngleRotation3D(rightVector, 95 + GetRandom(5)));
                var d1 = t1.Transform(direction);
                int nBranches = 5+GetRandom(2);
                for (int i = 0; i < nBranches; i++)
                {
                    double a = 360.0 * i / nBranches + GetRandom(25);
                    var t2 = new RotateTransform3D(new AxisAngleRotation3D(UpVector, a));
                    AddBranch(mesh, p1, t2.Transform(d1), p + 1);
                }
            } else
            {
                var rightVector=Vector3D.CrossProduct(direction, UpVector);
                var t1 = new RotateTransform3D(new AxisAngleRotation3D(rightVector, -5 + GetRandom(5)));
                var t2 = new RotateTransform3D(new AxisAngleRotation3D(UpVector, 45+GetRandom(10)));
                var t3 = new RotateTransform3D(new AxisAngleRotation3D(UpVector, -45 + GetRandom(10)));
                var d1 = t1.Transform(direction);
                AddBranch(mesh, p1, d1, p + 1);
                AddBranch(mesh, p1, t2.Transform(d1), p + 1);
                AddBranch(mesh, p1, t3.Transform(d1), p + 1);                
            }
        }
Ejemplo n.º 20
0
        public void A_B_C()
        {
            var mb = new MeshBuilder();

        }
Ejemplo n.º 21
0
        private static GeometryModel3D CreateFace(int face, Point3D center, double width, double length, double height, Brush brush)
        {
            var m = new GeometryModel3D();
            var b = new MeshBuilder();
            switch (face)
            {
                case 0:
                    b.AddCubeFace(center, new Vector3D(-1, 0, 0), new Vector3D(0, 0, 1), length, width, height);
                    break;
                case 1:
                    b.AddCubeFace(center, new Vector3D(1, 0, 0), new Vector3D(0, 0, -1), length, width, height);
                    break;
                case 2:
                    b.AddCubeFace(center, new Vector3D(0, -1, 0), new Vector3D(0, 0, 1), width, length, height);
                    break;
                case 3:
                    b.AddCubeFace(center, new Vector3D(0, 1, 0), new Vector3D(0, 0, -1), width, length, height);
                    break;
                case 4:
                    b.AddCubeFace(center, new Vector3D(0, 0, -1), new Vector3D(0, 1, 0), height, length, width);
                    break;
                case 5:
                    b.AddCubeFace(center, new Vector3D(0, 0, 1), new Vector3D(0, -1, 0), height, length, width);
                    break;
            }

            m.Geometry = b.ToMesh();
            m.Material = MaterialHelper.CreateMaterial(brush);
            return m;
        }
Ejemplo n.º 22
0
    public static ModelVisual3D Representation3D(this XYPolygon Poly, IXYPoint refpoint, double height)
    {
      MeshBuilder mb = new MeshBuilder();
      var pts = new Point3DCollection();

      foreach (var p in Poly.Points)
      {
        pts.Add(new Point3D(refpoint.X - p.X, refpoint.Y - p.Y, height));
      }

      // POLYGONS (flat and convex)
      var poly3D = new Polygon3D(pts);
      // Transform the polygon to 2D
      var poly2D = poly3D.Flatten();
      // Triangulate
      var tri = poly2D.Triangulate();
      if (tri != null)
      {
        // Add the triangle indices with the 3D points
        mb.Append(pts, tri);
      }

      var m = MaterialHelper.CreateMaterial(Colors.DimGray, 0.5);

      var mv3D = new ModelVisual3D();
      mv3D.Content = new GeometryModel3D(mb.ToMesh(), m);
      return mv3D;

    }
Ejemplo n.º 23
0
        private void CreateInitialMesh()
        {
            var pts = new Point3D[n, m];
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                {
                    pts[i, j] = new Point3D(-Length * j / (m - 1), 0, PoleHeight - Height * i / (n - 1));
                }

            var mb = new MeshBuilder(false, true);
            mb.AddRectangularMesh(pts, false, false);
            Mesh = mb.ToMesh();
        }
Ejemplo n.º 24
0
 private static Geometry3D GetDefaultGeometry()
 {
     // The default geometry is a box
     var mb = new MeshBuilder();
     mb.AddBox(new Point3D(0, 0, 0.5), 0.8, 0.8, 1);
     return mb.ToMesh();
 }
Ejemplo n.º 25
0
        private void AddFace(string values)
        {
            // A polygonal face. The numbers are indexes into the arrays of vertex positions, 
            // texture coordinates, and normals respectively. A number may be omitted if, 
            // for example, texture coordinates are not being defined in the model.
            // There is no maximum number of vertices that a single polygon may contain. 
            // The .obj file specification says that each face must be flat and convex. 

            var fields = values.Split(' ');
            var pts = new Point3DCollection();
            var tex = new PointCollection();
            var norm = new Vector3DCollection();
            foreach (var field in fields)
            {
                if (String.IsNullOrEmpty(field))
                    continue;
                var ff = field.Split('/');
                int vi = int.Parse(ff[0]);
                int vti = ff.Length > 1 && ff[1].Length > 0 ? int.Parse(ff[1]) : -1;
                int vni = ff.Length > 2 && ff[2].Length > 0 ? int.Parse(ff[2]) : -1;
                pts.Add(Points[vi - 1]);
                if (vti >= 0)
                    tex.Add(TexCoords[vti - 1]);
                if (vni >= 0)
                    norm.Add(Normals[vni - 1]);
            }
            if (tex.Count == 0) tex = null;
            if (norm.Count == 0) norm = null;

            // QUAD
            if (pts.Count == 4)
            {
                CurrentGroup.MeshBuilder.AddQuads(pts, norm, tex);
                return;
            }

            // TRIANGLE
            if (pts.Count == 3)
            {
                CurrentGroup.MeshBuilder.AddTriangles(pts, norm, tex);
                return;
            }

            // POLYGONS (flat and convex)
            var poly3D = new Polygon3D(pts);
            // Transform the polygon to 2D
            var poly2D = poly3D.Flatten();
            // Triangulate
            var tri = poly2D.Triangulate();
            if (tri != null)
            {
                // Add the triangle indices with the 3D points
                var mesh = new MeshBuilder();
                mesh.Append(pts, tri);
                CurrentGroup.MeshBuilder.Append(mesh);
            }
        }
Ejemplo n.º 26
0
 protected override MeshGeometry3D Tessellate()
 {
     var builder = new MeshBuilder(true, true);
     builder.AddSphere(Center, Radius, ThetaDiv, PhiDiv);
     return builder.ToMesh();
 }
Ejemplo n.º 27
0
 protected override MeshGeometry3D Tessellate()
 {
     var builder = new MeshBuilder();
     builder.AddQuad(Point1, Point2, Point3, Point4, new Point(0, 1), new Point(1, 1), new Point(1, 0), new Point(0, 0));
     return builder.ToMesh();
 }
Ejemplo n.º 28
0
 private GeometryModel3D Box(double width, double length, double height)
 {
     var model = new GeometryModel3D();
     model.SetValue(FrameworkElement.NameProperty, "box");
     var mb = new MeshBuilder();
     mb.AddBox(new Point3D(0, 0, height*0.5), width, length, height);
     model.Geometry = mb.ToMesh();
     model.Material = material;
     return model;
 }
Ejemplo n.º 29
0
 public Group(string name)
 {
     Name = name;
     Material = MaterialHelper.CreateMaterial(Brushes.Green);
     MeshBuilder = new MeshBuilder();
 }
Ejemplo n.º 30
0
        /// <summary>
        /// Creates the 3D model of the terrain.
        /// </summary>
        /// <param name="lod">The level of detail.</param>
        /// <returns></returns>
        public GeometryModel3D CreateModel(int lod)
        {

            int ni = Height / lod;
            int nj = Width / lod;
            var pts = new Point3DCollection(ni * nj);

            double mx = (Left + Right) / 2;
            double my = (Top + Bottom) / 2;
            double mz = (MinimumZ + MaximumZ) / 2;

            Offset = new Point3D(mx, my, mz);

            for (int i = 0; i < ni; i++)
                for (int j = 0; j < nj; j++)
                {
                    double x = Left + (Right - Left) * j / (nj - 1);
                    double y = Top + (Bottom - Top) * i / (ni - 1);
                    double z = Data[i * lod * Width + j * lod];

                    x -= Offset.X;
                    y -= Offset.Y;
                    z -= Offset.Z;
                    pts.Add(new Point3D(x, y, z));
                }

            var mb = new MeshBuilder(false, false);
            mb.AddRectangularMesh(pts, nj);
            var mesh = mb.ToMesh();

            var material = Materials.Green;

            if (Texture != null)
            {
                Texture.Calculate(this, mesh);
                material = Texture.Material;
                mesh.TextureCoordinates = Texture.TextureCoordinates;
            }

            var model = new GeometryModel3D();
            model.Geometry = mesh;
            model.Material = material;
            model.BackMaterial = material;
            return model;
        }