@zappar/zappar-cv 3.6.0-beta.0 → 3.7.0-beta.0

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.
@@ -0,0 +1,331 @@
1
+ import { anchor_status_t, plane_orientation_t, world_scale_mode_t } from "./gen/zappar";
2
+ import { Pipeline } from "./pipeline";
3
+ import { WebXRCameraSource } from "./webxr-camera-source";
4
+ const _emptyUint8Array = new Uint8Array(0);
5
+ export class WebXRWorldTracker {
6
+ constructor() {
7
+ this._latestId = 1;
8
+ this._wtStateById = new Map();
9
+ this._anchorStateById = new Map();
10
+ this._identityPose = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
11
+ this._emptyArray = new Float32Array([]);
12
+ this.impl = {
13
+ world_tracker_create: (pipeline) => {
14
+ let p = Pipeline.get(pipeline);
15
+ if (!p)
16
+ throw new Error("Invalid pipeline in world_tracker_create");
17
+ let newId = (this._latestId++);
18
+ let s = {
19
+ pipeline: p,
20
+ enabled: true,
21
+ horizontal_plane_detection_enabled: true,
22
+ vertical_plane_detection_enabled: false,
23
+ world_anchor_id: 'world',
24
+ ground_anchor_id: 'ground',
25
+ tracks_data_enabled: false,
26
+ projections_data_enabled: false,
27
+ mesh_anchors_enabled: false
28
+ };
29
+ this._wtStateById.set(newId, s);
30
+ this._notifyOptionsUpdated(s.pipeline);
31
+ return newId;
32
+ },
33
+ world_tracker_destroy: (o) => {
34
+ let s = this._getWtState(o);
35
+ this._wtStateById.delete(o);
36
+ },
37
+ world_tracker_enabled: (o) => {
38
+ let s = this._getWtState(o);
39
+ return s.enabled;
40
+ },
41
+ world_tracker_enabled_set: (o, enabled) => {
42
+ let s = this._getWtState(o);
43
+ s.enabled = enabled;
44
+ this._notifyOptionsUpdated(s.pipeline);
45
+ },
46
+ world_tracker_quality: (o) => {
47
+ let wtData = this._getWtData(o);
48
+ if (!wtData)
49
+ return 0;
50
+ return wtData.quality;
51
+ },
52
+ world_tracker_horizontal_plane_detection_enabled: (o) => {
53
+ let s = this._getWtState(o);
54
+ return s.horizontal_plane_detection_enabled;
55
+ },
56
+ world_tracker_horizontal_plane_detection_enabled_set: (o, enabled) => {
57
+ let s = this._getWtState(o);
58
+ s.horizontal_plane_detection_enabled = enabled;
59
+ this._notifyOptionsUpdated(s.pipeline);
60
+ },
61
+ world_tracker_vertical_plane_detection_enabled: (o) => {
62
+ let s = this._getWtState(o);
63
+ return s.vertical_plane_detection_enabled;
64
+ },
65
+ world_tracker_vertical_plane_detection_enabled_set: (o, enabled) => {
66
+ let s = this._getWtState(o);
67
+ s.vertical_plane_detection_enabled = enabled;
68
+ this._notifyOptionsUpdated(s.pipeline);
69
+ },
70
+ world_tracker_plane_anchor_count: (o) => {
71
+ let wtData = this._getWtData(o);
72
+ let plane_anchors = wtData === null || wtData === void 0 ? void 0 : wtData.plane_anchors;
73
+ if (!plane_anchors)
74
+ return 0;
75
+ return plane_anchors.length;
76
+ },
77
+ world_tracker_plane_anchor_id: (o, indx) => {
78
+ var _a, _b, _c;
79
+ let wtData = this._getWtData(o);
80
+ return (_c = (_b = (_a = wtData === null || wtData === void 0 ? void 0 : wtData.plane_anchors) === null || _a === void 0 ? void 0 : _a[indx]) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : "";
81
+ },
82
+ world_tracker_plane_anchor_pose_raw: (o, indx) => {
83
+ var _a, _b;
84
+ let wtData = this._getWtData(o);
85
+ let pose = (_b = (_a = wtData === null || wtData === void 0 ? void 0 : wtData.plane_anchors) === null || _a === void 0 ? void 0 : _a[indx]) === null || _b === void 0 ? void 0 : _b.pose;
86
+ if (pose === undefined)
87
+ return this._identityPose;
88
+ return pose;
89
+ },
90
+ world_tracker_plane_anchor_status: (o, indx) => {
91
+ var _a, _b, _c, _d;
92
+ return (_d = (_c = (_b = (_a = this._getWtData(o)) === null || _a === void 0 ? void 0 : _a.plane_anchors) === null || _b === void 0 ? void 0 : _b[indx]) === null || _c === void 0 ? void 0 : _c.status) !== null && _d !== void 0 ? _d : anchor_status_t.ANCHOR_STATUS_STOPPED;
93
+ },
94
+ world_tracker_plane_anchor_polygon_data_size: (o, indx) => {
95
+ var _a, _b, _c, _d, _e;
96
+ return (_e = (_d = (_c = (_b = (_a = this._getWtData(o)) === null || _a === void 0 ? void 0 : _a.plane_anchors) === null || _b === void 0 ? void 0 : _b[indx]) === null || _c === void 0 ? void 0 : _c.boundary) === null || _d === void 0 ? void 0 : _d.length) !== null && _e !== void 0 ? _e : 0;
97
+ },
98
+ world_tracker_plane_anchor_polygon_data: (o, indx) => {
99
+ var _a, _b, _c;
100
+ let boundary = (_c = (_b = (_a = this._getWtData(o)) === null || _a === void 0 ? void 0 : _a.plane_anchors) === null || _b === void 0 ? void 0 : _b[indx]) === null || _c === void 0 ? void 0 : _c.boundary;
101
+ if (!boundary)
102
+ return this._emptyArray;
103
+ return boundary;
104
+ },
105
+ world_tracker_plane_anchor_polygon_version: (o, indx) => {
106
+ var _a, _b, _c, _d;
107
+ return (_d = (_c = (_b = (_a = this._getWtData(o)) === null || _a === void 0 ? void 0 : _a.plane_anchors) === null || _b === void 0 ? void 0 : _b[indx]) === null || _c === void 0 ? void 0 : _c.boundaryVersion) !== null && _d !== void 0 ? _d : 0;
108
+ },
109
+ world_tracker_plane_anchor_orientation: (o, indx) => {
110
+ var _a, _b, _c, _d;
111
+ return ((_d = (_c = (_b = (_a = this._getWtData(o)) === null || _a === void 0 ? void 0 : _a.plane_anchors) === null || _b === void 0 ? void 0 : _b[indx]) === null || _c === void 0 ? void 0 : _c.horizontal) !== null && _d !== void 0 ? _d : true) ? plane_orientation_t.PLANE_ORIENTATION_HORIZONTAL : plane_orientation_t.PLANE_ORIENTATION_VERTICAL;
112
+ },
113
+ world_tracker_world_anchor_status: (o) => {
114
+ var _a, _b;
115
+ return (_b = (_a = this._getWtData(o)) === null || _a === void 0 ? void 0 : _a.world_anchor_status) !== null && _b !== void 0 ? _b : anchor_status_t.ANCHOR_STATUS_INITIALIZING;
116
+ },
117
+ world_tracker_world_anchor_id: (o) => {
118
+ let s = this._getWtState(o);
119
+ return s.world_anchor_id;
120
+ },
121
+ world_tracker_world_anchor_pose_raw: (o) => {
122
+ let wtData = this._getWtData(o);
123
+ let pose = wtData === null || wtData === void 0 ? void 0 : wtData.world_anchor_pose;
124
+ if (pose === undefined)
125
+ return this._identityPose;
126
+ return pose;
127
+ },
128
+ world_tracker_ground_anchor_status: (o) => {
129
+ var _a, _b;
130
+ return (_b = (_a = this._getWtData(o)) === null || _a === void 0 ? void 0 : _a.ground_anchor_status) !== null && _b !== void 0 ? _b : anchor_status_t.ANCHOR_STATUS_INITIALIZING;
131
+ },
132
+ world_tracker_ground_anchor_id: (o) => {
133
+ let s = this._getWtState(o);
134
+ return s.ground_anchor_id;
135
+ },
136
+ world_tracker_ground_anchor_pose_raw: (o) => {
137
+ let wtData = this._getWtData(o);
138
+ let pose = wtData === null || wtData === void 0 ? void 0 : wtData.ground_anchor_pose;
139
+ if (pose === undefined)
140
+ return this._identityPose;
141
+ return pose;
142
+ },
143
+ world_tracker_reset: (o) => {
144
+ // Reset local pose_version state
145
+ for (const entry of this._anchorStateById.values()) {
146
+ if (entry.wt === o)
147
+ entry.pose_version = 0;
148
+ }
149
+ let s = this._getWtState(o);
150
+ let source = s.pipeline.currentCameraSource;
151
+ if (source instanceof WebXRCameraSource) {
152
+ source.resetTracking();
153
+ }
154
+ },
155
+ world_tracker_tracks_data_enabled: (o) => {
156
+ let s = this._getWtState(o);
157
+ return s.tracks_data_enabled;
158
+ },
159
+ world_tracker_tracks_data_enabled_set: (o, tracks_data_enabled) => {
160
+ let s = this._getWtState(o);
161
+ s.tracks_data_enabled = tracks_data_enabled;
162
+ this._notifyOptionsUpdated(s.pipeline);
163
+ },
164
+ world_tracker_tracks_data_size: (o) => {
165
+ // Stub
166
+ let s = this._getWtState(o);
167
+ return 0;
168
+ },
169
+ world_tracker_tracks_data: (o) => {
170
+ // Stub
171
+ let s = this._getWtState(o);
172
+ return this._emptyArray;
173
+ },
174
+ world_tracker_tracks_type_data_size: (o) => {
175
+ // Stub
176
+ let s = this._getWtState(o);
177
+ return 0;
178
+ },
179
+ world_tracker_tracks_type_data: (o) => {
180
+ // Stub
181
+ return _emptyUint8Array;
182
+ },
183
+ world_tracker_projections_data_enabled: (o) => {
184
+ let s = this._getWtState(o);
185
+ return s.projections_data_enabled;
186
+ },
187
+ world_tracker_projections_data_enabled_set: (o, projections_data_enabled) => {
188
+ let s = this._getWtState(o);
189
+ s.projections_data_enabled = projections_data_enabled;
190
+ this._notifyOptionsUpdated(s.pipeline);
191
+ },
192
+ world_tracker_projections_data_size: (o) => {
193
+ // Stub
194
+ let s = this._getWtState(o);
195
+ return 0;
196
+ },
197
+ world_tracker_projections_data: (o) => {
198
+ // Stub
199
+ let s = this._getWtState(o);
200
+ return this._emptyArray;
201
+ },
202
+ world_tracker_session_number: (o) => {
203
+ var _a, _b;
204
+ let s = this._getWtState(o);
205
+ let source = (_b = (_a = s.pipeline) === null || _a === void 0 ? void 0 : _a.getCurrentCameraInfo()) === null || _b === void 0 ? void 0 : _b.cameraSource;
206
+ if (source instanceof WebXRCameraSource) {
207
+ return source.sessionNumber;
208
+ }
209
+ return 0;
210
+ },
211
+ world_tracker_scale_mode_set: (o, mode) => {
212
+ // Stub - no-op
213
+ },
214
+ world_tracker_scale_mode: (o) => {
215
+ // Stub - no-op
216
+ return world_scale_mode_t.DEFAULT;
217
+ },
218
+ // #### custom_anchor ####
219
+ custom_anchor_create: (pipeline, worldtracker, id) => {
220
+ let newId = (this._latestId++);
221
+ let wtState = this._getWtState(worldtracker);
222
+ let s = {
223
+ pipeline: wtState.pipeline,
224
+ wt: worldtracker,
225
+ pose_version: 0,
226
+ anchor_id: id,
227
+ };
228
+ this._anchorStateById.set(newId, s);
229
+ return newId;
230
+ },
231
+ custom_anchor_destroy: (o) => {
232
+ var _a, _b;
233
+ let s = this._getAnchorState(o);
234
+ this._anchorStateById.delete(o);
235
+ let source = (_b = (_a = s.pipeline) === null || _a === void 0 ? void 0 : _a.getCurrentCameraInfo()) === null || _b === void 0 ? void 0 : _b.cameraSource;
236
+ if (source instanceof WebXRCameraSource) {
237
+ source.deleteCustomAnchor(o);
238
+ }
239
+ },
240
+ custom_anchor_status: (o) => {
241
+ var _a, _b;
242
+ let s = this._getAnchorState(o);
243
+ let wtData = this._getWtDataFromPipeline(s.pipeline);
244
+ const anchorData = ((_a = wtData === null || wtData === void 0 ? void 0 : wtData.custom_anchors) !== null && _a !== void 0 ? _a : []).find(entry => entry.id === o);
245
+ return (_b = anchorData === null || anchorData === void 0 ? void 0 : anchorData.status) !== null && _b !== void 0 ? _b : anchor_status_t.ANCHOR_STATUS_INITIALIZING;
246
+ },
247
+ custom_anchor_pose_version: (o) => {
248
+ var _a, _b;
249
+ let s = this._getAnchorState(o);
250
+ let wtData = this._getWtDataFromPipeline(s.pipeline);
251
+ const anchorData = ((_a = wtData === null || wtData === void 0 ? void 0 : wtData.custom_anchors) !== null && _a !== void 0 ? _a : []).find(entry => entry.id === o);
252
+ return (_b = anchorData === null || anchorData === void 0 ? void 0 : anchorData.poseVersion) !== null && _b !== void 0 ? _b : -1;
253
+ },
254
+ custom_anchor_pose_raw: (o) => {
255
+ var _a;
256
+ let s = this._getAnchorState(o);
257
+ let wtData = this._getWtDataFromPipeline(s.pipeline);
258
+ const anchorData = ((_a = wtData === null || wtData === void 0 ? void 0 : wtData.custom_anchors) !== null && _a !== void 0 ? _a : []).find(entry => entry.id === o);
259
+ if (!anchorData)
260
+ return this._identityPose;
261
+ return anchorData.pose;
262
+ },
263
+ custom_anchor_pose_set: (o, pose) => {
264
+ var _a, _b;
265
+ let s = this._getAnchorState(o);
266
+ s.pose_version++;
267
+ let source = (_b = (_a = s.pipeline) === null || _a === void 0 ? void 0 : _a.getCurrentCameraInfo()) === null || _b === void 0 ? void 0 : _b.cameraSource;
268
+ if (source instanceof WebXRCameraSource) {
269
+ source.setCustomAnchorPose(o, s.pose_version, pose);
270
+ }
271
+ },
272
+ custom_anchor_pose_set_with_parent: (o, pose, anchor_id) => {
273
+ var _a, _b;
274
+ (_b = (_a = this.impl).custom_anchor_pose_set) === null || _b === void 0 ? void 0 : _b.call(_a, o, pose);
275
+ },
276
+ };
277
+ }
278
+ static SharedInstance() {
279
+ if (!this._sharedInstance) {
280
+ this._sharedInstance = new WebXRWorldTracker();
281
+ }
282
+ return this._sharedInstance;
283
+ }
284
+ _getWtState(o) {
285
+ let s = this._wtStateById.get(o);
286
+ if (!s)
287
+ throw new Error("This object has been destroyed");
288
+ return s;
289
+ }
290
+ _getAnchorState(o) {
291
+ let s = this._anchorStateById.get(o);
292
+ if (!s)
293
+ throw new Error("This object has been destroyed");
294
+ return s;
295
+ }
296
+ _getWtDataFromPipeline(p) {
297
+ var _a, _b;
298
+ return (_b = (_a = p === null || p === void 0 ? void 0 : p.getCurrentCameraInfo()) === null || _a === void 0 ? void 0 : _a.cameraSourceData) === null || _b === void 0 ? void 0 : _b.wtData;
299
+ }
300
+ _getWtData(o) {
301
+ let s = this._getWtState(o);
302
+ return this._getWtDataFromPipeline(s.pipeline);
303
+ }
304
+ // private _getFrameData(o : zappar_world_tracker_t): ParsedBridgeArrayBuffer | undefined {
305
+ // let s = this._getWtState(o);
306
+ // return s?.pipeline?.getCurrentCameraInfo()?.cameraSourceData?.frameData;
307
+ // }
308
+ _notifyOptionsUpdated(p) {
309
+ if (p.currentCameraSource instanceof WebXRCameraSource) {
310
+ p.currentCameraSource.optionsUpdated(this.getCombinedOptionsForPipeline(p));
311
+ }
312
+ }
313
+ getCombinedOptionsForPipeline(p) {
314
+ const options = {
315
+ horizontalPlaneDetection: false,
316
+ verticalPlaneDetection: false
317
+ };
318
+ for (let state of this._wtStateById.values()) {
319
+ if (state.pipeline != p)
320
+ continue;
321
+ if (!state.enabled)
322
+ continue;
323
+ if (state.horizontal_plane_detection_enabled)
324
+ options.horizontalPlaneDetection = true;
325
+ if (state.vertical_plane_detection_enabled)
326
+ options.verticalPlaneDetection = true;
327
+ }
328
+ return options;
329
+ }
330
+ }
331
+ WebXRWorldTracker._sharedInstance = null;
@@ -34,13 +34,23 @@ export function handleImageBitmap(m, r, server, mgr) {
34
34
  }
35
35
  if (!texture)
36
36
  return;
37
- const [dataWidth, dataHeight] = profile.getDataSize(m.cp);
37
+ let [dataWidth, dataHeight] = profile.getDataSize(m.cp);
38
+ let isPortrait = (m.r == 90) || (m.r == 270);
39
+ let ibWidth = isPortrait ? m.i.height : m.i.width;
40
+ let ibHeight = isPortrait ? m.i.width : m.i.height;
41
+ let cropHeight = Math.round(dataWidth * ibHeight / ibWidth);
42
+ if (cropHeight < dataHeight) {
43
+ dataHeight = cropHeight;
44
+ }
45
+ else {
46
+ dataWidth = Math.round(dataHeight * ibWidth / ibHeight);
47
+ }
38
48
  if (!pixels || pixels.byteLength < dataWidth * dataHeight) {
39
49
  // console.log("Generating pixel buffer", profile.dataWidth * profile.dataHeight);
40
50
  pixels = new ArrayBuffer(dataWidth * dataHeight);
41
51
  }
42
- imageProcessor.uploadFrame(texture, m.i, m.r, m.userFacing, m.cp);
43
- let info = imageProcessor.readFrame(texture, pixels, m.cp);
52
+ imageProcessor.uploadFrameForSize(texture, m.i, m.r, m.userFacing, dataWidth, dataHeight);
53
+ let info = imageProcessor.readFrameForSize(texture, pixels, dataWidth, dataHeight);
44
54
  let msg = {
45
55
  t: "imageBitmapS2C",
46
56
  dataWidth: info.dataWidth,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zappar/zappar-cv",
3
- "version": "3.6.0-beta.0",
3
+ "version": "3.7.0-beta.0",
4
4
  "description": "Zappar's core computer vision library, supporting image, face and world tracking.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -33,6 +33,7 @@
33
33
  "@types/offscreencanvas": "2019.6.4",
34
34
  "@types/pngjs": "6.0.0",
35
35
  "@types/ua-parser-js": "0.7.33",
36
+ "@types/webxr": "0.5.22",
36
37
  "@zappar/zapworks-cli": "2.0.6",
37
38
  "better-npm-audit": "^3.7.3",
38
39
  "dat.gui": "0.7.7",