Doodle3D-Slicer/src/slicer.js

111 lines
3.7 KiB
JavaScript
Raw Normal View History

2017-12-06 11:54:09 +01:00
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';
import slice from './sliceActions/slice.js';
import SlicerWorker from './slicer.worker.js';
import { FrontSide, DoubleSide } from 'three/src/constants.js';
import { BufferGeometry } from 'three/src/core/BufferGeometry.js'
export function sliceMesh(settings, mesh, sync = false, constructLinePreview = false, onProgress) {
if (!mesh || !mesh.isMesh) {
throw new Error('Provided mesh is not intance of THREE.Mesh');
}
mesh.updateMatrix();
const { geometry, matrix, material } = mesh;
return sliceGeometry(settings, geometry, material, matrix, sync, constructLinePreview, onProgress);
}
export function sliceGeometry(settings, geometry, materials, matrix, sync = false, constructLinePreview = false, onProgress) {
if (!geometry) {
throw new Error('Missing required geometry argument');
} else if (geometry.isBufferGeometry) {
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');
}
if (geometry.faces.length === 0) {
throw new Error('Geometry does not contain any data');
}
if (matrix && matrix.isMatrix4) {
geometry.applyMatrix(matrix);
}
const openObjectIndexes = materials instanceof Array ? materials.map(({ side }) => {
switch (side) {
case FrontSide:
return false;
case DoubleSide:
return true;
default:
return false;
}
}) : [false];
2015-07-26 15:32:10 +02:00
if (sync) {
return sliceSync(settings, geometry, openObjectIndexes, constructLinePreview, onProgress);
} else {
return sliceAsync(settings, geometry, openObjectIndexes, constructLinePreview, onProgress);
2017-05-13 14:48:48 +02:00
}
}
function sliceSync(settings, geometry, openObjectIndexes, constructLinePreview, onProgress) {
return slice(settings, geometry, openObjectIndexes, constructLinePreview, onProgress);
}
2017-05-13 14:48:48 +02:00
function sliceAsync(settings, geometry, openObjectIndexes, 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
geometry = geometry.toJSON();
slicerWorker.postMessage({
message: 'SLICE',
data: { settings, geometry, openObjectIndexes, constructLinePreview }
2017-05-13 14:48:48 +02:00
});
});
2015-07-26 15:32:10 +02:00
}