Skip to content

Commit

Permalink
✨ feat: display picked points
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Mar 8, 2024
1 parent 83ffd44 commit e84a37e
Show file tree
Hide file tree
Showing 21 changed files with 152 additions and 86 deletions.
30 changes: 13 additions & 17 deletions packages/chili/src/commands/create/arc.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import {
Colors,
GeometryModel,
Plane,
PlaneAngle,
Precision,
ShapeMeshData,
VertexMeshData,
XYZ,
command,
} from "chili-core";
import { GeometryModel, Plane, PlaneAngle, Precision, ShapeMeshData, XYZ, command } from "chili-core";
import { ArcBody } from "../../bodys/arcBody";
import { Dimension, SnapLengthAtPlaneData } from "../../snap";
import { AngleStep, IStep, LengthAtPlaneStep, PointStep } from "../../step";
Expand Down Expand Up @@ -55,14 +45,14 @@ export class Arc extends CreateCommand {
private getAngleData = () => {
let [center, p1] = [this.stepDatas[0].point!, this.stepDatas[1].point!];
let plane = new Plane(center, this.stepDatas[0].view.workplane.normal, p1.sub(center));
let points: ShapeMeshData[] = [
VertexMeshData.from(center, 5, Colors.Red),
VertexMeshData.from(p1, 5, Colors.Red),
];
let points: ShapeMeshData[] = [this.previewPoint(center), this.previewPoint(p1)];
this._planeAngle = new PlaneAngle(plane);
return {
dimension: Dimension.D1D2,
preview: (point: XYZ) => {
preview: (point: XYZ | undefined) => {
if (point === undefined) {
point = p1;
}
this._planeAngle!.movePoint(point);
let result = [...points];
if (Math.abs(this._planeAngle!.angle) > Precision.Angle) {
Expand Down Expand Up @@ -92,10 +82,16 @@ export class Arc extends CreateCommand {
return new GeometryModel(this.document, `Arc ${Arc.count++}`, body);
}

private circlePreview = (point: XYZ) => {
private circlePreview = (point: XYZ | undefined) => {
let p1 = this.previewPoint(this.stepDatas[0].point!);
if (!point) {
return [p1];
}
let start = this.stepDatas[0].point!;
let plane = this.stepDatas[0].view.workplane;
return [
p1,
this.previewLine(this.stepDatas[0].point!, point),
this.application.shapeFactory
.circle(plane.normal, start, this.getDistanceAtPlane(plane, start, point))
.unwrap().mesh.edges!,
Expand Down
9 changes: 8 additions & 1 deletion packages/chili/src/commands/create/box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ export class Box extends RectCommandBase {
};
};

private previewBox = (end: XYZ) => {
private previewBox = (end: XYZ | undefined) => {
if (!end) {
return this.previewRect(this.stepDatas[1].point);
}
let p1 = this.previewPoint(this.stepDatas[0].point!);
let p2 = this.previewPoint(this.stepDatas[1].point!);
let data = this.getRectData(end);
return [
p1,
p2,
this.application.shapeFactory
.box(data.plane, data.dx, data.dy, this.getHeight(data.plane, end))
.unwrap().mesh.edges!,
Expand Down
8 changes: 7 additions & 1 deletion packages/chili/src/commands/create/circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,16 @@ export class Circle extends CreateFaceableCommand {
return new GeometryModel(this.document, `Circle ${Circle.count++}`, body);
}

private circlePreview = (point: XYZ) => {
private circlePreview = (point: XYZ | undefined) => {
let p1 = this.previewPoint(this.stepDatas[0].point!);
if (!point) {
return [p1];
}
let start = this.stepDatas[0].point!;
let plane = this.stepDatas[0].view.workplane;
return [
p1,
this.previewLine(this.stepDatas[0].point!, point),
this.application.shapeFactory
.circle(plane.normal, start, this.getDistanceAtPlane(plane, start, point))
.unwrap().mesh.edges!,
Expand Down
1 change: 1 addition & 0 deletions packages/chili/src/commands/create/fuse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class Fuse extends CreateCommand {
point,
direction: normal,
preview: (p) => {
if (!p) return [];
let dist = p.sub(point).dot(normal);
if (Math.abs(dist) < Precision.Float) return [];
let vec = normal.multiply(dist);
Expand Down
11 changes: 9 additions & 2 deletions packages/chili/src/commands/create/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ export class Line extends CreateCommand {
};
};

private linePreview = (point: XYZ) => {
return [this.application.shapeFactory.line(this.stepDatas[0].point!, point).unwrap().mesh.edges!];
private linePreview = (point: XYZ | undefined) => {
let p1 = this.previewPoint(this.stepDatas[0].point!);
if (!point) {
return [p1];
}
return [
p1,
this.application.shapeFactory.line(this.stepDatas[0].point!, point).unwrap().mesh.edges!,
];
};
}
9 changes: 6 additions & 3 deletions packages/chili/src/commands/create/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,14 @@ export class Polygon extends CreateFaceableCommand {
};
};

private preview = (point: XYZ): ShapeMeshData[] => {
private preview = (point: XYZ | undefined): ShapeMeshData[] => {
let ps = this.stepDatas.map((data) => this.previewPoint(data.point!));
let edges = new EdgeMeshDataBuilder();
this.stepDatas.forEach((data) => edges.addPosition(data.point!.x, data.point!.y, data.point!.z));
edges.addPosition(point.x, point.y, point.z);
return [edges.build()];
if (point) {
edges.addPosition(point.x, point.y, point.z);
}
return [...ps, edges.build()];
};

private validator = (point: XYZ): boolean => {
Expand Down
1 change: 1 addition & 0 deletions packages/chili/src/commands/create/prism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class Prism extends CreateCommand {
point,
direction: normal,
preview: (p) => {
if (!p) return [];
let dist = p.sub(point).dot(normal);
if (Math.abs(dist) < Precision.Float) return [];
let vec = normal.multiply(dist);
Expand Down
13 changes: 11 additions & 2 deletions packages/chili/src/commands/create/rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,18 @@ export abstract class RectCommandBase extends CreateCommand {
return !MathUtils.anyEqualZero(data.dx, data.dy);
};

private previewRect = (end: XYZ) => {
protected previewRect = (end: XYZ | undefined) => {
let p1 = this.previewPoint(this.stepDatas[0].point!);
if (end === undefined) {
return [p1];
}
let data = this.getRectData(end);
return [this.application.shapeFactory.rect(data.plane, data.dx, data.dy).unwrap().mesh.edges!];
let p2 = this.previewPoint(end);
return [
p1,
p2,
this.application.shapeFactory.rect(data.plane, data.dx, data.dy).unwrap().mesh.edges!,
];
};

protected getRectData(point: XYZ): RectData {
Expand Down
4 changes: 3 additions & 1 deletion packages/chili/src/commands/modify/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export class Array extends MultistepCommand {
};
};

private movePreview = (point: XYZ) => {
private movePreview = (point: XYZ | undefined) => {
let p1 = this.previewPoint(this.stepDatas[0].point!);
if (!point) return [p1];
let start = this.stepDatas[0].point!;
let positions = [...this.positions!];
let { x, y, z } = point.sub(start);
Expand Down
6 changes: 4 additions & 2 deletions packages/chili/src/commands/modify/mirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ export class Mirror extends TransformedCommand {
};
};

private mirrorPreview = (point: XYZ): ShapeMeshData[] => {
private mirrorPreview = (point: XYZ | undefined): ShapeMeshData[] => {
let p1 = this.previewPoint(this.stepDatas[0].point!);
if (!point) return [p1];
let shape = this.transformPreview(point);
let offset = point.sub(this.stepDatas[0].point!).normalize()!.multiply(1e6);
let line = this.getTempLineData(this.stepDatas[0].point!.sub(offset), point.add(offset));
return [shape, line];
return [p1, shape, line];
};
}
6 changes: 4 additions & 2 deletions packages/chili/src/commands/modify/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ export class Move extends TransformedCommand {
};
};

private movePreview = (point: XYZ) => {
private movePreview = (point: XYZ | undefined) => {
let p1 = this.previewPoint(this.stepDatas[0].point!);
if (!point) return [p1];
let models = this.transformPreview(point);
let line = this.getTempLineData(this.stepDatas[0].point!, point);
return [models, line];
return [p1, models, line];
};

protected override transfrom(point: XYZ): Matrix4 {
Expand Down
21 changes: 15 additions & 6 deletions packages/chili/src/commands/modify/rotate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,16 @@ export class Rotate extends TransformedCommand {
};
};

private rotatePreview = (point: XYZ): ShapeMeshData[] => {
let shape = this.transformPreview(point);
private rotatePreview = (point: XYZ | undefined): ShapeMeshData[] => {
let p1 = this.previewPoint(this.stepDatas[0].point!);
let l1 = this.getRayData(this.stepDatas[1].point!);
let l2 = this.getRayData(point);
return [shape, l1, l2];
let result = [p1, l1, this.previewPoint(this.stepDatas[1].point!)];
if (point) {
let shape = this.transformPreview(point);
let l2 = this.getRayData(point);
result.push(l2, shape);
}
return result;
};

private getRayData(end: XYZ) {
Expand All @@ -72,7 +77,11 @@ export class Rotate extends TransformedCommand {
return this.getTempLineData(start, e);
}

private linePreview = (point: XYZ): ShapeMeshData[] => {
return [this.getTempLineData(this.stepDatas[0].point!, point)];
private linePreview = (point: XYZ | undefined): ShapeMeshData[] => {
let p1 = this.previewPoint(this.stepDatas[0].point!);
if (!point) {
return [p1];
}
return [p1, this.getTempLineData(this.stepDatas[0].point!, point)];
};
}
23 changes: 22 additions & 1 deletion packages/chili/src/commands/multistepCommand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { AsyncController, CancelableCommand, Property } from "chili-core";
import {
AsyncController,
CancelableCommand,
Config,
EdgeMeshData,
LineType,
Property,
VertexMeshData,
XYZ,
} from "chili-core";
import { SnapedData } from "../snap";
import { IStep } from "../step";

Expand Down Expand Up @@ -58,6 +67,18 @@ export abstract class MultistepCommand extends CancelableCommand {
this.stepDatas.length = 0;
}

protected previewPoint(point: XYZ) {
return VertexMeshData.from(
point,
Config.instance.visual.editVertexSize,
Config.instance.visual.editVertexColor,
);
}

protected previewLine(start: XYZ, end: XYZ) {
return EdgeMeshData.from(start, end, Config.instance.visual.temporaryEdgeColor, LineType.Dash);
}

protected abstract getSteps(): IStep[];

protected abstract executeMainTask(): void;
Expand Down
2 changes: 1 addition & 1 deletion packages/chili/src/snap/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IDocument, IModel, IView, ShapeMeshData, VisualShapeData, XYZ } from "c

export type SnapValidator = (point: XYZ) => boolean;

export type SnapPreviewer = (point: XYZ) => ShapeMeshData[];
export type SnapPreviewer = (point: XYZ | undefined) => ShapeMeshData[];

export interface SnapedData {
view: IView;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { AsyncController, Config, IView, Plane, PlaneAngle, XYZ } from "chili-core";
import { AsyncController, Config, IDocument, IView, Plane, PlaneAngle, XYZ } from "chili-core";
import { SnapedData } from "../interfaces";
import { ObjectSnap } from "../objectSnap";
import { PlaneSnap } from "../planeSnap";
Expand All @@ -13,6 +13,7 @@ export class SnapAngleEventHandler extends SnapEventHandler {
readonly planeAngle: PlaneAngle;

constructor(
document: IDocument,
controller: AsyncController,
readonly center: () => XYZ,
p1: XYZ,
Expand All @@ -23,7 +24,7 @@ export class SnapAngleEventHandler extends SnapEventHandler {
let workplaneSnap = new PlaneSnap(snapPointData.plane, center);
let trackingSnap = new TrackingSnap(center, false);
let snaps = [objectSnap, trackingSnap, workplaneSnap];
super(controller, snaps, snapPointData);
super(document, controller, snaps, snapPointData);
let xvec = p1.sub(center()).normalize()!;
this.plane = new Plane(center(), snapPointData.plane().normal, xvec);
this.planeAngle = new PlaneAngle(this.plane);
Expand Down
Loading

0 comments on commit e84a37e

Please sign in to comment.