Browse Source

Update the computation of the modelViewMatrix

customisations
alemart 3 years ago
parent
commit
f55cfbc7d3

+ 0
- 19
src/geometry/camera-model.ts View File

@@ -315,25 +315,6 @@ export class CameraModel
315 315
     }
316 316
 
317 317
     /**
318
-     * The inverse of denormalizer()
319
-     * @returns 4x4 matrix
320
-     */
321
-    denormalizerInverse(): SpeedyMatrix
322
-    {
323
-        const w = this._screenSize.width / 2;
324
-        const h = this._screenSize.height / 2;
325
-        const d = Math.min(w, h);
326
-        const b = 1 / d;
327
-
328
-        return Speedy.Matrix(4, 4, [
329
-            b, 0, 0, 0,
330
-            0,-b, 0, 0,
331
-            0, 0,-b, 0,
332
-            -w*b, h*b, 0, 1,
333
-        ]);
334
-    }
335
-
336
-    /**
337 318
      * Size of the AR screen space, in pixels
338 319
      * @returns size in pixels
339 320
      */

+ 8
- 7
src/geometry/pose.ts View File

@@ -22,34 +22,35 @@
22 22
 
23 23
 import Speedy from 'speedy-vision';
24 24
 import { SpeedyMatrix } from 'speedy-vision/types/core/speedy-matrix';
25
-import { RigidTransform } from './transform';
25
+import { RigidTransform, StandardTransform } from './transform';
26 26
 
27 27
 
28 28
 
29 29
 /**
30 30
  * A pose represents a position and an orientation in a 3D space
31
+ * (and sometimes a scale, too...)
31 32
  */
32 33
 export class Pose
33 34
 {
34
-    /** A rigid transform in 3D world space */
35
-    private _transform: RigidTransform;
35
+    /** A transform in 3D world space */
36
+    private _transform: StandardTransform;
36 37
 
37 38
 
38 39
 
39 40
     /**
40 41
      * Constructor
41
-     * @param transform a rigid transform in a 3D space (e.g., world space, viewer space or other)
42
+     * @param transform usually a rigid transform in a 3D space (e.g., world space, viewer space or other)
42 43
      */
43
-    constructor(transform: RigidTransform)
44
+    constructor(transform: StandardTransform)
44 45
     {
45 46
         this._transform = transform;
46 47
     }
47 48
 
48 49
     /**
49
-     * A rigid transform describing the position and the orientation
50
+     * A transform describing the position and the orientation
50 51
      * of the pose relative to the 3D space to which it belongs
51 52
      */
52
-    get transform(): RigidTransform
53
+    get transform(): StandardTransform
53 54
     {
54 55
         return this._transform;
55 56
     }

+ 56
- 13
src/geometry/transform.ts View File

@@ -58,41 +58,84 @@ abstract class BaseTransform
58 58
 /**
59 59
  * An invertible 3D transformation
60 60
  */
61
-abstract class InvertibleTransform extends BaseTransform
61
+class InvertibleTransform extends BaseTransform
62 62
 {
63
-    abstract get inverse(): InvertibleTransform;
63
+    /**
64
+     * Constructor
65
+     * @param matrix a 4x4 matrix
66
+     */
67
+    constructor(matrix: SpeedyMatrix)
68
+    {
69
+        // WARNING: we do not check if the matrix actually encodes an invertible transform!
70
+        super(matrix);
71
+    }
72
+
73
+    /**
74
+     * The inverse of the transform
75
+     */
76
+    get inverse(): InvertibleTransform
77
+    {
78
+        const inverseMatrix = Speedy.Matrix(this._matrix.inverse());
79
+        return new InvertibleTransform(inverseMatrix);
80
+    }
64 81
 }
65 82
 
66 83
 /**
67
- * A 3D transformation described by position and orientation
84
+ * A 3D transformation described by translation, rotation and scale
68 85
  */
69
-export class RigidTransform extends InvertibleTransform
86
+export class StandardTransform extends InvertibleTransform
70 87
 {
71
-    // TODO: position and orientation attributes
88
+    // TODO: position, rotation and scale attributes
72 89
 
73 90
     /**
74 91
      * Constructor
75 92
      * @param matrix a 4x4 matrix
76 93
      */
77
-    private constructor(matrix: SpeedyMatrix)
94
+    constructor(matrix: SpeedyMatrix)
78 95
     {
96
+        // WARNING: we do not check if the matrix actually encodes a standard transform!
79 97
         super(matrix);
80 98
     }
81 99
 
82 100
     /**
83
-     * Create a new rigid transform using a pre-computed transformation matrix
84
-     * @param matrix a 4x4 matrix encoding a rigid transform
85
-     * @returns a new rigid transform
86
-     * @internal
101
+     * The inverse of the transform
102
+     */
103
+    get inverse(): StandardTransform
104
+    {
105
+        /*
106
+
107
+        The inverse of a 4x4 standard transform T * R * S...
108
+
109
+        [  RS  t  ]    is    [  ZR' -ZR't ]
110
+        [  0'  1  ]          [  0'    1   ]
111
+
112
+        where S is 3x3, R is 3x3, t is 3x1, 0' is 1x3 and Z is the inverse of S
113
+
114
+        */
115
+
116
+        return super.inverse as StandardTransform;
117
+    }
118
+}
119
+
120
+/**
121
+ * A 3D transformation described by position and orientation
122
+ */
123
+export class RigidTransform extends StandardTransform
124
+{
125
+    // TODO: position and rotation attributes (need to decompose the matrix)
126
+
127
+    /**
128
+     * Constructor
129
+     * @param matrix a 4x4 matrix
87 130
      */
88
-    static fromMatrix(matrix: SpeedyMatrix): RigidTransform
131
+    constructor(matrix: SpeedyMatrix)
89 132
     {
90 133
         // WARNING: we do not check if the matrix actually encodes a rigid transform!
91
-        return new RigidTransform(matrix);
134
+        super(matrix);
92 135
     }
93 136
 
94 137
     /**
95
-     * The inverse of the rigid transform
138
+     * The inverse of the transform
96 139
      */
97 140
     get inverse(): RigidTransform
98 141
     {

+ 3
- 10
src/geometry/viewer-pose.ts View File

@@ -46,17 +46,10 @@ export class ViewerPose extends Pose
46 46
     {
47 47
         // compute the view matrix and its inverse in AR screen space
48 48
         const viewMatrix = ViewerPose._computeViewMatrix(camera);
49
-        const viewMatrixInverse = RigidTransform.fromMatrix(viewMatrix).inverse.matrix;
49
+        const inverseTransform = new RigidTransform(viewMatrix);
50 50
 
51
-        // perform a change of coordinates
52
-        const M = camera.denormalizer();
53
-        const W = camera.denormalizerInverse();
54
-        const myViewMatrix = Speedy.Matrix(viewMatrix.times(M));
55
-        const myViewMatrixInverse = Speedy.Matrix(W.times(viewMatrixInverse));
56
-
57
-        // create the viewer pose
58
-        super(RigidTransform.fromMatrix(myViewMatrixInverse));
59
-        this._viewMatrix = myViewMatrix;
51
+        super(inverseTransform.inverse);
52
+        this._viewMatrix = viewMatrix;
60 53
     }
61 54
 
62 55
     /**

+ 2
- 2
src/geometry/viewer.ts View File

@@ -26,7 +26,7 @@ import { CameraModel } from './camera-model';
26 26
 import { Pose } from './pose';
27 27
 import { ViewerPose } from './viewer-pose';
28 28
 import { View, PerspectiveView } from './view';
29
-import { RigidTransform } from './transform';
29
+import { StandardTransform } from './transform';
30 30
 import { IllegalOperationError } from '../utils/errors';
31 31
 
32 32
 
@@ -97,7 +97,7 @@ export class Viewer
97 97
         const viewMatrix = this._pose.viewMatrix;
98 98
         const modelViewMatrix = Speedy.Matrix(viewMatrix.times(modelMatrix));
99 99
 
100
-        const transform = RigidTransform.fromMatrix(modelViewMatrix);
100
+        const transform = new StandardTransform(modelViewMatrix);
101 101
         return new Pose(transform);
102 102
     }
103 103
 }

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

@@ -46,7 +46,7 @@ import { ReferenceImage } from '../reference-image';
46 46
 import { CameraModel } from '../../../geometry/camera-model';
47 47
 import { Viewer } from '../../../geometry/viewer';
48 48
 import { Pose } from '../../../geometry/pose';
49
-import { RigidTransform } from '../../../geometry/transform';
49
+import { RigidTransform, StandardTransform } from '../../../geometry/transform';
50 50
 import { IllegalOperationError, IllegalArgumentError, TrackingError } from '../../../utils/errors';
51 51
 import {
52 52
     TRACK_RECTIFIED_BORDER, TRACK_CLIPPING_BORDER, TRACK_MIN_MATCHES, TRACK_LOST_TOLERANCE,
@@ -70,12 +70,6 @@ const NUMBER_OF_PBOS = 2;
70 70
 /** Frame skipping; meaningful only when using turbo */
71 71
 const TURBO_SKIP = 2;
72 72
 
73
-/** An identity transform computed lazily */
74
-const identityTransform: () => RigidTransform = (() => {
75
-    let transform: Nullable<RigidTransform> = null;
76
-    return () => (transform = transform || RigidTransform.fromMatrix(Speedy.Matrix.Eye(4)));
77
-})();
78
-
79 73
 /** A pair (a,b) of arrays of keypoints such that keypoint a[i] is a match to keypoint b[i] for all i */
80 74
 type QualityMatches = [ SpeedyMatchedKeypoint[], SpeedyKeypoint[] ];
81 75
 
@@ -389,7 +383,11 @@ export class ImageTrackerTrackingState extends ImageTrackerState
389 383
         }).then(polyline => {
390 384
 
391 385
             // we let the target object be at the origin of the world space
392
-            const pose = new Pose(identityTransform());
386
+            // (identity transform). We also perform a change of coordinates,
387
+            // so that we move out from pixel space and into normalized space
388
+            const modelMatrix = this._camera.denormalizer(); // ~ "identity matrix"
389
+            const transform = new StandardTransform(modelMatrix);
390
+            const pose = new Pose(transform);
393 391
 
394 392
             // given the current state of the camera model, we get a viewer
395 393
             // compatible with the pose of the target

Loading…
Cancel
Save