File size: 4,611 Bytes
7bd1cc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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();