Member 12763843 Ответов: 0

Я создал объект цилиндра в C# WPF, я хочу повернуть его вдоль оси, как диск pls help


я создал 3D цилиндр в 3D в WPF в C#.я хочу, чтобы повернуть его как диск вдоль оси просьба помочь.....я отправляю мой код ниже

namespace Cylinder
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        MouseUtilities trackball;
        public ScreenSpaceLines3D CircleWireFrame = new ScreenSpaceLines3D();
        public MainWindow()
        {
            InitializeComponent();
            trackball = new MouseUtilities();
            trackball.EventSource = objGrid;
            camera.Transform = trackball.Transform;
            objGrid.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(objGrid_MouseWheel);
            Create3DCircle(new Point3D(0, 0, 1.1), new Point3D(0, 0, 1.2), Colors.Green);
           // Create3DCircle(new Point3D(0, 0, 1.2), new Point3D(0, 0, 1.3), Colors.White);
            //Create3DCircle(new Point3D(0, 0, 1.3), new Point3D(0, 0, 1.4), Colors.Orange);
        }

        private void objGrid_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            e.Handled = true;
        }
        public void Create3DCircle(Point3D firstPoint, Point3D secondPoint, Color color)
        {

            ModelVisual3D models = new ModelVisual3D();
            Custom3DCircle cy = new Custom3DCircle(firstPoint, secondPoint, 0.4, color, CircleWireFrame);
            
                models = cy.GetModelVisual3D();
                models.Transform = new Transform3DGroup();

                CircleWireFrame = cy.m_objScreenSpaceLines3D;
                CircleWireFrame.Color = Colors.Black;
                if (!this.mainViewPort.Children.Contains(CircleWireFrame))
                    this.mainViewPort.Children.Add(CircleWireFrame);
                if (!this.mainViewPort.Children.Contains(models))
                    this.mainViewPort.Children.Add(models);
            }
        }
    }
    #region CustomCylinder
   
    class Custom3DCircle
    {
        #region Member Variables
       public ModelVisual3D models = new ModelVisual3D();

       public Point3D firstPoint = new Point3D();

       public Point3D secondPoint = new Point3D();

       public Color WellColors = Colors.Purple;

       public double radius;

       public ScreenSpaceLines3D m_objScreenSpaceLines3D;
        #endregion
     
        #region Constructor

       public Custom3DCircle(Point3D objfirstPoint,Point3D objSecondPoint, double objRadius, Color objColor, ScreenSpaceLines3D objScreenSpaceLines3D)
        {
            
            firstPoint = objfirstPoint;
            secondPoint = objSecondPoint;
            WellColors = objColor;
            radius = objRadius;
            m_objScreenSpaceLines3D = objScreenSpaceLines3D;
        }
        #endregion

        #region Methods
        public ModelVisual3D GetModelVisual3D()
        {
            var midPoint =  (firstPoint - secondPoint);
            models = Get3Dcircle(GetSurfaceMaterial(WellColors),secondPoint,radius,midPoint.Z);
            return models;

        }

        public ModelVisual3D Get3Dcircle(MaterialGroup materialGroup, Point3D midPoint, double radius, double depth)
        {
            var Circle3D = new Model3DGroup();
            var nearCircle = new circle();
            var farCircle = new circle();

            var twoPi = Math.PI * 2;
            var firstPass = true;

            double x;
            double y;
           
                var increment = 0.1d;
                for (double i = 0; i < twoPi + increment; i = i + increment)
                {
                    x = (radius * Math.Cos(i));
                    y = (-radius * Math.Sin(i));

                      farCircle.CurrentTriangle.P0 = midPoint;
                      farCircle.CurrentTriangle.P1 = farCircle.LastPoint;
                      farCircle.CurrentTriangle.P2 = new Point3D(x + midPoint.X, y + midPoint.Y, midPoint.Z);

                    nearCircle.CurrentTriangle = farCircle.CurrentTriangle.Clone(depth, true);

                    if (!firstPass)
                    {
                        Circle3D.Children.Add(CreateTriangleModel(materialGroup, farCircle.CurrentTriangle));
                        Circle3D.Children.Add(CreateTriangleModel(materialGroup, nearCircle.CurrentTriangle));

                        Circle3D.Children.Add(CreateTriangleModel(materialGroup, farCircle.CurrentTriangle.P2, farCircle.CurrentTriangle.P1, nearCircle.CurrentTriangle.P2));
                        Circle3D.Children.Add(CreateTriangleModel(materialGroup, nearCircle.CurrentTriangle.P2, nearCircle.CurrentTriangle.P1, farCircle.CurrentTriangle.P2));
                    }
                    else
                    {
                       farCircle.FirstPoint = farCircle.CurrentTriangle.P1;
                        nearCircle.FirstPoint = nearCircle.CurrentTriangle.P1;
                        firstPass = false;
                    }
                    farCircle.LastPoint = farCircle.CurrentTriangle.P2;
                    nearCircle.LastPoint = nearCircle.CurrentTriangle.P2;
                }

                var  model = new ModelVisual3D() { Content = Circle3D };
                return model;
            }
            
        
    public class circle
        {
            public circle()
            {
                CurrentTriangle = new Triangle();
            }

            public Point3D FirstPoint { get; set; }
            public Point3D LastPoint { get; set; }
            public Triangle CurrentTriangle { get; set; }
        }

        public class Triangle
        {
            public Point3D P0 { get; set; }
            public Point3D P1 { get; set; }
            public Point3D P2 { get; set; }

            public Triangle Clone(double z, bool switchP1andP2)
            {
                var newTriangle = new Triangle();
                newTriangle.P0 = GetPointAdjustedBy(this.P0, new Point3D(0, 0, z));

                var point1 = GetPointAdjustedBy(this.P1, new Point3D(0, 0, z));
                var point2 = GetPointAdjustedBy(this.P2, new Point3D(0, 0, z));

                if (!switchP1andP2)
                {
                    newTriangle.P1 = point1;
                    newTriangle.P2 = point2;
                }
                else
                {
                    newTriangle.P1 = point2;
                    newTriangle.P2 = point1;
                }
                return newTriangle;
            }

            private Point3D GetPointAdjustedBy(Point3D point, Point3D adjustBy)
            {
                var newPoint = new Point3D { X = point.X, Y = point.Y, Z = point.Z };
                newPoint.Offset(adjustBy.X, adjustBy.Y, adjustBy.Z);
                return newPoint;
            }
        }

        private Model3DGroup CreateTriangleModel(MaterialGroup materialGroup, Triangle triangle)
        {
            return CreateTriangleModel(materialGroup, triangle.P0, triangle.P1, triangle.P2);
        }

        private Model3DGroup CreateTriangleModel(Material material, Point3D p0, Point3D p1, Point3D p2)
        {

            var mesh = new MeshGeometry3D();

            mesh.Positions.Add(p0);
            mesh.Positions.Add(p1);
            mesh.Positions.Add(p2);

            mesh.TriangleIndices.Add(0);
            mesh.TriangleIndices.Add(1);
            mesh.TriangleIndices.Add(2);

            var normal = CalculateNormal(p0, p1, p2);

            mesh.Normals.Add(normal);
            mesh.Normals.Add(normal);
            mesh.Normals.Add(normal);

            m_objScreenSpaceLines3D.Points.Add(p1);
            m_objScreenSpaceLines3D.Points.Add(p2);          
           
            var model = new GeometryModel3D(mesh, material);
            var group = new Model3DGroup();
            group.Children.Add(model);
            return group;
        }
        private Vector3D CalculateNormal(Point3D p0, Point3D p1, Point3D p2)
        {
            var v0 = new Vector3D(p1.X - p0.X, p1.Y - p0.Y, p1.Z - p0.Z);
            var v1 = new Vector3D(p2.X - p1.X, p2.Y - p1.Y, p2.Z - p1.Z);
            return Vector3D.CrossProduct(v0, v1);
        }

        public MaterialGroup GetSurfaceMaterial(Color colour)
        {
            var materialGroup = new MaterialGroup();
            var emmMat = new EmissiveMaterial(new SolidColorBrush(colour));
            materialGroup.Children.Add(emmMat);
            materialGroup.Children.Add(new DiffuseMaterial(new SolidColorBrush(colour)));
            var specMat = new SpecularMaterial(new SolidColorBrush(Colors.White), 30);
            materialGroup.Children.Add(specMat);
            return materialGroup;
        }
        #endregion


    }

    #endregion


Что я уже пробовал:

namespace Cylinder
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        MouseUtilities trackball;
        public ScreenSpaceLines3D CircleWireFrame = new ScreenSpaceLines3D();
        public MainWindow()
        {
            InitializeComponent();
            trackball = new MouseUtilities();
            trackball.EventSource = objGrid;
            camera.Transform = trackball.Transform;
            objGrid.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(objGrid_MouseWheel);
            Create3DCircle(new Point3D(0, 0, 1.1), new Point3D(0, 0, 1.2), Colors.Green);
           // Create3DCircle(new Point3D(0, 0, 1.2), new Point3D(0, 0, 1.3), Colors.White);
            //Create3DCircle(new Point3D(0, 0, 1.3), new Point3D(0, 0, 1.4), Colors.Orange);
        }

        private void objGrid_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            e.Handled = true;
        }
        public void Create3DCircle(Point3D firstPoint, Point3D secondPoint, Color color)
        {

            ModelVisual3D models = new ModelVisual3D();
            Custom3DCircle cy = new Custom3DCircle(firstPoint, secondPoint, 0.4, color, CircleWireFrame);
            
                models = cy.GetModelVisual3D();
                models.Transform = new Transform3DGroup();

                CircleWireFrame = cy.m_objScreenSpaceLines3D;
                CircleWireFrame.Color = Colors.Black;
                if (!this.mainViewPort.Children.Contains(CircleWireFrame))
                    this.mainViewPort.Children.Add(CircleWireFrame);
                if (!this.mainViewPort.Children.Contains(models))
                    this.mainViewPort.Children.Add(models);
            }
        }
    }
    #region CustomCylinder
   
    class Custom3DCircle
    {
        #region Member Variables
       public ModelVisual3D models = new ModelVisual3D();

       public Point3D firstPoint = new Point3D();

       public Point3D secondPoint = new Point3D();

       public Color WellColors = Colors.Purple;

       public double radius;

       public ScreenSpaceLines3D m_objScreenSpaceLines3D;
        #endregion
     
        #region Constructor

       public Custom3DCircle(Point3D objfirstPoint,Point3D objSecondPoint, double objRadius, Color objColor, ScreenSpaceLines3D objScreenSpaceLines3D)
        {
            
            firstPoint = objfirstPoint;
            secondPoint = objSecondPoint;
            WellColors = objColor;
            radius = objRadius;
            m_objScreenSpaceLines3D = objScreenSpaceLines3D;
        }
        #endregion

        #region Methods
        public ModelVisual3D GetModelVisual3D()
        {
            var midPoint =  (firstPoint - secondPoint);
            models = Get3Dcircle(GetSurfaceMaterial(WellColors),secondPoint,radius,midPoint.Z);
            return models;

        }

        public ModelVisual3D Get3Dcircle(MaterialGroup materialGroup, Point3D midPoint, double radius, double depth)
        {
            var Circle3D = new Model3DGroup();
            var nearCircle = new circle();
            var farCircle = new circle();

            var twoPi = Math.PI * 2;
            var firstPass = true;

            double x;
            double y;
           
                var increment = 0.1d;
                for (double i = 0; i < twoPi + increment; i = i + increment)
                {
                    x = (radius * Math.Cos(i));
                    y = (-radius * Math.Sin(i));

                      farCircle.CurrentTriangle.P0 = midPoint;
                      farCircle.CurrentTriangle.P1 = farCircle.LastPoint;
                      farCircle.CurrentTriangle.P2 = new Point3D(x + midPoint.X, y + midPoint.Y, midPoint.Z);

                    nearCircle.CurrentTriangle = farCircle.CurrentTriangle.Clone(depth, true);

                    if (!firstPass)
                    {
                        Circle3D.Children.Add(CreateTriangleModel(materialGroup, farCircle.CurrentTriangle));
                        Circle3D.Children.Add(CreateTriangleModel(materialGroup, nearCircle.CurrentTriangle));

                        Circle3D.Children.Add(CreateTriangleModel(materialGroup, farCircle.CurrentTriangle.P2, farCircle.CurrentTriangle.P1, nearCircle.CurrentTriangle.P2));
                        Circle3D.Children.Add(CreateTriangleModel(materialGroup, nearCircle.CurrentTriangle.P2, nearCircle.CurrentTriangle.P1, farCircle.CurrentTriangle.P2));
                    }
                    else
                    {
                       farCircle.FirstPoint = farCircle.CurrentTriangle.P1;
                        nearCircle.FirstPoint = nearCircle.CurrentTriangle.P1;
                        firstPass = false;
                    }
                    farCircle.LastPoint = farCircle.CurrentTriangle.P2;
                    nearCircle.LastPoint = nearCircle.CurrentTriangle.P2;
                }

                var  model = new ModelVisual3D() { Content = Circle3D };
                return model;
            }
            
        
    public class circle
        {
            public circle()
            {
                CurrentTriangle = new Triangle();
            }

            public Point3D FirstPoint { get; set; }
            public Point3D LastPoint { get; set; }
            public Triangle CurrentTriangle { get; set; }
        }

        public class Triangle
        {
            public Point3D P0 { get; set; }
            public Point3D P1 { get; set; }
            public Point3D P2 { get; set; }

            public Triangle Clone(double z, bool switchP1andP2)
            {
                var newTriangle = new Triangle();
                newTriangle.P0 = GetPointAdjustedBy(this.P0, new Point3D(0, 0, z));

                var point1 = GetPointAdjustedBy(this.P1, new Point3D(0, 0, z));
                var point2 = GetPointAdjustedBy(this.P2, new Point3D(0, 0, z));

                if (!switchP1andP2)
                {
                    newTriangle.P1 = point1;
                    newTriangle.P2 = point2;
                }
                else
                {
                    newTriangle.P1 = point2;
                    newTriangle.P2 = point1;
                }
                return newTriangle;
            }

            private Point3D GetPointAdjustedBy(Point3D point, Point3D adjustBy)
            {
                var newPoint = new Point3D { X = point.X, Y = point.Y, Z = point.Z };
                newPoint.Offset(adjustBy.X, adjustBy.Y, adjustBy.Z);
                return newPoint;
            }
        }

        private Model3DGroup CreateTriangleModel(MaterialGroup materialGroup, Triangle triangle)
        {
            return CreateTriangleModel(materialGroup, triangle.P0, triangle.P1, triangle.P2);
        }

        private Model3DGroup CreateTriangleModel(Material material, Point3D p0, Point3D p1, Point3D p2)
        {

            var mesh = new MeshGeometry3D();

            mesh.Positions.Add(p0);
            mesh.Positions.Add(p1);
            mesh.Positions.Add(p2);

            mesh.TriangleIndices.Add(0);
            mesh.TriangleIndices.Add(1);
            mesh.TriangleIndices.Add(2);

            var normal = CalculateNormal(p0, p1, p2);

            mesh.Normals.Add(normal);
            mesh.Normals.Add(normal);
            mesh.Normals.Add(normal);

            m_objScreenSpaceLines3D.Points.Add(p1);
            m_objScreenSpaceLines3D.Points.Add(p2);          
           
            var model = new GeometryModel3D(mesh, material);
            var group = new Model3DGroup();
            group.Children.Add(model);
            return group;
        }
        private Vector3D CalculateNormal(Point3D p0, Point3D p1, Point3D p2)
        {
            var v0 = new Vector3D(p1.X - p0.X, p1.Y - p0.Y, p1.Z - p0.Z);
            var v1 = new Vector3D(p2.X - p1.X, p2.Y - p1.Y, p2.Z - p1.Z);
            return Vector3D.CrossProduct(v0, v1);
        }

        public MaterialGroup GetSurfaceMaterial(Color colour)
        {
            var materialGroup = new MaterialGroup();
            var emmMat = new EmissiveMaterial(new SolidColorBrush(colour));
            materialGroup.Children.Add(emmMat);
            materialGroup.Children.Add(new DiffuseMaterial(new SolidColorBrush(colour)));
            var specMat = new SpecularMaterial(new SolidColorBrush(Colors.White), 30);
            materialGroup.Children.Add(specMat);
            return materialGroup;
        }
        #endregion


    }

    #endregion

0 Ответов