-
Notifications
You must be signed in to change notification settings - Fork 1
/
calcworker.js
1497 lines (1372 loc) · 59.7 KB
/
calcworker.js
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var useWorkers = true;
if (!self.Worker) {
useWorkers = false;
self.postMessage({subworkerNoWorky: true});
self.close();
}
// temporary polyfill since Chrome/Safari don't quite yet support this
if (!self.structuredClone) {
// thanks to https://stackoverflow.com/a/70315718/259456
BigInt.prototype.toJSON = function() {
return this.toString();
};
self.structuredClone = function(obj) {
return JSON.parse(JSON.stringify(obj));
};
}
const forceWorkerReloadUrlParam = "force-worker-reload=true";
const forceWorkerReload = self.location.toString().includes(forceWorkerReloadUrlParam);
const urlParams = new URLSearchParams(self.location.search);
const appVersion = urlParams.has("v") ? urlParams.get('v') : "unk";
if (forceWorkerReload) {
importScripts("infnum.js?v=" + appVersion + "&" + forceWorkerReloadUrlParam + "&t=" + (Date.now()));
importScripts("floatexp.js?v=" + appVersion + "&" + forceWorkerReloadUrlParam + "&t=" + (Date.now()));
importScripts("mathiface.js?v=" + appVersion + "&" + forceWorkerReloadUrlParam + "&t=" + (Date.now()));
importScripts("plots.js?v=" + appVersion + "&" + forceWorkerReloadUrlParam + "&t=" + (Date.now()));
} else {
importScripts("infnum.js?v=" + appVersion);
importScripts("floatexp.js?v=" + appVersion);
importScripts("mathiface.js?v=" + appVersion);
importScripts("plots.js?v=" + appVersion);
}
const plotsByName = {};
for (let i = 0; i < plots.length; i++) {
plotsByName[plots[i].name] = plots[i];
}
const allCachedIndicesArray = [-1];
const startPassNumber = 0;
// create subworkers
// for each pass:
// - calculate chunks
// - give a chunk to each subworker
// - when subworker completes chunk:
// - pass it up to main thread
// - give that subworker another chunk
// - when pass is complete, repeat if there's another pass
const windowCalc = {
"timeout": null,
"plot": null,
"pointCalcFunction": null,
"eachPixUnits": null,
"edges": null,
"edgesM": null,
"n": null,
"precision": null,
"algorithm": null,
"math": null,
"passNumber": null,
"lineWidth": null,
"finalWidth": null,
"chunksComplete": null,
"canvasWidth": null,
"canvasHeight": null,
"xPixelChunks": null,
"pointsCache": null,
"pointsCacheAlgorithm": null,
"cacheScannedChunks": null,
"cacheScannedChunksCursor": null,
"passTotalPoints": null,
"passCachedPoints": null,
"totalChunks": null,
"workersCount": null,
"workers": null,
"minWorkersCount": null,
"maxWorkersCount": null,
"plotId": null,
"stopped": true,
"referencePx": null,
"referencePy": null,
"referencePeriod": null,
"referenceOrbit": null,
"referenceOrbitPrecision": null,
"referenceOrbitN": null,
"referenceOrbitSmooth": null,
"referenceBlaTables": null,
"referenceBlaN": null,
"referenceBottomLeftDeltaX": null,
"referenceBottomLeftDeltaY": null,
"saCoefficients": null,
"saCoefficientsN": null,
"saCoefficientsEdges": null,
"saCoefficientsParams": null,
"passBlaPixels": null,
"passBlaIterationsSkipped": null,
"passBlaSkips": null,
"totalBlaPixels": null,
"totalBlaIterationsSkipped": null,
"totalBlaSkips": null,
"referenceBlaEpsilon": null,
"setupStage": null,
"setupStageState": null,
"setupStageIsStarted": null,
"setupStageIsFinished": null,
"caching" : null,
"smooth": null
};
// long-running setup tasks are split in chunks, and we use the state
// to, after a setTimeout() callback, resume the current task or
// move to and begin the next task
const setupStages = {
checkRefOrbit: 0,
calcRefOrbit: 1,
checkBlaCoeff: 2,
calcBlaCoeff: 3,
checkSaCoeff: 4,
calcSaCoeff: 5,
done: 6
};
self.onmessage = function(e) {
if (!useWorkers) {
self.postMessage({subworkerNoWorky: true});
return;
}
console.log("got message [" + e.data.t + "]");
if (e.data.t == "worker-calc") {
runCalc(e.data.v);
} else if (e.data.t == "chunk-ordering") {
windowCalc.chunkOrdering = e.data.v;
updateRunningChunksOrdering();
} else if (e.data.t == "workers-count") {
updateWorkerCount(e.data.v);
} else if (e.data.t == "wipe-cache") {
windowCalc.pointsCache = new Map();
} else if (e.data.t == "wipe-ref-orbit") {
wipeReferenceOrbitStuff();
} else if (e.data.t == "stop") {
windowCalc.stopped = true;
stopAndRemoveAllWorkers();
}
};
// for now, the main thread will not pass a "worker-calc" message to this
// worker once a calculation is already running
function runCalc(msg) {
windowCalc.plotId = msg.plotId;
windowCalc.plot = msg.plot;
windowCalc.stopped = false;
windowCalc.eachPixUnits = msg.eachPixUnits;
windowCalc.eachPixUnitsM = msg.eachPixUnitsM;
// edges in InfNum objects
windowCalc.edges = {
left: msg.leftEdge,
right: msg.rightEdge,
top: msg.topEdge,
bottom: msg.bottomEdge
};
// edges for algorithm's math inteface
windowCalc.edgesM = {
left: msg.leftEdgeM,
right: msg.rightEdgeM,
top: msg.topEdgeM,
bottom: msg.bottomEdgeM
};
windowCalc.n = msg.n;
windowCalc.precision = msg.precision;
windowCalc.algorithm = msg.algorithm;
windowCalc.math = selectMathInterfaceFromAlgorithm(windowCalc.algorithm);
windowCalc.passNumber = startPassNumber - 1;
// the main thread does its own 64-wide pixels synchronously,
// so the worker threads should start at 32-wide (set to 64
// here so that after dividing by two initially we are at 32)
windowCalc.lineWidth = msg.startWidth * 2;
windowCalc.finalWidth = msg.finalWidth;
windowCalc.chunksComplete = 0;
windowCalc.canvasWidth = msg.canvasWidth;
windowCalc.canvasHeight = msg.canvasHeight;
windowCalc.chunkOrdering = msg.chunkOrdering;
windowCalc.smooth = msg.smooth;
// since pixel position math is now dependent on algorithm,
// the old caching no longer works as-is, so we will just
// turn it off for now
windowCalc.caching = false;
// // include "nocache" in algorithm name to turn off caching
// windowCalc.caching = !windowCalc.algorithm.includes("nocache");
if (windowCalc.pointsCache === null || !windowCalc.caching ||
(windowCalc.pointsCacheAlgorithm !== null &&
windowCalc.pointsCacheAlgorithm != windowCalc.algorithm)) {
windowCalc.pointsCache = new Map();
}
windowCalc.pointsCacheAlgorithm = windowCalc.algorithm;
windowCalc.totalChunks = null;
windowCalc.workersCount = msg.workers;
windowCalc.workers = [];
windowCalc.minWorkersCount = windowCalc.workersCount;
windowCalc.maxWorkersCount = windowCalc.workersCount;
for (let i = 0; i < windowCalc.workersCount; i++) {
if (forceWorkerReload) {
windowCalc.workers.push(new Worker("calcsubworker.js?v=" + appVersion + "&" + forceWorkerReloadUrlParam + "&t=" + (Date.now())));
} else {
windowCalc.workers.push(new Worker("calcsubworker.js?v=" + appVersion));
}
windowCalc.workers[i].onmessage = onSubWorkerMessage;
}
if (windowCalc.algorithm.includes("basic-")) {
// do these really need to be cleared? what if the user stays at the same
// center+scale and toggles the algorithm a few times? shouldn't these
// computation-heavy items just sit untouched if not actually required to change?
windowCalc.referencePx = null;
windowCalc.referencePy = null;
windowCalc.referenceOrbit = null;
windowCalc.referenceBlaTables = null;
windowCalc.saCoefficients = null;
// TODO make this a separate thing that can be run with a controls
// menu button or something (for non-"basic" algorithms, it will
// always be run)
// try ball arithmetic here for basic, too
// start with middle of window for reference point (doesn't have to
// exactly align with a pixel)
// const newReferencePx = infNumAdd(windowCalc.edges.left, infNumMul(windowCalc.eachPixUnits, infNum(BigInt(Math.floor(windowCalc.canvasWidth/2)), 0n)));
// const newReferencePy = infNumAdd(windowCalc.edges.bottom, infNumMul(windowCalc.eachPixUnits, infNum(BigInt(Math.floor(windowCalc.canvasHeight/2)), 0n)));
// const rectHalfX = infNumMul(windowCalc.eachPixUnits, infNum(BigInt(Math.floor(windowCalc.canvasWidth / 2)), 0n));
// const rectHalfY = infNumMul(windowCalc.eachPixUnits, infNum(BigInt(Math.floor(windowCalc.canvasHeight / 2)), 0n));
// const period = plotsByName[windowCalc.plot].findPeriodBallArithmetic2ndOrder(windowCalc.n, windowCalc.precision, windowCalc.algorithm, newReferencePx, newReferencePy, rectHalfX, rectHalfY, false);
// if (period > 0) {
// const getNthIterationAndDerivative = plotsByName[windowCalc.plot].getNthIterationAndDerivative;
// const foundMinibrotNucleus = plotsByName[windowCalc.plot].newtonsMethod(period, newReferencePx, newReferencePy, windowCalc.precision, getNthIterationAndDerivative);
// if (
// infNumGt(foundMinibrotNucleus.x, windowCalc.edges.right) ||
// infNumLt(foundMinibrotNucleus.x, windowCalc.edges.left) ||
// infNumGt(foundMinibrotNucleus.y, windowCalc.edges.top) ||
// infNumLt(foundMinibrotNucleus.y, windowCalc.edges.bottom)) {
// console.log("newton nucleus is off screen!");
// windowCalc.referencePeriod = -1;
// } else {
// windowCalc.referencePeriod = period;
// windowCalc.referencePx = foundMinibrotNucleus.x;
// windowCalc.referencePy = foundMinibrotNucleus.y;
// console.log("found ref x/y/period!");
// setMinibrotNucleusMessage({
// x: foundMinibrotNucleus.x,
// y: foundMinibrotNucleus.y,
// period: period
// });
// }
// }
// since the basic algorithm has no setup tasks, we just start here
calculatePass();
// if we are using perturbation theory, we'll now calculate the
// reference point and its full orbit (which will be used for
// all chunks in all passes)
} else {
if (windowCalc.timeout != null) {
clearTimeout(windowCalc.timeout);
}
windowCalc.timeout = setTimeout(kickoffSetupTasks, 250);
}
}
function wipeReferenceOrbitStuff() {
windowCalc.referenceOrbit = null;
// since SA and BLA computed coefficients/terms are dependent on
// the ref orbit, wipe those when we calculate a new ref orbit
windowCalc.saCoefficients = null;
windowCalc.referenceBlaTables = null;
}
function setupCheckReferenceOrbit() {
sendStatusMessage("Finding reference point");
// start with middle of window for reference point (doesn't have to
// exactly align with a pixel)
let newReferencePx = infNumAdd(windowCalc.edges.left, infNumMul(windowCalc.eachPixUnits, infNum(BigInt(Math.floor(windowCalc.canvasWidth/2)), 0n)));
let newReferencePy = infNumAdd(windowCalc.edges.bottom, infNumMul(windowCalc.eachPixUnits, infNum(BigInt(Math.floor(windowCalc.canvasHeight/2)), 0n)));
let refPointHasMoved = false;
if (windowCalc.referencePx === null || windowCalc.referencePy === null) {
refPointHasMoved = true;
} else {
// check difference between this and previous x position
let xDiff = infNumSub(windowCalc.referencePx, newReferencePx);
let yDiff = infNumSub(windowCalc.referencePy, newReferencePy);
let squaredDiff = infNumAdd(infNumMul(xDiff, xDiff), infNumMul(yDiff, yDiff));
// if using BLA, and the ref point is a periodic, as long as it's still
// within the middle 98% of the image (by radius) then it can be re-used
// otherwise, we'll re-use it if it's in the middle 30% of the
// image
let maxAllowablePixelsMove = (windowCalc.referencePeriod > 0 && windowCalc.algorithm.includes("bla-")) ?
Math.ceil(Math.min(windowCalc.canvasHeight, windowCalc.canvasWidth) * 0.49)
:
Math.ceil(Math.min(windowCalc.canvasHeight, windowCalc.canvasWidth) * 0.15);
let maxAllowableMove = infNumMul(windowCalc.eachPixUnits, infNum(BigInt(maxAllowablePixelsMove), 0n));
// square this as well
maxAllowableMove = infNumMul(maxAllowableMove, maxAllowableMove);
if (infNumGt(squaredDiff, maxAllowableMove)) {
refPointHasMoved = true;
console.log("the previous ref orbit is NOT within [" + maxAllowablePixelsMove + "] pixels of the center, so we need a new ref orbit");
} else {
console.log("the previous ref orbit is within [" + maxAllowablePixelsMove + "] pixels of the center, so it's still valid");
if (windowCalc.referencePeriod > 0 && windowCalc.algorithm.includes("bla-")) {
setMinibrotNucleusMessage({
x: windowCalc.referencePx,
y: windowCalc.referencePy,
period: windowCalc.referencePeriod
});
}
}
}
if (windowCalc.referenceOrbitN === null || windowCalc.referenceOrbitN < windowCalc.n ||
windowCalc.referenceOrbitPrecision === null || windowCalc.referenceOrbitPrecision / windowCalc.precision < 0.98 ||
windowCalc.referenceOrbitSmooth !== windowCalc.smooth ||
windowCalc.referenceOrbit === null || refPointHasMoved) {
windowCalc.referencePx = newReferencePx;
windowCalc.referencePy = newReferencePy;
// wipe this to signal to next stage that the reference orbit
// needs to be calculated
wipeReferenceOrbitStuff();
} else {
console.log("re-using previously-calculated reference orbit, with [" + windowCalc.referenceOrbit.length + "] iterations, for point:");
console.log("referencePx: " + infNumToString(windowCalc.referencePx));
console.log("referencePy: " + infNumToString(windowCalc.referencePy));
// no need to calculate a new reference orbit
return false;
}
// we need to calculate a new reference orbit
return true;
}
function setupReferenceOrbit(state) {
if (state === null || !state.done) {
if (state === null) {
state = {
minibrotFindingState: null,
computeRefOrbitState: null,
status: "",
done: false
};
}
// temporary, try to find period only when "bla-" is used
const findPeriod = windowCalc.algorithm.includes("bla-");
if (!findPeriod) {
windowCalc.referencePeriod = -1;
} else {
if (state.minibrotFindingState === null) {
// ball arithmetic method
state.rectHalfX = infNumMul(windowCalc.eachPixUnits, infNum(BigInt(Math.floor(windowCalc.canvasWidth / 2)), 0n));
state.rectHalfY = infNumMul(windowCalc.eachPixUnits, infNum(BigInt(Math.floor(windowCalc.canvasHeight / 2)), 0n));
}
const getNthIterationAndDerivative = plotsByName[windowCalc.plot].getNthIterationAndDerivative;
const newtonsMethod = plotsByName[windowCalc.plot].newtonsMethod;
if (state.minibrotFindingState === null || !state.minibrotFindingState.done) {
state.minibrotFindingState = plotsByName[windowCalc.plot].findMinibrotWithBallArithmetic1stOrderAndNewton(windowCalc.n, windowCalc.precision, windowCalc.algorithm, windowCalc.referencePx, windowCalc.referencePy, state.rectHalfX, state.rectHalfY, getNthIterationAndDerivative, newtonsMethod, state.minibrotFindingState);
sendStatusMessage(state.minibrotFindingState.status);
if (state.minibrotFindingState.done) {
const foundMinibrotNucleus = state.minibrotFindingState.nucleus;
if (foundMinibrotNucleus === null) {
console.log("no found newton nucleus is within the window");
windowCalc.referencePeriod = -1;
} else {
windowCalc.referencePeriod = foundMinibrotNucleus.period;
windowCalc.referencePx = foundMinibrotNucleus.x;
windowCalc.referencePy = foundMinibrotNucleus.y;
console.log("found ref x/y/period!");
setMinibrotNucleusMessage({
x: foundMinibrotNucleus.x,
y: foundMinibrotNucleus.y,
period: foundMinibrotNucleus.period
});
}
}
return state;
}
}
if (state.computeRefOrbitState === null || !state.computeRefOrbitState.done) {
state.computeRefOrbitState = plotsByName[windowCalc.plot].computeReferenceOrbit(windowCalc.n, windowCalc.precision, windowCalc.algorithm, windowCalc.referencePx, windowCalc.referencePy, windowCalc.referencePeriod, windowCalc.smooth, state.computeRefOrbitState);
sendStatusMessage(state.computeRefOrbitState.status);
if (state.computeRefOrbitState.done) {
state.done = true;
} else {
return state;
}
}
}
if (state.done) {
windowCalc.referenceOrbit = state.computeRefOrbitState.orbit;
windowCalc.referenceOrbitN = windowCalc.n;
windowCalc.referenceOrbitPrecision = windowCalc.precision;
windowCalc.referenceOrbitSmooth = windowCalc.smooth;
console.log("calculated new " + (windowCalc.referencePeriod === -1 ? "middle" : "periodic") + " reference orbit, with [" + windowCalc.referenceOrbit.length + "] iterations, for point:");
console.log("referencePx: " + infNumToString(windowCalc.referencePx));
console.log("referencePy: " + infNumToString(windowCalc.referencePy));
}
return state;
}
function setupCheckBlaCoefficients() {
// if we are using bivariate linear approximation, and we haven't already
// calculated them based on the ref orbit, calculate the coefficients
if (windowCalc.algorithm.includes("bla-")) {
windowCalc.totalBlaPixels = 0;
windowCalc.totalBlaIterationsSkipped = 0;
windowCalc.totalBlaSkips = 0;
const algoEpsilon = getBLAEpsilonFromAlgorithm(windowCalc.algorithm);
if (windowCalc.referenceBlaTables === null ||
// not sure how changing N (max iterations) affects BLA coefficients,
// so just require a full re-compute for now if it has changed
windowCalc.n !== windowCalc.referenceBlaN ||
windowCalc.referenceBlaWindowEdges === null ||
// if any window edge changed, we need to re-compute BLAs
!infNumEq(windowCalc.edges.top, windowCalc.referenceBlaWindowEdges.top) ||
!infNumEq(windowCalc.edges.bottom, windowCalc.referenceBlaWindowEdges.bottom) ||
!infNumEq(windowCalc.edges.left, windowCalc.referenceBlaWindowEdges.left) ||
!infNumEq(windowCalc.edges.right, windowCalc.referenceBlaWindowEdges.right) ||
(algoEpsilon !== null && !infNumEq(algoEpsilon, windowCalc.referenceBlaEpsilon))) {
return true;
} else {
console.log("re-using previously-calculated BLA coefficient tables");
}
}
// no need to calculate BLA coefficients
return false;
}
function findFathestCornerFromPoint(infNumComplexPt, edges) {
let testPoint = {x:edges.left, y:edges.top};
let dist = infNumMath.complexAbs(infNumMath.complexSub(infNumComplexPt, testPoint));
let farthestPoint = structuredClone(testPoint);
let farthestDist = structuredClone(dist);
testPoint = {x:edges.left, y:edges.bottom};
dist = infNumMath.complexAbs(infNumMath.complexSub(infNumComplexPt, testPoint));
if (infNumGt(dist, farthestDist)) {
farthestPoint = structuredClone(testPoint);
farthestDist = structuredClone(dist);
}
testPoint = {x:edges.right, y:edges.top};
dist = infNumMath.complexAbs(infNumMath.complexSub(infNumComplexPt, testPoint));
if (infNumGt(dist, farthestDist)) {
farthestPoint = structuredClone(testPoint);
farthestDist = structuredClone(dist);
}
testPoint = {x:edges.right, y:edges.bottom};
dist = infNumMath.complexAbs(infNumMath.complexSub(infNumComplexPt, testPoint));
if (infNumGt(dist, farthestDist)) {
farthestPoint = structuredClone(testPoint);
}
return farthestPoint;
}
// put 8 test points along the line from the corner to 33% along the line
// put 16 test points along the line from the 33% point to 66% point
// put 32 test points along the line from the 66% point to the ref point
function getTestPointsInOrderFromAToB(infNumComplexA, infNumComplexB, windowCalcMath, precis) {
const pointsDifference = infNumMath.complexSub(infNumComplexB, infNumComplexA);
const testLineLength = infNumMath.complexAbs(pointsDifference);
const testLinePixels = infNumDiv(testLineLength, windowCalc.eachPixUnits, precis);
const firstThirdPointsFloat = floatMath.createFromInfNum(infNumDiv(testLinePixels, infNum(64n, 0n), 10));
const firstThirdPoints = BigInt(Math.round(firstThirdPointsFloat));
console.log("test line covers [" + infNumToString(infNumTruncateToLen(testLinePixels, 15)) + "] pixels, so dividing the far third of that line into [" + firstThirdPoints + "] test points");
// each third of the line needs to be subdivided into
// 4x firstThirdPoints
const stepDiv = firstThirdPoints * 4n * 3n;
const firstThirdSteps = firstThirdPoints * 4n;
const secondThirdSteps = firstThirdSteps * 2n;
const step = infNumMath.complexRealDiv(pointsDifference, infNum(stepDiv, 0n), precis);
let points = [];
let testPointComplex;
let testPointDelta;
for (let i = 0n; i < stepDiv; i += 1n) {
if (
(i < firstThirdSteps && i % 4n === 0n) ||
(i >= firstThirdSteps && i < secondThirdSteps && i % 2n === 0n) ||
(i >= secondThirdSteps)) {
testPointComplex = infNumMath.complexAdd(infNumComplexA, infNumMath.complexRealMul(step, infNum(i, 0n)));
// the delta is the difference from the reference point (point B)
// to the test point
testPointDelta = infNumMath.complexSub(testPointComplex, infNumComplexB);
points.push({
dx: windowCalcMath.createFromInfNum(testPointDelta.x),
dy: windowCalcMath.createFromInfNum(testPointDelta.y),
complex: testPointComplex
});
}
}
return points;
}
// somehow, the "epsilon" value with our BLA implementation here doesn't
// seem rigidly tied to the underlying math datatype. for example,
// floating-point math should in theory necessetate a particular value
// for epsilon, as that's the entire idea behind BLA in the first place:
// when the squared term is smaller than the smallest value that can be
// represented by the math datatype, it can be ignored thus leading to
// a linear approximation.
//
// for individual images, when the value used for epsilon is made a little
// smaller, the image looks the same but skips fewer iterations with the
// BLA and thus takes longer to render. or, if the epsilon is made a
// little larger, the image looks the same but renders faster. make the
// epsilon too big, however, and strange inaccurate results are rendered.
//
// this function picks the best valid epsilon for the location being
// rendered. it does this by comparing, at several points, the iteration
// count for an orbit calculated fully with perturbation-theory vs the
// iteration count calculated with BLA. if there's inaccuracy, the epsilon
// is considered "bad" and BLAs with a different epsilon are calculated,
// and all the test points are checked again. if an epsilon results in
// an accurate rendering, it's considered "better" than the previous "best"
// epsilon if it skips more iterations.
//
// a binary-ish search is used to find the best epsilon. since computing and
// testing BLAs is fast, this approach is reasonably fast.
function setupBlaCoefficients(state) {
if (state === null || !state.done) {
if (state === null) {
windowCalc.referenceBlaTables = null;
sendStatusMessage("Calculating BLA coefficient tables");
}
// if the window's algorithm string specifies a BLA epsilon, use
// that instead of auto-finding the epsilon
const algoSpecifiedEpsilon = getBLAEpsilonFromAlgorithm(windowCalc.algorithm);
if (algoSpecifiedEpsilon !== null) {
const algoSpecifiedEpsilonStr = infNumExpStringTruncToLen(algoSpecifiedEpsilon, 2);
while (state === null || !state.done) {
state = plotsByName[windowCalc.plot].computeBlaTables(windowCalc.algorithm, null, windowCalc.referenceOrbit, windowCalc.referencePx, windowCalc.referencePy, windowCalc.edges, state);
sendStatusMessage("for ε=" + algoSpecifiedEpsilonStr + ": " + state.status);
}
if (state.done) {
windowCalc.referenceBlaN = windowCalc.n;
windowCalc.referenceBlaWindowEdges = structuredClone(windowCalc.edges);
windowCalc.referenceBlaTables = state.blas;
windowCalc.referenceBlaEpsilon = state.infNumEpsilon;
}
return state;
}
// find the line from the ref point to the farthest image corner
const refPoint = {x:windowCalc.referencePx, y:windowCalc.referencePy};
const farthestCorner = findFathestCornerFromPoint(refPoint, windowCalc.edges);
//console.log("farthest corner is [(" + infNumToString(farthestCorner.x) + "," + infNumToString(farthestCorner.y) + ")]");
// calculate test points along that line
const testPoints = getTestPointsInOrderFromAToB(farthestCorner, refPoint, windowCalc.math, windowCalc.precision);
console.log("testing [" + testPoints.length + "] points to find the best BLA epsilon");
// set the test points as the debug points to be drawn, toggled with R key
//sendDebugPointsMessage({
// points: testPoints.map(x => x.complex)
//});
// perturb only algorithm
const perturbAlgo = "perturb-" + windowCalc.math.name;
const nullBlaTables = null; // for perturb only, pass null blaTables
const nullSaCoefficients = null; // for perturb and BLA, pass null saCoefficients
// perturb results need to only be calculated once for each
// test point, so save them in this array
const testPointsPerturb = [];
// calculate starting BLAs, using starting epsilon
let epsilon = windowCalc.math.name === "float" ? infNum(1n, -54n) : infNum(1n, -129n);
let epsilonStr = infNumExpStringTruncToLen(epsilon, 2);
let totalTestPointBLAIterSkips = 0;
// the "best" epsilon is the largest value where all test points are accurate
let bestEpsilon = null;
let bestTotalTestPointBLAIterSkips = 0;
// the "smallest bad" is the smallest epsilon where one or more test points are not accurate
let smallestBadEpsilon = null;
// start at farthest test point from the ref point
let testPointsCursor = 0;
const totalTestPoints = testPoints.length;
let dx, dy, blaResult;
// TODO this loop doesn't return the "state" object yet, so this
// loop (which may be slow) is not cancel-able
while (testPointsCursor < totalTestPoints) {
if (state === null) {
while (state === null || !state.done) {
state = plotsByName[windowCalc.plot].computeBlaTables(windowCalc.algorithm, epsilon, windowCalc.referenceOrbit, windowCalc.referencePx, windowCalc.referencePy, windowCalc.edges, state);
sendStatusMessage("For ε=" + epsilonStr + ": " + state.status);
}
}
// do both perturb and BLA for the point until it escapes or hits n
// results look like: {colorpct: iter, blaItersSkipped: blaItersSkipped, blaSkips: blaSkips};
if (testPointsPerturb[testPointsCursor] === undefined) {
testPointsPerturb[testPointsCursor] =
plotsByName[windowCalc.plot].computeBoundPointColorPerturbOrBla(windowCalc.n, windowCalc.precision, testPoints[testPointsCursor].dx, testPoints[testPointsCursor].dy, perturbAlgo, windowCalc.referencePx, windowCalc.referencePy, windowCalc.referenceOrbit, nullBlaTables, nullSaCoefficients, windowCalc.smooth);
}
blaResult = plotsByName[windowCalc.plot].computeBoundPointColorPerturbOrBla(windowCalc.n, windowCalc.precision, testPoints[testPointsCursor].dx, testPoints[testPointsCursor].dy, windowCalc.algorithm, windowCalc.referencePx, windowCalc.referencePy, windowCalc.referenceOrbit, state.blas, nullSaCoefficients, windowCalc.smooth);
totalTestPointBLAIterSkips += blaResult.blaItersSkipped;
sendStatusMessage("Tested " + Math.round(testPointsCursor * 100.0 / totalTestPoints) + "% of test pts for ε=" + epsilonStr);
// if the BLA is accurate, proceed to the test point next closest to the ref point
// compare as percentage of n, where 1e-2 is 1%, 1e-3 is 0.1%, etc
if ((Math.abs(testPointsPerturb[testPointsCursor].colorpct - blaResult.colorpct)/testPointsPerturb[testPointsCursor].colorpct) < 1e-5) {
testPointsCursor++;
if (testPointsCursor === totalTestPoints) {
// if all test points were accurate, and this is the best
// epsilon, save it to make the upper bound epsilon larger
if (bestEpsilon === null ||
(infNumGt(epsilon, bestEpsilon) && totalTestPointBLAIterSkips > bestTotalTestPointBLAIterSkips)) {
bestEpsilon = epsilon;
bestTotalTestPointBLAIterSkips = totalTestPointBLAIterSkips;
// if all test points were accurate, but this is not the
// best epsilon, save it to make the lower bound epsilon smaller
} else if (smallestBadEpsilon === null ||
(infNumLt(epsilon, smallestBadEpsilon))) {
smallestBadEpsilon = epsilon;
}
// pick a new epsilon below
state = null;
}
// if the BLA is not accurate, decrease epsilon and use BLA upon all the already-tried points again
} else {
if (smallestBadEpsilon === null || infNumLt(epsilon, smallestBadEpsilon)) {
smallestBadEpsilon = epsilon;
}
// pick a new epsilon below
state = null;
}
// if state is null here, that's our sigal from the lines above
// that a new epsilon (and thus BLAs) need to be calculated
if (state === null) {
// halve our epsilon's exponent if no bad epsilon has been
// found (which makes it much larger)
if (smallestBadEpsilon === null) {
epsilon = infNum(epsilon.v, epsilon.e / 2n);
// if bestEpsilon is null, double the power of the epsilon
// we tried (which makes it much smaller)
} else if (bestEpsilon === null) {
epsilon = infNum(epsilon.v, epsilon.e * 2n);
// try the middle value between smallestBadEpsilon and bestEpsilon
} else {
let exponentDiff = smallestBadEpsilon.e - bestEpsilon.e;
// if the difference between the exponents of smallestBadEpsilon and bestEpsilon is less than 2, we are done
if (exponentDiff < 2n) {
// compute final BLAs based on the best epsilon, where
// we go two powers of ten smaller, to increase
// render accuracy
epsilon = infNum(bestEpsilon.v, bestEpsilon.e - 2n);
console.log("final bestEpsilon found to be [" + infNumExpStringTruncToLen(bestEpsilon, 2) + "]");
console.log("using adjusted epsilon [" + infNumExpStringTruncToLen(epsilon, 2) + "] to render the image");
while (state === null || !state.done) {
state = plotsByName[windowCalc.plot].computeBlaTables(windowCalc.algorithm, epsilon, windowCalc.referenceOrbit, windowCalc.referencePx, windowCalc.referencePy, windowCalc.edges, state);
sendStatusMessage("For ε=" + epsilonStr + ": " + state.status);
}
break;
} else {
epsilon = infNum(smallestBadEpsilon.v, smallestBadEpsilon.e - (exponentDiff >> 1n));
}
}
epsilonStr = infNumExpStringTruncToLen(epsilon, 2);
totalTestPointBLAIterSkips = 0;
testPointsCursor = 0;
console.log("trying new epsilon [" + epsilonStr + "]");
sendStatusMessage("Trying new ε of " + epsilonStr);
}
// if at any point, BLA skips zero iters for all test points, stop decreasing BLA?
// (and note in console that SA should be used for this location)
}
}
if (state.done) {
windowCalc.referenceBlaN = windowCalc.n;
windowCalc.referenceBlaWindowEdges = structuredClone(windowCalc.edges);
windowCalc.referenceBlaTables = state.blas;
windowCalc.referenceBlaEpsilon = state.infNumEpsilon;
}
return state;
}
function setupCheckSaCoefficients() {
if (windowCalc.algorithm.includes("sapx")) {
let sapxParams = windowCalc.algorithm.split("-").find(e => e.startsWith("sapx"));
// regardless of whether we re-use the reference orbit, we have to re-calculate
// series approximation coefficients if any window edge has moved (it's probably
// true that if the edges have only slightly moved, the test points in the
// window would only be slightly different, and may still be valid, but that
// would require some testing)
if (windowCalc.saCoefficients === null || windowCalc.saCoefficientsEdges === null ||
// not sure how changing N (max iterations) affects SA coefficients,
// so just require a full re-compute for now if it has changed
windowCalc.n !== windowCalc.saCoefficientsN ||
windowCalc.saCoefficientsParams === null || windowCalc.saCoefficientsParams != sapxParams ||
!infNumEq(windowCalc.edges.left, windowCalc.saCoefficientsEdges.left) ||
!infNumEq(windowCalc.edges.right, windowCalc.saCoefficientsEdges.right) ||
!infNumEq(windowCalc.edges.top, windowCalc.saCoefficientsEdges.top) ||
!infNumEq(windowCalc.edges.bottom, windowCalc.saCoefficientsEdges.bottom)) {
return true;
} else {
console.log("re-using previously-calculated SA coefficients");
}
} else {
// no need to wipe these... in the future, if SA can be easily toggled on/off by
// the user, we'd want to re-use these if the window hasn't moved since when
// these were calculated
//windowCalc.saCoefficients = null;
}
// no need to calculate SA coefficients
return false;
}
function setupSaCoefficients(state) {
if (state === null || !state.done) {
if (state === null) {
windowCalc.saCoefficients = null;
sendStatusMessage("Calculating and testing SA coefficients");
}
state = plotsByName[windowCalc.plot].computeSaCoefficients(windowCalc.precision, windowCalc.algorithm, windowCalc.referencePx, windowCalc.referencePy, windowCalc.referenceOrbit, windowCalc.edges, state);
sendStatusMessage(state.status);
}
if (state.done) {
windowCalc.saCoefficientsN = windowCalc.n;
windowCalc.saCoefficientsEdges = structuredClone(windowCalc.edges);
windowCalc.saCoefficients = state.saCoefficients;
windowCalc.saCoefficientsParams = windowCalc.algorithm.split("-").find(e => e.startsWith("sapx"));
}
return state;
}
function kickoffSetupTasks() {
if (windowCalc.timeout != null) {
clearTimeout(windowCalc.timeout);
}
windowCalc.setupStage = 0;
windowCalc.timeout = setInterval(runSetupTasks, 5);
}
function runSetupTasks() {
if (windowCalc.stopped || windowCalc.setupStage >= setupStages.done) {
if (windowCalc.timeout != null) {
clearTimeout(windowCalc.timeout);
}
if (!windowCalc.stopped) {
setupPixelPositionDelta();
calculatePass();
}
return;
// =============================================================
} else if (windowCalc.setupStage === setupStages.checkRefOrbit) {
if (!setupCheckReferenceOrbit()) {
// increment an extra time here to skip the next stage, since
// we don't need to run it
windowCalc.setupStage++;
}
windowCalc.setupStageIsFinished = true;
// =============================================================
} else if (windowCalc.setupStage === setupStages.calcRefOrbit) {
if (!windowCalc.setupStageIsStarted) {
windowCalc.setupStageState = null;
windowCalc.setupStageIsStarted = true;
}
if (!windowCalc.setupStageIsFinished) {
windowCalc.setupStageState = setupReferenceOrbit(windowCalc.setupStageState);
}
if (windowCalc.setupStageState.done) {
windowCalc.setupStageIsFinished = true;
}
// ==============================================================
} else if (windowCalc.setupStage === setupStages.checkBlaCoeff) {
if (!setupCheckBlaCoefficients()) {
// increment an extra time here to skip the next stage, since
// we don't need to run it
windowCalc.setupStage++;
}
windowCalc.setupStageIsFinished = true;
// =============================================================
} else if (windowCalc.setupStage === setupStages.calcBlaCoeff) {
if (!windowCalc.setupStageIsStarted) {
windowCalc.setupStageState = null;
windowCalc.setupStageIsStarted = true;
}
if (!windowCalc.setupStageIsFinished) {
windowCalc.setupStageState = setupBlaCoefficients(windowCalc.setupStageState);
}
if (windowCalc.setupStageState.done) {
windowCalc.setupStageIsFinished = true;
}
// =============================================================
} else if (windowCalc.setupStage === setupStages.checkSaCoeff) {
if (!setupCheckSaCoefficients()) {
// increment an extra time here to skip the next stage, since
// we don't need to run it
windowCalc.setupStage++;
}
windowCalc.setupStageIsFinished = true;
// ============================================================
} else if (windowCalc.setupStage === setupStages.calcSaCoeff) {
if (!windowCalc.setupStageIsStarted) {
windowCalc.setupStageState = null;
windowCalc.setupStageIsStarted = true;
}
if (!windowCalc.setupStageIsFinished) {
windowCalc.setupStageState = setupSaCoefficients(windowCalc.setupStageState);
}
if (windowCalc.setupStageState.done) {
windowCalc.setupStageIsFinished = true;
}
} else {
console.log("unexpected calcworker setup stage [" + windowCalc.setupStage + "]... stopping setup");
windowCalc.setupStage = setupStages.done;
}
// move to the next stage if this stage has been marked as finished
if (windowCalc.setupStageIsFinished) {
windowCalc.setupStageIsStarted = false;
windowCalc.setupStageIsFinished = false;
windowCalc.setupStage++;
}
}
function stopAndRemoveAllWorkers() {
if (windowCalc.timeout != null) {
clearTimeout(windowCalc.timeout);
}
if (windowCalc.workers === null) {
return;
}
for (let i = 0; i < windowCalc.workers.length; i++) {
windowCalc.workers[i].terminate();
}
windowCalc.workers = null;
}
function updateWorkerCount(msg) {
windowCalc.workersCount = msg;
if (windowCalc.workers === null) {
return;
}
if (windowCalc.minWorkersCount > windowCalc.workersCount) {
windowCalc.minWorkersCount = windowCalc.workersCount;
}
if (windowCalc.maxWorkersCount < windowCalc.workersCount) {
windowCalc.maxWorkersCount = windowCalc.workersCount;
}
// if the worker count has been decreased, as workers finish their chunks they
// will be terminated
// if the worker count has been increased, create workers and give them chunks
for (let i = windowCalc.workers.length + 1; i <= windowCalc.workersCount; i++) {
let newWorker = null;
if (forceWorkerReload) {
newWorker = new Worker("calcsubworker.js?v=" + appVersion + "&" + forceWorkerReloadUrlParam + "&t=" + (Date.now()));
} else {
newWorker = new Worker("calcsubworker.js?v=" + appVersion);
}
windowCalc.workers.push(newWorker);
newWorker.onmessage = onSubWorkerMessage;
assignChunkToWorker(newWorker);
}
}
function removeWorkerIfNecessary(worker) {
if (windowCalc.workers.length <= windowCalc.workersCount) {
return false;
}
const index = windowCalc.workers.indexOf(worker);
if (index < 0) {
return false;
}
windowCalc.workers.splice(index, 1);
return true;
}
var calculatePass = function() {
if (windowCalc.stopped) {
return;
}
calculateWindowPassChunks();
for (const worker of windowCalc.workers) {
assignChunkToWorker(worker);
}
//if (isImageComplete()) {
// cleanUpWindowCache();
//}
};
// for the non-basic (perturbation theory) algorithm, we don't ever need
// to fully calculate the position of all points to our set "precision"
// (number of significant digits) with arbitrary precision math.
// instead (again, just for perturbation theory) all we need is the
// difference from the reference point to each pixel -- and that difference
// value will always be really small and expressable with float or
// floatexp
function setupPixelPositionDelta() {
windowCalc.referenceBottomLeftDeltaX = null;
windowCalc.referenceBottomLeftDeltaY = null;
if (windowCalc.algorithm.includes("basic") ||
windowCalc.referencePx === null ||
windowCalc.referencePy === null) {
// having just reset the delta to null is what we want
return;
}
// the subtraction is done backwards from how i'd expect,
// but that's how deltas are calculated now in the
// Mandelbrot perturbation theory function
windowCalc.referenceBottomLeftDeltaX = windowCalc.math.createFromInfNum(infNumSub(windowCalc.edges.left, windowCalc.referencePx));
windowCalc.referenceBottomLeftDeltaY = windowCalc.math.createFromInfNum(infNumSub(windowCalc.edges.bottom, windowCalc.referencePy));
}
function buildChunkId(chunkPos) {
return infNumFastStr(chunkPos.x) + "," + infNumFastStr(chunkPos.y);
}
// give next chunk, if any, to the worker
var assignChunkToWorker = function(worker) {
if (windowCalc.stopped || windowCalc.xPixelChunks === null || windowCalc.xPixelChunks.length === 0) {
return;
}
if (!windowCalc.caching) {
let nextChunk = windowCalc.xPixelChunks.shift();
let subWorkerMsg = {
"plotId": windowCalc.plotId,
"chunk": nextChunk,
"cachedIndices": [],
"algorithm": windowCalc.algorithm,
"smooth": windowCalc.smooth
};
worker.postMessage({
t: "compute-chunk",
v: subWorkerMsg
});
return;
}
// take the first chunk in the array, and decrement the cursor
let nextChunk = windowCalc.xPixelChunks.shift();
windowCalc.cacheScannedChunksCursor--;
const chunkId = buildChunkId(nextChunk.chunkPos);
let cacheScan = windowCalc.cacheScannedChunks.get(chunkId);
if (cacheScan === undefined) {
scanCacheForChunk(nextChunk);
cacheScan = windowCalc.cacheScannedChunks.get(chunkId);
}
// if the entire chunk is cached, don't bother sending all
// point indices to the worker -- send one -1 index
let subWorkerMsg = {
"plotId": windowCalc.plotId,
"chunk": nextChunk,
"cachedIndices": cacheScan.size === nextChunk.chunkLen ? allCachedIndicesArray : Array.from(cacheScan.keys()).sort((a, b) => a-b)
};
worker.postMessage({
t: "compute-chunk",
v: subWorkerMsg
});
scanCacheForChunkBeyondCursor();
scanCacheForChunkBeyondCursor();
};
function scanCacheForChunkBeyondCursor() {
if (windowCalc.cacheScannedChunksCursor >= windowCalc.xPixelChunks.length - 1 ||
windowCalc.xPixelChunks.length === 0) {
return;
}
windowCalc.cacheScannedChunksCursor++;
// this shouldn't happen but if after incrementing it's still <0,
// ensure we are pointing at the first element in the array
if (windowCalc.cacheScannedChunksCursor < 0) {
windowCalc.cacheScannedChunksCursor = 0;
}
scanCacheForChunk(windowCalc.xPixelChunks[windowCalc.cacheScannedChunksCursor]);