@mark1russell7/room-view 0.0.0-stage → 0.1.1

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.
@@ -0,0 +1,757 @@
1
+ import { a as unitsPer, i as metersPer } from "./units-BZmvVyu4.js";
2
+ // src/room.ts
3
+ /** The name of the format of a room record. */
4
+ const ROOM_SCHEMA = "room/1";
5
+ /** The name of the format of a scene record. */
6
+ const SCENE_SCHEMA = "scene/1";
7
+ /** These are the sides of the line of a wall that can have the wall. */
8
+ const WALL_SIDES = [
9
+ "center",
10
+ "left",
11
+ "right"
12
+ ];
13
+ /** These are the faces of a wall, for a person who looks from `from` to `to`. */
14
+ const WALL_FACES = ["left", "right"];
15
+ /** These are the kinds of an opening. A window has glass. A door and a gap are empty. */
16
+ const OPENING_KINDS = [
17
+ "door",
18
+ "window",
19
+ "gap"
20
+ ];
21
+ /** This function gives the unit vector of a direction in degrees. A direction along an axis gives an exact vector. */
22
+ function directionOf(degrees) {
23
+ const turn = (degrees % 360 + 360) % 360;
24
+ if (turn === 0) return [1, 0];
25
+ if (turn === 90) return [0, 1];
26
+ if (turn === 180) return [-1, 0];
27
+ if (turn === 270) return [0, -1];
28
+ const angle = turn * Math.PI / 180;
29
+ return [Math.cos(angle), Math.sin(angle)];
30
+ }
31
+ /** This function turns a vector of the plan by an angle in degrees, counter-clockwise. A turn by 90, 180 or 270 degrees is exact. */
32
+ function turned([x, y], degrees) {
33
+ const [cos, sin] = directionOf(degrees);
34
+ return [x * cos - y * sin, x * sin + y * cos];
35
+ }
36
+ /**
37
+ * This function gives the direction of a vector in degrees, from 0 to 360, counter-clockwise from the x axis.
38
+ * A direction near 0, 90, 180 or 270 degrees becomes that value, so a room with square corners keeps exact numbers.
39
+ */
40
+ function headingOf([x, y]) {
41
+ return squared(Math.atan2(y, x) * 180 / Math.PI);
42
+ }
43
+ /** This function gives an angle in degrees from 0 to 360. An angle near 0, 90, 180 or 270 degrees becomes that value. */
44
+ function squared(degrees) {
45
+ const turn = (degrees % 360 + 360) % 360;
46
+ const near = Math.round(turn / 90) * 90;
47
+ return Math.abs(turn - near) < 1e-9 ? near % 360 : turn;
48
+ }
49
+ // src/survey.ts
50
+ /** The name of the format of a survey record. */
51
+ const SURVEY_SCHEMA = "survey/1";
52
+ /** These are the names of the turns at a square corner. */
53
+ const SQUARE_TURNS = ["left", "right"];
54
+ /** The default limit of the gap of a closed walk, as a part of the length of the walk. */
55
+ const CLOSURE_LIMIT = .01;
56
+ /** This function tells if a turn is a measured angle: an angle from -360 degrees to 360 degrees. */
57
+ function isMeasured(turn) {
58
+ return typeof turn === "number" && Math.abs(turn) <= 360;
59
+ }
60
+ /** This function gives a turn in degrees. A turn that is not a square turn or a measured angle is 0. */
61
+ function degreesOf(turn) {
62
+ if (turn === "left") return 90;
63
+ if (turn === "right") return -90;
64
+ return isMeasured(turn) ? turn : 0;
65
+ }
66
+ /**
67
+ * This function gives a survey in a different unit. Each length of the record changes, and each angle stays.
68
+ * A survey that has the unit already is the result.
69
+ */
70
+ function convertSurvey(survey, unit) {
71
+ if (survey.unit === unit) return survey;
72
+ const factor = unitsPer(survey.unit, unit);
73
+ const to = (value) => value * factor;
74
+ const start = survey.start === void 0 ? void 0 : [to(survey.start[0]), to(survey.start[1])];
75
+ return {
76
+ ...survey,
77
+ unit,
78
+ height: to(survey.height),
79
+ wallThickness: to(survey.wallThickness),
80
+ sides: survey.sides.map((side) => ({
81
+ ...side,
82
+ length: to(side.length),
83
+ ...side.thickness === void 0 ? {} : { thickness: to(side.thickness) },
84
+ ...side.height === void 0 ? {} : { height: to(side.height) }
85
+ })),
86
+ ...start === void 0 ? {} : { start },
87
+ ...survey.checks === void 0 ? {} : { checks: survey.checks.map((check) => ({
88
+ ...check,
89
+ length: to(check.length)
90
+ })) },
91
+ ...survey.openings === void 0 ? {} : { openings: survey.openings.map((opening) => ({
92
+ ...opening,
93
+ offset: to(opening.offset),
94
+ width: to(opening.width),
95
+ ...opening.sill === void 0 ? {} : { sill: to(opening.sill) },
96
+ height: to(opening.height)
97
+ })) },
98
+ ...survey.outlets === void 0 ? {} : { outlets: survey.outlets.map((outlet) => ({
99
+ ...outlet,
100
+ offset: to(outlet.offset),
101
+ height: to(outlet.height)
102
+ })) },
103
+ ...survey.objects === void 0 ? {} : { objects: survey.objects.map((object) => ({
104
+ ...object,
105
+ offset: to(object.offset),
106
+ width: to(object.width),
107
+ depth: to(object.depth),
108
+ height: to(object.height),
109
+ ...object.gap === void 0 ? {} : { gap: to(object.gap) },
110
+ ...object.lift === void 0 ? {} : { lift: to(object.lift) }
111
+ })) }
112
+ };
113
+ }
114
+ /**
115
+ * This function makes the room record of a survey.
116
+ *
117
+ * The walk starts at `start` in the direction `heading`. Each side adds its length, and then the person turns.
118
+ * A length that is not a finite number above 0, or is not in the range of the model, counts as 0.
119
+ * A turn that is not a number from -360 to 360 counts as 0. Thus each number of the result is finite.
120
+ *
121
+ * A measured length has an error, so the walk does not end exactly at its start. The function corrects the two errors of a walk:
122
+ *
123
+ * 1. When the sum of the turns has an error below 45 degrees, each measured turn gets an equal part of the error.
124
+ * The turns `left` and `right` are exact, so they do not change. A larger error is a mistake, for example a wrong turn.
125
+ * 2. When the walk is closed, the sides share the gap. Along each axis of the first side, each side changes in proportion to its length along that axis.
126
+ * This is the transit rule of land survey. A side along an axis stays along that axis, so a room with square corners keeps them.
127
+ * Then the last wall ends exactly at the first corner, and the room gets a floor.
128
+ *
129
+ * An opening and an outlet keep their place on a side that changes its length: each `offset` and each `width` changes in the same proportion.
130
+ * Thus an opening that fits in its measured side fits in its wall, and two openings that do not overlap stay apart.
131
+ *
132
+ * An object keeps its size on a side that changes its length. It moves by a part of that change: its place in the free length of the side.
133
+ * Thus an object that touches a corner continues to touch it. The x axis of a placed object goes along its wall,
134
+ * and its y axis points into the room. An object with a holder has the holder as its parent, and its place is relative to the holder.
135
+ *
136
+ * Some objects have no place, and the result does not have them. A side that is not in the survey or has no length gives no place.
137
+ * A holder with no place gives no place. A size that is not a finite number above 0 gives no place.
138
+ * A distance that is not in the range of the model gives no place.
139
+ *
140
+ * A walk that is not closed gets no correction of the gap: its walls follow the walk, and the room has no floor.
141
+ * Thus the record of a survey in progress shows the walls that the person measured.
142
+ *
143
+ * The wall of each side is on the outer side of its line. Thus each `offset` of the survey is a distance along the inner face.
144
+ * `checkRoom` examines the room record.
145
+ */
146
+ function expandSurvey(survey, options = {}) {
147
+ const scale = metersPer(survey.unit);
148
+ const inRange = (value) => Number.isFinite(value) && Math.abs(value) * scale <= 1e6;
149
+ const sides = survey.sides;
150
+ const count = sides.length;
151
+ const lengths = sides.map((side) => side.length > 0 && inRange(side.length) ? side.length : 0);
152
+ const turns = sides.map((side) => degreesOf(side.turn));
153
+ const total = lengths.reduce((sum, length) => sum + length, 0);
154
+ const sum = turns.reduce((all, turn) => all + turn, 0);
155
+ const toLeft = sum >= 0;
156
+ const error = sum - (toLeft ? 360 : -360);
157
+ const measured = sides.filter((side) => isMeasured(side.turn)).length;
158
+ const corrected = measured > 0 && Math.abs(error) < 45;
159
+ const share = corrected ? error / measured : 0;
160
+ let direction = 0;
161
+ const steps = sides.map((side, index) => {
162
+ const [dx, dy] = directionOf(direction);
163
+ direction += turns[index] - (isMeasured(side.turn) ? share : 0);
164
+ return [dx * lengths[index], dy * lengths[index]];
165
+ });
166
+ const points = [[0, 0]];
167
+ steps.forEach(([dx, dy], index) => {
168
+ const [x, y] = points[index];
169
+ points.push([x + dx, y + dy]);
170
+ });
171
+ const [gapX, gapY] = points[count];
172
+ const gap = Math.hypot(gapX, gapY);
173
+ const ratio = total > 0 ? gap / total : 0;
174
+ const given = options.limit ?? .01;
175
+ const closed = count >= 3 && total > 0 && (corrected || Math.abs(error) < 1e-9) && ratio <= (Number.isFinite(given) && given >= 0 ? given : .01);
176
+ const moveX = steps.reduce((all, [dx]) => all + Math.abs(dx), 0);
177
+ const moveY = steps.reduce((all, [, dy]) => all + Math.abs(dy), 0);
178
+ let beforeX = 0;
179
+ let beforeY = 0;
180
+ const adjusted = steps.map(([dx, dy], index) => {
181
+ const [x, y] = points[index];
182
+ const partX = closed && moveX > 0 ? beforeX / moveX : 0;
183
+ const partY = closed && moveY > 0 ? beforeY / moveY : 0;
184
+ beforeX += Math.abs(dx);
185
+ beforeY += Math.abs(dy);
186
+ return [x - partX * gapX, y - partY * gapY];
187
+ });
188
+ const start = survey.start !== void 0 && inRange(survey.start[0]) && inRange(survey.start[1]) ? survey.start : [0, 0];
189
+ const [cos, sin] = directionOf(survey.heading !== void 0 && Number.isFinite(survey.heading) ? survey.heading : 0);
190
+ const place = ([x, y]) => [start[0] + (x * cos - y * sin), start[1] + (x * sin + y * cos)];
191
+ const walk = points.map(place);
192
+ const corners = adjusted.map(place);
193
+ const walls = sides.map((side, index) => {
194
+ const to = closed ? corners[(index + 1) % count] : walk[index + 1];
195
+ return {
196
+ id: side.id,
197
+ from: corners[index],
198
+ to,
199
+ side: toLeft ? "right" : "left",
200
+ ...side.thickness === void 0 ? {} : { thickness: side.thickness },
201
+ ...side.height === void 0 ? {} : { height: side.height }
202
+ };
203
+ });
204
+ const cornerOf = /* @__PURE__ */ new Map();
205
+ sides.forEach((side, index) => {
206
+ if (!cornerOf.has(side.id)) cornerOf.set(side.id, corners[index]);
207
+ });
208
+ const checks = (survey.checks ?? []).map((check) => {
209
+ const from = cornerOf.get(check.from);
210
+ const to = cornerOf.get(check.to);
211
+ const model = from === void 0 || to === void 0 ? void 0 : Math.hypot(to[0] - from[0], to[1] - from[1]);
212
+ return {
213
+ id: check.id,
214
+ measured: check.length,
215
+ model,
216
+ difference: model === void 0 ? void 0 : check.length - model
217
+ };
218
+ });
219
+ const shared = closed && gap > 0;
220
+ const factorOf = /* @__PURE__ */ new Map();
221
+ sides.forEach((side, index) => {
222
+ if (!shared || factorOf.has(side.id) || !(lengths[index] > 0)) return;
223
+ const { from, to } = walls[index];
224
+ factorOf.set(side.id, Math.hypot(to[0] - from[0], to[1] - from[1]) / lengths[index]);
225
+ });
226
+ const openings = survey.openings?.map((opening) => {
227
+ const factor = factorOf.get(opening.wall) ?? 1;
228
+ return factor === 1 ? opening : {
229
+ ...opening,
230
+ offset: opening.offset * factor,
231
+ width: opening.width * factor
232
+ };
233
+ });
234
+ const face = toLeft ? "left" : "right";
235
+ const room = {
236
+ schema: ROOM_SCHEMA,
237
+ ...survey.name === void 0 ? {} : { name: survey.name },
238
+ unit: survey.unit,
239
+ height: survey.height,
240
+ wallThickness: survey.wallThickness,
241
+ walls,
242
+ ...survey.openings === void 0 || openings === void 0 ? {} : { openings: shared ? openings : survey.openings },
243
+ ...closed ? { floors: [{
244
+ id: "floor",
245
+ ...survey.name === void 0 ? {} : { name: survey.name },
246
+ ring: toLeft ? corners : [...corners].reverse()
247
+ }] } : {},
248
+ ...survey.outlets === void 0 ? {} : { outlets: survey.outlets.map((outlet) => ({
249
+ ...outlet,
250
+ offset: outlet.offset * (factorOf.get(outlet.wall) ?? 1),
251
+ face
252
+ })) },
253
+ ...survey.circuits === void 0 ? {} : { circuits: survey.circuits }
254
+ };
255
+ const sideOf = /* @__PURE__ */ new Map();
256
+ sides.forEach((side, index) => {
257
+ if (!sideOf.has(side.id)) sideOf.set(side.id, index);
258
+ });
259
+ const listed = /* @__PURE__ */ new Map();
260
+ for (const object of survey.objects ?? []) if (!listed.has(object.id)) listed.set(object.id, object);
261
+ const places = /* @__PURE__ */ new Map();
262
+ const placeOf = (object) => {
263
+ if (places.has(object)) return places.get(object);
264
+ places.set(object, void 0);
265
+ const made = placed(object);
266
+ places.set(object, made);
267
+ return made;
268
+ };
269
+ const placed = (object) => {
270
+ const out = object.gap ?? 0;
271
+ if (![
272
+ object.width,
273
+ object.depth,
274
+ object.height
275
+ ].every((size) => size > 0 && inRange(size))) return void 0;
276
+ if (![
277
+ object.offset,
278
+ out,
279
+ object.lift ?? 0
280
+ ].every(inRange)) return void 0;
281
+ const record = {
282
+ id: object.id,
283
+ kind: object.kind,
284
+ ...object.name === void 0 ? {} : { name: object.name },
285
+ size: [
286
+ object.width,
287
+ object.depth,
288
+ object.height
289
+ ]
290
+ };
291
+ const power = object.outlet === void 0 ? {} : { outlet: object.outlet };
292
+ if (object.on !== void 0) {
293
+ const holder = listed.get(object.on);
294
+ if (holder === void 0 || placeOf(holder) === void 0) return void 0;
295
+ const x = object.offset + object.width / 2 - holder.width / 2;
296
+ return {
297
+ ...record,
298
+ at: [toLeft ? x : 0 - x, out + object.depth / 2 - holder.depth / 2],
299
+ lift: object.lift ?? holder.height,
300
+ parent: holder.id,
301
+ ...power
302
+ };
303
+ }
304
+ const index = object.wall === void 0 ? void 0 : sideOf.get(object.wall);
305
+ if (index === void 0) return void 0;
306
+ const { from, to } = walls[index];
307
+ const measure = lengths[index];
308
+ const line = Math.hypot(to[0] - from[0], to[1] - from[1]);
309
+ if (!(line > 0) || !(measure > 0)) return void 0;
310
+ const free = measure - object.width;
311
+ const part = Math.min(1, Math.max(0, object.offset / (free > 0 ? free : measure)));
312
+ const near = shared ? object.offset + (line - measure) * part : object.offset;
313
+ const along = [(to[0] - from[0]) / line, (to[1] - from[1]) / line];
314
+ const inward = toLeft ? [0 - along[1], along[0]] : [along[1], 0 - along[0]];
315
+ const a = near + object.width / 2;
316
+ const b = out + object.depth / 2;
317
+ const turn = headingOf(toLeft ? along : [0 - along[0], 0 - along[1]]);
318
+ return {
319
+ ...record,
320
+ at: [from[0] + along[0] * a + inward[0] * b, from[1] + along[1] * a + inward[1] * b],
321
+ ...object.lift === void 0 ? {} : { lift: object.lift },
322
+ ...turn === 0 ? {} : { turn },
323
+ ...power
324
+ };
325
+ };
326
+ const objects = (survey.objects ?? []).flatMap((object) => {
327
+ const made = placeOf(object);
328
+ return made === void 0 ? [] : [made];
329
+ });
330
+ return {
331
+ room,
332
+ corners,
333
+ walk,
334
+ closure: {
335
+ gap,
336
+ ratio,
337
+ turn: error,
338
+ closed
339
+ },
340
+ checks,
341
+ objects
342
+ };
343
+ }
344
+ // src/level.ts
345
+ /** The name of the format of a level record. */
346
+ const LEVEL_SCHEMA = "level/1";
347
+ /**
348
+ * These are the sources of the place of a room.
349
+ *
350
+ * - `first`: the room is the first room of the level. It keeps the `start` and the `heading` of its survey.
351
+ * - `connection`: a connection gives the place.
352
+ * - `apart`: the room has no path of connections to a room before it, so its place is not known.
353
+ */
354
+ const ROOM_PLACES = [
355
+ "first",
356
+ "connection",
357
+ "apart"
358
+ ];
359
+ /**
360
+ * These are the uses of a connection.
361
+ *
362
+ * - `place`: the connection gives the place of a room.
363
+ * - `check`: its two rooms have a place from other connections, so the connection examines the measurements.
364
+ * - `none`: the connection has a fault, so the level does not use it.
365
+ */
366
+ const CONNECTION_USES = [
367
+ "place",
368
+ "check",
369
+ "none"
370
+ ];
371
+ /**
372
+ * These are the faults of a connection.
373
+ *
374
+ * - `room`: an end names a room that the level does not have.
375
+ * - `opening`: an end names an opening that its room does not have, or the wall of the opening has no length.
376
+ * - `one-room`: the two ends are in one room.
377
+ */
378
+ const CONNECTION_FAULTS = [
379
+ "room",
380
+ "opening",
381
+ "one-room"
382
+ ];
383
+ /** The largest angle between the two walls of a connection that fits, in degrees. */
384
+ const CONNECTION_TURN_LIMIT = 2;
385
+ /** A length in meters at this value or less is 0 for the level. */
386
+ const SMALL = 1e-6;
387
+ /** Two unit vectors with a cross product below this value are parallel. */
388
+ const PARALLEL = 1e-6;
389
+ /** The number of steps of the grid of calculated lengths in one unit. The noise of float arithmetic is much smaller than one step. */
390
+ const GRID = 1e9;
391
+ /** This function gives the sum of two vectors of the plan. The second vector has a factor. */
392
+ function plus(a, b, factor = 1) {
393
+ return [a[0] + b[0] * factor, a[1] + b[1] * factor];
394
+ }
395
+ /** This function gives the dot product of two vectors of the plan. */
396
+ function dot(a, b) {
397
+ return a[0] * b[0] + a[1] * b[1];
398
+ }
399
+ /** This function gives the cross product of two vectors of the plan. */
400
+ function cross(a, b) {
401
+ return a[0] * b[1] - a[1] * b[0];
402
+ }
403
+ /**
404
+ * This function gives a calculated length on the grid. It removes the noise of float arithmetic,
405
+ * so a place of 0.1 does not become 0.10000000000000009 in a file.
406
+ */
407
+ function tidy(value) {
408
+ const steps = Math.round(value * GRID);
409
+ return Number.isSafeInteger(steps) ? steps / GRID + 0 : value;
410
+ }
411
+ /** This function gives a point of the frame of a room as a point of the level. */
412
+ function placed(place, point) {
413
+ return plus(place.start, turned(point, place.heading));
414
+ }
415
+ /** This function gives the unit vector along a wall and the unit vector from the room through the wall. A wall with no length has no vectors. */
416
+ function vectorsOf(wall) {
417
+ const dx = wall.to[0] - wall.from[0];
418
+ const dy = wall.to[1] - wall.from[1];
419
+ const length = Math.hypot(dx, dy);
420
+ if (!(length > 0) || !Number.isFinite(length)) return void 0;
421
+ const along = [dx / length, dy / length];
422
+ return {
423
+ along,
424
+ out: wall.side === "left" ? [0 - along[1], along[0]] : [along[1], 0 - along[0]],
425
+ length
426
+ };
427
+ }
428
+ /** This function gives an opening of a room in the frame of its survey. `own` is the expansion of the survey with no `start` and no `heading`. */
429
+ function holeOf(own, id, inRange) {
430
+ const opening = own.room.openings?.find((item) => item.id === id);
431
+ if (opening === void 0 || !inRange(opening.offset) || !inRange(opening.width)) return void 0;
432
+ const wall = own.room.walls.findIndex((item) => item.id === opening.wall);
433
+ const record = own.room.walls[wall];
434
+ const vectors = record === void 0 ? void 0 : vectorsOf(record);
435
+ if (record === void 0 || vectors === void 0) return void 0;
436
+ return {
437
+ wall,
438
+ center: plus(record.from, vectors.along, opening.offset + opening.width / 2),
439
+ out: vectors.out,
440
+ width: opening.width,
441
+ thickness: record.thickness ?? own.room.wallThickness
442
+ };
443
+ }
444
+ /**
445
+ * This function gives the place of a room from a connection to a room that has a place.
446
+ * The two walls are back to back, and the centers of the two openings are opposite, at the distance `depth`.
447
+ */
448
+ function placeFrom(known, knownHole, hole, depth) {
449
+ const out = turned(knownHole.out, known.heading);
450
+ const heading = squared(headingOf([0 - out[0], 0 - out[1]]) - headingOf(hole.out));
451
+ const center = turned(hole.center, heading);
452
+ const start = plus(plus(placed(known, knownHole.center), out, depth), center, -1);
453
+ return {
454
+ start: [tidy(start[0]), tidy(start[1])],
455
+ heading
456
+ };
457
+ }
458
+ /**
459
+ * This function makes one room record from the rooms of a level.
460
+ *
461
+ * The first room keeps the `start` and the `heading` of its survey. The function goes from that room along the connections.
462
+ * A connection to a room with no place gives the place of that room. The two walls are back to back, and the centers of the
463
+ * two openings are opposite. The faces of the two walls are at the distance `thickness`.
464
+ * A connection between two rooms that have a place examines the measurements: its `gap` and its `turn` tell a mistake.
465
+ *
466
+ * A room with no path of connections to a room before it starts a new group. Its place is not known,
467
+ * so its group goes to the right of the groups before it. Thus a drawing of a level in progress shows each room.
468
+ *
469
+ * The survey of a room gives the thickness of each wall. Two rooms have one wall between them, so two walls that are
470
+ * back to back share it. Each wall gets one half of the distance between the two faces.
471
+ * This applies to the two walls of a connection, and to two walls that overlap.
472
+ * A wall that is in part an outer wall gets the same thickness along its full length.
473
+ *
474
+ * `checkRoom` examines the room record. It tells when two rooms overlap.
475
+ */
476
+ function expandLevel(level, options = {}) {
477
+ const unit = level.unit;
478
+ const scale = metersPer(unit);
479
+ const inRange = (value) => Number.isFinite(value) && Math.abs(value) * scale <= 1e6;
480
+ const size = (value) => value !== void 0 && inRange(value) && value * scale > SMALL ? value : void 0;
481
+ const count = level.rooms.length;
482
+ const surveys = level.rooms.map((room) => convertSurvey(room.survey, unit));
483
+ const owns = surveys.map((survey) => expandSurvey({
484
+ ...survey,
485
+ start: void 0,
486
+ heading: void 0
487
+ }, options));
488
+ const positionOf = /* @__PURE__ */ new Map();
489
+ level.rooms.forEach((room, index) => {
490
+ if (!positionOf.has(room.id)) positionOf.set(room.id, index);
491
+ });
492
+ const connections = level.connections ?? [];
493
+ const faults = [];
494
+ const links = connections.map((connection, index) => {
495
+ const from = positionOf.get(connection.from.room);
496
+ const to = positionOf.get(connection.to.room);
497
+ if (from === void 0 || to === void 0) {
498
+ faults[index] = "room";
499
+ return;
500
+ }
501
+ if (from === to) {
502
+ faults[index] = "one-room";
503
+ return;
504
+ }
505
+ const fromHole = holeOf(owns[from], connection.from.opening, inRange);
506
+ const toHole = holeOf(owns[to], connection.to.opening, inRange);
507
+ if (fromHole === void 0 || toHole === void 0) {
508
+ faults[index] = "opening";
509
+ return;
510
+ }
511
+ return {
512
+ from,
513
+ to,
514
+ fromHole,
515
+ toHole,
516
+ depth: size(connection.thickness) ?? size(fromHole.thickness) ?? 0
517
+ };
518
+ });
519
+ const places = [];
520
+ const sources = [];
521
+ const groups = [];
522
+ const members = [];
523
+ const gives = /* @__PURE__ */ new Set();
524
+ for (let root = 0; root < count; root++) {
525
+ if (places[root] !== void 0) continue;
526
+ const group = members.length;
527
+ const survey = surveys[root];
528
+ const heading = survey.heading !== void 0 && Number.isFinite(survey.heading) ? survey.heading : 0;
529
+ const start = group === 0 && survey.start !== void 0 && inRange(survey.start[0]) && inRange(survey.start[1]) ? survey.start : [0, 0];
530
+ places[root] = {
531
+ start,
532
+ heading
533
+ };
534
+ sources[root] = group === 0 ? "first" : "apart";
535
+ groups[root] = group;
536
+ const queue = [root];
537
+ for (let next = 0; next < queue.length; next++) {
538
+ const known = queue[next];
539
+ links.forEach((link, index) => {
540
+ if (link === void 0) return;
541
+ const forward = link.from === known;
542
+ if (!forward && link.to !== known) return;
543
+ const room = forward ? link.to : link.from;
544
+ if (places[room] !== void 0) return;
545
+ places[room] = placeFrom(places[known], forward ? link.fromHole : link.toHole, forward ? link.toHole : link.fromHole, link.depth);
546
+ sources[room] = "connection";
547
+ groups[room] = group;
548
+ gives.add(index);
549
+ queue.push(room);
550
+ });
551
+ }
552
+ members.push(queue);
553
+ }
554
+ const givenGap = options.groupGap;
555
+ const groupGap = givenGap !== void 0 && inRange(givenGap) && givenGap >= 0 ? givenGap : 1 / scale;
556
+ let right;
557
+ let base = 0;
558
+ for (const [number, group] of members.entries()) {
559
+ let low;
560
+ let high;
561
+ for (const room of group) {
562
+ const survey = surveys[room];
563
+ const margin = Math.max(0, ...[survey.wallThickness, ...survey.sides.map((side) => side.thickness)].map((value) => size(value) ?? 0));
564
+ for (const point of [...owns[room].walk, ...owns[room].corners]) {
565
+ const [x, y] = placed(places[room], point);
566
+ low = low === void 0 ? [x - margin, y - margin] : [Math.min(low[0], x - margin), Math.min(low[1], y - margin)];
567
+ high = high === void 0 ? [x + margin, y + margin] : [Math.max(high[0], x + margin), Math.max(high[1], y + margin)];
568
+ }
569
+ }
570
+ if (low === void 0 || high === void 0) continue;
571
+ if (number === 0 || right === void 0) {
572
+ right = right === void 0 ? high[0] : Math.max(right, high[0]);
573
+ if (number === 0) base = low[1];
574
+ continue;
575
+ }
576
+ const move = [right + groupGap - low[0], base - low[1]];
577
+ for (const room of group) {
578
+ const [x, y] = plus(places[room].start, move);
579
+ places[room] = {
580
+ start: [tidy(x), tidy(y)],
581
+ heading: places[room].heading
582
+ };
583
+ }
584
+ right = Math.max(right, high[0] + move[0]);
585
+ }
586
+ const rooms = level.rooms.map((room, index) => {
587
+ const survey = {
588
+ ...surveys[index],
589
+ start: places[index].start,
590
+ heading: places[index].heading
591
+ };
592
+ return {
593
+ id: room.id,
594
+ place: sources[index],
595
+ group: groups[index],
596
+ survey,
597
+ result: expandSurvey(survey, options)
598
+ };
599
+ });
600
+ const givenLimit = options.limit ?? .01;
601
+ const limit = Number.isFinite(givenLimit) && givenLimit >= 0 ? givenLimit : CLOSURE_LIMIT;
602
+ const walkOf = (room) => {
603
+ const walk = owns[room].walk;
604
+ return walk.reduce((sum, point, index) => index === 0 ? 0 : sum + Math.hypot(point[0] - walk[index - 1][0], point[1] - walk[index - 1][1]), 0);
605
+ };
606
+ const results = connections.map((connection, index) => {
607
+ const link = links[index];
608
+ if (link === void 0) return {
609
+ id: connection.id,
610
+ use: "none",
611
+ fault: faults[index],
612
+ gap: void 0,
613
+ ratio: void 0,
614
+ turn: void 0,
615
+ width: void 0,
616
+ fits: false
617
+ };
618
+ const width = link.toHole.width - link.fromHole.width;
619
+ if (gives.has(index)) return {
620
+ id: connection.id,
621
+ use: "place",
622
+ gap: 0,
623
+ ratio: 0,
624
+ turn: 0,
625
+ width,
626
+ fits: true
627
+ };
628
+ const from = places[link.from];
629
+ const to = places[link.to];
630
+ const out = turned(link.fromHole.out, from.heading);
631
+ const back = turned(link.toHole.out, to.heading);
632
+ const target = plus(placed(from, link.fromHole.center), out, link.depth);
633
+ const center = placed(to, link.toHole.center);
634
+ const miss = Math.hypot(center[0] - target[0], center[1] - target[1]);
635
+ const walk = walkOf(link.from) + walkOf(link.to);
636
+ const ratio = walk > 0 ? miss / walk : 0;
637
+ const against = [0 - out[0], 0 - out[1]];
638
+ const turn = Math.atan2(cross(against, back), dot(against, back)) * 180 / Math.PI + 0;
639
+ return {
640
+ id: connection.id,
641
+ use: "check",
642
+ gap: miss,
643
+ ratio,
644
+ turn,
645
+ width,
646
+ fits: ratio <= limit && Math.abs(turn) <= 2
647
+ };
648
+ });
649
+ const faces = [];
650
+ rooms.forEach(({ result }, room) => {
651
+ result.room.walls.forEach((wall, index) => {
652
+ const vectors = vectorsOf(wall);
653
+ const thickness = size(wall.thickness ?? result.room.wallThickness);
654
+ if (vectors === void 0 || thickness === void 0) return;
655
+ faces.push({
656
+ room,
657
+ wall: index,
658
+ from: wall.from,
659
+ to: wall.to,
660
+ along: vectors.along,
661
+ out: vectors.out,
662
+ length: vectors.length,
663
+ thickness
664
+ });
665
+ });
666
+ });
667
+ const joined = /* @__PURE__ */ new Set();
668
+ links.forEach((link, index) => {
669
+ if (link === void 0 || !results[index].fits) return;
670
+ joined.add(`${link.from}:${link.fromHole.wall}:${link.to}:${link.toHole.wall}`);
671
+ joined.add(`${link.to}:${link.toHole.wall}:${link.from}:${link.fromHole.wall}`);
672
+ });
673
+ const shared = /* @__PURE__ */ new Map();
674
+ for (let i = 0; i < faces.length; i++) for (let j = i + 1; j < faces.length; j++) {
675
+ const a = faces[i];
676
+ const b = faces[j];
677
+ if (a.room === b.room || Math.abs(cross(a.along, b.along)) > PARALLEL || !(dot(a.out, b.out) < 0)) continue;
678
+ const first = [b.from[0] - a.from[0], b.from[1] - a.from[1]];
679
+ const second = [b.to[0] - a.from[0], b.to[1] - a.from[1]];
680
+ const between = dot(first, a.out);
681
+ const common = Math.min(a.length, Math.max(dot(first, a.along), dot(second, a.along))) - Math.max(0, Math.min(dot(first, a.along), dot(second, a.along)));
682
+ if (!(between * scale > SMALL) || !(common * scale > SMALL)) continue;
683
+ if (!joined.has(`${a.room}:${a.wall}:${b.room}:${b.wall}`) && !((a.thickness + b.thickness - between) * scale > SMALL)) continue;
684
+ const half = tidy(between / 2);
685
+ for (const face of [a, b]) {
686
+ const key = `${face.room}:${face.wall}`;
687
+ shared.set(key, Math.min(shared.get(key) ?? 1e400, half));
688
+ }
689
+ }
690
+ const first = rooms[0]?.result.room;
691
+ const height = first?.height ?? 1;
692
+ const wallThickness = first?.wallThickness ?? 1;
693
+ const walls = [];
694
+ const openings = [];
695
+ const floors = [];
696
+ const outlets = [];
697
+ const objects = [];
698
+ const circuits = /* @__PURE__ */ new Map();
699
+ for (const circuit of level.circuits ?? []) if (!circuits.has(circuit.id)) circuits.set(circuit.id, circuit);
700
+ rooms.forEach(({ id, survey, result }, room) => {
701
+ const record = result.room;
702
+ record.walls.forEach((wall, index) => {
703
+ const thickness = shared.get(`${room}:${index}`) ?? wall.thickness ?? (record.wallThickness === wallThickness ? void 0 : record.wallThickness);
704
+ const tall = wall.height ?? (record.height === height ? void 0 : record.height);
705
+ walls.push({
706
+ id: `${id}/${wall.id}`,
707
+ from: wall.from,
708
+ to: wall.to,
709
+ ...wall.side === void 0 ? {} : { side: wall.side },
710
+ ...thickness === void 0 ? {} : { thickness },
711
+ ...tall === void 0 ? {} : { height: tall }
712
+ });
713
+ });
714
+ for (const opening of record.openings ?? []) openings.push({
715
+ ...opening,
716
+ id: `${id}/${opening.id}`,
717
+ wall: `${id}/${opening.wall}`
718
+ });
719
+ for (const floor of record.floors ?? []) floors.push({
720
+ id,
721
+ ...survey.name === void 0 ? {} : { name: survey.name },
722
+ ring: floor.ring
723
+ });
724
+ for (const outlet of record.outlets ?? []) outlets.push({
725
+ ...outlet,
726
+ id: `${id}/${outlet.id}`,
727
+ wall: `${id}/${outlet.wall}`
728
+ });
729
+ for (const circuit of record.circuits ?? []) if (!circuits.has(circuit.id)) circuits.set(circuit.id, circuit);
730
+ for (const object of result.objects) objects.push({
731
+ ...object,
732
+ id: `${id}/${object.id}`,
733
+ ...object.parent === void 0 ? {} : { parent: `${id}/${object.parent}` },
734
+ ...object.outlet === void 0 ? {} : { outlet: `${id}/${object.outlet}` }
735
+ });
736
+ });
737
+ return {
738
+ room: {
739
+ schema: ROOM_SCHEMA,
740
+ ...level.name === void 0 ? {} : { name: level.name },
741
+ unit,
742
+ height,
743
+ wallThickness,
744
+ walls,
745
+ ...openings.length === 0 ? {} : { openings },
746
+ ...floors.length === 0 ? {} : { floors },
747
+ ...outlets.length === 0 ? {} : { outlets },
748
+ ...circuits.size === 0 ? {} : { circuits: [...circuits.values()] }
749
+ },
750
+ rooms,
751
+ connections: results,
752
+ objects
753
+ };
754
+ }
755
+ export { ROOM_PLACES as a, SQUARE_TURNS as c, expandSurvey as d, OPENING_KINDS as f, WALL_SIDES as g, WALL_FACES as h, LEVEL_SCHEMA as i, SURVEY_SCHEMA as l, SCENE_SCHEMA as m, CONNECTION_TURN_LIMIT as n, expandLevel as o, ROOM_SCHEMA as p, CONNECTION_USES as r, CLOSURE_LIMIT as s, CONNECTION_FAULTS as t, convertSurvey as u };
756
+
757
+ //# sourceMappingURL=level-xTO7P1Fn.js.map