Shuxia Ning
2024-11-19 a01861a95ede48fa4979a47b24f21616e362e534
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
namespace Yw.WinFrmUI.HydroL3d
{
    /// <summary>
    /// 水库
    /// </summary>
    public class Reservoir : Source
    {
 
        /// <summary>
        /// 长度
        /// </summary>
        public float? Length { get; set; }
 
        /// <summary>
        /// 宽度
        /// </summary>
        public float? Width { get; set; }
 
        /// <summary>
        /// 高度
        /// </summary>
        public float? Height { get; set; }
 
        /// <summary>
        /// 填充颜色
        /// </summary>
        public Color? FillColor { get; set; }
 
        /// <summary>
        /// 悬停长度
        /// </summary>
        public float? HoveredLength { get; set; }
 
        /// <summary>
        /// 悬停宽度
        /// </summary>
        public float? HoveredWidth { get; set; }
 
        /// <summary>
        /// 悬停高度
        /// </summary>
        public float? HoveredHeight { get; set; }
 
        /// <summary>
        /// 悬停填充颜色
        /// </summary>
        public Color? HoveredFillColor { get; set; }
 
        /// <summary>
        /// 选择长度
        /// </summary>
        public float? SelectedLength { get; set; }
 
        /// <summary> 
        /// 选择宽度
        /// </summary>
        public float? SelectedWidth { get; set; }
 
        /// <summary>
        /// 选择高度
        /// </summary>
        public float? SelectedHeight { get; set; }
 
        /// <summary>
        /// 选择填充颜色
        /// </summary>
        public Color? SelectedFillColor { get; set; }
 
        //包围盒
        private BoundingBox3d _boundingBox = null;
 
        //获取包围盒
        private BoundingBox3d GetBoundingBox()
        {
            var length = this.Length.HasValue ? this.Length.Value : CacheHelper.HydroL3d.Reservoir.Size.Length;
            var width = this.Width.HasValue ? this.Width.Value : CacheHelper.HydroL3d.Reservoir.Size.Width;
            var height = this.Height.HasValue ? this.Height.Value : CacheHelper.HydroL3d.Reservoir.Size.Height;
            return new BoundingBox3d()
            {
                Min = new Point3d()
                {
                    X = this.Position.X - length / 2f,
                    Y = this.Position.Y - width / 2f,
                    Z = this.Position.Z - height / 2f
                },
                Max = new Point3d()
                {
                    X = this.Position.X + length / 2f,
                    Y = this.Position.Y + width / 2f,
                    Z = this.Position.Z + height / 2f
                }
            };
        }
 
        /// <summary>
        /// 绘制
        /// </summary>
        public override void Draw(SharpGL.OpenGL gl)
        {
            _boundingBox = GetBoundingBox();
            if (this.Hovered)
            {
                var length = this.HoveredLength.HasValue ? this.HoveredLength.Value : CacheHelper.HydroL3d.Reservoir.HoveredSize.Length;
                var width = this.HoveredWidth.HasValue ? this.HoveredWidth.Value : CacheHelper.HydroL3d.Reservoir.HoveredSize.Width;
                var height = this.HoveredHeight.HasValue ? this.HoveredHeight.Value : CacheHelper.HydroL3d.Reservoir.HoveredSize.Height;
                var fillColor = this.HoveredFillColor.HasValue ? this.HoveredFillColor.Value : ColorTranslator.FromHtml(CacheHelper.HydroL3d.Reservoir.HoveredFill.Color);
                DrawEllipsoid(gl, length / 2f, width / 2f, height / 2f, fillColor);
            }
            else if (this.Selected)
            {
                var length = this.SelectedLength.HasValue ? this.SelectedLength.Value : CacheHelper.HydroL3d.Reservoir.SelectedSize.Length;
                var width = this.SelectedWidth.HasValue ? this.SelectedWidth.Value : CacheHelper.HydroL3d.Reservoir.SelectedSize.Width;
                var height = this.SelectedHeight.HasValue ? this.SelectedHeight.Value : CacheHelper.HydroL3d.Reservoir.SelectedSize.Height;
                var fillColor = this.SelectedFillColor.HasValue ? this.SelectedFillColor.Value : ColorTranslator.FromHtml(CacheHelper.HydroL3d.Reservoir.SelectedFill.Color);
                DrawEllipsoid(gl, length / 2f, width / 2f, height / 2f, fillColor);
            }
            else
            {
                var length = this.Length.HasValue ? this.Length.Value : CacheHelper.HydroL3d.Reservoir.Size.Length;
                var width = this.Width.HasValue ? this.Width.Value : CacheHelper.HydroL3d.Reservoir.Size.Width;
                var height = this.Height.HasValue ? this.Height.Value : CacheHelper.HydroL3d.Reservoir.Size.Height;
                var fillColor = this.FillColor.HasValue ? this.FillColor.Value : ColorTranslator.FromHtml(CacheHelper.HydroL3d.Reservoir.Fill.Color);
                DrawEllipsoid(gl, length / 2f, width / 2f, height / 2f, fillColor);
            }
        }
 
        //绘制椭球体
        private void DrawEllipsoid(OpenGL gl, float halfLength, float halfWidth, float halfHeight, Color fillColor)
        {
            const int stacks = 32;
            const int slices = 32;
 
            // 设置椭球体的中心点坐标
            float x0 = this.Position.X;
            float y0 = this.Position.Y;
            float z0 = this.Position.Z;
 
            // 设置椭球体的半轴长
            float a = halfLength;
            float b = halfWidth;
            float c = halfHeight;
 
            gl.Color(fillColor.R, fillColor.G, fillColor.B);
 
 
            for (int i = 0; i < stacks; i++)
            {
                float lat0 = ((float)i / (float)stacks - 0.5f) * (float)MathF.PI;
                float z0_ = (float)MathF.Sin(lat0);
                float zr0 = (float)MathF.Cos(lat0);
 
                float lat1 = ((float)(i + 1) / (float)stacks - 0.5f) * (float)MathF.PI;
                float z1_ = (float)MathF.Sin(lat1);
                float zr1 = (float)MathF.Cos(lat1);
 
                gl.Begin(SharpGL.OpenGL.GL_TRIANGLE_STRIP);
                for (int j = 0; j <= slices; j++)
                {
                    float lng = ((float)j / (float)slices) * 2.0f * (float)MathF.PI;
 
                    // 根据椭球体方程计算顶点坐标并加上中心点坐标
                    float x = a * (float)MathF.Cos(lng) * zr0 + x0;
                    float y = b * (float)MathF.Sin(lng) * zr0 + y0;
                    float z = c * z0_ + z0;
                    gl.Color(1.0f, 0.0f, 0.0f);
                    gl.Vertex(x, y, z);
 
                    x = a * (float)MathF.Cos(lng) * zr1 + x0;
                    y = b * (float)MathF.Sin(lng) * zr1 + y0;
                    z = c * z1_ + z0;
                    gl.Color(1.0f, 0.0f, 0.0f);
                    gl.Vertex(x, y, z);
                }
                gl.End();
            }
        }
 
        /// <summary>
        /// 包含
        /// </summary>
        public override bool Contains(Point3d pt)
        {
            if (_boundingBox == null)
            {
                return false;
            }
            return _boundingBox.Contains(pt);
        }
 
 
 
    }
}