Refactor drawWall function for improved readability and maintainability
This commit is contained in:
@@ -14,234 +14,205 @@ import { Socket } from "socket.io-client";
|
|||||||
import { getUserData } from "../../../../functions/getUserData";
|
import { getUserData } from "../../../../functions/getUserData";
|
||||||
|
|
||||||
async function drawWall(
|
async function drawWall(
|
||||||
raycaster: THREE.Raycaster,
|
raycaster: THREE.Raycaster,
|
||||||
plane: Types.RefMesh,
|
plane: Types.RefMesh,
|
||||||
floorPlanGroupPoint: Types.RefGroup,
|
floorPlanGroupPoint: Types.RefGroup,
|
||||||
snappedPoint: Types.RefVector3,
|
snappedPoint: Types.RefVector3,
|
||||||
isSnapped: Types.RefBoolean,
|
isSnapped: Types.RefBoolean,
|
||||||
isSnappedUUID: Types.RefString,
|
isSnappedUUID: Types.RefString,
|
||||||
line: Types.RefLine,
|
line: Types.RefLine,
|
||||||
ispreSnapped: Types.RefBoolean,
|
ispreSnapped: Types.RefBoolean,
|
||||||
anglesnappedPoint: Types.RefVector3,
|
anglesnappedPoint: Types.RefVector3,
|
||||||
isAngleSnapped: Types.RefBoolean,
|
isAngleSnapped: Types.RefBoolean,
|
||||||
lines: Types.RefLines,
|
lines: Types.RefLines,
|
||||||
floorPlanGroupLine: Types.RefGroup,
|
floorPlanGroupLine: Types.RefGroup,
|
||||||
floorPlanGroup: Types.RefGroup,
|
floorPlanGroup: Types.RefGroup,
|
||||||
ReferenceLineMesh: Types.RefMesh,
|
ReferenceLineMesh: Types.RefMesh,
|
||||||
LineCreated: Types.RefBoolean,
|
LineCreated: Types.RefBoolean,
|
||||||
currentLayerPoint: Types.RefMeshArray,
|
currentLayerPoint: Types.RefMeshArray,
|
||||||
dragPointControls: Types.RefDragControl,
|
dragPointControls: Types.RefDragControl,
|
||||||
setNewLines: any,
|
setNewLines: any,
|
||||||
setDeletedLines: any,
|
setDeletedLines: any,
|
||||||
activeLayer: Types.Number,
|
activeLayer: Types.Number,
|
||||||
socket: Socket<any>,
|
socket: Socket<any>,
|
||||||
projectId?: string,
|
projectId?: string,
|
||||||
versionId?: string,
|
versionId?: string,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const { userId, organization, email } = getUserData();
|
const { userId, organization } = getUserData();
|
||||||
////////// Creating lines Based on the positions clicked //////////
|
////////// Creating lines Based on the positions clicked //////////
|
||||||
|
|
||||||
////////// Allows the user lines that represents walls and roof, floor if forms a polygon //////////
|
////////// Allows the user lines that represents walls and roof, floor if forms a polygon //////////
|
||||||
|
|
||||||
if (!plane.current) return;
|
if (!plane.current) return;
|
||||||
let intersects = raycaster.intersectObject(plane.current, true);
|
let intersects = raycaster.intersectObject(plane.current, true);
|
||||||
|
|
||||||
let intersectsLines = raycaster.intersectObjects(
|
let intersectsLines = raycaster.intersectObjects(floorPlanGroupLine.current.children, true);
|
||||||
floorPlanGroupLine.current.children,
|
let intersectsPoint = raycaster.intersectObjects(floorPlanGroupPoint.current.children, true);
|
||||||
true
|
|
||||||
);
|
|
||||||
let intersectsPoint = raycaster.intersectObjects(
|
|
||||||
floorPlanGroupPoint.current.children,
|
|
||||||
true
|
|
||||||
);
|
|
||||||
|
|
||||||
const VisibleintersectsPoint = intersectsPoint.find(
|
const VisibleintersectsPoint = intersectsPoint.find((intersect) => intersect.object.visible);
|
||||||
(intersect) => intersect.object.visible
|
const visibleIntersect = intersectsLines.find(
|
||||||
);
|
(intersect) =>
|
||||||
const visibleIntersect = intersectsLines.find(
|
intersect.object.visible &&
|
||||||
(intersect) =>
|
intersect.object.name !== CONSTANTS.lineConfig.referenceName &&
|
||||||
intersect.object.visible &&
|
intersect.object.userData.linePoints[0][3] ===
|
||||||
intersect.object.name !== CONSTANTS.lineConfig.referenceName &&
|
CONSTANTS.lineConfig.wallName
|
||||||
intersect.object.userData.linePoints[0][3] ===
|
);
|
||||||
CONSTANTS.lineConfig.wallName
|
|
||||||
);
|
|
||||||
|
|
||||||
if (
|
if ((intersectsPoint.length === 0 || VisibleintersectsPoint === undefined) && intersectsLines.length > 0 && !isSnapped.current && !ispreSnapped.current) {
|
||||||
(intersectsPoint.length === 0 || VisibleintersectsPoint === undefined) &&
|
////////// Clicked on a preexisting Line //////////
|
||||||
intersectsLines.length > 0 &&
|
|
||||||
!isSnapped.current &&
|
|
||||||
!ispreSnapped.current
|
|
||||||
) {
|
|
||||||
////////// Clicked on a preexisting Line //////////
|
|
||||||
|
|
||||||
if (visibleIntersect && intersects) {
|
if (visibleIntersect && intersects) {
|
||||||
let IntersectsPoint = new THREE.Vector3(
|
let IntersectsPoint = new THREE.Vector3(intersects[0].point.x, 0.01, intersects[0].point.z);
|
||||||
intersects[0].point.x,
|
|
||||||
0.01,
|
|
||||||
intersects[0].point.z
|
|
||||||
);
|
|
||||||
|
|
||||||
if (isAngleSnapped.current && anglesnappedPoint.current) {
|
if (isAngleSnapped.current && anglesnappedPoint.current) {
|
||||||
IntersectsPoint = anglesnappedPoint.current;
|
IntersectsPoint = anglesnappedPoint.current;
|
||||||
}
|
}
|
||||||
if (visibleIntersect.object instanceof THREE.Mesh) {
|
if (visibleIntersect.object instanceof THREE.Mesh) {
|
||||||
const ThroughPoint =
|
const ThroughPoint = visibleIntersect.object.geometry.parameters.path.getPoints(CONSTANTS.lineConfig.lineIntersectionPoints);
|
||||||
visibleIntersect.object.geometry.parameters.path.getPoints(
|
let intersectionPoint = getClosestIntersection(ThroughPoint, IntersectsPoint);
|
||||||
CONSTANTS.lineConfig.lineIntersectionPoints
|
|
||||||
);
|
|
||||||
let intersectionPoint = getClosestIntersection(
|
|
||||||
ThroughPoint,
|
|
||||||
IntersectsPoint
|
|
||||||
);
|
|
||||||
|
|
||||||
if (intersectionPoint) {
|
if (intersectionPoint) {
|
||||||
const newLines = splitLine(
|
const newLines = splitLine(
|
||||||
visibleIntersect,
|
visibleIntersect,
|
||||||
intersectionPoint,
|
intersectionPoint,
|
||||||
currentLayerPoint,
|
currentLayerPoint,
|
||||||
floorPlanGroupPoint,
|
floorPlanGroupPoint,
|
||||||
dragPointControls,
|
dragPointControls,
|
||||||
isSnappedUUID,
|
isSnappedUUID,
|
||||||
lines,
|
lines,
|
||||||
setDeletedLines,
|
setDeletedLines,
|
||||||
floorPlanGroupLine,
|
floorPlanGroupLine,
|
||||||
socket,
|
socket,
|
||||||
CONSTANTS.pointConfig.wallOuterColor,
|
CONSTANTS.pointConfig.wallOuterColor,
|
||||||
CONSTANTS.lineConfig.wallColor,
|
CONSTANTS.lineConfig.wallColor,
|
||||||
CONSTANTS.lineConfig.wallName,
|
CONSTANTS.lineConfig.wallName,
|
||||||
projectId,
|
projectId,
|
||||||
versionId
|
versionId
|
||||||
);
|
);
|
||||||
setNewLines([newLines[0], newLines[1]]);
|
setNewLines([newLines[0], newLines[1]]);
|
||||||
|
|
||||||
(line.current as Types.Line).push([
|
(line.current as Types.Line).push([
|
||||||
new THREE.Vector3(intersectionPoint.x, 0.01, intersectionPoint.z),
|
new THREE.Vector3(intersectionPoint.x, 0.01, intersectionPoint.z),
|
||||||
isSnappedUUID.current!,
|
isSnappedUUID.current!,
|
||||||
activeLayer,
|
activeLayer,
|
||||||
CONSTANTS.lineConfig.wallName,
|
CONSTANTS.lineConfig.wallName,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (line.current.length >= 2 && line.current[0] && line.current[1]) {
|
if (line.current.length >= 2 && line.current[0] && line.current[1]) {
|
||||||
const data = arrayLineToObject(line.current as Types.Line);
|
const data = arrayLineToObject(line.current as Types.Line);
|
||||||
|
|
||||||
//REST
|
//REST
|
||||||
|
|
||||||
// setLine(organization, data.layer!, data.line!, data.type!);
|
// setLine(organization, data.layer!, data.line!, data.type!);
|
||||||
|
|
||||||
//SOCKET
|
//SOCKET
|
||||||
|
|
||||||
const input = {
|
const input = {
|
||||||
organization,
|
organization,
|
||||||
layer: data.layer,
|
layer: data.layer,
|
||||||
line: data.line,
|
line: data.line,
|
||||||
type: data.type,
|
type: data.type,
|
||||||
socketId: socket.id,
|
socketId: socket.id,
|
||||||
versionId,
|
versionId,
|
||||||
projectId,
|
projectId,
|
||||||
userId,
|
userId,
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log('input: ', input);
|
socket.emit("v1:Line:create", input);
|
||||||
socket.emit("v1:Line:create", input);
|
|
||||||
|
|
||||||
setNewLines([newLines[0], newLines[1], line.current]);
|
setNewLines([newLines[0], newLines[1], line.current]);
|
||||||
lines.current.push(line.current as Types.Line);
|
lines.current.push(line.current as Types.Line);
|
||||||
addLineToScene(
|
addLineToScene(
|
||||||
line.current[0][0],
|
line.current[0][0],
|
||||||
line.current[1][0],
|
line.current[1][0],
|
||||||
CONSTANTS.lineConfig.wallColor,
|
CONSTANTS.lineConfig.wallColor,
|
||||||
line.current,
|
line.current,
|
||||||
floorPlanGroupLine
|
floorPlanGroupLine
|
||||||
);
|
);
|
||||||
let lastPoint = line.current[line.current.length - 1];
|
let lastPoint = line.current[line.current.length - 1];
|
||||||
line.current = [lastPoint];
|
line.current = [lastPoint];
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (intersects && intersects.length > 0) {
|
if (intersects && intersects.length > 0) {
|
||||||
////////// Clicked on a emply place or a point //////////
|
////////// Clicked on a emply place or a point //////////
|
||||||
|
|
||||||
let intersectionPoint = intersects[0].point;
|
let intersectionPoint = intersects[0].point;
|
||||||
|
|
||||||
if (
|
if (isAngleSnapped.current && line.current.length > 0 && anglesnappedPoint.current) {
|
||||||
isAngleSnapped.current &&
|
intersectionPoint = anglesnappedPoint.current;
|
||||||
line.current.length > 0 &&
|
}
|
||||||
anglesnappedPoint.current
|
if (isSnapped.current && line.current.length > 0 && snappedPoint.current) {
|
||||||
) {
|
intersectionPoint = snappedPoint.current;
|
||||||
intersectionPoint = anglesnappedPoint.current;
|
}
|
||||||
}
|
if (ispreSnapped.current && snappedPoint.current) {
|
||||||
if (isSnapped.current && line.current.length > 0 && snappedPoint.current) {
|
intersectionPoint = snappedPoint.current;
|
||||||
intersectionPoint = snappedPoint.current;
|
}
|
||||||
}
|
|
||||||
if (ispreSnapped.current && snappedPoint.current) {
|
|
||||||
intersectionPoint = snappedPoint.current;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isSnapped.current && !ispreSnapped.current) {
|
if (!isSnapped.current && !ispreSnapped.current) {
|
||||||
addPointToScene(
|
addPointToScene(
|
||||||
intersectionPoint,
|
intersectionPoint,
|
||||||
CONSTANTS.pointConfig.wallOuterColor,
|
CONSTANTS.pointConfig.wallOuterColor,
|
||||||
currentLayerPoint,
|
currentLayerPoint,
|
||||||
floorPlanGroupPoint,
|
floorPlanGroupPoint,
|
||||||
dragPointControls,
|
dragPointControls,
|
||||||
isSnappedUUID,
|
isSnappedUUID,
|
||||||
CONSTANTS.lineConfig.wallName
|
CONSTANTS.lineConfig.wallName
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
ispreSnapped.current = false;
|
ispreSnapped.current = false;
|
||||||
isSnapped.current = false;
|
isSnapped.current = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
(line.current as Types.Line).push([
|
(line.current as Types.Line).push([
|
||||||
new THREE.Vector3(intersectionPoint.x, 0.01, intersectionPoint.z),
|
new THREE.Vector3(intersectionPoint.x, 0.01, intersectionPoint.z),
|
||||||
isSnappedUUID.current!,
|
isSnappedUUID.current!,
|
||||||
activeLayer,
|
activeLayer,
|
||||||
CONSTANTS.lineConfig.wallName,
|
CONSTANTS.lineConfig.wallName,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (line.current.length >= 2 && line.current[0] && line.current[1]) {
|
if (line.current.length >= 2 && line.current[0] && line.current[1]) {
|
||||||
const data = arrayLineToObject(line.current as Types.Line);
|
const data = arrayLineToObject(line.current as Types.Line);
|
||||||
|
|
||||||
//REST
|
//REST
|
||||||
|
|
||||||
// setLine(organization, data.layer!, data.line!, data.type!);
|
// setLine(organization, data.layer!, data.line!, data.type!);
|
||||||
|
|
||||||
//SOCKET
|
//SOCKET
|
||||||
|
|
||||||
const input = {
|
const input = {
|
||||||
organization,
|
organization,
|
||||||
layer: data.layer,
|
layer: data.layer,
|
||||||
line: data.line,
|
line: data.line,
|
||||||
type: data.type,
|
type: data.type,
|
||||||
socketId: socket.id,
|
socketId: socket.id,
|
||||||
versionId,
|
versionId,
|
||||||
projectId,
|
projectId,
|
||||||
userId,
|
userId,
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log('input: ', input);
|
socket.emit("v1:Line:create", input);
|
||||||
socket.emit("v1:Line:create", input);
|
|
||||||
|
|
||||||
setNewLines([line.current]);
|
setNewLines([line.current]);
|
||||||
lines.current.push(line.current as Types.Line);
|
lines.current.push(line.current as Types.Line);
|
||||||
addLineToScene(
|
addLineToScene(
|
||||||
line.current[0][0],
|
line.current[0][0],
|
||||||
line.current[1][0],
|
line.current[1][0],
|
||||||
CONSTANTS.lineConfig.wallColor,
|
CONSTANTS.lineConfig.wallColor,
|
||||||
line.current,
|
line.current,
|
||||||
floorPlanGroupLine
|
floorPlanGroupLine
|
||||||
);
|
);
|
||||||
let lastPoint = line.current[line.current.length - 1];
|
let lastPoint = line.current[line.current.length - 1];
|
||||||
line.current = [lastPoint];
|
line.current = [lastPoint];
|
||||||
}
|
}
|
||||||
if (isSnapped.current) {
|
if (isSnapped.current) {
|
||||||
removeReferenceLine(floorPlanGroup, ReferenceLineMesh, LineCreated, line);
|
removeReferenceLine(floorPlanGroup, ReferenceLineMesh, LineCreated, line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default drawWall;
|
export default drawWall;
|
||||||
|
|||||||
Reference in New Issue
Block a user