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 | import theConfig from '../../data/Config.js'; |
26 | import theGeometry from '../../core/lib/Geometry.js'; |
27 | import theSphericalTrigonometry from '../../core/lib/SphericalTrigonometry.js'; |
28 | |
29 | import { ICON_DIMENSIONS, ZERO, ONE, TWO, DEGREES, ICON_POSITION } from '../../main/Constants.js'; |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | class TranslationRotationFinder { |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | #computeData; |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | #noteData; |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | #rotationItineraryPoint; |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | #directionItineraryPoint; |
70 | |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | #iconPoint; |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | #computeTranslation ( ) { |
83 | this.#computeData.translation = theGeometry.subtrackPoints ( |
84 | [ ICON_DIMENSIONS.svgViewboxDim / TWO, ICON_DIMENSIONS.svgViewboxDim / TWO ], |
85 | theGeometry.project ( this.#noteData.latLng, theConfig.note.svgIcon.zoom ) |
86 | ); |
87 | } |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | #findRotationPoint ( ) { |
94 | |
95 | this.#rotationItineraryPoint = this.#computeData.route.itinerary.itineraryPoints.previous ( |
96 | this.#computeData.nearestItineraryPointObjId, |
97 | itineraryPoint => theSphericalTrigonometry.pointsDistance ( itineraryPoint.latLng, this.#noteData.latLng ) |
98 | > |
99 | theConfig.note.svgIcon.angleDistance |
100 | ) |
101 | || |
102 | this.#computeData.route.itinerary.itineraryPoints.first; |
103 | } |
104 | |
105 | |
106 | |
107 | |
108 | |
109 | #findDirectionPoint ( ) { |
110 | |
111 | this.#directionItineraryPoint = this.#computeData.route.itinerary.itineraryPoints.next ( |
112 | this.#computeData.nearestItineraryPointObjId, |
113 | itineraryPoint => theSphericalTrigonometry.pointsDistance ( itineraryPoint.latLng, this.#noteData.latLng ) |
114 | > |
115 | theConfig.note.svgIcon.angleDistance |
116 | ) |
117 | || |
118 | this.#computeData.route.itinerary.itineraryPoints.last; |
119 | } |
120 | |
121 | |
122 | |
123 | |
124 | |
125 | #computeIconPoint ( ) { |
126 | this.#iconPoint = theGeometry.addPoints ( |
127 | theGeometry.project ( this.#noteData.latLng, theConfig.note.svgIcon.zoom ), |
128 | this.#computeData.translation |
129 | ); |
130 | |
131 | } |
132 | |
133 | |
134 | |
135 | |
136 | |
137 | #findRotation ( ) { |
138 | |
139 | if ( |
140 | this.#computeData.nearestItineraryPointObjId |
141 | !== |
142 | this.#computeData.route.itinerary.itineraryPoints.first.objId |
143 | ) { |
144 | const rotationPoint = theGeometry.addPoints ( |
145 | theGeometry.project ( this.#rotationItineraryPoint.latLng, theConfig.note.svgIcon.zoom ), |
146 | this.#computeData.translation |
147 | ); |
148 | this.#computeData.rotation = |
149 | Math.atan ( |
150 | ( this.#iconPoint [ ONE ] - rotationPoint [ ONE ] ) |
151 | / |
152 | ( rotationPoint [ ZERO ] - this.#iconPoint [ ZERO ] ) |
153 | ) |
154 | * |
155 | DEGREES.d180 / Math.PI; |
156 | |
157 | if ( ZERO > this.#computeData.rotation ) { |
158 | this.#computeData.rotation += DEGREES.d360; |
159 | } |
160 | this.#computeData.rotation -= DEGREES.d270; |
161 | |
162 | |
163 | if ( ZERO > rotationPoint [ ZERO ] - this.#iconPoint [ ZERO ] ) { |
164 | this.#computeData.rotation += DEGREES.d180; |
165 | } |
166 | } |
167 | } |
168 | |
169 | |
170 | |
171 | |
172 | |
173 | #findDirection ( ) { |
174 | if ( |
175 | this.#computeData.nearestItineraryPointObjId |
176 | !== |
177 | this.#computeData.route.itinerary.itineraryPoints.last.objId |
178 | ) { |
179 | const directionPoint = theGeometry.addPoints ( |
180 | theGeometry.project ( this.#directionItineraryPoint.latLng, theConfig.note.svgIcon.zoom ), |
181 | this.#computeData.translation |
182 | ); |
183 | this.#computeData.direction = Math.atan ( |
184 | ( this.#iconPoint [ ONE ] - directionPoint [ ONE ] ) |
185 | / |
186 | ( directionPoint [ ZERO ] - this.#iconPoint [ ZERO ] ) |
187 | ) |
188 | * |
189 | DEGREES.d180 / Math.PI; |
190 | |
191 | |
192 | if ( ZERO > directionPoint [ ZERO ] - this.#iconPoint [ ZERO ] ) { |
193 | this.#computeData.direction += DEGREES.d180; |
194 | } |
195 | this.#computeData.direction -= this.#computeData.rotation; |
196 | |
197 | |
198 | while ( DEGREES.d0 > this.#computeData.direction ) { |
199 | this.#computeData.direction += DEGREES.d360; |
200 | } |
201 | while ( DEGREES.d360 < this.#computeData.direction ) { |
202 | this.#computeData.direction -= DEGREES.d360; |
203 | } |
204 | } |
205 | } |
206 | |
207 | |
208 | |
209 | |
210 | |
211 | #findPositionOnRoute ( ) { |
212 | if ( |
213 | this.#computeData.nearestItineraryPointObjId |
214 | === |
215 | this.#computeData.route.itinerary.itineraryPoints.first.objId |
216 | ) { |
217 | this.#computeData.rotation = -this.#computeData.direction - DEGREES.d90; |
218 | this.#computeData.direction = null; |
219 | this.#computeData.positionOnRoute = ICON_POSITION.atStart; |
220 | } |
221 | |
222 | if ( |
223 | this.#noteData.latLng [ ZERO ] === this.#computeData.route.itinerary.itineraryPoints.last.lat |
224 | && |
225 | this.#noteData.latLng [ ONE ] === this.#computeData.route.itinerary.itineraryPoints.last.lng |
226 | ) { |
227 | |
228 | |
229 | this.#computeData.direction = null; |
230 | this.#computeData.positionOnRoute = ICON_POSITION.atEnd; |
231 | } |
232 | } |
233 | |
234 | |
235 | |
236 | |
237 | |
238 | constructor ( ) { |
239 | Object.freeze ( this ); |
240 | } |
241 | |
242 | |
243 | |
244 | |
245 | |
246 | |
247 | |
248 | |
249 | findData ( computeData, noteData ) { |
250 | |
251 | this.#computeData = computeData; |
252 | this.#noteData = noteData; |
253 | |
254 | this.#computeTranslation ( ); |
255 | this.#findRotationPoint ( ); |
256 | this.#findDirectionPoint ( ); |
257 | this.#computeIconPoint ( ); |
258 | this.#findRotation ( ); |
259 | this.#findDirection ( ); |
260 | this.#findPositionOnRoute ( ); |
261 | } |
262 | } |
263 | |
264 | export default TranslationRotationFinder; |
265 | |
266 | |
267 | |