forked from opensourceBIM/BIMsurfer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvgoverlay.js
More file actions
583 lines (508 loc) · 18 KB
/
Copy pathsvgoverlay.js
File metadata and controls
583 lines (508 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import * as mat4 from "./glmatrix/mat4.js";
import * as vec4 from "./glmatrix/vec4.js";
import * as vec3 from "./glmatrix/vec3.js";
import * as vec2 from "./glmatrix/vec2.js"
import * as mat2 from "./glmatrix/mat2.js"
class SvgOverlayNode {
constructor(overlay, svgElem) {
this.overlay = overlay;
this.svgElem = svgElem;
this.additionalElems = [];
this._lastVisibilityState = null;
}
process() {
let v = this.isVisible();
if (v !== this._lastVisibilityState) {
this.svgElem.setAttribute("visibility", v ? "visible" : "hidden");
}
if (this.beforeUpdate) {
this.beforeUpdate();
}
if (this._lastVisibilityState = v) {
this.doUpdate();
}
}
destroy() {
this.overlay.nodes.splice(this.overlay.nodes.indexOf(this), 1);
let deleteSvgElem = (el) => {
el.parentElement.removeChild(el);
};
deleteSvgElem(this.svgElem);
this.additionalElems.forEach(deleteSvgElem);
}
}
class OrbitCenterOverlayNode extends SvgOverlayNode {
constructor(overlay, svgElem, camera) {
super(overlay, svgElem);
this.camera = camera;
}
isVisible() {
return this.camera.orbitting;
}
doUpdate() {
let xy = this.overlay.transformPoint(this.camera.center);
this.svgElem.setAttribute("cx", xy[0]);
this.svgElem.setAttribute("cy", xy[1]);
}
}
class PathOverlayNode extends SvgOverlayNode {
// Changed: for new section
constructor(overlay, points, sectionPlaneHelper) {
super(overlay, null);
this._points = points;
this.svgElem = overlay.create("path", {
fill: "lightblue",
stroke: "black",
"fill-opacity": 0.1,
d: this.createPathAttribute()
});
this.sectionPlaneHelper = sectionPlaneHelper;
}
createPathAttribute() {
return "M" + this._points.map((p) => this.overlay.toString(this.overlay.transformPoint(p))).join(" L");
}
isVisible() {
return true;
}
get points() {
return this._points;
}
set points(p) {
this._points = p;
this.doUpdate();
}
doUpdate() {
this.svgElem.setAttribute("d", this.createPathAttribute());
// Changed: for new section
this.svgElem.setAttribute("fill", this.sectionPlaneHelper.isSectionMoving ? "red" : "lightblue");
}
}
const tmp_mat_1 = mat2.create();
// Calculates the intersection between two (infinite) lines by
// means of determinants, as described on:
// https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
function intersectLine(R, p00, p01, p10, p11) {
mat2.set(tmp_mat_1, p00[0], p01[0], p00[1], p01[1]);
const d00 = mat2.determinant(tmp_mat_1);
mat2.set(tmp_mat_1, p10[0], p11[0], p10[1], p11[1]);
const d01 = mat2.determinant(tmp_mat_1);
const d10 = p00[0] - p01[0];
const d11 = p10[0] - p11[0];
mat2.set(tmp_mat_1, d00, d01, d10, d11);
const x0 = mat2.determinant(tmp_mat_1);
const d30 = p00[1] - p01[1];
const d31 = p10[1] - p11[1];
mat2.set(tmp_mat_1, d00, d01, d30, d31);
const y0 = mat2.determinant(tmp_mat_1);
mat2.set(tmp_mat_1, d10, d11, d30, d31);
const D = mat2.determinant(tmp_mat_1);
vec2.set(R, x0 / D, y0 / D);
}
const tmp_measurement_ = vec3.create();
const tmp_measurement_2 = [vec2.create(), vec2.create(), vec2.create(), vec2.create()];
class InfiniteLine extends SvgOverlayNode {
constructor(overlay, measurement, point, normal) {
super(overlay, null);
this.measurement = measurement;
this.points = [
vec3.clone(point), vec3.add(vec3.create(), point, normal)
];
// Changed: new measurement color
this.svgElem = overlay.create("path", {
stroke: "rgb(255, 0, 128)",
d: this.createPathAttribute()
}, {
strokeDasharray: 4,
opacity: 0.5
});
this.process();
}
isVisible() {
return this.measurement.constrain && !this.measurement.fixed;
}
createPathAttribute() {
let a = vec3.copy(tmp_measurement_, this.overlay.transformPoint(this.points[0]));
let b = this.overlay.transformPoint(this.points[1]);
for (var i = 0; i < 4; ++i) {
intersectLine(
tmp_measurement_2[i],
a,
b,
this.overlay.boundaryPoints[i],
this.overlay.boundaryPoints[(i + 1) % 4]
);
}
const dists = tmp_measurement_2.map((v, i) => [vec2.dist(v, this.overlay.centerPoint), i]);
dists.sort((a, b) => a[0] - b[0]);
return "M" + [
tmp_measurement_2[dists[0][1]],
tmp_measurement_2[dists[1][1]]
].map(this.overlay.toString).join(" L");
}
doUpdate() {
this.svgElem.setAttribute("d", this.createPathAttribute());
}
}
class MeasurementNode extends SvgOverlayNode {
constructor(overlay, point, normal, constrain) {
super(overlay, null);
this._points = [new Float32Array(point), new Float32Array(point)];
this._points_constrained = [new Float32Array(point), new Float32Array(point)];
this._constrain = !!constrain;
this.fixed = false;
this.normal = normal;
this.line = new InfiniteLine(overlay, this, point, normal);
overlay.nodes.push(this.line);
this.svgElem = overlay.create("path", {
// Changed: new measurement color
stroke: "rgb(255, 0, 128)",
fill: "none",
d: this.createPathAttribute()
}, {
markerStart: "url(#m1)",
markerEnd: "url(#m0)"
});
// Changed: backgroud of measurement value
this.labelBackground = overlay.create("rect", {
}, {
fill: "rgb(200, 200, 200)",
stroke: "rgb(200, 200, 200)",
strokeWidth: 0.5,
paintOrder: "stroke",
alignmentBaseline: "middle",
rx: 5
});
this.additionalElems.push(this.labelBackground);
this.label = overlay.create("text", {
}, {
textAnchor: "middle",
fontFamilt: "verdana",
fontSize: "12pt",
// Changed: new measurement color
fill: "rgb(255, 0, 128)",
stroke: "rgb(255, 0, 128)",
strokeWidth: 0.5,
paintOrder: "stroke",
alignmentBaseline: "middle"
});
this.label.appendChild(document.createTextNode(""));
this.additionalElems.push(this.label);
}
createPathAttribute() {
// Actually it's nicer to just show something to indicate to the user
// something is happening.
/*if (this.points.length < 2 || this.length <= 1) {
return "";
}*/
// return "M" + this.points.map((p) => this.overlay.toString(this.overlay.transformPoint(p))).join(" L");
return "M" + this.points
.map(this.overlay.transformPoint.bind(this.overlay))
.filter(p => p[3] > 0.)
.map(this.overlay.toString.bind(this.overlay))
.join(" L");
}
isVisible() {
return true;
}
get num_points() {
let n = this._points.length;
if (!this.fixed) {
// last point is still in progress
n--;
}
return n;
}
get constrain() {
return this._constrain;
}
set constrain(b) {
this._constrain = b;
this.doUpdate();
}
get length() {
if (this.points.length < 2) {
return 0;
}
let L = 0;
for (let i = 0; i < this.points.length - 1; ++i) {
vec3.subtract(tmp_measurement_, this.points[i], this.points[i + 1]);
L += vec3.length(tmp_measurement_);
}
return L;
}
get midpoint() {
if (this.points.length < 2) {
return null;
} else if (this.points.length == 2) {
let x = this.overlay.transformPoint(this.points[0]);
if (x[3] < 0.) {
tmp_measurement_.fill(-100);
} else {
vec3.copy(tmp_measurement_, x);
let t = this.overlay.transformPoint(this.points[1]);
vec2.add(tmp_measurement_, tmp_measurement_, t);
vec2.scale(tmp_measurement_, tmp_measurement_, 0.5);
}
} else {
let M = this.length / 2.;
let accum = 0.;
for (let i = 0; i < this.points.length - 1; ++i) {
vec3.subtract(tmp_measurement_, this.points[i], this.points[i + 1]);
let segmentLength = vec3.length(tmp_measurement_);
if (M - accum < segmentLength) {
let x = this.overlay.transformPoint(this.points[i]);
if (x[3] < 0.) {
tmp_measurement_.fill(-100);
} else {
vec3.copy(tmp_measurement_, x);
let t = this.overlay.transformPoint(this.points[i + 1]);
vec2.lerp(tmp_measurement_, tmp_measurement_, t, (M - accum) / segmentLength);
}
break;
}
accum += segmentLength;
}
}
return tmp_measurement_;
}
get angle() {
if (this.points.length < 2) {
return null;
} else if (this.points.length == 2) {
vec3.copy(tmp_measurement_, this.overlay.transformPoint(this.points[0]));
let t = this.overlay.transformPoint(this.points[1]);
vec2.subtract(tmp_measurement_, t, tmp_measurement_);
return Math.atan2(tmp_measurement_[1], tmp_measurement_[0]) * 180. / Math.PI;
} else {
let M = this.length / 2.;
let accum = 0.;
for (let i = 0; i < this.points.length - 1; ++i) {
vec3.subtract(tmp_measurement_, this.points[i], this.points[i + 1]);
let segmentLength = vec3.length(tmp_measurement_);
if (M - accum < segmentLength) {
vec3.copy(tmp_measurement_, this.overlay.transformPoint(this.points[i]));
let t = this.overlay.transformPoint(this.points[i + 1]);
vec2.subtract(tmp_measurement_, t, tmp_measurement_);
return Math.atan2(tmp_measurement_[1], tmp_measurement_[0]) * 180. / Math.PI;
}
accum += segmentLength;
}
}
}
get points() {
if (this.constrain) {
return this._points_constrained;
} else {
return this._points;
}
}
updatePoint(p) {
this.points[this.points.length - 1] = new Float32Array(p);
if (this.constrain) {
let p = this.points;
vec3.subtract(tmp_measurement_, p[1], p[0]);
let l = vec3.dot(tmp_measurement_, this.normal);
vec3.scale(tmp_measurement_, this.normal, l);
vec3.add(tmp_measurement_, tmp_measurement_, p[0]);
this._points_constrained = [p[0], new Float32Array(tmp_measurement_)];
}
this.doUpdate();
}
fixPoint(p) {
this.points.push(new Float32Array(this.points[this.points.length - 1]));
}
popLastPoint() {
this.points.pop();
this.doUpdate();
}
doUpdate() {
this.svgElem.setAttribute("d", this.createPathAttribute());
let mp = this.midpoint;
if (mp === null) return;
let x = mp[0];
let y = mp[1];
this.label.setAttribute("x", x);
// Changed: backgroud of measurement value
this.label.setAttribute("y", y - 10);
let l = this.length;
// Changed: show measure unit
this.label.childNodes[0].textContent = (l > 1 ? (l / 1000).toFixed(2) : "") + " [m]";
let a = this.angle;
if (a < -90 || a > 90) {
a += 180;
}
this.label.setAttribute("transform", `rotate(${a} ${x} ${y}) translate(0 -7)`);
// Changed: backgroud of measurement value
let measurementValueBox = this.label.getBBox();
measurementValueBox.height += 5;
measurementValueBox.width += 5;
this.labelBackground.setAttribute("x", x - measurementValueBox.width / 2);
this.labelBackground.setAttribute("y", y - measurementValueBox.height / 2 - 10);
this.labelBackground.setAttribute("width", measurementValueBox.width);
this.labelBackground.setAttribute("height", measurementValueBox.height);
this.labelBackground.setAttribute("transform", `rotate(${a} ${x} ${y}) translate(0 -7)`);
}
}
/**
* A SVG overlay that is synced with the WebGL viewport for efficiently rendering
* two-dimensional elements such as text, that are not easily rendered using WebGL.
*
* @class SvgOverlay
*/
export class SvgOverlay {
constructor(domNode, camera) {
this.track = domNode;
this.camera = camera;
this.tmp = vec3.create();
let svg = this.svg = this.create("svg", { id: "viewerOverlay" }, {
padding: 0,
margin: 0,
position: "absolute",
top: 0,
left: 0,
zIndex: 10000,
display: "block",
"pointer-events": "none"
});
// Changed: because of z-index problems
const wrapper = document.getElementsByTagName("kros-bim-view")[0];
(wrapper ? wrapper : document.body).appendChild(svg);
this.hasWrapper = !!wrapper;
let defs = this.create("defs", null, null);
for (let i = 0; i < 2; ++i) {
let marker = this.create("marker", {
id: `m${i}`,
markerWidth: 13,
markerHeight: 13,
refX: [10, 2][i],
refY: 6,
orient: "auto"
}, null, defs);
this.create("path", {
d: ["M2,2 L2,11 L10,6 L2,2", "M10,2 L10,11 L2,6 L10,2"][i],
}, {
// Changed: new measurement color
fill: "rgb(255, 0, 128)"
}, marker);
// Changed: disable | in arrow
// this.create("path", {
// d : ["M10,2 L10,11", "M2,2 L2,11"][i],
// }, {
// stroke: "#000000",
// strokeWidth: 1
// }, marker);
}
// Initialize by resize()
this.boundaryPoints = [
vec2.create(),
vec2.create(),
vec2.create(),
vec2.create()
];
this.centerPoint = vec2.create();
this.resize();
this.camera.listeners.push(this.update.bind(this));
this._orbitCenter = this.create("circle", {
visibility: "hidden",
r: 6,
fill: "white",
stroke: "black",
"fill-opacity": 0.4
});
// This is an array of elements that have methods to query their visibility
// and update their SVG positioning
this.nodes = [new OrbitCenterOverlayNode(this, this._orbitCenter, this.camera)];
window.addEventListener("resize", this.resize.bind(this), false);
}
transformPoint(p) {
// let t = this.tmp;
let t = vec4.create();
let x = new Float32Array(4);
x.set(p, 0);
x[3] = 1.;
vec4.transformMat4(t, x, this.camera.viewProjMatrix);
t[1] *= -1;
vec3.scale(t, t, 1. / t[3]);
vec2.multiply(t, t, this.wh);
vec2.add(t, t, this.wh);
return t;
}
toString(t) {
return t[0] + "," + t[1];
}
update() {
this.nodes.forEach((n) => {
n.process();
});
}
create(tag, attrs, style, parent) {
let elem = document.createElementNS("http://www.w3.org/2000/svg", tag);
for (let [k, v] of Object.entries(attrs || {})) {
elem.setAttribute(k, v);
}
let s = elem.style;
for (let [k, v] of Object.entries(style || {})) {
s[k] = v;
}
if (typeof (parent) === 'undefined') {
parent = this.svg;
}
if (parent) {
parent.appendChild(elem);
}
return elem;
}
createWorldSpacePolyline(points, sectionPlaneHelper) {
// Changed: for new section
let node = new PathOverlayNode(this, points, sectionPlaneHelper);
this.nodes.push(node);
return node;
}
addMeasurement(point, normal, constrain) {
let node = new MeasurementNode(this, point, normal, constrain);
this.nodes.push(node);
return node;
}
resize() {
function getElementXY(e) {
var x = 0, y = 0;
while (e) {
x += (e.offsetLeft - e.scrollLeft);
y += (e.offsetTop - e.scrollTop);
e = e.offsetParent;
}
var bodyRect = document.body.getBoundingClientRect();
return {
x: (x - bodyRect.left),
y: (y - bodyRect.top)
};
}
let svgStyle = this.svg.style;
var xy = getElementXY(this.track);
// Changed: because of z-index problems
if (!this.hasWrapper) {
var xy = getElementXY(this.track);
svgStyle.left = xy.x + "px";
svgStyle.top = xy.y + "px";
}
svgStyle.width = (this.w = this.track.clientWidth) + "px";
svgStyle.height = (this.h = this.track.clientHeight) + "px";
this.svg.setAttribute("width", this.w);
this.svg.setAttribute("height", this.h);
this.svg.setAttribute("viewBox", "0 0 " + this.w + " " + this.h);
this.w /= 2.;
this.h /= 2.;
this.wh = vec2.fromValues(this.w, this.h);
// (0,0)
// (w,0)
// (w,h)
// (0,h)
this.boundaryPoints[1][0] = this.w * 2;
this.boundaryPoints[2][0] = this.w * 2;
this.boundaryPoints[2][1] = this.h * 2;
this.boundaryPoints[3][1] = this.h * 2;
this.centerPoint[0] = this.w;
this.centerPoint[1] = this.h;
this.aspect = this.w / this.h;
}
}