text: import{EventDispatcher}from'./EventDispatcher.js';import{Face3}from'./Face3.js';import{Matrix3}from'../math/Matrix3.js';import{Sphere}from'../math/Sphere.js';import{Box3}from'../math/Box3.js';import{Vector3}from'../math/Vector3.js';import{Matrix4}from'../math/Matrix4.js';import{Vector2}from'../math/Vector2.js';import{Color}from'../math/Color.js';import{Object3D}from'./Object3D.js';import{_Math}from'../math/Math.js';var count=0;function GeometryIdCount(){return count++}function Geometry(){Object.defineProperty(this,'id',{value:GeometryIdCount()}),this.uuid=_Math.generateUUID(),this.name='',this.type='Geometry',this.vertices=[],this.colors=[],this.faces=[],this.faceVertexUvs=[[]],this.morphTargets=[],this.morphNormals=[],this.skinWeights=[],this.skinIndices=[],this.lineDistances=[],this.boundingBox=null,this.boundingSphere=null,this.elementsNeedUpdate=!1,this.verticesNeedUpdate=!1,this.uvsNeedUpdate=!1,this.normalsNeedUpdate=!1,this.colorsNeedUpdate=!1,this.lineDistancesNeedUpdate=!1,this.groupsNeedUpdate=!1}Object.assign(Geometry.prototype,EventDispatcher.prototype,{isGeometry:!0,applyMatrix:function(a){for(var b,c=new Matrix3().getNormalMatrix(a),d=0,e=this.vertices.length;d<e;d++)b=this.vertices[d],b.applyMatrix4(a);for(var f,d=0,e=this.faces.length;d<e;d++){f=this.faces[d],f.normal.applyMatrix3(c).normalize();for(var g=0,h=f.vertexNormals.length;g<h;g++)f.vertexNormals[g].applyMatrix3(c).normalize()}return null!==this.boundingBox&&this.computeBoundingBox(),null!==this.boundingSphere&&this.computeBoundingSphere(),this.verticesNeedUpdate=!0,this.normalsNeedUpdate=!0,this},rotateX:function(){var a=new Matrix4;return function(b){return a.makeRotationX(b),this.applyMatrix(a),this}}(),rotateY:function(){var a=new Matrix4;return function(b){return a.makeRotationY(b),this.applyMatrix(a),this}}(),rotateZ:function(){var a=new Matrix4;return function(b){return a.makeRotationZ(b),this.applyMatrix(a),this}}(),translate:function(){var a=new Matrix4;return function(b,c,d){return a.makeTranslation(b,c,d),this.applyMatrix(a),this}}(),scale:function(){var a=new Matrix4;return function(b,c,d){return a.makeScale(b,c,d),this.applyMatrix(a),this}}(),lookAt:function(){var a=new Object3D;return function(b){a.lookAt(b),a.updateMatrix(),this.applyMatrix(a.matrix)}}(),fromBufferGeometry:function(a){function b(e,a,b,c){var f=void 0===g?[]:[m[e].clone(),m[a].clone(),m[b].clone()],i=void 0===h?[]:[d.colors[e].clone(),d.colors[a].clone(),d.colors[b].clone()],j=new Face3(e,a,b,f,i,c);d.faces.push(j),void 0!==k&&d.faceVertexUvs[0].push([n[e].clone(),n[a].clone(),n[b].clone()]),void 0!==l&&d.faceVertexUvs[1].push([o[e].clone(),o[a].clone(),o[b].clone()])}var d=this,c=null===a.index?void 0:a.index.array,e=a.attributes,f=e.position.array,g=void 0===e.normal?void 0:e.normal.array,h=void 0===e.color?void 0:e.color.array,k=void 0===e.uv?void 0:e.uv.array,l=void 0===e.uv2?void 0:e.uv2.array;void 0!==l&&(this.faceVertexUvs[1]=[]);for(var m=[],n=[],o=[],p=0,i=0;p<f.length;p+=3,i+=2)d.vertices.push(new Vector3(f[p],f[p+1],f[p+2])),void 0!==g&&m.push(new Vector3(g[p],g[p+1],g[p+2])),void 0!==h&&d.colors.push(new Color(h[p],h[p+1],h[p+2])),void 0!==k&&n.push(new Vector2(k[i],k[i+1])),void 0!==l&&o.push(new Vector2(l[i],l[i+1]));var j=a.groups;if(0<j.length)for(var p=0;p<j.length;p++)for(var q=j[p],r=q.start,s=q.count,i=r;i<r+s;i+=3)void 0===c?b(i,i+1,i+2,q.materialIndex):b(c[i],c[i+1],c[i+2],q.materialIndex);else if(void 0!==c)for(var p=0;p<c.length;p+=3)b(c[p],c[p+1],c[p+2]);else for(var p=0;p<f.length/3;p+=3)b(p,p+1,p+2);return this.computeFaceNormals(),null!==a.boundingBox&&(this.boundingBox=a.boundingBox.clone()),null!==a.boundingSphere&&(this.boundingSphere=a.boundingSphere.clone()),this},center:function(){this.computeBoundingBox();var a=this.boundingBox.getCenter().negate();return this.translate(a.x,a.y,a.z),a},normalize:function(){this.computeBoundingSphere();var a=this.boundingSphere.center,b=this.boundingSphere.radius,c=0===b?1:1/b,d=new Matrix4;return d.set(c,0,0,-c*a.x,0,c,0,-c*a.y,0,0,c,-c*a.z,0,0,0,1),this.applyMatrix(d),this},computeFaceNormals:function(){for(var a=new Vector3,b=new Vector3,c=0,d=this.faces.length;c<d;c++){var e=this.faces[c],f=this.vertices[e.a],g=this.vertices[e.b],h=this.vertices[e.c];a.subVectors(h,g),b.subVectors(f,g),a.cross(b),a.normalize(),e.normal.copy(a)}},computeVertexNormals:function(a){a===void 0&&(a=!0);var b,c,d,e,f,g;for(g=Array(this.vertices.length),b=0,c=this.vertices.length;b<c;b++)g[b]=new Vector3;if(a){var h,i,j,k=new Vector3,l=new Vector3;for(d=0,e=this.faces.length;d<e;d++)f=this.faces[d],h=this.vertices[f.a],i=this.vertices[f.b],j=this.vertices[f.c],k.subVectors(j,i),l.subVectors(h,i),k.cross(l),g[f.a].add(k),g[f.b].add(k),g[f.c].add(k)}else for(this.computeFaceNormals(),d=0,e=this.faces.length;d<e;d++)f=this.faces[d],g[f.a].add(f.normal),g[f.b].add(f.normal),g[f.c].add(f.normal);for(b=0,c=this.vertices.length;b<c;b++)g[b].normalize();for(d=0,e=this.faces.length;d<e;d++){f=this.faces[d];var m=f.vertexNormals;3===m.length?(m[0].copy(g[f.a]),m[1].copy(g[f.b]),m[2].copy(g[f.c])):(m[0]=g[f.a].clone(),m[1]=g[f.b].clone(),m[2]=g[f.c].clone())}0<this.faces.length&&(this.normalsNeedUpdate=!0)},computeFlatVertexNormals:function(){var a,b,c;for(this.computeFaceNormals(),a=0,b=this.faces.length;a<b;a++){c=this.faces[a];var d=c.vertexNormals;3===d.length?(d[0].copy(c.normal),d[1].copy(c.normal),d[2].copy(c.normal)):(d[0]=c.normal.clone(),d[1]=c.normal.clone(),d[2]=c.normal.clone())}0<this.faces.length&&(this.normalsNeedUpdate=!0)},computeMorphNormals:function(){var a,b,c,d,e;for(c=0,d=this.faces.length;c<d;c++)for(e=this.faces[c],e.__originalFaceNormal?e.__originalFaceNormal.copy(e.normal):e.__originalFaceNormal=e.normal.clone(),e.__originalVertexNormals||(e.__originalVertexNormals=[]),(a=0,b=e.vertexNormals.length);a<b;a++)e.__originalVertexNormals[a]?e.__originalVertexNormals[a].copy(e.vertexNormals[a]):e.__originalVertexNormals[a]=e.vertexNormals[a].clone();var f=new Geometry;for(f.faces=this.faces,a=0,b=this.morphTargets.length;a<b;a++){if(!this.morphNormals[a]){this.morphNormals[a]={},this.morphNormals[a].faceNormals=[],this.morphNormals[a].vertexNormals=[];var g,h,i=this.morphNormals[a].faceNormals,j=this.morphNormals[a].vertexNormals;for(c=0,d=this.faces.length;c<d;c++)g=new Vector3,h={a:new Vector3,b:new Vector3,c:new Vector3},i.push(g),j.push(h)}var k=this.morphNormals[a];f.vertices=this.morphTargets[a].vertices,f.computeFaceNormals(),f.computeVertexNormals();var g,h;for(c=0,d=this.faces.length;c<d;c++)e=this.faces[c],g=k.faceNormals[c],h=k.vertexNormals[c],g.copy(e.normal),h.a.copy(e.vertexNormals[0]),h.b.copy(e.vertexNormals[1]),h.c.copy(e.vertexNormals[2])}for(c=0,d=this.faces.length;c<d;c++)e=this.faces[c],e.normal=e.__originalFaceNormal,e.vertexNormals=e.__originalVertexNormals},computeLineDistances:function(){for(var a=0,b=this.vertices,c=0,d=b.length;c<d;c++)0<c&&(a+=b[c].distanceTo(b[c-1])),this.lineDistances[c]=a},computeBoundingBox:function(){null===this.boundingBox&&(this.boundingBox=new Box3),this.boundingBox.setFromPoints(this.vertices)},computeBoundingSphere:function(){null===this.boundingSphere&&(this.boundingSphere=new Sphere),this.boundingSphere.setFromPoints(this.vertices)},merge:function(a,b,c){if(!1===(a&&a.isGeometry))return void console.error('THREE.Geometry.merge(): geometry not an instance of THREE.Geometry.',a);var d,e=this.vertices.length,f=this.vertices,g=a.vertices,h=this.faces,k=a.faces,l=this.faceVertexUvs[0],m=a.faceVertexUvs[0],n=this.colors,o=a.colors;c===void 0&&(c=0),b!==void 0&&(d=new Matrix3().getNormalMatrix(b));for(var p=0,i=g.length;p<i;p++){var q=g[p],r=q.clone();b!==void 0&&r.applyMatrix4(b),f.push(r)}for(var p=0,i=o.length;p<i;p++)n.push(o[p].clone());for(p=0,i=k.length;p<i;p++){var s,t,u,v=k[p],w=v.vertexNormals,x=v.vertexColors;s=new Face3(v.a+e,v.b+e,v.c+e),s.normal.copy(v.normal),d!==void 0&&s.normal.applyMatrix3(d).normalize();for(var y=0,j=w.length;y<j;y++)t=w[y].clone(),void 0!==d&&t.applyMatrix3(d).normalize(),s.vertexNormals.push(t);s.color.copy(v.color);for(var y=0,j=x.length;y<j;y++)u=x[y],s.vertexColors.push(u.clone());s.materialIndex=v.materialIndex+c,h.push(s)}for(p=0,i=m.length;p<i;p++){var z=m[p],A=[];if(void 0!==z){for(var y=0,j=z.length;y<j;y++)A.push(z[y].clone());l.push(A)}}},mergeMesh:function(a){return!1===(a&&a.isMesh)?void console.error('THREE.Geometry.mergeMesh(): mesh not an instance of THREE.Mesh.',a):void(a.matrixAutoUpdate&&a.updateMatrix(),this.merge(a.geometry,a.matrix))},mergeVertices:function(){var a,b,c,d,e,f,g,h,i=Math.round,j={},k=[],l=[],m=10000;for(c=0,d=this.vertices.length;c<d;c++)a=this.vertices[c],b=i(a.x*m)+'_'+i(a.y*m)+'_'+i(a.z*m),void 0===j[b]?(j[b]=c,k.push(this.vertices[c]),l[c]=k.length-1):l[c]=l[j[b]];var o=[];for(c=0,d=this.faces.length;c<d;c++){e=this.faces[c],e.a=l[e.a],e.b=l[e.b],e.c=l[e.c],f=[e.a,e.b,e.c];for(var p=0;3>p;p++)if(f[p]===f[(p+1)%3]){o.push(c);break}}for(c=o.length-1;0<=c;c--){var n=o[c];for(this.faces.splice(n,1),g=0,h=this.faceVertexUvs.length;g<h;g++)this.faceVertexUvs[g].splice(n,1)}var q=this.vertices.length-k.length;return this.vertices=k,q},sortFacesByMaterialIndex:function(){for(var a=this.faces,b=a.length,c=0;c<b;c++)a[c]._id=c;a.sort(function(c,a){return c.materialIndex-a.materialIndex});var d,e,f=this.faceVertexUvs[0],g=this.faceVertexUvs[1];f&&f.length===b&&(d=[]),g&&g.length===b&&(e=[]);for(var h,c=0;c<b;c++)h=a[c]._id,d&&d.push(f[h]),e&&e.push(g[h]);d&&(this.faceVertexUvs[0]=d),e&&(this.faceVertexUvs[1]=e)},toJSON:function(){function a(a,b,c){return c?a|1<<b:a&~(1<<b)}function b(a){var b=a.x.toString()+a.y.toString()+a.z.toString();return void 0===m[b]?(m[b]=l.length/3,l.push(a.x,a.y,a.z),m[b]):m[b]}function c(a){var b=a.r.toString()+a.g.toString()+a.b.toString();return void 0===o[b]?(o[b]=n.length,n.push(a.getHex()),o[b]):o[b]}function d(a){var b=a.x.toString()+a.y.toString();return void 0===q[b]?(q[b]=p.length/2,p.push(a.x,a.y),q[b]):q[b]}var e={metadata:{version:4.5,type:'Geometry',generator:'Geometry.toJSON'}};if(e.uuid=this.uuid,e.type=this.type,''!==this.name&&(e.name=this.name),void 0!==this.parameters){var f=this.parameters;for(var g in f)void 0!==f[g]&&(e[g]=f[g]);return e}for(var h,j=[],k=0;k<this.vertices.length;k++)h=this.vertices[k],j.push(h.x,h.y,h.z);for(var i=[],l=[],m={},n=[],o={},p=[],q={},k=0;k<this.faces.length;k++){var r=this.faces[k],s=void 0!==this.faceVertexUvs[0][k],t=0<r.normal.length(),u=0<r.vertexNormals.length,v=1!==r.color.r||1!==r.color.g||1!==r.color.b,w=0<r.vertexColors.length,x=0;if(x=a(x,0,0),x=a(x,1,!0),x=a(x,2,!1),x=a(x,3,s),x=a(x,4,t),x=a(x,5,u),x=a(x,6,v),x=a(x,7,w),i.push(x),i.push(r.a,r.b,r.c),i.push(r.materialIndex),s){var y=this.faceVertexUvs[0][k];i.push(d(y[0]),d(y[1]),d(y[2]))}if(t&&i.push(b(r.normal)),u){var z=r.vertexNormals;i.push(b(z[0]),b(z[1]),b(z[2]))}if(v&&i.push(c(r.color)),w){var A=r.vertexColors;i.push(c(A[0]),c(A[1]),c(A[2]))}}return e.data={},e.data.vertices=j,e.data.normals=l,0<n.length&&(e.data.colors=n),0<p.length&&(e.data.uvs=[p]),e.data.faces=i,e},clone:function(){return new Geometry().copy(this)},copy:function(a){var b,c,d,e,f,g;this.vertices=[],this.colors=[],this.faces=[],this.faceVertexUvs=[[]],this.morphTargets=[],this.morphNormals=[],this.skinWeights=[],this.skinIndices=[],this.lineDistances=[],this.boundingBox=null,this.boundingSphere=null,this.name=a.name;var h=a.vertices;for(b=0,c=h.length;b<c;b++)this.vertices.push(h[b].clone());var i=a.colors;for(b=0,c=i.length;b<c;b++)this.colors.push(i[b].clone());var j=a.faces;for(b=0,c=j.length;b<c;b++)this.faces.push(j[b].clone());for(b=0,c=a.faceVertexUvs.length;b<c;b++){var k=a.faceVertexUvs[b];for(void 0===this.faceVertexUvs[b]&&(this.faceVertexUvs[b]=[]),d=0,e=k.length;d<e;d++){var l=k[d],m=[];for(f=0,g=l.length;f<g;f++){var n=l[f];m.push(n.clone())}this.faceVertexUvs[b].push(m)}}var o=a.morphTargets;for(b=0,c=o.length;b<c;b++){var p={name:o[b].name};if(void 0!==o[b].vertices)for(p.vertices=[],d=0,e=o[b].vertices.length;d<e;d++)p.vertices.push(o[b].vertices[d].clone());if(void 0!==o[b].normals)for(p.normals=[],d=0,e=o[b].normals.length;d<e;d++)p.normals.push(o[b].normals[d].clone());this.morphTargets.push(p)}var q=a.morphNormals;for(b=0,c=q.length;b<c;b++){var r={};if(void 0!==q[b].vertexNormals)for(r.vertexNormals=[],d=0,e=q[b].vertexNormals.length;d<e;d++){var s=q[b].vertexNormals[d],t={};t.a=s.a.clone(),t.b=s.b.clone(),t.c=s.c.clone(),r.vertexNormals.push(t)}if(void 0!==q[b].faceNormals)for(r.faceNormals=[],d=0,e=q[b].faceNormals.length;d<e;d++)r.faceNormals.push(q[b].faceNormals[d].clone());this.morphNormals.push(r)}var u=a.skinWeights;for(b=0,c=u.length;b<c;b++)this.skinWeights.push(u[b].clone());var v=a.skinIndices;for(b=0,c=v.length;b<c;b++)this.skinIndices.push(v[b].clone());var w=a.lineDistances;for(b=0,c=w.length;b<c;b++)this.lineDistances.push(w[b]);var x=a.boundingBox;null!==x&&(this.boundingBox=x.clone());var y=a.boundingSphere;return null!==y&&(this.boundingSphere=y.clone()),this.elementsNeedUpdate=a.elementsNeedUpdate,this.verticesNeedUpdate=a.verticesNeedUpdate,this.uvsNeedUpdate=a.uvsNeedUpdate,this.normalsNeedUpdate=a.normalsNeedUpdate,this.colorsNeedUpdate=a.colorsNeedUpdate,this.lineDistancesNeedUpdate=a.lineDistancesNeedUpdate,this.groupsNeedUpdate=a.groupsNeedUpdate,this},dispose:function(){this.dispatchEvent({type:'dispose'})}});export{GeometryIdCount,Geometry};