@srsergio/taptapp-ar 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (207) hide show
  1. package/README.md +62 -0
  2. package/dist/compiler/aframe.d.ts +1 -0
  3. package/dist/compiler/aframe.js +275 -0
  4. package/dist/compiler/compiler-base.d.ts +12 -0
  5. package/dist/compiler/compiler-base.js +165 -0
  6. package/dist/compiler/compiler.d.ts +9 -0
  7. package/dist/compiler/compiler.js +24 -0
  8. package/dist/compiler/compiler.worker.d.ts +1 -0
  9. package/dist/compiler/compiler.worker.js +28 -0
  10. package/dist/compiler/controller.d.ts +101 -0
  11. package/dist/compiler/controller.js +400 -0
  12. package/dist/compiler/controller.worker.d.ts +1 -0
  13. package/dist/compiler/controller.worker.js +61 -0
  14. package/dist/compiler/detector/crop-detector.d.ts +65 -0
  15. package/dist/compiler/detector/crop-detector.js +59 -0
  16. package/dist/compiler/detector/detector.d.ts +98 -0
  17. package/dist/compiler/detector/detector.js +1049 -0
  18. package/dist/compiler/detector/freak.d.ts +1 -0
  19. package/dist/compiler/detector/freak.js +89 -0
  20. package/dist/compiler/detector/kernels/cpu/binomialFilter.d.ts +6 -0
  21. package/dist/compiler/detector/kernels/cpu/binomialFilter.js +51 -0
  22. package/dist/compiler/detector/kernels/cpu/buildExtremas.d.ts +6 -0
  23. package/dist/compiler/detector/kernels/cpu/buildExtremas.js +89 -0
  24. package/dist/compiler/detector/kernels/cpu/computeExtremaAngles.d.ts +7 -0
  25. package/dist/compiler/detector/kernels/cpu/computeExtremaAngles.js +79 -0
  26. package/dist/compiler/detector/kernels/cpu/computeExtremaFreak.d.ts +6 -0
  27. package/dist/compiler/detector/kernels/cpu/computeExtremaFreak.js +68 -0
  28. package/dist/compiler/detector/kernels/cpu/computeFreakDescriptors.d.ts +6 -0
  29. package/dist/compiler/detector/kernels/cpu/computeFreakDescriptors.js +57 -0
  30. package/dist/compiler/detector/kernels/cpu/computeLocalization.d.ts +6 -0
  31. package/dist/compiler/detector/kernels/cpu/computeLocalization.js +54 -0
  32. package/dist/compiler/detector/kernels/cpu/computeOrientationHistograms.d.ts +6 -0
  33. package/dist/compiler/detector/kernels/cpu/computeOrientationHistograms.js +118 -0
  34. package/dist/compiler/detector/kernels/cpu/downsampleBilinear.d.ts +6 -0
  35. package/dist/compiler/detector/kernels/cpu/downsampleBilinear.js +29 -0
  36. package/dist/compiler/detector/kernels/cpu/extremaReduction.d.ts +6 -0
  37. package/dist/compiler/detector/kernels/cpu/extremaReduction.js +50 -0
  38. package/dist/compiler/detector/kernels/cpu/fakeShader.d.ts +20 -0
  39. package/dist/compiler/detector/kernels/cpu/fakeShader.js +80 -0
  40. package/dist/compiler/detector/kernels/cpu/index.d.ts +1 -0
  41. package/dist/compiler/detector/kernels/cpu/index.js +25 -0
  42. package/dist/compiler/detector/kernels/cpu/prune.d.ts +1 -0
  43. package/dist/compiler/detector/kernels/cpu/prune.js +103 -0
  44. package/dist/compiler/detector/kernels/cpu/smoothHistograms.d.ts +6 -0
  45. package/dist/compiler/detector/kernels/cpu/smoothHistograms.js +47 -0
  46. package/dist/compiler/detector/kernels/cpu/upsampleBilinear.d.ts +6 -0
  47. package/dist/compiler/detector/kernels/cpu/upsampleBilinear.js +43 -0
  48. package/dist/compiler/detector/kernels/index.d.ts +1 -0
  49. package/dist/compiler/detector/kernels/index.js +2 -0
  50. package/dist/compiler/detector/kernels/webgl/binomialFilter.d.ts +6 -0
  51. package/dist/compiler/detector/kernels/webgl/binomialFilter.js +67 -0
  52. package/dist/compiler/detector/kernels/webgl/buildExtremas.d.ts +6 -0
  53. package/dist/compiler/detector/kernels/webgl/buildExtremas.js +101 -0
  54. package/dist/compiler/detector/kernels/webgl/computeExtremaAngles.d.ts +6 -0
  55. package/dist/compiler/detector/kernels/webgl/computeExtremaAngles.js +78 -0
  56. package/dist/compiler/detector/kernels/webgl/computeExtremaFreak.d.ts +6 -0
  57. package/dist/compiler/detector/kernels/webgl/computeExtremaFreak.js +86 -0
  58. package/dist/compiler/detector/kernels/webgl/computeFreakDescriptors.d.ts +6 -0
  59. package/dist/compiler/detector/kernels/webgl/computeFreakDescriptors.js +52 -0
  60. package/dist/compiler/detector/kernels/webgl/computeLocalization.d.ts +6 -0
  61. package/dist/compiler/detector/kernels/webgl/computeLocalization.js +58 -0
  62. package/dist/compiler/detector/kernels/webgl/computeOrientationHistograms.d.ts +6 -0
  63. package/dist/compiler/detector/kernels/webgl/computeOrientationHistograms.js +116 -0
  64. package/dist/compiler/detector/kernels/webgl/downsampleBilinear.d.ts +6 -0
  65. package/dist/compiler/detector/kernels/webgl/downsampleBilinear.js +46 -0
  66. package/dist/compiler/detector/kernels/webgl/extremaReduction.d.ts +6 -0
  67. package/dist/compiler/detector/kernels/webgl/extremaReduction.js +48 -0
  68. package/dist/compiler/detector/kernels/webgl/index.d.ts +1 -0
  69. package/dist/compiler/detector/kernels/webgl/index.js +25 -0
  70. package/dist/compiler/detector/kernels/webgl/smoothHistograms.d.ts +6 -0
  71. package/dist/compiler/detector/kernels/webgl/smoothHistograms.js +49 -0
  72. package/dist/compiler/detector/kernels/webgl/upsampleBilinear.d.ts +6 -0
  73. package/dist/compiler/detector/kernels/webgl/upsampleBilinear.js +56 -0
  74. package/dist/compiler/estimation/esimate-experiment.d.ts +5 -0
  75. package/dist/compiler/estimation/esimate-experiment.js +267 -0
  76. package/dist/compiler/estimation/estimate.d.ts +5 -0
  77. package/dist/compiler/estimation/estimate.js +51 -0
  78. package/dist/compiler/estimation/estimator.d.ts +13 -0
  79. package/dist/compiler/estimation/estimator.js +30 -0
  80. package/dist/compiler/estimation/refine-estimate-experiment.d.ts +6 -0
  81. package/dist/compiler/estimation/refine-estimate-experiment.js +429 -0
  82. package/dist/compiler/estimation/refine-estimate.d.ts +6 -0
  83. package/dist/compiler/estimation/refine-estimate.js +299 -0
  84. package/dist/compiler/estimation/utils.d.ts +10 -0
  85. package/dist/compiler/estimation/utils.js +80 -0
  86. package/dist/compiler/image-list.d.ts +13 -0
  87. package/dist/compiler/image-list.js +52 -0
  88. package/dist/compiler/index.d.ts +3 -0
  89. package/dist/compiler/index.js +10 -0
  90. package/dist/compiler/input-loader.d.ts +23 -0
  91. package/dist/compiler/input-loader.js +88 -0
  92. package/dist/compiler/matching/hamming-distance.d.ts +1 -0
  93. package/dist/compiler/matching/hamming-distance.js +20 -0
  94. package/dist/compiler/matching/hierarchical-clustering.d.ts +7 -0
  95. package/dist/compiler/matching/hierarchical-clustering.js +109 -0
  96. package/dist/compiler/matching/hough.d.ts +1 -0
  97. package/dist/compiler/matching/hough.js +169 -0
  98. package/dist/compiler/matching/matcher.d.ts +28 -0
  99. package/dist/compiler/matching/matcher.js +48 -0
  100. package/dist/compiler/matching/matching.d.ts +41 -0
  101. package/dist/compiler/matching/matching.js +197 -0
  102. package/dist/compiler/matching/ransacHomography.d.ts +1 -0
  103. package/dist/compiler/matching/ransacHomography.js +136 -0
  104. package/dist/compiler/offline-compiler.d.ts +10 -0
  105. package/dist/compiler/offline-compiler.js +450 -0
  106. package/dist/compiler/tensorflow-setup.d.ts +7 -0
  107. package/dist/compiler/tensorflow-setup.js +73 -0
  108. package/dist/compiler/three.d.ts +66 -0
  109. package/dist/compiler/three.js +310 -0
  110. package/dist/compiler/tracker/extract-utils.d.ts +1 -0
  111. package/dist/compiler/tracker/extract-utils.js +29 -0
  112. package/dist/compiler/tracker/extract.d.ts +4 -0
  113. package/dist/compiler/tracker/extract.js +349 -0
  114. package/dist/compiler/tracker/tracker.d.ts +38 -0
  115. package/dist/compiler/tracker/tracker.js +327 -0
  116. package/dist/compiler/utils/cumsum.d.ts +5 -0
  117. package/dist/compiler/utils/cumsum.js +39 -0
  118. package/dist/compiler/utils/geometry.d.ts +8 -0
  119. package/dist/compiler/utils/geometry.js +101 -0
  120. package/dist/compiler/utils/homography.d.ts +1 -0
  121. package/dist/compiler/utils/homography.js +138 -0
  122. package/dist/compiler/utils/images.d.ts +24 -0
  123. package/dist/compiler/utils/images.js +99 -0
  124. package/dist/compiler/utils/randomizer.d.ts +5 -0
  125. package/dist/compiler/utils/randomizer.js +25 -0
  126. package/dist/index.d.ts +6 -0
  127. package/dist/index.js +7 -0
  128. package/dist/react/AREditor.d.ts +5 -0
  129. package/dist/react/AREditor.js +159 -0
  130. package/dist/react/ProgressDialog.d.ts +13 -0
  131. package/dist/react/ProgressDialog.js +57 -0
  132. package/dist/react/types.d.ts +22 -0
  133. package/dist/react/types.js +14 -0
  134. package/package.json +53 -0
  135. package/src/astro/ARScene.astro +59 -0
  136. package/src/astro/ARVideoTrigger.astro +73 -0
  137. package/src/astro/overlays/ErrorOverlay.astro +40 -0
  138. package/src/astro/overlays/LoadingOverlay.astro +28 -0
  139. package/src/astro/overlays/ScanningOverlay.astro +119 -0
  140. package/src/astro/scripts/ARScripts.astro +118 -0
  141. package/src/astro/styles/ARStyles.astro +147 -0
  142. package/src/compiler/aframe.js +343 -0
  143. package/src/compiler/compiler-base.js +195 -0
  144. package/src/compiler/compiler.js +25 -0
  145. package/src/compiler/compiler.worker.js +30 -0
  146. package/src/compiler/controller.js +473 -0
  147. package/src/compiler/controller.worker.js +77 -0
  148. package/src/compiler/detector/crop-detector.js +68 -0
  149. package/src/compiler/detector/detector.js +1130 -0
  150. package/src/compiler/detector/freak.js +91 -0
  151. package/src/compiler/detector/kernels/cpu/binomialFilter.js +59 -0
  152. package/src/compiler/detector/kernels/cpu/buildExtremas.js +108 -0
  153. package/src/compiler/detector/kernels/cpu/computeExtremaAngles.js +91 -0
  154. package/src/compiler/detector/kernels/cpu/computeExtremaFreak.js +92 -0
  155. package/src/compiler/detector/kernels/cpu/computeFreakDescriptors.js +68 -0
  156. package/src/compiler/detector/kernels/cpu/computeLocalization.js +71 -0
  157. package/src/compiler/detector/kernels/cpu/computeOrientationHistograms.js +141 -0
  158. package/src/compiler/detector/kernels/cpu/downsampleBilinear.js +33 -0
  159. package/src/compiler/detector/kernels/cpu/extremaReduction.js +53 -0
  160. package/src/compiler/detector/kernels/cpu/fakeShader.js +88 -0
  161. package/src/compiler/detector/kernels/cpu/index.js +26 -0
  162. package/src/compiler/detector/kernels/cpu/prune.js +114 -0
  163. package/src/compiler/detector/kernels/cpu/smoothHistograms.js +57 -0
  164. package/src/compiler/detector/kernels/cpu/upsampleBilinear.js +51 -0
  165. package/src/compiler/detector/kernels/index.js +2 -0
  166. package/src/compiler/detector/kernels/webgl/binomialFilter.js +72 -0
  167. package/src/compiler/detector/kernels/webgl/buildExtremas.js +109 -0
  168. package/src/compiler/detector/kernels/webgl/computeExtremaAngles.js +82 -0
  169. package/src/compiler/detector/kernels/webgl/computeExtremaFreak.js +105 -0
  170. package/src/compiler/detector/kernels/webgl/computeFreakDescriptors.js +56 -0
  171. package/src/compiler/detector/kernels/webgl/computeLocalization.js +70 -0
  172. package/src/compiler/detector/kernels/webgl/computeOrientationHistograms.js +129 -0
  173. package/src/compiler/detector/kernels/webgl/downsampleBilinear.js +50 -0
  174. package/src/compiler/detector/kernels/webgl/extremaReduction.js +50 -0
  175. package/src/compiler/detector/kernels/webgl/index.js +26 -0
  176. package/src/compiler/detector/kernels/webgl/smoothHistograms.js +53 -0
  177. package/src/compiler/detector/kernels/webgl/upsampleBilinear.js +62 -0
  178. package/src/compiler/estimation/esimate-experiment.js +316 -0
  179. package/src/compiler/estimation/estimate.js +67 -0
  180. package/src/compiler/estimation/estimator.js +34 -0
  181. package/src/compiler/estimation/refine-estimate-experiment.js +512 -0
  182. package/src/compiler/estimation/refine-estimate.js +365 -0
  183. package/src/compiler/estimation/utils.js +97 -0
  184. package/src/compiler/image-list.js +62 -0
  185. package/src/compiler/index.js +13 -0
  186. package/src/compiler/input-loader.js +107 -0
  187. package/src/compiler/matching/hamming-distance.js +23 -0
  188. package/src/compiler/matching/hierarchical-clustering.js +131 -0
  189. package/src/compiler/matching/hough.js +206 -0
  190. package/src/compiler/matching/matcher.js +59 -0
  191. package/src/compiler/matching/matching.js +237 -0
  192. package/src/compiler/matching/ransacHomography.js +192 -0
  193. package/src/compiler/offline-compiler.js +553 -0
  194. package/src/compiler/tensorflow-setup.js +88 -0
  195. package/src/compiler/three.js +368 -0
  196. package/src/compiler/tracker/extract-utils.js +34 -0
  197. package/src/compiler/tracker/extract.js +419 -0
  198. package/src/compiler/tracker/tracker.js +397 -0
  199. package/src/compiler/utils/cumsum.js +40 -0
  200. package/src/compiler/utils/geometry.js +114 -0
  201. package/src/compiler/utils/homography.js +150 -0
  202. package/src/compiler/utils/images.js +111 -0
  203. package/src/compiler/utils/randomizer.js +29 -0
  204. package/src/index.ts +8 -0
  205. package/src/react/AREditor.tsx +394 -0
  206. package/src/react/ProgressDialog.tsx +185 -0
  207. package/src/react/types.ts +35 -0
@@ -0,0 +1,192 @@
1
+ import { Matrix, inverse } from "ml-matrix";
2
+ import { createRandomizer } from "../utils/randomizer.js";
3
+ import {
4
+ quadrilateralConvex,
5
+ matrixInverse33,
6
+ smallestTriangleArea,
7
+ multiplyPointHomographyInhomogenous,
8
+ checkThreePointsConsistent,
9
+ checkFourPointsConsistent,
10
+ determinant,
11
+ } from "../utils/geometry.js";
12
+ import { solveHomography } from "../utils/homography.js";
13
+
14
+ const CAUCHY_SCALE = 0.01;
15
+ const CHUNK_SIZE = 10;
16
+ const NUM_HYPOTHESES = 20;
17
+ const NUM_HYPOTHESES_QUICK = 10;
18
+
19
+ // Using RANSAC to estimate homography
20
+ const computeHomography = (options) => {
21
+ const { srcPoints, dstPoints, keyframe, quickMode } = options;
22
+
23
+ // testPoints is four corners of keyframe
24
+ const testPoints = [
25
+ [0, 0],
26
+ [keyframe.width, 0],
27
+ [keyframe.width, keyframe.height],
28
+ [0, keyframe.height],
29
+ ];
30
+
31
+ const sampleSize = 4; // use four points to compute homography
32
+ if (srcPoints.length < sampleSize) return null;
33
+
34
+ const scale = CAUCHY_SCALE;
35
+ const oneOverScale2 = 1.0 / (scale * scale);
36
+ const chuckSize = Math.min(CHUNK_SIZE, srcPoints.length);
37
+
38
+ const randomizer = createRandomizer();
39
+
40
+ const perm = [];
41
+ for (let i = 0; i < srcPoints.length; i++) {
42
+ perm[i] = i;
43
+ }
44
+
45
+ randomizer.arrayShuffle({ arr: perm, sampleSize: perm.length });
46
+
47
+ const numHypothesis = quickMode ? NUM_HYPOTHESES_QUICK : NUM_HYPOTHESES;
48
+ const maxTrials = numHypothesis * 2;
49
+
50
+ // build numerous hypotheses by randoming draw four points
51
+ // TODO: optimize: if number of points is less than certain number, can brute force all combinations
52
+ let trial = 0;
53
+ const Hs = [];
54
+ while (trial < maxTrials && Hs.length < numHypothesis) {
55
+ trial += 1;
56
+
57
+ randomizer.arrayShuffle({ arr: perm, sampleSize: sampleSize });
58
+
59
+ // their relative positions match each other
60
+ if (
61
+ !checkFourPointsConsistent(
62
+ srcPoints[perm[0]],
63
+ srcPoints[perm[1]],
64
+ srcPoints[perm[2]],
65
+ srcPoints[perm[3]],
66
+ dstPoints[perm[0]],
67
+ dstPoints[perm[1]],
68
+ dstPoints[perm[2]],
69
+ dstPoints[perm[3]],
70
+ )
71
+ ) {
72
+ continue;
73
+ }
74
+
75
+ const H = solveHomography(
76
+ [srcPoints[perm[0]], srcPoints[perm[1]], srcPoints[perm[2]], srcPoints[perm[3]]],
77
+ [dstPoints[perm[0]], dstPoints[perm[1]], dstPoints[perm[2]], dstPoints[perm[3]]],
78
+ );
79
+ if (H === null) continue;
80
+
81
+ if (!_checkHomographyPointsGeometricallyConsistent({ H, testPoints })) {
82
+ continue;
83
+ }
84
+
85
+ Hs.push(H);
86
+ }
87
+
88
+ if (Hs.length === 0) return null;
89
+
90
+ // pick the best hypothesis
91
+ const hypotheses = [];
92
+ for (let i = 0; i < Hs.length; i++) {
93
+ hypotheses.push({
94
+ H: Hs[i],
95
+ cost: 0,
96
+ });
97
+ }
98
+
99
+ let curChuckSize = chuckSize;
100
+ for (let i = 0; i < srcPoints.length && hypotheses.length > 2; i += curChuckSize) {
101
+ curChuckSize = Math.min(chuckSize, srcPoints.length - i);
102
+ let chuckEnd = i + curChuckSize;
103
+
104
+ for (let j = 0; j < hypotheses.length; j++) {
105
+ for (let k = i; k < chuckEnd; k++) {
106
+ const cost = _cauchyProjectiveReprojectionCost({
107
+ H: hypotheses[j].H,
108
+ srcPoint: srcPoints[k],
109
+ dstPoint: dstPoints[k],
110
+ oneOverScale2,
111
+ });
112
+ hypotheses[j].cost += cost;
113
+ }
114
+ }
115
+
116
+ hypotheses.sort((h1, h2) => {
117
+ return h1.cost - h2.cost;
118
+ });
119
+ hypotheses.splice(-Math.floor((hypotheses.length + 1) / 2)); // keep the best half
120
+ }
121
+
122
+ let finalH = null;
123
+ for (let i = 0; i < hypotheses.length; i++) {
124
+ const H = _normalizeHomography({ inH: hypotheses[i].H });
125
+ if (_checkHeuristics({ H: H, testPoints, keyframe })) {
126
+ finalH = H;
127
+ break;
128
+ }
129
+ }
130
+ return finalH;
131
+ };
132
+
133
+ const _checkHeuristics = ({ H, testPoints, keyframe }) => {
134
+ const HInv = matrixInverse33(H, 0.00001);
135
+ if (HInv === null) return false;
136
+
137
+ const mp = [];
138
+ for (let i = 0; i < testPoints.length; i++) {
139
+ // 4 test points, corner of keyframe
140
+ mp.push(multiplyPointHomographyInhomogenous(testPoints[i], HInv));
141
+ }
142
+ const smallArea = smallestTriangleArea(mp[0], mp[1], mp[2], mp[3]);
143
+
144
+ if (smallArea < keyframe.width * keyframe.height * 0.0001) return false;
145
+
146
+ if (!quadrilateralConvex(mp[0], mp[1], mp[2], mp[3])) return false;
147
+
148
+ return true;
149
+ };
150
+
151
+ const _normalizeHomography = ({ inH }) => {
152
+ const oneOver = 1.0 / inH[8];
153
+
154
+ const H = [];
155
+ for (let i = 0; i < 8; i++) {
156
+ H[i] = inH[i] * oneOver;
157
+ }
158
+ H[8] = 1.0;
159
+ return H;
160
+ };
161
+
162
+ const _cauchyProjectiveReprojectionCost = ({ H, srcPoint, dstPoint, oneOverScale2 }) => {
163
+ const x = multiplyPointHomographyInhomogenous(srcPoint, H);
164
+ const f = [x[0] - dstPoint[0], x[1] - dstPoint[1]];
165
+ return Math.log(1 + (f[0] * f[0] + f[1] * f[1]) * oneOverScale2);
166
+ };
167
+
168
+ const _checkHomographyPointsGeometricallyConsistent = ({ H, testPoints }) => {
169
+ const mappedPoints = [];
170
+ for (let i = 0; i < testPoints.length; i++) {
171
+ mappedPoints[i] = multiplyPointHomographyInhomogenous(testPoints[i], H);
172
+ }
173
+ for (let i = 0; i < testPoints.length; i++) {
174
+ const i1 = i;
175
+ const i2 = (i + 1) % testPoints.length;
176
+ const i3 = (i + 2) % testPoints.length;
177
+ if (
178
+ !checkThreePointsConsistent(
179
+ testPoints[i1],
180
+ testPoints[i2],
181
+ testPoints[i3],
182
+ mappedPoints[i1],
183
+ mappedPoints[i2],
184
+ mappedPoints[i3],
185
+ )
186
+ )
187
+ return false;
188
+ }
189
+ return true;
190
+ };
191
+
192
+ export { computeHomography };