updated webworkers

This commit is contained in:
casperlamboo 2015-07-29 16:07:52 +02:00
parent adafc44dc1
commit d4f6776aa1
3 changed files with 49 additions and 52 deletions

View File

@ -27,7 +27,6 @@ export default class {
} }
setGeometry (geometry, matrix) { setGeometry (geometry, matrix) {
//convert buffergeometry to geometry;
if (geometry.type === 'BufferGeometry') { if (geometry.type === 'BufferGeometry') {
geometry = new THREE.Geometry().fromBufferGeometry(geometry); geometry = new THREE.Geometry().fromBufferGeometry(geometry);
} }
@ -57,9 +56,9 @@ export default class {
// get unique lines from geometry; // get unique lines from geometry;
var lines = this._createLines(settings); var lines = this._createLines(settings);
var {layersIntersectionIndexes, layersIntersectionPoints} = this._calculateLayersIntersections(lines, settings); var {layerIntersectionIndexes, layerIntersectionPoints} = this._calculateLayersIntersections(lines, settings);
var shapes = this._intersectionsToShapes(layersIntersectionIndexes, layersIntersectionPoints, lines, settings); var shapes = this._intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings);
var slices = this._shapesToSlices(shapes, settings); var slices = this._shapesToSlices(shapes, settings);
@ -141,23 +140,24 @@ export default class {
var numLayers = height / layerHeight; var numLayers = height / layerHeight;
var layersIntersectionIndexes = []; var layerIntersectionIndexes = [];
var layersIntersectionPoints = []; var layerIntersectionPoints = [];
for (var layer = 0; layer < numLayers; layer ++) { for (var layer = 0; layer < numLayers; layer ++) {
layersIntersectionIndexes[layer] = []; layerIntersectionIndexes[layer] = [];
layersIntersectionPoints[layer] = []; layerIntersectionPoints[layer] = [];
} }
for (var lineIndex = 0; lineIndex < lines.length; lineIndex ++) { for (var lineIndex = 0; lineIndex < lines.length; lineIndex ++) {
var line = lines[lineIndex].line; var line = lines[lineIndex].line;
// not happy about this, toFixes returns a string which has to be parsed again
var min = Math.ceil(Math.min(line.start.y, line.end.y) / layerHeight); var min = Math.ceil(Math.min(line.start.y, line.end.y) / layerHeight);
var max = Math.floor(Math.max(line.start.y, line.end.y) / layerHeight); var max = Math.floor(Math.max(line.start.y, line.end.y) / layerHeight);
for (var layerIndex = min; layerIndex <= max; layerIndex ++) { for (var layerIndex = min; layerIndex <= max; layerIndex ++) {
if (layerIndex >= 0 && layerIndex < numLayers) { if (layerIndex >= 0 && layerIndex < numLayers) {
layersIntersectionIndexes[layerIndex].push(lineIndex); layerIntersectionIndexes[layerIndex].push(lineIndex);
var y = layerIndex * layerHeight; var y = layerIndex * layerHeight;
@ -171,7 +171,7 @@ export default class {
var z = line.end.z * alpha + line.start.z * (1 - alpha); var z = line.end.z * alpha + line.start.z * (1 - alpha);
} }
layersIntersectionPoints[layerIndex][lineIndex] = new THREE.Vector2(z, x); layerIntersectionPoints[layerIndex][lineIndex] = new THREE.Vector2(z, x);
} }
} }
} }
@ -180,31 +180,31 @@ export default class {
this._updateProgress(settings); this._updateProgress(settings);
return { return {
layersIntersectionIndexes, layerIntersectionIndexes,
layersIntersectionPoints layerIntersectionPoints
}; };
} }
_intersectionsToShapes (layersIntersectionIndexes, layersIntersectionPoints, lines, settings) { _intersectionsToShapes (layerIntersectionIndexes, layerIntersectionPoints, lines, settings) {
console.log("generating slices"); console.log("generating slices");
var layerHeight = settings.config["layerHeight"]; var layerHeight = settings.config["layerHeight"];
var shapes = []; var shapes = [];
for (var layer = 1; layer < layersIntersectionIndexes.length; layer ++) { for (var layer = 1; layer < layerIntersectionIndexes.length; layer ++) {
var layerIntersectionIndexes = layersIntersectionIndexes[layer]; var intersectionIndexes = layerIntersectionIndexes[layer];
var layerIntersectionPoints = layersIntersectionPoints[layer]; var intersectionPoints = layerIntersectionPoints[layer];
if (layerIntersectionIndexes.length === 0) { if (intersectionIndexes.length === 0) {
continue; continue;
} }
var shapeParts = []; var shapeParts = [];
for (var i = 0; i < layerIntersectionIndexes.length; i ++) { for (var i = 0; i < intersectionIndexes.length; i ++) {
var index = layerIntersectionIndexes[i]; var index = intersectionIndexes[i];
if (layerIntersectionPoints[index] === undefined) { if (intersectionPoints[index] === undefined) {
continue; continue;
} }
@ -214,11 +214,11 @@ export default class {
var shape = []; var shape = [];
while (index !== -1) { while (index !== -1) {
var intersection = layerIntersectionPoints[index]; var intersection = intersectionPoints[index];
//uppercase X and Y because clipper vector //uppercase X and Y because clipper vector
shape.push({X: intersection.x, Y: intersection.y}); shape.push({X: intersection.x, Y: intersection.y});
delete layerIntersectionPoints[index]; delete intersectionPoints[index];
var connects = lines[index].connects; var connects = lines[index].connects;
var faceNormals = lines[index].normals; var faceNormals = lines[index].normals;
@ -233,22 +233,22 @@ export default class {
} }
// Check if index has an intersection or is already used // Check if index has an intersection or is already used
if (layerIntersectionPoints[index] !== undefined) { if (intersectionPoints[index] !== undefined) {
var faceNormal = faceNormals[Math.floor(j / 2)]; var faceNormal = faceNormals[Math.floor(j / 2)];
var a = new THREE.Vector2(intersection.x, intersection.y); var a = new THREE.Vector2(intersection.x, intersection.y);
var b = new THREE.Vector2(layerIntersectionPoints[index].x, layerIntersectionPoints[index].y); var b = new THREE.Vector2(intersectionPoints[index].x, intersectionPoints[index].y);
//threejs can't calculate normal if distance is smaller as 0.0001 // can't calculate normal if distance is smaller as 0.0001
if ((faceNormal.x === 0 && faceNormal.y === 0) || a.distanceTo(b) < 0.0001) { if ((faceNormal.x === 0 && faceNormal.y === 0) || a.distanceTo(b) < 0.0001) {
delete layerIntersectionPoints[index]; delete intersectionPoints[index];
connects = connects.concat(lines[index].connects); connects = connects.concat(lines[index].connects);
faceNormals = faceNormals.concat(lines[index].normals); faceNormals = faceNormals.concat(lines[index].normals);
index = -1; index = -1;
} }
else { else {
// make sure the path goes the right direction
// THREE.Vector2.normal is not yet implimented // THREE.Vector2.normal is not yet implimented
// var normal = a.sub(b).normal().normalize(); // var normal = a.sub(b).normal().normalize();
var normal = a.sub(b); var normal = a.sub(b);
@ -273,13 +273,10 @@ export default class {
while (index !== -1) { while (index !== -1) {
if (index !== firstPoint) { if (index !== firstPoint) {
var intersection = layerIntersectionPoints[index]; var intersection = intersectionPoints[index];
// PERFORMACE
// maybe performance can be increased by unshifting to sepperate
// array and to later concat the original en the new array
shape.unshift({X: intersection.x, Y: intersection.y}); shape.unshift({X: intersection.x, Y: intersection.y});
delete layerIntersectionPoints[index]; delete intersectionPoints[index];
} }
var connects = lines[index].connects; var connects = lines[index].connects;
@ -287,11 +284,11 @@ export default class {
for (var i = 0; i < connects.length; i ++) { for (var i = 0; i < connects.length; i ++) {
var index = connects[i]; var index = connects[i];
if (layerIntersectionPoints[index] !== undefined) { if (intersectionPoints[index] !== undefined) {
break; break;
} }
else { else {
delete layerIntersectionPoints[index]; delete intersectionPoints[index];
index = -1; index = -1;
} }
} }
@ -320,6 +317,8 @@ export default class {
for (var layer = 0; layer < shapes.length; layer ++) { for (var layer = 0; layer < shapes.length; layer ++) {
var shapeParts = shapes[layer]; var shapeParts = shapes[layer];
// sort object for better hole detections
// holes always have a smaller bound as its parent
shapeParts.sort((a, b) => { shapeParts.sort((a, b) => {
return a.boundSize() - b.boundSize(); return a.boundSize() - b.boundSize();
}); });

View File

@ -5,24 +5,23 @@ export default class {
constructor () { constructor () {
this.worker = new Worker('./worker.js'); this.worker = new Worker('./worker.js');
var scope = this; this.worker.addEventListener('message', (event) => {
this.worker.addEventListener('message', function (event) {
switch (event.data['cmd']) { switch (event.data['cmd']) {
case 'PROGRESS': case 'PROGRESS':
if (scope.onprogress !== undefined) { if (this.onprogress !== undefined) {
var progress = event.data['progress']; var progress = event.data['progress'];
scope.onprogress(progress); this.onprogress(progress);
} }
break; break;
case 'GCODE': case 'GCODE':
if (scope.onfinish !== undefined) { if (this.onfinish !== undefined) {
var reader = new FileReader(); var reader = new FileReader();
reader.addEventListener("loadend", function() { reader.addEventListener("loadend", () => {
var gcode = reader.result; var gcode = reader.result;
scope.onfinish(gcode); this.onfinish(gcode);
}); });
reader.readAsBinaryString(event.data['gcode']); reader.readAsBinaryString(event.data['gcode']);
} }
@ -65,6 +64,9 @@ export default class {
buffers.push(geometry.attributes[key].array.buffer); buffers.push(geometry.attributes[key].array.buffer);
} }
delete geometry.boundingBox;
delete geometry.boundingSphere;
this.worker.postMessage({ this.worker.postMessage({
'cmd': 'SET_MESH', 'cmd': 'SET_MESH',
'geometry': { 'geometry': {

View File

@ -15,10 +15,6 @@ function init () {
self.addEventListener('message', function (event) { self.addEventListener('message', function (event) {
switch (event.data['cmd']) { switch (event.data['cmd']) {
case 'SET_MESH': case 'SET_MESH':
//hack...
//because boundings loses prototype functions when converting
event.data['geometry'].boundingBox = event.data['geometry'].boundingSphere = null;
var geometry = new THREE.Geometry().fromBufferGeometry(event.data['geometry']); var geometry = new THREE.Geometry().fromBufferGeometry(event.data['geometry']);
var matrix = new THREE.Matrix4().fromArray(event.data['matrix']); var matrix = new THREE.Matrix4().fromArray(event.data['matrix']);