-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathARToolKit.ts
348 lines (318 loc) · 11.2 KB
/
ARToolKit.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import * as Module from "./lib/arjs.artoolkit.min.js"
export class ARToolKit {
public static AR_DEBUG_DISABLE: number = 0;
public static AR_DEBUG_ENABLE: number = 1;
public static AR_DEFAULT_DEBUG_MODE: number = 0;
public static AR_DEFAULT_IMAGE_PROC_MODE: number = 0;
public static AR_DEFAULT_LABELING_MODE: number = 1;
public static AR_DEFAULT_LABELING_THRESH: number = 100;
public static AR_DEFAULT_MARKER_EXTRACTION_MODE: number = 2;
public static AR_DEFAULT_PATTERN_DETECTION_MODE: number = 0;
public static AR_IMAGE_PROC_FIELD_IMAGE: number = 1;
public static AR_IMAGE_PROC_FRAME_IMAGE: number = 0;
public static AR_LABELING_BLACK_REGION: number = 1;
public static AR_LABELING_THRESH_MODE_AUTO_ADAPTIVE: number = 3;
public static AR_LABELING_THRESH_MODE_AUTO_MEDIAN: number = 1;
public static AR_LABELING_THRESH_MODE_AUTO_OTSU: number = 2;
public static AR_LABELING_THRESH_MODE_MANUAL: number = 0;
public static AR_LABELING_WHITE_REGION: number = 0;
public static AR_LOG_LEVEL_DEBUG: number = 0;
public static AR_LOG_LEVEL_ERROR: number = 3;
public static AR_LOG_LEVEL_INFO: number = 1;
public static AR_LOG_LEVEL_REL_INFO: number = 4;
public static AR_LOG_LEVEL_WARN: number = 2;
public static AR_LOOP_BREAK_THRESH: number = 0;
public static AR_MARKER_INFO_CUTOFF_PHASE_HEURISTIC_TROUBLESOME_MATRIX_CODES: number = 9;
public static AR_MARKER_INFO_CUTOFF_PHASE_MATCH_BARCODE_EDC_FAIL: number = 5;
public static AR_MARKER_INFO_CUTOFF_PHASE_MATCH_BARCODE_NOT_FOUND: number = 4;
public static AR_MARKER_INFO_CUTOFF_PHASE_MATCH_CONFIDENCE: number = 6;
public static AR_MARKER_INFO_CUTOFF_PHASE_MATCH_CONTRAST: number = 3;
public static AR_MARKER_INFO_CUTOFF_PHASE_MATCH_GENERIC: number = 2;
public static AR_MARKER_INFO_CUTOFF_PHASE_NONE: number = 0;
public static AR_MARKER_INFO_CUTOFF_PHASE_PATTERN_EXTRACTION: number = 1;
public static AR_MARKER_INFO_CUTOFF_PHASE_POSE_ERROR: number = 7;
public static AR_MARKER_INFO_CUTOFF_PHASE_POSE_ERROR_MULTI: number = 8;
public static AR_MATRIX_CODE_3x3: number = 3;
public static AR_MATRIX_CODE_3x3_HAMMING63: number = 515;
public static AR_MATRIX_CODE_3x3_PARITY65: number = 259;
public static AR_MATRIX_CODE_4x4: number = 4;
public static AR_MATRIX_CODE_4x4_BCH_13_5_5: number = 1028;
public static AR_MATRIX_CODE_4x4_BCH_13_9_3: number = 772;
public static AR_MATRIX_CODE_DETECTION: number = 2;
public static AR_MAX_LOOP_COUNT: number = 5;
public static AR_NOUSE_TRACKING_HISTORY: number = 1;
public static AR_TEMPLATE_MATCHING_COLOR: number = 0;
public static AR_TEMPLATE_MATCHING_COLOR_AND_MATRIX: number = 3;
public static AR_TEMPLATE_MATCHING_MONO: number = 1;
public static AR_TEMPLATE_MATCHING_MONO_AND_MATRIX: number = 4;
public static AR_USE_TRACKING_HISTORY: number = 0;
public static AR_USE_TRACKING_HISTORY_V2: number = 2;
public static HEAPU8 = Module.HEAPU8;
public static UNKNOWN_MARKER = -1;
public static PATTERN_MARKER = 0;
public static BARCODE_MARKER = 1;
public static markerInfo: any;
public static multiEachMarkerInfo: any;
public static setup: (
width: number,
height: number,
cameraParamId: any
) => any = Module.setup;
public static teardown: (id: number) => any = Module.teardown;
public static setupAR2: () => any = Module.setupAR2;
public static setLogLevel: (mode: any) => any = Module.setLogLevel;
public static getLogLevel: () => any = Module.getLogLevel;
public static setDebugMode: (id: number, mode: number) => any =
Module.getLogLevel;
public static getDebugMode: (id: number) => any = Module.getDebugMode;
public static getProcessingImage: (id: number) => any =
Module.getProcessingImage;
public static setMarkerInfoDir: (
id: number,
markerIndex: number,
dir: any
) => any = Module.setMarkerInfoDir;
public static setMarkerInfoVertex: (id: number, markerIndex: number) => any =
Module.setMarkerInfoVertex;
public static getTransMatSquare: (
id: number,
markerUID: number,
markerWidth: number
) => any = Module.getTransMatSquare;
public static getTransMatSquareCont: (
id: any,
markerUID: number,
markerWidth: number
) => any = Module.getTransMatSquareCont;
public static getTransMatMultiSquare: (id: any, markerUID: number) => any =
Module.getTransMatMultiSquare;
public static getTransMatMultiSquareRobust: (id: number, i: number) => any =
Module.getTransMatMultiSquareRobust;
public static getMultiMarkerNum: (id: number, multiId: number) => any =
Module.getMultiMarkerNum;
public static getMultiMarkerCount: (id: number) => any =
Module.getMultiMarkerCount;
public static detectMarker: (id: number) => any = Module.detectMarker;
public static getMarkerNum: (id: number) => any = Module.getMarkerNum;
public static getMarker: (id: number, markerIndex: number) => any =
Module.getMarker;
public static getMultiEachMarker: (
id: number,
multiMarkerId: number,
markerIndex: number
) => any = Module.getMultiEachMarker;
public static detectNFTMarker: (id: number) => any = Module.detectNFTMarker;
public static setProjectionNearPlane: (id: number, value: any) => any =
Module.setProjectionNearPlane;
public static getProjectionNearPlane: (id: number) => any =
Module.getProjectionNearPlane;
public static setProjectionFarPlane: (id: number, value: any) => any =
Module.setProjectionFarPlane;
public static getProjectionFarPlane: (id: number) => any =
Module.getProjectionFarPlane;
public static setThresholdMode: (id: number, mode: any) => any =
Module.setThresholdMode;
public static getThresholdMode: (id: number) => any = Module.getThresholdMode;
public static setThreshold: (id: number, threshold: number) => any =
Module.setThreshold;
public static getThreshold: (id: number) => any = Module.getThreshold;
public static setPatternDetectionMode: (id: number, mode: any) => any =
Module.setPatternDetectionMode;
public static getPatternDetectionMode: (id: number) => any =
Module.getPatternDetectionMode;
public static setMatrixCodeType: (id: number, type: any) => any =
Module.setMatrixCodeType;
public static getMatrixCodeType: (id: number) => any =
Module.getMatrixCodeType;
public static setLabelingMode: (id: number, mode: any) => any =
Module.setLabelingMode;
public static getLabelingMode: (id: number) => any = Module.getLabelingMode;
public static setPattRatio: (id: number, ratio: number) => any =
Module.setPattRatio;
public static getPattRatio: (id: number) => any = Module.getPattRatio;
public static setImageProcMode: (id: number, mode: any) => any =
Module.setImageProcMode;
public static getImageProcMode: (id: number) => any = Module.getImageProcMode;
public static markerCount = 0;
public static multiMarkerCount = 0;
public static cameraCount = 0;
public static getMarkerInfo() {
return Module.markerInfo;
}
public static deleteCamera(id: number) {
throw Error("deleteCamera not implemented");
// TODO: This was never implemented
}
public static loadCamera(
url: string | Uint8Array,
callback: (id: number) => any,
onerror: (err: any) => any
) {
const filename = "/camera_param_" + ARToolKit.cameraCount++;
const writeCallback = () => {
const id = Module._loadCamera(filename);
if (callback) {
callback(id);
}
};
if (typeof url === "object") {
// Maybe it's a byte array
ARToolKit.writeByteArrayToFS(filename, url, writeCallback);
} else if (url.indexOf("\n") > -1) {
// Or a string with the camera param
ARToolKit.writeStringToFS(filename, url, writeCallback);
} else {
ARToolKit.ajax(url, filename, writeCallback);
}
}
public static getFrameMalloc() {
return Module.frameMalloc;
}
public static addMarker(
arId: number,
url: string,
callback: (id: number) => any,
onError: (err: any) => any
) {
const filename = "/marker_" + ARToolKit.markerCount++;
ARToolKit.ajax(url, filename, () => {
const id = Module._addMarker(arId, filename);
if (callback) {
callback(id);
}
});
}
public static addNFTMarker(
arId: number,
url: string,
callback: (id: number) => any,
onError: () => any
) {
const mId = ARToolKit.markerCount++;
const prefix = "/markerNFT_" + mId;
const filename1 = prefix + ".fset";
const filename2 = prefix + ".iset";
const filename3 = prefix + ".fset3";
ARToolKit.ajax(url + ".fset", filename1, () => {
ARToolKit.ajax(url + ".iset", filename2, () => {
ARToolKit.ajax(url + ".fset3", filename3, () => {
const id = Module._addNFTMarker(arId, prefix);
if (callback) {
callback(id);
}
});
});
});
}
public static bytesToString(array: Uint8Array) {
return String.fromCharCode.apply(String, array);
}
public static parseMultiFile(bytes: Uint8Array) {
const str = this.bytesToString(bytes);
const lines = str.split("\n");
const files: any[] = [];
let state = 0; // 0 - read,
lines.forEach((line: string) => {
line = line.trim();
if (!line || line.startsWith("#")) {
return;
}
switch (state) {
case 0:
state = 1;
return;
case 1: // filename or barcode
if (!line.match(/^\d+$/)) {
files.push(line);
}
case 2: // width
case 3: // matrices
case 4:
state++;
return;
case 5:
state = 1;
return;
}
});
return files;
}
public static addMultiMarker(
arId: number,
url: string,
callback: (id: number, markerNum: any) => any,
onError: () => any
) {
const filename = "/multi_marker_" + ARToolKit.multiMarkerCount++;
ARToolKit.ajax(url, filename, (bytes: Uint8Array) => {
let files = this.parseMultiFile(bytes);
const ok = () => {
const markerID = Module._addMultiMarker(arId, filename);
const markerNum = Module.getMultiMarkerNum(arId, markerID);
if (callback) {
callback(markerID, markerNum);
}
};
if (!files.length) {
return ok();
}
const path = url
.split("/")
.slice(0, -1)
.join("/");
files = files.map(file => {
return [path + "/" + file, file];
});
ARToolKit.ajaxDependencies(files, ok);
});
}
// transfer image
public static writeStringToFS(
filename: string,
string: string,
callback: () => any
) {
const byteArray = new Uint8Array(string.length);
for (let i = 0; i < byteArray.length; i++) {
byteArray[i] = string.charCodeAt(i) & 0xff;
}
ARToolKit.writeByteArrayToFS(filename, byteArray, callback);
}
public static writeByteArrayToFS(
filename: string,
byteArray: Uint8Array,
callback: (byteArray: Uint8Array) => any
) {
Module.FS.writeFile(filename, byteArray, { encoding: "binary" });
callback(byteArray);
}
// Eg.
// ajax('../bin/Data2/markers.dat', '/Data2/markers.dat', callback);
// ajax('../bin/Data/patt.hiro', '/patt.hiro', callback);
public static ajax(
url: string,
filename: string,
callback: (bytes: Uint8Array) => any
) {
const oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer"; // blob arraybuffer
oReq.onload = () => {
const arrayBuffer = oReq.response;
const byteArray = new Uint8Array(arrayBuffer);
ARToolKit.writeByteArrayToFS(filename, byteArray, callback);
};
oReq.send();
}
public static ajaxDependencies(files: any[], callback: () => any) {
const next = files.pop();
if (next) {
ARToolKit.ajax(next[0], next[1], () => {
ARToolKit.ajaxDependencies(files, callback);
});
} else {
callback();
}
}
}
export default ARToolKit;