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

View File

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

View File

@ -15,10 +15,6 @@ function init () {
self.addEventListener('message', function (event) {
switch (event.data['cmd']) {
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 matrix = new THREE.Matrix4().fromArray(event.data['matrix']);