/**
 * Cesium - https://github.com/AnalyticalGraphicsInc/cesium
 *
 * Copyright 2011-2017 Cesium Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Columbus View (Pat. Pend.)
 *
 * Portions licensed separately.
 * See https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md for full licensing details.
 */
define(["./defined-2a4f2d00","./Check-e5651467","./freezeObject-a51e076f","./defaultValue-29c9b1af","./Math-7782f09e","./Cartesian2-ba70b51f","./defineProperties-c817531e","./Transforms-5119c07b","./RuntimeError-51c34ab4","./WebGLConstants-90dbfe2f","./ComponentDatatype-418b1c61","./when-1faa3867","./AttributeCompression-5601f533","./IntersectionTests-35b85442","./Plane-475170f0","./WebMercatorProjection-1ecca5ba","./createTaskProcessorWorker","./EllipsoidTangentPlane-0decb876","./OrientedBoundingBox-2b5c2949","./EllipsoidalOccluder-e6218e4e","./TerrainEncoding-114aa591"],function(Fe,e,t,n,We,Oe,i,Ve,Ye,a,r,o,s,u,h,ke,c,Le,Ue,He,je){"use strict";var De=Uint16Array.BYTES_PER_ELEMENT,Ge=Int32Array.BYTES_PER_ELEMENT,ze=Uint32Array.BYTES_PER_ELEMENT,qe=Float32Array.BYTES_PER_ELEMENT,Je=Float64Array.BYTES_PER_ELEMENT;function Ke(e,t,i){i=n.defaultValue(i,We.CesiumMath);for(var a=e.length,r=0;r<a;++r)if(i.equalsEpsilon(e[r],t,We.CesiumMath.EPSILON12))return r;return-1}var Qe=new Oe.Cartographic,Xe=new Oe.Cartesian3,Ze=new Oe.Cartesian3,$e=new Oe.Cartesian3,et=new Ve.Matrix4;function tt(e,t,i,a,r,n,o,s,u,h){for(var c=o.length,d=0;d<c;++d){var l=o[d],g=l.cartographic,m=l.index,p=e.length,I=g.longitude,f=g.latitude;f=We.CesiumMath.clamp(f,-We.CesiumMath.PI_OVER_TWO,We.CesiumMath.PI_OVER_TWO);var v=g.height-n.skirtHeight;n.hMin=Math.min(n.hMin,v),Oe.Cartographic.fromRadians(I,f,v,Qe),u&&(Qe.longitude+=s),u?d===c-1?Qe.latitude+=h:0===d&&(Qe.latitude-=h):Qe.latitude+=s;var E=n.ellipsoid.cartographicToCartesian(Qe);e.push(E),t.push(v),i.push(Oe.Cartesian2.clone(i[m])),0<a.length&&a.push(a[m]),Ve.Matrix4.multiplyByPoint(n.toENU,E,Xe);var T=n.minimum,C=n.maximum;Oe.Cartesian3.minimumByComponent(Xe,T,T),Oe.Cartesian3.maximumByComponent(Xe,C,C);var M=n.lastBorderPoint;if(Fe.defined(M)){var N=M.index;r.push(N,p-1,p,p,m,N)}n.lastBorderPoint=l}}return c(function(e,t){e.ellipsoid=Oe.Ellipsoid.clone(e.ellipsoid),e.rectangle=Oe.Rectangle.clone(e.rectangle);var i=function(e,t,i,a,r,n,o,s,u,h){var c,d,l,g,m,p;p=Fe.defined(a)?(c=a.west,d=a.south,l=a.east,g=a.north,m=a.width,a.height):(c=We.CesiumMath.toRadians(r.west),d=We.CesiumMath.toRadians(r.south),l=We.CesiumMath.toRadians(r.east),g=We.CesiumMath.toRadians(r.north),m=We.CesiumMath.toRadians(a.width),We.CesiumMath.toRadians(a.height));var I,f,v=[d,g],E=[c,l],T=Ve.Transforms.eastNorthUpToFixedFrame(t,i),C=Ve.Matrix4.inverseTransformation(T,et);s&&(I=ke.WebMercatorProjection.geodeticLatitudeToMercatorAngle(d),f=1/(ke.WebMercatorProjection.geodeticLatitudeToMercatorAngle(g)-I));var M=new DataView(e),N=Number.POSITIVE_INFINITY,x=Number.NEGATIVE_INFINITY,b=Ze;b.x=Number.POSITIVE_INFINITY,b.y=Number.POSITIVE_INFINITY,b.z=Number.POSITIVE_INFINITY;var P=$e;P.x=Number.NEGATIVE_INFINITY,P.y=Number.NEGATIVE_INFINITY,P.z=Number.NEGATIVE_INFINITY;var S,w,B=0,R=0,A=0;for(w=0;w<4;++w){var y=B;S=M.getUint32(y,!0),y+=ze;var _=We.CesiumMath.toRadians(180*M.getFloat64(y,!0));y+=Je,-1===Ke(E,_)&&E.push(_);var F=We.CesiumMath.toRadians(180*M.getFloat64(y,!0));y+=Je,-1===Ke(v,F)&&v.push(F),y+=2*Je;var W=M.getInt32(y,!0);y+=Ge,R+=W,W=M.getInt32(y,!0),A+=3*W,B+=S+ze}var O=[],V=[],Y=new Array(R),k=new Array(R),L=new Array(R),U=s?new Array(R):[],H=new Array(A),j=[],D=[],G=[],z=[],q=0,J=0;for(w=B=0;w<4;++w){S=M.getUint32(B,!0);var K=B+=ze,Q=We.CesiumMath.toRadians(180*M.getFloat64(B,!0));B+=Je;var X=We.CesiumMath.toRadians(180*M.getFloat64(B,!0));B+=Je;var Z=We.CesiumMath.toRadians(180*M.getFloat64(B,!0)),$=.5*Z;B+=Je;var ee=We.CesiumMath.toRadians(180*M.getFloat64(B,!0)),te=.5*ee;B+=Je;var ie=M.getInt32(B,!0);B+=Ge;var ae=M.getInt32(B,!0);B+=Ge,B+=Ge;for(var re=new Array(ie),ne=0;ne<ie;++ne){var oe=Q+M.getUint8(B++)*Z;Qe.longitude=oe;var se=X+M.getUint8(B++)*ee;Qe.latitude=se;var ue=M.getFloat32(B,!0);if(B+=qe,0!==ue&&ue<h&&(ue*=-Math.pow(2,u)),ue*=6371010*n,Qe.height=ue,-1!==Ke(E,oe)||-1!==Ke(v,se)){var he=Ke(O,Qe,Oe.Cartographic);if(-1!==he){re[ne]=V[he];continue}O.push(Oe.Cartographic.clone(Qe)),V.push(q)}re[ne]=q,Math.abs(oe-c)<$?j.push({index:q,cartographic:Oe.Cartographic.clone(Qe)}):Math.abs(oe-l)<$?G.push({index:q,cartographic:Oe.Cartographic.clone(Qe)}):Math.abs(se-d)<te?D.push({index:q,cartographic:Oe.Cartographic.clone(Qe)}):Math.abs(se-g)<te&&z.push({index:q,cartographic:Oe.Cartographic.clone(Qe)}),N=Math.min(ue,N),x=Math.max(ue,x),L[q]=ue;var ce=i.cartographicToCartesian(Qe);Y[q]=ce,s&&(U[q]=(ke.WebMercatorProjection.geodeticLatitudeToMercatorAngle(se)-I)*f),Ve.Matrix4.multiplyByPoint(C,ce,Xe),Oe.Cartesian3.minimumByComponent(Xe,b,b),Oe.Cartesian3.maximumByComponent(Xe,P,P);var de=(oe-c)/(l-c);de=We.CesiumMath.clamp(de,0,1);var le=(se-d)/(g-d);le=We.CesiumMath.clamp(le,0,1),k[q]=new Oe.Cartesian2(de,le),++q}for(var ge=3*ae,me=0;me<ge;++me,++J)H[J]=re[M.getUint16(B,!0)],B+=De;if(S!==B-K)throw new Ye.RuntimeError("Invalid terrain tile.")}Y.length=q,k.length=q,L.length=q,s&&(U.length=q);var pe=q,Ie=J,fe={hMin:N,lastBorderPoint:void 0,skirtHeight:o,toENU:C,ellipsoid:i,minimum:b,maximum:P};j.sort(function(e,t){return t.cartographic.latitude-e.cartographic.latitude}),D.sort(function(e,t){return e.cartographic.longitude-t.cartographic.longitude}),G.sort(function(e,t){return e.cartographic.latitude-t.cartographic.latitude}),z.sort(function(e,t){return t.cartographic.longitude-e.cartographic.longitude});var ve=1e-5;if(tt(Y,L,k,U,H,fe,j,-ve*m,!0,-ve*p),tt(Y,L,k,U,H,fe,D,-ve*p,!1),tt(Y,L,k,U,H,fe,G,ve*m,!0,ve*p),tt(Y,L,k,U,H,fe,z,ve*p,!1),0<j.length&&0<z.length){var Ee=j[0].index,Te=z[z.length-1].index,Ce=Y.length-1;H.push(Te,Ce,pe,pe,Ee,Te)}R=Y.length;var Me,Ne=Ve.BoundingSphere.fromPoints(Y);Fe.defined(a)&&a.width<We.CesiumMath.PI_OVER_TWO+We.CesiumMath.EPSILON5&&(Me=Ue.OrientedBoundingBox.fromRectangle(a,N,x,i));for(var xe=new He.EllipsoidalOccluder(i).computeHorizonCullingPoint(t,Y),be=new Le.AxisAlignedBoundingBox(b,P,t),Pe=new je.TerrainEncoding(be,fe.hMin,x,T,!1,s),Se=new Float32Array(R*Pe.getStride()),we=0,Be=0;Be<R;++Be)we=Pe.encode(Se,we,Y[Be],k[Be],L[Be],void 0,U[Be]);var Re=j.map(function(e){return e.index}).reverse(),Ae=D.map(function(e){return e.index}).reverse(),ye=G.map(function(e){return e.index}).reverse(),_e=z.map(function(e){return e.index}).reverse();return Ae.unshift(ye[ye.length-1]),Ae.push(Re[0]),_e.unshift(Re[Re.length-1]),_e.push(ye[0]),{vertices:Se,indices:new Uint16Array(H),maximumHeight:x,minimumHeight:N,encoding:Pe,boundingSphere3D:Ne,orientedBoundingBox:Me,occludeePointInScaledSpace:xe,vertexCountWithoutSkirts:pe,skirtIndex:Ie,westIndicesSouthToNorth:Re,southIndicesEastToWest:Ae,eastIndicesNorthToSouth:ye,northIndicesWestToEast:_e}}(e.buffer,e.relativeToCenter,e.ellipsoid,e.rectangle,e.nativeRectangle,e.exaggeration,e.skirtHeight,e.includeWebMercatorT,e.negativeAltitudeExponentBias,e.negativeElevationThreshold),a=i.vertices;t.push(a.buffer);var r=i.indices;return t.push(r.buffer),{vertices:a.buffer,indices:r.buffer,numberOfAttributes:i.encoding.getStride(),minimumHeight:i.minimumHeight,maximumHeight:i.maximumHeight,boundingSphere3D:i.boundingSphere3D,orientedBoundingBox:i.orientedBoundingBox,occludeePointInScaledSpace:i.occludeePointInScaledSpace,encoding:i.encoding,vertexCountWithoutSkirts:i.vertexCountWithoutSkirts,skirtIndex:i.skirtIndex,westIndicesSouthToNorth:i.westIndicesSouthToNorth,southIndicesEastToWest:i.southIndicesEastToWest,eastIndicesNorthToSouth:i.eastIndicesNorthToSouth,northIndicesWestToEast:i.northIndicesWestToEast}})});
