@shapediver/viewer.features.drawing-tools 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +73 -0
- package/README.md +161 -0
- package/dist/api/implementation/DrawingToolsApi.d.ts +31 -0
- package/dist/api/implementation/DrawingToolsApi.d.ts.map +1 -0
- package/dist/api/implementation/DrawingToolsApi.js +111 -0
- package/dist/api/implementation/DrawingToolsApi.js.map +1 -0
- package/dist/api/implementation/restrictions/AbstractRestrictionApi.d.ts +10 -0
- package/dist/api/implementation/restrictions/AbstractRestrictionApi.d.ts.map +1 -0
- package/dist/api/implementation/restrictions/AbstractRestrictionApi.js +38 -0
- package/dist/api/implementation/restrictions/AbstractRestrictionApi.js.map +1 -0
- package/dist/api/implementation/restrictions/AbstractSnapRestrictionApi.d.ts +11 -0
- package/dist/api/implementation/restrictions/AbstractSnapRestrictionApi.d.ts.map +1 -0
- package/dist/api/implementation/restrictions/AbstractSnapRestrictionApi.js +40 -0
- package/dist/api/implementation/restrictions/AbstractSnapRestrictionApi.js.map +1 -0
- package/dist/api/implementation/restrictions/axis/AxisRestrictionApi.d.ts +7 -0
- package/dist/api/implementation/restrictions/axis/AxisRestrictionApi.d.ts.map +1 -0
- package/dist/api/implementation/restrictions/axis/AxisRestrictionApi.js +24 -0
- package/dist/api/implementation/restrictions/axis/AxisRestrictionApi.js.map +1 -0
- package/dist/api/implementation/restrictions/geometry/GeometryRestrictionApi.d.ts +15 -0
- package/dist/api/implementation/restrictions/geometry/GeometryRestrictionApi.d.ts.map +1 -0
- package/dist/api/implementation/restrictions/geometry/GeometryRestrictionApi.js +54 -0
- package/dist/api/implementation/restrictions/geometry/GeometryRestrictionApi.js.map +1 -0
- package/dist/api/implementation/restrictions/plane/PlaneRestrictionApi.d.ts +18 -0
- package/dist/api/implementation/restrictions/plane/PlaneRestrictionApi.d.ts.map +1 -0
- package/dist/api/implementation/restrictions/plane/PlaneRestrictionApi.js +61 -0
- package/dist/api/implementation/restrictions/plane/PlaneRestrictionApi.js.map +1 -0
- package/dist/api/implementation/restrictions/plane/snap/AngularRestrictionApi.d.ts +10 -0
- package/dist/api/implementation/restrictions/plane/snap/AngularRestrictionApi.d.ts.map +1 -0
- package/dist/api/implementation/restrictions/plane/snap/AngularRestrictionApi.js +40 -0
- package/dist/api/implementation/restrictions/plane/snap/AngularRestrictionApi.js.map +1 -0
- package/dist/api/implementation/restrictions/plane/snap/GridRestrictionApi.d.ts +10 -0
- package/dist/api/implementation/restrictions/plane/snap/GridRestrictionApi.d.ts.map +1 -0
- package/dist/api/implementation/restrictions/plane/snap/GridRestrictionApi.js +40 -0
- package/dist/api/implementation/restrictions/plane/snap/GridRestrictionApi.js.map +1 -0
- package/dist/api/interfaces/IDrawingToolsApi.d.ts +89 -0
- package/dist/api/interfaces/IDrawingToolsApi.d.ts.map +1 -0
- package/dist/api/interfaces/IDrawingToolsApi.js +3 -0
- package/dist/api/interfaces/IDrawingToolsApi.js.map +1 -0
- package/dist/api/interfaces/IRestrictionApi.d.ts +11 -0
- package/dist/api/interfaces/IRestrictionApi.d.ts.map +1 -0
- package/dist/api/interfaces/IRestrictionApi.js +3 -0
- package/dist/api/interfaces/IRestrictionApi.js.map +1 -0
- package/dist/api/interfaces/ISnapRestrictionApi.d.ts +13 -0
- package/dist/api/interfaces/ISnapRestrictionApi.d.ts.map +1 -0
- package/dist/api/interfaces/ISnapRestrictionApi.js +3 -0
- package/dist/api/interfaces/ISnapRestrictionApi.js.map +1 -0
- package/dist/business/implementation/DrawingToolsManager.d.ts +95 -0
- package/dist/business/implementation/DrawingToolsManager.d.ts.map +1 -0
- package/dist/business/implementation/DrawingToolsManager.js +544 -0
- package/dist/business/implementation/DrawingToolsManager.js.map +1 -0
- package/dist/business/implementation/managers/HistoryManager.d.ts +19 -0
- package/dist/business/implementation/managers/HistoryManager.d.ts.map +1 -0
- package/dist/business/implementation/managers/HistoryManager.js +89 -0
- package/dist/business/implementation/managers/HistoryManager.js.map +1 -0
- package/dist/business/implementation/managers/TextVisualizationManager.d.ts +14 -0
- package/dist/business/implementation/managers/TextVisualizationManager.d.ts.map +1 -0
- package/dist/business/implementation/managers/TextVisualizationManager.js +228 -0
- package/dist/business/implementation/managers/TextVisualizationManager.js.map +1 -0
- package/dist/business/implementation/managers/geometry/GeometryManager.d.ts +23 -0
- package/dist/business/implementation/managers/geometry/GeometryManager.d.ts.map +1 -0
- package/dist/business/implementation/managers/geometry/GeometryManager.js +82 -0
- package/dist/business/implementation/managers/geometry/GeometryManager.js.map +1 -0
- package/dist/business/implementation/managers/geometry/GeometryMathManager.d.ts +73 -0
- package/dist/business/implementation/managers/geometry/GeometryMathManager.d.ts.map +1 -0
- package/dist/business/implementation/managers/geometry/GeometryMathManager.js +230 -0
- package/dist/business/implementation/managers/geometry/GeometryMathManager.js.map +1 -0
- package/dist/business/implementation/managers/geometry/GeometryState.d.ts +46 -0
- package/dist/business/implementation/managers/geometry/GeometryState.d.ts.map +1 -0
- package/dist/business/implementation/managers/geometry/GeometryState.js +323 -0
- package/dist/business/implementation/managers/geometry/GeometryState.js.map +1 -0
- package/dist/business/implementation/managers/geometry/helpers/GeometryManagerHelper.d.ts +16 -0
- package/dist/business/implementation/managers/geometry/helpers/GeometryManagerHelper.d.ts.map +1 -0
- package/dist/business/implementation/managers/geometry/helpers/GeometryManagerHelper.js +157 -0
- package/dist/business/implementation/managers/geometry/helpers/GeometryManagerHelper.js.map +1 -0
- package/dist/business/implementation/managers/interaction/EventManager.d.ts +26 -0
- package/dist/business/implementation/managers/interaction/EventManager.d.ts.map +1 -0
- package/dist/business/implementation/managers/interaction/EventManager.js +63 -0
- package/dist/business/implementation/managers/interaction/EventManager.js.map +1 -0
- package/dist/business/implementation/managers/interaction/InteractionManager.d.ts +39 -0
- package/dist/business/implementation/managers/interaction/InteractionManager.d.ts.map +1 -0
- package/dist/business/implementation/managers/interaction/InteractionManager.js +237 -0
- package/dist/business/implementation/managers/interaction/InteractionManager.js.map +1 -0
- package/dist/business/implementation/managers/interaction/RestrictionManager.d.ts +19 -0
- package/dist/business/implementation/managers/interaction/RestrictionManager.d.ts.map +1 -0
- package/dist/business/implementation/managers/interaction/RestrictionManager.js +117 -0
- package/dist/business/implementation/managers/interaction/RestrictionManager.js.map +1 -0
- package/dist/business/implementation/managers/interaction/handlers/DeletionInteractionHandler.d.ts +10 -0
- package/dist/business/implementation/managers/interaction/handlers/DeletionInteractionHandler.d.ts.map +1 -0
- package/dist/business/implementation/managers/interaction/handlers/DeletionInteractionHandler.js +52 -0
- package/dist/business/implementation/managers/interaction/handlers/DeletionInteractionHandler.js.map +1 -0
- package/dist/business/implementation/managers/interaction/handlers/InsertionInteractionHandler.d.ts +14 -0
- package/dist/business/implementation/managers/interaction/handlers/InsertionInteractionHandler.d.ts.map +1 -0
- package/dist/business/implementation/managers/interaction/handlers/InsertionInteractionHandler.js +135 -0
- package/dist/business/implementation/managers/interaction/handlers/InsertionInteractionHandler.js.map +1 -0
- package/dist/business/implementation/managers/interaction/handlers/MidPointInteractionHandler.d.ts +16 -0
- package/dist/business/implementation/managers/interaction/handlers/MidPointInteractionHandler.d.ts.map +1 -0
- package/dist/business/implementation/managers/interaction/handlers/MidPointInteractionHandler.js +113 -0
- package/dist/business/implementation/managers/interaction/handlers/MidPointInteractionHandler.js.map +1 -0
- package/dist/business/implementation/managers/interaction/helpers/InteractionManagerHelper.d.ts +58 -0
- package/dist/business/implementation/managers/interaction/helpers/InteractionManagerHelper.d.ts.map +1 -0
- package/dist/business/implementation/managers/interaction/helpers/InteractionManagerHelper.js +377 -0
- package/dist/business/implementation/managers/interaction/helpers/InteractionManagerHelper.js.map +1 -0
- package/dist/business/implementation/managers/interaction/restrictions/AbstractRestriction.d.ts +19 -0
- package/dist/business/implementation/managers/interaction/restrictions/AbstractRestriction.d.ts.map +1 -0
- package/dist/business/implementation/managers/interaction/restrictions/AbstractRestriction.js +105 -0
- package/dist/business/implementation/managers/interaction/restrictions/AbstractRestriction.js.map +1 -0
- package/dist/business/implementation/managers/interaction/restrictions/axis/AxisRestriction.d.ts +22 -0
- package/dist/business/implementation/managers/interaction/restrictions/axis/AxisRestriction.d.ts.map +1 -0
- package/dist/business/implementation/managers/interaction/restrictions/axis/AxisRestriction.js +96 -0
- package/dist/business/implementation/managers/interaction/restrictions/axis/AxisRestriction.js.map +1 -0
- package/dist/business/implementation/managers/interaction/restrictions/geometry/GeometryRestriction.d.ts +39 -0
- package/dist/business/implementation/managers/interaction/restrictions/geometry/GeometryRestriction.d.ts.map +1 -0
- package/dist/business/implementation/managers/interaction/restrictions/geometry/GeometryRestriction.js +215 -0
- package/dist/business/implementation/managers/interaction/restrictions/geometry/GeometryRestriction.js.map +1 -0
- package/dist/business/implementation/managers/interaction/restrictions/plane/PlaneRestriction.d.ts +58 -0
- package/dist/business/implementation/managers/interaction/restrictions/plane/PlaneRestriction.d.ts.map +1 -0
- package/dist/business/implementation/managers/interaction/restrictions/plane/PlaneRestriction.js +180 -0
- package/dist/business/implementation/managers/interaction/restrictions/plane/PlaneRestriction.js.map +1 -0
- package/dist/business/implementation/managers/interaction/restrictions/plane/snap/AngularRestriction.d.ts +38 -0
- package/dist/business/implementation/managers/interaction/restrictions/plane/snap/AngularRestriction.d.ts.map +1 -0
- package/dist/business/implementation/managers/interaction/restrictions/plane/snap/AngularRestriction.js +308 -0
- package/dist/business/implementation/managers/interaction/restrictions/plane/snap/AngularRestriction.js.map +1 -0
- package/dist/business/implementation/managers/interaction/restrictions/plane/snap/GridRestriction.d.ts +37 -0
- package/dist/business/implementation/managers/interaction/restrictions/plane/snap/GridRestriction.d.ts.map +1 -0
- package/dist/business/implementation/managers/interaction/restrictions/plane/snap/GridRestriction.js +228 -0
- package/dist/business/implementation/managers/interaction/restrictions/plane/snap/GridRestriction.js.map +1 -0
- package/dist/business/implementation/utils/numberCleaner.d.ts +2 -0
- package/dist/business/implementation/utils/numberCleaner.d.ts.map +1 -0
- package/dist/business/implementation/utils/numberCleaner.js +9 -0
- package/dist/business/implementation/utils/numberCleaner.js.map +1 -0
- package/dist/business/interfaces/IDrawingToolsManager.d.ts +270 -0
- package/dist/business/interfaces/IDrawingToolsManager.d.ts.map +1 -0
- package/dist/business/interfaces/IDrawingToolsManager.js +16 -0
- package/dist/business/interfaces/IDrawingToolsManager.js.map +1 -0
- package/dist/business/interfaces/IManager.d.ts +4 -0
- package/dist/business/interfaces/IManager.d.ts.map +1 -0
- package/dist/business/interfaces/IManager.js +3 -0
- package/dist/business/interfaces/IManager.js.map +1 -0
- package/dist/business/interfaces/IRestriction.d.ts +41 -0
- package/dist/business/interfaces/IRestriction.d.ts.map +1 -0
- package/dist/business/interfaces/IRestriction.js +13 -0
- package/dist/business/interfaces/IRestriction.js.map +1 -0
- package/dist/business/interfaces/IRestrictionBase.d.ts +19 -0
- package/dist/business/interfaces/IRestrictionBase.d.ts.map +1 -0
- package/dist/business/interfaces/IRestrictionBase.js +5 -0
- package/dist/business/interfaces/IRestrictionBase.js.map +1 -0
- package/dist/business/interfaces/ISnapRestriction.d.ts +50 -0
- package/dist/business/interfaces/ISnapRestriction.d.ts.map +1 -0
- package/dist/business/interfaces/ISnapRestriction.js +4 -0
- package/dist/business/interfaces/ISnapRestriction.js.map +1 -0
- package/dist/business/interfaces/events/EventResponseMapping.d.ts +19 -0
- package/dist/business/interfaces/events/EventResponseMapping.d.ts.map +1 -0
- package/dist/business/interfaces/events/EventResponseMapping.js +4 -0
- package/dist/business/interfaces/events/EventResponseMapping.js.map +1 -0
- package/dist/business/interfaces/events/IDrawingToolsEvent.d.ts +12 -0
- package/dist/business/interfaces/events/IDrawingToolsEvent.d.ts.map +1 -0
- package/dist/business/interfaces/events/IDrawingToolsEvent.js +3 -0
- package/dist/business/interfaces/events/IDrawingToolsEvent.js.map +1 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +45 -0
- package/dist/index.js.map +1 -0
- package/dist/three/CSS2DRenderer.d.ts +32 -0
- package/dist/three/CSS2DRenderer.d.ts.map +1 -0
- package/dist/three/CSS2DRenderer.js +137 -0
- package/dist/three/CSS2DRenderer.js.map +1 -0
- package/package.json +55 -0
- package/src/api/implementation/DrawingToolsApi.ts +135 -0
- package/src/api/implementation/restrictions/AbstractRestrictionApi.ts +34 -0
- package/src/api/implementation/restrictions/AbstractSnapRestrictionApi.ts +36 -0
- package/src/api/implementation/restrictions/axis/AxisRestrictionApi.ts +19 -0
- package/src/api/implementation/restrictions/geometry/GeometryRestrictionApi.ts +56 -0
- package/src/api/implementation/restrictions/plane/PlaneRestrictionApi.ts +63 -0
- package/src/api/implementation/restrictions/plane/snap/AngularRestrictionApi.ts +35 -0
- package/src/api/implementation/restrictions/plane/snap/GridRestrictionApi.ts +35 -0
- package/src/api/interfaces/IDrawingToolsApi.ts +99 -0
- package/src/api/interfaces/IRestrictionApi.ts +15 -0
- package/src/api/interfaces/ISnapRestrictionApi.ts +18 -0
- package/src/business/implementation/DrawingToolsManager.ts +619 -0
- package/src/business/implementation/managers/HistoryManager.ts +101 -0
- package/src/business/implementation/managers/TextVisualizationManager.ts +245 -0
- package/src/business/implementation/managers/geometry/GeometryManager.ts +95 -0
- package/src/business/implementation/managers/geometry/GeometryMathManager.ts +273 -0
- package/src/business/implementation/managers/geometry/GeometryState.ts +436 -0
- package/src/business/implementation/managers/geometry/helpers/GeometryManagerHelper.ts +170 -0
- package/src/business/implementation/managers/interaction/EventManager.ts +80 -0
- package/src/business/implementation/managers/interaction/InteractionManager.ts +268 -0
- package/src/business/implementation/managers/interaction/RestrictionManager.ts +135 -0
- package/src/business/implementation/managers/interaction/handlers/DeletionInteractionHandler.ts +48 -0
- package/src/business/implementation/managers/interaction/handlers/InsertionInteractionHandler.ts +149 -0
- package/src/business/implementation/managers/interaction/handlers/MidPointInteractionHandler.ts +127 -0
- package/src/business/implementation/managers/interaction/helpers/InteractionManagerHelper.ts +411 -0
- package/src/business/implementation/managers/interaction/restrictions/AbstractRestriction.ts +99 -0
- package/src/business/implementation/managers/interaction/restrictions/axis/AxisRestriction.ts +107 -0
- package/src/business/implementation/managers/interaction/restrictions/geometry/GeometryRestriction.ts +237 -0
- package/src/business/implementation/managers/interaction/restrictions/plane/PlaneRestriction.ts +257 -0
- package/src/business/implementation/managers/interaction/restrictions/plane/snap/AngularRestriction.ts +384 -0
- package/src/business/implementation/managers/interaction/restrictions/plane/snap/GridRestriction.ts +262 -0
- package/src/business/implementation/utils/numberCleaner.ts +5 -0
- package/src/business/interfaces/IDrawingToolsManager.ts +313 -0
- package/src/business/interfaces/IManager.ts +7 -0
- package/src/business/interfaces/IRestriction.ts +64 -0
- package/src/business/interfaces/IRestrictionBase.ts +33 -0
- package/src/business/interfaces/ISnapRestriction.ts +70 -0
- package/src/business/interfaces/events/EventResponseMapping.ts +19 -0
- package/src/business/interfaces/events/IDrawingToolsEvent.ts +16 -0
- package/src/index.ts +72 -0
- package/src/three/CSS2DRenderer.ts +212 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { DrawingToolsManager } from '../../DrawingToolsManager';
|
|
2
|
+
import { IManager } from '../../../interfaces/IManager';
|
|
3
|
+
import { IRay, IViewportApi } from '@shapediver/viewer.features.interaction';
|
|
4
|
+
import { Settings } from '../../../interfaces/IDrawingToolsManager';
|
|
5
|
+
import { vec3 } from 'gl-matrix';
|
|
6
|
+
|
|
7
|
+
export class GeometryMathManager implements IManager {
|
|
8
|
+
// #region Properties (3)
|
|
9
|
+
|
|
10
|
+
readonly #drawingToolsManager: DrawingToolsManager;
|
|
11
|
+
readonly #settings: Settings;
|
|
12
|
+
readonly #viewport: IViewportApi;
|
|
13
|
+
|
|
14
|
+
// #endregion Properties (3)
|
|
15
|
+
|
|
16
|
+
// #region Constructors (1)
|
|
17
|
+
|
|
18
|
+
constructor(drawingToolsManager: DrawingToolsManager) {
|
|
19
|
+
this.#drawingToolsManager = drawingToolsManager;
|
|
20
|
+
this.#viewport = drawingToolsManager.viewport;
|
|
21
|
+
this.#settings = drawingToolsManager.settings;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// #endregion Constructors (1)
|
|
25
|
+
|
|
26
|
+
// #region Public Methods (7)
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Check which distances of lines to ray
|
|
30
|
+
*
|
|
31
|
+
* @param ray
|
|
32
|
+
* @returns
|
|
33
|
+
*/
|
|
34
|
+
public checkLineDistances(ray: IRay): { index: number[]; distance: number; }[] | undefined {
|
|
35
|
+
const positionArray = this.#drawingToolsManager.positionArray;
|
|
36
|
+
const indicesArrayLines = this.#drawingToolsManager.indicesArrayLines;
|
|
37
|
+
|
|
38
|
+
// if there are no line array indices, return
|
|
39
|
+
if (!indicesArrayLines) return;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Calculate line distances to ray
|
|
43
|
+
*/
|
|
44
|
+
const distances: {
|
|
45
|
+
index: number[];
|
|
46
|
+
distance: number;
|
|
47
|
+
}[] = [];
|
|
48
|
+
|
|
49
|
+
for (let i = 0; i < indicesArrayLines.length; i += 2) {
|
|
50
|
+
const firstIndex = indicesArrayLines.at(i)!;
|
|
51
|
+
const secondIndex = indicesArrayLines.at(i + 1)!;
|
|
52
|
+
const lineStart = vec3.fromValues(positionArray.at(firstIndex * 3)!, positionArray.at(firstIndex * 3 + 1)!, positionArray.at(firstIndex * 3 + 2)!);
|
|
53
|
+
const lineEnd = vec3.fromValues(positionArray.at(secondIndex * 3)!, positionArray.at(secondIndex * 3 + 1)!, positionArray.at(secondIndex * 3 + 2)!);
|
|
54
|
+
|
|
55
|
+
const { closestPointOnRay, closestPointOnLine } = this.closestPointsRayLine(ray, lineStart, lineEnd);
|
|
56
|
+
if (this.screenSpaceDistanceCheck(closestPointOnRay, closestPointOnLine, this.#settings.visualization.points.size_0! * this.#settings.visualization.distanceMultiplicationFactor).check === false) continue;
|
|
57
|
+
|
|
58
|
+
distances.push({ index: [firstIndex, secondIndex], distance: vec3.distance(closestPointOnRay, closestPointOnLine) });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// if there are no distances, return
|
|
62
|
+
if (distances.length === 0) return;
|
|
63
|
+
|
|
64
|
+
return distances.sort((a, b) => a.distance - b.distance);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Check which distances of points to ray
|
|
69
|
+
*
|
|
70
|
+
* @param ray
|
|
71
|
+
* @returns
|
|
72
|
+
*/
|
|
73
|
+
public checkPointDistances(ray: IRay): {
|
|
74
|
+
index: number;
|
|
75
|
+
distance: number;
|
|
76
|
+
}[] | undefined {
|
|
77
|
+
const positionArray = this.#drawingToolsManager.positionArray;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Calculate point distances to ray
|
|
81
|
+
*/
|
|
82
|
+
const distances: {
|
|
83
|
+
index: number;
|
|
84
|
+
distance: number;
|
|
85
|
+
}[] = [];
|
|
86
|
+
for (let i = 0; i < positionArray.length; i += 3) {
|
|
87
|
+
const point = vec3.fromValues(positionArray.at(i)!, positionArray.at(i + 1)!, positionArray.at(i + 2)!);
|
|
88
|
+
|
|
89
|
+
// distance from point to ray
|
|
90
|
+
const closestPoint = this.closestPoint(ray, point);
|
|
91
|
+
if (this.screenSpaceDistanceCheck(point, closestPoint, this.#settings.visualization.points.size_0! * this.#settings.visualization.distanceMultiplicationFactor).check === false) continue;
|
|
92
|
+
|
|
93
|
+
distances.push({ index: i / 3, distance: vec3.distance(point, closestPoint) });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// if there are no distances, return
|
|
97
|
+
if (distances.length === 0) return;
|
|
98
|
+
|
|
99
|
+
// sort distances
|
|
100
|
+
return distances.sort((a, b) => a.distance - b.distance);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public close(): void { }
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Calculate the closest point on a line to a point
|
|
107
|
+
*
|
|
108
|
+
* @param start
|
|
109
|
+
* @param end
|
|
110
|
+
* @param point
|
|
111
|
+
*/
|
|
112
|
+
public closestPointOnLine(start: vec3, end: vec3, point: vec3): vec3 {
|
|
113
|
+
const lineDir = vec3.sub(vec3.create(), end, start);
|
|
114
|
+
// Vector from linePoint to point
|
|
115
|
+
const v = vec3.sub(vec3.create(), point, start);
|
|
116
|
+
|
|
117
|
+
// Line direction dot product with itself
|
|
118
|
+
const dirDotDir = vec3.dot(lineDir, lineDir);
|
|
119
|
+
|
|
120
|
+
// If the direction vector is a zero vector, return the line point as closest point
|
|
121
|
+
if (dirDotDir === 0) return start;
|
|
122
|
+
|
|
123
|
+
// Projection factor t
|
|
124
|
+
const t = vec3.dot(v, lineDir) / dirDotDir;
|
|
125
|
+
|
|
126
|
+
// Closest point on the line
|
|
127
|
+
const closestPoint = vec3.add(vec3.create(), start, vec3.scale(vec3.create(), lineDir, t));
|
|
128
|
+
|
|
129
|
+
return closestPoint;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Calculate the distance between a ray and a line segment
|
|
134
|
+
*
|
|
135
|
+
* @param ray
|
|
136
|
+
* @param lineStart
|
|
137
|
+
* @param lineEnd
|
|
138
|
+
* @returns
|
|
139
|
+
*/
|
|
140
|
+
public closestPointsRayLine(ray: IRay, lineStart: vec3, lineEnd: vec3): { closestPointOnRay: vec3, closestPointOnLine: vec3 } {
|
|
141
|
+
// direction of line
|
|
142
|
+
const lineDirection = vec3.normalize(vec3.create(), vec3.subtract(vec3.create(), lineEnd, lineStart));
|
|
143
|
+
|
|
144
|
+
// cross product of ray direction and line direction
|
|
145
|
+
const crossProduct = vec3.cross(vec3.create(), ray.direction, lineDirection);
|
|
146
|
+
|
|
147
|
+
// length of cross product
|
|
148
|
+
const crossProductLength = vec3.length(crossProduct);
|
|
149
|
+
|
|
150
|
+
if (crossProductLength < 0.0001) {
|
|
151
|
+
// ray and line are parallel, calculate the distance differently
|
|
152
|
+
const closestPointOnRay = ray.origin;
|
|
153
|
+
const closestPointOnLine = vec3.add(vec3.create(), lineStart, vec3.scale(vec3.create(), lineDirection, vec3.dot(vec3.subtract(vec3.create(), ray.origin, lineStart), lineDirection)));
|
|
154
|
+
return {
|
|
155
|
+
closestPointOnRay, closestPointOnLine
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const t = vec3.sub(vec3.create(), lineStart, ray.origin);
|
|
160
|
+
const u = vec3.cross(vec3.create(), t, lineDirection);
|
|
161
|
+
const v = vec3.cross(vec3.create(), t, ray.direction);
|
|
162
|
+
|
|
163
|
+
const tValue = vec3.dot(u, crossProduct) / crossProductLength ** 2;
|
|
164
|
+
const uValue = vec3.dot(v, crossProduct) / crossProductLength ** 2;
|
|
165
|
+
|
|
166
|
+
const closestPointOnRay = vec3.add(vec3.create(), ray.origin, vec3.scale(vec3.create(), ray.direction, tValue));
|
|
167
|
+
|
|
168
|
+
// restrict the closest point on line to the line segment
|
|
169
|
+
let closestPointOnLine: vec3;
|
|
170
|
+
if (uValue < 0) {
|
|
171
|
+
closestPointOnLine = lineStart;
|
|
172
|
+
} else if (uValue > vec3.distance(lineStart, lineEnd)) {
|
|
173
|
+
closestPointOnLine = lineEnd;
|
|
174
|
+
} else {
|
|
175
|
+
closestPointOnLine = vec3.add(vec3.create(), lineStart, vec3.scale(vec3.create(), lineDirection, uValue));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
closestPointOnRay, closestPointOnLine
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Calculate the distance between two rays
|
|
185
|
+
*
|
|
186
|
+
* @param ray1
|
|
187
|
+
* @param ray2
|
|
188
|
+
* @returns
|
|
189
|
+
*/
|
|
190
|
+
public closestPointsRayRay(ray1: IRay, ray2: IRay): { closestPointOnRay1: vec3, closestPointOnRay2: vec3 } {
|
|
191
|
+
// cross product of ray1 direction and ray2 direction
|
|
192
|
+
const crossProduct = vec3.cross(vec3.create(), ray1.direction, ray2.direction);
|
|
193
|
+
|
|
194
|
+
// length of cross product
|
|
195
|
+
const crossProductLength = vec3.length(crossProduct);
|
|
196
|
+
|
|
197
|
+
if (crossProductLength < 0.0001) {
|
|
198
|
+
// ray1 and ray2 are parallel, calculate the distance differently
|
|
199
|
+
const closestPointOnRay1 = ray1.origin;
|
|
200
|
+
const closestPointOnRay2 = vec3.add(vec3.create(), ray2.origin, vec3.scale(vec3.create(), ray2.direction, vec3.dot(vec3.subtract(vec3.create(), ray1.origin, ray2.origin), ray2.direction)));
|
|
201
|
+
return {
|
|
202
|
+
closestPointOnRay1, closestPointOnRay2
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const t = vec3.sub(vec3.create(), ray2.origin, ray1.origin);
|
|
207
|
+
const u = vec3.cross(vec3.create(), t, ray2.direction);
|
|
208
|
+
const v = vec3.cross(vec3.create(), t, ray1.direction);
|
|
209
|
+
|
|
210
|
+
const tValue = vec3.dot(u, crossProduct) / crossProductLength ** 2;
|
|
211
|
+
const uValue = vec3.dot(v, crossProduct) / crossProductLength ** 2;
|
|
212
|
+
|
|
213
|
+
const closestPointOnRay1 = vec3.add(vec3.create(), ray1.origin, vec3.scale(vec3.create(), ray1.direction, tValue));
|
|
214
|
+
const closestPointOnRay2 = vec3.add(vec3.create(), ray2.origin, vec3.scale(vec3.create(), ray2.direction, uValue));
|
|
215
|
+
|
|
216
|
+
return {
|
|
217
|
+
closestPointOnRay1, closestPointOnRay2
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
public screenSpaceDistanceCheck(point1: vec3, point2: vec3, threshold: number) {
|
|
222
|
+
const camera = this.#viewport.camera!;
|
|
223
|
+
|
|
224
|
+
// Project points to NDC
|
|
225
|
+
const screenPos1 = camera.project(vec3.clone(point1));
|
|
226
|
+
const screenPos2 = camera.project(vec3.clone(point2));
|
|
227
|
+
|
|
228
|
+
const width = this.#viewport.canvas.width;
|
|
229
|
+
const height = this.#viewport.canvas.height;
|
|
230
|
+
|
|
231
|
+
const x1 = ((screenPos1[0] * (width / 2)) + (width / 2));
|
|
232
|
+
const y1 = - ((screenPos1[1] * (height / 2)) + (height / 2));
|
|
233
|
+
|
|
234
|
+
const x2 = ((screenPos2[0] * (width / 2)) + (width / 2));
|
|
235
|
+
const y2 = - ((screenPos2[1] * (height / 2)) + (height / 2));
|
|
236
|
+
|
|
237
|
+
const distanceSquared = (x2 - x1) ** 2 + (y2 - y1) ** 2;
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Logic: The actual calculation would be
|
|
241
|
+
* distance * 2 < threshold
|
|
242
|
+
* the multiplication by 2 is to account for the fact that the distance is from the center of the point
|
|
243
|
+
*
|
|
244
|
+
* However, we work with the squared distance to avoid the sqrt operation
|
|
245
|
+
* Therefore we square all values:
|
|
246
|
+
* distanceSquared * 4 < threshold ** 2
|
|
247
|
+
*/
|
|
248
|
+
return {
|
|
249
|
+
distanceSquared: distanceSquared,
|
|
250
|
+
check: distanceSquared * 4 < threshold ** 2
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// #endregion Public Methods (7)
|
|
255
|
+
|
|
256
|
+
// #region Private Methods (1)
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Calculate the closest point on a ray to a point
|
|
260
|
+
*
|
|
261
|
+
* @param ray
|
|
262
|
+
* @param point
|
|
263
|
+
* @returns
|
|
264
|
+
*/
|
|
265
|
+
private closestPoint(ray: IRay, point: vec3): vec3 {
|
|
266
|
+
// distance from point to ray
|
|
267
|
+
const dot = vec3.dot(ray.direction, vec3.sub(vec3.create(), point, ray.origin));
|
|
268
|
+
// closest point on ray to point
|
|
269
|
+
return vec3.add(vec3.create(), ray.origin, vec3.multiply(vec3.create(), ray.direction, vec3.fromValues(dot, dot, dot)));
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// #endregion Private Methods (1)
|
|
273
|
+
}
|