Doodle3D-Slicer/src/slicer.js

145 lines
3.8 KiB
JavaScript
Raw Normal View History

2017-07-18 12:38:03 +02:00
import * as THREE from 'three';
2016-08-19 14:46:02 +02:00
import slice from './sliceActions/slice.js';
import SlicerWorker from './slicer.worker.js';
2015-07-26 15:32:10 +02:00
2017-11-12 00:57:28 +01:00
export function sliceMesh(settings, mesh, sync = false, constructLinePreview = false, onProgress) {
2017-11-12 00:46:30 +01:00
if (!mesh || !mesh.isMesh) {
2017-07-24 13:01:22 +02:00
throw new Error('Provided mesh is not intance of THREE.Mesh');
2017-05-13 14:48:48 +02:00
}
mesh.updateMatrix();
const { geometry, matrix } = mesh;
return sliceGeometry(settings, geometry, matrix, sync, onProgress);
}
2017-05-13 14:48:48 +02:00
2017-11-12 00:57:28 +01:00
export async function sliceGeometry(settings, geometry, matrix, sync = false, constructLinePreview = false, onProgress) {
2017-11-12 00:46:30 +01:00
if (!geometry) {
throw new Error('Missing required geometry argument');
} else if (geometry.isBufferGeometry) {
geometry = new THREE.Geometry().fromBufferGeometry(geometry);
} else if (geometry.isGeometry) {
geometry = geometry.clone();
} else {
throw new Error('Geometry is not an instance of BufferGeometry or Geometry');
}
2017-05-13 14:48:48 +02:00
if (geometry.faces.length === 0) {
throw new Error('Geometry does not contain any data');
}
2017-11-12 00:46:30 +01:00
if (matrix && matrix.isMatrix4) {
geometry.applyMatrix(matrix);
2017-05-13 14:48:48 +02:00
}
2017-07-20 00:05:50 +02:00
2017-11-12 00:57:28 +01:00
let gcode;
if (sync) {
2017-11-12 00:57:28 +01:00
gcode = sliceSync(settings, geometry, onProgress);
} else {
2017-11-12 00:57:28 +01:00
gcode = await sliceAsync(settings, geometry, onProgress);
2017-05-13 14:48:48 +02:00
}
2017-11-12 00:57:28 +01:00
if (constructLinePreview) gcode.linePreview = createGcodeGeometry(gcode.gcode);
gcode.gcode = gcodeToString(gcode.gcode);
return gcode;
}
function sliceSync(settings, geometry, onProgress) {
return slice(settings, geometry, onProgress);
}
2017-05-13 14:48:48 +02:00
function sliceAsync(settings, geometry, onProgress) {
return new Promise((resolve, reject) => {
// create the slicer worker
const slicerWorker = new SlicerWorker();
2017-07-24 15:40:54 +02:00
slicerWorker.onerror = error => {
slicerWorker.terminate();
reject(error);
};
2017-05-13 14:48:48 +02:00
// listen to messages send from worker
slicerWorker.addEventListener('message', (event) => {
const { message, data } = event.data;
switch (message) {
case 'SLICE': {
slicerWorker.terminate();
resolve(data.gcode);
break;
}
case 'PROGRESS': {
if (typeof onProgress !== 'undefined') {
onProgress(data);
2017-07-04 14:19:07 +02:00
}
break;
2017-05-13 14:48:48 +02:00
}
}
});
2017-05-13 14:48:48 +02:00
// send geometry and settings to worker to start the slicing progress
2017-07-24 15:45:18 +02:00
geometry = geometry.toJSON();
slicerWorker.postMessage({
message: 'SLICE',
data: {
settings,
2017-07-24 15:45:18 +02:00
geometry
}
2017-05-13 14:48:48 +02:00
});
});
2015-07-26 15:32:10 +02:00
}
2017-11-12 00:57:28 +01:00
function gcodeToString(gcode) {
const currentValues = {};
return gcode.reduce((string, command) => {
let first = true;
for (const action in command) {
const value = command[action];
const currentValue = currentValues[action];
if (first) {
2017-11-12 01:15:38 +01:00
string += action + value;
2017-11-12 00:57:28 +01:00
first = false;
} else if (currentValue !== value) {
string += ` ${action}${value}`;
currentValues[action] = value;
}
}
string += '\n';
2017-11-12 01:15:38 +01:00
return string;
2017-11-12 00:57:28 +01:00
}, '');
}
const MAX_SPEED = 100 * 60;
function createGcodeGeometry(gcode) {
const geometry = new THREE.Geometry();
let lastPoint
for (let i = 0; i < gcode.length; i ++) {
const { G, F, X, Y, Z } = gcode[i];
if (X || Y || Z) {
const point = new THREE.Vector3(Y, Z, X);
let color;
if (G === 0) {
color = new THREE.Color(0x00ff00);
} else if (G === 1) {
color = new THREE.Color().setHSL(F / MAX_SPEED, 0.5, 0.5);
}
if (G === 1) {
if (lastPoint) geometry.vertices.push(lastPoint);
geometry.vertices.push(new THREE.Vector3(Y, Z, X));
geometry.colors.push(color);
geometry.colors.push(color);
}
lastPoint = point;
}
}
const material = new THREE.LineBasicMaterial({ vertexColors: THREE.VertexColors });
const line = new THREE.LineSegments(geometry, material);
return line;
}