-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathscalarfield.js
839 lines (709 loc) · 32.3 KB
/
scalarfield.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
///////////////////////////////////////////////////////////////////////////////
//
// Topographic Attribute Maps Demo
// Copyright 2020 Reinhold Preiner, Johanna Schmidt, Gabriel Mistelbauer
//
// This code is licensed under an MIT License.
// See the accompanying LICENSE file for details.
//
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// class Field
class Field
{
constructor(width, height, defaultValue)
{
this.width = width;
this.height = height;
this.values = new Array(width * height);
this.values.fill(defaultValue);
}
get(x, y)
{
if (this.inRange(x, y))
return this.values[this.index(x,y)];
return null;
}
set(x, y, value)
{
if (this.inRange(x, y))
this.values[this.index(x,y)] = value;
}
index(x, y)
{
return Math.trunc(y * this.width + x);
}
inRange(x, y)
{
return x >= 0 && x < this.width && y >= 0 && y < this.height;
}
}
///////////////////////////////////////////////////////////////////////////////
// class TopoMap
class TopoMap extends Field
{
constructor(data, interpolationType, resolution, dilationIters, xAccess, yAccess, valueAccess)
{
if (data == null)
return null;
// specify undefined data accessors
if (!xAccess) xAccess = function(p) { return p.x };
if (!yAccess) yAccess = function(p) { return p.y };
if (!valueAccess) valueAccess = function(p) { return p.value };
//--------------------------------------------------------------------
// define field extent based on data
// determine map size and required transformation properties
var xmin, ymin, vmin = xmin = ymin = 1e20;
var xmax, ymax, vmax = xmax = ymax = -1e20;
data.forEach(p => {
var x = xAccess(p);
xmin = Math.min(xmin, x);
xmax = Math.max(xmax, x);
var y = yAccess(p);
ymin = Math.min(ymin, y);
ymax = Math.max(ymax, y);
var value = valueAccess(p);
vmin = Math.min(value, vmin);
vmax = Math.max(value, vmax);
})
console.log("Value range in data: " + [vmin, vmax]);
// add some boundary
var boundary = Math.min(xmax-xmin, ymax-ymin) * 0.15;
xmin -= boundary;
ymin -= boundary;
ymax += boundary;
xmax += boundary + PARAM_FONT_SIZE * 5; // some more boundary to the right to account for text
// define dimensions
var cellSize = Math.max(xmax-xmin, ymax-ymin) / resolution;
var width = Math.ceil((xmax - xmin) / cellSize);
var height = Math.ceil((ymax - ymin) / cellSize);
console.log("Map dimensions: " + (xmax-xmin) + ", " + (ymax-ymin));
//--------------------------------------------------------------------
// initialize class (defining 'this')
super(width, height, 0);
this.xAccess = xAccess;
this.yAccess = yAccess;
this.valueAccess = valueAccess;
// save transformation
this.origin = {"x": xmin, "y": ymin };
this.cellSize = cellSize;
// create scalar field from points
if (PARAM_INTERPOLATE_NN)
this.interpolatePointsNaturalNeighbor(data, dilationIters, interpolationType);
else
this.interpolatePointsDiffusion(data, dilationIters, interpolationType);
}
map(x, y)
{
x = (x - this.origin.x) / this.cellSize;
y = (y - this.origin.y) / this.cellSize;
return {'x': x, 'y': y};
}
// sampling with bilinear interpolation
sampleBilinear(x, y)
{
// map
x = (x - this.origin.x) / this.cellSize;
y = (y - this.origin.y) / this.cellSize;
var x0 = Math.floor(x);
var y0 = Math.floor(y);
var x1 = x0 + 1;
var y1 = y0 + 1;
var tx = x - x0;
var ty = y - y0;
var v0 = this.get(x0, y0) * (1 - tx) + this.get(x1, y0) * tx;
var v1 = this.get(x0, y1) * (1 - tx) + this.get(x1, y1) * tx;
var value = v0 * (1 - ty) + v1 * ty;
if (value == null)
value = this.get(Math.round(x), Math.round(y));
return value;
}
sampleNearestNeighbor(x, y)
{
// map
x = Math.round((x - this.origin.x) / this.cellSize);
y = Math.round((y - this.origin.y) / this.cellSize);
var value = this.get(x, y);
return value;
}
interpolatePointsNaturalNeighbor(data, dilationIters, interpolationType)
{
var INIT_AVG = !PARAM_SHOW_TUNNELS;
console.log("+++ Starting Natural Neighbors");
this.values.fill(null);
var flagField = new Field(this.width, this.height, 0);
var front = new Array();
// set constraint values
//---------------------------------------------------------------
console.log("Setting Constraints");
data.forEach(p =>
{
var value = this.valueAccess(p);
if (!isNumber(value))
return;
var x = Math.round((this.xAccess(p) - this.origin.x) / this.cellSize);
var y = Math.round((this.yAccess(p) - this.origin.y) / this.cellSize);
var idx = this.index(x, y);
if (flagField.values[idx] == 0)
{
this.values[idx] = value;
flagField.values[idx] = 1;
front.push([x,y]);
}
else if (INIT_AVG)
{
// averaging
this.values[idx] += value;
flagField.values[idx] += 1;
}
else
{
// if data is present in this cell, take max value
this.values[idx] = Math.max(this.values[idx], value);
flagField.values[idx] = 1;
}
})
if (INIT_AVG)
{
for (var k = 0; k < this.values.length; k++)
{
var sum = this.values[k];
var count = flagField.values[k];
if (sum != null && count > 0)
{
this.values[k] /= count;
flagField.values = 1;
}
}
}
// dilate constraint values
//---------------------------------------------------------------
console.log("Dilate Constraints");
var dilatedField = new Field (this.width, this.height);
for (var dIters = 0; dIters < dilationIters; dIters++)
{
dilatedField.values = this.values.slice(0);
for (var x = 0; x < this.width; x++)
for (var y = 0; y < this.height; y++)
{
if (INIT_AVG) //--- FOR AVERAGING
{
if (flagField.get(x, y) == 0)
{
const w0 = 0.25, w1 = 0.125, w2 = 0.0625;
var w = 0;
var sum = 0;
var a;
a = this.get(x-1,y-1); if(a != null) { sum += a*w2; w += w2; }
a = this.get(x, y-1); if(a != null) { sum += a*w1; w += w1; }
a = this.get(x+1,y-1); if(a != null) { sum += a*w2; w += w2; }
a = this.get(x-1, y); if(a != null) { sum += a*w1; w += w1; }
a = this.get(x, y); if(a != null) { sum += a*w0; w += w0; }
a = this.get(x+1, y); if(a != null) { sum += a*w1; w += w1; }
a = this.get(x-1,y+1); if(a != null) { sum += a*w2; w += w2; }
a = this.get(x, y+1); if(a != null) { sum += a*w1; w += w1; }
a = this.get(x+1,y+1); if(a != null) { sum += a*w2; w += w2; }
if (w > 0)
{
dilatedField.set(x, y, sum / w);
flagField.set(x, y, 1);
front.push([x,y]);
}
}
}
else //--- FOR AVERAGING
{
var a_self = this.get(x, y);
const w0 = 0.25, w1 = 0.125, w2 = 0.0625;
var w = 0;
var sum = 0;
var a;
a = this.get(x-1,y-1); if(a > a_self) { sum += a*w2; w += w2; }
a = this.get(x, y-1); if(a > a_self) { sum += a*w1; w += w1; }
a = this.get(x+1,y-1); if(a > a_self) { sum += a*w2; w += w2; }
a = this.get(x-1, y); if(a > a_self) { sum += a*w1; w += w1; }
a = this.get(x, y); if(a > a_self) { sum += a*w0; w += w0; }
a = this.get(x+1, y); if(a > a_self) { sum += a*w1; w += w1; }
a = this.get(x-1,y+1); if(a > a_self) { sum += a*w2; w += w2; }
a = this.get(x, y+1); if(a > a_self) { sum += a*w1; w += w1; }
a = this.get(x+1,y+1); if(a > a_self) { sum += a*w2; w += w2; }
if (w > 0)
{
dilatedField.set(x, y, sum / w);
flagField.set(x, y, 1);
front.push([x,y]);
}
}
}
this.values = dilatedField.values.slice(0); // copy thickened data back to current field // TODO: SLICING NECESSARY????
}
// flood fill voronoi centers
//---------------------------------------------------------------
const offsets = [[1,0],[-1,0],[0,1],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]]
// init source field
var srcField = new Field(this.width, this.height, null);
for (var i = 0; i < front.length; i++) {
const [x,y] = front[i];
srcField.set(x,y, front[i]);
}
// propagate
console.log("Propagating");
while(front.length > 0)
{
// look at each point in current propagation front
var newFront = new Array();
for (var i = 0; i < front.length; i++)
{
const [x,y] = front[i];
const value = this.get(x,y);
const src = srcField.get(x,y);
// propagate to neighbors
for (var j = 0; j < offsets.length; j++)
{
const [dx,dy] = offsets[j];
const xn = x + dx;
const yn = y + dy;
if (!this.inRange(xn,yn))
continue;
// source that reached this neigbhor
const srcn = srcField.get(xn, yn);
// update if neighbor doesn't contain data yet
var update = srcn == null;
// if neighbor already contains data, compare distance of this neighbor to its current source to this point's source
if (!update)
{
const [xsrc, ysrc] = src;
const [xsrcn, ysrcn] = srcn;
const distFromNSrc = Math.sqrt(Math.pow(xn - xsrcn, 2) + Math.pow(yn - ysrcn, 2))
const distFromMySrc = Math.sqrt(Math.pow(xn - xsrc, 2) + Math.pow(yn - ysrc, 2))
update = distFromMySrc < distFromNSrc;
}
if (update)
{
// set own value and src in neighbor and set neighbor as new front point
this.set(xn, yn, value);
srcField.set(xn, yn, src);
newFront.push([xn, yn]);
}
}
}
// set new front current
front = newFront;
}
// Perform discrete Natural Neighbor Interpolation at certain subsampling points (performance!)
//------------------------------------------------------------------------------------------------
const INTERPOLATION_SUBSAMPLING = 4;
console.log("Interpolating ", this.width, " x ", this.height, ", 1/", INTERPOLATION_SUBSAMPLING, " Subsampling");
var indexField = new Field(this.width, this.height, null);
var smoothedField = new Field (this.width, this.height);
smoothedField.values = this.values.slice(0);
var index = 0;
for (var xsrc = 0; xsrc < this.width; xsrc += INTERPOLATION_SUBSAMPLING){
console.log(".");
for (var ysrc = 0; ysrc < this.height; ysrc += INTERPOLATION_SUBSAMPLING, index++)
{
//-- simulate inserting this point as new Voronoi center
//weightField.values.fill(null);
indexField.set(xsrc, ysrc, index);
front = [[xsrc,ysrc]];
var totalArea = 1;
var sumValues = this.get(xsrc, ysrc);
//-- propagating
while(front.length > 0)
{
// look at each point in current propagation front
var newFront = new Array();
for (var i = 0; i < front.length; i++)
{
const [x,y] = front[i];
// propagate to neighbors
for (var j = 0; j < offsets.length; j++)
{
const [dx,dy] = offsets[j];
const xn = x + INTERPOLATION_SUBSAMPLING * dx;
const yn = y + INTERPOLATION_SUBSAMPLING * dy;
if (!this.inRange(xn,yn))
continue;
const localIndex = indexField.get(xn, yn);
if (localIndex != index) // not yet visited in current iteration
{
// source that reached this neigbhor
const [xsrcn, ysrcn] = srcField.get(xn, yn);
// compare distance of this neighbor to its current source to this point's source
const distFromNSrc = Math.sqrt(Math.pow(xn - xsrcn, 2) + Math.pow(yn - ysrcn, 2))
const distFromMySrc = Math.sqrt(Math.pow(xn - xsrc, 2) + Math.pow(yn - ysrc, 2))
//console.log(distFromMySrc, " < ", distFromNSrc)
if (distFromMySrc < distFromNSrc)
{
const w = 1.0 / Math.sqrt(Math.sqrt(totalArea));
sumValues += w * this.get(xn, yn);
totalArea += w;
// mark cell as visited and push to new front
indexField.set(xn, yn, index);
newFront.push([xn, yn]);
}
}
}
}
// set new front current
front = newFront;
}
//-- finished propagation
smoothedField.set(xsrc, ysrc, sumValues / totalArea);
flagField.set(xsrc, ysrc, 2); // remember this is an intermediate source value (value 2)
}
}
// copy back smoothed field
this.values = smoothedField.values.slice(0);
// diffuse
//--------------------------------
console.log("Diffusing");
this.diffuse(flagField, interpolationType);
// final smooth
//---------------------------------------------------------------
console.log("Smoothing");
for (var i = 0; i < 2 * INTERPOLATION_SUBSAMPLING; i++)
{
// smooth unconstrained pixels of current field
var smoothedField = new Field (this.width, this.height, 0);
for (var x = 0; x < this.width; x++)
for (var y = 0; y < this.height; y++)
{
if (flagField.get(x,y) != 1) // smooth field value if not a constraint
{
const w0 = 0.25, w1 = 0.125, w2 = 0.0625;
var w = 0;
var sum = 0;
var a;
{
a = this.get(x-1,y-1); if(a != null) { sum += a*w2; w += w2; }
a = this.get(x, y-1); if(a != null) { sum += a*w1; w += w1; }
a = this.get(x+1,y-1); if(a != null) { sum += a*w2; w += w2; }
a = this.get(x-1, y); if(a != null) { sum += a*w1; w += w1; }
a = this.get(x, y); if(a != null) { sum += a*w0; w += w0; }
a = this.get(x+1, y); if(a != null) { sum += a*w1; w += w1; }
a = this.get(x-1,y+1); if(a != null) { sum += a*w2; w += w2; }
a = this.get(x, y+1); if(a != null) { sum += a*w1; w += w1; }
a = this.get(x+1,y+1); if(a != null) { sum += a*w2; w += w2; }
smoothedField.set(x, y, sum / w);
}
}
else
smoothedField.set(x, y, this.get(x,y)); // copy hard constrained values - introduces artifcats!
}
this.values = smoothedField.values.slice(0); // copy smoothed data back to current field
}
console.log("+++ Finished Diffusion");
}
interpolatePointsDiffusion(data, dilationIters, interpolationType)
{
var INIT_AVG = false; //!PARAM_SHOW_TUNNELS;
console.log("+++ Starting Diffusion");
this.values.fill(null);
var flagField = new Field(this.width, this.height, 0);
// set constraint values
//---------------------------------------------------------------
console.log("Setting Constraints");
data.forEach(p =>
{
var value = this.valueAccess(p);
if (!isNumber(value))
return;
var x = Math.round((this.xAccess(p) - this.origin.x) / this.cellSize);
var y = Math.round((this.yAccess(p) - this.origin.y) / this.cellSize);
var idx = this.index(x, y);
if (flagField.values[idx] == 0)
{
this.values[idx] = value;
flagField.values[idx] = 1;
}
else if (INIT_AVG)
{
// averaging
this.values[idx] += value;
flagField.values[idx] += 1;
}
else
{
// max value
this.values[idx] = Math.max(this.values[idx], value);
flagField.values[idx] = 1;
}
})
if (INIT_AVG)
{
for (var k = 0; k < this.values.length; k++)
{
var sum = this.values[k];
var count = flagField.values[k];
if (sum != null && count > 0)
this.values[k] /= count;
}
}
// dilate constraint values
//---------------------------------------------------------------
console.log("Dilate Constraints");
var dilatedField = new Field (this.width, this.height);
for (var dIters = 0; dIters < dilationIters; dIters++)
{
dilatedField.values = this.values.slice(0);
for (var x = 0; x < this.width; x++)
for (var y = 0; y < this.height; y++)
{
if (INIT_AVG) //--- FOR AVERAGING
{
if (flagField.get(x, y) == 0)
{
const w0 = 0.25, w1 = 0.125, w2 = 0.0625;
var w = 0;
var sum = 0;
var a;
a = this.get(x-1,y-1); if(a != null) { sum += a*w2; w += w2; }
a = this.get(x, y-1); if(a != null) { sum += a*w1; w += w1; }
a = this.get(x+1,y-1); if(a != null) { sum += a*w2; w += w2; }
a = this.get(x-1, y); if(a != null) { sum += a*w1; w += w1; }
a = this.get(x, y); if(a != null) { sum += a*w0; w += w0; }
a = this.get(x+1, y); if(a != null) { sum += a*w1; w += w1; }
a = this.get(x-1,y+1); if(a != null) { sum += a*w2; w += w2; }
a = this.get(x, y+1); if(a != null) { sum += a*w1; w += w1; }
a = this.get(x+1,y+1); if(a != null) { sum += a*w2; w += w2; }
if (w > 0)
{
dilatedField.set(x, y, sum / w);
flagField.set(x, y, 1);
}
}
}
else //--- FOR AVERAGING
{
var a_self = this.get(x, y);
const w0 = 0.25, w1 = 0.125, w2 = 0.0625;
var w = 0;
var sum = 0;
var a;
a = this.get(x-1,y-1); if(a > a_self) { sum += a*w2; w += w2; }
a = this.get(x, y-1); if(a > a_self) { sum += a*w1; w += w1; }
a = this.get(x+1,y-1); if(a > a_self) { sum += a*w2; w += w2; }
a = this.get(x-1, y); if(a > a_self) { sum += a*w1; w += w1; }
a = this.get(x, y); if(a > a_self) { sum += a*w0; w += w0; }
a = this.get(x+1, y); if(a > a_self) { sum += a*w1; w += w1; }
a = this.get(x-1,y+1); if(a > a_self) { sum += a*w2; w += w2; }
a = this.get(x, y+1); if(a > a_self) { sum += a*w1; w += w1; }
a = this.get(x+1,y+1); if(a > a_self) { sum += a*w2; w += w2; }
if (w > 0)
{
dilatedField.set(x, y, sum / w);
flagField.set(x, y, 1);
}
}
}
this.values = dilatedField.values.slice(0); // copy thickened data back to current field // TODO: SLICING NECESSARY????
}
// after setting hard constraints, diffuse
this.diffuse(flagField, interpolationType)
console.log("+++ Finished Diffusion");
}
diffuse(sourceField, interpolationType)
{
// Downsample
//---------------------------------------------------------------
console.log("Downsampling");
var currentField = this;
var currentFlags = sourceField; // marks hard constraints (heat sources) by 1, others 0
var pyramid = [];
while (Math.max(currentField.width, currentField.height) > 1)
{
var coarseField = new Field(Math.ceil(currentField.width / 2), Math.ceil(currentField.height / 2), 0);
var coarseFlags = new Field(Math.ceil(currentField.width / 2), Math.ceil(currentField.height / 2), 0);
for (var x = 0; x < coarseField.width; x++)
for (var y = 0; y < coarseField.height; y++)
{
var subc = [[2*x, 2*y],[2*x+1,2*y],[2*x,2*y+1],[2*x+1,2*y+1]];
var avg = 0, c = 0;
var max = 0;
var min = 1e20;
var arr = [];
for (var i = 0; i < 4; i++)
if (currentFlags.get(subc[i][0], subc[i][1])) {
var cell_value = currentField.get(subc[i][0], subc[i][1]);
min = Math.min(min, cell_value);
max = Math.max(max, cell_value);
avg += cell_value; c++;
arr.push(cell_value);
}
if (c > 0)
{
var value = max;
switch (interpolationType)
{
case InterpolationType.MIN: value = min; break;
case InterpolationType.MAX: value = max; break;
case InterpolationType.AVG: value = avg/c; break;
}
coarseField.set(x, y, value);
coarseFlags.set(x, y, 1);
}
}
// push current inmage onto pyramid
pyramid.push([currentField, currentFlags]);
currentField = coarseField;
currentFlags = coarseFlags;
}
// Upsample
//---------------------------------------------------------------
console.log("Upsampling");
while (pyramid.length > 0)
{
var tuple = pyramid.pop();
currentField = tuple[0];
currentFlags = tuple[1];
// fill holes using coarse field info
for (var x = 0; x < currentField.width; x++)
for (var y = 0; y < currentField.height; y++)
{
// if (!currentFlags.get(x,y)) // HardConstraint all nonzero Flag Values
if (currentFlags.get(x,y) != 1) // Only HardConstraint Flag Values 1 - others can be sources, but are overwritten when upsampled
currentField.set(x, y, coarseField.get(Math.trunc(x/2), Math.trunc(y/2)));
}
// smooth unconstrained pixels of current field
var smoothedField = new Field (currentField.width, currentField.height, 0);
for (var x = 0; x < currentField.width; x++)
for (var y = 0; y < currentField.height; y++)
{
const w0 = 0.25, w1 = 0.125, w2 = 0.0625;
var w = 0;
var sum = 0;
var a;
a = currentField.get(x-1,y-1); if(a != null) { sum += a*w2; w += w2; }
a = currentField.get(x, y-1); if(a != null) { sum += a*w1; w += w1; }
a = currentField.get(x+1,y-1); if(a != null) { sum += a*w2; w += w2; }
a = currentField.get(x-1, y); if(a != null) { sum += a*w1; w += w1; }
a = currentField.get(x, y); if(a != null) { sum += a*w0; w += w0; }
a = currentField.get(x+1, y); if(a != null) { sum += a*w1; w += w1; }
a = currentField.get(x-1,y+1); if(a != null) { sum += a*w2; w += w2; }
a = currentField.get(x, y+1); if(a != null) { sum += a*w1; w += w1; }
a = currentField.get(x+1,y+1); if(a != null) { sum += a*w2; w += w2; }
smoothedField.set(x, y, sum / w);
}
currentField.values = smoothedField.values.slice(0); // copy smoothed data back to current field
coarseField = currentField;
}
}
getContourPaths(thresholds)
{
// create contours
return d3.contours()
.size([this.width, this.height])
.thresholds(thresholds)
(this.values)
}
}
///////////////////////////////////////////////////////////////////////////////
// class GradientField
// defining a gradient field over a 2D scalar field, where the direction is
// encoded in the (normalized) xy-coordinates
class GradientField extends Field
{
constructor(scalarField)
{
super(scalarField.width, scalarField.height, new vec(0,0,0));
for (var y = 1; y < this.height - 1; y++)
for (var x = 1; x < this.width - 1; x++)
{
var ddx = scalarField.get(x+1, y) - scalarField.get(x-1, y);
var ddy = scalarField.get(x, y+1) - scalarField.get(x, y-1);
var grad = new vec(ddx/2, ddy/2);
this.set(x, y, grad);
}
// extrapolate boundaries
if (this.height > 1)
for (var x = 0; x < this.width; x++)
{
this.set(x, 0, this.get(x, 1));
this.set(x, this.height-1, this.get(x, this.height-2));
}
if (this.width > 1)
for (var y = 0; y < this.height; y++)
{
this.set(0, y, this.get(1, y));
this.set(this.width-1, y, this.get(this.width-2, y));
}
}
// sampling with bilinear interpolation
sampleBilinear(x, y)
{
// map
// get enclosing values
var x0 = Math.floor(x);
var y0 = Math.floor(y);
var x1 = x0 + 1;
var y1 = y0 + 1;
var tx = x - x0;
var ty = y - y0;
var v00 = this.get(x0, y0);
var v10 = this.get(x1, y0);
var v01 = this.get(x0, y1);
var v11 = this.get(x1, y1);
if (!v00 || !v10 || !v01 || !v11)
return null;
var v0 = v00.mul(1 - tx).add( v10.mul(tx) );
var v1 = v01.mul(1 - tx).add( v11.mul(tx) );
var value = v0.mul(1 - ty).add( v1.mul(ty) );
if (isNaN(value.x) || isNaN(value.y))
value = this.get(Math.round(x), Math.round(y));
return value;
}
}
///////////////////////////////////////////////////////////////////////////////
// class NormalField
// defining a field of normals over a 2D scalar field, where the field is embedded
// in the xy-plane, with scalar values extending to the z. direction.
// The resulting normals will thus point in upwards in positive z-direction.
class NormalField extends Field
{
constructor(scalarField, rangeToUnitFactor)
{
super(scalarField.width, scalarField.height, new vec(0,0,0));
for (var y = 1; y < this.height - 1; y++)
for (var x = 1; x < this.width - 1; x++)
{
var ddx = scalarField.get(x+1, y) - scalarField.get(x-1, y);
var ddy = scalarField.get(x, y+1) - scalarField.get(x, y-1);
var ds = 2 * scalarField.cellSize;
var tx = new vec(ds, 0, ddx * rangeToUnitFactor)
var ty = new vec(0, ds, ddy * rangeToUnitFactor)
var n = tx.cross(ty).normalize();
this.set(x, y, n);
}
// extrapolate boundaries
if (this.height > 1)
for (var x = 0; x < this.width; x++)
{
this.set(x, 0, this.get(x, 1));
this.set(x, this.height-1, this.get(x, this.height-2));
}
if (this.width > 1)
for (var y = 0; y < this.height; y++)
{
this.set(0, y, this.get(1, y));
this.set(this.width-1, y, this.get(this.width-2, y));
}
}
// lightdir: ivnerse vector of traveling direction of parallel light rays
getShadingContourPaths(lightdir)
{
// encodes the shadowing of the field (higher is darker)
var shadowField = new Field(this.width, this.height, 0);
for (var k = 0; k < this.values.length; k++)
{
var N = this.values[k];
var lambert = Math.max(0, N.dot(lightdir));
shadowField.values[k] = 1 - lambert; // we want contours at where shadow is, not where light iss
}
// create contours
return d3.contours()
.size([this.width, this.height])
.thresholds(d3.range(0, 1, 0.05))
(shadowField.values)
}
}