Skip to content

Commit

Permalink
feat: optimize path getBBox
Browse files Browse the repository at this point in the history
  • Loading branch information
F-star committed Feb 21, 2024
1 parent 3eb6ba1 commit eac55b5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
"dependencies": {
"@suika/common": "workspace:^",
"@suika/geo": "workspace:^",
"bezier-js": "^6.1.4",
"stats.js": "^0.17.0"
},
"devDependencies": {
"@types/bezier-js": "^4.1.3",
"@types/stats.js": "^0.17.3",
"vite": "^4.2.0"
}
Expand Down
41 changes: 29 additions & 12 deletions packages/core/src/graphs/path.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { parseRGBAStr } from '@suika/common';
import {
addPoint,
getRectByPoints,
IPoint,
IRect,
IRectWithRotation,
} from '@suika/geo';
import { addPoint, IPoint, IRect, IRectWithRotation } from '@suika/geo';
import { Bezier } from 'bezier-js';

import { ImgManager } from '../Img_manager';
import { TextureType } from '../texture';
Expand Down Expand Up @@ -34,15 +29,37 @@ export class Path extends Graph {
}

override getBBox(): IRect {
const points: IPoint[] = [];
// TODO: cache
const pathData = this.pathData;
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
for (const path of pathData) {
for (const seg of path) {
points.push(seg.point, Path.getHandleIn(seg), Path.getHandleOut(seg));
for (let i = 1; i < path.length; i++) {
const seg = path[i];
const prevSeg = path[i - 1];
const bbox = new Bezier(
prevSeg.point,
Path.getHandleOut(prevSeg),
Path.getHandleIn(seg),
seg.point,
).bbox();
minX = Math.min(minX, bbox.x.min);
minY = Math.min(minY, bbox.y.min);
maxX = Math.max(maxX, bbox.x.max);
maxY = Math.max(maxY, bbox.y.max);
}
}
const rect = getRectByPoints(points);
return rect;
if (minX === Infinity) {
return { x: 0, y: 0, width: 100, height: 100 };
}
return {
x: minX,
y: minY,
width: maxX - minX,
height: maxY - minY,
};
}

override getRectWithRotation(): IRectWithRotation {
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eac55b5

Please sign in to comment.