lixiaojun
2024-09-25 21c97aab9153758bebfc9b3944859b0eec271c76
WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/01-network/00-core/Network_Method.cs
@@ -1,4 +1,7 @@
namespace Yw.WinFrmUI.HydroL3d
using DevExpress.XtraCharts.Native;
using DevExpress.XtraDiagram.Bars;
namespace Yw.WinFrmUI.HydroL3d
{
    /// <summary>
    /// 
@@ -56,49 +59,23 @@
        }
        /// <summary>
        /// 获取观察信息
        /// 获取转换
        /// </summary>
        public LookAt3d GetLookAt()
        public Point3d GetTranslation(BoundingBox3d boundingBox, Point3d center)
        {
            return new LookAt3d()
            if (boundingBox == null)
            {
                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();
                boundingBox = GetBoundingBox();
            }
            var minDistance = float.MaxValue;
            var maxDistance = float.MinValue;
            foreach (var node in this.Nodes)
            if (center == null)
            {
                var distance = lookAt.Eye.Distance(node.Position);
                minDistance = MathF.Min(minDistance, distance);
                maxDistance = MathF.Max(maxDistance, distance);
                center = GetCenter(boundingBox);
            }
            var bufferFactor = 1.2f;
            var aspect = 1.25f;
            if (openglControl != null)
            return new Point3d()
            {
                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
                X = -center.X,
                Y = -center.Y,
                Z = -boundingBox.Max.Z * 3f
            };
        }
@@ -112,26 +89,77 @@
        }
        /// <summary>
        /// 获取转换
        /// 缩放
        /// </summary>
        public Point3d GetTranslation(BoundingBox3d boudingBox, Point3d center)
        public Point3d GetScale()
        {
            if (boudingBox == null)
            return new Point3d() { X = 1f, Y = 1f, Z = 1f };
        }
        /// <summary>
        /// 获取观察信息
        /// </summary>
        public LookAt3d GetLookAt(BoundingBox3d boundingBox, Point3d center)
        {
            if (boundingBox == null)
            {
                boudingBox = GetBoundingBox();
                boundingBox = GetBoundingBox();
            }
            if (center == null)
            {
                center = GetCenter(boudingBox);
                center = boundingBox.GetCenter();
            }
            var zBufferFactor = 3f;
            return new Point3d()
            return new LookAt3d()
            {
                X = -center.X,
                Y = -center.Y,
                Z = -boudingBox.Max.Z * zBufferFactor
                Eye = new Point3d(center.X, center.Y, center.Z),
                Center = new Point3d(center.X, center.Y, center.Z),
                Up = new Point3d(0, 1, 0)
            };
        }
        /// <summary>
        /// 获取透视信息
        /// </summary>
        public Perspective3d GetPerspective(BoundingBox3d boundingBox, Point3d center, LookAt3d lookAt, SharpGL.OpenGLControl openglControl = null)
        {
            if (boundingBox == null)
            {
                boundingBox = GetBoundingBox();
            }
            if (center == null)
            {
                center = boundingBox.GetCenter();
            }
            if (lookAt == null)
            {
                lookAt = GetLookAt(boundingBox, center);
            }
            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 fovy = 45f;
            var aspect = 1.25f;
            if (openglControl != null)
            {
                aspect = (float)openglControl.Width / (float)openglControl.Height;
            }
            var bufferFactor = 1.2f;
            var near = minDistance / 100f;
            var far = maxDistance * 5f * bufferFactor;
            return new Perspective3d()
            {
                Fovy = fovy,
                Aspect = aspect,
                Near = near,
                Far = far
            };
        }
        /// <summary>
        /// 获取参数
@@ -141,23 +169,94 @@
        {
            var boundingBox = GetBoundingBox();
            var center = boundingBox.GetCenter();
            var lookAt = GetLookAt();
            var perspective = GetPerspective(lookAt, openglControl);
            var rotation = GetRotation();
            var translation = GetTranslation(boundingBox, center);
            var scale = GetScale();
            var rotation = GetRotation();
            var lookAt = GetLookAt(boundingBox, center);
            var perspective = GetPerspective(boundingBox, center, lookAt, openglControl);
            return new NetworkParas()
            {
                BoundingBox = boundingBox,
                Ceneter = center,
                Perspective = perspective,
                LookAt = lookAt,
                Translation = translation,
                Rotation = rotation,
                Translation = translation
                Scale = scale,
                LookAt = lookAt,
                Perspective = perspective,
            };
        }
        /// <summary>
        /// 绘制
        /// </summary>
        public void Draw(SharpGL.OpenGL gl)
        {
            foreach (var link in this.Links)
            {
                link.Draw(gl);
            }
            foreach (var node in this.Nodes)
            {
                node.Draw(gl);
            }
        }
        /// <summary>
        /// 选择
        /// </summary>
        public List<Parter> Select(Point3d pt)
        {
            int i = 0;
            foreach (var node in this.Nodes)
            {
                node.Selected = node.Contains(pt);
                if (node.Selected)
                {
                    i++;
                }
            }
            if (i > 0)
            {
                this.Links.ForEach(x => x.Selected = false);
                return this.Nodes.Where(x => x.Selected).Select(x => x as Parter).ToList();
            }
            foreach (var link in this.Links)
            {
                link.Selected = link.Contains(pt);
            }
            return this.Links.Where(x => x.Selected).Select(x => x as Parter).ToList();
        }
        /// <summary>
        /// 悬停
        /// </summary>
        public List<Parter> Hover(Point3d pt)
        {
            int i = 0;
            foreach (var node in this.Nodes)
            {
                node.Hovered = node.Contains(pt);
                if (node.Hovered)
                {
                    i++;
                }
            }
            if (i > 0)
            {
                this.Links.ForEach(x => x.Hovered = false);
                return this.Nodes.Where(x => x.Hovered).Select(x => x as Parter).ToList();
            }
            foreach (var link in this.Links)
            {
                link.Hovered = link.Contains(pt);
            }
            return this.Links.Where(x => x.Hovered).Select(x => x as Parter).ToList();
        }