@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.
- package/LICENSE +21 -0
- package/README.md +297 -2
- package/build/index.d.ts +83 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +391 -0
- package/build/index.js.map +1 -0
- package/build/level-84nrw7oL.d.ts +344 -0
- package/build/level-84nrw7oL.d.ts.map +1 -0
- package/build/level-xTO7P1Fn.js +757 -0
- package/build/level-xTO7P1Fn.js.map +1 -0
- package/build/loads-Br73kIsQ.js +667 -0
- package/build/loads-Br73kIsQ.js.map +1 -0
- package/build/loads-H-VFEx6b.d.ts +223 -0
- package/build/loads-H-VFEx6b.d.ts.map +1 -0
- package/build/room-BUlXejkm.d.ts +149 -0
- package/build/room-BUlXejkm.d.ts.map +1 -0
- package/build/sample.d.ts +28 -0
- package/build/sample.d.ts.map +1 -0
- package/build/sample.js +863 -0
- package/build/sample.js.map +1 -0
- package/build/schema.d.ts +24 -0
- package/build/schema.d.ts.map +1 -0
- package/build/schema.js +167 -0
- package/build/schema.js.map +1 -0
- package/build/three.d.ts +117 -0
- package/build/three.d.ts.map +1 -0
- package/build/three.js +1069 -0
- package/build/three.js.map +1 -0
- package/build/units-BZmvVyu4.js +30 -0
- package/build/units-BZmvVyu4.js.map +1 -0
- package/package.json +89 -3
|
@@ -0,0 +1,667 @@
|
|
|
1
|
+
import { i as metersPer, r as lengthsInRange, t as LARGEST_LENGTH } from "./units-BZmvVyu4.js";
|
|
2
|
+
import { boundsOf3, box3Corners, box3Volume, clipRing, composeTransforms3, regionArea, rotationAbout, transformPoint3 } from "@mark1russell7/layout-contract";
|
|
3
|
+
// src/frames.ts
|
|
4
|
+
/** This function gives the frame of one wall. `scale` is the length of the unit of the room in meters. */
|
|
5
|
+
function frameOf(room, wall, scale) {
|
|
6
|
+
const start = {
|
|
7
|
+
x: wall.from[0] * scale,
|
|
8
|
+
y: wall.from[1] * scale
|
|
9
|
+
};
|
|
10
|
+
const end = {
|
|
11
|
+
x: wall.to[0] * scale,
|
|
12
|
+
y: wall.to[1] * scale
|
|
13
|
+
};
|
|
14
|
+
const length = Math.hypot(end.x - start.x, end.y - start.y);
|
|
15
|
+
const along = length > 0 && Number.isFinite(length) ? {
|
|
16
|
+
x: (end.x - start.x) / length,
|
|
17
|
+
y: (end.y - start.y) / length
|
|
18
|
+
} : {
|
|
19
|
+
x: 1,
|
|
20
|
+
y: 0
|
|
21
|
+
};
|
|
22
|
+
const thickness = (wall.thickness ?? room.wallThickness) * scale;
|
|
23
|
+
const side = wall.side ?? "center";
|
|
24
|
+
const leftOffset = side === "left" ? thickness : side === "right" ? 0 : thickness / 2;
|
|
25
|
+
return {
|
|
26
|
+
id: wall.id,
|
|
27
|
+
start,
|
|
28
|
+
end,
|
|
29
|
+
along,
|
|
30
|
+
left: {
|
|
31
|
+
x: 0 - along.y,
|
|
32
|
+
y: along.x
|
|
33
|
+
},
|
|
34
|
+
length,
|
|
35
|
+
thickness,
|
|
36
|
+
height: (wall.height ?? room.height) * scale,
|
|
37
|
+
leftOffset,
|
|
38
|
+
rightOffset: leftOffset - thickness
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** This function gives the frame of each wall of a room, in the sequence of the walls. */
|
|
42
|
+
function wallFrames(room) {
|
|
43
|
+
const scale = metersPer(room.unit);
|
|
44
|
+
return room.walls.map((wall) => frameOf(room, wall, scale));
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* This function tells if a wall has an outline in the plan. Its ends and its thickness must be lengths in the range of the model,
|
|
48
|
+
* and its length and its thickness must be above 0. Only such a wall makes a corner, and the model makes only such a wall.
|
|
49
|
+
*/
|
|
50
|
+
function wallHasOutline(frame) {
|
|
51
|
+
return lengthsInRange(frame.start.x, frame.start.y, frame.end.x, frame.end.y, frame.thickness) && frame.length > 0 && frame.thickness > 0;
|
|
52
|
+
}
|
|
53
|
+
/** This function gives the point of the plan at a distance along a wall and a distance across it, in meters. */
|
|
54
|
+
function wallPoint(frame, along, across) {
|
|
55
|
+
return {
|
|
56
|
+
x: frame.start.x + frame.along.x * along + frame.left.x * across,
|
|
57
|
+
y: frame.start.y + frame.along.y * along + frame.left.y * across
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* This function gives the transform from the frame of a wall to the room.
|
|
62
|
+
* In the frame of a wall, x is the distance along the wall and y is the distance across it. The height above the floor is z.
|
|
63
|
+
*/
|
|
64
|
+
function wallTransform(frame) {
|
|
65
|
+
return {
|
|
66
|
+
position: {
|
|
67
|
+
x: frame.start.x,
|
|
68
|
+
y: frame.start.y,
|
|
69
|
+
z: 0
|
|
70
|
+
},
|
|
71
|
+
rotation: rotationAbout({
|
|
72
|
+
x: 0,
|
|
73
|
+
y: 0,
|
|
74
|
+
z: 1
|
|
75
|
+
}, Math.atan2(frame.along.y, frame.along.x))
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
// src/joins.ts
|
|
79
|
+
/** The default distance in meters at which two ends of walls make one corner. */
|
|
80
|
+
const JOIN_TOLERANCE = 1e-6;
|
|
81
|
+
/** The default limit of a sharp corner, in thicknesses. */
|
|
82
|
+
const MITER_LIMIT = 4;
|
|
83
|
+
/** Two unit vectors with a cross product below this value are parallel. */
|
|
84
|
+
const PARALLEL = 1e-9;
|
|
85
|
+
/** A corner of an outline with a turn of this value or less is flat, so the outline does not need it. */
|
|
86
|
+
const FLAT = 1e-12;
|
|
87
|
+
/** Two points at this distance or less are one point. A point at this distance or less from a face of a wall is on the face. */
|
|
88
|
+
const SAME = 1e-9;
|
|
89
|
+
/** This function gives an option, or its default when the option has no value or a value that is not finite. */
|
|
90
|
+
function finiteOr(value, fallback) {
|
|
91
|
+
return value !== void 0 && Number.isFinite(value) ? value : fallback;
|
|
92
|
+
}
|
|
93
|
+
/** This function gives the point at a distance across from the point of an end. */
|
|
94
|
+
function across(end, distance) {
|
|
95
|
+
return {
|
|
96
|
+
x: end.point.x + end.normal.x * distance,
|
|
97
|
+
y: end.point.y + end.normal.y * distance
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/** This function makes the two ends of a wall, with square points. */
|
|
101
|
+
function endsOf(frame) {
|
|
102
|
+
const back = {
|
|
103
|
+
x: 0 - frame.along.x,
|
|
104
|
+
y: 0 - frame.along.y
|
|
105
|
+
};
|
|
106
|
+
const right = {
|
|
107
|
+
x: 0 - frame.left.x,
|
|
108
|
+
y: 0 - frame.left.y
|
|
109
|
+
};
|
|
110
|
+
const make = (point, out, normal, high, low) => {
|
|
111
|
+
const end = {
|
|
112
|
+
point,
|
|
113
|
+
out,
|
|
114
|
+
normal,
|
|
115
|
+
high,
|
|
116
|
+
low,
|
|
117
|
+
thickness: frame.thickness,
|
|
118
|
+
highPoint: point,
|
|
119
|
+
lowPoint: point,
|
|
120
|
+
joined: false
|
|
121
|
+
};
|
|
122
|
+
end.highPoint = across(end, high);
|
|
123
|
+
end.lowPoint = across(end, low);
|
|
124
|
+
return end;
|
|
125
|
+
};
|
|
126
|
+
return [make(frame.start, frame.along, frame.left, frame.leftOffset, frame.rightOffset), make(frame.end, back, right, 0 - frame.rightOffset, 0 - frame.leftOffset)];
|
|
127
|
+
}
|
|
128
|
+
/** This function gives the point where two lines meet, or `undefined` for parallel lines. Each line has a point and a unit direction. */
|
|
129
|
+
function meet(p, d, q, e) {
|
|
130
|
+
const cross = d.x * e.y - d.y * e.x;
|
|
131
|
+
if (Math.abs(cross) < PARALLEL) return void 0;
|
|
132
|
+
const t = ((q.x - p.x) * e.y - (q.y - p.y) * e.x) / cross;
|
|
133
|
+
return {
|
|
134
|
+
x: p.x + d.x * t,
|
|
135
|
+
y: p.y + d.y * t
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* This function makes the corner of the ends at one point. The ends are in sequence counter-clockwise.
|
|
140
|
+
* The left face of each end meets the right face of the next end.
|
|
141
|
+
*/
|
|
142
|
+
function joinEnds(ends, miterLimit) {
|
|
143
|
+
if (ends.length < 2) return;
|
|
144
|
+
for (let i = 0; i < ends.length; i++) {
|
|
145
|
+
const end = ends[i];
|
|
146
|
+
const next = ends[(i + 1) % ends.length];
|
|
147
|
+
end.joined = true;
|
|
148
|
+
const corner = meet(across(end, end.high), end.out, across(next, next.low), next.out);
|
|
149
|
+
if (corner === void 0) continue;
|
|
150
|
+
if (Math.hypot(corner.x - end.point.x, corner.y - end.point.y) > miterLimit * Math.max(end.thickness, next.thickness)) continue;
|
|
151
|
+
end.highPoint = corner;
|
|
152
|
+
next.lowPoint = corner;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/** This function gives the part of a ring to the left of the line from `p` to `q`. Two points at one position give the full ring. */
|
|
156
|
+
function leftOf(ring, p, q) {
|
|
157
|
+
const length = Math.hypot(q.x - p.x, q.y - p.y);
|
|
158
|
+
if (length <= SAME) return ring;
|
|
159
|
+
const dx = (q.x - p.x) / length;
|
|
160
|
+
const dy = (q.y - p.y) / length;
|
|
161
|
+
return clipRing(ring, dy, 0 - dx, dy * p.x - dx * p.y);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* This function cuts the outline of a wall at one end. An end with no corner gets a square cut.
|
|
165
|
+
* An end with a corner keeps the part between the two lines from the point of the corner to the points of the two faces.
|
|
166
|
+
*/
|
|
167
|
+
function cut(ring, end) {
|
|
168
|
+
if (!end.joined) return leftOf(ring, end.highPoint, end.lowPoint);
|
|
169
|
+
return leftOf(leftOf(ring, end.highPoint, end.point), end.point, end.lowPoint);
|
|
170
|
+
}
|
|
171
|
+
/** This function removes each corner of a convex outline that is flat or that is a copy of the corner before it. Fewer than 3 corners give an empty outline. */
|
|
172
|
+
function withoutFlat(points) {
|
|
173
|
+
const ring = [...points];
|
|
174
|
+
let changed = true;
|
|
175
|
+
while (changed && ring.length >= 3) {
|
|
176
|
+
changed = false;
|
|
177
|
+
for (let i = 0; i < ring.length && ring.length >= 3; i++) {
|
|
178
|
+
const [ax, ay] = ring[(i + ring.length - 1) % ring.length];
|
|
179
|
+
const [bx, by] = ring[i];
|
|
180
|
+
const [cx, cy] = ring[(i + 1) % ring.length];
|
|
181
|
+
if ((bx - ax) * (cy - by) - (by - ay) * (cx - bx) > FLAT) continue;
|
|
182
|
+
ring.splice(i, 1);
|
|
183
|
+
changed = true;
|
|
184
|
+
i--;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return ring.length >= 3 ? ring : [];
|
|
188
|
+
}
|
|
189
|
+
/** This function gives the range of the distances along a wall of the corners of an outline that are on one face. */
|
|
190
|
+
function faceRange(frame, ring, offset) {
|
|
191
|
+
let from = 1e400;
|
|
192
|
+
let to = -1e400;
|
|
193
|
+
for (const [x, y] of ring) {
|
|
194
|
+
const dx = x - frame.start.x;
|
|
195
|
+
const dy = y - frame.start.y;
|
|
196
|
+
if (Math.abs(dx * frame.left.x + dy * frame.left.y - offset) > SAME) continue;
|
|
197
|
+
const along = dx * frame.along.x + dy * frame.along.y;
|
|
198
|
+
from = Math.min(from, along);
|
|
199
|
+
to = Math.max(to, along);
|
|
200
|
+
}
|
|
201
|
+
return [from, to];
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* This function gives the outline of each wall in the plan, in the sequence of the frames.
|
|
205
|
+
*
|
|
206
|
+
* Two or more ends of walls at one point make a corner. At a corner, the faces of the walls that are adjacent meet at a point.
|
|
207
|
+
* Each wall ends at the lines from the corner to those points. Thus two walls make a miter, and the walls of a corner do not overlap.
|
|
208
|
+
* An end with no other end at its point is square. An end on the face of a different wall is also square: the two walls touch.
|
|
209
|
+
* A wall with no outline (`wallHasOutline`) gets an empty outline, and it makes no corner.
|
|
210
|
+
*/
|
|
211
|
+
function wallFootprints(frames, options = {}) {
|
|
212
|
+
const tolerance = finiteOr(options.tolerance, JOIN_TOLERANCE);
|
|
213
|
+
const miterLimit = Math.max(finiteOr(options.miterLimit, 4), 0);
|
|
214
|
+
const pairs = frames.map((frame) => wallHasOutline(frame) ? endsOf(frame) : void 0);
|
|
215
|
+
const corners = [];
|
|
216
|
+
for (const pair of pairs) for (const end of pair ?? []) {
|
|
217
|
+
const corner = corners.find((ends) => Math.hypot(ends[0].point.x - end.point.x, ends[0].point.y - end.point.y) <= tolerance);
|
|
218
|
+
if (corner === void 0) corners.push([end]);
|
|
219
|
+
else corner.push(end);
|
|
220
|
+
}
|
|
221
|
+
for (const ends of corners) {
|
|
222
|
+
ends.sort((a, b) => Math.atan2(a.out.y, a.out.x) - Math.atan2(b.out.y, b.out.x));
|
|
223
|
+
joinEnds(ends, miterLimit);
|
|
224
|
+
}
|
|
225
|
+
const thickest = frames.reduce((most, frame, index) => pairs[index] === void 0 ? most : Math.max(most, frame.thickness), 0);
|
|
226
|
+
const reach = (miterLimit + 1) * thickest;
|
|
227
|
+
return frames.map((frame, index) => {
|
|
228
|
+
const pair = pairs[index];
|
|
229
|
+
if (pair === void 0) return {
|
|
230
|
+
wall: frame.id,
|
|
231
|
+
ring: [],
|
|
232
|
+
clear: [0, 0]
|
|
233
|
+
};
|
|
234
|
+
const ring = withoutFlat(cut(cut([
|
|
235
|
+
wallPoint(frame, 0 - reach, frame.rightOffset),
|
|
236
|
+
wallPoint(frame, frame.length + reach, frame.rightOffset),
|
|
237
|
+
wallPoint(frame, frame.length + reach, frame.leftOffset),
|
|
238
|
+
wallPoint(frame, 0 - reach, frame.leftOffset)
|
|
239
|
+
].map((point) => [point.x, point.y]), pair[0]), pair[1]));
|
|
240
|
+
if (!ring.every(([x, y]) => Number.isFinite(x) && Number.isFinite(y))) return {
|
|
241
|
+
wall: frame.id,
|
|
242
|
+
ring: [],
|
|
243
|
+
clear: [0, 0]
|
|
244
|
+
};
|
|
245
|
+
const leftFace = faceRange(frame, ring, frame.leftOffset);
|
|
246
|
+
const rightFace = faceRange(frame, ring, frame.rightOffset);
|
|
247
|
+
const from = Math.max(leftFace[0], rightFace[0]);
|
|
248
|
+
const to = Math.min(leftFace[1], rightFace[1]);
|
|
249
|
+
return {
|
|
250
|
+
wall: frame.id,
|
|
251
|
+
ring,
|
|
252
|
+
clear: from < to ? [from, to] : [0, 0]
|
|
253
|
+
};
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
// src/solids.ts
|
|
257
|
+
/** These are the kinds of a solid. */
|
|
258
|
+
const SOLID_KINDS = [
|
|
259
|
+
"floor",
|
|
260
|
+
"wall",
|
|
261
|
+
"glass",
|
|
262
|
+
"outlet",
|
|
263
|
+
"object"
|
|
264
|
+
];
|
|
265
|
+
/** The default depth of the floor, in meters. */
|
|
266
|
+
const FLOOR_DEPTH = .1;
|
|
267
|
+
/** The thickness of the glass of a window, in meters. */
|
|
268
|
+
const GLASS_THICKNESS = .02;
|
|
269
|
+
/** The size of the plate of an outlet, in meters. The plate of a duplex outlet in North America is 70 mm by 115 mm. */
|
|
270
|
+
const OUTLET_PLATE = {
|
|
271
|
+
width: .07,
|
|
272
|
+
height: .115,
|
|
273
|
+
depth: .012
|
|
274
|
+
};
|
|
275
|
+
/**
|
|
276
|
+
* The transform from the room, where the z axis points up, to a space where the y axis points up.
|
|
277
|
+
* The glTF format and three.js have the y axis up. The x axis stays, and the y axis of the plan becomes the negative z axis.
|
|
278
|
+
*/
|
|
279
|
+
const TO_Y_UP = {
|
|
280
|
+
position: {
|
|
281
|
+
x: 0,
|
|
282
|
+
y: 0,
|
|
283
|
+
z: 0
|
|
284
|
+
},
|
|
285
|
+
rotation: {
|
|
286
|
+
x: -Math.SQRT1_2,
|
|
287
|
+
y: 0,
|
|
288
|
+
z: 0,
|
|
289
|
+
w: Math.SQRT1_2
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
/** A length in meters at this value or less is empty: the model does not make a part of this size. */
|
|
293
|
+
const EMPTY = 1e-9;
|
|
294
|
+
/** An area in square meters at this value or less is empty. */
|
|
295
|
+
const EMPTY_AREA = 1e-12;
|
|
296
|
+
/** This function gives an outline, or an empty ring when the outline has no area or a number that is not finite. */
|
|
297
|
+
function solidRing(ring) {
|
|
298
|
+
if (ring.length < 3 || !ring.every(([x, y]) => Number.isFinite(x) && Number.isFinite(y))) return [];
|
|
299
|
+
return regionArea({
|
|
300
|
+
kind: "polygon",
|
|
301
|
+
points: ring
|
|
302
|
+
}) > EMPTY_AREA ? ring : [];
|
|
303
|
+
}
|
|
304
|
+
/** This function tells if the model makes a wall. Such a wall has an outline in the plan, and its height is above 0 and in the range of the model. */
|
|
305
|
+
function isMade(frame) {
|
|
306
|
+
return wallHasOutline(frame) && frame.height > 0 && lengthsInRange(frame.height);
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* This function gives the openings of one wall that the model can make, in sequence along the wall.
|
|
310
|
+
* It skips an opening with no size and an opening out of the straight part of the wall. It also skips an opening that overlaps the opening before it.
|
|
311
|
+
*/
|
|
312
|
+
function holesOf(room, frame, footprint, scale) {
|
|
313
|
+
const [clearFrom, clearTo] = footprint.clear;
|
|
314
|
+
const holes = (room.openings ?? []).filter((opening) => opening.wall === frame.id).map((opening) => {
|
|
315
|
+
const sill = (opening.sill ?? 0) * scale;
|
|
316
|
+
return {
|
|
317
|
+
id: opening.id,
|
|
318
|
+
kind: opening.kind,
|
|
319
|
+
from: opening.offset * scale,
|
|
320
|
+
to: (opening.offset + opening.width) * scale,
|
|
321
|
+
sill,
|
|
322
|
+
top: Math.min(sill + opening.height * scale, frame.height)
|
|
323
|
+
};
|
|
324
|
+
}).filter((hole) => hole.to - hole.from > EMPTY && hole.sill >= 0 && hole.top - hole.sill > EMPTY && hole.from >= clearFrom - EMPTY && hole.to <= clearTo + EMPTY).sort((a, b) => a.from - b.from);
|
|
325
|
+
const kept = [];
|
|
326
|
+
let cursor = -1e400;
|
|
327
|
+
for (const hole of holes) {
|
|
328
|
+
if (hole.from < cursor - EMPTY) continue;
|
|
329
|
+
kept.push(hole);
|
|
330
|
+
cursor = hole.to;
|
|
331
|
+
}
|
|
332
|
+
return kept;
|
|
333
|
+
}
|
|
334
|
+
/** This function removes each corner of a ring that is at the position of the corner before it. */
|
|
335
|
+
function withoutRepeats(ring) {
|
|
336
|
+
return ring.filter(([x, y], index) => {
|
|
337
|
+
const [px, py] = ring[(index + ring.length - 1) % ring.length];
|
|
338
|
+
return ring.length === 1 || Math.hypot(x - px, y - py) > EMPTY;
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
/** This function gives the part of the outline of a wall between two distances along the wall. An empty part gives an empty ring. */
|
|
342
|
+
function slice(frame, ring, from, to) {
|
|
343
|
+
const base = frame.start.x * frame.along.x + frame.start.y * frame.along.y;
|
|
344
|
+
let part = ring;
|
|
345
|
+
if (Number.isFinite(from)) part = clipRing(part, 0 - frame.along.x, 0 - frame.along.y, 0 - (base + from));
|
|
346
|
+
if (Number.isFinite(to)) part = clipRing(part, frame.along.x, frame.along.y, base + to);
|
|
347
|
+
return solidRing(withoutRepeats(part));
|
|
348
|
+
}
|
|
349
|
+
/** This function gives the solids of one wall: the parts between, below and above its openings, and the glass of each window. */
|
|
350
|
+
function wallSolids(room, frame, footprint, scale, depth) {
|
|
351
|
+
if (footprint.ring.length < 3 || !isMade(frame)) return [];
|
|
352
|
+
const walls = [];
|
|
353
|
+
const glass = [];
|
|
354
|
+
const add = (ring, z, top) => {
|
|
355
|
+
if (ring.length < 3 || top - z <= EMPTY) return;
|
|
356
|
+
walls.push({
|
|
357
|
+
id: `wall:${frame.id}:${String(walls.length)}`,
|
|
358
|
+
kind: "wall",
|
|
359
|
+
source: frame.id,
|
|
360
|
+
shape: {
|
|
361
|
+
kind: "prism",
|
|
362
|
+
ring,
|
|
363
|
+
z,
|
|
364
|
+
d: top - z
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
};
|
|
368
|
+
let cursor = -1e400;
|
|
369
|
+
for (const hole of holesOf(room, frame, footprint, scale)) {
|
|
370
|
+
add(slice(frame, footprint.ring, cursor, hole.from), 0 - depth, frame.height);
|
|
371
|
+
const part = slice(frame, footprint.ring, hole.from, hole.to);
|
|
372
|
+
add(part, 0 - depth, hole.sill);
|
|
373
|
+
add(part, hole.top, frame.height);
|
|
374
|
+
cursor = hole.to;
|
|
375
|
+
if (hole.kind !== "window") continue;
|
|
376
|
+
glass.push({
|
|
377
|
+
id: `glass:${hole.id}`,
|
|
378
|
+
kind: "glass",
|
|
379
|
+
source: hole.id,
|
|
380
|
+
shape: {
|
|
381
|
+
kind: "box",
|
|
382
|
+
box: {
|
|
383
|
+
x: hole.from,
|
|
384
|
+
y: (frame.leftOffset + frame.rightOffset - GLASS_THICKNESS) / 2,
|
|
385
|
+
z: hole.sill,
|
|
386
|
+
w: hole.to - hole.from,
|
|
387
|
+
h: GLASS_THICKNESS,
|
|
388
|
+
d: hole.top - hole.sill
|
|
389
|
+
},
|
|
390
|
+
transform: wallTransform(frame)
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
add(slice(frame, footprint.ring, cursor, 1e400), 0 - depth, frame.height);
|
|
395
|
+
return [...walls, ...glass];
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* This function gives the 3D model of a room as a list of solids.
|
|
399
|
+
* The solids are the floors, the walls with their openings, the glass of the windows and the outlets.
|
|
400
|
+
*
|
|
401
|
+
* The function does not stop for a record with a fault. It skips each part that it cannot make.
|
|
402
|
+
* One example is a wall with no length. A second example is an opening on a wall that the room does not have.
|
|
403
|
+
* A third example is a part with a number that is not finite or not in the range of the model (`LARGEST_LENGTH`). `checkRoom` tells each fault.
|
|
404
|
+
*/
|
|
405
|
+
function roomSolids(room, options = {}) {
|
|
406
|
+
const scale = metersPer(room.unit);
|
|
407
|
+
const given = options.floorDepth ?? .1;
|
|
408
|
+
const depth = Number.isFinite(given) ? Math.min(Math.max(given, 0), LARGEST_LENGTH) : FLOOR_DEPTH;
|
|
409
|
+
const frames = wallFrames(room);
|
|
410
|
+
const footprints = wallFootprints(frames, options.joins);
|
|
411
|
+
const solids = [];
|
|
412
|
+
for (const floor of room.floors ?? []) {
|
|
413
|
+
const ring = solidRing(floor.ring.map(([x, y]) => [x * scale, y * scale]));
|
|
414
|
+
if (ring.length < 3 || depth <= EMPTY || !ring.every(([x, y]) => lengthsInRange(x, y))) continue;
|
|
415
|
+
solids.push({
|
|
416
|
+
id: `floor:${floor.id}`,
|
|
417
|
+
kind: "floor",
|
|
418
|
+
source: floor.id,
|
|
419
|
+
shape: {
|
|
420
|
+
kind: "prism",
|
|
421
|
+
ring,
|
|
422
|
+
z: 0 - depth,
|
|
423
|
+
d: depth
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
frames.forEach((frame, index) => {
|
|
428
|
+
solids.push(...wallSolids(room, frame, footprints[index], scale, depth));
|
|
429
|
+
});
|
|
430
|
+
const byId = new Map(frames.map((frame) => [frame.id, frame]));
|
|
431
|
+
for (const outlet of room.outlets ?? []) {
|
|
432
|
+
const frame = byId.get(outlet.wall);
|
|
433
|
+
if (frame === void 0 || !isMade(frame)) continue;
|
|
434
|
+
const along = outlet.offset * scale;
|
|
435
|
+
const height = outlet.height * scale;
|
|
436
|
+
if (!lengthsInRange(along, height)) continue;
|
|
437
|
+
solids.push({
|
|
438
|
+
id: `outlet:${outlet.id}`,
|
|
439
|
+
kind: "outlet",
|
|
440
|
+
source: outlet.id,
|
|
441
|
+
shape: {
|
|
442
|
+
kind: "box",
|
|
443
|
+
box: {
|
|
444
|
+
x: along - OUTLET_PLATE.width / 2,
|
|
445
|
+
y: outlet.face === "left" ? frame.leftOffset : frame.rightOffset - OUTLET_PLATE.depth,
|
|
446
|
+
z: height - OUTLET_PLATE.height / 2,
|
|
447
|
+
w: OUTLET_PLATE.width,
|
|
448
|
+
h: OUTLET_PLATE.depth,
|
|
449
|
+
d: OUTLET_PLATE.height
|
|
450
|
+
},
|
|
451
|
+
transform: wallTransform(frame)
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
return solids;
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* This function gives the transform of each placed object of a scene, from the frame of the object to the room, in meters.
|
|
459
|
+
* The origin of the frame of an object is the center of its bottom. An object with a parent gets the transform of the parent first.
|
|
460
|
+
* An object has no transform when a parent is not in the scene, or when its parents make a loop.
|
|
461
|
+
* An object also has no transform when one value of its place is not finite or not in the range of the model.
|
|
462
|
+
*/
|
|
463
|
+
function objectTransforms(scene) {
|
|
464
|
+
const scale = metersPer(scene.room.unit);
|
|
465
|
+
const objects = new Map((scene.objects ?? []).map((object) => [object.id, object]));
|
|
466
|
+
const done = /* @__PURE__ */ new Map();
|
|
467
|
+
const open = /* @__PURE__ */ new Set();
|
|
468
|
+
const resolve = (object) => {
|
|
469
|
+
const known = done.get(object.id);
|
|
470
|
+
if (known !== void 0) return known;
|
|
471
|
+
if (open.has(object.id)) return void 0;
|
|
472
|
+
const turn = object.turn ?? 0;
|
|
473
|
+
const position = {
|
|
474
|
+
x: object.at[0] * scale,
|
|
475
|
+
y: object.at[1] * scale,
|
|
476
|
+
z: (object.lift ?? 0) * scale
|
|
477
|
+
};
|
|
478
|
+
if (!Number.isFinite(turn) || !lengthsInRange(position.x, position.y, position.z)) return void 0;
|
|
479
|
+
const local = {
|
|
480
|
+
position,
|
|
481
|
+
rotation: rotationAbout({
|
|
482
|
+
x: 0,
|
|
483
|
+
y: 0,
|
|
484
|
+
z: 1
|
|
485
|
+
}, turn % 360 * Math.PI / 180)
|
|
486
|
+
};
|
|
487
|
+
let world = local;
|
|
488
|
+
if (object.parent !== void 0) {
|
|
489
|
+
open.add(object.id);
|
|
490
|
+
const parent = objects.get(object.parent);
|
|
491
|
+
const outer = parent === void 0 ? void 0 : resolve(parent);
|
|
492
|
+
world = outer === void 0 ? void 0 : composeTransforms3(outer, local);
|
|
493
|
+
open.delete(object.id);
|
|
494
|
+
}
|
|
495
|
+
if (world !== void 0) done.set(object.id, world);
|
|
496
|
+
return world;
|
|
497
|
+
};
|
|
498
|
+
for (const object of scene.objects ?? []) resolve(object);
|
|
499
|
+
return done;
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* This function gives the 3D model of a scene: the solids of its room, then one box for each placed object.
|
|
503
|
+
* It skips an object that has no transform, no size, or a size that is not in the range of the model. `checkScene` tells each fault.
|
|
504
|
+
*/
|
|
505
|
+
function sceneSolids(scene, options = {}) {
|
|
506
|
+
const scale = metersPer(scene.room.unit);
|
|
507
|
+
const transforms = objectTransforms(scene);
|
|
508
|
+
const solids = [...roomSolids(scene.room, options)];
|
|
509
|
+
for (const object of scene.objects ?? []) {
|
|
510
|
+
const transform = transforms.get(object.id);
|
|
511
|
+
const w = object.size[0] * scale;
|
|
512
|
+
const h = object.size[1] * scale;
|
|
513
|
+
const d = object.size[2] * scale;
|
|
514
|
+
if (transform === void 0 || ![
|
|
515
|
+
w,
|
|
516
|
+
h,
|
|
517
|
+
d
|
|
518
|
+
].every((size) => size > EMPTY && size <= 1e6)) continue;
|
|
519
|
+
solids.push({
|
|
520
|
+
id: `object:${object.id}`,
|
|
521
|
+
kind: "object",
|
|
522
|
+
source: object.id,
|
|
523
|
+
shape: {
|
|
524
|
+
kind: "box",
|
|
525
|
+
box: {
|
|
526
|
+
x: 0 - w / 2,
|
|
527
|
+
y: 0 - h / 2,
|
|
528
|
+
z: 0,
|
|
529
|
+
w,
|
|
530
|
+
h,
|
|
531
|
+
d
|
|
532
|
+
},
|
|
533
|
+
transform
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
return solids;
|
|
538
|
+
}
|
|
539
|
+
/** This function gives the corners of a solid in the room. A box has 8, and a prism has 2 for each corner of its outline. */
|
|
540
|
+
function solidCorners(solid) {
|
|
541
|
+
const shape = solid.shape;
|
|
542
|
+
if (shape.kind === "box") return box3Corners(shape.box).map((corner) => transformPoint3(shape.transform, corner));
|
|
543
|
+
return shape.ring.flatMap(([x, y]) => [{
|
|
544
|
+
x,
|
|
545
|
+
y,
|
|
546
|
+
z: shape.z
|
|
547
|
+
}, {
|
|
548
|
+
x,
|
|
549
|
+
y,
|
|
550
|
+
z: shape.z + shape.d
|
|
551
|
+
}]);
|
|
552
|
+
}
|
|
553
|
+
/** This function gives the smallest box along the axes that holds each solid, or `undefined` for no solids. */
|
|
554
|
+
function boundsOfSolids(solids) {
|
|
555
|
+
return boundsOf3(solids.flatMap((solid) => solidCorners(solid)));
|
|
556
|
+
}
|
|
557
|
+
/** This function gives the volume of a solid in cubic meters. */
|
|
558
|
+
function solidVolume(solid) {
|
|
559
|
+
const shape = solid.shape;
|
|
560
|
+
return shape.kind === "box" ? box3Volume(shape.box) : regionArea({
|
|
561
|
+
kind: "polygon",
|
|
562
|
+
points: shape.ring
|
|
563
|
+
}) * shape.d;
|
|
564
|
+
}
|
|
565
|
+
// src/loads.ts
|
|
566
|
+
/** These are the levels of the load of a circuit. */
|
|
567
|
+
const LOAD_LEVELS = [
|
|
568
|
+
"unknown",
|
|
569
|
+
"ok",
|
|
570
|
+
"near",
|
|
571
|
+
"over"
|
|
572
|
+
];
|
|
573
|
+
/** The default part of the limit at which a circuit has the level `near`. */
|
|
574
|
+
const NEAR_LIMIT = .8;
|
|
575
|
+
/**
|
|
576
|
+
* This function gives the state of each circuit of a scene, in the sequence of the circuits.
|
|
577
|
+
*
|
|
578
|
+
* The loads are input data: a calculation or a meter gives them. A circuit that no load record names has the level `unknown`,
|
|
579
|
+
* and so has a circuit with a limit that is not above 0. A load above the limit has the level `over`.
|
|
580
|
+
*/
|
|
581
|
+
function circuitStates(scene, options = {}) {
|
|
582
|
+
const near = options.near ?? .8;
|
|
583
|
+
const outlets = scene.room.outlets ?? [];
|
|
584
|
+
return (scene.room.circuits ?? []).map((circuit) => {
|
|
585
|
+
const loads = (scene.loads ?? []).filter((load) => load.circuit === circuit.id);
|
|
586
|
+
const watts = loads.reduce((sum, load) => sum + load.watts, 0);
|
|
587
|
+
const known = loads.length > 0 && circuit.limit > 0 && Number.isFinite(watts);
|
|
588
|
+
const ratio = known ? watts / circuit.limit : 0;
|
|
589
|
+
const mine = outlets.filter((outlet) => outlet.circuit === circuit.id).map((outlet) => outlet.id);
|
|
590
|
+
return {
|
|
591
|
+
circuit: circuit.id,
|
|
592
|
+
watts,
|
|
593
|
+
limit: circuit.limit,
|
|
594
|
+
ratio,
|
|
595
|
+
level: !known ? "unknown" : ratio > 1 ? "over" : ratio >= near ? "near" : "ok",
|
|
596
|
+
outlets: mine,
|
|
597
|
+
objects: (scene.objects ?? []).filter((object) => object.outlet !== void 0 && mine.includes(object.outlet)).map((object) => object.id)
|
|
598
|
+
};
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* This function gives the level of each outlet of a scene: the level of its circuit. The key is the `id` of the outlet.
|
|
603
|
+
* An outlet with no circuit of the room has no level, and the map does not have it.
|
|
604
|
+
*/
|
|
605
|
+
function outletLevels(scene, options = {}) {
|
|
606
|
+
const levels = /* @__PURE__ */ new Map();
|
|
607
|
+
for (const state of circuitStates(scene, options)) for (const outlet of state.outlets) levels.set(outlet, state.level);
|
|
608
|
+
return levels;
|
|
609
|
+
}
|
|
610
|
+
/** These are the shapes of the sign of a level. */
|
|
611
|
+
const LEVEL_SHAPES = [
|
|
612
|
+
"ring",
|
|
613
|
+
"disc",
|
|
614
|
+
"triangle",
|
|
615
|
+
"square"
|
|
616
|
+
];
|
|
617
|
+
/**
|
|
618
|
+
* This function gives the shape of the sign of a level: a disc for `ok`, a triangle for `near` and a square for `over`.
|
|
619
|
+
* The level `unknown` has a ring, and so has an outlet with no level.
|
|
620
|
+
*/
|
|
621
|
+
function levelShape(level) {
|
|
622
|
+
return level === "ok" ? "disc" : level === "near" ? "triangle" : level === "over" ? "square" : "ring";
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* The outline of the sign of each shape. Each viewer draws the signs from these values, so each viewer shows the same signs.
|
|
626
|
+
* A triangle and a square with the radius of the disc look small, so they are larger.
|
|
627
|
+
*/
|
|
628
|
+
const SHAPE_OUTLINES = {
|
|
629
|
+
ring: {
|
|
630
|
+
radius: 1,
|
|
631
|
+
sides: 40,
|
|
632
|
+
start: 0,
|
|
633
|
+
hole: .52
|
|
634
|
+
},
|
|
635
|
+
disc: {
|
|
636
|
+
radius: 1,
|
|
637
|
+
sides: 40,
|
|
638
|
+
start: 0,
|
|
639
|
+
hole: 0
|
|
640
|
+
},
|
|
641
|
+
triangle: {
|
|
642
|
+
radius: 1.3,
|
|
643
|
+
sides: 3,
|
|
644
|
+
start: Math.PI / 2,
|
|
645
|
+
hole: 0
|
|
646
|
+
},
|
|
647
|
+
square: {
|
|
648
|
+
radius: 1.25,
|
|
649
|
+
sides: 4,
|
|
650
|
+
start: Math.PI / 4,
|
|
651
|
+
hole: 0
|
|
652
|
+
}
|
|
653
|
+
};
|
|
654
|
+
/**
|
|
655
|
+
* This function gives the corners of the outline of a sign, counter-clockwise, with the y axis up.
|
|
656
|
+
* The center of the sign is the origin, and `radius` is the radius of a disc.
|
|
657
|
+
*/
|
|
658
|
+
function shapeRing(shape, radius = 1) {
|
|
659
|
+
const outline = SHAPE_OUTLINES[shape];
|
|
660
|
+
return Array.from({ length: outline.sides }, (_, index) => {
|
|
661
|
+
const angle = outline.start + index * 2 * Math.PI / outline.sides;
|
|
662
|
+
return [radius * outline.radius * Math.cos(angle), radius * outline.radius * Math.sin(angle)];
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
export { wallFrames as C, wallTransform as E, wallFootprints as S, wallPoint as T, sceneSolids as _, circuitStates as a, JOIN_TOLERANCE as b, shapeRing as c, OUTLET_PLATE as d, SOLID_KINDS as f, roomSolids as g, objectTransforms as h, SHAPE_OUTLINES as i, FLOOR_DEPTH as l, boundsOfSolids as m, LOAD_LEVELS as n, levelShape as o, TO_Y_UP as p, NEAR_LIMIT as r, outletLevels as s, LEVEL_SHAPES as t, GLASS_THICKNESS as u, solidCorners as v, wallHasOutline as w, MITER_LIMIT as x, solidVolume as y };
|
|
666
|
+
|
|
667
|
+
//# sourceMappingURL=loads-Br73kIsQ.js.map
|