Doodle3D-Slicer/src/slicer.js

104 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-12-06 11:54:09 +01:00
import { Geometry } from 'three/src/core/Geometry.js';
import { BufferGeometry } from 'three/src/core/BufferGeometry.js';
import { VertexColors } from 'three/src/constants.js';
import { BufferAttribute } from 'three/src/core/BufferAttribute.js';
import { LineBasicMaterial } from 'three/src/materials/LineBasicMaterial.js';
import { LineSegments } from 'three/src/objects/LineSegments.js';
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 11:53:45 +01:00
export 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) {
2017-12-06 11:54:09 +01:00
geometry = new 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
if (sync) {
2017-11-12 11:53:45 +01:00
return sliceSync(settings, geometry, constructLinePreview, onProgress);
} else {
2017-11-12 11:53:45 +01:00
return sliceAsync(settings, geometry, constructLinePreview, onProgress);
2017-05-13 14:48:48 +02:00
}
}
2017-11-12 11:53:45 +01:00
function sliceSync(settings, geometry, constructLinePreview, onProgress) {
return slice(settings, geometry, constructLinePreview, onProgress);
}
2017-05-13 14:48:48 +02:00
2017-11-12 11:53:45 +01:00
function sliceAsync(settings, geometry, constructLinePreview, 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();
2017-11-12 11:53:45 +01:00
if (data.gcode.linePreview) {
2017-12-06 11:54:09 +01:00
const geometry = new BufferGeometry();
2017-11-12 11:53:45 +01:00
const { position, color } = data.gcode.linePreview;
2017-12-06 11:54:09 +01:00
geometry.addAttribute('position', new BufferAttribute(new Float32Array(position), 3));
geometry.addAttribute('color', new BufferAttribute(new Float32Array(color), 3));
2017-11-12 11:53:45 +01:00
2017-12-06 11:54:09 +01:00
const material = new LineBasicMaterial({ vertexColors: VertexColors });
const linePreview = new LineSegments(geometry, material);
2017-11-12 11:53:45 +01:00
data.gcode.linePreview = linePreview;
}
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-11-12 11:53:45 +01:00
geometry,
constructLinePreview
}
2017-05-13 14:48:48 +02:00
});
});
2015-07-26 15:32:10 +02:00
}