/
ViewStateHelper.ts
160 lines (137 loc) · 5.18 KB
/
ViewStateHelper.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
/*!
* Copyright 2021 Cognite AS
*/
import * as THREE from 'three';
import { CogniteCadModel } from '@reveal/cad-model';
import { Cognite3DViewer } from '../public/migration/Cognite3DViewer';
import {
fromSerializableNodeAppearance,
NodeCollectionDeserializer,
toSerializableNodeAppearance
} from '@reveal/cad-styling';
import { CameraManager } from '@reveal/camera-manager';
import { CogniteClient } from '@cognite/sdk';
import { SerializableNodeAppearance } from '@reveal/cad-styling/src/NodeAppearance';
export type ViewerState = {
camera?: {
position: { x: number; y: number; z: number };
target: { x: number; y: number; z: number };
};
models?: ModelState[];
clippingPlanes?: ClippingPlanesState[];
};
export type ClippingPlanesState = {
nx: number;
ny: number;
nz: number;
constant: number;
};
export type ModelState = {
defaultNodeAppearance: SerializableNodeAppearance;
modelId: number;
revisionId: number;
styledSets: { token: string; state: any; options?: any; appearance: SerializableNodeAppearance }[];
};
export class ViewStateHelper {
private readonly _viewer: Cognite3DViewer;
private readonly _cdfClient: CogniteClient;
private get _cameraManager(): CameraManager {
return this._viewer.cameraManager;
}
constructor(viewer: Cognite3DViewer, cdfClient: CogniteClient) {
this._viewer = viewer;
this._cdfClient = cdfClient;
}
public getCurrentState(): ViewerState {
const { position: cameraPosition, target: cameraTarget } = this._cameraManager.getCameraState();
const modelStates = this.getModelsState();
const clippingPlanesState = this.getClippingPlanesState();
return {
camera: {
position: cameraPosition,
target: cameraTarget
},
models: modelStates,
clippingPlanes: clippingPlanesState
};
}
public async setState(viewerState: ViewerState): Promise<void> {
if (viewerState.camera !== undefined) {
this.setCameraFromState(viewerState.camera);
}
if (viewerState.models !== undefined) {
this.setModelState(viewerState.models);
}
if (viewerState.clippingPlanes !== undefined) {
this.setClippingPlanesState(viewerState.clippingPlanes);
}
}
private getModelsState(): ModelState[] {
return this._viewer.models
.filter(model => model instanceof CogniteCadModel)
.map(model => model as CogniteCadModel)
.map(model => {
const defaultNodeAppearance = toSerializableNodeAppearance(model.getDefaultNodeAppearance());
const modelId = model.modelId;
const revisionId = model.revisionId;
const styledCollections = model.styledNodeCollections.map(styledNodeCollection => {
const { nodeCollection, appearance } = styledNodeCollection;
return { ...nodeCollection.serialize(), appearance: toSerializableNodeAppearance(appearance) };
});
return {
defaultNodeAppearance: defaultNodeAppearance,
modelId,
revisionId,
styledSets: styledCollections
} as ModelState;
});
}
private getClippingPlanesState(): ClippingPlanesState[] {
return this._viewer
.getGlobalClippingPlanes()
.map(p => ({ nx: p.normal.x, ny: p.normal.y, nz: p.normal.z, constant: p.constant }));
}
private setCameraFromState(cameraState: Exclude<ViewerState['camera'], undefined>) {
const camPos = cameraState.position;
const camTarget = cameraState.target;
this._cameraManager.setCameraState({
position: new THREE.Vector3(camPos.x, camPos.y, camPos.z),
target: new THREE.Vector3(camTarget.x, camTarget.y, camTarget.z)
});
}
private async setModelState(modelsState: ModelState[]) {
const cadModels = this._viewer.models
.filter(model => model instanceof CogniteCadModel)
.map(model => model as CogniteCadModel);
await Promise.all(
modelsState
.map(state => {
const model = cadModels.find(model => model.modelId == state.modelId && model.revisionId == state.revisionId);
if (model === undefined) {
throw new Error(
`Cannot apply model state. Model (modelId: ${state.modelId}, revisionId: ${state.revisionId}) has not been added to viewer.`
);
}
return { model: model, state: state };
})
.map(async modelState => {
const { model, state } = modelState;
model.setDefaultNodeAppearance(fromSerializableNodeAppearance(state.defaultNodeAppearance));
await Promise.all(
state.styledSets.map(async styleFilter => {
const nodeCollection = await NodeCollectionDeserializer.Instance.deserialize(this._cdfClient, model, {
token: styleFilter.token,
state: styleFilter.state,
options: styleFilter.options
});
model.assignStyledNodeCollection(nodeCollection, fromSerializableNodeAppearance(styleFilter.appearance));
})
);
})
);
}
private setClippingPlanesState(clippingPlanes: ClippingPlanesState[]) {
const planes = clippingPlanes.map(p => new THREE.Plane().setComponents(p.nx, p.ny, p.nz, p.constant));
this._viewer.setGlobalClippingPlanes(planes);
}
}