@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.
Files changed (209) hide show
  1. package/LICENSE +73 -0
  2. package/README.md +161 -0
  3. package/dist/api/implementation/DrawingToolsApi.d.ts +31 -0
  4. package/dist/api/implementation/DrawingToolsApi.d.ts.map +1 -0
  5. package/dist/api/implementation/DrawingToolsApi.js +111 -0
  6. package/dist/api/implementation/DrawingToolsApi.js.map +1 -0
  7. package/dist/api/implementation/restrictions/AbstractRestrictionApi.d.ts +10 -0
  8. package/dist/api/implementation/restrictions/AbstractRestrictionApi.d.ts.map +1 -0
  9. package/dist/api/implementation/restrictions/AbstractRestrictionApi.js +38 -0
  10. package/dist/api/implementation/restrictions/AbstractRestrictionApi.js.map +1 -0
  11. package/dist/api/implementation/restrictions/AbstractSnapRestrictionApi.d.ts +11 -0
  12. package/dist/api/implementation/restrictions/AbstractSnapRestrictionApi.d.ts.map +1 -0
  13. package/dist/api/implementation/restrictions/AbstractSnapRestrictionApi.js +40 -0
  14. package/dist/api/implementation/restrictions/AbstractSnapRestrictionApi.js.map +1 -0
  15. package/dist/api/implementation/restrictions/axis/AxisRestrictionApi.d.ts +7 -0
  16. package/dist/api/implementation/restrictions/axis/AxisRestrictionApi.d.ts.map +1 -0
  17. package/dist/api/implementation/restrictions/axis/AxisRestrictionApi.js +24 -0
  18. package/dist/api/implementation/restrictions/axis/AxisRestrictionApi.js.map +1 -0
  19. package/dist/api/implementation/restrictions/geometry/GeometryRestrictionApi.d.ts +15 -0
  20. package/dist/api/implementation/restrictions/geometry/GeometryRestrictionApi.d.ts.map +1 -0
  21. package/dist/api/implementation/restrictions/geometry/GeometryRestrictionApi.js +54 -0
  22. package/dist/api/implementation/restrictions/geometry/GeometryRestrictionApi.js.map +1 -0
  23. package/dist/api/implementation/restrictions/plane/PlaneRestrictionApi.d.ts +18 -0
  24. package/dist/api/implementation/restrictions/plane/PlaneRestrictionApi.d.ts.map +1 -0
  25. package/dist/api/implementation/restrictions/plane/PlaneRestrictionApi.js +61 -0
  26. package/dist/api/implementation/restrictions/plane/PlaneRestrictionApi.js.map +1 -0
  27. package/dist/api/implementation/restrictions/plane/snap/AngularRestrictionApi.d.ts +10 -0
  28. package/dist/api/implementation/restrictions/plane/snap/AngularRestrictionApi.d.ts.map +1 -0
  29. package/dist/api/implementation/restrictions/plane/snap/AngularRestrictionApi.js +40 -0
  30. package/dist/api/implementation/restrictions/plane/snap/AngularRestrictionApi.js.map +1 -0
  31. package/dist/api/implementation/restrictions/plane/snap/GridRestrictionApi.d.ts +10 -0
  32. package/dist/api/implementation/restrictions/plane/snap/GridRestrictionApi.d.ts.map +1 -0
  33. package/dist/api/implementation/restrictions/plane/snap/GridRestrictionApi.js +40 -0
  34. package/dist/api/implementation/restrictions/plane/snap/GridRestrictionApi.js.map +1 -0
  35. package/dist/api/interfaces/IDrawingToolsApi.d.ts +89 -0
  36. package/dist/api/interfaces/IDrawingToolsApi.d.ts.map +1 -0
  37. package/dist/api/interfaces/IDrawingToolsApi.js +3 -0
  38. package/dist/api/interfaces/IDrawingToolsApi.js.map +1 -0
  39. package/dist/api/interfaces/IRestrictionApi.d.ts +11 -0
  40. package/dist/api/interfaces/IRestrictionApi.d.ts.map +1 -0
  41. package/dist/api/interfaces/IRestrictionApi.js +3 -0
  42. package/dist/api/interfaces/IRestrictionApi.js.map +1 -0
  43. package/dist/api/interfaces/ISnapRestrictionApi.d.ts +13 -0
  44. package/dist/api/interfaces/ISnapRestrictionApi.d.ts.map +1 -0
  45. package/dist/api/interfaces/ISnapRestrictionApi.js +3 -0
  46. package/dist/api/interfaces/ISnapRestrictionApi.js.map +1 -0
  47. package/dist/business/implementation/DrawingToolsManager.d.ts +95 -0
  48. package/dist/business/implementation/DrawingToolsManager.d.ts.map +1 -0
  49. package/dist/business/implementation/DrawingToolsManager.js +544 -0
  50. package/dist/business/implementation/DrawingToolsManager.js.map +1 -0
  51. package/dist/business/implementation/managers/HistoryManager.d.ts +19 -0
  52. package/dist/business/implementation/managers/HistoryManager.d.ts.map +1 -0
  53. package/dist/business/implementation/managers/HistoryManager.js +89 -0
  54. package/dist/business/implementation/managers/HistoryManager.js.map +1 -0
  55. package/dist/business/implementation/managers/TextVisualizationManager.d.ts +14 -0
  56. package/dist/business/implementation/managers/TextVisualizationManager.d.ts.map +1 -0
  57. package/dist/business/implementation/managers/TextVisualizationManager.js +228 -0
  58. package/dist/business/implementation/managers/TextVisualizationManager.js.map +1 -0
  59. package/dist/business/implementation/managers/geometry/GeometryManager.d.ts +23 -0
  60. package/dist/business/implementation/managers/geometry/GeometryManager.d.ts.map +1 -0
  61. package/dist/business/implementation/managers/geometry/GeometryManager.js +82 -0
  62. package/dist/business/implementation/managers/geometry/GeometryManager.js.map +1 -0
  63. package/dist/business/implementation/managers/geometry/GeometryMathManager.d.ts +73 -0
  64. package/dist/business/implementation/managers/geometry/GeometryMathManager.d.ts.map +1 -0
  65. package/dist/business/implementation/managers/geometry/GeometryMathManager.js +230 -0
  66. package/dist/business/implementation/managers/geometry/GeometryMathManager.js.map +1 -0
  67. package/dist/business/implementation/managers/geometry/GeometryState.d.ts +46 -0
  68. package/dist/business/implementation/managers/geometry/GeometryState.d.ts.map +1 -0
  69. package/dist/business/implementation/managers/geometry/GeometryState.js +323 -0
  70. package/dist/business/implementation/managers/geometry/GeometryState.js.map +1 -0
  71. package/dist/business/implementation/managers/geometry/helpers/GeometryManagerHelper.d.ts +16 -0
  72. package/dist/business/implementation/managers/geometry/helpers/GeometryManagerHelper.d.ts.map +1 -0
  73. package/dist/business/implementation/managers/geometry/helpers/GeometryManagerHelper.js +157 -0
  74. package/dist/business/implementation/managers/geometry/helpers/GeometryManagerHelper.js.map +1 -0
  75. package/dist/business/implementation/managers/interaction/EventManager.d.ts +26 -0
  76. package/dist/business/implementation/managers/interaction/EventManager.d.ts.map +1 -0
  77. package/dist/business/implementation/managers/interaction/EventManager.js +63 -0
  78. package/dist/business/implementation/managers/interaction/EventManager.js.map +1 -0
  79. package/dist/business/implementation/managers/interaction/InteractionManager.d.ts +39 -0
  80. package/dist/business/implementation/managers/interaction/InteractionManager.d.ts.map +1 -0
  81. package/dist/business/implementation/managers/interaction/InteractionManager.js +237 -0
  82. package/dist/business/implementation/managers/interaction/InteractionManager.js.map +1 -0
  83. package/dist/business/implementation/managers/interaction/RestrictionManager.d.ts +19 -0
  84. package/dist/business/implementation/managers/interaction/RestrictionManager.d.ts.map +1 -0
  85. package/dist/business/implementation/managers/interaction/RestrictionManager.js +117 -0
  86. package/dist/business/implementation/managers/interaction/RestrictionManager.js.map +1 -0
  87. package/dist/business/implementation/managers/interaction/handlers/DeletionInteractionHandler.d.ts +10 -0
  88. package/dist/business/implementation/managers/interaction/handlers/DeletionInteractionHandler.d.ts.map +1 -0
  89. package/dist/business/implementation/managers/interaction/handlers/DeletionInteractionHandler.js +52 -0
  90. package/dist/business/implementation/managers/interaction/handlers/DeletionInteractionHandler.js.map +1 -0
  91. package/dist/business/implementation/managers/interaction/handlers/InsertionInteractionHandler.d.ts +14 -0
  92. package/dist/business/implementation/managers/interaction/handlers/InsertionInteractionHandler.d.ts.map +1 -0
  93. package/dist/business/implementation/managers/interaction/handlers/InsertionInteractionHandler.js +135 -0
  94. package/dist/business/implementation/managers/interaction/handlers/InsertionInteractionHandler.js.map +1 -0
  95. package/dist/business/implementation/managers/interaction/handlers/MidPointInteractionHandler.d.ts +16 -0
  96. package/dist/business/implementation/managers/interaction/handlers/MidPointInteractionHandler.d.ts.map +1 -0
  97. package/dist/business/implementation/managers/interaction/handlers/MidPointInteractionHandler.js +113 -0
  98. package/dist/business/implementation/managers/interaction/handlers/MidPointInteractionHandler.js.map +1 -0
  99. package/dist/business/implementation/managers/interaction/helpers/InteractionManagerHelper.d.ts +58 -0
  100. package/dist/business/implementation/managers/interaction/helpers/InteractionManagerHelper.d.ts.map +1 -0
  101. package/dist/business/implementation/managers/interaction/helpers/InteractionManagerHelper.js +377 -0
  102. package/dist/business/implementation/managers/interaction/helpers/InteractionManagerHelper.js.map +1 -0
  103. package/dist/business/implementation/managers/interaction/restrictions/AbstractRestriction.d.ts +19 -0
  104. package/dist/business/implementation/managers/interaction/restrictions/AbstractRestriction.d.ts.map +1 -0
  105. package/dist/business/implementation/managers/interaction/restrictions/AbstractRestriction.js +105 -0
  106. package/dist/business/implementation/managers/interaction/restrictions/AbstractRestriction.js.map +1 -0
  107. package/dist/business/implementation/managers/interaction/restrictions/axis/AxisRestriction.d.ts +22 -0
  108. package/dist/business/implementation/managers/interaction/restrictions/axis/AxisRestriction.d.ts.map +1 -0
  109. package/dist/business/implementation/managers/interaction/restrictions/axis/AxisRestriction.js +96 -0
  110. package/dist/business/implementation/managers/interaction/restrictions/axis/AxisRestriction.js.map +1 -0
  111. package/dist/business/implementation/managers/interaction/restrictions/geometry/GeometryRestriction.d.ts +39 -0
  112. package/dist/business/implementation/managers/interaction/restrictions/geometry/GeometryRestriction.d.ts.map +1 -0
  113. package/dist/business/implementation/managers/interaction/restrictions/geometry/GeometryRestriction.js +215 -0
  114. package/dist/business/implementation/managers/interaction/restrictions/geometry/GeometryRestriction.js.map +1 -0
  115. package/dist/business/implementation/managers/interaction/restrictions/plane/PlaneRestriction.d.ts +58 -0
  116. package/dist/business/implementation/managers/interaction/restrictions/plane/PlaneRestriction.d.ts.map +1 -0
  117. package/dist/business/implementation/managers/interaction/restrictions/plane/PlaneRestriction.js +180 -0
  118. package/dist/business/implementation/managers/interaction/restrictions/plane/PlaneRestriction.js.map +1 -0
  119. package/dist/business/implementation/managers/interaction/restrictions/plane/snap/AngularRestriction.d.ts +38 -0
  120. package/dist/business/implementation/managers/interaction/restrictions/plane/snap/AngularRestriction.d.ts.map +1 -0
  121. package/dist/business/implementation/managers/interaction/restrictions/plane/snap/AngularRestriction.js +308 -0
  122. package/dist/business/implementation/managers/interaction/restrictions/plane/snap/AngularRestriction.js.map +1 -0
  123. package/dist/business/implementation/managers/interaction/restrictions/plane/snap/GridRestriction.d.ts +37 -0
  124. package/dist/business/implementation/managers/interaction/restrictions/plane/snap/GridRestriction.d.ts.map +1 -0
  125. package/dist/business/implementation/managers/interaction/restrictions/plane/snap/GridRestriction.js +228 -0
  126. package/dist/business/implementation/managers/interaction/restrictions/plane/snap/GridRestriction.js.map +1 -0
  127. package/dist/business/implementation/utils/numberCleaner.d.ts +2 -0
  128. package/dist/business/implementation/utils/numberCleaner.d.ts.map +1 -0
  129. package/dist/business/implementation/utils/numberCleaner.js +9 -0
  130. package/dist/business/implementation/utils/numberCleaner.js.map +1 -0
  131. package/dist/business/interfaces/IDrawingToolsManager.d.ts +270 -0
  132. package/dist/business/interfaces/IDrawingToolsManager.d.ts.map +1 -0
  133. package/dist/business/interfaces/IDrawingToolsManager.js +16 -0
  134. package/dist/business/interfaces/IDrawingToolsManager.js.map +1 -0
  135. package/dist/business/interfaces/IManager.d.ts +4 -0
  136. package/dist/business/interfaces/IManager.d.ts.map +1 -0
  137. package/dist/business/interfaces/IManager.js +3 -0
  138. package/dist/business/interfaces/IManager.js.map +1 -0
  139. package/dist/business/interfaces/IRestriction.d.ts +41 -0
  140. package/dist/business/interfaces/IRestriction.d.ts.map +1 -0
  141. package/dist/business/interfaces/IRestriction.js +13 -0
  142. package/dist/business/interfaces/IRestriction.js.map +1 -0
  143. package/dist/business/interfaces/IRestrictionBase.d.ts +19 -0
  144. package/dist/business/interfaces/IRestrictionBase.d.ts.map +1 -0
  145. package/dist/business/interfaces/IRestrictionBase.js +5 -0
  146. package/dist/business/interfaces/IRestrictionBase.js.map +1 -0
  147. package/dist/business/interfaces/ISnapRestriction.d.ts +50 -0
  148. package/dist/business/interfaces/ISnapRestriction.d.ts.map +1 -0
  149. package/dist/business/interfaces/ISnapRestriction.js +4 -0
  150. package/dist/business/interfaces/ISnapRestriction.js.map +1 -0
  151. package/dist/business/interfaces/events/EventResponseMapping.d.ts +19 -0
  152. package/dist/business/interfaces/events/EventResponseMapping.d.ts.map +1 -0
  153. package/dist/business/interfaces/events/EventResponseMapping.js +4 -0
  154. package/dist/business/interfaces/events/EventResponseMapping.js.map +1 -0
  155. package/dist/business/interfaces/events/IDrawingToolsEvent.d.ts +12 -0
  156. package/dist/business/interfaces/events/IDrawingToolsEvent.d.ts.map +1 -0
  157. package/dist/business/interfaces/events/IDrawingToolsEvent.js +3 -0
  158. package/dist/business/interfaces/events/IDrawingToolsEvent.js.map +1 -0
  159. package/dist/index.d.ts +32 -0
  160. package/dist/index.d.ts.map +1 -0
  161. package/dist/index.js +45 -0
  162. package/dist/index.js.map +1 -0
  163. package/dist/three/CSS2DRenderer.d.ts +32 -0
  164. package/dist/three/CSS2DRenderer.d.ts.map +1 -0
  165. package/dist/three/CSS2DRenderer.js +137 -0
  166. package/dist/three/CSS2DRenderer.js.map +1 -0
  167. package/package.json +55 -0
  168. package/src/api/implementation/DrawingToolsApi.ts +135 -0
  169. package/src/api/implementation/restrictions/AbstractRestrictionApi.ts +34 -0
  170. package/src/api/implementation/restrictions/AbstractSnapRestrictionApi.ts +36 -0
  171. package/src/api/implementation/restrictions/axis/AxisRestrictionApi.ts +19 -0
  172. package/src/api/implementation/restrictions/geometry/GeometryRestrictionApi.ts +56 -0
  173. package/src/api/implementation/restrictions/plane/PlaneRestrictionApi.ts +63 -0
  174. package/src/api/implementation/restrictions/plane/snap/AngularRestrictionApi.ts +35 -0
  175. package/src/api/implementation/restrictions/plane/snap/GridRestrictionApi.ts +35 -0
  176. package/src/api/interfaces/IDrawingToolsApi.ts +99 -0
  177. package/src/api/interfaces/IRestrictionApi.ts +15 -0
  178. package/src/api/interfaces/ISnapRestrictionApi.ts +18 -0
  179. package/src/business/implementation/DrawingToolsManager.ts +619 -0
  180. package/src/business/implementation/managers/HistoryManager.ts +101 -0
  181. package/src/business/implementation/managers/TextVisualizationManager.ts +245 -0
  182. package/src/business/implementation/managers/geometry/GeometryManager.ts +95 -0
  183. package/src/business/implementation/managers/geometry/GeometryMathManager.ts +273 -0
  184. package/src/business/implementation/managers/geometry/GeometryState.ts +436 -0
  185. package/src/business/implementation/managers/geometry/helpers/GeometryManagerHelper.ts +170 -0
  186. package/src/business/implementation/managers/interaction/EventManager.ts +80 -0
  187. package/src/business/implementation/managers/interaction/InteractionManager.ts +268 -0
  188. package/src/business/implementation/managers/interaction/RestrictionManager.ts +135 -0
  189. package/src/business/implementation/managers/interaction/handlers/DeletionInteractionHandler.ts +48 -0
  190. package/src/business/implementation/managers/interaction/handlers/InsertionInteractionHandler.ts +149 -0
  191. package/src/business/implementation/managers/interaction/handlers/MidPointInteractionHandler.ts +127 -0
  192. package/src/business/implementation/managers/interaction/helpers/InteractionManagerHelper.ts +411 -0
  193. package/src/business/implementation/managers/interaction/restrictions/AbstractRestriction.ts +99 -0
  194. package/src/business/implementation/managers/interaction/restrictions/axis/AxisRestriction.ts +107 -0
  195. package/src/business/implementation/managers/interaction/restrictions/geometry/GeometryRestriction.ts +237 -0
  196. package/src/business/implementation/managers/interaction/restrictions/plane/PlaneRestriction.ts +257 -0
  197. package/src/business/implementation/managers/interaction/restrictions/plane/snap/AngularRestriction.ts +384 -0
  198. package/src/business/implementation/managers/interaction/restrictions/plane/snap/GridRestriction.ts +262 -0
  199. package/src/business/implementation/utils/numberCleaner.ts +5 -0
  200. package/src/business/interfaces/IDrawingToolsManager.ts +313 -0
  201. package/src/business/interfaces/IManager.ts +7 -0
  202. package/src/business/interfaces/IRestriction.ts +64 -0
  203. package/src/business/interfaces/IRestrictionBase.ts +33 -0
  204. package/src/business/interfaces/ISnapRestriction.ts +70 -0
  205. package/src/business/interfaces/events/EventResponseMapping.ts +19 -0
  206. package/src/business/interfaces/events/IDrawingToolsEvent.ts +16 -0
  207. package/src/index.ts +72 -0
  208. package/src/three/CSS2DRenderer.ts +212 -0
  209. 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
+ }