Spaces:
Running
Running
Download frontend/src/components/scene/scanCell.js from xenecode/Jaguar_Project: direct link, hf CLI and curl.
- Browser
- Download file 14 kB
-
https://huggingface.co/spaces/xenecode/Jaguar_Project/resolve/main/frontend/src/components/scene/scanCell.js
- Command line
-
hf download hf://spaces/xenecode/Jaguar_Project/frontend/src/components/scene/scanCell.js
-
curl -L -o scanCell.js https://huggingface.co/spaces/xenecode/Jaguar_Project/resolve/main/frontend/src/components/scene/scanCell.js
14 kB
| import * as THREE from 'three'; | |
| import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'; | |
| import { poseMatrix } from './kinematics'; | |
| import { GRIPPER_LENGTH, LASER_POSITION, PART_SIZE, PICK_Z, SLOTS, TRAY_PLATES } from './cellLayout'; | |
| // Mech-Eye LNX-8080 model: converted from the STEP file on the Mech-Mind download center to GLB. | |
| // Origin is at the center of the laser window, laser points in +Y, the fan opens along the Z axis. | |
| const PROFILER_URL = `${process.env.PUBLIC_URL}/sensors/lnx8080.glb`; | |
| const PROFILER_DEPTH = 0.1136; // body height below the window | |
| const PROFILER_CENTER_X = -0.077; // center along the body's long axis | |
| // LNX-8080 FOV drawing: fan width is ~34 mm at the window, 76 mm at 210 mm, 96 mm at 310 mm | |
| const RAY_COUNT = 160; | |
| const WINDOW_HALF_WIDTH = 0.017; | |
| const FAR_DISTANCE = 0.31; | |
| const FAR_HALF_WIDTH = 0.048; | |
| const NEAR_RANGE = 0.21; | |
| const RANGE_DEPTH = 0.1; | |
| const MAX_CLOUD_POINTS = 60000; | |
| const PROFILE_STEP = 0.0008; // record a new profile once the part has moved this far | |
| const SENSOR_REACH = 0.4; // don't cast rays if the part is farther than this from the laser | |
| const SMALL_PART_SCALE = 0.7; | |
| const mm = (v) => v / 1000; | |
| // Machined aluminum block: has a notch and a step on its edges, so they show up in the laser profile | |
| function createPartGeometry() { | |
| const [sx, sy, sz] = PART_SIZE.map(mm); | |
| const hx = sx / 2; | |
| const hy = sy / 2; | |
| const r = 0.004; | |
| const notch = 0.008; | |
| const step = 0.012; | |
| const stepDepth = 0.005; | |
| const shape = new THREE.Shape(); | |
| shape.moveTo(-hx + r, -hy); | |
| // -Y edge: step | |
| shape.lineTo(-step, -hy); | |
| shape.lineTo(-step, -hy + stepDepth); | |
| shape.lineTo(step, -hy + stepDepth); | |
| shape.lineTo(step, -hy); | |
| shape.lineTo(hx - r, -hy); | |
| shape.quadraticCurveTo(hx, -hy, hx, -hy + r); | |
| // +X edge: semicircular notch | |
| shape.lineTo(hx, -notch); | |
| shape.absarc(hx, 0, notch, -Math.PI / 2, Math.PI / 2, true); | |
| shape.lineTo(hx, hy - r); | |
| shape.quadraticCurveTo(hx, hy, hx - r, hy); | |
| // +Y edge: step | |
| shape.lineTo(step, hy); | |
| shape.lineTo(step, hy - stepDepth); | |
| shape.lineTo(-step, hy - stepDepth); | |
| shape.lineTo(-step, hy); | |
| shape.lineTo(-hx + r, hy); | |
| shape.quadraticCurveTo(-hx, hy, -hx, hy - r); | |
| shape.lineTo(-hx, -hy + r); | |
| shape.quadraticCurveTo(-hx, -hy, -hx + r, -hy); | |
| const hole = new THREE.Path(); | |
| hole.absarc(-0.018, 0, 0.009, 0, Math.PI * 2, true); | |
| shape.holes.push(hole); | |
| const bevel = 0.0015; | |
| const geometry = new THREE.ExtrudeGeometry(shape, { | |
| depth: sz - 2 * bevel, | |
| bevelEnabled: true, | |
| bevelThickness: bevel, | |
| bevelSize: bevel, | |
| bevelSegments: 1, | |
| curveSegments: 16, | |
| }); | |
| // The local origin is the center of the held face, the part extends in +Z | |
| geometry.translate(0, 0, bevel); | |
| return geometry; | |
| } | |
| function createGripper() { | |
| const gripper = new THREE.Group(); | |
| const dark = new THREE.MeshStandardMaterial({ color: 0x3a3f46, metalness: 0.6, roughness: 0.4 }); | |
| const rubber = new THREE.MeshStandardMaterial({ color: 0x121316, roughness: 0.85 }); | |
| const length = mm(GRIPPER_LENGTH); | |
| const adapter = new THREE.Mesh(new THREE.CylinderGeometry(0.0315, 0.0315, 0.014, 32), dark); | |
| adapter.rotation.x = Math.PI / 2; | |
| adapter.position.z = 0.007; | |
| const body = new THREE.Mesh(new THREE.BoxGeometry(0.036, 0.036, 0.07), dark); | |
| body.position.z = 0.049; | |
| const stemLength = length - 0.084 - 0.012; | |
| const stem = new THREE.Mesh(new THREE.CylinderGeometry(0.006, 0.006, stemLength, 16), dark); | |
| stem.rotation.x = Math.PI / 2; | |
| stem.position.z = 0.084 + stemLength / 2; | |
| // The cylinder's +Y end becomes +Z after rotation: the wide mouth faces the part | |
| const cup = new THREE.Mesh(new THREE.CylinderGeometry(0.016, 0.007, 0.012, 24), rubber); | |
| cup.rotation.x = Math.PI / 2; | |
| cup.position.z = length - 0.006; | |
| [adapter, body, stem, cup].forEach((mesh) => { | |
| mesh.castShadow = true; | |
| gripper.add(mesh); | |
| }); | |
| const tip = new THREE.Object3D(); | |
| tip.position.z = length; | |
| gripper.add(tip); | |
| return { gripper, tip }; | |
| } | |
| export function createScanCell(robotBase) { | |
| let disposed = false; | |
| // Trays and parts | |
| const plateMaterial = new THREE.MeshStandardMaterial({ color: 0x24272c, roughness: 0.9 }); | |
| TRAY_PLATES.forEach(([cx, cy, width, depth]) => { | |
| const plate = new THREE.Mesh(new THREE.BoxGeometry(mm(width), mm(depth), 0.004), plateMaterial); | |
| plate.position.set(mm(cx), mm(cy), 0.002); | |
| plate.receiveShadow = true; | |
| robotBase.add(plate); | |
| }); | |
| const partGeometry = createPartGeometry(); | |
| const partMaterial = new THREE.MeshStandardMaterial({ color: 0xaab1b9, metalness: 0.55, roughness: 0.38 }); | |
| const tipOffset = new THREE.Matrix4().makeTranslation(0, 0, mm(GRIPPER_LENGTH)); | |
| const slotMatrix = new THREE.Matrix4(); | |
| const makePart = () => { | |
| const mesh = new THREE.Mesh(partGeometry, partMaterial); | |
| mesh.castShadow = true; | |
| mesh.receiveShadow = true; | |
| return mesh; | |
| }; | |
| const placeInSlot = (mesh, index) => { | |
| const slot = SLOTS[index]; | |
| poseMatrix([slot.x, slot.y, PICK_Z, 180, 0, slot.rz], slotMatrix).multiply(tipOffset); | |
| robotBase.add(mesh); | |
| slotMatrix.decompose(mesh.position, mesh.quaternion, mesh.scale); | |
| if (slot.small) mesh.scale.set(SMALL_PART_SCALE, SMALL_PART_SCALE, 1); | |
| }; | |
| const parts = SLOTS.map((_, index) => { | |
| const mesh = makePart(); | |
| placeInSlot(mesh, index); | |
| return mesh; | |
| }); | |
| // The part the robot is holding during a live scan (which slot it came from is unknown) | |
| const sparePart = makePart(); | |
| sparePart.visible = false; | |
| robotBase.add(sparePart); | |
| const { gripper, tip } = createGripper(); | |
| // Laser profiler and its stand. The station frame matches the model's own. | |
| // Lens up: the model's +Y (laser) maps to the robot's +Z. The body's long edge lies along the robot Y axis, | |
| // extending toward the robot; the laser line opens along robot X, the part is passed through along Y | |
| const station = new THREE.Group(); | |
| station.position.set(...LASER_POSITION.map(mm)); | |
| station.rotation.set(Math.PI / 2, 0, -Math.PI / 2, 'ZYX'); | |
| robotBase.add(station); | |
| const aluminium = new THREE.MeshStandardMaterial({ color: 0x8a9199, metalness: 0.7, roughness: 0.35 }); | |
| const laserHeight = mm(LASER_POSITION[2]); | |
| const bracket = new THREE.Mesh(new THREE.BoxGeometry(0.19, 0.006, 0.07), aluminium); | |
| bracket.position.set(PROFILER_CENTER_X, -PROFILER_DEPTH - 0.003, 0); | |
| const postHeight = laserHeight - PROFILER_DEPTH - 0.006 - 0.008; | |
| const post = new THREE.Mesh(new THREE.BoxGeometry(0.04, postHeight, 0.04), aluminium); | |
| post.position.set(PROFILER_CENTER_X, -laserHeight + 0.008 + postHeight / 2, 0); | |
| const foot = new THREE.Mesh(new THREE.BoxGeometry(0.14, 0.008, 0.1), aluminium); | |
| foot.position.set(PROFILER_CENTER_X, -laserHeight + 0.004, 0); | |
| [bracket, post, foot].forEach((mesh) => { | |
| mesh.castShadow = true; | |
| mesh.receiveShadow = true; | |
| station.add(mesh); | |
| }); | |
| new GLTFLoader().load( | |
| PROFILER_URL, | |
| (gltf) => { | |
| if (disposed) return; | |
| gltf.scene.traverse((child) => { | |
| if (child.isMesh) { | |
| child.castShadow = true; | |
| child.receiveShadow = true; | |
| } | |
| }); | |
| station.add(gltf.scene); | |
| }, | |
| undefined, | |
| (error) => console.error('Failed to load laser profiler model:', error) | |
| ); | |
| // Laser fan: for each ray, the bottom (window) and top (hit point or end of range) corner | |
| const laser = new THREE.Group(); | |
| laser.visible = false; | |
| station.add(laser); | |
| const rayStarts = []; | |
| const rayEnds = []; | |
| for (let i = 0; i < RAY_COUNT; i++) { | |
| const u = i / (RAY_COUNT - 1) - 0.5; | |
| rayStarts.push(new THREE.Vector3(0, 0, u * 2 * WINDOW_HALF_WIDTH)); | |
| rayEnds.push(new THREE.Vector3(0, FAR_DISTANCE, u * 2 * FAR_HALF_WIDTH)); | |
| } | |
| const fanPositions = new Float32Array(RAY_COUNT * 6); | |
| const fanColors = new Float32Array(RAY_COUNT * 6); | |
| const fanIndex = []; | |
| for (let i = 0; i < RAY_COUNT - 1; i++) { | |
| const a = 2 * i; | |
| fanIndex.push(a, a + 1, a + 2, a + 1, a + 3, a + 2); | |
| } | |
| const fanGeometry = new THREE.BufferGeometry(); | |
| fanGeometry.setAttribute('position', new THREE.BufferAttribute(fanPositions, 3)); | |
| fanGeometry.setAttribute('color', new THREE.BufferAttribute(fanColors, 3)); | |
| fanGeometry.setIndex(fanIndex); | |
| const fan = new THREE.Mesh( | |
| fanGeometry, | |
| new THREE.MeshBasicMaterial({ | |
| vertexColors: true, | |
| transparent: true, | |
| blending: THREE.AdditiveBlending, | |
| depthWrite: false, | |
| side: THREE.DoubleSide, | |
| }) | |
| ); | |
| fan.frustumCulled = false; | |
| laser.add(fan); | |
| const linePositions = new Float32Array(RAY_COUNT * 3); | |
| const lineGeometry = new THREE.BufferGeometry(); | |
| lineGeometry.setAttribute('position', new THREE.BufferAttribute(linePositions, 3)); | |
| const laserLine = new THREE.Points( | |
| lineGeometry, | |
| new THREE.PointsMaterial({ | |
| color: 0xdbe2ff, | |
| size: 3, | |
| sizeAttenuation: false, | |
| transparent: true, | |
| blending: THREE.AdditiveBlending, | |
| depthWrite: false, | |
| }) | |
| ); | |
| laserLine.frustumCulled = false; | |
| laser.add(laserLine); | |
| const emitter = new THREE.Mesh( | |
| new THREE.PlaneGeometry(0.004, 2 * WINDOW_HALF_WIDTH), | |
| new THREE.MeshBasicMaterial({ color: 0x8b9bff, side: THREE.DoubleSide }) | |
| ); | |
| emitter.rotation.x = -Math.PI / 2; | |
| emitter.position.y = 0.0005; | |
| laser.add(emitter); | |
| // The point cloud accumulates attached to the part: the cloud stays on it as the part moves | |
| const cloudPositions = new Float32Array(MAX_CLOUD_POINTS * 3); | |
| const cloudColors = new Float32Array(MAX_CLOUD_POINTS * 3); | |
| const cloudGeometry = new THREE.BufferGeometry(); | |
| cloudGeometry.setAttribute('position', new THREE.BufferAttribute(cloudPositions, 3)); | |
| cloudGeometry.setAttribute('color', new THREE.BufferAttribute(cloudColors, 3)); | |
| cloudGeometry.setDrawRange(0, 0); | |
| const cloud = new THREE.Points( | |
| cloudGeometry, | |
| new THREE.PointsMaterial({ size: 2, sizeAttenuation: false, vertexColors: true }) | |
| ); | |
| cloud.frustumCulled = false; | |
| let cloudCount = 0; | |
| let held = null; | |
| let heldSlot = -1; | |
| let laserOn = false; | |
| let recording = false; | |
| const lastProfile = new THREE.Vector3(Infinity, Infinity, Infinity); | |
| const attach = (mesh) => { | |
| tip.add(mesh); | |
| mesh.position.set(0, 0, 0); | |
| mesh.quaternion.identity(); | |
| mesh.visible = true; | |
| held = mesh; | |
| mesh.add(cloud); | |
| cloudCount = 0; | |
| cloudGeometry.setDrawRange(0, 0); | |
| lastProfile.set(Infinity, Infinity, Infinity); | |
| }; | |
| const pickFromSlot = (index) => { | |
| if (held) placeHeld(); | |
| attach(parts[index]); | |
| heldSlot = index; | |
| }; | |
| function placeHeld() { | |
| if (!held) return; | |
| if (held === sparePart) { | |
| sparePart.visible = false; | |
| robotBase.add(sparePart); | |
| } else { | |
| placeInSlot(held, heldSlot); | |
| } | |
| held = null; | |
| heldSlot = -1; | |
| } | |
| const setLiveHold = (holding) => { | |
| if (holding && !held) attach(sparePart); | |
| else if (!holding && held === sparePart) placeHeld(); | |
| }; | |
| const setLaser = (on, record = false) => { | |
| laserOn = on; | |
| recording = on && record; | |
| }; | |
| const raycaster = new THREE.Raycaster(); | |
| const intersections = []; | |
| const origin = new THREE.Vector3(); | |
| const direction = new THREE.Vector3(); | |
| const top = new THREE.Vector3(); | |
| const partPosition = new THREE.Vector3(); | |
| const cloudPoint = new THREE.Vector3(); | |
| const color = new THREE.Color(); | |
| // Called every frame after the scene's world matrices have been updated | |
| const update = () => { | |
| laser.visible = laserOn; | |
| if (!laserOn) return; | |
| let target = held; | |
| if (target) { | |
| target.getWorldPosition(partPosition); | |
| station.worldToLocal(partPosition); | |
| if (partPosition.length() > SENSOR_REACH) target = null; | |
| } | |
| const record = recording && target && partPosition.distanceTo(lastProfile) >= PROFILE_STEP; | |
| let hits = 0; | |
| for (let i = 0; i < RAY_COUNT; i++) { | |
| top.copy(rayEnds[i]); | |
| let hit = false; | |
| if (target) { | |
| origin.copy(rayStarts[i]).applyMatrix4(station.matrixWorld); | |
| direction.copy(rayEnds[i]).applyMatrix4(station.matrixWorld).sub(origin); | |
| const length = direction.length(); | |
| raycaster.set(origin, direction.divideScalar(length)); | |
| raycaster.far = length; | |
| intersections.length = 0; | |
| target.raycast(raycaster, intersections); | |
| if (intersections.length) { | |
| intersections.sort((a, b) => a.distance - b.distance); | |
| const { point } = intersections[0]; | |
| top.copy(point); | |
| station.worldToLocal(top); | |
| linePositions.set([top.x, top.y, top.z], hits * 3); | |
| hits += 1; | |
| hit = true; | |
| if (record && cloudCount < MAX_CLOUD_POINTS) { | |
| cloudPoint.copy(point); | |
| target.worldToLocal(cloudPoint); | |
| cloudPoint.toArray(cloudPositions, cloudCount * 3); | |
| // Distance colors: near is red, far is blue (like a depth map) | |
| const depth = THREE.MathUtils.clamp((top.y - NEAR_RANGE) / RANGE_DEPTH, 0, 1); | |
| color.setHSL(0.66 * depth, 1, 0.55).toArray(cloudColors, cloudCount * 3); | |
| cloudCount += 1; | |
| } | |
| } | |
| } | |
| const start = rayStarts[i]; | |
| fanPositions.set([start.x, start.y, start.z, top.x, top.y, top.z], i * 6); | |
| const glow = hit ? 0.55 : 0.12; | |
| fanColors.set([0.16, 0.2, 0.62, glow * 0.4, glow * 0.5, glow], i * 6); | |
| } | |
| fanGeometry.attributes.position.needsUpdate = true; | |
| fanGeometry.attributes.color.needsUpdate = true; | |
| lineGeometry.setDrawRange(0, hits); | |
| lineGeometry.attributes.position.needsUpdate = true; | |
| if (record && hits) { | |
| lastProfile.copy(partPosition); | |
| cloudGeometry.setDrawRange(0, cloudCount); | |
| cloudGeometry.attributes.position.needsUpdate = true; | |
| cloudGeometry.attributes.color.needsUpdate = true; | |
| } | |
| }; | |
| return { | |
| attachGripper: (flange) => flange.add(gripper), | |
| pickFromSlot, | |
| placeHeld, | |
| setLiveHold, | |
| setLaser, | |
| update, | |
| dispose: () => { | |
| disposed = true; | |
| }, | |
| }; | |
| } | |