Doodle3D-Slicer/src/sliceActions/slice.js

140 lines
4.2 KiB
JavaScript
Raw Normal View History

2017-11-12 11:53:45 +01:00
import * as THREE from 'three';
import calculateLayersIntersections from './calculateLayersIntersections.js';
import createLines from './createLines.js';
import generateInfills from './generateInfills.js';
import generateInnerLines from './generateInnerLines.js';
2017-07-19 17:33:55 +02:00
import generateOutlines from './generateOutlines.js';
import generateSupport from './generateSupport.js';
import intersectionsToShapes from './intersectionsToShapes.js';
import addBrim from './addBrim.js';
import optimizePaths from './optimizePaths.js';
import shapesToSlices from './shapesToSlices.js';
import slicesToGCode from './slicesToGCode.js';
import detectOpenClosed from './detectOpenClosed.js';
import applyPrecision from './applyPrecision.js';
2017-08-24 10:55:36 +02:00
// import removePrecision from './removePrecision.js';
2017-11-12 11:53:45 +01:00
export default function(settings, geometry, constructLinePreview, onProgress) {
const totalStages = 12;
2017-07-18 12:26:30 +02:00
let current = -1;
const updateProgress = (action) => {
current ++;
if (typeof onProgress !== 'undefined') {
onProgress({
progress: {
done: current,
total: totalStages,
action
}
});
}
2017-07-04 14:19:07 +02:00
};
geometry.computeFaceNormals();
// get unique lines from geometry;
2017-07-18 12:26:30 +02:00
updateProgress('Constructing unique lines from geometry');
2017-07-18 10:23:16 +02:00
const lines = createLines(geometry, settings);
2017-07-18 12:26:30 +02:00
updateProgress('Detecting open vs closed shapes');
detectOpenClosed(lines);
2017-07-18 12:26:30 +02:00
updateProgress('Calculating layer intersections');
const {
layerIntersectionIndexes,
layerIntersectionPoints
} = calculateLayersIntersections(lines, settings);
2017-07-18 12:26:30 +02:00
updateProgress('Constructing shapes from intersections');
const shapes = intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings);
applyPrecision(shapes);
2017-07-18 12:26:30 +02:00
updateProgress('Constructing slices from shapes');
const slices = shapesToSlices(shapes, settings);
2017-07-18 12:26:30 +02:00
updateProgress('Generating inner lines');
generateInnerLines(slices, settings);
2017-07-19 17:33:55 +02:00
updateProgress('Generating out lines');
generateOutlines(slices, settings);
2017-07-18 12:26:30 +02:00
updateProgress('Generating infills');
generateInfills(slices, settings);
2017-07-18 12:26:30 +02:00
updateProgress('Generating support');
generateSupport(slices, settings);
2017-07-18 12:26:30 +02:00
updateProgress('Adding brim');
addBrim(slices, settings);
2017-07-18 12:26:30 +02:00
updateProgress('Optimizing paths');
optimizePaths(slices, settings);
2017-07-18 12:26:30 +02:00
2017-08-24 10:55:36 +02:00
// removePrecision(slices);
2017-07-18 12:26:30 +02:00
updateProgress('Constructing gcode');
const gcode = slicesToGCode(slices, settings);
2017-07-18 12:26:30 +02:00
updateProgress('Finished');
2017-11-12 11:53:45 +01:00
if (constructLinePreview) gcode.linePreview = createGcodeGeometry(gcode.gcode);
gcode.gcode = gcodeToString(gcode.gcode);
return gcode;
}
2017-11-12 11:53:45 +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) {
string += action + value;
first = false;
} else if (currentValue !== value) {
string += ` ${action}${value}`;
currentValues[action] = value;
}
}
string += '\n';
return string;
}, '');
}
const MAX_SPEED = 100 * 60;
function createGcodeGeometry(gcode) {
const positions = [];
const colors = [];
let lastPoint
for (let i = 0; i < gcode.length; i ++) {
const { G, F, X, Y, Z } = gcode[i];
if (X || Y || Z) {
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) positions.push(lastPoint[0], lastPoint[1], lastPoint[2]);
positions.push(Y, Z, X);
colors.push(color.r, color.g, color.b);
colors.push(color.r, color.g, color.b);
}
lastPoint = [Y, Z, X];
}
}
const geometry = new THREE.BufferGeometry();
geometry.addAttribute('position', new THREE.BufferAttribute(new Float32Array(positions), 3));
geometry.addAttribute('color', new THREE.BufferAttribute(new Float32Array(colors), 3));
const material = new THREE.LineBasicMaterial({ vertexColors: THREE.VertexColors });
const linePreview = new THREE.LineSegments(geometry, material);
return linePreview;
}