Skip to content

Commit

Permalink
feat: offset path 40b757e
Browse files Browse the repository at this point in the history
  • Loading branch information
plantain-00 committed Nov 4, 2023
1 parent 55f784b commit 45c3d2e
Show file tree
Hide file tree
Showing 19 changed files with 505 additions and 149 deletions.
2 changes: 1 addition & 1 deletion dev/cad-editor/model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,7 @@ export function getSnapTargetRef(target: CoreSnapTarget<BaseContent> | undefined
} : undefined
}

export function trimOffsetResult(points: Position[], point: Position) {
export function trimOffsetResult(points: Position[], point: Position, closed: boolean) {
let intersectionPoints: Position[] = []
for (let i = 0; i < points.length - 1; i++) {
for (let j = i + 2; j < points.length - 1; j++) {
Expand Down
2 changes: 1 addition & 1 deletion dev/cad-editor/plugins/line-polyline.plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export function getModel(ctx: PluginContext) {
points.push(p)
}
}
return ctx.trimOffsetResult(points, point).map(p => ctx.produce(content, (d) => {
return ctx.trimOffsetResult(points, point, closed).map(p => ctx.produce(content, (d) => {
d.points = p
}))
},
Expand Down
7 changes: 7 additions & 0 deletions dev/cad-editor/plugins/path.plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ export function getModel(ctx: PluginContext): model.Model<PathContent> {
const lines = getPathGeometriesFromCache(content).lines
return ctx.breakGeometryLinesToPathCommands(lines, intersectionPoints)
},
offset(content, point, distance) {
const lines = getPathGeometriesFromCache(content).lines
return ctx.getParallelGeometryLinesByDistance(point, lines, distance).map(g => ({
...content,
commands: ctx.geometryLineToPathCommands(g),
}))
},
render(content, { target, getStrokeColor, getFillColor, transformStrokeWidth, getFillPattern, contents }) {
const strokeStyleContent = ctx.getStrokeStyleContent(content, contents)
const fillStyleContent = ctx.getFillStyleContent(content, contents)
Expand Down
2 changes: 1 addition & 1 deletion dev/cad-editor/plugins/polygon.plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function getModel(ctx: PluginContext): model.Model<PolygonContent> {
points.push(p)
}
}
return ctx.trimOffsetResult(points, point).map(p => ctx.produce(content, (d) => {
return ctx.trimOffsetResult(points, point, true).map(p => ctx.produce(content, (d) => {
d.points = p
}))
},
Expand Down
11 changes: 9 additions & 2 deletions dev/cad-editor/plugins/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4522,7 +4522,7 @@ function getModel(ctx) {
points.push(p);
}
}
return ctx.trimOffsetResult(points, point).map((p) => ctx.produce(content, (d) => {
return ctx.trimOffsetResult(points, point, closed).map((p) => ctx.produce(content, (d) => {
d.points = p;
}));
},
Expand Down Expand Up @@ -5778,6 +5778,13 @@ function getModel(ctx) {
const lines = getPathGeometriesFromCache(content).lines;
return ctx.breakGeometryLinesToPathCommands(lines, intersectionPoints);
},
offset(content, point, distance) {
const lines = getPathGeometriesFromCache(content).lines;
return ctx.getParallelGeometryLinesByDistance(point, lines, distance).map((g) => ({
...content,
commands: ctx.geometryLineToPathCommands(g)
}));
},
render(content, { target, getStrokeColor, getFillColor, transformStrokeWidth, getFillPattern, contents }) {
var _a;
const strokeStyleContent = ctx.getStrokeStyleContent(content, contents);
Expand Down Expand Up @@ -6737,7 +6744,7 @@ function getModel(ctx) {
points.push(p);
}
}
return ctx.trimOffsetResult(points, point).map((p) => ctx.produce(content, (d) => {
return ctx.trimOffsetResult(points, point, true).map((p) => ctx.produce(content, (d) => {
d.points = p;
}));
},
Expand Down
2 changes: 1 addition & 1 deletion main.bundle.js

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion spec/equation-calculater.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava'
import { calculateEquation2, calculateEquation3, calculateEquation4, isZero } from '../src'
import { calculateEquation2, calculateEquation3, calculateEquation4, calculateEquation5, isZero } from '../src'

test('calculate2', (t) => {
function calculate(b: number, c: number) {
Expand Down Expand Up @@ -32,4 +32,10 @@ test('calculate4', (t) => {
}
calculate(2, 3, 4, 5)
calculate(-91.9109137181446, -3883.2794987528127, 338909.5212583375, 1382.1028009808458 - 1404)
})

test('calculate5', (t) => {
t.snapshot(calculateEquation5([7.071056120832166, -23.262358240508846, 30.960350917062048, -14.07113243539834, -2.7073208816199905, 4.197849310408487, -0.044600854567165005], 0.5))
t.snapshot(calculateEquation5([7.0567150091369735, -24.17843958459045, 33.346089135242096, -16.62301668732119, -1.938654426185055, 4.3413557871976245, -0.08361877325786682], 0.5))
t.snapshot(calculateEquation5([7.0567150091369735, -24.17843958459045, 33.346089135242096, -16.62301668732119, -1.938654426185055, 4.3413557871976245, -0.08361877325786682], 0.5))
})
28 changes: 28 additions & 0 deletions spec/equation-calculater.ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Snapshot report for `spec/equation-calculater.ts`

The actual snapshot is saved in `equation-calculater.ts.snap`.

Generated by [AVA](https://avajs.dev).

## calculate5

> Snapshot 1
[
-0.41547266872803507,
0.010702569023709144,
]

> Snapshot 2
[
-0.39539911420633767,
0.01945724054548644,
]

> Snapshot 3
[
-0.39539911420633767,
0.01945724054548644,
]
Binary file added spec/equation-calculater.ts.snap
Binary file not shown.
4 changes: 3 additions & 1 deletion src/components/react-render-target/react-render-target.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ export function pathCommandsToGeometryLines(pathCommands: PathCommand[]): Geomet
const t1 = getPerpendicularPoint(center, line1)
const t2 = getPerpendicularPoint(center, line2)
if (last) {
lines.push([last, { x: t1.x, y: t1.y }])
if (!isSamePoint(last, t1)) {
lines.push([last, { x: t1.x, y: t1.y }])
}
last = t2
}
const startAngle = radianToAngle(getTwoPointsRadian(t1, center))
Expand Down
1 change: 0 additions & 1 deletion src/components/use-selected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export function useSelected<T extends SelectPath = SelectPath>(options?: Partial
}
}
const removeSelection = (value: readonly T[]) => {
console.info(selected, value)
setSelected(selected.filter(s => value.every(v => !isSamePath(v, s))))
}
const filterSelection = (selectable?: (value: T) => boolean, maxCount = options?.maxCount, s = selected) => {
Expand Down
19 changes: 1 addition & 18 deletions src/utils/break.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getBezierCurvePercentAtPoint, getPartOfBezierCurve, getPartOfQuadraticCurve, getQuadraticCurvePercentAtPoint, pointIsOnBezierCurve, pointIsOnQuadraticCurve } from "./bezier"
import { getAngleInRange, getCirclePointAtRadian, getCircleRadian, getEllipseAngle, getEllipseArcPointAtAngle, getPointSideOfLine, getTwoPointsDistance, isSamePoint, pointIsOnArc, pointIsOnCircle, pointIsOnEllipse, pointIsOnEllipseArc, pointIsOnLine, pointIsOnLineSegment, Position, twoPointLineToGeneralFormLine } from "./geometry"
import { getAngleInRange, getCirclePointAtRadian, getCircleRadian, getEllipseAngle, getEllipseArcPointAtAngle, getTwoPointsDistance, isSamePoint, pointIsOnArc, pointIsOnCircle, pointIsOnEllipse, pointIsOnEllipseArc, pointIsOnLine, pointIsOnLineSegment, Position } from "./geometry"
import { GeometryLine } from "./intersection"
import { getPointAndLineSegmentMinimumDistance } from "./perpendicular"
import { angleToRadian, radianToAngle } from "./radian"

/**
Expand Down Expand Up @@ -235,19 +234,3 @@ export function breakGeometryLines(lines: GeometryLine[], intersectionPoints: Po
}
return result
}

export function getLinesOffsetDirection(point: Position, lines: [Position, Position][]) {
let min: { distance: number, line: [Position, Position] } | undefined
for (const line of lines) {
const distance = getPointAndLineSegmentMinimumDistance(point, ...line)
if (!min || distance < min.distance) {
min = { distance, line }
}
}
const line = min?.line ?? lines[0]
return pointSideToIndex(getPointSideOfLine(point, twoPointLineToGeneralFormLine(...line)))
}

export function pointSideToIndex(side: number) {
return side > 0 ? 1 : 0
}
7 changes: 1 addition & 6 deletions src/utils/equation-calculater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,14 @@ export function newtonIterate(
f1:(x: number) => number,
f2:(x: number) => number,
delta: number,
maxIteratorCount = 20,
maxIteratorCount = 100,
) {
let x = x0
let count = 0
let last: number | undefined
for (; ;) {
const g = f1(x)
const d = Math.abs(g)
if (d < delta) break
if (last && count > 1 && d >= last) {
return
}
last = d
if (count > maxIteratorCount) {
return
}
Expand Down
1 change: 0 additions & 1 deletion src/utils/expression-expansion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,6 @@ export function groupAllFactors(factors: Factor[]): Expression2 | undefined {
}

export function groupFactorsByVariables(factors: Factor[], variables: string[]) {
console.info(variables)
const group = (factors: Factor[], level: number): Expression2 | undefined => {
if (factors.length <= 1) return
const v = variables[level]
Expand Down
41 changes: 41 additions & 0 deletions src/utils/geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,25 @@ export function getParallelLinesByDistance(line: GeneralFormLine, distance: numb
]
}

export function getParallelLineSegmentsByDistance(line: [Position, Position], distance: number): [[Position, Position], [Position, Position]] {
if (isZero(distance)) {
return [line, line]
}
const radian = getTwoPointsRadian(line[1], line[0])
const leftRadian = radian - Math.PI / 2
const rightRadian = radian + Math.PI / 2
return [
[
getPointByLengthAndRadian(line[0], distance, rightRadian),
getPointByLengthAndRadian(line[1], distance, rightRadian)
],
[
getPointByLengthAndRadian(line[0], distance, leftRadian),
getPointByLengthAndRadian(line[1], distance, leftRadian)
],
]
}

/**
* @public
*/
Expand Down Expand Up @@ -1124,3 +1143,25 @@ export type Image = Region & {
export const Image = /* @__PURE__ */ and(Region, {
url: string,
})

export function minimumBy<T>(values: T[], by: (value: T) => number) {
let result = values[0]
for (let i = 1; i < values.length; i++) {
const value = values[i]
if (by(value) < by(result)) {
result = value
}
}
return result
}

export function maxmiumBy<T>(values: T[], by: (value: T) => number) {
let result = values[0]
for (let i = 1; i < values.length; i++) {
const value = values[i]
if (by(value) < by(result)) {
result = value
}
}
return result
}
Loading

0 comments on commit 45c3d2e

Please sign in to comment.