Skip to content

Commit

Permalink
feat: transpose3D (#46)
Browse files Browse the repository at this point in the history
* feat: transpose3D

* refactor: remove redundant code

* chore: 1. remove pnpm lock file; 2. upgrade package version to 0.4.7
  • Loading branch information
VirusPC committed Nov 23, 2023
1 parent 862443a commit a02e131
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
30 changes: 30 additions & 0 deletions __tests__/unit/transforms/transpose3D.spec.ts
@@ -0,0 +1,30 @@
import { Coordinate3D, Vector3 } from '../../../src';

describe('Transpose3D', () => {
test('transpose() exchanges dimensions', () => {
const coord = new Coordinate3D();
coord.transform('transpose3D');
const constZ = 0.3;
expect(coord.map([0, 1, constZ])).toEqual([1, 0, constZ]);
expect(coord.map([0.2, 0.8, constZ])).toEqual([0.8, 0.2, constZ]);
expect(coord.invert([1, 0, constZ])).toEqual([0, 1, constZ]);
expect(coord.map([0.8, 0.2, constZ])).toEqual([0.2, 0.8, constZ]);
});

test('cartesian3D transpose', () => {
const coord = new Coordinate3D({
transformations: [['cartesian3D'], ['transpose3D']],
width: 300,
height: 150,
depth: 100,
});

const v1: Vector3 = [0, 0, 0];
const v2: Vector3 = [0, 0, 0];

expect(coord.map(v1)).toEqual(v2);
expect(coord.map([1, 1, 1])).toEqual([150, 300, 100]);
expect(coord.invert([0, 0, 0])).toEqual([0, 0, 0]);
expect(coord.invert([150, 300, 100])).toEqual([1, 1, 1]);
});
});
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@antv/coord",
"version": "0.4.6",
"version": "0.4.7",
"description": "Toolkit for mapping elements of sets into geometric objects.",
"license": "MIT",
"main": "lib/index.js",
Expand Down
3 changes: 2 additions & 1 deletion src/coordinate3D.ts
Expand Up @@ -2,7 +2,7 @@ import { deepMix, identity } from '@antv/util';
import { mat4, vec4 } from 'gl-matrix';
import { Vector3, Vector, Transform3D, Options3D, Transformation3D, Transformer3D, Matrix4, Vector4 } from './type';
import { compose, isMatrix, extend3D } from './utils';
import { cartesian3D, translate3D, scale3D } from './transforms';
import { cartesian3D, translate3D, scale3D, transpose3D } from './transforms';

export class Coordinate3D {
// 所有变换合成后的函数
Expand All @@ -27,6 +27,7 @@ export class Coordinate3D {
cartesian3D,
translate3D,
scale3D,
transpose3D,
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/transforms/index.ts
Expand Up @@ -14,4 +14,5 @@ export { fisheye, fisheyeX, fisheyeY, fisheyeCircular } from './fisheye';

export { cartesian3D } from './cartesian3D';
export { translate3D } from './translate3D';
export { transpose3D } from './transpose3D';
export { scale3D } from './scale3D';
18 changes: 18 additions & 0 deletions src/transforms/transpose3D.ts
@@ -0,0 +1,18 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { CreateTransformer3D, Vector3 } from '../type';

/**
* Exchange dimensions of the vector.
* @param params [tx, ty]
* @param x x of the the bounding box of coordinate
* @param y y of the the bounding box of coordinate
* @param width width of the the bounding box of coordinate
* @param height height of the the bounding box of coordinate
* @returns transformer
*/
export const transpose3D: CreateTransformer3D = (params, x, y, z, width, height, depth) => {
return {
transform: ([x, y, z]: Vector3) => [y, x, z],
untransform: ([x, y, z]: Vector3) => [y, x, z],
};
};
3 changes: 2 additions & 1 deletion src/type.ts
Expand Up @@ -32,6 +32,7 @@ type FisheyeCircular = ['fisheye.circular', number, number, number, number, bool

type Cartesian3D = ['cartesian3D'];
type Translate3D = ['translate3D', number, number, number];
type Transpose3D = ['transpose3D'];
type Scale3D = ['scale3D', number, number, number];

export type Transformation =
Expand All @@ -54,7 +55,7 @@ export type Transformation =
| FisheyeX
| FisheyeY
| FisheyeCircular;
export type Transformation3D = Cartesian3D | Translate3D | Scale3D;
export type Transformation3D = Cartesian3D | Translate3D | Scale3D | Transpose3D;

export type Options = {
x?: number;
Expand Down

0 comments on commit a02e131

Please sign in to comment.