@7365admin1/layer-common 4.23.0 → 4.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -4
- package/components/Robot/Fleet.vue +31 -0
- package/components/Robot/Map3D.vue +382 -0
- package/package.json +2 -2
- package/utils/building-map.ts +366 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# @iservice365/layer-common
|
|
2
2
|
|
|
3
|
+
## 4.24.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 96cc47d: The building in 3D, generated from the robot's own floor plan
|
|
8
|
+
|
|
9
|
+
Adds `RobotMap3D` and `utils/building-map.ts`. The robot's map image is a SLAM
|
|
10
|
+
occupancy grid — walls are already dark pixels — so it is fetched into a canvas,
|
|
11
|
+
thresholded, traced into wall segments and extruded. Nobody draws walls.
|
|
12
|
+
|
|
13
|
+
Reachable from the fleet list's "View layout" action, so every application that
|
|
14
|
+
shows robots gets it without a change of its own.
|
|
15
|
+
|
|
16
|
+
No 3D engine: the scene is extruded boxes on flat floors, which a parallel
|
|
17
|
+
projection and a back-to-front draw order render correctly — and this layer is
|
|
18
|
+
consumed by eleven web apps.
|
|
19
|
+
|
|
20
|
+
The robot is drawn only when its position AND its floor are both known. The
|
|
21
|
+
vendor's tracking stream carries no floor, so drawing it on whichever level
|
|
22
|
+
happens to be selected would put it on a storey nobody confirmed.
|
|
23
|
+
|
|
3
24
|
## 4.23.0
|
|
4
25
|
|
|
5
26
|
### Minor Changes
|
|
@@ -1280,11 +1301,11 @@
|
|
|
1280
1301
|
`utils/console-tier.ts` + `composables/useConsoleTier.ts` mirror the server's
|
|
1281
1302
|
own rule from two endpoints the console already calls, unprojected:
|
|
1282
1303
|
|
|
1283
|
-
|
|
1284
|
-
|
|
1304
|
+
GET /api/members/user/:user/app/admin the Seven365 staff membership
|
|
1305
|
+
GET /api/roles/id/:role that membership's role document
|
|
1285
1306
|
|
|
1286
|
-
|
|
1287
|
-
|
|
1307
|
+
owner = member.type === "admin" && role.type === "admin" && role.default === true
|
|
1308
|
+
staff = member.type === "admin" && role.type === "admin"
|
|
1288
1309
|
|
|
1289
1310
|
`role.default` is the marker because it is the only property of a platform
|
|
1290
1311
|
staff role no API caller can set - `role.controller.ts` validates create and
|
|
@@ -130,6 +130,9 @@
|
|
|
130
130
|
>
|
|
131
131
|
Return to dock
|
|
132
132
|
</AppButton>
|
|
133
|
+
<AppButton size="small" variant="text" @click="openMap(item)">
|
|
134
|
+
View layout
|
|
135
|
+
</AppButton>
|
|
133
136
|
</div>
|
|
134
137
|
</template>
|
|
135
138
|
|
|
@@ -142,6 +145,27 @@
|
|
|
142
145
|
|
|
143
146
|
<Snackbar v-model="toast" :text="toastText" :color="toastColor" />
|
|
144
147
|
|
|
148
|
+
<!--
|
|
149
|
+
The building in 3D, generated from this robot's own floor plan. Per robot
|
|
150
|
+
rather than per site because the maps come from the robot - it is the thing
|
|
151
|
+
that surveyed the building.
|
|
152
|
+
-->
|
|
153
|
+
<v-dialog v-model="mapDialog" width="960" scrollable>
|
|
154
|
+
<AppCard class="pa-0">
|
|
155
|
+
<v-toolbar density="comfortable" class="pl-4">
|
|
156
|
+
<span class="font-weight-medium text-h6">{{ mapRobot?.name }} — building layout</span>
|
|
157
|
+
<v-spacer />
|
|
158
|
+
<AppButton variant="icon" icon="mdi-close" aria-label="Close" @click="mapDialog = false" />
|
|
159
|
+
</v-toolbar>
|
|
160
|
+
<RobotMap3D
|
|
161
|
+
v-if="mapDialog && mapRobot"
|
|
162
|
+
:key="mapRobot._id"
|
|
163
|
+
:robot-id="mapRobot._id"
|
|
164
|
+
:robot-state="states[mapRobot._id] ?? null"
|
|
165
|
+
/>
|
|
166
|
+
</AppCard>
|
|
167
|
+
</v-dialog>
|
|
168
|
+
|
|
145
169
|
<v-dialog v-model="dialog" width="460" persistent>
|
|
146
170
|
<RobotForm
|
|
147
171
|
:organization="organization"
|
|
@@ -211,6 +235,13 @@ const page = ref(1);
|
|
|
211
235
|
const pages = ref(1);
|
|
212
236
|
const limit = 10;
|
|
213
237
|
const dialog = ref(false);
|
|
238
|
+
const mapDialog = ref(false);
|
|
239
|
+
const mapRobot = ref<Record<string, any> | null>(null);
|
|
240
|
+
|
|
241
|
+
function openMap(robot: Record<string, any>) {
|
|
242
|
+
mapRobot.value = robot;
|
|
243
|
+
mapDialog.value = true;
|
|
244
|
+
}
|
|
214
245
|
const busy = ref("");
|
|
215
246
|
const lastCommand = ref("");
|
|
216
247
|
|
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<AppCard class="pa-0">
|
|
3
|
+
<CardToolbar>
|
|
4
|
+
<template #left>
|
|
5
|
+
<AppSelect
|
|
6
|
+
v-model="selectedFloor"
|
|
7
|
+
:items="floorOptions"
|
|
8
|
+
item-title="title"
|
|
9
|
+
item-value="value"
|
|
10
|
+
label="Level"
|
|
11
|
+
density="compact"
|
|
12
|
+
hide-details
|
|
13
|
+
style="max-width: 180px"
|
|
14
|
+
:disabled="loading || !floorOptions.length"
|
|
15
|
+
/>
|
|
16
|
+
<span v-if="wallCount" class="text-caption text-medium-emphasis ml-4">
|
|
17
|
+
{{ wallCount }} walls found
|
|
18
|
+
</span>
|
|
19
|
+
</template>
|
|
20
|
+
<template #right>
|
|
21
|
+
<AppButton
|
|
22
|
+
variant="icon"
|
|
23
|
+
icon="mdi-rotate-left"
|
|
24
|
+
aria-label="Rotate left"
|
|
25
|
+
:disabled="loading"
|
|
26
|
+
@click="rotate(-1)"
|
|
27
|
+
/>
|
|
28
|
+
<AppButton
|
|
29
|
+
variant="icon"
|
|
30
|
+
icon="mdi-rotate-right"
|
|
31
|
+
aria-label="Rotate right"
|
|
32
|
+
:disabled="loading"
|
|
33
|
+
@click="rotate(1)"
|
|
34
|
+
/>
|
|
35
|
+
<AppButton
|
|
36
|
+
variant="icon"
|
|
37
|
+
:icon="tilted ? 'mdi-cube-outline' : 'mdi-floor-plan'"
|
|
38
|
+
:aria-label="tilted ? 'Show as a flat plan' : 'Show in 3D'"
|
|
39
|
+
:disabled="loading"
|
|
40
|
+
@click="toggleTilt"
|
|
41
|
+
/>
|
|
42
|
+
</template>
|
|
43
|
+
</CardToolbar>
|
|
44
|
+
|
|
45
|
+
<div ref="stage" class="robot-map3d">
|
|
46
|
+
<canvas ref="canvasEl" class="robot-map3d__canvas" />
|
|
47
|
+
|
|
48
|
+
<div v-if="loading" class="robot-map3d__overlay">
|
|
49
|
+
<v-progress-circular indeterminate size="28" />
|
|
50
|
+
<p class="text-body-2 mt-3 mb-0">Building the layout…</p>
|
|
51
|
+
</div>
|
|
52
|
+
|
|
53
|
+
<!--
|
|
54
|
+
A failure is named rather than left as an empty canvas. An empty canvas
|
|
55
|
+
and a failed load look identical, and they need opposite actions from
|
|
56
|
+
the person looking at the screen.
|
|
57
|
+
-->
|
|
58
|
+
<div v-else-if="error" class="robot-map3d__overlay">
|
|
59
|
+
<v-icon size="36" class="mb-2">mdi-map-marker-off-outline</v-icon>
|
|
60
|
+
<p class="text-body-2 mb-3">{{ error }}</p>
|
|
61
|
+
<AppButton size="small" variant="outlined" @click="load">Try again</AppButton>
|
|
62
|
+
</div>
|
|
63
|
+
|
|
64
|
+
<div v-else-if="!wallCount" class="robot-map3d__overlay">
|
|
65
|
+
<v-icon size="36" class="mb-2">mdi-floor-plan</v-icon>
|
|
66
|
+
<p class="text-body-2 mb-0">
|
|
67
|
+
No walls could be found in this floor plan.
|
|
68
|
+
</p>
|
|
69
|
+
</div>
|
|
70
|
+
</div>
|
|
71
|
+
</AppCard>
|
|
72
|
+
</template>
|
|
73
|
+
|
|
74
|
+
<script lang="ts" setup>
|
|
75
|
+
/**
|
|
76
|
+
* The building, in 3D, generated from the robot's own floor plan.
|
|
77
|
+
*
|
|
78
|
+
* Nobody draws walls. The robot's map image is a SLAM occupancy grid — walls
|
|
79
|
+
* are already dark pixels — so it is fetched into a canvas, thresholded, traced
|
|
80
|
+
* into wall segments and extruded. Open-RMF does the same thing from an
|
|
81
|
+
* annotated plan; this skips the annotation because the robot has surveyed the
|
|
82
|
+
* building already.
|
|
83
|
+
*
|
|
84
|
+
* ## Why the image is fetched every time
|
|
85
|
+
*
|
|
86
|
+
* The vendor signs its map URLs for about an hour. A URL kept in a record is a
|
|
87
|
+
* URL that stops working, so it is asked for at the moment of display — the
|
|
88
|
+
* same trap the Virtual Patrol camera addresses hit.
|
|
89
|
+
*
|
|
90
|
+
* ## Why there is no 3D engine
|
|
91
|
+
*
|
|
92
|
+
* The scene is extruded boxes on flat floors. A parallel projection and a
|
|
93
|
+
* back-to-front draw order render that correctly, and `layer-common` is
|
|
94
|
+
* consumed by eleven web apps — an engine is a large thing to put in all of
|
|
95
|
+
* them for a shape this simple.
|
|
96
|
+
*
|
|
97
|
+
* ponytail: painter's algorithm, fixed camera, flat shading by face angle. No
|
|
98
|
+
* lighting model, no textures, no orbit. Swap Three.js in behind `levelSolids`
|
|
99
|
+
* and `projectPoint` if those ever matter; the geometry does not change.
|
|
100
|
+
*/
|
|
101
|
+
import {
|
|
102
|
+
depthOf,
|
|
103
|
+
fitView,
|
|
104
|
+
gridFromImageData,
|
|
105
|
+
levelSolids,
|
|
106
|
+
projectPoint,
|
|
107
|
+
wallCorners,
|
|
108
|
+
wallsFromOccupancyGrid,
|
|
109
|
+
type TView,
|
|
110
|
+
type TWallSolid,
|
|
111
|
+
} from "../../utils/building-map";
|
|
112
|
+
import { robotErrorMessage } from "../../utils/robot-display";
|
|
113
|
+
|
|
114
|
+
const props = defineProps<{
|
|
115
|
+
/** The robot whose maps describe this building. */
|
|
116
|
+
robotId: string;
|
|
117
|
+
/** Drawn on the plan when its position and floor are both known. */
|
|
118
|
+
robotState?: Record<string, any> | null;
|
|
119
|
+
}>();
|
|
120
|
+
|
|
121
|
+
const { getRobotMaps, getRobotMapImages } = useRobots();
|
|
122
|
+
|
|
123
|
+
const stage = ref<HTMLElement | null>(null);
|
|
124
|
+
const canvasEl = ref<HTMLCanvasElement | null>(null);
|
|
125
|
+
|
|
126
|
+
const maps = ref<Array<Record<string, any>>>([]);
|
|
127
|
+
const selectedFloor = ref<number | null>(null);
|
|
128
|
+
const solids = ref<TWallSolid[]>([]);
|
|
129
|
+
const imageHeight = ref(0);
|
|
130
|
+
const loading = ref(true);
|
|
131
|
+
const error = ref("");
|
|
132
|
+
const rotation = ref(Math.PI / 5);
|
|
133
|
+
const tilted = ref(true);
|
|
134
|
+
|
|
135
|
+
const wallCount = computed(() => solids.value.length);
|
|
136
|
+
|
|
137
|
+
const floorOptions = computed(() =>
|
|
138
|
+
maps.value
|
|
139
|
+
.map((m) => ({ title: m.mapName?.trim() || `Floor ${m.floorNo}`, value: m.floorNo }))
|
|
140
|
+
.sort((a, b) => a.value - b.value),
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const currentMap = computed(() =>
|
|
144
|
+
maps.value.find((m) => m.floorNo === selectedFloor.value) ?? null,
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
/* ------------------------------------------------------------------ load */
|
|
148
|
+
|
|
149
|
+
async function load() {
|
|
150
|
+
loading.value = true;
|
|
151
|
+
error.value = "";
|
|
152
|
+
try {
|
|
153
|
+
if (!maps.value.length) {
|
|
154
|
+
const res = await getRobotMaps(props.robotId);
|
|
155
|
+
maps.value = res?.items ?? [];
|
|
156
|
+
if (!maps.value.length) {
|
|
157
|
+
error.value = "This robot has no floor maps yet.";
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
selectedFloor.value = floorOptions.value[0]?.value ?? null;
|
|
161
|
+
}
|
|
162
|
+
await buildLevel();
|
|
163
|
+
} catch (e) {
|
|
164
|
+
error.value = robotErrorMessage(e, "Could not load this building's floor plan.");
|
|
165
|
+
} finally {
|
|
166
|
+
loading.value = false;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Fetch this floor's plan, trace its walls, and extrude them. */
|
|
171
|
+
async function buildLevel() {
|
|
172
|
+
const map = currentMap.value;
|
|
173
|
+
solids.value = [];
|
|
174
|
+
if (!map?.backgroundFileKey) {
|
|
175
|
+
error.value = "This floor has no plan image.";
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const files = await getRobotMapImages(props.robotId, [map.backgroundFileKey]);
|
|
180
|
+
const url = files?.items?.find((f: any) => f.fileKey === map.backgroundFileKey)?.url;
|
|
181
|
+
if (!url) {
|
|
182
|
+
error.value = "The floor plan image could not be fetched.";
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const image = await loadImage(url);
|
|
187
|
+
imageHeight.value = image.height;
|
|
188
|
+
|
|
189
|
+
const work = document.createElement("canvas");
|
|
190
|
+
work.width = image.width;
|
|
191
|
+
work.height = image.height;
|
|
192
|
+
const wctx = work.getContext("2d", { willReadFrequently: true });
|
|
193
|
+
if (!wctx) {
|
|
194
|
+
error.value = "This browser could not read the floor plan image.";
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
wctx.drawImage(image, 0, 0);
|
|
198
|
+
|
|
199
|
+
const { data } = wctx.getImageData(0, 0, image.width, image.height);
|
|
200
|
+
const grid = gridFromImageData(data, image.width, image.height);
|
|
201
|
+
const { vertices, walls } = wallsFromOccupancyGrid(grid, { minRunPx: 6 });
|
|
202
|
+
|
|
203
|
+
solids.value = levelSolids(
|
|
204
|
+
{
|
|
205
|
+
name: map.mapName ?? `L${map.floorNo}`,
|
|
206
|
+
elevationM: 0,
|
|
207
|
+
scaleMPerPx: Number(map.resolution) || 0.05,
|
|
208
|
+
originXM: Number(map.originX) || 0,
|
|
209
|
+
originYM: Number(map.originY) || 0,
|
|
210
|
+
vertices,
|
|
211
|
+
walls,
|
|
212
|
+
},
|
|
213
|
+
image.height,
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
draw();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* `crossOrigin` is set because the pixels are read back, and a canvas tainted
|
|
221
|
+
* by a cross-origin image throws on `getImageData` — which would look like a
|
|
222
|
+
* broken floor plan rather than a CORS rule.
|
|
223
|
+
*/
|
|
224
|
+
function loadImage(url: string): Promise<HTMLImageElement> {
|
|
225
|
+
return new Promise((resolve, reject) => {
|
|
226
|
+
const img = new Image();
|
|
227
|
+
img.crossOrigin = "anonymous";
|
|
228
|
+
img.onload = () => resolve(img);
|
|
229
|
+
img.onerror = () => reject(new Error("The floor plan image could not be loaded."));
|
|
230
|
+
img.src = url;
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/* ------------------------------------------------------------------ draw */
|
|
235
|
+
|
|
236
|
+
function draw() {
|
|
237
|
+
const canvas = canvasEl.value;
|
|
238
|
+
const host = stage.value;
|
|
239
|
+
if (!canvas || !host) return;
|
|
240
|
+
|
|
241
|
+
const dpr = Math.min(window.devicePixelRatio || 1, 2);
|
|
242
|
+
const width = host.clientWidth;
|
|
243
|
+
const height = host.clientHeight;
|
|
244
|
+
canvas.width = width * dpr;
|
|
245
|
+
canvas.height = height * dpr;
|
|
246
|
+
canvas.style.width = `${width}px`;
|
|
247
|
+
canvas.style.height = `${height}px`;
|
|
248
|
+
|
|
249
|
+
const ctx = canvas.getContext("2d");
|
|
250
|
+
if (!ctx) return;
|
|
251
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
252
|
+
ctx.clearRect(0, 0, width, height);
|
|
253
|
+
|
|
254
|
+
const view: TView = fitView(solids.value, { width, height }, {
|
|
255
|
+
rotation: rotation.value,
|
|
256
|
+
tilt: tilted.value ? 0.95 : 0,
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
// Painter's algorithm: with no depth buffer, draw order IS the depth test.
|
|
260
|
+
const ordered = [...solids.value].sort(
|
|
261
|
+
(a, b) => depthOf(a.centre, view) - depthOf(b.centre, view),
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
for (const solid of ordered) drawBox(ctx, solid, view);
|
|
265
|
+
drawRobot(ctx, view);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** The three visible faces of a box: top, and two sides. */
|
|
269
|
+
function drawBox(ctx: CanvasRenderingContext2D, solid: TWallSolid, view: TView) {
|
|
270
|
+
const c = wallCorners(solid).map((p) => projectPoint(p.x, p.y, p.z, view));
|
|
271
|
+
// wallCorners emits the bottom four then the top four, in the same order.
|
|
272
|
+
const faces: Array<{ idx: number[]; shade: number }> = [
|
|
273
|
+
{ idx: [4, 5, 6, 7], shade: 1 }, // top, lit
|
|
274
|
+
{ idx: [0, 1, 5, 4], shade: 0.78 },
|
|
275
|
+
{ idx: [1, 2, 6, 5], shade: 0.62 },
|
|
276
|
+
{ idx: [2, 3, 7, 6], shade: 0.78 },
|
|
277
|
+
{ idx: [3, 0, 4, 7], shade: 0.62 },
|
|
278
|
+
];
|
|
279
|
+
|
|
280
|
+
for (const face of faces) {
|
|
281
|
+
ctx.beginPath();
|
|
282
|
+
face.idx.forEach((i, n) => (n === 0 ? ctx.moveTo(c[i].x, c[i].y) : ctx.lineTo(c[i].x, c[i].y)));
|
|
283
|
+
ctx.closePath();
|
|
284
|
+
const v = Math.round(150 * face.shade + 60);
|
|
285
|
+
ctx.fillStyle = `rgb(${v},${v + 4},${v + 8})`;
|
|
286
|
+
ctx.fill();
|
|
287
|
+
ctx.strokeStyle = "rgba(20,28,38,0.22)";
|
|
288
|
+
ctx.lineWidth = 0.6;
|
|
289
|
+
ctx.stroke();
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* The robot, only when its position AND its floor are both known.
|
|
295
|
+
*
|
|
296
|
+
* The vendor's tracking stream carries no floor, so drawing it on whichever
|
|
297
|
+
* level happens to be selected would put it on a storey nobody confirmed — and
|
|
298
|
+
* a robot shown in the wrong place is worse than one not shown.
|
|
299
|
+
*/
|
|
300
|
+
function drawRobot(ctx: CanvasRenderingContext2D, view: TView) {
|
|
301
|
+
const state = props.robotState;
|
|
302
|
+
const pos = state?.position;
|
|
303
|
+
if (!pos || state?.floorKnown !== true) return;
|
|
304
|
+
if (state.floorNo !== selectedFloor.value) return;
|
|
305
|
+
|
|
306
|
+
const base = projectPoint(pos.x, pos.y, 0, view);
|
|
307
|
+
const top = projectPoint(pos.x, pos.y, 0.9, view);
|
|
308
|
+
|
|
309
|
+
ctx.beginPath();
|
|
310
|
+
ctx.moveTo(base.x, base.y);
|
|
311
|
+
ctx.lineTo(top.x, top.y);
|
|
312
|
+
ctx.strokeStyle = "#FF5A1F";
|
|
313
|
+
ctx.lineWidth = 2;
|
|
314
|
+
ctx.stroke();
|
|
315
|
+
|
|
316
|
+
ctx.beginPath();
|
|
317
|
+
ctx.arc(top.x, top.y, 6, 0, Math.PI * 2);
|
|
318
|
+
ctx.fillStyle = "#FF5A1F";
|
|
319
|
+
ctx.fill();
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/* --------------------------------------------------------------- controls */
|
|
323
|
+
|
|
324
|
+
function rotate(direction: number) {
|
|
325
|
+
rotation.value += (direction * Math.PI) / 12;
|
|
326
|
+
draw();
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function toggleTilt() {
|
|
330
|
+
tilted.value = !tilted.value;
|
|
331
|
+
draw();
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
watch(selectedFloor, async () => {
|
|
335
|
+
if (!maps.value.length) return;
|
|
336
|
+
loading.value = true;
|
|
337
|
+
error.value = "";
|
|
338
|
+
try {
|
|
339
|
+
await buildLevel();
|
|
340
|
+
} catch (e) {
|
|
341
|
+
error.value = robotErrorMessage(e, "Could not load this floor.");
|
|
342
|
+
} finally {
|
|
343
|
+
loading.value = false;
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
watch(() => props.robotState, draw, { deep: true });
|
|
348
|
+
|
|
349
|
+
let onResize: (() => void) | undefined;
|
|
350
|
+
onMounted(() => {
|
|
351
|
+
load();
|
|
352
|
+
onResize = () => draw();
|
|
353
|
+
window.addEventListener("resize", onResize);
|
|
354
|
+
});
|
|
355
|
+
onBeforeUnmount(() => {
|
|
356
|
+
if (onResize) window.removeEventListener("resize", onResize);
|
|
357
|
+
});
|
|
358
|
+
</script>
|
|
359
|
+
|
|
360
|
+
<style scoped>
|
|
361
|
+
.robot-map3d {
|
|
362
|
+
position: relative;
|
|
363
|
+
width: 100%;
|
|
364
|
+
height: 460px;
|
|
365
|
+
background: linear-gradient(180deg, rgba(0, 0, 0, 0.02), rgba(0, 0, 0, 0.06));
|
|
366
|
+
}
|
|
367
|
+
.robot-map3d__canvas {
|
|
368
|
+
display: block;
|
|
369
|
+
width: 100%;
|
|
370
|
+
height: 100%;
|
|
371
|
+
}
|
|
372
|
+
.robot-map3d__overlay {
|
|
373
|
+
position: absolute;
|
|
374
|
+
inset: 0;
|
|
375
|
+
display: flex;
|
|
376
|
+
flex-direction: column;
|
|
377
|
+
align-items: center;
|
|
378
|
+
justify-content: center;
|
|
379
|
+
text-align: center;
|
|
380
|
+
padding: 24px;
|
|
381
|
+
}
|
|
382
|
+
</style>
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@7365admin1/layer-common",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "4.
|
|
5
|
+
"version": "4.24.0",
|
|
6
6
|
"author": "7365admin1",
|
|
7
7
|
"main": "./nuxt.config.ts",
|
|
8
8
|
"//files": "What a consumer extending this layer actually loads. Without this npm ships the whole working tree - the changesets, the CI workflows, the render harness in tools/ and any scratch directory that happened to exist at publish time. Nuxt resolves a layer by directory, so every runtime directory below has to stay listed; adding a new top-level runtime directory means adding it here too.",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"build": "nuxt build .playground",
|
|
37
37
|
"generate": "nuxt generate .playground",
|
|
38
38
|
"preview": "nuxt preview .playground",
|
|
39
|
-
"test": "esbuild composables/useVisitorSocket.ts --format=esm --outfile=test/.build/useVisitorSocket.mjs --log-level=error && esbuild composables/usePlateToastWindow.ts --format=esm --outfile=test/.build/usePlateToastWindow.mjs --log-level=error && esbuild utils/client-invite-modules.ts --bundle --platform=node --format=esm --outfile=test/.build/client-invite-modules.mjs --log-level=error && esbuild utils/robot-display.ts --bundle --platform=node --format=esm --outfile=test/.build/robot-display.mjs --log-level=error && node --experimental-strip-types --test \"test/*.test.mjs\" && yarn test:units",
|
|
39
|
+
"test": "esbuild composables/useVisitorSocket.ts --format=esm --outfile=test/.build/useVisitorSocket.mjs --log-level=error && esbuild composables/usePlateToastWindow.ts --format=esm --outfile=test/.build/usePlateToastWindow.mjs --log-level=error && esbuild utils/client-invite-modules.ts --bundle --platform=node --format=esm --outfile=test/.build/client-invite-modules.mjs --log-level=error && esbuild utils/robot-display.ts --bundle --platform=node --format=esm --outfile=test/.build/robot-display.mjs --log-level=error && esbuild utils/building-map.ts --bundle --platform=node --format=esm --outfile=test/.build/building-map.mjs --log-level=error && node --experimental-strip-types --test \"test/*.test.mjs\" && yarn test:units",
|
|
40
40
|
"test:units": "node --experimental-strip-types --test \"utils/*.test.ts\"",
|
|
41
41
|
"release": "yarn run build && changeset publish"
|
|
42
42
|
},
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Floor plan to 3D layout, in the browser.
|
|
3
|
+
*
|
|
4
|
+
* This is a deliberate copy of the same logic in `@7365admin1/core`, for the
|
|
5
|
+
* same reason `robot-display.ts` is: core is a backend package and a browser
|
|
6
|
+
* cannot import it. The wall extraction has to run where the floor plan image
|
|
7
|
+
* is — the vendor signs its map URLs for about an hour, so the picture is
|
|
8
|
+
* fetched into a canvas at the moment of display and never stored, and the
|
|
9
|
+
* pixels only exist client-side.
|
|
10
|
+
*
|
|
11
|
+
* The two copies must agree, and the agreement is held by the same tests on
|
|
12
|
+
* both sides.
|
|
13
|
+
*
|
|
14
|
+
* ## Why there is no 3D library here
|
|
15
|
+
*
|
|
16
|
+
* The scene is extruded boxes standing on flat floors. A projection and a
|
|
17
|
+
* back-to-front draw order render that correctly in a few hundred lines, and
|
|
18
|
+
* `layer-common` is consumed by eleven web apps — a 3D engine is a large thing
|
|
19
|
+
* to put in all of them for a shape this simple.
|
|
20
|
+
*
|
|
21
|
+
* ponytail: painter's algorithm with a fixed camera. No lighting model, no
|
|
22
|
+
* textures, no orbit. If any of those matter later, swap Three.js in behind
|
|
23
|
+
* `levelSolids` / `projectPoint` — the geometry above them does not change.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/** `traffic_editor`'s defaults, matched so our layout and RMF's agree. */
|
|
27
|
+
export const WALL_HEIGHT_M = 2.5;
|
|
28
|
+
export const WALL_THICKNESS_M = 0.1;
|
|
29
|
+
export const DEFAULT_SCALE_M_PER_PX = 0.05;
|
|
30
|
+
|
|
31
|
+
export type TMapVertex = { x: number; y: number; name?: string };
|
|
32
|
+
export type TMapWall = { start: number; end: number; heightM?: number; thicknessM?: number };
|
|
33
|
+
|
|
34
|
+
export type TPlanLevel = {
|
|
35
|
+
name: string;
|
|
36
|
+
elevationM: number;
|
|
37
|
+
scaleMPerPx: number;
|
|
38
|
+
originXM: number;
|
|
39
|
+
originYM: number;
|
|
40
|
+
vertices: TMapVertex[];
|
|
41
|
+
walls: TMapWall[];
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type TWallSolid = {
|
|
45
|
+
centre: { x: number; y: number; z: number };
|
|
46
|
+
lengthM: number;
|
|
47
|
+
thicknessM: number;
|
|
48
|
+
heightM: number;
|
|
49
|
+
yaw: number;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/* ------------------------------------------------- image to occupancy grid */
|
|
53
|
+
|
|
54
|
+
export type TOccupancyGrid = { data: Uint8Array; width: number; height: number };
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Turn canvas RGBA into one luminance value per pixel.
|
|
58
|
+
*
|
|
59
|
+
* A fully transparent pixel is treated as FREE space, not dark. Map PNGs are
|
|
60
|
+
* commonly transparent outside the surveyed area, and reading alpha 0 as black
|
|
61
|
+
* would wrap the whole plan in a wall.
|
|
62
|
+
*/
|
|
63
|
+
export function gridFromImageData(
|
|
64
|
+
rgba: ArrayLike<number>,
|
|
65
|
+
width: number,
|
|
66
|
+
height: number,
|
|
67
|
+
): TOccupancyGrid {
|
|
68
|
+
const data = new Uint8Array(width * height);
|
|
69
|
+
for (let i = 0, p = 0; p < width * height; i += 4, p++) {
|
|
70
|
+
const alpha = Number(rgba[i + 3]);
|
|
71
|
+
if (alpha === 0) {
|
|
72
|
+
data[p] = 255;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
// Rec. 601 luma — close enough for "is this pixel dark", and cheap.
|
|
76
|
+
data[p] =
|
|
77
|
+
(0.299 * Number(rgba[i]) + 0.587 * Number(rgba[i + 1]) + 0.114 * Number(rgba[i + 2])) | 0;
|
|
78
|
+
}
|
|
79
|
+
return { data, width, height };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Trace walls out of a floor plan, so nobody has to draw them.
|
|
84
|
+
*
|
|
85
|
+
* Horizontal runs first, then vertical runs from what those did not cover, so a
|
|
86
|
+
* long wall is one segment rather than one per pixel. Corners are de-duplicated
|
|
87
|
+
* so the result stays editable afterwards.
|
|
88
|
+
*/
|
|
89
|
+
export function wallsFromOccupancyGrid(
|
|
90
|
+
grid: TOccupancyGrid,
|
|
91
|
+
options?: { threshold?: number; minRunPx?: number },
|
|
92
|
+
): { vertices: TMapVertex[]; walls: TMapWall[] } {
|
|
93
|
+
const threshold = options?.threshold ?? 100;
|
|
94
|
+
const minRun = Math.max(2, options?.minRunPx ?? 6);
|
|
95
|
+
const { data, width, height } = grid;
|
|
96
|
+
|
|
97
|
+
if (!width || !height || !data || data.length < width * height) {
|
|
98
|
+
return { vertices: [], walls: [] };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const occupied = (x: number, y: number) => Number(data[y * width + x]) <= threshold;
|
|
102
|
+
const consumed = new Uint8Array(width * height);
|
|
103
|
+
const vertices: TMapVertex[] = [];
|
|
104
|
+
const walls: TMapWall[] = [];
|
|
105
|
+
const index = new Map<string, number>();
|
|
106
|
+
|
|
107
|
+
const vertexAt = (x: number, y: number) => {
|
|
108
|
+
const key = `${x}:${y}`;
|
|
109
|
+
const found = index.get(key);
|
|
110
|
+
if (found !== undefined) return found;
|
|
111
|
+
vertices.push({ x, y });
|
|
112
|
+
index.set(key, vertices.length - 1);
|
|
113
|
+
return vertices.length - 1;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
for (let y = 0; y < height; y++) {
|
|
117
|
+
let run = 0;
|
|
118
|
+
for (let x = 0; x <= width; x++) {
|
|
119
|
+
if (x < width && occupied(x, y)) {
|
|
120
|
+
run++;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (run >= minRun) {
|
|
124
|
+
const startX = x - run;
|
|
125
|
+
walls.push({ start: vertexAt(startX, y), end: vertexAt(x - 1, y) });
|
|
126
|
+
for (let i = startX; i < x; i++) consumed[y * width + i] = 1;
|
|
127
|
+
}
|
|
128
|
+
run = 0;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
for (let x = 0; x < width; x++) {
|
|
133
|
+
let run = 0;
|
|
134
|
+
for (let y = 0; y <= height; y++) {
|
|
135
|
+
if (y < height && occupied(x, y) && !consumed[y * width + x]) {
|
|
136
|
+
run++;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (run >= minRun) {
|
|
140
|
+
walls.push({ start: vertexAt(x, y - run), end: vertexAt(x, y - 1) });
|
|
141
|
+
}
|
|
142
|
+
run = 0;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return { vertices, walls };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/* ------------------------------------------------------------- geometry */
|
|
150
|
+
|
|
151
|
+
export function levelScale(level: TPlanLevel): number {
|
|
152
|
+
return level.scaleMPerPx > 0 ? level.scaleMPerPx : DEFAULT_SCALE_M_PER_PX;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* A drawing pixel in site metres.
|
|
157
|
+
*
|
|
158
|
+
* The vertical flip is the occupancy-grid convention: image origin top-left,
|
|
159
|
+
* map origin bottom-left. Without it the building renders mirrored, which reads
|
|
160
|
+
* as "the robot is in the wrong room" rather than as a transform bug.
|
|
161
|
+
*/
|
|
162
|
+
export function levelPixelToMetres(
|
|
163
|
+
level: TPlanLevel,
|
|
164
|
+
px: number,
|
|
165
|
+
py: number,
|
|
166
|
+
imageHeightPx: number,
|
|
167
|
+
): { x: number; y: number } {
|
|
168
|
+
const scale = levelScale(level);
|
|
169
|
+
return {
|
|
170
|
+
x: level.originXM + px * scale,
|
|
171
|
+
y: level.originYM + (imageHeightPx - py) * scale,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Extrude a level's walls into boxes a renderer can place. */
|
|
176
|
+
export function levelSolids(level: TPlanLevel, imageHeightPx: number): TWallSolid[] {
|
|
177
|
+
const solids: TWallSolid[] = [];
|
|
178
|
+
for (const wall of level.walls ?? []) {
|
|
179
|
+
const a = level.vertices[wall.start];
|
|
180
|
+
const b = level.vertices[wall.end];
|
|
181
|
+
if (!a || !b) continue;
|
|
182
|
+
|
|
183
|
+
const pa = levelPixelToMetres(level, a.x, a.y, imageHeightPx);
|
|
184
|
+
const pb = levelPixelToMetres(level, b.x, b.y, imageHeightPx);
|
|
185
|
+
const lengthM = Math.hypot(pb.x - pa.x, pb.y - pa.y);
|
|
186
|
+
// A zero-length wall renders as a flickering sliver that looks like a
|
|
187
|
+
// graphics fault rather than a map mistake.
|
|
188
|
+
if (lengthM <= 0) continue;
|
|
189
|
+
|
|
190
|
+
const heightM = wall.heightM ?? WALL_HEIGHT_M;
|
|
191
|
+
solids.push({
|
|
192
|
+
centre: {
|
|
193
|
+
x: (pa.x + pb.x) / 2,
|
|
194
|
+
y: (pa.y + pb.y) / 2,
|
|
195
|
+
// Boxes are centred, so a wall standing ON the floor sits at half its
|
|
196
|
+
// own height above the level's elevation.
|
|
197
|
+
z: level.elevationM + heightM / 2,
|
|
198
|
+
},
|
|
199
|
+
lengthM,
|
|
200
|
+
thicknessM: wall.thicknessM ?? WALL_THICKNESS_M,
|
|
201
|
+
heightM,
|
|
202
|
+
yaw: Math.atan2(pb.y - pa.y, pb.x - pa.x),
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
return solids;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/* ----------------------------------------------------------- projection */
|
|
209
|
+
|
|
210
|
+
export type TView = {
|
|
211
|
+
/** Radians. How far the scene is turned about the vertical axis. */
|
|
212
|
+
rotation: number;
|
|
213
|
+
/** Radians. 0 is a plan view looking straight down; ~0.9 reads as 3D. */
|
|
214
|
+
tilt: number;
|
|
215
|
+
/** Screen pixels per metre. */
|
|
216
|
+
zoom: number;
|
|
217
|
+
/** Where the site origin lands on the canvas. */
|
|
218
|
+
originPx: { x: number; y: number };
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* A point in site metres, on the canvas.
|
|
223
|
+
*
|
|
224
|
+
* An axonometric projection: rotate about the vertical axis, then foreshorten
|
|
225
|
+
* the depth axis by the tilt. Parallel, so there is no perspective distortion
|
|
226
|
+
* and a wall is the same size wherever it sits — which is what you want for a
|
|
227
|
+
* floor plan, and is why a full camera is not needed.
|
|
228
|
+
*
|
|
229
|
+
* Screen y grows downward, so height is SUBTRACTED: a taller wall draws further
|
|
230
|
+
* up the canvas.
|
|
231
|
+
*/
|
|
232
|
+
export function projectPoint(
|
|
233
|
+
x: number,
|
|
234
|
+
y: number,
|
|
235
|
+
z: number,
|
|
236
|
+
view: TView,
|
|
237
|
+
): { x: number; y: number } {
|
|
238
|
+
const cos = Math.cos(view.rotation);
|
|
239
|
+
const sin = Math.sin(view.rotation);
|
|
240
|
+
|
|
241
|
+
const rx = x * cos - y * sin;
|
|
242
|
+
const ry = x * sin + y * cos;
|
|
243
|
+
|
|
244
|
+
return {
|
|
245
|
+
x: view.originPx.x + rx * view.zoom,
|
|
246
|
+
y: view.originPx.y + ry * view.zoom * Math.cos(view.tilt) - z * view.zoom * Math.sin(view.tilt),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Back-to-front order, so nearer walls are drawn over farther ones.
|
|
252
|
+
*
|
|
253
|
+
* Painter's algorithm: with no depth buffer, draw order IS the depth test. Sort
|
|
254
|
+
* by the rotated depth axis, lowest first — get this backwards and the building
|
|
255
|
+
* renders inside out, which looks like a rendering bug and is really a sort.
|
|
256
|
+
*/
|
|
257
|
+
export function depthOf(point: { x: number; y: number }, view: TView): number {
|
|
258
|
+
return point.x * Math.sin(view.rotation) + point.y * Math.cos(view.rotation);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** The eight corners of a wall box, in site metres. */
|
|
262
|
+
export function wallCorners(solid: TWallSolid): Array<{ x: number; y: number; z: number }> {
|
|
263
|
+
const hl = solid.lengthM / 2;
|
|
264
|
+
const ht = solid.thicknessM / 2;
|
|
265
|
+
const hh = solid.heightM / 2;
|
|
266
|
+
const cos = Math.cos(solid.yaw);
|
|
267
|
+
const sin = Math.sin(solid.yaw);
|
|
268
|
+
|
|
269
|
+
const corners: Array<{ x: number; y: number; z: number }> = [];
|
|
270
|
+
for (const dz of [-hh, hh]) {
|
|
271
|
+
for (const [dl, dt] of [
|
|
272
|
+
[-hl, -ht],
|
|
273
|
+
[hl, -ht],
|
|
274
|
+
[hl, ht],
|
|
275
|
+
[-hl, ht],
|
|
276
|
+
] as const) {
|
|
277
|
+
corners.push({
|
|
278
|
+
x: solid.centre.x + dl * cos - dt * sin,
|
|
279
|
+
y: solid.centre.y + dl * sin + dt * cos,
|
|
280
|
+
z: solid.centre.z + dz,
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return corners;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* How big the site is, so a view can be framed without anyone choosing numbers.
|
|
289
|
+
*
|
|
290
|
+
* Returns null for an empty scene rather than a zero-size box: a caller must be
|
|
291
|
+
* able to tell "nothing to show" from "everything is at the origin".
|
|
292
|
+
*/
|
|
293
|
+
export function solidsBounds(solids: TWallSolid[]): {
|
|
294
|
+
minX: number; maxX: number; minY: number; maxY: number;
|
|
295
|
+
} | null {
|
|
296
|
+
if (!solids.length) return null;
|
|
297
|
+
|
|
298
|
+
let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
|
|
299
|
+
for (const s of solids) {
|
|
300
|
+
for (const c of wallCorners(s)) {
|
|
301
|
+
if (c.x < minX) minX = c.x;
|
|
302
|
+
if (c.x > maxX) maxX = c.x;
|
|
303
|
+
if (c.y < minY) minY = c.y;
|
|
304
|
+
if (c.y > maxY) maxY = c.y;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return { minX, maxX, minY, maxY };
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* A view that fits the whole site on the canvas.
|
|
312
|
+
*
|
|
313
|
+
* Falls back to a sane zoom for a degenerate scene — a single wall has zero
|
|
314
|
+
* extent on one axis, and dividing by it would give an infinite zoom and a
|
|
315
|
+
* blank canvas.
|
|
316
|
+
*/
|
|
317
|
+
export function fitView(
|
|
318
|
+
solids: TWallSolid[],
|
|
319
|
+
canvas: { width: number; height: number },
|
|
320
|
+
options?: { rotation?: number; tilt?: number; paddingPx?: number },
|
|
321
|
+
): TView {
|
|
322
|
+
const rotation = options?.rotation ?? Math.PI / 5;
|
|
323
|
+
const tilt = options?.tilt ?? 0.95;
|
|
324
|
+
const padding = options?.paddingPx ?? 32;
|
|
325
|
+
|
|
326
|
+
const base: TView = {
|
|
327
|
+
rotation,
|
|
328
|
+
tilt,
|
|
329
|
+
zoom: 20,
|
|
330
|
+
originPx: { x: canvas.width / 2, y: canvas.height / 2 },
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
const bounds = solidsBounds(solids);
|
|
334
|
+
if (!bounds) return base;
|
|
335
|
+
|
|
336
|
+
const projected = [
|
|
337
|
+
{ x: bounds.minX, y: bounds.minY },
|
|
338
|
+
{ x: bounds.maxX, y: bounds.minY },
|
|
339
|
+
{ x: bounds.maxX, y: bounds.maxY },
|
|
340
|
+
{ x: bounds.minX, y: bounds.maxY },
|
|
341
|
+
].map((p) => projectPoint(p.x, p.y, 0, { ...base, zoom: 1, originPx: { x: 0, y: 0 } }));
|
|
342
|
+
|
|
343
|
+
const spanX = Math.max(...projected.map((p) => p.x)) - Math.min(...projected.map((p) => p.x));
|
|
344
|
+
const spanY = Math.max(...projected.map((p) => p.y)) - Math.min(...projected.map((p) => p.y));
|
|
345
|
+
|
|
346
|
+
const usableW = Math.max(1, canvas.width - padding * 2);
|
|
347
|
+
const usableH = Math.max(1, canvas.height - padding * 2);
|
|
348
|
+
const zoom =
|
|
349
|
+
spanX > 0 && spanY > 0
|
|
350
|
+
? Math.min(usableW / spanX, usableH / spanY)
|
|
351
|
+
: base.zoom;
|
|
352
|
+
|
|
353
|
+
const centre = projectPoint(
|
|
354
|
+
(bounds.minX + bounds.maxX) / 2,
|
|
355
|
+
(bounds.minY + bounds.maxY) / 2,
|
|
356
|
+
0,
|
|
357
|
+
{ ...base, zoom, originPx: { x: 0, y: 0 } },
|
|
358
|
+
);
|
|
359
|
+
|
|
360
|
+
return {
|
|
361
|
+
rotation,
|
|
362
|
+
tilt,
|
|
363
|
+
zoom,
|
|
364
|
+
originPx: { x: canvas.width / 2 - centre.x, y: canvas.height / 2 - centre.y },
|
|
365
|
+
};
|
|
366
|
+
}
|