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
|
|
|
|
|
if (!self.kiri) self.kiri = {};
|
|
|
|
|
|
|
|
|
|
let loc = self.location,
|
|
|
|
|
host = loc.hostname,
|
|
|
|
|
port = loc.port,
|
|
|
|
|
proto = loc.protocol,
|
|
|
|
|
time = function() { return new Date().getTime() },
|
|
|
|
|
KIRI = self.kiri,
|
|
|
|
|
BASE = self.base,
|
|
|
|
|
seqid = 1,
|
|
|
|
|
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
|
|
|
|
|
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-02-29 20:13:49 -05:00
|
|
|
isSlicing : function() {
|
|
|
|
|
let current = 0;
|
|
|
|
|
for (let key in slicing) {
|
|
|
|
|
current++;
|
|
|
|
|
}
|
|
|
|
|
return current > 0;
|
|
|
|
|
},
|
2017-04-18 17:09:33 -04:00
|
|
|
|
2020-02-29 20:13:49 -05:00
|
|
|
restart : function() {
|
|
|
|
|
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-06 10:47:49 -05:00
|
|
|
decimate : function(vertices, options, callback) {
|
|
|
|
|
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-09-14 09:43:34 -04:00
|
|
|
config : function(obj) {
|
|
|
|
|
send("config", obj, function(reply) {
|
|
|
|
|
// console.log({config:reply});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2020-02-29 20:13:49 -05:00
|
|
|
clear : function(widget) {
|
|
|
|
|
send("clear", widget ? {id:widget.id} : {}, function(reply) {
|
|
|
|
|
// console.log({clear:reply});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2020-04-15 21:44:37 -04:00
|
|
|
snap : function(data) {
|
|
|
|
|
send("snap", data, function(reply) {
|
|
|
|
|
// console.log({snap:reply})
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2020-02-29 20:13:49 -05:00
|
|
|
slice : function(settings, widget, callback) {
|
2020-05-06 14:04:43 -04:00
|
|
|
let rotation = (Math.PI/180) * (settings.process.sliceRotation || 0);
|
|
|
|
|
let centerz;
|
|
|
|
|
let movez;
|
|
|
|
|
if (rotation) {
|
|
|
|
|
let bbox1 = widget.getBoundingBox(true);
|
|
|
|
|
widget._rotate(0,rotation,0,true);
|
|
|
|
|
widget.center();
|
|
|
|
|
let bbox2 = widget.getBoundingBox(true);
|
|
|
|
|
centerz = (bbox2.max.z - bbox2.min.z)/2;
|
|
|
|
|
movez = centerz - (bbox1.max.z - bbox1.min.z)/2;
|
|
|
|
|
}
|
2020-06-15 22:47:11 -04:00
|
|
|
let vertices = widget.getGeoVertices().buffer.slice(0);
|
|
|
|
|
// snapshot = KIRI.api.view.snapshot;
|
2020-05-06 14:04:43 -04:00
|
|
|
if (rotation) {
|
|
|
|
|
widget._rotate(0,-rotation,0,true);
|
|
|
|
|
widget.center();
|
|
|
|
|
}
|
2020-02-29 20:13:49 -05:00
|
|
|
slicing[widget.id] = callback;
|
|
|
|
|
send("slice", {
|
|
|
|
|
id: widget.id,
|
|
|
|
|
settings: settings,
|
|
|
|
|
vertices: vertices,
|
2020-04-15 21:44:37 -04:00
|
|
|
position: widget.mesh.position,
|
2020-04-19 21:32:06 -04:00
|
|
|
tracking: widget.track,
|
2020-06-15 22:47:11 -04:00
|
|
|
// snapshot: snapshot,
|
2020-05-06 14:04:43 -04:00
|
|
|
// for rotation / unrotation
|
|
|
|
|
state: {
|
|
|
|
|
rotation: rotation,
|
|
|
|
|
centerz: centerz,
|
|
|
|
|
movez: movez
|
|
|
|
|
}
|
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-06 10:47:49 -05:00
|
|
|
}, [vertices]);
|
2020-02-29 20:13:49 -05:00
|
|
|
},
|
|
|
|
|
|
2020-11-08 13:04:54 -05:00
|
|
|
prepare : function(settings, update, done) {
|
|
|
|
|
send("prepare", {settings:settings}, function(reply) {
|
|
|
|
|
if (reply.progress) {
|
|
|
|
|
update(reply.progress, reply.message);
|
|
|
|
|
}
|
|
|
|
|
if (reply.done) {
|
|
|
|
|
done(reply.output);
|
|
|
|
|
}
|
2020-11-06 10:47:49 -05:00
|
|
|
});
|
2020-02-29 20:13:49 -05:00
|
|
|
},
|
|
|
|
|
|
2020-11-08 17:35:40 -05:00
|
|
|
export : function(settings, online, ondone) {
|
2020-04-14 09:26:59 -04:00
|
|
|
let lines = [];
|
2020-11-08 17:35:40 -05:00
|
|
|
send("export", {settings: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-08 17:35:40 -05:00
|
|
|
gcode : function(callback) {
|
2020-02-29 20:13:49 -05:00
|
|
|
let gcode = [];
|
|
|
|
|
let start = BASE.util.time();
|
2020-11-08 17:35:40 -05:00
|
|
|
send("gcode", {}, function(reply) {
|
2020-02-29 20:13:49 -05:00
|
|
|
if (reply.line) {
|
|
|
|
|
gcode.push(reply.line);
|
|
|
|
|
} else {
|
|
|
|
|
if (!reply.gcode) reply.gcode = gcode.join("\n");
|
|
|
|
|
// console.log({printGCode:(BASE.util.time() - start)});
|
2017-04-18 17:09:33 -04:00
|
|
|
callback(reply);
|
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();
|