-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProfilePanel.cs
394 lines (346 loc) · 13.1 KB
/
ProfilePanel.cs
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace NacaProfile
{
enum FieldMode
{
Polygon,
Bezier,
PSpline
}
class Segment
{
public int FirstIndex;
public int LastIndex;
public bool Negative;
public Segment(int firstIndex, int lastIndex, bool negative)
{
FirstIndex = firstIndex;
LastIndex = lastIndex;
Negative = negative;
}
}
class ProfilePanel : Panel
{
private Dictionary<string, Color> colors = new Dictionary<string, Color>();
private Profile profile;
private PointF viewCenter = new PointF(0.5f, 0);
private float viewRadius = 1.5f;
private float[] data = new float[0];
private bool showFields = true;
private bool showProbes = true;
private bool showValues = true;
private bool showValuesText = false;
private bool showNormals = false;
private bool showCentroid = false;
private bool antiAlias = true;
private FieldMode fieldMode = FieldMode.PSpline;
private float normalFactor = 0.15f;
private float valueFactor = 1.0f;
public Profile Profile
{
get { return profile; }
set { profile = value; }
}
public PointF ViewCenter
{
get { return viewCenter; }
set { viewCenter = value; Invalidate(); }
}
public float ViewRadius
{
get { return viewRadius; }
set { viewRadius = value; Invalidate(); }
}
public float UnitsPerPixel
{
get { return viewRadius / Math.Min(Width, Height); }
}
public float[] Data
{
get { return data; }
set { data = value; Invalidate(); }
}
public bool ShowFields
{
get { return showFields; }
set { showFields = value; Invalidate(); }
}
public bool ShowProbes
{
get { return showProbes; }
set { showProbes = value; Invalidate(); }
}
public bool ShowValues
{
get { return showValues; }
set { showValues = value; Invalidate(); }
}
public bool ShowValuesText
{
get { return showValuesText; }
set { showValuesText = value; Invalidate(); }
}
public bool ShowNormals
{
get { return showNormals; }
set { showNormals = value; Invalidate(); }
}
public bool ShowCentroid
{
get { return showCentroid; }
set { showCentroid = value; Invalidate(); }
}
public bool AntiAliasing
{
get { return antiAlias; }
set { antiAlias = value; Invalidate(); }
}
public FieldMode FieldMode
{
get { return fieldMode; }
set { fieldMode = value; Invalidate(); }
}
public float NormalFactor
{
get { return normalFactor; }
set { normalFactor = value; Invalidate(); }
}
public float ValueFactor
{
get { return valueFactor; }
set { valueFactor = value; Invalidate(); }
}
public ProfilePanel()
{
DoubleBuffered = true;
ResizeRedraw = true;
colors.Add("profile_bg", Color.LightGray);
colors.Add("profile_border", Color.Black);
colors.Add("probes", Color.Red);
colors.Add("normal_arrows", Color.Red);
colors.Add("value_points_positive", Color.Blue);
colors.Add("value_points_negative", Color.Red);
colors.Add("value_text_positive", Color.Black);
colors.Add("value_text_negative", Color.Black);
colors.Add("fields_positive", Color.LightBlue);
colors.Add("fields_negative", Color.LightPink);
colors.Add("centroid", Color.Black);
}
public void SetDummyData(float sinusIntervall)
{
data = new float[profile.Probes.Count];
for (int i = 0; i < data.Length; i++)
{
data[i] = (float)Math.Sin(i * sinusIntervall);
}
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
if (antiAlias)
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
else
e.Graphics.SmoothingMode = SmoothingMode.HighSpeed;
e.Graphics.FillRectangle(Brushes.White, 0, 0, Width, Height);
if (profile != null)
{
PointF[] profilePoints = CalculateProfilePoints();
PointF[] probePoints = CalculateProbePoints(profilePoints);
VectorF[] probeNormals = CalculateProbeNormals();
PointF[] valuePoints = CalculateValuePoints(probePoints, probeNormals, data);
if (showFields) DrawFields(e.Graphics, data, valuePoints);
DrawProfile(e.Graphics, profilePoints);
if (showProbes) DrawProbes(e.Graphics, probePoints);
if (ShowNormals) DrawProbeNormals(e.Graphics, probePoints, probeNormals);
if (showValues) DrawValues(e.Graphics, valuePoints, data);
if (showCentroid) DrawDot(e.Graphics, colors["centroid"], ToScreenCoords(profile.Centroid), 3);
}
}
private void DrawDot(Graphics g, Color c, PointF pos, int radius = 3)
{
g.FillEllipse(new SolidBrush(c), new RectangleF(pos.X - radius, pos.Y - radius, radius * 2, radius * 2));
}
private void DrawArrow(Graphics g, Color c, PointF p1, PointF p2, int thinkness = 1)
{
Pen pen = new Pen(c, thinkness);
pen.EndCap = LineCap.ArrowAnchor;
g.DrawLine(pen, p1, p2);
}
private PointF ToScreenCoords(PointF p)
{
float unitsPerPixel = UnitsPerPixel;
float width = Width * unitsPerPixel;
float height = Height * unitsPerPixel;
RectangleF rect = new RectangleF(
viewCenter.X - width / 2.0f,
viewCenter.Y - height / 2.0f,
width,
height
);
float xd = Width / rect.Width;
float yd = Height / rect.Height;
float ox = (0 - rect.X) * xd;
float oy = (0 - rect.Y) * yd;
return new PointF(p.X * xd + ox, -p.Y * yd + oy);
}
private PointF[] CalculateProfilePoints()
{
PointF[] profilePoints = new PointF[profile.Points.Count];
for (int i = 0; i < profile.Points.Count; i++)
{
profilePoints[i] = ToScreenCoords(profile.Points[i]);
}
return profilePoints;
}
private PointF[] CalculateProbePoints(PointF[] profilePoints)
{
PointF[] probePoints = new PointF[profile.Probes.Count];
for (int i = 0; i < profile.Probes.Count; i++)
{
int index = profile.Probes[i].Index;
probePoints[i] = profilePoints[index];
}
return probePoints;
}
private VectorF[] CalculateProbeNormals()
{
VectorF[] normals = new VectorF[profile.Probes.Count];
for (int i = 0; i < profile.Probes.Count; i++)
{
normals[i] = profile.Probes[i].NormalVector.Norm() * normalFactor;
}
return normals;
}
private PointF[] CalculateValuePoints(PointF[] probePoints, VectorF[] normals, float[] data)
{
PointF[] valuePoints = new PointF[profile.Probes.Count];
for (int i = 0; i < profile.Probes.Count; i++)
{
float value = 0;
if (data.Length > i) value = data[i] * valueFactor;
if (value < 0) value *= -1;
int index = profile.Probes[i].Index;
valuePoints[i] = ToScreenCoords(profile.Points[index] + normals[i] * value);
}
return valuePoints;
}
private void DrawProfile(Graphics g, PointF[] profilePoints)
{
g.FillPolygon(new SolidBrush(colors["profile_bg"]), profilePoints);
g.DrawPolygon(new Pen(colors["profile_border"]), profilePoints);
}
private void DrawProbes(Graphics g, PointF[] probePoints)
{
for (int i = 0; i < probePoints.Length; i++)
DrawDot(g, colors["probes"], probePoints[i], 3);
}
private void DrawProbeNormals(Graphics g, PointF[] probePoints, VectorF[] normals)
{
for (int i = 0; i < profile.Probes.Count; i++)
{
int index = profile.Probes[i].Index;
PointF normalPoint = profile.Points[index] + normals[i];
DrawArrow(g, colors["normal_arrows"], probePoints[i], ToScreenCoords(normalPoint));
}
}
private void DrawValues(Graphics g, PointF[] valuePoints, float[] data)
{
for (int i = 0; i < profile.Probes.Count; i++)
{
Color pointColor = colors["value_points_positive"];
Color textColor = colors["value_text_positive"];
if (data[i] < 0)
{
pointColor = colors["value_points_negative"];
textColor = colors["value_text_negative"];
}
DrawDot(g, pointColor, valuePoints[i], 2);
if (showValuesText)
{
PointF position = valuePoints[i];
position.X += 5;
position.Y -= 5;
string str = String.Format(new System.Globalization.CultureInfo("en-US"), "{0:N}", data[i]);
g.DrawString(str, DefaultFont, new SolidBrush(textColor), position);
}
}
}
public void DrawFields(Graphics g, float[] data, PointF[] valuePoints)
{
if (data != null && data.Length == profile.Probes.Count)
{
List<Segment> segments = CreateDataSegments();
DrawSegments(g, valuePoints, segments);
}
}
private void DrawSegments(Graphics g, PointF[] valuePoints, List<Segment> segments)
{
for (int j = 0; j < segments.Count; j++)
{
// Create segment points for polygon
List<PointF> segmentPoints = new List<PointF>();
segmentPoints.Add(InterpolateFieldBorder(segments, j));
for (int k = segments[j].FirstIndex; k <= segments[j].LastIndex; k++)
{
segmentPoints.Add(valuePoints[k]);
}
segmentPoints.Add(InterpolateFieldBorder(segments, j + 1));
// Draw the polygon
DrawFieldSegment(g, segments[j].Negative, segmentPoints.ToArray());
}
}
private void DrawFieldSegment(Graphics g, bool negative, PointF[] segmentPoints)
{
Brush brush = new SolidBrush(colors["fields_positive"]);
if (negative)
brush = new SolidBrush(colors["fields_negative"]);
if (fieldMode == FieldMode.Bezier)
g.FillPolygon(brush, Bezier.CreateBezierPath(segmentPoints));
else if (fieldMode == FieldMode.PSpline)
g.FillPolygon(brush, Spline.CreateSplinePath(segmentPoints));
else
g.FillPolygon(brush, segmentPoints);
}
private List<Segment> CreateDataSegments()
{
List<Segment> segments = new List<Segment>();
int i = 0;
int first = i;
int last = i;
bool negative = data[i] < 0;
i++;
while (i < data.Length)
{
if (negative != data[i] < 0)
{
segments.Add(new Segment(first, last, negative));
first = last = i;
negative = data[i] < 0;
}
else last = i;
i++;
}
segments.Add(new Segment(first, last, negative));
return segments;
}
private PointF InterpolateFieldBorder(List<Segment> segments, int segment)
{
if (segment == 0 || segment == segments.Count)
return ToScreenCoords(profile.Points[0]);
else
{
int before = segments[segment - 1].LastIndex;
int after = segments[segment].FirstIndex;
int index1 = profile.Probes[before].Index;
int index2 = profile.Probes[after].Index;
float sum = Math.Abs(data[before]) + Math.Abs(data[after]);
float ratio = Math.Abs(data[before]) / sum;
return ToScreenCoords(profile.InterpolateNewPoint(index1, index2, ratio));
}
}
}
}