All files / engine/Source/Scene Geometry3DTileContent.js

92.61% Statements 163/176
88.88% Branches 120/135
90% Functions 27/30
92.61% Lines 163/176

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                      128x 128x 128x 128x   128x   128x 128x         128x 128x   128x   128x     1x     2479x           1815x           1690x 1690x               125x 125x               125x           125x               1816x                             466x           126x           251x                       1x     126x           704x           1x     1x           126x 120x 120x                       126x 126x 126x 126x   126x   5x 5x             126x   4x   4x             126x   4x   4x             126x   3x   3x               126x         126x         126x 1x           125x       125x 115x 115x 58x 58x 64x     115x 21x 21x 27x     115x 21x 21x 27x     115x 21x 21x 27x         125x               1x     128x   128x 128x 128x   128x 128x 1x       127x   127x 127x   127x         127x 127x   127x 1x         126x 126x 126x 126x 126x 126x   126x         126x   126x         126x       126x           125x         125x   125x                       126x 126x 126x 126x     126x   126x             126x   126x       126x     126x         126x   125x                     125x   62x 62x             125x   24x 24x             125x   24x 24x             125x   24x 24x             125x                             125x             330x 330x 68x 68x 68x   68x       1x 1x     1x   116x 116x 3x               113x 113x     1x 5x 5x       1x 217x 217x 217x       1x 2156x 2156x 2156x 2156x     2156x 1940x 1940x       1x       1x       1x 125x 125x 125x      
import Cartesian3 from "../Core/Cartesian3.js";
import defined from "../Core/defined.js";
import destroyObject from "../Core/destroyObject.js";
import DeveloperError from "../Core/DeveloperError.js";
import getJsonFromTypedArray from "../Core/getJsonFromTypedArray.js";
import Matrix4 from "../Core/Matrix4.js";
import RuntimeError from "../Core/RuntimeError.js";
import Cesium3DTileBatchTable from "./Cesium3DTileBatchTable.js";
import Vector3DTileGeometry from "./Vector3DTileGeometry.js";
 
/**
 * <p>
 * Implements the {@link Cesium3DTileContent} interface.
 * </p>
 *
 * @alias Geometry3DTileContent
 * @constructor
 *
 * @private
 */
function Geometry3DTileContent(
  tileset,
  tile,
  resource,
  arrayBuffer,
  byteOffset,
) {
  this._tileset = tileset;
  this._tile = tile;
  this._resource = resource;
  this._geometries = undefined;
 
  this._metadata = undefined;
 
  this._batchTable = undefined;
  this._features = undefined;
 
  /**
   * Part of the {@link Cesium3DTileContent} interface.
   */
  this.featurePropertiesDirty = false;
  this._group = undefined;
 
  this._ready = false;
 
  initialize(this, arrayBuffer, byteOffset);
}
 
Object.defineProperties(Geometry3DTileContent.prototype, {
  featuresLength: {
    get: function () {
      return defined(this._batchTable) ? this._batchTable.featuresLength : 0;
    },
  },
 
  pointsLength: {
    get: function () {
      return 0;
    },
  },
 
  trianglesLength: {
    get: function () {
      Eif (defined(this._geometries)) {
        return this._geometries.trianglesLength;
      }
      return 0;
    },
  },
 
  geometryByteLength: {
    get: function () {
      Eif (defined(this._geometries)) {
        return this._geometries.geometryByteLength;
      }
      return 0;
    },
  },
 
  texturesByteLength: {
    get: function () {
      return 0;
    },
  },
 
  batchTableByteLength: {
    get: function () {
      return defined(this._batchTable)
        ? this._batchTable.batchTableByteLength
        : 0;
    },
  },
 
  innerContents: {
    get: function () {
      return undefined;
    },
  },
 
  /**
   * Returns true when the tile's content is ready to render; otherwise false
   *
   * @memberof Geometry3DTileContent.prototype
   *
   * @type {boolean}
   * @readonly
   * @private
   */
  ready: {
    get: function () {
      return this._ready;
    },
  },
 
  tileset: {
    get: function () {
      return this._tileset;
    },
  },
 
  tile: {
    get: function () {
      return this._tile;
    },
  },
 
  url: {
    get: function () {
      return this._resource.getUrlComponent(true);
    },
  },
 
  metadata: {
    get: function () {
      return this._metadata;
    },
    set: function (value) {
      this._metadata = value;
    },
  },
 
  batchTable: {
    get: function () {
      return this._batchTable;
    },
  },
 
  group: {
    get: function () {
      return this._group;
    },
    set: function (value) {
      this._group = value;
    },
  },
});
 
function createColorChangedCallback(content) {
  return function (batchId, color) {
    Eif (defined(content._geometries)) {
      content._geometries.updateCommands(batchId, color);
    }
  };
}
 
function getBatchIds(featureTableJson, featureTableBinary) {
  let boxBatchIds;
  let cylinderBatchIds;
  let ellipsoidBatchIds;
  let sphereBatchIds;
  let i;
 
  const numberOfBoxes = featureTableJson.BOXES_LENGTH ?? 0;
  const numberOfCylinders = featureTableJson.CYLINDERS_LENGTH ?? 0;
  const numberOfEllipsoids = featureTableJson.ELLIPSOIDS_LENGTH ?? 0;
  const numberOfSpheres = featureTableJson.SPHERES_LENGTH ?? 0;
 
  if (numberOfBoxes > 0 && defined(featureTableJson.BOX_BATCH_IDS)) {
    const boxBatchIdsByteOffset =
      featureTableBinary.byteOffset + featureTableJson.BOX_BATCH_IDS.byteOffset;
    boxBatchIds = new Uint16Array(
      featureTableBinary.buffer,
      boxBatchIdsByteOffset,
      numberOfBoxes,
    );
  }
 
  if (numberOfCylinders > 0 && defined(featureTableJson.CYLINDER_BATCH_IDS)) {
    const cylinderBatchIdsByteOffset =
      featureTableBinary.byteOffset +
      featureTableJson.CYLINDER_BATCH_IDS.byteOffset;
    cylinderBatchIds = new Uint16Array(
      featureTableBinary.buffer,
      cylinderBatchIdsByteOffset,
      numberOfCylinders,
    );
  }
 
  if (numberOfEllipsoids > 0 && defined(featureTableJson.ELLIPSOID_BATCH_IDS)) {
    const ellipsoidBatchIdsByteOffset =
      featureTableBinary.byteOffset +
      featureTableJson.ELLIPSOID_BATCH_IDS.byteOffset;
    ellipsoidBatchIds = new Uint16Array(
      featureTableBinary.buffer,
      ellipsoidBatchIdsByteOffset,
      numberOfEllipsoids,
    );
  }
 
  if (numberOfSpheres > 0 && defined(featureTableJson.SPHERE_BATCH_IDS)) {
    const sphereBatchIdsByteOffset =
      featureTableBinary.byteOffset +
      featureTableJson.SPHERE_BATCH_IDS.byteOffset;
    sphereBatchIds = new Uint16Array(
      featureTableBinary.buffer,
      sphereBatchIdsByteOffset,
      numberOfSpheres,
    );
  }
 
  const atLeastOneDefined =
    defined(boxBatchIds) ||
    defined(cylinderBatchIds) ||
    defined(ellipsoidBatchIds) ||
    defined(sphereBatchIds);
  const atLeastOneUndefined =
    (numberOfBoxes > 0 && !defined(boxBatchIds)) ||
    (numberOfCylinders > 0 && !defined(cylinderBatchIds)) ||
    (numberOfEllipsoids > 0 && !defined(ellipsoidBatchIds)) ||
    (numberOfSpheres > 0 && !defined(sphereBatchIds));
 
  if (atLeastOneDefined && atLeastOneUndefined) {
    throw new RuntimeError(
      "If one group of batch ids is defined, then all batch ids must be defined",
    );
  }
 
  const allUndefinedBatchIds =
    !defined(boxBatchIds) &&
    !defined(cylinderBatchIds) &&
    !defined(ellipsoidBatchIds) &&
    !defined(sphereBatchIds);
  if (allUndefinedBatchIds) {
    let id = 0;
    if (!defined(boxBatchIds) && numberOfBoxes > 0) {
      boxBatchIds = new Uint16Array(numberOfBoxes);
      for (i = 0; i < numberOfBoxes; ++i) {
        boxBatchIds[i] = id++;
      }
    }
    if (!defined(cylinderBatchIds) && numberOfCylinders > 0) {
      cylinderBatchIds = new Uint16Array(numberOfCylinders);
      for (i = 0; i < numberOfCylinders; ++i) {
        cylinderBatchIds[i] = id++;
      }
    }
    if (!defined(ellipsoidBatchIds) && numberOfEllipsoids > 0) {
      ellipsoidBatchIds = new Uint16Array(numberOfEllipsoids);
      for (i = 0; i < numberOfEllipsoids; ++i) {
        ellipsoidBatchIds[i] = id++;
      }
    }
    if (!defined(sphereBatchIds) && numberOfSpheres > 0) {
      sphereBatchIds = new Uint16Array(numberOfSpheres);
      for (i = 0; i < numberOfSpheres; ++i) {
        sphereBatchIds[i] = id++;
      }
    }
  }
 
  return {
    boxes: boxBatchIds,
    cylinders: cylinderBatchIds,
    ellipsoids: ellipsoidBatchIds,
    spheres: sphereBatchIds,
  };
}
 
const sizeOfUint32 = Uint32Array.BYTES_PER_ELEMENT;
 
function initialize(content, arrayBuffer, byteOffset) {
  byteOffset = byteOffset ?? 0;
 
  const uint8Array = new Uint8Array(arrayBuffer);
  const view = new DataView(arrayBuffer);
  byteOffset += sizeOfUint32; // Skip magic number
 
  const version = view.getUint32(byteOffset, true);
  if (version !== 1) {
    throw new RuntimeError(
      `Only Geometry tile version 1 is supported.  Version ${version} is not.`,
    );
  }
  byteOffset += sizeOfUint32;
 
  const byteLength = view.getUint32(byteOffset, true);
  byteOffset += sizeOfUint32;
 
  Iif (byteLength === 0) {
    content._ready = true;
    return;
  }
 
  const featureTableJSONByteLength = view.getUint32(byteOffset, true);
  byteOffset += sizeOfUint32;
 
  if (featureTableJSONByteLength === 0) {
    throw new RuntimeError(
      "Feature table must have a byte length greater than zero",
    );
  }
 
  const featureTableBinaryByteLength = view.getUint32(byteOffset, true);
  byteOffset += sizeOfUint32;
  const batchTableJSONByteLength = view.getUint32(byteOffset, true);
  byteOffset += sizeOfUint32;
  const batchTableBinaryByteLength = view.getUint32(byteOffset, true);
  byteOffset += sizeOfUint32;
 
  const featureTableJson = getJsonFromTypedArray(
    uint8Array,
    byteOffset,
    featureTableJSONByteLength,
  );
  byteOffset += featureTableJSONByteLength;
 
  const featureTableBinary = new Uint8Array(
    arrayBuffer,
    byteOffset,
    featureTableBinaryByteLength,
  );
  byteOffset += featureTableBinaryByteLength;
 
  let batchTableJson;
  let batchTableBinary;
  if (batchTableJSONByteLength > 0) {
    // PERFORMANCE_IDEA: is it possible to allocate this on-demand?  Perhaps keep the
    // arraybuffer/string compressed in memory and then decompress it when it is first accessed.
    //
    // We could also make another request for it, but that would make the property set/get
    // API async, and would double the number of numbers in some cases.
    batchTableJson = getJsonFromTypedArray(
      uint8Array,
      byteOffset,
      batchTableJSONByteLength,
    );
    byteOffset += batchTableJSONByteLength;
 
    Iif (batchTableBinaryByteLength > 0) {
      // Has a batch table binary
      batchTableBinary = new Uint8Array(
        arrayBuffer,
        byteOffset,
        batchTableBinaryByteLength,
      );
      // Copy the batchTableBinary section and let the underlying ArrayBuffer be freed
      batchTableBinary = new Uint8Array(batchTableBinary);
    }
  }
 
  const numberOfBoxes = featureTableJson.BOXES_LENGTH ?? 0;
  const numberOfCylinders = featureTableJson.CYLINDERS_LENGTH ?? 0;
  const numberOfEllipsoids = featureTableJson.ELLIPSOIDS_LENGTH ?? 0;
  const numberOfSpheres = featureTableJson.SPHERES_LENGTH ?? 0;
 
  const totalPrimitives =
    numberOfBoxes + numberOfCylinders + numberOfEllipsoids + numberOfSpheres;
 
  const batchTable = new Cesium3DTileBatchTable(
    content,
    totalPrimitives,
    batchTableJson,
    batchTableBinary,
    createColorChangedCallback(content),
  );
  content._batchTable = batchTable;
 
  Iif (totalPrimitives === 0) {
    return;
  }
 
  const modelMatrix = content.tile.computedTransform;
 
  let center;
  Iif (defined(featureTableJson.RTC_CENTER)) {
    center = Cartesian3.unpack(featureTableJson.RTC_CENTER);
    Matrix4.multiplyByPoint(modelMatrix, center, center);
  }
 
  const batchIds = getBatchIds(featureTableJson, featureTableBinary);
 
  Eif (
    numberOfBoxes > 0 ||
    numberOfCylinders > 0 ||
    numberOfEllipsoids > 0 ||
    numberOfSpheres > 0
  ) {
    let boxes;
    let cylinders;
    let ellipsoids;
    let spheres;
 
    if (numberOfBoxes > 0) {
      const boxesByteOffset =
        featureTableBinary.byteOffset + featureTableJson.BOXES.byteOffset;
      boxes = new Float32Array(
        featureTableBinary.buffer,
        boxesByteOffset,
        Vector3DTileGeometry.packedBoxLength * numberOfBoxes,
      );
    }
 
    if (numberOfCylinders > 0) {
      const cylindersByteOffset =
        featureTableBinary.byteOffset + featureTableJson.CYLINDERS.byteOffset;
      cylinders = new Float32Array(
        featureTableBinary.buffer,
        cylindersByteOffset,
        Vector3DTileGeometry.packedCylinderLength * numberOfCylinders,
      );
    }
 
    if (numberOfEllipsoids > 0) {
      const ellipsoidsByteOffset =
        featureTableBinary.byteOffset + featureTableJson.ELLIPSOIDS.byteOffset;
      ellipsoids = new Float32Array(
        featureTableBinary.buffer,
        ellipsoidsByteOffset,
        Vector3DTileGeometry.packedEllipsoidLength * numberOfEllipsoids,
      );
    }
 
    if (numberOfSpheres > 0) {
      const spheresByteOffset =
        featureTableBinary.byteOffset + featureTableJson.SPHERES.byteOffset;
      spheres = new Float32Array(
        featureTableBinary.buffer,
        spheresByteOffset,
        Vector3DTileGeometry.packedSphereLength * numberOfSpheres,
      );
    }
 
    content._geometries = new Vector3DTileGeometry({
      boxes: boxes,
      boxBatchIds: batchIds.boxes,
      cylinders: cylinders,
      cylinderBatchIds: batchIds.cylinders,
      ellipsoids: ellipsoids,
      ellipsoidBatchIds: batchIds.ellipsoids,
      spheres: spheres,
      sphereBatchIds: batchIds.spheres,
      center: center,
      modelMatrix: modelMatrix,
      batchTable: batchTable,
      boundingVolume: content.tile.boundingVolume.boundingVolume,
    });
 
    return content;
  }
 
  return Promise.resolve(content);
}
 
function createFeatures(content) {
  const featuresLength = content.featuresLength;
  if (!defined(content._features) && featuresLength > 0) {
    const features = new Array(featuresLength);
    Eif (defined(content._geometries)) {
      content._geometries.createFeatures(content, features);
    }
    content._features = features;
  }
}
 
Geometry3DTileContent.prototype.hasProperty = function (batchId, name) {
  return this._batchTable.hasProperty(batchId, name);
};
 
Geometry3DTileContent.prototype.getFeature = function (batchId) {
  //>>includeStart('debug', pragmas.debug);
  const featuresLength = this.featuresLength;
  if (!defined(batchId) || batchId < 0 || batchId >= featuresLength) {
    throw new DeveloperError(
      `batchId is required and between zero and featuresLength - 1 (${
        featuresLength - 1
      }).`,
    );
  }
  //>>includeEnd('debug');
 
  createFeatures(this);
  return this._features[batchId];
};
 
Geometry3DTileContent.prototype.applyDebugSettings = function (enabled, color) {
  Eif (defined(this._geometries)) {
    this._geometries.applyDebugSettings(enabled, color);
  }
};
 
Geometry3DTileContent.prototype.applyStyle = function (style) {
  createFeatures(this);
  Eif (defined(this._geometries)) {
    this._geometries.applyStyle(style, this._features);
  }
};
 
Geometry3DTileContent.prototype.update = function (tileset, frameState) {
  Eif (defined(this._geometries)) {
    this._geometries.classificationType = this._tileset.classificationType;
    this._geometries.debugWireframe = this._tileset.debugWireframe;
    this._geometries.update(frameState);
  }
 
  if (defined(this._batchTable) && this._geometries.ready) {
    this._batchTable.update(tileset, frameState);
    this._ready = true;
  }
};
 
Geometry3DTileContent.prototype.pick = function (ray, frameState, result) {
  return undefined;
};
 
Geometry3DTileContent.prototype.isDestroyed = function () {
  return false;
};
 
Geometry3DTileContent.prototype.destroy = function () {
  this._geometries = this._geometries && this._geometries.destroy();
  this._batchTable = this._batchTable && this._batchTable.destroy();
  return destroyObject(this);
};
export default Geometry3DTileContent;