Skip to content

Commit 3ee1c37

Browse files
committed
fix for jsartoolkitNFT example
- updated to the newest version
1 parent 267bb5b commit 3ee1c37

4 files changed

+194
-42
lines changed

dist/ARToolkitNFT.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ARToolkitNFT_simd.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/artoolkitNFT_ES6.worker.js

+124-40
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,152 @@
1-
importScripts('../dist/ARToolkitNFT.js')
1+
var browser = (function () {
2+
var test = function (regexp) {
3+
return regexp.test(navigator.userAgent);
4+
};
5+
switch (true) {
6+
case test(/edg/i):
7+
return "Microsoft Edge";
8+
case test(/trident/i):
9+
return "Microsoft Internet Explorer";
10+
case test(/firefox|fxios/i):
11+
return "Mozilla Firefox";
12+
case test(/opr\//i):
13+
return "Opera";
14+
case test(/ucbrowser/i):
15+
return "UC Browser";
16+
case test(/samsungbrowser/i):
17+
return "Samsung Browser";
18+
case test(/chrome|chromium|crios/i):
19+
return "Google Chrome";
20+
case test(/safari/i):
21+
return "Apple Safari";
22+
default:
23+
return "Other";
24+
}
25+
})();
26+
27+
if (browser == "Apple Safari") {
28+
importScripts("../dist/ARToolkitNFT.js");
29+
} else {
30+
importScripts("../dist/ARToolkitNFT_simd.js");
31+
}
32+
// Import OneEuroFilter class into the worker.
33+
importScripts("./one-euro-filter.js");
234

335
self.onmessage = function (e) {
4-
var msg = e.data
36+
var msg = e.data;
537
switch (msg.type) {
6-
case 'load': {
7-
load(msg)
8-
return
38+
case "load": {
39+
load(msg);
40+
return;
941
}
10-
case 'process': {
11-
next = msg.imagedata
12-
process()
42+
case "process": {
43+
next = msg.imagedata;
44+
process();
1345
}
1446
}
15-
}
47+
};
1648

17-
var next = null
18-
var ar = null
19-
var markerResult = null
49+
var next = null;
50+
var ar = null;
51+
var markerResult = null;
52+
var marker;
2053

21-
function load (msg) {
22-
console.debug('Loading marker at: ', msg.marker)
54+
const WARM_UP_TOLERANCE = 5;
55+
let tickCount = 0;
56+
57+
// initialize the OneEuroFilter
58+
var oef = true;
59+
let filterMinCF = 0.0001;
60+
let filterBeta = 0.01;
61+
const filter = new OneEuroFilter({
62+
minCutOff: filterMinCF,
63+
beta: filterBeta,
64+
});
65+
66+
function oefFilter(matrixGL_RH) {
67+
tickCount += 1;
68+
var mat;
69+
if (tickCount > WARM_UP_TOLERANCE) {
70+
mat = filter.filter(Date.now(), matrixGL_RH);
71+
} else {
72+
mat = matrixGL_RH;
73+
}
74+
return mat;
75+
}
76+
77+
function load(msg) {
78+
console.debug("Loading marker at: ", msg.marker);
2379

2480
var onLoad = function (arController) {
25-
ar = arController
26-
var cameraMatrix = ar.getCameraMatrix()
27-
28-
ar.addEventListener('getNFTMarker', function (ev) {
29-
markerResult = { type: 'found', matrixGL_RH: JSON.stringify(ev.data.matrixGL_RH)}
30-
})
31-
32-
ar.loadNFTMarker(msg.marker).then(function (nft) {
33-
ar.trackNFTMarkerId(nft.id)
34-
console.log('loadNFTMarker -> ', nft.id)
35-
console.log('nftMarker struct: ', nft)
36-
postMessage({ type: 'endLoading', end: true })
81+
ar = arController;
82+
var cameraMatrix = ar.getCameraMatrix();
83+
84+
ar.addEventListener("getNFTMarker", function (ev) {
85+
var mat;
86+
if (oef == true) {
87+
mat = oefFilter(ev.data.matrixGL_RH);
88+
} else {
89+
mat = ev.data.matrixGL_RH;
90+
}
91+
markerResult = {
92+
type: "found",
93+
matrixGL_RH: JSON.stringify(mat),
94+
};
95+
});
96+
97+
ar.addEventListener("lostNFTMarker", function (ev) {
98+
filter.reset();
99+
});
100+
101+
ar.loadNFTMarker(msg.marker, function (id) {
102+
ar.trackNFTMarkerId(id);
103+
let marker = ar.getNFTData(0);
104+
console.log("nftMarker data: ", marker);
105+
postMessage({
106+
type: "markerInfos",
107+
marker: marker,
108+
});
109+
console.log("loadNFTMarker -> ", id);
110+
postMessage({
111+
type: "endLoading",
112+
end: true,
113+
});
37114
}).catch(function (err) {
38-
console.log('Error in loading marker on Worker', err)
39-
})
115+
console.log("Error in loading marker on Worker", err);
116+
});
40117

41-
postMessage({ type: 'loaded', proj: JSON.stringify(cameraMatrix) })
42-
}
118+
postMessage({
119+
type: "loaded",
120+
proj: JSON.stringify(cameraMatrix),
121+
});
122+
};
43123

44124
var onError = function (error) {
45-
console.error(error)
46-
}
125+
console.error(error);
126+
};
47127

48-
console.debug('Loading camera at:', msg.camera_para)
128+
console.debug("Loading camera at:", msg.camera_para);
49129

50130
// we cannot pass the entire ARControllerNFT, so we re-create one inside the Worker, starting from camera_param
51-
ARToolkitNFT.ARControllerNFT.initWithDimensions(msg.pw, msg.ph, msg.camera_para).then(onLoad).catch(onError)
131+
ARControllerNFT.initWithDimensions(msg.pw, msg.ph, msg.camera_para)
132+
.then(onLoad)
133+
.catch(onError);
52134
}
53135

54-
function process () {
55-
markerResult = null
136+
function process() {
137+
markerResult = null;
56138

57139
if (ar && ar.process) {
58-
ar.process(next)
140+
ar.process(next);
59141
}
60142

61143
if (markerResult) {
62-
postMessage(markerResult)
144+
postMessage(markerResult);
63145
} else {
64-
postMessage({ type: 'not found' })
146+
postMessage({
147+
type: "not found",
148+
});
65149
}
66150

67-
next = null
151+
next = null;
68152
}

js/one-euro-filter.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Ref: https://jaantollander.com/post/noise-filtering-using-one-euro-filter/#mjx-eqn%3A1
2+
3+
const smoothingFactor = (te, cutoff) => {
4+
const r = 2 * Math.PI * cutoff * te;
5+
return r / (r + 1);
6+
};
7+
8+
const exponentialSmoothing = (a, x, xPrev) => {
9+
return a * x + (1 - a) * xPrev;
10+
};
11+
12+
class OneEuroFilter {
13+
constructor({ minCutOff, beta }) {
14+
this.minCutOff = minCutOff;
15+
this.beta = beta;
16+
this.dCutOff = 0.001; // period in milliseconds, so default to 0.001 = 1Hz
17+
18+
this.xPrev = null;
19+
this.dxPrev = null;
20+
this.tPrev = null;
21+
this.initialized = false;
22+
}
23+
24+
reset() {
25+
this.initialized = false;
26+
}
27+
28+
filter(t, x) {
29+
if (!this.initialized) {
30+
this.initialized = true;
31+
this.xPrev = x;
32+
this.dxPrev = x.map(() => 0);
33+
this.tPrev = t;
34+
return x;
35+
}
36+
37+
const { xPrev, tPrev, dxPrev } = this;
38+
39+
//console.log("filter", x, xPrev, x.map((xx, i) => x[i] - xPrev[i]));
40+
41+
const te = t - tPrev;
42+
43+
const ad = smoothingFactor(te, this.dCutOff);
44+
45+
const dx = [];
46+
const dxHat = [];
47+
const xHat = [];
48+
for (let i = 0; i < x.length; i++) {
49+
// The filtered derivative of the signal.
50+
dx[i] = (x[i] - xPrev[i]) / te;
51+
dxHat[i] = exponentialSmoothing(ad, dx[i], dxPrev[i]);
52+
53+
// The filtered signal
54+
const cutOff = this.minCutOff + this.beta * Math.abs(dxHat[i]);
55+
const a = smoothingFactor(te, cutOff);
56+
xHat[i] = exponentialSmoothing(a, x[i], xPrev[i]);
57+
}
58+
59+
// update prev
60+
this.xPrev = xHat;
61+
this.dxPrev = dxHat;
62+
this.tPrev = t;
63+
64+
return xHat;
65+
}
66+
}

0 commit comments

Comments
 (0)