import {readFileSync,writeFileSync,mkdirSync} from 'node:fs'; import {createHash} from 'node:crypto'; import {pathToFileURL} from 'node:url'; export function validateParameters(p){ if(p.unit!=='mm'||typeof p.version!=='string'||!/^LB3D-[A-Z0-9-]+$/.test(p.version))throw RangeError('Version and mm units required'); const ranges={shaftDiameter:[4,20],engagementLength:[3,12],shaftLength:[4,24],couponWidth:[12,40],couponDepth:[12,40],flangeDiameter:[8,30],flangeHeight:[1,5],segments:[64,512]}; for(const [name,[min,max]]of Object.entries(ranges))if(typeof p[name]!=='number'||!Number.isFinite(p[name])||p[name]max)throw RangeError('Invalid parameter: '+name); if(!Number.isInteger(p.segments)||p.segments%8)throw RangeError('Segments must be an integer multiple of eight'); if(p.couponWidth!==p.couponDepth)throw RangeError('This source requires a square coupon'); if(!Array.isArray(p.diametralClearances)||!p.diametralClearances.length||p.diametralClearances.length>9||p.diametralClearances.some(c=>typeof c!=='number'||!Number.isFinite(c)||c<0||c>1)||new Set(p.diametralClearances).size!==p.diametralClearances.length)throw RangeError('Unique clearances between zero and one mm required'); const maximum=p.shaftDiameter+Math.max(...p.diametralClearances); if(Math.min(p.couponWidth,p.couponDepth)Math.abs(n)<1e-12?0:n; const ring=(radius,z,n)=>Array.from({length:n},(_,i)=>{const angle=2*Math.PI*i/n;return [clean(radius*Math.cos(angle)),clean(radius*Math.sin(angle)),z];}); const subtract=(a,b)=>a.map((x,i)=>x-b[i]); const cross=(a,b)=>[a[1]*b[2]-a[2]*b[1],a[2]*b[0]-a[0]*b[2],a[0]*b[1]-a[1]*b[0]]; function triangulator(){const faces=[];const triangle=(a,b,c)=>{if(Math.hypot(...cross(subtract(b,a),subtract(c,a)))>1e-12)faces.push([a,b,c]);};return{faces,quad(a,b,c,d){triangle(a,b,c);triangle(a,c,d);}};} function coupon(p,diameter){ const t=triangulator(),n=p.segments; const inner0=ring(diameter/2,0,n),inner1=ring(diameter/2,p.engagementLength,n); const square=z=>Array.from({length:n},(_,i)=>{const angle=2*Math.PI*i/n,c=Math.cos(angle),s=Math.sin(angle),radius=Math.min(p.couponWidth/2/Math.abs(c),p.couponDepth/2/Math.abs(s));return[clean(radius*c),clean(radius*s),z];}); const outer0=square(0),outer1=square(p.engagementLength); for(let i=0;ivertex.map(Math.fround)),normal=cross(subtract(face[1],face[0]),subtract(face[2],face[0])),length=Math.hypot(...normal),at=84+i*50; normal.forEach((value,j)=>bytes.writeFloatLE(length?value/length:0,at+j*4));face.flat().forEach((value,j)=>bytes.writeFloatLE(value,at+12+j*4)); } return bytes; } export function generate(p=JSON.parse(readFileSync(new URL('./parameters.json',import.meta.url),'utf8')),directory=new URL('./stl/',import.meta.url)){ validateParameters(p);mkdirSync(directory,{recursive:true}); const models=p.diametralClearances.map((clearance,i)=>({id:String.fromCharCode(65+i),file:`coupon-${String.fromCharCode(65+i)}.stl`,clearance,holeDiameter:p.shaftDiameter+clearance,faces:coupon(p,p.shaftDiameter+clearance)})); models.push({id:'shaft',file:'axe.stl',faces:shaft(p)}); const entries=models.map(({faces,...model})=>{const bytes=binaryStl(faces);writeFileSync(new URL(model.file,directory),bytes);return{...model,bytes:bytes.length,triangles:faces.length,sha256:createHash('sha256').update(bytes).digest('hex')};}); const manifest={version:p.version,unit:'mm',status:'preview-unprinted',parameters:p,files:entries,licenseStatus:'CC BY 4.0. Credit: LayerBench3D. No physical test is claimed.'}; if(directory.href===new URL('./stl/',import.meta.url).href){ writeFileSync(new URL('./manifest.json',import.meta.url),JSON.stringify(manifest,null,2)+'\n'); const rows=['version;essai;objet;repere;nominal_mm;z_depuis_base_mm;direction;repetition;valeur_mesuree_mm;instrument;conditions;retouches;commentaire']; for(const model of models)for(const z of model.id==='shaft'?[p.flangeHeight+p.shaftLength/3,p.flangeHeight+2*p.shaftLength/3]:[p.engagementLength/3,2*p.engagementLength/3])for(const direction of ['X','Y'])for(let repetition=1;repetition<=3;repetition++)rows.push([p.version,'',model.id==='shaft'?'axe':'alesage',model.id,model.id==='shaft'?p.shaftDiameter:model.holeDiameter,z,direction,repetition,'','','','',''].join(';')); writeFileSync(new URL('./mesures-brutes.csv',import.meta.url),rows.join('\n')+'\n'); } return manifest; } if(process.argv[1]&&import.meta.url===pathToFileURL(process.argv[1]).href){const manifest=generate();console.log(JSON.stringify({version:manifest.version,files:manifest.files.map(({file,triangles,bytes})=>({file,triangles,bytes}))},null,2));}