All files / src/objects/derivedState AddMargin.js

0% Statements 0/105
0% Branches 0/28
0% Functions 0/9
0% Lines 0/101

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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181                                                                                                                                                                                                                                                                                                                                                                         
import { multiplyVector } from '../../vector'
import { calculateArea, getMarginPoints, isClockWise, simplifyOutline } from '../../geometry'
import { calculateBestFittingPlanePolygon } from './updateComputedGeometryPolygon'
import { intersectOutlines } from '../../intersectionPolygon'
//update and calculate margins for all polygon
export function updateMarginsOutline(state) {
	state.polygons.forEach((polygon) => {
		updateMarginOutlinePolygon(polygon)
	})
	return state
}
 
export function updateMarginOutlinePolygon(polygon) {
	if (polygon.layer == 'roof') {
		return updateRoofMarginOutline(polygon)
	} else if (polygon.layer == 'obstacle') {
		return updateObstacleMarginOutline(polygon)
	} else {
		return polygon
	}
}
//FYI: polygon.margins={
//  distances:array[#point] //primitives
//  isSameMargin:Boolean //primitives
//  sameMargin:Number //primitives
//  outterOutline:array[Vector3D] //derived (for roof this is the wall)
//  innerOutline:array[Vector3D] //derived (for obstacle this is the wall)
//  }
 
export function updateRoofMarginOutline(polygon) {
	let clockwiseNormalVector = polygon.normalVector
	if (!polygon.isClockwise) {
		clockwiseNormalVector = multiplyVector(-1, clockwiseNormalVector)
	}
	//outterOutline is the outline close to the wall
	polygon.margins.outterOutline = polygon.flatOutline
	polygon.margins.innerOutline = []
	let innerOutlinePerIndex = []
	const length = polygon.outline.length
	polygon.margins.distances = polygon.margins.distances.slice(0, length)
	for (let index = 0; index < length; index++) {
		if (polygon.margins.distances[index] == undefined) {
			polygon.margins.distances[index] = polygon.margins.sameMargin
		}
		const B = polygon.flatOutline[index]
		//A,B,C three consecutive points on the polygon.
		//n is a vector in the plan normal to AB of length margin[A] directed towards the inside
		//m is a vector in the plan normal to BC of length margin[B] directed towards the inside
		//K are the margin corner outline linked with B
		const A = polygon.flatOutline[(index - 1 + length) % length]
		const C = polygon.flatOutline[(index + 1) % length]
		//filling the margins values if not initiated.
		let marginA
		let marginB
		if (polygon.margins.isSameMargin) {
			marginA = polygon.margins.sameMargin
			marginB = polygon.margins.sameMargin
			polygon.margins.distances[index] = polygon.margins.sameMargin
		} else {
			if (
				!polygon.margins.distances[(index - 1 + length) % length] === undefined
			) {
				polygon.margins.distances[(index - 1 + length) % length] =
          polygon.margins.sameMargin
			}
			if (!polygon.margins.distances[index] === undefined) {
				polygon.margins.distances[index] = polygon.margins.sameMargin
			}
			marginA = polygon.margins.distances[(index - 1 + length) % length]
			marginB = polygon.margins.distances[index]
		}
		let cornerPoints = getMarginPoints(A, B, C, marginA, marginB, clockwiseNormalVector)
		
		polygon.margins.innerOutline.push(...cornerPoints[0],...cornerPoints[1])
		innerOutlinePerIndex[index] = cornerPoints
	}
	polygon.margins.innerMargins = innerOutlinePerIndex.map((cornerOutline,index)=>{
		let marginOutline=[]
		marginOutline.push(polygon.flatOutline[index])
		marginOutline.push(...cornerOutline[1])
		const nextInnerOutlinePerIndex = innerOutlinePerIndex[(index+1)%innerOutlinePerIndex.length]
		marginOutline.push(...nextInnerOutlinePerIndex[0])
		marginOutline.push(polygon.flatOutline[(index+1)%polygon.flatOutline.length])
		const intersections = intersectOutlines(marginOutline,polygon.flatOutline)
		if(intersections.length>0){
			intersections.sort((a,b)=>calculateArea(b)-calculateArea(a))
			marginOutline = intersections[0]
		}else{
			marginOutline = []
		}
		return marginOutline
	})
	polygon.margins.outterMargins = []
	return polygon
}
 
export function updateObstacleMarginOutline(polygon) {
	let clockwiseNormalVector = polygon.normalVector
	if (polygon.isClockwise) {
		clockwiseNormalVector = multiplyVector(-1, clockwiseNormalVector)
	}
	//outterOutline is the outline close to the wall
	polygon.margins.innerOutline = polygon.flatOutline
	polygon.margins.outterOutline = []
	let outterOutlinePerIndex = []
	const length = polygon.margins.innerOutline.length
	polygon.margins.distances = polygon.margins.distances.slice(0, length)
	for (let index = 0; index < length; index++) {
		if (polygon.margins.distances[index] == undefined) {
			polygon.margins.distances[index] = polygon.margins.sameMargin
		}
		const B = polygon.flatOutline[index]
		//A,B,C three consecutive points on the polygon.
		//n is a vector in the plan normal to AB of length margin[A] directed towards the inside
		//m is a vector in the plan normal to BC of length margin[B] directed towards the inside
		//K is the margin outline linked with B
		const A = polygon.flatOutline[(index - 1 + length) % length]
		const C = polygon.flatOutline[(index + 1) % length]
		//filling the margins values if not initiated.
		let marginA
		let marginB
		if (polygon.margins.isSameMargin) {
			marginA = polygon.margins.sameMargin
			marginB = polygon.margins.sameMargin
			polygon.margins.distances[index] = polygon.margins.sameMargin
		} else {
			if (
				polygon.margins.distances[(index - 1 + length) % length] === undefined
			) {
				polygon.margins.distances[(index - 1 + length) % length] =
          polygon.margins.sameMargin
			}
			if (polygon.margins.distances[index] === undefined) {
				polygon.margins.distances[index] = polygon.margins.sameMargin
			}
			marginA = polygon.margins.distances[(index - 1 + length) % length]
			marginB = polygon.margins.distances[index]
		}
		let cornerPoints = getMarginPoints(A, B, C, marginA, marginB, clockwiseNormalVector)
		polygon.margins.outterOutline.push(...cornerPoints[0],...cornerPoints[1])
		outterOutlinePerIndex[index]=cornerPoints
	}
	polygon.margins.outterMargins = outterOutlinePerIndex.map((cornerOutline,index)=>{
		const marginOutline=[]
		marginOutline.push(polygon.flatOutline[index])
		marginOutline.push(...cornerOutline[1])
		const nextOutterOutlinePerIndex = outterOutlinePerIndex[(index+1)%outterOutlinePerIndex.length]
		marginOutline.push(...nextOutterOutlinePerIndex[0])
		marginOutline.push(polygon.flatOutline[(index+1)%polygon.flatOutline.length])
		return marginOutline
	})
	polygon.margins.innerMargins = []
	return polygon
}
 
export function getOutterOutlineWithConstantMargin(outline, margin) {
	const isClockwise = isClockWise(outline)
	const { projectedOutline, normalVector } =
    calculateBestFittingPlanePolygon(outline)
	let clockwiseNormalVector = normalVector
	if (isClockwise) {
		clockwiseNormalVector = multiplyVector(-1, clockwiseNormalVector)
	}
	//outterOutline is the outline close to the wall
	const outterOutline = []
	const length = outline.length
	for (let index = 0; index < length; index++) {
		const B = projectedOutline[index]
		//A,B,C three consecutive points on the polygon.
		//n is a vector in the plan normal to AB of length margin[A] directed towards the inside
		//m is a vector in the plan normal to BC of length margin[B] directed towards the inside
		//K is the margin outline linked with B
		const A = projectedOutline[(index - 1 + length) % length]
		const C = projectedOutline[(index + 1) % length]
		//filling the margins values if not initiated.
		let cornerPoints = getMarginPoints(A, B, C, margin, margin, clockwiseNormalVector)
		outterOutline.push(...cornerPoints[0],...cornerPoints[1])
	}
	return outterOutline
}