Cesium3DTilesetHeatmap.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import Color from '../Core/Color.js';
  2. import defined from '../Core/defined.js';
  3. import JulianDate from '../Core/JulianDate.js';
  4. import CesiumMath from '../Core/Math.js';
  5. /**
  6. * A heatmap colorizer in a {@link Cesium3DTileset}. A tileset can colorize its visible tiles in a heatmap style.
  7. *
  8. * @alias Cesium3DTilesetHeatmap
  9. * @constructor
  10. * @private
  11. */
  12. function Cesium3DTilesetHeatmap(tilePropertyName) {
  13. /**
  14. * The tile variable to track for heatmap colorization.
  15. * Tile's will be colorized relative to the other visible tile's values for this variable.
  16. *
  17. * @type {String}
  18. */
  19. this.tilePropertyName = tilePropertyName;
  20. // Members that are updated every time a tile is colorized
  21. this._minimum = Number.MAX_VALUE;
  22. this._maximum = -Number.MAX_VALUE;
  23. // Members that are updated once every frame
  24. this._previousMinimum = Number.MAX_VALUE;
  25. this._previousMaximum = -Number.MAX_VALUE;
  26. // If defined uses a reference minimum maximum to colorize by instead of using last frames minimum maximum of rendered tiles.
  27. // For example, the _loadTimestamp can get a better colorization using setReferenceMinimumMaximum in order to take accurate colored timing diffs of various scenes.
  28. this._referenceMinimum = {};
  29. this._referenceMaximum = {};
  30. }
  31. /**
  32. * Convert to a usable heatmap value (i.e. a number). Ensures that tile values that aren't stored as numbers can be used for colorization.
  33. */
  34. function getHeatmapValue(tileValue, tilePropertyName) {
  35. var value;
  36. if (tilePropertyName === '_loadTimestamp') {
  37. value = JulianDate.toDate(tileValue).getTime();
  38. } else {
  39. value = tileValue;
  40. }
  41. return value;
  42. }
  43. /**
  44. * Sets the reference minimum and maximum for the variable name. Converted to numbers before they are stored.
  45. *
  46. * @param {Object} minimum The minimum reference value.
  47. * @param {Object} maximum The maximum reference value.
  48. * @param {String} tilePropertyName The tile variable that will use these reference values when it is colorized.
  49. */
  50. Cesium3DTilesetHeatmap.prototype.setReferenceMinimumMaximum = function(minimum, maximum, tilePropertyName) {
  51. this._referenceMinimum[tilePropertyName] = getHeatmapValue(minimum, tilePropertyName);
  52. this._referenceMaximum[tilePropertyName] = getHeatmapValue(maximum, tilePropertyName);
  53. };
  54. function getHeatmapValueAndUpdateMinimumMaximum(heatmap, tile) {
  55. var tilePropertyName = heatmap.tilePropertyName;
  56. if (defined(tilePropertyName)) {
  57. var heatmapValue = getHeatmapValue(tile[tilePropertyName], tilePropertyName);
  58. if (!defined(heatmapValue)) {
  59. heatmap.tilePropertyName = undefined;
  60. return heatmapValue;
  61. }
  62. heatmap._maximum = Math.max(heatmapValue, heatmap._maximum);
  63. heatmap._minimum = Math.min(heatmapValue, heatmap._minimum);
  64. return heatmapValue;
  65. }
  66. }
  67. var heatmapColors = [new Color(0.100, 0.100, 0.100, 1), // Dark Gray
  68. new Color(0.153, 0.278, 0.878, 1), // Blue
  69. new Color(0.827, 0.231, 0.490, 1), // Pink
  70. new Color(0.827, 0.188, 0.220, 1), // Red
  71. new Color(1.000, 0.592, 0.259, 1), // Orange
  72. new Color(1.000, 0.843, 0.000, 1)]; // Yellow
  73. /**
  74. * Colorize the tile in heat map style based on where it lies within the minimum maximum window.
  75. * Heatmap colors are black, blue, pink, red, orange, yellow. 'Cold' or low numbers will be black and blue, 'Hot' or high numbers will be orange and yellow,
  76. * @param {Cesium3DTile} tile The tile to colorize relative to last frame's minimum and maximum values of all visible tiles.
  77. * @param {FrameState} frameState The frame state.
  78. */
  79. Cesium3DTilesetHeatmap.prototype.colorize = function (tile, frameState) {
  80. var tilePropertyName = this.tilePropertyName;
  81. if (!defined(tilePropertyName) || !tile.contentAvailable || tile._selectedFrame !== frameState.frameNumber) {
  82. return;
  83. }
  84. var heatmapValue = getHeatmapValueAndUpdateMinimumMaximum(this, tile);
  85. var minimum = this._previousMinimum;
  86. var maximum = this._previousMaximum;
  87. if (minimum === Number.MAX_VALUE || maximum === -Number.MAX_VALUE) {
  88. return;
  89. }
  90. // Shift the minimum maximum window down to 0
  91. var shiftedMax = (maximum - minimum) + CesiumMath.EPSILON7; // Prevent divide by 0
  92. var shiftedValue = CesiumMath.clamp(heatmapValue - minimum, 0.0, shiftedMax);
  93. // Get position between minimum and maximum and convert that to a position in the color array
  94. var zeroToOne = shiftedValue / shiftedMax;
  95. var lastIndex = heatmapColors.length - 1.0;
  96. var colorPosition = zeroToOne * lastIndex;
  97. // Take floor and ceil of the value to get the two colors to lerp between, lerp using the fractional portion
  98. var colorPositionFloor = Math.floor(colorPosition);
  99. var colorPositionCeil = Math.ceil(colorPosition);
  100. var t = colorPosition - colorPositionFloor;
  101. var colorZero = heatmapColors[colorPositionFloor];
  102. var colorOne = heatmapColors[colorPositionCeil];
  103. // Perform the lerp
  104. var finalColor = Color.clone(Color.WHITE);
  105. finalColor.red = CesiumMath.lerp(colorZero.red, colorOne.red, t);
  106. finalColor.green = CesiumMath.lerp(colorZero.green, colorOne.green, t);
  107. finalColor.blue = CesiumMath.lerp(colorZero.blue, colorOne.blue, t);
  108. tile._debugColor = finalColor;
  109. };
  110. /**
  111. * Resets the tracked minimum maximum values for heatmap colorization. Happens right before tileset traversal.
  112. */
  113. Cesium3DTilesetHeatmap.prototype.resetMinimumMaximum = function() {
  114. // For heat map colorization
  115. var tilePropertyName = this.tilePropertyName;
  116. if (defined(tilePropertyName)) {
  117. var referenceMinimum = this._referenceMinimum[tilePropertyName];
  118. var referenceMaximum = this._referenceMaximum[tilePropertyName];
  119. var useReference = defined(referenceMinimum) && defined(referenceMaximum);
  120. this._previousMinimum = useReference ? referenceMinimum : this._minimum;
  121. this._previousMaximum = useReference ? referenceMaximum : this._maximum;
  122. this._minimum = Number.MAX_VALUE;
  123. this._maximum = -Number.MAX_VALUE;
  124. }
  125. };
  126. export default Cesium3DTilesetHeatmap;