Skip to content

Commit

Permalink
refactor: simpify utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarebecca committed Oct 17, 2024
1 parent 1dd60b5 commit abd828e
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 187 deletions.
35 changes: 1 addition & 34 deletions __tests__/unit/util/assert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,9 @@ import {
Circle,
isDisplayObject,
} from '../../../packages/g-lite/src/display-objects';
import {
DCHECK,
DCHECK_EQ,
DCHECK_NE,
definedProps,
isFunction,
isSymbol,
} from '../../../packages/g-lite/src/utils';
import { definedProps } from '../../../packages/g-lite/src/utils';

describe('Assert utils', () => {
it('should check isFunction correctly', () => {
expect(isFunction(undefined)).toBeFalsy();
expect(isFunction(null)).toBeFalsy();
expect(isFunction('')).toBeFalsy();
expect(isFunction(() => {})).toBeTruthy();
expect(isFunction(async () => {})).toBeTruthy();
});

it('should check isSymbol correctly', () => {
expect(isSymbol(undefined)).toBeFalsy();
expect(isSymbol(null)).toBeFalsy();
expect(isSymbol('')).toBeFalsy();
expect(isSymbol(() => {})).toBeFalsy();
expect(isSymbol(20)).toBeFalsy();
expect(isSymbol(Symbol('test'))).toBeTruthy();
});

it('should check isDisplayObject correctly', () => {
expect(isDisplayObject(undefined)).toBeFalsy();
expect(isDisplayObject(null)).toBeFalsy();
Expand All @@ -38,15 +14,6 @@ describe('Assert utils', () => {
expect(isDisplayObject(new Circle())).toBeTruthy();
});

it('should assert correctly', () => {
expect(() => DCHECK(true)).not.toThrow();
expect(() => DCHECK(false)).toThrow();
expect(() => DCHECK_EQ(1, 1)).not.toThrow();
expect(() => DCHECK_EQ(1, 2)).toThrow();
expect(() => DCHECK_NE(1, 1)).toThrow();
expect(() => DCHECK_NE(1, 2)).not.toThrow();
});

it('should definedProps correctly', () => {
expect(definedProps({ a: 1, b: undefined })).toStrictEqual({ a: 1 });
});
Expand Down
6 changes: 3 additions & 3 deletions packages/g-lite/src/css/StyleValueRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isNil, isUndefined } from '@antv/util';
import { vec3 } from 'gl-matrix';
import { GeometryAABBUpdater } from '..';
import type { DisplayObject } from '../display-objects';
import { EMPTY_PARSED_PATH } from '../display-objects/constants';
Expand All @@ -10,7 +11,6 @@ import type {
Tuple3Number,
} from '../types';
import { Shape } from '../types';
import { addVec3 } from '../utils/math';
import type { CSSRGB } from './cssom';
import type {
PropertyMetadata,
Expand Down Expand Up @@ -932,11 +932,11 @@ export class DefaultStyleValueRegistry implements StyleValueRegistry {
const blurRadius = params[0].value as number;
geometry.renderBounds.update(
geometry.renderBounds.center,
addVec3(
vec3.add(
geometry.renderBounds.halfExtents,
geometry.renderBounds.halfExtents,
[blurRadius, blurRadius, 0],
),
) as Tuple3Number,
);
} else if (name === 'drop-shadow') {
const shadowOffsetX = params[0].value;
Expand Down
2 changes: 0 additions & 2 deletions packages/g-lite/src/css/cssom/CSSNumericValue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DCHECK } from '../../utils/assert';
import {
canonicalUnitTypeForCategory,
conversionToCanonicalUnitsScaleFactor,
Expand All @@ -18,7 +17,6 @@ const formatInfinityOrNaN = (number: number, suffix = '') => {
if (number > 0) result = 'infinity';
else result = '-infinity';
} else {
DCHECK(Number.isNaN(number));
result = 'NaN';
}
return (result += suffix);
Expand Down
2 changes: 1 addition & 1 deletion packages/g-lite/src/dom/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Shape } from '../types';
import {
ERROR_MSG_METHOD_NOT_IMPLEMENTED,
ERROR_MSG_USE_DOCUMENT_ELEMENT,
isFunction,
} from '../utils';
import type {
DisplayObjectConfig,
Expand All @@ -18,6 +17,7 @@ import type {
INode,
} from './interfaces';
import { Node } from './Node';
import { isFunction } from '@antv/util';

/**
* the entry of DOM tree
Expand Down
3 changes: 1 addition & 2 deletions packages/g-lite/src/dom/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { Strategy } from '../components';
import { runtime } from '../global-runtime';
import type { AABB, Rectangle } from '../shapes';
import type { BaseStyleProps, ParsedBaseStyleProps } from '../types';
import { isSymbol } from '../utils/assert';
import {
ERROR_MSG_APPEND_DESTROYED_ELEMENT,
ERROR_MSG_METHOD_NOT_IMPLEMENTED,
Expand Down Expand Up @@ -560,7 +559,7 @@ export class Element<
*/
getAttribute(name: keyof StyleProps) {
// @see https://github.com/antvis/G/issues/1267
if (isSymbol(name)) {
if (typeof name === 'symbol') {
return undefined;
}

Expand Down
3 changes: 1 addition & 2 deletions packages/g-lite/src/dom/EventTarget.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isBoolean, isObject } from '@antv/util';
import { isBoolean, isFunction, isObject } from '@antv/util';
import EventEmitter from 'eventemitter3';
import { isFunction } from '../utils';
import { CustomEvent } from './CustomEvent';
import { FederatedEvent, isFederatedEvent } from './FederatedEvent';
import type {
Expand Down
71 changes: 22 additions & 49 deletions packages/g-lite/src/shapes/AABB.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import type { mat4 } from 'gl-matrix';
// import { vec3 } from 'gl-matrix';
import { vec3 } from 'gl-matrix';
import { Tuple3Number } from '../types';
import {
addVec3,
copyVec3,
maxVec3,
minVec3,
scaleVec3,
subVec3,
} from '../utils/math';
import type { Plane } from './Plane';

const {
add: addVec3,
copy: copyVec3,
max: maxVec3,
min: minVec3,
scale: scaleVec3,
sub: subVec3,
} = vec3;

/**
* Axis-Aligned Bounding Box
* 为了便于后续 Frustum Culling,通过查找表定义 p-vertex 和 n-vertex
Expand All @@ -31,34 +32,14 @@ export class AABB {
min: Tuple3Number = [0, 0, 0];
max: Tuple3Number = [0, 0, 0];

// center: vec3 = vec3.create();

// halfExtents: vec3 = vec3.create();

// min: vec3 = vec3.create();
// max: vec3 = vec3.create();

update(center: Tuple3Number, halfExtents: Tuple3Number) {
copyVec3(this.center, center);
copyVec3(this.halfExtents, halfExtents);
subVec3(this.min, this.center, this.halfExtents);
addVec3(this.max, this.center, this.halfExtents);
// vec3.copy(this.center, center);
// vec3.copy(this.halfExtents, halfExtents);
// vec3.sub(this.min, this.center, this.halfExtents);
// vec3.add(this.max, this.center, this.halfExtents);
}

setMinMax(min: Tuple3Number, max: Tuple3Number) {
// vec3.add(this.center, max, min);
// vec3.scale(this.center, this.center, 0.5);

// vec3.sub(this.halfExtents, max, min);
// vec3.scale(this.halfExtents, this.halfExtents, 0.5);

// vec3.copy(this.min, min);
// vec3.copy(this.max, max);

addVec3(this.center, max, min);
scaleVec3(this.center, this.center, 0.5);
subVec3(this.halfExtents, max, min);
Expand Down Expand Up @@ -225,36 +206,28 @@ export class AABB {
}

const intersection = new AABB();
// const min = vec3.max(vec3.create(), this.getMin(), aabb.getMin());
// const max = vec3.min(vec3.create(), this.getMax(), aabb.getMax());
const min = maxVec3([0, 0, 0], this.getMin(), aabb.getMin());
const max = minVec3([0, 0, 0], this.getMax(), aabb.getMax());
const min = maxVec3(
[0, 0, 0],
this.getMin(),
aabb.getMin(),
) as Tuple3Number;
const max = minVec3(
[0, 0, 0],
this.getMax(),
aabb.getMax(),
) as Tuple3Number;
intersection.setMinMax(min, max);

return intersection;
}

// containsPoint(point: vec3) {
// const min = this.getMin();
// const max = this.getMax();

// return !(
// point[0] < min[0] ||
// point[0] > max[0] ||
// point[1] < min[1] ||
// point[1] > max[1] ||
// point[2] < min[2] ||
// point[2] > max[2]
// );
// }

/**
* get n-vertex
* @param plane plane of CullingVolume
*/
getNegativeFarPoint(plane: Plane): Tuple3Number {
if (plane.pnVertexFlag === 0x111) {
return copyVec3([0, 0, 0], this.min);
return copyVec3([0, 0, 0], this.min) as Tuple3Number;
// return vec3.copy(vec3.create(), this.min);
} else if (plane.pnVertexFlag === 0x110) {
return [this.min[0], this.min[1], this.max[2]];
Expand Down Expand Up @@ -286,7 +259,7 @@ export class AABB {
*/
getPositiveFarPoint(plane: Plane): Tuple3Number {
if (plane.pnVertexFlag === 0x111) {
return copyVec3([0, 0, 0], this.max);
return copyVec3([0, 0, 0], this.max) as Tuple3Number;
// return vec3.copy(vec3.create(), this.max);
} else if (plane.pnVertexFlag === 0x110) {
return [this.max[0], this.max[1], this.min[2]];
Expand Down
27 changes: 0 additions & 27 deletions packages/g-lite/src/utils/assert.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,2 @@
export function DCHECK(bool: boolean) {
if (!bool) {
throw new Error();
}
}

export function DCHECK_EQ(a: any, b: any) {
if (a !== b) {
throw new Error();
}
}

export function DCHECK_NE(a: any, b: any) {
if (a === b) {
throw new Error();
}
}

export function isFunction(func: any): func is (...args: any[]) => any {
return typeof func === 'function';
}

export function isSymbol(value: any): value is symbol {
// @see https://github.com/lodash/lodash/blob/master/isSymbol.js
return typeof value === 'symbol';
}

export const definedProps = (obj: Record<string, unknown>) =>
Object.fromEntries(Object.entries(obj).filter(([, v]) => v !== undefined));
Loading

0 comments on commit abd828e

Please sign in to comment.