Pārlūkot izejas kodu

Gizmos: add the ability to render polylines and keypoints in NDC and NIS, respectively

customisations
alemart 10 mēnešus atpakaļ
vecāks
revīzija
27e5bba23f
1 mainītis faili ar 98 papildinājumiem un 0 dzēšanām
  1. 98
    0
      src/ui/gizmos.ts

+ 98
- 0
src/ui/gizmos.ts Parādīt failu

@@ -28,6 +28,7 @@ import { SpeedyKeypoint, SpeedyMatchedKeypoint } from 'speedy-vision/types/core/
28 28
 import { Viewport } from '../core/viewport';
29 29
 import { Tracker, TrackerOutput } from '../trackers/tracker';
30 30
 import { ImageTrackerOutput } from '../trackers/image-tracker/image-tracker';
31
+import { NIS_SIZE } from '../trackers/image-tracker/settings';
31 32
 
32 33
 
33 34
 
@@ -124,7 +125,9 @@ class ImageTrackerGizmos implements GizmosRenderer
124 125
         const viewportSize = viewport._realSize;
125 126
         const screenSize = output.screenSize;
126 127
         const keypoints = output.keypoints;
128
+        const keypointsNIS = output.keypointsNIS;
127 129
         const polyline = output.polyline;
130
+        const polylineNDC = output.polylineNDC;
128 131
         const cameraMatrix = output.cameraMatrix;
129 132
 
130 133
         // debug
@@ -136,10 +139,18 @@ class ImageTrackerGizmos implements GizmosRenderer
136 139
         if(keypoints !== undefined && screenSize !== undefined)
137 140
             this._splitAndRenderKeypoints(ctx, keypoints, screenSize, viewportSize);
138 141
 
142
+        // render keypoints
143
+        if(keypointsNIS !== undefined)
144
+            this._splitAndRenderKeypointsNIS(ctx, keypointsNIS, viewportSize);
145
+
139 146
         // render polylines
140 147
         if(polyline !== undefined && screenSize !== undefined)
141 148
             this._renderPolyline(ctx, polyline, screenSize, viewportSize);
142 149
 
150
+        // render polylines
151
+        if(polylineNDC !== undefined)
152
+            this._renderPolylineNDC(ctx, polylineNDC, viewportSize, '#f8f');
153
+
143 154
         // render the axes of the 3D coordinate system
144 155
         if(cameraMatrix !== undefined && screenSize !== undefined)
145 156
             this._renderAxes(ctx, cameraMatrix, screenSize, viewportSize);
@@ -173,6 +184,38 @@ class ImageTrackerGizmos implements GizmosRenderer
173 184
     }
174 185
 
175 186
     /**
187
+     * Split keypoints in matched/unmatched categories and
188
+     * render them for testing & development purposes
189
+     * @param ctx canvas 2D context
190
+     * @param keypoints keypoints in Normalized Image Space (NIS)
191
+     * @param viewportSize viewport size
192
+     * @param size base keypoint rendering size
193
+     */
194
+    private _splitAndRenderKeypointsNIS(ctx: CanvasRenderingContext2D, keypoints: SpeedyKeypoint[], viewportSize: SpeedySize, size = 1): void
195
+    {
196
+        if(keypoints.length == 0)
197
+            return;
198
+
199
+        if(!Object.prototype.hasOwnProperty.call(keypoints[0], '_matches')) { // hack...
200
+            this._renderKeypointsNIS(ctx, keypoints, viewportSize, '#f00', size);
201
+            return;
202
+        }
203
+
204
+        const goodMatches = [], badMatches = [];
205
+        for(let i = 0; i < keypoints.length; i++) {
206
+            const keypoint = keypoints[i] as SpeedyMatchedKeypoint;
207
+
208
+            if(this._isGoodMatch(keypoint))
209
+                goodMatches.push(keypoint);
210
+            else
211
+                badMatches.push(keypoint);
212
+        }
213
+
214
+        this._renderKeypointsNIS(ctx, badMatches, viewportSize, '#f00', size);
215
+        this._renderKeypointsNIS(ctx, goodMatches, viewportSize, '#0f0', size);
216
+    }
217
+
218
+    /**
176 219
      * Check if a matched keypoint is "good enough"
177 220
      * @param keypoint matched keypoint
178 221
      * @returns a boolean
@@ -226,6 +269,35 @@ class ImageTrackerGizmos implements GizmosRenderer
226 269
     }
227 270
 
228 271
     /**
272
+     * Render keypoints for testing & development purposes
273
+     * @param ctx canvas 2D context
274
+     * @param keypoints keypoints in Normalized Image Space (NIS)
275
+     * @param viewportSize viewport size
276
+     * @param color color of the rendered keypoints
277
+     * @param size base keypoint rendering size
278
+     */
279
+    private _renderKeypointsNIS(ctx: CanvasRenderingContext2D, keypoints: SpeedyKeypoint[], viewportSize: SpeedySize, color = 'red', size = 1): void
280
+    {
281
+        const sx = viewportSize.width / NIS_SIZE;
282
+        const sy = viewportSize.height / NIS_SIZE;
283
+
284
+        ctx.beginPath();
285
+
286
+        for(let i = keypoints.length - 1; i >= 0; i--) {
287
+            const keypoint = keypoints[i];
288
+            const x = (keypoint.x * sx + 0.5) | 0;
289
+            const y = (keypoint.y * sy + 0.5) | 0;
290
+            const r = (size * keypoint.scale + 0.5) | 0;
291
+
292
+            ctx.rect(x-r, y-r, 2*r, 2*r);
293
+        }
294
+
295
+        ctx.strokeStyle = color;
296
+        ctx.lineWidth = 1;
297
+        ctx.stroke();
298
+    }
299
+
300
+    /**
229 301
      * Render polyline for testing & development purposes
230 302
      * @param ctx canvas 2D context
231 303
      * @param polyline vertices
@@ -256,6 +328,32 @@ class ImageTrackerGizmos implements GizmosRenderer
256 328
     }
257 329
 
258 330
     /**
331
+     * Render a polyline for testing & development purposes
332
+     * @param ctx canvas 2D context
333
+     * @param polyline vertices in NDC
334
+     * @param viewportSize viewport size
335
+     * @param color color of the rendered polyline
336
+     * @param lineWidth
337
+     */
338
+    private _renderPolylineNDC(ctx: CanvasRenderingContext2D, polyline: SpeedyPoint2[], viewportSize: SpeedySize, color = '#0f0', lineWidth = 2): void
339
+    {
340
+        const n = polyline.length;
341
+        const w = viewportSize.width;
342
+        const h = viewportSize.height;
343
+
344
+        if(n == 0)
345
+            return;
346
+
347
+        ctx.beginPath();
348
+        ctx.moveTo((polyline[n-1].x * 0.5 + 0.5) * w, (polyline[n-1].y * -0.5 + 0.5) * h);
349
+        for(let j = 0; j < n; j++)
350
+            ctx.lineTo((polyline[j].x * 0.5 + 0.5) * w, (polyline[j].y * -0.5 + 0.5) * h);
351
+        ctx.strokeStyle = color;
352
+        ctx.lineWidth = lineWidth;
353
+        ctx.stroke();
354
+    }
355
+
356
+    /**
259 357
      * Render the axes of a 3D coordinate system
260 358
      * @param ctx canvas 2D context
261 359
      * @param cameraMatrix 3x4 camera matrix that maps normalized coordinates [-1,1]^3 to AR screen space

Notiek ielāde…
Atcelt
Saglabāt