Skip to content

Commit

Permalink
Merge pull request #9 from andrewisen-tikab/feature
Browse files Browse the repository at this point in the history
Add `updateVertex`
  • Loading branch information
andrewisen-tikab authored Jul 16, 2023
2 parents 7ae9cf2 + 93b455a commit 106e417
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
58 changes: 49 additions & 9 deletions src/Shape3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ export default class Shape3D extends Shape3DCore {
setFromPoints(points: THREE.Vector3[]): Shape3D {
const vertices: Vertex[] = points.map((point) => point.toArray());
this.vertices = vertices;
this.update();
this.updateGeometry();
return this;
}

setPrimaryColor(color: ColorRepresentation) {
this.primaryColor = new THREE.Color(color);
this.update();
this.updateMaterial();
}

setSecondaryColor(color: ColorRepresentation) {
Expand All @@ -108,7 +108,7 @@ export default class Shape3D extends Shape3DCore {
this.secondaryColor.set(color);
}

this.update();
this.updateMaterial();
}

setCloseLine(closeLine: boolean) {
Expand Down Expand Up @@ -136,6 +136,46 @@ export default class Shape3D extends Shape3DCore {
}
}

/**
* Update geometry for existing (!) shape.
*/
public updateGeometry(): void {
if (this.vertices.length === 0) return;
switch (this.shape) {
case SUPPORTED_SHAPES.LINE:
this.line === null ? this.createLine() : this.updateLineGeometry();
break;
case SUPPORTED_SHAPES.AREA:
this.area === null ? this.createArea() : this.updateAreaGeometry();
break;
case SUPPORTED_SHAPES.VOLUME:
this.volume === null ? this.createVolume() : this.updateVolumeGeometry();
break;
default:
break;
}
}

/**
* Update material for existing (!) shape.
*/
public updateMaterial(): void {
if (this.vertices.length === 0) return;
switch (this.shape) {
case SUPPORTED_SHAPES.LINE:
this.line === null ? this.createLine() : this.updateLineMaterial();
break;
case SUPPORTED_SHAPES.AREA:
this.area === null ? this.createArea() : this.updateAreaMaterial();
break;
case SUPPORTED_SHAPES.VOLUME:
this.volume === null ? this.createVolume() : this.updateVolumeMaterial();
break;
default:
break;
}
}

private checkLine() {
if (this.shape !== SUPPORTED_SHAPES.LINE) throw new Error('Shape is not "line"');
}
Expand Down Expand Up @@ -175,7 +215,7 @@ export default class Shape3D extends Shape3DCore {
}

/**
* Must be called by `updateLine`.
* N.B: Must be called by an `update` method!
*/
private updateLineGeometry(): void {
this.checkLine();
Expand All @@ -195,7 +235,7 @@ export default class Shape3D extends Shape3DCore {
}

/**
* Must be called by `updateLine`.
* N.B: Must be called by an `update` method!
*/
private updateLineMaterial(): void {
this.checkLine();
Expand Down Expand Up @@ -230,7 +270,7 @@ export default class Shape3D extends Shape3DCore {
}

/**
* Must be called by `updateArea`.
* N.B: Must be called by an `update` method!
*/
private updateAreaGeometry(): void {
this.checkArea();
Expand All @@ -247,7 +287,7 @@ export default class Shape3D extends Shape3DCore {
}

/**
* Must be called by `updateArea`.
* N.B: Must be called by an `update` method!
*/
private updateAreaMaterial(): void {
this.checkArea();
Expand Down Expand Up @@ -283,7 +323,7 @@ export default class Shape3D extends Shape3DCore {
}

/**
* Must be called by `updateVolume`.
* N.B: Must be called by an `update` method!
*/
private updateVolumeGeometry(): void {
this.checkVolume();
Expand All @@ -304,7 +344,7 @@ export default class Shape3D extends Shape3DCore {
}

/**
* Must be called by `updateArea`.
* N.B: Must be called by an `update` method!
*/
private updateVolumeMaterial(): void {
this.checkVolume();
Expand Down
9 changes: 8 additions & 1 deletion src/controls/TransformShapeControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,13 @@ class TransformShapeControls extends THREE.Object3D {
// Simply copy the raycast point and adjust for the Shape3D's position.
this.lastSelectedVertex.position.copy(planeIntersect.point).sub(this.object!.position);

// Update the vertex position in the Shape3D object
const index = +this.lastSelectedVertex.name;
this.object!.updateVertex(index, [
this.lastSelectedVertex.position.x,
this.lastSelectedVertex.position.y,
this.lastSelectedVertex.position.z,
]);
return;
}

Expand Down Expand Up @@ -754,7 +761,7 @@ class TransformShapeControls extends THREE.Object3D {
this.object!.getVertices().forEach((vertex, index) => {
const cube = new THREE.Mesh(geometry, material);
cube.position.set(vertex[0], vertex[1], vertex[2]);
cube.name = 'vertex ' + index;
cube.name = `${index}`;
this.vertexGroup.add(cube);
});
this.vertexGroup.position.set(-this.position.x, -this.position.y, -this.position.z);
Expand Down
9 changes: 9 additions & 0 deletions src/core/Shape3DCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ export default class Shape3DCore extends THREE.Object3D {
return this.vertices;
}

public updateVertex(index: number, vertex: Vertex): void {
this.vertices[index] = vertex;
this.updateGeometry();
}

public update(): void {
throw new Error('Method not implemented.');
}

public updateGeometry(): void {}

public updateMaterial(): void {}
}

0 comments on commit 106e417

Please sign in to comment.