-
Notifications
You must be signed in to change notification settings - Fork 2
/
TrackerSingleMarkerLoader.ts
127 lines (112 loc) · 3.21 KB
/
TrackerSingleMarkerLoader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import TrackerSM from "../build/trackerSM_ES6";
import { AbstractTrackerSingleMarker } from "./abstractions/AbstractTrackerSingleMarker";
import Utils from "./Utils";
export class TrackerSingleMarkerLoader {
private instance: any;
private cameraCount: number;
public FS: any;
/**
* Deafult constructor.
*/
constructor() {
// reference to WASM module
this.instance;
this.cameraCount = 0;
}
// ---------------------------------------------------------------------------
// initialization
/**
* Init the class injecting the Wasm Module, link the instanced methods.
* @return {object} the this object
*/
public async init() {
this.instance = await TrackerSM();
this._decorate();
return this;
}
// private methods
/**
* Used internally to link the instance in the ARToolKitPlusLoader to the
* ARToolKitPlus internal methods.
* @return {void}
*/
private _decorate(): void {
// add delegate methods
["TrackerSingleMarker"].forEach((method: string) => {
this.converter()[method] = this.instance[method];
});
this.FS = this.instance.FS;
}
/**
* Used internally to convert and inject code.
* @return {this} the this object
*/
private converter(): any {
return this;
}
// ---------------------------------------------------------------------------
// public accessors
//----------------------------------------------------------------------------
/**
* Load the camera, this is an important and required step. Internally fill
* the Camera class (old ARToolKit ARParam struct).
* @param {string} urlOrData: the camera parameter, usually a path to a .cal file
* @return {Promise} the tracker.
*/
public async loadCalib(
urlOrData: Uint8Array | string,
useBCH: boolean,
width: number,
height: number,
patternWidth: number,
maxImagePatterns: number = 8,
pattWidth: number = 6,
pattHeight: number = 6,
pattSamples: number = 6,
maxLoadPatterns: number = 0
): Promise<AbstractTrackerSingleMarker> {
const target = "/load_calib_" + this.cameraCount++;
let data: Uint8Array;
const tracker = new this.instance.TrackerSingleMarker(
useBCH,
width,
height,
patternWidth
);
if (urlOrData instanceof Uint8Array) {
// assume preloaded camera params
data = urlOrData;
} else {
// fetch data via HTTP
try {
data = await Utils.fetchRemoteData(urlOrData);
} catch (error: any) {
throw new Error("Error in loadCalib function: ", error);
}
}
this._storeDataFile(data, target);
// return the internal marker ID
tracker.setup(
target,
maxImagePatterns,
pattWidth,
pattHeight,
pattSamples,
maxLoadPatterns
);
return tracker;
}
// ---------------------------------------------------------------------------
// implementation
/**
* Used internally by LoadCamera method
* @return {void}
*/
private _storeDataFile(data: Uint8Array, target: string) {
// FS is provided by emscripten
// Note: valid data must be in binary format encoded as Uint8Array
this.instance.FS.writeFile(target, data, {
encoding: "binary",
});
}
}