Skip to content

Commit

Permalink
fix: Delete all connections when a line as converted to a line polygon
Browse files Browse the repository at this point in the history
- Its body still kept connections.
  • Loading branch information
miyanokomiya committed Oct 12, 2024
1 parent 030c482 commit 7bb1c27
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/shapes/polygons/linePolygon.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, test, expect } from "vitest";
import { struct as lineStruct } from "../line";
import { struct } from "./linePolygon";
import { LinePolygonShape, struct } from "./linePolygon";
import { patchLinePolygonFromLine } from "../utils/linePolygon";
import { getCommonStruct } from "..";

Expand All @@ -12,7 +12,7 @@ describe("resize", () => {
curves: [{ c1: { x: 150, y: 100 }, c2: { x: 150, y: 0 } }],
q: { x: 0, y: 0 },
});
const shape = { ...line, ...patchLinePolygonFromLine(getCommonStruct, line) };
const shape = { ...line, ...patchLinePolygonFromLine(getCommonStruct, line) } as LinePolygonShape;
const result = struct.resize(shape, [2, 0, 0, 1, 10, 0]);
expect(result.p).toEqualPoint({ x: 10, y: 0 });
expect(result.width).toBeCloseTo(275);
Expand All @@ -32,7 +32,7 @@ describe("resize", () => {
body: [{ p: { x: 100, y: 0 } }],
q: { x: 0, y: 0 },
});
const shape = { ...line, ...patchLinePolygonFromLine(getCommonStruct, line) };
const shape = { ...line, ...patchLinePolygonFromLine(getCommonStruct, line) } as LinePolygonShape;
const result0 = struct.resize(shape, [2, 0, 0, 1, 10, 0]);
expect(result0).toHaveProperty("srcLine");
expect(result0.srcLine).toBe(undefined);
Expand Down
35 changes: 26 additions & 9 deletions src/shapes/utils/linePolygon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,27 @@ import { createShape, getCommonStruct } from "..";
import { LineShape } from "../line";
import { ISegment } from "../../utils/geometry";
import { getArcLerpFn } from "okageo";
import { LinePolygonShape } from "../polygons/linePolygon";

describe("patchLinePolygonFromLine", () => {
test("should make a line polygon from the line", () => {
const line = createShape<LineShape>(getCommonStruct, "line", { p: { x: 100, y: 0 }, q: { x: 0, y: 50 } });
const line = createShape<LineShape>(getCommonStruct, "line", {
p: { x: 100, y: 0 },
body: [{ p: { x: 100, y: 50 }, c: { id: "a", rate: { x: 0.1, y: 0.2 } } }],
q: { x: 0, y: 50 },
pConnection: { id: "b", rate: { x: 0.1, y: 0.2 } },
});
const result = patchLinePolygonFromLine(getCommonStruct, line);
expect(result.p).toEqualPoint({ x: 0, y: 0 });
expect(result.width).toBeCloseTo(100);
expect(result.height).toBeCloseTo(50);
expect(result).toHaveProperty("pConnection");
expect(result.pConnection).toBe(undefined);
expect(result.srcLine?.vertices, "should delete all connections").toEqual([
{ p: { x: 100, y: 0 } },
{ p: { x: 100, y: 50 } },
{ p: { x: 0, y: 50 } },
]);
});

test("should convert arc segment to bezier segment", () => {
Expand All @@ -22,12 +35,12 @@ describe("patchLinePolygonFromLine", () => {
curves: [{ d: { x: 0, y: 50 } }, { d: { x: 0, y: -50 } }],
});
const result = patchLinePolygonFromLine(getCommonStruct, line);
expect(result.path.path).toHaveLength(9);
expect(result.path.path[2]).toEqualPoint({ x: 50, y: 50 });
expect(result.path.path[4]).toEqualPoint({ x: 100, y: 0 });
expect(result.path.path[6]).toEqualPoint({ x: 150, y: 50 });
expect(result.path.path[8]).toEqualPoint({ x: 100, y: 100 });
expect(result.path.curves).toHaveLength(8);
expect(result.path?.path).toHaveLength(9);
expect(result.path?.path[2]).toEqualPoint({ x: 50, y: 50 });
expect(result.path?.path[4]).toEqualPoint({ x: 100, y: 0 });
expect(result.path?.path[6]).toEqualPoint({ x: 150, y: 50 });
expect(result.path?.path[8]).toEqualPoint({ x: 100, y: 100 });
expect(result.path?.curves).toHaveLength(8);
});
});

Expand All @@ -40,13 +53,17 @@ describe("patchLineFromLinePolygon", () => {

test("should make a line from the line polygon", () => {
const linePolygon = patchLinePolygonFromLine(getCommonStruct, line);
const result = patchLineFromLinePolygon(getCommonStruct, linePolygon);
const result = patchLineFromLinePolygon(getCommonStruct, { ...line, ...linePolygon } as LinePolygonShape);
expect(line).toEqual(result);
});

test("should regard rotation", () => {
const linePolygon = patchLinePolygonFromLine(getCommonStruct, line);
const result = patchLineFromLinePolygon(getCommonStruct, { ...linePolygon, rotation: Math.PI / 2 });
const result = patchLineFromLinePolygon(getCommonStruct, {
...line,
...linePolygon,
rotation: Math.PI / 2,
} as LinePolygonShape);
expect(result.rotation).toBeCloseTo(0);
expect(result.p).toEqualPoint({ x: 100, y: 100 });
expect(result.body?.[0].p).toEqualPoint({ x: 0, y: 100 });
Expand Down
15 changes: 11 additions & 4 deletions src/shapes/utils/linePolygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import { ArcCurveControl } from "../../models";
import { getNakedLineShape } from "./line";
import { getRectShapeCenter } from "../rectPolygon";

export function patchLinePolygonFromLine(getStruct: GetShapeStruct, line: LineShape): LinePolygonShape {
export function patchLinePolygonFromLine(
getStruct: GetShapeStruct,
line: LineShape,
): Partial<LineShape & LinePolygonShape> {
const result = createLinePolygonFromLine(getStruct, line);
const patch = mapReduce<any, any, any>(line, () => undefined);
return { ...patch, ...result };
Expand All @@ -26,7 +29,8 @@ function createLinePolygonFromLine(getStruct: GetShapeStruct, line: LineShape):
const rect = getWrapperRect(getStruct, getNakedLineShape(line), undefined, true);
const p = { x: rect.x, y: rect.y };
const normalizedLine = { ...line, ...resizeShape(getStruct, line, [1, 0, 0, 1, -p.x, -p.y]) };
const polygonPath = convertLinePathToSimplePath(getLinePath(normalizedLine), normalizedLine.curves);
const normalizedVertices = getLinePath(normalizedLine);
const polygonPath = convertLinePathToSimplePath(normalizedVertices, normalizedLine.curves);

const linePolygon = createShape<LinePolygonShape>(getStruct, "line_polygon", {
...normalizedLine,
Expand All @@ -36,7 +40,7 @@ function createLinePolygonFromLine(getStruct: GetShapeStruct, line: LineShape):
curves: polygonPath.curves,
},
srcLine: {
vertices: [{ p: normalizedLine.p }, ...(normalizedLine.body ?? []), { p: normalizedLine.q }],
vertices: normalizedVertices.map((p) => ({ p })),
curves: normalizedLine.curves,
lineType: normalizedLine.lineType,
curveType: normalizedLine.curveType,
Expand All @@ -48,7 +52,10 @@ function createLinePolygonFromLine(getStruct: GetShapeStruct, line: LineShape):
return linePolygon;
}

export function patchLineFromLinePolygon(getStruct: GetShapeStruct, linePolygon: LinePolygonShape): LineShape {
export function patchLineFromLinePolygon(
getStruct: GetShapeStruct,
linePolygon: LinePolygonShape,
): Partial<LineShape & LinePolygonShape> {
const result = createLineFromLinePolygon(getStruct, linePolygon);
const patch = mapReduce<any, any, any>(linePolygon, () => undefined);
return { ...patch, ...result };
Expand Down

0 comments on commit 7bb1c27

Please sign in to comment.