All files / src/objects/graph graphCreation.js

0% Statements 0/23
0% Branches 0/11
0% Functions 0/5
0% Lines 0/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48                                                                                               
import { Graph } from './DFS'
import { isSameSegment2D } from '../../geometry'
 
export function generateGraph(polygons, { includingRoofOnRoofs = true } = {}) {
	const edges2D = []
	const g = new Graph()
	polygons
		.filter((poly) => poly.layer == 'roof')
		.forEach((polygon, index) => {
			g.dict.push(polygon.id)
			const outline = polygon.outline
			outline.forEach((point, i) => {
				//check if edge2D already exist
				const nextPoint = outline[(i + 1) % outline.length]
				let edge2D = edges2D.find((edge) => {
					return isSameSegment2D([point, nextPoint], edge.outline)
				})
				if (!edge2D) {
					//create an edge at this position
					edge2D = {}
					edge2D.outline = [point, nextPoint]
					edge2D.belongsTo = []
					edges2D.push(edge2D)
				}
				//add BelongToPolygon
				edge2D.belongsTo.push({
					polygonId: polygon.id,
					index: i,
					polygon
				})
				if (includingRoofOnRoofs && polygon.roofs && polygon.roofs.length == 1) {
					edge2D.belongsTo.push({
						polygonId: polygon.roofs[0].id,
						index: null,
						polygon: polygon.roofs[0]
					})
				}
			})
		})
	for (let k in edges2D) {
		let edge = edges2D[k]
		if (edge.belongsTo.length == 2) {
			g.addEdge(edge.belongsTo[0].polygonId, edge.belongsTo[1].polygonId)
		}
	}
	return g
}