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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | 1x 1x 2x 1x 3x 3x 3x 3x 2x 2x 2x 2x 2x 1x 11x 41x 41x 41x 41x 41x 41x 4x 4x 3x 3x 3x 4x 3x 5x 5x 1x 2x 1x 2x 118x 118x 118x 118x 118x 118x 118x 118x 118x 118x 118x 118x 118x 3x 115x 115x 115x 115x 47x 47x 47x 12x 5x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 6x 1x 35x 68x 68x 68x 6x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 59x 26x 36x 36x | import { GeometryHelper } from "./geometry-helper";
import * as Inputs from "../inputs";
import { Point } from "./point";
import { Vector } from "./vector";
/**
* Contains various methods for lines and segments. Line in bitbybit is a simple object that has start and end point properties.
* { start: [ x, y, z ], end: [ x, y, z ] }
*/
export class Line {
constructor(private readonly vector: Vector, private readonly point: Point, private readonly geometryHelper: GeometryHelper) { }
/**
* Extracts start point from a line.
* Example: line={start:[0,0,0], end:[10,5,0]} → [0,0,0]
* @param inputs a line
* @returns start point
* @group get
* @shortname line start point
* @drawable true
*/
getStartPoint(inputs: Inputs.Line.LineDto): Inputs.Base.Point3 {
return inputs.line.start;
}
/**
* Extracts end point from a line.
* Example: line={start:[0,0,0], end:[10,5,0]} → [10,5,0]
* @param inputs a line
* @returns end point
* @group get
* @shortname line end point
* @drawable true
*/
getEndPoint(inputs: Inputs.Line.LineDto): Inputs.Base.Point3 {
return inputs.line.end;
}
/**
* Calculates length (distance) of a line segment.
* Example: line={start:[0,0,0], end:[3,4,0]} → 5 (using Pythagorean theorem)
* @param inputs a line
* @returns line length
* @group get
* @shortname line length
* @drawable false
*/
length(inputs: Inputs.Line.LineDto): number {
return this.point.distance({ startPoint: inputs.line.start, endPoint: inputs.line.end });
}
/**
* Reverses line direction by swapping start and end points.
* Example: line={start:[0,0,0], end:[10,5,0]} → {start:[10,5,0], end:[0,0,0]}
* @param inputs a line
* @returns reversed line
* @group operations
* @shortname reversed line
* @drawable true
*/
reverse(inputs: Inputs.Line.LineDto): Inputs.Base.Line3 {
return { start: inputs.line.end, end: inputs.line.start };
}
/**
* Applies transformation matrix to line (rotates, scales, or translates both endpoints).
* Example: line={start:[0,0,0], end:[10,0,0]} with translation [5,5,0] → {start:[5,5,0], end:[15,5,0]}
* @param inputs a line
* @returns transformed line
* @group transforms
* @shortname transform line
* @drawable true
*/
transformLine(inputs: Inputs.Line.TransformLineDto): Inputs.Base.Line3 {
const transformation = inputs.transformation;
let transformedControlPoints = [inputs.line.start, inputs.line.end];
transformedControlPoints = this.geometryHelper.transformControlPoints(transformation, transformedControlPoints);
return {
start: transformedControlPoints[0],
end: transformedControlPoints[1]
};
}
/**
* Applies multiple transformations to multiple lines (one transform per line).
* Example: 3 lines with 3 different translation matrices → each line moved independently
* @param inputs lines
* @returns transformed lines
* @group transforms
* @shortname transform lines
* @drawable true
*/
transformsForLines(inputs: Inputs.Line.TransformsLinesDto): Inputs.Base.Line3[] {
return inputs.lines.map((line, index) => {
const transformation = inputs.transformation[index];
let transformedControlPoints = [line.start, line.end];
transformedControlPoints = this.geometryHelper.transformControlPoints(transformation, transformedControlPoints);
return {
start: transformedControlPoints[0],
end: transformedControlPoints[1]
};
});
}
/**
* Creates a line from two points (line object with start and end properties).
* Example: start=[0,0,0], end=[10,5,0] → {start:[0,0,0], end:[10,5,0]}
* @param inputs start and end points of the line
* @returns line
* @group create
* @shortname line
* @drawable true
*/
create(inputs: Inputs.Line.LinePointsDto): Inputs.Base.Line3 {
return {
start: inputs.start,
end: inputs.end,
};
}
/**
* Creates a segment from two points (array format: [start, end]).
* Example: start=[0,0,0], end=[10,5,0] → [[0,0,0], [10,5,0]]
* @param inputs start and end points of the segment
* @returns segment
* @group create
* @shortname segment
* @drawable true
*/
createSegment(inputs: Inputs.Line.LinePointsDto): Inputs.Base.Segment3 {
return [
inputs.start,
inputs.end,
];
}
/**
* Calculates point at parameter t along line segment (0=start, 1=end, linear interpolation).
* Example: line={start:[0,0,0], end:[10,0,0]}, param=0.5 → [5,0,0] (midpoint)
* @param inputs line
* @returns point on line
* @group get
* @shortname point on line
* @drawable true
*/
getPointOnLine(inputs: Inputs.Line.PointOnLineDto): Inputs.Base.Point3 {
// Calculate direction vector of line segment
const point1 = inputs.line.start;
const point2 = inputs.line.end;
const parameter = inputs.param;
const direction = [point2[0] - point1[0], point2[1] - point1[1], point2[2] - point1[2]];
// Calculate point on line segment corresponding to parameter value
const point = [point1[0] + parameter * direction[0], point1[1] + parameter * direction[1], point1[2] + parameter * direction[2]] as Inputs.Base.Point3;
return point;
}
/**
* Creates line segments connecting consecutive points in a list (forms a polyline path).
* Example: points=[[0,0,0], [5,0,0], [5,5,0]] → 2 lines: [0→5] and [5→5,5]
* @param inputs points
* @returns lines
* @group create
* @shortname lines between points
* @drawable true
*/
linesBetweenPoints(inputs: Inputs.Line.PointsLinesDto): Inputs.Base.Line3[] {
const lines = [];
for (let i = 1; i < inputs.points.length; i++) {
const previousPoint = inputs.points[i - 1];
const currentPoint = inputs.points[i];
lines.push({ start: previousPoint, end: currentPoint });
}
return lines;
}
/**
* Creates lines by pairing corresponding start and end points from two arrays.
* Filters out zero-length lines.
* Example: starts=[[0,0,0], [5,0,0]], ends=[[0,5,0], [5,5,0]] → 2 lines connecting paired points
* @param inputs start points and end points
* @returns lines
* @group create
* @shortname start and end points to lines
* @drawable true
*/
linesBetweenStartAndEndPoints(inputs: Inputs.Line.LineStartEndPointsDto): Inputs.Base.Line3[] {
return inputs.startPoints
.map((s, index) => ({ start: s, end: inputs.endPoints[index] }))
.filter(line => this.point.distance({ startPoint: line.start, endPoint: line.end }) !== 0);
}
/**
* Converts line object to segment array format.
* Example: {start:[0,0,0], end:[10,5,0]} → [[0,0,0], [10,5,0]]
* @param inputs line
* @returns segment
* @group convert
* @shortname line to segment
* @drawable false
*/
lineToSegment(inputs: Inputs.Line.LineDto): Inputs.Base.Segment3 {
return [inputs.line.start, inputs.line.end];
}
/**
* Converts multiple line objects to segment array format (batch conversion).
* Example: 3 line objects → 3 segment arrays [[start1, end1], [start2, end2], ...]
* @param inputs lines
* @returns segments
* @group convert
* @shortname lines to segments
* @drawable false
*/
linesToSegments(inputs: Inputs.Line.LinesDto): Inputs.Base.Segment3[] {
return inputs.lines.map(line => [line.start, line.end]);
}
/**
* Converts segment array to line object format.
* Example: [[0,0,0], [10,5,0]] → {start:[0,0,0], end:[10,5,0]}
* @param inputs segment
* @returns line
* @group convert
* @shortname segment to line
* @drawable true
*/
segmentToLine(inputs: Inputs.Line.SegmentDto): Inputs.Base.Line3 {
return { start: inputs.segment[0], end: inputs.segment[1] };
}
/**
* Converts multiple segment arrays to line object format (batch conversion).
* Example: 3 segment arrays → 3 line objects with start/end properties
* @param inputs segments
* @returns lines
* @group convert
* @shortname segments to lines
* @drawable true
*/
segmentsToLines(inputs: Inputs.Line.SegmentsDto): Inputs.Base.Line3[] {
return inputs.segments.map(segment => ({ start: segment[0], end: segment[1] }));
}
/**
* Calculates intersection point of two lines (or segments if checkSegmentsOnly=true).
* Returns undefined if lines are parallel, skew, or segments don't overlap.
* Example: line1={start:[0,0,0], end:[10,0,0]}, line2={start:[5,-5,0], end:[5,5,0]} → [5,0,0]
* @param inputs line1 and line2
* @returns intersection point or undefined if no intersection
* @group intersection
* @shortname line-line int
* @drawable true
*/
lineLineIntersection(inputs: Inputs.Line.LineLineIntersectionDto): Inputs.Base.Point3 | undefined {
const epsilon = inputs.tolerance || 1e-6; // Default tolerance
const checkSegments = inputs.checkSegmentsOnly;
const line1 = inputs.line1;
const line2 = inputs.line2;
// Input validation
Iif (!line1?.start || !line1.end || !line2?.start || !line2.end ||
line1.start.length !== 3 || line1.end.length !== 3 ||
line2.start.length !== 3 || line2.end.length !== 3) {
console.error("Invalid line input to lineLineIntersection");
return undefined;
}
const p1 = line1.start;
const d1 = this.vector.sub({ first: line1.end, second: line1.start }); // Direction vector line 1
const p2 = line2.start;
const d2 = this.vector.sub({ first: line2.end, second: line2.start }); // Direction vector line 2
const p21 = this.vector.sub({ first: p2, second: p1 }); // Vector between start points
// --- Check for Zero-Length Segments ---
const lenSq1 = this.vector.lengthSq({ vector: d1 as Inputs.Base.Vector3 });
const lenSq2 = this.vector.lengthSq({ vector: d2 as Inputs.Base.Vector3 });
// Compare squared length against squared epsilon
if (lenSq1 < epsilon * epsilon || lenSq2 < epsilon * epsilon) {
return undefined;
}
// --- Check for Parallelism ---
const d1_cross_d2 = this.vector.cross({ first: d1, second: d2 });
const crossMagSq = this.vector.lengthSq({ vector: d1_cross_d2 as Inputs.Base.Vector3 });
// Check if squared magnitude of cross product is near zero (relative to segment lengths)
// Use epsilon squared as a base tolerance, potentially scale by magnitudes
const parallel_tolerance_sq = epsilon * epsilon; // May need adjustment: * lenSq1 * lenSq2;
if (crossMagSq < parallel_tolerance_sq) {
// Potentially Parallel or Collinear
// Check if collinear: p21 must be parallel to d1
const p21_cross_d1 = this.vector.cross({ first: p21, second: d1 });
// Use similar tolerance logic for collinear check
const collinear_tolerance_sq = epsilon * epsilon * lenSq1; // Scale by line1 length
if (this.vector.lengthSq({ vector: p21_cross_d1 as Inputs.Base.Vector3 }) < collinear_tolerance_sq) {
// Collinear
if (!checkSegments) {
return p1; // Infinite lines intersect everywhere, return p1 arbitrarily
} else {
// --- Check for Segment Overlap (Collinear case) ---
const d1d1 = lenSq1; // Reuse calculated squared length
// Avoid division by zero if lenSq1 is extremely small (should be caught earlier)
const safe_d1d1 = (d1d1 < epsilon * epsilon) ? 1.0 : d1d1;
const d1p21 = this.vector.dot({ first: d1, second: p21 }); // Dot product d1·(p2-p1)
const t_p2 = d1p21 / safe_d1d1; // Parameter for p2 projected onto line1's frame
const vec_e2_p1 = this.vector.sub({ first: line2.end, second: p1 });
const t_e2 = this.vector.dot({ first: d1, second: vec_e2_p1 }) / safe_d1d1; // Param for e2
const interval2_t = [Math.min(t_p2, t_e2), Math.max(t_p2, t_e2)];
const interval1_t = [0, 1]; // Line1 segment parameter range
const overlap_start = Math.max(interval1_t[0], interval2_t[0]);
const overlap_end = Math.min(interval1_t[1], interval2_t[1]);
// Check for overlap including tolerance
if (overlap_start <= overlap_end + epsilon) {
// Overlap exists, but intersection is a segment, not a single point
return undefined;
} else {
// Collinear but segments do not overlap
return undefined;
}
}
} else {
// Parallel but not collinear
return undefined;
}
}
// --- Lines are NOT Parallel - Check for Skewness using Scalar Triple Product ---
const scalarTripleProduct = this.vector.dot({ first: p21, second: d1_cross_d2 });
// If the scalar triple product is significantly non-zero, the lines are skew.
// Tolerance needs consideration - relates to the "volume" formed by the vectors.
// A simple absolute check against epsilon^3 or similar might work for typical scales.
// Consider scaling tolerance if coordinates can be very large/small.
const skew_tolerance = epsilon * epsilon * epsilon;
if (Math.abs(scalarTripleProduct) > skew_tolerance) {
// Lines are Skew
return undefined;
}
// --- Lines are Intersecting (Coplanar and Non-Parallel) ---
// Calculate intersection parameters t (for line1) and u (for line2)
// We can use the formulas derived earlier, which are valid for intersecting lines.
const d1d1 = lenSq1;
const d2d2 = lenSq2;
const d1d2 = this.vector.dot({ first: d1, second: d2 });
const d1p21 = this.vector.dot({ first: d1, second: p21 });
const d2p21 = this.vector.dot({ first: d2, second: p21 });
// Denominator for parameter calculation (same as crossMagSq, essentially)
const denominator = d1d1 * d2d2 - d1d2 * d1d2;
// Denominator *should* be non-zero based on the parallelism check above,
// but add a defensive check.
Iif (Math.abs(denominator) < epsilon * epsilon) {
console.error("Internal error: Denominator near zero after non-parallel check.");
return undefined; // Should not happen
}
const t = (d2d2 * d1p21 - d1d2 * d2p21) / denominator;
const u = (d1d2 * d1p21 - d1d1 * d2p21) / denominator;
// --- Optional check: Is intersection within segment bounds? ---
if (checkSegments) {
// Check if t and u are within the range [0, 1] (using tolerance)
if (t < -epsilon || t > 1.0 + epsilon || u < -epsilon || u > 1.0 + epsilon) {
return undefined; // Intersection point is outside one or both segments
}
}
// --- Calculate Intersection Point ---
const intersectionPoint = this.getPointOnLine({ param: t, line: line1 });
// Clip near-zero results based on the input epsilon
return [
Math.abs(intersectionPoint[0]) < epsilon ? 0 : intersectionPoint[0],
Math.abs(intersectionPoint[1]) < epsilon ? 0 : intersectionPoint[1],
Math.abs(intersectionPoint[2]) < epsilon ? 0 : intersectionPoint[2],
] as Inputs.Base.Point3;
}
}
|