Skip to content

Commit f8d7b4d

Browse files
committed
🦄 refactor: enhance scalability
1 parent 656efb2 commit f8d7b4d

File tree

16 files changed

+52
-28
lines changed

16 files changed

+52
-28
lines changed

packages/chili-core/src/math/ray.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ export class Ray {
3131
return vec.isParallelTo(right.direction) ? result : undefined;
3232
}
3333

34+
distanceTo(right: Ray): number {
35+
const neareast1 = this.nearestTo(right);
36+
const neareast2 = this.nearestToPoint(neareast1);
37+
return neareast1.distanceTo(neareast2);
38+
}
39+
3440
nearestTo(right: Ray): XYZ {
3541
const n = right.direction.cross(this.direction).normalize();
3642
if (n === undefined) return this.nearestToPoint(right.location);

packages/chili-core/src/visual/visualContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ export interface IVisualContext extends IDisposable, INodeChangedObserver {
2020
redrawNode(nodes: INode[]): void;
2121
setVisible(node: INode, visible: boolean): void;
2222
visuals(): IVisualObject[];
23-
displayMesh(...datas: ShapeMeshData[]): number;
23+
displayMesh(datas: ShapeMeshData[], opacity?: number): number;
2424
removeMesh(id: number): void;
2525
}

packages/chili-three/src/threeGeometryFactory.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ export class ThreeGeometryFactory {
3131
return new Points(buff, material);
3232
}
3333

34-
static createFaceGeometry(data: FaceMeshData) {
34+
static createFaceGeometry(data: FaceMeshData, opacity?: number) {
3535
let buff = ThreeGeometryFactory.createFaceBufferGeometry(data);
3636
let material = new MeshLambertMaterial({ side: DoubleSide });
37+
if (opacity !== undefined) {
38+
material.transparent = true;
39+
material.opacity = opacity;
40+
}
3741
this.setColor(buff, data, material);
3842

3943
return new Mesh(buff, material);

packages/chili-three/src/threeVisual.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@ export class ThreeVisual implements IVisual {
3131

3232
constructor(readonly document: IDocument) {
3333
this.scene = this.initScene();
34-
this.defaultEventHandler = new NodeSelectionHandler(document, true);
34+
this.defaultEventHandler = this.createDefaultSelectionHandler(document);
3535
this.viewHandler = new ThreeViewHandler();
3636
this.context = new ThreeVisualContext(this, this.scene);
3737
this.highlighter = new ThreeHighlighter(this.context);
3838
this._eventHandler = this.defaultEventHandler;
3939
}
4040

41+
protected createDefaultSelectionHandler(document: IDocument) {
42+
return new NodeSelectionHandler(document, true);
43+
}
44+
4145
initScene() {
4246
let scene = new Scene();
4347
let envLight = new AmbientLight(0x888888, 4);

packages/chili-three/src/threeVisualContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,15 @@ export class ThreeVisualContext implements IVisualContext {
238238
}
239239
}
240240

241-
displayMesh(...datas: ShapeMeshData[]): number {
241+
displayMesh(datas: ShapeMeshData[], opacity?: number): number {
242242
let group = new Group();
243243
datas.forEach((data) => {
244244
if (ShapeMeshData.isVertex(data)) {
245245
group.add(ThreeGeometryFactory.createVertexGeometry(data));
246246
} else if (ShapeMeshData.isEdge(data)) {
247247
group.add(ThreeGeometryFactory.createEdgeGeometry(data));
248248
} else if (ShapeMeshData.isFace(data)) {
249-
group.add(ThreeGeometryFactory.createFaceGeometry(data));
249+
group.add(ThreeGeometryFactory.createFaceGeometry(data, opacity));
250250
}
251251
});
252252
this.tempShapes.add(group);

packages/chili-vis/src/nodeSelectionEventHandler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export class NodeSelectionHandler extends SelectionHandler {
1818
private _highlights: IVisualObject[] | undefined;
1919
private _detectAtMouse: IVisualObject[] | undefined;
2020
private _lockDetected: IVisualObject | undefined; // 用于切换捕获的对象
21+
protected highlighState = VisualState.edgeHighlight;
2122

2223
nodes(): VisualNode[] {
2324
return this.document.selection.getSelectedNodes() as VisualNode[];
@@ -87,15 +88,15 @@ export class NodeSelectionHandler extends SelectionHandler {
8788
private highlightDetecteds(view: IView, detecteds: IVisualObject[]) {
8889
this.cleanHighlights();
8990
detecteds.forEach((x) => {
90-
view.document.visual.highlighter.addState(x, VisualState.edgeHighlight, ShapeType.Shape);
91+
view.document.visual.highlighter.addState(x, this.highlighState, ShapeType.Shape);
9192
});
9293
this._highlights = detecteds;
9394
view.update();
9495
}
9596

9697
protected override cleanHighlights(): void {
9798
this._highlights?.forEach((x) => {
98-
this.document.visual.highlighter.removeState(x, VisualState.edgeHighlight, ShapeType.Shape);
99+
this.document.visual.highlighter.removeState(x, this.highlighState, ShapeType.Shape);
99100
});
100101
this._highlights = undefined;
101102
}

packages/chili/src/commands/measure/angle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ export class AngleMeasure extends MultistepCommand {
117117
.rotate(normal, rad * 0.5)!
118118
.multiply(this.lineLength(this.stepDatas[2].point) * ARC_POSITION)
119119
.add(this.stepDatas[0].point!);
120-
const visualId = this.document.visual.context.displayMesh(
120+
const visualId = this.document.visual.context.displayMesh([
121121
this.meshPoint(this.stepDatas[2].point!),
122122
...this.arcPreview(this.stepDatas[2].point),
123-
);
123+
]);
124124
this.application.activeView?.htmlText(((rad * 180) / Math.PI).toFixed(2) + "°", arcMid, {
125125
onDispose: () => {
126126
this.document.visual.context.removeMesh(visualId);

packages/chili/src/commands/measure/length.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ export class LengthMeasure extends MultistepCommand {
3939
const firstPoint = this.stepDatas[0].point!;
4040
const secondPoint = this.stepDatas[1].point!;
4141
const distance = firstPoint.distanceTo(secondPoint);
42-
const visualId = this.document.visual.context.displayMesh(
42+
const visualId = this.document.visual.context.displayMesh([
4343
this.meshPoint(firstPoint),
4444
this.meshLine(firstPoint, secondPoint, VisualConfig.highlightEdgeColor, 3),
4545
this.meshPoint(secondPoint),
46-
);
46+
]);
4747
this.application.activeView?.htmlText(
4848
distance.toFixed(2),
4949
firstPoint.add(secondPoint).multiply(0.5),

packages/chili/src/commands/measure/select.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export class SelectMeasure extends CancelableCommand {
159159
mesh.lineWidth = 3;
160160
mesh.color = VisualConfig.highlightEdgeColor;
161161

162-
const id = this.document.visual.context.displayMesh(mesh);
162+
const id = this.document.visual.context.displayMesh([mesh]);
163163
this.#disposeSet.add(
164164
this.application.activeView!.htmlText(length.toFixed(2), start.add(end).multiply(0.5), {
165165
hideDelete: true,
@@ -181,7 +181,7 @@ export class SelectMeasure extends CancelableCommand {
181181
const area = face.area();
182182
this.addSumItem(area);
183183
const center = this.wireCenter(mesh.position);
184-
const id = this.document.visual.context.displayMesh(mesh);
184+
const id = this.document.visual.context.displayMesh([mesh]);
185185
this.#disposeSet.add(
186186
this.application.activeView!.htmlText(area.toFixed(2), center, {
187187
hideDelete: true,
@@ -215,7 +215,7 @@ export class SelectMeasure extends CancelableCommand {
215215

216216
const volume = solid.volume();
217217
this.addSumItem(volume);
218-
const id = this.document.visual.context.displayMesh(mesh);
218+
const id = this.document.visual.context.displayMesh([mesh]);
219219
this.#disposeSet.add(
220220
this.application.activeView!.htmlText(volume.toFixed(2), transform.ofPoint(center), {
221221
hideDelete: true,

packages/chili/src/snap/handlers/pointSnapEventHandler.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import {
88
ICurve,
99
IDocument,
1010
IView,
11-
Matrix4,
1211
Plane,
13-
XYZ,
12+
XYZ
1413
} from "chili-core";
1514
import { Dimension } from "../dimension";
16-
import { SnapData, SnapResult } from "../snap";
15+
import { ISnap, SnapData, SnapResult } from "../snap";
1716
import { ObjectSnap, PlaneSnap, PointOnCurveSnap, WorkplaneSnap } from "../snaps";
1817
import { TrackingSnap } from "../tracking";
1918
import { SnapEventHandler } from "./snapEventHandler";
@@ -30,12 +29,17 @@ export interface SnapPointOnCurveData extends PointSnapData {
3029

3130
export class PointSnapEventHandler extends SnapEventHandler<PointSnapData> {
3231
constructor(document: IDocument, controller: AsyncController, pointData: PointSnapData) {
32+
super(document, controller, [], pointData);
33+
this.snaps.push(...this.getInitSnaps(pointData));
34+
}
35+
36+
protected getInitSnaps(pointData: PointSnapData): ISnap[] {
3337
const objectSnap = new ObjectSnap(Config.instance.snapType, pointData.refPoint);
3438
const workplaneSnap = pointData.plane
3539
? new PlaneSnap(pointData.plane, pointData.refPoint)
3640
: new WorkplaneSnap(pointData.refPoint);
3741
const trackingSnap = new TrackingSnap(pointData.refPoint, true);
38-
super(document, controller, [objectSnap, trackingSnap, workplaneSnap], pointData);
42+
return [objectSnap, trackingSnap, workplaneSnap]
3943
}
4044

4145
protected getPointFromInput(view: IView, text: string): SnapResult {

packages/chili/src/snap/handlers/snapEventHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,12 @@ export abstract class SnapEventHandler<D extends SnapData = SnapData> implements
189189
VisualConfig.temporaryVertexSize,
190190
VisualConfig.temporaryVertexColor,
191191
);
192-
this._tempPoint = this.document.visual.context.displayMesh(data);
192+
this._tempPoint = this.document.visual.context.displayMesh([data]);
193193
}
194194

195195
this._tempShapes = this.data
196196
.preview?.(point)
197-
?.map((shape) => this.document.visual.context.displayMesh(shape));
197+
?.map((shape) => this.document.visual.context.displayMesh([shape]));
198198
}
199199

200200
private removeTempShapes() {

packages/chili/src/snap/snaps/axisSnap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class AxisSnap implements ISnap {
4343
VisualConfig.temporaryEdgeColor,
4444
LineType.Dash,
4545
);
46-
const id = view.document.visual.context.displayMesh(lineDats);
46+
const id = view.document.visual.context.displayMesh([lineDats]);
4747
this._tempLines = [view, id];
4848
}
4949

packages/chili/src/snap/snaps/objectSnap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class ObjectSnap extends BaseSnap {
125125
VisualConfig.hintVertexSize,
126126
VisualConfig.hintVertexColor,
127127
);
128-
this._hintVertex = [view.document.visual.context, view.document.visual.context.displayMesh(data)];
128+
this._hintVertex = [view.document.visual.context, view.document.visual.context.displayMesh([data])];
129129
}
130130

131131
private snapeInvisible(view: IView, x: number, y: number): SnapResult | undefined {
@@ -175,7 +175,7 @@ export class ObjectSnap extends BaseSnap {
175175
VisualConfig.hintVertexSize,
176176
VisualConfig.hintVertexColor,
177177
);
178-
let id = view.document.visual.context.displayMesh(temporary);
178+
let id = view.document.visual.context.displayMesh([temporary]);
179179
this._invisibleInfos.set(shape, {
180180
view,
181181
snaps: [

packages/chili/src/snap/tracking/trackingBase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export abstract class TrackingBase {
3333

3434
protected displayPoint(document: IDocument, point: SnapResult, size: number, color: number): number {
3535
const data = VertexMeshData.from(point.point!, size, color);
36-
const id = document.visual.context.displayMesh(data);
36+
const id = document.visual.context.displayMesh([data]);
3737
this.addTempMesh(document, id);
3838
return id;
3939
}

packages/chili/src/snap/tracking/trackingSnap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class TrackingSnap implements ISnap {
9898
const distance = Math.min(vector.length() * 1e10, 1e20);
9999
const newEnd = start.add(normal.multiply(distance));
100100
const lineDats = EdgeMeshData.from(start, newEnd, VisualConfig.temporaryEdgeColor, LineType.Dash);
101-
return view.document.visual.context.displayMesh(lineDats);
101+
return view.document.visual.context.displayMesh([lineDats]);
102102
}
103103

104104
private shapeIntersectTracking(

packages/chili/src/step/step.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
// Part of the Chili3d Project, under the AGPL-3.0 License.
22
// See LICENSE file in the project root for full license information.
33

4-
import { AsyncController, I18nKeys, IDocument, XYZ } from "chili-core";
4+
import { AsyncController, CursorType, I18nKeys, IDocument, XYZ } from "chili-core";
55
import { SnapData, SnapEventHandler, SnapResult } from "../snap";
66

77
export interface IStep {
88
execute(document: IDocument, controller: AsyncController): Promise<SnapResult | undefined>;
99
}
1010

1111
export abstract class SnapStep<D extends SnapData> implements IStep {
12+
protected cursor: CursorType = "draw";
13+
1214
constructor(
1315
readonly tip: I18nKeys,
1416
private readonly handleStepData: () => D,
@@ -25,9 +27,12 @@ export abstract class SnapStep<D extends SnapData> implements IStep {
2527
this.setValidator(data);
2628

2729
const executorHandler = this.getEventHandler(document, controller, data);
28-
await document.selection.pickAsync(executorHandler, this.tip, controller, false, "draw");
30+
await document.selection.pickAsync(executorHandler, this.tip, controller, false, this.cursor);
31+
const snaped = executorHandler.snaped;
32+
33+
executorHandler.dispose();
2934

30-
return controller.result?.status === "success" ? executorHandler.snaped : undefined;
35+
return controller.result?.status === "success" ? snaped : undefined;
3136
}
3237

3338
private setValidator(data: D) {

0 commit comments

Comments
 (0)