-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
465 lines (362 loc) · 15.1 KB
/
Program.cs
File metadata and controls
465 lines (362 loc) · 15.1 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// makes a grid based on zoom, pos, res, and maxIter
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using Natsu.Graphics;
using Natsu.Graphics.Elements;
using Natsu.Input;
using Natsu.Mathematics;
using Natsu.Platforms.Desktop;
using Natsu.Platforms.Skia;
using SkiaSharp;
public class Vector2d {
public double X { get; set; }
public double Y { get; set; }
public Vector2d(double x, double y) {
X = x;
Y = y;
}
public static Vector2d operator +(Vector2d a, Vector2d b) => new(a.X + b.X, a.Y + b.Y);
public static Vector2d operator -(Vector2d a, Vector2d b) => new(a.X - b.X, a.Y - b.Y);
public static Vector2d operator *(Vector2d a, double b) => new(a.X * b, a.Y * b);
public static Vector2d operator /(Vector2d a, double b) => new(a.X / b, a.Y / b);
public static Vector2d operator *(Vector2d a, Vector2d b) => new(a.X * b.X, a.Y * b.Y);
public static Vector2d operator /(Vector2d a, Vector2d b) => new(a.X / b.X, a.Y / b.Y);
public static Vector2d operator +(Vector2d a, double b) => new(a.X + b, a.Y + b);
public static Vector2d operator -(Vector2d a, double b) => new(a.X - b, a.Y - b);
public static Vector2d operator -(Vector2d a) => new(-a.X, -a.Y);
public static bool operator ==(Vector2d a, Vector2d b) => a.X == b.X && a.Y == b.Y;
public static bool operator !=(Vector2d a, Vector2d b) => a.X != b.X || a.Y != b.Y;
public override bool Equals(object? obj) => obj is Vector2d vec && vec == this;
public override int GetHashCode() => X.GetHashCode() ^ Y.GetHashCode();
}
public class ValueRect {
public double Value { get; set; }
public Vector2d Position { get; set; }
public Vector2d Size { get; set; }
public void Render(ICanvas canvas) {
canvas.DrawRect(new((float)Position.X, (float)Position.Y, (float)Size.X, (float)Size.Y), new Paint() {
Color = new Color((float)Value * 255, (float)Value * 255, (float)Value * 255),
IsAntialias = false,
FilterQuality = FilterQuality.High,
});
// canvas.DrawRect(new((float)Position.X, (float)Position.Y, (float)Size.X, (float)Size.Y), new Paint() {
// Color = Colors.Red,
// IsAntialias = true,
// FilterQuality = FilterQuality.High,
// IsStroke = true,
// StrokeWidth = 0.1f
// });
}
public ValueRect(double value, Vector2d position, Vector2d size) {
Value = value;
Position = position;
Size = size;
}
}
public static class Mandelbrot {
public static double[,] Generate(Vector2d center, double zoom, Vector2d resolution, int maxIter, int divide) {
int width = (int)resolution.X;
int height = (int)resolution.Y;
double[,] grid = new double[width, height];
Parallel.For(0, divide, i => {
int startRow = height / divide * i;
int endRow = (i == divide - 1) ? height : startRow + height / divide;
ComputeSegment(grid, center, zoom, resolution, maxIter, startRow, endRow);
});
return grid;
}
private static void ComputeSegment(double[,] grid, Vector2d center, double zoom, Vector2d resolution, int maxIter, int startRow, int endRow) {
int width = (int)resolution.X;
for (int y = startRow; y < endRow; y++) {
double scaledY = (y - resolution.Y / 2) / zoom + center.Y;
for (int x = 0; x < width; x++) {
double scaledX = (x - resolution.X / 2) / zoom + center.X;
grid[x, y] = ComputePoint(scaledX, scaledY, maxIter);
}
}
}
private static double ComputePoint(double cx, double cy, int maxIter) {
double zx = 0, zy = 0, zx2 = 0, zy2 = 0;
for (int n = 0; n < maxIter; n++) {
zx2 = zx * zx;
zy2 = zy * zy;
if (zx2 + zy2 > 16) {
return (double)n / maxIter;
}
zy = 2 * zx * zy + cy;
zx = zx2 - zy2 + cx;
}
return 1.0;
}
public static List<ValueRect> GenerateRects(double[,] grid, Vector2 resolution, int divide, double epsilon) {
int width = (int)resolution.X;
int height = (int)resolution.Y;
List<ValueRect> rects = new();
for (int i = 0; i < divide; i++) {
int startRow = height / divide * i;
int endRow = (i == divide - 1) ? height : startRow + height / divide;
for (int y = startRow; y < endRow; y++) {
int x = 0;
while (x < width) {
double value = grid[x, y];
int startX = x;
while (x < width && Math.Abs(grid[x, y] - value) < epsilon) {
x++;
}
rects.Add(new ValueRect(value, new Vector2d(startX, y), new Vector2d(x - startX, 1)));
}
}
}
return rects;
}
}
public class MandelbrotDisplay : Element {
public Vector2d Center { get; set; } = new(0, 0);
public float Zoom { get; set; } = 100;
public int MaxIter { get; set; } = 100;
private readonly object _lock = new object();
private CancellationTokenSource? _renderCts;
private IOffscreenSurface? surface;
public bool NeedsRerender { get; private set; } = true;
public float LastComputeTime = 0.2f;
// Mark for rerender
public void Invalidate() {
lock (_lock) {
NeedsRerender = true;
_renderPoint++;
}
}
private void StartRendering() {
lock (_lock) {
_renderCts?.Cancel();
_renderCts = new CancellationTokenSource();
var token = _renderCts.Token;
Task.Run(() => Rerender(token), token);
}
}
public void Rerender(CancellationToken token) {
try {
if (App?.Renderer == null || token.IsCancellationRequested)
return;
Stopwatch sw = Stopwatch.StartNew();
var newGrid = Mandelbrot.Generate(Center, Zoom, new(DrawSize.X, DrawSize.Y), MaxIter, Environment.ProcessorCount);
var rects = Mandelbrot.GenerateRects(newGrid, DrawSize, Environment.ProcessorCount, 0.01f);
var nSurface = App.Renderer.CreateOffscreenSurface((int)DrawSize.X, (int)DrawSize.Y);
var canvas = nSurface.Canvas;
List<float> values = new();
int chunkSize = rects.Count / Environment.ProcessorCount;
List<List<ValueRect>> chunks = new();
List<IOffscreenSurface> surfaces = new();
for (int i = 0; i < rects.Count; i += chunkSize) {
chunks.Add(rects.GetRange(i, Math.Min(chunkSize, rects.Count - i)));
surfaces.Add(App.Renderer.CreateOffscreenSurface((int)DrawSize.X, (int)DrawSize.Y));
}
object _renderLock = new();
foreach (var rect in rects) {
if (token.IsCancellationRequested) throw new OperationCanceledException();
rect.Render(canvas);
values.Add((float)rect.Value);
}
nSurface.Flush();
lock (_lock) {
surface?.Dispose();
surface = nSurface;
NeedsRerender = false;
}
StopTransformSequences();
Scale = Vector2.One;
Position = Vector2.Zero;
LastComputeTime = (float)sw.ElapsedMilliseconds / 1000;
} catch (OperationCanceledException) {
Console.WriteLine("Task was canceled");
}
}
private int _renderPoint = 0, _currentPoint = 0;
protected override void OnRender(ICanvas canvas) {
base.OnRender(canvas);
lock (_lock) {
if (NeedsRerender && _renderPoint != _currentPoint) {
StartRendering();
_currentPoint = _renderPoint;
}
}
if (surface != null) {
canvas.DrawOffscreenSurface(surface, new(0, 0));
}
}
}
public class TestApp : Application {
public MandelbrotDisplay Display { get; }
public Element Wrapper { get; }
public TextElement LastComputeTimeText, FPSText, PrecisionText;
public TestApp() {
Display = new MandelbrotDisplay() {
RelativeSizeAxes = Axes.Both,
OffsetPosition = new(0.5f),
AnchorPosition = new(0.5f)
};
Wrapper = new() {
RelativeSizeAxes = Axes.Both,
ScaleAffectsDrawSize = false,
};
LastComputeTimeText = new TextElement() {
Text = "Last Compute Time: 0ms",
Position = new(10, 10),
Paint = new() {
Color = Colors.Red,
TextSize = 20,
}
};
FPSText = new TextElement() {
Text = "FPS: 0",
Position = new(10, 40),
Paint = new() {
Color = Colors.Red,
TextSize = 20,
}
};
PrecisionText = new TextElement() {
Text = "Precision: 0",
Position = new(10, 70),
Paint = new() {
Color = Colors.Red,
TextSize = 20,
}
};
Display.Invalidate();
Add(Wrapper, LastComputeTimeText, FPSText, PrecisionText);
Wrapper.Add(Display);
Add(new RectElement() {
RoundedCorners = new(5f),
Size = new(5f),
AnchorPosition = new(0.5f),
OffsetPosition = new(0.5f),
Paint = new() {
IsAntialias = true,
Color = Colors.Red,
},
Index = 999
});
}
protected override void OnKeyDown(Key key) {
base.OnKeyDown(key);
if (Display.NeedsRerender) return;
double moveAmount = 150 / Display.Zoom;
float scaleAmount = 2f;
Vector2d origPos = Display.Center;
Ease easeType = Ease.CubicOut;
switch (key) {
case Key.W:
Display.Center -= new Vector2d(0, moveAmount);
break;
case Key.S:
Display.Center += new Vector2d(0, moveAmount);
break;
case Key.A:
Display.Center -= new Vector2d(moveAmount, 0);
break;
case Key.D:
Display.Center += new Vector2d(moveAmount, 0);
break;
case Key.Up:
Display.Zoom *= scaleAmount;
Display.StopTransformSequences(nameof(Display.Scale));
Display.ScaleTo(new Vector2(scaleAmount), Display.LastComputeTime, easeType);
break;
case Key.Down:
Display.Zoom /= scaleAmount;
Display.StopTransformSequences(nameof(Display.Scale));
Display.ScaleTo(new Vector2(1 / scaleAmount), Display.LastComputeTime, easeType);
break;
case Key.E:
Display.MaxIter += 10;
break;
case Key.Q:
Display.MaxIter -= 10;
break;
case Key.R:
Wrapper.Scale /= 1.5f;
break;
case Key.T:
Wrapper.Scale *= 1.5f;
break;
case Key.X:
Display.Invalidate();
break;
default:
return;
}
if (origPos != Display.Center) {
Display.StopTransformSequences(nameof(Display.Position));
float posX = origPos.X == Display.Center.X ? 0 : (float)(origPos.X - Display.Center.X);
float posY = origPos.Y == Display.Center.Y ? 0 : (float)(origPos.Y - Display.Center.Y);
posX *= Display.Zoom;
posY *= Display.Zoom;
Display.MoveTo(new Vector2(posX, posY), Display.LastComputeTime, easeType);
}
Display.Invalidate();
}
protected override void OnResize(int width, int height) {
base.OnResize(width, height);
Display.Invalidate();
}
protected override void OnUpdate() {
base.OnUpdate();
FPSText.Text = $"FPS: {Math.Round(1 / UpdateTime.DeltaTime)}";
LastComputeTimeText.Text = $"Last Compute Time: {Math.Round(Display.LastComputeTime * 1000)}ms";
double epsilon = 1 / Display.Zoom;
double precision = -Math.Log10(epsilon);
double accuracy = 100 - (precision / 16 * 100);
double zoomPercent = Display.Zoom / 100;
PrecisionText.Text = $"Precision: {precision:0.00} ({accuracy:0.00}%), Zoom: {zoomPercent:0.00}, Scale: {Wrapper.Scale.X:0.00}, Max Iter: {Display.MaxIter}";
}
private Vector2d _startPos = new(0, 0);
protected override void OnMouseDown(MouseButton button, Vector2 position) {
base.OnMouseDown(button, position);
if (button != MouseButton.Left) return;
_startPos = new(position.X, position.Y);
}
protected override void OnMouseMove(Vector2 position) {
base.OnMouseMove(position);
if (!MouseState.TryGetValue(MouseButton.Left, out bool x) || !x) return;
Display.Center += new Vector2d(_startPos.X - position.X, _startPos.Y - position.Y) / (Display.Zoom * Wrapper.Scale.X);
Vector2d pos = new(position.X, position.Y);
Vector2d offset = (_startPos - pos) / Wrapper.Scale.X;
Display.Position -= new Vector2((float)offset.X, (float)offset.Y);
_startPos = pos;
}
protected override void OnMouseUp(MouseButton button, Vector2 position) {
base.OnMouseUp(button, position);
if (button == MouseButton.Right) {
Display.Center = new Vector2d(0, 0);
Display.Zoom = 100;
Display.MaxIter = 100;
Display.Invalidate();
}
if (button != MouseButton.Left) return;
Display.Invalidate();
}
protected override void OnMouseWheel(Vector2 delta) {
base.OnMouseWheel(delta);
if (Display.NeedsRerender) return;
float scaleAmount = 2f;
if (delta.Y > 0) {
Display.Zoom *= scaleAmount;
Display.StopTransformSequences(nameof(Display.Scale));
Display.ScaleTo(new Vector2(scaleAmount), Display.LastComputeTime, Ease.CubicOut);
} else {
Display.Zoom /= scaleAmount;
Display.StopTransformSequences(nameof(Display.Scale));
Display.ScaleTo(new Vector2(1 / scaleAmount), Display.LastComputeTime, Ease.CubicOut);
}
Display.Invalidate();
}
}
public static class Program {
public static void Main() {
using var app = new TestApp();
DesktopWindow window = new(app);
window.Run();
}
}