瀏覽代碼

Refactor Gizmos

customisations
alemart 10 月之前
父節點
當前提交
bd94d60168
共有 1 個文件被更改,包括 79 次插入53 次删除
  1. 79
    53
      src/ui/gizmos.ts

+ 79
- 53
src/ui/gizmos.ts 查看文件

@@ -21,22 +21,14 @@
21 21
  */
22 22
 
23 23
 import Speedy from 'speedy-vision';
24
-import { SpeedyMedia } from 'speedy-vision/types/core/speedy-media';
25 24
 import { SpeedyPoint2 } from 'speedy-vision/types/core/speedy-point';
26 25
 import { SpeedySize } from 'speedy-vision/types/core/speedy-size';
27 26
 import { SpeedyMatrix } from 'speedy-vision/types/core/speedy-matrix';
28 27
 import { SpeedyKeypoint, SpeedyMatchedKeypoint } from 'speedy-vision/types/core/speedy-keypoint';
29
-import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
30
-import { Resolution } from '../utils/resolution';
31
-import { Nullable, Utils } from '../utils/utils';
32
-import { IllegalArgumentError, IllegalOperationError, TrackingError } from '../utils/errors';
33 28
 import { Viewport } from '../core/viewport';
34
-import { Tracker } from '../trackers/tracker';
29
+import { Tracker, TrackerOutput } from '../trackers/tracker';
35 30
 import { ImageTrackerOutput } from '../trackers/image-tracker/image-tracker';
36 31
 
37
-/** The maximum match distance ratio we'll consider to be "good" */
38
-const GOOD_MATCH_THRESHOLD = 0.7;
39
-
40 32
 
41 33
 
42 34
 /**
@@ -47,12 +39,18 @@ export class Gizmos
47 39
     /** Should we render the gizmos? */
48 40
     private _visible: boolean;
49 41
 
42
+    /** Gizmos renderer of Image Trackers */
43
+    private _imageTrackerGizmos: ImageTrackerGizmos;
44
+
45
+
46
+
50 47
     /**
51 48
      * Constructor
52 49
      */
53 50
     constructor()
54 51
     {
55 52
         this._visible = false;
53
+        this._imageTrackerGizmos = new ImageTrackerGizmos();
56 54
     }
57 55
 
58 56
     /**
@@ -83,56 +81,68 @@ export class Gizmos
83 81
         if(!this._visible)
84 82
             return;
85 83
 
86
-        // viewport
87
-        const viewportSize = viewport._realSize;
84
+        // render the gizmos of each tracker
85
+        for(let i = 0; i < trackers.length; i++) {
86
+            if(trackers[i].type == 'image-tracker') {
87
+                const output = trackers[i]._output as ImageTrackerOutput;
88
+                this._imageTrackerGizmos.render(viewport, output);
89
+            }
90
+        }
91
+    }
92
+}
93
+
94
+
95
+
96
+/**
97
+ * Gizmos renderer
98
+ */
99
+interface GizmosRenderer
100
+{
101
+    render(viewport: Viewport, output: TrackerOutput): void;
102
+}
103
+
104
+
105
+
106
+/**
107
+ * Gizmos renderer of Image Trackers
108
+ */
109
+class ImageTrackerGizmos implements GizmosRenderer
110
+{
111
+    /**
112
+     * Render gizmos
113
+     * @param viewport viewport
114
+     * @param output tracker output
115
+     */
116
+    render(viewport: Viewport, output: ImageTrackerOutput): void
117
+    {
88 118
         const canvas = viewport._backgroundCanvas;
89 119
         const ctx = canvas.getContext('2d', { alpha: false });
120
+
90 121
         if(!ctx)
91 122
             return;
92 123
 
124
+        const viewportSize = viewport._realSize;
125
+        const screenSize = output.screenSize;
126
+        const keypoints = output.keypoints;
127
+        const polyline = output.polyline;
128
+        const cameraMatrix = output.cameraMatrix;
129
+
93 130
         // debug
94 131
         //ctx.fillStyle = '#000';
95 132
         //ctx.fillRect(0, 0, canvas.width, canvas.height);
96 133
         //ctx.clearRect(0, 0, canvas.width, canvas.height);
97 134
 
98 135
         // render keypoints
99
-        for(let i = 0; i < trackers.length; i++) {
100
-            if(trackers[i].type != 'image-tracker')
101
-                continue;
102
-
103
-            const output = trackers[i]._output as ImageTrackerOutput;
104
-            const keypoints = output.keypoints;
105
-            const screenSize = output.screenSize;
106
-
107
-            if(keypoints !== undefined && screenSize !== undefined)
108
-                this._splitAndRenderKeypoints(ctx, keypoints, screenSize, viewportSize);
109
-        }
136
+        if(keypoints !== undefined && screenSize !== undefined)
137
+            this._splitAndRenderKeypoints(ctx, keypoints, screenSize, viewportSize);
110 138
 
111 139
         // render polylines
112
-        for(let i = 0; i < trackers.length; i++) {
113
-            if(trackers[i].type != 'image-tracker')
114
-                continue;
115
-
116
-            const output = trackers[i]._output as ImageTrackerOutput;
117
-            const polyline = output.polyline;
118
-            const screenSize = output.screenSize;
119
-
120
-            if(polyline !== undefined && screenSize !== undefined)
121
-                this._renderPolyline(ctx, polyline, screenSize, viewportSize);
122
-        }
140
+        if(polyline !== undefined && screenSize !== undefined)
141
+            this._renderPolyline(ctx, polyline, screenSize, viewportSize);
123 142
 
124 143
         // render the axes of the 3D coordinate system
125
-        for(let i = 0; i < trackers.length; i++) {
126
-            if(trackers[i].type != 'image-tracker')
127
-                continue;
128
-
129
-            const output = trackers[i]._output as ImageTrackerOutput;
130
-            const cameraMatrix = output.cameraMatrix;
131
-            const screenSize = output.screenSize;
132
-
133
-            if(cameraMatrix !== undefined && screenSize !== undefined)
134
-                this._renderAxes(ctx, cameraMatrix, screenSize, viewportSize);
135
-        }
144
+        if(cameraMatrix !== undefined && screenSize !== undefined)
145
+            this._renderAxes(ctx, cameraMatrix, screenSize, viewportSize);
136 146
     }
137 147
 
138 148
     /**
@@ -154,22 +164,38 @@ export class Gizmos
154 164
             return;
155 165
         }
156 166
 
157
-        const isGoodMatch = (keypoint: SpeedyMatchedKeypoint) =>
158
-            (keypoint.matches.length == 1 && keypoint.matches[0].index >= 0) ||
159
-            (keypoint.matches.length > 1 &&
160
-                keypoint.matches[0].index >= 0 && keypoint.matches[1].index >= 0 &&
161
-                keypoint.matches[0].distance <= GOOD_MATCH_THRESHOLD * keypoint.matches[1].distance
162
-            );
163
-
164 167
         const matchedKeypoints = keypoints as SpeedyMatchedKeypoint[];
165
-        const goodMatches = matchedKeypoints.filter(keypoint => isGoodMatch(keypoint));
166
-        const badMatches = matchedKeypoints.filter(keypoint => !isGoodMatch(keypoint));
168
+        const goodMatches = matchedKeypoints.filter(keypoint => this._isGoodMatch(keypoint));
169
+        const badMatches = matchedKeypoints.filter(keypoint => !this._isGoodMatch(keypoint));
167 170
 
168 171
         this._renderKeypoints(ctx, badMatches, screenSize, viewportSize, '#f00', size);
169 172
         this._renderKeypoints(ctx, goodMatches, screenSize, viewportSize, '#0f0', size);
170 173
     }
171 174
 
172 175
     /**
176
+     * Check if a matched keypoint is "good enough"
177
+     * @param keypoint matched keypoint
178
+     * @returns a boolean
179
+     */
180
+    private _isGoodMatch(keypoint: SpeedyMatchedKeypoint): boolean
181
+    {
182
+        const GOOD_MATCH_THRESHOLD = 0.7; // the maximum match distance ratio we'll consider to be "good"
183
+        const n = keypoint.matches.length;
184
+
185
+        if(n > 1) {
186
+            return (
187
+                keypoint.matches[0].index >= 0 &&
188
+                keypoint.matches[1].index >= 0 &&
189
+                keypoint.matches[0].distance <= GOOD_MATCH_THRESHOLD * keypoint.matches[1].distance
190
+            );
191
+        }
192
+        else if(n == 1)
193
+            return keypoint.matches[0].index >= 0;
194
+
195
+        return false;
196
+    }
197
+
198
+    /**
173 199
      * Render keypoints for testing & development purposes
174 200
      * @param ctx canvas 2D context
175 201
      * @param keypoints keypoints to render

Loading…
取消
儲存