diff --git a/src/util/skeleton-utils.js b/src/util/skeleton-utils.js index 750d512b..5db8af4d 100644 --- a/src/util/skeleton-utils.js +++ b/src/util/skeleton-utils.js @@ -212,12 +212,14 @@ const movingLineFromSkeleton = (roofId, canvas) => { point.y = Big(point.y).plus(moveUpDownLength).toNumber(); } }else if(moveDirection === 'out'){ - if(isSamePoint(roof.basePoints[index], originalStartPoint)) { - point.y = Big(point.y).minus(moveUpDownLength).toNumber(); - } - if (isSamePoint(roof.basePoints[index], originalEndPoint)) { + if(isSamePoint(roof.basePoints[index], originalStartPoint) || isSamePoint(roof.basePoints[index], originalEndPoint)) { point.y = Big(point.y).minus(moveUpDownLength).toNumber(); + console.log('roof.basePoints[index]', roof.basePoints[index]) + console.log('point.x::::', point) + console.log('originalStartPoint', originalStartPoint) + console.log('originalEndPoint', originalEndPoint) } + } }else if(position === 'left'){ @@ -262,7 +264,69 @@ const movingLineFromSkeleton = (roofId, canvas) => { line.startPoint = newStartPoint; line.endPoint = newEndPoint; }); - return newPoints; + + /** + * 직선다각형을 이루지 못하는 좌표를 삭제합니다. + * @param {Array} points - 폴리곤 좌표 배열 + * @returns {Array} 정리된 좌표 배열 + */ + function removeNonOrthogonalPoints(points) { + if (!points || points.length < 3) return points; + + const EPSILON = 1.0; + + const isOrthogonal = (p1, p2) => + Math.abs(p1.x - p2.x) < EPSILON || Math.abs(p1.y - p2.y) < EPSILON; + + let current = [...points]; + let changed = true; + + // 1. 대각선을 만드는 점 제거 + while (changed && current.length >= 3) { + changed = false; + for (let i = 0; i < current.length; i++) { + const pPrev = current[(i - 1 + current.length) % current.length]; + const pCurr = current[i]; + const pNext = current[(i + 1) % current.length]; + + // 현재 점(pCurr)을 기준으로 앞뒤 연결이 모두 직교하지 않거나, + // 현재 점을 제거했을 때 앞뒤 점(pPrev, pNext)이 직교하게 된다면 현재 점이 불필요한 "꺾임"일 수 있음. + if (!isOrthogonal(pPrev, pCurr) || !isOrthogonal(pCurr, pNext)) { + if (isOrthogonal(pPrev, pNext)) { + current.splice(i, 1); + changed = true; + break; + } + } + } + } + + // 2. 일직선상의 중간 점 제거 (수평 또는 수직선상에 세 점이 있는 경우) + changed = true; + while (changed && current.length >= 3) { + changed = false; + for (let i = 0; i < current.length; i++) { + const p1 = current[i]; + const p2 = current[(i + 1) % current.length]; + const p3 = current[(i + 2) % current.length]; + + if ((Math.abs(p1.x - p2.x) < EPSILON && Math.abs(p2.x - p3.x) < EPSILON) || + (Math.abs(p1.y - p2.y) < EPSILON && Math.abs(p2.y - p3.y) < EPSILON)) { + current.splice((i + 1) % current.length, 1); + changed = true; + break; + } + } + } + + return current; + } + + + const cleaned = removeNonOrthogonalPoints(newPoints); + console.log(cleaned); // 결과: 4개의 점만 남음 (P1, P2, P3, P4) + + return cleaned; } } @@ -376,7 +440,7 @@ export const skeletonBuilder = (roofId, canvas, textMode) => { canvas.set('skeleton', cleanSkeleton) canvas.renderAll() - console.log('skeleton rendered.', canvas) + //console.log('skeleton rendered.', canvas) } catch (e) { console.error('스켈레톤 생성 중 오류 발생:', e) if (canvas.skeletonStates) { @@ -470,9 +534,14 @@ const createInnerLinesFromSkeleton = (roofId, canvas, skeleton, textMode) => { line.attributes.type === 'gable' && isSameLine(Begin.X, Begin.Y, End.X, End.Y, line) ); - if (gableBaseLine) { - // Store current state before processing - const beforeGableProcessing = JSON.parse(JSON.stringify(skeletonLines)); + if (gableBaseLine) { + // Store current state before processing - avoid circular refs by only picking needed data + const beforeGableProcessing = skeletonLines.map(line => ({ + p1: { x: line.p1.x, y: line.p1.y }, + p2: { x: line.p2.x, y: line.p2.y }, + attributes: { ...line.attributes }, + lineStyle: { ...line.lineStyle } + })); // if(canvas.skeletonLines.length > 0){ // skeletonLines = canvas.skeletonLines; @@ -854,10 +923,13 @@ const createInnerLinesFromSkeleton = (roofId, canvas, skeleton, textMode) => { } else { getAddLine({ y: inLine.y2, x: inLine.x2 }, { y: bStartY, x: wallLine.x2 }, 'pink') } + getAddLine({ y: bStartY, x: wallLine.x2 }, { y: roofLine.y1, x: wallLine.x1 }, 'magenta') + getAddLine({ y: newLine.y1, x: newLine.x1 }, { y: newLine.y2, x: wallLine.x2 }, 'Gray') + findPoints.push({ y: aStartY, x: newPStart.x, position: 'left_out_start' }) + }else{ + newPStart.y = roofLine.y1 } - getAddLine({ y: bStartY, x: wallLine.x2 }, { y: roofLine.y1, x: wallLine.x1 }, 'magenta') - getAddLine({ y: newLine.y1, x: newLine.x1 }, { y: newLine.y2, x: wallLine.x2 }, 'Gray') - findPoints.push({ y: aStartY, x: newPStart.x, position: 'left_out_start' }) + } else { const cLineY = Big(wallBaseLine.x1).minus(wallLine.x1).abs().toNumber() newPStart.y = Big(newPStart.y).minus(cLineY).toNumber() @@ -909,10 +981,13 @@ const createInnerLinesFromSkeleton = (roofId, canvas, skeleton, textMode) => { } else { getAddLine({ y: inLine.y1, x: inLine.x1 }, { y: bStartY, x: wallLine.x1 }, 'pink') } + getAddLine({ y: bStartY, x: wallLine.x1 }, { y: roofLine.y2, x: wallLine.x2 }, 'magenta') + getAddLine({ y: newLine.y2, x: newLine.x2 }, { y: newLine.y1, x: wallLine.x1 }, 'Gray') + findPoints.push({ y: aStartY, x: newPEnd.x, position: 'left_out_end' }) + }else{ + newPEnd.y = roofLine.y2 } - getAddLine({ y: bStartY, x: wallLine.x1 }, { y: roofLine.y2, x: wallLine.x2 }, 'magenta') - getAddLine({ y: newLine.y2, x: newLine.x2 }, { y: newLine.y1, x: wallLine.x1 }, 'Gray') - findPoints.push({ y: aStartY, x: newPEnd.x, position: 'left_out_end' }) + } else { const cLineY = Big(wallBaseLine.x2).minus(wallLine.x2).abs().toNumber() newPEnd.y = Big(newPEnd.y).plus(cLineY).toNumber() @@ -1024,10 +1099,13 @@ const createInnerLinesFromSkeleton = (roofId, canvas, skeleton, textMode) => { } else { getAddLine({ y: inLine.y1, x: inLine.x1 }, { y: bStartY, x: wallLine.x2 }, 'pink') } + getAddLine({ y: bStartY, x: wallLine.x2 }, { y: roofLine.y1, x: wallLine.x1 }, 'magenta') + getAddLine({ y: newLine.y1, x: newLine.x1 }, { y: newLine.y2, x: wallLine.x2 }, 'Gray') + findPoints.push({ y: aStartY, x: newPEnd.x, position: 'right_out_start' }) + }else{ + newPStart.y = roofLine.y1 } - getAddLine({ y: bStartY, x: wallLine.x2 }, { y: roofLine.y1, x: wallLine.x1 }, 'magenta') - getAddLine({ y: newLine.y1, x: newLine.x1 }, { y: newLine.y2, x: wallLine.x2 }, 'Gray') - findPoints.push({ y: aStartY, x: newPEnd.x, position: 'right_out_start' }) + } else { const cLineY = Big(wallBaseLine.x1).minus(wallLine.x1).abs().toNumber() newPStart.y = Big(newPStart.y).plus(cLineY).toNumber() @@ -1213,10 +1291,13 @@ const createInnerLinesFromSkeleton = (roofId, canvas, skeleton, textMode) => { } else { getAddLine({ x: inLine.x1, y: inLine.y1 }, { x: bStartX, y: wallLine.y1 }, 'pink') } + getAddLine({ x: bStartX, y: wallLine.y1 }, { x: roofLine.x1, y: wallLine.y1 }, 'magenta') + getAddLine({ x: newLine.x1, y: newLine.y1 }, { x: newLine.x1, y: wallLine.y1 }, 'Gray') + findPoints.push({ x: aStartX, y: newPEnd.y, position: 'top_out_start' }) + }else{ //라인머지 + newPStart.x = roofLine.x1 } - getAddLine({ x: bStartX, y: wallLine.y1 }, { x: roofLine.x1, y: wallLine.y1 }, 'magenta') - getAddLine({ x: newLine.x1, y: newLine.y1 }, { x: newLine.x1, y: wallLine.y1 }, 'Gray') - findPoints.push({ x: aStartX, y: newPEnd.y, position: 'top_out_start' }) + } else { const cLineX = Big(wallBaseLine.y1).minus(wallLine.y1).abs().toNumber() newPStart.x = Big(newPStart.x).plus(cLineX).toNumber() @@ -1265,10 +1346,13 @@ const createInnerLinesFromSkeleton = (roofId, canvas, skeleton, textMode) => { } else { getAddLine({ x: inLine.x1, y: inLine.y1 }, { x: bStartX, y: wallLine.y1 }, 'pink') } + getAddLine({ x: bStartX, y: wallLine.y1 }, { x: roofLine.x2, y: wallLine.y2 }, 'magenta') + getAddLine({ x: newLine.x2, y: newLine.y2 }, { x: newLine.x1, y: wallLine.y1 }, 'Gray') + findPoints.push({ x: aStartX, y: newPEnd.y, position: 'top_out_end' }) + }else{ + newPEnd.x = roofLine.x2 } - getAddLine({ x: bStartX, y: wallLine.y1 }, { x: roofLine.x2, y: wallLine.y2 }, 'magenta') - getAddLine({ x: newLine.x2, y: newLine.y2 }, { x: newLine.x1, y: wallLine.y1 }, 'Gray') - findPoints.push({ x: aStartX, y: newPEnd.y, position: 'top_out_end' }) + } else { const cLineX = Big(wallLine.y2).minus(wallBaseLine.y2).abs().toNumber() newPEnd.x = Big(newPEnd.x).minus(cLineX).toNumber() @@ -1375,10 +1459,13 @@ const createInnerLinesFromSkeleton = (roofId, canvas, skeleton, textMode) => { } else { getAddLine({ x: inLine.x1, y: inLine.y1 }, { x: bStartX, y: wallLine.y1 }, 'pink') } + getAddLine({ x: bStartX, y: wallLine.y1 }, { x: roofLine.x1, y: wallLine.y1 }, 'magenta') + getAddLine({ x: newLine.x1, y: newLine.y1 }, { x: newLine.x1, y: wallLine.y1 }, 'Gray') + findPoints.push({ x: aStartX, y: newPEnd.y, position: 'bottom_out_start' }) + }else { + newPStart.x = roofLine.x1 } - getAddLine({ x: bStartX, y: wallLine.y1 }, { x: roofLine.x1, y: wallLine.y1 }, 'magenta') - getAddLine({ x: newLine.x1, y: newLine.y1 }, { x: newLine.x1, y: wallLine.y1 }, 'Gray') - findPoints.push({ x: aStartX, y: newPEnd.y, position: 'bottom_out_start' }) + } else { const cLineX = Big(wallBaseLine.y1).minus(wallLine.y1).abs().toNumber() newPStart.x = Big(newPStart.x).minus(cLineX).toNumber() @@ -1429,10 +1516,13 @@ const createInnerLinesFromSkeleton = (roofId, canvas, skeleton, textMode) => { } else { getAddLine({ x: inLine.x1, y: inLine.y1 }, { x: bStartX, y: wallLine.y1 }, 'pink') } + getAddLine({ x: bStartX, y: wallLine.y1 }, { x: roofLine.x2, y: wallLine.y2 }, 'magenta') + getAddLine({ x: newLine.x2, y: newLine.y2 }, { x: newLine.x1, y: wallLine.y1 }, 'Gray') + findPoints.push({ x: aStartX, y: newPEnd.y, position: 'bottom_out_end' }) + }else{ + newPEnd.x = roofLine.x2 } - getAddLine({ x: bStartX, y: wallLine.y1 }, { x: roofLine.x2, y: wallLine.y2 }, 'magenta') - getAddLine({ x: newLine.x2, y: newLine.y2 }, { x: newLine.x1, y: wallLine.y1 }, 'Gray') - findPoints.push({ x: aStartX, y: newPEnd.y, position: 'bottom_out_end' }) + } else { const cLineX = Big(wallBaseLine.y2).minus(wallLine.y2).abs().toNumber() newPEnd.x = Big(newPEnd.x).plus(cLineX).toNumber() @@ -3424,58 +3514,77 @@ function getTurnDirection(p1, p2, p3) { function isValleyVertex(targetPoint, connectedLine, allLines, isStartVertex) { const tolerance = 0.1; - // 1. 연결된 '다른' 라인을 찾습니다. - // isStartVertex가 true면 : 이 점으로 '들어오는' 라인(Previous Line)을 찾아야 함 - // isStartVertex가 false면 : 이 점에서 '나가는' 라인(Next Line)을 찾아야 함 + const connectedLineData = { + x1: connectedLine.x1 ?? connectedLine.get?.('x1'), + y1: connectedLine.y1 ?? connectedLine.get?.('y1'), + x2: connectedLine.x2 ?? connectedLine.get?.('x2'), + y2: connectedLine.y2 ?? connectedLine.get?.('y2'), + startPoint: connectedLine.startPoint, + endPoint: connectedLine.endPoint + }; let neighborLine = null; if (isStartVertex) { - // targetPoint가 Start이므로, 어떤 라인의 End가 targetPoint와 같아야 함 (Previous Line) - neighborLine = allLines.find(l => - l !== connectedLine && - isSamePoint(l.endPoint || {x:l.x2, y:l.y2}, targetPoint, tolerance) - ); + neighborLine = allLines.find(l => { + if (l === connectedLine) return false; + const lx1 = l.x1 ?? l.get?.('x1'); + const ly1 = l.y1 ?? l.get?.('y1'); + const lx2 = l.x2 ?? l.get?.('x2'); + const ly2 = l.y2 ?? l.get?.('y2'); + const end = l.endPoint || { x: lx2, y: ly2 }; + return isSamePoint(end, targetPoint, tolerance); + }); } else { - // targetPoint가 End이므로, 어떤 라인의 Start가 targetPoint와 같아야 함 (Next Line) - neighborLine = allLines.find(l => - l !== connectedLine && - isSamePoint(l.startPoint || {x:l.x1, y:l.y1}, targetPoint, tolerance) - ); + neighborLine = allLines.find(l => { + if (l === connectedLine) return false; + const lx1 = l.x1 ?? l.get?.('x1'); + const ly1 = l.y1 ?? l.get?.('y1'); + const lx2 = l.x2 ?? l.get?.('x2'); + const ly2 = l.y2 ?? l.get?.('y2'); + const start = l.startPoint || { x: lx1, y: ly1 }; + return isSamePoint(start, targetPoint, tolerance); + }); } - // 연결된 라인을 못 찾았거나 끊겨있으면 판단 불가 (일단 false) if (!neighborLine) return false; - // 2. 세 점을 구성하여 회전 방향(Turn) 계산 - // 순서: PrevLine.Start -> [TargetVertex] -> NextLine.End + const nlx1 = neighborLine.x1 ?? neighborLine.get?.('x1'); + const nly1 = neighborLine.y1 ?? neighborLine.get?.('y1'); + const nlx2 = neighborLine.x2 ?? neighborLine.get?.('x2'); + const nly2 = neighborLine.y2 ?? neighborLine.get?.('y2'); + + const clx1 = connectedLineData.x1; + const cly1 = connectedLineData.y1; + const clx2 = connectedLineData.x2; + const cly2 = connectedLineData.y2; + let p1, p2, p3; if (isStartVertex) { - // neighbor(Prev) -> connected(Current) - p1 = neighborLine.startPoint || {x: neighborLine.x1, y: neighborLine.y1}; - p2 = targetPoint; // 접점 - p3 = connectedLine.endPoint || {x: connectedLine.x2, y: connectedLine.y2}; + p1 = neighborLine.startPoint || { x: nlx1, y: nly1 }; + p2 = targetPoint; + p3 = connectedLineData.endPoint || { x: clx2, y: cly2 }; } else { - // connected(Current) -> neighbor(Next) - p1 = connectedLine.startPoint || {x: connectedLine.x1, y: connectedLine.y1}; - p2 = targetPoint; // 접점 - p3 = neighborLine.endPoint || {x: neighborLine.x2, y: neighborLine.y2}; + p1 = connectedLineData.startPoint || { x: clx1, y: cly1 }; + p2 = targetPoint; + p3 = neighborLine.endPoint || { x: nlx2, y: nly2 }; } - // 3. 외적 계산 (Y축이 아래로 증가하는 캔버스 좌표계 + CCW 진행 기준) - // 값이 양수(+)면 오른쪽 턴 = 골짜기 const crossProduct = getTurnDirection(p1, p2, p3); - return crossProduct > 0; } function findInteriorPoint(line, polygonLines) { - const { x1, y1, x2, y2 } = line; + const x1 = line.x1 ?? line.get?.('x1'); + const y1 = line.y1 ?? line.get?.('y1'); + const x2 = line.x2 ?? line.get?.('x2'); + const y2 = line.y2 ?? line.get?.('y2'); - // line 객체 포맷 통일 + // line 객체 포맷 통일 (함수 내부용) const currentLine = { ...line, + x1, y1, x2, y2, startPoint: { x: x1, y: y1 }, endPoint: { x: x2, y: y2 } }; @@ -3486,9 +3595,32 @@ function findInteriorPoint(line, polygonLines) { // 2. 끝점이 골짜기인지 확인 const endIsValley = isValleyVertex(currentLine.endPoint, currentLine, polygonLines, false); + // 3. 라인의 방향성 확인 + const isHorizontal = y1 === y2; // 정확히 수평 + const isVertical = x1 === x2; // 정확히 수직 + const isLeftToRight = x1 < x2; // 왼쪽에서 오른쪽 방향 + const isTopToBottom = y1 < y2; // 위에서 아래 방향 + + // 4. 평행한 라인 중에서 start 또는 end가 true인 경우 isParallelWithValley = true + let isParallelWithValley = false; + if (isHorizontal || isVertical) { + if (isHorizontal) { + // 수평선인 경우: 왼쪽이 start, 오른쪽이 end + if ((isLeftToRight && startIsValley) || (!isLeftToRight && endIsValley)) { + isParallelWithValley = true; + } + } else { + // 수직선인 경우: 위가 start, 아래가 end + if ((isTopToBottom && startIsValley) || (!isTopToBottom && endIsValley)) { + isParallelWithValley = true; + } + } + } + return { start: startIsValley, - end: endIsValley + end: endIsValley, + isParallel: isParallelWithValley }; }