All files / src/objects Line.js

0% Statements 0/127
0% Branches 0/75
0% Functions 0/23
0% Lines 0/122

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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
import {
	getDistanceBetweenPoints,
	get3DDistanceBetweenPoints,
	getPointOnLine,
	isSamePoint3D,
	isSamePoint2D,
	translate2D,
	midPoint,
	getDataAboutTwo3DLines,
	isPointBetweenSegment,
  verticalProjectionOnPlane
} from '../geometry'
import { v4 as uuidv4 } from 'uuid'
import { Point } from './Point'
import {
	substractVector,
	normalizeVector,
	addVector,
	multiplyVector,
	dotProduct
} from '../vector'
 
export class Line {
	constructor(firstPoint, lastPoint, layer, infiniteLine = false) {
		this.id = uuidv4()
		this.version = 0
		this.type = 'line'
		if (firstPoint && lastPoint) {
			this.outline = [firstPoint, lastPoint]
		} else {
			this.outline = []
		}
		this.layer = layer
		this.visible = true
		this.infiniteLine = infiniteLine
	}
	clone() {
		const line = new Line()
		this.outline.forEach((p) => {
			line.outline.push(new Point(p.x, p.y, p.z))
		})
		line.id = uuidv4()
		line.layer = this.layer
		line.infiniteLine = this.infiniteLine
		return line
	}
	getDirectionVector() {
		return normalizeVector(substractVector(this.outline[1], this.outline[0]))
	}
	getIntersections(object) {
		const intersections = []
		if (!object) return
		if (object.type == 'point') {
			if (isSamePoint3D(this.getProjectedPoint(object), object)) {
				intersections.push({ ...object })
			}
		} else if (object.type == 'line') {
			const data = getDataAboutTwo3DLines(
				this.outline[0],
				this.getDirectionVector(),
				object.outline[0],
				object.getDirectionVector()
			)
			if (!data) {
				return []
			}
			let { distance, N } = data
			if (distance < 0.1 && !!N) {
				intersections.push({ ...N })
			}
		} else if (object.type == 'circle') {
			let P = this.getProjectedPoint(object.center)
			let distance = get3DDistanceBetweenPoints(P, object.center)
			if (distance <= object.radius) {
				let A = addVector(
					P,
					multiplyVector(
						Math.sqrt(Math.pow(object.radius, 2) - Math.pow(distance, 2)),
						this.getDirectionVector()
					)
				)
				let B = addVector(
					P,
					multiplyVector(
						-Math.sqrt(Math.pow(object.radius, 2) - Math.pow(distance, 2)),
						this.getDirectionVector()
					)
				)
				intersections.push({ ...A })
				intersections.push({ ...B })
			}
		}
		return intersections
	}
	pxLength(canvasContext) {
		return (
			getDistanceBetweenPoints(this.outline[0], this.outline[1]) /
      canvasContext.mmPerPx
		)
	}
	mmLength() {
		return get3DDistanceBetweenPoints(this.outline[0], this.outline[1])
	}
 
	getMidPoint() {
		return midPoint(this.outline[0], this.outline[1])
	}
	getAngleDeg() {
		const angle = Math.atan2(
			this.outline[1].y - this.outline[0].y,
			this.outline[0].x - this.outline[1].x
		)
		return (angle * 180) / Math.PI
	}
	getPxDistanceTo(point, canvasContext) {
		const toCanvasRef = canvasContext.toCanvasRef
		const toRealityRef = canvasContext.toRealityRef
 
		if (this.outline.length < 2) {
			console.error('line undefined')
			return null
		}
		if (isSamePoint3D(this.outline[0], this.outline[1])) {
			console.error('line undefined')
			return null
		}
 
		let realityRefPoint = toRealityRef(point)
		let A = { x: this.outline[0].x, y: this.outline[0].y, z: 0 }
		let B = { x: this.outline[1].x, y: this.outline[1].y, z: 0 }
		let M = { x: realityRefPoint.x, y: realityRefPoint.y, z: 0 }
		let P = getPointOnLine(M, A, B)
		P.z = 0
		if (!this.infiniteLine && !isPointBetweenSegment(P, A, B)) {
			return (
				Math.min(
					getDistanceBetweenPoints(M, A),
					getDistanceBetweenPoints(M, B)
				) / canvasContext.mmPerPx
			)
		}
		return getDistanceBetweenPoints(M, P) / canvasContext.mmPerPx
	}
	isInPlane(plane) {
		if (!plane) return false
		const { point, normalVector } = plane
		if (dotProduct(this.getDirectionVector(), normalVector) > 0.1) return false
		if (
			dotProduct(
				normalizeVector(substractVector(point, this.outline[0])),
				normalVector
			) > 0.1
		)
			return false
		return true
	}
	includesPoint2D(point, tolerance) {
		const projection = this.getVerticalProjectedPoint(point)
		return (
			this.getHorizontalDistanceToPoint(point) < tolerance &&
      isPointBetweenSegment(projection, this.outline[0], this.outline[1])
		)
	}
	getDistanceToPoint(point) {
		return get3DDistanceBetweenPoints(point, this.getProjectedPoint(point))
	}
	getHorizontalDistanceToPoint(point) {
		return getDistanceBetweenPoints(
			point,
			this.getVerticalProjectedPoint(point)
		)
	}
	getVerticalProjectedPoint(point) {
		if (this.outline.length == 0) {
			return null
		}
		if (isSamePoint3D(this.outline[0], this.outline[1])) {
			console.error('A == B Can\'t project point on Line')
			return null
		}
		let A = { ...this.outline[0], z: 0 }
		let B = { ...this.outline[1], z: 0 }
		let P = getPointOnLine({ ...point, z: 0 }, A, B)
		let AP = substractVector(P, A)
		let AB = substractVector(B, A)
		let k = dotProduct(AP, AB) / dotProduct(AB, AB)
		let zP = this.outline[0].z + k * (this.outline[1].z - this.outline[0].z)
		P.z = zP
		return P
	}
	getProjectedPoint(point) {
		if (this.outline.length == 0) {
			return null
		}
		if (isSamePoint3D(this.outline[0], this.outline[1])) {
			console.error('A == B Can\'t project point on Line')
			return null
		}
		let P = getPointOnLine(point, this.outline[0], this.outline[1])
		return P
	}
	isEffectivelySplit(polygons) {
		if (this.belongsTo.length != 2) {
			return false
		}
		const polygon0 = polygons.find((p) => p.id == this.belongsTo[0].polygonId)
		const polygon1 = polygons.find((p) => p.id == this.belongsTo[1].polygonId)
		if (!polygon0 || !polygon1) {
			return false
		}
		let A, B, C, D
		if (this.belongsTo[0].index !== null) {
			A = polygon0.outline[this.belongsTo[0].index]
			B =
        polygon0.outline[
          (this.belongsTo[0].index + 1) % polygon0.outline.length
        ]
		}
		if (this.belongsTo[1].index !== null) {
			C = polygon1.outline[this.belongsTo[1].index] 
			D =
        polygon1.outline[
          (this.belongsTo[1].index + 1) % polygon1.outline.length
        ]
		}
		if (!A || !B) {
			A = verticalProjectionOnPlane(
				C,
				polygon0.normalVector,
				polygon0.flatOutline[0]
			)
			B = verticalProjectionOnPlane(
				D,
				polygon0.normalVector,
				polygon0.flatOutline[0]
			)
		}
		if (!C || !D) {
			C = verticalProjectionOnPlane(
				A,
				polygon1.normalVector,
				polygon1.flatOutline[0]
			)
			D = verticalProjectionOnPlane(
				B,
				polygon1.normalVector,
				polygon1.flatOutline[0]
			)
		}
		if (isSamePoint2D(A, C)) {
			const isSplit = Math.abs(A.z - C.z) > 10 || Math.abs(B.z - D.z) > 10
			return isSplit
		} else {
			const isSplit = Math.abs(A.z - D.z) > 10 || Math.abs(B.z - C.z) > 10
			return isSplit
		}
	}
	translate(vectorInMm) {
		this.outline = this.outline.map((point) => {
			return translate2D(point, vectorInMm)
		})
	}
	serialize() {
		if (!this.belongsTo) {
			this.belongsTo = []
		}
		const belongsTo = this.belongsTo.map((item) => {
			return {
				polygon: item.polygon.serialize(),
				polygonId: item.polygonId,
				index: item.index
			}
		})
		const serializedEdge = {
			outline: [this.outline[0], this.outline[1]],
			layer: this.layer,
			type: this.type,
			belongsTo: belongsTo || [],
			itemType: this.itemType
		}
		return JSON.parse(JSON.stringify(serializedEdge))
	}
}