Browse Source

Introduced TrackerResult.of(). Made other changes

customisations
alemart 2 months ago
parent
commit
e18ce66af9

+ 17
- 5
src/core/session.ts View File

33
 import { Stats } from './stats';
33
 import { Stats } from './stats';
34
 import { Gizmos } from '../ui/gizmos';
34
 import { Gizmos } from '../ui/gizmos';
35
 import { Frame } from './frame';
35
 import { Frame } from './frame';
36
-import { Tracker } from '../trackers/tracker';
36
+import { Trackable, Tracker, TrackerResult } from '../trackers/tracker';
37
 import { TimeManager } from './time-manager';
37
 import { TimeManager } from './time-manager';
38
 import { Source } from '../sources/source';
38
 import { Source } from '../sources/source';
39
 import { VideoSource } from '../sources/video-source';
39
 import { VideoSource } from '../sources/video-source';
87
     gizmos: false,
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
 
722
 
708
                 // create a frame
723
                 // create a frame
709
                 const results = this._trackers.map(tracker =>
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
                 const frame = new Frame(this, results);
727
                 const frame = new Frame(this, results);
716
 
728
 

+ 1
- 2
src/sources/canvas-source.ts View File

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

+ 1
- 2
src/sources/pointer-source.ts View File

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

+ 2
- 2
src/sources/source.ts View File

31
 export interface Source
31
 export interface Source
32
 {
32
 {
33
     /** @internal type-identifier of the source of data */
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
     _is<T extends keyof SourceType>(type: T): this is SourceType[T];
37
     _is<T extends keyof SourceType>(type: T): this is SourceType[T];
38
 
38
 
39
     /** @internal method to initialize the source of data (gets the data ready) */
39
     /** @internal method to initialize the source of data (gets the data ready) */

+ 1
- 2
src/sources/video-source.ts View File

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

+ 16
- 3
src/trackers/image-tracker/image-tracker.ts View File

33
 import { SpeedyKeypoint } from 'speedy-vision/types/core/speedy-keypoint';
33
 import { SpeedyKeypoint } from 'speedy-vision/types/core/speedy-keypoint';
34
 import { VideoSource } from '../../sources/video-source';
34
 import { VideoSource } from '../../sources/video-source';
35
 import { CanvasSource } from '../../sources/canvas-source';
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
 import { Session } from '../../core/session';
37
 import { Session } from '../../core/session';
38
 import { IllegalOperationError, IllegalArgumentError } from '../../utils/errors';
38
 import { IllegalOperationError, IllegalArgumentError } from '../../utils/errors';
39
 import { Resolution } from '../../utils/resolution';
39
 import { Resolution } from '../../utils/resolution';
65
 }
65
 }
66
 
66
 
67
 /** Image Tracker result to be consumed by the user */
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
     /** tracker */
70
     /** tracker */
71
     readonly tracker: ImageTracker;
71
     readonly tracker: ImageTracker;
75
 
75
 
76
     /** 3D virtual camera */
76
     /** 3D virtual camera */
77
     readonly viewer: Viewer;
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
 /** Image Tracker output */
94
 /** Image Tracker output */
185
 
199
 
186
     /**
200
     /**
187
      * Check if this tracker is of a certain type
201
      * Check if this tracker is of a certain type
188
-     * This is a convenient type-narrowing utility
189
      */
202
      */
190
     is<T extends keyof TrackerType>(type: T): this is TrackerType[T]
203
     is<T extends keyof TrackerType>(type: T): this is TrackerType[T]
191
     {
204
     {

+ 5
- 5
src/trackers/image-tracker/states/tracking.ts View File

433
             };
433
             };
434
 
434
 
435
             // the result generated by the image tracker
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
             // tracker output
442
             // tracker output
443
             const trackerOutput: ImageTrackerOutput = {
443
             const trackerOutput: ImageTrackerOutput = {

+ 15
- 8
src/trackers/pointer-tracker/pointer-tracker.ts View File

34
 /**
34
 /**
35
  * A result of a PointerTracker. It's meant to be consumed by the user/application
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
     /** the tracker that generated this result */
39
     /** the tracker that generated this result */
40
     readonly tracker: PointerTracker;
40
     readonly tracker: PointerTracker;
41
 
41
 
42
     /** the trackables */
42
     /** the trackables */
43
     readonly trackables: TrackablePointer[];
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
 
208
 
197
     /**
209
     /**
198
      * Check if this tracker is of a certain type
210
      * Check if this tracker is of a certain type
199
-     * This is a convenient type-narrowing utility
200
      */
211
      */
201
     is<T extends keyof TrackerType>(type: T): this is TrackerType[T]
212
     is<T extends keyof TrackerType>(type: T): this is TrackerType[T]
202
     {
213
     {
480
         const trackables: TrackablePointer[] = [];
491
         const trackables: TrackablePointer[] = [];
481
         this._activePointers.forEach(trackable => trackables.push(trackable));
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 View File

23
 import { Session } from '../core/session';
23
 import { Session } from '../core/session';
24
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
24
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
25
 import { SpeedyMedia } from 'speedy-vision/types/core/speedy-media';
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
  * A Trackable is something that can be tracked
30
  * A Trackable is something that can be tracked
39
  * The result of a Tracker in a particular frame of a session. Such result is
39
  * The result of a Tracker in a particular frame of a session. Such result is
40
  * meant to be consumed by the user/application.
40
  * meant to be consumed by the user/application.
41
  */
41
  */
42
-export interface TrackerResult
42
+export abstract class TrackerResult
43
 {
43
 {
44
     /** the tracker that generated this result */
44
     /** the tracker that generated this result */
45
-    readonly tracker: Tracker;
45
+    abstract readonly tracker: Tracker;
46
 
46
 
47
     /** an array of trackables (possibly empty) */
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
  */
71
  */
66
 export interface Tracker
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
     is<T extends keyof TrackerType>(type: T): this is TrackerType[T];
75
     is<T extends keyof TrackerType>(type: T): this is TrackerType[T];
70
 
76
 
71
     /** a string that identifies the type of the tracker @deprecated */
77
     /** a string that identifies the type of the tracker @deprecated */
94
 export type TrackerType = {
100
 export type TrackerType = {
95
     'image-tracker': ImageTracker,
101
     'image-tracker': ImageTracker,
96
     'pointer-tracker': PointerTracker
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…
Cancel
Save