duheng
2025-03-28 9be9ba4e159969fb5e32648c2c34e912ccc3ae6d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
namespace Yw.WinFrmUI.HydroL3d
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Network
    {
 
        /// <summary>
        /// 判断是否存在
        /// </summary>
        public bool IsExist(string id)
        {
            return _parters.Exists(x => x.Id == id);
        }
 
        /// <summary>
        /// 获取包围盒
        /// </summary>
        public BoundingBox3d GetBoundingBox()
        {
            var minX = float.MaxValue;
            var minY = float.MaxValue;
            var minZ = float.MaxValue;
            var maxX = float.MinValue;
            var maxY = float.MinValue;
            var maxZ = float.MinValue;
 
            foreach (var node in this.Nodes)
            {
                minX = Math.Min(minX, node.Position.X);
                minY = Math.Min(minY, node.Position.Y);
                minZ = Math.Min(minZ, node.Position.Z);
                maxX = Math.Max(maxX, node.Position.X);
                maxY = Math.Max(maxY, node.Position.Y);
                maxZ = Math.Max(maxZ, node.Position.Z);
            }
 
            return new BoundingBox3d()
            {
                Min = new Point3d(minX, minY, minZ),
                Max = new Point3d(maxX, maxY, maxZ)
            };
        }
 
        /// <summary>
        /// 获取中心点
        /// </summary>
        public Point3d GetCenter(BoundingBox3d boundingBox)
        {
            if (boundingBox == null)
            {
                boundingBox = GetBoundingBox();
            }
            return boundingBox.GetCenter();
        }
 
        /// <summary>
        /// 获取转换
        /// </summary>
        public Point3d GetTranslation(BoundingBox3d boundingBox, Point3d center)
        {
            if (boundingBox == null)
            {
                boundingBox = GetBoundingBox();
            }
            if (center == null)
            {
                center = GetCenter(boundingBox);
            }
            return new Point3d()
            {
                X = -center.X,
                Y = -center.Y,
                Z = -center.Z
            };
        }
 
        /// <summary>
        /// 获取旋转信息
        /// </summary>
        /// <returns></returns>
        public Point3d GetRotation()
        {
            return new Point3d();
        }
 
        /// <summary>
        /// 缩放
        /// </summary>
        public Point3d GetScale()
        {
            return new Point3d() { X = 1f, Y = 1f, Z = 1f };
        }
 
        /// <summary>
        /// 获取观察信息
        /// </summary>
        public LookAt3d GetLookAt(BoundingBox3d boundingBox, Point3d center)
        {
            if (boundingBox == null)
            {
                boundingBox = GetBoundingBox();
            }
            if (center == null)
            {
                center = boundingBox.GetCenter();
            }
            return new LookAt3d()
            {
                Eye = new Point3d(0, 0, 3 * boundingBox.Max.Z),
                Center = new Point3d(0, 0, 0),
                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>
        /// 获取参数
        /// </summary>
        /// <returns></returns>
        public NetworkParas GetParas(SharpGL.OpenGLControl openglControl = null)
        {
            var boundingBox = GetBoundingBox();
            var center = boundingBox.GetCenter();
            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,
                Translation = translation,
                Rotation = rotation,
                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();
        }
 
 
 
    }
}