Skip to content

Commit

Permalink
🦄 refactor: refactor Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Apr 9, 2024
1 parent 72c74f3 commit e4e9816
Show file tree
Hide file tree
Showing 27 changed files with 197 additions and 178 deletions.
45 changes: 19 additions & 26 deletions packages/chili-core/src/model/entity.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { HistoryObservable, IEqualityComparer, Result } from "../foundation";
import { HistoryObservable } from "../foundation";
import { I18nKeys } from "../i18n";
import { IShape } from "../shape";
import { Matrix4 } from "../math";
import { Serializer } from "../serialize";

export abstract class Entity extends HistoryObservable {
protected shouldRegenerate: boolean = true;
abstract display: I18nKeys;

protected _shape: Result<IShape> = Result.err("Not initialised");
get shape(): Result<IShape> {
if (this.shouldRegenerate) {
this._shape = this.generateShape();
this.shouldRegenerate = false;
}
return this._shape;
protected _matrix: Matrix4 = Matrix4.identity();
@Serializer.serialze()
get matrix(): Matrix4 {
return this._matrix;
}

protected setPropertyAndUpdate<K extends keyof this>(
property: K,
newValue: this[K],
onPropertyChanged?: (property: K, oldValue: this[K]) => void,
equals?: IEqualityComparer<this[K]>,
) {
if (this.setProperty(property, newValue, onPropertyChanged, equals)) {
this.shouldRegenerate = true;
this.emitShapeChanged();
}
}

protected emitShapeChanged() {
this.emitPropertyChanged("shape", this._shape);
set matrix(value: Matrix4) {
this.setProperty(
"matrix",
value,
(_p, oldMatrix) => {
this.onMatrixChanged(value, oldMatrix);
},
{
equals: (left, right) => left.equals(right),
},
);
}

protected abstract generateShape(): Result<IShape>;
protected onMatrixChanged(newMatrix: Matrix4, oldMatrix: Matrix4): void {}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { IDocument } from "../document";
import { IEqualityComparer, Result } from "../foundation";
import { Matrix4 } from "../math";
import { Property } from "../property";
import { Serializer } from "../serialize";
import { IShape } from "../shape";
import { Entity } from "./entity";

export abstract class GeometryObject extends Entity {
export abstract class GeometryEntity extends Entity {
private _materialId: string;
@Serializer.serialze()
@Property.define("common.material", { type: "materialId" })
Expand All @@ -17,30 +19,48 @@ export abstract class GeometryObject extends Entity {
this.setProperty("materialId", value);
}

protected _matrix: Matrix4 = Matrix4.identity();
@Serializer.serialze()
get matrix(): Matrix4 {
return this._matrix;
protected shouldRegenerate: boolean = true;

protected _shape: Result<IShape> = Result.err("Not initialised");
get shape(): Result<IShape> {
if (this.shouldRegenerate) {
this._shape = this.generateShape();
if (this._shape.isOk) {
this._shape.value.matrix = this._matrix;
}
this.shouldRegenerate = false;
}
return this._shape;
}
set matrix(value: Matrix4) {
this.setProperty(
"matrix",
value,
() => {
if (this.shape.isOk) {
this.shape.value.matrix = value;
}
},
{
equals: (left, right) => left.equals(right),
},
);

protected setPropertyAndUpdate<K extends keyof this>(
property: K,
newValue: this[K],
onPropertyChanged?: (property: K, oldValue: this[K]) => void,
equals?: IEqualityComparer<this[K]>,
) {
if (this.setProperty(property, newValue, onPropertyChanged, equals)) {
this.shouldRegenerate = true;
this.emitShapeChanged();
}
}

constructor(document: IDocument, materialId?: string) {
super(document);
this._materialId = materialId ?? document.materials.at(0)!.id;
}

protected emitShapeChanged() {
this.emitPropertyChanged("shape", this._shape);
}

override onMatrixChanged(newMatrix: Matrix4, oldMatrix: Matrix4): void {
if (this.shape.isOk) {
this.shape.value.matrix = newMatrix;
}
}

protected abstract generateShape(): Result<IShape>;
}

/*
Expand Down Expand Up @@ -111,7 +131,7 @@ export abstract class HistoryBody extends Body {
}
*/

export abstract class FaceableGeometry extends GeometryObject {
export abstract class FaceableGeometry extends GeometryEntity {
protected _isFace: boolean = false;
@Serializer.serialze()
@Property.define("command.faceable.isFace")
Expand Down
2 changes: 1 addition & 1 deletion packages/chili-core/src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

export * from "./entity";
export * from "./feature";
export * from "./geometry";
export * from "./geometryEntity";
export * from "./model";
export * from "./node";
export * from "./nodeLinkedList";
8 changes: 4 additions & 4 deletions packages/chili-core/src/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
import { IDocument } from "../document";
import { Id } from "../foundation";
import { Serializer } from "../serialize";
import { GeometryObject } from "./geometry";
import { GeometryEntity } from "./geometryEntity";
import { IModel, IModelGroup, Node } from "./node";

export abstract class Model extends Node implements IModel {
@Serializer.serialze()
readonly geometry: GeometryObject;
readonly geometry: GeometryEntity;

constructor(document: IDocument, name: string, body: GeometryObject, id: string = Id.generate()) {
constructor(document: IDocument, name: string, body: GeometryEntity, id: string = Id.generate()) {
super(document, name, id);
this.geometry = body;
}
}

@Serializer.register("GeometryModel", ["document", "name", "geometry", "id"])
export class GeometryModel extends Model {
constructor(document: IDocument, name: string, geometry: GeometryObject, id: string = Id.generate()) {
constructor(document: IDocument, name: string, geometry: GeometryEntity, id: string = Id.generate()) {
super(document, name, geometry, id);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/chili-core/src/model/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IDocument } from "../document";
import { HistoryObservable, IDisposable, IPropertyChanged, Id } from "../foundation";
import { Property } from "../property";
import { Serialized, Serializer } from "../serialize";
import { GeometryObject } from "./geometry";
import { GeometryEntity } from "./geometryEntity";

export interface INode extends IPropertyChanged, IDisposable {
readonly id: string;
Expand All @@ -30,7 +30,7 @@ export interface INodeLinkedList extends INode {

export interface IModel extends INode {
readonly document: IDocument;
readonly geometry: GeometryObject;
readonly geometry: GeometryEntity;
}

export interface IModelGroup extends IModel {
Expand Down
4 changes: 2 additions & 2 deletions packages/chili-core/src/visual/visualShape.ts
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 { GeometryObject } from "../model";
import { GeometryEntity } from "../model";
import { ShapeType } from "../shape";
import { IVisualObject } from "./visualObject";

Expand Down Expand Up @@ -31,7 +31,7 @@ export interface VisualGroup {
}

export interface IVisualGeometry extends IVisualObject {
get geometry(): GeometryObject;
get geometryEngity(): GeometryEntity;
addState(state: VisualState, type: ShapeType, ...indexes: number[]): void;
removeState(state: VisualState, type: ShapeType, ...indexes: number[]): void;
resetState(): void;
Expand Down
59 changes: 19 additions & 40 deletions packages/chili-occ/src/occMesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
FaceMeshDataBuilder,
IShapeMeshData,
MeshDataBuilder,
VertexMeshData,
} from "chili-core";
import {
Handle_Poly_Triangulation,
Expand All @@ -16,7 +15,6 @@ import {
TopAbs_ShapeEnum,
TopoDS_Edge,
TopoDS_Face,
TopoDS_Shape,
gp_Trsf,
} from "../occ-wasm/chili_occ";

Expand All @@ -25,64 +23,43 @@ import { OccShape } from "./occShape";

export class OccMesh implements IShapeMeshData {
private maxDeviation: number = 8;
private _vertexs?: VertexMeshData;
private _lines?: EdgeMeshData;
private _faces?: FaceMeshData;

private constructor(readonly shape: OccShape) {
this._mesh(shape.shape);
}

static create(shape: OccShape): IShapeMeshData {
constructor(readonly shape: OccShape) {
new occ.BRepMesh_IncrementalMesh_2(shape.shape, 0.1, false, 0.1, false);
return new OccMesh(shape);
}

updateMeshShape(): void {
this.updateEdgeMeshShapes();
this.updateFaceMeshShapes();
}

get vertexs(): VertexMeshData | undefined {
return this._vertexs;
}
get edges(): EdgeMeshData | undefined {
if (!this._lines) this._lines = this.edgeMeshs();
return this._lines;
}

get faces(): FaceMeshData | undefined {
if (!this._faces) this._faces = this.faceMeshs();
return this._faces;
}

private _mesh(shape: TopoDS_Shape) {
let shapeType = shape.ShapeType();
if (shapeType === occ.TopAbs_ShapeEnum.TopAbs_VERTEX) {
this.pointMesh(shape);
return;
}
if (shapeType !== occ.TopAbs_ShapeEnum.TopAbs_EDGE) {
this.faceMeshs(shape);
}

this.edgeMeshs(shape);
}

private pointMesh(shape: TopoDS_Shape) {
console.log("暂不支持");
}

private edgeMeshs(shape: TopoDS_Shape) {
private edgeMeshs() {
let shapes = OccHelps.findSubShapes(
shape,
this.shape.shape,
occ.TopAbs_ShapeEnum.TopAbs_EDGE as TopAbs_ShapeEnum,
true,
);
let builder = new EdgeMeshDataBuilder();
for (const e of shapes) {
this.addEdgeMesh(e as TopoDS_Edge, builder);
}
this._lines = builder.build();
let matrix = OccHelps.convertToMatrix(shape.Location_1().Transformation()).invert()!;
this._lines.positions = matrix.ofPoints(this._lines.positions);
let lines = builder.build();
if (lines.positions.length === 0) return undefined;
let matrix = OccHelps.convertToMatrix(this.shape.shape.Location_1().Transformation()).invert()!;
lines.positions = matrix.ofPoints(lines.positions);
return lines;
}

private updateEdgeMeshShapes() {
Expand Down Expand Up @@ -117,20 +94,22 @@ export class OccMesh implements IShapeMeshData {
builder.endGroup(OccHelps.wrapShape(edge));
}

private faceMeshs(shape: TopoDS_Shape) {
private faceMeshs() {
let shapes = OccHelps.findSubShapes(
shape,
this.shape.shape,
occ.TopAbs_ShapeEnum.TopAbs_FACE as TopAbs_ShapeEnum,
true,
);
let builder = new FaceMeshDataBuilder();
for (const f of shapes) {
this.addFaceMesh(f as TopoDS_Face, builder);
}
this._faces = builder.build();
let matrix = OccHelps.convertToMatrix(shape.Location_1().Transformation()).invert()!;
this._faces.positions = matrix.ofPoints(this._faces.positions);
this._faces.normals = matrix.ofVectors(this._faces.normals);
let faces = builder.build();
if (faces.positions.length === 0) return undefined;
let matrix = OccHelps.convertToMatrix(this.shape.shape.Location_1().Transformation()).invert()!;
faces.positions = matrix.ofPoints(faces.positions);
faces.normals = matrix.ofVectors(faces.normals);
return faces;
}

private updateFaceMeshShapes() {
Expand Down
2 changes: 1 addition & 1 deletion packages/chili-occ/src/occShape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class OccShape implements IShape {
private _mesh: IShapeMeshData | undefined;
get mesh(): IShapeMeshData {
if (this._mesh === undefined) {
this._mesh = OccMesh.create(this);
this._mesh = new OccMesh(this);
}
return this._mesh;
}
Expand Down
Loading

0 comments on commit e4e9816

Please sign in to comment.