Explorar el Código

Introduced TrackerResult.of(). Made other changes

customisations
alemart hace 2 meses
padre
commit
e18ce66af9

+ 17
- 5
src/core/session.ts Ver fichero

@@ -33,7 +33,7 @@ import { Settings } from './settings';
33 33
 import { Stats } from './stats';
34 34
 import { Gizmos } from '../ui/gizmos';
35 35
 import { Frame } from './frame';
36
-import { Tracker } from '../trackers/tracker';
36
+import { Trackable, Tracker, TrackerResult } from '../trackers/tracker';
37 37
 import { TimeManager } from './time-manager';
38 38
 import { Source } from '../sources/source';
39 39
 import { VideoSource } from '../sources/video-source';
@@ -87,6 +87,21 @@ const DEFAULT_OPTIONS: Readonly<Required<SessionOptions>> = {
87 87
     gizmos: false,
88 88
 };
89 89
 
90
+/** Helper class */
91
+class EmptyTrackerResult extends TrackerResult
92
+{
93
+    readonly tracker: Tracker;
94
+    readonly trackables: Trackable[];
95
+
96
+    constructor(tracker: Tracker)
97
+    {
98
+        super();
99
+        this.tracker = tracker;
100
+        this.trackables = [];
101
+    }
102
+}
103
+
104
+
90 105
 
91 106
 
92 107
 /**
@@ -707,10 +722,7 @@ export class Session extends AREventTarget<SessionEvent>
707 722
 
708 723
                 // create a frame
709 724
                 const results = this._trackers.map(tracker =>
710
-                    tracker._output.exports || ({
711
-                        tracker: tracker,
712
-                        trackables: [],
713
-                    })
725
+                    tracker._output.exports || new EmptyTrackerResult(tracker)
714 726
                 );
715 727
                 const frame = new Frame(this, results);
716 728
 

+ 1
- 2
src/sources/canvas-source.ts Ver fichero

@@ -55,14 +55,13 @@ export class CanvasSource implements Source
55 55
      * A type-identifier of the source of data
56 56
      * @internal
57 57
      */
58
-    get _type(): string
58
+    get _type(): keyof SourceType
59 59
     {
60 60
         return 'canvas';
61 61
     }
62 62
 
63 63
     /**
64 64
      * Check if this source is of a certain type
65
-     * This is a convenient type-narrowing utility
66 65
      * @internal
67 66
      */
68 67
     _is<T extends keyof SourceType>(type: T): this is SourceType[T]

+ 1
- 2
src/sources/pointer-source.ts Ver fichero

@@ -55,14 +55,13 @@ export class PointerSource implements Source
55 55
      * A type-identifier of the source of data
56 56
      * @internal
57 57
      */
58
-    get _type(): string
58
+    get _type(): keyof SourceType
59 59
     {
60 60
         return 'pointer-source';
61 61
     }
62 62
 
63 63
     /**
64 64
      * Check if this source is of a certain type
65
-     * This is a convenient type-narrowing utility
66 65
      * @internal
67 66
      */
68 67
     _is<T extends keyof SourceType>(type: T): this is SourceType[T]

+ 2
- 2
src/sources/source.ts Ver fichero

@@ -31,9 +31,9 @@ import { VideoSource } from './video-source';
31 31
 export interface Source
32 32
 {
33 33
     /** @internal type-identifier of the source of data */
34
-    readonly _type: string;
34
+    readonly _type: keyof SourceType;
35 35
 
36
-    /** @internal check if this source is of a certain type - this is a convenient type-narrowing utility */
36
+    /** @internal check if this source is of a certain type */
37 37
     _is<T extends keyof SourceType>(type: T): this is SourceType[T];
38 38
 
39 39
     /** @internal method to initialize the source of data (gets the data ready) */

+ 1
- 2
src/sources/video-source.ts Ver fichero

@@ -70,14 +70,13 @@ export class VideoSource implements Source
70 70
      * A type-identifier of the source of data
71 71
      * @internal
72 72
      */
73
-    get _type(): string
73
+    get _type(): keyof SourceType
74 74
     {
75 75
         return 'video';
76 76
     }
77 77
 
78 78
     /**
79 79
      * Check if this source is of a certain type
80
-     * This is a convenient type-narrowing utility
81 80
      * @internal
82 81
      */
83 82
     _is<T extends keyof SourceType>(type: T): this is SourceType[T]

+ 16
- 3
src/trackers/image-tracker/image-tracker.ts Ver fichero

@@ -33,7 +33,7 @@ import { SpeedyPipelineNodeFASTKeypointDetector } from 'speedy-vision/types/core
33 33
 import { SpeedyKeypoint } from 'speedy-vision/types/core/speedy-keypoint';
34 34
 import { VideoSource } from '../../sources/video-source';
35 35
 import { CanvasSource } from '../../sources/canvas-source';
36
-import { Tracker, TrackerOutput, TrackerResult, Trackable, TrackerType } from '../tracker';
36
+import { Tracker, TrackerOutput, TrackerResult, Trackable, TrackerType, TrackerResultType } from '../tracker';
37 37
 import { Session } from '../../core/session';
38 38
 import { IllegalOperationError, IllegalArgumentError } from '../../utils/errors';
39 39
 import { Resolution } from '../../utils/resolution';
@@ -65,7 +65,7 @@ export interface TrackableImage extends Trackable
65 65
 }
66 66
 
67 67
 /** Image Tracker result to be consumed by the user */
68
-export interface ImageTrackerResult extends TrackerResult
68
+export class ImageTrackerResult extends TrackerResult
69 69
 {
70 70
     /** tracker */
71 71
     readonly tracker: ImageTracker;
@@ -75,6 +75,20 @@ export interface ImageTrackerResult extends TrackerResult
75 75
 
76 76
     /** 3D virtual camera */
77 77
     readonly viewer: Viewer;
78
+
79
+    /**
80
+     * Constructor
81
+     * @param tracker
82
+     * @param trackables
83
+     * @param viewer
84
+     */
85
+    constructor(tracker: ImageTracker, trackables: TrackableImage[], viewer: Viewer)
86
+    {
87
+        super();
88
+        this.tracker = tracker;
89
+        this.trackables = trackables;
90
+        this.viewer = viewer;
91
+    }
78 92
 }
79 93
 
80 94
 /** Image Tracker output */
@@ -185,7 +199,6 @@ export class ImageTracker extends AREventTarget<ImageTrackerEvent> implements Tr
185 199
 
186 200
     /**
187 201
      * Check if this tracker is of a certain type
188
-     * This is a convenient type-narrowing utility
189 202
      */
190 203
     is<T extends keyof TrackerType>(type: T): this is TrackerType[T]
191 204
     {

+ 5
- 5
src/trackers/image-tracker/states/tracking.ts Ver fichero

@@ -433,11 +433,11 @@ export class ImageTrackerTrackingState extends ImageTrackerState
433 433
             };
434 434
 
435 435
             // the result generated by the image tracker
436
-            const result: ImageTrackerResult = {
437
-                tracker: this._imageTracker,
438
-                trackables: [ trackable ],
439
-                viewer: viewer
440
-            };
436
+            const result = new ImageTrackerResult(
437
+                this._imageTracker,
438
+                [ trackable ],
439
+                viewer
440
+            );
441 441
 
442 442
             // tracker output
443 443
             const trackerOutput: ImageTrackerOutput = {

+ 15
- 8
src/trackers/pointer-tracker/pointer-tracker.ts Ver fichero

@@ -34,13 +34,25 @@ import { Viewport } from '../../core/viewport';
34 34
 /**
35 35
  * A result of a PointerTracker. It's meant to be consumed by the user/application
36 36
  */
37
-export interface PointerTrackerResult extends TrackerResult
37
+export class PointerTrackerResult extends TrackerResult
38 38
 {
39 39
     /** the tracker that generated this result */
40 40
     readonly tracker: PointerTracker;
41 41
 
42 42
     /** the trackables */
43 43
     readonly trackables: TrackablePointer[];
44
+
45
+    /**
46
+     * Constructor
47
+     * @param tracker
48
+     * @param trackables
49
+     */
50
+    constructor(tracker: PointerTracker, trackables: TrackablePointer[])
51
+    {
52
+        super();
53
+        this.tracker = tracker;
54
+        this.trackables = trackables;
55
+    }
44 56
 }
45 57
 
46 58
 /**
@@ -196,7 +208,6 @@ export class PointerTracker implements Tracker
196 208
 
197 209
     /**
198 210
      * Check if this tracker is of a certain type
199
-     * This is a convenient type-narrowing utility
200 211
      */
201 212
     is<T extends keyof TrackerType>(type: T): this is TrackerType[T]
202 213
     {
@@ -480,12 +491,8 @@ export class PointerTracker implements Tracker
480 491
         const trackables: TrackablePointer[] = [];
481 492
         this._activePointers.forEach(trackable => trackables.push(trackable));
482 493
 
483
-        return {
484
-            exports: {
485
-                tracker: this,
486
-                trackables: this._sortTrackables(trackables)
487
-            }
488
-        };
494
+        const result = new PointerTrackerResult(this, this._sortTrackables(trackables));
495
+        return { exports: result };
489 496
     }
490 497
 
491 498
     /**

+ 21
- 6
src/trackers/tracker.ts Ver fichero

@@ -23,8 +23,8 @@
23 23
 import { Session } from '../core/session';
24 24
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
25 25
 import { SpeedyMedia } from 'speedy-vision/types/core/speedy-media';
26
-import { ImageTracker } from './image-tracker/image-tracker';
27
-import { PointerTracker } from './pointer-tracker/pointer-tracker';
26
+import { ImageTracker, ImageTrackerResult } from './image-tracker/image-tracker';
27
+import { PointerTracker, PointerTrackerResult } from './pointer-tracker/pointer-tracker';
28 28
 
29 29
 /**
30 30
  * A Trackable is something that can be tracked
@@ -39,13 +39,19 @@ export interface Trackable
39 39
  * The result of a Tracker in a particular frame of a session. Such result is
40 40
  * meant to be consumed by the user/application.
41 41
  */
42
-export interface TrackerResult
42
+export abstract class TrackerResult
43 43
 {
44 44
     /** the tracker that generated this result */
45
-    readonly tracker: Tracker;
45
+    abstract readonly tracker: Tracker;
46 46
 
47 47
     /** an array of trackables (possibly empty) */
48
-    readonly trackables: Trackable[];
48
+    abstract readonly trackables: Trackable[];
49
+
50
+    /** check if this result was generated by a tracker of a certain type */
51
+    of<T extends keyof TrackerResultType>(trackerType: T): this is TrackerResultType[T]
52
+    {
53
+        return this.tracker.is(trackerType);
54
+    }
49 55
 }
50 56
 
51 57
 /**
@@ -65,7 +71,7 @@ export interface TrackerOutput
65 71
  */
66 72
 export interface Tracker
67 73
 {
68
-    /** check if this tracker is of a certain type - this is a convenient type-narrowing utility */
74
+    /** check if this tracker is of a certain type */
69 75
     is<T extends keyof TrackerType>(type: T): this is TrackerType[T];
70 76
 
71 77
     /** a string that identifies the type of the tracker @deprecated */
@@ -94,4 +100,13 @@ export interface Tracker
94 100
 export type TrackerType = {
95 101
     'image-tracker': ImageTracker,
96 102
     'pointer-tracker': PointerTracker
103
+};
104
+
105
+/**
106
+ * A helper for type-narrowing
107
+ * @internal
108
+ */
109
+export type TrackerResultType = {
110
+    'image-tracker': ImageTrackerResult,
111
+    'pointer-tracker': PointerTrackerResult
97 112
 };

Loading…
Cancelar
Guardar