ICP-Lab / app.js
devkumarhf's picture
Publish browser-verified educational ICP static demo
7bd1cc6 verified
Raw History Blame Contribute Delete
4.61 kB
import {referenceCloud,transformCloud,nearestRMSE,stepICP} from './icp.js';
const $=id=>document.getElementById(id);
const canvas=$('cloud'),chart=$('chart'),ctx=canvas.getContext('2d'),chartCtx=chart.getContext('2d');
const sliders=['rotation','shift','noise'].map($);
let target=[],moving=[],history=[],running=false,timer=null,iteration=0,yaw=-.55,pitch=.58,drag=null,initial=0;
function canvasSize(el,context){
const rect=el.getBoundingClientRect(),ratio=window.devicePixelRatio||1;
const w=Math.max(1,Math.round(rect.width*ratio)),h=Math.max(1,Math.round(rect.height*ratio));
if(el.width!==w||el.height!==h){el.width=w;el.height=h;}
context.setTransform(ratio,0,0,ratio,0,0);return [rect.width,rect.height];
}
function project([x,y,z],w,h){
const cy=Math.cos(yaw),sy=Math.sin(yaw),cp=Math.cos(pitch),sp=Math.sin(pitch);
const x1=cy*x-sy*y,y1=sy*x+cy*y;
const y2=cp*y1-sp*z,depth=sp*y1+cp*z;
const size=Math.min(w,h)*.295,perspective=3.8/(3.8-depth);
return [w*.5+size*x1*perspective,h*.49-size*y2*perspective,depth];
}
function drawCloud(){
const [w,h]=canvasSize(canvas,ctx);ctx.clearRect(0,0,w,h);
ctx.strokeStyle='rgba(175,228,187,.075)';ctx.lineWidth=1;
for(let i=-4;i<=4;i++){
const a=project([i/4,-1,-.35],w,h),b=project([i/4,1,-.35],w,h);
const c=project([-1,i/4,-.35],w,h),d=project([1,i/4,-.35],w,h);
ctx.beginPath();ctx.moveTo(a[0],a[1]);ctx.lineTo(b[0],b[1]);ctx.moveTo(c[0],c[1]);ctx.lineTo(d[0],d[1]);ctx.stroke();
}
const dots=[...target.map(p=>({p,color:'184,255,191'})),...moving.map(p=>({p,color:'230,164,134'}))];
dots.map(d=>({...d,screen:project(d.p,w,h)})).sort((a,b)=>a.screen[2]-b.screen[2]).forEach(({screen,color})=>{
const [x,y,z]=screen,r=Math.max(1.5,2.4+z*.7);
ctx.beginPath();ctx.fillStyle=`rgba(${color},${Math.min(.95,Math.max(.35,.64+z*.2))})`;ctx.arc(x,y,r,0,2*Math.PI);ctx.fill();
});
}
function drawChart(){
const [w,h]=canvasSize(chart,chartCtx);chartCtx.clearRect(0,0,w,h);
const pad=5;chartCtx.strokeStyle='#304238';chartCtx.lineWidth=1;
for(let k=1;k<4;k++){const y=pad+(h-2*pad)*k/4;chartCtx.beginPath();chartCtx.moveTo(pad,y);chartCtx.lineTo(w-pad,y);chartCtx.stroke();}
if(history.length<2)return;
const max=Math.max(...history,1e-6)*1.1;
chartCtx.beginPath();history.forEach((v,i)=>{const x=pad+(w-2*pad)*i/(history.length-1),y=h-pad-(h-2*pad)*v/max;i?chartCtx.lineTo(x,y):chartCtx.moveTo(x,y);});
chartCtx.strokeStyle='#b8ffbf';chartCtx.lineWidth=2;chartCtx.stroke();
}
function show(){
$('iterations').textContent=iteration;$('rmse').textContent=history.at(-1).toFixed(4);$('initial').textContent=initial.toFixed(4);
drawCloud();drawChart();
}
function stop(message){
running=false;clearTimeout(timer);$('play').innerHTML='▶ &nbsp; Run ICP';
$('status').textContent=message;$('statusDot').style.background='#91d89a';
}
function reset(){
stop('Ready to align');target=referenceCloud();
const rotation=Number($('rotation').value),shift=Number($('shift').value),noise=Number($('noise').value);
$('rotationVal').textContent=`${rotation}°`;$('shiftVal').textContent=shift.toFixed(2);$('noiseVal').textContent=noise.toFixed(3);
moving=transformCloud(target,rotation,shift,noise);iteration=0;initial=nearestRMSE(moving,target);history=[initial];show();
}
function step(){
if(iteration>=80){stop('Iteration limit reached');return false;}
const old=history.at(-1),result=stepICP(moving,target);moving=result.points;iteration++;history.push(result.rmse);show();
if(Math.abs(old-result.rmse)<1e-7){stop('Converged to a local solution');return false;}
$('status').textContent=`Iteration ${iteration} · matching and fitting`;return true;
}
function tick(){if(!running)return;if(step()&&running)timer=setTimeout(tick,180);}
$('play').addEventListener('click',()=>{if(running){stop('Paused');return;}running=true;$('play').textContent='Ⅱ Pause';$('status').textContent='Matching and fitting';tick();});
$('step').addEventListener('click',()=>{if(running)stop('Paused');step();});
$('reset').addEventListener('click',reset);sliders.forEach(el=>el.addEventListener('input',reset));
canvas.addEventListener('pointerdown',e=>{drag={x:e.clientX,y:e.clientY};canvas.setPointerCapture(e.pointerId);});
canvas.addEventListener('pointermove',e=>{if(!drag)return;yaw+=(e.clientX-drag.x)*.009;pitch=Math.max(-1.35,Math.min(1.35,pitch+(e.clientY-drag.y)*.009));drag={x:e.clientX,y:e.clientY};drawCloud();});
canvas.addEventListener('pointerup',()=>drag=null);canvas.addEventListener('pointercancel',()=>drag=null);
window.addEventListener('resize',()=>{drawCloud();drawChart();});reset();