UNPKG

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