Skip to content

Commit

Permalink
make _storeDataFile a static method in the Utils class
Browse files Browse the repository at this point in the history
- improve typedoc of ARToolkitNFT classes
  • Loading branch information
kalwalt committed Nov 22, 2024
1 parent 8d8b305 commit 7fd730c
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 20,178 deletions.
6,700 changes: 2 additions & 6,698 deletions dist/ARToolkitNFT.js

Large diffs are not rendered by default.

6,700 changes: 2 additions & 6,698 deletions dist/ARToolkitNFT_simd.js

Large diffs are not rendered by default.

6,703 changes: 2 additions & 6,701 deletions dist/ARToolkitNFT_td.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/js/threading_files/ARToolkitNFT_td.js

Large diffs are not rendered by default.

37 changes: 14 additions & 23 deletions src/ARToolkitNFT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ export class ARToolkitNFT implements IARToolkitNFT {
/**
* Load the camera, this is an important and required step, Internally fill
* the ARParam struct.
* @param {string} urlOrData: the camera parameter, usually a path to a .dat file
* @return {number} a number, the internal id.
* @param {Uint8Array|string} urlOrData: the camera parameter, usually a path to a .dat file
* @return {Promise<number>} a promise that resolves to a number, the internal id.
*/
public async loadCamera(urlOrData: Uint8Array | string): Promise<number> {
const target = "/camera_param_" + this.cameraCount++;
Expand All @@ -329,7 +329,7 @@ export class ARToolkitNFT implements IARToolkitNFT {
}
}

this._storeDataFile(data, target);
Utils._storeDataFile(data, target, this);

// return the internal marker ID
return this.instance._loadCamera(target);
Expand All @@ -338,10 +338,10 @@ export class ARToolkitNFT implements IARToolkitNFT {
/**
* Load the NFT Markers (.fset, .iset and .fset3) in the code, Must be provided
* the url of the file without the extension. If fails to load it raise an error.
* @param {number} arId internal id
* @param {Array<string>} urls array of urls of the descriptors files without ext
* @param {function} callback the callback to retrieve the ids.
* @param {function} onError2 the error callback.
* @return {Array<number>} an array of ids.
*/
public addNFTMarkers(
urls: Array<string | Array<string>>,
Expand Down Expand Up @@ -401,9 +401,9 @@ export class ARToolkitNFT implements IARToolkitNFT {
hexStrFset3.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)),
);

this._storeDataFile(contentFset, prefix + ".fset");
this._storeDataFile(contentIset, prefix + ".iset");
this._storeDataFile(contentFset3, prefix + ".fset3");
Utils._storeDataFile(contentFset, prefix + ".fset", this);
Utils._storeDataFile(contentIset, prefix + ".iset", this);
Utils._storeDataFile(contentFset3, prefix + ".fset3", this);
onSuccess(contentFset);
};

Expand Down Expand Up @@ -484,24 +484,15 @@ export class ARToolkitNFT implements IARToolkitNFT {
// ---------------------------------------------------------------------------

// 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.FS.writeFile(target, data, {
encoding: "binary",
});
}

/**
* Used internally by the addNFTMarkers method
* @param url url of the marker.
* @param target the target of the marker.
* @param callback callback to get the binary data.
* @param errorCallback the error callback.
* @param {string} url url of the marker.
* @param {string} target the target of the marker.
* @param {function} callback callback to get the binary data.
* @param {function} errorCallback the error callback.
* @param {string} prefix the prefix for the marker.
* @return {void}
*/
private ajax(
url: string,
Expand All @@ -519,7 +510,7 @@ export class ARToolkitNFT implements IARToolkitNFT {
callback: (byteArray: Uint8Array, prefix: string) => void,
prefix: string,
) => {
this.FS.writeFile(target, byteArray, { encoding: "binary" });
Utils._storeDataFile(byteArray, target, this);
callback(byteArray, prefix);
};

Expand Down
36 changes: 13 additions & 23 deletions src/ARToolkitNFT_simd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ export class ARToolkitNFT implements IARToolkitNFT {
/**
* Load the camera, this is an important and required step, Internally fill
* the ARParam struct.
* @param {string} urlOrData: the camera parameter, usually a path to a .dat file
* @return {number} a number, the internal id.
* @param {Uint8Array|string} urlOrData: the camera parameter, usually a path to a .dat file
* @return {Promise<number>} a promise that resolves to a number, the internal id.
*/
public async loadCamera(urlOrData: Uint8Array | string): Promise<number> {
const target = "/camera_param_" + this.cameraCount++;
Expand All @@ -329,7 +329,7 @@ export class ARToolkitNFT implements IARToolkitNFT {
}
}

this._storeDataFile(data, target);
Utils._storeDataFile(data, target, this);

// return the internal marker ID
return this.instance._loadCamera(target);
Expand All @@ -338,10 +338,10 @@ export class ARToolkitNFT implements IARToolkitNFT {
/**
* Load the NFT Markers (.fset, .iset and .fset3) in the code, Must be provided
* the url of the file without the extension. If fails to load it raise an error.
* @param {number} arId internal id
* @param {Array<string>} urls array of urls of the descriptors files without ext
* @param {function} callback the callback to retrieve the ids.
* @param {function} onError2 the error callback.
* @return {Array<number>} an array of ids.
*/
public addNFTMarkers(
urls: Array<string | Array<string>>,
Expand Down Expand Up @@ -400,9 +400,9 @@ export class ARToolkitNFT implements IARToolkitNFT {
hexStrFset3.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)),
);

this._storeDataFile(contentFset, prefix + ".fset");
this._storeDataFile(contentIset, prefix + ".iset");
this._storeDataFile(contentFset3, prefix + ".fset3");
Utils._storeDataFile(contentFset, prefix + ".fset", this);
Utils._storeDataFile(contentIset, prefix + ".iset", this);
Utils._storeDataFile(contentFset3, prefix + ".fset3", this);
onSuccess(contentFset);
};

Expand Down Expand Up @@ -483,24 +483,14 @@ export class ARToolkitNFT implements IARToolkitNFT {
// ---------------------------------------------------------------------------

// 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.FS.writeFile(target, data, {
encoding: "binary",
});
}

/**
* Used internally by the addNFTMarkers method
* @param url url of the marker.
* @param target the target of the marker.
* @param callback callback to get the binary data.
* @param errorCallback the error callback.
* @param {string} url url of the marker.
* @param {string} target the target of the marker.
* @param {function} callback callback to get the binary data.
* @param {function} errorCallback the error callback.
* @param {string} prefix the prefix for the marker.
*/
private ajax(
url: string,
Expand All @@ -518,7 +508,7 @@ export class ARToolkitNFT implements IARToolkitNFT {
callback: (byteArray: Uint8Array, prefix: string) => void,
prefix: string,
) => {
this.FS.writeFile(target, byteArray, { encoding: "binary" });
Utils._storeDataFile(byteArray, target, this);
callback(byteArray, prefix);
};

Expand Down
36 changes: 13 additions & 23 deletions src/ARToolkitNFT_td.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ export class ARToolkitNFT implements IARToolkitNFT {
/**
* Load the camera, this is an important and required step, Internally fill
* the ARParam struct.
* @param {string} urlOrData: the camera parameter, usually a path to a .dat file
* @return {number} a number, the internal id.
* @param {Uint8Array|string} urlOrData: the camera parameter, usually a path to a .dat file
* @return {Promise<number>} a promise that resolves to a number, the internal id.
*/
public async loadCamera(urlOrData: Uint8Array | string): Promise<number> {
const target = "/camera_param_" + this.cameraCount++;
Expand All @@ -329,7 +329,7 @@ export class ARToolkitNFT implements IARToolkitNFT {
}
}

this._storeDataFile(data, target);
Utils._storeDataFile(data, target, this);

// return the internal marker ID
return this.instance._loadCamera(target);
Expand All @@ -338,10 +338,10 @@ export class ARToolkitNFT implements IARToolkitNFT {
/**
* Load the NFT Markers (.fset, .iset and .fset3) in the code, Must be provided
* the url of the file without the extension. If fails to load it raise an error.
* @param {number} arId internal id
* @param {Array<string>} urls array of urls of the descriptors files without ext
* @param {function} callback the callback to retrieve the ids.
* @param {function} onError2 the error callback.
* @return {Array<number>} an array of ids.
*/
public addNFTMarkers(
urls: Array<string | Array<string>>,
Expand Down Expand Up @@ -400,9 +400,9 @@ export class ARToolkitNFT implements IARToolkitNFT {
hexStrFset3.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)),
);

this._storeDataFile(contentFset, prefix + ".fset");
this._storeDataFile(contentIset, prefix + ".iset");
this._storeDataFile(contentFset3, prefix + ".fset3");
Utils._storeDataFile(contentFset, prefix + ".fset", this);
Utils._storeDataFile(contentIset, prefix + ".iset", this);
Utils._storeDataFile(contentFset3, prefix + ".fset3", this);
onSuccess(contentFset);
};

Expand Down Expand Up @@ -482,24 +482,14 @@ export class ARToolkitNFT implements IARToolkitNFT {
// ---------------------------------------------------------------------------

// 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.FS.writeFile(target, data, {
encoding: "binary",
});
}

/**
* Used internally by the addNFTMarkers method
* @param url url of the marker.
* @param target the target of the marker.
* @param callback callback to get the binary data.
* @param errorCallback the error callback.
* @param {string} url url of the marker.
* @param {string} target the target of the marker.
* @param {function} callback callback to get the binary data.
* @param {function} errorCallback the error callback.
* @param {string} prefix the prefix for the marker.
*/
private ajax(
url: string,
Expand All @@ -517,7 +507,7 @@ export class ARToolkitNFT implements IARToolkitNFT {
callback: (byteArray: Uint8Array, prefix: string) => void,
prefix: string,
) => {
this.FS.writeFile(target, byteArray, { encoding: "binary" });
Utils._storeDataFile(byteArray, target, this);
callback(byteArray, prefix);
};

Expand Down
28 changes: 21 additions & 7 deletions src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,26 @@
import axios, { AxiosResponse } from "axios";

export default class Utils {
static async fetchRemoteData(url: string) {
static async fetchRemoteData(url: string): Promise<Uint8Array> {
try {
const response: AxiosResponse<any> = await axios.get(url, {
responseType: "arraybuffer",
});
console.log(response);
return new Uint8Array(response.data);
} catch (error) {
throw new Error("Error in Utils.fetchRemoteData: ", error);
}
}

static async fetchRemoteDataCallback(url: string, callback: any) {
static async fetchRemoteDataCallback(
url: string,
callback: any,
): Promise<any> {
try {
const response: any = await axios
.get(url, { responseType: "arraybuffer" })
.then((response: any) => {
const data = new Uint8Array(response.data);
console.log(data);
callback(response);
});
return response;
Expand All @@ -63,15 +64,15 @@ export default class Utils {
}
}

static string2Uint8Data(string: string) {
static string2Uint8Data(string: string): Uint8Array {
const data = new Uint8Array(string.length);
for (let i = 0; i < data.length; i++) {
data[i] = string.charCodeAt(i) & 0xff;
}
return data;
}

static Uint8ArrayToStr(array: any) {
static Uint8ArrayToStr(array: any): string {
let out, i, len, c;
let char2, char3;

Expand Down Expand Up @@ -112,7 +113,7 @@ export default class Utils {
return out;
}

static checkZFT(url: string) {
static checkZFT(url: string): any {
let isZFT: boolean = null;

try {
Expand All @@ -130,4 +131,17 @@ export default class Utils {
throw new Error("Error in Utils.checkZFT: ", error);
}
}
/**
* Stores data in the Emscripten filesystem.
* Note: FS is provided by emscripten and valid data must be in binary format encoded as Uint8Array
* @param {Uint8Array} data - The binary data to store.
* @param {string} target - The target file path in the Emscripten filesystem.
* @param {any} instance - The instance of the class containing the FS object.
* @return {void}
*/
static _storeDataFile(data: Uint8Array, target: string, instance: any) {
instance.FS.writeFile(target, data, {
encoding: "binary",
});
}
}
1 change: 0 additions & 1 deletion types/src/ARToolkitNFT.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,5 @@ export declare class ARToolkitNFT implements IARToolkitNFT {
passVideoData(videoFrame: Uint8ClampedArray, videoLuma: Uint8Array): void;
loadCamera(urlOrData: Uint8Array | string): Promise<number>;
addNFTMarkers(urls: Array<string | Array<string>>, callback: (filename: number[]) => void, onError2: (errorNumber: number) => void): Array<number>;
private _storeDataFile;
private ajax;
}
1 change: 0 additions & 1 deletion types/src/ARToolkitNFT_simd.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,5 @@ export declare class ARToolkitNFT implements IARToolkitNFT {
passVideoData(videoFrame: Uint8ClampedArray, videoLuma: Uint8Array): void;
loadCamera(urlOrData: Uint8Array | string): Promise<number>;
addNFTMarkers(urls: Array<string | Array<string>>, callback: (filename: number[]) => void, onError2: (errorNumber: number) => void): Array<number>;
private _storeDataFile;
private ajax;
}
1 change: 0 additions & 1 deletion types/src/ARToolkitNFT_td.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,5 @@ export declare class ARToolkitNFT implements IARToolkitNFT {
passVideoData(videoFrame: Uint8ClampedArray, videoLuma: Uint8Array): void;
loadCamera(urlOrData: Uint8Array | string): Promise<number>;
addNFTMarkers(urls: Array<string | Array<string>>, callback: (filename: number[]) => void, onError2: (errorNumber: number) => void): Array<number>;
private _storeDataFile;
private ajax;
}
3 changes: 2 additions & 1 deletion types/src/Utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export default class Utils {
static fetchRemoteDataCallback(url: string, callback: any): Promise<any>;
static string2Uint8Data(string: string): Uint8Array;
static Uint8ArrayToStr(array: any): string;
static checkZFT(url: string): () => boolean;
static checkZFT(url: string): any;
static _storeDataFile(data: Uint8Array, target: string, instance: any): void;
}

0 comments on commit 7fd730c

Please sign in to comment.