/**
 * Minified by jsDelivr using Terser v5.48.0.
 * Original file: /npm/three-3mf-exporter@45.2.1/dist/index.mjs
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
import JSZip from"jszip";import{Vector3,Color}from"three";const DEFAULT_NOZZLE_VOLUME=92,MAX_FLUSH_VOLUME=900,MIN_COLOR_FLUSH=60,f32=Math.fround;function computeFlushVolumesMatrix(e,t={}){const n=t.nozzleVolume??92,a=e.map(parseHexColor);return a.flatMap((e,t)=>a.map((a,o)=>t===o?0:Math.min(Math.trunc(colorFlushVolume(e,a))+n,900)))}function colorFlushVolume(e,t){const n=rgbToHsv(e),a=rgbToHsv(t),o=luma(e),r=luma(t);let i,s=Math.min(f32(1.2),f32(Math.hypot(f32(hsvWheelX(n)-hsvWheelX(a)),f32(hsvWheelY(n)-hsvWheelY(a)))));r>=o?i=f32(560*f32(f32(r-o)**f32(.7))):(i=f32(80*f32(o-r)),s=Math.min(s,f32(.67*a.v+.33*n.v)));const l=f32(230*s),m=f32(Math.sqrt(l**2+i**2+l*i));return Math.max(m,60)}const WHITE={r:1,g:1,b:1};function parseHexColor(e){let t=e.trim().replace(/^#/,"");if(!/^(?:[\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$/i.test(t))return WHITE;if(t.length<=4&&(t=[...t].map(e=>e+e).join("")),8===t.length&&"00"===t.slice(6))return WHITE;const n=e=>f32(Number.parseInt(t.slice(e,e+2),16)/255);return{r:n(0),g:n(2),b:n(4)}}function luma({r:e,g:t,b:n}){return f32(.3*e+.59*t+.11*n)}function rgbToHsv({r:e,g:t,b:n}){const a=Math.max(e,t,n),o=f32(a-Math.min(e,t,n));let r=0;o>=.001&&(r=f32(a===e?60*f32(f32((t-n)/o)%6):a===t?60*f32(f32((n-e)/o)+2):60*f32(f32((e-t)/o)+4)));return{h:r,s:a<.001?0:f32(o/a),v:a}}function hsvWheelX({h:e,s:t,v:n}){return Math.cos(e*Math.PI/180)*t*n}function hsvWheelY({h:e,s:t,v:n}){return Math.sin(e*Math.PI/180)*t*n}const defaultPrintConfig={printer_name:"Bambu Lab A1",filament:"Bambu PLA Basic @BBL A1",printableWidth:256,printableDepth:256,printableHeight:256,printableArea:["0x0","256x0","256x256","0x256"],printerSettingsId:"Bambu Lab A1 0.4 nozzle",printSettingsId:"0.20mm Standard @BBL A1",compression:"standard",metadata:{Application:"BambuStudio-02.04.00.70",ApplicationTitle:"Exported 3D Model"}},JSZipCompressionMap={standard:"DEFLATE",none:"STORE"};async function exportTo3MF(e,t){const n=new JSZip,a=Object.assign({},defaultPrintConfig,t),o=JSZipCompressionMap[a.compression],r=[],i=[],s=[],l=e=>{if("Mesh"===e.type)return(e=>{e.updateMatrixWorld(!0);const t=e.geometry,n=t.attributes.position,a=t.index;let o=null;if(e.material){const t=new Color,n=Array.isArray(e.material)?e.material[0]:e.material;n&&"color"in n&&n.color?t.copy(n.color):t.set(8421504);const a=i.find(e=>e.color.r===t.r&&e.color.g===t.g&&e.color.b===t.b);if(a)o=a;else{const n=i.length+1;o={id:i.length+1,color:t,name:e.name?`${e.name}_material`:`material_${i.length}`,extruder:n},i.push(o)}}const s=r.length+1,{positions:l,indices:m}=collectMeshData(n,a);return r.push({id:s,type:"mesh",positions:l,indices:m,material:o,name:e.name||`Mesh-${s}`,subComponents:[],uuid:generateUUID()}),s})(e);if("Group"===e.type||"Object3D"===e.type||"Scene"===e.type){const t=[];if(e.updateMatrixWorld(!0),e.children.forEach(n=>{const a=l(n);if(-1!==a){const o=n.matrixWorld.clone().premultiply(e.matrixWorld.clone().invert());t.push({objectId:a,transform:o})}}),t.length>0){const n=r.length+1;return r.push({id:n,type:"assembly",subComponents:t,name:e.name||`Group-${n}`,positions:new Float64Array(0),indices:new Uint32Array(0),material:null,uuid:generateUUID()}),n}}return-1},m="Scene"===e.type?e.children:[e],c=createBoundsAccumulator();m.forEach(e=>{const t=l(e);if(-1!==t){e.updateMatrix();const n=e.matrix.clone(),a=(e,t)=>{"assembly"===e.type?e.subComponents.forEach(e=>{a(r.find(t=>t.id===e.objectId),[e.transform,...t])}):c.addPositions(e.positions,t)};a(r.find(e=>e.id===t),[n]),s.push({objectId:t,transformMatrix:n,uuid:generateUUID()})}});const{min:d,max:p}=c.result(),u=(d.x+p.x)/2,f=(d.y+p.y)/2,h={x:a.printableWidth/2-u,y:a.printableDepth/2-f,z:0-d.z},x=createMainModelXML(r,s,h,a),g=createModelSettingsXML(r,s),y=createProjectSettingsConfig(i,a);return n.file("_rels/.rels",relationshipsXML()),n.file("3D/3dmodel.model",x),n.file("Metadata/model_settings.config",g),n.file("Metadata/project_settings.config",y),n.file("[Content_Types].xml",contentTypesXML()),await n.generateAsync({type:"blob",mimeType:"application/vnd.ms-package.3dmanufacturing-3dmodel+xml",compression:o})}function createMainModelXML(e,t,n,a){const o=[],r=a.metadata;o.push(`<metadata name="CreationDate">${(new Date).toISOString()}</metadata>`);for(const e in r)o.push(`<metadata name="${escapeXml(e)}">${escapeXml(r[e])}</metadata>`);const i=`<?xml version="1.0" encoding="UTF-8"?>\n<model unit="millimeter" xml:lang="en-US" xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02" xmlns:slic3rpe="http://schemas.slic3r.org/3mf/2017/06" xmlns:p="http://schemas.microsoft.com/3dmanufacturing/production/2015/06" requiredextensions="p">\n    ${o.join("\n    ")}\n    <resources>\n`,s=e.map(e=>{if("assembly"===e.type){const t=e.subComponents.map(e=>{const t=e.transform.elements,n=`${t[0].toFixed(5)} ${t[1].toFixed(5)} ${t[2].toFixed(5)} ${t[4].toFixed(5)} ${t[5].toFixed(5)} ${t[6].toFixed(5)} ${t[8].toFixed(5)} ${t[9].toFixed(5)} ${t[10].toFixed(5)} ${t[12].toFixed(5)} ${t[13].toFixed(5)} ${t[14].toFixed(5)}`;return`<component objectid="${e.objectId}" transform="${n}" />`}).join("");return`<object id="${e.id}" type="model" name="${escapeXml(e.name)}"><components>${t}</components></object>`}return`<object id="${e.id}" type="model" name="${escapeXml(e.name)}"><mesh><vertices>`}),l=`\n    </resources>\n    <build>\n${t.map(e=>{const t=e.transformMatrix.elements,a=t[12]+n.x,o=t[13]+n.y,r=t[14]+n.z,i=`${t[0].toFixed(5)} ${t[1].toFixed(5)} ${t[2].toFixed(5)} ${t[4].toFixed(5)} ${t[5].toFixed(5)} ${t[6].toFixed(5)} ${t[8].toFixed(5)} ${t[9].toFixed(5)} ${t[10].toFixed(5)} ${a.toFixed(5)} ${o.toFixed(5)} ${r.toFixed(5)}`;return`<item objectid="${e.objectId}" transform="${i}" printable="1" />`}).join("\n")}\n    </build>\n</model>`;let m=3*(i.length+l.length+e.length);s.forEach(e=>m+=3*e.length),e.forEach(e=>m+=meshXmlByteBound(e));const c=createUtf8Writer(m);return c.write(i),e.forEach((e,t)=>{if(t>0&&c.write("\n"),c.write(s[t]),"mesh"===e.type){const{positions:t,indices:n}=e;for(let e=0;e<t.length;e+=3)c.write(`${e>0?" ":""}<vertex x="${t[e].toFixed(5)}" y="${t[e+1].toFixed(5)}" z="${t[e+2].toFixed(5)}" />`);c.write("</vertices><triangles>");for(let e=0;e<n.length;e+=3)c.write(`${e>0?" ":""}<triangle v1="${n[e]}" v2="${n[e+1]}" v3="${n[e+2]}" />`);c.write("</triangles></mesh></object>")}}),c.write(l),c.toUint8Array()}function meshXmlByteBound(e){if("mesh"!==e.type)return 0;const t=e=>{let t=1;for(let n=10;n<=e;n*=10)t++;return t},n=e=>Math.abs(e)<1e21?(e<0?7:6)+t(Math.floor(Math.abs(e))+1):e.toFixed(5).length,{positions:a,indices:o}=e;let r=64+a.length/3*26+o.length/3*(31+3*t(Math.max(a.length/3-1,0)));for(let e=0;e<a.length;e++)r+=n(a[e]);return r}function collectMeshData(e,t){const n=3*Math.floor((t?t.count:e.count)/3),a=new Uint32Array(n),o=Math.min(n,e.count);let r=Math.min(o,Math.max(16,t?e.count:Math.ceil(n/6))),i=new Float64Array(3*r),s=16;for(;s<2*r;)s*=2;let l=s-1,m=new Uint32Array(s);const c=new Float64Array(3),d=new Uint32Array(c.buffer),p=e=>0===e?0:Number.isNaN(e)?Number.NaN:e,u=(e,t)=>e===t||Number.isNaN(e)&&Number.isNaN(t),f=()=>{let e=2654435769;for(let t=0;t<6;t++)e=Math.imul(e^d[t],2246822507),e^=e>>>13;return e=Math.imul(e^e>>>16,3266489909),e^=e>>>16,(e&l)>>>0};let h=0;for(let d=0;d<n;d++){const n=t?t.getX(d):d,x=c[0]=p(e.getX(n)),g=c[1]=p(e.getY(n)),y=c[2]=p(e.getZ(n));let b=f(),M=-1;for(;0!==m[b];){const e=3*(m[b]-1);if(u(i[e],x)&&u(i[e+1],g)&&u(i[e+2],y)){M=m[b]-1;break}b=(b+1&l)>>>0}if(-1===M){if(M=h++,m[b]=M+1,M===r){r=Math.min(o,2*r);const e=new Float64Array(3*r);e.set(i),i=e}if(i[3*M]=x,i[3*M+1]=g,i[3*M+2]=y,2*h>s){s*=2,l=s-1,m=new Uint32Array(s);for(let e=0;e<h;e++){c.set(i.subarray(3*e,3*e+3));let t=f();for(;0!==m[t];)t=(t+1&l)>>>0;m[t]=e+1}}}a[d]=M}return{positions:i.subarray(0,3*h),indices:a}}function createBoundsAccumulator(){const e={x:1/0,y:1/0,z:1/0},t={x:-1/0,y:-1/0,z:-1/0},n=new Vector3;let a=!0;return{addPositions(o,r){for(let i=0;i<o.length;i+=3){n.set(o[i],o[i+1],o[i+2]);for(const e of r)n.applyMatrix4(e);e.x=Math.min(e.x,n.x),e.y=Math.min(e.y,n.y),e.z=Math.min(e.z,n.z),t.x=Math.max(t.x,n.x),t.y=Math.max(t.y,n.y),t.z=Math.max(t.z,n.z),a=!1}},result:()=>a?{min:{x:0,y:0,z:0},max:{x:0,y:0,z:0}}:{min:e,max:t}}}function createUtf8Writer(e){const t=new TextEncoder;let n=new Uint8Array(e),a=0,o="";const r=()=>{for(;o;){const{read:e,written:r}=t.encodeInto(o,n.subarray(a));if(a+=r,o=o.slice(e),o){const e=new Uint8Array(Math.ceil(Math.max(1.5*n.length,a+3*o.length)));e.set(n.subarray(0,a)),n=e}}};return{write(e){o+=e,o.length>=65536&&r()},toUint8Array:()=>(r(),n.subarray(0,a))}}function createModelSettingsXML(e,t){let n="",a="",o="";return t.forEach((t,r)=>{const i=t.objectId,s=e.find(e=>e.id===i),l=[],m=t=>{"mesh"===t.type?l.push(t):t.subComponents.forEach(t=>m(e.find(e=>e.id===t.objectId)))};m(s);const c=l.map(e=>{const t=e.material?e.material.extruder:1;return`    <part id="${e.id}" subtype="normal_part">\n      <metadata key="name" value="${escapeXml(e.name)}"/>\n      <metadata key="extruder" value="${t}"/>\n      <mesh_stat edges_fixed="0" degenerate_facets="0" facets_removed="0" facets_reversed="0" backwards_edges="0"/>\n    </part>`}).join("\n");n+=`  <object id="${i}">\n    <metadata key="name" value="${escapeXml(s.name)}"/>\n    <metadata key="extruder" value="1"/>\n    <metadata key="thumbnail_file" value=""/>\n${c}\n  </object>\n`,a+=`    <model_instance>\n      <metadata key="object_id" value="${i}"/>\n      <metadata key="instance_id" value="0"/>\n      <metadata key="identify_id" value="${r+1}"/>\n    </model_instance>\n`,o+=`    <assemble_item object_id="${i}" instance_id="0" offset="0 0 0"/>\n`}),`<?xml version="1.0" encoding="UTF-8"?>\n<config>\n${n}\n  <plate>\n    <metadata key="plater_id" value="1"/>\n    <metadata key="plater_name" value="plate-1"/>\n${a}\n  </plate>\n  <assemble>\n${o}\n  </assemble>\n</config>`}function createProjectSettingsConfig(e,t){const n=e.map(e=>`#${e.color.getHexString()}`);for(;n.length<2;)n.push("#FFFFFF");const a=new Set(t.different_settings_to_system||[]),o={printable_area:t.printableArea,printable_height:t.printableHeight.toString(),bed_exclude_area:[],filament_colour:n,filament_settings_id:Array.from({length:n.length}).fill(t.filament),filament_diameter:Array.from({length:n.length}).fill("1.75"),filament_is_support:Array.from({length:n.length}).fill("0"),printer_model:t.printer_name,layer_height:"0.2",wall_loops:"2",sparse_infill_density:"15%",printer_settings_id:t.printerSettingsId,printer_variant:"0.4",nozzle_diameter:["0.4"],enable_support:"0",support_type:"normal(auto)",print_settings_id:t.printSettingsId,flush_volumes_matrix:(t.flush_volumes_matrix?.length===n.length**2?t.flush_volumes_matrix:computeFlushVolumesMatrix(n,{nozzleVolume:t.nozzleVolume})).map(String)};return void 0!==t.flush_multiplier&&(o.flush_multiplier=[String(t.flush_multiplier)]),t.seam_position&&(o.seam_position=t.seam_position,a.add("seam_position")),a.size>0&&(o.different_settings_to_system=Array.from(a)),JSON.stringify(o)}function relationshipsXML(){return'<?xml version="1.0" encoding="UTF-8"?>\n<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">\n  <Relationship Id="rel-1" Target="/3D/3dmodel.model" Type="http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel"/>\n  <Relationship Id="rel-2" Target="/Metadata/model_settings.config" Type="http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel"/>\n  <Relationship Id="rel-3" Target="/Metadata/project_settings.config" Type="http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel"/>\n</Relationships>'}function contentTypesXML(){return'<?xml version="1.0" encoding="UTF-8"?>\n<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">\n  <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />\n  <Default Extension="model" ContentType="application/vnd.ms-package.3dmanufacturing-3dmodel+xml" />\n  <Default Extension="config" ContentType="application/vnd.ms-package.3dmanufacturing-3dmodel+xml" />\n  <Default Extension="png" ContentType="image/png" />\n  <Default Extension="gcode" ContentType="text/x.gcode"/>\n</Types>'}const XML_ESCAPES={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&apos;","\t":"&#9;","\n":"&#10;","\r":"&#13;"};function escapeXml(e){return String(e).replace(/[\0-\x08\v\f\x0E-\x1F\uFFFE\uFFFF\p{Cs}]/gu,"").replace(/[&<>"'\t\n\r]/g,e=>XML_ESCAPES[e])}function generateUUID(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,e=>{const t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)})}export{computeFlushVolumesMatrix,defaultPrintConfig,exportTo3MF};
//# sourceMappingURL=/sm/d46a497070c2a44669065a090a9e75edc8c209927caa3863e7f357bb3221d9f9.map