deblan.io-murph/public/stl_viewer/parser.min.js

2 lines
9.6 KiB
JavaScript
Raw Normal View History

2021-07-09 23:42:38 +02:00
//=========== 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;a<i;a+=16383){var o=16383;a+16383>i&&(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<c;f++){l=[];for(var d=0;d<3;d++)f1=parseFloat(o[a+3*d+3]),f2=parseFloat(o[a+3*d+4]),f3=parseFloat(o[a+3*d+5]),null==(s=i[[f1,f2,f3]])&&(s=t.length,t.push(new Array(f1,f2,f3)),i[[f1,f2,f3]]=s),l.push(s);n.push(new Array(l[0],l[1],l[2])),a+=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,s,a,o,l=[],c=[],f={};if(!e)return null;var d=arrayBufferToString(e.slice(0,80)).toLowerCase().indexOf("color"),u=new DataView(e,0),p=!0;d>-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<s;i++)t.push(new Array(r.vertices[i][0],r.vertices[i][1],r.vertices[i][2]));s=r.faces.length;for(i=0;i<s;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=[],s=[],a=!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,u=/<component\s+.*objectid=([0-9\"]+)/i,p=/item\s+.*objectid=(