1//=========== Stl Viewer v1.13.1, by Omri Rips, Viewstl.com, October 2021 ; admin@viewstl.com =========== 2function parse_3d_file(e,r,t,n){var i=null;switch(e.split(".").pop().split("?")[0].toLowerCase()){case"stl":i=parse_stl_bin(r);break;case"obj":i=parse_obj(r);break;case"vf":i=parse_vf(arrayBufferToString(r));break;case"3mf":return void parse_3mf(r,t,n);default:i=parse_stl_bin(r)}t&&t(i)}function arrayBufferToString(e,r,t){if("undefined"!=typeof TextDecoder)return new TextDecoder("utf-8").decode(e);for(var n=new Uint8Array(e),i=n.length,a="",s=0;s<i;s+=16383){var o=16383;s+16383>i&&(o=i-s),a+=String.fromCharCode.apply(null,n.subarray(s,s+o))}return a}function parse_stl_ascii(e){try{var r=arrayBufferToString(e),t=[],n=[],i={};r=(r=(r=(r=(r=(r=(r=(r=(r=(r=(r=(r=r.replace(/\r/,"\n")).replace(/^solid[^\n]*/,"")).replace(/\n/g," ")).replace(/facet normal /g,"")).replace(/outer loop/g,"")).replace(/vertex /g,"")).replace(/endloop/g,"")).replace(/endfacet/g,"")).replace(/endsolid[^\n]*/,"")).replace(/facet/g,"")).replace(/\s+/g," ")).replace(/^\s+/,"");for(var a,s=0,o=r.split(" "),l=[],c=o.length/12-1,f=0;f<c;f++){l=[];for(var d=0;d<3;d++)f1=parseFloat(o[s+3*d+3]),f2=parseFloat(o[s+3*d+4]),f3=parseFloat(o[s+3*d+5]),null==(a=i[[f1,f2,f3]])&&(a=t.length,t.push(new Array(f1,f2,f3)),i[[f1,f2,f3]]=a),l.push(a);n.push(new Array(l[0],l[1],l[2])),s+=12}return{vertices:t,faces:n,colors:!1}}catch(e){return"Can't parse file"}}function parse_stl_bin(e){var r,t,n,i,a,s,o,l=[],c=[],f={},d=!1;if(!e)return null;var p=arrayBufferToString(e.slice(0,80)).toLowerCase().indexOf("color"),u=new DataView(e,0),v=!1,_=-1,g=-1,h=-1;p>-1&&(d=!0,_=u.getUint8(p+6,!0)/31,g=u.getUint8(p+7,!0)/31,h=u.getUint8(p+8,!0)/31);var x=80;try{var y=u.getUint32(x,!0)}catch(e){return"Can't parse file"}var m=84+50*y;if(e.byteLength!=m)return parse_stl_ascii(e);try{for(x+=4;y--;)x+=12,null==(r=f[[t=u.getFloat32(x,!0),n=u.getFloat32(x+4,!0),i=u.getFloat32(x+8,!0)]])&&(r=l.length,l.push(new Array(t,n,i)),f[[t,n,i]]=r),a=r,x+=12,null==(r=f[[t=u.getFloat32(x,!0),n=u.getFloat32(x+4,!0),i=u.getFloat32(x+8,!0)]])&&(r=l.length,l.push(new Array(t,n,i)),f[[t,n,i]]=r),s=r,x+=12,null==(r=f[[t=u.getFloat32(x,!0),n=u.getFloat32(x+4,!0),i=u.getFloat32(x+8,!0)]])&&(r=l.length,l.push(new Array(t,n,i)),f[[t,n,i]]=r),o=r,x+=12,face_color=u.getUint16(x,!0),(d?1:(32768&face_color)>>15)?(d?32768==face_color||65535==face_color?(color_red=_,color_green=g,color_blue=h):(v=!0,color_red=(31&face_color)/31,color_green=((992&face_color)>>5)/31,color_blue=((31744&face_color)>>10)/31):(v=!0,color_blue=(31&face_color)/31,color_green=((992&face_color)>>5)/31,color_red=((31744&face_color)>>10)/31),c.push(new Array(a,s,o,color_red,color_green,color_blue))):c.push(new Array(a,s,o)),x+=2;return f=null,{vertices:l,faces:c,colors:v}}catch(e){return"Can't parse file"}}function parse_vf(e){var r=JSON.parse(e),t=[],n=[];try{var a=r.vertices.length;for(i=0;i<a;i++)t.push(new Array(r.vertices[i][0],r.vertices[i][1],r.vertices[i][2]));a=r.faces.length;for(i=0;i<a;i++)n.push(new Array(r.faces[i][0],r.faces[i][1],r.faces[i][2]));return{vertices:t,faces:n,colors:!1}}catch(e){return"Can't parse file"}}function init_zip(e,r){var t=null;try{t=new JSZip}catch(r){e&&console.log("JSZip is missing",r.message),t=null}return t||e?t:(importScripts(r),init_zip(!0,r))}function parse_3mf(e,r,t){if("<?xml"==arrayBufferToString(e.slice(0,5)))return parse_3mf_from_txt(arrayBufferToString(e),r);var n=init_zip(!1,t);if(!n)return!1;var i=!1;n.loadAsync(e).then(function(){for(var e=Object.keys(n.files),t=e.length;t--;)"3D/3dmodel.model"==n.files[e[t]].name&&(i=!0,n.files[e[t]].async("text").then(function(e){return parse_3mf_from_txt(e,r)}));i||r("3D/3dmodel.model in 3mf file not found")})}function parse_3mf_from_txt(e,r){var t=[],n=[],i=[],a=[],s=!1,o=/vertex\s+.*(x|y|z)\s*=\s*([0-9,\.\"\+\-e]+)\s+.*(x|y|z)\s*=\s*([0-9,\.\"\+\-e]+)\s+.*(x|y|z)\s*=\s*([0-9,\.\"\+\-e]+)\s*/i,l=/triangle\s+.*(v1|v2|v3)\s*=\s*([0-9\"]+)\s+.*(v1|v2|v3)\s*=\s*([0-9\"]+)\s+.*(v1|v2|v3)\s*=\s*([0-9\"]+)\s*(?:pid=([0-9\"]+)\s+)?(?:p[1|2|3]=([0-9\"]+)\s)?\s*/i,c=/(?:m:\S+|basematerials)\s+id=([0-9\"]+)\s*/i,f=/(?:m:(\S+)|base)\s+.*color=([0-9A-F\"\#]+)\s*/i,d=/<object\s+/i,p=/<component\s+.*objectid=([0-9\"]+)/i,u=/item\s+.*objectid=([0-9\"]+)\s+.*transform=(([0-9\".e-]+\s+){12})/i,v={},_={},g=null,h=0,x=0,y=0,m=/<build/i,b=/<\/build/i,w=/<item\s+.*objectid=([0-9\"]+)/i,A=e.split(/[\r\n]+/g);A.length<5&&(A=e.split(/(?=<)/g));for(var F=!1,S=0;S<A.length;S++){var B=A[S];if(B=B.replace(/"/g,""),M=o.exec(B)){var z={x:0,y:0,z:0};z[M[1]]=M[2],z[M[3]]=M[4],z[M[5]]=M[6],t.push([z.x,z.y,z.z]),x++}else{if(M=l.exec(B)){var I={v1:0,v2:0,v3:0},J=null,O=g?g.v_start_index:0;I[M[1]]=parseInt(M[2])+O,I[M[3]]=parseInt(M[4])+O,I[M[5]]=parseInt(M[6])+O,void 0!==M[7]&&v[M[7]]&&void 0!==M[8]&&v[M[7]]&&v[M[7]].color&&v[M[7]].color[M[8]]&&(J=v[M[7]].color[M[8]].substr(1)),!J&&g&&void 0!==g.pid&&void 0!==g.pindex&&v[g.pid]&&v[g.pid].color&&v[g.pid].color[g.pindex]&&(J=v[g.pid].color[g.pindex].substr(1)),J?(s=!0,J={red:parseInt(J.substr(0,2),16)/255,green:parseInt(J.substr(2,2),16)/255,blue:parseInt(J.substr(4,2),16)/255},n.push([I.v1,I.v2,I.v3,J.red,J.green,J.blue])):n.push([I.v1,I.v2,I.v3]),y++}else{if(M=c.exec(B))v[h=M[1]]||(v[h]={});else if(M=p.exec(B)){if(!g)continue;if(!_[M[1]])continue;t=t.concat(JSON.parse(JSON.stringify(t.slice(_[M[1]].v_start_index,_[M[1]].v_end_index+1))));var T=/transform=(([0-9\".e-]+\s+){12})/i.exec(B);if(T&&12==(D=T[1].trim().split(/[ ,]+/)).length){var j=t.length-1;transform_vertices(t,j-(_[M[1]].v_end_index-_[M[1]].v_start_index),j,D)}for(var C=JSON.parse(JSON.stringify(n.slice(_[M[1]].f_start_index,_[M[1]].f_end_index+1))),L=t.length-_[M[1]].v_end_index-1,U=C.length;U--;)C[U][0]+=L,C[U][1]+=L,C[U][2]+=L;n=n.concat(C),x=t.length,y=n.length}else{if(M=d.exec(B)){if(!(M=/id=([0-9\"]+)/i.exec(B)))continue;g&&(g.v_end_index=x-1),g&&(g.f_end_index=y-1);var N={};N.id=M[1],N.v_start_index=x,N.f_start_index=y,(M=/pid=([0-9\"]+)/i.exec(B))&&(N.pid=parseInt(M[1])),(M=/pindex=([0-9\"]+)/i.exec(B))&&(N.pindex=parseInt(M[1])),_[(g=N).id]=g}else{if(M=f.exec(B)){if(void 0===M[1]&&(M[1]="color"),!v[h]){console.log("warning: no source id for "+M[1]);continue}v[h][M[1]]||(v[h][M[1]]=[]),v[h][M[1]].push(M[2])}else{var M;if(m.exec(B))F=!0;else if(b.exec(B))F=!1;else if(F)if(M=w.exec(B)){if(!_[M[1]])continue;var D,k=void 0===_[M[1]].v_end_index?x-1:_[M[1]].v_end_index,Z=u.exec(B);Z&&12==(D=Z[2].trim().split(/[ ,]+/)).length&&transform_vertices(t,_[Z[1]].v_start_index,k,D);for(L=(i=i.concat(t.slice(_[M[1]].v_start_index,k+1))).length-k-1,k=void 0===_[M[1]].f_end_index?y-1:_[M[1]].f_end_index,U=(C=JSON.parse(JSON.stringify(n.slice(_[M[1]].f_start_index,k+1)))).length;U--;)C[U][0]+=L,C[U][1]+=L,C[U][2]+=L;a=a.concat(C)}else;}}}}}}r({vertices:i,faces:a,colors:s})}function transform_vertices(e,r,t,n){for(var i=[[],[],[],[]],a=0;a<3;a++)i[0][a]=parseFloat(n[a]);i[0].push(0);for(a=3;a<6;a++)i[1][a-3]=parseFloat(n[a]);i[1].push(0);for(a=6;a<9;a++)i[2][a-6]=parseFloat(n[a]);i[2].push(0);for(a=9;a<12;a++)i[3][a-9]=parseFloat(n[a]);i[3].push(1);for(var s=r;s<=t;s++){var o=matrix_multiply([e[s].concat(1)],i);e[s]=[o[0][0],o[0][1],o[0][2]]}}function matrix_multiply(e,r){console.log();for(var t=e.length,n=e[0].length,i=(r.length,r[0].length),a=new Array(t),s=0;s<t;++s){a[s]=new Array(i);for(var o=0;o<i;++o){a[s][o]=0;for(var l=0;l<n;++l)a[s][o]+=e[s][l]*r[l][o]}}return a}function geo_to_vf(e){var r=[],t=[],n=e.vertices.length;for(i=0;i<n;i++)r.push([e.vertices[i].x,e.vertices[i].y,e.vertices[i].z]);n=e.faces.length;for(i=0;i<n;i++)t.push([e.faces[i].a,e.faces[i].b,e.faces[i].c]);return{vertices:r,faces:t,colors:!1}}function parse_obj(e){var r=arrayBufferToString(e);function t(e,r,t){return new Array(parseFloat(e),parseFloat(r),parseFloat(t))}function n(e){return(e=parseInt(e))>=0?e-1:e+l.length}function i(e,r,t,i){d.push(new Array(n(e),n(r),n(t)))}function a(e,r,t){void 0===e[3]?(i(e[0],e[1],e[2]),void 0!==r&&r.length>0&&(r[0],r[1],r[2])):(void 0!==t&&t.length>0?(i(e[0],e[1],e[3],(t[0],t[1],t[3])),i(e[1],e[2],e[3],(t[1],t[2],t[3]))):(i(e[0],e[1],e[3]),i(e[1],e[2],e[3])),void 0!==r&&r.length>0&&(r[0],r[1],r[3],r[1],r[2],r[3]))}/^o /gm.test(r);for(var s,o,l=[],c=[],f=[],d=[],p=/v( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)/,u=/vn( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)/,v=/vt( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)/,_=/f( +-?\d+)( +-?\d+)( +-?\d+)( +-?\d+)?/,g=/f( +(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+))?/,h=/f( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))?/,x=/f( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))?/,y=r.split("\n"),m=0;m<y.length;m++){var b,w=y[m];0!==(w=w.trim()).length&&"#"!==w.charAt(0)&&(null!==(b=p.exec(w))?l.push(t(b[1],b[2],b[3])):null!==(b=u.exec(w))?c.push(t(b[1],b[2],b[3])):null!==(b=v.exec(w))?f.push((s=b[1],o=b[2],new Array(parseFloat(s),parseFloat(o)))):null!==(b=_.exec(w))?a([b[1],b[2],b[3],b[4]]):null!==(b=g.exec(w))?a([b[2],b[5],b[8],b[11]],[b[3],b[6],b[9],b[12]]):null!==(b=h.exec(w))?a([b[2],b[6],b[10],b[14]],[b[3],b[7],b[11],b[15]],[b[4],b[8],b[12],b[16]]):null!==(b=x.exec(w))?a([b[2],b[5],b[8],b[11]],[],[b[3],b[6],b[9],b[12]]):/^o /.test(w)||/^g /.test(w)||/^usemtl /.test(w)||/^mtllib /.test(w)||/^s /.test(w))}return{vertices:l,faces:d,colors:!1}}ArrayBuffer.prototype.slice||(ArrayBuffer.prototype.slice=function(e,r){if(void 0===e&&(e=0),void 0===r&&(r=this.byteLength),e=Math.floor(e),r=Math.floor(r),e<0&&(e+=this.byteLength),r<0&&(r+=this.byteLength),e=Math.min(Math.max(0,e),this.byteLength),(r=Math.min(Math.max(0,r),this.byteLength))-e<=0)return new ArrayBuffer(0);var t=new ArrayBuffer(r-e),n=new Uint8Array(t),i=new Uint8Array(this,e,r-e);return n.set(i),t});