-
Notifications
You must be signed in to change notification settings - Fork 48
/
ContentBoundingVolumes.ts
365 lines (343 loc) · 12.7 KB
/
ContentBoundingVolumes.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
import { Node, PropertyType, Scene } from "@gltf-transform/core";
import { ContentDataTypeRegistry } from "../../base";
import { ContentDataTypes } from "../../base";
import { B3dmFeatureTable } from "../../structure";
import { BatchTable } from "../../structure";
import { I3dmFeatureTable } from "../../structure";
import { PntsFeatureTable } from "../../structure";
import { TileFormats } from "../../tilesets";
import { TileFormatError } from "../../tilesets";
import { TileTableData } from "../../tilesets";
import { TileTableDataI3dm } from "../../tilesets";
import { GltfTransform } from "../contentProcessing/GltfTransform";
import { PntsPointClouds } from "../pointClouds/PntsPointClouds";
import { BoundingVolumes } from "./BoundingVolumes";
import { Loggers } from "../../base";
const logger = Loggers.get("tilesetProcessing");
/**
* Methods to compute bounding volumes from tile content data.
*
* (The term "bounding volume box" refers to the 12-element
* number arrays that are the `boundingVolume.box`)
*/
export class ContentBoundingVolumes {
/**
* Computes the bounding volume box from the given content data.
*
* The exact set of content data types that is supported by this
* method is not specified (but it should include GLB and the
* common 'legacy' content types).
*
* @param contentUri - The content URI
* @param data - The content data
* @param externalGlbResolver - The resolver for external GLBs in I3DMs
* @returns The bounding volume box, or undefined if no bounding
* volume box could be computed from the given content.
* @throws Error if the I3DM referred to a GLB that could not be
* resolved
*/
static async computeContentDataBoundingVolumeBox(
contentUri: string,
data: Buffer,
externalGlbResolver: (glbUri: string) => Promise<Buffer | undefined>
): Promise<number[] | undefined> {
const contentDataType = await ContentDataTypeRegistry.findType(
contentUri,
data
);
if (contentDataType === ContentDataTypes.CONTENT_TYPE_GLB) {
return ContentBoundingVolumes.computeBoundingVolumeBoxFromGlb(data);
} else if (contentDataType === ContentDataTypes.CONTENT_TYPE_PNTS) {
return ContentBoundingVolumes.computeBoundingBoxFromPnts(data);
} else if (contentDataType === ContentDataTypes.CONTENT_TYPE_B3DM) {
return ContentBoundingVolumes.computeBoundingVolumeBoxFromB3dm(data);
} else if (contentDataType === ContentDataTypes.CONTENT_TYPE_I3DM) {
return ContentBoundingVolumes.computeBoundingVolumeBoxFromI3dm(
data,
externalGlbResolver
);
} else if (contentDataType === ContentDataTypes.CONTENT_TYPE_CMPT) {
return ContentBoundingVolumes.computeBoundingVolumeBoxFromCmpt(
data,
externalGlbResolver
);
}
return undefined;
}
/**
* Computes the bounding volume box of the given PNTS data
*
* @param pntsBuffer - The PNTS data buffer
* @returns A promise to the bounding volume box
*/
private static async computeBoundingBoxFromPnts(
pntsBuffer: Buffer
): Promise<number[]> {
// Read the tile data from the input data
const tileData = TileFormats.readTileData(pntsBuffer);
const batchTable = tileData.batchTable.json as BatchTable;
const featureTable = tileData.featureTable.json as PntsFeatureTable;
const featureTableBinary = tileData.featureTable.binary;
// Create a `ReadablePointCloud` that allows accessing
// the PNTS data
const pntsPointCloud = await PntsPointClouds.create(
featureTable,
featureTableBinary,
batchTable
);
// Compute the positions, taking the global position
// into account
const globalPosition = pntsPointCloud.getGlobalPosition() ?? [0, 0, 0];
const localPositions = pntsPointCloud.getPositions();
const positions: number[][] = [];
for (const localPosition of localPositions) {
const position: number[] = [
localPosition[0] + globalPosition[0],
localPosition[1] + globalPosition[1],
localPosition[2] + globalPosition[2],
];
positions.push(position);
}
return BoundingVolumes.createBoundingVolumeBoxFromPoints(positions);
}
/**
* Computes the bounding volume box of the given B3DM data
*
* @param b3dmBuffer - The B3DM data buffer
* @returns A promise to the bounding volume box
*/
private static async computeBoundingVolumeBoxFromB3dm(
b3dmBuffer: Buffer
): Promise<number[]> {
// Compute the bounding volume box from the payload (GLB data)
const tileData = TileFormats.readTileData(b3dmBuffer);
const glbBuffer = tileData.payload;
const gltfBoundingVolumeBox =
await ContentBoundingVolumes.computeBoundingVolumeBoxFromGlb(glbBuffer);
// If the feature table defines an `RTC_CENTER`, then
// translate the bounding volume box by this amount
const featureTable = tileData.featureTable.json as B3dmFeatureTable;
if (featureTable.RTC_CENTER) {
const featureTableBinary = tileData.featureTable.binary;
const rtcCenter = TileTableData.obtainRtcCenter(
featureTable.RTC_CENTER,
featureTableBinary
);
const b3dmBoundingVolumeBox = BoundingVolumes.translateBoundingVolumeBox(
gltfBoundingVolumeBox,
rtcCenter
);
return b3dmBoundingVolumeBox;
}
return gltfBoundingVolumeBox;
}
/**
* Computes the bounding volume box of the given I3DM data
*
* @param i3dmBuffer - The I3DM data buffer
* @param externalGlbResolver - The resolver for external GLB data from I3DMs
* @returns A promise to the bounding volume box
* @throws Error if the I3DM referred to a GLB that could not be
* resolved
*/
private static async computeBoundingVolumeBoxFromI3dm(
i3dmBuffer: Buffer,
externalGlbResolver: (glbUri: string) => Promise<Buffer | undefined>
): Promise<number[]> {
// Obtain the GLB buffer for the tile data. With `gltfFormat===1`, it
// is stored directly as the payload. Otherwise (with `gltfFormat===0`)
// the payload is a URI that has to be resolved.
const tileData = TileFormats.readTileData(i3dmBuffer);
const glbBuffer = await TileFormats.obtainGlbPayload(
tileData,
externalGlbResolver
);
if (!glbBuffer) {
throw new TileFormatError(
`Could not resolve external GLB from I3DM file`
);
}
// Note: The approach here is to compute the bounding volume box
// corners of the GLB, and then compute a bounding volume box
// from these corners when they are transformed with each
// instancing transform. A tighter bounding volume MIGHT be
// achievable by computing the bounding volume from all
// points of the GLB after the transformation. But this would
// be VERY inefficient for MANY instances (and defeat the
// purpose of instancing itself...)
// Compute the bounding volume box from the payload (GLB data)
const gltfBoundingVolumeBox =
await ContentBoundingVolumes.computeBoundingVolumeBoxFromGlb(glbBuffer);
const gltfCorners = BoundingVolumes.computeBoundingVolumeBoxCorners(
gltfBoundingVolumeBox
);
// Compute the instance matrices of the I3DM data
const featureTable = tileData.featureTable.json as I3dmFeatureTable;
const featureTableBinary = tileData.featureTable.binary;
const numInstances = featureTable.INSTANCES_LENGTH;
const instanceMatrices = TileTableDataI3dm.createInstanceMatrices(
featureTable,
featureTableBinary,
numInstances
);
// Compute the set of all corner points of the glTF bounding volume box
// when they are transformed with the instancing transforms.
const transformedCorners: number[][] = [];
for (const matrix of instanceMatrices) {
for (const gltfCorner of gltfCorners) {
const transformedCorner = ContentBoundingVolumes.transformPoint3D(
matrix,
gltfCorner
);
transformedCorners.push(transformedCorner);
}
}
return BoundingVolumes.createBoundingVolumeBoxFromPoints(
transformedCorners
);
}
/**
* Computes the bounding volume box of the given CMPT data
*
* @param cmptBuffer - The CMPT data buffer
* @returns A promise to the bounding volume box
*/
private static async computeBoundingVolumeBoxFromCmpt(
cmptBuffer: Buffer,
externalGlbResolver: (glbUri: string) => Promise<Buffer | undefined>
): Promise<number[]> {
const compositeTileData = TileFormats.readCompositeTileData(cmptBuffer);
const buffers = compositeTileData.innerTileBuffers;
const innerBoundingVolumeBoxes: number[][] = [];
for (const buffer of buffers) {
const innerBoundingVolumeBox =
await ContentBoundingVolumes.computeContentDataBoundingVolumeBox(
"[inner tile of CMPT]",
buffer,
externalGlbResolver
);
if (innerBoundingVolumeBox) {
innerBoundingVolumeBoxes.push(innerBoundingVolumeBox);
}
}
return BoundingVolumes.computeUnionBoundingVolumeBox(
innerBoundingVolumeBoxes
);
}
/**
* Computes the bounding volume box of the given glTF asset.
*
* This will compute the bounding volume box of the default scene
* (or the first scene of the asset). If there is no scene,
* then a warning will be printed, and a unit cube bounding
* box will be returned.
*
* @param glbBuffer - The buffer containing GLB data
* @returns A promise to the bounding volume box
*/
private static async computeBoundingVolumeBoxFromGlb(
glbBuffer: Buffer
): Promise<number[]> {
return ContentBoundingVolumes.computeOrientedBoundingVolumeBoxFromGlb(
glbBuffer
);
}
/**
* Computes the bounding volume box of the given glTF asset.
*
* This will compute the bounding volume box of the default scene
* (or the first scene of the asset). If there is no scene,
* then a warning will be printed, and a unit cube bounding
* box will be returned.
*
* @param glbBuffer - The buffer containing GLB data
* @returns A promise to the bounding volume box
*/
static async computeOrientedBoundingVolumeBoxFromGlb(
glbBuffer: Buffer
): Promise<number[]> {
const io = await GltfTransform.getIO();
const document = await io.readBinary(glbBuffer);
const root = document.getRoot();
let scene = root.getDefaultScene();
if (!scene) {
const scenes = root.listScenes();
if (scenes.length > 0) {
scene = scenes[0];
}
}
if (scene) {
const positions: number[][] = [];
ContentBoundingVolumes.processVertexPositions(scene, (p: number[]) => {
// take y-up-to-z-up into account
const q = [p[0], -p[2], p[1]];
positions.push(q);
});
return BoundingVolumes.createBoundingVolumeBoxFromPoints(positions);
}
logger.warn("No scenes found in glTF - using unit bounding box");
return BoundingVolumes.createUnitCubeBoundingVolumeBox();
}
private static processVertexPositions(
root: Node | Scene,
consumer: (p: number[]) => void
): void {
const position = [0, 0, 0];
const rootNodes =
root.propertyType === PropertyType.NODE ? [root] : root.listChildren();
for (const rootNode of rootNodes) {
rootNode.traverse((node: Node) => {
const mesh = node.getMesh();
if (!mesh) {
return;
}
const worldMatrix = node.getWorldMatrix();
const primitives = mesh.listPrimitives();
for (const primitive of primitives) {
const positionAccessor = primitive.getAttribute("POSITION");
if (!positionAccessor) {
continue;
}
for (let i = 0; i < positionAccessor.getCount(); i++) {
positionAccessor.getElement(i, position);
ContentBoundingVolumes.transformPoint3D(
worldMatrix,
position,
position
);
consumer(position);
}
}
});
}
}
/**
* Transforms the given 3D point with the given 4x4 matrix, writes
* the result into the given target, and returns it. If no target
* is given, then a new point will be created and returned.
*
* @param matrix - The 4x4 matrix
* @param point - The 3D point
* @param target - The target
* @returns The result
*/
private static transformPoint3D(
matrix: number[],
point: number[],
target?: number[]
): number[] {
const px = point[0];
const py = point[1];
const pz = point[2];
const x = matrix[0] * px + matrix[4] * py + matrix[8] * pz + matrix[12];
const y = matrix[1] * px + matrix[5] * py + matrix[9] * pz + matrix[13];
const z = matrix[2] * px + matrix[6] * py + matrix[10] * pz + matrix[14];
if (!target) {
return [x, y, z];
}
target[0] = x;
target[1] = y;
target[2] = z;
return target;
}
}