Skip to content

Commit

Permalink
Merge pull request #314 from jotatoledo/fix/remove-webworker
Browse files Browse the repository at this point in the history
Fix/remove webworker
  • Loading branch information
hiukim authored Dec 16, 2022
2 parents f97dcc1 + add4ce4 commit cb9857d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 43 deletions.
5 changes: 5 additions & 0 deletions src/image-target/aframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ AFRAME.registerSystem('mindar-image-system', {
track.stop();
});
this.video.remove();
this.controller.dispose();
},

pause: function(keepVideo=false) {
Expand Down Expand Up @@ -229,6 +230,10 @@ AFRAME.registerComponent('mindar-image', {
arSystem.start();
});
}
},
remove: function () {
const arSystem = this.el.sceneEl.systems['mindar-image-system'];
arSystem.stop();
}
});

Expand Down
7 changes: 7 additions & 0 deletions src/image-target/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ class Controller {
return {dimensions: dimensions, matchingDataList, trackingDataList};
}

dispose() {
this.stopProcessVideo();
this.worker.postMessage({
type: "dispose"
});
}

// warm up gpu - build kernels is slow
dummyRun(input) {
const inputT = this.inputLoader.loadInput(input);
Expand Down
97 changes: 54 additions & 43 deletions src/image-target/controller.worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Matcher} from './matching/matcher.js';
import {Estimator} from './estimation/estimator.js';
import { Matcher } from './matching/matcher.js';
import { Estimator } from './estimation/estimator.js';

let projectionTransform = null;
let matchingDataList = null;
Expand All @@ -8,53 +8,64 @@ let matcher = null;
let estimator = null;

onmessage = (msg) => {
const {data} = msg;

if (data.type === 'setup') {
projectionTransform = data.projectionTransform;
matchingDataList = data.matchingDataList;
debugMode = data.debugMode;
matcher = new Matcher(data.inputWidth, data.inputHeight, debugMode);
estimator = new Estimator(data.projectionTransform);
}
else if (data.type === 'match') {
const interestedTargetIndexes = data.targetIndexes;
const { data } = msg;

switch (data.type) {
case "setup":
projectionTransform = data.projectionTransform;
matchingDataList = data.matchingDataList;
debugMode = data.debugMode;
matcher = new Matcher(data.inputWidth, data.inputHeight, debugMode);
estimator = new Estimator(data.projectionTransform);
break;

let matchedTargetIndex = -1;
let matchedModelViewTransform = null;
let matchedDebugExtra = null;
case "match":
const interestedTargetIndexes = data.targetIndexes;

for (let i = 0; i < interestedTargetIndexes.length; i++) {
const matchingIndex = interestedTargetIndexes[i];
let matchedTargetIndex = -1;
let matchedModelViewTransform = null;
let matchedDebugExtra = null;

const {keyframeIndex, screenCoords, worldCoords, debugExtra} = matcher.matchDetection(matchingDataList[matchingIndex], data.featurePoints);
matchedDebugExtra = debugExtra;
for (let i = 0; i < interestedTargetIndexes.length; i++) {
const matchingIndex = interestedTargetIndexes[i];

if (keyframeIndex !== -1) {
const modelViewTransform = estimator.estimate({screenCoords, worldCoords});
const { keyframeIndex, screenCoords, worldCoords, debugExtra } = matcher.matchDetection(matchingDataList[matchingIndex], data.featurePoints);
matchedDebugExtra = debugExtra;

if (modelViewTransform) {
matchedTargetIndex = matchingIndex;
matchedModelViewTransform = modelViewTransform;
}
break;
if (keyframeIndex !== -1) {
const modelViewTransform = estimator.estimate({ screenCoords, worldCoords });

if (modelViewTransform) {
matchedTargetIndex = matchingIndex;
matchedModelViewTransform = modelViewTransform;
}
break;
}
}
}

postMessage({
type: 'matchDone',
targetIndex: matchedTargetIndex,
modelViewTransform: matchedModelViewTransform,
debugExtra: matchedDebugExtra
});
}
else if (data.type === 'trackUpdate') {
const {modelViewTransform, worldCoords, screenCoords} = data;
const finalModelViewTransform = estimator.refineEstimate({initialModelViewTransform: modelViewTransform, worldCoords, screenCoords});
postMessage({
type: 'trackUpdateDone',
modelViewTransform: finalModelViewTransform,
});

postMessage({
type: 'matchDone',
targetIndex: matchedTargetIndex,
modelViewTransform: matchedModelViewTransform,
debugExtra: matchedDebugExtra
});
break;

case 'trackUpdate':
const { modelViewTransform, worldCoords, screenCoords } = data;
const finalModelViewTransform = estimator.refineEstimate({ initialModelViewTransform: modelViewTransform, worldCoords, screenCoords });
postMessage({
type: 'trackUpdateDone',
modelViewTransform: finalModelViewTransform,
});
break;

case "dispose":
close();
break;

default:
throw new Error(`Invalid message type '${data.type}'`);
}
};

0 comments on commit cb9857d

Please sign in to comment.