codex-copilot-dx 1.4.21 → 1.4.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,450 @@
1
+ (() => {
2
+ "use strict";
3
+ // One fixed camera and one set of 3D joint coordinates for the entire assembly.
4
+ const tau = 2 * Math.PI, diagonal = Math.SQRT1_2;
5
+ const camera = [.5,.5,diagonal];
6
+ const X = [1,0,0], Y = [0,1,0], Z = [0,0,1];
7
+ const add = (a,b) => [a[0]+b[0],a[1]+b[1],a[2]+b[2]];
8
+ const sub = (a,b) => [a[0]-b[0],a[1]-b[1],a[2]-b[2]];
9
+ const scale = (a,k) => [a[0]*k,a[1]*k,a[2]*k];
10
+ const dot = (a,b) => a[0]*b[0]+a[1]*b[1]+a[2]*b[2];
11
+ 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]];
12
+ const len = a => Math.hypot(...a);
13
+ const unit = a => scale(a,1/len(a));
14
+ const mix = (a,b,s) => add(a,scale(sub(b,a),s));
15
+ const clamp = (x,lo,hi) => Math.min(hi,Math.max(lo,x));
16
+ const project = ([x,y,z]) => [(x-y)*diagonal,(x+y)/2-z*diagonal];
17
+ const radial = (u,v,angle) => add(scale(u,Math.cos(angle)),scale(v,Math.sin(angle)));
18
+ const withZ = ([x,y],z) => [x,y,z];
19
+
20
+ const fixtureA = [89,-42], fixtureB = [86,40], park = [64,-4,79];
21
+ const floor = 6, gripZ = 35, travelZ = 76;
22
+ const upperLength = 62, foreLength = 53, closedGap = 7.4, openGap = 10.8;
23
+ const transferStart = 3.85, transferEnd = 5.55, duration = 9.25;
24
+ const queue = [];
25
+
26
+ function progress(t) {
27
+ // Integrate a piecewise linear speed profile. The outer quarters have constant acceleration.
28
+ const r = .25, speed = 1/(1-r);
29
+ if (t <= r) return speed*t*t/(2*r);
30
+ if (t >= 1-r) return 1-speed*(1-t)*(1-t)/(2*r);
31
+ return speed*(t-r/2);
32
+ }
33
+
34
+ function makeStages(a, b, yOffset) {
35
+ const approach = [a[0]-5,a[1]+yOffset,61];
36
+ return [
37
+ [0,park,openGap], [.40,park,openGap], [1.50,approach,openGap], [2.00,withZ(a,61),openGap],
38
+ [2.80,withZ(a,gripZ),openGap], [3.15,withZ(a,gripZ),closedGap], [3.35,withZ(a,gripZ),closedGap],
39
+ [transferStart,withZ(a,travelZ),closedGap], [transferEnd,withZ(b,travelZ),closedGap],
40
+ [6.45,withZ(b,gripZ),closedGap], [6.72,withZ(b,gripZ),closedGap], [7.09,withZ(b,gripZ),openGap],
41
+ [7.82,withZ(b,61),openGap], [duration,park,openGap],
42
+ ];
43
+ }
44
+ const stagesForward = makeStages(fixtureA, fixtureB, 4);
45
+ const stagesReverse = makeStages(fixtureB, fixtureA, -4);
46
+ function trajectory(t, reverse) {
47
+ const stages = reverse ? stagesReverse : stagesForward;
48
+ const i = stages.findIndex((stage, j) => j+1 === stages.length-1 || t < stages[j+1][0]);
49
+ const [from, p, g] = stages[i], [to, q, h] = stages[i+1];
50
+ const s = progress(clamp((t-from)/(to-from),0,1));
51
+ return { position:mix(p,q,s), jaw:g+(h-g)*s,
52
+ a:reverse?fixtureB:fixtureA, b:reverse?fixtureA:fixtureB };
53
+ }
54
+
55
+ function solve([x,y,z]) {
56
+ const yaw = Math.atan2(y,x);
57
+ const radialAxis = [Math.cos(yaw),Math.sin(yaw),0];
58
+ const side = [-Math.sin(yaw),Math.cos(yaw),0];
59
+ const reach = Math.hypot(x,y), shoulder = [0,0,50], lift = z-shoulder[2];
60
+ const bend = -Math.acos(clamp((reach*reach+lift*lift-upperLength**2-foreLength**2)/(2*upperLength*foreLength),-1,1));
61
+ const pitch = Math.atan2(lift,reach)-Math.atan2(foreLength*Math.sin(bend),upperLength+foreLength*Math.cos(bend));
62
+ const planeVector = angle => add(scale(radialAxis,Math.cos(angle)),scale(Z,Math.sin(angle)));
63
+ const upper = planeVector(pitch), fore = planeVector(pitch+bend);
64
+ const elbow = add(shoulder,scale(upper,upperLength));
65
+ return {yaw,bend,radialAxis,side,shoulder,elbow,wrist:add(elbow,scale(fore,foreLength)),upper,fore};
66
+ }
67
+
68
+ function face(points,fill,stroke=null,width=.43,bias=0) {
69
+ const depth = points.reduce((sum,p)=>sum+dot(p,camera),0)/points.length;
70
+ queue.push({points,fill,stroke,width,depth:depth+bias,bias});
71
+ }
72
+ function line(a,b,stroke="fine",width=.46,bias=.15) {face([a,b],null,stroke,width,bias);}
73
+ function ring(at,u,v,r,n=24,phase=0) {
74
+ return Array.from({length:n},(_,i)=>add(at,scale(radial(u,v,phase+tau*i/n),r)));
75
+ }
76
+ function disk(at,u,v,r,fill="metal",stroke="edge",bias=.2) {face(ring(at,u,v,r),fill,stroke,.55,bias);}
77
+ function visible(normal) {return dot(normal,camera) > .005;}
78
+ function turnedPart(origin,axis,guide,profile,options={}) {
79
+ const u=unit(cross(guide,axis)),v=cross(axis,u);
80
+ const facing=Math.atan2(dot(v,camera),dot(u,camera));
81
+ const n=Math.max(8,(options.sectors??24)/2|0);
82
+ // The two silhouette tangents move continuously; the rim does not switch facets.
83
+ const normals=Array.from({length:n+1},(_,i)=>radial(u,v,facing-Math.PI/2+Math.PI*i/n));
84
+ const sample=([offset,radius])=>{
85
+ const center=add(origin,scale(axis,offset));
86
+ return normals.map(normal=>add(center,scale(normal,radius)));
87
+ };
88
+ let previous=sample(profile[0]);
89
+ face(previous,null,"edge",.46,.12);
90
+ for(let i=1;i<profile.length;i++) {
91
+ const next=sample(profile[i]);
92
+ const [begin,r1]=profile[i-1], [end,r2,palette]=profile[i];
93
+ const sections=Math.max(1,Math.ceil((end-begin)/9));
94
+ for(let j=0;j<n;j++) {
95
+ const normal=unit(add(normals[j],normals[j+1]));
96
+ const shade=clamp(Math.round((dot(normal,[-.42,-.49,.76])+1)*3),0,6);
97
+ const paint=palette??options.color??"shade"+shade;
98
+ for(let k=0;k<sections;k++) {
99
+ const t=k/sections,s=(k+1)/sections;
100
+ face([mix(previous[j],next[j],t),mix(previous[j+1],next[j+1],t),
101
+ mix(previous[j+1],next[j+1],s),mix(previous[j],next[j],s)],paint);
102
+ }
103
+ }
104
+ line(previous[0],next[0],"edge",.53,.12);
105
+ line(previous[n],next[n],"edge",.53,.12);
106
+ if(r1!==r2||i===profile.length-1)face(next,null,"edge",.43,.12);
107
+ previous=next;
108
+ }
109
+ const [offset,radius]=dot(axis,camera)>0?profile.at(-1):profile[0];
110
+ disk(add(origin,scale(axis,offset)),u,v,radius,options.cap??"metal");
111
+ return sample;
112
+ }
113
+ function body(a,b,r,options={}) {
114
+ const offset=sub(b,a),axis=unit(offset);
115
+ const guide=options.guide??(Math.abs(dot(axis,Z))>.95?X:Z);
116
+ return turnedPart(a,axis,guide,[[0,r],[len(offset),r]],options);
117
+ }
118
+ function slab(corners,z0,z1,offset=[0,0,0],top="metal") {
119
+ const layer=z=>corners.map(([x,y])=>add([x,y,z],offset));
120
+ const lower=layer(z0),upper=layer(z1);
121
+ for(let i=0;i<corners.length;i++) {
122
+ const j=(i+1)%corners.length;
123
+ if(visible(cross(sub(lower[j],lower[i]),Z)))face([lower[i],lower[j],upper[j],upper[i]],"dark-metal","edge",.58);
124
+ }
125
+ face(upper,top,"edge",.68);
126
+ }
127
+ const rect=(x0,y0,x1,y1)=>[[x0,y0],[x1,y0],[x1,y1],[x0,y1]];
128
+ function bolt(at,u,v,r=1.35) {
129
+ face(ring(at,u,v,r,6,Math.PI/6),"dark-metal","edge",.39,.4);
130
+ line(add(at,scale(u,-r*.55)),add(at,scale(u,r*.55)),"recess",.36,.55);
131
+ }
132
+ function gear(at,u,v,r,n,angle) {
133
+ const points=Array.from({length:n*4},(_,i)=>{
134
+ const distance=i%4===0||i%4===3?r-.9:r+.55;
135
+ return add(at,scale(radial(u,v,angle+tau*i/(n*4)),distance));
136
+ });
137
+ face(points,"dark-metal","edge",.50,.36);
138
+ disk(add(at,scale(unit(cross(u,v)),.12)),u,v,r-2.5,"metal","edge",.56);
139
+ disk(at,u,v,r*.24,"recess","edge",.8);
140
+ for(let i=0;i<3;i++) {
141
+ const spoke=radial(u,v,angle+i*tau/3);
142
+ line(add(at,scale(spoke,r*.33)),add(at,scale(spoke,r*.72)),"edge",.58,.85);
143
+ }
144
+ const notch=radial(u,v,angle);
145
+ line(add(at,scale(notch,r*.70)),add(at,scale(notch,r*.93)),"recess",.85,.94);
146
+ }
147
+
148
+ const platePath = [[-46,-63],[104,-63],[115,-52],[115,53],[104,64],[-46,64],[-56,54],[-56,-53]];
149
+ function fixtureMark(at) {
150
+ slab(rect(-12,-10,12,10),4.8,6,withZ(at,0),"paper");
151
+ const center=withZ(at,6.12);
152
+ for(const s of [-1,1]) {
153
+ for(const q of [-1,1])bolt(add(center,[s*9,q*7,0]),X,Y,1.05);
154
+ line(add(center,[s*7,-5,0]),add(center,[s*7,5,0]),"fine",.43,.2);
155
+ }
156
+ line(add(center,[0,-4,0]),add(center,[0,4,0]),"fine",.4,.2);
157
+ line(add(center,[-4,0,0]),add(center,[4,0,0]),"fine",.4,.2);
158
+ }
159
+ function staticPlate() {
160
+ slab(platePath,0,2,[0,0,0],"dark-metal");
161
+ slab(platePath.map(([x,y])=>[x*.99,y*.983]),2,5);
162
+ for(const [x,y] of [[-43,-51],[102,-51],[102,52],[-43,52]]) {
163
+ disk([x,y,5.08],X,Y,3.8,"paper","fine");
164
+ bolt([x,y,5.19],X,Y,2);
165
+ }
166
+ for(const at of [fixtureA,fixtureB])fixtureMark(at);
167
+ const motor=[-27,19,5];
168
+ slab(rect(-11,-9,11,9),0,4,motor,"dark-metal");
169
+ for(let i=-3;i<=3;i++)line(add(motor,[i*2.6,-6,4.1]),add(motor,[i*2.6,6,4.1]),"edge",.55,.2);
170
+ for(const shift of [0,3]) {
171
+ line([-15,21+shift,5.4],[-8,21+shift,5.4],"edge",.75);
172
+ line([-8,21+shift,5.4],[-5,16+shift,5.4],"edge",.75);
173
+ }
174
+ }
175
+ function part(at) {
176
+ const oct=[[-6,-4],[6,-4],[6,4],[4,6],[-4,6],[-6,4]];
177
+ slab(oct,0,5,at,"paper");
178
+ disk(add(at,[0,0,5.15]),X,Y,2.4,"recess","edge",.45);
179
+ for(const x of [-4,4])bolt(add(at,[x,0,5.25]),X,Y,.95);
180
+ }
181
+ function scanner(at, locked) {
182
+ const base=withZ(at,6.4);
183
+ const stroke=locked?"sensor":"fine";
184
+ for(const s of [-1,1])for(const q of [-1,1]){
185
+ line(add(base,[s*11,q*5,0]),add(base,[s*11,q*9,0]),stroke,.85,.25);
186
+ line(add(base,[s*11,q*9,0]),add(base,[s*7,q*9,0]),stroke,.85,.25);
187
+ }
188
+ }
189
+ function piston(anchor,pin,side) {
190
+ const d=sub(pin,anchor), axis=unit(d);
191
+ turnedPart(anchor,axis,side,[
192
+ [1,2.20],[2.8,2.20],[2.8,2.03],[13.1,2.03],[13.1,2.5],
193
+ [15.8,2.5,"metal"],[15.8,1.11],[len(d),1.11,"paper"],
194
+ ],{sectors:16,cap:"metal"});
195
+ for(const joint of [anchor,pin])body(add(joint,scale(side,-1.8)),add(joint,scale(side,1.8)),2.05,{sectors:12});
196
+ }
197
+ function ringBolts(at,u,v,r,count=6) {
198
+ for(const p of ring(at,u,v,r,count,Math.PI/6))bolt(p,u,v,1.08);
199
+ }
200
+
201
+ function actuatorTube(a,b,side,r) {
202
+ const extent=len(sub(b,a)),axis=unit(sub(b,a));
203
+ const collar=r+1.05;
204
+ const surface=turnedPart(a,axis,side,[
205
+ [0,r],[1.3,r],[1.3,collar],[3.2,collar,"metal"],[3.2,r],
206
+ [extent-3.2,r],[extent-3.2,collar],[extent-1.3,collar,"metal"],[extent-1.3,r],[extent,r],
207
+ ]);
208
+ for(const offset of [5,6.6,extent*.48-1.8,extent*.48,extent*.48+1.8,extent-6.6,extent-5])
209
+ face(surface([offset,r+.085]),null,"edge",.46,.22);
210
+ }
211
+ function joint(at,side,d,r,u,v) {
212
+ body(add(at,scale(side,-d)),add(at,scale(side,d)),r,{guide:u,cap:"paper"});
213
+ const near=add(at,scale(side,visible(side)?d+.15:-d-.15));
214
+ disk(near,u,v,r*.85,"metal","edge",.35);
215
+ ringBolts(near,u,v,r*.69);
216
+ disk(near,u,v,r*.36,"recess","edge",.62);
217
+ disk(near,u,v,r*.21,"paper","edge",.8);
218
+ }
219
+ function gripper(wrist,jaw,yaw) {
220
+ const at=(x,y,z)=>add(wrist,[x,y,z]);
221
+ body(wrist,at(0,0,-13),4.65,{cap:"paper"});
222
+ body(at(0,0,-6),at(0,0,-11),6.2,{cap:"dark-metal"});
223
+ const index=radial(X,Y,-yaw);
224
+ line(add(at(0,0,-6),scale(index,4.8)),add(at(0,0,-6),scale(index,6.1)),"ink",.85,.55);
225
+ slab(rect(-13,-3.8,13,3.8),-17,-13,wrist,"metal");
226
+ for(const y of [-3,3])body(at(-12,y,-19),at(12,y,-19),.94,{sectors:10,color:"paper"});
227
+ body(at(-12,4.1,-19),at(12,4.1,-19),1.0,{sectors:12,color:"dark-metal"});
228
+ const screwAngle=tau*(openGap-jaw)/2.4;
229
+ for(const sign of [-1,1]) {
230
+ let prev=null;
231
+ for(let j=0;j<=32;j++) {
232
+ const k=j/4,a=screwAngle+tau*k/2.4;
233
+ const n=[0,Math.cos(a),Math.sin(a)];
234
+ const p=at(sign*k,4.1+1.15*n[1],-19+1.15*n[2]);
235
+ if(prev&&visible(n))line(prev,p,"ink",.38,.21);
236
+ prev=p;
237
+ }
238
+ const x=sign*jaw;
239
+ slab(rect(x-1.6,-3.15,x+1.6,3.15),-22,-18,wrist,"dark-metal");
240
+ slab(rect(x-1.3,-2.8,x+1.3,2.8),-28,-21,wrist,"metal");
241
+ const pad=x-sign*1.35;
242
+ slab(rect(pad-.1,-2.25,pad+.1,2.25),-27.4,-23.25,wrist,"recess");
243
+ for(let i=0;i<3;i++)line(at(x-.8,2.9,-24-i),at(x+.8,2.9,-24-i),"fine",.4,.29);
244
+ }
245
+ }
246
+
247
+ function drawArm(t, reverse) {
248
+ const state=trajectory(t,reverse), pose=solve(state.position);
249
+ const {wrist,shoulder,elbow,upper,fore,side,radialAxis,yaw,bend}=pose;
250
+ const beforePickup=t<3.15, afterPlacement=t>=6.45;
251
+ const cargo=beforePickup?withZ(state.a,floor):afterPlacement?withZ(state.b,floor):add(wrist,[0,0,floor-gripZ]);
252
+ part(cargo);
253
+ if(t>0&&t<3.35)scanner(state.a,t>=2);
254
+ if(t>=5.55&&t<7.82)scanner(state.b,t>=6.45);
255
+
256
+ body([0,0,5],[0,0,11],25,{color:"dark-metal",cap:"metal"});
257
+ for(const p of ring([0,0,11.12],X,Y,21,8))bolt(p,X,Y,1.5);
258
+ body([0,0,11],[0,0,18],19,{cap:"paper"});
259
+ gear([0,0,18.16],X,Y,17.4,32,yaw);
260
+ body([0,0,18],[0,0,23],14,{cap:"paper"});
261
+ body([0,0,23],[0,0,43],10.6,{cap:"metal"});
262
+ body([0,0,24],[0,0,27],11.2,{color:"dark-metal"});
263
+ body([0,0,40],[0,0,44],11.75,{cap:"paper"});
264
+ const baseNotch=add([0,0,44.2],scale(radialAxis,8));
265
+ line(baseNotch,add(baseNotch,scale(radialAxis,3)),"ink",1.1,.38);
266
+ for(const sign of [-1,1]) {
267
+ const y=scale(side,sign*8.6);
268
+ body(add([0,0,36],y),add(shoulder,y),3.7,{cap:"dark-metal",sectors:14});
269
+ }
270
+
271
+ actuatorTube(add(shoulder,scale(upper,7)),add(elbow,scale(upper,-6)),side,7);
272
+ actuatorTube(add(elbow,scale(fore,6)),add(wrist,scale(fore,-5)),side,5.5);
273
+ const outward=side;
274
+ const lateral=scale(outward,13);
275
+ const anchor=add(add(shoulder,scale(radialAxis,-1)),add(scale(Z,-14),lateral));
276
+ const pin=add(add(shoulder,scale(upper,24)),lateral);
277
+ piston(anchor,pin,side);
278
+ const elbowAnchor=add(add(elbow,scale(upper,-21)),scale(outward,12));
279
+ const elbowPin=add(add(elbow,scale(fore,11)),scale(outward,12));
280
+ piston(elbowAnchor,elbowPin,side);
281
+
282
+ const planeV=unit(cross(outward,upper));
283
+ joint(shoulder,side,10,9,radialAxis,Z);
284
+ joint(elbow,side,9.5,8,upper,planeV);
285
+ joint(wrist,side,6,4.8,radialAxis,Z);
286
+
287
+ // The pair is mounted on the upper-arm bracket. Both pitch circles meet on its centerline.
288
+ const ringCenter=add(elbow,scale(outward,11.6));
289
+ const pinionCenter=add(ringCenter,scale(upper,-16.2));
290
+ const bracket=scale(outward,-2.4);
291
+ body(add(pinionCenter,bracket),add(ringCenter,bracket),3.7,{guide:outward,color:"dark-metal",sectors:12});
292
+ body(add(pinionCenter,scale(outward,-4)),pinionCenter,4.65,{guide:upper,color:"dark-metal"});
293
+ const drivenAngle=-bend;
294
+ gear(ringCenter,upper,planeV,10.8,24,drivenAngle);
295
+ gear(pinionCenter,upper,planeV,5.4,12,Math.PI/12-2*drivenAngle);
296
+ gripper(wrist,state.jaw,yaw);
297
+ }
298
+
299
+ function palette(ink, paper) {
300
+ const pixel = document.createElement("canvas");
301
+ pixel.width=pixel.height=1;
302
+ const sampler = pixel.getContext("2d", { willReadFrequently:true });
303
+ const rgb = value => {
304
+ sampler.fillStyle=value;
305
+ sampler.fillRect(0,0,1,1);
306
+ return Array.from(sampler.getImageData(0,0,1,1).data).slice(0,3);
307
+ };
308
+ const a=rgb(ink), b=rgb(paper);
309
+ const tone = amount => "rgb("+b.map((c,i)=>Math.round(c+(a[i]-c)*amount)).join(",")+")";
310
+ const colors={paper,ink,edge:tone(.82),fine:tone(.59),metal:tone(.11),
311
+ "dark-metal":tone(.25),recess:tone(.44),sensor:ink};
312
+ for(let i=0;i<7;i++) colors["shade"+i]=tone(.36-i*.048);
313
+ return colors;
314
+ }
315
+
316
+ function paint(context,colors) {
317
+ context.lineJoin="round";context.lineCap="round";
318
+ queue.sort((a,b)=>a.depth-b.depth);
319
+ for(const {points,fill,stroke,width} of queue) {
320
+ context.beginPath();context.moveTo(...project(points[0]));
321
+ for(let i=1;i<points.length;i++)context.lineTo(...project(points[i]));
322
+ if(fill){context.closePath();context.fillStyle=colors[fill];context.fill();}
323
+ if(stroke){context.strokeStyle=colors[stroke];context.lineWidth=width;context.stroke();}
324
+ }
325
+ queue.length=0;
326
+ }
327
+
328
+ function createDepthRenderer(context) {
329
+ const layer=document.createElement("canvas");
330
+ const gl=layer.getContext("webgl",{alpha:true,antialias:true,depth:true,premultipliedAlpha:false,preserveDrawingBuffer:true});
331
+ if(!gl)return null;
332
+ function shader(type,source) {
333
+ const compiled=gl.createShader(type);
334
+ gl.shaderSource(compiled,source);gl.compileShader(compiled);
335
+ return gl.getShaderParameter(compiled,gl.COMPILE_STATUS)?compiled:null;
336
+ }
337
+ const vertex=shader(gl.VERTEX_SHADER,
338
+ "attribute vec3 position; attribute vec3 color; varying vec3 ink; void main(){gl_Position=vec4(position,1.0);ink=color;}");
339
+ const fragment=shader(gl.FRAGMENT_SHADER,
340
+ "precision mediump float; varying vec3 ink; void main(){gl_FragColor=vec4(ink,1.0);}");
341
+ if(!vertex||!fragment)return null;
342
+ const program=gl.createProgram();
343
+ gl.attachShader(program,vertex);gl.attachShader(program,fragment);gl.linkProgram(program);
344
+ if(!gl.getProgramParameter(program,gl.LINK_STATUS))return null;
345
+ gl.useProgram(program);
346
+ gl.enable(gl.DEPTH_TEST);gl.depthFunc(gl.LEQUAL);
347
+ gl.bindBuffer(gl.ARRAY_BUFFER,gl.createBuffer());
348
+ for(const [name,offset] of [["position",0],["color",12]]){
349
+ const index=gl.getAttribLocation(program,name);
350
+ gl.enableVertexAttribArray(index);gl.vertexAttribPointer(index,3,gl.FLOAT,false,24,offset);
351
+ }
352
+ const pixel=document.createElement("canvas");pixel.width=pixel.height=1;
353
+ const sampler=pixel.getContext("2d",{willReadFrequently:true});
354
+ const rgb=new Map();
355
+ const colorFor=(key,colors)=>{
356
+ if(!rgb.has(key)){
357
+ sampler.fillStyle=colors[key]??key;sampler.fillRect(0,0,1,1);
358
+ const rgba=sampler.getImageData(0,0,1,1).data;
359
+ rgb.set(key,[rgba[0]/255,rgba[1]/255,rgba[2]/255]);
360
+ }
361
+ return rgb.get(key);
362
+ };
363
+ let stream=new Float32Array(65536),cursor=0;
364
+ function vertexAt(p,ink) {
365
+ if(cursor+6>stream.length){const next=new Float32Array(stream.length*2);next.set(stream);stream=next;}
366
+ stream[cursor++]=p[0];stream[cursor++]=p[1];stream[cursor++]=p[2];
367
+ stream[cursor++]=ink[0];stream[cursor++]=ink[1];stream[cursor++]=ink[2];
368
+ }
369
+ function triangle(a,b,c,ink) {vertexAt(a,ink);vertexAt(b,ink);vertexAt(c,ink);}
370
+ function frame(geometry,zoom,posX,posY,dpr,colors) {
371
+ if(gl.isContextLost())return false;
372
+ const {width:w,height:h}=context.canvas;
373
+ if(layer.width!==w||layer.height!==h){layer.width=w;layer.height=h;}
374
+ gl.viewport(0,0,w,h);
375
+ gl.clearColor(0,0,0,0);gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT);
376
+ cursor=0;
377
+ for(const {points,fill,stroke,width,bias} of geometry){
378
+ const vertices=points.map(p=>{
379
+ const [x,y]=project(p);
380
+ return [2*(x*zoom+posX)/w-1,1-2*(y*zoom+posY)/h,-(dot(p,camera)+bias*.05)/384];
381
+ });
382
+ if(fill){
383
+ const ink=colorFor(fill,colors);
384
+ if(vertices.length===4) {
385
+ triangle(vertices[0],vertices[1],vertices[2],ink);
386
+ triangle(vertices[0],vertices[2],vertices[3],ink);
387
+ } else {
388
+ const center=scale(vertices.reduce(add,[0,0,0]),1/vertices.length);
389
+ for(let i=0;i<vertices.length;i++)triangle(center,vertices[i],vertices[(i+1)%vertices.length],ink);
390
+ }
391
+ }
392
+ if(stroke){
393
+ const ink=colorFor(stroke,colors),count=fill?vertices.length:vertices.length-1;
394
+ for(let i=0;i<count;i++){
395
+ const a=vertices[i], b=vertices[(i+1)%vertices.length];
396
+ const dx=(b[0]-a[0])*w,dy=(b[1]-a[1])*h;
397
+ const distance=Math.hypot(dx,dy);
398
+ if(distance<.001)continue;
399
+ const thick=Math.max(.74*dpr,width*zoom),x=-dy/distance*thick/w,y=dx/distance*thick/h;
400
+ const forward=.00023;
401
+ const p=[a[0]-x,a[1]-y,a[2]-forward],q=[a[0]+x,a[1]+y,a[2]-forward];
402
+ const r=[b[0]-x,b[1]-y,b[2]-forward],s=[b[0]+x,b[1]+y,b[2]-forward];
403
+ triangle(p,q,r,ink);triangle(q,s,r,ink);
404
+ }
405
+ }
406
+ }
407
+ gl.bufferData(gl.ARRAY_BUFFER,stream.subarray(0,cursor),gl.DYNAMIC_DRAW);
408
+ gl.drawArrays(gl.TRIANGLES,0,cursor/6);
409
+ context.drawImage(layer,0,0);
410
+ return true;
411
+ }
412
+ return {frame,setColors:()=>rgb.clear()};
413
+ }
414
+ globalThis.ccdxToolArm = context => {
415
+ let key, platform, colors, zoom, left, top;
416
+ let depthView;
417
+ try { depthView=createDepthRenderer(context); } catch { /* Continuous 2D geometry is available without WebGL. */ }
418
+ staticPlate();
419
+ const plate=queue.splice(0);
420
+ return (_style,{time,width,height,dpr,ink,surface}) => {
421
+ const next=[width,height,dpr,ink,surface].join("|");
422
+ if(key!==next){
423
+ const {width:w,height:h}=context.canvas;
424
+ const size=Math.min((width-12)/254,(height-12)/180)*dpr;
425
+ if(!(size>0))return;
426
+ zoom=size;left=w/2-21*zoom;top=h/2-5*zoom;
427
+ colors=palette(ink,surface);
428
+ platform=null;depthView?.setColors();
429
+ key=next;
430
+ }
431
+ context.setTransform(1,0,0,1,0,0);
432
+ const elapsed=Math.max(0,time),cycle=Math.floor(elapsed/duration);
433
+ drawArm(elapsed-cycle*duration,cycle%2===1);
434
+ if(depthView?.frame([...plate,...queue],zoom,left,top,dpr,colors)){
435
+ queue.length=0;
436
+ return;
437
+ }
438
+ if(!platform){
439
+ const moving=queue.splice(0);
440
+ platform=document.createElement("canvas");
441
+ platform.width=context.canvas.width;platform.height=context.canvas.height;
442
+ const base=platform.getContext("2d");
443
+ base.setTransform(zoom,0,0,zoom,left,top);
444
+ queue.push(...plate);paint(base,colors);queue.push(...moving);
445
+ }
446
+ context.drawImage(platform,0,0);
447
+ context.setTransform(zoom,0,0,zoom,left,top);paint(context,colors);
448
+ };
449
+ };
450
+ })();