From dc717b051a3ae56e361f7706de74d057f783265b Mon Sep 17 00:00:00 2001 From: yjnoh Date: Thu, 28 Nov 2024 10:18:41 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9E=90=EB=8F=99=20=EC=84=A4=EC=B9=98=20?= =?UTF-8?q?=EC=9E=91=EC=97=85=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/module/useModuleBasicSetting.js | 245 +++++++++++++--------- 1 file changed, 143 insertions(+), 102 deletions(-) diff --git a/src/hooks/module/useModuleBasicSetting.js b/src/hooks/module/useModuleBasicSetting.js index 067d9f8a..7b56c5f1 100644 --- a/src/hooks/module/useModuleBasicSetting.js +++ b/src/hooks/module/useModuleBasicSetting.js @@ -434,19 +434,25 @@ export function useModuleBasicSetting() { } }) - const moduleSetupArray = [] - moduleSetupSurfaces.forEach((moduleSetupSurface, index) => { - moduleSetupSurface.fire('mousedown') + const moduleOptions = { + fill: '#BFFD9F', + stroke: 'black', + strokeWidth: 0.1, + selectable: true, // 선택 가능하게 설정 + lockMovementX: false, // X 축 이동 잠금 + lockMovementY: false, // Y 축 이동 잠금 + lockRotation: false, // 회전 잠금 + lockScalingX: false, // X 축 크기 조정 잠금 + lockScalingY: false, // Y 축 크기 조정 잠금 + opacity: 0.8, + parentId: moduleSetupSurface.parentId, + name: 'module', + } - const surfaceMaxLines = findSetupSurfaceMaxLines(moduleSetupSurface) - - let maxLengthLine = moduleSetupSurface.lines.reduce((acc, cur) => { - return acc.length > cur.length ? acc : cur - }) - - const turfModuleSetupSurface = polygonToTurfPolygon(moduleSetupSurface) //폴리곤을 turf 객체로 변환 - - const containsBatchObjects = batchObjects.filter((batchObject) => { + //선택된 지붕안에 오브젝트(도머, 개구등)이 있는지 확인하는 로직 포함되면 배열 반환 + const objectsIncludeSurface = (turfModuleSetupSurface) => { + let containsBatchObjects = [] + containsBatchObjects = batchObjects.filter((batchObject) => { let convertBatchObject if (batchObject.type === 'group') { @@ -454,9 +460,7 @@ export function useModuleBasicSetting() { convertBatchObject = batchObjectGroupToTurfPolygon(batchObject) } else { //개구, 그림자 - batchObject.set({ - points: rectToPolygon(batchObject), - }) + batchObject.set({ points: rectToPolygon(batchObject) }) canvas?.renderAll() // set된걸 바로 적용하기 위해 convertBatchObject = polygonToTurfPolygon(batchObject) //rect를 폴리곤으로 변환 -> turf 폴리곤으로 변환 } @@ -465,19 +469,59 @@ export function useModuleBasicSetting() { return turf.booleanContains(turfModuleSetupSurface, convertBatchObject) || turf.booleanWithin(convertBatchObject, turfModuleSetupSurface) }) - let difference = turfModuleSetupSurface //기본 객체(면형상) + return containsBatchObjects + } + + /** + * 도머나 개구가 모듈에 걸치는지 확인하는 로직 + * @param {*} squarePolygon + * @param {*} containsBatchObjects + * @returns + */ + const checkModuleDisjointObjects = (squarePolygon, containsBatchObjects) => { + let isDisjoint = false if (containsBatchObjects.length > 0) { - //turf로 도머를 제외시키는 로직 - for (let i = 0; i < containsBatchObjects.length; i++) { - let convertBatchObject - if (containsBatchObjects[i].type === 'group') { - convertBatchObject = batchObjectGroupToTurfPolygon(containsBatchObjects[i]) + let convertBatchObject + //도머가 있으면 적용되는 로직 + isDisjoint = containsBatchObjects.every((batchObject) => { + if (batchObject.type === 'group') { + convertBatchObject = batchObjectGroupToTurfPolygon(batchObject) } else { - convertBatchObject = polygonToTurfPolygon(containsBatchObjects[i]) + convertBatchObject = polygonToTurfPolygon(batchObject) } - } + /** + * 도머가 여러개일수있으므로 겹치는게 있다면... + * 안겹치는지 확인하는 로직이라 안겹치면 true를 반환 + */ + return turf.booleanDisjoint(squarePolygon, convertBatchObject) + }) + } else { + isDisjoint = true } + return isDisjoint + } + + /** + * 배치면 안에 있는지 확인 + * @param {*} squarePolygon + * @param {*} turfModuleSetupSurface + * @returns + */ + const checkModuleDisjointSurface = (squarePolygon, turfModuleSetupSurface) => { + return turf.booleanContains(turfModuleSetupSurface, squarePolygon) || turf.booleanWithin(squarePolygon, turfModuleSetupSurface) + } + + moduleSetupSurfaces.forEach((moduleSetupSurface, index) => { + moduleSetupSurface.fire('mousedown') + const moduleSetupArray = [] + + let maxLengthLine = moduleSetupSurface.lines.reduce((acc, cur) => { + return acc.length > cur.length ? acc : cur + }) + + const turfModuleSetupSurface = polygonToTurfPolygon(moduleSetupSurface) //폴리곤을 turf 객체로 변환 + const containsBatchObjects = objectsIncludeSurface(turfModuleSetupSurface) //배치면에 오브젝트(도머, 개구등)이 있는지 확인하는 로직 let width = maxLengthLine.flowDirection === 'east' || maxLengthLine.flowDirection === 'west' ? 172.2 : 113.4 let height = maxLengthLine.flowDirection === 'east' || maxLengthLine.flowDirection === 'west' ? 113.4 : 172.2 @@ -488,102 +532,99 @@ export function useModuleBasicSetting() { height = moduleSetupSurface.flowDirection === 'south' || moduleSetupSurface.flowDirection === 'north' ? 113.4 : 172.2 } - let square - let startPoint, endPoint + const surfaceMaxLines = findSetupSurfaceMaxLines(moduleSetupSurface) + const maxLeftEndPoint = surfaceMaxLines.left.x1 //최 좌측 + const maxRightEndPoint = surfaceMaxLines.right.x1 //최 우측 + const maxTopEndPoint = surfaceMaxLines.top.y1 //최 상단 + const maxBottomEndPoint = surfaceMaxLines.bottom.y1 //최 하단 + let leftMargin, bottomMargin, square if (setupLocation === 'eaves') { if (moduleSetupSurface.flowDirection === 'south') { - startPoint = flowModuleLine.find((obj) => obj.target === 'bottom') - endPoint = flowModuleLine.find((obj) => obj.target === 'top') - const totalHeight = endPoint.y1 - startPoint.y1 - const diffHeight = Math.abs(totalHeight / height) - let leftMargin = 0 - let bottomMargin = 0 + let startPoint = flowModuleLine.find((obj) => obj.target === 'bottom') + let totalLeftEndPoint = maxLeftEndPoint - startPoint.x1 + let totalTopEndPoint = maxTopEndPoint - startPoint.y1 + let totalWidth = Math.ceil(Math.abs(maxRightEndPoint - maxLeftEndPoint) / width) + let diffLeftEndPoint = Math.abs(totalLeftEndPoint / width) + let diffTopEndPoint = Math.abs(totalTopEndPoint / height) + let startColPoint = Math.abs(width * Math.ceil(diffLeftEndPoint) - startPoint.x1) - for (let i = 0; i < diffHeight; i++) { - leftMargin = i === 0 ? 1 : 0 - bottomMargin = i === 0 ? 0 : 1 - - square = [ - [startPoint.x1 + leftMargin, startPoint.y1 - height - bottomMargin], - [startPoint.x1 + leftMargin, startPoint.y1 - bottomMargin], - [startPoint.x1 + leftMargin + width, startPoint.y1 - bottomMargin], - [startPoint.x1 + leftMargin + width, startPoint.y1 - height - bottomMargin], - [startPoint.x1 + leftMargin, startPoint.y1 - height - bottomMargin], - ] - - const squarePolygon = turf.polygon([square]) - - //설치면 안에 있는지 확인 - const disjointFromTrestle = - turf.booleanContains(turfModuleSetupSurface, squarePolygon) || turf.booleanWithin(squarePolygon, turfModuleSetupSurface) - - if (disjointFromTrestle) { + for (let j = 0; j < diffTopEndPoint; j++) { + bottomMargin = 1 + for (let i = 0; i <= totalWidth; i++) { + leftMargin = i === 0 ? 0 : 1 //숫자가 0이면 0, 1이면 1로 바꾸기 + square = [ + [startColPoint + width * i + leftMargin, startPoint.y1 - height * j - bottomMargin], + [startColPoint + width * i + width - leftMargin, startPoint.y1 - height * j - bottomMargin], + [startColPoint + width * i + width - leftMargin, startPoint.y1 - height * j - height - bottomMargin], + [startColPoint + width * i + leftMargin, startPoint.y1 - height * j - height - bottomMargin], + [startColPoint + width * i + leftMargin, startPoint.y1 - height * j - bottomMargin], + ] + let squarePolygon = turf.polygon([square]) let turfCoordnates = squarePolygon.geometry.coordinates[0].slice(0, -1) - const points = turfCoordnates.map((coord) => ({ x: coord[0], y: coord[1] })) + let points = turfCoordnates.map((coord) => ({ x: coord[0], y: coord[1] })) - if (containsBatchObjects.length > 0) { - let convertBatchObject - //도머가 있으면 적용되는 로직 - const isDisjoint = containsBatchObjects.every((batchObject) => { - if (batchObject.type === 'group') { - convertBatchObject = batchObjectGroupToTurfPolygon(batchObject) - } else { - convertBatchObject = polygonToTurfPolygon(batchObject) - } - return turf.booleanDisjoint(squarePolygon, convertBatchObject) //도머가 여러개일수있으므로 겹치는게 있다면... - }) - if (isDisjoint) { - const tempModule = new QPolygon(points, { - fill: '#BFFD9F', - stroke: 'black', - strokeWidth: 0.1, - selectable: true, // 선택 가능하게 설정 - lockMovementX: false, // X 축 이동 잠금 - lockMovementY: false, // Y 축 이동 잠금 - lockRotation: false, // 회전 잠금 - lockScalingX: false, // X 축 크기 조정 잠금 - lockScalingY: false, // Y 축 크기 조정 잠금 - opacity: 0.8, - parentId: moduleSetupSurface.parentId, - name: 'module', - }) - tempModule.setViewLengthText(false) - canvas?.add(tempModule) - moduleSetupArray.push(tempModule) - } - } else { - //도머가 없을땐 그냥 그림 - const tempModule = new QPolygon(points, { - fill: '#BFFD9F', - stroke: 'black', - selectable: true, // 선택 가능하게 설정 - lockMovementX: true, // X 축 이동 잠금 - lockMovementY: true, // Y 축 이동 잠금 - lockRotation: true, // 회전 잠금 - lockScalingX: true, // X 축 크기 조정 잠금 - lockScalingY: true, // Y 축 크기 조정 잠금 - opacity: 0.8, - parentId: moduleSetupSurface.parentId, - name: 'module', - }) - canvas?.add(tempModule) - moduleSetupArray.push(tempModule) - } - startPoint = { x1: points[0].x, y1: points[0].y, x2: points[3].x, y2: points[3].y } + // if (disjointFromTrestle && isDisjoint) { + let tempModule = new QPolygon(points, { ...moduleOptions, turfPoints: squarePolygon }) + canvas?.add(tempModule) + moduleSetupArray.push(tempModule) } } } } else if (setupLocation === 'ridge') { + if (moduleSetupSurface.flowDirection === 'south') { + let startPoint = flowModuleLine.find((obj) => obj.target === 'top') + let totalLeftEndPoint = maxLeftEndPoint - startPoint.x1 + let totalRightEndPoint = maxLeftEndPoint - maxRightEndPoint + let totalBottomEndPoint = maxBottomEndPoint - startPoint.y1 + let diffLeftEndPoint = Math.abs(totalLeftEndPoint / width) + let diffRightEndPoint = Math.abs(totalRightEndPoint / width) + let diffBottomEndPoint = Math.abs(totalBottomEndPoint / height) + let startColPoint = Math.abs(width * Math.ceil(diffLeftEndPoint) - startPoint.x1) + + for (let j = 0; j < diffBottomEndPoint; j++) { + bottomMargin = 1 + for (let i = 0; i < diffRightEndPoint; i++) { + leftMargin = i === 0 ? 0 : 1 + + square = [ + [startColPoint + width * i - leftMargin, startPoint.y1 + height * j + bottomMargin], + [startColPoint + width * i - leftMargin, startPoint.y1 + height * j + height + bottomMargin], + [startColPoint + width * i + width - leftMargin, startPoint.y1 + height * j + height + bottomMargin], + [startColPoint + width * i + width - leftMargin, startPoint.y1 + height * j + bottomMargin], + [startColPoint + width * i - leftMargin, startPoint.y1 + height * j + bottomMargin], + ] + let squarePolygon = turf.polygon([square]) + let turfCoordnates = squarePolygon.geometry.coordinates[0].slice(0, -1) + let points = turfCoordnates.map((coord) => ({ x: coord[0], y: coord[1] })) + + // if (disjointFromTrestle && isDisjoint) { + let tempModule = new QPolygon(points, { ...moduleOptions, turfPoints: squarePolygon }) + canvas?.add(tempModule) + moduleSetupArray.push(tempModule) + } + } + } } else { } - moduleSetupSurface.set({ modules: moduleSetupArray }) + const setupedModules = moduleSetupArray.filter((module) => { + let disjointFromTrestle = checkModuleDisjointSurface(module.turfPoints, turfModuleSetupSurface) + let isDisjoint = checkModuleDisjointObjects(module.turfPoints, containsBatchObjects) + + if (!(disjointFromTrestle && isDisjoint)) { + canvas?.remove(module) + return false + } else { + return module + } + }) + moduleSetupSurface.set({ modules: setupedModules }) }) setModuleIsSetup(moduleSetupArray) - console.log(calculateForApi(moduleSetupArray)) + // console.log(calculateForApi(moduleSetupArray)) } const calculateForApi = (moduleSetupArray) => {