오차범위 계산 추가
This commit is contained in:
parent
69763dd413
commit
a45bba3a7b
@ -822,10 +822,10 @@ export const usePolygon = () => {
|
|||||||
line.endPoint = endPoint
|
line.endPoint = endPoint
|
||||||
})
|
})
|
||||||
|
|
||||||
polygonLines.forEach((line) => {
|
/*polygonLines.forEach((line) => {
|
||||||
line.set({ strokeWidth: 10 })
|
line.set({ strokeWidth: 10 })
|
||||||
canvas.add(line)
|
canvas.add(line)
|
||||||
})
|
})*/
|
||||||
canvas.renderAll()
|
canvas.renderAll()
|
||||||
|
|
||||||
polygonLines.forEach((line) => {
|
polygonLines.forEach((line) => {
|
||||||
@ -1148,31 +1148,47 @@ export const usePolygon = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getSplitRoofsPoints = (startLines, allLines, innerLines, uniquePoints) => {
|
const getSplitRoofsPoints = (startLines, allLines, innerLines, uniquePoints) => {
|
||||||
|
// ==== Utility functions ====
|
||||||
|
|
||||||
|
function isSamePoint(p1, p2, epsilon = 1) {
|
||||||
|
return Math.abs(p1.x - p2.x) <= epsilon && Math.abs(p1.y - p2.y) <= epsilon
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizePoint(p, epsilon = 1) {
|
||||||
|
return {
|
||||||
|
x: Math.round(p.x / epsilon) * epsilon,
|
||||||
|
y: Math.round(p.y / epsilon) * epsilon,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function pointToKey(p, epsilon = 1) {
|
||||||
|
const norm = normalizePoint(p, epsilon)
|
||||||
|
return `${norm.x},${norm.y}`
|
||||||
|
}
|
||||||
|
|
||||||
// 거리 계산
|
// 거리 계산
|
||||||
function calcDistance(p1, p2) {
|
function calcDistance(p1, p2) {
|
||||||
return Math.hypot(p2.x - p1.x, p2.y - p1.y)
|
return Math.hypot(p2.x - p1.x, p2.y - p1.y)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 바로 연결 체크
|
// ==== Direct edge check ====
|
||||||
function isDirectlyConnected(start, end, graph) {
|
|
||||||
const startKey = `${start.x},${start.y}`
|
|
||||||
if (!graph[startKey]) return false
|
|
||||||
return graph[startKey].some((neighbor) => neighbor.point.x === end.x && neighbor.point.y === end.y)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dijkstra 최단 경로
|
function isDirectlyConnected(start, end, graph, epsilon = 1) {
|
||||||
function findShortestPath(start, end, graph) {
|
const startKey = pointToKey(start, epsilon)
|
||||||
const startKey = `${start.x},${start.y}`
|
return (graph[startKey] || []).some((neighbor) => isSamePoint(neighbor.point, end, epsilon))
|
||||||
const endKey = `${end.x},${end.y}`
|
}
|
||||||
|
// ==== Dijkstra pathfinding ====
|
||||||
|
|
||||||
|
function findShortestPath(start, end, graph, epsilon = 1) {
|
||||||
|
const startKey = pointToKey(start, epsilon)
|
||||||
|
const endKey = pointToKey(end, epsilon)
|
||||||
|
|
||||||
const distances = {}
|
const distances = {}
|
||||||
const previous = {}
|
const previous = {}
|
||||||
const visited = new Set()
|
const visited = new Set()
|
||||||
const queue = [{ key: startKey, dist: 0 }]
|
const queue = [{ key: startKey, dist: 0 }]
|
||||||
|
|
||||||
for (const key in graph) {
|
for (const key in graph) distances[key] = Infinity
|
||||||
distances[key] = Infinity
|
|
||||||
}
|
|
||||||
distances[startKey] = 0
|
distances[startKey] = 0
|
||||||
|
|
||||||
while (queue.length > 0) {
|
while (queue.length > 0) {
|
||||||
@ -1181,8 +1197,8 @@ export const usePolygon = () => {
|
|||||||
if (visited.has(key)) continue
|
if (visited.has(key)) continue
|
||||||
visited.add(key)
|
visited.add(key)
|
||||||
|
|
||||||
for (const neighbor of graph[key]) {
|
for (const neighbor of graph[key] || []) {
|
||||||
const neighborKey = `${neighbor.point.x},${neighbor.point.y}`
|
const neighborKey = pointToKey(neighbor.point, epsilon)
|
||||||
const alt = distances[key] + neighbor.distance
|
const alt = distances[key] + neighbor.distance
|
||||||
if (alt < distances[neighborKey]) {
|
if (alt < distances[neighborKey]) {
|
||||||
distances[neighborKey] = alt
|
distances[neighborKey] = alt
|
||||||
@ -1192,35 +1208,37 @@ export const usePolygon = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 경로 복구
|
|
||||||
const path = []
|
const path = []
|
||||||
let currentKey = endKey
|
let currentKey = endKey
|
||||||
|
|
||||||
if (!previous[currentKey]) {
|
if (!previous[currentKey]) return null
|
||||||
return null // 경로 없음
|
|
||||||
}
|
|
||||||
|
|
||||||
while (currentKey !== startKey) {
|
while (currentKey !== startKey) {
|
||||||
const [x, y] = currentKey.split(',').map(Number)
|
const [x, y] = currentKey.split(',').map(Number)
|
||||||
path.unshift({ x, y })
|
path.unshift({ x, y })
|
||||||
currentKey = previous[currentKey]
|
currentKey = previous[currentKey]
|
||||||
}
|
}
|
||||||
path.unshift(start)
|
|
||||||
|
const [sx, sy] = startKey.split(',').map(Number)
|
||||||
|
path.unshift({ x: sx, y: sy })
|
||||||
|
|
||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
// 최종 함수
|
// 최종 함수
|
||||||
function getPath(start, end, graph) {
|
function getPath(start, end, graph, epsilon = 1) {
|
||||||
if (isDirectlyConnected(start, end, graph)) {
|
if (isDirectlyConnected(start, end, graph, epsilon)) {
|
||||||
|
console.log('직선 연결 있음. 무시.')
|
||||||
return null
|
return null
|
||||||
} else {
|
|
||||||
const path = findShortestPath(start, end, graph)
|
|
||||||
if (!path || path.length < 3) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
return path
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const path = findShortestPath(start, end, graph, epsilon)
|
||||||
|
if (!path || path.length < 3) {
|
||||||
|
console.log('경로 존재하나 3개 미만 좌표. 무시.')
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
const roofs = []
|
const roofs = []
|
||||||
@ -1235,8 +1253,8 @@ export const usePolygon = () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
for (const [p1, p2] of edges) {
|
for (const [p1, p2] of edges) {
|
||||||
const key1 = `${p1.x},${p1.y}`
|
const key1 = pointToKey(p1)
|
||||||
const key2 = `${p2.x},${p2.y}`
|
const key2 = pointToKey(p2)
|
||||||
const distance = calcDistance(p1, p2)
|
const distance = calcDistance(p1, p2)
|
||||||
|
|
||||||
if (!graph[key1]) graph[key1] = []
|
if (!graph[key1]) graph[key1] = []
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user