UNPKG

8.79 kBJavaScriptView Raw
1"use strict";
2var __read = (this && this.__read) || function (o, n) {
3 var m = typeof Symbol === "function" && o[Symbol.iterator];
4 if (!m) return o;
5 var i = m.call(o), r, ar = [], e;
6 try {
7 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8 }
9 catch (error) { e = { error: error }; }
10 finally {
11 try {
12 if (r && !r.done && (m = i["return"])) m.call(i);
13 }
14 finally { if (e) throw e.error; }
15 }
16 return ar;
17};
18var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
19 if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
20 if (ar || !(i in from)) {
21 if (!ar) ar = Array.prototype.slice.call(from, 0, i);
22 ar[i] = from[i];
23 }
24 }
25 return to.concat(ar || Array.prototype.slice.call(from));
26};
27var __values = (this && this.__values) || function(o) {
28 var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
29 if (m) return m.call(o);
30 if (o && typeof o.length === "number") return {
31 next: function () {
32 if (o && i >= o.length) o = void 0;
33 return { value: o && o[i++], done: !o };
34 }
35 };
36 throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
37};
38Object.defineProperty(exports, "__esModule", { value: true });
39exports.Coordinate3D = void 0;
40var util_1 = require("@antv/util");
41var gl_matrix_1 = require("gl-matrix");
42var utils_1 = require("./utils");
43var transforms_1 = require("./transforms");
44var Coordinate3D = /** @class */ (function () {
45 /**
46 * Create a new Coordinate Object.
47 * @param options Custom options
48 */
49 function Coordinate3D(options) {
50 // 当前的选项
51 this.options = {
52 x: 0,
53 y: 0,
54 z: 0,
55 width: 300,
56 height: 150,
57 depth: 150,
58 transformations: [],
59 };
60 // 当前可以使用的变换
61 this.transformers = {
62 cartesian3D: transforms_1.cartesian3D,
63 translate3D: transforms_1.translate3D,
64 scale3D: transforms_1.scale3D,
65 transpose3D: transforms_1.transpose3D,
66 };
67 this.update(options);
68 }
69 /**
70 * Update options and inner state.
71 * @param options Options to be updated
72 */
73 Coordinate3D.prototype.update = function (options) {
74 this.options = (0, util_1.deepMix)({}, this.options, options);
75 this.recoordinate();
76 };
77 /**
78 * Returns a new Coordinate with same options.
79 * @returns Coordinate
80 */
81 Coordinate3D.prototype.clone = function () {
82 return new Coordinate3D(this.options);
83 };
84 /**
85 * Returns current options.
86 * @returns options
87 */
88 Coordinate3D.prototype.getOptions = function () {
89 return this.options;
90 };
91 /**
92 * Clear transformations and update.
93 */
94 Coordinate3D.prototype.clear = function () {
95 this.update({
96 transformations: [],
97 });
98 };
99 /**
100 * Returns the size of the bounding box of the coordinate.
101 * @returns [width, height, depth]
102 */
103 Coordinate3D.prototype.getSize = function () {
104 var _a = this.options, width = _a.width, height = _a.height, depth = _a.depth;
105 return [width, height, depth];
106 };
107 /**
108 * Returns the center of the bounding box of the coordinate.
109 * @returns [centerX, centerY, centerZ]
110 */
111 Coordinate3D.prototype.getCenter = function () {
112 var _a = this.options, x = _a.x, y = _a.y, z = _a.z, width = _a.width, height = _a.height, depth = _a.depth;
113 return [(x * 2 + width) / 2, (y * 2 + height) / 2, (z * 2 + depth) / 2];
114 };
115 /**
116 * Add selected transformation.
117 * @param args transform type and params
118 * @returns Coordinate
119 */
120 Coordinate3D.prototype.transform = function () {
121 var args = [];
122 for (var _i = 0; _i < arguments.length; _i++) {
123 args[_i] = arguments[_i];
124 }
125 var transformations = this.options.transformations;
126 this.update({
127 transformations: __spreadArray(__spreadArray([], __read(transformations), false), [__spreadArray([], __read(args), false)], false),
128 });
129 return this;
130 };
131 /**
132 * Apples transformations for the current vector.
133 * @param vector original vector3
134 * @returns transformed vector3
135 */
136 Coordinate3D.prototype.map = function (vector) {
137 return this.output(vector);
138 };
139 /**
140 * Apples invert transformations for the current vector.
141 * @param vector transformed vector3
142 * @param vector original vector3
143 */
144 Coordinate3D.prototype.invert = function (vector) {
145 return this.input(vector);
146 };
147 Coordinate3D.prototype.recoordinate = function () {
148 this.output = this.compose();
149 this.input = this.compose(true);
150 };
151 // 将所有的变换合成一个函数
152 // 变换有两种类型:矩阵变换和函数变换
153 // 处理过程中需要把连续的矩阵变换合成一个变换函数,然后在和其他变换函数合成最终的变换函数
154 Coordinate3D.prototype.compose = function (invert) {
155 var e_1, _a;
156 if (invert === void 0) { invert = false; }
157 var transformations = invert ? __spreadArray([], __read(this.options.transformations), false).reverse() : this.options.transformations;
158 var getter = invert ? function (d) { return d.untransform; } : function (d) { return d.transform; };
159 var matrixes = [];
160 var transforms = [];
161 var add = function (transform, extended) {
162 if (extended === void 0) { extended = true; }
163 return transforms.push(extended ? (0, utils_1.extend3D)(transform) : transform);
164 };
165 try {
166 for (var transformations_1 = __values(transformations), transformations_1_1 = transformations_1.next(); !transformations_1_1.done; transformations_1_1 = transformations_1.next()) {
167 var _b = __read(transformations_1_1.value), name_1 = _b[0], args = _b.slice(1);
168 var createTransformer = this.transformers[name_1];
169 if (createTransformer) {
170 var _c = this.options, x = _c.x, y = _c.y, z = _c.z, width = _c.width, height = _c.height, depth = _c.depth;
171 var transformer = createTransformer(__spreadArray([], __read(args), false), x, y, z, width, height, depth);
172 if ((0, utils_1.isMatrix)(transformer)) {
173 // 如果当前变换是矩阵变换,那么先保存下来
174 matrixes.push(transformer);
175 }
176 else {
177 // 如果当前变换是函数变换,并且之前有没有合成的矩阵变换,那么现将之前的矩阵变换合成
178 if (matrixes.length) {
179 var transform_1 = this.createMatrixTransform(matrixes, invert);
180 add(transform_1);
181 matrixes.splice(0, matrixes.length);
182 }
183 var transform = getter(transformer) || util_1.identity;
184 add(transform, true);
185 }
186 }
187 }
188 }
189 catch (e_1_1) { e_1 = { error: e_1_1 }; }
190 finally {
191 try {
192 if (transformations_1_1 && !transformations_1_1.done && (_a = transformations_1.return)) _a.call(transformations_1);
193 }
194 finally { if (e_1) throw e_1.error; }
195 }
196 // 合成剩下的矩阵变换
197 if (matrixes.length) {
198 var transform = this.createMatrixTransform(matrixes, invert);
199 add(transform);
200 }
201 return utils_1.compose.apply(void 0, __spreadArray([], __read(transforms), false));
202 };
203 // 将连续的矩阵的运算合成一个变换函数
204 Coordinate3D.prototype.createMatrixTransform = function (matrixes, invert) {
205 var matrix = gl_matrix_1.mat4.create();
206 if (invert)
207 matrixes.reverse();
208 matrixes.forEach(function (m) { return gl_matrix_1.mat4.mul(matrix, matrix, m); });
209 if (invert) {
210 gl_matrix_1.mat4.invert(matrix, gl_matrix_1.mat4.clone(matrix));
211 }
212 return function (vector) {
213 var vector4 = [vector[0], vector[1], vector[2], 1];
214 gl_matrix_1.vec4.transformMat4(vector4, vector4, matrix);
215 return [vector4[0], vector4[1], vector4[2]];
216 };
217 };
218 return Coordinate3D;
219}());
220exports.Coordinate3D = Coordinate3D;
221//# sourceMappingURL=coordinate3D.js.map
\No newline at end of file