@mark1russell7/room-view 0.3.0 → 0.3.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.
- package/README.md +28 -0
- package/build/{check-DI_B57qy.js → check-lFfxIUS3.js} +3 -29
- package/build/check-lFfxIUS3.js.map +1 -0
- package/build/index.d.ts +4 -3
- package/build/index.d.ts.map +1 -1
- package/build/index.js +5 -3
- package/build/index.js.map +1 -1
- package/build/{layers-CYZwt1bH.d.ts → layers-BiiyFJwZ.d.ts} +3 -2
- package/build/layers-BiiyFJwZ.d.ts.map +1 -0
- package/build/{layers-CDNFVXds.js → layers-DmTro5Ny.js} +5 -29
- package/build/layers-DmTro5Ny.js.map +1 -0
- package/build/{level-BERjgyrT.js → level-CvbiwvhH.js} +4 -3
- package/build/{level-BERjgyrT.js.map → level-CvbiwvhH.js.map} +1 -1
- package/build/{level-m-mJ_3Ok.d.ts → level-DmEftWYn.d.ts} +3 -3
- package/build/{level-m-mJ_3Ok.d.ts.map → level-DmEftWYn.d.ts.map} +1 -1
- package/build/marks.d.ts +2 -2
- package/build/marks.js +1 -1
- package/build/plan-import.d.ts +325 -0
- package/build/plan-import.d.ts.map +1 -0
- package/build/plan-import.js +855 -0
- package/build/plan-import.js.map +1 -0
- package/build/room-BQua5eSs.d.ts +124 -0
- package/build/room-BQua5eSs.d.ts.map +1 -0
- package/build/room-CiVZ_07s.js +29 -0
- package/build/room-CiVZ_07s.js.map +1 -0
- package/build/sample.d.ts +2 -2
- package/build/schema.d.ts +3 -3
- package/build/schema.js +4 -3
- package/build/schema.js.map +1 -1
- package/build/{solids-OiTlb_Pm.d.ts → solids-CdJf0U39.d.ts} +3 -123
- package/build/solids-CdJf0U39.d.ts.map +1 -0
- package/build/three.d.ts +2 -1
- package/build/three.d.ts.map +1 -1
- package/build/three.js +2 -1
- package/build/three.js.map +1 -1
- package/build/units-BZmvVyu4.js +30 -0
- package/build/units-BZmvVyu4.js.map +1 -0
- package/package.json +7 -1
- package/build/check-DI_B57qy.js.map +0 -1
- package/build/layers-CDNFVXds.js.map +0 -1
- package/build/layers-CYZwt1bH.d.ts.map +0 -1
- package/build/solids-OiTlb_Pm.d.ts.map +0 -1
|
@@ -0,0 +1,855 @@
|
|
|
1
|
+
import { n as ROOM_SCHEMA } from "./room-CiVZ_07s.js";
|
|
2
|
+
import { a as unitsPer } from "./units-BZmvVyu4.js";
|
|
3
|
+
import { regionArea } from "@mark1russell7/layout-contract";
|
|
4
|
+
import { median } from "@mark1russell7/rigor-statistics";
|
|
5
|
+
// src/plan-import/sheet.ts
|
|
6
|
+
/** This function gives the name of the pen of a line: its width, a bar, then its color. */
|
|
7
|
+
function penOf(line) {
|
|
8
|
+
return `${line.width}|${line.color}`;
|
|
9
|
+
}
|
|
10
|
+
/** This function gives the length of a line. */
|
|
11
|
+
function lineLength(line) {
|
|
12
|
+
return Math.hypot(line.to[0] - line.from[0], line.to[1] - line.from[1]);
|
|
13
|
+
}
|
|
14
|
+
/** This function gives the box of a fill: the smallest x, the smallest y, the largest x and the largest y. */
|
|
15
|
+
function fillBox(fill) {
|
|
16
|
+
let x0 = 1e400;
|
|
17
|
+
let y0 = 1e400;
|
|
18
|
+
let x1 = -1e400;
|
|
19
|
+
let y1 = -1e400;
|
|
20
|
+
for (const ring of fill.rings) for (const [x, y] of ring) {
|
|
21
|
+
x0 = Math.min(x0, x);
|
|
22
|
+
y0 = Math.min(y0, y);
|
|
23
|
+
x1 = Math.max(x1, x);
|
|
24
|
+
y1 = Math.max(y1, y);
|
|
25
|
+
}
|
|
26
|
+
return [
|
|
27
|
+
x0,
|
|
28
|
+
y0,
|
|
29
|
+
x1,
|
|
30
|
+
y1
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* This function reads the legend of a sheet. A sample is a small square fill with a text near its right side.
|
|
35
|
+
* The text is the name of the color of the sample.
|
|
36
|
+
*/
|
|
37
|
+
function readLegend(sheet, options = {}) {
|
|
38
|
+
const smallest = options.smallest ?? 4;
|
|
39
|
+
const largest = options.largest ?? 30;
|
|
40
|
+
const reach = options.reach ?? 12;
|
|
41
|
+
const smallestText = options.smallestText ?? 2;
|
|
42
|
+
const texts = sheet.texts ?? [];
|
|
43
|
+
const names = /* @__PURE__ */ new Map();
|
|
44
|
+
const samples = [];
|
|
45
|
+
(sheet.fills ?? []).forEach((fill, index) => {
|
|
46
|
+
const [x0, y0, x1, y1] = fillBox(fill);
|
|
47
|
+
const w = x1 - x0;
|
|
48
|
+
const h = y1 - y0;
|
|
49
|
+
if (w < smallest || h < smallest || w > largest || h > largest || Math.abs(w - h) > .5 * Math.max(w, h)) return;
|
|
50
|
+
const text = texts.find((item) => item.height > smallestText && item.at[0] > x1 && item.at[0] - x1 < reach && item.at[1] > y0 - 2 && item.at[1] < y1 + 4);
|
|
51
|
+
if (text === void 0 || /^\W*$/.test(text.text)) return;
|
|
52
|
+
samples.push(index);
|
|
53
|
+
const list = names.get(fill.color) ?? [];
|
|
54
|
+
const name = text.text.trim();
|
|
55
|
+
if (!list.includes(name)) list.push(name);
|
|
56
|
+
names.set(fill.color, list);
|
|
57
|
+
});
|
|
58
|
+
return {
|
|
59
|
+
names: Object.fromEntries([...names].map(([color, list]) => [color, list.join("/")])),
|
|
60
|
+
samples
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
// src/plan-import/walls.ts
|
|
64
|
+
/** This function gives the direction of a line, from 0 to pi. */
|
|
65
|
+
function angleOf(line) {
|
|
66
|
+
let angle = Math.atan2(line.to[1] - line.from[1], line.to[0] - line.from[0]);
|
|
67
|
+
if (angle < 0) angle += Math.PI;
|
|
68
|
+
if (angle >= Math.PI) angle -= Math.PI;
|
|
69
|
+
return angle;
|
|
70
|
+
}
|
|
71
|
+
/** This function gives the place of a line in the frame of the unit direction (`ux`, `uy`). */
|
|
72
|
+
function frameOf(line, ux, uy) {
|
|
73
|
+
const offset = -uy * (line.from[0] + line.to[0]) / 2 + ux * (line.from[1] + line.to[1]) / 2;
|
|
74
|
+
const t0 = ux * line.from[0] + uy * line.from[1];
|
|
75
|
+
const t1 = ux * line.to[0] + uy * line.to[1];
|
|
76
|
+
return {
|
|
77
|
+
offset,
|
|
78
|
+
low: Math.min(t0, t1),
|
|
79
|
+
high: Math.max(t0, t1)
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* This function finds the pairs of parallel lines with a distance from `nearest` to `farthest`, and a common part of `overlap` or more.
|
|
84
|
+
* Two lines are parallel when their directions differ by `tolerance` radians or less. The lines go in buckets by direction, so the search is fast.
|
|
85
|
+
*/
|
|
86
|
+
function parallelPairs(lines, nearest, farthest, overlap, tolerance = .0175) {
|
|
87
|
+
const count = Math.round(Math.PI / tolerance);
|
|
88
|
+
const buckets = /* @__PURE__ */ new Map();
|
|
89
|
+
lines.forEach((line, index) => {
|
|
90
|
+
const angle = angleOf(line);
|
|
91
|
+
const key = Math.round(angle / tolerance) % count;
|
|
92
|
+
const theta = key * tolerance;
|
|
93
|
+
const { offset } = frameOf(line, Math.cos(theta), Math.sin(theta));
|
|
94
|
+
const bucket = buckets.get(key) ?? [];
|
|
95
|
+
bucket.push({
|
|
96
|
+
index,
|
|
97
|
+
angle,
|
|
98
|
+
offset
|
|
99
|
+
});
|
|
100
|
+
buckets.set(key, bucket);
|
|
101
|
+
});
|
|
102
|
+
for (const bucket of buckets.values()) bucket.sort((p, q) => p.offset - q.offset);
|
|
103
|
+
const pairs = [];
|
|
104
|
+
const sides = new Int32Array(lines.length * 2);
|
|
105
|
+
const slack = 3;
|
|
106
|
+
const lower = (list, value) => {
|
|
107
|
+
let low = 0;
|
|
108
|
+
let high = list.length;
|
|
109
|
+
while (low < high) {
|
|
110
|
+
const middle = low + high >> 1;
|
|
111
|
+
if (list[middle].offset < value) low = middle + 1;
|
|
112
|
+
else high = middle;
|
|
113
|
+
}
|
|
114
|
+
return low;
|
|
115
|
+
};
|
|
116
|
+
for (const [key, bucket] of buckets) for (const next of [key, (key + 1) % count]) {
|
|
117
|
+
const other = buckets.get(next);
|
|
118
|
+
if (other === void 0 || next === key && count === 1) continue;
|
|
119
|
+
for (const p of bucket) {
|
|
120
|
+
const ux = Math.cos(p.angle);
|
|
121
|
+
const uy = Math.sin(p.angle);
|
|
122
|
+
const pp = frameOf(lines[p.index], ux, uy);
|
|
123
|
+
for (let j = lower(other, p.offset - farthest - slack); j < other.length && other[j].offset <= p.offset + farthest + slack; j++) {
|
|
124
|
+
const q = other[j];
|
|
125
|
+
if (next === key && q.index <= p.index) continue;
|
|
126
|
+
let turn = Math.abs(q.angle - p.angle);
|
|
127
|
+
if (turn > Math.PI / 2) turn = Math.PI - turn;
|
|
128
|
+
if (turn > tolerance) continue;
|
|
129
|
+
const qq = frameOf(lines[q.index], ux, uy);
|
|
130
|
+
const across = qq.offset - pp.offset;
|
|
131
|
+
const distance = Math.abs(across);
|
|
132
|
+
if (distance < nearest || distance > farthest) continue;
|
|
133
|
+
const common = Math.min(pp.high, qq.high) - Math.max(pp.low, qq.low);
|
|
134
|
+
if (common < overlap) continue;
|
|
135
|
+
pairs.push({
|
|
136
|
+
a: p.index,
|
|
137
|
+
b: q.index,
|
|
138
|
+
distance,
|
|
139
|
+
overlap: common
|
|
140
|
+
});
|
|
141
|
+
sides[p.index * 2 + (across > 0 ? 0 : 1)]++;
|
|
142
|
+
sides[q.index * 2 + (across > 0 ? 1 : 0)]++;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
pairs,
|
|
148
|
+
sides
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* This function reads each pen of the straight lines of a sheet.
|
|
153
|
+
* A pen is a wall pen when most of its long lines have a parallel partner at the distance of a wall thickness.
|
|
154
|
+
* Also, most of the paired lines of a wall pen have the partner on one side only.
|
|
155
|
+
* The limits are in the units of the sheet. The pens with the most lines come first.
|
|
156
|
+
*/
|
|
157
|
+
function readPens(lines, limits, scale) {
|
|
158
|
+
const byPen = /* @__PURE__ */ new Map();
|
|
159
|
+
for (const line of lines) {
|
|
160
|
+
if (line.curve === true || lineLength(line) < limits.longLine) continue;
|
|
161
|
+
const pen = penOf(line);
|
|
162
|
+
const list = byPen.get(pen) ?? [];
|
|
163
|
+
list.push(line);
|
|
164
|
+
byPen.set(pen, list);
|
|
165
|
+
}
|
|
166
|
+
const reads = [];
|
|
167
|
+
for (const [pen, list] of byPen) {
|
|
168
|
+
if (list.length < limits.fewestLines) {
|
|
169
|
+
reads.push({
|
|
170
|
+
pen,
|
|
171
|
+
lines: list.length,
|
|
172
|
+
paired: 0,
|
|
173
|
+
oneSide: 0,
|
|
174
|
+
length: 0,
|
|
175
|
+
wall: false
|
|
176
|
+
});
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const { sides } = parallelPairs(list, limits.thinnest, limits.thickest, limits.longLine);
|
|
180
|
+
let paired = 0;
|
|
181
|
+
let oneSide = 0;
|
|
182
|
+
let length = 0;
|
|
183
|
+
list.forEach((line, index) => {
|
|
184
|
+
const left = sides[2 * index];
|
|
185
|
+
const right = sides[2 * index + 1];
|
|
186
|
+
if (left === 0 && right === 0) return;
|
|
187
|
+
paired++;
|
|
188
|
+
length += lineLength(line);
|
|
189
|
+
if (left === 0 || right === 0) oneSide++;
|
|
190
|
+
});
|
|
191
|
+
const share = paired / list.length;
|
|
192
|
+
const single = paired === 0 ? 0 : oneSide / paired;
|
|
193
|
+
reads.push({
|
|
194
|
+
pen,
|
|
195
|
+
lines: list.length,
|
|
196
|
+
paired: share,
|
|
197
|
+
oneSide: single,
|
|
198
|
+
length: length / scale,
|
|
199
|
+
wall: share >= .5 && single >= .6 && length >= limits.leastLength
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
return reads.sort((p, q) => q.lines - p.lines);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* This function gives the walls of the straight lines of the wall pens. Each line keeps its nearest partner, so a face has one wall.
|
|
206
|
+
* The wall is the center line of the common part of the two faces.
|
|
207
|
+
*/
|
|
208
|
+
function wallPairs(lines, limits) {
|
|
209
|
+
const { pairs } = parallelPairs(lines, limits.thinnest, limits.thickest, limits.shortestWall);
|
|
210
|
+
const best = /* @__PURE__ */ new Map();
|
|
211
|
+
for (const pair of pairs) for (const index of [pair.a, pair.b]) {
|
|
212
|
+
const known = best.get(index);
|
|
213
|
+
if (known === void 0 || pair.distance < known.distance) best.set(index, pair);
|
|
214
|
+
}
|
|
215
|
+
const used = /* @__PURE__ */ new Set();
|
|
216
|
+
const walls = [];
|
|
217
|
+
for (const pair of best.values()) {
|
|
218
|
+
const key = `${Math.min(pair.a, pair.b)}:${Math.max(pair.a, pair.b)}`;
|
|
219
|
+
if (used.has(key)) continue;
|
|
220
|
+
used.add(key);
|
|
221
|
+
const a = lines[pair.a];
|
|
222
|
+
const b = lines[pair.b];
|
|
223
|
+
const length = lineLength(a);
|
|
224
|
+
const ux = (a.to[0] - a.from[0]) / length;
|
|
225
|
+
const uy = (a.to[1] - a.from[1]) / length;
|
|
226
|
+
const nx = -uy;
|
|
227
|
+
const ny = ux;
|
|
228
|
+
const along = [(b.from[0] - a.from[0]) * ux + (b.from[1] - a.from[1]) * uy, (b.to[0] - a.from[0]) * ux + (b.to[1] - a.from[1]) * uy];
|
|
229
|
+
const low = Math.max(0, Math.min(...along));
|
|
230
|
+
const high = Math.min(length, Math.max(...along));
|
|
231
|
+
if (!(high > low)) continue;
|
|
232
|
+
const across = ((b.from[0] - a.from[0]) * nx + (b.from[1] - a.from[1]) * ny + (b.to[0] - a.from[0]) * nx + (b.to[1] - a.from[1]) * ny) / 2;
|
|
233
|
+
const point = (t) => [a.from[0] + ux * t + nx * across / 2, a.from[1] + uy * t + ny * across / 2];
|
|
234
|
+
walls.push({
|
|
235
|
+
a: pair.a,
|
|
236
|
+
b: pair.b,
|
|
237
|
+
from: point(low),
|
|
238
|
+
to: point(high),
|
|
239
|
+
thickness: pair.distance
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
return walls;
|
|
243
|
+
}
|
|
244
|
+
// src/plan-import/bridges.ts
|
|
245
|
+
/** These are the sources of a bridge. `ends` is a pair of end lines of two walls. `open` is a pair of open ends of two lines. */
|
|
246
|
+
const BRIDGE_SOURCES = ["ends", "open"];
|
|
247
|
+
/**
|
|
248
|
+
* This function finds the bridges across the gaps in the walls. A gap is from `narrowest` to `widest`.
|
|
249
|
+
*
|
|
250
|
+
* - Two short end lines of walls can be parallel and face each other across a gap. They give two lines, one along each face of the wall.
|
|
251
|
+
* - An open end of a long line, with no other line near it, joins the nearest open end that faces it.
|
|
252
|
+
*/
|
|
253
|
+
function findBridges(lines, limits) {
|
|
254
|
+
const bridges = [];
|
|
255
|
+
const ends = lines.filter((line) => {
|
|
256
|
+
const length = lineLength(line);
|
|
257
|
+
return length >= limits.shortestEnd && length <= limits.longestEnd;
|
|
258
|
+
});
|
|
259
|
+
const { pairs } = parallelPairs(ends, limits.narrowest, limits.widest, .6 * limits.shortestEnd, .06);
|
|
260
|
+
for (const pair of pairs) {
|
|
261
|
+
const a = ends[pair.a];
|
|
262
|
+
const b = ends[pair.b];
|
|
263
|
+
const width = Math.max(a.width, b.width);
|
|
264
|
+
const color = a.color;
|
|
265
|
+
const [b0, b1] = Math.hypot(a.from[0] - b.from[0], a.from[1] - b.from[1]) + Math.hypot(a.to[0] - b.to[0], a.to[1] - b.to[1]) <= Math.hypot(a.from[0] - b.to[0], a.from[1] - b.to[1]) + Math.hypot(a.to[0] - b.from[0], a.to[1] - b.from[1]) ? [b.from, b.to] : [b.to, b.from];
|
|
266
|
+
bridges.push({
|
|
267
|
+
source: "ends",
|
|
268
|
+
from: [(a.from[0] + a.to[0]) / 2, (a.from[1] + a.to[1]) / 2],
|
|
269
|
+
to: [(b.from[0] + b.to[0]) / 2, (b.from[1] + b.to[1]) / 2],
|
|
270
|
+
thickness: (lineLength(a) + lineLength(b)) / 2,
|
|
271
|
+
lines: [{
|
|
272
|
+
from: a.from,
|
|
273
|
+
to: b0,
|
|
274
|
+
width,
|
|
275
|
+
color
|
|
276
|
+
}, {
|
|
277
|
+
from: a.to,
|
|
278
|
+
to: b1,
|
|
279
|
+
width,
|
|
280
|
+
color
|
|
281
|
+
}]
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
const cell = Math.max(limits.touch * 3, 1e-9);
|
|
285
|
+
const grid = /* @__PURE__ */ new Map();
|
|
286
|
+
const keyOf = (x, y) => `${Math.floor(x / cell)},${Math.floor(y / cell)}`;
|
|
287
|
+
lines.forEach((line, index) => {
|
|
288
|
+
const steps = Math.max(1, Math.ceil(lineLength(line) / cell));
|
|
289
|
+
for (let k = 0; k <= steps; k++) {
|
|
290
|
+
const key = keyOf(line.from[0] + (line.to[0] - line.from[0]) * k / steps, line.from[1] + (line.to[1] - line.from[1]) * k / steps);
|
|
291
|
+
const list = grid.get(key) ?? [];
|
|
292
|
+
if (list[list.length - 1] !== index) list.push(index);
|
|
293
|
+
grid.set(key, list);
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
const near = (x, y, self) => {
|
|
297
|
+
const cx = Math.floor(x / cell);
|
|
298
|
+
const cy = Math.floor(y / cell);
|
|
299
|
+
for (let dx = -1; dx <= 1; dx++) for (let dy = -1; dy <= 1; dy++) for (const index of grid.get(`${cx + dx},${cy + dy}`) ?? []) {
|
|
300
|
+
if (index === self) continue;
|
|
301
|
+
const line = lines[index];
|
|
302
|
+
const vx = line.to[0] - line.from[0];
|
|
303
|
+
const vy = line.to[1] - line.from[1];
|
|
304
|
+
const square = vx * vx + vy * vy;
|
|
305
|
+
const t = square > 0 ? Math.max(0, Math.min(1, ((x - line.from[0]) * vx + (y - line.from[1]) * vy) / square)) : 0;
|
|
306
|
+
if (Math.hypot(x - line.from[0] - t * vx, y - line.from[1] - t * vy) < limits.touch) return true;
|
|
307
|
+
}
|
|
308
|
+
return false;
|
|
309
|
+
};
|
|
310
|
+
const open = [];
|
|
311
|
+
lines.forEach((line, index) => {
|
|
312
|
+
const length = lineLength(line);
|
|
313
|
+
if (length < limits.longestEnd) return;
|
|
314
|
+
const ux = (line.to[0] - line.from[0]) / length;
|
|
315
|
+
const uy = (line.to[1] - line.from[1]) / length;
|
|
316
|
+
if (!near(line.to[0], line.to[1], index)) open.push({
|
|
317
|
+
x: line.to[0],
|
|
318
|
+
y: line.to[1],
|
|
319
|
+
dx: ux,
|
|
320
|
+
dy: uy,
|
|
321
|
+
line: index
|
|
322
|
+
});
|
|
323
|
+
if (!near(line.from[0], line.from[1], index)) open.push({
|
|
324
|
+
x: line.from[0],
|
|
325
|
+
y: line.from[1],
|
|
326
|
+
dx: -ux,
|
|
327
|
+
dy: -uy,
|
|
328
|
+
line: index
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
const span = Math.max(limits.widest, 1e-9);
|
|
332
|
+
const endGrid = /* @__PURE__ */ new Map();
|
|
333
|
+
const spanKey = (x, y) => `${Math.floor(x / span)},${Math.floor(y / span)}`;
|
|
334
|
+
open.forEach((end, index) => {
|
|
335
|
+
const key = spanKey(end.x, end.y);
|
|
336
|
+
const list = endGrid.get(key) ?? [];
|
|
337
|
+
list.push(index);
|
|
338
|
+
endGrid.set(key, list);
|
|
339
|
+
});
|
|
340
|
+
const cosine = Math.cos(limits.angle * Math.PI / 180);
|
|
341
|
+
const best = open.map((end, a) => {
|
|
342
|
+
let found = -1;
|
|
343
|
+
let distance = 1e400;
|
|
344
|
+
const cx = Math.floor(end.x / span);
|
|
345
|
+
const cy = Math.floor(end.y / span);
|
|
346
|
+
for (let dx = -1; dx <= 1; dx++) for (let dy = -1; dy <= 1; dy++) for (const b of endGrid.get(`${cx + dx},${cy + dy}`) ?? []) {
|
|
347
|
+
const other = open[b];
|
|
348
|
+
if (a === b || other.line === end.line) continue;
|
|
349
|
+
const gx = other.x - end.x;
|
|
350
|
+
const gy = other.y - end.y;
|
|
351
|
+
const gap = Math.hypot(gx, gy);
|
|
352
|
+
if (gap < limits.narrowest || gap > limits.widest) continue;
|
|
353
|
+
if ((gx * end.dx + gy * end.dy) / gap < cosine) continue;
|
|
354
|
+
if (-(other.dx * end.dx + other.dy * end.dy) < cosine) continue;
|
|
355
|
+
if (gap < distance || gap === distance && b < found) {
|
|
356
|
+
distance = gap;
|
|
357
|
+
found = b;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return found;
|
|
361
|
+
});
|
|
362
|
+
best.forEach((b, a) => {
|
|
363
|
+
if (b < 0 || best[b] !== a || a > b) return;
|
|
364
|
+
const end = open[a];
|
|
365
|
+
const other = open[b];
|
|
366
|
+
const line = lines[end.line];
|
|
367
|
+
bridges.push({
|
|
368
|
+
source: "open",
|
|
369
|
+
from: [end.x, end.y],
|
|
370
|
+
to: [other.x, other.y],
|
|
371
|
+
thickness: void 0,
|
|
372
|
+
lines: [{
|
|
373
|
+
from: [end.x, end.y],
|
|
374
|
+
to: [other.x, other.y],
|
|
375
|
+
width: line.width,
|
|
376
|
+
color: line.color
|
|
377
|
+
}]
|
|
378
|
+
});
|
|
379
|
+
});
|
|
380
|
+
return bridges;
|
|
381
|
+
}
|
|
382
|
+
// src/plan-import/raster.ts
|
|
383
|
+
/** This function gives a grid that covers a sheet. */
|
|
384
|
+
function gridOf(width, height, cell) {
|
|
385
|
+
return {
|
|
386
|
+
columns: Math.max(1, Math.ceil(width / cell)),
|
|
387
|
+
rows: Math.max(1, Math.ceil(height / cell)),
|
|
388
|
+
cell
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
/** This function sets each cell under a line to 1. A line covers a square of cells around each point, with the half width of its pen, or half a cell or more. */
|
|
392
|
+
function stampLines(grid, barrier, lines) {
|
|
393
|
+
const { columns, rows, cell } = grid;
|
|
394
|
+
for (const line of lines) {
|
|
395
|
+
const half = Math.max(.5, line.width / 2 / cell);
|
|
396
|
+
const reach = Math.ceil(half - .5);
|
|
397
|
+
const length = Math.hypot(line.to[0] - line.from[0], line.to[1] - line.from[1]) / cell;
|
|
398
|
+
const steps = Math.max(1, Math.ceil(length * 2));
|
|
399
|
+
for (let k = 0; k <= steps; k++) {
|
|
400
|
+
const x = Math.floor((line.from[0] + (line.to[0] - line.from[0]) * k / steps) / cell);
|
|
401
|
+
const y = Math.floor((line.from[1] + (line.to[1] - line.from[1]) * k / steps) / cell);
|
|
402
|
+
for (let dy = -reach; dy <= reach; dy++) for (let dx = -reach; dx <= reach; dx++) {
|
|
403
|
+
const xx = x + dx;
|
|
404
|
+
const yy = y + dy;
|
|
405
|
+
if (xx >= 0 && yy >= 0 && xx < columns && yy < rows) barrier[yy * columns + xx] = 1;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
/** This function labels the free regions of a grid, with side neighbors only. The labels go in the sequence of the first cell of each region. */
|
|
411
|
+
function labelRegions(grid, barrier) {
|
|
412
|
+
const { columns, rows } = grid;
|
|
413
|
+
const size = columns * rows;
|
|
414
|
+
const labels = new Int32Array(size);
|
|
415
|
+
const queue = new Int32Array(size);
|
|
416
|
+
const regions = [];
|
|
417
|
+
for (let start = 0; start < size; start++) {
|
|
418
|
+
if (barrier[start] !== 0 || labels[start] !== 0) continue;
|
|
419
|
+
const label = regions.length + 1;
|
|
420
|
+
let head = 0;
|
|
421
|
+
let tail = 0;
|
|
422
|
+
queue[tail++] = start;
|
|
423
|
+
labels[start] = label;
|
|
424
|
+
let cells = 0;
|
|
425
|
+
let sumX = 0;
|
|
426
|
+
let sumY = 0;
|
|
427
|
+
let border = false;
|
|
428
|
+
let perimeter = 0;
|
|
429
|
+
while (head < tail) {
|
|
430
|
+
const index = queue[head++];
|
|
431
|
+
const x = index % columns;
|
|
432
|
+
const y = (index - x) / columns;
|
|
433
|
+
cells++;
|
|
434
|
+
sumX += x;
|
|
435
|
+
sumY += y;
|
|
436
|
+
if (x === 0 || y === 0 || x === columns - 1 || y === rows - 1) border = true;
|
|
437
|
+
const near = [
|
|
438
|
+
x > 0 ? index - 1 : -1,
|
|
439
|
+
x < columns - 1 ? index + 1 : -1,
|
|
440
|
+
y > 0 ? index - columns : -1,
|
|
441
|
+
y < rows - 1 ? index + columns : -1
|
|
442
|
+
];
|
|
443
|
+
for (const next of near) {
|
|
444
|
+
if (next < 0) continue;
|
|
445
|
+
if (barrier[next] !== 0) {
|
|
446
|
+
perimeter++;
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
if (labels[next] === 0) {
|
|
450
|
+
labels[next] = label;
|
|
451
|
+
queue[tail++] = next;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
regions.push({
|
|
456
|
+
label,
|
|
457
|
+
first: start,
|
|
458
|
+
cells,
|
|
459
|
+
center: [sumX / cells, sumY / cells],
|
|
460
|
+
border,
|
|
461
|
+
perimeter
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
return {
|
|
465
|
+
labels,
|
|
466
|
+
regions
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* This function gives each cell with its center inside a set of rings to `visit`, by the even-odd rule over all the rings.
|
|
471
|
+
* It goes row by row.
|
|
472
|
+
*/
|
|
473
|
+
function scanRings(grid, rings, visit) {
|
|
474
|
+
const { columns, rows, cell } = grid;
|
|
475
|
+
let top = 1e400;
|
|
476
|
+
let bottom = -1e400;
|
|
477
|
+
for (const ring of rings) for (const [, y] of ring) {
|
|
478
|
+
top = Math.min(top, y);
|
|
479
|
+
bottom = Math.max(bottom, y);
|
|
480
|
+
}
|
|
481
|
+
if (!(bottom >= top)) return;
|
|
482
|
+
const firstRow = Math.max(0, Math.floor(top / cell));
|
|
483
|
+
const lastRow = Math.min(rows - 1, Math.ceil(bottom / cell));
|
|
484
|
+
for (let y = firstRow; y <= lastRow; y++) {
|
|
485
|
+
const py = (y + .5) * cell;
|
|
486
|
+
const xs = [];
|
|
487
|
+
for (const ring of rings) for (let i = 0; i < ring.length; i++) {
|
|
488
|
+
const [ax, ay] = ring[i];
|
|
489
|
+
const [bx, by] = ring[(i + 1) % ring.length];
|
|
490
|
+
if (ay <= py !== by <= py) xs.push(ax + (py - ay) * (bx - ax) / (by - ay));
|
|
491
|
+
}
|
|
492
|
+
xs.sort((p, q) => p - q);
|
|
493
|
+
for (let k = 0; k + 1 < xs.length; k += 2) {
|
|
494
|
+
const first = Math.max(0, Math.ceil(xs[k] / cell - .5));
|
|
495
|
+
const last = Math.min(columns - 1, Math.floor(xs[k + 1] / cell - .5));
|
|
496
|
+
if (last >= first) visit(y, first, last);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
/** This function gives the number of cells of each label with the center inside one ring. */
|
|
501
|
+
function ringCells(grid, labels, ring) {
|
|
502
|
+
const counts = /* @__PURE__ */ new Map();
|
|
503
|
+
scanRings(grid, [ring], (row, first, last) => {
|
|
504
|
+
for (let x = first; x <= last; x++) {
|
|
505
|
+
const label = labels[row * grid.columns + x];
|
|
506
|
+
if (label !== 0) counts.set(label, (counts.get(label) ?? 0) + 1);
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
return counts;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* This function gives the outline of one region: the cell sides between the region and the other cells, as one ring.
|
|
513
|
+
* The ring goes with the region on its right side in the coordinates of the grid. Thus in a plan with the y axis up, it goes counter-clockwise.
|
|
514
|
+
* A hole of the region is not in the ring. The ring has a corner only where its direction changes.
|
|
515
|
+
*/
|
|
516
|
+
function regionOutline(grid, labels, region) {
|
|
517
|
+
const { columns, rows, cell } = grid;
|
|
518
|
+
const inside = (x, y) => x >= 0 && y >= 0 && x < columns && y < rows && labels[y * columns + x] === region.label;
|
|
519
|
+
const startX = region.first % columns;
|
|
520
|
+
const startY = (region.first - startX) / columns;
|
|
521
|
+
let x = startX;
|
|
522
|
+
let y = startY;
|
|
523
|
+
let dx = 1;
|
|
524
|
+
let dy = 0;
|
|
525
|
+
const corners = [[x * cell, y * cell]];
|
|
526
|
+
const limit = 4 * (columns + 1) * (rows + 1);
|
|
527
|
+
for (let step = 0; step < limit; step++) {
|
|
528
|
+
x += dx;
|
|
529
|
+
y += dy;
|
|
530
|
+
const left = inside(x + Math.floor((dx + dy) / 2), y + Math.floor((dy - dx) / 2));
|
|
531
|
+
const right = inside(x + Math.floor((dx - dy) / 2), y + Math.floor((dy + dx) / 2));
|
|
532
|
+
let nx = dx;
|
|
533
|
+
let ny = dy;
|
|
534
|
+
if (!right) {
|
|
535
|
+
nx = -dy;
|
|
536
|
+
ny = dx;
|
|
537
|
+
} else if (left) {
|
|
538
|
+
nx = dy;
|
|
539
|
+
ny = -dx;
|
|
540
|
+
}
|
|
541
|
+
if (x === startX && y === startY && nx === 1 && ny === 0) break;
|
|
542
|
+
if (nx !== dx || ny !== dy) corners.push([x * cell, y * cell]);
|
|
543
|
+
dx = nx;
|
|
544
|
+
dy = ny;
|
|
545
|
+
}
|
|
546
|
+
return corners;
|
|
547
|
+
}
|
|
548
|
+
// src/plan-import/importer.ts
|
|
549
|
+
/** The name of the side of an opening that is outside the building. */
|
|
550
|
+
const OUTSIDE = "outside";
|
|
551
|
+
/** The kinds of the free regions of the grid. */
|
|
552
|
+
const REGION_KINDS = [
|
|
553
|
+
"outside",
|
|
554
|
+
"speck",
|
|
555
|
+
"core",
|
|
556
|
+
"room"
|
|
557
|
+
];
|
|
558
|
+
/** The fill colors of a grid: a color number for each cell, or `NO_COLOR`. */
|
|
559
|
+
const NO_COLOR = 65535;
|
|
560
|
+
/**
|
|
561
|
+
* This function turns the vector paths of a floor plan into a room record with rooms, walls and openings.
|
|
562
|
+
*
|
|
563
|
+
* 1. A wall pen has long lines with a parallel partner at the distance of a wall thickness. Two such lines give a wall.
|
|
564
|
+
* 2. A bridge closes each gap between two wall ends. The bridge is an opening of the room record.
|
|
565
|
+
* 3. The walls, the door pens and the bridges go on a grid. Each free region of the grid that does not touch the border is a room, a speck or the space in a wall.
|
|
566
|
+
* 4. A room gets the fill color under most of its cells, and its use from the legend.
|
|
567
|
+
* 5. The area gate compares each fill ring of the legend with the rooms that have most of their cells in it.
|
|
568
|
+
*
|
|
569
|
+
* The coordinates of the sheet have the y axis down. The plan of the result has the y axis up, with the bottom left corner of the sheet at the origin.
|
|
570
|
+
*/
|
|
571
|
+
function importPlan(sheet, options = {}) {
|
|
572
|
+
const scale = sheet.scale;
|
|
573
|
+
const feet = unitsPer("ft", sheet.unit);
|
|
574
|
+
const sheetLength = (value, defaultFeet) => (value ?? defaultFeet * feet) * scale;
|
|
575
|
+
const areaUnit = (cells, cell) => cells * cell * cell / (scale * scale);
|
|
576
|
+
const toPlan = (point) => [tidy(point[0] / scale), tidy((sheet.height - point[1]) / scale)];
|
|
577
|
+
const planRing = (ring) => ring.map((point) => toPlan(point));
|
|
578
|
+
const wallOptions = options.walls ?? {};
|
|
579
|
+
const wallLimits = {
|
|
580
|
+
thinnest: sheetLength(wallOptions.thinnest, .25),
|
|
581
|
+
thickest: sheetLength(wallOptions.thickest, 1.5),
|
|
582
|
+
longLine: sheetLength(wallOptions.longLine, 1.5),
|
|
583
|
+
fewestLines: wallOptions.fewestLines ?? 8,
|
|
584
|
+
leastLength: sheetLength(wallOptions.leastLength, 40),
|
|
585
|
+
shortestWall: sheetLength(wallOptions.shortestWall, 1)
|
|
586
|
+
};
|
|
587
|
+
const bridgeOptions = options.bridges ?? {};
|
|
588
|
+
const bridgeLimits = {
|
|
589
|
+
narrowest: sheetLength(bridgeOptions.narrowest, 1.5),
|
|
590
|
+
widest: sheetLength(bridgeOptions.widest, 4.5),
|
|
591
|
+
shortestEnd: sheetLength(bridgeOptions.shortestEnd, .2),
|
|
592
|
+
longestEnd: sheetLength(bridgeOptions.longestEnd, 1.6),
|
|
593
|
+
touch: sheetLength(bridgeOptions.touch, .225),
|
|
594
|
+
angle: bridgeOptions.angle ?? 25
|
|
595
|
+
};
|
|
596
|
+
const pens = readPens(sheet.lines, wallLimits, scale);
|
|
597
|
+
const wallPens = options.wallPens ?? pens.filter((read) => read.wall).map((read) => read.pen);
|
|
598
|
+
const curves = /* @__PURE__ */ new Map();
|
|
599
|
+
for (const line of sheet.lines) if (line.curve === true) curves.set(penOf(line), (curves.get(penOf(line)) ?? 0) + 1);
|
|
600
|
+
const swing = [...curves].sort((a, b) => b[1] - a[1])[0];
|
|
601
|
+
const doorPens = options.doorPens ?? (swing === void 0 ? [] : [swing[0]]);
|
|
602
|
+
const wallSet = new Set(wallPens);
|
|
603
|
+
const doorSet = new Set(doorPens);
|
|
604
|
+
const wallLines = sheet.lines.filter((line) => wallSet.has(penOf(line)));
|
|
605
|
+
const straight = wallLines.filter((line) => line.curve !== true);
|
|
606
|
+
const doorLines = sheet.lines.filter((line) => doorSet.has(penOf(line)));
|
|
607
|
+
const pairs = wallPairs(straight, wallLimits);
|
|
608
|
+
const bridges = findBridges(straight, bridgeLimits);
|
|
609
|
+
let cell = sheetLength(options.cell, .1);
|
|
610
|
+
const largest = options.largestGrid ?? 4e7;
|
|
611
|
+
const cellsOf = (side) => Math.ceil(sheet.width / side) * Math.ceil(sheet.height / side);
|
|
612
|
+
if (cellsOf(cell) > largest) cell *= Math.sqrt(cellsOf(cell) / largest) * 1.001;
|
|
613
|
+
const grid = gridOf(sheet.width, sheet.height, cell);
|
|
614
|
+
let barrier = new Uint8Array(grid.columns * grid.rows);
|
|
615
|
+
stampLines(grid, barrier, wallLines);
|
|
616
|
+
stampLines(grid, barrier, doorLines);
|
|
617
|
+
for (const bridge of bridges) stampLines(grid, barrier, bridge.lines);
|
|
618
|
+
const { labels, regions } = labelRegions(grid, barrier);
|
|
619
|
+
barrier = void 0;
|
|
620
|
+
const fills = sheet.fills ?? [];
|
|
621
|
+
const samples = new Set(options.legend?.samples ?? []);
|
|
622
|
+
const names = options.legend?.names ?? {};
|
|
623
|
+
const colors = [...new Set(fills.map((fill) => fill.color))];
|
|
624
|
+
const colorIndex = new Map(colors.map((color, index) => [color, index]));
|
|
625
|
+
const paint = new Uint16Array(grid.columns * grid.rows).fill(NO_COLOR);
|
|
626
|
+
fills.forEach((fill, index) => {
|
|
627
|
+
if (samples.has(index)) return;
|
|
628
|
+
const value = colorIndex.get(fill.color);
|
|
629
|
+
scanRings(grid, fill.rings, (row, first, last) => paint.fill(value, row * grid.columns + first, row * grid.columns + last + 1));
|
|
630
|
+
});
|
|
631
|
+
const counts = /* @__PURE__ */ new Map();
|
|
632
|
+
for (let index = 0; index < labels.length; index++) {
|
|
633
|
+
const label = labels[index];
|
|
634
|
+
const color = paint[index];
|
|
635
|
+
if (label === 0 || color === NO_COLOR) continue;
|
|
636
|
+
const key = label * colors.length + color;
|
|
637
|
+
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
638
|
+
}
|
|
639
|
+
const smallest = options.smallestRoom ?? 12 * feet * feet;
|
|
640
|
+
const thinnest = options.thinnestRoom ?? 1.6 * feet;
|
|
641
|
+
const kindOf = (region) => {
|
|
642
|
+
if (region.border) return "outside";
|
|
643
|
+
const area = areaUnit(region.cells, cell);
|
|
644
|
+
if (area < smallest) return "speck";
|
|
645
|
+
return 2 * area / (region.perimeter * cell / scale) < thinnest ? "core" : "room";
|
|
646
|
+
};
|
|
647
|
+
const kinds = regions.map((region) => kindOf(region));
|
|
648
|
+
const tally = {
|
|
649
|
+
outside: 0,
|
|
650
|
+
speck: 0,
|
|
651
|
+
core: 0,
|
|
652
|
+
room: 0
|
|
653
|
+
};
|
|
654
|
+
for (const kind of kinds) tally[kind]++;
|
|
655
|
+
const idOf = /* @__PURE__ */ new Map();
|
|
656
|
+
const rooms = [];
|
|
657
|
+
const floors = [];
|
|
658
|
+
regions.forEach((region, index) => {
|
|
659
|
+
if (kinds[index] !== "room") return;
|
|
660
|
+
const id = `room-${rooms.length + 1}`;
|
|
661
|
+
idOf.set(region.label, id);
|
|
662
|
+
let best = -1;
|
|
663
|
+
let most = 0;
|
|
664
|
+
for (let color = 0; color < colors.length; color++) {
|
|
665
|
+
const count = counts.get(region.label * colors.length + color) ?? 0;
|
|
666
|
+
if (count > most) {
|
|
667
|
+
most = count;
|
|
668
|
+
best = color;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
const color = best < 0 ? void 0 : colors[best];
|
|
672
|
+
const use = color === void 0 ? void 0 : names[color];
|
|
673
|
+
const center = toPlan([(region.center[0] + .5) * cell, (region.center[1] + .5) * cell]);
|
|
674
|
+
rooms.push({
|
|
675
|
+
id,
|
|
676
|
+
...use === void 0 ? {} : { use },
|
|
677
|
+
...color === void 0 ? {} : { color },
|
|
678
|
+
area: tidy(areaUnit(region.cells, cell)),
|
|
679
|
+
center
|
|
680
|
+
});
|
|
681
|
+
floors.push({
|
|
682
|
+
id,
|
|
683
|
+
...use === void 0 ? {} : { name: use },
|
|
684
|
+
ring: planRing(regionOutline(grid, labels, region))
|
|
685
|
+
});
|
|
686
|
+
});
|
|
687
|
+
const walls = pairs.map((pair, index) => ({
|
|
688
|
+
id: `wall-${index + 1}`,
|
|
689
|
+
from: toPlan(pair.from),
|
|
690
|
+
to: toPlan(pair.to),
|
|
691
|
+
thickness: tidy(pair.thickness / scale)
|
|
692
|
+
}));
|
|
693
|
+
const wallThickness = walls.length === 0 ? .5 * feet : tidy(median(walls.map((wall) => wall.thickness)));
|
|
694
|
+
const height = options.height ?? 8 * feet;
|
|
695
|
+
const openingHeight = options.openingHeight ?? 80 / 12 * feet;
|
|
696
|
+
const swings = doorLines.filter((line) => line.curve === true).map((line) => [(line.from[0] + line.to[0]) / 2, (line.from[1] + line.to[1]) / 2]);
|
|
697
|
+
const sideOf = (label) => {
|
|
698
|
+
if (label === 0) return void 0;
|
|
699
|
+
const kind = kinds[label - 1];
|
|
700
|
+
return kind === "outside" ? OUTSIDE : kind === "room" ? idOf.get(label) : void 0;
|
|
701
|
+
};
|
|
702
|
+
const openings = [];
|
|
703
|
+
const imported = [];
|
|
704
|
+
bridges.forEach((bridge, index) => {
|
|
705
|
+
const width = lineLength(bridge);
|
|
706
|
+
if (!(width > 0)) return;
|
|
707
|
+
const id = `opening-${index + 1}`;
|
|
708
|
+
const wall = `bridge-${index + 1}`;
|
|
709
|
+
const center = [(bridge.from[0] + bridge.to[0]) / 2, (bridge.from[1] + bridge.to[1]) / 2];
|
|
710
|
+
const along = [(bridge.to[0] - bridge.from[0]) / width, (bridge.to[1] - bridge.from[1]) / width];
|
|
711
|
+
const half = (bridge.thickness ?? 0) / 2;
|
|
712
|
+
const reach = width * 1.2 + half;
|
|
713
|
+
const kind = swings.some((point) => Math.hypot(point[0] - center[0], point[1] - center[1]) <= reach) ? "door" : "gap";
|
|
714
|
+
const sides = openingSides(grid, labels, center, along, width, half + 8 * feet * scale, sideOf);
|
|
715
|
+
walls.push({
|
|
716
|
+
id: wall,
|
|
717
|
+
from: toPlan(bridge.from),
|
|
718
|
+
to: toPlan(bridge.to),
|
|
719
|
+
...bridge.thickness === void 0 ? {} : { thickness: tidy(bridge.thickness / scale) }
|
|
720
|
+
});
|
|
721
|
+
openings.push({
|
|
722
|
+
id,
|
|
723
|
+
wall,
|
|
724
|
+
kind,
|
|
725
|
+
offset: 0,
|
|
726
|
+
width: tidy(width / scale),
|
|
727
|
+
height: openingHeight
|
|
728
|
+
});
|
|
729
|
+
imported.push({
|
|
730
|
+
id,
|
|
731
|
+
wall,
|
|
732
|
+
kind,
|
|
733
|
+
source: bridge.source,
|
|
734
|
+
...sides === void 0 ? {} : { sides }
|
|
735
|
+
});
|
|
736
|
+
});
|
|
737
|
+
const fits = [];
|
|
738
|
+
const ringArea = options.smallestRing ?? 15 * feet * feet;
|
|
739
|
+
const gateRings = [];
|
|
740
|
+
fills.forEach((fill, index) => {
|
|
741
|
+
const use = names[fill.color];
|
|
742
|
+
if (use === void 0 || samples.has(index)) return;
|
|
743
|
+
fill.rings.forEach((points, ring) => {
|
|
744
|
+
const area = regionArea({
|
|
745
|
+
kind: "polygon",
|
|
746
|
+
points
|
|
747
|
+
}) / (scale * scale);
|
|
748
|
+
if (area >= ringArea) gateRings.push({
|
|
749
|
+
fill: index,
|
|
750
|
+
ring,
|
|
751
|
+
use,
|
|
752
|
+
area,
|
|
753
|
+
points
|
|
754
|
+
});
|
|
755
|
+
});
|
|
756
|
+
});
|
|
757
|
+
const overlaps = gateRings.map((ring) => ringCells(grid, labels, ring.points));
|
|
758
|
+
const owners = /* @__PURE__ */ new Map();
|
|
759
|
+
regions.forEach((region, index) => {
|
|
760
|
+
if (kinds[index] !== "room") return;
|
|
761
|
+
let best = -1;
|
|
762
|
+
let most = 0;
|
|
763
|
+
overlaps.forEach((overlap, ring) => {
|
|
764
|
+
const count = overlap.get(region.label) ?? 0;
|
|
765
|
+
if (count > most) {
|
|
766
|
+
most = count;
|
|
767
|
+
best = ring;
|
|
768
|
+
}
|
|
769
|
+
});
|
|
770
|
+
if (best < 0 || most < .5 * region.cells) return;
|
|
771
|
+
const list = owners.get(best) ?? [];
|
|
772
|
+
list.push(idOf.get(region.label));
|
|
773
|
+
owners.set(best, list);
|
|
774
|
+
});
|
|
775
|
+
const areaOf = new Map(rooms.map((room) => [room.id, room.area]));
|
|
776
|
+
gateRings.forEach((ring, index) => {
|
|
777
|
+
const held = owners.get(index) ?? [];
|
|
778
|
+
const sum = held.reduce((total, id) => total + areaOf.get(id), 0);
|
|
779
|
+
fits.push({
|
|
780
|
+
fill: ring.fill,
|
|
781
|
+
ring: ring.ring,
|
|
782
|
+
use: ring.use,
|
|
783
|
+
area: tidy(ring.area),
|
|
784
|
+
rooms: held,
|
|
785
|
+
...held.length === 0 ? {} : { error: sum / ring.area - 1 }
|
|
786
|
+
});
|
|
787
|
+
});
|
|
788
|
+
const errors = fits.flatMap((fit) => fit.error === void 0 ? [] : [fit.error]);
|
|
789
|
+
const gate = {
|
|
790
|
+
rings: fits,
|
|
791
|
+
within: (options.gateLimits ?? [.05, .1]).map((limit) => ({
|
|
792
|
+
limit,
|
|
793
|
+
rings: errors.filter((error) => Math.abs(error) <= limit).length
|
|
794
|
+
})),
|
|
795
|
+
...errors.length === 0 ? {} : { median: median(errors) }
|
|
796
|
+
};
|
|
797
|
+
return {
|
|
798
|
+
room: {
|
|
799
|
+
schema: ROOM_SCHEMA,
|
|
800
|
+
unit: sheet.unit,
|
|
801
|
+
height,
|
|
802
|
+
wallThickness,
|
|
803
|
+
walls,
|
|
804
|
+
...openings.length === 0 ? {} : { openings },
|
|
805
|
+
...floors.length === 0 ? {} : { floors }
|
|
806
|
+
},
|
|
807
|
+
rooms,
|
|
808
|
+
openings: imported,
|
|
809
|
+
regions: tally,
|
|
810
|
+
pens,
|
|
811
|
+
wallPens,
|
|
812
|
+
doorPens,
|
|
813
|
+
grid: {
|
|
814
|
+
...grid,
|
|
815
|
+
cell: cell / scale
|
|
816
|
+
},
|
|
817
|
+
gate
|
|
818
|
+
};
|
|
819
|
+
}
|
|
820
|
+
/**
|
|
821
|
+
* This function finds the rooms on the two sides of an opening. It goes from the center across the wall in the two directions.
|
|
822
|
+
* It goes past the cells of a speck or of the space in a wall, to the first room or the outside.
|
|
823
|
+
* It tries the center, then two points along the opening.
|
|
824
|
+
*/
|
|
825
|
+
function openingSides(grid, labels, center, along, width, reach, sideOf) {
|
|
826
|
+
const { columns, rows, cell } = grid;
|
|
827
|
+
const across = [-along[1], along[0]];
|
|
828
|
+
const side = (sign, shift) => {
|
|
829
|
+
const ox = center[0] + along[0] * shift;
|
|
830
|
+
const oy = center[1] + along[1] * shift;
|
|
831
|
+
for (let t = 0; t <= reach; t += cell / 2) {
|
|
832
|
+
const x = Math.floor((ox + sign * across[0] * t) / cell);
|
|
833
|
+
const y = Math.floor((oy + sign * across[1] * t) / cell);
|
|
834
|
+
if (x < 0 || y < 0 || x >= columns || y >= rows) return void 0;
|
|
835
|
+
const found = sideOf(labels[y * columns + x]);
|
|
836
|
+
if (found !== void 0) return found;
|
|
837
|
+
}
|
|
838
|
+
};
|
|
839
|
+
for (const shift of [
|
|
840
|
+
0,
|
|
841
|
+
-.25,
|
|
842
|
+
.25
|
|
843
|
+
]) {
|
|
844
|
+
const a = side(-1, shift * width);
|
|
845
|
+
const b = side(1, shift * width);
|
|
846
|
+
if (a !== void 0 && b !== void 0) return a === b ? void 0 : [a, b];
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
/** This function puts a calculated length on a grid of 1e-9, so the noise of float arithmetic does not go to a file. */
|
|
850
|
+
function tidy(value) {
|
|
851
|
+
return Math.round(value * 1e9) / 1e9 + 0;
|
|
852
|
+
}
|
|
853
|
+
export { BRIDGE_SOURCES, OUTSIDE, REGION_KINDS, fillBox, findBridges, importPlan, lineLength, parallelPairs, penOf, readLegend, readPens, wallPairs };
|
|
854
|
+
|
|
855
|
+
//# sourceMappingURL=plan-import.js.map
|