Permalink
Browse files

Added layer splitting, type decoding, relative/absolute etc

Lots of new things, I apologize for the formatting, somehow my editor
doesn't seem to grok this tabulation, or lack of.

The decoder now splits the object into 1) layers and 2) segment types.
Segment types are sorted by extrusion rate (speed) and whether they
are extruding or not.

materials and geometry are made for each of the layer, and all added to
the resulting object.

There is also support for G92, G90 and G91.

The decoder also computes the object min/max on the fly, excluding
non-extruding movements.

With this code, we can implement partial viewing of layers, and change the
alpha value and/or color relative to movement speed.

Signed-off-by: Michel Pollet <buserror@gmail.com>
  • Loading branch information...
1 parent ad2fee7 commit 3cb6d18f874c87be12b9d41c61fe6bfab9206217 @buserror committed Apr 25, 2012
Showing with 105 additions and 36 deletions.
  1. +105 −36 web/gcode-model.js
View
@@ -3,15 +3,69 @@ function createObjectFromGCode(gcode) {
// http://reprap.org/wiki/G-code
// http://en.wikipedia.org/wiki/G-code
// SprintRun source code
-
- var object = new THREE.Object3D();
-
- var geometry = new THREE.Geometry();
-
+
var lastLine = {x:0, y:0, z:0, e:0, f:0, extruding:false};
-
- var parser = new GCodeParser({
-
+
+ var layers = [];
+ var layer = undefined;
+ var bbbox = { min: { x:100000,y:100000,z:100000 }, max: { x:-100000,y:-100000,z:-100000 } };
+
+ function newLayer(line) {
+ layer = { type: {}, layer: layers.count(), z: line.z, };
+ layers.push(layer);
+ }
+ function getLineGroup(line) {
+ if (layer == undefined)
+ newLayer(line);
+ var speed = Math.round(line.e / 1000);
+ var grouptype = (line.extruding ? 10000 : 0) + speed;
+ var color = new THREE.Color(line.extruding ? 0xffffff : 0x0000ff);
+ if (layer.type[grouptype] == undefined) {
+ layer.type[grouptype] = {
+ type: grouptype,
+ feed: line.e,
+ extruding: line.extruding,
+ color: color,
+ segmentCount: 0,
+ material: new THREE.LineBasicMaterial({
+ opacity:line.extruding ? 0.5 : 0.4,
+ transparent: true,
+ linewidth: 1,
+ vertexColors: THREE.FaceColors }),
+ geometry: new THREE.Geometry(),
+ }
+ }
+ return layer.type[grouptype];
+ }
+ function addSegment(p1, p2) {
+ var group = getLineGroup(p2);
+ var geometry = group.geometry;
+
+ group.segmentCount++;
+ geometry.vertices.push(new THREE.Vertex(
+ new THREE.Vector3(p1.x, p1.y, p1.z)));
+ geometry.vertices.push(new THREE.Vertex(
+ new THREE.Vector3(p2.x, p2.y, p2.z)));
+ geometry.colors.push(group.color);
+ geometry.colors.push(group.color);
+ if (p2.extruding) {
+ bbbox.min.x = Math.min(bbbox.min.x, p2.x);
+ bbbox.min.y = Math.min(bbbox.min.y, p2.y);
+ bbbox.min.z = Math.min(bbbox.min.z, p2.z);
+ bbbox.max.x = Math.max(bbbox.max.x, p2.x);
+ bbbox.max.y = Math.max(bbbox.max.y, p2.y);
+ bbbox.max.z = Math.max(bbbox.max.z, p2.z);
+ }
+ }
+ var relative = false;
+ function delta(v1, v2) {
+ return relative ? v2 : v2 - v1;
+ }
+ function absolute (v1, v2) {
+ return relative ? v1 + v2 : v2;
+ }
+
+ var parser = new GCodeParser({
G1: function(args, line) {
// Example: G1 Z1.0 F3000
// G1 X99.9948 Y80.0611 Z15.0 F1500.0 E981.64869
@@ -22,25 +76,20 @@ function createObjectFromGCode(gcode) {
// 22.4 mm.
var newLine = {
- x: args.x !== undefined ? args.x : lastLine.x,
- y: args.y !== undefined ? args.y : lastLine.y,
- z: args.z !== undefined ? args.z : lastLine.z,
- e: args.e !== undefined ? args.e : lastLine.e,
- f: args.f !== undefined ? args.f : lastLine.f,
+ x: args.x !== undefined ? absolute(lastLine.x, args.x) : lastLine.x,
+ y: args.y !== undefined ? absolute(lastLine.y, args.y) : lastLine.y,
+ z: args.z !== undefined ? absolute(lastLine.z, args.z) : lastLine.z,
+ e: args.e !== undefined ? absolute(lastLine.e, args.e) : lastLine.e,
+ f: args.f !== undefined ? absolute(lastLine.f, args.f) : lastLine.f,
};
-
- newLine.extruding = (newLine.e - lastLine.e) > 0;
- var color = new THREE.Color(newLine.extruding ? 0xFFFFFF : 0x0000FF);
-
- if (newLine.extruding) {
- geometry.vertices.push(new THREE.Vertex(
- new THREE.Vector3(lastLine.x, lastLine.y, lastLine.z)));
- geometry.vertices.push(new THREE.Vertex(
- new THREE.Vector3(newLine.x, newLine.y, newLine.z)));
- geometry.colors.push(color);
- geometry.colors.push(color);
- }
-
+ /* layer change detection is or made by watching Z, it's made by
+ watching when we extrude at a new Z position */
+ if (delta(lastLine.e, newLine.e) > 0) {
+ newLine.extruding = delta(lastLine.e, newLine.e) > 0;
+ if (layer == undefined || newLine.z != layer.z)
+ newLayer(newLine);
+ }
+ addSegment(lastLine, newLine);
lastLine = newLine;
},
@@ -58,7 +107,7 @@ function createObjectFromGCode(gcode) {
// All coordinates from now on are absolute relative to the
// origin of the machine. (This is the RepRap default.)
- // TODO!
+ relative = false;
},
G91: function(args) {
@@ -67,6 +116,7 @@ function createObjectFromGCode(gcode) {
// All coordinates from now on are relative to the last position.
// TODO!
+ relative = true;
},
G92: function(args) { // E0
@@ -78,6 +128,12 @@ function createObjectFromGCode(gcode) {
// No physical motion will occur.
// TODO: Only support E0
+ var newLine = lastLine;
+ newLine.x= args.x !== undefined ? args.x : newLine.x;
+ newLine.y= args.y !== undefined ? args.y : newLine.y;
+ newLine.z= args.z !== undefined ? args.z : newLine.z;
+ newLine.e= args.e !== undefined ? args.e : newLine.e;
+ lastLine = newLine;
},
M82: function(args) {
@@ -106,19 +162,32 @@ function createObjectFromGCode(gcode) {
parser.parse(gcode);
- var lineMaterial = new THREE.LineBasicMaterial({
- opacity:0.4,
- linewidth: 1,
- vertexColors: THREE.FaceColors});
- object.add(new THREE.Line(geometry, lineMaterial, THREE.LinePieces));
+ console.log("Layer Count ", layers.count());
+
+ var object = new THREE.Object3D();
+
+ for (var lid in layers) {
+ var layer = layers[lid];
+// console.log("Layer ", layer.layer);
+ for (var tid in layer.type) {
+ var type = layer.type[tid];
+// console.log("Layer ", layer.layer, " type ", type.type, " seg ", type.segmentCount);
+ object.add(new THREE.Line(type.geometry, type.material, THREE.LinePieces));
+ }
+ }
+ console.log("bbox ", bbbox);
// Center
- geometry.computeBoundingBox();
- var center = new THREE.Vector3()
- .add(geometry.boundingBox.min, geometry.boundingBox.max)
- .divideScalar(2);
var scale = 3; // TODO: Auto size
+
+ var center = new THREE.Vector3(
+ bbbox.min.x + ((bbbox.max.x - bbbox.min.x) / 2),
+ bbbox.min.y + ((bbbox.max.y - bbbox.min.y) / 2),
+ bbbox.min.z + ((bbbox.max.z - bbbox.min.z) / 2));
+ console.log("center ", center);
+
object.position = center.multiplyScalar(-scale);
+
object.scale.multiplyScalar(scale);
return object;

0 comments on commit 3cb6d18

Please sign in to comment.