Skip to content

Commit

Permalink
feat: first working version of exportable wall corners
Browse files Browse the repository at this point in the history
  • Loading branch information
agviegas committed Oct 9, 2024
1 parent f5551d9 commit 6f14cbe
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 179 deletions.
119 changes: 26 additions & 93 deletions packages/clay/src/elements/Walls/SimpleWall/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,101 +49,41 @@ const simpleWallType = new CLAY.SimpleWallType(model);
const wall1 = simpleWallType.addInstance();
world.scene.three.add(...wall1.meshes);
wall1.startPoint = new THREE.Vector2(0, 0);
wall1.endPoint = new THREE.Vector2(0, 1);
wall1.endPoint = new THREE.Vector2(1, 0);
wall1.update(true);
wall1.meshes[0].setColorAt(0, new THREE.Color(1, 0, 0));
//
// const wall2 = simpleWallType.addInstance();
// world.scene.three.add(...wall2.meshes);
// wall2.startPoint = new THREE.Vector2(0, 0);
// wall2.endPoint = new THREE.Vector2(0, 1);
// wall2.update(true);

const wall2 = simpleWallType.addInstance();
world.scene.three.add(...wall2.meshes);
wall2.startPoint = new THREE.Vector2(0, 0);
wall2.endPoint = new THREE.Vector2(0, 1);
wall2.update(true);

site.children.add(wall1.attributes.expressID);
// site.children.add(wall2.attributes.expressID);
site.children.add(wall2.attributes.expressID);

simpleWallType.addCorner({
wall1,
wall2,
to: "exterior",
cut: "exterior",
cutDirection: "interior",
priority: "start",
});

// simpleWallType.addCorner({
// wall1,
// wall2,
// to: "interior",
// cut: "interior",
// cutDirection: "interior",
// priority: "start",
// });
simpleWallType.addCorner({
wall1: wall2,
wall2: wall1,
to: "exterior",
cut: "exterior",
cutDirection: "exterior",
priority: "start",
});

simpleWallType.updateCorners();

world.camera.controls.fitToSphere(wall1.meshes[0], false);

// const simpleOpeningType = new CLAY.SimpleOpeningType(model);
// const opening = simpleOpeningType.addInstance();
// // world.scene.three.add(...opening.meshes);
// console.log(simpleOpeningType);
// opening.transformation.position.x += 1;
//
// await wall1.addSubtraction(opening, true);
// wall1.update(true);

const test = new THREE.Mesh(
new THREE.PlaneGeometry(),
new THREE.MeshLambertMaterial({
color: "blue",
transparent: true,
opacity: 0.3,
side: 2,
}),
);

const wallAxis = new THREE.AxesHelper();
wallAxis.material.depthTest = false;
wallAxis.material.transparent = true;
wall1.transformation.add(wallAxis);
world.scene.three.add(wall1.transformation);

world.scene.three.add(test);
test.position.set(0.5, 0.5, 0.5);
test.lookAt(0, 0, 0);
test.updateMatrix();

const halfSpace = new CLAY.HalfSpace(model);
wall1.body.addSubtraction(halfSpace);

function updatePlane() {
const delta = 0.000000001;
const vector = new THREE.Vector3(0, 0, 1);

const planeRotation = new THREE.Matrix4();
planeRotation.extractRotation(test.matrix);
vector.applyMatrix4(planeRotation);

wall1.transformation.updateMatrix();
const rotation = new THREE.Matrix4();
const inverseWall = wall1.transformation.matrix.clone();
inverseWall.invert();
rotation.extractRotation(inverseWall);
vector.applyMatrix4(rotation);

const position = test.position.clone();
position.applyMatrix4(inverseWall);

halfSpace.transformation.position.copy(position);
halfSpace.direction.copy(vector).normalize();

halfSpace.update();
wall1.update(true);
}

updatePlane();

function animate() {
test.rotation.x += Math.PI / 180;
test.updateMatrix();
updatePlane();
requestAnimationFrame(animate);
}

animate();

// Stats

const stats = new Stats();
Expand Down Expand Up @@ -171,7 +111,6 @@ const panel = BUI.Component.create<BUI.PanelSection>(() => {
) => {
wall1.startPoint.x = event.target.value;
wall1.update(true);
updatePlane();
simpleWallType.updateCorners();
// simpleWallType.updateCorners();
Expand All @@ -182,7 +121,6 @@ const panel = BUI.Component.create<BUI.PanelSection>(() => {
) => {
wall1.startPoint.y = event.target.value;
wall1.update(true);
updatePlane();
simpleWallType.updateCorners();
console.log("hey");
Expand All @@ -199,7 +137,6 @@ const panel = BUI.Component.create<BUI.PanelSection>(() => {
) => {
wall1.endPoint.x = event.target.value;
wall1.update(true);
updatePlane();
simpleWallType.updateCorners();
// simpleWallType.updateCorners();
Expand All @@ -210,7 +147,6 @@ const panel = BUI.Component.create<BUI.PanelSection>(() => {
) => {
wall1.endPoint.y = event.target.value;
wall1.update(true);
updatePlane();
simpleWallType.updateCorners();
// simpleWallType.updateCorners();
Expand All @@ -224,7 +160,6 @@ const panel = BUI.Component.create<BUI.PanelSection>(() => {
) => {
wall1.transformation.position.y = event.target.value;
wall1.update(true);
updatePlane();
simpleWallType.updateCorners();
}}"></bim-number-input>
Expand All @@ -233,7 +168,6 @@ const panel = BUI.Component.create<BUI.PanelSection>(() => {
) => {
wall1.offset = event.target.value;
wall1.update(true);
updatePlane();
simpleWallType.updateCorners();
// simpleWallType.updateCorners();
Expand All @@ -244,15 +178,14 @@ const panel = BUI.Component.create<BUI.PanelSection>(() => {
) => {
simpleWallType.width = event.target.value;
simpleWallType.update(true);
// simpleWallType.updateCorners();
simpleWallType.updateCorners();
}}"></bim-number-input>
<bim-number-input slider step="0.05" label="Height" value="${wall1.height}" @change="${(
event: any,
) => {
wall1.height = event.target.value;
wall1.update(true);
updatePlane();
simpleWallType.updateCorners();
// simpleWallType.updateCorners();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ interface WallCorner extends WallCornerConfig {
export class SimpleWallCornerer {
list = new Map<number, Map<number, WallCorner>>();

private _temp = new THREE.Object3D();

add(config: WallCornerConfig) {
const id1 = config.wall1.attributes.expressID;
const id2 = config.wall2.attributes.expressID;
Expand Down Expand Up @@ -58,14 +60,16 @@ export class SimpleWallCornerer {
// B) Each point of the wall are on one side of this wall
// In that case, keep the point specified in priority

wall1.transformation.updateMatrix();

const dir1 = wall1.direction;
const dir2 = wall2.direction;
if (dir1.equals(dir2)) {
// Same direction, so walls can't intersect
return;
}

const { plane } = wall1.getPlane(to);
const { plane, p1 } = wall1.getPlane(to);
if (plane === null) {
// Malformed wall (e.g. zero length)
return;
Expand All @@ -77,92 +81,68 @@ export class SimpleWallCornerer {
const extendStart = priority === "start";

const pointToExtend = extendStart ? start : end;
if (plane.distanceToPoint(pointToExtend) === 0) {
if (plane.distanceToPoint(pointToExtend) !== 0) {
// Point is already aligned with wall
return;
}
const origin = extendStart ? end : start;
const direction = extendStart ? start : end;
direction.sub(origin);

const origin = extendStart ? end : start;
const direction = extendStart ? start : end;
direction.sub(origin);
const ray = new THREE.Ray(origin, direction);

const ray = new THREE.Ray(origin, direction);
const intersection = ray.intersectPlane(plane, new THREE.Vector3());

const intersection = ray.intersectPlane(plane, new THREE.Vector3());
if (intersection === null) {
return;
}

if (intersection === null) {
return;
const offsetDist = offset === "auto" ? wall2.type.width : offset;
const factor = extendStart ? -1 : 1;
const offsetVec = dir2.multiplyScalar(offsetDist * factor);
intersection.add(offsetVec);

const extended = extendStart ? wall2.startPoint : wall2.endPoint;
extended.x = intersection.x;
extended.y = intersection.z;

wall2.update();
}

const offsetDist = offset === "auto" ? wall2.type.width : offset;
const factor = extendStart ? -1 : 1;
const offsetVec = dir2.multiplyScalar(offsetDist * factor);
intersection.add(offsetVec);

const extended = extendStart ? wall2.startPoint : wall2.endPoint;
extended.x = intersection.x;
extended.y = intersection.z;

// if (corner.cut && corner.cutDirection) {
// if (!corner.halfSpace) {
// const halfSpace = new HalfSpace(wall1.model);
// corner.halfSpace = halfSpace;
// wall2.body.addSubtraction(halfSpace);
// }
//
// const halfSpace = corner.halfSpace as HalfSpace;
//
// const temp = new THREE.Mesh(
// new THREE.PlaneGeometry(10, 10, 10),
// new THREE.MeshLambertMaterial({
// color: "blue",
// }),
// );
//
// const temp2 = new THREE.Object3D();
// temp2.position.copy(p1);
// const p4 = p1.clone().add(plane.normal);
// temp2.lookAt(p4);
//
// temp2.applyMatrix4(new THREE.Matrix4().makeRotationX(-Math.PI / 2));
// temp2.applyMatrix4(new THREE.Matrix4().makeRotationY(Math.PI / 2));
// temp2.applyMatrix4(new THREE.Matrix4().makeRotationX(Math.PI / 2));
//
// temp.applyMatrix4(temp2.matrix);
//
// // temp.updateMatrix();
//
// const temp3 = new THREE.Object3D();
// const transform = wall2.transformation.matrix.clone();
// transform.invert();
//
// temp3.applyMatrix4(transform);
// temp3.applyMatrix4(new THREE.Matrix4().makeRotationZ(Math.PI / 2));
// temp.applyMatrix4(temp3.matrix);
//
// // const temp4 = new THREE.Object3D();
// // temp4.position.copy(p1);
// // temp4.applyMatrix4(transform);
//
// // const obj = MathUtils.getTempObject3DToDisplayIfcCoords();
// // obj.add(temp);
// // wall1.meshes[0].parent.add(obj);
//
// // halfSpace.position.copy(temp.position);
// // halfSpace.rotation.copy(temp.rotation);
//
// // const transform = wall2.getTransform();
// // transform.invert();
// // halfSpace.applyTransform(transform);
//
// // halfSpace.rotation.y = Math.PI / 2;
// // halfSpace.position.x = 0.25;
//
// halfSpace.transformation.rotation.copy(temp.rotation);
// halfSpace.transformation.position.copy(temp.position);
//
// halfSpace.update();
// }
if (corner.cut && corner.cutDirection) {
if (!corner.halfSpace) {
const halfSpace = new HalfSpace(wall1.model);
corner.halfSpace = halfSpace;
wall2.body.addSubtraction(halfSpace);
}

const halfSpace = corner.halfSpace as HalfSpace;

const rotation = new THREE.Vector3(0, 0, 1);

this._temp.position.copy(p1);
const factor = corner.cutDirection === "interior" ? -1 : 1;
const minusNormal = plane.normal.clone().multiplyScalar(factor);
this._temp.lookAt(p1.clone().add(minusNormal));
this._temp.updateMatrix();

const planeRotation = new THREE.Matrix4();
planeRotation.extractRotation(this._temp.matrix);
rotation.applyMatrix4(planeRotation);

wall2.transformation.updateMatrix();
const wallRotation = new THREE.Matrix4();
const inverseWall = wall2.transformation.matrix.clone();
inverseWall.invert();
wallRotation.extractRotation(inverseWall);
rotation.applyMatrix4(wallRotation);

const position = this._temp.position.clone();
position.applyMatrix4(inverseWall);

halfSpace.transformation.position.copy(position);
halfSpace.direction.copy(rotation).normalize();

halfSpace.update();
}

wall2.update(true);
}
Expand Down
12 changes: 6 additions & 6 deletions packages/clay/src/elements/Walls/SimpleWall/src/simple-wall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class SimpleWall extends ClayElement {

get normal() {
const direction = this.direction;
const up = new THREE.Vector3(0, 0, 1);
const up = new THREE.Vector3(0, 1, 0);
return direction.cross(up);
}

Expand Down Expand Up @@ -155,12 +155,12 @@ export class SimpleWall extends ClayElement {
return { p1, p2, p3, plane };
}

const offsetCorrection = normal.clone();
offsetCorrection.multiplyScalar(-this.offset);
// const offsetCorrection = normal.clone();
// offsetCorrection.multiplyScalar(-this.offset);

p1.add(offsetCorrection);
p2.add(offsetCorrection);
p3.add(offsetCorrection);
// p1.add(offsetCorrection);
// p2.add(offsetCorrection);
// p3.add(offsetCorrection);

if (type !== "center") {
const offset = normal.clone();
Expand Down

0 comments on commit 6f14cbe

Please sign in to comment.