All files / engine/Source/DataSources PointVisualizer.js

92.21% Statements 154/167
75.64% Branches 59/78
100% Functions 8/8
92.07% Lines 151/164

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                            1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x     7x 7x 7x 7x 7x 7x 7x                         195x 1x   194x 1x       193x         193x 193x 193x 193x                   1x   777x 1x       776x 776x 776x 14x 14x 14x 14x 14x 14x           14x       14x 10x         10x   14x 4x 4x     10x       10x 10x 10x 1x         1x 1x 1x 1x 1x           1x     9x       6x         6x 6x 6x     10x 8x 8x 8x         8x         8x           8x           8x         8x         8x         8x         8x         2x 2x 2x 2x         2x         2x         2x         2x         2x   2x           2x           2x             2x                     2x 2x 2x                               2x 2x 2x 2x 2x   2x 2x 2x 2x             2x                       2x         776x                           1x   8x 1x   7x 1x       6x 6x       5x     1x 1x                       1x 1x               1x 1x           1x 187x       187x 187x 28x   187x     1x               251x 251x   251x 33x 33x 2x       251x 36x 36x 28x 5x     8x 8x       251x 2x 2x 2x         14x 5x 5x 3x 3x 3x   2x 2x 1x 1x          
import AssociativeArray from "../Core/AssociativeArray.js";
import Cartesian3 from "../Core/Cartesian3.js";
import Color from "../Core/Color.js";
import defined from "../Core/defined.js";
import destroyObject from "../Core/destroyObject.js";
import DeveloperError from "../Core/DeveloperError.js";
import DistanceDisplayCondition from "../Core/DistanceDisplayCondition.js";
import NearFarScalar from "../Core/NearFarScalar.js";
import createBillboardPointCallback from "../Scene/createBillboardPointCallback.js";
import HeightReference from "../Scene/HeightReference.js";
import BoundingSphereState from "./BoundingSphereState.js";
import Property from "./Property.js";
import SplitDirection from "../Scene/SplitDirection.js";
 
const defaultColor = Color.WHITE;
const defaultOutlineColor = Color.BLACK;
const defaultOutlineWidth = 0.0;
const defaultPixelSize = 1.0;
const defaultDisableDepthTestDistance = 0.0;
const defaultSplitDirection = SplitDirection.NONE;
 
const colorScratch = new Color();
const positionScratch = new Cartesian3();
const outlineColorScratch = new Color();
const scaleByDistanceScratch = new NearFarScalar();
const translucencyByDistanceScratch = new NearFarScalar();
const distanceDisplayConditionScratch = new DistanceDisplayCondition();
 
function EntityData(entity) {
  this.entity = entity;
  this.pointPrimitive = undefined;
  this.billboard = undefined;
  this.color = undefined;
  this.outlineColor = undefined;
  this.pixelSize = undefined;
  this.outlineWidth = undefined;
}
 
/**
 * A {@link Visualizer} which maps {@link Entity#point} to a {@link PointPrimitive}.
 * @alias PointVisualizer
 * @constructor
 *
 * @param {EntityCluster} entityCluster The entity cluster to manage the collection of billboards and optionally cluster with other entities.
 * @param {EntityCollection} entityCollection The entityCollection to visualize.
 */
function PointVisualizer(entityCluster, entityCollection) {
  //>>includeStart('debug', pragmas.debug);
  if (!defined(entityCluster)) {
    throw new DeveloperError("entityCluster is required.");
  }
  if (!defined(entityCollection)) {
    throw new DeveloperError("entityCollection is required.");
  }
  //>>includeEnd('debug');
 
  entityCollection.collectionChanged.addEventListener(
    PointVisualizer.prototype._onCollectionChanged,
    this,
  );
 
  this._cluster = entityCluster;
  this._entityCollection = entityCollection;
  this._items = new AssociativeArray();
  this._onCollectionChanged(entityCollection, entityCollection.values, [], []);
}
 
/**
 * Updates the primitives created by this visualizer to match their
 * Entity counterpart at the given time.
 *
 * @param {JulianDate} time The time to update to.
 * @returns {boolean} This function always returns true.
 */
PointVisualizer.prototype.update = function (time) {
  //>>includeStart('debug', pragmas.debug);
  if (!defined(time)) {
    throw new DeveloperError("time is required.");
  }
  //>>includeEnd('debug');
 
  const items = this._items.values;
  const cluster = this._cluster;
  for (let i = 0, len = items.length; i < len; i++) {
    const item = items[i];
    const entity = item.entity;
    const pointGraphics = entity._point;
    let pointPrimitive = item.pointPrimitive;
    let billboard = item.billboard;
    const heightReference = Property.getValueOrDefault(
      pointGraphics._heightReference,
      time,
      HeightReference.NONE,
    );
    let show =
      entity.isShowing &&
      entity.isAvailable(time) &&
      Property.getValueOrDefault(pointGraphics._show, time, true);
    let position;
    if (show) {
      position = Property.getValueOrUndefined(
        entity._position,
        time,
        positionScratch,
      );
      show = defined(position);
    }
    if (!show) {
      returnPrimitive(item, entity, cluster);
      continue;
    }
 
    Iif (!Property.isConstant(entity._position)) {
      cluster._clusterDirty = true;
    }
 
    let needsRedraw = false;
    let updateClamping = false;
    if (heightReference !== HeightReference.NONE && !defined(billboard)) {
      Iif (defined(pointPrimitive)) {
        returnPrimitive(item, entity, cluster);
        pointPrimitive = undefined;
      }
 
      billboard = cluster.getBillboard(entity);
      billboard.id = entity;
      billboard.image = undefined;
      item.billboard = billboard;
      needsRedraw = true;
 
      // If this new billboard happens to have a position and height reference that match our new values,
      // billboard._updateClamping will not be called automatically. That's a problem because the clamped
      // height may be based on different terrain than is now loaded. So we'll manually call
      // _updateClamping below.
      updateClamping =
        Cartesian3.equals(billboard.position, position) &&
        billboard.heightReference === heightReference;
    } else if (
      heightReference === HeightReference.NONE &&
      !defined(pointPrimitive)
    ) {
      Iif (defined(billboard)) {
        returnPrimitive(item, entity, cluster);
        billboard = undefined;
      }
 
      pointPrimitive = cluster.getPoint(entity);
      pointPrimitive.id = entity;
      item.pointPrimitive = pointPrimitive;
    }
 
    if (defined(pointPrimitive)) {
      pointPrimitive.show = true;
      pointPrimitive.position = position;
      pointPrimitive.scaleByDistance = Property.getValueOrUndefined(
        pointGraphics._scaleByDistance,
        time,
        scaleByDistanceScratch,
      );
      pointPrimitive.translucencyByDistance = Property.getValueOrUndefined(
        pointGraphics._translucencyByDistance,
        time,
        translucencyByDistanceScratch,
      );
      pointPrimitive.color = Property.getValueOrDefault(
        pointGraphics._color,
        time,
        defaultColor,
        colorScratch,
      );
      pointPrimitive.outlineColor = Property.getValueOrDefault(
        pointGraphics._outlineColor,
        time,
        defaultOutlineColor,
        outlineColorScratch,
      );
      pointPrimitive.outlineWidth = Property.getValueOrDefault(
        pointGraphics._outlineWidth,
        time,
        defaultOutlineWidth,
      );
      pointPrimitive.pixelSize = Property.getValueOrDefault(
        pointGraphics._pixelSize,
        time,
        defaultPixelSize,
      );
      pointPrimitive.distanceDisplayCondition = Property.getValueOrUndefined(
        pointGraphics._distanceDisplayCondition,
        time,
        distanceDisplayConditionScratch,
      );
      pointPrimitive.disableDepthTestDistance = Property.getValueOrDefault(
        pointGraphics._disableDepthTestDistance,
        time,
        defaultDisableDepthTestDistance,
      );
      pointPrimitive.splitDirection = Property.getValueOrDefault(
        pointGraphics._splitDirection,
        time,
        defaultSplitDirection,
      );
    } else Eif (defined(billboard)) {
      billboard.show = true;
      billboard.position = position;
      billboard.scaleByDistance = Property.getValueOrUndefined(
        pointGraphics._scaleByDistance,
        time,
        scaleByDistanceScratch,
      );
      billboard.translucencyByDistance = Property.getValueOrUndefined(
        pointGraphics._translucencyByDistance,
        time,
        translucencyByDistanceScratch,
      );
      billboard.distanceDisplayCondition = Property.getValueOrUndefined(
        pointGraphics._distanceDisplayCondition,
        time,
        distanceDisplayConditionScratch,
      );
      billboard.disableDepthTestDistance = Property.getValueOrDefault(
        pointGraphics._disableDepthTestDistance,
        time,
        defaultDisableDepthTestDistance,
      );
      billboard.splitDirection = Property.getValueOrDefault(
        pointGraphics._splitDirection,
        time,
        defaultSplitDirection,
      );
      billboard.heightReference = heightReference;
 
      const newColor = Property.getValueOrDefault(
        pointGraphics._color,
        time,
        defaultColor,
        colorScratch,
      );
      const newOutlineColor = Property.getValueOrDefault(
        pointGraphics._outlineColor,
        time,
        defaultOutlineColor,
        outlineColorScratch,
      );
      const newOutlineWidth = Math.round(
        Property.getValueOrDefault(
          pointGraphics._outlineWidth,
          time,
          defaultOutlineWidth,
        ),
      );
      let newPixelSize = Math.max(
        1,
        Math.round(
          Property.getValueOrDefault(
            pointGraphics._pixelSize,
            time,
            defaultPixelSize,
          ),
        ),
      );
 
      if (newOutlineWidth > 0) {
        billboard.scale = 1.0;
        needsRedraw =
          needsRedraw || //
          newOutlineWidth !== item.outlineWidth || //
          newPixelSize !== item.pixelSize || //
          !Color.equals(newColor, item.color) || //
          !Color.equals(newOutlineColor, item.outlineColor);
      } else E{
        billboard.scale = newPixelSize / 50.0;
        newPixelSize = 50.0;
        needsRedraw =
          needsRedraw || //
          newOutlineWidth !== item.outlineWidth || //
          !Color.equals(newColor, item.color) || //
          !Color.equals(newOutlineColor, item.outlineColor);
      }
 
      Eif (needsRedraw) {
        item.color = Color.clone(newColor, item.color);
        item.outlineColor = Color.clone(newOutlineColor, item.outlineColor);
        item.pixelSize = newPixelSize;
        item.outlineWidth = newOutlineWidth;
 
        const centerAlpha = newColor.alpha;
        const cssColor = newColor.toCssColorString();
        const cssOutlineColor = newOutlineColor.toCssColorString();
        const textureId = JSON.stringify([
          cssColor,
          newPixelSize,
          cssOutlineColor,
          newOutlineWidth,
        ]);
 
        billboard.setImage(
          textureId,
          createBillboardPointCallback(
            centerAlpha,
            cssColor,
            cssOutlineColor,
            newOutlineWidth,
            newPixelSize,
          ),
        );
      }
 
      Iif (updateClamping) {
        billboard._updateClamping();
      }
    }
  }
  return true;
};
 
/**
 * Computes a bounding sphere which encloses the visualization produced for the specified entity.
 * The bounding sphere is in the fixed frame of the scene's globe.
 *
 * @param {Entity} entity The entity whose bounding sphere to compute.
 * @param {BoundingSphere} result The bounding sphere onto which to store the result.
 * @returns {BoundingSphereState} BoundingSphereState.DONE if the result contains the bounding sphere,
 *                       BoundingSphereState.PENDING if the result is still being computed, or
 *                       BoundingSphereState.FAILED if the entity has no visualization in the current scene.
 * @private
 */
PointVisualizer.prototype.getBoundingSphere = function (entity, result) {
  //>>includeStart('debug', pragmas.debug);
  if (!defined(entity)) {
    throw new DeveloperError("entity is required.");
  }
  if (!defined(result)) {
    throw new DeveloperError("result is required.");
  }
  //>>includeEnd('debug');
 
  const item = this._items.get(entity.id);
  if (
    !defined(item) ||
    !(defined(item.pointPrimitive) || defined(item.billboard))
  ) {
    return BoundingSphereState.FAILED;
  }
 
  if (defined(item.pointPrimitive)) {
    result.center = Cartesian3.clone(
      item.pointPrimitive.position,
      result.center,
    );
  } else E{
    const billboard = item.billboard;
    if (!defined(billboard._clampedPosition)) {
      return BoundingSphereState.PENDING;
    }
    result.center = Cartesian3.clone(billboard._clampedPosition, result.center);
  }
 
  result.radius = 0;
  return BoundingSphereState.DONE;
};
 
/**
 * Returns true if this object was destroyed; otherwise, false.
 *
 * @returns {boolean} True if this object was destroyed; otherwise, false.
 */
PointVisualizer.prototype.isDestroyed = function () {
  return false;
};
 
/**
 * Removes and destroys all primitives created by this instance.
 */
PointVisualizer.prototype.destroy = function () {
  this._entityCollection.collectionChanged.removeEventListener(
    PointVisualizer.prototype._onCollectionChanged,
    this,
  );
  const entities = this._entityCollection.values;
  for (let i = 0; i < entities.length; i++) {
    this._cluster.removePoint(entities[i]);
  }
  return destroyObject(this);
};
 
PointVisualizer.prototype._onCollectionChanged = function (
  entityCollection,
  added,
  removed,
  changed,
) {
  let i;
  let entity;
  const items = this._items;
  const cluster = this._cluster;
 
  for (i = added.length - 1; i > -1; i--) {
    entity = added[i];
    if (defined(entity._point) && defined(entity._position)) {
      items.set(entity.id, new EntityData(entity));
    }
  }
 
  for (i = changed.length - 1; i > -1; i--) {
    entity = changed[i];
    if (defined(entity._point) && defined(entity._position)) {
      if (!items.contains(entity.id)) {
        items.set(entity.id, new EntityData(entity));
      }
    } else {
      returnPrimitive(items.get(entity.id), entity, cluster);
      items.remove(entity.id);
    }
  }
 
  for (i = removed.length - 1; i > -1; i--) {
    entity = removed[i];
    returnPrimitive(items.get(entity.id), entity, cluster);
    items.remove(entity.id);
  }
};
 
function returnPrimitive(item, entity, cluster) {
  if (defined(item)) {
    const pointPrimitive = item.pointPrimitive;
    if (defined(pointPrimitive)) {
      item.pointPrimitive = undefined;
      cluster.removePoint(entity);
      return;
    }
    const billboard = item.billboard;
    if (defined(billboard)) {
      item.billboard = undefined;
      cluster.removeBillboard(entity);
    }
  }
}
export default PointVisualizer;