-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization.js
More file actions
281 lines (243 loc) · 10.5 KB
/
Copy pathvisualization.js
File metadata and controls
281 lines (243 loc) · 10.5 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* visualization.js
* Canvas-based 2D room simulation for the Karpfen Robot Demo.
*
* Exposes globals used by the main app script:
* worldModel – parsed model object, null until initWorldModel() is called
* robot – { x, y, dx, dy, radius } mutable robot state
* obstacles – mutable array of obstacle states
* initWorldModel(parsed) – initialise from a parseKmodel() result
* updateObstaclePosition(posObjectId, x, y) – update an obstacle's position
* scheduleRedraw() – request an animation-frame redraw
*/
'use strict';
// ── World model – null until kmodel is parsed and initWorldModel() is called ─
let worldModel = null;
// ── Mutable robot state (reset by initWorldModel, updated by WS callbacks) ───
const robot = { x: 0, y: 0, dx: 0, dy: 1, radius: 0.3 };
// ── Mutable obstacle list (reset by initWorldModel, updated by WS callbacks) ─
let obstacles = [];
// ── Canvas setup ─────────────────────────────────────────────────────────────
const canvas = document.getElementById('sim-canvas');
const ctx = canvas.getContext('2d');
const PAD = 28; // pixel padding inside canvas on each side
let rafPending = false;
// ── Coordinate transform ─────────────────────────────────────────────────────
// World coords (x right, y up) → canvas pixels (x right, y down).
// roomPxH is the pixel height of the room rectangle (passed from draw context).
function w2c(wx, wy, scale, roomPxH) {
return [PAD + wx * scale, PAD + roomPxH - wy * scale];
}
// ── Initialise world from parsed kmodel ──────────────────────────────────────
function initWorldModel(parsed) {
worldModel = parsed;
robot.x = parsed.robot.x;
robot.y = parsed.robot.y;
robot.dx = parsed.robot.dx;
robot.dy = parsed.robot.dy;
robot.radius = parsed.robot.radius;
// Deep-copy so caller mutations don't affect our live state
obstacles = parsed.obstacles.map(o => Object.assign({}, o));
scheduleRedraw();
}
// ── Update an obstacle position by its position-vector object ID ──────────────
function updateObstaclePosition(posObjectId, x, y) {
const obs = obstacles.find(o => o.positionObjectId === posObjectId);
if (!obs) return false;
if (!isNaN(x)) obs.x = x;
if (!isNaN(y)) obs.y = y;
return true;
}
// ── Main draw ────────────────────────────────────────────────────────────────
function draw() {
if (!canvas.width) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (!worldModel) {
drawPlaceholder();
return;
}
const roomW = worldModel.roomWidth;
const roomH = worldModel.roomHeight;
// Uniform scale so obstacles and robot don't appear stretched
const scale = Math.min(
(canvas.width - 2 * PAD) / roomW,
(canvas.height - 2 * PAD) / roomH
);
const roomPxW = roomW * scale;
const roomPxH = roomH * scale;
const [rx, ry] = w2c(0, roomH, scale, roomPxH);
// Floor fill
ctx.fillStyle = '#F8FFF0';
ctx.fillRect(rx, ry, roomPxW, roomPxH);
// Grid
ctx.strokeStyle = 'rgba(0,0,0,0.04)';
ctx.lineWidth = 1;
for (let i = 1; i < roomW; i++) {
const gx = rx + i * scale;
ctx.beginPath(); ctx.moveTo(gx, ry); ctx.lineTo(gx, ry + roomPxH); ctx.stroke();
}
for (let j = 1; j < roomH; j++) {
const gy = ry + j * scale;
ctx.beginPath(); ctx.moveTo(rx, gy); ctx.lineTo(rx + roomPxW, gy); ctx.stroke();
}
// Room border walls
ctx.strokeStyle = '#37474F';
ctx.lineWidth = 3;
ctx.lineJoin = 'round';
ctx.strokeRect(rx, ry, roomPxW, roomPxH);
// Cardinal direction labels
ctx.fillStyle = '#78909C';
ctx.font = `bold ${Math.max(9, scale * 0.35)}px Roboto, sans-serif`;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText('N', rx + roomPxW / 2, ry - 13);
ctx.fillText('S', rx + roomPxW / 2, ry + roomPxH + 13);
ctx.fillText('W', rx - 13, ry + roomPxH / 2);
ctx.fillText('E', rx + roomPxW + 13, ry + roomPxH / 2);
// Obstacles
for (const obs of obstacles) {
drawObstacle(obs, scale, roomPxH);
}
// Robot
drawRobot(scale, roomPxH);
}
// ── Placeholder rendered before any model is loaded ──────────────────────────
function drawPlaceholder() {
const w = canvas.width, h = canvas.height;
ctx.fillStyle = '#F8F9FA';
ctx.fillRect(0, 0, w, h);
ctx.save();
ctx.strokeStyle = '#B0BEC5';
ctx.lineWidth = 2;
ctx.setLineDash([8, 6]);
ctx.strokeRect(24, 24, w - 48, h - 48);
ctx.setLineDash([]);
ctx.restore();
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillStyle = '#90A4AE';
ctx.font = `500 ${Math.max(12, Math.min(16, w * 0.04))}px Roboto, sans-serif`;
ctx.fillText('No world model loaded', w / 2, h / 2 - 16);
ctx.fillStyle = '#B0BEC5';
ctx.font = `${Math.max(10, Math.min(13, w * 0.03))}px Roboto, sans-serif`;
ctx.fillText('Upload a .kmodel file in Step\u202F3 to visualise the room', w / 2, h / 2 + 14);
}
// ── Obstacle rendering ───────────────────────────────────────────────────────
function drawObstacle(obs, scale, roomPxH) {
const [cx, cy] = w2c(obs.x, obs.y, scale, roomPxH);
const r = obs.radius * scale;
// Drop shadow
ctx.save();
ctx.shadowColor = 'rgba(0,0,0,0.18)';
ctx.shadowBlur = 8;
ctx.shadowOffsetY = 3;
ctx.beginPath();
ctx.arc(cx, cy, r, 0, Math.PI * 2);
ctx.fillStyle = obs.color;
ctx.fill();
ctx.restore();
// Border
ctx.beginPath();
ctx.arc(cx, cy, r, 0, Math.PI * 2);
ctx.strokeStyle = shadeColor(obs.color, -30);
ctx.lineWidth = 1.5;
ctx.stroke();
// Label (obstacle ID)
ctx.fillStyle = '#fff';
ctx.font = `bold ${Math.max(9, r * 0.38)}px Roboto, sans-serif`;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(obs.id, cx, cy);
}
// ── Robot rendering ──────────────────────────────────────────────────────────
function drawRobot(scale, roomPxH) {
const [cx, cy] = w2c(robot.x, robot.y, scale, roomPxH);
const r = robot.radius * scale;
// Direction arrow (0.9 m long from centre)
// World: dx right, dy up → canvas: dx right, dy down (invert y)
const arrowLen = 0.9 * scale;
const toX = cx + robot.dx * arrowLen;
const toY = cy - robot.dy * arrowLen;
drawArrow(cx, cy, toX, toY, '#FFD600', Math.max(2, r * 0.4));
// Glow halo
const grad = ctx.createRadialGradient(cx, cy, 0, cx, cy, r * 1.8);
grad.addColorStop(0, 'rgba(255,109,0,0.25)');
grad.addColorStop(1, 'rgba(255,109,0,0)');
ctx.beginPath();
ctx.arc(cx, cy, r * 1.8, 0, Math.PI * 2);
ctx.fillStyle = grad;
ctx.fill();
// Body
ctx.save();
ctx.shadowColor = 'rgba(230,74,25,0.5)';
ctx.shadowBlur = 10;
ctx.beginPath();
ctx.arc(cx, cy, r, 0, Math.PI * 2);
ctx.fillStyle = '#FF6D00';
ctx.fill();
ctx.strokeStyle = '#E64A19';
ctx.lineWidth = 2;
ctx.stroke();
ctx.restore();
// Centre dot
ctx.beginPath();
ctx.arc(cx, cy, Math.max(2, r * 0.25), 0, Math.PI * 2);
ctx.fillStyle = '#fff';
ctx.fill();
}
// ── Arrow helper ─────────────────────────────────────────────────────────────
function drawArrow(fromX, fromY, toX, toY, color, lineWidth) {
const angle = Math.atan2(toY - fromY, toX - fromX);
const headLen = Math.max(8, lineWidth * 3.5);
ctx.save();
ctx.strokeStyle = color;
ctx.fillStyle = color;
ctx.lineWidth = lineWidth;
ctx.lineCap = 'round';
ctx.beginPath();
ctx.moveTo(fromX, fromY);
ctx.lineTo(toX, toY);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(toX, toY);
ctx.lineTo(
toX - headLen * Math.cos(angle - Math.PI / 6),
toY - headLen * Math.sin(angle - Math.PI / 6)
);
ctx.lineTo(
toX - headLen * Math.cos(angle + Math.PI / 6),
toY - headLen * Math.sin(angle + Math.PI / 6)
);
ctx.closePath();
ctx.fill();
ctx.restore();
}
// ── Colour utility ───────────────────────────────────────────────────────────
/** Darken (negative) or lighten (positive) a CSS hex colour by `amount`. */
function shadeColor(hex, amount) {
const num = parseInt(hex.slice(1), 16);
const r = Math.min(255, Math.max(0, (num >> 16) + amount));
const g = Math.min(255, Math.max(0, ((num >> 8) & 0xFF) + amount));
const b = Math.min(255, Math.max(0, (num & 0xFF) + amount));
return `rgb(${r},${g},${b})`;
}
// ── Redraw scheduling ────────────────────────────────────────────────────────
function scheduleRedraw() {
if (!rafPending) {
rafPending = true;
requestAnimationFrame(() => { draw(); rafPending = false; });
}
}
// ── Responsive canvas resize ─────────────────────────────────────────────────
function resizeCanvas() {
const area = document.getElementById('canvas-area');
const size = Math.min(area.clientWidth - 4, area.clientHeight - 4);
if (size > 0 && (canvas.width !== size || canvas.height !== size)) {
canvas.width = size;
canvas.height = size;
draw();
}
}
const _ro = new ResizeObserver(() => resizeCanvas());
_ro.observe(document.getElementById('canvas-area'));
window.addEventListener('load', () => { resizeCanvas(); draw(); });