3 lines
6.3 KiB
JavaScript
3 lines
6.3 KiB
JavaScript
import{C as e,K as t,_t as n,gt as r,j as i,p as a,u as o,w as s,wt as c}from"./three.module-BFsVh7r1.js";var l=class extends t{constructor(e){super(e),this.littleEndian=!0}load(t,n,r,i){let a=this,o=new e(a.manager);o.setPath(a.path),o.setResponseType(`arraybuffer`),o.setRequestHeader(a.requestHeader),o.setWithCredentials(a.withCredentials),o.load(t,function(e){try{n(a.parse(e))}catch(e){i?i(e):console.error(e),a.manager.itemError(t)}},r,i)}_getDataView(e,t,n,r){switch(n){case`F`:return r===8?e.getFloat64(t,this.littleEndian):e.getFloat32(t,this.littleEndian);case`I`:return r===1?e.getInt8(t):r===2?e.getInt16(t,this.littleEndian):e.getInt32(t,this.littleEndian);case`U`:return r===1?e.getUint8(t):r===2?e.getUint16(t,this.littleEndian):e.getUint32(t,this.littleEndian)}}parse(e){function t(e,t){let n=e.length,r=new Uint8Array(t),i=0,a=0,o,s,c;do if(o=e[i++],o<32){if(o++,a+o>t)throw Error(`Output buffer is not large enough`);if(i+o>n)throw Error(`Invalid compressed data`);do r[a++]=e[i++];while(--o)}else{if(s=o>>5,c=a-((o&31)<<8)-1,i>=n||s===7&&(s+=e[i++],i>=n))throw Error(`Invalid compressed data`);if(c-=e[i++],a+s+2>t)throw Error(`Output buffer is not large enough`);if(c<0||c>=a)throw Error(`Invalid compressed data`);do r[a++]=r[c++];while(--s+2)}while(i<n);return r}function l(e){let t={},n=new Uint8Array(e),r=``,i=``,a=0,o=!1,s=n.length;for(;a<s&&o===!1;){let e=String.fromCharCode(n[a++]);e===`
|
|
`||e===`\r`?(i.trim().toLowerCase().startsWith(`data`)&&(o=!0),i=``):i+=e,r+=e}let c=r.search(/[\r\n]DATA\s(\S*)\s/i),l=/[\r\n]DATA\s(\S*)\s/i.exec(r.slice(c-1));if(t.data=l[1],t.headerLen=l[0].length+c,t.str=r.slice(0,t.headerLen),t.str=t.str.replace(/#.*/gi,``),t.version=/^VERSION (.*)/im.exec(t.str),t.fields=/^FIELDS (.*)/im.exec(t.str),t.size=/^SIZE (.*)/im.exec(t.str),t.type=/^TYPE (.*)/im.exec(t.str),t.count=/^COUNT (.*)/im.exec(t.str),t.width=/^WIDTH (.*)/im.exec(t.str),t.height=/^HEIGHT (.*)/im.exec(t.str),t.viewpoint=/^VIEWPOINT (.*)/im.exec(t.str),t.points=/^POINTS (.*)/im.exec(t.str),t.version!==null&&(t.version=parseFloat(t.version[1])),t.fields=t.fields===null?[]:t.fields[1].split(` `),t.type!==null&&(t.type=t.type[1].split(` `)),t.width!==null&&(t.width=parseInt(t.width[1])),t.height!==null&&(t.height=parseInt(t.height[1])),t.viewpoint!==null&&(t.viewpoint=t.viewpoint[1]),t.points!==null&&(t.points=parseInt(t.points[1],10)),t.points===null&&(t.points=t.width*t.height),t.size!==null&&(t.size=t.size[1].split(` `).map(function(e){return parseInt(e,10)})),t.count!==null)t.count=t.count[1].split(` `).map(function(e){return parseInt(e,10)});else{t.count=[];for(let e=0,n=t.fields.length;e<n;e++)t.count.push(1)}t.offset={};let u=0;for(let e=0,n=t.fields.length;e<n;e++)t.data===`ascii`?t.offset[t.fields[e]]=e:(t.offset[t.fields[e]]=u,u+=t.size[e]*t.count[e]);return t.rowSize=u,t}let u=l(e),d=[],f=[],p=[],m=[],h=[],g=new a;if(u.data===`ascii`){let t=u.offset,n=new TextDecoder().decode(e).slice(u.headerLen).split(`
|
|
`);for(let e=0,r=n.length;e<r;e++){if(n[e]===``)continue;let r=n[e].split(` `);if(t.x!==void 0&&(d.push(parseFloat(r[t.x])),d.push(parseFloat(r[t.y])),d.push(parseFloat(r[t.z]))),t.rgb!==void 0){let e=u.fields.findIndex(e=>e===`rgb`),n=u.type[e],i=parseFloat(r[t.rgb]),a=i;if(n===`F`){let e=new Float32Array(1);e[0]=i,a=new Int32Array(e.buffer)[0]}let o=(a>>16&255)/255,s=(a>>8&255)/255,l=(a>>0&255)/255;g.setRGB(o,s,l,c),p.push(g.r,g.g,g.b)}t.normal_x!==void 0&&(f.push(parseFloat(r[t.normal_x])),f.push(parseFloat(r[t.normal_y])),f.push(parseFloat(r[t.normal_z]))),t.intensity!==void 0&&m.push(parseFloat(r[t.intensity])),t.label!==void 0&&h.push(parseInt(r[t.label]))}}if(u.data===`binary_compressed`){let n=new Uint32Array(e.slice(u.headerLen,u.headerLen+8)),r=n[0],i=n[1],a=t(new Uint8Array(e,u.headerLen+8,r),i),o=new DataView(a.buffer),s=u.offset;for(let e=0;e<u.points;e++){if(s.x!==void 0){let t=u.fields.indexOf(`x`),n=u.fields.indexOf(`y`),r=u.fields.indexOf(`z`);d.push(this._getDataView(o,u.points*s.x+u.size[t]*e,u.type[t],u.size[t])),d.push(this._getDataView(o,u.points*s.y+u.size[n]*e,u.type[n],u.size[n])),d.push(this._getDataView(o,u.points*s.z+u.size[r]*e,u.type[r],u.size[r]))}if(s.rgb!==void 0){let t=u.fields.indexOf(`rgb`),n=o.getUint8(u.points*s.rgb+u.size[t]*e+2)/255,r=o.getUint8(u.points*s.rgb+u.size[t]*e+1)/255,i=o.getUint8(u.points*s.rgb+u.size[t]*e+0)/255;g.setRGB(n,r,i,c),p.push(g.r,g.g,g.b)}if(s.normal_x!==void 0){let t=u.fields.indexOf(`normal_x`),n=u.fields.indexOf(`normal_y`),r=u.fields.indexOf(`normal_z`);f.push(this._getDataView(o,u.points*s.normal_x+u.size[t]*e,u.type[t],u.size[t])),f.push(this._getDataView(o,u.points*s.normal_y+u.size[n]*e,u.type[n],u.size[n])),f.push(this._getDataView(o,u.points*s.normal_z+u.size[r]*e,u.type[r],u.size[r]))}if(s.intensity!==void 0){let t=u.fields.indexOf(`intensity`);m.push(this._getDataView(o,u.points*s.intensity+u.size[t]*e,u.type[t],u.size[t]))}if(s.label!==void 0){let t=u.fields.indexOf(`label`);h.push(this._getDataView(o,u.points*s.label+u.size[t]*e,u.type[t],u.size[t]))}}}if(u.data===`binary`){let t=new DataView(e,u.headerLen),n=u.offset;for(let e=0,r=0;e<u.points;e++,r+=u.rowSize){if(n.x!==void 0){let e=u.fields.indexOf(`x`),i=u.fields.indexOf(`y`),a=u.fields.indexOf(`z`);d.push(this._getDataView(t,r+n.x,u.type[e],u.size[e])),d.push(this._getDataView(t,r+n.y,u.type[i],u.size[i])),d.push(this._getDataView(t,r+n.z,u.type[a],u.size[a]))}if(n.rgb!==void 0){let e=t.getUint8(r+n.rgb+2)/255,i=t.getUint8(r+n.rgb+1)/255,a=t.getUint8(r+n.rgb+0)/255;g.setRGB(e,i,a,c),p.push(g.r,g.g,g.b)}if(n.normal_x!==void 0){let e=u.fields.indexOf(`normal_x`),i=u.fields.indexOf(`normal_y`),a=u.fields.indexOf(`normal_z`);f.push(this._getDataView(t,r+n.normal_x,u.type[e],u.size[e])),f.push(this._getDataView(t,r+n.normal_y,u.type[i],u.size[i])),f.push(this._getDataView(t,r+n.normal_z,u.type[a],u.size[a]))}if(n.intensity!==void 0){let e=u.fields.indexOf(`intensity`);m.push(this._getDataView(t,r+n.intensity,u.type[e],u.size[e]))}if(n.label!==void 0){let e=u.fields.indexOf(`label`);h.push(this._getDataView(t,r+n.label,u.type[e],u.size[e]))}}}let _=new o;d.length>0&&_.setAttribute(`position`,new s(d,3)),f.length>0&&_.setAttribute(`normal`,new s(f,3)),p.length>0&&_.setAttribute(`color`,new s(p,3)),m.length>0&&_.setAttribute(`intensity`,new s(m,1)),h.length>0&&_.setAttribute(`label`,new i(h,1)),_.computeBoundingSphere();let v=new n({size:.005});return p.length>0&&(v.vertexColors=!0),new r(_,v)}};export{l as PCDLoader}; |