/
Image.ts
669 lines (613 loc) · 17.3 KB
/
Image.ts
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
import { Observable } from "rxjs";
import { map } from "rxjs/operators";
import { ImageCache } from "./ImageCache";
import { NavigationEdge } from "./edge/interfaces/NavigationEdge";
import { NavigationEdgeStatus } from "./interfaces/NavigationEdgeStatus";
import { CoreImageEnt } from "../api/ents/CoreImageEnt";
import { SpatialImageEnt } from "../api/ents/SpatialImageEnt";
import { LngLat } from "../api/interfaces/LngLat";
import { MeshContract } from "../api/contracts/MeshContract";
/**
* @class Image
*
* @classdesc Represents a image in the navigation graph.
*
* Explanation of position and bearing properties:
*
* When images are uploaded they will have GPS information in the EXIF, this is what
* is called `originalLngLat` {@link Image.originalLngLat}.
*
* When Structure from Motions has been run for a image a `computedLngLat` that
* differs from the `originalLngLat` will be created. It is different because
* GPS positions are not very exact and SfM aligns the camera positions according
* to the 3D reconstruction {@link Image.computedLngLat}.
*
* At last there exist a `lngLat` property which evaluates to
* the `computedLngLat` from SfM if it exists but falls back
* to the `originalLngLat` from the EXIF GPS otherwise {@link Image.lngLat}.
*
* Everything that is done in in the Viewer is based on the SfM positions,
* i.e. `computedLngLat`. That is why the smooth transitions go in the right
* direction (nd not in strange directions because of bad GPS).
*
* E.g. when placing a marker in the Viewer it is relative to the SfM
* position i.e. the `computedLngLat`.
*
* The same concept as above also applies to the compass angle (or bearing) properties
* `originalCa`, `computedCa` and `ca`.
*/
export class Image {
private _cache: ImageCache;
private _core: CoreImageEnt;
private _spatial: SpatialImageEnt;
/**
* Create a new image instance.
*
* @description Images are always created internally by the library.
* Images can not be added to the library through any API method.
*
* @param {CoreImageEnt} core- Raw core image data.
* @ignore
*/
constructor(core: CoreImageEnt) {
if (!core) {
throw new Error(`Incorrect core image data ${core}`);
}
this._cache = null;
this._core = core;
this._spatial = null;
}
/**
* Get assets cached.
*
* @description The assets that need to be cached for this property
* to report true are the following: fill properties, image and mesh.
* The library ensures that the current image will always have the
* assets cached.
*
* @returns {boolean} Value indicating whether all assets have been
* cached.
*
* @ignore
*/
public get assetsCached(): boolean {
return this._core != null &&
this._spatial != null &&
this._cache != null &&
this._cache.image != null &&
this._cache.mesh != null;
}
/**
* Get cameraParameters.
*
* @description Will be undefined if SfM has
* not been run.
*
* Camera type dependent parameters.
*
* For perspective and fisheye camera types,
* the camera parameters array should be
* constructed according to
*
* `[focal, k1, k2]`
*
* where focal is the camera focal length,
* and k1, k2 are radial distortion parameters.
*
* For spherical camera type the camera
* parameters are unset or emtpy array.
*
* @returns {Array<number>} The parameters
* related to the camera type.
*/
public get cameraParameters(): number[] {
return this._spatial.camera_parameters;
}
/**
* Get cameraType.
*
* @description Will be undefined if SfM has not been run.
*
* @returns {string} The camera type that captured the image.
*/
public get cameraType(): string {
return this._spatial.camera_type;
}
/**
* Get capturedAt.
*
* @description Timestamp of the image capture date
* and time represented as a Unix epoch timestamp in milliseconds.
*
* @returns {number} Timestamp when the image was captured.
*/
public get capturedAt(): number {
return this._spatial.captured_at;
}
/**
* Get clusterId.
*
* @returns {string} Globally unique id of the SfM cluster to which
* the image belongs.
*/
public get clusterId(): string {
return !!this._spatial.cluster ?
this._spatial.cluster.id :
null;
}
/**
* Get clusterUrl.
*
* @returns {string} Url to the cluster reconstruction file.
*
* @ignore
*/
public get clusterUrl(): string {
return !!this._spatial.cluster ?
this._spatial.cluster.url :
null;
}
/**
* Get compassAngle.
*
* @description If the SfM computed compass angle exists it will
* be returned, otherwise the original EXIF compass angle.
*
* @returns {number} Compass angle, measured in degrees
* clockwise with respect to north.
*/
public get compassAngle(): number {
return this._spatial.computed_compass_angle != null ?
this._spatial.computed_compass_angle :
this._spatial.compass_angle;
}
/**
* Get complete.
*
* @description The library ensures that the current image will
* always be full.
*
* @returns {boolean} Value indicating whether the image has all
* properties filled.
*
* @ignore
*/
public get complete(): boolean {
return this._spatial != null;
}
/**
* Get computedAltitude.
*
* @description If SfM has not been run the computed altitude is
* set to a default value of two meters.
*
* @returns {number} Altitude, in meters.
*/
public get computedAltitude(): number {
return this._spatial.computed_altitude;
}
/**
* Get computedCompassAngle.
*
* @description Will not be set if SfM has not been run.
*
* @returns {number} SfM computed compass angle, measured
* in degrees clockwise with respect to north.
*/
public get computedCompassAngle(): number {
return this._spatial.computed_compass_angle;
}
/**
* Get computedLngLat.
*
* @description Will not be set if SfM has not been run.
*
* @returns {LngLat} SfM computed longitude, latitude in WGS84 datum,
* measured in degrees.
*/
public get computedLngLat(): LngLat {
return this._core.computed_geometry;
}
/**
* Get creatorId.
*
* @description Note that the creator ID will not be set when using
* the Mapillary API.
*
* @returns {string} Globally unique id of the user who uploaded
* the image.
*/
public get creatorId(): string {
return this._spatial.creator.id;
}
/**
* Get creatorUsername.
*
* @description Note that the creator username will not be set when
* using the Mapillary API.
*
* @returns {string} Username of the creator who uploaded
* the image.
*/
public get creatorUsername(): string {
return this._spatial.creator.username;
}
/**
* Get exifOrientation.
*
* @returns {number} EXIF orientation of original image.
*/
public get exifOrientation(): number {
return this._spatial.exif_orientation;
}
/**
* Get height.
*
* @returns {number} Height of original image, not adjusted
* for orientation.
*/
public get height(): number {
return this._spatial.height;
}
/**
* Get image.
*
* @description The image will always be set on the current image.
*
* @returns {HTMLImageElement} Cached image element of the image.
*/
public get image(): HTMLImageElement {
return this._cache.image;
}
/**
* Get image$.
*
* @returns {Observable<HTMLImageElement>} Observable emitting
* the cached image when it is updated.
*
* @ignore
*/
public get image$(): Observable<HTMLImageElement> {
return this._cache.image$;
}
/**
* Get id.
*
* @returns {string} Globally unique id of the image.
*/
public get id(): string {
return this._core.id;
}
/**
* Get lngLat.
*
* @description If the SfM computed longitude, latitude exist
* it will be returned, otherwise the original EXIF latitude
* longitude.
*
* @returns {LngLat} Longitude, latitude in WGS84 datum,
* measured in degrees.
*/
public get lngLat(): LngLat {
return this._core.computed_geometry != null ?
this._core.computed_geometry :
this._core.geometry;
}
/**
* Get merged.
*
* @returns {boolean} Value indicating whether SfM has been
* run on the image and the image has been merged into a
* connected component.
*/
public get merged(): boolean {
return this._spatial != null &&
this._spatial.merge_id != null;
}
/**
* Get mergeId.
*
* @description Will not be set if SfM has not yet been run on
* image.
*
* @returns {stirng} Id of connected component to which image
* belongs after the aligning merge.
*/
public get mergeId(): string {
return this._spatial.merge_id;
}
/**
* Get mesh.
*
* @description The mesh will always be set on the current image.
*
* @returns {MeshContract} SfM triangulated mesh of reconstructed
* atomic 3D points.
*/
public get mesh(): MeshContract {
return this._cache.mesh;
}
/**
* Get originalAltitude.
*
* @returns {number} EXIF altitude, in meters, if available.
*/
public get originalAltitude(): number {
return this._spatial.altitude;
}
/**
* Get originalCompassAngle.
*
* @returns {number} Original EXIF compass angle, measured in
* degrees.
*/
public get originalCompassAngle(): number {
return this._spatial.compass_angle;
}
/**
* Get originalLngLat.
*
* @returns {LngLat} Original EXIF longitude, latitude in
* WGS84 datum, measured in degrees.
*/
public get originalLngLat(): LngLat {
return this._core.geometry;
}
/**
* Get ownerId.
*
* @returns {string} Globally unique id of the owner to which
* the image belongs. If the image does not belong to an
* owner the owner id will be undefined.
*/
public get ownerId(): string {
return !!this._spatial.owner ?
this._spatial.owner.id :
null;
}
/**
* Get private.
*
* @returns {boolean} Value specifying if image is accessible to
* organization members only or to everyone.
*/
public get private(): boolean {
return this._spatial.private;
}
/**
* Get qualityScore.
*
* @returns {number} A number between zero and one
* determining the quality of the image. Blurriness
* (motion blur / out-of-focus), occlusion (camera
* mount, ego vehicle, water-drops), windshield
* reflections, bad illumination (exposure, glare),
* and bad weather condition (fog, rain, snow)
* affect the quality score.
*
* @description Value should be on the interval [0, 1].
*/
public get qualityScore(): number {
return this._spatial.quality_score;
}
/**
* Get rotation.
*
* @description Will not be set if SfM has not been run.
*
* @returns {Array<number>} Rotation vector in angle axis representation.
*/
public get rotation(): number[] {
return this._spatial.computed_rotation;
}
/**
* Get scale.
*
* @description Will not be set if SfM has not been run.
*
* @returns {number} Scale of reconstruction the image
* belongs to.
*/
public get scale(): number {
return this._spatial.atomic_scale;
}
/**
* Get sequenceId.
*
* @returns {string} Globally unique id of the sequence
* to which the image belongs.
*/
public get sequenceId(): string {
return !!this._core.sequence ?
this._core.sequence.id :
null;
}
/**
* Get sequenceEdges.
*
* @returns {NavigationEdgeStatus} Value describing the status of the
* sequence edges.
*
* @ignore
*/
public get sequenceEdges(): NavigationEdgeStatus {
return this._cache.sequenceEdges;
}
/**
* Get sequenceEdges$.
*
* @description Internal observable, should not be used as an API.
*
* @returns {Observable<NavigationEdgeStatus>} Observable emitting
* values describing the status of the sequence edges.
*
* @ignore
*/
public get sequenceEdges$(): Observable<NavigationEdgeStatus> {
return this._cache.sequenceEdges$;
}
/**
* Get spatialEdges.
*
* @returns {NavigationEdgeStatus} Value describing the status of the
* spatial edges.
*
* @ignore
*/
public get spatialEdges(): NavigationEdgeStatus {
return this._cache.spatialEdges;
}
/**
* Get spatialEdges$.
*
* @description Internal observable, should not be used as an API.
*
* @returns {Observable<NavigationEdgeStatus>} Observable emitting
* values describing the status of the spatial edges.
*
* @ignore
*/
public get spatialEdges$(): Observable<NavigationEdgeStatus> {
return this._cache.spatialEdges$;
}
/**
* Get width.
*
* @returns {number} Width of original image, not
* adjusted for orientation.
*/
public get width(): number {
return this._spatial.width;
}
/**
* Cache the image and mesh assets.
*
* @description The assets are always cached internally by the
* library prior to setting a image as the current image.
*
* @returns {Observable<Image>} Observable emitting this image whenever the
* load status has changed and when the mesh or image has been fully loaded.
*
* @ignore
*/
public cacheAssets$(): Observable<Image> {
return this._cache
.cacheAssets$(this._spatial, this.merged)
.pipe(
map((): Image => { return this; }));
}
/**
* Cache the image asset.
*
* @description Use for caching a differently sized image than
* the one currently held by the image.
*
* @returns {Observable<Image>} Observable emitting this image whenever the
* load status has changed and when the mesh or image has been fully loaded.
*
* @ignore
*/
public cacheImage$(): Observable<Image> {
return this._cache
.cacheImage$(this._spatial)
.pipe(
map((): Image => { return this; }));
}
/**
* Cache the sequence edges.
*
* @description The sequence edges are cached asynchronously
* internally by the library.
*
* @param {Array<NavigationEdge>} edges - Sequence edges to cache.
* @ignore
*/
public cacheSequenceEdges(edges: NavigationEdge[]): void {
this._cache.cacheSequenceEdges(edges);
}
/**
* Cache the spatial edges.
*
* @description The spatial edges are cached asynchronously
* internally by the library.
*
* @param {Array<NavigationEdge>} edges - Spatial edges to cache.
* @ignore
*/
public cacheSpatialEdges(edges: NavigationEdge[]): void {
this._cache.cacheSpatialEdges(edges);
}
/**
* Dispose the image.
*
* @description Disposes all cached assets.
* @ignore
*/
public dispose(): void {
if (this._cache != null) {
this._cache.dispose();
this._cache = null;
}
this._core = null;
this._spatial = null;
}
/**
* Initialize the image cache.
*
* @description The image cache is initialized internally by
* the library.
*
* @param {ImageCache} cache - The image cache to set as cache.
* @ignore
*/
public initializeCache(cache: ImageCache): void {
if (this._cache != null) {
throw new Error(`Image cache already initialized (${this.id}).`);
}
this._cache = cache;
}
/**
* Complete an image with spatial properties.
*
* @description The image is completed internally by
* the library.
*
* @param {SpatialImageEnt} fill - The spatial image struct.
* @ignore
*/
public makeComplete(fill: SpatialImageEnt): void {
if (fill == null) {
throw new Error("Fill can not be null.");
}
this._spatial = fill;
}
/**
* Reset the sequence edges.
*
* @ignore
*/
public resetSequenceEdges(): void {
this._cache.resetSequenceEdges();
}
/**
* Reset the spatial edges.
*
* @ignore
*/
public resetSpatialEdges(): void {
this._cache.resetSpatialEdges();
}
/**
* Clears the image and mesh assets, aborts
* any outstanding requests and resets edges.
*
* @ignore
*/
public uncache(): void {
if (this._cache == null) {
return;
}
this._cache.dispose();
this._cache = null;
}
}