duheng
2024-11-14 60908c00556f4f53d82f5588ae3013f80c443590
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
namespace Yw.WinFrmUI.HydroL3d
{
    /// <summary>
    /// 水泵
    /// </summary>
    public class Pump : Link
    {
        /// <summary>
        /// 线宽
        /// </summary>
        public float? LineWidth { get; set; }
 
        /// <summary>
        /// 线色
        /// </summary>
        public Color? LineColor { get; set; }
 
        /// <summary>
        /// 悬停线色
        /// </summary>
        public Color? HoveredLineColor { get; set; }
 
        /// <summary>
        /// 悬停线宽
        /// </summary>
        public float? HoveredLineWidth { get; set; }
 
        /// <summary>
        /// 选择线色
        /// </summary>
        public Color? SelectedLineColor { get; set; }
 
        /// <summary>
        /// 选择线宽
        /// </summary>
        public float? SelectedLineWidth { get; set; }
 
        /// <summary>
        /// 宽度
        /// </summary>
        public float? Width { get; set; }
 
        /// <summary>
        /// 高度
        /// </summary>
        public float? Height { get; set; }
 
        /// <summary>
        /// 悬停宽度
        /// </summary>
        public float? HoveredWidth { get; set; }
 
        /// <summary>
        /// 悬停高度
        /// </summary>
        public float? HoveredHeight { get; set; }
 
        /// <summary>
        /// 选择宽度
        /// </summary>
        public float? SelectedWidth { get; set; }
 
        /// <summary>
        /// 选择高度
        /// </summary>
        public float? SelectedHeight { get; set; }
 
 
 
        //包围盒
        private BoundingBox3d _boundingBox = null;
 
        //获取包围盒
        private BoundingBox3d GetBoundingBox()
        {
            var lineWidth = this.LineWidth.HasValue ? this.LineWidth.Value : CacheHelper.HydroL3d.Pipe.Line.Width;
            return new BoundingBox3d()
            {
                Min = new Point3d()
                {
                    X = Math.Min(this.StartPosition.X, this.EndPosition.X) - lineWidth / 2f,
                    Y = Math.Min(this.StartPosition.Y, this.EndPosition.Y) - lineWidth / 2f,
                    Z = Math.Min(this.StartPosition.Z, this.EndPosition.Z) - lineWidth / 2f
                },
                Max = new Point3d()
                {
                    X = Math.Max(this.StartPosition.X, this.EndPosition.X) + lineWidth / 2f,
                    Y = Math.Max(this.StartPosition.Y, this.EndPosition.Y) + lineWidth / 2f,
                    Z = Math.Max(this.StartPosition.Z, this.EndPosition.Z) + lineWidth / 2f
                }
            };
        }
 
        /// <summary>
        /// 绘制
        /// </summary>
        public override void Draw(SharpGL.OpenGL gl)
        {
            _boundingBox = GetBoundingBox();
            if (this.Hovered)
            {
                var lineWidth = this.HoveredLineWidth.HasValue ? this.HoveredLineWidth.Value : CacheHelper.HydroL3d.Pipe.HoveredLine.Width;
                var lineColor = this.HoveredLineColor.HasValue ? this.HoveredLineColor.Value : ColorTranslator.FromHtml(CacheHelper.HydroL3d.Pipe.HoveredLine.Color);
                DrawPipe(gl, lineWidth / 2f, lineColor);
            }
            else if (this.Selected)
            {
                var lineWidth = this.SelectedLineWidth.HasValue ? this.SelectedLineWidth.Value : CacheHelper.HydroL3d.Pipe.SelectedLine.Width;
                var lineColor = this.SelectedLineColor.HasValue ? this.SelectedLineColor.Value : ColorTranslator.FromHtml(CacheHelper.HydroL3d.Pipe.SelectedLine.Color);
                DrawPipe(gl, lineWidth / 2f, lineColor);
            }
            else
            {
                var lineWidth = this.LineWidth.HasValue ? this.LineWidth.Value : CacheHelper.HydroL3d.Pipe.Line.Width;
                var lineColor = this.LineColor.HasValue ? this.LineColor.Value : ColorTranslator.FromHtml(CacheHelper.HydroL3d.Pipe.Line.Color);
                DrawPipe(gl, lineWidth / 2f, lineColor);
            }
        }
 
        //绘制管道
        private void DrawPipe(SharpGL.OpenGL gl, float radius, Color fillColor)
        {
            gl.Color(fillColor.R / 255.0f, fillColor.G / 255.0f, fillColor.B / 255.0f);
 
            int slices = 32;  // 管道圆周的分段数,可根据需要修改
 
            Point3d direction = new Point3d(this.EndPosition - this.StartPosition);
            float length = direction.Length();
            direction.Normalize();
 
            // 计算旋转轴
            Point3d axis = Point3d.CrossProduct(new Point3d(0, 0, 1), direction);
            if (axis.LengthSquared() < 0.0001f)
            {
                axis = new Point3d(1, 0, 0);
            }
            axis.Normalize();
 
            float angle = (float)Math.Acos(Point3d.DotProduct(new Point3d(0, 0, 1), direction));
 
            gl.PushMatrix();
 
            // 平移到起点
            gl.Translate(this.StartPosition.X, this.StartPosition.Y, this.StartPosition.Z);
 
            // 绕轴旋转
            gl.Rotate(angle * 180.0f / (float)Math.PI, axis.X, axis.Y, axis.Z);
 
            // 绘制管道侧面
            gl.Begin(OpenGL.GL_QUAD_STRIP);
            for (int i = 0; i <= slices; i++)
            {
                float theta = (float)i / slices * 2.0f * (float)Math.PI;
                float x = radius * (float)Math.Cos(theta);
                float y = radius * (float)Math.Sin(theta);
 
                gl.Vertex(x, y, 0.0f);
                gl.Vertex(x, y, length);
            }
            gl.End();
 
            // 绘制管道两端的圆面
            gl.Begin(OpenGL.GL_POLYGON);
            for (int i = 0; i <= slices; i++)
            {
                float theta = (float)i / slices * 2.0f * (float)Math.PI;
                float x = radius * (float)Math.Cos(theta);
                float y = radius * (float)Math.Sin(theta);
 
                gl.Vertex(x, y, 0.0f);
            }
            gl.End();
 
            gl.Begin(OpenGL.GL_POLYGON);
            for (int i = 0; i <= slices; i++)
            {
                float theta = (float)i / slices * 2.0f * (float)Math.PI;
                float x = radius * (float)Math.Cos(theta);
                float y = radius * (float)Math.Sin(theta);
 
                gl.Vertex(x, y, length);
            }
            gl.End();
 
            gl.PopMatrix();
        }
 
        /// <summary>
        /// 包含
        /// </summary>
        public override bool Contains(Point3d pt)
        {
            if (_boundingBox == null)
            {
                return false;
            }
            return _boundingBox.Contains(pt);
        }
 
 
    }
}