define(["./when-cbf8cd21","./Check-35e1a91d","./Math-e66fad2a","./Cartesian2-44433f55","./Matrix3-146a3f23","./IntersectionTests-caa0ba1a","./createTaskProcessorWorker","./GeographicTilingScheme-835dd561"],function(_,e,t,y,i,d,a,h){"use strict";var T=0,u=1,f=2;function n(e){this.level=e.level,this.x=e.x,this.y=e.y,this.rectangle=e.rectangle}function r(e,t,i){if(this._samplesPerTile=_.defaultValue(i.samplesPerTile,150),this._templateUrl=i.urlTemplate,this._minLevel=_.defaultValue(i.minLevel,0),this._maxLevel=i.maxLevel,this._numTilesXAtMinLevel=i.numTilesXAtMinLevel,this._numTilesYAtMinLevel=i.numTilesYAtMinLevel,this._dataType=i.datatype,this._rectangle=new y.Rectangle(i.boundingBox.west,i.boundingBox.south,i.boundingBox.east,i.boundingBox.north),this._tilingScheme=new h.GeographicTilingScheme({ellipsoid:y.Ellipsoid.WGS84,rectangle:this._rectangle,numberOfLevelZeroTilesX:i.numTilesXAtMinLevel,numberOfLevelZeroTilesY:i.numTilesYAtMinLevel}),this._avaliablesByLevel=[],i.avaliables&&0<i.avaliables.length)for(var a=new y.Rectangle,r=new y.Rectangle,n=0;n<i.avaliables.length;++n){var l=i.avaliables[n],s=l.level;_.defined(this._avaliablesByLevel[s])||(this._avaliablesByLevel[s]=[]),this._tilingScheme.tileXYToRectangle(l.minX,l.minY,s,a),this._tilingScheme.tileXYToRectangle(l.maxX,l.maxY,s,r),this._avaliablesByLevel[s].push(new y.Rectangle(a.west,r.south,r.east,a.north))}this._layerId=e,this._blackList=[],this._manager=t,this._bufferMap=new Map}n.prototype.toString=function(){return this.level+"_"+this.x+"_"+this.y};var l=new y.Cartesian2;function s(){this._layers=[],this._index=0,this._maxBufferIndexArrayLength=500,this._bufferIndexArray=[]}function o(e,t){return e.timestamp<t.timestamp?-1:e.timestamp>t.timestamp?1:0}r.prototype.getTileKey=function(e,t){var i=this._tilingScheme.positionToTileXY(e,t,l),a=parseInt(i.x),r=parseInt(i.y);return new n({level:t,x:a,y:r,rectangle:this._tilingScheme.tileXYToRectangle(a,r,t)})},r.prototype.getElevationAtTile=function(e,t){var i=t.toString();if(function(e,t){for(var i=e.length;i--;)if(e[i]===t)return 1}(this._blackList,i))return!1;var a,r,n=this._bufferMap.get(i);if(n?(r=(new Date).getTime(),this._manager.updateBufferIndex(this._layerId,i,r)):void 0!==(a=function(e,t){var i=new XMLHttpRequest;i.open("GET",e,!1),i.overrideMimeType("text/plain; charset=x-user-defined"),i.send(null);var a=i.getResponseHeader("Content-Type");if(a&&-1!==a.indexOf("null"))return null;if(200===i.status&&i.responseText&&!(i.responseText.length<100)){for(var r=i.responseText,n=r.length,l=new ArrayBuffer(n),s=new Uint8Array(l),h=0;h<n;++h){var o=255&r.charCodeAt(h);s[h]=o}return new(_.defined(t)&&"float32"===t.toLowerCase()?Float32Array:Int16Array)(l)}}(this._templateUrl.replace("{TileMatrix}",t.level).replace("{TileRow}",t.y).replace("{TileCol}",t.x),this._dataType))&&(a?(n=a,this._bufferMap.set(i,n),r=(new Date).getTime(),this._manager.insertBufferIndex(this._layerId,i,r)):this._blackList.push(i)),!n)return!1;var l=(this._samplesPerTile-1)*(e.longitude-t.rectangle.west)/y.Rectangle.computeWidth(t.rectangle),s=(this._samplesPerTile-1)*(t.rectangle.north-e.latitude)/y.Rectangle.computeHeight(t.rectangle),h=Math.floor(l),o=Math.min(h+1,this._samplesPerTile-1),u=Math.floor(s),f=Math.min(u+1,this._samplesPerTile-1),g=n[u*this._samplesPerTile+h],p=n[u*this._samplesPerTile+o],c=n[f*this._samplesPerTile+h],m=n[f*this._samplesPerTile+o],v=l-h,d=s-u;return e.height=(g*(1-v)+p*v)*(1-d)+(c*(1-v)+m*v)*d,!0},r.prototype.getElevation=function(e,t,i){var a=T;if(!y.Rectangle.contains(this._rectangle,e))return a;for(var r=this._minLevel,n=this._samplesPerTile/(y.Rectangle.computeWidth(this._rectangle)/(this._numTilesXAtMinLevel*Math.pow(2,r)));r<=this._maxLevel;++r){if(t<=n){a=f;break}n*=2}if(r>this._maxLevel){if(n<=i)return T;r=this._maxLevel,a=u}for(var l=!1,s=this._avaliablesByLevel[r],h=0;h<s.length;++h)if(y.Rectangle.contains(s[h],e)){l=!0;break}if(!l)return T;var o=this.getTileKey(e,r);return this.getElevationAtTile(e,o)?a:T},r.prototype.removeBufferData=function(e){this._bufferMap.delete(e)},s.prototype.addLayer=function(e){var t=new r(this._index++,this,e);this._layers.push(t)},s.prototype.getElevation=function(e,t){for(var i=T,a=0;a<this._layers.length;++a){var r=this._layers[a].getElevation(e,t,0);if(r===f)return r;r===u&&(i=r)}return i},s.prototype.insertBufferIndex=function(e,t,i){var a,r=this._bufferIndexArray,n=this._maxBufferIndexArrayLength;r.length===n&&(a=r.shift(),this.removeBufferData(a)),r.push({layerId:e,tileKey:t,timestamp:i})},s.prototype.removeBufferData=function(e){for(var t=this._layers,i=t.length,a=0;a<i;a++){var r=t[a];if(r._layerId===e.layerId){r.removeBufferData(e.tileKey);break}}},s.prototype.updateBufferIndex=function(e,t,i){for(var a=this._bufferIndexArray,r=a.length,n=0;n<r;n++){var l=a[n];if(l.layerId===e&&l.tileKey===t){l.timestamp=i,a.sort(o);break}}};var x=new s;return a(function(e,t){if("addLayer"===e.method)return x.addLayer(e.layer),{succeed:!0};for(var i=new y.Rectangle(e.west,e.south,e.east,e.north),a=e.constraintRegions,r=d.IntersectionTests.intersetRectangleWithConstraintRegion(i,a),n=e.heightmapWidth,l=n/y.Rectangle.computeWidth(i),s=y.Rectangle.computeWidth(i)/(n-1),h=y.Rectangle.computeHeight(i)/(n-1),o=new Float32Array(n*n),u=new y.Cartographic,f=0,g=0,p=0,f=0;f<n;f++)for(g=0;g<n;g++){u.longitude=i.west+g*s,u.latitude=i.north-f*h,u.height=0;var c,m,v=!1;-1===r||-1!==(c=d.IntersectionTests.isCartographicInConstraintRegion(u,r,a))&&("ERASE"===a[c].eraseType?o[p++]=a[c].height:"OFFSET"===a[c].eraseType?(x.getElevation(u,l)===T&&(u.height=0),o[p++]=u.height+a[c].height):"SMOOTH"===a[c].eraseType&&((m=d.IntersectionTests.interoplateCartographicWhenSmooth(u,a[c]))<a[c].height&&(m=a[c].height),o[p++]=m),v=!0),v||(x.getElevation(u,l)===T&&(u.height=0),o[p++]=u.height)}return t.push(o.buffer),{heightmap:o.buffer}})});