@antv/coord 0.4.3 → 0.4.5
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/dist/coordinate.min.js +1 -1
- package/esm/coordinate.d.ts +1 -1
- package/esm/coordinate.js +60 -23
- package/esm/coordinate.js.map +1 -1
- package/esm/coordinate3D.d.ts +64 -0
- package/esm/coordinate3D.js +217 -0
- package/esm/coordinate3D.js.map +1 -0
- package/esm/index.d.ts +2 -1
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -1
- package/esm/transforms/cartesian.js +18 -3
- package/esm/transforms/cartesian.js.map +1 -1
- package/esm/transforms/cartesian3D.d.ts +2 -0
- package/esm/transforms/cartesian3D.js +39 -0
- package/esm/transforms/cartesian3D.js.map +1 -0
- package/esm/transforms/custom.js +17 -1
- package/esm/transforms/custom.js.map +1 -1
- package/esm/transforms/fisheye.d.ts +1 -1
- package/esm/transforms/fisheye.js +77 -27
- package/esm/transforms/fisheye.js.map +1 -1
- package/esm/transforms/helix.js +19 -3
- package/esm/transforms/helix.js.map +1 -1
- package/esm/transforms/index.d.ts +3 -0
- package/esm/transforms/index.js +3 -0
- package/esm/transforms/index.js.map +1 -1
- package/esm/transforms/matrix.js +17 -1
- package/esm/transforms/matrix.js.map +1 -1
- package/esm/transforms/parallel.js +17 -1
- package/esm/transforms/parallel.js.map +1 -1
- package/esm/transforms/polar.js +19 -3
- package/esm/transforms/polar.js.map +1 -1
- package/esm/transforms/reflect.js +19 -3
- package/esm/transforms/reflect.js.map +1 -1
- package/esm/transforms/rotate.js +18 -3
- package/esm/transforms/rotate.js.map +1 -1
- package/esm/transforms/scale.js +18 -3
- package/esm/transforms/scale.js.map +1 -1
- package/esm/transforms/scale3D.d.ts +13 -0
- package/esm/transforms/scale3D.js +33 -0
- package/esm/transforms/scale3D.js.map +1 -0
- package/esm/transforms/shear.js +22 -6
- package/esm/transforms/shear.js.map +1 -1
- package/esm/transforms/translate.js +18 -3
- package/esm/transforms/translate.js.map +1 -1
- package/esm/transforms/translate3D.d.ts +13 -0
- package/esm/transforms/translate3D.js +33 -0
- package/esm/transforms/translate3D.js.map +1 -0
- package/esm/transforms/transpose.js +18 -2
- package/esm/transforms/transpose.js.map +1 -1
- package/esm/type.d.ts +28 -6
- package/esm/utils/extend.d.ts +2 -1
- package/esm/utils/extend.js +37 -1
- package/esm/utils/extend.js.map +1 -1
- package/esm/utils/index.d.ts +1 -1
- package/esm/utils/index.js +1 -1
- package/esm/utils/index.js.map +1 -1
- package/lib/coordinate.d.ts +1 -1
- package/lib/coordinate.js +64 -27
- package/lib/coordinate.js.map +1 -1
- package/lib/coordinate3D.d.ts +64 -0
- package/lib/coordinate3D.js +220 -0
- package/lib/coordinate3D.js.map +1 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.js +3 -1
- package/lib/index.js.map +1 -1
- package/lib/transforms/cartesian.js +18 -3
- package/lib/transforms/cartesian.js.map +1 -1
- package/lib/transforms/cartesian3D.d.ts +2 -0
- package/lib/transforms/cartesian3D.js +43 -0
- package/lib/transforms/cartesian3D.js.map +1 -0
- package/lib/transforms/custom.js +17 -1
- package/lib/transforms/custom.js.map +1 -1
- package/lib/transforms/fisheye.d.ts +1 -1
- package/lib/transforms/fisheye.js +77 -27
- package/lib/transforms/fisheye.js.map +1 -1
- package/lib/transforms/helix.js +19 -3
- package/lib/transforms/helix.js.map +1 -1
- package/lib/transforms/index.d.ts +3 -0
- package/lib/transforms/index.js +7 -1
- package/lib/transforms/index.js.map +1 -1
- package/lib/transforms/matrix.js +17 -1
- package/lib/transforms/matrix.js.map +1 -1
- package/lib/transforms/parallel.js +17 -1
- package/lib/transforms/parallel.js.map +1 -1
- package/lib/transforms/polar.js +19 -3
- package/lib/transforms/polar.js.map +1 -1
- package/lib/transforms/reflect.js +19 -3
- package/lib/transforms/reflect.js.map +1 -1
- package/lib/transforms/rotate.js +20 -5
- package/lib/transforms/rotate.js.map +1 -1
- package/lib/transforms/scale.js +20 -5
- package/lib/transforms/scale.js.map +1 -1
- package/lib/transforms/scale3D.d.ts +13 -0
- package/lib/transforms/scale3D.js +37 -0
- package/lib/transforms/scale3D.js.map +1 -0
- package/lib/transforms/shear.js +22 -6
- package/lib/transforms/shear.js.map +1 -1
- package/lib/transforms/translate.js +20 -5
- package/lib/transforms/translate.js.map +1 -1
- package/lib/transforms/translate3D.d.ts +13 -0
- package/lib/transforms/translate3D.js +37 -0
- package/lib/transforms/translate3D.js.map +1 -0
- package/lib/transforms/transpose.js +18 -2
- package/lib/transforms/transpose.js.map +1 -1
- package/lib/type.d.ts +28 -6
- package/lib/utils/extend.d.ts +2 -1
- package/lib/utils/extend.js +39 -2
- package/lib/utils/extend.js.map +1 -1
- package/lib/utils/index.d.ts +1 -1
- package/lib/utils/index.js +2 -1
- package/lib/utils/index.js.map +1 -1
- package/package.json +7 -6
- package/src/coordinate.ts +2 -2
- package/src/coordinate3D.ts +185 -0
- package/src/index.ts +2 -1
- package/src/transforms/cartesian.ts +1 -2
- package/src/transforms/cartesian3D.ts +25 -0
- package/src/transforms/fisheye.ts +66 -19
- package/src/transforms/index.ts +4 -0
- package/src/transforms/rotate.ts +1 -2
- package/src/transforms/scale.ts +1 -2
- package/src/transforms/scale3D.ts +18 -0
- package/src/transforms/translate.ts +1 -2
- package/src/transforms/translate3D.ts +18 -0
- package/src/type.ts +44 -7
- package/src/utils/extend.ts +13 -1
- package/src/utils/index.ts +1 -1
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { deepMix, identity } from '@antv/util';
|
|
2
|
+
import { mat4, vec4 } from 'gl-matrix';
|
|
3
|
+
import { Vector3, Vector, Transform3D, Options3D, Transformation3D, Transformer3D, Matrix4, Vector4 } from './type';
|
|
4
|
+
import { compose, isMatrix, extend3D } from './utils';
|
|
5
|
+
import { cartesian3D, translate3D, scale3D } from './transforms';
|
|
6
|
+
|
|
7
|
+
export class Coordinate3D {
|
|
8
|
+
// 所有变换合成后的函数
|
|
9
|
+
private output: Transform3D;
|
|
10
|
+
|
|
11
|
+
// 所有变换合成后的逆函数
|
|
12
|
+
private input: Transform3D;
|
|
13
|
+
|
|
14
|
+
// 当前的选项
|
|
15
|
+
private options: Options3D = {
|
|
16
|
+
x: 0,
|
|
17
|
+
y: 0,
|
|
18
|
+
z: 0,
|
|
19
|
+
width: 300,
|
|
20
|
+
height: 150,
|
|
21
|
+
depth: 150,
|
|
22
|
+
transformations: [],
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// 当前可以使用的变换
|
|
26
|
+
private transformers = {
|
|
27
|
+
cartesian3D,
|
|
28
|
+
translate3D,
|
|
29
|
+
scale3D,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Create a new Coordinate Object.
|
|
34
|
+
* @param options Custom options
|
|
35
|
+
*/
|
|
36
|
+
constructor(options?: Partial<Options3D>) {
|
|
37
|
+
this.update(options);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Update options and inner state.
|
|
42
|
+
* @param options Options to be updated
|
|
43
|
+
*/
|
|
44
|
+
public update(options: Partial<Options3D>) {
|
|
45
|
+
this.options = deepMix({}, this.options, options);
|
|
46
|
+
this.recoordinate();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Returns a new Coordinate with same options.
|
|
51
|
+
* @returns Coordinate
|
|
52
|
+
*/
|
|
53
|
+
public clone() {
|
|
54
|
+
return new Coordinate3D(this.options);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Returns current options.
|
|
59
|
+
* @returns options
|
|
60
|
+
*/
|
|
61
|
+
public getOptions() {
|
|
62
|
+
return this.options;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Clear transformations and update.
|
|
67
|
+
*/
|
|
68
|
+
public clear() {
|
|
69
|
+
this.update({
|
|
70
|
+
transformations: [],
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Returns the size of the bounding box of the coordinate.
|
|
76
|
+
* @returns [width, height, depth]
|
|
77
|
+
*/
|
|
78
|
+
public getSize(): [number, number, number] {
|
|
79
|
+
const { width, height, depth } = this.options;
|
|
80
|
+
return [width, height, depth];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Returns the center of the bounding box of the coordinate.
|
|
85
|
+
* @returns [centerX, centerY, centerZ]
|
|
86
|
+
*/
|
|
87
|
+
public getCenter(): [number, number, number] {
|
|
88
|
+
const { x, y, z, width, height, depth } = this.options;
|
|
89
|
+
return [(x * 2 + width) / 2, (y * 2 + height) / 2, (z * 2 + depth) / 2];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Add selected transformation.
|
|
94
|
+
* @param args transform type and params
|
|
95
|
+
* @returns Coordinate
|
|
96
|
+
*/
|
|
97
|
+
public transform(...args: Transformation3D) {
|
|
98
|
+
const { transformations } = this.options;
|
|
99
|
+
this.update({
|
|
100
|
+
transformations: [...transformations, [...args]],
|
|
101
|
+
});
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Apples transformations for the current vector.
|
|
107
|
+
* @param vector original vector3
|
|
108
|
+
* @returns transformed vector3
|
|
109
|
+
*/
|
|
110
|
+
public map(vector: Vector3 | Vector) {
|
|
111
|
+
return this.output(vector);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Apples invert transformations for the current vector.
|
|
116
|
+
* @param vector transformed vector3
|
|
117
|
+
* @param vector original vector3
|
|
118
|
+
*/
|
|
119
|
+
public invert(vector: Vector3 | Vector) {
|
|
120
|
+
return this.input(vector);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private recoordinate() {
|
|
124
|
+
this.output = this.compose();
|
|
125
|
+
this.input = this.compose(true);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// 将所有的变换合成一个函数
|
|
129
|
+
// 变换有两种类型:矩阵变换和函数变换
|
|
130
|
+
// 处理过程中需要把连续的矩阵变换合成一个变换函数,然后在和其他变换函数合成最终的变换函数
|
|
131
|
+
private compose(invert = false) {
|
|
132
|
+
const transformations = invert ? [...this.options.transformations].reverse() : this.options.transformations;
|
|
133
|
+
const getter = invert ? (d: Transformer3D) => d.untransform : (d: Transformer3D) => d.transform;
|
|
134
|
+
const matrixes = [];
|
|
135
|
+
const transforms = [];
|
|
136
|
+
const add = (transform: Transform3D, extended = true) =>
|
|
137
|
+
transforms.push(extended ? extend3D(transform) : transform);
|
|
138
|
+
|
|
139
|
+
for (const [name, ...args] of transformations) {
|
|
140
|
+
const createTransformer = this.transformers[name];
|
|
141
|
+
if (createTransformer) {
|
|
142
|
+
const { x, y, z, width, height, depth } = this.options;
|
|
143
|
+
const transformer = createTransformer([...args], x, y, z, width, height, depth);
|
|
144
|
+
|
|
145
|
+
if (isMatrix(transformer)) {
|
|
146
|
+
// 如果当前变换是矩阵变换,那么先保存下来
|
|
147
|
+
matrixes.push(transformer);
|
|
148
|
+
} else {
|
|
149
|
+
// 如果当前变换是函数变换,并且之前有没有合成的矩阵变换,那么现将之前的矩阵变换合成
|
|
150
|
+
if (matrixes.length) {
|
|
151
|
+
const transform = this.createMatrixTransform(matrixes, invert);
|
|
152
|
+
add(transform);
|
|
153
|
+
matrixes.splice(0, matrixes.length);
|
|
154
|
+
}
|
|
155
|
+
const transform = getter(transformer as Transformer3D) || identity;
|
|
156
|
+
add(transform, true);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// 合成剩下的矩阵变换
|
|
162
|
+
if (matrixes.length) {
|
|
163
|
+
const transform = this.createMatrixTransform(matrixes, invert);
|
|
164
|
+
add(transform);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return compose<Vector3 | Vector>(...transforms);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// 将连续的矩阵的运算合成一个变换函数
|
|
171
|
+
private createMatrixTransform(matrixes: Matrix4[], invert: boolean): Transform3D {
|
|
172
|
+
const matrix = mat4.create();
|
|
173
|
+
if (invert) matrixes.reverse();
|
|
174
|
+
matrixes.forEach((m) => mat4.mul(matrix, matrix, m));
|
|
175
|
+
if (invert) {
|
|
176
|
+
mat4.invert(matrix, mat4.clone(matrix));
|
|
177
|
+
}
|
|
178
|
+
return (vector: Vector3): Vector3 => {
|
|
179
|
+
const vector4: Vector4 = [vector[0], vector[1], vector[2], 1];
|
|
180
|
+
|
|
181
|
+
vec4.transformMat4(vector4, vector4, matrix);
|
|
182
|
+
return [vector4[0], vector4[1], vector4[2]];
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
1
|
import { Linear } from '@antv/scale';
|
|
3
|
-
import {
|
|
2
|
+
import { CreateTransformer, Vector2 } from '../type';
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
5
|
* Maps normalized value to the bounding box of coordinate.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Linear } from '@antv/scale';
|
|
2
|
+
import { Vector3, CreateTransformer3D } from '../type';
|
|
3
|
+
|
|
4
|
+
export const cartesian3D: CreateTransformer3D = (params, x, y, z, width, height, depth) => {
|
|
5
|
+
const sx = new Linear({
|
|
6
|
+
range: [x, x + width],
|
|
7
|
+
});
|
|
8
|
+
const sy = new Linear({
|
|
9
|
+
range: [y, y + height],
|
|
10
|
+
});
|
|
11
|
+
const sz = new Linear({
|
|
12
|
+
range: [z, z + depth],
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
transform(vector: Vector3) {
|
|
17
|
+
const [v1, v2, v3] = vector;
|
|
18
|
+
return [sx.map(v1), sy.map(v2), sz.map(v3)];
|
|
19
|
+
},
|
|
20
|
+
untransform(vector: Vector3) {
|
|
21
|
+
const [v1, v2, v3] = vector;
|
|
22
|
+
return [sx.invert(v1), sy.invert(v2), sz.invert(v3)];
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
2
|
// https://github.com/d3/d3-plugins/blob/master/fisheye/fisheye.js
|
|
3
|
+
import { Linear } from '@antv/scale';
|
|
3
4
|
import { CreateTransformer, Vector2 } from '../type';
|
|
4
5
|
|
|
5
6
|
function fisheyeTransform(x: number, focus: number, distortion: number, min: number, max: number) {
|
|
@@ -16,6 +17,19 @@ function fisheyeUntransform(tx: number, focus: number, distortion: number, min:
|
|
|
16
17
|
return m / ((m * (distortion + 1)) / (tx - focus) - distortion * f) + focus;
|
|
17
18
|
}
|
|
18
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Map actual visual point to abstract focus point(0, 1)
|
|
22
|
+
*/
|
|
23
|
+
function normalize(focus: number, length: number, isVisual: boolean): number {
|
|
24
|
+
if (!isVisual) return focus;
|
|
25
|
+
|
|
26
|
+
const s = new Linear({
|
|
27
|
+
range: [0, 1],
|
|
28
|
+
domain: [0, length],
|
|
29
|
+
});
|
|
30
|
+
return s.map(focus);
|
|
31
|
+
}
|
|
32
|
+
|
|
19
33
|
/**
|
|
20
34
|
* Applies cartesian fisheye transforms for the first dimension of vector2.
|
|
21
35
|
* @param params [focus, distortion]
|
|
@@ -26,16 +40,17 @@ function fisheyeUntransform(tx: number, focus: number, distortion: number, min:
|
|
|
26
40
|
* @returns transformer
|
|
27
41
|
*/
|
|
28
42
|
export const fisheyeX: CreateTransformer = (params, x, y, width, height) => {
|
|
29
|
-
const [focus, distortion] = params as number
|
|
43
|
+
const [focus, distortion, isVisual = false] = params as [number, number, boolean];
|
|
44
|
+
const normalizedFocusX = normalize(focus, width, isVisual);
|
|
30
45
|
return {
|
|
31
46
|
transform(vector: Vector2) {
|
|
32
47
|
const [vx, vy] = vector;
|
|
33
|
-
const fx = fisheyeTransform(vx,
|
|
48
|
+
const fx = fisheyeTransform(vx, normalizedFocusX, distortion, 0, 1);
|
|
34
49
|
return [fx, vy];
|
|
35
50
|
},
|
|
36
51
|
untransform(vector: Vector2) {
|
|
37
52
|
const [fx, vy] = vector;
|
|
38
|
-
const vx = fisheyeUntransform(fx,
|
|
53
|
+
const vx = fisheyeUntransform(fx, normalizedFocusX, distortion, 0, 1);
|
|
39
54
|
return [vx, vy];
|
|
40
55
|
},
|
|
41
56
|
};
|
|
@@ -51,16 +66,18 @@ export const fisheyeX: CreateTransformer = (params, x, y, width, height) => {
|
|
|
51
66
|
* @returns transformer
|
|
52
67
|
*/
|
|
53
68
|
export const fisheyeY: CreateTransformer = (params, x, y, width, height) => {
|
|
54
|
-
const [focus, distortion] = params as number
|
|
69
|
+
const [focus, distortion, isVisual = false] = params as [number, number, boolean];
|
|
70
|
+
const normalizedFocusY = normalize(focus, height, isVisual);
|
|
71
|
+
|
|
55
72
|
return {
|
|
56
73
|
transform(vector: Vector2) {
|
|
57
74
|
const [vx, vy] = vector;
|
|
58
|
-
const fy = fisheyeTransform(vy,
|
|
75
|
+
const fy = fisheyeTransform(vy, normalizedFocusY, distortion, 0, 1);
|
|
59
76
|
return [vx, fy];
|
|
60
77
|
},
|
|
61
78
|
untransform(vector: Vector2) {
|
|
62
79
|
const [vx, fy] = vector;
|
|
63
|
-
const vy = fisheyeUntransform(fy,
|
|
80
|
+
const vy = fisheyeUntransform(fy, normalizedFocusY, distortion, 0, 1);
|
|
64
81
|
return [vx, vy];
|
|
65
82
|
},
|
|
66
83
|
};
|
|
@@ -76,18 +93,27 @@ export const fisheyeY: CreateTransformer = (params, x, y, width, height) => {
|
|
|
76
93
|
* @returns transformer
|
|
77
94
|
*/
|
|
78
95
|
export const fisheye: CreateTransformer = (params, x, y, width, height) => {
|
|
79
|
-
const [focusX, focusY, distortionX, distortionY] = params as
|
|
96
|
+
const [focusX, focusY, distortionX, distortionY, isVisual = false] = params as [
|
|
97
|
+
number,
|
|
98
|
+
number,
|
|
99
|
+
number,
|
|
100
|
+
number,
|
|
101
|
+
boolean,
|
|
102
|
+
];
|
|
103
|
+
const normalizedFocusX = normalize(focusX, width, isVisual);
|
|
104
|
+
const normalizedFocusY = normalize(focusY, height, isVisual);
|
|
105
|
+
|
|
80
106
|
return {
|
|
81
107
|
transform(vector: Vector2) {
|
|
82
108
|
const [vx, vy] = vector;
|
|
83
|
-
const fx = fisheyeTransform(vx,
|
|
84
|
-
const fy = fisheyeTransform(vy,
|
|
109
|
+
const fx = fisheyeTransform(vx, normalizedFocusX, distortionX, 0, 1);
|
|
110
|
+
const fy = fisheyeTransform(vy, normalizedFocusY, distortionY, 0, 1);
|
|
85
111
|
return [fx, fy];
|
|
86
112
|
},
|
|
87
113
|
untransform(vector: Vector2) {
|
|
88
114
|
const [fx, fy] = vector;
|
|
89
|
-
const vx = fisheyeUntransform(fx,
|
|
90
|
-
const vy = fisheyeUntransform(fy,
|
|
115
|
+
const vx = fisheyeUntransform(fx, normalizedFocusX, distortionX, 0, 1);
|
|
116
|
+
const vy = fisheyeUntransform(fy, normalizedFocusY, distortionY, 0, 1);
|
|
91
117
|
return [vx, vy];
|
|
92
118
|
},
|
|
93
119
|
};
|
|
@@ -95,7 +121,7 @@ export const fisheye: CreateTransformer = (params, x, y, width, height) => {
|
|
|
95
121
|
|
|
96
122
|
/**
|
|
97
123
|
* Applies circular fisheye transforms.
|
|
98
|
-
* @param params [focusX, focusY, radius, distortion]
|
|
124
|
+
* @param params [focusX, focusY, radius, distortion, isVisual?]
|
|
99
125
|
* @param x x of the the bounding box of coordinate
|
|
100
126
|
* @param y y of the the bounding box of coordinate
|
|
101
127
|
* @param width width of the the bounding box of coordinate
|
|
@@ -103,27 +129,48 @@ export const fisheye: CreateTransformer = (params, x, y, width, height) => {
|
|
|
103
129
|
* @returns transformer
|
|
104
130
|
*/
|
|
105
131
|
export const fisheyeCircular: CreateTransformer = (params, x, y, width, height) => {
|
|
106
|
-
const [focusX, focusY, radius, distortion] = params as number
|
|
132
|
+
const [focusX, focusY, radius, distortion, isVisual = false] = params as [number, number, number, number, boolean];
|
|
133
|
+
|
|
134
|
+
const scaleX = new Linear({
|
|
135
|
+
range: [0, width],
|
|
136
|
+
});
|
|
137
|
+
const scaleY = new Linear({
|
|
138
|
+
range: [0, height],
|
|
139
|
+
});
|
|
140
|
+
// focus point => visual point
|
|
141
|
+
const nx = isVisual ? focusX : scaleX.map(focusX);
|
|
142
|
+
const ny = isVisual ? focusY : scaleY.map(focusY);
|
|
143
|
+
|
|
107
144
|
return {
|
|
108
145
|
transform(vector: Vector2) {
|
|
109
146
|
const [x, y] = vector;
|
|
110
|
-
|
|
111
|
-
const
|
|
147
|
+
// focus point => visual point
|
|
148
|
+
const dx = scaleX.map(x) - nx;
|
|
149
|
+
const dy = scaleY.map(y) - ny;
|
|
112
150
|
const dd = Math.sqrt(dx * dx + dy * dy);
|
|
151
|
+
|
|
113
152
|
if (dd > radius) return [x, y];
|
|
114
153
|
const r = fisheyeTransform(dd, 0, distortion, 0, radius);
|
|
115
154
|
const theta = Math.atan2(dy, dx);
|
|
116
|
-
|
|
155
|
+
|
|
156
|
+
const fx = nx + r * Math.cos(theta);
|
|
157
|
+
const fy = ny + r * Math.sin(theta);
|
|
158
|
+
// visual point => focus point
|
|
159
|
+
return [scaleX.invert(fx), scaleY.invert(fy)];
|
|
117
160
|
},
|
|
118
161
|
untransform(vector: Vector2) {
|
|
119
162
|
const [tx, ty] = vector;
|
|
120
|
-
const dx = tx -
|
|
121
|
-
const dy = ty -
|
|
163
|
+
const dx = scaleX.map(tx) - nx;
|
|
164
|
+
const dy = scaleY.map(ty) - ny;
|
|
122
165
|
const dd = Math.sqrt(dx * dx + dy * dy);
|
|
123
166
|
if (dd > radius) return [tx, ty];
|
|
167
|
+
|
|
124
168
|
const x = fisheyeUntransform(dd, 0, distortion, 0, radius);
|
|
125
169
|
const theta = Math.atan2(dy, dx);
|
|
126
|
-
|
|
170
|
+
|
|
171
|
+
const fx = nx + x * Math.cos(theta);
|
|
172
|
+
const fy = ny + x * Math.sin(theta);
|
|
173
|
+
return [scaleX.invert(fx), scaleY.invert(fy)];
|
|
127
174
|
},
|
|
128
175
|
};
|
|
129
176
|
};
|
package/src/transforms/index.ts
CHANGED
|
@@ -11,3 +11,7 @@ export { helix } from './helix';
|
|
|
11
11
|
export { parallel } from './parallel';
|
|
12
12
|
export { shearX, shearY } from './shear';
|
|
13
13
|
export { fisheye, fisheyeX, fisheyeY, fisheyeCircular } from './fisheye';
|
|
14
|
+
|
|
15
|
+
export { cartesian3D } from './cartesian3D';
|
|
16
|
+
export { translate3D } from './translate3D';
|
|
17
|
+
export { scale3D } from './scale3D';
|
package/src/transforms/rotate.ts
CHANGED
package/src/transforms/scale.ts
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { mat4 } from 'gl-matrix';
|
|
2
|
+
import { CreateTransformer3D } from '../type';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Apply scale transformation for current vector.
|
|
6
|
+
* @param params [sx, sy, sz]
|
|
7
|
+
* @param x x of the the bounding box of coordinate
|
|
8
|
+
* @param y y of the the bounding box of coordinate
|
|
9
|
+
* @param z z of the the bounding box of coordinate
|
|
10
|
+
* @param width width of the the bounding box of coordinate
|
|
11
|
+
* @param height height of the the bounding box of coordinate
|
|
12
|
+
* @param depth depth of the the bounding box of coordinate
|
|
13
|
+
* @returns transformer
|
|
14
|
+
*/
|
|
15
|
+
export const scale3D: CreateTransformer3D = (params, x, y, z, width, height, depth) => {
|
|
16
|
+
const [sx, sy, sz] = params as number[];
|
|
17
|
+
return mat4.fromScaling(mat4.create(), [sx, sy, sz]);
|
|
18
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { mat4 } from 'gl-matrix';
|
|
2
|
+
import { CreateTransformer3D } from '../type';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Apply translate transformation for current vector.
|
|
6
|
+
* @param params [tx, ty, tz]
|
|
7
|
+
* @param x x of the the bounding box of coordinate
|
|
8
|
+
* @param y y of the the bounding box of coordinate
|
|
9
|
+
* @param z z of the the bounding box of coordinate
|
|
10
|
+
* @param width width of the the bounding box of coordinate
|
|
11
|
+
* @param height height of the the bounding box of coordinate
|
|
12
|
+
* @param depth depth of the the bounding box of coordinate
|
|
13
|
+
* @returns transformer
|
|
14
|
+
*/
|
|
15
|
+
export const translate3D: CreateTransformer3D = (params, x, y, z, width, height, depth) => {
|
|
16
|
+
const [tx, ty, tz] = params as number[];
|
|
17
|
+
return mat4.fromTranslation(mat4.create(), [tx, ty, tz]);
|
|
18
|
+
};
|
package/src/type.ts
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
import { vec3, mat3 } from '
|
|
1
|
+
import { vec3, mat3, mat4, vec4 } from 'gl-matrix';
|
|
2
2
|
|
|
3
3
|
export type TransformCallback = (x: number, y: number, width: number, height: number) => Transformer;
|
|
4
|
+
export type Transform3DCallback = (
|
|
5
|
+
x: number,
|
|
6
|
+
y: number,
|
|
7
|
+
z: number,
|
|
8
|
+
width: number,
|
|
9
|
+
height: number,
|
|
10
|
+
depth: number,
|
|
11
|
+
) => Transformer3D;
|
|
4
12
|
|
|
5
13
|
type Translate = ['translate', number, number];
|
|
6
14
|
type Cartesian = ['cartesian'];
|
|
@@ -17,10 +25,14 @@ type ReflectY = ['reflect.y'];
|
|
|
17
25
|
type Rotate = ['rotate', number];
|
|
18
26
|
type Helix = ['helix', number, number, number, number];
|
|
19
27
|
type Parallel = ['parallel', number, number, number, number];
|
|
20
|
-
type Fisheye = ['fisheye', number, number, number, number];
|
|
21
|
-
type FisheyeX = ['fisheye.x', number, number];
|
|
22
|
-
type FisheyeY = ['fisheye.y', number, number];
|
|
23
|
-
type FisheyeCircular = ['fisheye.circular', number, number, number, number];
|
|
28
|
+
type Fisheye = ['fisheye', number, number, number, number, boolean?];
|
|
29
|
+
type FisheyeX = ['fisheye.x', number, number, boolean?];
|
|
30
|
+
type FisheyeY = ['fisheye.y', number, number, boolean?];
|
|
31
|
+
type FisheyeCircular = ['fisheye.circular', number, number, number, number, boolean?];
|
|
32
|
+
|
|
33
|
+
type Cartesian3D = ['cartesian3D'];
|
|
34
|
+
type Translate3D = ['translate3D', number, number, number];
|
|
35
|
+
type Scale3D = ['scale3D', number, number, number];
|
|
24
36
|
|
|
25
37
|
export type Transformation =
|
|
26
38
|
| Translate
|
|
@@ -42,6 +54,7 @@ export type Transformation =
|
|
|
42
54
|
| FisheyeX
|
|
43
55
|
| FisheyeY
|
|
44
56
|
| FisheyeCircular;
|
|
57
|
+
export type Transformation3D = Cartesian3D | Translate3D | Scale3D;
|
|
45
58
|
|
|
46
59
|
export type Options = {
|
|
47
60
|
x?: number;
|
|
@@ -50,26 +63,50 @@ export type Options = {
|
|
|
50
63
|
height?: number;
|
|
51
64
|
transformations?: Transformation[];
|
|
52
65
|
};
|
|
66
|
+
export type Options3D = {
|
|
67
|
+
x?: number;
|
|
68
|
+
y?: number;
|
|
69
|
+
z?: number;
|
|
70
|
+
width?: number;
|
|
71
|
+
height?: number;
|
|
72
|
+
depth?: number;
|
|
73
|
+
transformations?: Transformation3D[];
|
|
74
|
+
};
|
|
53
75
|
|
|
54
76
|
export type Vector2 = [number, number];
|
|
55
|
-
|
|
56
77
|
export type Vector3 = vec3;
|
|
78
|
+
export type Vector4 = vec4;
|
|
57
79
|
|
|
58
80
|
export type Vector = number[];
|
|
59
81
|
|
|
60
82
|
export type Matrix3 = mat3;
|
|
83
|
+
export type Matrix4 = mat4;
|
|
61
84
|
|
|
62
85
|
export type Transform = (vector: Vector2 | Vector) => Vector2 | Vector;
|
|
86
|
+
export type Transform3D = (vector: Vector3 | Vector) => Vector3 | Vector;
|
|
63
87
|
|
|
64
88
|
export type Transformer = {
|
|
65
89
|
transform?: Transform;
|
|
66
90
|
untransform?: Transform;
|
|
67
91
|
};
|
|
92
|
+
export type Transformer3D = {
|
|
93
|
+
transform?: Transform3D;
|
|
94
|
+
untransform?: Transform3D;
|
|
95
|
+
};
|
|
68
96
|
|
|
69
97
|
export type CreateTransformer = (
|
|
70
|
-
params: number[] | [TransformCallback] | [Matrix3],
|
|
98
|
+
params: number[] | (number | boolean)[] | [TransformCallback] | [Matrix3],
|
|
71
99
|
x: number,
|
|
72
100
|
y: number,
|
|
73
101
|
width: number,
|
|
74
102
|
height: number,
|
|
75
103
|
) => Transformer | Matrix3;
|
|
104
|
+
export type CreateTransformer3D = (
|
|
105
|
+
params: number[] | (number | boolean)[] | [TransformCallback] | [Matrix3],
|
|
106
|
+
x: number,
|
|
107
|
+
y: number,
|
|
108
|
+
z: number,
|
|
109
|
+
width: number,
|
|
110
|
+
height: number,
|
|
111
|
+
depth: number,
|
|
112
|
+
) => Transformer3D | Matrix4;
|
package/src/utils/extend.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Transform, Vector } from '../type';
|
|
1
|
+
import { Transform, Transform3D, Vector } from '../type';
|
|
2
2
|
|
|
3
3
|
// 对普通的变换函数进行扩展
|
|
4
4
|
// 对于长度大于2的向量,两两为一个点的 x 和 y 坐标
|
|
@@ -14,3 +14,15 @@ export function extend(transform: Transform) {
|
|
|
14
14
|
return v;
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
|
+
|
|
18
|
+
export function extend3D(transform: Transform3D) {
|
|
19
|
+
return (vector: Vector) => {
|
|
20
|
+
const v = [];
|
|
21
|
+
for (let i = 0; i < vector.length - 1; i += 3) {
|
|
22
|
+
const from = [vector[i], vector[i + 1], vector[i + 2]];
|
|
23
|
+
const to = transform(from);
|
|
24
|
+
v.push(...to);
|
|
25
|
+
}
|
|
26
|
+
return v;
|
|
27
|
+
};
|
|
28
|
+
}
|
package/src/utils/index.ts
CHANGED