//=========== Stl Viewer v1.13, by Omri Rips, Viewstl.com, July 2021 ; admin@viewstl.com =========== function 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,s="",a=0;ai&&(o=i-a),s+=String.fromCharCode.apply(null,n.subarray(a,a+o))}return s}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 s,a=0,o=r.split(" "),l=[],c=o.length/12-1,f=0;f-1&&(def_red_color=u.getUint8(d+6,!0)/31,def_green_color=u.getUint8(d+7,!0)/31,def_blue_color=u.getUint8(d+8,!0)/31);var v=80;try{var _=u.getUint32(v,!0)}catch(e){return"Can't parse file"}var g=84+50*_;if(e.byteLength!=g)return parse_stl_ascii(e);try{for(v+=4;_--;)v+=12,null==(r=f[[t=u.getFloat32(v,!0),n=u.getFloat32(v+4,!0),i=u.getFloat32(v+8,!0)]])&&(r=l.length,l.push(new Array(t,n,i)),f[[t,n,i]]=r),s=r,v+=12,null==(r=f[[t=u.getFloat32(v,!0),n=u.getFloat32(v+4,!0),i=u.getFloat32(v+8,!0)]])&&(r=l.length,l.push(new Array(t,n,i)),f[[t,n,i]]=r),a=r,v+=12,null==(r=f[[t=u.getFloat32(v,!0),n=u.getFloat32(v+4,!0),i=u.getFloat32(v+8,!0)]])&&(r=l.length,l.push(new Array(t,n,i)),f[[t,n,i]]=r),o=r,d>-1?(v+=12,face_color=u.getUint16(v,!0),32768==face_color||65535==face_color?(color_red=def_red_color,color_green=def_green_color,color_blue=def_blue_color):(p=!1,color_red=(31&face_color)/31,color_green=((992&face_color)>>5)/31,color_blue=((31744&face_color)>>10)/31),c.push(new Array(s,a,o,color_red,color_green,color_blue)),v+=2):(c.push(new Array(s,a,o)),v+=14);return f=null,{vertices:l,faces:c,colors:d>-1&&!p}}catch(e){return"Can't parse file"}}function parse_vf(e){var r=JSON.parse(e),t=[],n=[];try{var s=r.vertices.length;for(i=0;i"!=F.trim())if(""!=F.trim()){if(w)if(U=m.exec(F)){if(!_[U[1]])continue;var N,M=void 0===_[U[1]].v_end_index?x-1:_[U[1]].v_end_index,D=p.exec(F);D&&12==(N=D[2].trim().split(/[ ,]+/)).length&&transform_vertices(t,_[D[1]].v_start_index,M,N);for(j=(i=i.concat(t.slice(_[U[1]].v_start_index,M+1))).length-M-1,M=void 0===_[U[1]].f_end_index?y-1:_[U[1]].f_end_index,C=(T=JSON.parse(JSON.stringify(n.slice(_[U[1]].f_start_index,M+1)))).length;C--;)T[C][0]+=j,T[C][1]+=j,T[C][2]+=j;s=s.concat(T)}else;}else w=!1;else w=!0}}}}}}r({vertices:i,faces:s,colors:a})}function transform_vertices(e,r,t,n){for(var i=[[],[],[],[]],s=0;s<3;s++)i[0][s]=parseFloat(n[s]);i[0].push(0);for(s=3;s<6;s++)i[1][s-3]=parseFloat(n[s]);i[1].push(0);for(s=6;s<9;s++)i[2][s-6]=parseFloat(n[s]);i[2].push(0);for(s=9;s<12;s++)i[3][s-9]=parseFloat(n[s]);i[3].push(1);for(var a=r;a<=t;a++){var o=matrix_multiply([e[a].concat(1)],i);e[a]=[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),s=new Array(t),a=0;a=0?e-1:e+l.length}function i(e,r,t,i){d.push(new Array(n(e),n(r),n(t)))}function s(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 a,o,l=[],c=[],f=[],d=[],u=/v( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)/,p=/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