UNPKG

itowns

Version:

A JS/WebGL framework for 3D geospatial data visualization

92 lines (77 loc) 2.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.computeMinMaxElevation = computeMinMaxElevation; exports.checkNodeElevationTextureValidity = checkNodeElevationTextureValidity; exports.insertSignificantValuesFromParent = insertSignificantValuesFromParent; /** * Calculates the minimum maximum elevation of xbil buffer * * @param {number} buffer The buffer to parse * @param {number} width The buffer's width * @param {number} height The buffer's height * @param {THREE.Vector4} pitch The pitch, restrict zone to parse * @return {Object} The minimum maximum elevation. */ function computeMinMaxElevation(buffer, width, height, pitch) { var min = 1000000; var max = -1000000; if (!buffer) { // Return null values means there's no elevation values. // They can't be determined. // Don't return 0 because the result will be wrong return { min: null, max: null }; } var sizeX = pitch ? Math.floor(pitch.z * width) : buffer.length; var sizeY = pitch ? Math.floor(pitch.z * height) : 1; var xs = pitch ? Math.floor(pitch.x * width) : 0; var ys = pitch ? Math.floor(pitch.y * height) : 0; var inc = pitch ? Math.max(Math.floor(sizeX / 8), 2) : 16; for (var y = ys; y < ys + sizeY; y += inc) { var pit = y * (width || 0); for (var x = xs; x < xs + sizeX; x += inc) { var val = buffer[pit + x]; if (val > -10) { max = Math.max(max, val); min = Math.min(min, val); } } } if (max === -1000000 || min === 1000000) { // Return null values means the elevation values are incoherent // They can't be determined. // Don't return 0, -1000000 or 1000000 because the result will be wrong return { min: null, max: null }; } return { min: min, max: max }; } // We check if the elevation texture has some significant values through corners function checkNodeElevationTextureValidity(data, noDataValue) { var l = data.length; return data[0] > noDataValue && data[l - 1] > noDataValue && data[Math.sqrt(l) - 1] > noDataValue && data[l - Math.sqrt(l)] > noDataValue; } function getIndiceWithPitch(i, pitch, w) { // Return corresponding indice in parent tile using pitch // normalized var currentY = Math.floor(i / w) / w; // normalized var newX = pitch.x + i % w / w * pitch.z; var newY = pitch.y + currentY * pitch.w; var newIndice = Math.floor(newY * w) * w + Math.floor(newX * w); return newIndice; } // This function replaces noDataValue by significant values from parent texture function insertSignificantValuesFromParent(data, dataParent, noDataValue, pitch) { for (var i = 0, l = data.length; i < l; ++i) { if (data[i] === noDataValue) { data[i] = dataParent[getIndiceWithPitch(i, pitch, 256)]; } } }