Skip to content

Commit 0720bb4

Browse files
claudekalwalt
authored andcommitted
Fix writeFP/writeFS per-point wasm->JS loop and listener leak
readNFTMarker previously called writeFP/writeFS once per feature point, and each call added a fresh 'imageEv' listener that was never removed. N feature points produced N wasm->JS crossings *and* leaked N listeners, doubling draw work on every subsequent marker load. Populate nftPoints (FP) and the new nftFsetPoints (FS) vectors on the returned nftMarker struct, drop the JS-library callbacks, and draw both sets in a single inline pass after the image is painted. - ARimageFsetDisplay.cpp: add nftFsetPoints field, populate both vectors, remove writeFP/writeFS extern declarations and call sites. - bindings.cpp: expose nftFsetPoints. - js/jslibrary.js: deleted (no callers left). - tools/makem.js: drop the --js-library flag. - src/ARFset.js, js/arfset.api.js: thread nftPoints/nftFsetPoints through the nftMarker CustomEvent and draw them once via a shared helper that also releases the embind vector handle. Closes #18
1 parent 913e176 commit 0720bb4

6 files changed

Lines changed: 57 additions & 110 deletions

File tree

emscripten/ARimageFsetDisplay.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ struct nftMarker {
6666
int imgBWsize;
6767
int nftFeaturePoints;
6868
std::vector<nftPoint> nftPoints;
69+
std::vector<nftPoint> nftFsetPoints;
6970
int pointer;
7071
};
7172

@@ -96,9 +97,6 @@ static int gARFsetID = 0;
9697

9798
extern "C" {
9899

99-
extern void writeFP(int x, int y);
100-
extern void writeFS(int x, int y);
101-
102100
int loadNFTMarker(arFset *arc, int surfaceSetCount,
103101
const char *datasetPathname) {
104102
int i, pageNo, numIset, width, height, dpi;
@@ -189,20 +187,20 @@ nftMarker readNFTMarker(int id, std::string datasetPathname) {
189187
},
190188
arc->id, arc->imgBW, arc->imgBWsize, arc->F_points_NFT);
191189

192-
/*for (int i = 0; i < arc->num_F_points_NFT; i++) {
193-
nft.nftPoints.push_back(nftPoint());
194-
nft.nftPoints[i].x = arc->F_points_NFT->coord[i].x;
195-
nft.nftPoints[i].y = arc->F_points_NFT->coord[i].y;
196-
ARLOGi("points x: %d\n", nft.nftPoints[i].x);
197-
ARLOGi("points y: %d\n", nft.nftPoints[i].y);
198-
}*/
199-
190+
nft.nftPoints.reserve(arc->num_F_points_NFT);
200191
for (int i = 0; i < arc->num_F_points_NFT; i++) {
201-
writeFP(arc->F_points_NFT->coord[i].x, arc->F_points_NFT->coord[i].y);
192+
nftPoint p;
193+
p.x = arc->F_points_NFT->coord[i].x;
194+
p.y = arc->F_points_NFT->coord[i].y;
195+
nft.nftPoints.push_back(p);
202196
}
203197

198+
nft.nftFsetPoints.reserve(arc->refDataSet->num);
204199
for (int i = 0; i < arc->refDataSet->num; i++) {
205-
writeFS(arc->refDataSet->refPoint[i].coord2D.x, arc->refDataSet->refPoint[i].coord2D.y);
200+
nftPoint p;
201+
p.x = arc->refDataSet->refPoint[i].coord2D.x;
202+
p.y = arc->refDataSet->refPoint[i].coord2D.y;
203+
nft.nftFsetPoints.push_back(p);
206204
}
207205

208206
nft.widthNFT = arc->width_NFT;

emscripten/bindings.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ EMSCRIPTEN_BINDINGS(constant_bindings) {
5252
.field("imgBWsize", &nftMarker::imgBWsize)
5353
.field("nftFeaturePoints", &nftMarker::nftFeaturePoints)
5454
.field("nftPoints", &nftMarker::nftPoints)
55+
.field("nftFsetPoints", &nftMarker::nftFsetPoints)
5556
.field("pointer", &nftMarker::pointer);
5657

5758
value_array<nftPoint>("nftPoint")

js/arfset.api.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,13 @@ ARfset.prototype.display = function () {
7979
self.numIset = ev.detail.numIset;
8080
self.imageSetWidth = ev.detail.widthNFT;
8181
self.imageSetHeight = ev.detail.heightNFT;
82-
self.frameFeaturePoints = ev.detail.pointerFeaturePoints;
8382
self.numFpoints = ev.detail.numFpoints;
8483
self.dpi = ev.detail.dpi;
8584
var debugBuffer = new Uint8ClampedArray(
8685
Module.HEAPU8.buffer,
8786
self.frameIbwpointer,
8887
self.frameimgBWsize
8988
);
90-
var pointerFeaturePoints = new Uint16Array(
91-
Module.HEAPU16.buffer,
92-
self.frameFeaturePoints,
93-
self.numFpoints * 2
94-
)
95-
//console.log(pointerFeaturePoints);
9689
var id = new ImageData(
9790
new Uint8ClampedArray(self.canvas.width * self.canvas.height * 4),
9891
self.canvas.width,
@@ -105,16 +98,31 @@ ARfset.prototype.display = function () {
10598
id.data[j + 2] = v;
10699
id.data[j + 3] = 255;
107100
}
108-
101+
109102
self.ctx.putImageData(id, 0, 0);
110103

104+
drawPoints(self.ctx, ev.detail.nftPoints, 10, '#34FF19', 2);
105+
drawPoints(self.ctx, ev.detail.nftFsetPoints, 4, '#FF0119', 1);
106+
111107
var imageEv = new Event('imageEv');
112108
document.dispatchEvent(imageEv);
113-
114-
Module._free(debugBuffer);
115109
})
116110
};
117111

112+
function drawPoints(ctx, points, radius, strokeStyle, lineWidth) {
113+
if (!points) return;
114+
ctx.strokeStyle = strokeStyle;
115+
ctx.lineWidth = lineWidth;
116+
var size = points.size();
117+
for (var i = 0; i < size; i++) {
118+
var p = points.get(i);
119+
ctx.beginPath();
120+
ctx.arc(p[0], p[1], radius, 0, 2 * Math.PI, false);
121+
ctx.stroke();
122+
}
123+
points.delete();
124+
}
125+
118126
ARfset.prototype.loadNFTMarker = function (markerURL, onSuccess, onError) {
119127
var self = this;
120128
if (markerURL) {
@@ -132,7 +140,8 @@ ARfset.prototype.loadNFTMarker = function (markerURL, onSuccess, onError) {
132140
dpi: nftMarker.dpi,
133141
numFpoints: nftMarker.numFpoints,
134142
pointerFeaturePoints: nftMarker.nftFeaturePoints,
135-
nftPoints: nftMarker.nftPoints
143+
nftPoints: nftMarker.nftPoints,
144+
nftFsetPoints: nftMarker.nftFsetPoints
136145
}
137146
});
138147
document.dispatchEvent(nftEvent);

js/jslibrary.js

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/ARFset.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,28 +101,20 @@ export default class ARFset {
101101
}
102102

103103
display () {
104-
105-
106104
var self = this;
107105
document.addEventListener('nftMarker', function(ev) {
108106
self.canvas.width = ev.detail.widthNFT;
109107
self.canvas.height = ev.detail.heightNFT;
110108
self.numIset = ev.detail.numIset;
111109
self.imageSetWidth = ev.detail.widthNFT;
112110
self.imageSetHeight = ev.detail.heightNFT;
113-
self.frameFeaturePoints = ev.detail.pointerFeaturePoints;
114111
self.numFpoints = ev.detail.numFpoints;
115112
self.dpi = ev.detail.dpi;
116113
var debugBuffer = new Uint8ClampedArray(
117114
self.instance.HEAPU8.buffer,
118115
self.frameIbwpointer,
119116
self.frameimgBWsize
120117
);
121-
var pointerFeaturePoints = new Uint16Array(
122-
self.instance.HEAPU16.buffer,
123-
self.frameFeaturePoints,
124-
self.numFpoints * 2
125-
)
126118
var id = new ImageData(
127119
new Uint8ClampedArray(self.canvas.width * self.canvas.height * 4),
128120
self.canvas.width,
@@ -135,16 +127,31 @@ export default class ARFset {
135127
id.data[j + 2] = v;
136128
id.data[j + 3] = 255;
137129
}
138-
130+
139131
self.ctx.putImageData(id, 0, 0);
140-
132+
133+
self._drawPoints(ev.detail.nftPoints, 10, '#34FF19', 2);
134+
self._drawPoints(ev.detail.nftFsetPoints, 4, '#FF0119', 1);
135+
141136
var imageEv = new Event('imageEv');
142137
document.dispatchEvent(imageEv);
143-
144-
self.instance._free(debugBuffer);
145138
})
146139
};
147140

141+
_drawPoints(points, radius, strokeStyle, lineWidth) {
142+
if (!points) return;
143+
this.ctx.strokeStyle = strokeStyle;
144+
this.ctx.lineWidth = lineWidth;
145+
const size = points.size();
146+
for (let i = 0; i < size; i++) {
147+
const p = points.get(i);
148+
this.ctx.beginPath();
149+
this.ctx.arc(p[0], p[1], radius, 0, 2 * Math.PI, false);
150+
this.ctx.stroke();
151+
}
152+
points.delete();
153+
};
154+
148155
async loadNFTMarker (urlOrData) {
149156
let nft = await this.addNFTMarker(this.id, urlOrData)
150157
.then((nftMarker) => {
@@ -159,13 +166,14 @@ export default class ARFset {
159166
dpi: nftMarker.dpi,
160167
numFpoints: nftMarker.numFpoints,
161168
pointerFeaturePoints: nftMarker.nftFeaturePoints,
162-
nftPoints: nftMarker.nftPoints
169+
nftPoints: nftMarker.nftPoints,
170+
nftFsetPoints: nftMarker.nftFsetPoints
163171
}
164172
});
165173
document.dispatchEvent(nftEvent);
166174
this.nftMarkerCount = nftMarker.id + 1;
167175
})
168-
176+
169177
return nft
170178
};
171179

@@ -183,13 +191,14 @@ export default class ARFset {
183191
dpi: nftMarker.dpi,
184192
numFpoints: nftMarker.numFpoints,
185193
pointerFeaturePoints: nftMarker.nftFeaturePoints,
186-
nftPoints: nftMarker.nftPoints
194+
nftPoints: nftMarker.nftPoints,
195+
nftFsetPoints: nftMarker.nftFsetPoints
187196
}
188197
});
189198
document.dispatchEvent(nftEvent);
190199
this.nftMarkerCount = nftMarker.id + 1;
191200
})
192-
201+
193202
return nft
194203
};
195204

tools/makem.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ FLAGS += ' -s USE_LIBJPEG';
171171
FLAGS += ' --memory-init-file 0 '; // for memless file
172172
FLAGS += ' -s "EXPORTED_RUNTIME_METHODS=[\'FS\']"';
173173
FLAGS += ' -s ALLOW_MEMORY_GROWTH=1';
174-
FLAGS += ' --js-library js/jslibrary.js '
175174

176175
var PRE_FLAGS = ' --pre-js ' + path.resolve(__dirname, '../js/arfset.api.js') +' ';
177176

0 commit comments

Comments
 (0)