@vulcan-energy/mcp-helper 0.2.5 → 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 +57 -54
- package/THIRD_PARTY_NOTICES.md +2 -2
- package/dist/bin/vulcan-mcp-helper.js +50 -104
- package/dist/hem-target-runtime/browser-adapter.mjs +89 -46
- package/dist/hem-target-runtime/host.mjs +297 -17895
- package/dist/hem-target-runtime/preparation.mjs +25372 -0
- package/dist/hem-target-runtime/python-worker.mjs +2885 -680
- package/dist/hem-target-runtime/release-manifests.mjs +32 -29
- package/dist/hem-target-runtime/rust-worker.mjs +2464 -39
- package/dist/hem-target-runtime/wasm-loader.mjs +1 -0
- package/dist/server.d.ts +26 -6
- package/dist/server.js +36 -92
- package/package.json +18 -12
- package/schemas/core-input.schema.json +8020 -0
- package/schemas/input_fhs.schema.json +4728 -0
- package/wasm/wasm_version.json +9 -9
- package/wasm/wasm_wrapper.js +6 -6
- package/wasm/wasm_wrapper_bg.wasm +0 -0
- package/dist/hem-target-runtime/csv-target.mjs +0 -126
|
@@ -16,445 +16,2458 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
}
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
|
-
var __toESM = (mod, isNodeMode,
|
|
19
|
+
var __toESM = (mod, isNodeMode, target2) => (target2 = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
20
|
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
21
|
// file that has been converted to a CommonJS file using a Babel-
|
|
22
22
|
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
23
|
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target2, "default", { value: mod, enumerable: true }) : target2,
|
|
25
25
|
mod
|
|
26
26
|
));
|
|
27
27
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
28
28
|
|
|
29
|
-
// ../node_modules/
|
|
30
|
-
var
|
|
31
|
-
"../node_modules/
|
|
32
|
-
((
|
|
33
|
-
"
|
|
34
|
-
})(exports, function
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
29
|
+
// ../node_modules/polygon-clipping/dist/polygon-clipping.umd.js
|
|
30
|
+
var require_polygon_clipping_umd = __commonJS({
|
|
31
|
+
"../node_modules/polygon-clipping/dist/polygon-clipping.umd.js"(exports, module) {
|
|
32
|
+
(function(global, factory) {
|
|
33
|
+
typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory() : typeof define === "function" && define.amd ? define(factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, global.polygonClipping = factory());
|
|
34
|
+
})(exports, function() {
|
|
35
|
+
"use strict";
|
|
36
|
+
function __generator(thisArg, body) {
|
|
37
|
+
var _ = {
|
|
38
|
+
label: 0,
|
|
39
|
+
sent: function() {
|
|
40
|
+
if (t[0] & 1) throw t[1];
|
|
41
|
+
return t[1];
|
|
42
|
+
},
|
|
43
|
+
trys: [],
|
|
44
|
+
ops: []
|
|
45
|
+
}, f, y, t, g;
|
|
46
|
+
return g = {
|
|
47
|
+
next: verb(0),
|
|
48
|
+
"throw": verb(1),
|
|
49
|
+
"return": verb(2)
|
|
50
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
51
|
+
return this;
|
|
52
|
+
}), g;
|
|
53
|
+
function verb(n) {
|
|
54
|
+
return function(v) {
|
|
55
|
+
return step([n, v]);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function step(op) {
|
|
59
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
60
|
+
while (_) try {
|
|
61
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
62
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
63
|
+
switch (op[0]) {
|
|
64
|
+
case 0:
|
|
65
|
+
case 1:
|
|
66
|
+
t = op;
|
|
67
|
+
break;
|
|
68
|
+
case 4:
|
|
69
|
+
_.label++;
|
|
70
|
+
return {
|
|
71
|
+
value: op[1],
|
|
72
|
+
done: false
|
|
73
|
+
};
|
|
74
|
+
case 5:
|
|
75
|
+
_.label++;
|
|
76
|
+
y = op[1];
|
|
77
|
+
op = [0];
|
|
78
|
+
continue;
|
|
79
|
+
case 7:
|
|
80
|
+
op = _.ops.pop();
|
|
81
|
+
_.trys.pop();
|
|
82
|
+
continue;
|
|
83
|
+
default:
|
|
84
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
85
|
+
_ = 0;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
89
|
+
_.label = op[1];
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
93
|
+
_.label = t[1];
|
|
94
|
+
t = op;
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
if (t && _.label < t[2]) {
|
|
98
|
+
_.label = t[2];
|
|
99
|
+
_.ops.push(op);
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
if (t[2]) _.ops.pop();
|
|
103
|
+
_.trys.pop();
|
|
104
|
+
continue;
|
|
56
105
|
}
|
|
57
|
-
|
|
106
|
+
op = body.call(thisArg, _);
|
|
107
|
+
} catch (e) {
|
|
108
|
+
op = [6, e];
|
|
109
|
+
y = 0;
|
|
110
|
+
} finally {
|
|
111
|
+
f = t = 0;
|
|
58
112
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
113
|
+
if (op[0] & 5) throw op[1];
|
|
114
|
+
return {
|
|
115
|
+
value: op[0] ? op[1] : void 0,
|
|
116
|
+
done: true
|
|
117
|
+
};
|
|
118
|
+
}
|
|
63
119
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
120
|
+
var Node = (
|
|
121
|
+
/** @class */
|
|
122
|
+
/* @__PURE__ */ function() {
|
|
123
|
+
function Node2(key, data) {
|
|
124
|
+
this.next = null;
|
|
125
|
+
this.key = key;
|
|
126
|
+
this.data = data;
|
|
127
|
+
this.left = null;
|
|
128
|
+
this.right = null;
|
|
129
|
+
}
|
|
130
|
+
return Node2;
|
|
131
|
+
}()
|
|
132
|
+
);
|
|
133
|
+
function DEFAULT_COMPARE(a, b) {
|
|
134
|
+
return a > b ? 1 : a < b ? -1 : 0;
|
|
135
|
+
}
|
|
136
|
+
function splay(i, t, comparator) {
|
|
137
|
+
var N = new Node(null, null);
|
|
138
|
+
var l = N;
|
|
139
|
+
var r = N;
|
|
140
|
+
while (true) {
|
|
141
|
+
var cmp2 = comparator(i, t.key);
|
|
142
|
+
if (cmp2 < 0) {
|
|
143
|
+
if (t.left === null) break;
|
|
144
|
+
if (comparator(i, t.left.key) < 0) {
|
|
145
|
+
var y = t.left;
|
|
146
|
+
t.left = y.right;
|
|
147
|
+
y.right = t;
|
|
148
|
+
t = y;
|
|
149
|
+
if (t.left === null) break;
|
|
78
150
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
151
|
+
r.left = t;
|
|
152
|
+
r = t;
|
|
153
|
+
t = t.left;
|
|
154
|
+
} else if (cmp2 > 0) {
|
|
155
|
+
if (t.right === null) break;
|
|
156
|
+
if (comparator(i, t.right.key) > 0) {
|
|
157
|
+
var y = t.right;
|
|
158
|
+
t.right = y.left;
|
|
159
|
+
y.left = t;
|
|
160
|
+
t = y;
|
|
161
|
+
if (t.right === null) break;
|
|
85
162
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
163
|
+
l.right = t;
|
|
164
|
+
l = t;
|
|
165
|
+
t = t.right;
|
|
166
|
+
} else break;
|
|
167
|
+
}
|
|
168
|
+
l.right = t.left;
|
|
169
|
+
r.left = t.right;
|
|
170
|
+
t.left = N.right;
|
|
171
|
+
t.right = N.left;
|
|
172
|
+
return t;
|
|
94
173
|
}
|
|
95
|
-
function
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
|
|
174
|
+
function insert(i, data, t, comparator) {
|
|
175
|
+
var node = new Node(i, data);
|
|
176
|
+
if (t === null) {
|
|
177
|
+
node.left = node.right = null;
|
|
178
|
+
return node;
|
|
179
|
+
}
|
|
180
|
+
t = splay(i, t, comparator);
|
|
181
|
+
var cmp2 = comparator(i, t.key);
|
|
182
|
+
if (cmp2 < 0) {
|
|
183
|
+
node.left = t.left;
|
|
184
|
+
node.right = t;
|
|
185
|
+
t.left = null;
|
|
186
|
+
} else if (cmp2 >= 0) {
|
|
187
|
+
node.right = t.right;
|
|
188
|
+
node.left = t;
|
|
189
|
+
t.right = null;
|
|
190
|
+
}
|
|
191
|
+
return node;
|
|
110
192
|
}
|
|
111
|
-
function
|
|
112
|
-
var
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
var
|
|
117
|
-
if (
|
|
193
|
+
function split(key, v, comparator) {
|
|
194
|
+
var left = null;
|
|
195
|
+
var right = null;
|
|
196
|
+
if (v) {
|
|
197
|
+
v = splay(key, v, comparator);
|
|
198
|
+
var cmp2 = comparator(v.key, key);
|
|
199
|
+
if (cmp2 === 0) {
|
|
200
|
+
left = v.left;
|
|
201
|
+
right = v.right;
|
|
202
|
+
} else if (cmp2 < 0) {
|
|
203
|
+
right = v.right;
|
|
204
|
+
v.right = null;
|
|
205
|
+
left = v;
|
|
206
|
+
} else {
|
|
207
|
+
left = v.left;
|
|
208
|
+
v.left = null;
|
|
209
|
+
right = v;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return {
|
|
213
|
+
left,
|
|
214
|
+
right
|
|
118
215
|
};
|
|
119
216
|
}
|
|
120
|
-
function
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
u.prototype.resume.apply(this, arguments), this._input.resume();
|
|
127
|
-
}, this.stream = function(e2) {
|
|
128
|
-
this._input = e2, this._input.on("data", this._streamData), this._input.on("end", this._streamEnd), this._input.on("error", this._streamError);
|
|
129
|
-
}, this._checkIsFinished = function() {
|
|
130
|
-
r2 && 1 === t.length && (this._finished = true);
|
|
131
|
-
}, this._nextChunk = function() {
|
|
132
|
-
this._checkIsFinished(), t.length ? this.parseChunk(t.shift()) : i2 = true;
|
|
133
|
-
}, this._streamData = y(function(e2) {
|
|
134
|
-
try {
|
|
135
|
-
t.push("string" == typeof e2 ? e2 : e2.toString(this._config.encoding)), i2 && (i2 = false, this._checkIsFinished(), this.parseChunk(t.shift()));
|
|
136
|
-
} catch (e3) {
|
|
137
|
-
this._streamError(e3);
|
|
138
|
-
}
|
|
139
|
-
}, this), this._streamError = y(function(e2) {
|
|
140
|
-
this._streamCleanUp(), this._sendError(e2);
|
|
141
|
-
}, this), this._streamEnd = y(function() {
|
|
142
|
-
this._streamCleanUp(), r2 = true, this._streamData("");
|
|
143
|
-
}, this), this._streamCleanUp = y(function() {
|
|
144
|
-
this._input.removeListener("data", this._streamData), this._input.removeListener("end", this._streamEnd), this._input.removeListener("error", this._streamError);
|
|
145
|
-
}, this);
|
|
217
|
+
function merge(left, right, comparator) {
|
|
218
|
+
if (right === null) return left;
|
|
219
|
+
if (left === null) return right;
|
|
220
|
+
right = splay(left.key, right, comparator);
|
|
221
|
+
right.left = left;
|
|
222
|
+
return right;
|
|
146
223
|
}
|
|
147
|
-
function
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
for (var e2 = 0; _2() && e2 < p2.data.length; e2++) p2.data[e2].forEach(t3);
|
|
162
|
-
p2.data.splice(0, 1);
|
|
163
|
-
} else p2.data.forEach(t3);
|
|
164
|
-
}
|
|
165
|
-
function i3(e3, t3) {
|
|
166
|
-
for (var i4 = m2.header ? {} : [], r4 = 0; r4 < e3.length; r4++) {
|
|
167
|
-
var n3 = r4, s3 = e3[r4], s3 = ((e4, t4) => ((e5) => (m2.dynamicTypingFunction && void 0 === m2.dynamicTyping[e5] && (m2.dynamicTyping[e5] = m2.dynamicTypingFunction(e5)), true === (m2.dynamicTyping[e5] || m2.dynamicTyping)))(e4) ? "true" === t4 || "TRUE" === t4 || "false" !== t4 && "FALSE" !== t4 && (((e5) => {
|
|
168
|
-
if (u2.test(e5)) {
|
|
169
|
-
e5 = parseFloat(e5);
|
|
170
|
-
if (h2 < e5 && e5 < o2) return 1;
|
|
171
|
-
}
|
|
172
|
-
})(t4) ? parseFloat(t4) : d2.test(t4) ? new Date(t4) : "" === t4 ? null : t4) : t4)(n3 = m2.header ? r4 >= c2.length ? "__parsed_extra" : c2[r4] : n3, s3 = m2.transform ? m2.transform(s3, n3) : s3);
|
|
173
|
-
"__parsed_extra" === n3 ? (i4[n3] = i4[n3] || [], i4[n3].push(s3)) : i4[n3] = s3;
|
|
224
|
+
function printRow(root, prefix, isTail, out, printNode) {
|
|
225
|
+
if (root) {
|
|
226
|
+
out("" + prefix + (isTail ? "\u2514\u2500\u2500 " : "\u251C\u2500\u2500 ") + printNode(root) + "\n");
|
|
227
|
+
var indent = prefix + (isTail ? " " : "\u2502 ");
|
|
228
|
+
if (root.left) printRow(root.left, indent, false, out, printNode);
|
|
229
|
+
if (root.right) printRow(root.right, indent, true, out, printNode);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
var Tree = (
|
|
233
|
+
/** @class */
|
|
234
|
+
function() {
|
|
235
|
+
function Tree2(comparator) {
|
|
236
|
+
if (comparator === void 0) {
|
|
237
|
+
comparator = DEFAULT_COMPARE;
|
|
174
238
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
}), this.parse = function(e2, t2, i3) {
|
|
190
|
-
var r3 = m2.quoteChar || '"', r3 = (m2.newline || (m2.newline = this.guessLineEndings(e2, r3)), a2 = false, m2.delimiter ? U(m2.delimiter) && (m2.delimiter = m2.delimiter(e2), p2.meta.delimiter = m2.delimiter) : ((r3 = ((e3, t3, i4, r4, n3) => {
|
|
191
|
-
var s3, a3, o3, h3;
|
|
192
|
-
n3 = n3 || [",", " ", "|", ";", v.RECORD_SEP, v.UNIT_SEP];
|
|
193
|
-
for (var u3 = 0; u3 < n3.length; u3++) {
|
|
194
|
-
for (var d3, f3 = n3[u3], l3 = 0, c3 = 0, p3 = 0, g3 = (o3 = void 0, new E({ comments: r4, delimiter: f3, newline: t3, preview: 10 }).parse(e3)), _3 = 0; _3 < g3.data.length; _3++) i4 && y2(g3.data[_3]) ? p3++ : (d3 = g3.data[_3].length, c3 += d3, void 0 === o3 ? o3 = d3 : 0 < d3 && (l3 += Math.abs(d3 - o3), o3 = d3));
|
|
195
|
-
0 < g3.data.length && (c3 /= g3.data.length - p3), (void 0 === a3 || l3 <= a3) && (void 0 === h3 || h3 < c3) && 1.99 < c3 && (a3 = l3, s3 = f3, h3 = c3);
|
|
239
|
+
this._root = null;
|
|
240
|
+
this._size = 0;
|
|
241
|
+
this._comparator = comparator;
|
|
242
|
+
}
|
|
243
|
+
Tree2.prototype.insert = function(key, data) {
|
|
244
|
+
this._size++;
|
|
245
|
+
return this._root = insert(key, data, this._root, this._comparator);
|
|
246
|
+
};
|
|
247
|
+
Tree2.prototype.add = function(key, data) {
|
|
248
|
+
var node = new Node(key, data);
|
|
249
|
+
if (this._root === null) {
|
|
250
|
+
node.left = node.right = null;
|
|
251
|
+
this._size++;
|
|
252
|
+
this._root = node;
|
|
196
253
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
}, this.guessLineEndings = function(e2, t2) {
|
|
211
|
-
e2 = e2.substring(0, 1048576);
|
|
212
|
-
var t2 = new RegExp(P(t2) + "([^]*?)" + P(t2), "gm"), i3 = (e2 = e2.replace(t2, "")).split("\r"), t2 = e2.split("\n"), e2 = 1 < t2.length && t2[0].length < i3[0].length;
|
|
213
|
-
if (1 === i3.length || e2) return "\n";
|
|
214
|
-
for (var r3 = 0, n3 = 0; n3 < i3.length; n3++) "\n" === i3[n3][0] && r3++;
|
|
215
|
-
return r3 >= i3.length / 2 ? "\r\n" : "\r";
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
function P(e) {
|
|
219
|
-
return e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
220
|
-
}
|
|
221
|
-
function E(C) {
|
|
222
|
-
var S = (C = C || {}).delimiter, O = C.newline, x = C.comments, I = C.step, A = C.preview, T = C.fastMode, D = null, L = false, F = null == C.quoteChar ? '"' : C.quoteChar, j = F;
|
|
223
|
-
if (void 0 !== C.escapeChar && (j = C.escapeChar), ("string" != typeof S || -1 < v.BAD_DELIMITERS.indexOf(S)) && (S = ","), x === S) throw new Error("Comment character same as delimiter");
|
|
224
|
-
true === x ? x = "#" : ("string" != typeof x || -1 < v.BAD_DELIMITERS.indexOf(x)) && (x = false), "\n" !== O && "\r" !== O && "\r\n" !== O && (O = "\n");
|
|
225
|
-
var z = 0, M = false;
|
|
226
|
-
this.parse = function(i2, t, r2) {
|
|
227
|
-
if ("string" != typeof i2) throw new Error("Input must be a string");
|
|
228
|
-
var n2 = i2.length, e = S.length, s2 = O.length, a2 = x.length, o2 = U(I), h2 = [], u2 = [], d2 = [], f2 = z = 0;
|
|
229
|
-
if (!i2) return w();
|
|
230
|
-
if (T || false !== T && -1 === i2.indexOf(F)) {
|
|
231
|
-
for (var l2 = i2.split(O), c2 = 0; c2 < l2.length; c2++) {
|
|
232
|
-
if (d2 = l2[c2], z += d2.length, c2 !== l2.length - 1) z += O.length;
|
|
233
|
-
else if (r2) return w();
|
|
234
|
-
if (!x || d2.substring(0, a2) !== x) {
|
|
235
|
-
if (o2) {
|
|
236
|
-
if (h2 = [], k(d2.split(S)), R(), M) return w();
|
|
237
|
-
} else k(d2.split(S));
|
|
238
|
-
if (A && A <= c2) return h2 = h2.slice(0, A), w(true);
|
|
254
|
+
var comparator = this._comparator;
|
|
255
|
+
var t = splay(key, this._root, comparator);
|
|
256
|
+
var cmp2 = comparator(key, t.key);
|
|
257
|
+
if (cmp2 === 0) this._root = t;
|
|
258
|
+
else {
|
|
259
|
+
if (cmp2 < 0) {
|
|
260
|
+
node.left = t.left;
|
|
261
|
+
node.right = t;
|
|
262
|
+
t.left = null;
|
|
263
|
+
} else if (cmp2 > 0) {
|
|
264
|
+
node.right = t.right;
|
|
265
|
+
node.left = t;
|
|
266
|
+
t.right = null;
|
|
239
267
|
}
|
|
268
|
+
this._size++;
|
|
269
|
+
this._root = node;
|
|
240
270
|
}
|
|
241
|
-
return
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
271
|
+
return this._root;
|
|
272
|
+
};
|
|
273
|
+
Tree2.prototype.remove = function(key) {
|
|
274
|
+
this._root = this._remove(key, this._root, this._comparator);
|
|
275
|
+
};
|
|
276
|
+
Tree2.prototype._remove = function(i, t, comparator) {
|
|
277
|
+
var x;
|
|
278
|
+
if (t === null) return null;
|
|
279
|
+
t = splay(i, t, comparator);
|
|
280
|
+
var cmp2 = comparator(i, t.key);
|
|
281
|
+
if (cmp2 === 0) {
|
|
282
|
+
if (t.left === null) {
|
|
283
|
+
x = t.right;
|
|
284
|
+
} else {
|
|
285
|
+
x = splay(i, t.left, comparator);
|
|
286
|
+
x.right = t.right;
|
|
253
287
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
288
|
+
this._size--;
|
|
289
|
+
return x;
|
|
290
|
+
}
|
|
291
|
+
return t;
|
|
292
|
+
};
|
|
293
|
+
Tree2.prototype.pop = function() {
|
|
294
|
+
var node = this._root;
|
|
295
|
+
if (node) {
|
|
296
|
+
while (node.left) node = node.left;
|
|
297
|
+
this._root = splay(node.key, this._root, this._comparator);
|
|
298
|
+
this._root = this._remove(node.key, this._root, this._comparator);
|
|
299
|
+
return {
|
|
300
|
+
key: node.key,
|
|
301
|
+
data: node.data
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
return null;
|
|
305
|
+
};
|
|
306
|
+
Tree2.prototype.findStatic = function(key) {
|
|
307
|
+
var current = this._root;
|
|
308
|
+
var compare = this._comparator;
|
|
309
|
+
while (current) {
|
|
310
|
+
var cmp2 = compare(key, current.key);
|
|
311
|
+
if (cmp2 === 0) return current;
|
|
312
|
+
else if (cmp2 < 0) current = current.left;
|
|
313
|
+
else current = current.right;
|
|
314
|
+
}
|
|
315
|
+
return null;
|
|
316
|
+
};
|
|
317
|
+
Tree2.prototype.find = function(key) {
|
|
318
|
+
if (this._root) {
|
|
319
|
+
this._root = splay(key, this._root, this._comparator);
|
|
320
|
+
if (this._comparator(key, this._root.key) !== 0) return null;
|
|
321
|
+
}
|
|
322
|
+
return this._root;
|
|
323
|
+
};
|
|
324
|
+
Tree2.prototype.contains = function(key) {
|
|
325
|
+
var current = this._root;
|
|
326
|
+
var compare = this._comparator;
|
|
327
|
+
while (current) {
|
|
328
|
+
var cmp2 = compare(key, current.key);
|
|
329
|
+
if (cmp2 === 0) return true;
|
|
330
|
+
else if (cmp2 < 0) current = current.left;
|
|
331
|
+
else current = current.right;
|
|
332
|
+
}
|
|
333
|
+
return false;
|
|
334
|
+
};
|
|
335
|
+
Tree2.prototype.forEach = function(visitor, ctx) {
|
|
336
|
+
var current = this._root;
|
|
337
|
+
var Q = [];
|
|
338
|
+
var done = false;
|
|
339
|
+
while (!done) {
|
|
340
|
+
if (current !== null) {
|
|
341
|
+
Q.push(current);
|
|
342
|
+
current = current.left;
|
|
343
|
+
} else {
|
|
344
|
+
if (Q.length !== 0) {
|
|
345
|
+
current = Q.pop();
|
|
346
|
+
visitor.call(ctx, current);
|
|
347
|
+
current = current.right;
|
|
348
|
+
} else done = true;
|
|
259
349
|
}
|
|
260
|
-
u2.push({ type: "Quotes", code: "InvalidQuotes", message: "Trailing quote on quoted field is malformed", row: h2.length, index: z }), m2++;
|
|
261
350
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
}
|
|
283
|
-
function b2(e2) {
|
|
284
|
-
z = e2, k(d2), d2 = [], g2 = i2.indexOf(O, z);
|
|
285
|
-
}
|
|
286
|
-
function w(e2) {
|
|
287
|
-
if (C.header && !t && h2.length && !L) {
|
|
288
|
-
var s3 = h2[0], a3 = /* @__PURE__ */ Object.create(null), o3 = new Set(s3);
|
|
289
|
-
let n3 = false;
|
|
290
|
-
for (let r3 = 0; r3 < s3.length; r3++) {
|
|
291
|
-
let i3 = s3[r3];
|
|
292
|
-
if (a3[i3 = U(C.transformHeader) ? C.transformHeader(i3, r3) : i3]) {
|
|
293
|
-
let e3, t2 = a3[i3];
|
|
294
|
-
for (; e3 = i3 + "_" + t2, t2++, o3.has(e3); ) ;
|
|
295
|
-
o3.add(e3), s3[r3] = e3, a3[i3]++, n3 = true, (D = null === D ? {} : D)[e3] = i3;
|
|
296
|
-
} else a3[i3] = 1, s3[r3] = i3;
|
|
297
|
-
o3.add(i3);
|
|
351
|
+
return this;
|
|
352
|
+
};
|
|
353
|
+
Tree2.prototype.range = function(low, high, fn, ctx) {
|
|
354
|
+
var Q = [];
|
|
355
|
+
var compare = this._comparator;
|
|
356
|
+
var node = this._root;
|
|
357
|
+
var cmp2;
|
|
358
|
+
while (Q.length !== 0 || node) {
|
|
359
|
+
if (node) {
|
|
360
|
+
Q.push(node);
|
|
361
|
+
node = node.left;
|
|
362
|
+
} else {
|
|
363
|
+
node = Q.pop();
|
|
364
|
+
cmp2 = compare(node.key, high);
|
|
365
|
+
if (cmp2 > 0) {
|
|
366
|
+
break;
|
|
367
|
+
} else if (compare(node.key, low) >= 0) {
|
|
368
|
+
if (fn.call(ctx, node)) return this;
|
|
369
|
+
}
|
|
370
|
+
node = node.right;
|
|
298
371
|
}
|
|
299
|
-
n3 && console.warn("Duplicate headers found and renamed."), L = true;
|
|
300
372
|
}
|
|
301
|
-
return
|
|
373
|
+
return this;
|
|
374
|
+
};
|
|
375
|
+
Tree2.prototype.keys = function() {
|
|
376
|
+
var keys = [];
|
|
377
|
+
this.forEach(function(_a) {
|
|
378
|
+
var key = _a.key;
|
|
379
|
+
return keys.push(key);
|
|
380
|
+
});
|
|
381
|
+
return keys;
|
|
382
|
+
};
|
|
383
|
+
Tree2.prototype.values = function() {
|
|
384
|
+
var values = [];
|
|
385
|
+
this.forEach(function(_a) {
|
|
386
|
+
var data = _a.data;
|
|
387
|
+
return values.push(data);
|
|
388
|
+
});
|
|
389
|
+
return values;
|
|
390
|
+
};
|
|
391
|
+
Tree2.prototype.min = function() {
|
|
392
|
+
if (this._root) return this.minNode(this._root).key;
|
|
393
|
+
return null;
|
|
394
|
+
};
|
|
395
|
+
Tree2.prototype.max = function() {
|
|
396
|
+
if (this._root) return this.maxNode(this._root).key;
|
|
397
|
+
return null;
|
|
398
|
+
};
|
|
399
|
+
Tree2.prototype.minNode = function(t) {
|
|
400
|
+
if (t === void 0) {
|
|
401
|
+
t = this._root;
|
|
402
|
+
}
|
|
403
|
+
if (t) while (t.left) t = t.left;
|
|
404
|
+
return t;
|
|
405
|
+
};
|
|
406
|
+
Tree2.prototype.maxNode = function(t) {
|
|
407
|
+
if (t === void 0) {
|
|
408
|
+
t = this._root;
|
|
409
|
+
}
|
|
410
|
+
if (t) while (t.right) t = t.right;
|
|
411
|
+
return t;
|
|
412
|
+
};
|
|
413
|
+
Tree2.prototype.at = function(index2) {
|
|
414
|
+
var current = this._root;
|
|
415
|
+
var done = false;
|
|
416
|
+
var i = 0;
|
|
417
|
+
var Q = [];
|
|
418
|
+
while (!done) {
|
|
419
|
+
if (current) {
|
|
420
|
+
Q.push(current);
|
|
421
|
+
current = current.left;
|
|
422
|
+
} else {
|
|
423
|
+
if (Q.length > 0) {
|
|
424
|
+
current = Q.pop();
|
|
425
|
+
if (i === index2) return current;
|
|
426
|
+
i++;
|
|
427
|
+
current = current.right;
|
|
428
|
+
} else done = true;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
return null;
|
|
432
|
+
};
|
|
433
|
+
Tree2.prototype.next = function(d) {
|
|
434
|
+
var root = this._root;
|
|
435
|
+
var successor = null;
|
|
436
|
+
if (d.right) {
|
|
437
|
+
successor = d.right;
|
|
438
|
+
while (successor.left) successor = successor.left;
|
|
439
|
+
return successor;
|
|
440
|
+
}
|
|
441
|
+
var comparator = this._comparator;
|
|
442
|
+
while (root) {
|
|
443
|
+
var cmp2 = comparator(d.key, root.key);
|
|
444
|
+
if (cmp2 === 0) break;
|
|
445
|
+
else if (cmp2 < 0) {
|
|
446
|
+
successor = root;
|
|
447
|
+
root = root.left;
|
|
448
|
+
} else root = root.right;
|
|
449
|
+
}
|
|
450
|
+
return successor;
|
|
451
|
+
};
|
|
452
|
+
Tree2.prototype.prev = function(d) {
|
|
453
|
+
var root = this._root;
|
|
454
|
+
var predecessor = null;
|
|
455
|
+
if (d.left !== null) {
|
|
456
|
+
predecessor = d.left;
|
|
457
|
+
while (predecessor.right) predecessor = predecessor.right;
|
|
458
|
+
return predecessor;
|
|
459
|
+
}
|
|
460
|
+
var comparator = this._comparator;
|
|
461
|
+
while (root) {
|
|
462
|
+
var cmp2 = comparator(d.key, root.key);
|
|
463
|
+
if (cmp2 === 0) break;
|
|
464
|
+
else if (cmp2 < 0) root = root.left;
|
|
465
|
+
else {
|
|
466
|
+
predecessor = root;
|
|
467
|
+
root = root.right;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return predecessor;
|
|
471
|
+
};
|
|
472
|
+
Tree2.prototype.clear = function() {
|
|
473
|
+
this._root = null;
|
|
474
|
+
this._size = 0;
|
|
475
|
+
return this;
|
|
476
|
+
};
|
|
477
|
+
Tree2.prototype.toList = function() {
|
|
478
|
+
return toList(this._root);
|
|
479
|
+
};
|
|
480
|
+
Tree2.prototype.load = function(keys, values, presort) {
|
|
481
|
+
if (values === void 0) {
|
|
482
|
+
values = [];
|
|
483
|
+
}
|
|
484
|
+
if (presort === void 0) {
|
|
485
|
+
presort = false;
|
|
486
|
+
}
|
|
487
|
+
var size = keys.length;
|
|
488
|
+
var comparator = this._comparator;
|
|
489
|
+
if (presort) sort(keys, values, 0, size - 1, comparator);
|
|
490
|
+
if (this._root === null) {
|
|
491
|
+
this._root = loadRecursive(keys, values, 0, size);
|
|
492
|
+
this._size = size;
|
|
493
|
+
} else {
|
|
494
|
+
var mergedList = mergeLists(this.toList(), createList(keys, values), comparator);
|
|
495
|
+
size = this._size + size;
|
|
496
|
+
this._root = sortedListToBST({
|
|
497
|
+
head: mergedList
|
|
498
|
+
}, 0, size);
|
|
499
|
+
}
|
|
500
|
+
return this;
|
|
501
|
+
};
|
|
502
|
+
Tree2.prototype.isEmpty = function() {
|
|
503
|
+
return this._root === null;
|
|
504
|
+
};
|
|
505
|
+
Object.defineProperty(Tree2.prototype, "size", {
|
|
506
|
+
get: function() {
|
|
507
|
+
return this._size;
|
|
508
|
+
},
|
|
509
|
+
enumerable: true,
|
|
510
|
+
configurable: true
|
|
511
|
+
});
|
|
512
|
+
Object.defineProperty(Tree2.prototype, "root", {
|
|
513
|
+
get: function() {
|
|
514
|
+
return this._root;
|
|
515
|
+
},
|
|
516
|
+
enumerable: true,
|
|
517
|
+
configurable: true
|
|
518
|
+
});
|
|
519
|
+
Tree2.prototype.toString = function(printNode) {
|
|
520
|
+
if (printNode === void 0) {
|
|
521
|
+
printNode = function(n) {
|
|
522
|
+
return String(n.key);
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
var out = [];
|
|
526
|
+
printRow(this._root, "", true, function(v) {
|
|
527
|
+
return out.push(v);
|
|
528
|
+
}, printNode);
|
|
529
|
+
return out.join("");
|
|
530
|
+
};
|
|
531
|
+
Tree2.prototype.update = function(key, newKey, newData) {
|
|
532
|
+
var comparator = this._comparator;
|
|
533
|
+
var _a = split(key, this._root, comparator), left = _a.left, right = _a.right;
|
|
534
|
+
if (comparator(key, newKey) < 0) {
|
|
535
|
+
right = insert(newKey, newData, right, comparator);
|
|
536
|
+
} else {
|
|
537
|
+
left = insert(newKey, newData, left, comparator);
|
|
538
|
+
}
|
|
539
|
+
this._root = merge(left, right, comparator);
|
|
540
|
+
};
|
|
541
|
+
Tree2.prototype.split = function(key) {
|
|
542
|
+
return split(key, this._root, this._comparator);
|
|
543
|
+
};
|
|
544
|
+
Tree2.prototype[Symbol.iterator] = function() {
|
|
545
|
+
var current, Q, done;
|
|
546
|
+
return __generator(this, function(_a) {
|
|
547
|
+
switch (_a.label) {
|
|
548
|
+
case 0:
|
|
549
|
+
current = this._root;
|
|
550
|
+
Q = [];
|
|
551
|
+
done = false;
|
|
552
|
+
_a.label = 1;
|
|
553
|
+
case 1:
|
|
554
|
+
if (!!done) return [3, 6];
|
|
555
|
+
if (!(current !== null)) return [3, 2];
|
|
556
|
+
Q.push(current);
|
|
557
|
+
current = current.left;
|
|
558
|
+
return [3, 5];
|
|
559
|
+
case 2:
|
|
560
|
+
if (!(Q.length !== 0)) return [3, 4];
|
|
561
|
+
current = Q.pop();
|
|
562
|
+
return [4, current];
|
|
563
|
+
case 3:
|
|
564
|
+
_a.sent();
|
|
565
|
+
current = current.right;
|
|
566
|
+
return [3, 5];
|
|
567
|
+
case 4:
|
|
568
|
+
done = true;
|
|
569
|
+
_a.label = 5;
|
|
570
|
+
case 5:
|
|
571
|
+
return [3, 1];
|
|
572
|
+
case 6:
|
|
573
|
+
return [
|
|
574
|
+
2
|
|
575
|
+
/*return*/
|
|
576
|
+
];
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
};
|
|
580
|
+
return Tree2;
|
|
581
|
+
}()
|
|
582
|
+
);
|
|
583
|
+
function loadRecursive(keys, values, start, end) {
|
|
584
|
+
var size = end - start;
|
|
585
|
+
if (size > 0) {
|
|
586
|
+
var middle = start + Math.floor(size / 2);
|
|
587
|
+
var key = keys[middle];
|
|
588
|
+
var data = values[middle];
|
|
589
|
+
var node = new Node(key, data);
|
|
590
|
+
node.left = loadRecursive(keys, values, start, middle);
|
|
591
|
+
node.right = loadRecursive(keys, values, middle + 1, end);
|
|
592
|
+
return node;
|
|
593
|
+
}
|
|
594
|
+
return null;
|
|
595
|
+
}
|
|
596
|
+
function createList(keys, values) {
|
|
597
|
+
var head = new Node(null, null);
|
|
598
|
+
var p = head;
|
|
599
|
+
for (var i = 0; i < keys.length; i++) {
|
|
600
|
+
p = p.next = new Node(keys[i], values[i]);
|
|
601
|
+
}
|
|
602
|
+
p.next = null;
|
|
603
|
+
return head.next;
|
|
604
|
+
}
|
|
605
|
+
function toList(root) {
|
|
606
|
+
var current = root;
|
|
607
|
+
var Q = [];
|
|
608
|
+
var done = false;
|
|
609
|
+
var head = new Node(null, null);
|
|
610
|
+
var p = head;
|
|
611
|
+
while (!done) {
|
|
612
|
+
if (current) {
|
|
613
|
+
Q.push(current);
|
|
614
|
+
current = current.left;
|
|
615
|
+
} else {
|
|
616
|
+
if (Q.length > 0) {
|
|
617
|
+
current = p = p.next = Q.pop();
|
|
618
|
+
current = current.right;
|
|
619
|
+
} else done = true;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
p.next = null;
|
|
623
|
+
return head.next;
|
|
624
|
+
}
|
|
625
|
+
function sortedListToBST(list, start, end) {
|
|
626
|
+
var size = end - start;
|
|
627
|
+
if (size > 0) {
|
|
628
|
+
var middle = start + Math.floor(size / 2);
|
|
629
|
+
var left = sortedListToBST(list, start, middle);
|
|
630
|
+
var root = list.head;
|
|
631
|
+
root.left = left;
|
|
632
|
+
list.head = list.head.next;
|
|
633
|
+
root.right = sortedListToBST(list, middle + 1, end);
|
|
634
|
+
return root;
|
|
635
|
+
}
|
|
636
|
+
return null;
|
|
637
|
+
}
|
|
638
|
+
function mergeLists(l1, l2, compare) {
|
|
639
|
+
var head = new Node(null, null);
|
|
640
|
+
var p = head;
|
|
641
|
+
var p1 = l1;
|
|
642
|
+
var p2 = l2;
|
|
643
|
+
while (p1 !== null && p2 !== null) {
|
|
644
|
+
if (compare(p1.key, p2.key) < 0) {
|
|
645
|
+
p.next = p1;
|
|
646
|
+
p1 = p1.next;
|
|
647
|
+
} else {
|
|
648
|
+
p.next = p2;
|
|
649
|
+
p2 = p2.next;
|
|
302
650
|
}
|
|
303
|
-
|
|
304
|
-
|
|
651
|
+
p = p.next;
|
|
652
|
+
}
|
|
653
|
+
if (p1 !== null) {
|
|
654
|
+
p.next = p1;
|
|
655
|
+
} else if (p2 !== null) {
|
|
656
|
+
p.next = p2;
|
|
657
|
+
}
|
|
658
|
+
return head.next;
|
|
659
|
+
}
|
|
660
|
+
function sort(keys, values, left, right, compare) {
|
|
661
|
+
if (left >= right) return;
|
|
662
|
+
var pivot = keys[left + right >> 1];
|
|
663
|
+
var i = left - 1;
|
|
664
|
+
var j = right + 1;
|
|
665
|
+
while (true) {
|
|
666
|
+
do
|
|
667
|
+
i++;
|
|
668
|
+
while (compare(keys[i], pivot) < 0);
|
|
669
|
+
do
|
|
670
|
+
j--;
|
|
671
|
+
while (compare(keys[j], pivot) > 0);
|
|
672
|
+
if (i >= j) break;
|
|
673
|
+
var tmp = keys[i];
|
|
674
|
+
keys[i] = keys[j];
|
|
675
|
+
keys[j] = tmp;
|
|
676
|
+
tmp = values[i];
|
|
677
|
+
values[i] = values[j];
|
|
678
|
+
values[j] = tmp;
|
|
679
|
+
}
|
|
680
|
+
sort(keys, values, left, j, compare);
|
|
681
|
+
sort(keys, values, j + 1, right, compare);
|
|
682
|
+
}
|
|
683
|
+
const isInBbox = (bbox, point) => {
|
|
684
|
+
return bbox.ll.x <= point.x && point.x <= bbox.ur.x && bbox.ll.y <= point.y && point.y <= bbox.ur.y;
|
|
685
|
+
};
|
|
686
|
+
const getBboxOverlap = (b1, b2) => {
|
|
687
|
+
if (b2.ur.x < b1.ll.x || b1.ur.x < b2.ll.x || b2.ur.y < b1.ll.y || b1.ur.y < b2.ll.y) return null;
|
|
688
|
+
const lowerX = b1.ll.x < b2.ll.x ? b2.ll.x : b1.ll.x;
|
|
689
|
+
const upperX = b1.ur.x < b2.ur.x ? b1.ur.x : b2.ur.x;
|
|
690
|
+
const lowerY = b1.ll.y < b2.ll.y ? b2.ll.y : b1.ll.y;
|
|
691
|
+
const upperY = b1.ur.y < b2.ur.y ? b1.ur.y : b2.ur.y;
|
|
692
|
+
return {
|
|
693
|
+
ll: {
|
|
694
|
+
x: lowerX,
|
|
695
|
+
y: lowerY
|
|
696
|
+
},
|
|
697
|
+
ur: {
|
|
698
|
+
x: upperX,
|
|
699
|
+
y: upperY
|
|
305
700
|
}
|
|
306
|
-
}, this.abort = function() {
|
|
307
|
-
M = true;
|
|
308
|
-
}, this.getCharIndex = function() {
|
|
309
|
-
return z;
|
|
310
701
|
};
|
|
702
|
+
};
|
|
703
|
+
let epsilon$1 = Number.EPSILON;
|
|
704
|
+
if (epsilon$1 === void 0) epsilon$1 = Math.pow(2, -52);
|
|
705
|
+
const EPSILON_SQ = epsilon$1 * epsilon$1;
|
|
706
|
+
const cmp = (a, b) => {
|
|
707
|
+
if (-epsilon$1 < a && a < epsilon$1) {
|
|
708
|
+
if (-epsilon$1 < b && b < epsilon$1) {
|
|
709
|
+
return 0;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
const ab = a - b;
|
|
713
|
+
if (ab * ab < EPSILON_SQ * a * b) {
|
|
714
|
+
return 0;
|
|
715
|
+
}
|
|
716
|
+
return a < b ? -1 : 1;
|
|
717
|
+
};
|
|
718
|
+
class PtRounder {
|
|
719
|
+
constructor() {
|
|
720
|
+
this.reset();
|
|
721
|
+
}
|
|
722
|
+
reset() {
|
|
723
|
+
this.xRounder = new CoordRounder();
|
|
724
|
+
this.yRounder = new CoordRounder();
|
|
725
|
+
}
|
|
726
|
+
round(x, y) {
|
|
727
|
+
return {
|
|
728
|
+
x: this.xRounder.round(x),
|
|
729
|
+
y: this.yRounder.round(y)
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
class CoordRounder {
|
|
734
|
+
constructor() {
|
|
735
|
+
this.tree = new Tree();
|
|
736
|
+
this.round(0);
|
|
737
|
+
}
|
|
738
|
+
// Note: this can rounds input values backwards or forwards.
|
|
739
|
+
// You might ask, why not restrict this to just rounding
|
|
740
|
+
// forwards? Wouldn't that allow left endpoints to always
|
|
741
|
+
// remain left endpoints during splitting (never change to
|
|
742
|
+
// right). No - it wouldn't, because we snap intersections
|
|
743
|
+
// to endpoints (to establish independence from the segment
|
|
744
|
+
// angle for t-intersections).
|
|
745
|
+
round(coord) {
|
|
746
|
+
const node = this.tree.add(coord);
|
|
747
|
+
const prevNode = this.tree.prev(node);
|
|
748
|
+
if (prevNode !== null && cmp(node.key, prevNode.key) === 0) {
|
|
749
|
+
this.tree.remove(coord);
|
|
750
|
+
return prevNode.key;
|
|
751
|
+
}
|
|
752
|
+
const nextNode = this.tree.next(node);
|
|
753
|
+
if (nextNode !== null && cmp(node.key, nextNode.key) === 0) {
|
|
754
|
+
this.tree.remove(coord);
|
|
755
|
+
return nextNode.key;
|
|
756
|
+
}
|
|
757
|
+
return coord;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
const rounder = new PtRounder();
|
|
761
|
+
const epsilon = 11102230246251565e-32;
|
|
762
|
+
const splitter = 134217729;
|
|
763
|
+
const resulterrbound = (3 + 8 * epsilon) * epsilon;
|
|
764
|
+
function sum(elen, e, flen, f, h) {
|
|
765
|
+
let Q, Qnew, hh, bvirt;
|
|
766
|
+
let enow = e[0];
|
|
767
|
+
let fnow = f[0];
|
|
768
|
+
let eindex = 0;
|
|
769
|
+
let findex = 0;
|
|
770
|
+
if (fnow > enow === fnow > -enow) {
|
|
771
|
+
Q = enow;
|
|
772
|
+
enow = e[++eindex];
|
|
773
|
+
} else {
|
|
774
|
+
Q = fnow;
|
|
775
|
+
fnow = f[++findex];
|
|
776
|
+
}
|
|
777
|
+
let hindex = 0;
|
|
778
|
+
if (eindex < elen && findex < flen) {
|
|
779
|
+
if (fnow > enow === fnow > -enow) {
|
|
780
|
+
Qnew = enow + Q;
|
|
781
|
+
hh = Q - (Qnew - enow);
|
|
782
|
+
enow = e[++eindex];
|
|
783
|
+
} else {
|
|
784
|
+
Qnew = fnow + Q;
|
|
785
|
+
hh = Q - (Qnew - fnow);
|
|
786
|
+
fnow = f[++findex];
|
|
787
|
+
}
|
|
788
|
+
Q = Qnew;
|
|
789
|
+
if (hh !== 0) {
|
|
790
|
+
h[hindex++] = hh;
|
|
791
|
+
}
|
|
792
|
+
while (eindex < elen && findex < flen) {
|
|
793
|
+
if (fnow > enow === fnow > -enow) {
|
|
794
|
+
Qnew = Q + enow;
|
|
795
|
+
bvirt = Qnew - Q;
|
|
796
|
+
hh = Q - (Qnew - bvirt) + (enow - bvirt);
|
|
797
|
+
enow = e[++eindex];
|
|
798
|
+
} else {
|
|
799
|
+
Qnew = Q + fnow;
|
|
800
|
+
bvirt = Qnew - Q;
|
|
801
|
+
hh = Q - (Qnew - bvirt) + (fnow - bvirt);
|
|
802
|
+
fnow = f[++findex];
|
|
803
|
+
}
|
|
804
|
+
Q = Qnew;
|
|
805
|
+
if (hh !== 0) {
|
|
806
|
+
h[hindex++] = hh;
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
while (eindex < elen) {
|
|
811
|
+
Qnew = Q + enow;
|
|
812
|
+
bvirt = Qnew - Q;
|
|
813
|
+
hh = Q - (Qnew - bvirt) + (enow - bvirt);
|
|
814
|
+
enow = e[++eindex];
|
|
815
|
+
Q = Qnew;
|
|
816
|
+
if (hh !== 0) {
|
|
817
|
+
h[hindex++] = hh;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
while (findex < flen) {
|
|
821
|
+
Qnew = Q + fnow;
|
|
822
|
+
bvirt = Qnew - Q;
|
|
823
|
+
hh = Q - (Qnew - bvirt) + (fnow - bvirt);
|
|
824
|
+
fnow = f[++findex];
|
|
825
|
+
Q = Qnew;
|
|
826
|
+
if (hh !== 0) {
|
|
827
|
+
h[hindex++] = hh;
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
if (Q !== 0 || hindex === 0) {
|
|
831
|
+
h[hindex++] = Q;
|
|
832
|
+
}
|
|
833
|
+
return hindex;
|
|
311
834
|
}
|
|
312
|
-
function
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
var n2 = { abort: function() {
|
|
317
|
-
r2 = true, _(t.workerId, { data: [], errors: [], meta: { aborted: true } });
|
|
318
|
-
}, pause: m, resume: m };
|
|
319
|
-
if (U(i2.userStep)) {
|
|
320
|
-
for (var s2 = 0; s2 < t.results.data.length && (i2.userStep({ data: t.results.data[s2], errors: t.results.errors, meta: t.results.meta }, n2), !r2); s2++) ;
|
|
321
|
-
delete t.results;
|
|
322
|
-
} else U(i2.userChunk) && (i2.userChunk(t.results, n2, t.file), delete t.results);
|
|
323
|
-
}
|
|
324
|
-
t.finished && !r2 && _(t.workerId, t.results);
|
|
835
|
+
function estimate(elen, e) {
|
|
836
|
+
let Q = e[0];
|
|
837
|
+
for (let i = 1; i < elen; i++) Q += e[i];
|
|
838
|
+
return Q;
|
|
325
839
|
}
|
|
326
|
-
function
|
|
327
|
-
|
|
328
|
-
U(i2.userComplete) && i2.userComplete(t), i2.terminate(), delete o[e];
|
|
840
|
+
function vec(n) {
|
|
841
|
+
return new Float64Array(n);
|
|
329
842
|
}
|
|
330
|
-
|
|
331
|
-
|
|
843
|
+
const ccwerrboundA = (3 + 16 * epsilon) * epsilon;
|
|
844
|
+
const ccwerrboundB = (2 + 12 * epsilon) * epsilon;
|
|
845
|
+
const ccwerrboundC = (9 + 64 * epsilon) * epsilon * epsilon;
|
|
846
|
+
const B = vec(4);
|
|
847
|
+
const C1 = vec(8);
|
|
848
|
+
const C2 = vec(12);
|
|
849
|
+
const D = vec(16);
|
|
850
|
+
const u = vec(4);
|
|
851
|
+
function orient2dadapt(ax, ay, bx, by, cx, cy, detsum) {
|
|
852
|
+
let acxtail, acytail, bcxtail, bcytail;
|
|
853
|
+
let bvirt, c, ahi, alo, bhi, blo, _i, _j, _0, s1, s0, t1, t0, u3;
|
|
854
|
+
const acx = ax - cx;
|
|
855
|
+
const bcx = bx - cx;
|
|
856
|
+
const acy = ay - cy;
|
|
857
|
+
const bcy = by - cy;
|
|
858
|
+
s1 = acx * bcy;
|
|
859
|
+
c = splitter * acx;
|
|
860
|
+
ahi = c - (c - acx);
|
|
861
|
+
alo = acx - ahi;
|
|
862
|
+
c = splitter * bcy;
|
|
863
|
+
bhi = c - (c - bcy);
|
|
864
|
+
blo = bcy - bhi;
|
|
865
|
+
s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
|
|
866
|
+
t1 = acy * bcx;
|
|
867
|
+
c = splitter * acy;
|
|
868
|
+
ahi = c - (c - acy);
|
|
869
|
+
alo = acy - ahi;
|
|
870
|
+
c = splitter * bcx;
|
|
871
|
+
bhi = c - (c - bcx);
|
|
872
|
+
blo = bcx - bhi;
|
|
873
|
+
t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
|
|
874
|
+
_i = s0 - t0;
|
|
875
|
+
bvirt = s0 - _i;
|
|
876
|
+
B[0] = s0 - (_i + bvirt) + (bvirt - t0);
|
|
877
|
+
_j = s1 + _i;
|
|
878
|
+
bvirt = _j - s1;
|
|
879
|
+
_0 = s1 - (_j - bvirt) + (_i - bvirt);
|
|
880
|
+
_i = _0 - t1;
|
|
881
|
+
bvirt = _0 - _i;
|
|
882
|
+
B[1] = _0 - (_i + bvirt) + (bvirt - t1);
|
|
883
|
+
u3 = _j + _i;
|
|
884
|
+
bvirt = u3 - _j;
|
|
885
|
+
B[2] = _j - (u3 - bvirt) + (_i - bvirt);
|
|
886
|
+
B[3] = u3;
|
|
887
|
+
let det = estimate(4, B);
|
|
888
|
+
let errbound = ccwerrboundB * detsum;
|
|
889
|
+
if (det >= errbound || -det >= errbound) {
|
|
890
|
+
return det;
|
|
891
|
+
}
|
|
892
|
+
bvirt = ax - acx;
|
|
893
|
+
acxtail = ax - (acx + bvirt) + (bvirt - cx);
|
|
894
|
+
bvirt = bx - bcx;
|
|
895
|
+
bcxtail = bx - (bcx + bvirt) + (bvirt - cx);
|
|
896
|
+
bvirt = ay - acy;
|
|
897
|
+
acytail = ay - (acy + bvirt) + (bvirt - cy);
|
|
898
|
+
bvirt = by - bcy;
|
|
899
|
+
bcytail = by - (bcy + bvirt) + (bvirt - cy);
|
|
900
|
+
if (acxtail === 0 && acytail === 0 && bcxtail === 0 && bcytail === 0) {
|
|
901
|
+
return det;
|
|
902
|
+
}
|
|
903
|
+
errbound = ccwerrboundC * detsum + resulterrbound * Math.abs(det);
|
|
904
|
+
det += acx * bcytail + bcy * acxtail - (acy * bcxtail + bcx * acytail);
|
|
905
|
+
if (det >= errbound || -det >= errbound) return det;
|
|
906
|
+
s1 = acxtail * bcy;
|
|
907
|
+
c = splitter * acxtail;
|
|
908
|
+
ahi = c - (c - acxtail);
|
|
909
|
+
alo = acxtail - ahi;
|
|
910
|
+
c = splitter * bcy;
|
|
911
|
+
bhi = c - (c - bcy);
|
|
912
|
+
blo = bcy - bhi;
|
|
913
|
+
s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
|
|
914
|
+
t1 = acytail * bcx;
|
|
915
|
+
c = splitter * acytail;
|
|
916
|
+
ahi = c - (c - acytail);
|
|
917
|
+
alo = acytail - ahi;
|
|
918
|
+
c = splitter * bcx;
|
|
919
|
+
bhi = c - (c - bcx);
|
|
920
|
+
blo = bcx - bhi;
|
|
921
|
+
t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
|
|
922
|
+
_i = s0 - t0;
|
|
923
|
+
bvirt = s0 - _i;
|
|
924
|
+
u[0] = s0 - (_i + bvirt) + (bvirt - t0);
|
|
925
|
+
_j = s1 + _i;
|
|
926
|
+
bvirt = _j - s1;
|
|
927
|
+
_0 = s1 - (_j - bvirt) + (_i - bvirt);
|
|
928
|
+
_i = _0 - t1;
|
|
929
|
+
bvirt = _0 - _i;
|
|
930
|
+
u[1] = _0 - (_i + bvirt) + (bvirt - t1);
|
|
931
|
+
u3 = _j + _i;
|
|
932
|
+
bvirt = u3 - _j;
|
|
933
|
+
u[2] = _j - (u3 - bvirt) + (_i - bvirt);
|
|
934
|
+
u[3] = u3;
|
|
935
|
+
const C1len = sum(4, B, 4, u, C1);
|
|
936
|
+
s1 = acx * bcytail;
|
|
937
|
+
c = splitter * acx;
|
|
938
|
+
ahi = c - (c - acx);
|
|
939
|
+
alo = acx - ahi;
|
|
940
|
+
c = splitter * bcytail;
|
|
941
|
+
bhi = c - (c - bcytail);
|
|
942
|
+
blo = bcytail - bhi;
|
|
943
|
+
s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
|
|
944
|
+
t1 = acy * bcxtail;
|
|
945
|
+
c = splitter * acy;
|
|
946
|
+
ahi = c - (c - acy);
|
|
947
|
+
alo = acy - ahi;
|
|
948
|
+
c = splitter * bcxtail;
|
|
949
|
+
bhi = c - (c - bcxtail);
|
|
950
|
+
blo = bcxtail - bhi;
|
|
951
|
+
t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
|
|
952
|
+
_i = s0 - t0;
|
|
953
|
+
bvirt = s0 - _i;
|
|
954
|
+
u[0] = s0 - (_i + bvirt) + (bvirt - t0);
|
|
955
|
+
_j = s1 + _i;
|
|
956
|
+
bvirt = _j - s1;
|
|
957
|
+
_0 = s1 - (_j - bvirt) + (_i - bvirt);
|
|
958
|
+
_i = _0 - t1;
|
|
959
|
+
bvirt = _0 - _i;
|
|
960
|
+
u[1] = _0 - (_i + bvirt) + (bvirt - t1);
|
|
961
|
+
u3 = _j + _i;
|
|
962
|
+
bvirt = u3 - _j;
|
|
963
|
+
u[2] = _j - (u3 - bvirt) + (_i - bvirt);
|
|
964
|
+
u[3] = u3;
|
|
965
|
+
const C2len = sum(C1len, C1, 4, u, C2);
|
|
966
|
+
s1 = acxtail * bcytail;
|
|
967
|
+
c = splitter * acxtail;
|
|
968
|
+
ahi = c - (c - acxtail);
|
|
969
|
+
alo = acxtail - ahi;
|
|
970
|
+
c = splitter * bcytail;
|
|
971
|
+
bhi = c - (c - bcytail);
|
|
972
|
+
blo = bcytail - bhi;
|
|
973
|
+
s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
|
|
974
|
+
t1 = acytail * bcxtail;
|
|
975
|
+
c = splitter * acytail;
|
|
976
|
+
ahi = c - (c - acytail);
|
|
977
|
+
alo = acytail - ahi;
|
|
978
|
+
c = splitter * bcxtail;
|
|
979
|
+
bhi = c - (c - bcxtail);
|
|
980
|
+
blo = bcxtail - bhi;
|
|
981
|
+
t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
|
|
982
|
+
_i = s0 - t0;
|
|
983
|
+
bvirt = s0 - _i;
|
|
984
|
+
u[0] = s0 - (_i + bvirt) + (bvirt - t0);
|
|
985
|
+
_j = s1 + _i;
|
|
986
|
+
bvirt = _j - s1;
|
|
987
|
+
_0 = s1 - (_j - bvirt) + (_i - bvirt);
|
|
988
|
+
_i = _0 - t1;
|
|
989
|
+
bvirt = _0 - _i;
|
|
990
|
+
u[1] = _0 - (_i + bvirt) + (bvirt - t1);
|
|
991
|
+
u3 = _j + _i;
|
|
992
|
+
bvirt = u3 - _j;
|
|
993
|
+
u[2] = _j - (u3 - bvirt) + (_i - bvirt);
|
|
994
|
+
u[3] = u3;
|
|
995
|
+
const Dlen = sum(C2len, C2, 4, u, D);
|
|
996
|
+
return D[Dlen - 1];
|
|
332
997
|
}
|
|
333
|
-
function
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
998
|
+
function orient2d(ax, ay, bx, by, cx, cy) {
|
|
999
|
+
const detleft = (ay - cy) * (bx - cx);
|
|
1000
|
+
const detright = (ax - cx) * (by - cy);
|
|
1001
|
+
const det = detleft - detright;
|
|
1002
|
+
const detsum = Math.abs(detleft + detright);
|
|
1003
|
+
if (Math.abs(det) >= ccwerrboundA * detsum) return det;
|
|
1004
|
+
return -orient2dadapt(ax, ay, bx, by, cx, cy, detsum);
|
|
338
1005
|
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
1006
|
+
const crossProduct = (a, b) => a.x * b.y - a.y * b.x;
|
|
1007
|
+
const dotProduct = (a, b) => a.x * b.x + a.y * b.y;
|
|
1008
|
+
const compareVectorAngles = (basePt, endPt1, endPt2) => {
|
|
1009
|
+
const res = orient2d(basePt.x, basePt.y, endPt1.x, endPt1.y, endPt2.x, endPt2.y);
|
|
1010
|
+
if (res > 0) return -1;
|
|
1011
|
+
if (res < 0) return 1;
|
|
1012
|
+
return 0;
|
|
1013
|
+
};
|
|
1014
|
+
const length = (v) => Math.sqrt(dotProduct(v, v));
|
|
1015
|
+
const sineOfAngle = (pShared, pBase, pAngle) => {
|
|
1016
|
+
const vBase = {
|
|
1017
|
+
x: pBase.x - pShared.x,
|
|
1018
|
+
y: pBase.y - pShared.y
|
|
1019
|
+
};
|
|
1020
|
+
const vAngle = {
|
|
1021
|
+
x: pAngle.x - pShared.x,
|
|
1022
|
+
y: pAngle.y - pShared.y
|
|
1023
|
+
};
|
|
1024
|
+
return crossProduct(vAngle, vBase) / length(vAngle) / length(vBase);
|
|
1025
|
+
};
|
|
1026
|
+
const cosineOfAngle = (pShared, pBase, pAngle) => {
|
|
1027
|
+
const vBase = {
|
|
1028
|
+
x: pBase.x - pShared.x,
|
|
1029
|
+
y: pBase.y - pShared.y
|
|
1030
|
+
};
|
|
1031
|
+
const vAngle = {
|
|
1032
|
+
x: pAngle.x - pShared.x,
|
|
1033
|
+
y: pAngle.y - pShared.y
|
|
1034
|
+
};
|
|
1035
|
+
return dotProduct(vAngle, vBase) / length(vAngle) / length(vBase);
|
|
1036
|
+
};
|
|
1037
|
+
const horizontalIntersection = (pt, v, y) => {
|
|
1038
|
+
if (v.y === 0) return null;
|
|
1039
|
+
return {
|
|
1040
|
+
x: pt.x + v.x / v.y * (y - pt.y),
|
|
1041
|
+
y
|
|
1042
|
+
};
|
|
1043
|
+
};
|
|
1044
|
+
const verticalIntersection = (pt, v, x) => {
|
|
1045
|
+
if (v.x === 0) return null;
|
|
1046
|
+
return {
|
|
1047
|
+
x,
|
|
1048
|
+
y: pt.y + v.y / v.x * (x - pt.x)
|
|
342
1049
|
};
|
|
1050
|
+
};
|
|
1051
|
+
const intersection$1 = (pt1, v1, pt2, v2) => {
|
|
1052
|
+
if (v1.x === 0) return verticalIntersection(pt2, v2, pt1.x);
|
|
1053
|
+
if (v2.x === 0) return verticalIntersection(pt1, v1, pt2.x);
|
|
1054
|
+
if (v1.y === 0) return horizontalIntersection(pt2, v2, pt1.y);
|
|
1055
|
+
if (v2.y === 0) return horizontalIntersection(pt1, v1, pt2.y);
|
|
1056
|
+
const kross = crossProduct(v1, v2);
|
|
1057
|
+
if (kross == 0) return null;
|
|
1058
|
+
const ve = {
|
|
1059
|
+
x: pt2.x - pt1.x,
|
|
1060
|
+
y: pt2.y - pt1.y
|
|
1061
|
+
};
|
|
1062
|
+
const d1 = crossProduct(ve, v1) / kross;
|
|
1063
|
+
const d2 = crossProduct(ve, v2) / kross;
|
|
1064
|
+
const x1 = pt1.x + d2 * v1.x, x2 = pt2.x + d1 * v2.x;
|
|
1065
|
+
const y1 = pt1.y + d2 * v1.y, y2 = pt2.y + d1 * v2.y;
|
|
1066
|
+
const x = (x1 + x2) / 2;
|
|
1067
|
+
const y = (y1 + y2) / 2;
|
|
1068
|
+
return {
|
|
1069
|
+
x,
|
|
1070
|
+
y
|
|
1071
|
+
};
|
|
1072
|
+
};
|
|
1073
|
+
class SweepEvent {
|
|
1074
|
+
// for ordering sweep events in the sweep event queue
|
|
1075
|
+
static compare(a, b) {
|
|
1076
|
+
const ptCmp = SweepEvent.comparePoints(a.point, b.point);
|
|
1077
|
+
if (ptCmp !== 0) return ptCmp;
|
|
1078
|
+
if (a.point !== b.point) a.link(b);
|
|
1079
|
+
if (a.isLeft !== b.isLeft) return a.isLeft ? 1 : -1;
|
|
1080
|
+
return Segment.compare(a.segment, b.segment);
|
|
1081
|
+
}
|
|
1082
|
+
// for ordering points in sweep line order
|
|
1083
|
+
static comparePoints(aPt, bPt) {
|
|
1084
|
+
if (aPt.x < bPt.x) return -1;
|
|
1085
|
+
if (aPt.x > bPt.x) return 1;
|
|
1086
|
+
if (aPt.y < bPt.y) return -1;
|
|
1087
|
+
if (aPt.y > bPt.y) return 1;
|
|
1088
|
+
return 0;
|
|
1089
|
+
}
|
|
1090
|
+
// Warning: 'point' input will be modified and re-used (for performance)
|
|
1091
|
+
constructor(point, isLeft) {
|
|
1092
|
+
if (point.events === void 0) point.events = [this];
|
|
1093
|
+
else point.events.push(this);
|
|
1094
|
+
this.point = point;
|
|
1095
|
+
this.isLeft = isLeft;
|
|
1096
|
+
}
|
|
1097
|
+
link(other) {
|
|
1098
|
+
if (other.point === this.point) {
|
|
1099
|
+
throw new Error("Tried to link already linked events");
|
|
1100
|
+
}
|
|
1101
|
+
const otherEvents = other.point.events;
|
|
1102
|
+
for (let i = 0, iMax = otherEvents.length; i < iMax; i++) {
|
|
1103
|
+
const evt = otherEvents[i];
|
|
1104
|
+
this.point.events.push(evt);
|
|
1105
|
+
evt.point = this.point;
|
|
1106
|
+
}
|
|
1107
|
+
this.checkForConsuming();
|
|
1108
|
+
}
|
|
1109
|
+
/* Do a pass over our linked events and check to see if any pair
|
|
1110
|
+
* of segments match, and should be consumed. */
|
|
1111
|
+
checkForConsuming() {
|
|
1112
|
+
const numEvents = this.point.events.length;
|
|
1113
|
+
for (let i = 0; i < numEvents; i++) {
|
|
1114
|
+
const evt1 = this.point.events[i];
|
|
1115
|
+
if (evt1.segment.consumedBy !== void 0) continue;
|
|
1116
|
+
for (let j = i + 1; j < numEvents; j++) {
|
|
1117
|
+
const evt2 = this.point.events[j];
|
|
1118
|
+
if (evt2.consumedBy !== void 0) continue;
|
|
1119
|
+
if (evt1.otherSE.point.events !== evt2.otherSE.point.events) continue;
|
|
1120
|
+
evt1.segment.consume(evt2.segment);
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
getAvailableLinkedEvents() {
|
|
1125
|
+
const events = [];
|
|
1126
|
+
for (let i = 0, iMax = this.point.events.length; i < iMax; i++) {
|
|
1127
|
+
const evt = this.point.events[i];
|
|
1128
|
+
if (evt !== this && !evt.segment.ringOut && evt.segment.isInResult()) {
|
|
1129
|
+
events.push(evt);
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
return events;
|
|
1133
|
+
}
|
|
1134
|
+
/**
|
|
1135
|
+
* Returns a comparator function for sorting linked events that will
|
|
1136
|
+
* favor the event that will give us the smallest left-side angle.
|
|
1137
|
+
* All ring construction starts as low as possible heading to the right,
|
|
1138
|
+
* so by always turning left as sharp as possible we'll get polygons
|
|
1139
|
+
* without uncessary loops & holes.
|
|
1140
|
+
*
|
|
1141
|
+
* The comparator function has a compute cache such that it avoids
|
|
1142
|
+
* re-computing already-computed values.
|
|
1143
|
+
*/
|
|
1144
|
+
getLeftmostComparator(baseEvent) {
|
|
1145
|
+
const cache = /* @__PURE__ */ new Map();
|
|
1146
|
+
const fillCache = (linkedEvent) => {
|
|
1147
|
+
const nextEvent = linkedEvent.otherSE;
|
|
1148
|
+
cache.set(linkedEvent, {
|
|
1149
|
+
sine: sineOfAngle(this.point, baseEvent.point, nextEvent.point),
|
|
1150
|
+
cosine: cosineOfAngle(this.point, baseEvent.point, nextEvent.point)
|
|
1151
|
+
});
|
|
1152
|
+
};
|
|
1153
|
+
return (a, b) => {
|
|
1154
|
+
if (!cache.has(a)) fillCache(a);
|
|
1155
|
+
if (!cache.has(b)) fillCache(b);
|
|
1156
|
+
const {
|
|
1157
|
+
sine: asine,
|
|
1158
|
+
cosine: acosine
|
|
1159
|
+
} = cache.get(a);
|
|
1160
|
+
const {
|
|
1161
|
+
sine: bsine,
|
|
1162
|
+
cosine: bcosine
|
|
1163
|
+
} = cache.get(b);
|
|
1164
|
+
if (asine >= 0 && bsine >= 0) {
|
|
1165
|
+
if (acosine < bcosine) return 1;
|
|
1166
|
+
if (acosine > bcosine) return -1;
|
|
1167
|
+
return 0;
|
|
1168
|
+
}
|
|
1169
|
+
if (asine < 0 && bsine < 0) {
|
|
1170
|
+
if (acosine < bcosine) return -1;
|
|
1171
|
+
if (acosine > bcosine) return 1;
|
|
1172
|
+
return 0;
|
|
1173
|
+
}
|
|
1174
|
+
if (bsine < asine) return -1;
|
|
1175
|
+
if (bsine > asine) return 1;
|
|
1176
|
+
return 0;
|
|
1177
|
+
};
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
let segmentId = 0;
|
|
1181
|
+
class Segment {
|
|
1182
|
+
/* This compare() function is for ordering segments in the sweep
|
|
1183
|
+
* line tree, and does so according to the following criteria:
|
|
1184
|
+
*
|
|
1185
|
+
* Consider the vertical line that lies an infinestimal step to the
|
|
1186
|
+
* right of the right-more of the two left endpoints of the input
|
|
1187
|
+
* segments. Imagine slowly moving a point up from negative infinity
|
|
1188
|
+
* in the increasing y direction. Which of the two segments will that
|
|
1189
|
+
* point intersect first? That segment comes 'before' the other one.
|
|
1190
|
+
*
|
|
1191
|
+
* If neither segment would be intersected by such a line, (if one
|
|
1192
|
+
* or more of the segments are vertical) then the line to be considered
|
|
1193
|
+
* is directly on the right-more of the two left inputs.
|
|
1194
|
+
*/
|
|
1195
|
+
static compare(a, b) {
|
|
1196
|
+
const alx = a.leftSE.point.x;
|
|
1197
|
+
const blx = b.leftSE.point.x;
|
|
1198
|
+
const arx = a.rightSE.point.x;
|
|
1199
|
+
const brx = b.rightSE.point.x;
|
|
1200
|
+
if (brx < alx) return 1;
|
|
1201
|
+
if (arx < blx) return -1;
|
|
1202
|
+
const aly = a.leftSE.point.y;
|
|
1203
|
+
const bly = b.leftSE.point.y;
|
|
1204
|
+
const ary = a.rightSE.point.y;
|
|
1205
|
+
const bry = b.rightSE.point.y;
|
|
1206
|
+
if (alx < blx) {
|
|
1207
|
+
if (bly < aly && bly < ary) return 1;
|
|
1208
|
+
if (bly > aly && bly > ary) return -1;
|
|
1209
|
+
const aCmpBLeft = a.comparePoint(b.leftSE.point);
|
|
1210
|
+
if (aCmpBLeft < 0) return 1;
|
|
1211
|
+
if (aCmpBLeft > 0) return -1;
|
|
1212
|
+
const bCmpARight = b.comparePoint(a.rightSE.point);
|
|
1213
|
+
if (bCmpARight !== 0) return bCmpARight;
|
|
1214
|
+
return -1;
|
|
1215
|
+
}
|
|
1216
|
+
if (alx > blx) {
|
|
1217
|
+
if (aly < bly && aly < bry) return -1;
|
|
1218
|
+
if (aly > bly && aly > bry) return 1;
|
|
1219
|
+
const bCmpALeft = b.comparePoint(a.leftSE.point);
|
|
1220
|
+
if (bCmpALeft !== 0) return bCmpALeft;
|
|
1221
|
+
const aCmpBRight = a.comparePoint(b.rightSE.point);
|
|
1222
|
+
if (aCmpBRight < 0) return 1;
|
|
1223
|
+
if (aCmpBRight > 0) return -1;
|
|
1224
|
+
return 1;
|
|
1225
|
+
}
|
|
1226
|
+
if (aly < bly) return -1;
|
|
1227
|
+
if (aly > bly) return 1;
|
|
1228
|
+
if (arx < brx) {
|
|
1229
|
+
const bCmpARight = b.comparePoint(a.rightSE.point);
|
|
1230
|
+
if (bCmpARight !== 0) return bCmpARight;
|
|
1231
|
+
}
|
|
1232
|
+
if (arx > brx) {
|
|
1233
|
+
const aCmpBRight = a.comparePoint(b.rightSE.point);
|
|
1234
|
+
if (aCmpBRight < 0) return 1;
|
|
1235
|
+
if (aCmpBRight > 0) return -1;
|
|
1236
|
+
}
|
|
1237
|
+
if (arx !== brx) {
|
|
1238
|
+
const ay = ary - aly;
|
|
1239
|
+
const ax = arx - alx;
|
|
1240
|
+
const by = bry - bly;
|
|
1241
|
+
const bx = brx - blx;
|
|
1242
|
+
if (ay > ax && by < bx) return 1;
|
|
1243
|
+
if (ay < ax && by > bx) return -1;
|
|
1244
|
+
}
|
|
1245
|
+
if (arx > brx) return 1;
|
|
1246
|
+
if (arx < brx) return -1;
|
|
1247
|
+
if (ary < bry) return -1;
|
|
1248
|
+
if (ary > bry) return 1;
|
|
1249
|
+
if (a.id < b.id) return -1;
|
|
1250
|
+
if (a.id > b.id) return 1;
|
|
1251
|
+
return 0;
|
|
1252
|
+
}
|
|
1253
|
+
/* Warning: a reference to ringWindings input will be stored,
|
|
1254
|
+
* and possibly will be later modified */
|
|
1255
|
+
constructor(leftSE, rightSE, rings, windings) {
|
|
1256
|
+
this.id = ++segmentId;
|
|
1257
|
+
this.leftSE = leftSE;
|
|
1258
|
+
leftSE.segment = this;
|
|
1259
|
+
leftSE.otherSE = rightSE;
|
|
1260
|
+
this.rightSE = rightSE;
|
|
1261
|
+
rightSE.segment = this;
|
|
1262
|
+
rightSE.otherSE = leftSE;
|
|
1263
|
+
this.rings = rings;
|
|
1264
|
+
this.windings = windings;
|
|
1265
|
+
}
|
|
1266
|
+
static fromRing(pt1, pt2, ring) {
|
|
1267
|
+
let leftPt, rightPt, winding;
|
|
1268
|
+
const cmpPts = SweepEvent.comparePoints(pt1, pt2);
|
|
1269
|
+
if (cmpPts < 0) {
|
|
1270
|
+
leftPt = pt1;
|
|
1271
|
+
rightPt = pt2;
|
|
1272
|
+
winding = 1;
|
|
1273
|
+
} else if (cmpPts > 0) {
|
|
1274
|
+
leftPt = pt2;
|
|
1275
|
+
rightPt = pt1;
|
|
1276
|
+
winding = -1;
|
|
1277
|
+
} else throw new Error(`Tried to create degenerate segment at [${pt1.x}, ${pt1.y}]`);
|
|
1278
|
+
const leftSE = new SweepEvent(leftPt, true);
|
|
1279
|
+
const rightSE = new SweepEvent(rightPt, false);
|
|
1280
|
+
return new Segment(leftSE, rightSE, [ring], [winding]);
|
|
1281
|
+
}
|
|
1282
|
+
/* When a segment is split, the rightSE is replaced with a new sweep event */
|
|
1283
|
+
replaceRightSE(newRightSE) {
|
|
1284
|
+
this.rightSE = newRightSE;
|
|
1285
|
+
this.rightSE.segment = this;
|
|
1286
|
+
this.rightSE.otherSE = this.leftSE;
|
|
1287
|
+
this.leftSE.otherSE = this.rightSE;
|
|
1288
|
+
}
|
|
1289
|
+
bbox() {
|
|
1290
|
+
const y1 = this.leftSE.point.y;
|
|
1291
|
+
const y2 = this.rightSE.point.y;
|
|
1292
|
+
return {
|
|
1293
|
+
ll: {
|
|
1294
|
+
x: this.leftSE.point.x,
|
|
1295
|
+
y: y1 < y2 ? y1 : y2
|
|
1296
|
+
},
|
|
1297
|
+
ur: {
|
|
1298
|
+
x: this.rightSE.point.x,
|
|
1299
|
+
y: y1 > y2 ? y1 : y2
|
|
1300
|
+
}
|
|
1301
|
+
};
|
|
1302
|
+
}
|
|
1303
|
+
/* A vector from the left point to the right */
|
|
1304
|
+
vector() {
|
|
1305
|
+
return {
|
|
1306
|
+
x: this.rightSE.point.x - this.leftSE.point.x,
|
|
1307
|
+
y: this.rightSE.point.y - this.leftSE.point.y
|
|
1308
|
+
};
|
|
1309
|
+
}
|
|
1310
|
+
isAnEndpoint(pt) {
|
|
1311
|
+
return pt.x === this.leftSE.point.x && pt.y === this.leftSE.point.y || pt.x === this.rightSE.point.x && pt.y === this.rightSE.point.y;
|
|
1312
|
+
}
|
|
1313
|
+
/* Compare this segment with a point.
|
|
1314
|
+
*
|
|
1315
|
+
* A point P is considered to be colinear to a segment if there
|
|
1316
|
+
* exists a distance D such that if we travel along the segment
|
|
1317
|
+
* from one * endpoint towards the other a distance D, we find
|
|
1318
|
+
* ourselves at point P.
|
|
1319
|
+
*
|
|
1320
|
+
* Return value indicates:
|
|
1321
|
+
*
|
|
1322
|
+
* 1: point lies above the segment (to the left of vertical)
|
|
1323
|
+
* 0: point is colinear to segment
|
|
1324
|
+
* -1: point lies below the segment (to the right of vertical)
|
|
1325
|
+
*/
|
|
1326
|
+
comparePoint(point) {
|
|
1327
|
+
if (this.isAnEndpoint(point)) return 0;
|
|
1328
|
+
const lPt = this.leftSE.point;
|
|
1329
|
+
const rPt = this.rightSE.point;
|
|
1330
|
+
const v = this.vector();
|
|
1331
|
+
if (lPt.x === rPt.x) {
|
|
1332
|
+
if (point.x === lPt.x) return 0;
|
|
1333
|
+
return point.x < lPt.x ? 1 : -1;
|
|
1334
|
+
}
|
|
1335
|
+
const yDist = (point.y - lPt.y) / v.y;
|
|
1336
|
+
const xFromYDist = lPt.x + yDist * v.x;
|
|
1337
|
+
if (point.x === xFromYDist) return 0;
|
|
1338
|
+
const xDist = (point.x - lPt.x) / v.x;
|
|
1339
|
+
const yFromXDist = lPt.y + xDist * v.y;
|
|
1340
|
+
if (point.y === yFromXDist) return 0;
|
|
1341
|
+
return point.y < yFromXDist ? -1 : 1;
|
|
1342
|
+
}
|
|
1343
|
+
/**
|
|
1344
|
+
* Given another segment, returns the first non-trivial intersection
|
|
1345
|
+
* between the two segments (in terms of sweep line ordering), if it exists.
|
|
1346
|
+
*
|
|
1347
|
+
* A 'non-trivial' intersection is one that will cause one or both of the
|
|
1348
|
+
* segments to be split(). As such, 'trivial' vs. 'non-trivial' intersection:
|
|
1349
|
+
*
|
|
1350
|
+
* * endpoint of segA with endpoint of segB --> trivial
|
|
1351
|
+
* * endpoint of segA with point along segB --> non-trivial
|
|
1352
|
+
* * endpoint of segB with point along segA --> non-trivial
|
|
1353
|
+
* * point along segA with point along segB --> non-trivial
|
|
1354
|
+
*
|
|
1355
|
+
* If no non-trivial intersection exists, return null
|
|
1356
|
+
* Else, return null.
|
|
1357
|
+
*/
|
|
1358
|
+
getIntersection(other) {
|
|
1359
|
+
const tBbox = this.bbox();
|
|
1360
|
+
const oBbox = other.bbox();
|
|
1361
|
+
const bboxOverlap = getBboxOverlap(tBbox, oBbox);
|
|
1362
|
+
if (bboxOverlap === null) return null;
|
|
1363
|
+
const tlp = this.leftSE.point;
|
|
1364
|
+
const trp = this.rightSE.point;
|
|
1365
|
+
const olp = other.leftSE.point;
|
|
1366
|
+
const orp = other.rightSE.point;
|
|
1367
|
+
const touchesOtherLSE = isInBbox(tBbox, olp) && this.comparePoint(olp) === 0;
|
|
1368
|
+
const touchesThisLSE = isInBbox(oBbox, tlp) && other.comparePoint(tlp) === 0;
|
|
1369
|
+
const touchesOtherRSE = isInBbox(tBbox, orp) && this.comparePoint(orp) === 0;
|
|
1370
|
+
const touchesThisRSE = isInBbox(oBbox, trp) && other.comparePoint(trp) === 0;
|
|
1371
|
+
if (touchesThisLSE && touchesOtherLSE) {
|
|
1372
|
+
if (touchesThisRSE && !touchesOtherRSE) return trp;
|
|
1373
|
+
if (!touchesThisRSE && touchesOtherRSE) return orp;
|
|
1374
|
+
return null;
|
|
1375
|
+
}
|
|
1376
|
+
if (touchesThisLSE) {
|
|
1377
|
+
if (touchesOtherRSE) {
|
|
1378
|
+
if (tlp.x === orp.x && tlp.y === orp.y) return null;
|
|
1379
|
+
}
|
|
1380
|
+
return tlp;
|
|
1381
|
+
}
|
|
1382
|
+
if (touchesOtherLSE) {
|
|
1383
|
+
if (touchesThisRSE) {
|
|
1384
|
+
if (trp.x === olp.x && trp.y === olp.y) return null;
|
|
1385
|
+
}
|
|
1386
|
+
return olp;
|
|
1387
|
+
}
|
|
1388
|
+
if (touchesThisRSE && touchesOtherRSE) return null;
|
|
1389
|
+
if (touchesThisRSE) return trp;
|
|
1390
|
+
if (touchesOtherRSE) return orp;
|
|
1391
|
+
const pt = intersection$1(tlp, this.vector(), olp, other.vector());
|
|
1392
|
+
if (pt === null) return null;
|
|
1393
|
+
if (!isInBbox(bboxOverlap, pt)) return null;
|
|
1394
|
+
return rounder.round(pt.x, pt.y);
|
|
1395
|
+
}
|
|
1396
|
+
/**
|
|
1397
|
+
* Split the given segment into multiple segments on the given points.
|
|
1398
|
+
* * Each existing segment will retain its leftSE and a new rightSE will be
|
|
1399
|
+
* generated for it.
|
|
1400
|
+
* * A new segment will be generated which will adopt the original segment's
|
|
1401
|
+
* rightSE, and a new leftSE will be generated for it.
|
|
1402
|
+
* * If there are more than two points given to split on, new segments
|
|
1403
|
+
* in the middle will be generated with new leftSE and rightSE's.
|
|
1404
|
+
* * An array of the newly generated SweepEvents will be returned.
|
|
1405
|
+
*
|
|
1406
|
+
* Warning: input array of points is modified
|
|
1407
|
+
*/
|
|
1408
|
+
split(point) {
|
|
1409
|
+
const newEvents = [];
|
|
1410
|
+
const alreadyLinked = point.events !== void 0;
|
|
1411
|
+
const newLeftSE = new SweepEvent(point, true);
|
|
1412
|
+
const newRightSE = new SweepEvent(point, false);
|
|
1413
|
+
const oldRightSE = this.rightSE;
|
|
1414
|
+
this.replaceRightSE(newRightSE);
|
|
1415
|
+
newEvents.push(newRightSE);
|
|
1416
|
+
newEvents.push(newLeftSE);
|
|
1417
|
+
const newSeg = new Segment(newLeftSE, oldRightSE, this.rings.slice(), this.windings.slice());
|
|
1418
|
+
if (SweepEvent.comparePoints(newSeg.leftSE.point, newSeg.rightSE.point) > 0) {
|
|
1419
|
+
newSeg.swapEvents();
|
|
1420
|
+
}
|
|
1421
|
+
if (SweepEvent.comparePoints(this.leftSE.point, this.rightSE.point) > 0) {
|
|
1422
|
+
this.swapEvents();
|
|
1423
|
+
}
|
|
1424
|
+
if (alreadyLinked) {
|
|
1425
|
+
newLeftSE.checkForConsuming();
|
|
1426
|
+
newRightSE.checkForConsuming();
|
|
1427
|
+
}
|
|
1428
|
+
return newEvents;
|
|
1429
|
+
}
|
|
1430
|
+
/* Swap which event is left and right */
|
|
1431
|
+
swapEvents() {
|
|
1432
|
+
const tmpEvt = this.rightSE;
|
|
1433
|
+
this.rightSE = this.leftSE;
|
|
1434
|
+
this.leftSE = tmpEvt;
|
|
1435
|
+
this.leftSE.isLeft = true;
|
|
1436
|
+
this.rightSE.isLeft = false;
|
|
1437
|
+
for (let i = 0, iMax = this.windings.length; i < iMax; i++) {
|
|
1438
|
+
this.windings[i] *= -1;
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
/* Consume another segment. We take their rings under our wing
|
|
1442
|
+
* and mark them as consumed. Use for perfectly overlapping segments */
|
|
1443
|
+
consume(other) {
|
|
1444
|
+
let consumer = this;
|
|
1445
|
+
let consumee = other;
|
|
1446
|
+
while (consumer.consumedBy) consumer = consumer.consumedBy;
|
|
1447
|
+
while (consumee.consumedBy) consumee = consumee.consumedBy;
|
|
1448
|
+
const cmp2 = Segment.compare(consumer, consumee);
|
|
1449
|
+
if (cmp2 === 0) return;
|
|
1450
|
+
if (cmp2 > 0) {
|
|
1451
|
+
const tmp = consumer;
|
|
1452
|
+
consumer = consumee;
|
|
1453
|
+
consumee = tmp;
|
|
1454
|
+
}
|
|
1455
|
+
if (consumer.prev === consumee) {
|
|
1456
|
+
const tmp = consumer;
|
|
1457
|
+
consumer = consumee;
|
|
1458
|
+
consumee = tmp;
|
|
1459
|
+
}
|
|
1460
|
+
for (let i = 0, iMax = consumee.rings.length; i < iMax; i++) {
|
|
1461
|
+
const ring = consumee.rings[i];
|
|
1462
|
+
const winding = consumee.windings[i];
|
|
1463
|
+
const index2 = consumer.rings.indexOf(ring);
|
|
1464
|
+
if (index2 === -1) {
|
|
1465
|
+
consumer.rings.push(ring);
|
|
1466
|
+
consumer.windings.push(winding);
|
|
1467
|
+
} else consumer.windings[index2] += winding;
|
|
1468
|
+
}
|
|
1469
|
+
consumee.rings = null;
|
|
1470
|
+
consumee.windings = null;
|
|
1471
|
+
consumee.consumedBy = consumer;
|
|
1472
|
+
consumee.leftSE.consumedBy = consumer.leftSE;
|
|
1473
|
+
consumee.rightSE.consumedBy = consumer.rightSE;
|
|
1474
|
+
}
|
|
1475
|
+
/* The first segment previous segment chain that is in the result */
|
|
1476
|
+
prevInResult() {
|
|
1477
|
+
if (this._prevInResult !== void 0) return this._prevInResult;
|
|
1478
|
+
if (!this.prev) this._prevInResult = null;
|
|
1479
|
+
else if (this.prev.isInResult()) this._prevInResult = this.prev;
|
|
1480
|
+
else this._prevInResult = this.prev.prevInResult();
|
|
1481
|
+
return this._prevInResult;
|
|
1482
|
+
}
|
|
1483
|
+
beforeState() {
|
|
1484
|
+
if (this._beforeState !== void 0) return this._beforeState;
|
|
1485
|
+
if (!this.prev) this._beforeState = {
|
|
1486
|
+
rings: [],
|
|
1487
|
+
windings: [],
|
|
1488
|
+
multiPolys: []
|
|
1489
|
+
};
|
|
1490
|
+
else {
|
|
1491
|
+
const seg = this.prev.consumedBy || this.prev;
|
|
1492
|
+
this._beforeState = seg.afterState();
|
|
1493
|
+
}
|
|
1494
|
+
return this._beforeState;
|
|
1495
|
+
}
|
|
1496
|
+
afterState() {
|
|
1497
|
+
if (this._afterState !== void 0) return this._afterState;
|
|
1498
|
+
const beforeState = this.beforeState();
|
|
1499
|
+
this._afterState = {
|
|
1500
|
+
rings: beforeState.rings.slice(0),
|
|
1501
|
+
windings: beforeState.windings.slice(0),
|
|
1502
|
+
multiPolys: []
|
|
1503
|
+
};
|
|
1504
|
+
const ringsAfter = this._afterState.rings;
|
|
1505
|
+
const windingsAfter = this._afterState.windings;
|
|
1506
|
+
const mpsAfter = this._afterState.multiPolys;
|
|
1507
|
+
for (let i = 0, iMax = this.rings.length; i < iMax; i++) {
|
|
1508
|
+
const ring = this.rings[i];
|
|
1509
|
+
const winding = this.windings[i];
|
|
1510
|
+
const index2 = ringsAfter.indexOf(ring);
|
|
1511
|
+
if (index2 === -1) {
|
|
1512
|
+
ringsAfter.push(ring);
|
|
1513
|
+
windingsAfter.push(winding);
|
|
1514
|
+
} else windingsAfter[index2] += winding;
|
|
1515
|
+
}
|
|
1516
|
+
const polysAfter = [];
|
|
1517
|
+
const polysExclude = [];
|
|
1518
|
+
for (let i = 0, iMax = ringsAfter.length; i < iMax; i++) {
|
|
1519
|
+
if (windingsAfter[i] === 0) continue;
|
|
1520
|
+
const ring = ringsAfter[i];
|
|
1521
|
+
const poly = ring.poly;
|
|
1522
|
+
if (polysExclude.indexOf(poly) !== -1) continue;
|
|
1523
|
+
if (ring.isExterior) polysAfter.push(poly);
|
|
1524
|
+
else {
|
|
1525
|
+
if (polysExclude.indexOf(poly) === -1) polysExclude.push(poly);
|
|
1526
|
+
const index2 = polysAfter.indexOf(ring.poly);
|
|
1527
|
+
if (index2 !== -1) polysAfter.splice(index2, 1);
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
for (let i = 0, iMax = polysAfter.length; i < iMax; i++) {
|
|
1531
|
+
const mp = polysAfter[i].multiPoly;
|
|
1532
|
+
if (mpsAfter.indexOf(mp) === -1) mpsAfter.push(mp);
|
|
1533
|
+
}
|
|
1534
|
+
return this._afterState;
|
|
1535
|
+
}
|
|
1536
|
+
/* Is this segment part of the final result? */
|
|
1537
|
+
isInResult() {
|
|
1538
|
+
if (this.consumedBy) return false;
|
|
1539
|
+
if (this._isInResult !== void 0) return this._isInResult;
|
|
1540
|
+
const mpsBefore = this.beforeState().multiPolys;
|
|
1541
|
+
const mpsAfter = this.afterState().multiPolys;
|
|
1542
|
+
switch (operation.type) {
|
|
1543
|
+
case "union": {
|
|
1544
|
+
const noBefores = mpsBefore.length === 0;
|
|
1545
|
+
const noAfters = mpsAfter.length === 0;
|
|
1546
|
+
this._isInResult = noBefores !== noAfters;
|
|
1547
|
+
break;
|
|
1548
|
+
}
|
|
1549
|
+
case "intersection": {
|
|
1550
|
+
let least;
|
|
1551
|
+
let most;
|
|
1552
|
+
if (mpsBefore.length < mpsAfter.length) {
|
|
1553
|
+
least = mpsBefore.length;
|
|
1554
|
+
most = mpsAfter.length;
|
|
1555
|
+
} else {
|
|
1556
|
+
least = mpsAfter.length;
|
|
1557
|
+
most = mpsBefore.length;
|
|
1558
|
+
}
|
|
1559
|
+
this._isInResult = most === operation.numMultiPolys && least < most;
|
|
1560
|
+
break;
|
|
1561
|
+
}
|
|
1562
|
+
case "xor": {
|
|
1563
|
+
const diff = Math.abs(mpsBefore.length - mpsAfter.length);
|
|
1564
|
+
this._isInResult = diff % 2 === 1;
|
|
1565
|
+
break;
|
|
1566
|
+
}
|
|
1567
|
+
case "difference": {
|
|
1568
|
+
const isJustSubject = (mps) => mps.length === 1 && mps[0].isSubject;
|
|
1569
|
+
this._isInResult = isJustSubject(mpsBefore) !== isJustSubject(mpsAfter);
|
|
1570
|
+
break;
|
|
1571
|
+
}
|
|
1572
|
+
default:
|
|
1573
|
+
throw new Error(`Unrecognized operation type found ${operation.type}`);
|
|
1574
|
+
}
|
|
1575
|
+
return this._isInResult;
|
|
1576
|
+
}
|
|
343
1577
|
}
|
|
344
|
-
|
|
345
|
-
|
|
1578
|
+
class RingIn {
|
|
1579
|
+
constructor(geomRing, poly, isExterior) {
|
|
1580
|
+
if (!Array.isArray(geomRing) || geomRing.length === 0) {
|
|
1581
|
+
throw new Error("Input geometry is not a valid Polygon or MultiPolygon");
|
|
1582
|
+
}
|
|
1583
|
+
this.poly = poly;
|
|
1584
|
+
this.isExterior = isExterior;
|
|
1585
|
+
this.segments = [];
|
|
1586
|
+
if (typeof geomRing[0][0] !== "number" || typeof geomRing[0][1] !== "number") {
|
|
1587
|
+
throw new Error("Input geometry is not a valid Polygon or MultiPolygon");
|
|
1588
|
+
}
|
|
1589
|
+
const firstPoint = rounder.round(geomRing[0][0], geomRing[0][1]);
|
|
1590
|
+
this.bbox = {
|
|
1591
|
+
ll: {
|
|
1592
|
+
x: firstPoint.x,
|
|
1593
|
+
y: firstPoint.y
|
|
1594
|
+
},
|
|
1595
|
+
ur: {
|
|
1596
|
+
x: firstPoint.x,
|
|
1597
|
+
y: firstPoint.y
|
|
1598
|
+
}
|
|
1599
|
+
};
|
|
1600
|
+
let prevPoint = firstPoint;
|
|
1601
|
+
for (let i = 1, iMax = geomRing.length; i < iMax; i++) {
|
|
1602
|
+
if (typeof geomRing[i][0] !== "number" || typeof geomRing[i][1] !== "number") {
|
|
1603
|
+
throw new Error("Input geometry is not a valid Polygon or MultiPolygon");
|
|
1604
|
+
}
|
|
1605
|
+
let point = rounder.round(geomRing[i][0], geomRing[i][1]);
|
|
1606
|
+
if (point.x === prevPoint.x && point.y === prevPoint.y) continue;
|
|
1607
|
+
this.segments.push(Segment.fromRing(prevPoint, point, this));
|
|
1608
|
+
if (point.x < this.bbox.ll.x) this.bbox.ll.x = point.x;
|
|
1609
|
+
if (point.y < this.bbox.ll.y) this.bbox.ll.y = point.y;
|
|
1610
|
+
if (point.x > this.bbox.ur.x) this.bbox.ur.x = point.x;
|
|
1611
|
+
if (point.y > this.bbox.ur.y) this.bbox.ur.y = point.y;
|
|
1612
|
+
prevPoint = point;
|
|
1613
|
+
}
|
|
1614
|
+
if (firstPoint.x !== prevPoint.x || firstPoint.y !== prevPoint.y) {
|
|
1615
|
+
this.segments.push(Segment.fromRing(prevPoint, firstPoint, this));
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
getSweepEvents() {
|
|
1619
|
+
const sweepEvents = [];
|
|
1620
|
+
for (let i = 0, iMax = this.segments.length; i < iMax; i++) {
|
|
1621
|
+
const segment = this.segments[i];
|
|
1622
|
+
sweepEvents.push(segment.leftSE);
|
|
1623
|
+
sweepEvents.push(segment.rightSE);
|
|
1624
|
+
}
|
|
1625
|
+
return sweepEvents;
|
|
1626
|
+
}
|
|
346
1627
|
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
1628
|
+
class PolyIn {
|
|
1629
|
+
constructor(geomPoly, multiPoly) {
|
|
1630
|
+
if (!Array.isArray(geomPoly)) {
|
|
1631
|
+
throw new Error("Input geometry is not a valid Polygon or MultiPolygon");
|
|
1632
|
+
}
|
|
1633
|
+
this.exteriorRing = new RingIn(geomPoly[0], this, true);
|
|
1634
|
+
this.bbox = {
|
|
1635
|
+
ll: {
|
|
1636
|
+
x: this.exteriorRing.bbox.ll.x,
|
|
1637
|
+
y: this.exteriorRing.bbox.ll.y
|
|
1638
|
+
},
|
|
1639
|
+
ur: {
|
|
1640
|
+
x: this.exteriorRing.bbox.ur.x,
|
|
1641
|
+
y: this.exteriorRing.bbox.ur.y
|
|
1642
|
+
}
|
|
1643
|
+
};
|
|
1644
|
+
this.interiorRings = [];
|
|
1645
|
+
for (let i = 1, iMax = geomPoly.length; i < iMax; i++) {
|
|
1646
|
+
const ring = new RingIn(geomPoly[i], this, false);
|
|
1647
|
+
if (ring.bbox.ll.x < this.bbox.ll.x) this.bbox.ll.x = ring.bbox.ll.x;
|
|
1648
|
+
if (ring.bbox.ll.y < this.bbox.ll.y) this.bbox.ll.y = ring.bbox.ll.y;
|
|
1649
|
+
if (ring.bbox.ur.x > this.bbox.ur.x) this.bbox.ur.x = ring.bbox.ur.x;
|
|
1650
|
+
if (ring.bbox.ur.y > this.bbox.ur.y) this.bbox.ur.y = ring.bbox.ur.y;
|
|
1651
|
+
this.interiorRings.push(ring);
|
|
1652
|
+
}
|
|
1653
|
+
this.multiPoly = multiPoly;
|
|
1654
|
+
}
|
|
1655
|
+
getSweepEvents() {
|
|
1656
|
+
const sweepEvents = this.exteriorRing.getSweepEvents();
|
|
1657
|
+
for (let i = 0, iMax = this.interiorRings.length; i < iMax; i++) {
|
|
1658
|
+
const ringSweepEvents = this.interiorRings[i].getSweepEvents();
|
|
1659
|
+
for (let j = 0, jMax = ringSweepEvents.length; j < jMax; j++) {
|
|
1660
|
+
sweepEvents.push(ringSweepEvents[j]);
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
return sweepEvents;
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
class MultiPolyIn {
|
|
1667
|
+
constructor(geom, isSubject) {
|
|
1668
|
+
if (!Array.isArray(geom)) {
|
|
1669
|
+
throw new Error("Input geometry is not a valid Polygon or MultiPolygon");
|
|
1670
|
+
}
|
|
1671
|
+
try {
|
|
1672
|
+
if (typeof geom[0][0][0] === "number") geom = [geom];
|
|
1673
|
+
} catch (ex) {
|
|
1674
|
+
}
|
|
1675
|
+
this.polys = [];
|
|
1676
|
+
this.bbox = {
|
|
1677
|
+
ll: {
|
|
1678
|
+
x: Number.POSITIVE_INFINITY,
|
|
1679
|
+
y: Number.POSITIVE_INFINITY
|
|
1680
|
+
},
|
|
1681
|
+
ur: {
|
|
1682
|
+
x: Number.NEGATIVE_INFINITY,
|
|
1683
|
+
y: Number.NEGATIVE_INFINITY
|
|
1684
|
+
}
|
|
1685
|
+
};
|
|
1686
|
+
for (let i = 0, iMax = geom.length; i < iMax; i++) {
|
|
1687
|
+
const poly = new PolyIn(geom[i], this);
|
|
1688
|
+
if (poly.bbox.ll.x < this.bbox.ll.x) this.bbox.ll.x = poly.bbox.ll.x;
|
|
1689
|
+
if (poly.bbox.ll.y < this.bbox.ll.y) this.bbox.ll.y = poly.bbox.ll.y;
|
|
1690
|
+
if (poly.bbox.ur.x > this.bbox.ur.x) this.bbox.ur.x = poly.bbox.ur.x;
|
|
1691
|
+
if (poly.bbox.ur.y > this.bbox.ur.y) this.bbox.ur.y = poly.bbox.ur.y;
|
|
1692
|
+
this.polys.push(poly);
|
|
1693
|
+
}
|
|
1694
|
+
this.isSubject = isSubject;
|
|
1695
|
+
}
|
|
1696
|
+
getSweepEvents() {
|
|
1697
|
+
const sweepEvents = [];
|
|
1698
|
+
for (let i = 0, iMax = this.polys.length; i < iMax; i++) {
|
|
1699
|
+
const polySweepEvents = this.polys[i].getSweepEvents();
|
|
1700
|
+
for (let j = 0, jMax = polySweepEvents.length; j < jMax; j++) {
|
|
1701
|
+
sweepEvents.push(polySweepEvents[j]);
|
|
1702
|
+
}
|
|
1703
|
+
}
|
|
1704
|
+
return sweepEvents;
|
|
1705
|
+
}
|
|
1706
|
+
}
|
|
1707
|
+
class RingOut {
|
|
1708
|
+
/* Given the segments from the sweep line pass, compute & return a series
|
|
1709
|
+
* of closed rings from all the segments marked to be part of the result */
|
|
1710
|
+
static factory(allSegments) {
|
|
1711
|
+
const ringsOut = [];
|
|
1712
|
+
for (let i = 0, iMax = allSegments.length; i < iMax; i++) {
|
|
1713
|
+
const segment = allSegments[i];
|
|
1714
|
+
if (!segment.isInResult() || segment.ringOut) continue;
|
|
1715
|
+
let prevEvent = null;
|
|
1716
|
+
let event = segment.leftSE;
|
|
1717
|
+
let nextEvent = segment.rightSE;
|
|
1718
|
+
const events = [event];
|
|
1719
|
+
const startingPoint = event.point;
|
|
1720
|
+
const intersectionLEs = [];
|
|
1721
|
+
while (true) {
|
|
1722
|
+
prevEvent = event;
|
|
1723
|
+
event = nextEvent;
|
|
1724
|
+
events.push(event);
|
|
1725
|
+
if (event.point === startingPoint) break;
|
|
1726
|
+
while (true) {
|
|
1727
|
+
const availableLEs = event.getAvailableLinkedEvents();
|
|
1728
|
+
if (availableLEs.length === 0) {
|
|
1729
|
+
const firstPt = events[0].point;
|
|
1730
|
+
const lastPt = events[events.length - 1].point;
|
|
1731
|
+
throw new Error(`Unable to complete output ring starting at [${firstPt.x}, ${firstPt.y}]. Last matching segment found ends at [${lastPt.x}, ${lastPt.y}].`);
|
|
1732
|
+
}
|
|
1733
|
+
if (availableLEs.length === 1) {
|
|
1734
|
+
nextEvent = availableLEs[0].otherSE;
|
|
1735
|
+
break;
|
|
1736
|
+
}
|
|
1737
|
+
let indexLE = null;
|
|
1738
|
+
for (let j = 0, jMax = intersectionLEs.length; j < jMax; j++) {
|
|
1739
|
+
if (intersectionLEs[j].point === event.point) {
|
|
1740
|
+
indexLE = j;
|
|
1741
|
+
break;
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
if (indexLE !== null) {
|
|
1745
|
+
const intersectionLE = intersectionLEs.splice(indexLE)[0];
|
|
1746
|
+
const ringEvents = events.splice(intersectionLE.index);
|
|
1747
|
+
ringEvents.unshift(ringEvents[0].otherSE);
|
|
1748
|
+
ringsOut.push(new RingOut(ringEvents.reverse()));
|
|
1749
|
+
continue;
|
|
1750
|
+
}
|
|
1751
|
+
intersectionLEs.push({
|
|
1752
|
+
index: events.length,
|
|
1753
|
+
point: event.point
|
|
1754
|
+
});
|
|
1755
|
+
const comparator = event.getLeftmostComparator(prevEvent);
|
|
1756
|
+
nextEvent = availableLEs.sort(comparator)[0].otherSE;
|
|
1757
|
+
break;
|
|
1758
|
+
}
|
|
1759
|
+
}
|
|
1760
|
+
ringsOut.push(new RingOut(events));
|
|
1761
|
+
}
|
|
1762
|
+
return ringsOut;
|
|
1763
|
+
}
|
|
1764
|
+
constructor(events) {
|
|
1765
|
+
this.events = events;
|
|
1766
|
+
for (let i = 0, iMax = events.length; i < iMax; i++) {
|
|
1767
|
+
events[i].segment.ringOut = this;
|
|
1768
|
+
}
|
|
1769
|
+
this.poly = null;
|
|
1770
|
+
}
|
|
1771
|
+
getGeom() {
|
|
1772
|
+
let prevPt = this.events[0].point;
|
|
1773
|
+
const points = [prevPt];
|
|
1774
|
+
for (let i = 1, iMax = this.events.length - 1; i < iMax; i++) {
|
|
1775
|
+
const pt2 = this.events[i].point;
|
|
1776
|
+
const nextPt2 = this.events[i + 1].point;
|
|
1777
|
+
if (compareVectorAngles(pt2, prevPt, nextPt2) === 0) continue;
|
|
1778
|
+
points.push(pt2);
|
|
1779
|
+
prevPt = pt2;
|
|
1780
|
+
}
|
|
1781
|
+
if (points.length === 1) return null;
|
|
1782
|
+
const pt = points[0];
|
|
1783
|
+
const nextPt = points[1];
|
|
1784
|
+
if (compareVectorAngles(pt, prevPt, nextPt) === 0) points.shift();
|
|
1785
|
+
points.push(points[0]);
|
|
1786
|
+
const step = this.isExteriorRing() ? 1 : -1;
|
|
1787
|
+
const iStart = this.isExteriorRing() ? 0 : points.length - 1;
|
|
1788
|
+
const iEnd = this.isExteriorRing() ? points.length : -1;
|
|
1789
|
+
const orderedPoints = [];
|
|
1790
|
+
for (let i = iStart; i != iEnd; i += step) orderedPoints.push([points[i].x, points[i].y]);
|
|
1791
|
+
return orderedPoints;
|
|
1792
|
+
}
|
|
1793
|
+
isExteriorRing() {
|
|
1794
|
+
if (this._isExteriorRing === void 0) {
|
|
1795
|
+
const enclosing = this.enclosingRing();
|
|
1796
|
+
this._isExteriorRing = enclosing ? !enclosing.isExteriorRing() : true;
|
|
1797
|
+
}
|
|
1798
|
+
return this._isExteriorRing;
|
|
1799
|
+
}
|
|
1800
|
+
enclosingRing() {
|
|
1801
|
+
if (this._enclosingRing === void 0) {
|
|
1802
|
+
this._enclosingRing = this._calcEnclosingRing();
|
|
1803
|
+
}
|
|
1804
|
+
return this._enclosingRing;
|
|
1805
|
+
}
|
|
1806
|
+
/* Returns the ring that encloses this one, if any */
|
|
1807
|
+
_calcEnclosingRing() {
|
|
1808
|
+
let leftMostEvt = this.events[0];
|
|
1809
|
+
for (let i = 1, iMax = this.events.length; i < iMax; i++) {
|
|
1810
|
+
const evt = this.events[i];
|
|
1811
|
+
if (SweepEvent.compare(leftMostEvt, evt) > 0) leftMostEvt = evt;
|
|
1812
|
+
}
|
|
1813
|
+
let prevSeg = leftMostEvt.segment.prevInResult();
|
|
1814
|
+
let prevPrevSeg = prevSeg ? prevSeg.prevInResult() : null;
|
|
1815
|
+
while (true) {
|
|
1816
|
+
if (!prevSeg) return null;
|
|
1817
|
+
if (!prevPrevSeg) return prevSeg.ringOut;
|
|
1818
|
+
if (prevPrevSeg.ringOut !== prevSeg.ringOut) {
|
|
1819
|
+
if (prevPrevSeg.ringOut.enclosingRing() !== prevSeg.ringOut) {
|
|
1820
|
+
return prevSeg.ringOut;
|
|
1821
|
+
} else return prevSeg.ringOut.enclosingRing();
|
|
1822
|
+
}
|
|
1823
|
+
prevSeg = prevPrevSeg.prevInResult();
|
|
1824
|
+
prevPrevSeg = prevSeg ? prevSeg.prevInResult() : null;
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
}
|
|
1828
|
+
class PolyOut {
|
|
1829
|
+
constructor(exteriorRing) {
|
|
1830
|
+
this.exteriorRing = exteriorRing;
|
|
1831
|
+
exteriorRing.poly = this;
|
|
1832
|
+
this.interiorRings = [];
|
|
1833
|
+
}
|
|
1834
|
+
addInterior(ring) {
|
|
1835
|
+
this.interiorRings.push(ring);
|
|
1836
|
+
ring.poly = this;
|
|
1837
|
+
}
|
|
1838
|
+
getGeom() {
|
|
1839
|
+
const geom = [this.exteriorRing.getGeom()];
|
|
1840
|
+
if (geom[0] === null) return null;
|
|
1841
|
+
for (let i = 0, iMax = this.interiorRings.length; i < iMax; i++) {
|
|
1842
|
+
const ringGeom = this.interiorRings[i].getGeom();
|
|
1843
|
+
if (ringGeom === null) continue;
|
|
1844
|
+
geom.push(ringGeom);
|
|
1845
|
+
}
|
|
1846
|
+
return geom;
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
class MultiPolyOut {
|
|
1850
|
+
constructor(rings) {
|
|
1851
|
+
this.rings = rings;
|
|
1852
|
+
this.polys = this._composePolys(rings);
|
|
1853
|
+
}
|
|
1854
|
+
getGeom() {
|
|
1855
|
+
const geom = [];
|
|
1856
|
+
for (let i = 0, iMax = this.polys.length; i < iMax; i++) {
|
|
1857
|
+
const polyGeom = this.polys[i].getGeom();
|
|
1858
|
+
if (polyGeom === null) continue;
|
|
1859
|
+
geom.push(polyGeom);
|
|
1860
|
+
}
|
|
1861
|
+
return geom;
|
|
1862
|
+
}
|
|
1863
|
+
_composePolys(rings) {
|
|
1864
|
+
const polys = [];
|
|
1865
|
+
for (let i = 0, iMax = rings.length; i < iMax; i++) {
|
|
1866
|
+
const ring = rings[i];
|
|
1867
|
+
if (ring.poly) continue;
|
|
1868
|
+
if (ring.isExteriorRing()) polys.push(new PolyOut(ring));
|
|
1869
|
+
else {
|
|
1870
|
+
const enclosingRing = ring.enclosingRing();
|
|
1871
|
+
if (!enclosingRing.poly) polys.push(new PolyOut(enclosingRing));
|
|
1872
|
+
enclosingRing.poly.addInterior(ring);
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
return polys;
|
|
1876
|
+
}
|
|
1877
|
+
}
|
|
1878
|
+
class SweepLine {
|
|
1879
|
+
constructor(queue) {
|
|
1880
|
+
let comparator = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : Segment.compare;
|
|
1881
|
+
this.queue = queue;
|
|
1882
|
+
this.tree = new Tree(comparator);
|
|
1883
|
+
this.segments = [];
|
|
1884
|
+
}
|
|
1885
|
+
process(event) {
|
|
1886
|
+
const segment = event.segment;
|
|
1887
|
+
const newEvents = [];
|
|
1888
|
+
if (event.consumedBy) {
|
|
1889
|
+
if (event.isLeft) this.queue.remove(event.otherSE);
|
|
1890
|
+
else this.tree.remove(segment);
|
|
1891
|
+
return newEvents;
|
|
1892
|
+
}
|
|
1893
|
+
const node = event.isLeft ? this.tree.add(segment) : this.tree.find(segment);
|
|
1894
|
+
if (!node) throw new Error(`Unable to find segment #${segment.id} [${segment.leftSE.point.x}, ${segment.leftSE.point.y}] -> [${segment.rightSE.point.x}, ${segment.rightSE.point.y}] in SweepLine tree.`);
|
|
1895
|
+
let prevNode = node;
|
|
1896
|
+
let nextNode = node;
|
|
1897
|
+
let prevSeg = void 0;
|
|
1898
|
+
let nextSeg = void 0;
|
|
1899
|
+
while (prevSeg === void 0) {
|
|
1900
|
+
prevNode = this.tree.prev(prevNode);
|
|
1901
|
+
if (prevNode === null) prevSeg = null;
|
|
1902
|
+
else if (prevNode.key.consumedBy === void 0) prevSeg = prevNode.key;
|
|
1903
|
+
}
|
|
1904
|
+
while (nextSeg === void 0) {
|
|
1905
|
+
nextNode = this.tree.next(nextNode);
|
|
1906
|
+
if (nextNode === null) nextSeg = null;
|
|
1907
|
+
else if (nextNode.key.consumedBy === void 0) nextSeg = nextNode.key;
|
|
1908
|
+
}
|
|
1909
|
+
if (event.isLeft) {
|
|
1910
|
+
let prevMySplitter = null;
|
|
1911
|
+
if (prevSeg) {
|
|
1912
|
+
const prevInter = prevSeg.getIntersection(segment);
|
|
1913
|
+
if (prevInter !== null) {
|
|
1914
|
+
if (!segment.isAnEndpoint(prevInter)) prevMySplitter = prevInter;
|
|
1915
|
+
if (!prevSeg.isAnEndpoint(prevInter)) {
|
|
1916
|
+
const newEventsFromSplit = this._splitSafely(prevSeg, prevInter);
|
|
1917
|
+
for (let i = 0, iMax = newEventsFromSplit.length; i < iMax; i++) {
|
|
1918
|
+
newEvents.push(newEventsFromSplit[i]);
|
|
1919
|
+
}
|
|
1920
|
+
}
|
|
1921
|
+
}
|
|
1922
|
+
}
|
|
1923
|
+
let nextMySplitter = null;
|
|
1924
|
+
if (nextSeg) {
|
|
1925
|
+
const nextInter = nextSeg.getIntersection(segment);
|
|
1926
|
+
if (nextInter !== null) {
|
|
1927
|
+
if (!segment.isAnEndpoint(nextInter)) nextMySplitter = nextInter;
|
|
1928
|
+
if (!nextSeg.isAnEndpoint(nextInter)) {
|
|
1929
|
+
const newEventsFromSplit = this._splitSafely(nextSeg, nextInter);
|
|
1930
|
+
for (let i = 0, iMax = newEventsFromSplit.length; i < iMax; i++) {
|
|
1931
|
+
newEvents.push(newEventsFromSplit[i]);
|
|
1932
|
+
}
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
366
1935
|
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
} else if ("object" == typeof e) return "string" == typeof e.data && (e.data = JSON.parse(e.data)), Array.isArray(e.data) && (e.fields || (e.fields = e.meta && e.meta.fields || r2), e.fields || (e.fields = Array.isArray(e.data[0]) ? e.fields : "object" == typeof e.data[0] ? Object.keys(e.data[0]) : []), Array.isArray(e.data[0]) || "object" == typeof e.data[0] || (e.data = [e.data])), u2(e.fields || [], e.data || [], i2);
|
|
375
|
-
throw new Error("Unable to serialize unrecognized input");
|
|
376
|
-
function u2(e2, t2, i3) {
|
|
377
|
-
var r3 = "", n3 = ("string" == typeof e2 && (e2 = JSON.parse(e2)), "string" == typeof t2 && (t2 = JSON.parse(t2)), Array.isArray(e2) && 0 < e2.length), s3 = !Array.isArray(t2[0]);
|
|
378
|
-
if (n3 && _2) {
|
|
379
|
-
for (var a3 = 0; a3 < e2.length; a3++) 0 < a3 && (r3 += m2), r3 += k(e2[a3], a3);
|
|
380
|
-
0 < t2.length && (r3 += y2);
|
|
381
|
-
}
|
|
382
|
-
for (var o3 = 0; o3 < t2.length; o3++) {
|
|
383
|
-
var h3 = (n3 ? e2 : t2[o3]).length, u3 = false, d2 = n3 ? 0 === Object.keys(t2[o3]).length : 0 === t2[o3].length;
|
|
384
|
-
if (i3 && !n3 && (u3 = "greedy" === i3 ? "" === t2[o3].join("").trim() : 1 === t2[o3].length && 0 === t2[o3][0].length), "greedy" === i3 && n3) {
|
|
385
|
-
for (var f2 = [], l2 = 0; l2 < h3; l2++) {
|
|
386
|
-
var c2 = s3 ? e2[l2] : l2;
|
|
387
|
-
f2.push(t2[o3][c2]);
|
|
1936
|
+
if (prevMySplitter !== null || nextMySplitter !== null) {
|
|
1937
|
+
let mySplitter = null;
|
|
1938
|
+
if (prevMySplitter === null) mySplitter = nextMySplitter;
|
|
1939
|
+
else if (nextMySplitter === null) mySplitter = prevMySplitter;
|
|
1940
|
+
else {
|
|
1941
|
+
const cmpSplitters = SweepEvent.comparePoints(prevMySplitter, nextMySplitter);
|
|
1942
|
+
mySplitter = cmpSplitters <= 0 ? prevMySplitter : nextMySplitter;
|
|
388
1943
|
}
|
|
389
|
-
|
|
1944
|
+
this.queue.remove(segment.rightSE);
|
|
1945
|
+
newEvents.push(segment.rightSE);
|
|
1946
|
+
const newEventsFromSplit = segment.split(mySplitter);
|
|
1947
|
+
for (let i = 0, iMax = newEventsFromSplit.length; i < iMax; i++) {
|
|
1948
|
+
newEvents.push(newEventsFromSplit[i]);
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
if (newEvents.length > 0) {
|
|
1952
|
+
this.tree.remove(segment);
|
|
1953
|
+
newEvents.push(event);
|
|
1954
|
+
} else {
|
|
1955
|
+
this.segments.push(segment);
|
|
1956
|
+
segment.prev = prevSeg;
|
|
390
1957
|
}
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
1958
|
+
} else {
|
|
1959
|
+
if (prevSeg && nextSeg) {
|
|
1960
|
+
const inter = prevSeg.getIntersection(nextSeg);
|
|
1961
|
+
if (inter !== null) {
|
|
1962
|
+
if (!prevSeg.isAnEndpoint(inter)) {
|
|
1963
|
+
const newEventsFromSplit = this._splitSafely(prevSeg, inter);
|
|
1964
|
+
for (let i = 0, iMax = newEventsFromSplit.length; i < iMax; i++) {
|
|
1965
|
+
newEvents.push(newEventsFromSplit[i]);
|
|
1966
|
+
}
|
|
1967
|
+
}
|
|
1968
|
+
if (!nextSeg.isAnEndpoint(inter)) {
|
|
1969
|
+
const newEventsFromSplit = this._splitSafely(nextSeg, inter);
|
|
1970
|
+
for (let i = 0, iMax = newEventsFromSplit.length; i < iMax; i++) {
|
|
1971
|
+
newEvents.push(newEventsFromSplit[i]);
|
|
1972
|
+
}
|
|
1973
|
+
}
|
|
396
1974
|
}
|
|
397
|
-
o3 < t2.length - 1 && (!i3 || 0 < h3 && !d2) && (r3 += y2);
|
|
398
1975
|
}
|
|
1976
|
+
this.tree.remove(segment);
|
|
399
1977
|
}
|
|
400
|
-
return
|
|
1978
|
+
return newEvents;
|
|
401
1979
|
}
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
1980
|
+
/* Safely split a segment that is currently in the datastructures
|
|
1981
|
+
* IE - a segment other than the one that is currently being processed. */
|
|
1982
|
+
_splitSafely(seg, pt) {
|
|
1983
|
+
this.tree.remove(seg);
|
|
1984
|
+
const rightSE = seg.rightSE;
|
|
1985
|
+
this.queue.remove(rightSE);
|
|
1986
|
+
const newEvents = seg.split(pt);
|
|
1987
|
+
newEvents.push(rightSE);
|
|
1988
|
+
if (seg.consumedBy === void 0) this.tree.add(seg);
|
|
1989
|
+
return newEvents;
|
|
1990
|
+
}
|
|
1991
|
+
}
|
|
1992
|
+
const POLYGON_CLIPPING_MAX_QUEUE_SIZE = typeof process !== "undefined" && process.env.POLYGON_CLIPPING_MAX_QUEUE_SIZE || 1e6;
|
|
1993
|
+
const POLYGON_CLIPPING_MAX_SWEEPLINE_SEGMENTS = typeof process !== "undefined" && process.env.POLYGON_CLIPPING_MAX_SWEEPLINE_SEGMENTS || 1e6;
|
|
1994
|
+
class Operation {
|
|
1995
|
+
run(type, geom, moreGeoms) {
|
|
1996
|
+
operation.type = type;
|
|
1997
|
+
rounder.reset();
|
|
1998
|
+
const multipolys = [new MultiPolyIn(geom, true)];
|
|
1999
|
+
for (let i = 0, iMax = moreGeoms.length; i < iMax; i++) {
|
|
2000
|
+
multipolys.push(new MultiPolyIn(moreGeoms[i], false));
|
|
2001
|
+
}
|
|
2002
|
+
operation.numMultiPolys = multipolys.length;
|
|
2003
|
+
if (operation.type === "difference") {
|
|
2004
|
+
const subject = multipolys[0];
|
|
2005
|
+
let i = 1;
|
|
2006
|
+
while (i < multipolys.length) {
|
|
2007
|
+
if (getBboxOverlap(multipolys[i].bbox, subject.bbox) !== null) i++;
|
|
2008
|
+
else multipolys.splice(i, 1);
|
|
2009
|
+
}
|
|
2010
|
+
}
|
|
2011
|
+
if (operation.type === "intersection") {
|
|
2012
|
+
for (let i = 0, iMax = multipolys.length; i < iMax; i++) {
|
|
2013
|
+
const mpA = multipolys[i];
|
|
2014
|
+
for (let j = i + 1, jMax = multipolys.length; j < jMax; j++) {
|
|
2015
|
+
if (getBboxOverlap(mpA.bbox, multipolys[j].bbox) === null) return [];
|
|
2016
|
+
}
|
|
426
2017
|
}
|
|
427
|
-
var a2 = n2.instanceConfig.complete;
|
|
428
|
-
n2.instanceConfig.complete = function(e3) {
|
|
429
|
-
U(a2) && a2(e3, n2.file, n2.inputElem), u2();
|
|
430
|
-
}, v.parse(n2.file, n2.instanceConfig);
|
|
431
2018
|
}
|
|
2019
|
+
const queue = new Tree(SweepEvent.compare);
|
|
2020
|
+
for (let i = 0, iMax = multipolys.length; i < iMax; i++) {
|
|
2021
|
+
const sweepEvents = multipolys[i].getSweepEvents();
|
|
2022
|
+
for (let j = 0, jMax = sweepEvents.length; j < jMax; j++) {
|
|
2023
|
+
queue.insert(sweepEvents[j]);
|
|
2024
|
+
if (queue.size > POLYGON_CLIPPING_MAX_QUEUE_SIZE) {
|
|
2025
|
+
throw new Error("Infinite loop when putting segment endpoints in a priority queue (queue size too big).");
|
|
2026
|
+
}
|
|
2027
|
+
}
|
|
2028
|
+
}
|
|
2029
|
+
const sweepLine = new SweepLine(queue);
|
|
2030
|
+
let prevQueueSize = queue.size;
|
|
2031
|
+
let node = queue.pop();
|
|
2032
|
+
while (node) {
|
|
2033
|
+
const evt = node.key;
|
|
2034
|
+
if (queue.size === prevQueueSize) {
|
|
2035
|
+
const seg = evt.segment;
|
|
2036
|
+
throw new Error(`Unable to pop() ${evt.isLeft ? "left" : "right"} SweepEvent [${evt.point.x}, ${evt.point.y}] from segment #${seg.id} [${seg.leftSE.point.x}, ${seg.leftSE.point.y}] -> [${seg.rightSE.point.x}, ${seg.rightSE.point.y}] from queue.`);
|
|
2037
|
+
}
|
|
2038
|
+
if (queue.size > POLYGON_CLIPPING_MAX_QUEUE_SIZE) {
|
|
2039
|
+
throw new Error("Infinite loop when passing sweep line over endpoints (queue size too big).");
|
|
2040
|
+
}
|
|
2041
|
+
if (sweepLine.segments.length > POLYGON_CLIPPING_MAX_SWEEPLINE_SEGMENTS) {
|
|
2042
|
+
throw new Error("Infinite loop when passing sweep line over endpoints (too many sweep line segments).");
|
|
2043
|
+
}
|
|
2044
|
+
const newEvents = sweepLine.process(evt);
|
|
2045
|
+
for (let i = 0, iMax = newEvents.length; i < iMax; i++) {
|
|
2046
|
+
const evt2 = newEvents[i];
|
|
2047
|
+
if (evt2.consumedBy === void 0) queue.insert(evt2);
|
|
2048
|
+
}
|
|
2049
|
+
prevQueueSize = queue.size;
|
|
2050
|
+
node = queue.pop();
|
|
2051
|
+
}
|
|
2052
|
+
rounder.reset();
|
|
2053
|
+
const ringsOut = RingOut.factory(sweepLine.segments);
|
|
2054
|
+
const result = new MultiPolyOut(ringsOut);
|
|
2055
|
+
return result.getGeom();
|
|
432
2056
|
}
|
|
433
|
-
|
|
434
|
-
|
|
2057
|
+
}
|
|
2058
|
+
const operation = new Operation();
|
|
2059
|
+
const union = function(geom) {
|
|
2060
|
+
for (var _len = arguments.length, moreGeoms = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
2061
|
+
moreGeoms[_key - 1] = arguments[_key];
|
|
2062
|
+
}
|
|
2063
|
+
return operation.run("union", geom, moreGeoms);
|
|
2064
|
+
};
|
|
2065
|
+
const intersection = function(geom) {
|
|
2066
|
+
for (var _len2 = arguments.length, moreGeoms = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
2067
|
+
moreGeoms[_key2 - 1] = arguments[_key2];
|
|
435
2068
|
}
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
2069
|
+
return operation.run("intersection", geom, moreGeoms);
|
|
2070
|
+
};
|
|
2071
|
+
const xor = function(geom) {
|
|
2072
|
+
for (var _len3 = arguments.length, moreGeoms = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
|
|
2073
|
+
moreGeoms[_key3 - 1] = arguments[_key3];
|
|
2074
|
+
}
|
|
2075
|
+
return operation.run("xor", geom, moreGeoms);
|
|
2076
|
+
};
|
|
2077
|
+
const difference = function(subjectGeom) {
|
|
2078
|
+
for (var _len4 = arguments.length, clippingGeoms = new Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) {
|
|
2079
|
+
clippingGeoms[_key4 - 1] = arguments[_key4];
|
|
2080
|
+
}
|
|
2081
|
+
return operation.run("difference", subjectGeom, clippingGeoms);
|
|
2082
|
+
};
|
|
2083
|
+
var index = {
|
|
2084
|
+
union,
|
|
2085
|
+
intersection,
|
|
2086
|
+
xor,
|
|
2087
|
+
difference
|
|
2088
|
+
};
|
|
2089
|
+
return index;
|
|
441
2090
|
});
|
|
442
2091
|
}
|
|
443
2092
|
});
|
|
444
2093
|
|
|
445
|
-
// ../
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
2094
|
+
// ../community/packages/geometry-editor/src/lib/issueTarget.ts
|
|
2095
|
+
var issuePathSegments = (path) => (path ?? "").split("/").filter(Boolean).map((segment) => segment.replace(/~1/g, "/").replace(/~0/g, "~"));
|
|
2096
|
+
|
|
2097
|
+
// ../community/packages/geometry-editor/src/lib/hemieFabric.ts
|
|
2098
|
+
var ENGLAND_AREAS = new Set("AL B BA BB BD BH BL BN BR BS CB CM CO CR CT CV CW DA DE DH DL DN DT DY E EC EN EX FY GL GU HA HD HG HP HU HX IG IP KT L LA LE LN LS LU M ME MK N NE NG NN NR NW OL OX PE PL PO PR RG RH RM S SE SG SK SL SM SN SO SP SR SS ST SW TA TF TN TQ TR TS TW UB W WA WC WD WF WN WR WS WV YO".split(" "));
|
|
2099
|
+
var HEMIE_NATION = "HEMIE_nation";
|
|
2100
|
+
var words = (value) => value.charAt(0).toUpperCase() + value.slice(1).replace(/_/g, " ");
|
|
2101
|
+
var hemieConstructionTypeLabel = words;
|
|
2102
|
+
|
|
2103
|
+
// ../community/packages/geometry-editor/src/lib/zoneDerivation.ts
|
|
2104
|
+
var import_polygon_clipping2 = __toESM(require_polygon_clipping_umd(), 1);
|
|
2105
|
+
|
|
2106
|
+
// ../community/packages/geometry-editor/src/lib/polygonSync.ts
|
|
2107
|
+
var import_polygon_clipping = __toESM(require_polygon_clipping_umd(), 1);
|
|
2108
|
+
|
|
2109
|
+
// ../community/packages/geometry-editor-host/src/schemaPort.ts
|
|
2110
|
+
function unavailable(operation) {
|
|
2111
|
+
throw new Error(`Geometry schema ${operation} is unavailable`);
|
|
457
2112
|
}
|
|
2113
|
+
var unavailableGeometrySchemaPort = Object.freeze({
|
|
2114
|
+
availability: "unavailable",
|
|
2115
|
+
preload: async () => unavailable("preload"),
|
|
2116
|
+
getRootSchema: () => unavailable("root lookup"),
|
|
2117
|
+
getElementSubschema: () => unavailable("element lookup"),
|
|
2118
|
+
getBaseFieldsForElementType: () => unavailable("base-field lookup"),
|
|
2119
|
+
getApplianceKeys: () => unavailable("appliance-key lookup"),
|
|
2120
|
+
getStrictestIntegerKeysForElementType: () => unavailable("numeric-typing lookup"),
|
|
2121
|
+
getSchemaSubtypeForElementData: () => unavailable("subtype lookup"),
|
|
2122
|
+
getConditionalRequiredFields: () => unavailable("conditional-required lookup"),
|
|
2123
|
+
validateProperty: () => unavailable("property validation"),
|
|
2124
|
+
findParameter: () => unavailable("parameter lookup")
|
|
2125
|
+
});
|
|
2126
|
+
|
|
2127
|
+
// ../community/packages/geometry-editor/src/lib/roofCutouts.ts
|
|
2128
|
+
var import_polygon_clipping4 = __toESM(require_polygon_clipping_umd(), 1);
|
|
2129
|
+
|
|
2130
|
+
// ../community/packages/geometry-editor/src/lib/geometry3dMapper.ts
|
|
2131
|
+
var import_polygon_clipping3 = __toESM(require_polygon_clipping_umd(), 1);
|
|
2132
|
+
|
|
2133
|
+
// ../community/packages/geometry-editor/src/lib/assemblyCalculator.ts
|
|
2134
|
+
var H_RI = 5.13;
|
|
2135
|
+
var H_CI_UPWARDS = 5;
|
|
2136
|
+
var H_CI_HORIZONTAL = 2.5;
|
|
2137
|
+
var H_CI_DOWNWARDS = 0.7;
|
|
2138
|
+
var H_CE = 20;
|
|
2139
|
+
var H_RE = 4.14;
|
|
2140
|
+
var R_SI_HORIZONTAL = 1 / (H_RI + H_CI_HORIZONTAL);
|
|
2141
|
+
var R_SI_UPWARDS = 1 / (H_RI + H_CI_UPWARDS);
|
|
2142
|
+
var R_SI_DOWNWARDS = 1 / (H_RI + H_CI_DOWNWARDS);
|
|
2143
|
+
var R_SE = 1 / (H_CE + H_RE);
|
|
2144
|
+
|
|
2145
|
+
// ../community/packages/geometry-editor/src/lib/assemblyMassHeuristic.ts
|
|
2146
|
+
var FHS_MASS_DISTRIBUTION_CLASS_BY_CODE = {
|
|
2147
|
+
I: "I: Mass concentrated at internal side",
|
|
2148
|
+
E: "E: Mass concentrated at external side",
|
|
2149
|
+
IE: "IE: Mass divided over internal and external side",
|
|
2150
|
+
D: "D: Mass equally distributed",
|
|
2151
|
+
M: "M: Mass concentrated inside"
|
|
2152
|
+
};
|
|
2153
|
+
var FHS_MASS_DISTRIBUTION_CLASS_VALUES = new Set(
|
|
2154
|
+
Object.values(FHS_MASS_DISTRIBUTION_CLASS_BY_CODE)
|
|
2155
|
+
);
|
|
2156
|
+
|
|
2157
|
+
// ../community/packages/geometry-editor/src/lib/assemblyAppliedUi.ts
|
|
2158
|
+
var MASS_CLASS_MEANINGS = {
|
|
2159
|
+
I: "Mass mainly on the room side (e.g. external insulation).",
|
|
2160
|
+
E: "Mass mainly on the external side (e.g. internal insulation).",
|
|
2161
|
+
IE: "Mass on both sides of the insulation (e.g. filled cavity).",
|
|
2162
|
+
D: "Evenly distributed or negligible mass (e.g. uninsulated solid, lightweight).",
|
|
2163
|
+
M: "Mass near the middle (insulation both sides of the structural leaf)."
|
|
2164
|
+
};
|
|
2165
|
+
var FHS_MASS_CLASS_COMMENTARY = {
|
|
2166
|
+
"I: Mass concentrated at internal side": MASS_CLASS_MEANINGS.I,
|
|
2167
|
+
"E: Mass concentrated at external side": MASS_CLASS_MEANINGS.E,
|
|
2168
|
+
"IE: Mass divided over internal and external side": MASS_CLASS_MEANINGS.IE,
|
|
2169
|
+
"D: Mass equally distributed": MASS_CLASS_MEANINGS.D,
|
|
2170
|
+
"M: Mass concentrated inside": MASS_CLASS_MEANINGS.M
|
|
2171
|
+
};
|
|
2172
|
+
|
|
2173
|
+
// ../community/packages/geometry-editor/src/lib/overrideProvenance.ts
|
|
2174
|
+
var SLOPED_WIDTH_OVERRIDE_DESCRIPTOR = {
|
|
2175
|
+
flag: "_widthUserOverride",
|
|
2176
|
+
key: "_width_user_override",
|
|
2177
|
+
positiveSense: true,
|
|
2178
|
+
since: 1
|
|
2179
|
+
};
|
|
2180
|
+
var SLOPED_HEIGHT_OVERRIDE_DESCRIPTOR = {
|
|
2181
|
+
flag: "_heightUserOverride",
|
|
2182
|
+
key: "_height_user_override",
|
|
2183
|
+
positiveSense: true,
|
|
2184
|
+
since: 1
|
|
2185
|
+
};
|
|
2186
|
+
var PV_BASE_HEIGHT_OVERRIDE_DESCRIPTOR = {
|
|
2187
|
+
flag: "_baseHeightUserOverride",
|
|
2188
|
+
key: "_pv_base_height_user_override",
|
|
2189
|
+
positiveSense: true,
|
|
2190
|
+
since: 1
|
|
2191
|
+
};
|
|
2192
|
+
var PV_PITCH_OVERRIDE_DESCRIPTOR = {
|
|
2193
|
+
flag: "_pitchUserOverride",
|
|
2194
|
+
key: "_pv_pitch_user_override",
|
|
2195
|
+
positiveSense: true,
|
|
2196
|
+
since: 1
|
|
2197
|
+
};
|
|
2198
|
+
var PV_ORIENTATION_OVERRIDE_DESCRIPTOR = {
|
|
2199
|
+
flag: "_orientationUserOverride",
|
|
2200
|
+
key: "_pv_orientation_user_override",
|
|
2201
|
+
positiveSense: true,
|
|
2202
|
+
since: 1
|
|
2203
|
+
};
|
|
2204
|
+
var ELEMENT_NAME_AUTO_SYNC_DESCRIPTOR = {
|
|
2205
|
+
flag: "_nameAutoSync",
|
|
2206
|
+
key: "_name_auto_sync",
|
|
2207
|
+
positiveSense: false,
|
|
2208
|
+
since: 1,
|
|
2209
|
+
absentIsAuthoritative: false
|
|
2210
|
+
};
|
|
2211
|
+
var GROUND_TOTAL_AREA_OVERRIDE_DESCRIPTOR = {
|
|
2212
|
+
flag: "_totalAreaUserOverride",
|
|
2213
|
+
key: "_ground_total_area_manual",
|
|
2214
|
+
positiveSense: true,
|
|
2215
|
+
since: 2
|
|
2216
|
+
};
|
|
2217
|
+
var WINDOW_SECURITY_RISK_OVERRIDE_DESCRIPTOR = {
|
|
2218
|
+
flag: "_windowSecurityRiskUserOverride",
|
|
2219
|
+
key: "_window_security_risk_user_override",
|
|
2220
|
+
positiveSense: true,
|
|
2221
|
+
since: 3
|
|
2222
|
+
};
|
|
2223
|
+
var ZONE_FLOOR_AREA_OVERRIDE_DESCRIPTOR = {
|
|
2224
|
+
flag: "_floorAreaUserOverride",
|
|
2225
|
+
key: "_floor_area_user_override",
|
|
2226
|
+
positiveSense: true,
|
|
2227
|
+
since: 1
|
|
2228
|
+
};
|
|
2229
|
+
var ZONE_HEIGHT_OVERRIDE_DESCRIPTOR = {
|
|
2230
|
+
flag: "_heightUserOverride",
|
|
2231
|
+
key: "_height_user_override",
|
|
2232
|
+
positiveSense: true,
|
|
2233
|
+
since: 1
|
|
2234
|
+
};
|
|
2235
|
+
var FLOOR_HEIGHT_OVERRIDE_DESCRIPTOR = {
|
|
2236
|
+
flag: "heightUserOverride",
|
|
2237
|
+
key: "FloorHeightOverride",
|
|
2238
|
+
positiveSense: true,
|
|
2239
|
+
since: 0
|
|
2240
|
+
};
|
|
2241
|
+
var FLOOR_BASE_HEIGHT_OVERRIDE_DESCRIPTOR = {
|
|
2242
|
+
flag: "baseHeightUserOverride",
|
|
2243
|
+
key: "FloorBaseHeightOverride",
|
|
2244
|
+
positiveSense: true,
|
|
2245
|
+
since: 0
|
|
2246
|
+
};
|
|
2247
|
+
var SPACE_LABEL_NAME_AUTO_SYNC_DESCRIPTOR = {
|
|
2248
|
+
flag: "_nameAutoSync",
|
|
2249
|
+
key: "_name_auto_sync",
|
|
2250
|
+
positiveSense: false,
|
|
2251
|
+
since: 1,
|
|
2252
|
+
absentIsAuthoritative: false
|
|
2253
|
+
};
|
|
2254
|
+
var OVERRIDE_PROVENANCE_REGISTRY = {
|
|
2255
|
+
element: [
|
|
2256
|
+
SLOPED_WIDTH_OVERRIDE_DESCRIPTOR,
|
|
2257
|
+
SLOPED_HEIGHT_OVERRIDE_DESCRIPTOR,
|
|
2258
|
+
PV_BASE_HEIGHT_OVERRIDE_DESCRIPTOR,
|
|
2259
|
+
PV_PITCH_OVERRIDE_DESCRIPTOR,
|
|
2260
|
+
PV_ORIENTATION_OVERRIDE_DESCRIPTOR,
|
|
2261
|
+
ELEMENT_NAME_AUTO_SYNC_DESCRIPTOR,
|
|
2262
|
+
GROUND_TOTAL_AREA_OVERRIDE_DESCRIPTOR,
|
|
2263
|
+
WINDOW_SECURITY_RISK_OVERRIDE_DESCRIPTOR
|
|
2264
|
+
],
|
|
2265
|
+
zone: [
|
|
2266
|
+
ZONE_FLOOR_AREA_OVERRIDE_DESCRIPTOR,
|
|
2267
|
+
ZONE_HEIGHT_OVERRIDE_DESCRIPTOR
|
|
2268
|
+
],
|
|
2269
|
+
floor: [FLOOR_HEIGHT_OVERRIDE_DESCRIPTOR, FLOOR_BASE_HEIGHT_OVERRIDE_DESCRIPTOR],
|
|
2270
|
+
spaceLabel: [SPACE_LABEL_NAME_AUTO_SYNC_DESCRIPTOR]
|
|
2271
|
+
};
|
|
2272
|
+
var EXTRA_JSON_OVERRIDE_MARKER_KEYS = [
|
|
2273
|
+
...new Set([
|
|
2274
|
+
...OVERRIDE_PROVENANCE_REGISTRY.element,
|
|
2275
|
+
...OVERRIDE_PROVENANCE_REGISTRY.spaceLabel
|
|
2276
|
+
].map((descriptor) => descriptor.key))
|
|
2277
|
+
];
|
|
2278
|
+
|
|
2279
|
+
// ../community/packages/geometry-editor/src/lib/globalSettingsValidation.ts
|
|
2280
|
+
var R_SI_UPWARDS2 = 1 / (5.13 + 5);
|
|
2281
|
+
var R_SE2 = 1 / (20 + 4.14);
|
|
2282
|
+
var DWELLING_FIELD_KEYS = /* @__PURE__ */ new Set([
|
|
2283
|
+
"Postcode",
|
|
2284
|
+
"PartO_active_cooling_required",
|
|
2285
|
+
"GroundFloorArea",
|
|
2286
|
+
"HeatingControlType",
|
|
2287
|
+
"PartGcompliance",
|
|
2288
|
+
"ColdWaterSource",
|
|
2289
|
+
"BuildingLength",
|
|
2290
|
+
"BuildingWidth",
|
|
2291
|
+
"KitchenExtractorHoodExternal",
|
|
2292
|
+
"HasElectricitySmartMeter"
|
|
2293
|
+
]);
|
|
2294
|
+
function globalSettingsFieldSection(key) {
|
|
2295
|
+
if (key.startsWith("DefaultAssembly")) return "defaults";
|
|
2296
|
+
if (key === "TargetBundleId" || key === "IsExistingBuilding" || key.startsWith("HEMIE_")) return "target";
|
|
2297
|
+
if (key.startsWith("AirPermeability_test_")) return "airTightness";
|
|
2298
|
+
if (key.startsWith("Ventilation_") || key === "AirPermeability_ventilation_zone_height") return "ventilation";
|
|
2299
|
+
if (key === "JunctionPsiDefaultsPath" || key.startsWith("DetailedBridgePsiProfile")) return "thermalBridging";
|
|
2300
|
+
if (key.startsWith("General_") || key.startsWith("NumberOf") || key.startsWith("Appliances_") || DWELLING_FIELD_KEYS.has(key)) return "dwelling";
|
|
2301
|
+
return null;
|
|
2302
|
+
}
|
|
2303
|
+
var TARGET_INPUT_ROWS = [
|
|
2304
|
+
"HasElectricitySmartMeter",
|
|
2305
|
+
"IsExistingBuilding",
|
|
2306
|
+
"Ventilation_cross_vent_possible",
|
|
2307
|
+
"Appliances_has_dryer",
|
|
2308
|
+
"Appliances_has_dishwasher",
|
|
2309
|
+
"Appliances_has_separate_fridge_or_freezer"
|
|
2310
|
+
];
|
|
2311
|
+
var TARGET_INPUT_ROW_SET = new Set(TARGET_INPUT_ROWS);
|
|
2312
|
+
|
|
2313
|
+
// ../community/packages/geometry-editor/src/lib/buildErrorDisplay.ts
|
|
2314
|
+
var SECTION_LOCATIONS = {
|
|
2315
|
+
defaults: "Global Settings > Defaults",
|
|
2316
|
+
target: "Global Settings",
|
|
2317
|
+
dwelling: "Global Settings > Dwelling Details",
|
|
2318
|
+
airTightness: "Global Settings > Air Tightness",
|
|
2319
|
+
ventilation: "Global Settings > Ventilation Environment",
|
|
2320
|
+
thermalBridging: "Global Settings > Thermal Bridging"
|
|
2321
|
+
};
|
|
2322
|
+
var VENTILATION_SETTINGS = /* @__PURE__ */ new Set(["cross_vent_possible", "terrain_class", "altitude", "shield_class", "noise_nuisance", "ventilation_zone_base_height"]);
|
|
2323
|
+
var settingsRow = (segments) => {
|
|
2324
|
+
if (segments[0] === "Metadata") return segments[1];
|
|
2325
|
+
if (segments[0] !== "InfiltrationVentilation") return void 0;
|
|
2326
|
+
if (segments[1] === "Leaks") {
|
|
2327
|
+
return segments[2] === "ventilation_zone_height" ? "AirPermeability_ventilation_zone_height" : "AirPermeability_test_result";
|
|
2328
|
+
}
|
|
2329
|
+
return segments[1] && VENTILATION_SETTINGS.has(segments[1]) ? `Ventilation_${segments[1]}` : void 0;
|
|
2330
|
+
};
|
|
2331
|
+
var titleCaseToken = (token) => token.replace(/_/g, " ").replace(/([a-z])([A-Z])/g, "$1 $2").replace(/\s+/g, " ").trim().replace(/\b\w/g, (c) => c.toUpperCase());
|
|
2332
|
+
var pointerSegments = issuePathSegments;
|
|
2333
|
+
var buildErrorLocation = (path) => {
|
|
2334
|
+
const segments = pointerSegments(path);
|
|
2335
|
+
const zoneIdx = segments.indexOf("Zone");
|
|
2336
|
+
const elementIdx = segments.indexOf("BuildingElement");
|
|
2337
|
+
if (zoneIdx >= 0 && segments[zoneIdx + 1]) {
|
|
2338
|
+
const zone = segments[zoneIdx + 1];
|
|
2339
|
+
if (elementIdx >= 0 && segments[elementIdx + 1]) {
|
|
2340
|
+
return {
|
|
2341
|
+
location: `${zone} > ${segments[elementIdx + 1]}`,
|
|
2342
|
+
field: segments[elementIdx + 2]
|
|
2343
|
+
};
|
|
2344
|
+
}
|
|
2345
|
+
return {
|
|
2346
|
+
location: zone,
|
|
2347
|
+
field: segments[zoneIdx + 2]
|
|
2348
|
+
};
|
|
2349
|
+
}
|
|
2350
|
+
if (segments[0] === "Metadata" && segments[1]?.startsWith("HEMIE_")) {
|
|
2351
|
+
const constructionType = segments[1].match(/^HEMIE_construction_(.+)$/)?.[1];
|
|
2352
|
+
return {
|
|
2353
|
+
location: segments[1] === HEMIE_NATION ? "Global Settings > Dwelling Details" : `Global Settings > Defaults > Fabric${constructionType ? ` > ${hemieConstructionTypeLabel(constructionType)}` : ""}`
|
|
2354
|
+
};
|
|
2355
|
+
}
|
|
2356
|
+
const row = settingsRow(segments);
|
|
2357
|
+
const section = row ? globalSettingsFieldSection(row) : null;
|
|
2358
|
+
if (section) return { location: SECTION_LOCATIONS[section] };
|
|
2359
|
+
if (segments.length >= 2) {
|
|
2360
|
+
return {
|
|
2361
|
+
location: `${titleCaseToken(segments[0])} > ${titleCaseToken(segments[1])}`,
|
|
2362
|
+
field: segments[2]
|
|
2363
|
+
};
|
|
2364
|
+
}
|
|
2365
|
+
if (segments.length === 1) {
|
|
2366
|
+
return { location: titleCaseToken(segments[0]) };
|
|
2367
|
+
}
|
|
2368
|
+
return { location: "Model" };
|
|
2369
|
+
};
|
|
2370
|
+
var compactPathLabel = (segments) => {
|
|
2371
|
+
if (segments.length === 0) return "model";
|
|
2372
|
+
return segments.map(titleCaseToken).join(" > ");
|
|
2373
|
+
};
|
|
2374
|
+
var requiredFieldFromMessage = (message) => {
|
|
2375
|
+
const match = message.match(/^"([^"]+)" is a required property$/);
|
|
2376
|
+
return match?.[1] ? titleCaseToken(match[1]) : null;
|
|
2377
|
+
};
|
|
2378
|
+
var minimumFromMessage = (message) => {
|
|
2379
|
+
const match = message.match(/^(.+?) is less than the minimum of (.+)$/);
|
|
2380
|
+
return match?.[2]?.trim() || null;
|
|
2381
|
+
};
|
|
2382
|
+
var maximumFromMessage = (message) => {
|
|
2383
|
+
const match = message.match(/^(.+?) is greater than the maximum of (.+)$/);
|
|
2384
|
+
return match?.[2]?.trim() || null;
|
|
2385
|
+
};
|
|
2386
|
+
var unexpectedFieldsFromMessage = (message) => {
|
|
2387
|
+
const match = message.match(/were unexpected\)$/);
|
|
2388
|
+
if (!match) return null;
|
|
2389
|
+
const fields = [...message.matchAll(/'([^']+)'/g)].flatMap((m) => m[1] ? [m[1]] : []);
|
|
2390
|
+
if (fields.length === 0) return null;
|
|
2391
|
+
const shown = fields.slice(0, 5).map(titleCaseToken).join(", ");
|
|
2392
|
+
return fields.length > 5 ? `${shown}, and ${fields.length - 5} more` : shown;
|
|
2393
|
+
};
|
|
2394
|
+
var isSchemaKeywordError = (item) => Boolean(item.keyword || item.schemaPath);
|
|
2395
|
+
var formatBuildErrorForDisplay = (item) => {
|
|
2396
|
+
const pathSegments = pointerSegments(item.path);
|
|
2397
|
+
const { location, field } = buildErrorLocation(item.path);
|
|
2398
|
+
const fieldLabel = field ? titleCaseToken(field) : void 0;
|
|
2399
|
+
const keyword = item.keyword || "";
|
|
2400
|
+
const requiredField = requiredFieldFromMessage(item.message);
|
|
2401
|
+
const minimum = minimumFromMessage(item.message);
|
|
2402
|
+
const maximum = maximumFromMessage(item.message);
|
|
2403
|
+
const unexpectedFields = unexpectedFieldsFromMessage(item.message);
|
|
2404
|
+
const explicitUserMessage = item.userMessage?.trim();
|
|
2405
|
+
let userMessage;
|
|
2406
|
+
if (explicitUserMessage) {
|
|
2407
|
+
userMessage = explicitUserMessage;
|
|
2408
|
+
} else if (location === "Appliances" && (item.message.includes("is not allowed for {}") || item.message.includes("has less than"))) {
|
|
2409
|
+
userMessage = "Add at least one appliance entry.";
|
|
2410
|
+
} else if (pathSegments[0] === "HotWaterDemand" && (item.message.includes("has less than") || keyword === "minProperties")) {
|
|
2411
|
+
userMessage = "Add at least one hot water outlet.";
|
|
2412
|
+
} else if (pathSegments[0] === "Zone" && pathSegments[2] === "Lighting" && pathSegments.length === 3 && item.message.startsWith("null ")) {
|
|
2413
|
+
userMessage = "Add at least one light to this zone.";
|
|
2414
|
+
} else if (pathSegments[0] === "InfiltrationVentilation" && pathSegments[1] === "MechanicalVentilation" && (item.message.includes("schemas listed in the 'oneOf' keyword") || keyword === "oneOf" || keyword === "anyOf")) {
|
|
2415
|
+
userMessage = "Add ventilation position details: height, orientation, and pitch.";
|
|
2416
|
+
} else if (requiredField || keyword === "required") {
|
|
2417
|
+
userMessage = requiredField ? `Add ${requiredField}.` : "Add the missing required field.";
|
|
2418
|
+
} else if (minimum || keyword === "minimum" || keyword === "exclusiveMinimum") {
|
|
2419
|
+
userMessage = `${fieldLabel || "Value"} must be at least ${minimum || "the allowed minimum"}.`;
|
|
2420
|
+
} else if (maximum || keyword === "maximum" || keyword === "exclusiveMaximum") {
|
|
2421
|
+
userMessage = `${fieldLabel || "Value"} must be no more than ${maximum || "the allowed maximum"}.`;
|
|
2422
|
+
} else if (item.message.includes("has less than") || keyword === "minProperties") {
|
|
2423
|
+
userMessage = `Add at least one entry for ${compactPathLabel(pathSegments)}.`;
|
|
2424
|
+
} else if (item.message.includes("schemas listed in the 'oneOf' keyword") || keyword === "oneOf" || keyword === "anyOf") {
|
|
2425
|
+
userMessage = fieldLabel ? `${fieldLabel} does not match an allowed option.` : `${compactPathLabel(pathSegments)} matches none of the forms this HEM version takes. Check its type and the inputs it states.`;
|
|
2426
|
+
} else if (unexpectedFields || keyword === "unevaluatedProperties" || keyword === "additionalProperties") {
|
|
2427
|
+
userMessage = unexpectedFields ? `Remove fields not allowed here: ${unexpectedFields}.` : "Remove fields that are not allowed here.";
|
|
2428
|
+
} else if (keyword === "const" || keyword === "allOf" || item.message.includes("is not allowed for")) {
|
|
2429
|
+
userMessage = location === "Appliances" ? "Check the appliance entries against the selected schema." : "These settings conflict with the selected schema.";
|
|
2430
|
+
} else if (!isSchemaKeywordError(item)) {
|
|
2431
|
+
userMessage = item.message;
|
|
2432
|
+
} else {
|
|
2433
|
+
userMessage = fieldLabel ? `Check ${fieldLabel}.` : "Check this section.";
|
|
2434
|
+
}
|
|
2435
|
+
return { item, location, field, userMessage, pathSegments };
|
|
2436
|
+
};
|
|
2437
|
+
var isNoisyUnexpectedPropertiesError = (error) => error.item.keyword === "unevaluatedProperties" || error.item.keyword === "additionalProperties" || error.item.message.includes("Unevaluated properties are not allowed") || error.item.message.includes("Additional properties are not allowed");
|
|
2438
|
+
var filterDisplayBuildErrors = (errors) => {
|
|
2439
|
+
const concreteErrorParents = /* @__PURE__ */ new Set();
|
|
2440
|
+
for (const error of errors) {
|
|
2441
|
+
if (isNoisyUnexpectedPropertiesError(error)) continue;
|
|
2442
|
+
if (error.pathSegments.length > 1) {
|
|
2443
|
+
concreteErrorParents.add(error.pathSegments.slice(0, -1).join("/"));
|
|
2444
|
+
}
|
|
2445
|
+
}
|
|
2446
|
+
const converterFailed = errors.some((error) => !isSchemaKeywordError(error.item) && !error.item.severity);
|
|
2447
|
+
return errors.filter((error) => {
|
|
2448
|
+
if (converterFailed && isSchemaKeywordError(error.item) && error.pathSegments.length <= 1 && (error.item.keyword === "anyOf" || error.item.keyword === "oneOf")) return false;
|
|
2449
|
+
if (!isNoisyUnexpectedPropertiesError(error)) return true;
|
|
2450
|
+
return !concreteErrorParents.has(error.pathSegments.join("/"));
|
|
2451
|
+
});
|
|
2452
|
+
};
|
|
2453
|
+
var collapseDerivedBuildErrors = (errors, resolve) => {
|
|
2454
|
+
const rows = errors.map((error) => ({ ...error, target: resolve(error.item.path), derived: [] }));
|
|
2455
|
+
const sourceRowByOwner = /* @__PURE__ */ new Map();
|
|
2456
|
+
for (const row of rows) {
|
|
2457
|
+
if (!isSchemaKeywordError(row.item) && !row.item.severity && row.target.section === "Zone" && !sourceRowByOwner.has(row.target.ownerKey)) {
|
|
2458
|
+
sourceRowByOwner.set(row.target.ownerKey, row);
|
|
2459
|
+
}
|
|
2460
|
+
}
|
|
2461
|
+
return rows.filter((row) => {
|
|
2462
|
+
const sourceRow = isSchemaKeywordError(row.item) ? sourceRowByOwner.get(row.target.ownerKey) : void 0;
|
|
2463
|
+
if (!sourceRow) return true;
|
|
2464
|
+
sourceRow.derived.push(row);
|
|
2465
|
+
return false;
|
|
2466
|
+
});
|
|
2467
|
+
};
|
|
2468
|
+
var displayBuildErrorRows = (items, resolve) => collapseDerivedBuildErrors(filterDisplayBuildErrors(items.map(formatBuildErrorForDisplay)), resolve);
|
|
2469
|
+
|
|
2470
|
+
// ../hem-target-runtime/src/hem/pythonFailureMessage.ts
|
|
458
2471
|
var MAX_DETAIL_LINE = 200;
|
|
459
2472
|
function pythonFailureMessage(text) {
|
|
460
2473
|
const lastFrame = text.lastIndexOf('\n File "');
|
|
@@ -465,173 +2478,219 @@ function pythonFailureMessage(text) {
|
|
|
465
2478
|
const exception = lines.slice(header).map((line) => line.trim().replace(/, input_value=.*$/, "]")).filter((line) => line && !line.startsWith("For further information visit")).map((line) => line.length > MAX_DETAIL_LINE ? `${line.slice(0, MAX_DETAIL_LINE - 1)}\u2026` : line);
|
|
466
2479
|
return `HEM could not calculate this model. ${exception.join(" ")}`;
|
|
467
2480
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
const keys = profile.type === "reference" ? ["type"] : ["type", "weekday", "weekend", "setback"];
|
|
474
|
-
if (Object.keys(profile).some((key) => !keys.includes(key))) throw new Error("Unknown heating profile field");
|
|
475
|
-
if (profile.type === "reference") return { type: "reference" };
|
|
476
|
-
if (profile.type !== "weekly") throw new Error("Heating profile type must be reference or weekly");
|
|
477
|
-
if (typeof profile.setback !== "number" || !Number.isFinite(profile.setback) || profile.setback < 0 || profile.setback > 30) throw new Error("Heating setback must be between 0 and 30 C");
|
|
478
|
-
for (const day of ["weekday", "weekend"]) {
|
|
479
|
-
const periods = profile[day];
|
|
480
|
-
if (!Array.isArray(periods) || !periods.length) throw new Error(`Heating ${day} needs at least one period`);
|
|
481
|
-
let end = 0;
|
|
482
|
-
for (const period of periods) {
|
|
483
|
-
if (!Array.isArray(period) || period.length !== 2 || period.some((time) => typeof time !== "number" || !Number.isFinite(time)) || period[0] < 0 || period[1] > 24 || period[0] >= period[1]) throw new Error("Heating periods must be [start, end] within 0\u201324 hours");
|
|
484
|
-
if (period[0] < end) throw new Error("Heating periods must be ordered without overlap");
|
|
485
|
-
end = period[1];
|
|
486
|
-
}
|
|
2481
|
+
var TargetRunError = class extends Error {
|
|
2482
|
+
constructor(message, failure, traceback) {
|
|
2483
|
+
super(message);
|
|
2484
|
+
this.failure = failure;
|
|
2485
|
+
this.traceback = traceback;
|
|
487
2486
|
}
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
if (profile.type === "reference") return input;
|
|
493
|
-
const model = JSON.parse(input);
|
|
494
|
-
const names = /* @__PURE__ */ new Set();
|
|
495
|
-
for (const system of Object.values(model.SpaceHeatSystem ?? {})) {
|
|
496
|
-
if (typeof system.Control !== "string") throw new Error("Space heating system has no generated setpoint control");
|
|
497
|
-
names.add(system.Control);
|
|
2487
|
+
};
|
|
2488
|
+
function targetRunFailureOf(error) {
|
|
2489
|
+
for (let current = error; current instanceof Error; current = current.cause) {
|
|
2490
|
+
if (current instanceof TargetRunError) return current.failure;
|
|
498
2491
|
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
const target = [...targets][0];
|
|
520
|
-
if (profile.setback > target) throw new Error("Heating setback exceeds the generated occupied target");
|
|
521
|
-
const day = (periods) => {
|
|
522
|
-
if (periods.flat().some((hour) => !Number.isInteger(hour / step))) throw new Error("Heating periods must align with the generated control timestep");
|
|
523
|
-
return Array.from({ length: 24 / step }, (_, index) => periods.some(([start, end]) => index * step >= start && index * step < end) ? target : null);
|
|
524
|
-
};
|
|
525
|
-
control.schedule = {
|
|
526
|
-
main: [{ value: "week", repeat: Math.ceil(model.SimulationTime.end / 168) + 1 }],
|
|
527
|
-
week: [{ value: "weekday", repeat: 5 }, { value: "weekend", repeat: 2 }],
|
|
528
|
-
weekday: day(profile.weekday),
|
|
529
|
-
weekend: day(profile.weekend)
|
|
530
|
-
};
|
|
531
|
-
control.advanced_start = 0;
|
|
532
|
-
control.setpoint_min = profile.setback;
|
|
2492
|
+
return void 0;
|
|
2493
|
+
}
|
|
2494
|
+
var pointer = (segments) => segments.map((segment) => `/${segment.replace(/~/g, "~0").replace(/\//g, "~1")}`).join("");
|
|
2495
|
+
var SYSTEM_SECTIONS = /* @__PURE__ */ new Set(["HotWaterSource", "HeatSourceWet", "SpaceHeatSystem", "SpaceCoolSystem"]);
|
|
2496
|
+
function walk(input, loc, type) {
|
|
2497
|
+
const segments = [];
|
|
2498
|
+
let node = input;
|
|
2499
|
+
let building = false;
|
|
2500
|
+
let arm;
|
|
2501
|
+
for (const [index, raw] of loc.entries()) {
|
|
2502
|
+
const key = String(raw);
|
|
2503
|
+
const present = node !== null && typeof node === "object" && Object.prototype.hasOwnProperty.call(node, key);
|
|
2504
|
+
if (present) {
|
|
2505
|
+
segments.push(key);
|
|
2506
|
+
node = node[key];
|
|
2507
|
+
if (node?.model_type === "hemie" && !SYSTEM_SECTIONS.has(segments[0])) building = true;
|
|
2508
|
+
} else if (index === loc.length - 1) {
|
|
2509
|
+
segments.push(key);
|
|
2510
|
+
return { segments, building, arm, missing: type === "missing" };
|
|
2511
|
+
} else arm ?? (arm = { place: pointer(segments), tag: key, hemieInput: node?.model_type !== void 0 });
|
|
533
2512
|
}
|
|
534
|
-
return
|
|
2513
|
+
return { segments, building, arm, missing: false };
|
|
535
2514
|
}
|
|
536
|
-
|
|
537
|
-
const
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
const
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
2515
|
+
function chosenArms(all) {
|
|
2516
|
+
const key = (error) => `${pointer(error.segments)}
|
|
2517
|
+
${error.msg}`;
|
|
2518
|
+
const dropped = /* @__PURE__ */ new Set();
|
|
2519
|
+
for (const [index, error] of all.entries()) {
|
|
2520
|
+
if (error.type !== "extra_forbidden" || error.segments[error.segments.length - 1] !== "model_type") continue;
|
|
2521
|
+
dropped.add(index);
|
|
2522
|
+
const parent = pointer(error.segments.slice(0, -1));
|
|
2523
|
+
for (let previous = index - 1; previous >= 0; previous -= 1) {
|
|
2524
|
+
const candidate = all[previous];
|
|
2525
|
+
if (!pointer(candidate.segments).startsWith(parent) || candidate.arm?.tag !== error.arm?.tag) break;
|
|
2526
|
+
const copyElsewhere = all.some((other, at) => at !== previous && !dropped.has(at) && key(other) === key(candidate));
|
|
2527
|
+
if (!candidate.missing && !copyElsewhere) break;
|
|
2528
|
+
dropped.add(previous);
|
|
2529
|
+
}
|
|
2530
|
+
}
|
|
2531
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2532
|
+
const kept = all.filter((_, index) => !dropped.has(index));
|
|
2533
|
+
const walked = (kept.length ? kept : all).filter((error) => !seen.has(key(error)) && seen.add(key(error)));
|
|
2534
|
+
const arms = /* @__PURE__ */ new Map();
|
|
2535
|
+
for (const { arm } of walked) {
|
|
2536
|
+
if (!arm) continue;
|
|
2537
|
+
const place = arms.get(arm.place) ?? /* @__PURE__ */ new Map();
|
|
2538
|
+
const container = /^(dict|list)\[/.test(arm.tag);
|
|
2539
|
+
const entry = place.get(arm.tag) ?? { count: 0, stated: arm.hemieInput !== container };
|
|
2540
|
+
entry.count += 1;
|
|
2541
|
+
place.set(arm.tag, entry);
|
|
2542
|
+
arms.set(arm.place, place);
|
|
2543
|
+
}
|
|
2544
|
+
const best = new Map([...arms].map(([place, tags]) => [place, [...tags].sort(([, a], [, b]) => Number(b.stated) - Number(a.stated) || a.count - b.count)[0][0]]));
|
|
2545
|
+
return walked.filter(({ arm }) => !arm || best.get(arm.place) === arm.tag);
|
|
546
2546
|
}
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
2547
|
+
var STAGE_TEXT = {
|
|
2548
|
+
input_check: "the input check",
|
|
2549
|
+
product_lookup: "the product lookup",
|
|
2550
|
+
existing_building_conversion: "the existing building conversion",
|
|
2551
|
+
preprocessing: "preparation",
|
|
2552
|
+
core: "the simulation",
|
|
2553
|
+
metrics: "the rating calculation",
|
|
2554
|
+
unclassified: "the calculation"
|
|
2555
|
+
};
|
|
2556
|
+
var firstLine = (text) => (text.trim().split("\n")[0] ?? "").replace(/^Value error, /, "").slice(0, MAX_DETAIL_LINE);
|
|
2557
|
+
var SYNTHESISED_FIELDS = /* @__PURE__ */ new Set(["model_type", "reference_building_part", "storey_index", "floorplan_index"]);
|
|
2558
|
+
var CODE_FAULTS = /* @__PURE__ */ new Set(["TypeError", "KeyError", "AttributeError", "IndexError", "NameError", "AssertionError", "UnboundLocalError"]);
|
|
2559
|
+
var VULCAN_FAULT = "Vulcan could not convert this existing building for the EPC calculation. This is a fault in Vulcan, not in your model. Report it to Vulcan support with this model.";
|
|
2560
|
+
var BUILDING_SECTIONS = { Zone: "the building", InfiltrationVentilation: "ventilation and air tightness", ExternalConditions: "shading" };
|
|
2561
|
+
function buildingPlace(segments) {
|
|
2562
|
+
const parts = [];
|
|
2563
|
+
for (let i = 1; i < segments.length - 1; i += 1) {
|
|
2564
|
+
const [key, next] = [segments[i], segments[i + 1]];
|
|
2565
|
+
if (key === "building_parts") {
|
|
2566
|
+
parts.push(`building part ${next}`);
|
|
2567
|
+
i += 1;
|
|
2568
|
+
} else if (/^\d+$/.test(next)) {
|
|
2569
|
+
parts.push(`${key.replace(/_/g, " ").replace(/s$/, "")} ${Number(next) + 1}`);
|
|
2570
|
+
i += 1;
|
|
563
2571
|
}
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
});
|
|
2572
|
+
}
|
|
2573
|
+
return parts.length ? parts.join(", ") : BUILDING_SECTIONS[segments[0]] ?? segments[0];
|
|
567
2574
|
}
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
const
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
const
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
const
|
|
594
|
-
const
|
|
595
|
-
|
|
596
|
-
const target_primary_energy_rate = rate(notional, "TPER", "kWh/m2", notionalPath);
|
|
597
|
-
const dwelling_fabric_energy_efficiency = rate(rows(feePath), "Fabric Energy Efficiency", "kWh / m2.yr", feePath);
|
|
598
|
-
const target_fabric_energy_efficiency = rate(rows(notionalFeePath), "Fabric Energy Efficiency", "kWh / m2.yr", notionalFeePath);
|
|
599
|
-
return {
|
|
600
|
-
dwelling_emission_rate,
|
|
601
|
-
target_emission_rate,
|
|
602
|
-
emission_rate_compliant: dwelling_emission_rate <= target_emission_rate,
|
|
603
|
-
dwelling_primary_energy_rate,
|
|
604
|
-
target_primary_energy_rate,
|
|
605
|
-
primary_energy_rate_compliant: dwelling_primary_energy_rate <= target_primary_energy_rate,
|
|
606
|
-
dwelling_fabric_energy_efficiency,
|
|
607
|
-
target_fabric_energy_efficiency,
|
|
608
|
-
fabric_energy_efficiency_compliant: dwelling_fabric_energy_efficiency <= target_fabric_energy_efficiency
|
|
609
|
-
};
|
|
2575
|
+
var readable = (field, text) => text.replace(
|
|
2576
|
+
/^Input should be (.*?)\.?$/,
|
|
2577
|
+
(_, rule) => `${field.replace(/_/g, " ").replace(/^\w/, (c) => c.toUpperCase())} must be ${rule}.`
|
|
2578
|
+
);
|
|
2579
|
+
function targetRunError(report, input) {
|
|
2580
|
+
const model = JSON.parse(input);
|
|
2581
|
+
const walked = chosenArms((report.errors ?? []).map((error) => ({ ...walk(model, error.loc, error.type), type: error.type, msg: error.msg })));
|
|
2582
|
+
const notes = /* @__PURE__ */ new Set();
|
|
2583
|
+
const issues = [];
|
|
2584
|
+
for (const { segments, building, missing, msg } of walked) {
|
|
2585
|
+
const field = segments[segments.length - 1] ?? "";
|
|
2586
|
+
if (building && SYNTHESISED_FIELDS.has(field)) notes.add(VULCAN_FAULT);
|
|
2587
|
+
else if (building) {
|
|
2588
|
+
const reason = missing ? `${field.replace(/_/g, " ")} is missing` : readable(field, firstLine(msg ?? "")).replace(/\.$/, "");
|
|
2589
|
+
notes.add(`The EPC calculation could not use this existing building: ${reason} (${buildingPlace(segments)}). Check the elements it is built from; if they are correct, report it to Vulcan support.`);
|
|
2590
|
+
} else issues.push(missing ? { path: pointer(segments.slice(0, -1)), keyword: "required", message: `"${field}" is a required property` } : { path: pointer(segments), message: readable(field, firstLine(msg ?? "")) });
|
|
2591
|
+
}
|
|
2592
|
+
const conversionStage = report.stage === "existing_building_conversion";
|
|
2593
|
+
if (!walked.length && conversionStage) {
|
|
2594
|
+
notes.add(CODE_FAULTS.has(report.kind) ? VULCAN_FAULT : `The EPC calculation could not use this existing building: ${firstLine(report.message)}. Check the elements it is built from; if they are correct, report it to Vulcan support.`);
|
|
2595
|
+
}
|
|
2596
|
+
const cause = notes.size ? "conversion" : issues.length || ["input_check", "product_lookup"].includes(report.stage) ? "input" : "simulation";
|
|
2597
|
+
if (!notes.size && !issues.length) {
|
|
2598
|
+
notes.add(cause === "input" ? `The EPC calculation stopped at ${STAGE_TEXT[report.stage]}: ${firstLine(report.message)}` : `The EPC calculation stopped during ${STAGE_TEXT[report.stage]}: ${firstLine(report.message)}. Report it to Vulcan support with this model if it happens again.`);
|
|
2599
|
+
}
|
|
2600
|
+
const rows = displayBuildErrorRows(issues.map((issue) => ({ source: "schema", ...issue })), () => ({ ownerKey: "" }));
|
|
2601
|
+
const message = [...notes, ...rows.length ? [`The EPC calculation found ${rows.length === 1 ? "an error" : `${rows.length} errors`} in the model. ${rows.map((row) => `${row.location}: ${row.userMessage}`).join(" ")}`] : []].join(" ");
|
|
2602
|
+
return new TargetRunError(message, { stage: report.stage, kind: report.kind, cause, issues, notes: [...notes] }, report.traceback);
|
|
610
2603
|
}
|
|
611
2604
|
|
|
612
2605
|
// ../hem-target-runtime/src/hem/targets.ts
|
|
613
2606
|
var WRAPPERS = {
|
|
614
2607
|
fhs: {
|
|
2608
|
+
/** Wire name the target preparation artifacts accept. */
|
|
615
2609
|
profile: "fhs",
|
|
2610
|
+
/** Community defaults template the family's bundles pin for preparation. */
|
|
2611
|
+
preparationDefaults: "defaults_template.json",
|
|
2612
|
+
/** Schema dialect the editor reads; separate from the wire profile. */
|
|
2613
|
+
schemaMode: "fhs",
|
|
616
2614
|
modes: ["actual", "actual-fee", "notional", "notional-fee"],
|
|
2615
|
+
/** Modes of a scenario that selects no model wrapper. */
|
|
2616
|
+
defaultModes: ["actual"],
|
|
617
2617
|
scenarioModes: {
|
|
618
2618
|
fhs_assumptions: ["actual"],
|
|
619
2619
|
fhs_compliance: ["actual", "actual-fee", "notional", "notional-fee"]
|
|
620
2620
|
},
|
|
621
|
-
|
|
2621
|
+
/** The run-mode name users see, per scenario wrapper. */
|
|
2622
|
+
scenarioLabels: { fhs_assumptions: "Single run", fhs_compliance: "Compliance" },
|
|
2623
|
+
/** How the run's results are read; written to target-run.json. */
|
|
2624
|
+
resultsKind: "fhs",
|
|
622
2625
|
coreDistribution: "hem-core",
|
|
623
|
-
wrapperDistribution: "hem-fhs-wrapper"
|
|
2626
|
+
wrapperDistribution: "hem-fhs-wrapper",
|
|
2627
|
+
/** Further pinned distributions; each target pins their version and commit in `components`. */
|
|
2628
|
+
componentDistributions: [],
|
|
2629
|
+
/** Systems take HEM Database products, picked in the editor's product slots. */
|
|
2630
|
+
hemDatabaseProducts: false,
|
|
2631
|
+
/** The scenario wrapper an agent's single-model run uses when the caller names none. */
|
|
2632
|
+
singleRunWrapper: "fhs_compliance",
|
|
2633
|
+
/** Fabric from construction bands on an existing building (the editor's `constructionBands` rule). */
|
|
2634
|
+
constructionBands: false,
|
|
2635
|
+
/** A scenario may pick its own weather file. */
|
|
2636
|
+
customWeather: true
|
|
2637
|
+
},
|
|
2638
|
+
epc: {
|
|
2639
|
+
/** vulcan-model-transform `ModelProfile::Epc`. */
|
|
2640
|
+
profile: "epc",
|
|
2641
|
+
/** The shared template plus the HEM:EPC fields with no Vulcan source. */
|
|
2642
|
+
preparationDefaults: "defaults_template_epc.json",
|
|
2643
|
+
/** The full-input schema is a Core-shaped input (pydantic unions), not the FHS dialect. */
|
|
2644
|
+
schemaMode: "core",
|
|
2645
|
+
/** `quick` is the main EPC run only (upstream --skip-smart-metric --skip-fees-metric); `full` adds the smart and FEES runs. */
|
|
2646
|
+
modes: ["quick", "full"],
|
|
2647
|
+
defaultModes: ["quick"],
|
|
2648
|
+
scenarioModes: {
|
|
2649
|
+
epc_quick: ["quick"],
|
|
2650
|
+
epc_full: ["full"]
|
|
2651
|
+
},
|
|
2652
|
+
scenarioLabels: { epc_quick: "Rating and energy", epc_full: "Full EPC" },
|
|
2653
|
+
resultsKind: "epc",
|
|
2654
|
+
coreDistribution: "hem-core",
|
|
2655
|
+
wrapperDistribution: "hem-epc-wrapper",
|
|
2656
|
+
componentDistributions: ["hem-epc-orchestrator", "hemie", "hem-db-client"],
|
|
2657
|
+
hemDatabaseProducts: true,
|
|
2658
|
+
singleRunWrapper: "epc_quick",
|
|
2659
|
+
// An existing building converts to HEMIE, whose fabric is the age band and constructions.
|
|
2660
|
+
constructionBands: true,
|
|
2661
|
+
// The Orchestrator runs on its pinned weather (pythonTarget.ts).
|
|
2662
|
+
customWeather: false
|
|
624
2663
|
}
|
|
625
2664
|
};
|
|
626
|
-
function targetWrapper(
|
|
627
|
-
const wrapper = WRAPPERS[
|
|
628
|
-
if (!wrapper) throw new Error(`Unsupported HEM wrapper: ${
|
|
2665
|
+
function targetWrapper(target2) {
|
|
2666
|
+
const wrapper = WRAPPERS[target2.wrapper];
|
|
2667
|
+
if (!wrapper) throw new Error(`Unsupported HEM wrapper: ${target2.wrapper}`);
|
|
629
2668
|
return wrapper;
|
|
630
2669
|
}
|
|
631
2670
|
var HEM_TARGETS = [
|
|
2671
|
+
// The Rust a9 that ECaaS runs in production (epb-ecaas-api e1c090e, lambda/Cargo.lock). Its wrapper
|
|
2672
|
+
// schema equals the Python a9 one, so it keeps that conversion profile's wire name. It is built
|
|
2673
|
+
// with the carried patch set fhs-a9-schema-v1 (targetSchemaCorrections.ts); build and parity:
|
|
2674
|
+
// docs/development/HEM_Rust_A9_Target.md.
|
|
2675
|
+
{
|
|
2676
|
+
id: "rust-fhs-a9-9e41af89-f2c2425f-v1",
|
|
2677
|
+
label: "HEM:FHS 1.0.0a9",
|
|
2678
|
+
runtime: "rust",
|
|
2679
|
+
version: "1.0.0a9",
|
|
2680
|
+
engineVersion: "1.0.0a9",
|
|
2681
|
+
wrapperVersion: "1.0.0a9",
|
|
2682
|
+
wrapper: "fhs",
|
|
2683
|
+
engineCommit: "9e41af890cc62a1cec08957340231c3591956dac",
|
|
2684
|
+
wrapperCommit: "f2c2425f00870e1611750bd2ac1be20132301e0d",
|
|
2685
|
+
conversionProfile: "python_fhs_a9",
|
|
2686
|
+
// Measured, example_semi_detached in wasm-browser-cli at load 13-19 (13 November): actual mode
|
|
2687
|
+
// 5.9-9.0 s, all four modes 48.3-48.6 s (docs/development/HEM_Rust_A9_Target.md).
|
|
2688
|
+
status: "provisional",
|
|
2689
|
+
typicalRunMinutes: { fhs_assumptions: 0.15, fhs_compliance: 0.8 }
|
|
2690
|
+
},
|
|
632
2691
|
{
|
|
633
2692
|
id: "rust-fhs-a7-62d3df70-c5ba2673-v1",
|
|
634
|
-
label: "
|
|
2693
|
+
label: "HEM:FHS 1.0.0a7",
|
|
635
2694
|
runtime: "rust",
|
|
636
2695
|
version: "1.0.0a7",
|
|
637
2696
|
engineVersion: "1.0.0a7",
|
|
@@ -639,54 +2698,62 @@ var HEM_TARGETS = [
|
|
|
639
2698
|
wrapper: "fhs",
|
|
640
2699
|
engineCommit: "62d3df705690f33b3fc3e905c9971d4f3743bf2e",
|
|
641
2700
|
wrapperCommit: "c5ba2673fbd886cfe4fb528f61b376bdf406ebbd",
|
|
642
|
-
conversionProfile: "current_rust_fhs"
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
wrapperVersion: "1.0.0a9",
|
|
651
|
-
wrapper: "fhs",
|
|
652
|
-
engineCommit: "918addad2f9ffbcd9c7a4ed73497d047210be2d2",
|
|
653
|
-
wrapperCommit: "b9b901382feb0e5d27f969532842250147fb4068",
|
|
654
|
-
conversionProfile: "python_fhs_a9"
|
|
655
|
-
},
|
|
2701
|
+
conversionProfile: "current_rust_fhs",
|
|
2702
|
+
// Actual mode: house_two_storey 7.4 s and 8.1 s through wasm-browser-cli at load 16-20 (13 November). Compliance not measured.
|
|
2703
|
+
status: "legacy",
|
|
2704
|
+
typicalRunMinutes: { fhs_assumptions: 0.12 }
|
|
2705
|
+
}
|
|
2706
|
+
];
|
|
2707
|
+
var RECOMMENDED_HEM_TARGET = HEM_TARGETS[0].id;
|
|
2708
|
+
var UNRELEASED_HEM_TARGETS = [
|
|
656
2709
|
{
|
|
657
|
-
id: "python-
|
|
658
|
-
label: "
|
|
2710
|
+
id: "python-epc-0.13.2-6367f1ec-18032feb-v1",
|
|
2711
|
+
label: "HEM:EPC 0.13.2",
|
|
659
2712
|
runtime: "python",
|
|
660
|
-
version: "
|
|
661
|
-
engineVersion: "
|
|
662
|
-
wrapperVersion: "
|
|
663
|
-
wrapper: "
|
|
664
|
-
engineCommit: "
|
|
665
|
-
wrapperCommit: "
|
|
666
|
-
conversionProfile: "
|
|
2713
|
+
version: "0.13.2",
|
|
2714
|
+
engineVersion: "2.0.0a4",
|
|
2715
|
+
wrapperVersion: "0.13.2",
|
|
2716
|
+
wrapper: "epc",
|
|
2717
|
+
engineCommit: "6367f1ecb101c7f72a2006a921c732c790b0d338",
|
|
2718
|
+
wrapperCommit: "18032feb59294b323fc28af4fb032d34bd44bf67",
|
|
2719
|
+
conversionProfile: "epc_full_input_v1",
|
|
2720
|
+
components: {
|
|
2721
|
+
"hem-epc-orchestrator": { version: "0.13.2a0", commit: "993463233ad0c416e83c4de3b84889280d3c4619" },
|
|
2722
|
+
hemie: { version: "0.18.1", commit: "2876e2ec1c7bad2b738c9c3b3685d86860a91f41" },
|
|
2723
|
+
"hem-db-client": { version: "1.0.0a10", commit: "9eeb026dd52a612197b5552ca113d8121fa72679" }
|
|
2724
|
+
},
|
|
2725
|
+
// db-client/data/*.csv at the hem-db-client commit.
|
|
2726
|
+
productDataSha256: "d0acf4ebf050e80f3dc576ffbb1295ebeb1176e9fd4a9ec1d151a9e8f4df7457",
|
|
2727
|
+
// Quick, measured: house_two_storey 491-535 s in wasm-browser-cli (HEM_EPC_Integration_Plan.md, Status).
|
|
2728
|
+
// Full, estimated: 35 min natively at the measured 2.4-2.6x browser ratio (same plan, Measured).
|
|
2729
|
+
status: "provisional",
|
|
2730
|
+
typicalRunMinutes: { epc_quick: 9, epc_full: 90 },
|
|
2731
|
+
estimatedRunMinutes: ["epc_full"]
|
|
667
2732
|
}
|
|
668
2733
|
];
|
|
669
|
-
var
|
|
2734
|
+
var findHemTarget = (id) => [...HEM_TARGETS, ...UNRELEASED_HEM_TARGETS].find((target2) => target2.id === id);
|
|
670
2735
|
function resolveHemTarget(id) {
|
|
671
|
-
const
|
|
672
|
-
if (!
|
|
673
|
-
return
|
|
2736
|
+
const target2 = findHemTarget(id);
|
|
2737
|
+
if (!target2) throw new Error(`HEM target '${id}' is unavailable. Select a supported target explicitly; saved runs are never upgraded automatically.`);
|
|
2738
|
+
return target2;
|
|
674
2739
|
}
|
|
675
2740
|
function modelArtifacts(manifest2) {
|
|
676
2741
|
const runtime2 = [manifest2.runtime.javascript, manifest2.runtime.wasm];
|
|
677
2742
|
const converter = [manifest2.preparation.javascript, manifest2.preparation.wasm].filter((path) => !runtime2.includes(path));
|
|
678
2743
|
return manifest2.artifacts.filter((artifact) => !artifact.path.startsWith("source/") && !artifact.path.startsWith("notices/") && !converter.includes(artifact.path));
|
|
679
2744
|
}
|
|
680
|
-
function validateTargetManifest(manifest2,
|
|
681
|
-
if (manifest2.bundleId !==
|
|
682
|
-
throw new Error(`Target manifest identity mismatch for ${
|
|
2745
|
+
function validateTargetManifest(manifest2, target2) {
|
|
2746
|
+
if (manifest2.bundleId !== target2.id || manifest2.protocol !== 1 || manifest2.engineCommit !== target2.engineCommit || manifest2.wrapperCommit !== target2.wrapperCommit || manifest2.conversionProfile !== target2.conversionProfile || JSON.stringify(sortedComponents(manifest2.components)) !== JSON.stringify(sortedComponents(target2.components)) || manifest2.productDataSha256 !== target2.productDataSha256) {
|
|
2747
|
+
throw new Error(`Target manifest identity mismatch for ${target2.id}`);
|
|
683
2748
|
}
|
|
2749
|
+
const wrapper = targetWrapper(target2);
|
|
2750
|
+
if (JSON.stringify(Object.keys(target2.components ?? {}).sort()) !== JSON.stringify([...wrapper.componentDistributions].sort())) throw new Error(`${target2.id} must pin exactly the ${target2.wrapper.toUpperCase()} component distributions`);
|
|
684
2751
|
const paths = /* @__PURE__ */ new Set();
|
|
685
2752
|
const sizesByHash = /* @__PURE__ */ new Map();
|
|
686
2753
|
const isRelativePath = (path) => typeof path === "string" && /^[a-zA-Z0-9_./+-]+$/.test(path) && path.split("/").every((segment) => segment !== "" && segment !== "." && segment !== "..");
|
|
687
2754
|
for (const artifact of manifest2.artifacts) {
|
|
688
2755
|
if (!isRelativePath(artifact.path) || !/^[a-f0-9]{64}$/.test(artifact.sha256) || !Number.isSafeInteger(artifact.bytes) || artifact.bytes <= 0 || paths.has(artifact.path)) {
|
|
689
|
-
throw new Error(`Invalid artifact in ${
|
|
2756
|
+
throw new Error(`Invalid artifact in ${target2.id}: ${artifact.path}`);
|
|
690
2757
|
}
|
|
691
2758
|
const priorSize = sizesByHash.get(artifact.sha256);
|
|
692
2759
|
if (priorSize !== void 0 && priorSize !== artifact.bytes) throw new Error(`Conflicting sizes for pinned artifact hash ${artifact.sha256}`);
|
|
@@ -697,10 +2764,13 @@ function validateTargetManifest(manifest2, target) {
|
|
|
697
2764
|
if (path.startsWith("source/") || path.startsWith("notices/")) throw new Error(`Runtime/preparation cannot reference reserved source/notice material: ${path}`);
|
|
698
2765
|
if (!paths.has(path)) throw new Error(`Target manifest is missing required artifact ${path}`);
|
|
699
2766
|
}
|
|
700
|
-
if (!manifest2.modes.length || new Set(manifest2.modes).size !== manifest2.modes.length || manifest2.modes.some((mode) => !
|
|
701
|
-
|
|
2767
|
+
if (!manifest2.modes.length || new Set(manifest2.modes).size !== manifest2.modes.length || manifest2.modes.some((mode) => !wrapper.modes.includes(mode))) throw new Error(`Unsupported or duplicate ${target2.wrapper.toUpperCase()} calculation modes`);
|
|
2768
|
+
const { data } = manifest2.runtime;
|
|
2769
|
+
if (!!data !== !!target2.productDataSha256) throw new Error("Runtime product data and its pinned hash must be declared together");
|
|
2770
|
+
if (data && (target2.runtime !== "python" || data.startsWith("source/") || data.startsWith("notices/") || !data.endsWith("/") || !isRelativePath(data.slice(0, -1)) || !manifest2.artifacts.some((artifact) => artifact.path.startsWith(data)))) throw new Error("Runtime product data must be a non-empty relative Python bundle directory ending in /");
|
|
2771
|
+
if (target2.runtime === "rust") {
|
|
702
2772
|
if (!manifest2.runtime.wasm) throw new Error("Rust target manifest is missing its runtime WASM artifact");
|
|
703
|
-
if (manifest2.modes.some((mode) => mode !== "actual") && manifest2.modes.length !== targetWrapper(
|
|
2773
|
+
if (manifest2.modes.some((mode) => mode !== "actual") && manifest2.modes.length !== targetWrapper(target2).modes.length) throw new Error("Rust target modes require actual alone or the complete compliance set");
|
|
704
2774
|
} else {
|
|
705
2775
|
const { index, wheels, requiredVersions } = manifest2.runtime;
|
|
706
2776
|
if (index?.startsWith("source/") || index?.startsWith("notices/")) throw new Error("Python runtime cannot use the reserved source/notice directory");
|
|
@@ -709,11 +2779,154 @@ function validateTargetManifest(manifest2, target) {
|
|
|
709
2779
|
for (const file of ["pyodide.asm.js", "pyodide.asm.wasm", "pyodide-lock.json", "python_stdlib.zip"]) {
|
|
710
2780
|
if (!paths.has(`${index}${file}`)) throw new Error(`Python runtime is missing pinned artifact ${index}${file}`);
|
|
711
2781
|
}
|
|
712
|
-
if (requiredVersions?.[
|
|
713
|
-
throw new Error(`Python runtime must pin matching engine ${
|
|
2782
|
+
if (requiredVersions?.[wrapper.coreDistribution] !== target2.engineVersion || requiredVersions?.[wrapper.wrapperDistribution] !== target2.wrapperVersion) {
|
|
2783
|
+
throw new Error(`Python runtime must pin matching engine ${target2.engineVersion} and wrapper ${target2.wrapperVersion} distributions`);
|
|
2784
|
+
}
|
|
2785
|
+
for (const [distribution, { version }] of Object.entries(target2.components ?? {})) {
|
|
2786
|
+
if (requiredVersions?.[distribution] !== version) throw new Error(`Python runtime must pin ${distribution} ${version}`);
|
|
714
2787
|
}
|
|
715
2788
|
}
|
|
716
2789
|
}
|
|
2790
|
+
var sortedComponents = (components) => Object.entries(components ?? {}).sort(([a], [b]) => a.localeCompare(b));
|
|
2791
|
+
async function bundleDataSha256(artifacts, directory) {
|
|
2792
|
+
const lines = artifacts.filter((artifact) => artifact.path.startsWith(directory)).map((artifact) => `${artifact.path.slice(directory.length)} ${artifact.sha256}
|
|
2793
|
+
`).sort().join("");
|
|
2794
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(lines));
|
|
2795
|
+
return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
2796
|
+
}
|
|
2797
|
+
|
|
2798
|
+
// ../hem-target-runtime/src/hem/pythonTarget.ts
|
|
2799
|
+
function pythonEpcArguments(modes) {
|
|
2800
|
+
if (modes.length !== 1 || !["quick", "full"].includes(modes[0])) throw new Error(`An EPC calculation runs exactly one of quick or full, not ${modes.join(", ") || "no mode"}`);
|
|
2801
|
+
const quick = modes[0] === "quick";
|
|
2802
|
+
return { skip_smart_metric: quick, skip_fees_metric: quick };
|
|
2803
|
+
}
|
|
2804
|
+
var epc = {
|
|
2805
|
+
initialise(py, _bundleId, data) {
|
|
2806
|
+
if (!data) throw new Error("The EPC target bundle has no product data");
|
|
2807
|
+
py.runPython(`import contextlib, io, json, os
|
|
2808
|
+
os.environ['MPLBACKEND'] = 'Agg'
|
|
2809
|
+
from pathlib import Path
|
|
2810
|
+
import sqlalchemy
|
|
2811
|
+
from sqlalchemy.pool import StaticPool
|
|
2812
|
+
import hem_db_client
|
|
2813
|
+
from hem_db_client.tools.import_data import import_data
|
|
2814
|
+
from hem_core.hem import OutputFormat
|
|
2815
|
+
from hem_epc_orchestrator.orchestrator import Orchestrator
|
|
2816
|
+
vulcan_epc_engine = sqlalchemy.create_engine('sqlite://', poolclass=StaticPool, connect_args={'check_same_thread': False})
|
|
2817
|
+
# import_data returns False when a CSV has a header and no rows; five pinned CSVs are like that.
|
|
2818
|
+
# Any other import failure raises.
|
|
2819
|
+
with contextlib.redirect_stdout(io.StringIO()):
|
|
2820
|
+
import_data(vulcan_epc_engine, Path(${JSON.stringify(data)}))
|
|
2821
|
+
vulcan_epc_config = Path('/vulcan-epc/db-config.json')
|
|
2822
|
+
vulcan_epc_config.parent.mkdir(parents=True, exist_ok=True)
|
|
2823
|
+
vulcan_epc_config.write_text(json.dumps({'dialect': 'sqlite', 'database': ':memory:'}))
|
|
2824
|
+
import traceback
|
|
2825
|
+
# Calls of the upstream epc.py main, in order, and the stage each starts. Main's later lines are metrics.
|
|
2826
|
+
VULCAN_EPC_MAIN_STAGES = (('model_validate', None), ('create_custom_energy_supply_factors', 'preprocessing'),
|
|
2827
|
+
('smart_controls_24hr_pre_simulation', 'preprocessing'), ('create_core_input', 'preprocessing'),
|
|
2828
|
+
('run_core(', 'core'), ('write_project_outputs', 'core'))
|
|
2829
|
+
def vulcan_epc_stage(error, existing):
|
|
2830
|
+
# The innermost recognised frame names the stage: the Orchestrator's own input parse, its
|
|
2831
|
+
# product substitution (and database), or the line of the wrapper's main that raised.
|
|
2832
|
+
stage = 'unclassified'
|
|
2833
|
+
for frame in traceback.extract_tb(error.__traceback__):
|
|
2834
|
+
if frame.name == 'orchestrator_input': stage = 'input_check'
|
|
2835
|
+
elif frame.name in ('_add_metadata_to_input', '_write_epc_input_file') or '/hem_db_client/' in frame.filename: stage = 'product_lookup'
|
|
2836
|
+
elif frame.filename.endswith('/bin/epc.py') and frame.name == 'main':
|
|
2837
|
+
line = frame.line or ''
|
|
2838
|
+
stage = next(((found or ('existing_building_conversion' if existing else 'input_check')) for call, found in VULCAN_EPC_MAIN_STAGES if call in line), 'metrics')
|
|
2839
|
+
return stage
|
|
2840
|
+
def vulcan_epc_run(input_path, output_dir, options):
|
|
2841
|
+
try:
|
|
2842
|
+
orchestrator = Orchestrator(db_config_file=vulcan_epc_config, input_file=Path(input_path), output_dir=Path(output_dir))
|
|
2843
|
+
orchestrator.client = hem_db_client.Client(engine_url=vulcan_epc_engine)
|
|
2844
|
+
orchestrator.run(**options, core_output_formats=(OutputFormat.CSV,))
|
|
2845
|
+
except Exception as error:
|
|
2846
|
+
existing = json.loads(Path(input_path).read_text()).get('IsExistingBuilding') is True
|
|
2847
|
+
errors = error.errors(include_url=False, include_input=False, include_context=False) if hasattr(error, 'errors') and callable(error.errors) else []
|
|
2848
|
+
# A KeyError's text is only the quoted key, which may be an input field or an internal name.
|
|
2849
|
+
message = f'a value named {error.args[0]} was not found' if isinstance(error, KeyError) and error.args else str(error)
|
|
2850
|
+
return json.dumps({'stage': vulcan_epc_stage(error, existing), 'kind': type(error).__name__, 'message': message,
|
|
2851
|
+
'errors': [{'loc': list(e['loc']), 'type': e['type'], 'msg': e['msg']} for e in errors],
|
|
2852
|
+
'traceback': ''.join(traceback.format_exception(error))})`);
|
|
2853
|
+
},
|
|
2854
|
+
call({ input, output, weather }, modes) {
|
|
2855
|
+
if (weather) throw new Error("HEM:EPC runs on its pinned weather; it does not accept a custom weather file");
|
|
2856
|
+
const options = pythonEpcArguments(modes);
|
|
2857
|
+
return `vulcan_epc_run(${JSON.stringify(input)}, ${JSON.stringify(output)}, {'skip_smart_metric': ${options.skip_smart_metric ? "True" : "False"}, 'skip_fees_metric': ${options.skip_fees_metric ? "True" : "False"}})`;
|
|
2858
|
+
},
|
|
2859
|
+
check({ input, output }) {
|
|
2860
|
+
return `vulcan_epc_run(${JSON.stringify(input)}, ${JSON.stringify(output)}, {'preprocess_only': True})`;
|
|
2861
|
+
},
|
|
2862
|
+
outputs(files) {
|
|
2863
|
+
const metrics = Object.keys(files).filter((name) => name.endsWith("_metrics.json"));
|
|
2864
|
+
if (metrics.length !== 1) throw new Error(`An EPC run must write exactly one metrics file, found ${metrics.length}`);
|
|
2865
|
+
return files;
|
|
2866
|
+
}
|
|
2867
|
+
};
|
|
2868
|
+
var PYTHON_TARGET_ENTRIES = { epc };
|
|
2869
|
+
var pythonEntry = (target2) => {
|
|
2870
|
+
const entry = PYTHON_TARGET_ENTRIES[target2.wrapper];
|
|
2871
|
+
if (!entry) throw new Error(`${target2.label} has no Python runtime`);
|
|
2872
|
+
return entry;
|
|
2873
|
+
};
|
|
2874
|
+
async function startPythonTarget(py, bundle, target2, artifactUrl, artifactBytes) {
|
|
2875
|
+
const { wheels, requiredVersions, data } = bundle.runtime;
|
|
2876
|
+
if (!wheels?.length || !requiredVersions) throw new Error("Incomplete Python runtime manifest");
|
|
2877
|
+
await py.loadPackage("micropip");
|
|
2878
|
+
const install = (paths) => py.runPythonAsync(`import micropip
|
|
2879
|
+
await micropip.install(${JSON.stringify(paths.map(artifactUrl))}, deps=False, index_urls=[])`);
|
|
2880
|
+
const isNumpy = (path) => path.split("/").pop().startsWith("numpy-");
|
|
2881
|
+
await install(wheels.filter(isNumpy));
|
|
2882
|
+
await py.loadPackage(bundle.runtime.packages ?? []);
|
|
2883
|
+
await install(wheels.filter((path) => !isNumpy(path)));
|
|
2884
|
+
const versions = JSON.parse(py.runPython("import importlib.metadata as m,json\njson.dumps({d.metadata['Name'].lower().replace('_','-'):d.version for d in m.distributions()})"));
|
|
2885
|
+
for (const [name, version] of Object.entries(requiredVersions)) {
|
|
2886
|
+
if (versions[name.toLowerCase().replace(/_/g, "-")] !== version) throw new Error(`Python runtime identity mismatch for ${name}`);
|
|
2887
|
+
}
|
|
2888
|
+
if (data) {
|
|
2889
|
+
if (await bundleDataSha256(bundle.artifacts, data) !== target2.productDataSha256) throw new Error(`Runtime data identity mismatch for ${target2.id}`);
|
|
2890
|
+
for (const artifact of bundle.artifacts.filter((artifact2) => artifact2.path.startsWith(data))) {
|
|
2891
|
+
const path = `/${artifact.path}`;
|
|
2892
|
+
py.FS.mkdirTree(path.slice(0, path.lastIndexOf("/")));
|
|
2893
|
+
py.FS.writeFile(path, await artifactBytes(artifact.path));
|
|
2894
|
+
}
|
|
2895
|
+
}
|
|
2896
|
+
pythonEntry(target2).initialise(py, bundle.bundleId, data ? `/${data}` : void 0);
|
|
2897
|
+
}
|
|
2898
|
+
async function inJobFolder(py, input, run2) {
|
|
2899
|
+
const folder = `/runs/${crypto.randomUUID()}`;
|
|
2900
|
+
py.FS.mkdirTree(folder);
|
|
2901
|
+
py.FS.writeFile(`${folder}/model.json`, input);
|
|
2902
|
+
try {
|
|
2903
|
+
return await run2(folder);
|
|
2904
|
+
} finally {
|
|
2905
|
+
await py.runPythonAsync(`import shutil
|
|
2906
|
+
shutil.rmtree(${JSON.stringify(folder)})`);
|
|
2907
|
+
}
|
|
2908
|
+
}
|
|
2909
|
+
async function runEntry(py, code, input) {
|
|
2910
|
+
const report = await py.runPythonAsync(code);
|
|
2911
|
+
if (typeof report === "string") throw targetRunError(JSON.parse(report), input);
|
|
2912
|
+
}
|
|
2913
|
+
async function runPythonTargetJob(py, target2, input, modes, weather, heatingProfile) {
|
|
2914
|
+
const entry = pythonEntry(target2);
|
|
2915
|
+
return inJobFolder(py, input, async (folder) => {
|
|
2916
|
+
const weatherPath = weather ? `${folder}/weather.epw` : void 0;
|
|
2917
|
+
if (weather) py.FS.writeFile(weatherPath, weather.epw);
|
|
2918
|
+
if (heatingProfile) throw new Error(`${target2.label} does not support heating profiles`);
|
|
2919
|
+
const output = `${folder}/model__results`;
|
|
2920
|
+
await runEntry(py, entry.call({ input: `${folder}/model.json`, output, weather: weatherPath }, modes), input);
|
|
2921
|
+
const files = Object.fromEntries(py.FS.readdir(output).flatMap((name) => name !== "." && name !== ".." ? [[name, py.FS.readFile(`${output}/${name}`, { encoding: "utf8" })]] : []));
|
|
2922
|
+
return entry.outputs(files, modes);
|
|
2923
|
+
});
|
|
2924
|
+
}
|
|
2925
|
+
async function checkPythonTargetJob(py, target2, input) {
|
|
2926
|
+
const { check } = pythonEntry(target2);
|
|
2927
|
+
if (!check) throw new Error(`${target2.label} has no preprocess-only run`);
|
|
2928
|
+
await inJobFolder(py, input, (folder) => runEntry(py, check({ input: `${folder}/model.json`, output: `${folder}/model__results` }), input));
|
|
2929
|
+
}
|
|
717
2930
|
|
|
718
2931
|
// ../hem-target-runtime/src/hem/targetAssets.ts
|
|
719
2932
|
var CACHE = "vulcan-hem-artifacts-v1";
|
|
@@ -852,58 +3065,6 @@ var TargetAssets = class _TargetAssets {
|
|
|
852
3065
|
}
|
|
853
3066
|
};
|
|
854
3067
|
|
|
855
|
-
// ../hem-target-runtime/src/hem/targetSchemaCorrections.ts
|
|
856
|
-
var PYTHON_TARGETS = /* @__PURE__ */ new Set(["python-fhs-a8-f2ab6cf7-8ca182b0-v1", "python-fhs-a9-918addad-b9b90138-v1"]);
|
|
857
|
-
var RUST_A7 = "rust-fhs-a7-62d3df70-c5ba2673-v1";
|
|
858
|
-
var FIELD = "is_adjacent_space_within_dwelling";
|
|
859
|
-
var WARM_AIR_FIELD = "temp_diff_emit_dsgn";
|
|
860
|
-
var STORAGE_FAN_FIELD = "fan_pwr";
|
|
861
|
-
var hasTargetSchemaCorrection = (targetId) => PYTHON_TARGETS.has(targetId) || targetId === RUST_A7;
|
|
862
|
-
var ruleTypes = (rule) => [rule.if?.properties?.type?.const, ...(rule.if?.anyOf ?? []).map((branch) => branch?.properties?.type?.const)].filter((type) => !!type);
|
|
863
|
-
function thenFor(targetId, rules, ...types) {
|
|
864
|
-
const matches = rules.filter((rule) => {
|
|
865
|
-
const own = ruleTypes(rule);
|
|
866
|
-
return own.length === types.length && types.every((type) => own.includes(type));
|
|
867
|
-
});
|
|
868
|
-
if (matches.length !== 1) throw new Error(`Schema correction for ${targetId} no longer applies: ${matches.length} rules for ${types.join(" or ")}`);
|
|
869
|
-
return matches[0].then;
|
|
870
|
-
}
|
|
871
|
-
function correctTargetSchema(targetId, schemaText) {
|
|
872
|
-
if (!hasTargetSchemaCorrection(targetId)) return schemaText;
|
|
873
|
-
const schema = JSON.parse(schemaText);
|
|
874
|
-
const elementRules = schema.properties.Zone.additionalProperties.properties.BuildingElement.additionalProperties.allOf;
|
|
875
|
-
if (targetId === RUST_A7) {
|
|
876
|
-
const window2 = thenFor(targetId, elementRules, "BuildingElementTransparent");
|
|
877
|
-
const shared = schema.$defs.WindowShading;
|
|
878
|
-
const types = shared.items.properties.type.enum;
|
|
879
|
-
if (window2.properties.shading?.$ref !== "#/$defs/WindowShading" || !types.includes("reveal")) {
|
|
880
|
-
throw new Error(`Schema correction for ${targetId} no longer applies`);
|
|
881
|
-
}
|
|
882
|
-
const shading = structuredClone(shared);
|
|
883
|
-
shading.items.properties.type.enum = types.filter((type) => type !== "reveal");
|
|
884
|
-
window2.properties.shading = shading;
|
|
885
|
-
return JSON.stringify(schema);
|
|
886
|
-
}
|
|
887
|
-
const branch = thenFor(
|
|
888
|
-
targetId,
|
|
889
|
-
elementRules,
|
|
890
|
-
"BuildingElementAdjacentConditionedSpace",
|
|
891
|
-
"BuildingElementAdjacentUnconditionedSpace_Simple"
|
|
892
|
-
);
|
|
893
|
-
const spaceHeatRules = schema.properties.SpaceHeatSystem.additionalProperties.allOf;
|
|
894
|
-
const storage = thenFor(targetId, spaceHeatRules, "ElecStorageHeater");
|
|
895
|
-
const warmAir = thenFor(targetId, spaceHeatRules, "WarmAir");
|
|
896
|
-
if (!branch.required.includes(FIELD) || !warmAir.required.includes(WARM_AIR_FIELD) || storage.properties[STORAGE_FAN_FIELD]) {
|
|
897
|
-
throw new Error(`Schema correction for ${targetId} no longer applies`);
|
|
898
|
-
}
|
|
899
|
-
branch.required = branch.required.filter((key) => key !== FIELD);
|
|
900
|
-
warmAir.required = warmAir.required.filter((key) => key !== WARM_AIR_FIELD);
|
|
901
|
-
warmAir.properties[WARM_AIR_FIELD] = false;
|
|
902
|
-
storage.properties[STORAGE_FAN_FIELD] = { type: "number", minimum: 0 };
|
|
903
|
-
storage.required = [...storage.required, STORAGE_FAN_FIELD];
|
|
904
|
-
return JSON.stringify(schema);
|
|
905
|
-
}
|
|
906
|
-
|
|
907
3068
|
// ../hem-target-runtime/src/workers/workerProtocol.ts
|
|
908
3069
|
var FS_REQUEST_TYPE = "file_system";
|
|
909
3070
|
var FS_RESPONSE_TYPE = "file_system_response";
|
|
@@ -1005,13 +3166,36 @@ var BatchWorkerFileSystemAcknowledgements = class {
|
|
|
1005
3166
|
}
|
|
1006
3167
|
};
|
|
1007
3168
|
|
|
3169
|
+
// ../hem-target-runtime/src/hem/heatingProfile.ts
|
|
3170
|
+
function parseHeatingProfile(value) {
|
|
3171
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("Heating profile must be an object");
|
|
3172
|
+
const profile = value;
|
|
3173
|
+
const keys = profile.type === "reference" ? ["type"] : ["type", "weekday", "weekend", "setback"];
|
|
3174
|
+
if (Object.keys(profile).some((key) => !keys.includes(key))) throw new Error("Unknown heating profile field");
|
|
3175
|
+
if (profile.type === "reference") return { type: "reference" };
|
|
3176
|
+
if (profile.type !== "weekly") throw new Error("Heating profile type must be reference or weekly");
|
|
3177
|
+
if (typeof profile.setback !== "number" || !Number.isFinite(profile.setback) || profile.setback < 0 || profile.setback > 30) throw new Error("Heating setback must be between 0 and 30 C");
|
|
3178
|
+
for (const day of ["weekday", "weekend"]) {
|
|
3179
|
+
const periods = profile[day];
|
|
3180
|
+
if (!Array.isArray(periods) || !periods.length) throw new Error(`Heating ${day} needs at least one period`);
|
|
3181
|
+
let end = 0;
|
|
3182
|
+
for (const period of periods) {
|
|
3183
|
+
if (!Array.isArray(period) || period.length !== 2 || period.some((time) => typeof time !== "number" || !Number.isFinite(time)) || period[0] < 0 || period[1] > 24 || period[0] >= period[1]) throw new Error("Heating periods must be [start, end] within 0\u201324 hours");
|
|
3184
|
+
if (period[0] < end) throw new Error("Heating periods must be ordered without overlap");
|
|
3185
|
+
end = period[1];
|
|
3186
|
+
}
|
|
3187
|
+
}
|
|
3188
|
+
return structuredClone(profile);
|
|
3189
|
+
}
|
|
3190
|
+
|
|
1008
3191
|
// ../hem-target-runtime/src/hem/preparedRun.ts
|
|
1009
|
-
async function executePreparedJobs(manifest2, bundleId, jobId, jobs, execute, write2, progress) {
|
|
3192
|
+
async function executePreparedJobs(manifest2, bundleId, jobId, jobs, execute, write2, progress, check) {
|
|
1010
3193
|
if (manifest2.bundleId !== bundleId) throw new Error("Worker target identity mismatch");
|
|
1011
3194
|
if (!jobs.length) throw new Error("Batch has no prepared jobs");
|
|
1012
3195
|
const allowedModes = new Set(manifest2.modes);
|
|
3196
|
+
const { resultsKind } = targetWrapper(resolveHemTarget(manifest2.bundleId));
|
|
1013
3197
|
const reservedNames = /* @__PURE__ */ new Set(["model.json", "source.csv", "weather.epw", "target-run.json"]);
|
|
1014
|
-
for (const
|
|
3198
|
+
for (const job of jobs) {
|
|
1015
3199
|
if (!/^[a-zA-Z0-9_./ -]+$/.test(job.outputDirectory) || job.outputDirectory.split("/").some((segment) => segment === "..") || !job.outputDirectory.startsWith("output/")) throw new Error("Invalid run output path");
|
|
1016
3200
|
if (!job.modes.length || job.modes.some((mode) => !allowedModes.has(mode))) throw new Error("Unsupported calculation mode for selected target");
|
|
1017
3201
|
if (job.archiveDirectory && (!job.archiveDirectory.startsWith(`${job.outputDirectory}/`) || job.archiveDirectory.split("/").some((segment) => segment === ".."))) throw new Error("Invalid archive path");
|
|
@@ -1019,10 +3203,35 @@ async function executePreparedJobs(manifest2, bundleId, jobId, jobs, execute, wr
|
|
|
1019
3203
|
parseHeatingProfile(job.heatingProfile);
|
|
1020
3204
|
if (job.modes.length !== 1 || job.modes[0] !== "actual") throw new Error("Heating profiles require actual alone; they are not FHS compliance calculations");
|
|
1021
3205
|
}
|
|
3206
|
+
}
|
|
3207
|
+
const scenario = (job) => `Scenario ${job.name}${job.parameterFiles?.length ? ` (${job.parameterFiles.join(", ")})` : ""}`;
|
|
3208
|
+
const traceback = (error) => error instanceof TargetRunError ? { traceback: error.traceback } : {};
|
|
3209
|
+
if (check) {
|
|
3210
|
+
const failures = [];
|
|
3211
|
+
for (const [index, job] of jobs.entries()) {
|
|
3212
|
+
progress(index, jobs.length, "Checking");
|
|
3213
|
+
try {
|
|
3214
|
+
await check(job.input);
|
|
3215
|
+
} catch (error) {
|
|
3216
|
+
failures.push({ job, error, message: error instanceof Error ? error.message : String(error) });
|
|
3217
|
+
}
|
|
3218
|
+
}
|
|
3219
|
+
for (const { job, error, message } of failures) {
|
|
3220
|
+
const archive = job.archiveDirectory ?? job.outputDirectory;
|
|
3221
|
+
const record = { target: manifest2, jobId, name: job.name, modes: job.modes, status: "failed", check: true, error: message };
|
|
3222
|
+
await write2(`${archive}/model.json`, job.input);
|
|
3223
|
+
await write2(`${archive}/target-run.json`, JSON.stringify({ ...record, ...traceback(error) }));
|
|
3224
|
+
if (archive !== job.outputDirectory) await write2(`${job.outputDirectory}/target-run.json`, JSON.stringify({ ...record, archive }));
|
|
3225
|
+
}
|
|
3226
|
+
if (failures.length) {
|
|
3227
|
+
throw Object.assign(new Error(failures.map(({ job, message }) => `${scenario(job)}: ${message}`).join("\n")), { cause: failures[0].error });
|
|
3228
|
+
}
|
|
3229
|
+
}
|
|
3230
|
+
for (const [index, job] of jobs.entries()) {
|
|
1022
3231
|
const archive = job.archiveDirectory ?? job.outputDirectory;
|
|
1023
3232
|
const inputSha256 = await sha256(new TextEncoder().encode(job.input));
|
|
1024
3233
|
if (job.weather && (!job.weather.epw.trim() || await sha256(new TextEncoder().encode(job.weather.epw)) !== job.weather.sha256)) throw new Error("Prepared weather hash mismatch or empty EPW");
|
|
1025
|
-
const provenance = { ...job.weather ? { weather: { path: job.weather.path, sha256: job.weather.sha256 } } : {}, ...job.heatingProfile ? { calculationKind: "operating-sensitivity", heatingProfile: job.heatingProfile } : {}, target: manifest2, inputSha256, ...job.preparedBy ? { preparedBy: job.preparedBy } : {}, modes: job.modes, jobId, ...job.startedAt ? { startedAt: job.startedAt } : {}, name: job.name, ...job.warnings?.length ? { warnings: job.warnings } : {} };
|
|
3234
|
+
const provenance = { ...job.weather ? { weather: { path: job.weather.path, sha256: job.weather.sha256 } } : {}, ...job.heatingProfile ? { calculationKind: "operating-sensitivity", heatingProfile: job.heatingProfile } : {}, target: manifest2, resultsKind, inputSha256, ...job.preparedBy ? { preparedBy: job.preparedBy } : {}, modes: job.modes, jobId, ...job.startedAt ? { startedAt: job.startedAt } : {}, name: job.name, ...job.warnings?.length ? { warnings: job.warnings } : {} };
|
|
1026
3235
|
try {
|
|
1027
3236
|
await write2(`${archive}/target-run.json`, JSON.stringify({ ...provenance, status: "running" }));
|
|
1028
3237
|
if (archive !== job.outputDirectory) await write2(`${job.outputDirectory}/target-run.json`, JSON.stringify({ ...provenance, status: "running", archive }));
|
|
@@ -1044,17 +3253,17 @@ async function executePreparedJobs(manifest2, bundleId, jobId, jobs, execute, wr
|
|
|
1044
3253
|
await write2(`${archive}/target-run.json`, JSON.stringify({ ...provenance, status: "complete", files: names }));
|
|
1045
3254
|
} catch (error) {
|
|
1046
3255
|
const message = error instanceof Error ? error.message : String(error);
|
|
1047
|
-
await write2(`${archive}/target-run.json`, JSON.stringify({ ...provenance, status: "failed", error: message }));
|
|
3256
|
+
await write2(`${archive}/target-run.json`, JSON.stringify({ ...provenance, status: "failed", error: message, ...traceback(error) }));
|
|
1048
3257
|
if (archive !== job.outputDirectory) await write2(`${job.outputDirectory}/target-run.json`, JSON.stringify({ ...provenance, status: "failed", error: message, archive }));
|
|
1049
|
-
|
|
1050
|
-
throw new Error(`Scenario ${job.name}${files}: ${message}`);
|
|
3258
|
+
throw Object.assign(new Error(`${scenario(job)}: ${message}`), { cause: error });
|
|
1051
3259
|
}
|
|
1052
3260
|
}
|
|
1053
3261
|
}
|
|
1054
3262
|
|
|
1055
|
-
// ../hem-target-runtime/src/hem/
|
|
3263
|
+
// ../hem-target-runtime/src/hem/pythonTargetWorker.ts
|
|
1056
3264
|
var runtime;
|
|
1057
3265
|
var manifest;
|
|
3266
|
+
var target;
|
|
1058
3267
|
var busy = false;
|
|
1059
3268
|
var acknowledgements = new BatchWorkerFileSystemAcknowledgements();
|
|
1060
3269
|
var send = (message) => self.postMessage(message);
|
|
@@ -1065,15 +3274,15 @@ async function write(path, content) {
|
|
|
1065
3274
|
await acknowledgements.waitForPending();
|
|
1066
3275
|
}
|
|
1067
3276
|
async function initialize(bundle) {
|
|
1068
|
-
const
|
|
1069
|
-
validateTargetManifest(bundle,
|
|
1070
|
-
if (
|
|
3277
|
+
const selected = resolveHemTarget(bundle.bundleId);
|
|
3278
|
+
validateTargetManifest(bundle, selected);
|
|
3279
|
+
if (selected.runtime !== "python") throw new Error("Python worker requires a Python target");
|
|
1071
3280
|
const assets = await TargetAssets.open();
|
|
1072
3281
|
const responses = /* @__PURE__ */ new Map();
|
|
3282
|
+
const artifactUrl = (path) => new URL(`/hem-targets/${bundle.bundleId}/${path}`, self.location.origin).href;
|
|
1073
3283
|
await Promise.all(modelArtifacts(bundle).map(async (artifact) => {
|
|
1074
3284
|
const bytes = await assets.bytes(artifact);
|
|
1075
|
-
|
|
1076
|
-
responses.set(url, new Response(bytes, { headers: { "Content-Type": artifact.path.endsWith(".wasm") ? "application/wasm" : "application/octet-stream" } }));
|
|
3285
|
+
responses.set(artifactUrl(artifact.path), new Response(bytes, { headers: { "Content-Type": artifact.path.endsWith(".wasm") ? "application/wasm" : "application/octet-stream" } }));
|
|
1077
3286
|
}));
|
|
1078
3287
|
self.fetch = async (input) => {
|
|
1079
3288
|
const url = new URL(typeof input === "string" ? input : input instanceof URL ? input.href : input.url, self.location.origin).href;
|
|
@@ -1081,10 +3290,13 @@ async function initialize(bundle) {
|
|
|
1081
3290
|
if (!response) throw new Error(`Python attempted to load an artifact outside its pinned bundle: ${url}`);
|
|
1082
3291
|
return response.clone();
|
|
1083
3292
|
};
|
|
1084
|
-
const
|
|
1085
|
-
const response = responses.get(
|
|
3293
|
+
const artifactBytes = async (path) => {
|
|
3294
|
+
const response = responses.get(artifactUrl(path));
|
|
1086
3295
|
if (!response) throw new Error(`Missing executable artifact ${path}`);
|
|
1087
|
-
|
|
3296
|
+
return new Uint8Array(await response.clone().arrayBuffer());
|
|
3297
|
+
};
|
|
3298
|
+
const importAsset = async (path) => {
|
|
3299
|
+
const url = URL.createObjectURL(new Blob([await artifactBytes(path)], { type: "text/javascript" }));
|
|
1088
3300
|
try {
|
|
1089
3301
|
return await import(
|
|
1090
3302
|
/* @vite-ignore */
|
|
@@ -1095,58 +3307,34 @@ async function initialize(bundle) {
|
|
|
1095
3307
|
}
|
|
1096
3308
|
};
|
|
1097
3309
|
const index = bundle.runtime.index;
|
|
1098
|
-
if (!index
|
|
3310
|
+
if (!index) throw new Error("Incomplete Python runtime manifest");
|
|
1099
3311
|
await importAsset(`${index}pyodide.asm.js`);
|
|
1100
3312
|
const { loadPyodide } = await importAsset(bundle.runtime.javascript);
|
|
1101
|
-
|
|
1102
|
-
indexURL:
|
|
3313
|
+
const py = await loadPyodide({
|
|
3314
|
+
indexURL: artifactUrl(index),
|
|
1103
3315
|
stdout: (message) => send({ type: "worker_log", message }),
|
|
1104
3316
|
stderr: (message) => send({ type: "worker_log", message })
|
|
1105
3317
|
});
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
const install = (paths) => py.runPythonAsync(`import micropip
|
|
1109
|
-
await micropip.install(${JSON.stringify(paths.map((path) => new URL(`/hem-targets/${bundle.bundleId}/${path}`, self.location.origin).href))}, index_urls=[])`);
|
|
1110
|
-
await install(bundle.runtime.wheels.filter((path) => path.split("/").pop().startsWith("numpy-")));
|
|
1111
|
-
await py.loadPackage(bundle.runtime.packages ?? []);
|
|
1112
|
-
await install(bundle.runtime.wheels.filter((path) => !path.split("/").pop().startsWith("numpy-")));
|
|
1113
|
-
py.runPython(`import os
|
|
1114
|
-
os.environ['MPLBACKEND']='Agg'
|
|
1115
|
-
from ${targetWrapper(target).pythonModule} import main`);
|
|
1116
|
-
const versions = JSON.parse(py.runPython("import importlib.metadata as m,json\njson.dumps({d.metadata['Name'].lower().replace('_','-'):d.version for d in m.distributions()})"));
|
|
1117
|
-
for (const [name, version] of Object.entries(bundle.runtime.requiredVersions)) {
|
|
1118
|
-
if (versions[name.toLowerCase().replace(/_/g, "-")] !== version) throw new Error(`Python runtime identity mismatch for ${name}`);
|
|
1119
|
-
}
|
|
1120
|
-
if (hasTargetSchemaCorrection(bundle.bundleId)) {
|
|
1121
|
-
const schemaPath = py.runPython(`import os, future_homes_standard.schema_validation as v
|
|
1122
|
-
os.path.join(os.path.dirname(v.__file__), 'schema.json')`);
|
|
1123
|
-
py.FS.writeFile(schemaPath, correctTargetSchema(bundle.bundleId, py.FS.readFile(schemaPath, { encoding: "utf8" })));
|
|
1124
|
-
}
|
|
3318
|
+
await startPythonTarget(py, bundle, selected, artifactUrl, artifactBytes);
|
|
3319
|
+
runtime = py;
|
|
1125
3320
|
manifest = bundle;
|
|
3321
|
+
target = selected;
|
|
1126
3322
|
send({ type: "worker_ready", targetBundleId: bundle.bundleId });
|
|
1127
3323
|
}
|
|
1128
3324
|
async function run(jobId, bundleId, jobs) {
|
|
1129
|
-
if (!runtime || !manifest) throw new Error("Python worker is not initialized");
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
const compliance = buildPythonFhsComplianceReport(files, modes);
|
|
1143
|
-
if (compliance) files["fhs_compliance_report.json"] = JSON.stringify(compliance);
|
|
1144
|
-
return files;
|
|
1145
|
-
} finally {
|
|
1146
|
-
await py.runPythonAsync(`import shutil
|
|
1147
|
-
shutil.rmtree(${JSON.stringify(folder)})`);
|
|
1148
|
-
}
|
|
1149
|
-
}, write, (completed, total) => send({ type: "progress", jobId, progress: { completed, total, status: "Calculating" } }));
|
|
3325
|
+
if (!runtime || !manifest || !target) throw new Error("Python worker is not initialized");
|
|
3326
|
+
const [py, selected] = [runtime, target];
|
|
3327
|
+
const check = jobs.length > 1 && PYTHON_TARGET_ENTRIES[selected.wrapper]?.check ? (input) => checkPythonTargetJob(py, selected, input) : void 0;
|
|
3328
|
+
await executePreparedJobs(
|
|
3329
|
+
manifest,
|
|
3330
|
+
bundleId,
|
|
3331
|
+
jobId,
|
|
3332
|
+
jobs,
|
|
3333
|
+
(input, modes, weather, heatingProfile) => runPythonTargetJob(py, selected, input, modes, weather, heatingProfile),
|
|
3334
|
+
write,
|
|
3335
|
+
(completed, total, status) => send({ type: "progress", jobId, progress: { completed, total, status: status ?? "Calculating" } }),
|
|
3336
|
+
check
|
|
3337
|
+
);
|
|
1150
3338
|
send({ type: "complete", jobId, targetBundleId: bundleId, result: { status: "success", targetBundleId: bundleId } });
|
|
1151
3339
|
}
|
|
1152
3340
|
self.onmessage = async ({ data }) => {
|
|
@@ -1166,18 +3354,35 @@ self.onmessage = async ({ data }) => {
|
|
|
1166
3354
|
} catch (error) {
|
|
1167
3355
|
const text = error instanceof Error ? error.message : String(error);
|
|
1168
3356
|
console.error(text);
|
|
1169
|
-
|
|
3357
|
+
const failure = targetRunFailureOf(error);
|
|
3358
|
+
send({ type: data.type === "initialize" ? "init_error" : "error", jobId: data.jobId, error: failure ? text : pythonFailureMessage(text), ...failure ? { failure } : {} });
|
|
1170
3359
|
} finally {
|
|
1171
3360
|
busy = false;
|
|
1172
3361
|
}
|
|
1173
3362
|
};
|
|
1174
3363
|
/*! Bundled license information:
|
|
1175
3364
|
|
|
1176
|
-
|
|
1177
|
-
(
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
3365
|
+
polygon-clipping/dist/polygon-clipping.umd.js:
|
|
3366
|
+
(**
|
|
3367
|
+
* splaytree v3.1.2
|
|
3368
|
+
* Fast Splay tree for Node and browser
|
|
3369
|
+
*
|
|
3370
|
+
* @author Alexander Milevski <info@w8r.name>
|
|
3371
|
+
* @license MIT
|
|
3372
|
+
* @preserve
|
|
3373
|
+
*)
|
|
3374
|
+
(*! *****************************************************************************
|
|
3375
|
+
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3376
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
3377
|
+
this file except in compliance with the License. You may obtain a copy of the
|
|
3378
|
+
License at http://www.apache.org/licenses/LICENSE-2.0
|
|
3379
|
+
|
|
3380
|
+
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
3381
|
+
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
|
|
3382
|
+
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
|
3383
|
+
MERCHANTABLITY OR NON-INFRINGEMENT.
|
|
3384
|
+
|
|
3385
|
+
See the Apache Version 2.0 License for specific language governing permissions
|
|
3386
|
+
and limitations under the License.
|
|
3387
|
+
***************************************************************************** *)
|
|
1183
3388
|
*/
|