lixiaojun
2024-09-25 17b67713b60e9940ddb0d3f9ea35bea04d99cea8
l3d修改
已修改4个文件
367 ■■■■ 文件已修改
WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/00-core/Point3d.cs 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/01-network/00-core/Network_Method.cs 115 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/02-panel/NetworkPanelDebug.Designer.cs 7 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/02-panel/NetworkPanelDebug.cs 233 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/00-core/Point3d.cs
@@ -90,5 +90,17 @@
            return !InValid();
        }
        /// <summary>
        /// 距离
        /// </summary>
        public float Distance(Point3d other)
        {
            if (other == null)
            {
                return default;
            }
            return MathF.Sqrt(MathF.Pow(this.X - other.X, 2) + MathF.Pow(this.Y - other.Y, 2) + MathF.Pow(this.Z - other.Z, 2));
        }
    }
}
WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/01-network/00-core/Network_Method.cs
@@ -1,7 +1,4 @@
using DevExpress.CodeParser;
using DevExpress.DataAccess.MongoDB;
namespace Yw.WinFrmUI.HydroL3d
namespace Yw.WinFrmUI.HydroL3d
{
    /// <summary>
    /// 
@@ -46,8 +43,118 @@
            };
        }
        /// <summary>
        /// 获取中心点
        /// </summary>
        public Point3d GetCenter(BoundingBox3d boundingBox)
        {
            if (boundingBox == null)
            {
                boundingBox = GetBoundingBox();
            }
            return boundingBox.GetCenter();
        }
        /// <summary>
        /// 获取观察信息
        /// </summary>
        public LookAt3d GetLookAt()
        {
            return new LookAt3d()
            {
                Eye = new Point3d(0, 0, 0),
                Center = new Point3d(0, 0, 0),
                Up = new Point3d(0, 1, 0)
            };
        }
        /// <summary>
        /// 获取透视信息
        /// </summary>
        public Perspective3d GetPerspective(LookAt3d lookAt, SharpGL.OpenGLControl openglControl = null)
        {
            if (lookAt == null)
            {
                lookAt = GetLookAt();
            }
            var minDistance = float.MaxValue;
            var maxDistance = float.MinValue;
            foreach (var node in this.Nodes)
            {
                var distance = lookAt.Eye.Distance(node.Position);
                minDistance = MathF.Min(minDistance, distance);
                maxDistance = MathF.Max(maxDistance, distance);
            }
            var bufferFactor = 1.2f;
            var aspect = 1.25f;
            if (openglControl != null)
            {
                aspect = (float)openglControl.Width / (float)openglControl.Height;
            }
            var near = minDistance * bufferFactor;
            var far = maxDistance * bufferFactor;
            return new Perspective3d()
            {
                Fovy = 45f,
                Aspect = aspect,
                Near = near,
                Far = far
            };
        }
        /// <summary>
        /// 获取旋转信息
        /// </summary>
        /// <returns></returns>
        public Point3d GetRotation()
        {
            return new Point3d();
        }
        /// <summary>
        /// 获取转换
        /// </summary>
        public Point3d GetTranslation(BoundingBox3d boudingBox, Point3d center)
        {
            if (boudingBox == null)
            {
                boudingBox = GetBoundingBox();
            }
            if (center == null)
            {
                center = GetCenter(boudingBox);
            }
            var zBufferFactor = 3f;
            return new Point3d()
            {
                X = -center.X,
                Y = -center.Y,
                Z = -boudingBox.Max.Z * zBufferFactor
            };
        }
        /// <summary>
        /// 获取参数
        /// </summary>
        /// <returns></returns>
        public NetworkParas GetParas(SharpGL.OpenGLControl openglControl = null)
        {
            var boundingBox = GetBoundingBox();
            var center = boundingBox.GetCenter();
            var lookAt = GetLookAt();
            var perspective = GetPerspective(lookAt, openglControl);
            var rotation = GetRotation();
            var translation = GetTranslation(boundingBox, center);
            return new NetworkParas()
            {
                BoundingBox = boundingBox,
                Ceneter = center,
                Perspective = perspective,
                LookAt = lookAt,
                Rotation = rotation,
                Translation = translation
            };
        }
WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/02-panel/NetworkPanelDebug.Designer.cs
@@ -76,6 +76,12 @@
            openGLControl1.OpenGLInitialized += openGLControl1_OpenGLInitialized;
            openGLControl1.OpenGLDraw += openGLControl1_OpenGLDraw;
            openGLControl1.Resized += openGLControl1_Resized;
            openGLControl1.MouseClick += openGLControl1_MouseClick;
            openGLControl1.MouseDoubleClick += openGLControl1_MouseDoubleClick;
            openGLControl1.MouseDown += openGLControl1_MouseDown;
            openGLControl1.MouseHover += openGLControl1_MouseHover;
            openGLControl1.MouseMove += openGLControl1_MouseMove;
            openGLControl1.MouseUp += openGLControl1_MouseUp;
            // 
            // propertyGridControl1
            // 
@@ -85,7 +91,6 @@
            propertyGridControl1.OptionsView.AllowReadOnlyRowAppearance = DevExpress.Utils.DefaultBoolean.True;
            propertyGridControl1.Size = new Size(299, 613);
            propertyGridControl1.TabIndex = 0;
            propertyGridControl1.CellValueChanged += propertyGridControl1_CellValueChanged;
            // 
            // NetworkPanelDebug
            // 
WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/02-panel/NetworkPanelDebug.cs
@@ -1,5 +1,6 @@
using DevExpress.Mvvm.Native;
using DevExpress.XtraCharts.GLGraphics;
using DevExpress.XtraScheduler.Outlook.Interop;
using SharpGL;
namespace Yw.WinFrmUI.HydroL3d
@@ -9,7 +10,9 @@
        public NetworkPanelDebug()
        {
            InitializeComponent();
            this.openGLControl1.MouseWheel += OpenGLControl1_MouseWheel;
        }
        private Network _network = null;//管网
@@ -27,27 +30,18 @@
        public virtual void Initial(Network network)
        {
            _network = network;
            _paras = new NetworkParas();
            _paras.BoundingBox = _network.GetBoundingBox();
            _paras.Ceneter = _paras.BoundingBox.GetCenter();
            _paras.Rotation = new Point3d();
            _paras.Translation = new Point3d();
            _paras.Perspective = new Perspective3d()
            {
                Fovy = 60f,
                Aspect = this.openGLControl1.Width / (float)this.openGLControl1.Height,
                Near = 5,
                Far = 100
            };
            _paras.LookAt = new LookAt3d()
            {
                Eye = new Point3d(),
                Center = new Point3d(),
                Up = new Point3d()
            };
            _paras = _network.GetParas();
            this.propertyGridControl1.SelectedObject = _paras;
        }
        //gl 初始化
        private void openGLControl1_OpenGLInitialized(object sender, EventArgs e)
        {
            OpenGL gl = openGLControl1.OpenGL;
            gl.ClearColor(0, 0, 0, 0);
        }
        //gl 绘制
        private void openGLControl1_OpenGLDraw(object sender, RenderEventArgs args)
        {
            if (!Initialized)
@@ -67,8 +61,7 @@
            gl.LoadIdentity();
            // 创建透视投影变换
            gl.Perspective(45f, openGLControl1.Width / openGLControl1.Height, 15696f, 38395f);
            gl.Perspective(_paras.Perspective.Fovy, _paras.Perspective.Aspect, _paras.Perspective.Near, _paras.Perspective.Far);
            // 设置当前矩阵为模型视图矩阵
            gl.MatrixMode(OpenGL.GL_MODELVIEW);
@@ -76,12 +69,11 @@
            //重置当前指定的矩阵为单位矩阵,将当前的用户坐标系的原点移到了屏幕中心
            gl.LoadIdentity();
           // gl.Translate(-_paras.Ceneter.X, -_paras.Ceneter.Y, -_paras.Ceneter.Z);
            //gl.Translate(0, 0, -5f);
            gl.Translate(_paras.Translation.X, _paras.Translation.Y, _paras.Translation.Z);
            //gl.Rotate(_paras.Rotation.X, 1, 0, 0);
            // gl.Rotate(_paras.Rotation.Y, 0, 1, 0);
            //gl.Rotate(_paras.Rotation.Z, 0, 0, 1);
            gl.Rotate(_paras.Rotation.X, 1, 0, 0);
            gl.Rotate(_paras.Rotation.Y, 0, 1, 0);
            gl.Rotate(_paras.Rotation.Z, 0, 0, 1);
            gl.Color(1.0f, 1.0f, 1.0f);
            foreach (var pipe in _network.Pipes)
@@ -89,99 +81,160 @@
                var startPosition = pipe.StartPosition;
                var endPosition = pipe.EndPosition;
                gl.Begin(OpenGL.GL_LINES);
                gl.Vertex(startPosition.X, startPosition.Y, startPosition.Z);//左顶点
                gl.Vertex(endPosition.X, endPosition.Y, endPosition.Z);//右顶点
                gl.End();
            }
            gl.Color(1.0f, 1.0f, 1.0f);
            gl.Begin(OpenGL.GL_LINES);
            gl.LookAt(_paras.LookAt.Eye.X, _paras.LookAt.Eye.Y, _paras.LookAt.Eye.Z,
                _paras.LookAt.Center.X, _paras.LookAt.Center.Y, _paras.LookAt.Center.Z,
                _paras.LookAt.Up.X, _paras.LookAt.Up.Y, _paras.LookAt.Up.Z);
            gl.Vertex(_paras.BoundingBox.Min.X, _paras.BoundingBox.Min.Y, _paras.BoundingBox.Min.Z);//左顶点
            gl.Vertex(_paras.BoundingBox.Max.X, _paras.BoundingBox.Max.Y, _paras.BoundingBox.Max.Z);//右顶点
            gl.End();
            #region 点到线
            gl.Begin(OpenGL.GL_LINES);
            gl.Color(1.0f, 1.0f, 1.0f);
            gl.Vertex(-2.0f, 0.0f, 0.0f);//左顶点
            gl.Vertex(2.0f, 2.0f, 0.0f);//右顶点
            gl.End();
            #endregion
            var minX = _paras.BoundingBox.Min.X;
            var maxX = _paras.BoundingBox.Max.X;
            var minY = _paras.BoundingBox.Min.Y;
            var maxY = _paras.BoundingBox.Max.Y;
            var minZ = _paras.BoundingBox.Min.Z;
            var maxZ = _paras.BoundingBox.Max.Z;
            float distance = Math.Max(maxX - minX, Math.Max(maxY - minY, maxZ - minZ)) * 1.5f;
            var centerX = (minX + maxX) / 2f;
            var centerY = (minY + maxY) / 2f;
            var centerZ = (minZ + maxZ) / 2f;
            var cameraX = centerX + distance;
            var cameraY = centerY + distance;
            var cameraZ = centerZ + distance;
            //gl.LookAt(cameraX, cameraY, cameraZ, centerX, centerY, centerZ, 0, 1, 0);
            gl.LookAt(-27484f, -12576f, 8833f, 23675f, 23079f, -7200f, 0f, 0f, 1f);
            gl.Flush();   //强制刷新
        }
        private void openGLControl1_OpenGLInitialized(object sender, EventArgs e)
        private void openGLControl1_MouseDown(object sender, MouseEventArgs e)
        {
            OpenGL gl = openGLControl1.OpenGL;
            gl.ClearColor(0, 0, 0, 0);
            HasMouseRightDown(e);
        }
        private void openGLControl1_MouseMove(object sender, MouseEventArgs e)
        {
            var hasMouseRightDownMove = MouseRightDownMove(e);
            if (hasMouseRightDownMove)
            {
                this.Invalidate();
            }
        }
        private void openGLControl1_MouseUp(object sender, MouseEventArgs e)
        {
            var hasMouseRightUp = HasMouseRightUp(e);
            if (hasMouseRightUp)
            {
                this.Invalidate();
            }
        }
        private void openGLControl1_MouseHover(object sender, EventArgs e)
        {
        }
        private void openGLControl1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (!Initialized)
            {
                return;
            }
            if (e.Button == MouseButtons.Left)
            {
                _paras = _network.GetParas(this.openGLControl1);
                this.openGLControl1.Invalidate();
                this.propertyGridControl1.SelectedObject = _paras;
                this.propertyGridControl1.UpdateRows();
            }
        }
        private void openGLControl1_MouseClick(object sender, MouseEventArgs e)
        {
        }
        private void OpenGLControl1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                _paras.Perspective.Fovy += 1f;
            }
            else
            {
                _paras.Perspective.Fovy -= 1f;
            }
            this.openGLControl1.Invalidate();
            this.propertyGridControl1.UpdateRows();
        }
        //Resize
        private void openGLControl1_Resized(object sender, EventArgs e)
        {
            if (!Initialized)
            {
                return;
            }
            // _paras.Perspective.Aspect = this.openGLControl1.Width / (float)this.openGLControl1.Height;
            //Reset();
            _paras.Perspective.Aspect = (float)this.openGLControl1.Width / (float)this.openGLControl1.Height;
            this.propertyGridControl1.UpdateRows();
        }
        //重置
        private void Reset()
        #region 鼠标右键按下拖动
        /// <summary>
        /// 当鼠标右键按下时允许拖动
        /// </summary>
        [Browsable(true)]
        [Description("当鼠标右键按下时允许拖动")]
        [DefaultValue(true)]
        public bool AllowMoveWhenMouseRightDown
        {
            if (!Initialized)
            {
                return;
            }
            OpenGL gl = openGLControl1.OpenGL;
            // 设置当前矩阵模式,对投影矩阵应用随后的矩阵操作
            gl.MatrixMode(OpenGL.GL_PROJECTION);
            // 重置当前指定的矩阵为单位矩阵,将当前的用户坐标系的原点移到了屏幕中心
            gl.LoadIdentity();
            // 创建透视投影变换
            gl.Perspective(_paras.Perspective.Fovy, _paras.Perspective.Aspect, _paras.Perspective.Near, _paras.Perspective.Far);
            // 视点变换
            // gl.LookAt(_paras.LookAt.Eye.X, _paras.LookAt.Eye.Y, _paras.LookAt.Eye.Z, _paras.LookAt.Center.X, _paras.LookAt.Center.Y, _paras.LookAt.Center.Z, _paras.LookAt.Up.X, _paras.LookAt.Up.Y, _paras.LookAt.Up.Z);
            // 设置当前矩阵为模型视图矩阵
            gl.MatrixMode(OpenGL.GL_MODELVIEW);
            get => _allowMoveWhenMouseRightDown;
            set => _allowMoveWhenMouseRightDown = value;
        }
        private bool _allowMoveWhenMouseRightDown = true;
        private void propertyGridControl1_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
        protected bool _hasMouseRightDown = false;//鼠标右键是否按下
        protected Point _mouseRightDownMovePoint;//鼠标右键按下移动点
        /// <summary>
        /// 判断鼠标右键是否按下
        /// </summary>
        protected virtual bool HasMouseRightDown(MouseEventArgs e)
        {
            if (!Initialized)
            if (e.Button == MouseButtons.Right)
            {
                return;
                _hasMouseRightDown = true;
                _mouseRightDownMovePoint = e.Location;
                return true;
            }
            //Reset();
            return false;
        }
        /// <summary>
        /// 鼠标右键按下移动
        /// </summary>
        protected virtual bool MouseRightDownMove(MouseEventArgs e)
        {
            if (_hasMouseRightDown)
            {
                if (this.AllowMoveWhenMouseRightDown)
                {
                    if (this.Initialized)
                    {
                        var pt = new PointF(e.X - _mouseRightDownMovePoint.X, e.Y - _mouseRightDownMovePoint.Y);
                        _paras.Translation.X += pt.X;
                        _paras.Translation.Y -= pt.Y;
                        _mouseRightDownMovePoint = e.Location;
                        return true;
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// 判断鼠标左键是否弹起
        /// </summary>
        protected virtual bool HasMouseRightUp(MouseEventArgs e)
        {
            if (_hasMouseRightDown)
            {
                _hasMouseRightDown = false;
                return true;
            }
            return false;
        }
        #endregion
    }
}