-
Notifications
You must be signed in to change notification settings - Fork 0
/
3D5.js
executable file
·599 lines (485 loc) · 17.3 KB
/
3D5.js
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/**
* REIMPLEMENT D3CONNECTGROUPs
*/
"use strict";
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var P2 = Math.PI * 2;
function D3mod(n) {
var m = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var M = arguments[2];
var _sort = [m, M].sort();
var _sort2 = _slicedToArray(_sort, 2);
m = _sort2[0];
M = _sort2[1];
var d = M - m;
return n < m ? n + d : n > M ? n - d : n;
} //D3mod
function D3inherit(a, b) {
for (var i in b) {
a[i] = b[i];
}
return a;
} //D3inherit
var D3Map = function () {
function D3Map() {
var d = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 5;
var pen = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document.getElementsByTagName("canvas")[0].getContext("2d");
_classCallCheck(this, D3Map);
this.surface = [0, 0, d]; //coordinates
this.camera = [0, 0, 0]; //coordinates
this.size = [pen.canvas.width, pen.canvas.height]; //sizes
this.orientation = [0, 0, 0]; //angles - radians
this.children = []; //OBSOLETE(?)
this.Vertex = D3Map.Vertex;
this.Cube = D3Map.Cube;
this.Line = D3Map.Line;
this.RENDER_MODES = D3Map.RENDER_MODES;
this.MAPPINGS = D3Map.MAPPINGS;
this.SPLITTERS = D3Map.SPLITTERS;
} //ctor
_createClass(D3Map, [{
key: "rotate",
//g-field
value: function rotate() {
var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : -this.orientation[0];
var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var z = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
this.orientation[0] = D3mod(this.orientation[0] + x, 0, P2);
this.orientation[1] = D3mod(this.orientation[1] + y, 0, P2);
this.orientation[2] = D3mod(this.orientation[2] + z, 0, P2);
return this;
} //rotate
}, {
key: "translate",
value: function translate() {
var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : -this.camera[0];
var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var z = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
this.camera[0] += x;
this.camera[1] += y;
this.camera[2] += z;
this.surface[2] += z;
return this;
} //translate
}, {
key: "display",
value: function display() {
var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var z = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.surface[2];
D3inherit(this.surface, [x, y, z]);
return this;
} //display
}, {
key: "reset",
value: function reset() {
this.orientation = [0, 0, 0];
this.camera = [0, 0, 0];
return this;
} //reset
}, {
key: "add",
value: function add() {
var _this = this;
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
if (args.every(function (arg) {
return typeof arg === "number";
})) {
return this.add(new (Function.prototype.bind.apply(this.Vertex, [null].concat(args, [this])))());
} else if (args.every(function (arg) {
return arg instanceof Array;
})) {
return args.forEach(function (arg) {
return _this.add.apply(_this, _toConsumableArray(arg));
});
}
return this.children.push(args.shift());
} //add
}, {
key: "segmentConnect",
value: function segmentConnect() {
var _this2 = this;
var verticeArray = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.children;
var pen = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document.getElementsByTagName("canvas")[0].getContext("2d");
if (!(verticeArray instanceof Array && verticeArray.every(function (vtex) {
return vtex instanceof _this2.Vertex;
}))) {
throw "ENOVERTEX";
}
for (var i = 0; i < verticeArray.length; i++) {
if (i && verticeArray[i].coords[2] > 0) {
pen.lineTo.apply(pen, _toConsumableArray(verticeArray[i].coord2d));
} else if (!i && verticeArray[i].coords[2] >= 0) {
pen.moveTo.apply(pen, _toConsumableArray(verticeArray[i].coord2d));
} else {
try {
var chk = verticeArray[i].clip(verticeArray[i ? i - 1 : i + 1]).coord2d;
if (Number.isNaN(chk[0]) || Number.isNaN(chk[1]) || !Number.isFinite(chk[0]) || !Number.isFinite(chk[1])) break;
if (i) {
pen.lineTo.apply(pen, _toConsumableArray(chk));
} else {
pen.moveTo.apply(pen, _toConsumableArray(chk));
}
} catch (r) {
continue;
}
}
}
return this;
} //segmentConnect
}, {
key: "v0",
//parse
get: function get() {
// Point on Plane
return new D3Vertex(0, 0, this.surface[2], this);
} //g-v0
}, {
key: "n",
get: function get() {
//https://stackoverflow.com/questions/10781639/how-to-compute-normal-vector-to-least-square-plane-in-povray-only
var pts = [new this.Vertex(-this.size[0] / 2, -this.size[1] / 2, this.surface[2], this), //C
new this.Vertex(-this.size[0] / 2, this.size[1] / 2, this.surface[2], this), //B
new this.Vertex(this.size[0] / 2, -this.size[1] / 2, this.surface[2], this) //A
],
a = pts[2].sub(pts[0]),
b = pts[1].sub(pts[0]),
xvec = a.cross(b);
return xvec;
} //g-n
}, {
key: "field",
get: function get() {
return 2 * Math.atan(1 / this.surface[2]);
}
}], [{
key: "parse",
value: function parse(data, d, pen) {
var map = new D3Map(d, pen);
data = data.split('').filter(function (chunk) {
return !map.SPLITTERS.Ignore.includes(chunk);
}).join('');
var groups = data.split(map.SPLITTERS.Group).map(function (group) {
return group.split(map.SPLITTERS.Data).map(function (i, ii) {
return ii ? i * 1 : i;
});
});
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = groups[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _data = _step.value;
switch (_data.shift()) {
case map.MAPPINGS.Vertex:
map.add.apply(map, _toConsumableArray(_data));
break;
case map.MAPPINGS.Line:
map.add(new map.Line(new map.Vertex(_data.shift(), _data.shift(), _data.shift(), map), new map.Vertex(_data.shift(), _data.shift(), _data.shift(), map)));
break;
case map.MAPPINGS.Cube:
map.add(new map.Cube([new map.Vertex(_data.shift(), _data.shift(), _data.shift(), map), new map.Vertex(_data.shift(), _data.shift(), _data.shift(), map), new map.Vertex(_data.shift(), _data.shift(), _data.shift(), map), new map.Vertex(_data.shift(), _data.shift(), _data.shift(), map), new map.Vertex(_data.shift(), _data.shift(), _data.shift(), map), new map.Vertex(_data.shift(), _data.shift(), _data.shift(), map), new map.Vertex(_data.shift(), _data.shift(), _data.shift(), map), new map.Vertex(_data.shift(), _data.shift(), _data.shift(), map)]));
break;
default:
throw "EBADPARSE";
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return map;
}
}]);
return D3Map;
}(); //D3Map
var D3Renderable = function () {
function D3Renderable() {
_classCallCheck(this, D3Renderable);
this.trans = [0, 0, 0];
this.children = [];
this.middlew = function (data) {};
} //ctor
_createClass(D3Renderable, [{
key: "render",
value: function render(data) {
return this.middlew(data);
} //render @Override
}]);
return D3Renderable;
}(); //D3Renderable
var D3Vertex = function () {
function D3Vertex(x, y, z, map) {
_classCallCheck(this, D3Vertex);
if (x instanceof D3Vertex) {
var _ref = [x.x, x.y, x.z, x.map];
x = _ref[0];
y = _ref[1];
z = _ref[2];
map = _ref[3];
}
this.x = x;
this.y = y;
this.z = z;
this.map = map;
this._id = this.map.Vertex.idcnt++; //OBSOLETE
} //ctor
_createClass(D3Vertex, [{
key: "clip",
//g-coord2d
value: function clip(p0) {
//http://geomalgorithms.com/a05-_intersect-1.html
if (!(p0 instanceof D3Vertex)) {
throw "ENOVERTEX";
}
var s = this.map.v0.sub(p0).dot(this.map.n) / this.sub(p0).dot(this.map.n),
pd = this.sub(p0);
if (s < 0 || s > 1 || !this.map.n.dot(pd)) {
throw "ENOINTERSCT";
}
return p0.add(pd.mult(s));
} //clip
}, {
key: "add",
value: function add(p1) {
if (!(p1 instanceof D3Vertex)) {
throw "ENOVERTEX";
}
var p0 = new D3Vertex(this);
p0.x += p1.x;
p0.y += p1.y;
p0.z += p1.z;
return p0;
} //add
}, {
key: "sub",
value: function sub(p1) {
if (!(p1 instanceof D3Vertex)) {
throw "ENOVERTEX";
}
var p0 = new D3Vertex(this);
p0.x -= p1.x;
p0.y -= p1.y;
p0.z -= p1.z;
return p0;
} //sub
}, {
key: "dot",
value: function dot(p1) {
if (!(p1 instanceof D3Vertex)) {
throw "ENOVERTEX";
}
return this.x * p1.x + this.y * p1.y + this.z * p1.z;
} //dot
}, {
key: "mult",
value: function mult(num) {
if (typeof num === "string" && /^[0-9]+$/.test(num)) {
num = num * 1;
} else if (typeof num === "number") {
num = [num, num, num];
} else if (!(num instanceof Array)) {
throw "EINVALNUM";
}
var p0 = new D3Vertex(this);
p0.x *= num[0];
p0.y *= num[1];
p0.z *= num[2];
return p0;
} //mult
}, {
key: "cross",
value: function cross(vtex) {
if (!(p1 instanceof D3Vertex)) {
throw "ENOVERTEX";
}
var out = new D3Vertex(0, 0, 0, this.map);
out.x = this.y * vtex.z - this.z * vtex.y;
out.y = this.x * vtex.z - this.z * vtex.x;
out.z = this.x * vtex.y - this.y * vtex.x;
return out;
} //cross
}, {
key: "coords",
get: function get() {
var c1 = Math.cos(this.map.orientation[0]),
c2 = Math.cos(this.map.orientation[1]),
c3 = Math.cos(this.map.orientation[2]),
s1 = Math.sin(this.map.orientation[0]),
s2 = Math.sin(this.map.orientation[1]),
s3 = Math.sin(this.map.orientation[2]),
x = this.x - this.map.camera[0],
y = this.y - this.map.camera[1],
z = this.z - this.map.camera[2],
s3y = s3 * y,
s3x = s3 * x,
c3x = c3 * x,
c3y = c3 * y,
s2z = s2 * z,
c2z = c2 * z;
return [c2 * (s3y + c3x) - s2z, s1 * (c2z + s2 * (s3y + c3x)) + c1 * (c3y - s3x), c1 * (c2z + s2 * (s3y + c3x)) - s1 * (c3y - s3x)];
} //g-coords
}, {
key: "scoords",
get: function get() {
return new (Function.prototype.bind.apply(D3Vertex, [null].concat(_toConsumableArray(this.coords), [this.map])))();
} //g-scoords
}, {
key: "coord2d",
get: function get() {
var tmp = this.coords,
p = this.map.surface[2] / tmp[2];
return [p * tmp[0] + this.map.surface[0], p * tmp[1] + this.map.surface[1]];
}
}]);
return D3Vertex;
}(); //D3Vertex
var D3Line = function (_D3Renderable) {
_inherits(D3Line, _D3Renderable);
function D3Line(a, b) {
_classCallCheck(this, D3Line);
var _this3 = _possibleConstructorReturn(this, (D3Line.__proto__ || Object.getPrototypeOf(D3Line)).call(this));
_this3.children = [a, b];
_this3.middlew = function stroke(pen) {
return pen.stroke();
};
return _this3;
} //ctor
_createClass(D3Line, [{
key: "render",
//make
value: function render() {
var _this4 = this;
var map = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new D3Map();
var pen = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document.getElementsByTagName("canvas")[0].getContext("2d");
pen.save();
var pts = this.children.map(function (pt) {
return new map.Vertex(pt.x + _this4.trans[0], pt.y + _this4.trans[1], pt.z + _this4.trans[2], map);
});
pen.beginPath();
map.segmentConnect(pts, pen);
this.middlew(pen);
pen.closePath();
pen.restore();
} //render
}], [{
key: "make",
value: function make(a, b, map) {
return new D3Line(new (Function.prototype.bind.apply(D3Vertex, [null].concat(_toConsumableArray(a), [map])))(), new (Function.prototype.bind.apply(D3Vertex, [null].concat(_toConsumableArray(b), [map])))());
}
}]);
return D3Line;
}(D3Renderable); //D3Line
var D3Cube = function (_D3Renderable2) {
_inherits(D3Cube, _D3Renderable2);
function D3Cube() {
var points8 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var matrices6_4 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : D3Cube.matrix;
_classCallCheck(this, D3Cube);
var _this5 = _possibleConstructorReturn(this, (D3Cube.__proto__ || Object.getPrototypeOf(D3Cube)).call(this));
D3inherit(_this5.children, points8);
_this5.matrix = [];
D3inherit(_this5.matrix, matrices6_4);
return _this5;
} //ctor
_createClass(D3Cube, [{
key: "render",
value: function render() {
var map = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new D3Map();
var _this6 = this;
var pen = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document.getElementsByTagName("canvas")[0].getContext("2d");
var mode = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : map.RENDER_MODES.BOTH;
pen.save();
var pts = this.children.map(function (pt) {
return new map.Vertex(pt.x + _this6.trans[0], pt.y + _this6.trans[1], pt.z + _this6.trans[2], map);
});
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = this.matrix[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var i = _step2.value;
pen.beginPath();
map.segmentConnect(i.map(function (idx) {
return pts[idx];
}), pen);
this.middlew();
pen.closePath();
if ((mode & map.RENDER_MODES.STROKE) == map.RENDER_MODES.STROKE) {
pen.stroke();
}
if ((mode & map.RENDER_MODES.FILL) == map.RENDER_MODES.FILL) {
pen.fill();
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
pen.restore();
return this;
} //render
}]);
return D3Cube;
}(D3Renderable); //D3Cube
D3Cube.matrix = [[0, 1, 2, 3], //FRONT
[4, 5, 6, 7], //BACK
[0, 1, 5, 4], //UP
[3, 2, 6, 7], //DOWN
[0, 4, 7, 3], //LEFT
[1, 5, 6, 2] //RIGHT
];
D3Vertex.idcnt = 0; // OBS
D3Map.Vertex = D3Vertex;
D3Map.Cube = D3Cube;
D3Map.Line = D3Line;
D3Map.RENDER_MODES = {
FILL: 1,
STROKE: 2,
BOTH: 3
};
D3Map.MAPPINGS = {
Cube: 'c',
Line: 'l',
Vertex: 'p'
};
D3Map.SPLITTERS = {
Group: '|',
Data: ',',
Ignore: [' ', '\n', '\t', '(', ')', '[', ']', '{', '}', '<', '>']
};
/**
* p,x,y,z|l,x1,y1,z1,x2,y2,z2
*/
/*
https://en.m.wikipedia.org/wiki/D3_projection
Orthographic: coords.x coords.y
Perspective: coord2d.x coord2d.y
*/