Point Cloud with 3D Detection Boxes
Three target boxes overlaid on 180,000 street-scene points, with colouring by height or by distance and a thickened base frame marking ground contact.
Business context
Perception and data-labelling teams use it to check how well detection boxes fit the point cloud, inspect heading and size error, and support label QA and model regression review. Data comes from LiDAR point clouds and model output, each target with centre, dimensions and yaw. A frame has over 100,000 points and dozens of boxes, a scene has hundreds of frames; this example uses 180,000 points and three targets.
Boxes must be brought into the same coordinate frame as the cloud, and which axis yaw is measured from must be stated. Eight corners are generated by bitwise flags on bit0/1/2, and twelve edges are enumerated rather than written by hand. Cloud and boxes are separate layers: box width uses pixels to stay visible at distance, the base frame gets its own thicker layer to mark ground contact, and the cloud switches between height and distance ramps.
The box is easily taken as an exact measurement of object size: it is an estimate, and dimension error is amplified at oblique viewing angles, so a visually tight fit does not imply a high 3D IoU. Colour also misleads: distance-based shading darkens distant targets, which reads as low confidence. A thickened base frame is only a visual cue; judging ground contact still requires a side view.
How it works
The 180,000 points consist of 60,000 on the ground, 80,000 on facades along y = ±1.68, and 40,000 filling the three target boxes. Each box is parameterised by centre, size and yaw: bit0, bit1 and bit2 of vertex index k decide whether x, y and z take the positive or negative side, producing the bottom face 0-1-3-2 and the top face 4-5-7-6, eight points in total. The twelve edges are enumerated by index, four bottom, four top and four uprights; the car is 1.10 × 0.60 × 0.56, the pedestrian 0.36 × 0.36 × 1.70 and the cyclist 0.80 × 0.32 × 1.24, coloured orange, green and purple.
The base frame gets its own LineLayer because getWidth on a single layer returns one value; putting the bottom edges in detect-box-base at 3 px and the remaining edges in detect-box at 1.6 px lets both layers share vertices with different indices, which makes ground contact clearer in perspective. Width uses pixels rather than metres so frames stay at 1.6 px when zoomed out and never thin into invisibility, while the point cloud uses updateTriggers keyed on colorMode to switch between the height and distance ramps, both computed at point construction so switching allocates no new arrays.
| Configuration | What it does |
|---|---|
| bit0/1/2 of vertex k | Sign of x, y and z, giving 8 vertices without hand-writing them |
| 12 edge indices | 4 bottom edges, 4 top edges, 4 uprights |
| detect-box-base 3 px | Base frame thickened separately to mark ground contact |
| widthUnits: 'pixels' | Edge width stays visible at any zoom level |
Note. Build boxes from centre, size, yaw and bitwise vertices, and enumerate edges as bottom, top and upright; a separate thickened base layer reads better than widening the whole box.
Source
<!--
示例:点云与三维感知框
要点:8 个顶点参数化生成 12 条立方框边,底框单独加粗,点云按高度或距离切换配色
依赖:deck.gl 9.0 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>点云与三维感知框</title>
<style>
html,body{height:100%;margin:0;background:#0d1117}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#e6e8ec}
.bar{position:relative;z-index:5;flex:0 0 auto;padding:8px 12px;background:#161b22;border-bottom:1px solid #252c36;
display:flex;gap:12px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
.bar b{color:#58a6ff;font-weight:600}
.bar label{display:flex;align-items:center;gap:5px;cursor:pointer}
.bar input[type=checkbox]{accent-color:#4f9ef8}
.bar input[type=range]{width:140px;accent-color:#4f9ef8}
.bar button{font:inherit;font-size:12px;padding:3px 10px;border-radius:5px;cursor:pointer;
border:1px solid #303845;background:#1d2531;color:#c8d2e0}
.bar button:hover{background:#253040}
.bar button.on{background:#2c4a7a;border-color:#4f8ad6;color:#dfe9ff}
.bar .sep{width:1px;height:18px;background:#2a313c;margin:0 4px}
#cv{flex:1 1 auto;min-height:0;width:100%;display:block}
</style>
</head>
<body>
<div class="bar">
<span>点数 <b id="n">—</b></span>
<span class="sep"></span>
<button id="box" class="on">显示框</button>
<button data-c="height" class="on">按高度着色</button>
<button data-c="dist">按距离着色</button>
</div>
<canvas id="cv"></canvas>
<script src="https://cdn.jsdelivr.net/npm/deck.gl@9.0.35/dist.min.js"></script>
<script>
(function () {
'use strict';
if (typeof deck === 'undefined') {
document.body.innerHTML =
'<div style="padding:24px;color:#ff8c74">deck.gl 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
function rng(seed) {
var a = seed >>> 0;
return function () {
a = (a + 0x6D2B79F5) >>> 0;
var t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
var BOXES = [
{ c: [-1.45, 0.50, 0.28], s: [1.10, 0.60, 0.56], yaw: 0.18, color: [246, 186, 78] },
{ c: [0.35, -0.55, 0.85], s: [0.36, 0.36, 1.70], yaw: -0.30, color: [86, 214, 168] },
{ c: [1.75, 0.42, 0.62], s: [0.80, 0.32, 1.24], yaw: 0.42, color: [176, 132, 255] }
];
var rand = rng(8842);
var N = 180000;
var pts = [];
var i;
function addPoint(x, y, z) {
var h = z / 2.2;
h = h < 0 ? 0 : (h > 1 ? 1 : h);
var dist = Math.sqrt(x * x + y * y + z * z) / 4.6;
dist = dist < 0 ? 0 : (dist > 1 ? 1 : dist);
var hc = [
Math.round(36 + (246 - 36) * h),
Math.round(66 + (212 - 66) * h),
Math.round(148 + (112 - 148) * h),
228
];
var dc = [
Math.round(236 + (74 - 236) * dist),
Math.round(92 + (150 - 92) * dist),
Math.round(66 + (222 - 66) * dist),
228
];
pts.push({ p: [x, y, z], cH: hc, cD: dc });
}
for (i = 0; i < 60000; i++) {
addPoint(-3.6 + rand() * 7.2, -1.7 + rand() * 3.4, rand() * 0.02);
}
for (i = 0; i < 80000; i++) {
var side = i % 2 === 0 ? -1 : 1;
addPoint(-3.6 + rand() * 7.2, side * (1.68 + (rand() - 0.5) * 0.05), rand() * 2.2);
}
for (i = 0; i < 40000; i++) {
var b = BOXES[i % 3];
addPoint(
b.c[0] + (rand() - 0.5) * b.s[0] * 0.9,
b.c[1] + (rand() - 0.5) * b.s[1] * 0.9,
b.c[2] + (rand() - 0.5) * b.s[2] * 0.9
);
}
document.getElementById('n').textContent = pts.length.toLocaleString();
// 顶点位序:bit0 → x,bit1 → y,bit2 → z
var EDGES = [
[0, 1], [1, 3], [3, 2], [2, 0],
[4, 5], [5, 7], [7, 6], [6, 4],
[0, 4], [1, 5], [2, 6], [3, 7]
];
function boxCorners(box) {
var c = box.c;
var s = box.s;
var cs = Math.cos(box.yaw);
var sn = Math.sin(box.yaw);
var out = [];
for (var k = 0; k < 8; k++) {
var x = ((k & 1) ? 0.5 : -0.5) * s[0];
var y = ((k & 2) ? 0.5 : -0.5) * s[1];
var z = ((k & 4) ? 0.5 : -0.5) * s[2];
out.push([c[0] + x * cs - y * sn, c[1] + x * sn + y * cs, c[2] + z]);
}
return out;
}
var boxSegs = [], baseSegs = [];
for (i = 0; i < BOXES.length; i++) {
var v = boxCorners(BOXES[i]);
var col = BOXES[i].color;
for (var e = 0; e < EDGES.length; e++) {
var seg = {
s: v[EDGES[e][0]],
t: v[EDGES[e][1]],
c: [col[0], col[1], col[2], 235]
};
boxSegs.push(seg);
if (e < 4) {
baseSegs.push({ s: seg.s, t: seg.t, c: [col[0], col[1], col[2], 255] });
}
}
}
var showBox = true, colorMode = 'height';
function buildLayers() {
var out = [
new deck.PointCloudLayer({
id: 'detect-cloud',
data: pts,
coordinateSystem: deck.COORDINATE_SYSTEM.CARTESIAN,
getPosition: function (d) { return d.p; },
getColor: function (d) { return colorMode === 'height' ? d.cH : d.cD; },
pointSize: 1.6,
sizeUnits: 'pixels',
updateTriggers: { getColor: colorMode }
})
];
if (showBox) {
out.push(new deck.LineLayer({
id: 'detect-box',
data: boxSegs,
coordinateSystem: deck.COORDINATE_SYSTEM.CARTESIAN,
getSourcePosition: function (d) { return d.s; },
getTargetPosition: function (d) { return d.t; },
getColor: function (d) { return d.c; },
getWidth: 1.6,
widthUnits: 'pixels'
}));
out.push(new deck.LineLayer({
id: 'detect-box-base',
data: baseSegs,
coordinateSystem: deck.COORDINATE_SYSTEM.CARTESIAN,
getSourcePosition: function (d) { return d.s; },
getTargetPosition: function (d) { return d.t; },
getColor: function (d) { return d.c; },
getWidth: 3,
widthUnits: 'pixels'
}));
}
return out;
}
var deckgl = new deck.Deck({
canvas: 'cv',
width: '100%',
height: '100%',
views: new deck.OrbitView({ target: [0, 0, 1.0] }),
initialViewState: { target: [0, 0, 1.0], rotationX: 31, rotationOrbit: -34, zoom: 0.26 },
controller: true,
layers: buildLayers()
});
document.getElementById('box').addEventListener('click', function (e) {
showBox = !showBox;
e.currentTarget.classList.toggle('on', showBox);
deckgl.setProps({ layers: buildLayers() });
});
var buttons = document.querySelectorAll('[data-c]');
for (i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', function (e) {
colorMode = e.currentTarget.getAttribute('data-c');
for (var k = 0; k < buttons.length; k++) {
buttons[k].classList.toggle('on', buttons[k] === e.currentTarget);
}
deckgl.setProps({ layers: buildLayers() });
});
}
window.__deck = deckgl;
})();
</script>
</body>
</html>