!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("three")):"function"==typeof define&&define.amd?define(["exports","three"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).GCodePreview={},t.THREE)}(this,(function(t,e){"use strict";function n(t,e,n,i){return new(n||(n=Promise))((function(o,r){function s(t){try{l(i.next(t))}catch(t){r(t)}}function a(t){try{l(i.throw(t))}catch(t){r(t)}}function l(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,a)}l((i=i.apply(t,e||[])).next())}))}class i{constructor(t,e,n,i){this.size=t,this.width=e,this.height=n,this.charLength=i,this.chars=""}static parse(t){const e=t.split(" "),n=e[0],o=n.split("x");return new i(n,+o[0],+o[1],+e[1])}get src(){return"data:image/jpeg;base64,"+this.chars}get isValid(){return this.chars.length==this.charLength&&/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(this.chars)}}class o{constructor(t,e,n,i){this.src=t,this.gcode=e,this.params=n,this.comment=i}}class r extends o{constructor(t,e,n,i){super(t,e,n,i),this.params=n}}class s{constructor(t,e,n){this.layer=t,this.commands=e,this.lineNumber=n}}class a{constructor(t){this.lines=[],this.preamble=new s(-1,[],0),this.layers=[],this.curZ=0,this.maxZ=-1/0,this.metadata={thumbnails:{}},this.tolerance=0,this.tolerance=null!=t?t:this.tolerance}parseGCode(t){const e=Array.isArray(t)?t:t.split("\n");this.lines=this.lines.concat(e);const n=this.lines2commands(e);this.groupIntoLayers(n);const i=this.parseMetadata(n.filter((t=>t.comment))).thumbnails;for(const[t,e]of Object.entries(i))this.metadata.thumbnails[t]=e;return{layers:this.layers,metadata:this.metadata}}lines2commands(t){return t.map((t=>this.parseCommand(t))).filter((t=>null!=t))}parseCommand(t,e=!0){const n=t.trim().split(";"),i=n[0],s=e&&n[1]||void 0,a=i.split(/ +/g),l=a[0].toLowerCase();let c;switch(l){case"g0":case"g00":case"g1":case"g01":case"g2":case"g02":case"g3":case"g03":return c=this.parseMove(a.slice(1)),new r(t,l,c,s);default:return c=this.parseParams(a.slice(1)),new o(t,l,c,s)}}parseMove(t){return t.reduce(((t,e)=>{const n=e.charAt(0).toLowerCase();return"x"!=n&&"y"!=n&&"z"!=n&&"e"!=n&&"r"!=n&&"f"!=n&&"i"!=n&&"j"!=n||(t[n]=parseFloat(e.slice(1))),t}),{})}isAlpha(t){const e=t.charCodeAt(0);return e>=97&&e<=122||e>=65&&e<=90}parseParams(t){return t.reduce(((t,e)=>{const n=e.charAt(0).toLowerCase();return this.isAlpha(n)&&(t[n]=parseFloat(e.slice(1))),t}),{})}groupIntoLayers(t){var e;for(let n=0;n0&&(null!=t.x||null!=t.y)&&Math.abs(this.curZ-this.maxZ)>this.tolerance&&(this.maxZ=this.curZ,this.layers.push(new s(this.layers.length,[],n)))}this.maxLayer.commands.push(i)}return this.layers}get maxLayer(){var t;return null!==(t=this.layers[this.layers.length-1])&&void 0!==t?t:this.preamble}parseMetadata(t){const e={};let n;for(const o of t){const t=o.comment;if(!t)continue;const r=t.indexOf("thumbnail begin"),s=t.indexOf("thumbnail end");r>-1?n=i.parse(t.slice(r+15).trim()):n&&(-1==s?n.chars+=t.trim():(n.isValid&&(e[n.size]=n),n=void 0))}return{thumbnails:e}}}a.prototype.parseGcode=a.prototype.parseGCode;const l={type:"change"},c={type:"start"},d={type:"end"},u=new e.Ray,h=new e.Plane,p=Math.cos(70*e.MathUtils.DEG2RAD);class m extends e.EventDispatcher{constructor(t,n){super(),this.object=t,this.domElement=n,this.domElement.style.touchAction="none",this.enabled=!0,this.target=new e.Vector3,this.minDistance=0,this.maxDistance=1/0,this.minZoom=0,this.maxZoom=1/0,this.minPolarAngle=0,this.maxPolarAngle=Math.PI,this.minAzimuthAngle=-1/0,this.maxAzimuthAngle=1/0,this.enableDamping=!1,this.dampingFactor=.05,this.enableZoom=!0,this.zoomSpeed=1,this.enableRotate=!0,this.rotateSpeed=1,this.enablePan=!0,this.panSpeed=1,this.screenSpacePanning=!0,this.keyPanSpeed=7,this.zoomToCursor=!1,this.autoRotate=!1,this.autoRotateSpeed=2,this.keys={LEFT:"ArrowLeft",UP:"ArrowUp",RIGHT:"ArrowRight",BOTTOM:"ArrowDown"},this.mouseButtons={LEFT:e.MOUSE.ROTATE,MIDDLE:e.MOUSE.DOLLY,RIGHT:e.MOUSE.PAN},this.touches={ONE:e.TOUCH.ROTATE,TWO:e.TOUCH.DOLLY_PAN},this.target0=this.target.clone(),this.position0=this.object.position.clone(),this.zoom0=this.object.zoom,this._domElementKeyEvents=null,this.getPolarAngle=function(){return a.phi},this.getAzimuthalAngle=function(){return a.theta},this.getDistance=function(){return this.object.position.distanceTo(this.target)},this.listenToKeyEvents=function(t){t.addEventListener("keydown",$),this._domElementKeyEvents=t},this.stopListenToKeyEvents=function(){this._domElementKeyEvents.removeEventListener("keydown",$),this._domElementKeyEvents=null},this.saveState=function(){i.target0.copy(i.target),i.position0.copy(i.object.position),i.zoom0=i.object.zoom},this.reset=function(){i.target.copy(i.target0),i.object.position.copy(i.position0),i.object.zoom=i.zoom0,i.object.updateProjectionMatrix(),i.dispatchEvent(l),i.update(),r=o.NONE},this.update=function(){const n=new e.Vector3,c=(new e.Quaternion).setFromUnitVectors(t.up,new e.Vector3(0,1,0)),d=c.clone().invert(),v=new e.Vector3,b=new e.Quaternion,y=new e.Vector3,w=2*Math.PI;return function(){const x=i.object.position;n.copy(x).sub(i.target),n.applyQuaternion(c),a.setFromVector3(n),i.autoRotate&&r===o.NONE&&P(2*Math.PI/60/60*i.autoRotateSpeed),i.enableDamping?(a.theta+=m.theta*i.dampingFactor,a.phi+=m.phi*i.dampingFactor):(a.theta+=m.theta,a.phi+=m.phi);let S=i.minAzimuthAngle,E=i.maxAzimuthAngle;isFinite(S)&&isFinite(E)&&(S<-Math.PI?S+=w:S>Math.PI&&(S-=w),E<-Math.PI?E+=w:E>Math.PI&&(E-=w),a.theta=S<=E?Math.max(S,Math.min(E,a.theta)):a.theta>(S+E)/2?Math.max(S,a.theta):Math.min(E,a.theta)),a.phi=Math.max(i.minPolarAngle,Math.min(i.maxPolarAngle,a.phi)),a.makeSafe(),!0===i.enableDamping?i.target.addScaledVector(g,i.dampingFactor):i.target.add(g),i.zoomToCursor&&T||i.object.isOrthographicCamera?a.radius=R(a.radius):a.radius=R(a.radius*f),n.setFromSpherical(a),n.applyQuaternion(d),x.copy(i.target).add(n),i.object.lookAt(i.target),!0===i.enableDamping?(m.theta*=1-i.dampingFactor,m.phi*=1-i.dampingFactor,g.multiplyScalar(1-i.dampingFactor)):(m.set(0,0,0),g.set(0,0,0));let L=!1;if(i.zoomToCursor&&T){let o=null;if(i.object.isPerspectiveCamera){const t=n.length();o=R(t*f);const e=t-o;i.object.position.addScaledVector(C,e),i.object.updateMatrixWorld()}else if(i.object.isOrthographicCamera){const t=new e.Vector3(M.x,M.y,0);t.unproject(i.object),i.object.zoom=Math.max(i.minZoom,Math.min(i.maxZoom,i.object.zoom/f)),i.object.updateProjectionMatrix(),L=!0;const r=new e.Vector3(M.x,M.y,0);r.unproject(i.object),i.object.position.sub(r).add(t),i.object.updateMatrixWorld(),o=n.length()}else console.warn("WARNING: OrbitControls.js encountered an unknown camera type - zoom to cursor disabled."),i.zoomToCursor=!1;null!==o&&(this.screenSpacePanning?i.target.set(0,0,-1).transformDirection(i.object.matrix).multiplyScalar(o).add(i.object.position):(u.origin.copy(i.object.position),u.direction.set(0,0,-1).transformDirection(i.object.matrix),Math.abs(i.object.up.dot(u.direction))s||8*(1-b.dot(i.object.quaternion))>s||y.distanceToSquared(i.target)>0)&&(i.dispatchEvent(l),v.copy(i.object.position),b.copy(i.object.quaternion),y.copy(i.target),L=!1,!0)}}(),this.dispose=function(){i.domElement.removeEventListener("contextmenu",tt),i.domElement.removeEventListener("pointerdown",X),i.domElement.removeEventListener("pointercancel",Q),i.domElement.removeEventListener("wheel",J),i.domElement.removeEventListener("pointermove",q),i.domElement.removeEventListener("pointerup",Q),null!==i._domElementKeyEvents&&(i._domElementKeyEvents.removeEventListener("keydown",$),i._domElementKeyEvents=null)};const i=this,o={NONE:-1,ROTATE:0,DOLLY:1,PAN:2,TOUCH_ROTATE:3,TOUCH_PAN:4,TOUCH_DOLLY_PAN:5,TOUCH_DOLLY_ROTATE:6};let r=o.NONE;const s=1e-6,a=new e.Spherical,m=new e.Spherical;let f=1;const g=new e.Vector3,v=new e.Vector2,b=new e.Vector2,y=new e.Vector2,w=new e.Vector2,x=new e.Vector2,S=new e.Vector2,E=new e.Vector2,L=new e.Vector2,A=new e.Vector2,C=new e.Vector3,M=new e.Vector2;let T=!1;const _=[],O={};function z(){return Math.pow(.95,i.zoomSpeed)}function P(t){m.theta-=t}function j(t){m.phi-=t}const D=function(){const t=new e.Vector3;return function(e,n){t.setFromMatrixColumn(n,0),t.multiplyScalar(-e),g.add(t)}}(),I=function(){const t=new e.Vector3;return function(e,n){!0===i.screenSpacePanning?t.setFromMatrixColumn(n,1):(t.setFromMatrixColumn(n,0),t.crossVectors(i.object.up,t)),t.multiplyScalar(e),g.add(t)}}(),U=function(){const t=new e.Vector3;return function(e,n){const o=i.domElement;if(i.object.isPerspectiveCamera){const r=i.object.position;t.copy(r).sub(i.target);let s=t.length();s*=Math.tan(i.object.fov/2*Math.PI/180),D(2*e*s/o.clientHeight,i.object.matrix),I(2*n*s/o.clientHeight,i.object.matrix)}else i.object.isOrthographicCamera?(D(e*(i.object.right-i.object.left)/i.object.zoom/o.clientWidth,i.object.matrix),I(n*(i.object.top-i.object.bottom)/i.object.zoom/o.clientHeight,i.object.matrix)):(console.warn("WARNING: OrbitControls.js encountered an unknown camera type - pan disabled."),i.enablePan=!1)}}();function B(t){i.object.isPerspectiveCamera||i.object.isOrthographicCamera?f/=t:(console.warn("WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled."),i.enableZoom=!1)}function V(t){i.object.isPerspectiveCamera||i.object.isOrthographicCamera?f*=t:(console.warn("WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled."),i.enableZoom=!1)}function N(e){if(!i.zoomToCursor)return;T=!0;const n=i.domElement.getBoundingClientRect(),o=e.clientX-n.left,r=e.clientY-n.top,s=n.width,a=n.height;M.x=o/s*2-1,M.y=-r/a*2+1,C.set(M.x,M.y,1).unproject(t).sub(t.position).normalize()}function R(t){return Math.max(i.minDistance,Math.min(i.maxDistance,t))}function H(t){v.set(t.clientX,t.clientY)}function k(t){w.set(t.clientX,t.clientY)}function F(){if(1===_.length)v.set(_[0].pageX,_[0].pageY);else{const t=.5*(_[0].pageX+_[1].pageX),e=.5*(_[0].pageY+_[1].pageY);v.set(t,e)}}function G(){if(1===_.length)w.set(_[0].pageX,_[0].pageY);else{const t=.5*(_[0].pageX+_[1].pageX),e=.5*(_[0].pageY+_[1].pageY);w.set(t,e)}}function Y(){const t=_[0].pageX-_[1].pageX,e=_[0].pageY-_[1].pageY,n=Math.sqrt(t*t+e*e);E.set(0,n)}function W(t){if(1==_.length)b.set(t.pageX,t.pageY);else{const e=nt(t),n=.5*(t.pageX+e.x),i=.5*(t.pageY+e.y);b.set(n,i)}y.subVectors(b,v).multiplyScalar(i.rotateSpeed);const e=i.domElement;P(2*Math.PI*y.x/e.clientHeight),j(2*Math.PI*y.y/e.clientHeight),v.copy(b)}function Z(t){if(1===_.length)x.set(t.pageX,t.pageY);else{const e=nt(t),n=.5*(t.pageX+e.x),i=.5*(t.pageY+e.y);x.set(n,i)}S.subVectors(x,w).multiplyScalar(i.panSpeed),U(S.x,S.y),w.copy(x)}function K(t){const e=nt(t),n=t.pageX-e.x,o=t.pageY-e.y,r=Math.sqrt(n*n+o*o);L.set(0,r),A.set(0,Math.pow(L.y/E.y,i.zoomSpeed)),B(A.y),E.copy(L)}function X(t){!1!==i.enabled&&(0===_.length&&(i.domElement.setPointerCapture(t.pointerId),i.domElement.addEventListener("pointermove",q),i.domElement.addEventListener("pointerup",Q)),function(t){_.push(t)}(t),"touch"===t.pointerType?function(t){switch(et(t),_.length){case 1:switch(i.touches.ONE){case e.TOUCH.ROTATE:if(!1===i.enableRotate)return;F(),r=o.TOUCH_ROTATE;break;case e.TOUCH.PAN:if(!1===i.enablePan)return;G(),r=o.TOUCH_PAN;break;default:r=o.NONE}break;case 2:switch(i.touches.TWO){case e.TOUCH.DOLLY_PAN:if(!1===i.enableZoom&&!1===i.enablePan)return;i.enableZoom&&Y(),i.enablePan&&G(),r=o.TOUCH_DOLLY_PAN;break;case e.TOUCH.DOLLY_ROTATE:if(!1===i.enableZoom&&!1===i.enableRotate)return;i.enableZoom&&Y(),i.enableRotate&&F(),r=o.TOUCH_DOLLY_ROTATE;break;default:r=o.NONE}break;default:r=o.NONE}r!==o.NONE&&i.dispatchEvent(c)}(t):function(t){let n;switch(t.button){case 0:n=i.mouseButtons.LEFT;break;case 1:n=i.mouseButtons.MIDDLE;break;case 2:n=i.mouseButtons.RIGHT;break;default:n=-1}switch(n){case e.MOUSE.DOLLY:if(!1===i.enableZoom)return;!function(t){N(t),E.set(t.clientX,t.clientY)}(t),r=o.DOLLY;break;case e.MOUSE.ROTATE:if(t.ctrlKey||t.metaKey||t.shiftKey){if(!1===i.enablePan)return;k(t),r=o.PAN}else{if(!1===i.enableRotate)return;H(t),r=o.ROTATE}break;case e.MOUSE.PAN:if(t.ctrlKey||t.metaKey||t.shiftKey){if(!1===i.enableRotate)return;H(t),r=o.ROTATE}else{if(!1===i.enablePan)return;k(t),r=o.PAN}break;default:r=o.NONE}r!==o.NONE&&i.dispatchEvent(c)}(t))}function q(t){!1!==i.enabled&&("touch"===t.pointerType?function(t){switch(et(t),r){case o.TOUCH_ROTATE:if(!1===i.enableRotate)return;W(t),i.update();break;case o.TOUCH_PAN:if(!1===i.enablePan)return;Z(t),i.update();break;case o.TOUCH_DOLLY_PAN:if(!1===i.enableZoom&&!1===i.enablePan)return;!function(t){i.enableZoom&&K(t),i.enablePan&&Z(t)}(t),i.update();break;case o.TOUCH_DOLLY_ROTATE:if(!1===i.enableZoom&&!1===i.enableRotate)return;!function(t){i.enableZoom&&K(t),i.enableRotate&&W(t)}(t),i.update();break;default:r=o.NONE}}(t):function(t){switch(r){case o.ROTATE:if(!1===i.enableRotate)return;!function(t){b.set(t.clientX,t.clientY),y.subVectors(b,v).multiplyScalar(i.rotateSpeed);const e=i.domElement;P(2*Math.PI*y.x/e.clientHeight),j(2*Math.PI*y.y/e.clientHeight),v.copy(b),i.update()}(t);break;case o.DOLLY:if(!1===i.enableZoom)return;!function(t){L.set(t.clientX,t.clientY),A.subVectors(L,E),A.y>0?B(z()):A.y<0&&V(z()),E.copy(L),i.update()}(t);break;case o.PAN:if(!1===i.enablePan)return;!function(t){x.set(t.clientX,t.clientY),S.subVectors(x,w).multiplyScalar(i.panSpeed),U(S.x,S.y),w.copy(x),i.update()}(t)}}(t))}function Q(t){!function(t){delete O[t.pointerId];for(let e=0;e<_.length;e++)if(_[e].pointerId==t.pointerId)return void _.splice(e,1)}(t),0===_.length&&(i.domElement.releasePointerCapture(t.pointerId),i.domElement.removeEventListener("pointermove",q),i.domElement.removeEventListener("pointerup",Q)),i.dispatchEvent(d),r=o.NONE}function J(t){!1!==i.enabled&&!1!==i.enableZoom&&r===o.NONE&&(t.preventDefault(),i.dispatchEvent(c),function(t){N(t),t.deltaY<0?V(z()):t.deltaY>0&&B(z()),i.update()}(t),i.dispatchEvent(d))}function $(t){!1!==i.enabled&&!1!==i.enablePan&&function(t){let e=!1;switch(t.code){case i.keys.UP:t.ctrlKey||t.metaKey||t.shiftKey?j(2*Math.PI*i.rotateSpeed/i.domElement.clientHeight):U(0,i.keyPanSpeed),e=!0;break;case i.keys.BOTTOM:t.ctrlKey||t.metaKey||t.shiftKey?j(-2*Math.PI*i.rotateSpeed/i.domElement.clientHeight):U(0,-i.keyPanSpeed),e=!0;break;case i.keys.LEFT:t.ctrlKey||t.metaKey||t.shiftKey?P(2*Math.PI*i.rotateSpeed/i.domElement.clientHeight):U(i.keyPanSpeed,0),e=!0;break;case i.keys.RIGHT:t.ctrlKey||t.metaKey||t.shiftKey?P(-2*Math.PI*i.rotateSpeed/i.domElement.clientHeight):U(-i.keyPanSpeed,0),e=!0}e&&(t.preventDefault(),i.update())}(t)}function tt(t){!1!==i.enabled&&t.preventDefault()}function et(t){let n=O[t.pointerId];void 0===n&&(n=new e.Vector2,O[t.pointerId]=n),n.set(t.pageX,t.pageY)}function nt(t){const e=t.pointerId===_[0].pointerId?_[1]:_[0];return O[e.pointerId]}i.domElement.addEventListener("contextmenu",tt),i.domElement.addEventListener("pointerdown",X),i.domElement.addEventListener("pointercancel",Q),i.domElement.addEventListener("wheel",J,{passive:!1}),this.update()}}e.UniformsLib.line={worldUnits:{value:1},linewidth:{value:1},resolution:{value:new e.Vector2(1,1)},dashOffset:{value:0},dashScale:{value:1},dashSize:{value:1},gapSize:{value:1}},e.ShaderLib.line={uniforms:e.UniformsUtils.merge([e.UniformsLib.common,e.UniformsLib.fog,e.UniformsLib.line]),vertexShader:"\n\t\t#include \n\t\t#include \n\t\t#include \n\t\t#include \n\t\t#include \n\n\t\tuniform float linewidth;\n\t\tuniform vec2 resolution;\n\n\t\tattribute vec3 instanceStart;\n\t\tattribute vec3 instanceEnd;\n\n\t\tattribute vec3 instanceColorStart;\n\t\tattribute vec3 instanceColorEnd;\n\n\t\t#ifdef WORLD_UNITS\n\n\t\t\tvarying vec4 worldPos;\n\t\t\tvarying vec3 worldStart;\n\t\t\tvarying vec3 worldEnd;\n\n\t\t\t#ifdef USE_DASH\n\n\t\t\t\tvarying vec2 vUv;\n\n\t\t\t#endif\n\n\t\t#else\n\n\t\t\tvarying vec2 vUv;\n\n\t\t#endif\n\n\t\t#ifdef USE_DASH\n\n\t\t\tuniform float dashScale;\n\t\t\tattribute float instanceDistanceStart;\n\t\t\tattribute float instanceDistanceEnd;\n\t\t\tvarying float vLineDistance;\n\n\t\t#endif\n\n\t\tvoid trimSegment( const in vec4 start, inout vec4 end ) {\n\n\t\t\t// trim end segment so it terminates between the camera plane and the near plane\n\n\t\t\t// conservative estimate of the near plane\n\t\t\tfloat a = projectionMatrix[ 2 ][ 2 ]; // 3nd entry in 3th column\n\t\t\tfloat b = projectionMatrix[ 3 ][ 2 ]; // 3nd entry in 4th column\n\t\t\tfloat nearEstimate = - 0.5 * b / a;\n\n\t\t\tfloat alpha = ( nearEstimate - start.z ) / ( end.z - start.z );\n\n\t\t\tend.xyz = mix( start.xyz, end.xyz, alpha );\n\n\t\t}\n\n\t\tvoid main() {\n\n\t\t\t#ifdef USE_COLOR\n\n\t\t\t\tvColor.xyz = ( position.y < 0.5 ) ? instanceColorStart : instanceColorEnd;\n\n\t\t\t#endif\n\n\t\t\t#ifdef USE_DASH\n\n\t\t\t\tvLineDistance = ( position.y < 0.5 ) ? dashScale * instanceDistanceStart : dashScale * instanceDistanceEnd;\n\t\t\t\tvUv = uv;\n\n\t\t\t#endif\n\n\t\t\tfloat aspect = resolution.x / resolution.y;\n\n\t\t\t// camera space\n\t\t\tvec4 start = modelViewMatrix * vec4( instanceStart, 1.0 );\n\t\t\tvec4 end = modelViewMatrix * vec4( instanceEnd, 1.0 );\n\n\t\t\t#ifdef WORLD_UNITS\n\n\t\t\t\tworldStart = start.xyz;\n\t\t\t\tworldEnd = end.xyz;\n\n\t\t\t#else\n\n\t\t\t\tvUv = uv;\n\n\t\t\t#endif\n\n\t\t\t// special case for perspective projection, and segments that terminate either in, or behind, the camera plane\n\t\t\t// clearly the gpu firmware has a way of addressing this issue when projecting into ndc space\n\t\t\t// but we need to perform ndc-space calculations in the shader, so we must address this issue directly\n\t\t\t// perhaps there is a more elegant solution -- WestLangley\n\n\t\t\tbool perspective = ( projectionMatrix[ 2 ][ 3 ] == - 1.0 ); // 4th entry in the 3rd column\n\n\t\t\tif ( perspective ) {\n\n\t\t\t\tif ( start.z < 0.0 && end.z >= 0.0 ) {\n\n\t\t\t\t\ttrimSegment( start, end );\n\n\t\t\t\t} else if ( end.z < 0.0 && start.z >= 0.0 ) {\n\n\t\t\t\t\ttrimSegment( end, start );\n\n\t\t\t\t}\n\n\t\t\t}\n\n\t\t\t// clip space\n\t\t\tvec4 clipStart = projectionMatrix * start;\n\t\t\tvec4 clipEnd = projectionMatrix * end;\n\n\t\t\t// ndc space\n\t\t\tvec3 ndcStart = clipStart.xyz / clipStart.w;\n\t\t\tvec3 ndcEnd = clipEnd.xyz / clipEnd.w;\n\n\t\t\t// direction\n\t\t\tvec2 dir = ndcEnd.xy - ndcStart.xy;\n\n\t\t\t// account for clip-space aspect ratio\n\t\t\tdir.x *= aspect;\n\t\t\tdir = normalize( dir );\n\n\t\t\t#ifdef WORLD_UNITS\n\n\t\t\t\t// get the offset direction as perpendicular to the view vector\n\t\t\t\tvec3 worldDir = normalize( end.xyz - start.xyz );\n\t\t\t\tvec3 offset;\n\t\t\t\tif ( position.y < 0.5 ) {\n\n\t\t\t\t\toffset = normalize( cross( start.xyz, worldDir ) );\n\n\t\t\t\t} else {\n\n\t\t\t\t\toffset = normalize( cross( end.xyz, worldDir ) );\n\n\t\t\t\t}\n\n\t\t\t\t// sign flip\n\t\t\t\tif ( position.x < 0.0 ) offset *= - 1.0;\n\n\t\t\t\tfloat forwardOffset = dot( worldDir, vec3( 0.0, 0.0, 1.0 ) );\n\n\t\t\t\t// don't extend the line if we're rendering dashes because we\n\t\t\t\t// won't be rendering the endcaps\n\t\t\t\t#ifndef USE_DASH\n\n\t\t\t\t\t// extend the line bounds to encompass endcaps\n\t\t\t\t\tstart.xyz += - worldDir * linewidth * 0.5;\n\t\t\t\t\tend.xyz += worldDir * linewidth * 0.5;\n\n\t\t\t\t\t// shift the position of the quad so it hugs the forward edge of the line\n\t\t\t\t\toffset.xy -= dir * forwardOffset;\n\t\t\t\t\toffset.z += 0.5;\n\n\t\t\t\t#endif\n\n\t\t\t\t// endcaps\n\t\t\t\tif ( position.y > 1.0 || position.y < 0.0 ) {\n\n\t\t\t\t\toffset.xy += dir * 2.0 * forwardOffset;\n\n\t\t\t\t}\n\n\t\t\t\t// adjust for linewidth\n\t\t\t\toffset *= linewidth * 0.5;\n\n\t\t\t\t// set the world position\n\t\t\t\tworldPos = ( position.y < 0.5 ) ? start : end;\n\t\t\t\tworldPos.xyz += offset;\n\n\t\t\t\t// project the worldpos\n\t\t\t\tvec4 clip = projectionMatrix * worldPos;\n\n\t\t\t\t// shift the depth of the projected points so the line\n\t\t\t\t// segments overlap neatly\n\t\t\t\tvec3 clipPose = ( position.y < 0.5 ) ? ndcStart : ndcEnd;\n\t\t\t\tclip.z = clipPose.z * clip.w;\n\n\t\t\t#else\n\n\t\t\t\tvec2 offset = vec2( dir.y, - dir.x );\n\t\t\t\t// undo aspect ratio adjustment\n\t\t\t\tdir.x /= aspect;\n\t\t\t\toffset.x /= aspect;\n\n\t\t\t\t// sign flip\n\t\t\t\tif ( position.x < 0.0 ) offset *= - 1.0;\n\n\t\t\t\t// endcaps\n\t\t\t\tif ( position.y < 0.0 ) {\n\n\t\t\t\t\toffset += - dir;\n\n\t\t\t\t} else if ( position.y > 1.0 ) {\n\n\t\t\t\t\toffset += dir;\n\n\t\t\t\t}\n\n\t\t\t\t// adjust for linewidth\n\t\t\t\toffset *= linewidth;\n\n\t\t\t\t// adjust for clip-space to screen-space conversion // maybe resolution should be based on viewport ...\n\t\t\t\toffset /= resolution.y;\n\n\t\t\t\t// select end\n\t\t\t\tvec4 clip = ( position.y < 0.5 ) ? clipStart : clipEnd;\n\n\t\t\t\t// back to clip space\n\t\t\t\toffset *= clip.w;\n\n\t\t\t\tclip.xy += offset;\n\n\t\t\t#endif\n\n\t\t\tgl_Position = clip;\n\n\t\t\tvec4 mvPosition = ( position.y < 0.5 ) ? start : end; // this is an approximation\n\n\t\t\t#include \n\t\t\t#include \n\t\t\t#include \n\n\t\t}\n\t\t",fragmentShader:"\n\t\tuniform vec3 diffuse;\n\t\tuniform float opacity;\n\t\tuniform float linewidth;\n\n\t\t#ifdef USE_DASH\n\n\t\t\tuniform float dashOffset;\n\t\t\tuniform float dashSize;\n\t\t\tuniform float gapSize;\n\n\t\t#endif\n\n\t\tvarying float vLineDistance;\n\n\t\t#ifdef WORLD_UNITS\n\n\t\t\tvarying vec4 worldPos;\n\t\t\tvarying vec3 worldStart;\n\t\t\tvarying vec3 worldEnd;\n\n\t\t\t#ifdef USE_DASH\n\n\t\t\t\tvarying vec2 vUv;\n\n\t\t\t#endif\n\n\t\t#else\n\n\t\t\tvarying vec2 vUv;\n\n\t\t#endif\n\n\t\t#include \n\t\t#include \n\t\t#include \n\t\t#include \n\t\t#include \n\n\t\tvec2 closestLineToLine(vec3 p1, vec3 p2, vec3 p3, vec3 p4) {\n\n\t\t\tfloat mua;\n\t\t\tfloat mub;\n\n\t\t\tvec3 p13 = p1 - p3;\n\t\t\tvec3 p43 = p4 - p3;\n\n\t\t\tvec3 p21 = p2 - p1;\n\n\t\t\tfloat d1343 = dot( p13, p43 );\n\t\t\tfloat d4321 = dot( p43, p21 );\n\t\t\tfloat d1321 = dot( p13, p21 );\n\t\t\tfloat d4343 = dot( p43, p43 );\n\t\t\tfloat d2121 = dot( p21, p21 );\n\n\t\t\tfloat denom = d2121 * d4343 - d4321 * d4321;\n\n\t\t\tfloat numer = d1343 * d4321 - d1321 * d4343;\n\n\t\t\tmua = numer / denom;\n\t\t\tmua = clamp( mua, 0.0, 1.0 );\n\t\t\tmub = ( d1343 + d4321 * ( mua ) ) / d4343;\n\t\t\tmub = clamp( mub, 0.0, 1.0 );\n\n\t\t\treturn vec2( mua, mub );\n\n\t\t}\n\n\t\tvoid main() {\n\n\t\t\t#include \n\n\t\t\t#ifdef USE_DASH\n\n\t\t\t\tif ( vUv.y < - 1.0 || vUv.y > 1.0 ) discard; // discard endcaps\n\n\t\t\t\tif ( mod( vLineDistance + dashOffset, dashSize + gapSize ) > dashSize ) discard; // todo - FIX\n\n\t\t\t#endif\n\n\t\t\tfloat alpha = opacity;\n\n\t\t\t#ifdef WORLD_UNITS\n\n\t\t\t\t// Find the closest points on the view ray and the line segment\n\t\t\t\tvec3 rayEnd = normalize( worldPos.xyz ) * 1e5;\n\t\t\t\tvec3 lineDir = worldEnd - worldStart;\n\t\t\t\tvec2 params = closestLineToLine( worldStart, worldEnd, vec3( 0.0, 0.0, 0.0 ), rayEnd );\n\n\t\t\t\tvec3 p1 = worldStart + lineDir * params.x;\n\t\t\t\tvec3 p2 = rayEnd * params.y;\n\t\t\t\tvec3 delta = p1 - p2;\n\t\t\t\tfloat len = length( delta );\n\t\t\t\tfloat norm = len / linewidth;\n\n\t\t\t\t#ifndef USE_DASH\n\n\t\t\t\t\t#ifdef USE_ALPHA_TO_COVERAGE\n\n\t\t\t\t\t\tfloat dnorm = fwidth( norm );\n\t\t\t\t\t\talpha = 1.0 - smoothstep( 0.5 - dnorm, 0.5 + dnorm, norm );\n\n\t\t\t\t\t#else\n\n\t\t\t\t\t\tif ( norm > 0.5 ) {\n\n\t\t\t\t\t\t\tdiscard;\n\n\t\t\t\t\t\t}\n\n\t\t\t\t\t#endif\n\n\t\t\t\t#endif\n\n\t\t\t#else\n\n\t\t\t\t#ifdef USE_ALPHA_TO_COVERAGE\n\n\t\t\t\t\t// artifacts appear on some hardware if a derivative is taken within a conditional\n\t\t\t\t\tfloat a = vUv.x;\n\t\t\t\t\tfloat b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0;\n\t\t\t\t\tfloat len2 = a * a + b * b;\n\t\t\t\t\tfloat dlen = fwidth( len2 );\n\n\t\t\t\t\tif ( abs( vUv.y ) > 1.0 ) {\n\n\t\t\t\t\t\talpha = 1.0 - smoothstep( 1.0 - dlen, 1.0 + dlen, len2 );\n\n\t\t\t\t\t}\n\n\t\t\t\t#else\n\n\t\t\t\t\tif ( abs( vUv.y ) > 1.0 ) {\n\n\t\t\t\t\t\tfloat a = vUv.x;\n\t\t\t\t\t\tfloat b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0;\n\t\t\t\t\t\tfloat len2 = a * a + b * b;\n\n\t\t\t\t\t\tif ( len2 > 1.0 ) discard;\n\n\t\t\t\t\t}\n\n\t\t\t\t#endif\n\n\t\t\t#endif\n\n\t\t\tvec4 diffuseColor = vec4( diffuse, alpha );\n\n\t\t\t#include \n\t\t\t#include \n\n\t\t\tgl_FragColor = vec4( diffuseColor.rgb, alpha );\n\n\t\t\t#include \n\t\t\t#include \n\t\t\t#include \n\t\t\t#include \n\n\t\t}\n\t\t"};class f extends e.ShaderMaterial{constructor(t){super({type:"LineMaterial",uniforms:e.UniformsUtils.clone(e.ShaderLib.line.uniforms),vertexShader:e.ShaderLib.line.vertexShader,fragmentShader:e.ShaderLib.line.fragmentShader,clipping:!0}),this.isLineMaterial=!0,Object.defineProperties(this,{color:{enumerable:!0,get:function(){return this.uniforms.diffuse.value},set:function(t){this.uniforms.diffuse.value=t}},worldUnits:{enumerable:!0,get:function(){return"WORLD_UNITS"in this.defines},set:function(t){!0===t?this.defines.WORLD_UNITS="":delete this.defines.WORLD_UNITS}},linewidth:{enumerable:!0,get:function(){return this.uniforms.linewidth.value},set:function(t){this.uniforms.linewidth.value=t}},dashed:{enumerable:!0,get:function(){return Boolean("USE_DASH"in this.defines)},set(t){Boolean(t)!==Boolean("USE_DASH"in this.defines)&&(this.needsUpdate=!0),!0===t?this.defines.USE_DASH="":delete this.defines.USE_DASH}},dashScale:{enumerable:!0,get:function(){return this.uniforms.dashScale.value},set:function(t){this.uniforms.dashScale.value=t}},dashSize:{enumerable:!0,get:function(){return this.uniforms.dashSize.value},set:function(t){this.uniforms.dashSize.value=t}},dashOffset:{enumerable:!0,get:function(){return this.uniforms.dashOffset.value},set:function(t){this.uniforms.dashOffset.value=t}},gapSize:{enumerable:!0,get:function(){return this.uniforms.gapSize.value},set:function(t){this.uniforms.gapSize.value=t}},opacity:{enumerable:!0,get:function(){return this.uniforms.opacity.value},set:function(t){this.uniforms.opacity.value=t}},resolution:{enumerable:!0,get:function(){return this.uniforms.resolution.value},set:function(t){this.uniforms.resolution.value.copy(t)}},alphaToCoverage:{enumerable:!0,get:function(){return Boolean("USE_ALPHA_TO_COVERAGE"in this.defines)},set:function(t){Boolean(t)!==Boolean("USE_ALPHA_TO_COVERAGE"in this.defines)&&(this.needsUpdate=!0),!0===t?(this.defines.USE_ALPHA_TO_COVERAGE="",this.extensions.derivatives=!0):(delete this.defines.USE_ALPHA_TO_COVERAGE,this.extensions.derivatives=!1)}}}),this.setValues(t)}}const g=new e.Box3,v=new e.Vector3;class b extends e.InstancedBufferGeometry{constructor(){super(),this.isLineSegmentsGeometry=!0,this.type="LineSegmentsGeometry";this.setIndex([0,2,1,2,3,1,2,4,3,4,5,3,4,6,5,6,7,5]),this.setAttribute("position",new e.Float32BufferAttribute([-1,2,0,1,2,0,-1,1,0,1,1,0,-1,0,0,1,0,0,-1,-1,0,1,-1,0],3)),this.setAttribute("uv",new e.Float32BufferAttribute([-1,2,1,2,-1,1,1,1,-1,-1,1,-1,-1,-2,1,-2],2))}applyMatrix4(t){const e=this.attributes.instanceStart,n=this.attributes.instanceEnd;return void 0!==e&&(e.applyMatrix4(t),n.applyMatrix4(t),e.needsUpdate=!0),null!==this.boundingBox&&this.computeBoundingBox(),null!==this.boundingSphere&&this.computeBoundingSphere(),this}setPositions(t){let n;t instanceof Float32Array?n=t:Array.isArray(t)&&(n=new Float32Array(t));const i=new e.InstancedInterleavedBuffer(n,6,1);return this.setAttribute("instanceStart",new e.InterleavedBufferAttribute(i,3,0)),this.setAttribute("instanceEnd",new e.InterleavedBufferAttribute(i,3,3)),this.computeBoundingBox(),this.computeBoundingSphere(),this}setColors(t){let n;t instanceof Float32Array?n=t:Array.isArray(t)&&(n=new Float32Array(t));const i=new e.InstancedInterleavedBuffer(n,6,1);return this.setAttribute("instanceColorStart",new e.InterleavedBufferAttribute(i,3,0)),this.setAttribute("instanceColorEnd",new e.InterleavedBufferAttribute(i,3,3)),this}fromWireframeGeometry(t){return this.setPositions(t.attributes.position.array),this}fromEdgesGeometry(t){return this.setPositions(t.attributes.position.array),this}fromMesh(t){return this.fromWireframeGeometry(new e.WireframeGeometry(t.geometry)),this}fromLineSegments(t){const e=t.geometry;return this.setPositions(e.attributes.position.array),this}computeBoundingBox(){null===this.boundingBox&&(this.boundingBox=new e.Box3);const t=this.attributes.instanceStart,n=this.attributes.instanceEnd;void 0!==t&&void 0!==n&&(this.boundingBox.setFromBufferAttribute(t),g.setFromBufferAttribute(n),this.boundingBox.union(g))}computeBoundingSphere(){null===this.boundingSphere&&(this.boundingSphere=new e.Sphere),null===this.boundingBox&&this.computeBoundingBox();const t=this.attributes.instanceStart,n=this.attributes.instanceEnd;if(void 0!==t&&void 0!==n){const e=this.boundingSphere.center;this.boundingBox.getCenter(e);let i=0;for(let o=0,r=t.count;ou&&S.z>u)continue;if(x.z>u){const t=x.z-S.z,e=(x.z-u)/t;x.lerp(S,e)}else if(S.z>u){const t=S.z-x.z,e=(S.z-u)/t;S.lerp(x,e)}x.applyMatrix4(o),S.applyMatrix4(o),x.multiplyScalar(1/x.w),S.multiplyScalar(1/S.w),x.x*=r.x/2,x.y*=r.y/2,S.x*=r.x/2,S.y*=r.y/2,C.start.copy(x),C.start.z=0,C.end.copy(S),C.end.z=0;const a=C.closestPointToPointParameter(L,!0);C.at(a,M);const d=e.MathUtils.lerp(x.z,S.z,a),h=d>=-1&&d<=1,p=L.distanceTo(M)<.5*P;if(h&&p){C.start.fromBufferAttribute(l,n),C.end.fromBufferAttribute(c,n),C.start.applyMatrix4(s),C.end.applyMatrix4(s);const o=new e.Vector3,r=new e.Vector3;z.distanceSqToSegment(C.start,C.end,r,o),i.push({point:r,pointOnLine:o,distance:z.origin.distanceTo(r),object:t,face:null,faceIndex:n,uv:null,uv1:null})}}}(this,o,n))}}}class I extends e.LineSegments{constructor(t,n,i,o,r=4473924,s=8947848){r=new e.Color(r),s=new e.Color(s);const a=Math.round(t/n);i=Math.round(i/o)*o/2;const l=[],c=[];let d=0;for(let e=-1*(t=a*n/2);e<=t;e+=n){l.push(e,0,-1*i,e,0,i);const t=0===e?r:s;t.toArray(c,d),d+=3,t.toArray(c,d),d+=3,t.toArray(c,d),d+=3,t.toArray(c,d),d+=3}for(let e=-1*i;e<=i;e+=o){l.push(-1*t,0,e,t,0,e);const n=0===e?r:s;n.toArray(c,d),d+=3,n.toArray(c,d),d+=3,n.toArray(c,d),d+=3,n.toArray(c,d),d+=3}const u=new e.BufferGeometry;u.setAttribute("position",new e.Float32BufferAttribute(l,3)),u.setAttribute("color",new e.Float32BufferAttribute(c,3));super(u,new e.LineBasicMaterial({vertexColors:!0,toneMapped:!1}))}}function U(t,n,i,o){const r=function(t,n,i){t*=.5,n*=.5,i*=.5;const o=new e.BufferGeometry,r=[];return r.push(-t,-n,-i,-t,n,-i,-t,n,-i,t,n,-i,t,n,-i,t,-n,-i,t,-n,-i,-t,-n,-i,-t,-n,i,-t,n,i,-t,n,i,t,n,i,t,n,i,t,-n,i,t,-n,i,-t,-n,i,-t,-n,-i,-t,-n,i,-t,n,-i,-t,n,i,t,n,-i,t,n,i,t,-n,-i,t,-n,i),o.setAttribute("position",new e.Float32BufferAttribute(r,3)),o}(t,n,i),s=new e.LineSegments(r,new e.LineDashedMaterial({color:new e.Color(o),dashSize:3,gapSize:1}));return s.computeLineDistances(),s}const B={h:0,s:0,l:0};class V{constructor(t){var n,i,o,r,s,l,c,d;if(this.minLayerThreshold=.05,this.renderExtrusion=!0,this.renderTravel=!1,this.renderTubes=!1,this.singleLayerMode=!1,this.initialCameraPosition=[-100,400,450],this.debug=!1,this.allowDragNDrop=!1,this.beyondFirstMove=!1,this.inches=!1,this.nonTravelmoves=[],this.disposables=[],this._extrusionColor=new e.Color(16776960),this._backgroundColor=new e.Color(14737632),this._travelColor=new e.Color(10027008),this.minLayerThreshold=null!==(n=t.minLayerThreshold)&&void 0!==n?n:this.minLayerThreshold,this.parser=new a(this.minLayerThreshold),this.scene=new e.Scene,this.scene.background=this._backgroundColor,void 0!==t.backgroundColor&&(this.backgroundColor=new e.Color(t.backgroundColor)),this.targetId=t.targetId,this.endLayer=t.endLayer,this.startLayer=t.startLayer,this.lineWidth=t.lineWidth,this.buildVolume=t.buildVolume,this.initialCameraPosition=null!==(i=t.initialCameraPosition)&&void 0!==i?i:this.initialCameraPosition,this.debug=null!==(o=t.debug)&&void 0!==o?o:this.debug,this.allowDragNDrop=null!==(r=t.allowDragNDrop)&&void 0!==r?r:this.allowDragNDrop,this.renderExtrusion=null!==(s=t.renderExtrusion)&&void 0!==s?s:this.renderExtrusion,this.renderTravel=null!==(l=t.renderTravel)&&void 0!==l?l:this.renderTravel,this.nonTravelmoves=null!==(c=t.nonTravelMoves)&&void 0!==c?c:this.nonTravelmoves,this.renderTubes=null!==(d=t.renderTubes)&&void 0!==d?d:this.renderTubes,null!=t.extrusionColor&&(this.extrusionColor=new e.Color(t.extrusionColor)),null!=t.travelColor&&(this.travelColor=new e.Color(t.travelColor)),null!=t.topLayerColor&&(this.topLayerColor=new e.Color(t.topLayerColor)),null!=t.lastSegmentColor&&(this.lastSegmentColor=new e.Color(t.lastSegmentColor)),console.info("Using THREE r"+e.REVISION),console.debug("opts",t),this.targetId&&console.warn("`targetId` is deprecated and will removed in the future. Use `canvas` instead."),t.canvas)this.canvas=t.canvas,this.renderer=new e.WebGLRenderer({canvas:this.canvas,preserveDrawingBuffer:!0});else{if(!this.targetId)throw Error("Set either opts.canvas or opts.targetId");const t=document.getElementById(this.targetId);if(!t)throw new Error("Unable to find element "+this.targetId);this.renderer=new e.WebGLRenderer({preserveDrawingBuffer:!0}),this.canvas=this.renderer.domElement,t.appendChild(this.canvas)}this.camera=new e.PerspectiveCamera(25,this.canvas.offsetWidth/this.canvas.offsetHeight,10,5e3),this.camera.position.fromArray(this.initialCameraPosition);const u=this.camera.far,h=.8*u;this.scene.fog=new e.Fog(this._backgroundColor,h,u),this.resize(),this.controls=new m(this.camera,this.renderer.domElement),this.animate(),this.allowDragNDrop&&this._enableDropHandler()}get extrusionColor(){return this._extrusionColor}set extrusionColor(t){this._extrusionColor=new e.Color(t)}get backgroundColor(){return this._backgroundColor}set backgroundColor(t){this._backgroundColor=new e.Color(t),this.scene.background=this._backgroundColor}get travelColor(){return this._travelColor}set travelColor(t){this._travelColor=new e.Color(t)}get topLayerColor(){return this._topLayerColor}set topLayerColor(t){this._topLayerColor=void 0!==t?new e.Color(t):void 0}get lastSegmentColor(){return this._lastSegmentColor}set lastSegmentColor(t){this._lastSegmentColor=void 0!==t?new e.Color(t):void 0}get layers(){return[this.parser.preamble].concat(this.parser.layers.concat())}get maxLayerIndex(){var t;return(null!==(t=this.endLayer)&&void 0!==t?t:this.layers.length)-1}get minLayerIndex(){var t;return this.singleLayerMode?this.maxLayerIndex:(null!==(t=this.startLayer)&&void 0!==t?t:0)-1}animate(){this._animationFrameId=requestAnimationFrame((()=>this.animate())),this.controls.update(),this.renderer.render(this.scene,this.camera)}processGCode(t){this.parser.parseGCode(t),this.render()}render(){for(;this.scene.children.length>0;)this.scene.remove(this.scene.children[0]);for(;this.disposables.length>0;){const t=this.disposables.pop();t&&t.dispose()}if(this.debug&&this.buildVolume){const t=new e.AxesHelper(Math.max(this.buildVolume.x/2,this.buildVolume.y/2)+20);this.scene.add(t)}if(this.buildVolume&&this.drawBuildVolume(),this.renderTubes){console.warn("Volumetric rendering is experimental and may not work as expected or change in the future.");const t=new e.AmbientLight(13421772,.3*Math.PI),n=new e.PointLight(16777215,Math.PI,void 0,.001);n.position.set(0,500,500),this.scene.add(t),this.scene.add(n)}this.group=new e.Group,this.group.name="gcode";const t={x:0,y:0,z:0,r:0,e:0,i:0,j:0};for(let e=0;ethis.maxLayerIndex)return;const d={extrusion:[],travel:[],z:e.z},u=this.layers[t];for(const h of u.commands)if("g20"==h.gcode)this.setInches();else if(["g0","g00","g1","g01","g2","g02","g3","g03"].indexOf(h.gcode)>-1){const u=h,p={x:null!==(n=u.params.x)&&void 0!==n?n:e.x,y:null!==(i=u.params.y)&&void 0!==i?i:e.y,z:null!==(o=u.params.z)&&void 0!==o?o:e.z,r:null!==(r=u.params.r)&&void 0!==r?r:e.r,e:null!==(s=u.params.e)&&void 0!==s?s:e.e,i:null!==(a=u.params.i)&&void 0!==a?a:e.i,j:null!==(l=u.params.j)&&void 0!==l?l:e.j};if(t>=this.minLayerIndex){const t=(null!==(c=u.params.e)&&void 0!==c?c:0)>0||this.nonTravelmoves.indexOf(h.gcode)>-1;(p.x!=e.x||p.y!=e.y||p.z!=e.z)&&(t&&this.renderExtrusion||!t&&this.renderTravel)&&("g2"==h.gcode||"g3"==h.gcode||"g02"==h.gcode||"g03"==h.gcode?this.addArcSegment(d,e,p,t,"g2"==h.gcode||"g02"==h.gcode):this.addLineSegment(d,e,p,t))}e.x=p.x,e.y=p.y,e.z=p.z,this.beyondFirstMove||(this.beyondFirstMove=!0)}this.doRenderExtrusion(d,t)}doRenderExtrusion(t,n){var i,o;if(this.renderExtrusion){let r;if(this.singleLayerMode||this.renderTubes)r=this._extrusionColor;else{const t=.1+.7*n/this.layers.length;this._extrusionColor.getHSL(B),r=(new e.Color).setHSL(B.h,B.s,t)}if(n==this.layers.length-1){const e=null!==(i=this._topLayerColor)&&void 0!==i?i:r,n=null!==(o=this._lastSegmentColor)&&void 0!==o?o:e,s=t.extrusion.splice(-3),a=t.extrusion.splice(-3);this.renderTubes?(this.addTubeLine(t.extrusion,e.getHex()),this.addTubeLine([...a,...s],n.getHex())):(this.addLine(t.extrusion,e.getHex()),this.addLine([...a,...s],n.getHex()))}else this.renderTubes?this.addTubeLine(t.extrusion,r.getHex()):this.addLine(t.extrusion,r.getHex())}this.renderTravel&&this.addLine(t.travel,this._travelColor.getHex())}setInches(){this.beyondFirstMove?console.warn("Switching units after movement is already made is discouraged and is not supported."):this.inches=!0}drawBuildVolume(){if(!this.buildVolume)return;this.scene.add(new I(this.buildVolume.x,10,this.buildVolume.y,10));const t=U(this.buildVolume.x,this.buildVolume.z,this.buildVolume.y,8947848);t.position.setY(this.buildVolume.z/2),this.scene.add(t)}clear(){this.startLayer=1,this.endLayer=1/0,this.singleLayerMode=!1,this.parser=new a(this.minLayerThreshold),this.beyondFirstMove=!1}resize(){const[t,e]=[this.canvas.offsetWidth,this.canvas.offsetHeight];this.camera.aspect=t/e,this.camera.updateProjectionMatrix(),this.renderer.setPixelRatio(window.devicePixelRatio),this.renderer.setSize(t,e,!1)}addLineSegment(t,e,n,i){(i?t.extrusion:t.travel).push(e.x,e.y,e.z,n.x,n.y,n.z)}addArcSegment(t,e,n,i,o){const r=i?t.extrusion:t.travel,s=e.x,a=e.y,l=e.z,c=n.x,d=n.y,u=n.z;let h=n.r,p=n.i,m=n.j;if(h){const t=c-s,e=d-a,n=Math.sqrt(Math.pow(t/2,2)+Math.pow(e/2,2));h=Math.max(h,n);const i=Math.pow(t,2)+Math.pow(e,2),r=Math.pow(h,2)-i/4;let l=Math.sqrt(r/i);(o&&h<0||!o&&h>0)&&(l=-l),p=t/2+e*l,m=e/2-t*l}const f=s==c&&a==d,g=s+p,v=a+m,b=Math.sqrt(p*p+m*m),y=Math.atan2(-m,-p),w=Math.atan2(d-v,c-g);let x;f?x=2*Math.PI:(x=o?y-w:w-y,x<0&&(x+=2*Math.PI));let S=b*x/1.8;this.inches&&(S*=25),S<1&&(S=1);let E=x/S;E*=o?-1:1;const L=[];L.push({x:s,y:a,z:l});const A=(l-u)/S;let C=s,M=a,T=l,_=y;for(let t=0;t0)return void this.addThickLine(t,n);const o=new e.BufferGeometry;o.setAttribute("position",new e.Float32BufferAttribute(t,3)),this.disposables.push(o);const r=new e.LineBasicMaterial({color:n});this.disposables.push(r);const s=new e.LineSegments(o,r);null===(i=this.group)||void 0===i||i.add(s)}addTubeLine(t,n){let i=[];const o=[];for(let n=0;n2&&o.push(new e.CatmullRomCurve3(i,!1,"catmullrom",0)),o.forEach((t=>{var i;const o=new e.MeshLambertMaterial({color:n});this.disposables.push(o);const r=Math.ceil(2*t.getLength()),s=new e.TubeGeometry(t,r,.3,4,!1);this.disposables.push(s);const a=new e.Mesh(s,o);null===(i=this.group)||void 0===i||i.add(a)}))}addThickLine(t,e){var n;if(!t.length||!this.lineWidth)return;const i=new b;this.disposables.push(i);const o=new f({color:e,linewidth:this.lineWidth/(1e3*window.devicePixelRatio)});this.disposables.push(o),i.setPositions(t);const r=new D(i,o);null===(n=this.group)||void 0===n||n.add(r)}dispose(){this.disposables.forEach((t=>t.dispose())),this.disposables=[],this.controls.dispose(),this.renderer.dispose(),this.cancelAnimation()}cancelAnimation(){void 0!==this._animationFrameId&&cancelAnimationFrame(this._animationFrameId),this._animationFrameId=void 0}_enableDropHandler(){console.warn("Drag and drop is deprecated as a library feature. See the demo how to implement your own."),this.canvas.addEventListener("dragover",(t=>{t.stopPropagation(),t.preventDefault(),t.dataTransfer&&(t.dataTransfer.dropEffect="copy"),this.canvas.classList.add("dragging")})),this.canvas.addEventListener("dragleave",(t=>{t.stopPropagation(),t.preventDefault(),this.canvas.classList.remove("dragging")})),this.canvas.addEventListener("drop",(t=>n(this,void 0,void 0,(function*(){var e,n;t.stopPropagation(),t.preventDefault(),this.canvas.classList.remove("dragging");const i=(null!==(n=null===(e=t.dataTransfer)||void 0===e?void 0:e.files)&&void 0!==n?n:[])[0];this.clear(),yield this._readFromStream(i.stream()),this.render()}))))}_readFromStream(t){var e,i;return n(this,void 0,void 0,(function*(){const n=t.getReader();let o,r="",s=0;do{o=yield n.read(),s+=null!==(i=null===(e=o.value)||void 0===e?void 0:e.length)&&void 0!==i?i:0;const t=(a=o.value,new TextDecoder("utf-8").decode(a)),l=t.lastIndexOf("\n"),c=t.slice(0,l);this.parser.parseGCode(r+c),r=t.slice(l)}while(!o.done);var a;console.debug("read from stream",s)}))}}t.WebGLPreview=V,t.init=function(t){return new V(t)},Object.defineProperty(t,"__esModule",{value:!0})}));