Spaces:
Running
Running
Publish browser-verified educational ICP static demo
Browse files- LICENSE +21 -0
- README.md +47 -4
- app.js +70 -0
- icp.js +94 -0
- index.html +22 -18
- package.json +1 -0
- style.css +1 -28
- test-ui.mjs +23 -0
- test.mjs +16 -0
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2026 Dev Kumar
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
README.md
CHANGED
|
@@ -1,10 +1,53 @@
|
|
| 1 |
---
|
| 2 |
-
title: ICP Lab
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: green
|
| 5 |
-
colorTo:
|
| 6 |
sdk: static
|
|
|
|
| 7 |
pinned: false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: ICP Lab - 3D Point Cloud Alignment
|
| 3 |
+
emoji: 🎯
|
| 4 |
colorFrom: green
|
| 5 |
+
colorTo: gray
|
| 6 |
sdk: static
|
| 7 |
+
app_file: index.html
|
| 8 |
pinned: false
|
| 9 |
+
license: mit
|
| 10 |
+
tags:
|
| 11 |
+
- robotics
|
| 12 |
+
- computer-vision
|
| 13 |
+
- point-cloud
|
| 14 |
+
- education
|
| 15 |
+
short_description: Explore 3D point-cloud alignment step by step.
|
| 16 |
---
|
| 17 |
|
| 18 |
+
# ICP Lab: 3D point-cloud alignment
|
| 19 |
+
|
| 20 |
+
An interactive, CPU-friendly educational demonstration of rigid point-to-point Iterative Closest Point (ICP), inspired by [Dev Kumar's 3D Point Cloud Registration with ICP project](https://github.com/devkumar-projects/Classical-Computer-Vision-Toolkit/tree/main/3d-point-cloud-registration-icp). It generates a synthetic asymmetric surface, applies a known perturbation and lets visitors inspect the iterative alignment.
|
| 21 |
+
|
| 22 |
+
Drag to rotate the 3D view. Adjust rotation, translation and Gaussian noise; run the algorithm or advance one iteration at a time. The chart shows nearest-neighbour root mean square error (RMSE) in arbitrary units. The UI uses a perspective-projected view, while ICP itself fits a full 3D rigid transformation.
|
| 23 |
+
|
| 24 |
+
## Method and limits
|
| 25 |
+
|
| 26 |
+
Each iteration finds Euclidean nearest-neighbour correspondences by brute force, then estimates the least-squares rigid transformation via Horn's unit-quaternion formulation. This implementation is a new, independent educational adaptation, not an exact execution of the source notebook or Open3D. It does not benchmark hardware, reconstruct a real scene, or claim globally optimal alignment. ICP is local: initialization, point-cloud geometry and noise can produce incorrect correspondences and local minima. RMSE here is measured against the current nearest neighbours, not a guarantee of recovery of the true pose.
|
| 27 |
+
|
| 28 |
+
All points are generated in the browser; the demo requires no account, GPU, model downloads, or server-side inference. No data is uploaded. No libraries are needed.
|
| 29 |
+
|
| 30 |
+
## Run locally
|
| 31 |
+
|
| 32 |
+
Serve the directory with any static server; for example:
|
| 33 |
+
|
| 34 |
+
```bash
|
| 35 |
+
python3 -m http.server 8000
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
Then open `http://localhost:8000`. For an optional algorithm check, run `node test.mjs` (Node.js 18+).
|
| 39 |
+
|
| 40 |
+
## Publish as a Hugging Face Space
|
| 41 |
+
|
| 42 |
+
Create a **Static** Space and upload the contents of this folder at its root, preserving `README.md`, `index.html`, `style.css`, `app.js`, and `icp.js`. The YAML header configures the Space. No paid hardware is needed for a static Space. Inspect the finished public page before sharing.
|
| 43 |
+
|
| 44 |
+
## Further reading
|
| 45 |
+
|
| 46 |
+
- [Original project and notebook](https://github.com/devkumar-projects/Classical-Computer-Vision-Toolkit/tree/main/3d-point-cloud-registration-icp)
|
| 47 |
+
- [Related Kaggle notebook](https://www.kaggle.com/code/devkumarkaggle/3d-point-cloud-registration-with-icp)
|
| 48 |
+
- B. K. P. Horn, “Closed-form solution of absolute orientation using unit quaternions,” *Journal of the Optical Society of America A* 4(4), 629–642 (1987), DOI: [10.1364/JOSAA.4.000629](https://doi.org/10.1364/JOSAA.4.000629).
|
| 49 |
+
- P. J. Besl and N. D. McKay, “A method for registration of 3-D shapes,” *IEEE Transactions on Pattern Analysis and Machine Intelligence* 14(2), 239–256 (1992), DOI: [10.1109/34.121791](https://doi.org/10.1109/34.121791).
|
| 50 |
+
|
| 51 |
+
## License
|
| 52 |
+
|
| 53 |
+
The demo code is available under the MIT License. The referenced original project has its own [MIT license](https://github.com/devkumar-projects/Classical-Computer-Vision-Toolkit/blob/main/LICENSE).
|
app.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import {referenceCloud,transformCloud,nearestRMSE,stepICP} from './icp.js';
|
| 2 |
+
const $=id=>document.getElementById(id);
|
| 3 |
+
const canvas=$('cloud'),chart=$('chart'),ctx=canvas.getContext('2d'),chartCtx=chart.getContext('2d');
|
| 4 |
+
const sliders=['rotation','shift','noise'].map($);
|
| 5 |
+
let target=[],moving=[],history=[],running=false,timer=null,iteration=0,yaw=-.55,pitch=.58,drag=null,initial=0;
|
| 6 |
+
|
| 7 |
+
function canvasSize(el,context){
|
| 8 |
+
const rect=el.getBoundingClientRect(),ratio=window.devicePixelRatio||1;
|
| 9 |
+
const w=Math.max(1,Math.round(rect.width*ratio)),h=Math.max(1,Math.round(rect.height*ratio));
|
| 10 |
+
if(el.width!==w||el.height!==h){el.width=w;el.height=h;}
|
| 11 |
+
context.setTransform(ratio,0,0,ratio,0,0);return [rect.width,rect.height];
|
| 12 |
+
}
|
| 13 |
+
function project([x,y,z],w,h){
|
| 14 |
+
const cy=Math.cos(yaw),sy=Math.sin(yaw),cp=Math.cos(pitch),sp=Math.sin(pitch);
|
| 15 |
+
const x1=cy*x-sy*y,y1=sy*x+cy*y;
|
| 16 |
+
const y2=cp*y1-sp*z,depth=sp*y1+cp*z;
|
| 17 |
+
const size=Math.min(w,h)*.295,perspective=3.8/(3.8-depth);
|
| 18 |
+
return [w*.5+size*x1*perspective,h*.49-size*y2*perspective,depth];
|
| 19 |
+
}
|
| 20 |
+
function drawCloud(){
|
| 21 |
+
const [w,h]=canvasSize(canvas,ctx);ctx.clearRect(0,0,w,h);
|
| 22 |
+
ctx.strokeStyle='rgba(175,228,187,.075)';ctx.lineWidth=1;
|
| 23 |
+
for(let i=-4;i<=4;i++){
|
| 24 |
+
const a=project([i/4,-1,-.35],w,h),b=project([i/4,1,-.35],w,h);
|
| 25 |
+
const c=project([-1,i/4,-.35],w,h),d=project([1,i/4,-.35],w,h);
|
| 26 |
+
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();
|
| 27 |
+
}
|
| 28 |
+
const dots=[...target.map(p=>({p,color:'184,255,191'})),...moving.map(p=>({p,color:'230,164,134'}))];
|
| 29 |
+
dots.map(d=>({...d,screen:project(d.p,w,h)})).sort((a,b)=>a.screen[2]-b.screen[2]).forEach(({screen,color})=>{
|
| 30 |
+
const [x,y,z]=screen,r=Math.max(1.5,2.4+z*.7);
|
| 31 |
+
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();
|
| 32 |
+
});
|
| 33 |
+
}
|
| 34 |
+
function drawChart(){
|
| 35 |
+
const [w,h]=canvasSize(chart,chartCtx);chartCtx.clearRect(0,0,w,h);
|
| 36 |
+
const pad=5;chartCtx.strokeStyle='#304238';chartCtx.lineWidth=1;
|
| 37 |
+
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();}
|
| 38 |
+
if(history.length<2)return;
|
| 39 |
+
const max=Math.max(...history,1e-6)*1.1;
|
| 40 |
+
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);});
|
| 41 |
+
chartCtx.strokeStyle='#b8ffbf';chartCtx.lineWidth=2;chartCtx.stroke();
|
| 42 |
+
}
|
| 43 |
+
function show(){
|
| 44 |
+
$('iterations').textContent=iteration;$('rmse').textContent=history.at(-1).toFixed(4);$('initial').textContent=initial.toFixed(4);
|
| 45 |
+
drawCloud();drawChart();
|
| 46 |
+
}
|
| 47 |
+
function stop(message){
|
| 48 |
+
running=false;clearTimeout(timer);$('play').innerHTML='▶ Run ICP';
|
| 49 |
+
$('status').textContent=message;$('statusDot').style.background='#91d89a';
|
| 50 |
+
}
|
| 51 |
+
function reset(){
|
| 52 |
+
stop('Ready to align');target=referenceCloud();
|
| 53 |
+
const rotation=Number($('rotation').value),shift=Number($('shift').value),noise=Number($('noise').value);
|
| 54 |
+
$('rotationVal').textContent=`${rotation}°`;$('shiftVal').textContent=shift.toFixed(2);$('noiseVal').textContent=noise.toFixed(3);
|
| 55 |
+
moving=transformCloud(target,rotation,shift,noise);iteration=0;initial=nearestRMSE(moving,target);history=[initial];show();
|
| 56 |
+
}
|
| 57 |
+
function step(){
|
| 58 |
+
if(iteration>=80){stop('Iteration limit reached');return false;}
|
| 59 |
+
const old=history.at(-1),result=stepICP(moving,target);moving=result.points;iteration++;history.push(result.rmse);show();
|
| 60 |
+
if(Math.abs(old-result.rmse)<1e-7){stop('Converged to a local solution');return false;}
|
| 61 |
+
$('status').textContent=`Iteration ${iteration} · matching and fitting`;return true;
|
| 62 |
+
}
|
| 63 |
+
function tick(){if(!running)return;if(step()&&running)timer=setTimeout(tick,180);}
|
| 64 |
+
$('play').addEventListener('click',()=>{if(running){stop('Paused');return;}running=true;$('play').textContent='Ⅱ Pause';$('status').textContent='Matching and fitting';tick();});
|
| 65 |
+
$('step').addEventListener('click',()=>{if(running)stop('Paused');step();});
|
| 66 |
+
$('reset').addEventListener('click',reset);sliders.forEach(el=>el.addEventListener('input',reset));
|
| 67 |
+
canvas.addEventListener('pointerdown',e=>{drag={x:e.clientX,y:e.clientY};canvas.setPointerCapture(e.pointerId);});
|
| 68 |
+
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();});
|
| 69 |
+
canvas.addEventListener('pointerup',()=>drag=null);canvas.addEventListener('pointercancel',()=>drag=null);
|
| 70 |
+
window.addEventListener('resize',()=>{drawCloud();drawChart();});reset();
|
icp.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Point-to-point rigid registration on small, synthetic point clouds.
|
| 2 |
+
// The optimal rotation is found with Horn's unit-quaternion formulation.
|
| 3 |
+
const add=(a,b)=>a.map((v,i)=>v+b[i]);
|
| 4 |
+
const sub=(a,b)=>a.map((v,i)=>v-b[i]);
|
| 5 |
+
const scale=(a,s)=>a.map(v=>v*s);
|
| 6 |
+
const dot=(a,b)=>a.reduce((sum,v,i)=>sum+v*b[i],0);
|
| 7 |
+
const norm2=(a)=>dot(a,a);
|
| 8 |
+
const mean=(pts)=>scale(pts.reduce((a,p)=>add(a,p),[0,0,0]),1/pts.length);
|
| 9 |
+
|
| 10 |
+
export function rng(seed=2026){
|
| 11 |
+
let state=seed>>>0;
|
| 12 |
+
return ()=>{state=(Math.imul(1664525,state)+1013904223)>>>0;return state/4294967296;};
|
| 13 |
+
}
|
| 14 |
+
function normal(random){
|
| 15 |
+
return Math.sqrt(-2*Math.log(Math.max(random(),1e-12)))*Math.cos(2*Math.PI*random());
|
| 16 |
+
}
|
| 17 |
+
export function referenceCloud(seed=2026){
|
| 18 |
+
const random=rng(seed), pts=[];
|
| 19 |
+
for(let i=0;i<230;i++){
|
| 20 |
+
const x=2*random()-1,y=1.7*random()-.85;
|
| 21 |
+
// An asymmetric curved surface avoids an artificially symmetric toy shape.
|
| 22 |
+
const z=.23*Math.sin(2.7*x)+.18*Math.cos(3.4*y)+.19*x*y+.13*Math.exp(-((x-.45)**2+(y+.22)**2)/.08);
|
| 23 |
+
pts.push([x,y,z]);
|
| 24 |
+
}
|
| 25 |
+
return pts;
|
| 26 |
+
}
|
| 27 |
+
export function transformCloud(points,angleDeg,shift,noise=0,seed=71){
|
| 28 |
+
const r=angleDeg*Math.PI/180,c=Math.cos(r),s=Math.sin(r), random=rng(seed);
|
| 29 |
+
return points.map(([x,y,z])=>[c*x-s*y+shift,s*x+c*y-.72*shift,z+.30*shift].map(v=>v+noise*normal(random)));
|
| 30 |
+
}
|
| 31 |
+
export function nearest(source,target){
|
| 32 |
+
return source.map(p=>{
|
| 33 |
+
let best=Infinity,index=0;
|
| 34 |
+
for(let j=0;j<target.length;j++){
|
| 35 |
+
const d=norm2(sub(p,target[j])); if(d<best){best=d;index=j;}
|
| 36 |
+
}
|
| 37 |
+
return {index,d2:best};
|
| 38 |
+
});
|
| 39 |
+
}
|
| 40 |
+
export function nearestRMSE(source,target){
|
| 41 |
+
const matches=nearest(source,target);
|
| 42 |
+
return Math.sqrt(matches.reduce((sum,m)=>sum+m.d2,0)/matches.length);
|
| 43 |
+
}
|
| 44 |
+
export function pairedRMSE(source,target){
|
| 45 |
+
return Math.sqrt(source.reduce((sum,p,i)=>sum+norm2(sub(p,target[i])),0)/source.length);
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
// Jacobi diagonalization is robust for the small symmetric 4x4 Horn matrix.
|
| 49 |
+
function largestEigenvector(matrix){
|
| 50 |
+
const a=matrix.map(row=>row.slice()), v=Array.from({length:4},(_,i)=>Array.from({length:4},(_,j)=>+(i===j)));
|
| 51 |
+
for(let it=0;it<80;it++){
|
| 52 |
+
let p=0,q=1,big=0;
|
| 53 |
+
for(let i=0;i<4;i++)for(let j=i+1;j<4;j++) if(Math.abs(a[i][j])>big){big=Math.abs(a[i][j]);p=i;q=j;}
|
| 54 |
+
if(big<1e-13) break;
|
| 55 |
+
const phi=.5*Math.atan2(2*a[p][q],a[q][q]-a[p][p]),c=Math.cos(phi),s=Math.sin(phi);
|
| 56 |
+
const app=a[p][p],aqq=a[q][q],apq=a[p][q];
|
| 57 |
+
for(let k=0;k<4;k++) if(k!==p&&k!==q){
|
| 58 |
+
const akp=a[k][p],akq=a[k][q];
|
| 59 |
+
a[k][p]=a[p][k]=c*akp-s*akq;
|
| 60 |
+
a[k][q]=a[q][k]=s*akp+c*akq;
|
| 61 |
+
}
|
| 62 |
+
a[p][p]=c*c*app-2*s*c*apq+s*s*aqq;
|
| 63 |
+
a[q][q]=s*s*app+2*s*c*apq+c*c*aqq;
|
| 64 |
+
a[p][q]=a[q][p]=0;
|
| 65 |
+
for(let k=0;k<4;k++){const vp=v[k][p],vq=v[k][q];v[k][p]=c*vp-s*vq;v[k][q]=s*vp+c*vq;}
|
| 66 |
+
}
|
| 67 |
+
let index=0; for(let i=1;i<4;i++)if(a[i][i]>a[index][index])index=i;
|
| 68 |
+
let out=v.map(row=>row[index]);const length=Math.sqrt(norm2(out));return out.map(x=>x/length);
|
| 69 |
+
}
|
| 70 |
+
function rotate(q,p){
|
| 71 |
+
const [w,x,y,z]=q,[px,py,pz]=p;
|
| 72 |
+
const tx=2*(y*pz-z*py),ty=2*(z*px-x*pz),tz=2*(x*py-y*px);
|
| 73 |
+
return [px+w*tx+y*tz-z*ty,py+w*ty+z*tx-x*tz,pz+w*tz+x*ty-y*tx];
|
| 74 |
+
}
|
| 75 |
+
export function rigidFit(source,target){
|
| 76 |
+
if(source.length!==target.length||source.length<3)throw new Error('At least three paired points are required.');
|
| 77 |
+
const pc=mean(source),qc=mean(target),h=Array.from({length:3},()=>[0,0,0]);
|
| 78 |
+
for(let i=0;i<source.length;i++){
|
| 79 |
+
const p=sub(source[i],pc),q=sub(target[i],qc);
|
| 80 |
+
for(let r=0;r<3;r++)for(let c=0;c<3;c++)h[r][c]+=p[r]*q[c];
|
| 81 |
+
}
|
| 82 |
+
const [[xx,xy,xz],[yx,yy,yz],[zx,zy,zz]]=h;
|
| 83 |
+
const m=[[xx+yy+zz,yz-zy,zx-xz,xy-yx],
|
| 84 |
+
[yz-zy,xx-yy-zz,xy+yx,zx+xz],
|
| 85 |
+
[zx-xz,xy+yx,-xx+yy-zz,yz+zy],
|
| 86 |
+
[xy-yx,zx+xz,yz+zy,-xx-yy+zz]];
|
| 87 |
+
const quaternion=largestEigenvector(m),translation=sub(qc,rotate(quaternion,pc));
|
| 88 |
+
return {quaternion,translation,apply:p=>add(rotate(quaternion,p),translation)};
|
| 89 |
+
}
|
| 90 |
+
export function stepICP(source,target){
|
| 91 |
+
const matches=nearest(source,target),fit=rigidFit(source,matches.map(m=>target[m.index]));
|
| 92 |
+
const points=source.map(fit.apply);
|
| 93 |
+
return {points,rmse:nearestRMSE(points,target),paired:pairedRMSE(points,target)};
|
| 94 |
+
}
|
index.html
CHANGED
|
@@ -1,19 +1,23 @@
|
|
| 1 |
<!doctype html>
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
</
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
<!doctype html>
|
| 2 |
+
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="color-scheme" content="dark"><title>ICP Lab · Dev Kumar</title><link rel="stylesheet" href="style.css"></head>
|
| 3 |
+
<body><main class="shell">
|
| 4 |
+
<header class="top"><a class="brand" href="https://github.com/devkumar-projects/Classical-Computer-Vision-Toolkit/tree/main/3d-point-cloud-registration-icp" target="_blank" rel="noopener"><span class="brand-mark">DK</span><span>DEV KUMAR <small>ROBOTICS & AI</small></span></a><a class="source-link" href="https://github.com/devkumar-projects/Classical-Computer-Vision-Toolkit/tree/main/3d-point-cloud-registration-icp" target="_blank" rel="noopener">Source project <span aria-hidden="true">↗</span></a></header>
|
| 5 |
+
<section class="intro"><div><p class="eyebrow"><span></span> INTERACTIVE COMPUTER VISION</p><h1>Watch point clouds<br><em>find their place.</em></h1><p class="lead">Explore iterative closest point (ICP), one iteration at a time. Adjust a synthetic 3D scan and see how a rigid alignment evolves.</p></div><div class="intro-side"><span>01 / ALIGNMENT</span><p>NEAREST NEIGHBOURS<br>RIGID TRANSFORMATION<br>CONVERGENCE</p></div></section>
|
| 6 |
+
<section class="workspace" aria-label="Interactive ICP demo">
|
| 7 |
+
<div class="visual panel"><div class="panel-head"><div><span class="section-kicker">LIVE VIEW</span><h2>Rigid registration</h2></div><div class="legend"><span><i class="dot target"></i>Reference</span><span><i class="dot moving"></i>Moving scan</span></div></div>
|
| 8 |
+
<div class="canvas-wrap"><canvas id="cloud" aria-label="Rotatable three-dimensional view of the reference and moving point clouds"></canvas><div class="canvas-note">DRAG TO ROTATE VIEW <span>•</span> SYNTHETIC DATA</div></div>
|
| 9 |
+
<div class="stats"><div><span>ITERATIONS</span><strong id="iterations">0</strong></div><div><span>NEAREST RMSE</span><strong id="rmse">—</strong></div><div><span>INITIAL RMSE</span><strong id="initial">—</strong></div><div><span>POINTS / CLOUD</span><strong id="points">230</strong></div></div>
|
| 10 |
+
</div>
|
| 11 |
+
<aside class="controls panel"><div class="control-head"><span class="section-kicker">EXPERIMENT</span><h2>Set the scene.</h2><p>Move the scan, then align it to the reference.</p></div>
|
| 12 |
+
<label for="rotation"><span>Rotation <output id="rotationVal">18°</output></span><input id="rotation" type="range" min="-35" max="35" step="1" value="18"></label>
|
| 13 |
+
<label for="shift"><span>Translation <output id="shiftVal">0.24</output></span><input id="shift" type="range" min="0" max="0.55" step="0.01" value="0.24"></label>
|
| 14 |
+
<label for="noise"><span>Noise σ <output id="noiseVal">0.00</output></span><input id="noise" type="range" min="0" max="0.12" step="0.005" value="0"></label>
|
| 15 |
+
<div class="buttons"><button id="play" class="primary">▶ Run ICP</button><button id="step">Step</button><button id="reset" aria-label="Reset experiment">Reset</button></div>
|
| 16 |
+
<div class="run-status" role="status" aria-live="polite"><i id="statusDot"></i><span id="status">Ready to align</span></div>
|
| 17 |
+
<div class="chart-head"><span>CONVERGENCE</span><span>NEAREST-NEIGHBOUR RMSE</span></div><canvas id="chart" aria-label="Chart of nearest-neighbour root mean square error by iteration"></canvas>
|
| 18 |
+
<p class="footnote">Units are arbitrary. ICP is a local method: symmetry, noise and initial pose can lead to incorrect matches or local minima.</p>
|
| 19 |
+
</aside>
|
| 20 |
+
</section>
|
| 21 |
+
<section class="explain"><div><span class="section-kicker">HOW IT WORKS</span><h2>Three steps.<br><em>Repeated.</em></h2></div><div class="explain-grid"><article><b>01</b><h3>Find neighbours</h3><p>For each moving point, select its closest point in the reference cloud.</p></article><article><b>02</b><h3>Fit the pose</h3><p>Estimate the rigid rotation and translation that reduce the paired distances.</p></article><article><b>03</b><h3>Iterate</h3><p>Transform the moving scan and recompute correspondences until improvement slows.</p></article></div></section>
|
| 22 |
+
<footer><span>DEV KUMAR · OPEN ROBOTICS & COMPUTER VISION</span><div><a href="https://www.kaggle.com/code/devkumarkaggle/3d-point-cloud-registration-with-icp" target="_blank" rel="noopener">Kaggle notebook ↗</a><a href="https://github.com/devkumar-projects/Classical-Computer-Vision-Toolkit/tree/main/3d-point-cloud-registration-icp" target="_blank" rel="noopener">GitHub project ↗</a></div></footer>
|
| 23 |
+
</main><script type="module" src="app.js"></script></body></html>
|
package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"type":"module","private":true,"scripts":{"test":"node test.mjs"}}
|
style.css
CHANGED
|
@@ -1,28 +1 @@
|
|
| 1 |
-
body {
|
| 2 |
-
padding: 2rem;
|
| 3 |
-
font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
|
| 4 |
-
}
|
| 5 |
-
|
| 6 |
-
h1 {
|
| 7 |
-
font-size: 16px;
|
| 8 |
-
margin-top: 0;
|
| 9 |
-
}
|
| 10 |
-
|
| 11 |
-
p {
|
| 12 |
-
color: rgb(107, 114, 128);
|
| 13 |
-
font-size: 15px;
|
| 14 |
-
margin-bottom: 10px;
|
| 15 |
-
margin-top: 5px;
|
| 16 |
-
}
|
| 17 |
-
|
| 18 |
-
.card {
|
| 19 |
-
max-width: 620px;
|
| 20 |
-
margin: 0 auto;
|
| 21 |
-
padding: 16px;
|
| 22 |
-
border: 1px solid lightgray;
|
| 23 |
-
border-radius: 16px;
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
-
.card p:last-child {
|
| 27 |
-
margin-bottom: 0;
|
| 28 |
-
}
|
|
|
|
| 1 |
+
:root{font-family:'DM Sans',system-ui,sans-serif;color:#eaf0ed;background:#0c1113;font-synthesis:none;--mint:#b8ffbf;--muted:#8f9c98;--line:#293532}*{box-sizing:border-box}body{margin:0;background:radial-gradient(ellipse at 60% 5%,#1b2923 0,transparent 37%),#0c1113}.shell{max-width:1440px;margin:auto;padding:0 5.5%}.top{height:91px;display:flex;align-items:center;justify-content:space-between;border-bottom:1px solid var(--line)}a{color:inherit;text-decoration:none}.brand{display:flex;gap:12px;align-items:center;font:700 12px 'Space Grotesk',sans-serif;letter-spacing:.11em}.brand small{display:block;color:#8fa099;font:500 9px 'DM Sans',sans-serif;letter-spacing:.15em;margin-top:4px}.brand-mark{width:37px;height:37px;border:1px solid #8ddb9b;color:var(--mint);display:grid;place-items:center;border-radius:9px;font-size:14px;letter-spacing:0}.source-link{font-size:12px;color:#c6d4ce;border-bottom:1px solid #678775;padding-bottom:5px}.source-link span{color:var(--mint);margin-left:8px}.intro{display:flex;justify-content:space-between;align-items:end;padding:64px 0 40px;gap:32px}.eyebrow,.section-kicker{font-size:10px;font-weight:700;letter-spacing:.17em;color:#a4b6ad}.eyebrow span{display:inline-block;width:6px;height:6px;border-radius:50%;background:var(--mint);vertical-align:2px;margin-right:8px}.intro h1,.explain h2{font:500 clamp(42px,5.2vw,74px)/1.02 'Space Grotesk',sans-serif;letter-spacing:-.065em;margin:17px 0}.intro h1 em,.explain h2 em{font-style:normal;color:var(--mint)}.lead{color:#9facaa;max-width:520px;font-size:15px;line-height:1.7;margin:0}.intro-side{border-left:1px solid #405248;padding-left:20px;min-width:180px;font:10px/1.9 'Space Grotesk',sans-serif;letter-spacing:.12em;color:#82938c}.intro-side span{color:#c0f2c6}.workspace{display:grid;grid-template-columns:minmax(0,1.75fr) minmax(300px,.82fr);gap:15px}.panel{border:1px solid #293b33;border-radius:14px;background:rgba(20,29,27,.9);overflow:hidden}.visual{display:flex;flex-direction:column}.panel-head{display:flex;justify-content:space-between;gap:18px;align-items:center;padding:23px 26px 8px}.panel-head h2,.control-head h2{font:600 19px 'Space Grotesk',sans-serif;letter-spacing:-.035em;margin:7px 0}.legend{display:flex;gap:16px;color:#b6c3bc;font-size:11px}.dot{display:inline-block;width:8px;height:8px;border-radius:50%;margin-right:6px}.target{background:#b9ffc1}.moving{background:#d8a98c}.canvas-wrap{height:375px;position:relative;background:radial-gradient(ellipse at center,#24362c 0,#18231e 40%,#121b18 100%);margin:12px 16px 0;border-radius:8px;overflow:hidden}#cloud{width:100%;height:100%;display:block;cursor:grab;touch-action:none}#cloud:active{cursor:grabbing}.canvas-note{position:absolute;bottom:15px;left:16px;color:#96a89c;font-size:9px;letter-spacing:.14em;font-weight:700;pointer-events:none}.canvas-note span{padding:0 6px;color:#5f7568}.stats{display:grid;grid-template-columns:repeat(4,1fr);padding:24px 26px;gap:12px}.stats div{border-right:1px solid var(--line)}.stats div:last-child{border:0}.stats span,.chart-head{font-size:9px;letter-spacing:.11em;color:#91a39a;font-weight:700}.stats strong{display:block;font:500 22px 'Space Grotesk',sans-serif;margin-top:7px;color:#f0f5ef}.controls{padding:26px}.control-head h2{font-size:24px;margin:10px 0 6px}.control-head p{font-size:12px;color:#99aaa1;margin:0 0 29px;line-height:1.5}label{display:block;margin-bottom:24px}label span{display:flex;justify-content:space-between;font-size:12px;color:#d3e0d7}output{font-family:'Space Grotesk',sans-serif;color:var(--mint)}input[type=range]{width:100%;height:3px;appearance:none;background:#42544a;margin:15px 0 0;border-radius:5px;accent-color:var(--mint)}input[type=range]::-webkit-slider-thumb{appearance:none;width:13px;height:13px;border-radius:50%;background:var(--mint);box-shadow:0 0 0 4px #273d2e;cursor:pointer}.buttons{display:flex;gap:7px;margin-top:27px}button{font:600 11px 'DM Sans',sans-serif;color:#d6e6db;border:1px solid #415348;border-radius:7px;background:#24342b;padding:11px 13px;cursor:pointer}button:hover{border-color:#a3e4aa}button.primary{flex:1;color:#102014;background:var(--mint);border-color:var(--mint)}button:focus-visible,input:focus-visible,a:focus-visible{outline:2px solid #fff;outline-offset:3px}.run-status{display:flex;align-items:center;gap:8px;margin:17px 0 25px;font-size:11px;color:#afc3b4}.run-status i{width:7px;height:7px;border-radius:50%;background:#91d89a}.chart-head{display:flex;justify-content:space-between;font-size:8px}#chart{display:block;width:100%;height:76px;margin-top:8px}.footnote{color:#83958b;font-size:11px;line-height:1.55;margin:16px 0 0}.explain{padding:80px 0;display:grid;grid-template-columns:.8fr 2fr;gap:50px}.explain h2{font-size:38px;line-height:1.1}.explain-grid{display:grid;grid-template-columns:repeat(3,1fr);gap:30px}.explain article{border-top:1px solid #44564c;padding-top:18px}.explain article b{font:500 11px 'Space Grotesk';color:var(--mint)}.explain h3{font:600 15px 'Space Grotesk';margin:22px 0 9px}.explain article p{font-size:12px;line-height:1.7;color:#96a89d}footer{border-top:1px solid var(--line);display:flex;justify-content:space-between;gap:18px;padding:24px 0 32px;color:#8c9f94;font-size:9px;font-weight:700;letter-spacing:.1em}footer div{display:flex;gap:20px}footer a{color:#cfddd1;letter-spacing:0;font-size:11px}@media(max-width:800px){.intro-side{display:none}.workspace{grid-template-columns:1fr}.canvas-wrap{height:340px}.explain{grid-template-columns:1fr;gap:20px;padding:55px 0}.intro{padding:50px 0 30px}}@media(max-width:500px){.shell{padding:0 5%}.top{height:76px}.intro h1{font-size:43px}.panel-head{padding:20px 17px 8px}.legend{gap:9px;font-size:10px}.canvas-wrap{height:285px}.stats{grid-template-columns:repeat(2,1fr);gap:18px;padding:20px}.stats div:nth-child(2){border:0}.controls{padding:21px}.explain-grid{grid-template-columns:1fr;gap:10px}.explain article h3{margin:8px 0}.explain article p{margin:0 0 15px}footer{flex-direction:column}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test-ui.mjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import assert from 'node:assert/strict';
|
| 2 |
+
const context={setTransform(){},clearRect(){},beginPath(){},moveTo(){},lineTo(){},stroke(){},arc(){},fill(){}};
|
| 3 |
+
const elements=new Map();
|
| 4 |
+
for(const id of ['cloud','chart','rotation','shift','noise','rotationVal','shiftVal','noiseVal','iterations','rmse','initial','points','play','step','reset','status','statusDot']){
|
| 5 |
+
elements.set(id,{value:'',textContent:'',innerHTML:'',style:{},events:{},
|
| 6 |
+
addEventListener(name,fn){this.events[name]=fn;},getContext(){return context;},
|
| 7 |
+
getBoundingClientRect(){return {width:640,height:320};},setPointerCapture(){}});
|
| 8 |
+
}
|
| 9 |
+
elements.get('rotation').value='18';elements.get('shift').value='.24';elements.get('noise').value='0';
|
| 10 |
+
globalThis.document={getElementById:id=>elements.get(id)};
|
| 11 |
+
globalThis.window={devicePixelRatio:1,addEventListener(){}};
|
| 12 |
+
await import('./app.js');
|
| 13 |
+
assert.equal(elements.get('iterations').textContent,0);
|
| 14 |
+
const initial=Number(elements.get('rmse').textContent);
|
| 15 |
+
elements.get('step').events.click();
|
| 16 |
+
assert.equal(elements.get('iterations').textContent,1);
|
| 17 |
+
assert.ok(Number(elements.get('rmse').textContent)<=initial);
|
| 18 |
+
elements.get('rotation').value='5';elements.get('rotation').events.input();
|
| 19 |
+
assert.equal(elements.get('iterations').textContent,0);
|
| 20 |
+
assert.equal(elements.get('rotationVal').textContent,'5°');
|
| 21 |
+
elements.get('reset').events.click();
|
| 22 |
+
assert.equal(elements.get('status').textContent,'Ready to align');
|
| 23 |
+
console.log('Passed: UI initialization, step, slider reset and reset-button smoke checks.');
|
test.mjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import assert from 'node:assert/strict';
|
| 2 |
+
import {referenceCloud,transformCloud,rigidFit,nearestRMSE,pairedRMSE,stepICP} from './icp.js';
|
| 3 |
+
|
| 4 |
+
const reference=referenceCloud(), moved=transformCloud(reference,23,.29,0);
|
| 5 |
+
const fit=rigidFit(moved,reference), recovered=moved.map(fit.apply);
|
| 6 |
+
assert.ok(pairedRMSE(recovered,reference)<1e-9,'Rigid fit should recover an exactly paired transformation');
|
| 7 |
+
let cloud=transformCloud(reference,12,.14,0),last=nearestRMSE(cloud,reference);
|
| 8 |
+
for(let i=0;i<70;i++){
|
| 9 |
+
const result=stepICP(cloud,reference);
|
| 10 |
+
assert.ok(result.rmse<=last+1e-10,'Nearest-neighbour RMSE must not increase in ordinary ICP');
|
| 11 |
+
cloud=result.points;last=result.rmse;
|
| 12 |
+
}
|
| 13 |
+
assert.ok(pairedRMSE(cloud,reference)<1e-6,'ICP should recover this well-initialized noiseless example');
|
| 14 |
+
const noisy=transformCloud(reference,0,0,.02);
|
| 15 |
+
assert.ok(nearestRMSE(noisy,reference)>0,'Noisy observations should retain residual error');
|
| 16 |
+
console.log('Passed: rigid fit, ICP convergence, monotonic RMSE and noise checks.');
|