Kaynağa Gözat

Introduce Tracker.is() for type-narrowing

customisations
alemart 2 ay önce
ebeveyn
işleme
83468175d4

+ 1
- 1
demos/hello-three/demo.js Dosyayı Görüntüle

@@ -69,7 +69,7 @@ class Utils
69 69
             return null;
70 70
 
71 71
         for(const result of ar.frame.results) {
72
-            if(result.tracker.type == 'image-tracker') {
72
+            if(result.tracker.is('image-tracker')) {
73 73
                 if(result.trackables.length > 0) {
74 74
                     const trackable = result.trackables[0];
75 75
                     return trackable.referenceImage.name;

+ 1
- 1
demos/hello-world/demo.js Dosyayı Görüntüle

@@ -93,7 +93,7 @@ function animate(time, frame)
93 93
 function mix(frame)
94 94
 {
95 95
     for(const result of frame.results) {
96
-        if(result.tracker.type == 'image-tracker') {
96
+        if(result.tracker.is('image-tracker')) {
97 97
             if(result.trackables.length > 0) {
98 98
                 const trackable = result.trackables[0];
99 99
                 const projectionMatrix = result.viewer.view.projectionMatrix;

+ 1
- 1
demos/pointer-demo/demo.js Dosyayı Görüntüle

@@ -142,7 +142,7 @@ function animate(time, frame)
142 142
 function read(frame)
143 143
 {
144 144
     for(const result of frame.results) {
145
-        if(result.tracker.type == 'pointer-tracker') {
145
+        if(result.tracker.is('pointer-tracker')) {
146 146
             const pointers = result.trackables;
147 147
             return pointers;
148 148
         }

+ 38
- 2
docs/api/tracker.md Dosyayı Görüntüle

@@ -2,7 +2,7 @@
2 2
 
3 3
 An interface that represents a generic tracker. Trackers analyze input data in some way and are meant to be attached to a [session](session.md). Refer to the [concepts](../tutorial/concepts.md) for more information.
4 4
 
5
-An [Image Tracker](image-tracker.md) is an implementation of a tracker.
5
+An [Image Tracker](image-tracker.md) is an implementation of a tracker, and so is a [PointerTracker](pointer-tracker.md).
6 6
 
7 7
 ## Properties
8 8
 
@@ -10,4 +10,40 @@ An [Image Tracker](image-tracker.md) is an implementation of a tracker.
10 10
 
11 11
 `tracker.type: string, read-only`
12 12
 
13
-A string representing the type of the tracker.
13
+A string representing the type of the tracker.
14
+
15
+*Deprecated since:* 0.4.4. Use `tracker.is()` instead.
16
+
17
+## Methods
18
+
19
+### is
20
+
21
+`tracker.is(type: string): boolean`
22
+
23
+Checks if `this` tracker is of a certain `type`. This works as a convenient type-narrowing method for TypeScript users.
24
+
25
+*Since:* 0.4.4
26
+
27
+**Returns**
28
+
29
+`true` if and only if `type === tracker.type`
30
+
31
+**Example**
32
+
33
+```ts
34
+let tracker: Tracker;
35
+
36
+// ...
37
+
38
+if(tracker.is('image-tracker')) {
39
+    // tracker is inferred to be an ImageTracker
40
+    // ...
41
+}
42
+else if(tracker.is('pointer-tracker')) {
43
+    // tracker is inferred to be a PointerTracker
44
+    // ...
45
+}
46
+else {
47
+    // ...
48
+}
49
+```

+ 3
- 3
plugins/aframe-with-encantar.js Dosyayı Görüntüle

@@ -107,7 +107,7 @@ const Utils = () => ({
107 107
             return null;
108 108
 
109 109
         for(const result of frame.results) {
110
-            if(result.tracker.type == 'image-tracker') {
110
+            if(result.tracker.is('image-tracker')) {
111 111
                 for(const trackable of result.trackables) {
112 112
                     if(name === '' || name === trackable.referenceImage.name) {
113 113
                         return {
@@ -129,7 +129,7 @@ const Utils = () => ({
129 129
             return null;
130 130
 
131 131
         for(const result of frame.results) {
132
-            if(result.tracker.type == 'image-tracker') {
132
+            if(result.tracker.is('image-tracker')) {
133 133
                 if(result.trackables.length > 0)
134 134
                     return result.viewer;
135 135
             }
@@ -144,7 +144,7 @@ const Utils = () => ({
144 144
             return [];
145 145
 
146 146
         for(const result of frame.results) {
147
-            if(result.tracker.type == 'pointer-tracker')
147
+            if(result.tracker.is('pointer-tracker'))
148 148
                 return result.trackables;
149 149
         }
150 150
 

+ 2
- 2
plugins/babylon-with-encantar.js Dosyayı Görüntüle

@@ -268,7 +268,7 @@ function encantar(demo)
268 268
         ar._pointers.length = 0;
269 269
 
270 270
         for(const result of frame.results) {
271
-            if(result.tracker.type == 'image-tracker') {
271
+            if(result.tracker.is('image-tracker')) {
272 272
                 if(result.trackables.length > 0) {
273 273
                     const trackable = result.trackables[0];
274 274
                     const projectionMatrix = result.viewer.view.projectionMatrix;
@@ -282,7 +282,7 @@ function encantar(demo)
282 282
                     found = true;
283 283
                 }
284 284
             }
285
-            else if(result.tracker.type == 'pointer-tracker') {
285
+            else if(result.tracker.is('pointer-tracker')) {
286 286
                 if(result.trackables.length > 0)
287 287
                     ar._pointers.push.apply(ar._pointers, result.trackables);
288 288
             }

+ 2
- 2
plugins/three-with-encantar.js Dosyayı Görüntüle

@@ -262,7 +262,7 @@ export function encantar(demo)
262 262
         ar._pointers.length = 0;
263 263
 
264 264
         for(const result of frame.results) {
265
-            if(result.tracker.type == 'image-tracker') {
265
+            if(result.tracker.is('image-tracker')) {
266 266
                 if(result.trackables.length > 0) {
267 267
                     const trackable = result.trackables[0];
268 268
                     const projectionMatrix = result.viewer.view.projectionMatrix;
@@ -276,7 +276,7 @@ export function encantar(demo)
276 276
                     found = true;
277 277
                 }
278 278
             }
279
-            else if(result.tracker.type == 'pointer-tracker') {
279
+            else if(result.tracker.is('pointer-tracker')) {
280 280
                 if(result.trackables.length > 0)
281 281
                     ar._pointers.push.apply(ar._pointers, result.trackables);
282 282
             }

+ 12
- 2
src/trackers/image-tracker/image-tracker.ts Dosyayı Görüntüle

@@ -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 } from '../tracker';
36
+import { Tracker, TrackerOutput, TrackerResult, Trackable, TrackerType } from '../tracker';
37 37
 import { Session } from '../../core/session';
38 38
 import { IllegalOperationError, IllegalArgumentError } from '../../utils/errors';
39 39
 import { Resolution } from '../../utils/resolution';
@@ -176,13 +176,23 @@ export class ImageTracker extends AREventTarget<ImageTrackerEvent> implements Tr
176 176
 
177 177
     /**
178 178
      * The type of the tracker
179
+     * @deprecated
179 180
      */
180
-    get type(): string
181
+    get type(): keyof TrackerType
181 182
     {
182 183
         return 'image-tracker';
183 184
     }
184 185
 
185 186
     /**
187
+     * Check if this tracker is of a certain type
188
+     * This is a convenient type-narrowing utility
189
+     */
190
+    is<T extends keyof TrackerType>(type: T): this is TrackerType[T]
191
+    {
192
+        return type === this.type;
193
+    }
194
+
195
+    /**
186 196
      * Current state name
187 197
      */
188 198
     get state(): ImageTrackerStateName

+ 12
- 2
src/trackers/pointer-tracker/pointer-tracker.ts Dosyayı Görüntüle

@@ -22,7 +22,7 @@
22 22
 
23 23
 import Speedy from 'speedy-vision';
24 24
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
25
-import { TrackerResult, TrackerOutput, Tracker } from '../tracker';
25
+import { TrackerResult, TrackerOutput, Tracker, TrackerType } from '../tracker';
26 26
 import { TrackablePointer, TrackablePointerPhase } from './trackable-pointer';
27 27
 import { PointerSource } from '../../sources/pointer-source';
28 28
 import { Vector2 } from '../../geometry/vector2';
@@ -187,13 +187,23 @@ export class PointerTracker implements Tracker
187 187
 
188 188
     /**
189 189
      * The type of the tracker
190
+     * @deprecated
190 191
      */
191
-    get type(): string
192
+    get type(): keyof TrackerType
192 193
     {
193 194
         return 'pointer-tracker';
194 195
     }
195 196
 
196 197
     /**
198
+     * Check if this tracker is of a certain type
199
+     * This is a convenient type-narrowing utility
200
+     */
201
+    is<T extends keyof TrackerType>(type: T): this is TrackerType[T]
202
+    {
203
+        return type === this.type;
204
+    }
205
+
206
+    /**
197 207
      * Initialize the tracker
198 208
      * @param session
199 209
      * @returns a promise that is resolved as soon as the tracker is initialized

+ 20
- 6
src/trackers/tracker.ts Dosyayı Görüntüle

@@ -23,6 +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 28
 
27 29
 /**
28 30
  * A Trackable is something that can be tracked
@@ -63,21 +65,33 @@ export interface TrackerOutput
63 65
  */
64 66
 export interface Tracker
65 67
 {
66
-    /** a string that identifies the type of the tracker */
67
-    readonly type: string;
68
+    /** check if this tracker is of a certain type - this is a convenient type-narrowing utility */
69
+    is<T extends keyof TrackerType>(type: T): this is TrackerType[T];
70
+
71
+    /** a string that identifies the type of the tracker @deprecated */
72
+    readonly type: keyof TrackerType;
68 73
 
69 74
     /** initialize tracker @internal */
70
-    _init: (session: Session) => SpeedyPromise<void>;
75
+    _init(session: Session): SpeedyPromise<void>;
71 76
 
72 77
     /** release resources @internal */
73
-    _release: () => SpeedyPromise<void>;
78
+    _release(): SpeedyPromise<void>;
74 79
 
75 80
     /** update cycle @internal */
76
-    _update: () => SpeedyPromise<void>;
81
+    _update(): SpeedyPromise<void>;
77 82
 
78 83
     /** output of the last frame @internal */
79 84
     readonly _output: TrackerOutput;
80 85
 
81 86
     /** stats related to this tracker @internal */
82 87
     readonly _stats: string;
83
-}
88
+}
89
+
90
+/**
91
+ * A helper for type-narrowing
92
+ * @internal
93
+ */
94
+export type TrackerType = {
95
+    'image-tracker': ImageTracker,
96
+    'pointer-tracker': PointerTracker
97
+};

+ 3
- 2
src/ui/gizmos.ts Dosyayı Görüntüle

@@ -85,8 +85,9 @@ export class Gizmos
85 85
 
86 86
         // render the gizmos of each tracker
87 87
         for(let i = 0; i < trackers.length; i++) {
88
-            if(trackers[i].type == 'image-tracker') {
89
-                const output = trackers[i]._output as ImageTrackerOutput;
88
+            const tracker = trackers[i];
89
+            if(tracker.is('image-tracker')) {
90
+                const output = tracker._output;
90 91
                 this._imageTrackerGizmos.render(viewport, output);
91 92
             }
92 93
         }

Loading…
İptal
Kaydet