added algorithm to remove unnecessary polygons in shape

This commit is contained in:
casperlamboo 2015-05-08 14:29:06 +02:00
parent c4c32234ae
commit 498708e4e4
6 changed files with 86 additions and 34 deletions

View File

@ -17,7 +17,7 @@ var mesh = new THREE.Mesh(geometry, material);
doodleBox.onload = function () {
"use strict";
var slicer = new D3D.Slicer().setGeometry(mesh);
var slicer = new D3D.Slicer().setMesh(mesh);
var gcode = slicer.getGcode(doodleBox.printer);
doodleBox.print(gcode);

45
build/d3d.js vendored
View File

@ -90,7 +90,7 @@ Array.prototype.clone = function () {
return array;
};
function applyMouseControls (renderer, camera, maxDistance) {
function applyMouseControls (renderer, camera, center, maxDistance) {
"use strict";
//TODO
//impliment touch controls
@ -102,10 +102,12 @@ function applyMouseControls (renderer, camera, maxDistance) {
var moveCamera = false;
function updateCamera () {
camera.position.x = Math.cos(rotY)*Math.sin(rotX)*distance;
camera.position.y = Math.sin(rotY)*distance;
camera.position.z = Math.cos(rotY)*Math.cos(rotX)*distance;
camera.lookAt(new THREE.Vector3(0, 0, 0));
camera.position.set(
Math.cos(rotY)*Math.sin(rotX)*distance,
Math.sin(rotY)*distance,
Math.cos(rotY)*Math.cos(rotX)*distance
).add(center);
camera.lookAt(center);
}
$(renderer.domElement).on("mousedown", function (e) {
@ -632,9 +634,11 @@ D3D.Slicer = function () {
this.lines = [];
};
D3D.Slicer.prototype.setGeometry = function (mesh) {
D3D.Slicer.prototype.setMesh = function (mesh) {
"use strict";
mesh.updateMatrix();
var geometry = mesh.geometry.clone();
geometry.mergeVertices();
geometry.applyMatrix(mesh.matrix);
@ -731,8 +735,7 @@ D3D.Slicer.prototype.slice = function (height, step) {
var x = line.start.x * alpha + line.end.x * (1 - alpha);
var z = line.start.z * alpha + line.end.z * (1 - alpha);
//remove +100 when implimenting good stucture for creating geometry is complete
intersections[index] = new THREE.Vector2(x + 100, z + 100);
intersections[index] = new THREE.Vector2(x, z);
}
var done = [];
@ -773,6 +776,28 @@ D3D.Slicer.prototype.slice = function (height, step) {
}
}
/*
for (var i = 0; i < shape.length; i ++) {
var point = shape[i];
var previousPoint = shape[(i + shape.length - 1) % shape.length];
var nextPoint = shape[(i + 1) % shape.length];
var point = new THREE.Vector2(point.X, point.Y);
var previousPoint = new THREE.Vector2(previousPoint.X, previousPoint.Y);
var nextPoint = new THREE.Vector2(nextPoint.X, nextPoint.Y);
//var lineLength = nextPoint.sub(previousPoint).length();
var normal = nextPoint.sub(previousPoint).normal().normalize();
var distance = Math.abs(normal.dot(point.sub(previousPoint)));
//something better for offset check
if (distance <= 0.01) {
shape.splice(i, 1);
i --;
}
}
*/
//think this check is not nescesary, always higher as 0
if (shape.length > 0) {
slice.push(shape);
@ -1093,8 +1118,8 @@ D3D.Slicer.prototype.drawPaths = function (printer, min, max) {
var slice = data[layer % data.length];
drawLines(slice.outerLayer, "red");
//drawLines(slice.innerLayer, "green");
//drawLines(slice.fill, "blue");
drawLines(slice.innerLayer, "green");
drawLines(slice.fill, "blue");
drawVertexes(slice.outerLayer, "green");
}

2
build/d3d.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -43,7 +43,7 @@ var printerConfig = {
"printer.heatup.bed.temperature": 70,
"printer.heatup.enabled": true,
"printer.heatup.temperature": 180,
"printer.layerHeight": 0.2,
"printer.layerHeight": 0.3,
"printer.retraction.amount": 3,
"printer.retraction.enabled": true,
"printer.retraction.minDistance": 5,
@ -65,8 +65,8 @@ var printerConfig = {
};
var printer = new D3D.Printer(printerConfig);
//var localIp = location.hash.substring(1);
//var doodleBox = new D3D.Box(localIp);
var localIp = location.hash.substring(1);
var doodleBox = new D3D.Box(localIp);
//var printer = doodleBox.printer;
var scene = new THREE.Scene();
@ -76,16 +76,16 @@ renderer.setClearColor(0xffffff, 1);
var camera = new THREE.PerspectiveCamera(75, renderer.domElement.width/renderer.domElement.height, 1, 10000);
applyMouseControls(renderer, camera, 1000);
applyMouseControls(renderer, camera, new THREE.Vector3(100, 0, 100), 1000);
var geometry = (function () {
"use strict";
var circle = new THREE.Shape();
circle.absarc(0, 0, 10, 0, Math.PI*2, false);
circle.absarc(0, 0, 100, 0, Math.PI*2, false);
var hole = new THREE.Path();
hole.absarc(0, 0, 5, 0, Math.PI*2, true );
hole.absarc(0, 0, 15, 0, Math.PI*2, true );
circle.holes.push(hole);
@ -103,16 +103,18 @@ var geometry = (function () {
})();
var material = new THREE.MeshBasicMaterial({color: 0x000000, wireframe: true});
var geometry = new THREE.TorusGeometry(20, 10, 30, 30);
//var geometry = new THREE.BoxGeometry(20, 5, 20, 1, 1, 1);
var geometry = new THREE.SphereGeometry(10, 10, 10);
//var geometry = new THREE.TorusGeometry(20, 10, 30, 30);
var geometry = new THREE.BoxGeometry(20, 20, 20, 1, 1, 1);
//var geometry = new THREE.SphereGeometry(10, 10, 10);
var mesh = new THREE.Mesh(geometry, material);
mesh.position.x = 100;
mesh.position.z = 100;
scene.add(mesh);
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var slicer = new D3D.Slicer().setGeometry(mesh);
var slicer = new D3D.Slicer().setMesh(mesh);
gcode = slicer.getGcode(printer);
@ -139,7 +141,7 @@ loader.load('stl/overhang_test.stl', function (geometry) {
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var slicer = new D3D.Slicer().setGeometry(geometry);
var slicer = new D3D.Slicer().setMesh(geometry);
var layer = 149;
var img = slicer.drawPaths(printer, layer, layer + 1);

View File

@ -17,9 +17,11 @@ D3D.Slicer = function () {
this.lines = [];
};
D3D.Slicer.prototype.setGeometry = function (mesh) {
D3D.Slicer.prototype.setMesh = function (mesh) {
"use strict";
mesh.updateMatrix();
var geometry = mesh.geometry.clone();
geometry.mergeVertices();
geometry.applyMatrix(mesh.matrix);
@ -116,8 +118,7 @@ D3D.Slicer.prototype.slice = function (height, step) {
var x = line.start.x * alpha + line.end.x * (1 - alpha);
var z = line.start.z * alpha + line.end.z * (1 - alpha);
//remove +100 when implimenting good stucture for creating geometry is complete
intersections[index] = new THREE.Vector2(x + 100, z + 100);
intersections[index] = new THREE.Vector2(x, z);
}
var done = [];
@ -158,6 +159,28 @@ D3D.Slicer.prototype.slice = function (height, step) {
}
}
/*
for (var i = 0; i < shape.length; i ++) {
var point = shape[i];
var previousPoint = shape[(i + shape.length - 1) % shape.length];
var nextPoint = shape[(i + 1) % shape.length];
var point = new THREE.Vector2(point.X, point.Y);
var previousPoint = new THREE.Vector2(previousPoint.X, previousPoint.Y);
var nextPoint = new THREE.Vector2(nextPoint.X, nextPoint.Y);
//var lineLength = nextPoint.sub(previousPoint).length();
var normal = nextPoint.sub(previousPoint).normal().normalize();
var distance = Math.abs(normal.dot(point.sub(previousPoint)));
//something better for offset check
if (distance <= 0.01) {
shape.splice(i, 1);
i --;
}
}
*/
//think this check is not nescesary, always higher as 0
if (shape.length > 0) {
slice.push(shape);
@ -478,8 +501,8 @@ D3D.Slicer.prototype.drawPaths = function (printer, min, max) {
var slice = data[layer % data.length];
drawLines(slice.outerLayer, "red");
//drawLines(slice.innerLayer, "green");
//drawLines(slice.fill, "blue");
drawLines(slice.innerLayer, "green");
drawLines(slice.fill, "blue");
drawVertexes(slice.outerLayer, "green");
}

View File

@ -90,7 +90,7 @@ Array.prototype.clone = function () {
return array;
};
function applyMouseControls (renderer, camera, maxDistance) {
function applyMouseControls (renderer, camera, center, maxDistance) {
"use strict";
//TODO
//impliment touch controls
@ -102,10 +102,12 @@ function applyMouseControls (renderer, camera, maxDistance) {
var moveCamera = false;
function updateCamera () {
camera.position.x = Math.cos(rotY)*Math.sin(rotX)*distance;
camera.position.y = Math.sin(rotY)*distance;
camera.position.z = Math.cos(rotY)*Math.cos(rotX)*distance;
camera.lookAt(new THREE.Vector3(0, 0, 0));
camera.position.set(
Math.cos(rotY)*Math.sin(rotX)*distance,
Math.sin(rotY)*distance,
Math.cos(rotY)*Math.cos(rotX)*distance
).add(center);
camera.lookAt(center);
}
$(renderer.domElement).on("mousedown", function (e) {