/
CdfNodeCollectionBase.ts
84 lines (71 loc) · 2.78 KB
/
CdfNodeCollectionBase.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
/*!
* Copyright 2022 Cognite AS
*/
import * as THREE from 'three';
import { ListResponse, Node3D } from '@cognite/sdk';
import { PopulateIndexSetFromPagedResponseHelper } from './PopulateIndexSetFromPagedResponseHelper';
import { AreaCollection } from './prioritized/AreaCollection';
import { EmptyAreaCollection } from './prioritized/EmptyAreaCollection';
import { NodeCollection } from './NodeCollection';
import { CdfModelNodeCollectionDataProvider } from './CdfModelNodeCollectionDataProvider';
import { IndexSet, NumericRange, toThreeBox3 } from '@reveal/utilities';
export abstract class CdfNodeCollectionBase extends NodeCollection {
private _indexSet = new IndexSet();
private _areas: AreaCollection = EmptyAreaCollection.instance();
private readonly _cadModelData: CdfModelNodeCollectionDataProvider;
private _fetchResultHelper: PopulateIndexSetFromPagedResponseHelper<Node3D> | undefined;
constructor(classToken: string, model: CdfModelNodeCollectionDataProvider) {
super(classToken);
this._cadModelData = model;
}
get isLoading(): boolean {
return this._fetchResultHelper !== undefined && this._fetchResultHelper.isLoading;
}
protected async updateCollectionFromResults(requests: Promise<ListResponse<Node3D[]>>[]): Promise<void> {
if (this._fetchResultHelper !== undefined) {
// Interrupt any ongoing operation to avoid fetching results unnecessary
this._fetchResultHelper.interrupt();
}
const totalModelTransformation = this._cadModelData
.getModelTransformation()
.clone()
.multiply(this._cadModelData.getCdfToDefaultModelTransformation());
const fetchResultHelper = new PopulateIndexSetFromPagedResponseHelper<Node3D>(
nodes => nodes.map(node => new NumericRange(node.treeIndex, node.subtreeSize)),
async nodes =>
nodes.map(node => {
const bounds = new THREE.Box3();
if (node.boundingBox !== undefined) {
toThreeBox3(node.boundingBox, bounds);
bounds.applyMatrix4(totalModelTransformation);
}
return bounds;
}),
() => this.notifyChanged()
);
this._fetchResultHelper = fetchResultHelper;
this._indexSet = fetchResultHelper.indexSet;
this._areas = fetchResultHelper.areas;
const pageOperations = requests.map(request => {
return fetchResultHelper.pageResults(request);
});
this.notifyChanged();
await Promise.all(pageOperations);
}
/**
* Clears the node collection and interrupts any ongoing operations.
*/
clear(): void {
if (this._fetchResultHelper !== undefined) {
this._fetchResultHelper.interrupt();
}
this._indexSet.clear();
this.notifyChanged();
}
getIndexSet(): IndexSet {
return this._indexSet;
}
getAreas(): AreaCollection {
return this._areas;
}
}