@zappar/zappar-cv 2.0.0-beta.3 → 2.0.0-beta.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,5 @@
1
+ import { image_target_type_t } from "./gen/zappar-native";
2
+ import { getPreviewMesh } from "./imagetracker-previewmesh";
1
3
  import { RiffReader } from "./riff-reader";
2
4
  let byId = new Map();
3
5
  const decoder = new TextDecoder();
@@ -34,7 +36,10 @@ export class ImageTracker {
34
36
  topRadius: -1,
35
37
  bottomRadius: -1,
36
38
  sideLength: -1,
37
- physicalScaleFactor: -1
39
+ physicalScaleFactor: -1,
40
+ trainedWidth: 0,
41
+ trainedHeight: 0,
42
+ type: image_target_type_t.IMAGE_TRACKER_TYPE_PLANAR
38
43
  };
39
44
  try {
40
45
  const reader = new RiffReader(current.data, false);
@@ -59,54 +64,90 @@ export class ImageTracker {
59
64
  let currentOffset = 0;
60
65
  let version = "0";
61
66
  [version, currentOffset] = readLine(currentOffset, data);
62
- if (version === "3") {
63
- let treeOrFlat = "0";
64
- [treeOrFlat, currentOffset] = readLine(currentOffset, data);
65
- if (treeOrFlat !== "0" && treeOrFlat !== "1")
66
- return;
67
- let numberTargets = "0";
68
- [numberTargets, currentOffset] = readLine(currentOffset, data);
69
- const parsedTargets = parseInt(numberTargets);
70
- if (isNaN(parsedTargets) || parsedTargets < 1)
71
- return;
72
- let emptyLine = "";
73
- [emptyLine, currentOffset] = readLine(currentOffset, data);
74
- if (emptyLine.length !== 0)
75
- return;
76
- let infoLine = "";
77
- [infoLine, currentOffset] = readLine(currentOffset, data);
78
- const infoLineParts = infoLine.split(" ");
79
- if (infoLineParts.length < 7)
80
- return;
67
+ if (version === "1")
68
+ return this._parseOdleV1(data, currentOffset, info);
69
+ else if (version === "3")
70
+ return this._parseOdleV3(data, currentOffset, info);
71
+ }
72
+ _parseOdleV1(data, currentOffset, info) {
73
+ let treeOrFlat = "0";
74
+ [treeOrFlat, currentOffset] = readLine(currentOffset, data);
75
+ if (treeOrFlat !== "0" && treeOrFlat !== "1")
76
+ return;
77
+ let emptyLine = "";
78
+ [emptyLine, currentOffset] = readLine(currentOffset, data);
79
+ if (emptyLine.length !== 0)
80
+ return;
81
+ let infoLine = "";
82
+ [infoLine, currentOffset] = readLine(currentOffset, data);
83
+ const infoLineParts = infoLine.split(" ");
84
+ if (infoLineParts.length < 5)
85
+ return;
86
+ info.trainedWidth = parseInt(infoLineParts[3].replace("[", ""));
87
+ info.trainedHeight = parseInt(infoLineParts[4].replace("]", ""));
88
+ }
89
+ _parseOdleV3(data, currentOffset, info) {
90
+ let treeOrFlat = "0";
91
+ [treeOrFlat, currentOffset] = readLine(currentOffset, data);
92
+ if (treeOrFlat !== "0" && treeOrFlat !== "1")
93
+ return;
94
+ let numberTargets = "0";
95
+ [numberTargets, currentOffset] = readLine(currentOffset, data);
96
+ const parsedTargets = parseInt(numberTargets);
97
+ if (isNaN(parsedTargets) || parsedTargets < 1)
98
+ return;
99
+ let emptyLine = "";
100
+ [emptyLine, currentOffset] = readLine(currentOffset, data);
101
+ if (emptyLine.length !== 0)
102
+ return;
103
+ let infoLine = "";
104
+ [infoLine, currentOffset] = readLine(currentOffset, data);
105
+ const infoLineParts = infoLine.split(" ");
106
+ if (infoLineParts.length < 6)
107
+ return;
108
+ const targetType = parseInt(infoLineParts[0]);
109
+ if (targetType === 0 || targetType === 1 || targetType === 2)
110
+ info.type = targetType;
111
+ info.trainedWidth = parseInt(infoLineParts[4].replace("[", ""));
112
+ info.trainedHeight = parseInt(infoLineParts[5].replace("]", ""));
113
+ if (infoLineParts.length >= 7) {
81
114
  info.physicalScaleFactor = parseFloat(infoLineParts[6]);
82
115
  if (isNaN(info.physicalScaleFactor))
83
116
  info.physicalScaleFactor = -1;
84
- if (infoLineParts.length >= 8) {
85
- info.topRadius = parseFloat(infoLineParts[7]);
86
- if (isNaN(info.topRadius))
87
- info.topRadius = -1;
88
- info.bottomRadius = info.topRadius;
89
- }
90
- if (infoLineParts.length >= 9) {
91
- info.bottomRadius = parseFloat(infoLineParts[8]);
92
- if (isNaN(info.bottomRadius))
93
- info.bottomRadius = -1;
94
- }
95
- if (infoLineParts.length >= 10) {
96
- info.sideLength = parseFloat(infoLineParts[9]);
97
- if (isNaN(info.sideLength))
98
- info.sideLength = -1;
99
- }
117
+ }
118
+ if (infoLineParts.length >= 8) {
119
+ info.topRadius = parseFloat(infoLineParts[7]);
120
+ if (isNaN(info.topRadius))
121
+ info.topRadius = -1;
122
+ info.bottomRadius = info.topRadius;
123
+ }
124
+ if (infoLineParts.length >= 9) {
125
+ info.bottomRadius = parseFloat(infoLineParts[8]);
126
+ if (isNaN(info.bottomRadius))
127
+ info.bottomRadius = -1;
128
+ }
129
+ if (infoLineParts.length >= 10) {
130
+ info.sideLength = parseFloat(infoLineParts[9]);
131
+ if (isNaN(info.sideLength))
132
+ info.sideLength = -1;
100
133
  }
101
134
  }
102
135
  getDecodedPreview(i) {
103
136
  const info = this.getTargetInfo(i);
104
137
  if (!info.preview)
105
138
  return undefined;
106
- const blob = new Blob([info.preview.compressed], { type: info.preview.mimeType });
107
- const image = new Image();
108
- image.src = URL.createObjectURL(blob);
109
- return image;
139
+ if (!info.preview.image) {
140
+ const blob = new Blob([info.preview.compressed], { type: info.preview.mimeType });
141
+ info.preview.image = new Image();
142
+ info.preview.image.src = URL.createObjectURL(blob);
143
+ }
144
+ return info.preview.image;
145
+ }
146
+ getPreviewMesh(i) {
147
+ const info = this.getTargetInfo(i);
148
+ if (!info.previewMesh)
149
+ info.previewMesh = getPreviewMesh(info);
150
+ return info.previewMesh;
110
151
  }
111
152
  }
112
153
  function readLine(offset, str) {
package/lib/native.js CHANGED
@@ -7,6 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
+ import { image_target_type_t } from "./gen/zappar";
10
11
  import { zappar_client } from "./gen/zappar-client";
11
12
  import { drawPlane } from "./drawplane";
12
13
  import { cameraRotationForScreenOrientation, projectionMatrix } from "./cameramodel";
@@ -39,7 +40,7 @@ export function initialize(opts) {
39
40
  d: ab
40
41
  }, [ab]);
41
42
  });
42
- if (window.location.hostname.toLowerCase().indexOf(".zappar.io") > 0) {
43
+ if (window.location.hostname.toLowerCase().indexOf(".zappar.io") > 0 || window.location.hostname.toLowerCase().indexOf(".webar.run") > 0) {
43
44
  let pathParts = window.location.pathname.split("/");
44
45
  if (pathParts.length > 1 && pathParts[1].length > 0)
45
46
  c.impl.analytics_project_id_set(".wiz" + pathParts[1]);
@@ -117,7 +118,7 @@ export function initialize(opts) {
117
118
  }
118
119
  }
119
120
  });
120
- client = Object.assign(Object.assign({}, c.impl), { loaded: () => loaded, camera_default_device_id: userFacing => userFacing ? CameraSource.USER_DEFAULT_DEVICE_ID : CameraSource.DEFAULT_DEVICE_ID, camera_source_create: (p, deviceId) => createCameraSource(p, deviceId), camera_source_destroy: cam => { var _a; return (_a = getCameraSource(cam)) === null || _a === void 0 ? void 0 : _a.destroy(); }, camera_source_pause: cam => { var _a; return (_a = getCameraSource(cam)) === null || _a === void 0 ? void 0 : _a.pause(); }, camera_source_start: cam => { var _a; return (_a = getCameraSource(cam)) === null || _a === void 0 ? void 0 : _a.start(); }, pipeline_create: () => Pipeline.create(c.impl, messageManager), pipeline_frame_update: (p) => { var _a; return (_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.frameUpdate(c); }, pipeline_camera_frame_draw_gl: (pipeline, screenWidth, screenHeight, mirror) => {
121
+ client = Object.assign(Object.assign({}, c.impl), { loaded: () => loaded, camera_default_device_id: userFacing => userFacing ? CameraSource.USER_DEFAULT_DEVICE_ID : CameraSource.DEFAULT_DEVICE_ID, camera_source_create: (p, deviceId) => createCameraSource(p, deviceId), camera_source_destroy: cam => { var _a; return (_a = getCameraSource(cam)) === null || _a === void 0 ? void 0 : _a.destroy(); }, camera_source_pause: cam => { var _a; return (_a = getCameraSource(cam)) === null || _a === void 0 ? void 0 : _a.pause(); }, camera_source_start: cam => { var _a; return (_a = getCameraSource(cam)) === null || _a === void 0 ? void 0 : _a.start(); }, camera_count: () => 2, camera_id: indx => indx === 0 ? CameraSource.DEFAULT_DEVICE_ID : CameraSource.USER_DEFAULT_DEVICE_ID, camera_name: indx => indx === 0 ? "Rear-facing Camera" : "User-facing Camera", camera_user_facing: indx => indx !== 0, pipeline_create: () => Pipeline.create(c.impl, messageManager), pipeline_frame_update: (p) => { var _a; return (_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.frameUpdate(c); }, pipeline_camera_frame_draw_gl: (pipeline, screenWidth, screenHeight, mirror) => {
121
122
  var _a;
122
123
  (_a = Pipeline.get(pipeline)) === null || _a === void 0 ? void 0 : _a.cameraFrameDrawGL(screenWidth, screenHeight, mirror);
123
124
  }, draw_plane: (gl, projectionMatrix, cameraMatrix, targetMatrix, texture) => {
@@ -133,6 +134,14 @@ export function initialize(opts) {
133
134
  }, pipeline_draw_face_project: (p, matrix, vertices, uvMatrix, uvs, indices, texture) => {
134
135
  var _a;
135
136
  (_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.drawFaceProject(matrix, vertices, uvMatrix, uvs, indices, texture);
137
+ }, pipeline_draw_image_target_preview: (p, projectionMatrix, cameraMatrix, targetMatrix, o, indx) => {
138
+ var _a;
139
+ let obj = ImageTracker.get(o);
140
+ if (!obj) {
141
+ zcwarn("image tracker not found");
142
+ return;
143
+ }
144
+ (_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.drawImageTargetPreview(projectionMatrix, cameraMatrix, targetMatrix, indx, obj);
136
145
  }, projection_matrix_from_camera_model: projectionMatrix, projection_matrix_from_camera_model_ext: projectionMatrix, pipeline_process_gl: p => { var _a; return (_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.processGL(); }, pipeline_gl_context_set: (p, gl, texturePool) => { var _a; return (_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.glContextSet(gl, texturePool); }, pipeline_gl_context_lost: (p) => { var _a; return (_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.glContextLost(); }, pipeline_camera_frame_upload_gl: p => { var _a; return (_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.uploadGL(); }, pipeline_camera_frame_texture_gl: p => { var _a; return (_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.cameraFrameTexture(); }, pipeline_camera_frame_texture_matrix: (p, sw, sh, mirror) => { var _a; return ((_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.cameraFrameTextureMatrix(sw, sh, mirror)) || mat4.create(); }, pipeline_camera_frame_user_facing: p => { var _a; return ((_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.cameraFrameUserFacing()) || false; }, pipeline_camera_pose_default: () => mat4.create(), pipeline_camera_pose_with_attitude: (p, mirror) => { var _a; return ((_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.cameraPoseWithAttitude(mirror)) || mat4.create(); }, pipeline_camera_pose_with_origin: (p, o) => { let res = mat4.create(); mat4.invert(res, o); return res; }, pipeline_sequence_record_clear: p => { var _a; return (_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.sequenceRecordClear(); }, pipeline_sequence_record_start: (p, expectedFrames) => { var _a; return (_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.sequenceRecordStart(expectedFrames); }, pipeline_sequence_record_stop: p => { var _a; return (_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.sequenceRecordStop(); }, pipeline_sequence_record_device_attitude_matrices_set: (p, v) => { var _a; return (_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.sequenceRecordDeviceAttitudeMatrices(v); }, pipeline_sequence_record_data: p => { var _a; return ((_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.sequenceRecordData()) || new Uint8Array(0); }, pipeline_sequence_record_data_size: p => { var _a; return ((_a = Pipeline.get(p)) === null || _a === void 0 ? void 0 : _a.sequenceRecordData().byteLength) || 0; }, instant_world_tracker_anchor_pose_camera_relative: (o, mirror) => {
137
146
  let res = applyScreenCounterRotation(undefined, c.impl.instant_world_tracker_anchor_pose_raw(o));
138
147
  if (mirror) {
@@ -165,7 +174,14 @@ export function initialize(opts) {
165
174
  let rawVec = vec3.create();
166
175
  vec3.transformMat4(rawVec, [x, y, z], rotationMat);
167
176
  c.impl.instant_world_tracker_anchor_pose_set_from_camera_offset_raw(o, rawVec[0], rawVec[1], rawVec[2], orientation);
168
- }, image_tracker_create: pipeline => ImageTracker.create(pipeline, c.impl), image_tracker_destroy: t => { var _a; return (_a = ImageTracker.get(t)) === null || _a === void 0 ? void 0 : _a.destroy(); }, image_tracker_target_count: t => {
177
+ }, image_tracker_create: pipeline => ImageTracker.create(pipeline, c.impl), image_tracker_destroy: t => { var _a; return (_a = ImageTracker.get(t)) === null || _a === void 0 ? void 0 : _a.destroy(); }, image_tracker_target_type: (t, i) => {
178
+ let obj = ImageTracker.get(t);
179
+ if (!obj) {
180
+ zcwarn("attempting to call image_tracker_target_type on a destroyed zappar_image_tracker_t");
181
+ return image_target_type_t.IMAGE_TRACKER_TYPE_PLANAR;
182
+ }
183
+ return obj.getTargetInfo(i).type;
184
+ }, image_tracker_target_count: t => {
169
185
  let obj = ImageTracker.get(t);
170
186
  if (!obj) {
171
187
  zcwarn("attempting to call image_tracker_target_count on a destroyed zappar_image_tracker_t");
@@ -257,6 +273,62 @@ export function initialize(opts) {
257
273
  }
258
274
  mat4.multiply(res, cameraPose, res);
259
275
  return res;
276
+ }, image_tracker_target_preview_mesh_indices: (o, indx) => {
277
+ let obj = ImageTracker.get(o);
278
+ if (!obj) {
279
+ zcwarn("attempting to call image_tracker_target_preview_mesh_indices on a destroyed zappar_image_tracker_t");
280
+ return new Uint16Array();
281
+ }
282
+ return obj.getPreviewMesh(indx).indices;
283
+ }, image_tracker_target_preview_mesh_vertices: (o, indx) => {
284
+ let obj = ImageTracker.get(o);
285
+ if (!obj) {
286
+ zcwarn("attempting to call image_tracker_target_preview_mesh_vertices on a destroyed zappar_image_tracker_t");
287
+ return new Float32Array();
288
+ }
289
+ return obj.getPreviewMesh(indx).vertices;
290
+ }, image_tracker_target_preview_mesh_uvs: (o, indx) => {
291
+ let obj = ImageTracker.get(o);
292
+ if (!obj) {
293
+ zcwarn("attempting to call image_tracker_target_preview_mesh_uvs on a destroyed zappar_image_tracker_t");
294
+ return new Float32Array();
295
+ }
296
+ return obj.getPreviewMesh(indx).uvs;
297
+ }, image_tracker_target_preview_mesh_normals: (o, indx) => {
298
+ let obj = ImageTracker.get(o);
299
+ if (!obj) {
300
+ zcwarn("attempting to call image_tracker_target_preview_mesh_normals on a destroyed zappar_image_tracker_t");
301
+ return new Float32Array();
302
+ }
303
+ return obj.getPreviewMesh(indx).normals;
304
+ }, image_tracker_target_preview_mesh_indices_size: (o, indx) => {
305
+ let obj = ImageTracker.get(o);
306
+ if (!obj) {
307
+ zcwarn("attempting to call image_tracker_target_preview_mesh_indices_size on a destroyed zappar_image_tracker_t");
308
+ return 0;
309
+ }
310
+ return obj.getPreviewMesh(indx).indices.length;
311
+ }, image_tracker_target_preview_mesh_vertices_size: (o, indx) => {
312
+ let obj = ImageTracker.get(o);
313
+ if (!obj) {
314
+ zcwarn("attempting to call image_tracker_target_preview_mesh_vertices_size on a destroyed zappar_image_tracker_t");
315
+ return 0;
316
+ }
317
+ return obj.getPreviewMesh(indx).vertices.length;
318
+ }, image_tracker_target_preview_mesh_uvs_size: (o, indx) => {
319
+ let obj = ImageTracker.get(o);
320
+ if (!obj) {
321
+ zcwarn("attempting to call image_tracker_target_preview_mesh_uvs_size on a destroyed zappar_image_tracker_t");
322
+ return 0;
323
+ }
324
+ return obj.getPreviewMesh(indx).uvs.length;
325
+ }, image_tracker_target_preview_mesh_normals_size: (o, indx) => {
326
+ let obj = ImageTracker.get(o);
327
+ if (!obj) {
328
+ zcwarn("attempting to call image_tracker_target_preview_mesh_normals_size on a destroyed zappar_image_tracker_t");
329
+ return 0;
330
+ }
331
+ return obj.getPreviewMesh(indx).normals.length;
260
332
  }, face_tracker_anchor_pose_camera_relative: (o, indx, mirror) => {
261
333
  let res = applyScreenCounterRotation(undefined, c.impl.face_tracker_anchor_pose_raw(o, indx));
262
334
  if (mirror) {
package/lib/pipeline.d.ts CHANGED
@@ -7,6 +7,7 @@ import { Source, CameraFrameInfo } from "./source";
7
7
  import { CameraFrameReturnS2C, ImageBitmapS2C, VideoFrameS2C } from "./workerinterface";
8
8
  import { FaceMesh } from "./facemesh";
9
9
  import { Event } from "./event";
10
+ import { ImageTracker } from "./imagetracker";
10
11
  export declare class Pipeline {
11
12
  private _client;
12
13
  private _impl;
@@ -21,6 +22,7 @@ export declare class Pipeline {
21
22
  cameraPixelArrays: ArrayBuffer[];
22
23
  private _cameraDraw;
23
24
  private _faceDraw;
25
+ private _imageTargetPreviewDraw;
24
26
  private _faceProjectDraw;
25
27
  private _sequenceRecorder;
26
28
  private _sequenceRecordDeviceAttitudeMatrices;
@@ -44,6 +46,7 @@ export declare class Pipeline {
44
46
  glContextLost(): void;
45
47
  glContextSet(gl: WebGLRenderingContext, texturePool?: WebGLTexture[]): void;
46
48
  drawFace(projectionMatrix: Float32Array, cameraMatrix: Float32Array, targetMatrix: Float32Array, o: FaceMesh): void;
49
+ drawImageTargetPreview(projectionMatrix: Float32Array, cameraMatrix: Float32Array, targetMatrix: Float32Array, indx: number, o: ImageTracker): void;
47
50
  drawFaceProject(matrix: Float32Array, vertices: Float32Array, uvMatrix: Float32Array, uvs: Float32Array, indices: Uint16Array, texture: WebGLTexture): void;
48
51
  cameraFrameTexture(): WebGLTexture | undefined;
49
52
  cameraFrameTextureMatrix(sw: number, sh: number, mirror: boolean): Float32Array;
package/lib/pipeline.js CHANGED
@@ -8,6 +8,7 @@ import { Event } from "./event";
8
8
  import { zcerr } from "./loglevel";
9
9
  import { SequenceRecorder } from "./sequencerecorder";
10
10
  import { getCameraSource } from "./camera-source-map";
11
+ import { PreviewMeshDraw } from "./drawpreviewmesh";
11
12
  let byId = new Map();
12
13
  let identity = mat4.create();
13
14
  export class Pipeline {
@@ -121,10 +122,13 @@ export class Pipeline {
121
122
  this._cameraDraw.dispose();
122
123
  if (this._faceDraw)
123
124
  this._faceDraw.dispose();
125
+ if (this._imageTargetPreviewDraw)
126
+ this._imageTargetPreviewDraw.dispose();
124
127
  if (this._faceProjectDraw)
125
128
  this._faceProjectDraw.dispose();
126
129
  delete this._cameraDraw;
127
130
  delete this._faceDraw;
131
+ delete this._imageTargetPreviewDraw;
128
132
  delete this._faceProjectDraw;
129
133
  disposeDrawPlane();
130
134
  this.onGLContextReset.emit();
@@ -166,6 +170,16 @@ export class Pipeline {
166
170
  mat4.multiply(mat, mat, targetMatrix);
167
171
  this._faceDraw.drawFace(mat, o);
168
172
  }
173
+ drawImageTargetPreview(projectionMatrix, cameraMatrix, targetMatrix, indx, o) {
174
+ if (!this.glContext)
175
+ return;
176
+ if (!this._imageTargetPreviewDraw)
177
+ this._imageTargetPreviewDraw = new PreviewMeshDraw(this.glContext);
178
+ let mat = mat4.create();
179
+ mat4.multiply(mat, projectionMatrix, cameraMatrix);
180
+ mat4.multiply(mat, mat, targetMatrix);
181
+ this._imageTargetPreviewDraw.draw(mat, o, indx);
182
+ }
169
183
  drawFaceProject(matrix, vertices, uvMatrix, uvs, indices, texture) {
170
184
  if (!this.glContext)
171
185
  return;
package/lib/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "2.0.0-beta.3";
1
+ export declare const VERSION = "2.0.0-beta.7";
package/lib/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "2.0.0-beta.3";
1
+ export const VERSION = "2.0.0-beta.7";