Builds MeshGeometry3D objects.
Performance tips for MeshGeometry3D (See MSDN)

High impact: Mesh animation—changing the individual vertices of a mesh on a per-frame basis—is not always efficient in Windows Presentation Foundation (WPF). To minimize the performance impact of change notifications when each vertex is modified, detach the mesh from the visual tree before performing per-vertex modification. Once the mesh has been modified, reattach it to the visual tree. Also, try to minimize the size of meshes that will be animated in this way.

Medium impact: When a mesh is defined as abutting triangles with shared vertices and those vertices have the same position, normal, and texture coordinates, define each shared vertex only once and then define your triangles by index with TriangleIndices.

Low impact: To minimize the construction time of large collections in Windows Presentation Foundation (WPF), such as a MeshGeometry3D’s Positions, Normal vectors, TextureCoordinates, and TriangleIndices, pre-size the collections before value population. If possible, pass the collections’ constructors prepopulated data structures such as arrays or Lists.

Example #1
3
 public MainViewModel()
 {
     var gm = new MeshBuilder();
     gm.AddBox(new Point3D(0, 0, 0.5), 1, 1, 1);
     gm.AddCylinder(new Point3D(5, 0, 0), new Point3D(5, 0, 5), 1, 36);
     this.Model = new GeometryModel3D(gm.ToMesh(true), Materials.Blue);
     this.Model.Freeze();
 }
 private static object buildTri()
 {
     var mb = new MeshBuilder(true, false);
     var p0 = new Point3D(0, 0, 0);
     var p1 = new Point3D(1, 0, 0);
     var p2 = new Point3D(1, 1, 0);
     mb.AddTriangle(p0, p1, p2);
     mb.Normals.ToList().ForEach(x => System.Diagnostics.Trace.WriteLine(x.ToString()));
     return mb.ToMesh();
 }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MainViewModel"/> class.
        /// </summary>
        public MainViewModel()
        {
            // Create a model group
            var modelGroup = new Model3DGroup();

            // Create a mesh builder and add a box to it
            var meshBuilder = new MeshBuilder(false, false);
            meshBuilder.AddBox(new Point3D(0, 0, 1), 1, 2, 0.5);
            meshBuilder.AddBox(new Rect3D(0, 0, 1.2, 0.5, 1, 0.4));

            // Create a mesh from the builder (and freeze it)
            var mesh = meshBuilder.ToMesh(true);

            // Create some materials
            var greenMaterial = MaterialHelper.CreateMaterial(Colors.Green);
            var redMaterial = MaterialHelper.CreateMaterial(Colors.Red);
            var blueMaterial = MaterialHelper.CreateMaterial(Colors.Blue);
            var insideMaterial = MaterialHelper.CreateMaterial(Colors.Yellow);

            // Add 3 models to the group (using the same mesh, that's why we had to freeze it)
            modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Material = greenMaterial, BackMaterial = insideMaterial });
            modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Transform = new TranslateTransform3D(-2, 0, 0), Material = redMaterial, BackMaterial = insideMaterial });
            modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Transform = new TranslateTransform3D(2, 0, 0), Material = blueMaterial, BackMaterial = insideMaterial });

            //// Set the property, which will be bound to the Content property of the ModelVisual3D (see MainWindow.xaml)
            //AxisAngleRotation3D rotateAxis = new AxisAngleRotation3D(new Vector3D(0, 1, 0), 180/*or 360*/);
            //Rotation3DAnimation rotateAnimation = new Rotation3DAnimation(rotateAxis, TimeSpan.FromSeconds(2));

            //var rotateTransform = new RotateTransform3D();
            //rotateTransform.BeginAnimation(RotateTransform3D.RotationProperty, rotateAnimation);

            //modelGroup.Transform = rotateTransform;

            this.Model = modelGroup;
        }
Example #4
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);
            }

            this.StreamLinesModel = new GeometryModel3D();
            this.StreamLinesModel.Geometry = mb.ToMesh();
            this.StreamLinesModel.Material = Materials.Hue;
            this.StreamLinesModel.BackMaterial = Materials.Hue;
        }
        private static ModelVisual3D Planar(IFeature basePath)
        {
            TongJi.Geometry.Polyline curve = new TongJi.Geometry.Polyline(basePath.GeoData);
            if (curve.AlgebraicArea < 0)
            {
                curve = curve.ReversePoints();
            }
            var points = curve.Points.ToList();

            if (points.Last() == points.First())
            {
                points.RemoveAt(points.Count - 1);
            }
            var triangles = DelaunayOfPolygon(points);

            MeshBuilder mb = new MeshBuilder();

            foreach (var tri in triangles)
            {
                var a = new Point3D(tri.A.x, tri.A.y, 0);
                var b = new Point3D(tri.B.x, tri.B.y, 0);
                var c = new Point3D(tri.C.x, tri.C.y, 0);
                mb.AddTriangle(a, b, c);
            }
            ModelVisual3D visual = new ModelVisual3D();

            visual.Content = new GeometryModel3D(mb.ToMesh(), MaterialHelper.CreateMaterial(Colors.Blue));
            return(visual);
        }
        private void Initializer(Point3D location)
        {
            var geometryModel = new GeometryModel3D();

            var meshBuilder = new MeshBuilder();

            meshBuilder.AddBox(new Point3D(location.X, location.Y, location.Z + 3.5), 1.5, 1.5, 0.25);

            meshBuilder.AddBox(new Point3D(location.X, location.Y + 1, location.Z + 3.5), 0.25, 1.25, 0.25);
            meshBuilder.AddBox(new Point3D(location.X, location.Y - 1, location.Z + 3.5), 0.25, 1.25, 0.25);

            meshBuilder.AddBox(new Point3D(location.X + 1, location.Y , location.Z + 3.5), 1.25, 0.25, 0.25);
            meshBuilder.AddBox(new Point3D(location.X - 1, location.Y, location.Z + 3.5), 1.25, 0.25, 0.25);

            meshBuilder.AddBox(new Point3D(location.X, location.Y + 1.5, location.Z + 3), 0.25, 0.25, 0.75);
            meshBuilder.AddBox(new Point3D(location.X, location.Y - 1.5, location.Z + 3), 0.25, 0.25, 0.75);

            meshBuilder.AddBox(new Point3D(location.X + 1.5, location.Y, location.Z + 3), 0.25, 0.25, 0.75);
            meshBuilder.AddBox(new Point3D(location.X - 1.5, location.Y, location.Z + 3), 0.25, 0.25, 0.75);

            meshBuilder.AddBox(new Point3D(location.X, location.Y, location.Z + 4), 0.5, 0.5, 1);

            geometryModel.Geometry = meshBuilder.ToMesh();
            geometryModel.Material = Materials.Gold;

            Visual3DModel = geometryModel;
        }
Example #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MainViewModel"/> class.
        /// </summary>
        public MainViewModel()
        {
            // Create a model group
            var modelGroup = new Model3DGroup();

            // Create a mesh builder and add a box to it
            var meshBuilder = new MeshBuilder(false, false);
            meshBuilder.AddBox(new Point3D(0, 0, 1), 1, 2, 0.5);
            meshBuilder.AddBox(new Rect3D(0, 0, 1.2, 0.5, 1, 0.4));

            // Create a mesh from the builder (and freeze it)
            var mesh = meshBuilder.ToMesh(true);

            // Create some materials
            var greenMaterial = MaterialHelper.CreateMaterial(Colors.Green);
            var redMaterial = MaterialHelper.CreateMaterial(Colors.Red);
            var blueMaterial = MaterialHelper.CreateMaterial(Colors.Blue);
            var insideMaterial = MaterialHelper.CreateMaterial(Colors.Yellow);

            // Add 3 models to the group (using the same mesh, that's why we had to freeze it)
            modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Material = greenMaterial, BackMaterial = insideMaterial });
            modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Transform = new TranslateTransform3D(-2, 0, 0), Material = redMaterial, BackMaterial = insideMaterial });
            modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Transform = new TranslateTransform3D(2, 0, 0), Material = blueMaterial, BackMaterial = insideMaterial });

            // Set the property, which will be bound to the Content property of the ModelVisual3D (see MainWindow.xaml)
            this.Model = modelGroup;
        }
Example #8
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;
        }
        private static void AddCircleInZCross(MeshBuilder mb, Point3D centre, double radius, int div)
        {
            var points = MeshBuilder.GetCircle(div);

            var vectors = new Point3DCollection();
            var normals = new Vector3DCollection();
            var textures = new PointCollection();

            vectors.Add(new Point3D(centre.X, centre.Y, 0));
            normals.Add(new Vector3D(0, 0, 1));
            textures.Add(new Point(0.5, 0.5));

            for (int i = 0; i < points.Count - 1; i++)
            {
                vectors.Add(new Point3D(points[i].X * radius + centre.X, points[i].Y * radius + centre.Y, centre.Z));
                normals.Add(new Vector3D(0, 0, -1));
                textures.Add(new Point(points[i].X * 0.5 + 0.5, points[i].Y * 0.5 + 0.5));

                vectors.Add(new Point3D(points[i + 1].X * radius + centre.X, points[i + 1].Y * radius + centre.Y, centre.Z));
                normals.Add(new Vector3D(0, 0, 01));
                textures.Add(new Point(points[i + 1].X * 0.5 + 0.5, points[i + 1].Y * 0.5 + 0.5));
            }

            mb.AddTriangleFan(vectors, normals, textures);
        }
Example #10
0
        private void AppearanceChanged()
        {
            var builder = new MeshBuilder(false, true);

            // hard code a kerb section
            var section = new PointCollection();
            int m = 41;
            double n = 4;
            double a = this.Width / 2;
            double b = this.Height;
            for (int i = 0; i < m; i++)
            {
                double t = Math.PI * i / (m - 1);
                section.Add(new Point(
                    a * Math.Sign(Math.Cos(t)) * Math.Pow(Math.Abs(Math.Cos(t)), 2 / n),
                    -b * Math.Sign(Math.Sin(t)) * Math.Pow(Math.Abs(Math.Sin(t)), 2 / n)));
            }

            // calculate the texture coordinates
            var values = new List<double> { 0 };
            for (int i = 1; i < this.Positions.Count; i++)
            {
                var d = this.Positions[i - 1].DistanceTo(this.Positions[i]);
                values.Add(values[values.Count - 1] + (d / this.Length));
            }

            // create the extruded geometry
            builder.AddTube(this.Positions, null, values, null, section, new Vector3D(1, 0, 0), false, false);

            this.kerbModel.Geometry = builder.ToMesh();
        }
Example #11
0
        //// Using a DependencyProperty as the backing store for Direction.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty DirectionProperty =
        //    DependencyProperty.Register("Direction", typeof(Vector3D), typeof(SensorVisual3D), new PropertyMetadata(new Vector3D(1, 0, 0), GeometryChanged));

        //public string Name
        //{
        //    get { return (string)GetValue(NameProperty); }
        //    set { SetValue(NameProperty, value); }
        //}

        //// Using a DependencyProperty as the backing store for Name.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty NameProperty =
        //    DependencyProperty.Register("Name", typeof(Size3D), typeof(SensorVisual3D), new PropertyMetadata(new Size3D(), GeometryChanged));

        //public Size3D Size
        //{
        //    get { return (Size3D)GetValue(SizeProperty); }
        //    set { SetValue(SizeProperty, value); }
        //}

        //// Using a DependencyProperty as the backing store for Size.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty SizeProperty =
        //    DependencyProperty.Register("Size", typeof(Size3D), typeof(SensorVisual3D), new PropertyMetadata(new Size3D(0, 0, 0), GeometryChanged));

        /// <summary>
        /// The tessellate.
        /// </summary>
        /// <returns>The mesh.</returns>
        protected override MeshGeometry3D Tessellate()
        {
            var builder = new ht.MeshBuilder(true, true);

            builder.AddArrow(this.Position, this.Position + this.Orientation.Axis, 1);

            return(builder.ToMesh());
        }
        public MainViewModel()
        {
            // Create a model group
            var modelGroup = new Model3DGroup();
            points = new List<Point3D>();
            
            // Create a mesh builder and add a box to it
            var meshBuilder = new MeshBuilder(false, false);
            // -1 -1 -1 
            //1 -1 -1 
            //1 1 -1 
            //-1 1 -1 
            //-1 -1 1 
            //1 -1 1 
            //1 1 1 
            //-1 1 1 
            string path = @"teapot.ply";
            PLYReader reader = new PLYReader();

            using (var s = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                mg=reader.Read(s);
            }
           
            this.points = reader.Vertices;
            this.faces = reader.Faces;
           // meshBuilder.Positions.Add(new Point3D(-1, -1, -1));//, 1, 2, 0.5);
           // meshBuilder.Positions.Add(new Point3D(1, -1, -1));
           // meshBuilder.Positions.Add(new Point3D(1, 1, -1));
           // meshBuilder.Positions.Add(new Point3D(-1, 1, -1));
           // meshBuilder.Positions.Add(new Point3D(-1, -1, 1));
           // meshBuilder.Positions.Add(new Point3D(1, -1, 1));
           // meshBuilder.Positions.Add(new Point3D(1, 1, 1));
           // meshBuilder.Positions.Add(new Point3D(-1, 1, 1));
           // meshBuilder.AddPolygon(new int[4] {0,1,2,3 });
           // meshBuilder.AddPolygon(new int[4] { 5,4,7,6 });
           // meshBuilder.AddPolygon(new int[4] { 6,2,1,5 });
           // meshBuilder.AddPolygon(new int[4] { 3,7,4,0 });
           // meshBuilder.AddPolygon(new int[4] { 7,3,2,6 });
           // meshBuilder.AddPolygon(new int[4] { 5,1,0,4 });
            
           // // Create a mesh from the builder (and freeze it)
           // var mesh = meshBuilder.ToMesh(true);

           // // Create some materials
           // var greenMaterial = MaterialHelper.CreateMaterial(Colors.Green);
           // var redMaterial = MaterialHelper.CreateMaterial(Colors.Red);
           // var blueMaterial = MaterialHelper.CreateMaterial(Colors.Blue);
           // var insideMaterial = MaterialHelper.CreateMaterial(Colors.Yellow);

           // // Add 3 models to the group (using the same mesh, that's why we had to freeze it)
           // modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Material = greenMaterial, BackMaterial = insideMaterial });
           //// modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Transform = new TranslateTransform3D(-2, 0, 0), Material = redMaterial, BackMaterial = insideMaterial });
           //// modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Transform = new TranslateTransform3D(2, 0, 0), Material = blueMaterial, BackMaterial = insideMaterial });

           // // Set the property, which will be bound to the Content property of the ModelVisual3D (see MainWindow.xaml)
            this.Model = mg;
        }
Example #13
0
        /// <summary>
        /// Move the center of this point to here
        /// </summary>
        /// <param name="point"></param>
        public void MoveTo(Point3D point) {
            Center = point;
            GeometryModel3D g = (GeometryModel3D)ModelVisual3D.Content;
            MeshGeometry3D m = (MeshGeometry3D)g.Geometry;

            MeshBuilder meshBuilder = new MeshBuilder();
            meshBuilder.AddBox(Center, Radius, Radius, Radius);
            m.Positions = meshBuilder.Positions;
        }
        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;
        }
Example #15
0
        public static MeshGeometry3D GetSimpleMesh(Tool tool, Point3D position, Vector3D direction)
        {
            var t       = tool as SimpleTool;
            var builder = new HelixToolkit.Wpf.MeshBuilder();
            var p       = position + direction * t.Length;

            builder.AddCylinder(position, p, t.Diameter / 2.0);

            return(builder.ToMesh());
        }
Example #16
0
        public LineSegment(Point3D startPoint, Point3D endPoint)
        {
            StartPoint = startPoint;
            EndPoint = endPoint;

            var meshBuilder = new MeshBuilder(false, false);
            meshBuilder.AddCylinder(startPoint, endPoint, 0.2, 10);

            Model = meshBuilder.ToMesh();
        }
Example #17
0
        void AddTube(List<Point3D> path, Color color)
        {
            var mb = new MeshBuilder();

            mb.AddTube(path, 0.1, 3, false);
            var geom = new GeometryModel3D { Geometry = mb.ToMesh(true), Material = MaterialHelper.CreateMaterial(color) };          // create a model
            var model = new ModelVisual3D();
            model.Content = geom;
            _Helix.Children.Add(model);
        }
Example #18
0
        public static MeshGeometry3D GetTwoSectioMesh(Tool tool, Point3D position, Vector3D direction)
        {
            var t       = tool as TwoSectionTool;
            var builder = new HelixToolkit.Wpf.MeshBuilder();
            var p1      = position + direction * t.Length1;
            var p2      = position + direction * (t.Length1 + t.Length2);

            builder.AddCylinder(position, p1, t.Diameter1 / 2.0);
            builder.AddCylinder(p1, p2, t.Diameter2 / 2.0);

            return(builder.ToMesh());
        }
Example #19
0
        public static MeshGeometry3D GetPointedMesh(Tool tool, Point3D position, Vector3D direction)
        {
            var t       = tool as PointedTool;
            var builder = new HelixToolkit.Wpf.MeshBuilder();
            var p1      = position + direction * t.StraightLength;
            var p2      = position + direction * (t.StraightLength + t.ConeHeight);

            builder.AddCylinder(position, p1, t.Diameter / 2.0);
            builder.AddCone(p1, p2, t.Diameter / 2.0, false, 20);

            return(builder.ToMesh());
        }
 public void AddGroup(List<int> selectedVertex)
 {
     MeshBuilder mesh = new MeshBuilder(false, false);
     foreach (int index in selectedVertex)
         mesh.AddSphere(this.Points[index], pointRaduis);
     var mesh1 = mesh.ToMesh(true);
     var redMaterial = MaterialHelper.CreateMaterial(Colors.Red);
     var insideMaterial = MaterialHelper.CreateMaterial(Colors.Yellow);
     
     // Add 3 models to the group (using the same mesh, that's why we had to freeze it)
     mg.Children.Add(new GeometryModel3D { Geometry = mesh1, Material = redMaterial, BackMaterial = insideMaterial });
 }
        //private ht.TranslateManipulator translateModificator;

        /// <summary>
        /// The tessellate.
        /// </summary>
        /// <returns>The mesh.</returns>
        protected override MeshGeometry3D Tessellate()
        {
            var builder = new ht.MeshBuilder(true, true);
            var _bo     = (IpObject)this;

            if (_bo != null)
            {
                builder.AddSphere(_bo.Position, _bo.Size.X);
            }
            this.Material     = outsideMat;
            this.BackMaterial = insideMat;
            return(builder.ToMesh());
        }
Example #22
0
        public MainWindow()
        {
            InitializeComponent();

            var material = MaterialHelper.CreateMaterial(Colors.Aqua);

            var builder = new MeshBuilder();
            builder.AddTriangle(new Point3D(0, 0, 0), new Point3D(0, 10, 100), new Point3D(100, 0, 0));
            var mesh = builder.ToMesh();
            var geo = new GeometryModel3D { Geometry = mesh, Material = material , Transform = null};
            var model = new ModelVisual3D { Content = geo, Transform = null };
            _visual.Children.Add(model);
        }
Example #23
0
        //private ht.TranslateManipulator translateModificator;

        /// <summary>
        /// The tessellate.
        /// </summary>
        /// <returns>The mesh.</returns>
        protected override MeshGeometry3D Tessellate()
        {
            var builder = new ht.MeshBuilder(true, true);

            // Position and Rotation must not be concidered because they are handled by the visual 3d
            if (this.Sensor == null)
            {
                builder.AddPyramid(new Point3D(), 1, 1);
                return(builder.ToMesh());
            }
            var _zeroPosition = new Point3D();// this.Transform.Value.GetTranslation() - this.Position;

            var _length = new Spherical3D(this.Sensor.MaxRange, 0, 0);

            var _dirSp = this.Orientation.Axis.ToSpherical();

            _dirSp.Radius = 0;

            var _p0AzOffset = new Spherical3D(0, 0, this.Sensor.AzimuthRange / 2.0);
            var _p0ElOffset = new Spherical3D(0, this.Sensor.ElevationRange / 2.0, 0);

            var _pts = new List <Point3D>();

            _pts.Add(_zeroPosition + this.Orientation.Axis + (_length + _p0ElOffset - _p0AzOffset + _dirSp).ToDirection());
            _pts.Add(_zeroPosition + this.Orientation.Axis + (_length + _p0ElOffset + _p0AzOffset + _dirSp).ToDirection());
            _pts.Add(_zeroPosition + this.Orientation.Axis + (_length - _p0ElOffset + _p0AzOffset + _dirSp).ToDirection());
            _pts.Add(_zeroPosition + this.Orientation.Axis + (_length - _p0ElOffset - _p0AzOffset + _dirSp).ToDirection());

            _pts.Sort((Point3D a, Point3D b) =>
            {
                if (Math.Abs(a.X - b.X) < DBL_EqualOffset)
                {
                    if (Math.Abs(a.Y - b.Y) < DBL_EqualOffset)
                    {
                        return(a.Z.CompareTo(b.Z));
                    }
                    return(a.Y.CompareTo(b.Y));
                }
                return(a.X.CompareTo(b.X));
            });

            // outside
            builder.AddTriangle(_pts[0], _zeroPosition, _pts[1]);
            builder.AddTriangle(_pts[3], _zeroPosition, _pts[2]);
            builder.AddTriangle(_pts[2], _zeroPosition, _pts[0]);
            builder.AddTriangle(_pts[1], _zeroPosition, _pts[3]);

            this.Material     = this.outsideMat;
            this.BackMaterial = this.insideMat;
            return(builder.ToMesh());
        }
Example #24
0
        public Cube(Point3D center, double radius, Action action) {
            Center = center;
            Radius = radius;
            Action = action;
            Notified = false;
            SolidColorBrush = new SolidColorBrush();
            SolidColorBrush.Opacity = _Constants.BaseOpacity;
            SolidColorBrush.Color = _Constants.DefaultCubeColour;
            Material = new DiffuseMaterial(SolidColorBrush);
            MeshBuilder meshBuilder = new MeshBuilder();
            meshBuilder.AddBox(center, radius, radius, radius);

            ModelVisual3D = Engine._3DUtil.WrapMeshAndMaterialIntoModelVisual3D(meshBuilder.ToMesh(), Material);
        }
Example #25
0
        /// <summary>
        /// Creates a <see cref="MeshGeometry3D" /> object from the loaded file. Polygons are triangulated using triangle fans.
        /// </summary>
        /// <returns>
        /// A <see cref="MeshGeometry3D" />.
        /// </returns>
        public MeshGeometry3D CreateMeshGeometry3D()
        {
            var mb = new MeshBuilder(false, false);
            foreach (var p in this.Vertices)
            {
                mb.Positions.Add(p);
            }

            foreach (var face in this.Faces)
            {
                mb.AddTriangleFan(face);
            }

            return mb.ToMesh();
        }
Example #26
0
        /// <summary>
        /// Do the tessellation and return the <see cref="MeshGeometry3D"/>.
        /// </summary>
        /// <returns>The mesh geometry.</returns>
        protected override MeshGeometry3D Tessellate()
        {
            var b = new MeshBuilder(false, true);
            b.AddCubeFace(
                this.Center,
                new Vector3D(-1, 0, 0),
                new Vector3D(0, 0, 1),
                this.SideLength,
                this.SideLength,
                this.SideLength);
            b.AddCubeFace(
                this.Center,
                new Vector3D(1, 0, 0),
                new Vector3D(0, 0, -1),
                this.SideLength,
                this.SideLength,
                this.SideLength);
            b.AddCubeFace(
                this.Center,
                new Vector3D(0, -1, 0),
                new Vector3D(0, 0, 1),
                this.SideLength,
                this.SideLength,
                this.SideLength);
            b.AddCubeFace(
                this.Center,
                new Vector3D(0, 1, 0),
                new Vector3D(0, 0, -1),
                this.SideLength,
                this.SideLength,
                this.SideLength);
            b.AddCubeFace(
                this.Center,
                new Vector3D(0, 0, 1),
                new Vector3D(0, -1, 0),
                this.SideLength,
                this.SideLength,
                this.SideLength);
            b.AddCubeFace(
                this.Center,
                new Vector3D(0, 0, -1),
                new Vector3D(0, 1, 0),
                this.SideLength,
                this.SideLength,
                this.SideLength);

            return b.ToMesh();
        }
Example #27
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 } };
        }
Example #28
0
        public static System.Windows.Media.Media3D.MeshGeometry3D get3DText(string text)
        {
            var builder = new HelixToolkit.Wpf.MeshBuilder(false, false);

            //Debug.WriteLine("<<<get3DText:" + text + ">>>");
            builder.ExtrudeText(
                text,
                "Arial",
                FontStyles.Normal,
                FontWeights.Bold,
                29,
                new Vector3D(-1, 0, 0), //text direction
                new Point3D(0, 0, 0),
                new Point3D(0, 0, 0.001));

            return(builder.ToMesh(true));
        }
        public MainWindow()
        {
            InitializeComponent();

            var container = new ContainerUIElement3D();
            var element = new ModelUIElement3D();
            var geometry = new GeometryModel3D();
            var meshBuilder = new MeshBuilder();
            meshBuilder.AddSphere(new Point3D(0, 0, 0), 2, 100, 50);
            geometry.Geometry = meshBuilder.ToMesh();
            geometry.Material = Materials.Green;
            element.Model = geometry;
            element.Transform = new TranslateTransform3D(5, 0, 0);
            element.MouseDown += this.ContainerElementMouseDown;
            container.Children.Add(element);
            view1.Children.Add(container);
        }
Example #30
0
        public static MeshGeometry3D GetCountersinkMesh(Tool tool, Point3D position, Vector3D direction)
        {
            const double hSvasatore = 10.0;
            var          t          = tool as CountersinkTool;
            var          builder    = new HelixToolkit.Wpf.MeshBuilder();
            var          p1         = position + direction * (t.Length1 - hSvasatore);
            var          p12        = position + direction * t.Length1;
            var          p2         = position + direction * (t.Length1 + t.Length2);
            var          p3         = position + direction * (t.Length1 + t.Length2 + t.Length3);

            builder.AddCylinder(position, p1, t.Diameter1 / 2.0);
            builder.AddCylinder(p1, p12, t.Diameter2 / 2.0);
            builder.AddCone(p12, direction, t.Diameter2 / 2.0, t.Diameter1 / 2.0, t.Length2, false, false, 20);
            builder.AddCylinder(p2, p3, t.Diameter1 / 2.0);

            return(builder.ToMesh());
        }
Example #31
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;
        }
        private Model3D CreateModel()
        {
            var plotModel = new Model3DGroup();

            var mb = new MeshBuilder();

            Point3D centre = new Point3D(1, 0.5, 0);

            double radius = 0.4;
            AddCircleInZCross(mb,centre, radius, 121);

            var a = mb.ToMesh();

            plotModel.Children.Add(new GeometryModel3D(mb.ToMesh(), MaterialHelper.CreateMaterial(TopBrush.Clone())));

            return plotModel;
        }
Example #33
0
        public MainWindow()
        {
            this.InitializeComponent();
            var builder = new MeshBuilder(false, false);
            builder.ExtrudeText(
                "Helix Toolkit",
                "Arial",
                FontStyles.Normal,
                FontWeights.Bold,
                20,
                new Vector3D(1, 0, 0),
                new Point3D(0, 0, 0),
                new Point3D(0, 0, 1));

            this.textGeometry = builder.ToMesh(true);
            this.DataContext = this;
        }
        GeometryModel3D AddGeometry(IEnumerable<Point3D> centers, double l)
        {
            var builder = new MeshBuilder();

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

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

            TriangleCount = builder.TriangleIndices.Count / 3;

            return mv;
        }
Example #35
0
        public TubeSegment(Point3D startPoint, Point3D endPoint, double d1, double d2)
        {
            var meshBuilder = new MeshBuilder(false, false);

            var h1 = 0.1*d1;
            var h2 = 0.1*d2;

            var deltaStart = GeometryHelper.GetDeltaPoint(startPoint, endPoint, h1);
            var deltaEnd = GeometryHelper.GetDeltaPoint(endPoint, startPoint, h2);
            var points = new List<Point3D>() { deltaStart, deltaEnd };
            //var points = new List<Point3D>() { startPoint, endPoint };

            var dd1 = d1*0.9;
            var dd2 = d2*0.9;

            var diameters = new[] { dd1, dd2 };
            //var diameters = new[] { d1, d2 };
            meshBuilder.AddTube(points, new double[] { 0, 0 }, diameters, 10, false);

            Model = meshBuilder.ToMesh();
        }
        private void AppearanceChanged()
        {
            var redbuilder = new MeshBuilder(false, false);
            var whitebuilder = new MeshBuilder(false, false);
            for (int i = 0; i < this.Bands; i++)
            {
                var f0 = (double)i / this.Bands;
                var f1 = (double)(i + 1) / this.Bands;
                var y0 = this.Height * f0;
                var y1 = this.Height * f1;
                var d0 = (this.BaseDiameter * (1 - f0)) + (this.TopDiameter * f0);
                var d1 = (this.BaseDiameter * (1 - f1)) + (this.TopDiameter * f1);
                var builder = (this.Bands - i) % 2 == 1 ? redbuilder : whitebuilder;
                builder.AddCone(this.Position + new Vector3D(0, 0, y0), new Vector3D(0, 0, 1), d0 / 2, d1 / 2, y1 - y0, i == 0, i == this.Bands - 1, 20);
            }

            this.TopPosition = this.Position + new Vector3D(0, 0, this.Height);

            this.redbands.Geometry = redbuilder.ToMesh(true);
            this.whitebands.Geometry = whitebuilder.ToMesh(true);
        }
        public Model3D ToModel3D()
        {
            var m = new Model3DGroup();

            TriangleIndexToPanelIndex = new List<int>();

            // Add the triangles
            var tm = new MeshBuilder(false, false);
            int panelIndex = 0;
            foreach (var p in Panels)
            {
                p.TriangleIndex = tm.Positions.Count;
                tm.AddTriangleFan(p.Points);
                for (int i = 0; i < p.Points.Length - 2; i++) TriangleIndexToPanelIndex.Add(panelIndex);
                panelIndex++;
            }
            var panelsGeometry = tm.ToMesh();
            m.Children.Add(new GeometryModel3D(panelsGeometry, Materials.Red) { BackMaterial = Materials.Blue });

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

            // Add the edges
            var em = new MeshBuilder();
            foreach (var p in Panels)
            {
                for (int i = 0; i < p.Points.Length; i += 1)
                {
                    em.AddCylinder(p.Points[i], p.Points[(i + 1) % p.Points.Length], 0.05, 10);
                }
            }
            m.Children.Add(new GeometryModel3D(em.ToMesh(), Materials.Gray));

            return m;
        }
Example #38
0
        public static MeshGeometry3D GetDiskMesh(Tool tool, Point3D position, Vector3D direction)
        {
            var t       = tool as DiskTool;
            var builder = new HelixToolkit.Wpf.MeshBuilder();
            var d       = Math.Abs(t.BodyThickness - t.CuttingThickness) / 2.0;
            var r1      = t.Diameter / 2.0 - t.CuttingRadialThickness;
            var profile = new Point[]
            {
                new Point(t.BodyThickness, 10.0),
                new Point(t.BodyThickness, r1),
                new Point(t.BodyThickness + d, r1),
                new Point(t.BodyThickness + d, t.Diameter / 2.0),
                new Point(-d, t.Diameter / 2.0),
                new Point(-d, r1),
                new Point(0.0, r1),
                new Point(0.0, 10.0)
            };

            builder.AddRevolvedGeometry(profile, null, position, direction, 100);

            return(builder.ToMesh());
        }
Example #39
0
        public PartMesh(string filename)
        {
            var meshBuilder = new MeshBuilder(false, false);

            var min = new Point3D(double.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity);
            var max = new Point3D(double.NegativeInfinity, double.NegativeInfinity, double.NegativeInfinity);

            using (var stream = modelAssembly.GetManifestResourceStream("Hexenstein.Models." + filename))
            using (var reader = new BinaryReader(stream))
            {
                var header = reader.ReadBytes(80);

                var num = reader.ReadUInt32();

                for (int i = 0; i < num; i++)
                {
                    var normal = ReadPoint(reader);
                    var v1 = ReadPoint(reader);
                    var v2 = ReadPoint(reader);
                    var v3 = ReadPoint(reader);
                    var extra = reader.ReadInt16();

                    meshBuilder.AddTriangle(v1, v2, v3);

                    min = new Point3D(
                        Math.Min(Math.Min(Math.Min(min.X, v1.X), v2.X), v3.X),
                        Math.Min(Math.Min(Math.Min(min.Y, v1.Y), v2.Y), v3.Y),
                        Math.Min(Math.Min(Math.Min(min.Z, v1.Z), v2.Z), v3.Z));

                    max = new Point3D(
                        Math.Max(Math.Max(Math.Max(max.X, v1.X), v2.X), v3.X),
                        Math.Max(Math.Max(Math.Max(max.Y, v1.Y), v2.Y), v3.Y),
                        Math.Max(Math.Max(Math.Max(max.Z, v1.Z), v2.Z), v3.Z));
                }
            }

            Centre = ((max - min) / 2 + min);
            Mesh = meshBuilder.ToMesh(true);
        }
        private Model3D CreateModel()
        {
            var plotModel = new Model3DGroup();

            var axesMeshBuilder = new MeshBuilder();

            _realRect3D = GetSampleImageBox();
            axesMeshBuilder.AddBoundingBox(_realRect3D, 0.01);

            plotModel.Children.Add(new GeometryModel3D(axesMeshBuilder.ToMesh(), Materials.Yellow));

            switch (CurrentDirection)
            {
                case FrameDirection.Top:
                    CreateTopSelectionSlice(plotModel, SectionBrush);
                    CreateTopMaxSlice(plotModel);
                    CreateTopPartSlice(plotModel, SectionPosition);
                    break;

                case FrameDirection.Front:
                    CreateFrontSelectionSlice(plotModel, SectionBrush);
                    CreateFrontMaxSlice(plotModel);
                    CreateFrontPartSlice(plotModel, SectionPosition);

                    break;

                case FrameDirection.Left:
                    CreateLeftSelectionSlice(plotModel, SectionBrush);
                    CreateLeftMaxSlice(plotModel);
                    CreateLeftPartSlice(plotModel, SectionPosition);

                    break;

                default:
                    throw new InvalidOperationException("Can not process the direction.");
            }

            return plotModel;
        }