diff --git a/src/hooks/usePolygon.js b/src/hooks/usePolygon.js index b66996cb..f72b6446 100644 --- a/src/hooks/usePolygon.js +++ b/src/hooks/usePolygon.js @@ -845,6 +845,7 @@ export const usePolygon = () => { if (polygonLine.attributes?.type && innerLine.attributes) { polygonLine.need = false innerLine.attributes.type = polygonLine.attributes.type + innerLine.attributes.isStart = true } } }) @@ -1025,6 +1026,10 @@ export const usePolygon = () => { polygonLines = [...polygonLines, ...newLines] + polygonLines.forEach((polygonLine) => { + polygonLine.attributes = { ...polygonLine.attributes, isStart: true } + }) + let allLines = [...polygonLines, ...innerLines] /*allLines.forEach((line) => { @@ -1058,16 +1063,6 @@ export const usePolygon = () => { allPoints.push(endPoint) }) - // allPoints에서 중복을 제거한다. - const uniquePoints = allPoints.filter((point, index, self) => { - return ( - index === - self.findIndex((p) => { - return isSamePoint(p, point) - }) - ) - }) - // 2025-02-19 대각선은 케라바, 직선은 용마루로 세팅 innerLines.forEach((innerLine) => { const startPoint = innerLine.startPoint @@ -1393,6 +1388,20 @@ export const usePolygon = () => { // 최종 함수 function getPath(start, end, graph, epsilon = 1) { + // startPoint와 arrivalPoint가 될 수 있는 점은 line.attributes.type이 'default' 혹은 null이 아닌 line인 경우에만 가능 + const isValidPoint = (point) => { + return allLines.some((line) => { + const isOnLine = isSamePoint(line.startPoint, point, epsilon) || isSamePoint(line.endPoint, point, epsilon) + const hasValidType = line.attributes?.type && line.attributes.type !== 'default' + return isOnLine && hasValidType + }) + } + + if (!isValidPoint(start) || !isValidPoint(end)) { + console.log('시작점 또는 도착점이 유효하지 않음. 무시.') + return [] + } + if (isDirectlyConnected(start, end, graph, epsilon)) { console.log('직선 연결 있음. 무시.') return [] @@ -1404,16 +1413,32 @@ export const usePolygon = () => { return [] } + // 사용된 노드들을 graph에서 제거 + if (path.length > 0) { + path.forEach((point) => { + const pointKey = pointToKey(point, epsilon) + delete graph[pointKey] + // 다른 노드들의 연결에서도 이 노드를 제거 + Object.keys(graph).forEach((key) => { + graph[key] = graph[key].filter((neighbor) => !isSamePoint(neighbor.point, point, epsilon)) + }) + }) + } + return path } const roofs = [] + const remainingLines = [...allLines] // 사용 가능한 line들의 복사본 - allLines.forEach((line) => { - // 그래프 생성 + // isStart가 true인 line들만 시작점으로 사용 + const startLines = remainingLines.filter(line => line.attributes?.isStart === true) + + startLines.forEach((startLine) => { + // 현재 남아있는 line들로 그래프 생성 const graph = {} - const edges = allLines - .filter((line2) => line !== line2) + const edges = remainingLines + .filter((line2) => line2 !== startLine) .map((line) => { return [line.startPoint, line.endPoint] }) @@ -1430,10 +1455,19 @@ export const usePolygon = () => { graph[key2].push({ point: p1, distance }) } - const startPoint = { ...line.startPoint } // 시작점 - - let arrivalPoint = { ...line.endPoint } // 도착점 - roofs.push(getPath(startPoint, arrivalPoint, graph)) + const startPoint = { ...startLine.startPoint } // 시작점 + let arrivalPoint = { ...startLine.endPoint } // 도착점 + + const roof = getPath(startPoint, arrivalPoint, graph) + if (roof.length > 0) { + roofs.push(roof) + + // 사용된 startLine을 remainingLines에서 제거 + const startLineIndex = remainingLines.findIndex(line => line === startLine) + if (startLineIndex !== -1) { + remainingLines.splice(startLineIndex, 1) + } + } }) return removeDuplicatePolygons(