forked from seung-lab/neuroglancer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.ts
1068 lines (942 loc) · 38.1 KB
/
base.ts
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
/**
* @license
* Copyright 2016 Google Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {CoordinateTransform} from 'neuroglancer/coordinate_transform';
import {ChunkLayout} from 'neuroglancer/sliceview/chunk_layout';
import {TrackableBoolean} from 'neuroglancer/trackable_boolean';
import {WatchableValueInterface} from 'neuroglancer/trackable_value';
import {partitionArray} from 'neuroglancer/util/array';
import {approxEqual} from 'neuroglancer/util/compare';
import {DATA_TYPE_BYTES, DataType} from 'neuroglancer/util/data_type';
import {effectiveScalingFactorFromMat4, identityMat4, kAxes, kInfinityVec, kZeroVec, mat4, rectifyTransformMatrixIfAxisAligned, transformVectorByMat4, vec3} from 'neuroglancer/util/geom';
import {SharedObject} from 'neuroglancer/worker_rpc';
export {DATA_TYPE_BYTES, DataType};
export type GlobalCoordinateRectangle = [vec3, vec3, vec3, vec3];
const DEBUG_CHUNK_INTERSECTIONS = false;
const DEBUG_VISIBLE_SOURCES = false;
const tempVec3 = vec3.create();
/**
* Average cross-sectional area contained within a chunk of the specified size and rotation.
*
* This is estimated by taking the total volume of the chunk and dividing it by the total length of
* the chunk along the z axis.
*/
function estimateSliceAreaPerChunk(zAxis: vec3, chunkLayout: ChunkLayout) {
const chunkSize = chunkLayout.size;
const zAxisRotated = chunkLayout.globalToLocalSpatialVector(tempVec3, zAxis);
// Minimum and maximum dot product of zAxisRotated with each of the corners of the chunk. Both
// are initialized to 0 because the origin of the chunk has a projection of 0.
let minProjection = 0, maxProjection = 0;
let chunkVolume = 1;
for (let i = 0; i < 3; ++i) {
const chunkSizeValue = chunkSize[i];
chunkVolume *= chunkSizeValue;
const projection = chunkSizeValue * zAxisRotated[i];
minProjection = Math.min(minProjection, projection);
maxProjection = Math.max(maxProjection, projection);
}
const projectionLength = maxProjection - minProjection;
return chunkVolume / projectionLength;
}
/**
* All valid chunks are in the range [lowerBound, upperBound).
*
* @param lowerBound Output parameter for lowerBound.
* @param upperBound Output parameter for upperBound.
* @param sources Sources for which to compute the chunk bounds.
*/
function computeSourcesChunkBounds(
sourcesLowerBound: vec3, sourcesUpperBound: vec3, sources: Iterable<SliceViewChunkSource>) {
for (let i = 0; i < 3; ++i) {
sourcesLowerBound[i] = Number.POSITIVE_INFINITY;
sourcesUpperBound[i] = Number.NEGATIVE_INFINITY;
}
for (let source of sources) {
let {spec} = source;
let {lowerChunkBound, upperChunkBound} = spec;
for (let i = 0; i < 3; ++i) {
sourcesLowerBound[i] = Math.min(sourcesLowerBound[i], lowerChunkBound[i]);
sourcesUpperBound[i] = Math.max(sourcesUpperBound[i], upperChunkBound[i]);
}
}
}
enum BoundsComparisonResult {
// Needle is fully outside haystack.
FULLY_OUTSIDE,
// Needle is fully inside haystack.
FULLY_INSIDE,
// Needle is partially inside haystack.
PARTIALLY_INSIDE
}
function compareBoundsSingleDimension(
needleLower: number, needleUpper: number, haystackLower: number, haystackUpper: number) {
if (needleLower >= haystackUpper || needleUpper <= haystackLower) {
return BoundsComparisonResult.FULLY_OUTSIDE;
}
if (needleLower >= haystackLower && needleUpper <= haystackUpper) {
return BoundsComparisonResult.FULLY_INSIDE;
}
return BoundsComparisonResult.PARTIALLY_INSIDE;
}
function compareBounds(
needleLowerBound: vec3, needleUpperBound: vec3, haystackLowerBound: vec3,
haystackUpperBound: vec3) {
let curResult = BoundsComparisonResult.FULLY_INSIDE;
for (let i = 0; i < 3; ++i) {
let newResult = compareBoundsSingleDimension(
needleLowerBound[i], needleUpperBound[i], haystackLowerBound[i], haystackUpperBound[i]);
switch (newResult) {
case BoundsComparisonResult.FULLY_OUTSIDE:
return newResult;
case BoundsComparisonResult.PARTIALLY_INSIDE:
curResult = newResult;
break;
}
}
return curResult;
}
function areBoundsInvalid(lowerBound: vec3, upperBound: vec3) {
for (let i = 0; i < 3; ++i) {
if (lowerBound[i] > upperBound[i]) {
return true;
}
}
return false;
}
export interface TransformedSource<Source extends SliceViewChunkSource = SliceViewChunkSource> {
source: Source;
chunkLayout: ChunkLayout;
voxelSize: vec3;
}
export interface RenderLayer<Source extends SliceViewChunkSource> {
sources: Source[][];
transform: CoordinateTransform;
transformedSources: TransformedSource<Source>[][]|undefined;
transformedSourcesGeneration: number;
renderScaleTarget: WatchableValueInterface<number>;
renderRatioLimit?: number;
leafRequestsActive?: TrackableBoolean;
}
export function getTransformedSources<Source extends SliceViewChunkSource>(
renderLayer: RenderLayer<Source>) {
const {transform} = renderLayer;
let {transformedSources} = renderLayer;
const generation = transform.changed.count;
if (generation !== renderLayer.transformedSourcesGeneration) {
renderLayer.transformedSourcesGeneration = generation;
if (mat4.equals(transform.transform, identityMat4)) {
transformedSources = renderLayer.sources.map(
alternatives => alternatives.map(source => ({
source,
chunkLayout: source.spec.chunkLayout,
voxelSize: source.spec.voxelSize
})));
} else {
transformedSources = renderLayer.sources.map(alternatives => alternatives.map(source => {
const chunkLayout = source.spec.chunkLayout;
const transformedChunkLayout = ChunkLayout.get(
chunkLayout.size, getCombinedTransform(chunkLayout.transform, transform));
return {
chunkLayout: transformedChunkLayout,
source,
voxelSize: transformedChunkLayout.localSpatialVectorToGlobal(
vec3.create(), source.spec.voxelSize),
};
}));
}
renderLayer.transformedSources = transformedSources;
}
return transformedSources!;
}
function pickBestAlternativeSource<Source extends SliceViewChunkSource>(
zAxis: vec3, alternatives: TransformedSource<Source>[]) {
let numAlternatives = alternatives.length;
let bestAlternativeIndex = 0;
if (DEBUG_VISIBLE_SOURCES) {
console.log(alternatives);
}
if (numAlternatives > 1) {
let bestSliceArea = 0;
for (let alternativeIndex = 0; alternativeIndex < numAlternatives; ++alternativeIndex) {
let alternative = alternatives[alternativeIndex];
const {chunkLayout} = alternative;
let sliceArea = estimateSliceAreaPerChunk(zAxis, chunkLayout);
if (DEBUG_VISIBLE_SOURCES) {
console.log(`zAxis = ${zAxis}, chunksize = ${chunkLayout.size}, sliceArea = ${sliceArea}`);
}
if (sliceArea > bestSliceArea) {
bestSliceArea = sliceArea;
bestAlternativeIndex = alternativeIndex;
}
}
}
return alternatives[bestAlternativeIndex];
}
const tempGlobalRectangle: GlobalCoordinateRectangle = [vec3.create(), vec3.create(), vec3.create(), vec3.create()];
export class SliceViewBase<Source extends SliceViewChunkSource,
RLayer extends RenderLayer<Source>> extends SharedObject {
width = -1;
height = -1;
hasViewportToData = false;
/**
* Specifies whether width, height, and viewportToData are valid.
*/
hasValidViewport = false;
// Transforms (x,y) viewport coordinates in the range:
//
// x=[left: -width/2, right: width/2] and
//
// y=[top: -height/2, bottom: height/2],
//
// to data coordinates.
viewportToData = mat4.create();
// Normalized x, y, and z viewport axes in data coordinate space.
viewportAxes = [vec3.create(), vec3.create(), vec3.create()];
// Viewport axes used for selecting visible sources.
previousViewportAxes = [vec3.create(), vec3.create()];
centerDataPosition = vec3.create();
viewportPlaneDistanceToOrigin: number = 0;
/**
* For each visible ChunkLayout, maps each visible GenericVolumeChunkSource to its priority index.
* Overall chunk priority ordering is based on a lexicographical ordering of (priorityIndex,
* -distanceToCenter).
*/
visibleChunkLayouts = new Map<ChunkLayout, Map<Source, number>>();
visibleLayers = new Map<RLayer, TransformedSource<Source>[]>();
visibleSourcesStale = true;
/**
* Size in spatial units (nm) of a single pixel.
*/
pixelSize: number = 0;
constructor() {
super();
mat4.identity(this.viewportToData);
}
/**
* Called when hasValidViewport == true and the viewport width/height or data transform matrix
* changes.
*/
onViewportChanged() {}
maybeSetHasValidViewport() {
if (!this.hasValidViewport && this.width !== -1 && this.height !== -1 &&
this.hasViewportToData) {
this.hasValidViewport = true;
this.onHasValidViewport();
}
if (this.hasValidViewport) {
this.onViewportChanged();
}
}
onHasValidViewport() {}
setViewportSize(width: number, height: number) {
if (width !== this.width || height !== this.height) {
this.width = width;
this.height = height;
this.maybeSetHasValidViewport();
return true;
}
return false;
}
setViewportToDataMatrix(mat: mat4) {
if (this.hasViewportToData && mat4.equals(this.viewportToData, mat)) {
return false;
}
this.hasViewportToData = true;
let {viewportToData} = this;
mat4.copy(viewportToData, mat);
rectifyTransformMatrixIfAxisAligned(viewportToData);
vec3.transformMat4(this.centerDataPosition, kZeroVec, mat);
// Initialize to zero to avoid confusing TypeScript compiler.
let newPixelSize = 0;
// Swap previousViewportAxes with viewportAxes.
let viewportAxes = this.viewportAxes;
let previousViewportAxes = this.previousViewportAxes;
// Compute axes.
for (var i = 0; i < 3; ++i) {
let a = viewportAxes[i];
transformVectorByMat4(a, kAxes[i], viewportToData);
// a[3] is guaranteed to be 0.
if (i === 0) {
newPixelSize = vec3.length(a);
}
vec3.normalize(a, a);
}
this.viewportAxes = viewportAxes;
this.previousViewportAxes = previousViewportAxes;
if (!approxEqual(newPixelSize, this.pixelSize) ||
(vec3.dot(viewportAxes[0], previousViewportAxes[0]) < 0.95) ||
(vec3.dot(viewportAxes[1], previousViewportAxes[1]) < 0.95)) {
vec3.copy(previousViewportAxes[0], viewportAxes[0]);
vec3.copy(previousViewportAxes[1], viewportAxes[1]);
this.visibleSourcesStale = true;
this.pixelSize = newPixelSize;
}
// Compute viewport plane distance to origin.
this.viewportPlaneDistanceToOrigin = vec3.dot(this.centerDataPosition, this.viewportAxes[2]);
this.onViewportToDataMatrixChanged();
this.maybeSetHasValidViewport();
return true;
}
onViewportToDataMatrixChanged() {}
/**
* Computes the list of sources to use for each visible layer, based on the
* current pixelSize, and the user specified integers minMIPLevel and maxMIPLevel.
*/
updateVisibleSources() {
if (!this.visibleSourcesStale) {
return;
}
this.visibleSourcesStale = false;
// Increase pixel size by a small margin.
const pixelSize = this.pixelSize * 1.1;
// console.log("pixelSize", pixelSize);
const visibleChunkLayouts = this.visibleChunkLayouts;
const zAxis = this.viewportAxes[2];
const visibleLayers = this.visibleLayers;
visibleChunkLayouts.clear();
for (const [renderLayer, visibleSources] of visibleLayers) {
visibleSources.length = 0;
const transformedSources = getTransformedSources(renderLayer);
const numSources = transformedSources.length;
let scaleIndex: number;
// At the smallest scale, all alternative sources must have the same voxel size, which is
// considered to be the base voxel size.
const smallestVoxelSize = transformedSources[0][0].voxelSize;
if (renderLayer.renderRatioLimit !== undefined) {
// If the pixel nm size in the slice is bigger than the smallest dimension of the
// highest resolution voxel size (e.g. 4nm if the highest res is 4x4x40nm) by
// a certain ratio (right now semi-arbitarily set as a constant in chunked_graph/base.ts)
// we do not request the ChunkedGraph for root -> supervoxel mappings, and
// instead display a message to the user
const chunkedGraphVoxelSize = renderLayer.sources[0][0].spec.voxelSize;
if (renderLayer.renderRatioLimit < (pixelSize / Math.min(...chunkedGraphVoxelSize))) {
if (renderLayer.leafRequestsActive !== undefined) {
renderLayer.leafRequestsActive.value = false;
}
continue;
}
if (renderLayer.leafRequestsActive !== undefined) {
renderLayer.leafRequestsActive.value = true;
}
}
const renderScaleTarget = renderLayer.renderScaleTarget.value;
/**
* Determines whether we should continue to look for a finer-resolution source *after* one
* with the specified voxelSize.
*/
const canImproveOnVoxelSize = (voxelSize: vec3) => {
const targetSize = pixelSize * renderScaleTarget;
for (let i = 0; i < 3; ++i) {
const size = voxelSize[i];
// If size <= pixelSize, no need for improvement.
// If size === smallestVoxelSize, also no need for improvement.
if (size > targetSize && size > 1.01 * smallestVoxelSize[i]) {
return true;
}
}
return false;
};
const improvesOnPrevVoxelSize = (voxelSize: vec3, prevVoxelSize: vec3) => {
const targetSize = pixelSize * renderScaleTarget;
for (let i = 0; i < 3; ++i) {
const size = voxelSize[i];
const prevSize = prevVoxelSize[i];
if (Math.abs(targetSize - size) < Math.abs(targetSize - prevSize) &&
size < 1.01 * prevSize) {
return true;
}
}
return false;
};
/**
* Registers a source as being visible. This should be called with consecutively decreasing
* values of scaleIndex.
*/
const addVisibleSource =
(transformedSource: TransformedSource<Source>, sourceScaleIndex: number) => {
// Add to end of visibleSources list. We will reverse the list after all sources are
// added.
const {source, chunkLayout} = transformedSource;
visibleSources[visibleSources.length++] = transformedSource;
let existingSources = visibleChunkLayouts.get(chunkLayout);
if (existingSources === undefined) {
existingSources = new Map<Source, number>();
visibleChunkLayouts.set(chunkLayout, existingSources);
}
existingSources.set(source, sourceScaleIndex);
};
scaleIndex = numSources - 1;
let prevVoxelSize: vec3|undefined;
while (true) {
const transformedSource = pickBestAlternativeSource(zAxis, transformedSources[scaleIndex]);
if (prevVoxelSize !== undefined &&
!improvesOnPrevVoxelSize(transformedSource.voxelSize, prevVoxelSize)) {
break;
}
addVisibleSource(transformedSource, (scaleIndex + 1) / numSources);
if (scaleIndex === 0 || !canImproveOnVoxelSize(transformedSource.voxelSize)) {
break;
}
prevVoxelSize = transformedSource.voxelSize;
--scaleIndex;
}
// Reverse visibleSources list since we added sources from coarsest to finest resolution, but
// we want them ordered from finest to coarsest.
visibleSources.reverse();
}
}
protected computeVisibleChunks<T>(
getLayoutObject: (chunkLayout: ChunkLayout) => T,
addChunk:
(chunkLayout: ChunkLayout, layoutObject: T, lowerBound: vec3,
fullyVisibleSources: SliceViewChunkSource[]) => void,
rectangleOut?: GlobalCoordinateRectangle) {
this.updateVisibleSources();
const visibleRectangle = (rectangleOut) ? rectangleOut: tempGlobalRectangle;
this.computeGlobalRectangle(visibleRectangle);
return this.computeChunksWithinRectangle(
getLayoutObject, addChunk, visibleRectangle);
}
// Used to get global coordinates of viewport corners. These corners
// are used to find chunks within these corners in computeChunksFromGlobalCorners. The order of
// these corners are relevant in the backend in computePrefetchChunksWithinPlane to construct the corners of
// prefetch rectangles.
protected computeGlobalRectangle(rectangleOut: GlobalCoordinateRectangle, widthMultiplier = 1, heightMultiplier = 1) {
const {viewportToData, width, height} = this;
const modifiedWidth = widthMultiplier * width;
const modifiedHeight = heightMultiplier * height;
for (let i = 0; i < 3; ++i) {
rectangleOut[0][i] = -kAxes[0][i] * modifiedWidth / 2 - kAxes[1][i] * modifiedHeight / 2;
rectangleOut[1][i] = -kAxes[0][i] * modifiedWidth / 2 + kAxes[1][i] * modifiedHeight / 2;
rectangleOut[2][i] = kAxes[0][i] * modifiedWidth / 2 - kAxes[1][i] * modifiedHeight / 2;
rectangleOut[3][i] = kAxes[0][i] * modifiedWidth / 2 + kAxes[1][i] * modifiedHeight / 2;
}
for (let i = 0; i < 4; ++i) {
vec3.transformMat4(rectangleOut[i], rectangleOut[i], viewportToData);
}
}
protected static getChunkBoundsForRectangle(
chunkLayout: ChunkLayout, rectangle: GlobalCoordinateRectangle, lowerBoundOut: vec3,
upperBoundOut: vec3) {
vec3.set(
lowerBoundOut, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY,
Number.POSITIVE_INFINITY);
vec3.set(
upperBoundOut, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY,
Number.NEGATIVE_INFINITY);
for (let i = 0; i < 4; ++i) {
const localCorner = chunkLayout.globalToLocalGrid(tempVec3, rectangle[i]);
for (let j = 0; j < 3; ++j) {
lowerBoundOut[j] = Math.min(lowerBoundOut[j], Math.floor(localCorner[j]));
upperBoundOut[j] = Math.max(upperBoundOut[j], Math.floor(localCorner[j]) + 1);
}
}
}
protected computeChunksWithinRectangle<T>(
getLayoutObject: (chunkLayout: ChunkLayout) => T,
addChunk:
(chunkLayout: ChunkLayout, layoutObject: T, lowerBound: vec3,
fullyVisibleSources: SliceViewChunkSource[]) => void,
rectangle: GlobalCoordinateRectangle,
makeBoundsForRectangle?:
(chunkLayout: ChunkLayout, rectangle: GlobalCoordinateRectangle, lowerBoundOut: vec3,
upperBoundOut: vec3, voxelSize: vec3) => void) {
// These variables hold the lower and upper bounds on chunk grid positions that intersect the
// viewing plane.
var lowerChunkBound = vec3.create();
var upperChunkBound = vec3.create();
let sourcesLowerChunkBound = vec3.create();
let sourcesUpperChunkBound = vec3.create();
// Vertex with maximal dot product with the positive viewport plane normal.
// Implicitly, negativeVertex = 1 - positiveVertex.
var positiveVertex = vec3.create();
var planeNormal = vec3.create();
const centerDataPosition = vec3.create();
// Sources whose bounds partially contain the current bounding box.
let partiallyVisibleSources = new Array<SliceViewChunkSource>();
// Sources whose bounds fully contain the current bounding box.
let fullyVisibleSources = new Array<SliceViewChunkSource>();
const setCenterDataPosition = () => {
vec3.copy(centerDataPosition, rectangle[0]);
for (let i = 1; i < 4; ++i) {
vec3.add(centerDataPosition, centerDataPosition, rectangle[i]);
}
vec3.scale(centerDataPosition, centerDataPosition, 0.25);
};
setCenterDataPosition();
const computeChunksForLayout =
(chunkLayout: ChunkLayout,
visibleSources: Map<SliceViewChunkSource, number>): void => {
let layoutObject = getLayoutObject(chunkLayout);
const setupLocalChunkBounds = () => {
computeSourcesChunkBounds(
sourcesLowerChunkBound, sourcesUpperChunkBound, visibleSources.keys());
if (DEBUG_CHUNK_INTERSECTIONS) {
console.log(
`Initial sources chunk bounds: ` +
`${vec3.str(sourcesLowerChunkBound)}, ${vec3.str(sourcesUpperChunkBound)}`);
}
chunkLayout.globalToLocalSpatialVector(planeNormal, this.viewportAxes[2]);
for (let i = 0; i < 3; ++i) {
positiveVertex[i] = planeNormal[i] > 0 ? 1 : 0;
}
if (makeBoundsForRectangle) {
makeBoundsForRectangle(chunkLayout, rectangle, lowerChunkBound, upperChunkBound, visibleSources.keys().next().value.spec.voxelSize);
// rectangle modified in order to prefetch, reset center
setCenterDataPosition();
}
else {
SliceViewBase.getChunkBoundsForRectangle(chunkLayout, rectangle, lowerChunkBound, upperChunkBound);
}
vec3.max(lowerChunkBound, lowerChunkBound, sourcesLowerChunkBound);
vec3.min(upperChunkBound, upperChunkBound, sourcesUpperChunkBound);
};
setupLocalChunkBounds();
// Center position in chunk grid coordinates.
const planeDistanceToOrigin =
vec3.dot(chunkLayout.globalToLocalGrid(tempVec3, centerDataPosition), planeNormal);
// Make sure bounds are not invalid. This can only happen when the backend is prefetching
// along the normal to the plane, and tries to prefetch outside the bounds of the dataset
if (areBoundsInvalid(lowerChunkBound, upperChunkBound)) {
return;
}
// Checks whether [lowerBound, upperBound) intersects the viewport plane.
//
// positiveVertexDistanceToOrigin = dot(planeNormal, lowerBound +
// positiveVertex * (upperBound - lowerBound)) - planeDistanceToOrigin;
// negativeVertexDistanceToOrigin = dot(planeNormal, lowerBound +
// negativeVertex * (upperBound - lowerBound)) - planeDistanceToOrigin;
//
// positive vertex must have positive distance, and negative vertex must
// have negative distance.
function intersectsPlane() {
var positiveVertexDistanceToOrigin = 0;
var negativeVertexDistanceToOrigin = 0;
// Check positive vertex.
for (let i = 0; i < 3; ++i) {
let normalValue = planeNormal[i];
let lowerValue = lowerChunkBound[i];
let upperValue = upperChunkBound[i];
let diff = upperValue - lowerValue;
let positiveOffset = positiveVertex[i] * diff;
// console.log(
// normalValue, lowerValue, upperValue, diff, positiveOffset,
// positiveVertexDistanceToOrigin, negativeVertexDistanceToOrigin);
positiveVertexDistanceToOrigin += normalValue * (lowerValue + positiveOffset);
negativeVertexDistanceToOrigin += normalValue * (lowerValue + diff - positiveOffset);
}
if (DEBUG_CHUNK_INTERSECTIONS) {
console.log(` planeNormal = ${planeNormal}`);
console.log(
' {positive,negative}VertexDistanceToOrigin: ', positiveVertexDistanceToOrigin,
negativeVertexDistanceToOrigin, planeDistanceToOrigin);
console.log(
' intersectsPlane:', negativeVertexDistanceToOrigin, planeDistanceToOrigin,
positiveVertexDistanceToOrigin);
}
if (positiveVertexDistanceToOrigin < planeDistanceToOrigin) {
return false;
}
return negativeVertexDistanceToOrigin <= planeDistanceToOrigin;
}
function populateVisibleSources() {
fullyVisibleSources.length = 0;
partiallyVisibleSources.length = 0;
for (let source of visibleSources.keys()) {
let spec = source.spec;
let result = compareBounds(
lowerChunkBound, upperChunkBound, spec.lowerChunkBound, spec.upperChunkBound);
if (DEBUG_CHUNK_INTERSECTIONS) {
console.log(
`Comparing source bounds lowerBound=${vec3.str(lowerChunkBound)}, ` +
`upperBound=${vec3.str(upperChunkBound)}, ` +
`lowerChunkBound=${vec3.str(spec.lowerChunkBound)}, ` +
`upperChunkBound=${vec3.str(spec.upperChunkBound)}, ` +
`got ${BoundsComparisonResult[result]}`,
spec, source);
}
switch (result) {
case BoundsComparisonResult.FULLY_INSIDE:
fullyVisibleSources.push(source);
break;
case BoundsComparisonResult.PARTIALLY_INSIDE:
partiallyVisibleSources.push(source);
break;
}
}
}
populateVisibleSources();
let partiallyVisibleSourcesLength = partiallyVisibleSources.length;
// Mutates lowerBound and upperBound while running, but leaves them the
// same once finished.
function checkBounds(nextSplitDim: number) {
if (DEBUG_CHUNK_INTERSECTIONS) {
console.log(
`chunk bounds: ${lowerChunkBound} ${upperChunkBound} ` +
`fullyVisible: ${fullyVisibleSources} partiallyVisible: ` +
`${partiallyVisibleSources.slice(0, partiallyVisibleSourcesLength)}`);
}
if (fullyVisibleSources.length === 0 && partiallyVisibleSourcesLength === 0) {
if (DEBUG_CHUNK_INTERSECTIONS) {
console.log(' no visible sources');
}
return;
}
if (DEBUG_CHUNK_INTERSECTIONS) {
console.log(
`Check bounds: [ ${vec3.str(lowerChunkBound)}, ${vec3.str(upperChunkBound)} ]`);
}
var volume = 1;
for (let i = 0; i < 3; ++i) {
volume *= Math.max(0, upperChunkBound[i] - lowerChunkBound[i]);
}
if (volume === 0) {
if (DEBUG_CHUNK_INTERSECTIONS) {
console.log(' volume == 0');
}
return;
}
if (!intersectsPlane()) {
if (DEBUG_CHUNK_INTERSECTIONS) {
console.log(' doesn\'t intersect plane');
}
return;
}
if (DEBUG_CHUNK_INTERSECTIONS) {
console.log(
'Within bounds: [' + vec3.str(lowerChunkBound) + ', ' +
vec3.str(upperChunkBound) + ']');
}
if (volume === 1) {
addChunk(chunkLayout, layoutObject, lowerChunkBound, fullyVisibleSources);
return;
}
var dimLower: number, dimUpper: number, diff: number;
while (true) {
dimLower = lowerChunkBound[nextSplitDim];
dimUpper = upperChunkBound[nextSplitDim];
diff = dimUpper - dimLower;
if (diff === 1) {
nextSplitDim = (nextSplitDim + 1) % 3;
} else {
break;
}
}
let splitPoint = dimLower + Math.floor(0.5 * diff);
let newNextSplitDim = (nextSplitDim + 1) % 3;
let fullyVisibleSourcesLength = fullyVisibleSources.length;
upperChunkBound[nextSplitDim] = splitPoint;
let oldPartiallyVisibleSourcesLength = partiallyVisibleSourcesLength;
function adjustSources() {
partiallyVisibleSourcesLength = partitionArray(
partiallyVisibleSources, 0, oldPartiallyVisibleSourcesLength, source => {
let spec = source.spec;
let result = compareBounds(
lowerChunkBound, upperChunkBound, spec.lowerChunkBound,
spec.upperChunkBound);
switch (result) {
case BoundsComparisonResult.PARTIALLY_INSIDE:
return true;
case BoundsComparisonResult.FULLY_INSIDE:
fullyVisibleSources.push(source);
default:
return false;
}
});
}
adjustSources();
checkBounds(newNextSplitDim);
// Truncate list of fully visible sources.
fullyVisibleSources.length = fullyVisibleSourcesLength;
// Restore partiallyVisibleSources.
partiallyVisibleSourcesLength = oldPartiallyVisibleSourcesLength;
upperChunkBound[nextSplitDim] = dimUpper;
lowerChunkBound[nextSplitDim] = splitPoint;
adjustSources();
checkBounds(newNextSplitDim);
lowerChunkBound[nextSplitDim] = dimLower;
// Truncate list of fully visible sources.
fullyVisibleSources.length = fullyVisibleSourcesLength;
// Restore partiallyVisibleSources.
partiallyVisibleSourcesLength = oldPartiallyVisibleSourcesLength;
}
checkBounds(0);
};
for (const [curChunkLayout, curVisibleSources] of this.visibleChunkLayouts) {
computeChunksForLayout(curChunkLayout, curVisibleSources);
}
}
}
/**
* By default, choose a chunk size with at most 2^18 = 262144 voxels.
*/
export const DEFAULT_MAX_VOXELS_PER_CHUNK_LOG2 = 18;
/**
* Specifies common options for getNearIsotropicBlockSize and getTwoDimensionalBlockSize.
*/
export interface BaseChunkLayoutOptions {
/**
* Number of channels.
*/
numChannels: number;
/**
* Voxel size in nanometers.
*/
voxelSize: vec3;
/**
* This, together with upperVoxelBound, specifies the total volume dimensions, which serves as a
* bound on the maximum chunk size. If not specified, defaults to (0, 0, 0).
*/
lowerVoxelBound?: vec3;
/**
* Upper voxel bound. If not specified, the total volume dimensions are not used to bound the
* chunk size.
*/
upperVoxelBound?: vec3;
/**
* Base 2 logarithm of the maximum number of voxels per chunk. Defaults to
* DEFAULT_MAX_VOXELS_PER_CHUNK_LOG2.
*/
maxVoxelsPerChunkLog2?: number;
/**
* Specifies an optional transform from local spatial coordinates to global coordinates.
*/
transform?: mat4;
}
export interface GetNearIsotropicBlockSizeOptions extends BaseChunkLayoutOptions {
maxBlockSize?: vec3;
}
/**
* Determines a near-isotropic (in global spatial coordinates) block size. All dimensions will be
* powers of 2, and will not exceed upperVoxelBound - lowerVoxelBound. The total number of voxels
* will not exceed maxVoxelsPerChunkLog2.
*/
export function getNearIsotropicBlockSize(options: GetNearIsotropicBlockSizeOptions) {
let {
voxelSize,
lowerVoxelBound = kZeroVec,
upperVoxelBound,
maxVoxelsPerChunkLog2 = DEFAULT_MAX_VOXELS_PER_CHUNK_LOG2,
transform = identityMat4,
maxBlockSize = kInfinityVec,
} = options;
maxVoxelsPerChunkLog2 -= Math.log2(options.numChannels);
// Adjust voxelSize by effective scaling factor.
let temp = effectiveScalingFactorFromMat4(vec3.create(), transform);
voxelSize = vec3.multiply(temp, temp, voxelSize);
let chunkDataSize = vec3.fromValues(1, 1, 1);
let maxChunkDataSize: vec3;
if (upperVoxelBound === undefined) {
maxChunkDataSize = maxBlockSize;
} else {
maxChunkDataSize = vec3.create();
for (let i = 0; i < 3; ++i) {
maxChunkDataSize[i] =
Math.pow(2, Math.floor(Math.log2(upperVoxelBound[i] - lowerVoxelBound[i])));
}
vec3.min(maxChunkDataSize, maxChunkDataSize, maxBlockSize);
}
// Determine the dimension in which chunkDataSize should be increased. This is the smallest
// dimension (in nanometers) that is < maxChunkDataSize (in voxels).
//
// Returns -1 if there is no such dimension.
function findNextDimension() {
let minSize = Infinity;
let minDimension = -1;
for (let i = 0; i < 3; ++i) {
if (chunkDataSize[i] >= maxChunkDataSize[i]) {
continue;
}
let size = chunkDataSize[i] * voxelSize[i];
if (size < minSize) {
minSize = size;
minDimension = i;
}
}
return minDimension;
}
for (let i = 0; i < maxVoxelsPerChunkLog2; ++i) {
let nextDim = findNextDimension();
if (nextDim === -1) {
break;
}
chunkDataSize[nextDim] *= 2;
}
return chunkDataSize;
}
/**
* Computes a 3-d block size that has depth 1 in flatDimension and is near-isotropic (in nanometers)
* in the other two dimensions. The remaining options are the same as for
* getNearIsotropicBlockSize.
*/
export function getTwoDimensionalBlockSize(options: {flatDimension: number}&
BaseChunkLayoutOptions) {
let {
lowerVoxelBound = kZeroVec,
upperVoxelBound = kInfinityVec,
flatDimension,
voxelSize,
maxVoxelsPerChunkLog2,
transform
} = options;
vec3.subtract(tempVec3, upperVoxelBound, lowerVoxelBound);
tempVec3[flatDimension] = 1;
return getNearIsotropicBlockSize({
voxelSize,
upperVoxelBound: tempVec3,
maxVoxelsPerChunkLog2,
transform,
numChannels: options.numChannels
});
}
/**
* Returns an array of [xy, xz, yz] 2-dimensional block sizes.
*/
export function getTwoDimensionalBlockSizes(options: BaseChunkLayoutOptions) {
let chunkDataSizes = new Array<vec3>();
for (let i = 0; i < 3; ++i) {
chunkDataSizes[i] = getTwoDimensionalBlockSize({
numChannels: options.numChannels,
flatDimension: i,
voxelSize: options.voxelSize,
lowerVoxelBound: options.lowerVoxelBound,
upperVoxelBound: options.upperVoxelBound,
maxVoxelsPerChunkLog2: options.maxVoxelsPerChunkLog2,
transform: options.transform,
});
}
return chunkDataSizes;
}
export enum ChunkLayoutPreference {
/**
* Indicates that isotropic chunks are desired.
*/
ISOTROPIC = 0,
/**
* Indicates that 2-D chunks are desired.
*/
FLAT = 1,
}
export interface SliceViewSourceOptions {
/**
* Additional transform applied after the transform specified by the data source for transforming
* from local to global coordinates.
*/
transform?: mat4;
}
export function getCombinedTransform(transform: mat4|undefined, options: {transform?: mat4}) {
let additionalTransform = options.transform;
if (additionalTransform === undefined) {
if (transform === undefined) {
return identityMat4;
}
return transform;
}
if (transform === undefined) {
return additionalTransform;
}
return mat4.multiply(mat4.create(), additionalTransform, transform);
}
/**
* Specifies parameters for getChunkDataSizes.
*/
export interface ChunkLayoutOptions {
/**
* Chunk sizes in voxels.
*/
chunkDataSizes?: vec3[];
/**
* Preferred chunk layout, which determines chunk sizes to use if chunkDataSizes is not
* specified.
*/
chunkLayoutPreference?: ChunkLayoutPreference;
}
export function getChunkDataSizes(options: ChunkLayoutOptions&BaseChunkLayoutOptions) {
if (options.chunkDataSizes !== undefined) {
return options.chunkDataSizes;
}
const {chunkLayoutPreference = ChunkLayoutPreference.ISOTROPIC} = options;
switch (chunkLayoutPreference) {
case ChunkLayoutPreference.ISOTROPIC:
return [getNearIsotropicBlockSize(options)];
case ChunkLayoutPreference.FLAT:
let chunkDataSizes = getTwoDimensionalBlockSizes(options);
chunkDataSizes.push(getNearIsotropicBlockSize(options));
return chunkDataSizes;
}
throw new Error(`Invalid chunk layout preference: ${chunkLayoutPreference}.`);
}
/**
* Generic specification for SliceView chunks specifying a layout and voxel size.
*/
export abstract class SliceViewChunkSpecification {
chunkLayout: ChunkLayout;
voxelSize: vec3;