Skip to content

Commit

Permalink
feat: remove b-spline 590ebb0
Browse files Browse the repository at this point in the history
  • Loading branch information
plantain-00 committed Oct 12, 2023
1 parent 021b2dd commit ab47eb0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 142 deletions.
20 changes: 3 additions & 17 deletions dev/cad-editor/plugins/spline.plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { PluginContext } from './types'
import type * as core from '../../../src'
import type { Command } from '../command'
import type * as model from '../model'
import bspline from 'b-spline'
import type { LineContent } from './line-polyline.plugin'

export type SplineContent = model.BaseContent<'spline'> & model.StrokeFields & model.FillFields & model.SegmentCountFields & {
Expand Down Expand Up @@ -36,22 +35,9 @@ export function getModel(ctx: PluginContext): model.Model<SplineContent | Spline
points = curves.map(c => ctx.getBezierCurvePoints(c.from, c.cp1, c.cp2, c.to, splineSegmentCount)).flat()
lines = curves.map(c => ({ type: 'bezier curve' as const, curve: c }))
} else {
const degree = 2
const knots: number[] = []
for (let i = 0; i < inputPoints.length + degree + 1; i++) {
if (i < degree + 1) {
knots.push(0)
} else if (i < inputPoints.length) {
knots.push(i - degree)
} else {
knots.push(inputPoints.length - degree)
}
}
for (let t = 0; t <= splineSegmentCount; t++) {
const p = bspline(t / splineSegmentCount, degree, inputPoints, knots)
points.push({ x: p[0], y: p[1] })
}
lines = Array.from(ctx.iteratePolylineLines(points))
const curves = ctx.getQuadraticSplineCurves(content.points)
points = curves.map(c => ctx.getQuadraticCurvePoints(c.from, c.cp, c.to, splineSegmentCount)).flat()
lines = curves.map(c => ({ type: 'quadratic curve' as const, curve: c }))
}
} else {
points = content.points
Expand Down
115 changes: 4 additions & 111 deletions dev/cad-editor/plugins/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8070,101 +8070,7 @@ export {
isRoundedRectContent
};
`,
`var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
// node_modules/b-spline/index.js
var require_b_spline = __commonJS({
"node_modules/b-spline/index.js"(exports, module) {
function interpolate(t, degree, points, knots, weights, result) {
var i, j, s, l;
var n = points.length;
var d = points[0].length;
if (degree < 1)
throw new Error("degree must be at least 1 (linear)");
if (degree > n - 1)
throw new Error("degree must be less than or equal to point count - 1");
if (!weights) {
weights = [];
for (i = 0; i < n; i++) {
weights[i] = 1;
}
}
if (!knots) {
var knots = [];
for (i = 0; i < n + degree + 1; i++) {
knots[i] = i;
}
} else {
if (knots.length !== n + degree + 1)
throw new Error("bad knot vector length");
}
var domain = [
degree,
knots.length - 1 - degree
];
var low = knots[domain[0]];
var high = knots[domain[1]];
t = t * (high - low) + low;
if (t < low || t > high)
throw new Error("out of bounds");
for (s = domain[0]; s < domain[1]; s++) {
if (t >= knots[s] && t <= knots[s + 1]) {
break;
}
}
var v = [];
for (i = 0; i < n; i++) {
v[i] = [];
for (j = 0; j < d; j++) {
v[i][j] = points[i][j] * weights[i];
}
v[i][d] = weights[i];
}
var alpha;
for (l = 1; l <= degree + 1; l++) {
for (i = s; i > s - degree - 1 + l; i--) {
alpha = (t - knots[i]) / (knots[i + degree + 1 - l] - knots[i]);
for (j = 0; j < d + 1; j++) {
v[i][j] = (1 - alpha) * v[i - 1][j] + alpha * v[i][j];
}
}
}
var result = result || [];
for (i = 0; i < d; i++) {
result[i] = v[s][i] / v[s][d];
}
return result;
}
module.exports = interpolate;
}
});
// dev/cad-editor/plugins/spline.plugin.tsx
var import_b_spline = __toESM(require_b_spline());
`// dev/cad-editor/plugins/spline.plugin.tsx
function getModel(ctx) {
const SplineContent = ctx.and(ctx.BaseContent("spline"), ctx.StrokeFields, ctx.FillFields, ctx.SegmentCountFields, {
points: [ctx.Position],
Expand All @@ -8188,22 +8094,9 @@ function getModel(ctx) {
points = curves.map((c) => ctx.getBezierCurvePoints(c.from, c.cp1, c.cp2, c.to, splineSegmentCount)).flat();
lines = curves.map((c) => ({ type: "bezier curve", curve: c }));
} else {
const degree = 2;
const knots = [];
for (let i = 0; i < inputPoints.length + degree + 1; i++) {
if (i < degree + 1) {
knots.push(0);
} else if (i < inputPoints.length) {
knots.push(i - degree);
} else {
knots.push(inputPoints.length - degree);
}
}
for (let t = 0; t <= splineSegmentCount; t++) {
const p = (0, import_b_spline.default)(t / splineSegmentCount, degree, inputPoints, knots);
points.push({ x: p[0], y: p[1] });
}
lines = Array.from(ctx.iteratePolylineLines(points));
const curves = ctx.getQuadraticSplineCurves(content.points);
points = curves.map((c) => ctx.getQuadraticCurvePoints(c.from, c.cp, c.to, splineSegmentCount)).flat();
lines = curves.map((c) => ({ type: "quadratic curve", curve: c }));
}
} else {
points = content.points;
Expand Down
2 changes: 1 addition & 1 deletion main.bundle.js

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"homepage": "https://github.com/plantain-00/composable-editor-canvas#readme",
"devDependencies": {
"@protocol-based-web-framework/router": "4.0.0",
"@types/b-spline": "2.0.2",
"@types/earcut": "2.1.1",
"@types/offscreencanvas": "2019.7.1",
"@types/opentype.js": "1.3.6",
Expand All @@ -36,7 +35,6 @@
"@typescript-eslint/parser": "6.7.2",
"@webgpu/types": "0.1.34",
"ava": "4.3.3",
"b-spline": "2.0.2",
"clean-release": "2.18.0",
"clean-scripts": "1.21.1",
"css-loader": "6.8.1",
Expand Down
14 changes: 13 additions & 1 deletion src/utils/bezier.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { calculateEquation3 } from "./equation-calculater"
import { Position, isZero } from "./geometry"
import { Position, getTwoPointCenter, isZero } from "./geometry"
import { BezierCurve, QuadraticCurve } from "./intersection"
import { Vec3 } from "./types"

Expand Down Expand Up @@ -100,6 +100,18 @@ export function getBezierSplineCurves(points: Position[]) {
return result
}

export function getQuadraticSplineCurves(points: Position[]) {
const result: QuadraticCurve[] = []
for (let i = 1; i < points.length - 1; i++) {
result.push({
from: i === 1 ? points[i - 1] : getTwoPointCenter(points[i], points[i - 1]),
cp: points[i],
to: i === points.length - 2 ? points[i + 1] : getTwoPointCenter(points[i], points[i + 1]),
})
}
return result
}

export function getBezierSplinePoints(points: Position[], segmentCount: number) {
const curves = getBezierSplineCurves(points)
return curves.map(c => getBezierCurvePoints(c.from, c.cp1, c.cp2, c.to, segmentCount)).flat()
Expand Down
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,6 @@
qs "6"
tslib "1 || 2"

"@types/b-spline@2.0.2":
version "2.0.2"
resolved "https://registry.yarnpkg.com/@types/b-spline/-/b-spline-2.0.2.tgz#e6094184c6a9ed685b3a2e37f39d4461cc873b91"
integrity sha512-CeCJpH5tmSm/qoSUv4ht9LidFFgi/vNxrKIHUyZnsMabStzBHCOCEodn0f5QwT27Yo0ux42Bw9ce3GF6BunstQ==

"@types/body-parser@*":
version "1.19.3"
resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.19.3.tgz#fb558014374f7d9e56c8f34bab2042a3a07d25cd"
Expand Down Expand Up @@ -1168,11 +1163,6 @@ ava@4.3.3:
write-file-atomic "^4.0.1"
yargs "^17.5.1"

b-spline@2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/b-spline/-/b-spline-2.0.2.tgz#964becb6e33de6b11bae1ba229c4477eab2e4426"
integrity sha512-RPhM4apTUkejCpKo9sQrOiMdy/rUua4n98oppafwAnT64ZwqJDDp+fUk9+5EBEQNtTS3CsWWFRPukqFM3sasZQ==

balanced-match@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
Expand Down

0 comments on commit ab47eb0

Please sign in to comment.