1//=========== Stl Viewer v1.09, by Omri Rips, Viewstl.com ; admin@viewstl.com ===========
2function parse_3d_file(e,r){switch(e.split(".").pop().split("?")[0].toLowerCase()){case"stl":return parse_stl_bin(r);case"obj":return parse_obj(r);case"vf":return parse_vf(arrayBufferToString(r));default:return"Unknown file type"}}function arrayBufferToString(e,r,t){if("undefined"!=typeof TextDecoder)return new TextDecoder("utf-8").decode(e);for(var a=new Uint8Array(e),n=a.length,o="",l=0;l<n;l+=16383){var c=16383;l+16383>n&&(c=n-l),o+=String.fromCharCode.apply(null,a.subarray(l,l+c))}return o}function parse_stl_ascii(e){try{var r=arrayBufferToString(e),t=[],a=[],n={};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 o,l=0,c=r.split(" "),s=[],i=c.length/12-1,f=0;f<i;f++){s=[];for(var u=0;u<3;u++)f1=parseFloat(c[l+3*u+3]),f2=parseFloat(c[l+3*u+4]),f3=parseFloat(c[l+3*u+5]),null==(o=n[[f1,f2,f3]])&&(o=t.length,t.push(new Array(f1,f2,f3)),n[[f1,f2,f3]]=o),s.push(o);a.push(new Array(s[0],s[1],s[2])),l+=12}return{vertices:t,faces:a,colors:!1}}catch(e){return"Can't parse file"}}function parse_stl_bin(e){var r,t,a,n,o,l,c,s=[],i=[],f={};if(!e)return null;var u=arrayBufferToString(e.slice(0,80)).toLowerCase().indexOf("color"),d=new DataView(e,0),p=!0;u>-1&&(def_red_color=d.getUint8(u+6,!0)/31,def_green_color=d.getUint8(u+7,!0)/31,def_blue_color=d.getUint8(u+8,!0)/31);var g=80;try{var h=d.getUint32(g,!0)}catch(e){return"Can't parse file"}var v=84+50*h;if(e.byteLength!=v)return parse_stl_ascii(e);try{for(g+=4;h--;)g+=12,null==(r=f[[t=d.getFloat32(g,!0),a=d.getFloat32(g+4,!0),n=d.getFloat32(g+8,!0)]])&&(r=s.length,s.push(new Array(t,a,n)),f[[t,a,n]]=r),o=r,g+=12,null==(r=f[[t=d.getFloat32(g,!0),a=d.getFloat32(g+4,!0),n=d.getFloat32(g+8,!0)]])&&(r=s.length,s.push(new Array(t,a,n)),f[[t,a,n]]=r),l=r,g+=12,null==(r=f[[t=d.getFloat32(g,!0),a=d.getFloat32(g+4,!0),n=d.getFloat32(g+8,!0)]])&&(r=s.length,s.push(new Array(t,a,n)),f[[t,a,n]]=r),c=r,u>-1?(g+=12,face_color=d.getUint16(g,!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),i.push(new Array(o,l,c,color_red,color_green,color_blue)),g+=2):(i.push(new Array(o,l,c)),g+=14);return f=null,{vertices:s,faces:i,colors:u>-1&&!p}}catch(e){return"Can't parse file"}}function parse_vf(e){var r=JSON.parse(e),t=[],a=[];try{var n=r.vertices.length;for(i=0;i<n;i++)t.push(new Array(r.vertices[i][0],r.vertices[i][1],r.vertices[i][2]));n=r.faces.length;for(i=0;i<n;i++)a.push(new Array(r.faces[i][0],r.faces[i][1],r.faces[i][2]));return{vertices:t,faces:a,colors:!1}}catch(e){return"Can't parse file"}}function geo_to_vf(e){var r=[],t=[],a=e.vertices.length;for(i=0;i<a;i++)r.push([e.vertices[i].x,e.vertices[i].y,e.vertices[i].z]);a=e.faces.length;for(i=0;i<a;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 a(e){return(e=parseInt(e))>=0?e-1:e+s.length}function n(e,r,t,n){u.push(new Array(a(e),a(r),a(t)))}function o(e,r,t){void 0===e[3]?(n(e[0],e[1],e[2]),void 0!==r&&r.length>0&&(r[0],r[1],r[2])):(void 0!==t&&t.length>0?(n(e[0],e[1],e[3],(t[0],t[1],t[3])),n(e[1],e[2],e[3],(t[1],t[2],t[3]))):(n(e[0],e[1],e[3]),n(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 l,c,s=[],i=[],f=[],u=[],d=/v( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)/,p=/vn( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)/,g=/vt( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)/,h=/f( +-?\d+)( +-?\d+)( +-?\d+)( +-?\d+)?/,v=/f( +(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+))?/,_=/f( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))?/,y=/f( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))?/,w=r.split("\n"),A=0;A<w.length;A++){var b,F=w[A];0!==(F=F.trim()).length&&"#"!==F.charAt(0)&&(null!==(b=d.exec(F))?s.push(t(b[1],b[2],b[3])):null!==(b=p.exec(F))?i.push(t(b[1],b[2],b[3])):null!==(b=g.exec(F))?f.push((l=b[1],c=b[2],new Array(parseFloat(l),parseFloat(c)))):null!==(b=h.exec(F))?o([b[1],b[2],b[3],b[4]]):null!==(b=v.exec(F))?o([b[2],b[5],b[8],b[11]],[b[3],b[6],b[9],b[12]]):null!==(b=_.exec(F))?o([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=y.exec(F))?o([b[2],b[5],b[8],b[11]],[],[b[3],b[6],b[9],b[12]]):/^o /.test(F)||/^g /.test(F)||/^usemtl /.test(F)||/^mtllib /.test(F)||/^s /.test(F))}return{vertices:s,faces:u,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),a=new Uint8Array(t),n=new Uint8Array(this,e,r-e);return a.set(n),t});