diff --git a/README.md b/README.md index ffd3db3..abdf0c4 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,4 @@ and in `grblgui-gcode` next to `grblgui.jar`. ## Development For compiling you need: -* Eclipse 3.6(?) -* Eclipse Scala plugin with Scala 2.10 -* Or: Scala IDE from http://scala-ide.org/ +* Eclipse >= 3.6 diff --git a/grblgui-desktop/.classpath b/grblgui-desktop/.classpath index b5c9238..b1ecfc0 100644 --- a/grblgui-desktop/.classpath +++ b/grblgui-desktop/.classpath @@ -1,7 +1,6 @@ - diff --git a/grblgui-desktop/.project b/grblgui-desktop/.project index 9157e2c..96830be 100644 --- a/grblgui-desktop/.project +++ b/grblgui-desktop/.project @@ -6,13 +6,12 @@ - org.scala-ide.sdt.core.scalabuilder + org.eclipse.jdt.core.javabuilder - org.scala-ide.sdt.core.scalanature org.eclipse.jdt.core.javanature diff --git a/grblgui-desktop/src/cody/grblgui/ScalaSimulationConverter.java b/grblgui-desktop/src/cody/grblgui/ScalaSimulationConverter.java deleted file mode 100644 index 37576ca..0000000 --- a/grblgui-desktop/src/cody/grblgui/ScalaSimulationConverter.java +++ /dev/null @@ -1,20 +0,0 @@ -package cody.grblgui; - -import scala.Tuple2; -import cody.grblgui.Part; -import cody.grblgui.SimulationConverter; -import cody.grblgui.ToolInfo; -import cody.grblgui.Toolpath; - -public class ScalaSimulationConverter extends SimulationConverter { - @Override - public Part convertImpl(Toolpath path, ToolInfo info){ - Simulation sim = new Simulation(300, 400, 50, 1f); - sim.simulate(path, info); - //Tuple2 tmp = sim.getZminmax(); - //float min = (float)tmp._1; - //float max = (float)tmp._2; - - return new SimulationPart(sim); - } -} diff --git a/grblgui-desktop/src/cody/grblgui/Simulation.scala b/grblgui-desktop/src/cody/grblgui/Simulation.scala deleted file mode 100644 index 05605c3..0000000 --- a/grblgui-desktop/src/cody/grblgui/Simulation.scala +++ /dev/null @@ -1,79 +0,0 @@ -package cody.grblgui - -import Array.ofDim -import com.badlogic.gdx.math.Vector3 -import com.badlogic.gdx.math.Vector2 -import cody.grblgui.ToolInfo -import cody.grblgui.Toolpath - -class Simulation(size_x : Float, size_y : Float, size_z : Float, precision : Float) { - val count_x : Int = (size_x / precision).toInt; - val count_y : Int = (size_y / precision).toInt; - var map:Array[Array[Float]] = ofDim[Float](count_x,count_y) - var zero = new Vector2(-size_x / 2f,-size_y / 2f) - - def index_to_position(x : Int, y : Int) : Vector2 = new Vector2(zero.x + x * precision, zero.y + y * precision) - - def position_to_index(v : Vector2) = (((v.x - zero.x) / precision).toInt, ((v.y - zero.y) / precision).toInt) - def position_to_index(x : Float, y : Float) = (((x - zero.x) / precision).toInt, ((y - zero.y) / precision).toInt) - def length_to_count(l : Float) : Int = (l / precision).toInt - - def getZ(x : Int, y : Int) = map(x)(y) - def getZminmax() : (Float, Float) = { - var min : Float = Float.MaxValue - var max : Float = Float.MinValue - for(x <- 0 to count_x - 1) { - for(y <- 0 to count_y - 1) { - val z = getZ(x,y) - min = math.min(z, min) - max = math.max(z, max) - } - } - return (min, max) - } - - def mill(x : Int, y : Int, z : Float) { - if(x >= 0 && y >= 0 && x < count_x && y < count_y) - map(x)(y) = math.min(map(x)(y), z) - } - - def mill(pos : Vector3, tool : ToolInfo) { - val center = position_to_index(pos.x, pos.y) - val count = length_to_count(tool.getRadius()) - for(x <- center._1 - count to center._1 + count) { - for(y <- center._2 - count to center._2 + count) { - var p = index_to_position(x,y) - p = new Vector2(p.x - pos.x, p.y - pos.y) - - val dist = p.len() - if(dist < tool.getRadius()) { - //println("mill " + pos.x + " " + pos.y + " " + pos.z + " " + x + " " + y) - mill(x, y, pos.z) - } - } - } - } - - def simulate(path : Toolpath, tool : ToolInfo) { - var cur = path.path.get(0) - for(i <- 1 to path.path.size() - 1) { - val next = path.path.get(i) - if(next.z < 0 || cur.z < 0){ - var d = next.cpy() - d.sub(cur) - //println("line " + cur.toString() + " -> " + next.toString()) - val segments = math.max(1,(d.len() / precision).toInt) - for(j <- 0 to segments - 1) { - var p = d.cpy() - p.scl(j) - p.scl(1.0f/segments) - p.add(cur) - mill(new Vector3(p.x,p.y,p.z), tool) - } - } - cur = next - } - } - - -} \ No newline at end of file diff --git a/grblgui-desktop/src/cody/grblgui/ToolType.scala b/grblgui-desktop/src/cody/grblgui/ToolType.scala deleted file mode 100644 index b17d4de..0000000 --- a/grblgui-desktop/src/cody/grblgui/ToolType.scala +++ /dev/null @@ -1,6 +0,0 @@ -package cody.grblgui - - object ToolType extends Enumeration { - type Type = Value - val Ball, End, V = Value - } \ No newline at end of file diff --git a/grblgui-desktop/src/cody/grblgui/desktop/GrblGuiMainDesktop.java b/grblgui-desktop/src/cody/grblgui/desktop/GrblGuiMainDesktop.java index 83cd46e..0dc8752 100644 --- a/grblgui-desktop/src/cody/grblgui/desktop/GrblGuiMainDesktop.java +++ b/grblgui-desktop/src/cody/grblgui/desktop/GrblGuiMainDesktop.java @@ -6,7 +6,6 @@ import cody.grbl.GrblFactory; import cody.grbl.GrblStreamFactory; import cody.grblgui.Main; -import cody.grblgui.ScalaSimulationConverter; import cody.grblgui.SimulationConverter; import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; @@ -34,7 +33,7 @@ public static void main (String[] argv) { } GrblStreamFactory.instance = new GrblFactory(); - SimulationConverter.instance = new ScalaSimulationConverter(); + SimulationConverter.instance = new SimulationConverter(); Main main = new Main(dir, port); LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); diff --git a/grblgui/src/cody/grblgui/Simulation.java b/grblgui/src/cody/grblgui/Simulation.java new file mode 100644 index 0000000..9c5237a --- /dev/null +++ b/grblgui/src/cody/grblgui/Simulation.java @@ -0,0 +1,99 @@ +package cody.grblgui; + +import com.badlogic.gdx.math.GridPoint2; +import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.math.Vector3; + +public class Simulation { + public int count_x, count_y; + float map[][]; + Vector2 zero; + final float precision; + + public Simulation(float size_x, float size_y, float size_z, float precision) { + this.precision = precision; + count_x = (int)(size_x / precision); + count_y = (int)(size_y / precision); + + map = new float[count_x][count_y]; + zero = new Vector2(-size_x / 2f,-size_y / 2f); + } + + public Vector2 indexToPosition(int x, int y) { + return new Vector2(zero.x + x * precision, zero.y + y * precision); + } + + public GridPoint2 positionToIndex(Vector2 v) { + return new GridPoint2((int)((v.x - zero.x) / precision), (int)((v.y - zero.y) / precision)); + } + + public GridPoint2 positionToIndex(float x, float y) { + return new GridPoint2((int)((x - zero.x) / precision), (int)((y - zero.y) / precision)); + } + + public int lengthToCount(float l) { + return (int)(l/precision); + } + + public float getZ(int x, int y) { + return map[x][y]; + } + + public Vector2 getZminmax() { + float min = Float.MAX_VALUE; + float max = Float.MIN_VALUE; + for(int x = 0; x <= count_x - 1; ++x) { + for(int y = 0; y <= count_y - 1; ++y) { + float z = getZ(x,y); + min = Math.min(z, min); + max = Math.max(z, max); + } + } + return new Vector2(min, max); + } + + void mill(int x, int y, float z) { + if(x >= 0 && y >= 0 && x < count_x && y < count_y) + map[x][y] = Math.min(map[x][y], z); + } + + void mill(Vector3 pos, ToolInfo tool) { + GridPoint2 center = positionToIndex(pos.x, pos.y); + int count = lengthToCount(tool.getRadius()); + + for(int x = center.x - count; x <= center.x + count; ++x) { + for(int y = center.y - count; y <= center.y + count; ++y) { + Vector2 p = indexToPosition(x,y); + p = new Vector2(p.x - pos.x, p.y - pos.y); + + float dist = p.len(); + if(dist < tool.getRadius()) { + //println("mill " + pos.x + " " + pos.y + " " + pos.z + " " + x + " " + y) + mill(x, y, pos.z); + } + } + } + } + + void simulate(Toolpath path, ToolInfo tool) { + Vector3 cur = path.path.get(0); + + for(int i=1;i<=path.path.size() - 1;++i) { + Vector3 next = path.path.get(i); + if(next.z < 0 || cur.z < 0){ + Vector3 d = next.cpy(); + d.sub(cur); + //println("line " + cur.toString() + " -> " + next.toString()) + int segments = Math.max(1,(int)(d.len() / precision)); + for(int j = 0; j <= segments - 1; ++j) { + Vector3 p = d.cpy(); + p.scl(j); + p.scl(1.0f/segments); + p.add(cur); + mill(new Vector3(p.x,p.y,p.z), tool); + } + } + cur = next; + } + } +} diff --git a/grblgui/src/cody/grblgui/SimulationConverter.java b/grblgui/src/cody/grblgui/SimulationConverter.java index e510da6..72e0b2b 100644 --- a/grblgui/src/cody/grblgui/SimulationConverter.java +++ b/grblgui/src/cody/grblgui/SimulationConverter.java @@ -1,6 +1,6 @@ package cody.grblgui; -public abstract class SimulationConverter { +public class SimulationConverter { public static SimulationConverter instance; public static Part convert(Toolpath path, ToolInfo info){ @@ -11,6 +11,12 @@ public static Part convert(Toolpath path, ToolInfo info){ } public Part convertImpl(Toolpath path, ToolInfo info){ - return null; + Simulation sim = new Simulation(300, 400, 50, 1f); + sim.simulate(path, info); + //Tuple2 tmp = sim.getZminmax(); + //float min = (float)tmp._1; + //float max = (float)tmp._2; + + return new SimulationPart(sim); } } diff --git a/grblgui-desktop/src/cody/grblgui/SimulationPart.java b/grblgui/src/cody/grblgui/SimulationPart.java similarity index 82% rename from grblgui-desktop/src/cody/grblgui/SimulationPart.java rename to grblgui/src/cody/grblgui/SimulationPart.java index d86523c..551be64 100644 --- a/grblgui-desktop/src/cody/grblgui/SimulationPart.java +++ b/grblgui/src/cody/grblgui/SimulationPart.java @@ -2,8 +2,6 @@ import java.io.FileNotFoundException; -import scala.Tuple2; - import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Mesh; @@ -12,6 +10,7 @@ import com.badlogic.gdx.graphics.Texture.TextureWrap; import com.badlogic.gdx.graphics.VertexAttribute; import com.badlogic.gdx.graphics.VertexAttributes.Usage; +import com.badlogic.gdx.math.GridPoint2; import com.badlogic.gdx.math.Vector2; import cody.grblgui.Part; @@ -20,20 +19,20 @@ public class SimulationPart extends Part { Mesh generateMesh(Simulation sim) { - float[] verts = new float[(sim.count_x() + 1) * 2 * (sim.count_y() - 1) * (3 + 1)]; + float[] verts = new float[(sim.count_x + 1) * 2 * (sim.count_y - 1) * (3 + 1)]; System.out.println(verts.length / (3 + 1)); - Tuple2 tmp = sim.getZminmax(); - final float min = (float)tmp._1; - final float max = (float)tmp._2; + Vector2 tmp = sim.getZminmax(); + final float min = (float)tmp.x; + final float max = (float)tmp.y; int index = 0; - for(int y = 0; y < sim.count_y() - 1; ++y) { - for(int x = 0; x < sim.count_x(); ++x) { + for(int y = 0; y < sim.count_y - 1; ++y) { + for(int x = 0; x < sim.count_x; ++x) { float pz = sim.getZ(x, y); - Vector2 v = sim.index_to_position(x, y); + Vector2 v = sim.indexToPosition(x, y); float px = v.x; float py = v.y; float c1 = (max - pz) / (max - min); @@ -41,7 +40,7 @@ Mesh generateMesh(Simulation sim) { c1 = Math.max(0.2f, c1); float pz2 = sim.getZ(x, y + 1); - Vector2 v2 = sim.index_to_position(x, y + 1); + Vector2 v2 = sim.indexToPosition(x, y + 1); float px2 = v2.x; float py2 = v2.y; float c2 = (max - pz2) / (max - min); @@ -66,7 +65,7 @@ Mesh generateMesh(Simulation sim) { verts[index++] = pz2; verts[index++] = new Color(0,0,c2,1).toFloatBits(); - if(x == sim.count_x() - 1) { + if(x == sim.count_x - 1) { for(int i=0;i<4;++i) { verts[index] = verts[index-4]; index++;