2020-06-03 22:39:30 -04:00
|
|
|
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
|
|
|
|
|
|
2020-02-29 20:13:49 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
// this code runs in kiri's main loop
|
2020-12-31 23:22:39 -05:00
|
|
|
let KIRI = self.kiri = self.kiri || {},
|
|
|
|
|
loc = self.location,
|
2020-02-29 20:13:49 -05:00
|
|
|
host = loc.hostname,
|
|
|
|
|
port = loc.port,
|
|
|
|
|
proto = loc.protocol,
|
|
|
|
|
time = function() { return new Date().getTime() },
|
|
|
|
|
seqid = 1,
|
2020-11-30 22:36:06 -05:00
|
|
|
syncd = {},
|
2020-02-29 20:13:49 -05:00
|
|
|
running = {},
|
|
|
|
|
slicing = {},
|
|
|
|
|
worker = null;
|
|
|
|
|
|
2020-11-03 11:19:13 -05:00
|
|
|
/**
|
|
|
|
|
* @param {Function} fn name of function in KIRI.worker
|
|
|
|
|
* @param {Object} data to send to server
|
|
|
|
|
* @param {Function} onreply function to call on reply messages
|
|
|
|
|
* @param {Object[]} zerocopy array of objects to pass using zerocopy
|
|
|
|
|
*/
|
2020-11-06 10:47:49 -05:00
|
|
|
function send(fn, data, onreply, zerocopy) {
|
2020-02-29 20:13:49 -05:00
|
|
|
let seq = seqid++;
|
|
|
|
|
|
2020-11-06 10:47:49 -05:00
|
|
|
running[seq] = { fn:onreply };
|
2020-04-19 13:39:46 -04:00
|
|
|
// console.log('send', data);
|
2020-11-06 10:47:49 -05:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
worker.postMessage({
|
|
|
|
|
seq: seq,
|
|
|
|
|
task: fn,
|
|
|
|
|
time: time(),
|
|
|
|
|
data: data
|
|
|
|
|
}, zerocopy);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.trace('work send error', {data, error});
|
|
|
|
|
}
|
2020-02-29 20:13:49 -05:00
|
|
|
}
|
2017-04-18 17:09:33 -04:00
|
|
|
|
2020-11-03 11:19:13 -05:00
|
|
|
// code is running in the browser / client context
|
2020-11-30 18:01:32 -05:00
|
|
|
const CLIENT =
|
2020-11-03 11:19:13 -05:00
|
|
|
KIRI.client =
|
2020-02-29 20:13:49 -05:00
|
|
|
KIRI.work = {
|
2020-11-03 11:19:13 -05:00
|
|
|
send: send,
|
|
|
|
|
|
2020-06-15 22:47:11 -04:00
|
|
|
newWorker: function() {
|
|
|
|
|
if (self.createWorker) {
|
|
|
|
|
return self.createWorker();
|
|
|
|
|
} else {
|
|
|
|
|
return new Worker(`/code/worker.js?${self.kiri.version}`);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-11-26 23:18:28 -05:00
|
|
|
isSlicing: function() {
|
2020-02-29 20:13:49 -05:00
|
|
|
let current = 0;
|
|
|
|
|
for (let key in slicing) {
|
|
|
|
|
current++;
|
|
|
|
|
}
|
|
|
|
|
return current > 0;
|
|
|
|
|
},
|
2017-04-18 17:09:33 -04:00
|
|
|
|
2020-11-26 23:18:28 -05:00
|
|
|
restart: function() {
|
2020-02-29 20:13:49 -05:00
|
|
|
if (worker) {
|
|
|
|
|
worker.terminate();
|
|
|
|
|
}
|
2017-04-18 17:09:33 -04:00
|
|
|
|
2020-02-29 20:13:49 -05:00
|
|
|
for (let key in slicing) {
|
|
|
|
|
slicing[key]({error: "cancelled slicing"});
|
|
|
|
|
}
|
2017-04-18 17:09:33 -04:00
|
|
|
|
2020-02-29 20:13:49 -05:00
|
|
|
slicing = {};
|
|
|
|
|
running = {};
|
2020-06-15 22:47:11 -04:00
|
|
|
worker = KIRI.work.newWorker();
|
2018-03-14 17:47:40 -04:00
|
|
|
|
2020-02-29 20:13:49 -05:00
|
|
|
worker.onmessage = function(e) {
|
|
|
|
|
let now = time(),
|
|
|
|
|
reply = e.data,
|
|
|
|
|
record = running[reply.seq],
|
|
|
|
|
onreply = record.fn;
|
2020-06-15 22:47:11 -04:00
|
|
|
|
|
|
|
|
// console.log('recv', reply.data)
|
2020-02-29 20:13:49 -05:00
|
|
|
if (reply.done) {
|
|
|
|
|
delete running[reply.seq];
|
2018-03-08 10:42:26 -05:00
|
|
|
}
|
|
|
|
|
|
2020-02-29 20:13:49 -05:00
|
|
|
// calculate and replace recv time
|
|
|
|
|
reply.time_recv = now - reply.time_recv;
|
|
|
|
|
|
|
|
|
|
onreply(reply.data, reply);
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
2020-11-26 23:18:28 -05:00
|
|
|
decimate: function(vertices, options, callback) {
|
2020-11-06 10:47:49 -05:00
|
|
|
let alert = KIRI.api.show.alert('processing model', 1000);
|
2020-02-29 20:13:49 -05:00
|
|
|
vertices = vertices.buffer.slice(0);
|
2020-11-06 10:47:49 -05:00
|
|
|
send("decimate", {vertices, options}, function(output) {
|
2020-06-20 20:16:06 -04:00
|
|
|
KIRI.api.hide.alert(alert);
|
2020-02-29 20:13:49 -05:00
|
|
|
callback(output);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2020-11-26 23:18:28 -05:00
|
|
|
config: function(obj) {
|
2020-09-14 09:43:34 -04:00
|
|
|
send("config", obj, function(reply) {
|
|
|
|
|
// console.log({config:reply});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2020-11-26 23:18:28 -05:00
|
|
|
clear: function(widget) {
|
2020-02-29 20:13:49 -05:00
|
|
|
send("clear", widget ? {id:widget.id} : {}, function(reply) {
|
|
|
|
|
// console.log({clear:reply});
|
2020-11-30 22:36:06 -05:00
|
|
|
syncd = {};
|
2020-02-29 20:13:49 -05:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2020-11-26 23:18:28 -05:00
|
|
|
snap: function(data) {
|
2020-04-15 21:44:37 -04:00
|
|
|
send("snap", data, function(reply) {
|
|
|
|
|
// console.log({snap:reply})
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2020-11-30 18:01:32 -05:00
|
|
|
// widget sync
|
|
|
|
|
sync: function(widget) {
|
|
|
|
|
let widgets = widget ? [ widget ] : KIRI.api.widgets.all();
|
|
|
|
|
widgets.forEach(widget => {
|
2020-11-30 22:36:06 -05:00
|
|
|
if (widget.modified || !syncd[widget.id]) {
|
|
|
|
|
syncd[widget.id] = true;
|
2020-11-30 18:01:32 -05:00
|
|
|
let vertices = widget.getGeoVertices().buffer.slice(0);
|
|
|
|
|
send("sync", {
|
|
|
|
|
id: widget.id,
|
|
|
|
|
vertices: vertices,
|
|
|
|
|
position: widget.mesh.position,
|
|
|
|
|
tracking: widget.track
|
|
|
|
|
}, done => {
|
|
|
|
|
widget.modified = false;
|
|
|
|
|
}, [vertices]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2020-11-26 23:18:28 -05:00
|
|
|
slice: function(settings, widget, callback) {
|
2020-11-30 18:01:32 -05:00
|
|
|
CLIENT.sync(widget);
|
2020-02-29 20:13:49 -05:00
|
|
|
slicing[widget.id] = callback;
|
2020-12-04 19:44:31 -05:00
|
|
|
|
2020-02-29 20:13:49 -05:00
|
|
|
send("slice", {
|
|
|
|
|
id: widget.id,
|
2020-12-04 19:44:31 -05:00
|
|
|
settings: settings
|
2020-02-29 20:13:49 -05:00
|
|
|
}, function(reply) {
|
2020-11-08 13:04:54 -05:00
|
|
|
if (reply.done || reply.error) {
|
|
|
|
|
delete slicing[widget.id];
|
|
|
|
|
}
|
2020-02-29 20:13:49 -05:00
|
|
|
callback(reply);
|
2020-11-30 18:01:32 -05:00
|
|
|
});
|
2020-02-29 20:13:49 -05:00
|
|
|
},
|
|
|
|
|
|
2020-11-26 23:18:28 -05:00
|
|
|
prepare: function(settings, update, done) {
|
2020-11-09 13:57:18 -05:00
|
|
|
send("prepare", { settings }, function(reply) {
|
2020-11-08 13:04:54 -05:00
|
|
|
if (reply.progress) {
|
2020-11-26 09:10:20 -05:00
|
|
|
update(reply.progress, reply.message, reply.layer);
|
2020-11-08 13:04:54 -05:00
|
|
|
}
|
|
|
|
|
if (reply.done) {
|
2020-11-11 08:07:02 -05:00
|
|
|
done(reply.output, reply.maxSpeed);
|
2020-11-08 13:04:54 -05:00
|
|
|
}
|
2020-11-06 10:47:49 -05:00
|
|
|
});
|
2020-02-29 20:13:49 -05:00
|
|
|
},
|
|
|
|
|
|
2020-11-26 23:18:28 -05:00
|
|
|
export: function(settings, online, ondone) {
|
2020-11-09 13:57:18 -05:00
|
|
|
send("export", { settings }, function(reply) {
|
2020-04-14 09:26:59 -04:00
|
|
|
if (reply.line) {
|
|
|
|
|
online(reply.line);
|
|
|
|
|
}
|
|
|
|
|
if (reply.done) {
|
2020-11-08 17:35:40 -05:00
|
|
|
ondone(reply.output);
|
2020-04-14 09:26:59 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2020-11-26 23:18:28 -05:00
|
|
|
colors: function(colors, max, done) {
|
2020-11-11 08:07:02 -05:00
|
|
|
send("colors", { colors, max }, function(reply) {
|
|
|
|
|
done(reply);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2020-11-26 23:18:28 -05:00
|
|
|
parse: function(args, progress, done) {
|
2020-11-09 23:01:41 -05:00
|
|
|
// have to do this client side because DOMParser is not in workers
|
|
|
|
|
if (args.type === 'svg') {
|
|
|
|
|
const { settings, code, type } = args;
|
|
|
|
|
const origin = settings.origin;
|
|
|
|
|
const offset = {
|
|
|
|
|
x: origin.x,
|
|
|
|
|
y: -origin.y,
|
|
|
|
|
z: origin.z
|
|
|
|
|
};
|
|
|
|
|
const output = KIRI.newPrint().parseSVG(code, offset);
|
|
|
|
|
send("parse_svg", output, function(reply) {
|
|
|
|
|
if (reply.parsed) {
|
|
|
|
|
done(KIRI.codec.decode(reply.parsed));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-11-09 13:57:18 -05:00
|
|
|
send("parse", args, function(reply) {
|
|
|
|
|
if (reply.progress) {
|
|
|
|
|
progress(reply);
|
|
|
|
|
}
|
|
|
|
|
if (reply.parsed) {
|
2020-11-13 09:27:42 -05:00
|
|
|
done(KIRI.codec.decode(reply.parsed), reply.maxSpeed);
|
2020-02-29 20:13:49 -05:00
|
|
|
}
|
|
|
|
|
});
|
2020-11-26 23:18:28 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
image2mesh: function(data, progress, output) {
|
|
|
|
|
send("image2mesh", data, reply => {
|
|
|
|
|
if (reply.progress) {
|
|
|
|
|
progress(reply.progress);
|
|
|
|
|
}
|
|
|
|
|
if (reply.done) {
|
|
|
|
|
output(reply.done);
|
|
|
|
|
}
|
|
|
|
|
}, [ data.png ]);
|
2020-02-29 20:13:49 -05:00
|
|
|
}
|
|
|
|
|
};
|
2017-04-18 17:09:33 -04:00
|
|
|
|
2020-02-29 20:13:49 -05:00
|
|
|
// start worker
|
|
|
|
|
KIRI.work.restart();
|