Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added layer splitting, type decoding, relative/absolute etc #1

Merged
merged 1 commit into from

2 participants

@buserror

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

@buserror buserror 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>
3cb6d18
@joewalnes joewalnes merged commit 9114f98 into joewalnes:master
@joewalnes
Owner

That's awesome, Michel. Thanks for the contribution! Merged.

I'm hoping this improvement is the first of many :)

One issue: I'm seeing a lot of flicker with this new code that didn't exist before. I see this in Chrome on Mac and Linux. It seems fine in FireFox. Do you see flickery lines too? I suspect it may be caused by z-fighting.

Do you have any .gcode files that contain G90/91/92 commands that could be added to the examples?

Thanks again!
-Joe

@buserror

Thanks Joe! Yes I hope to be able to hack around that codebase again soon :>

The G92 is used by pretty much anything that uses Slic3r to make gcode, primarily to reset the E Axis before/after retraction..

The blinking I also see, somehow one layer blinks-- I think it might be because we use "one" object with all the layers, instead of multiple objects where it would have more freedom to use the Z sorting algorithm... I hope we can either sort that, OR implement our own z sorting since in our case it's fairly easy to do (all horizontal planes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 25, 2012
  1. @buserror

    Added layer splitting, type decoding, relative/absolute etc

    buserror authored
    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>
This page is out of date. Refresh to see the latest.
Showing with 105 additions and 36 deletions.
  1. +105 −36 web/gcode-model.js
View
141 web/gcode-model.js
@@ -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;
Something went wrong with that request. Please try again.