-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGcommand.java
More file actions
132 lines (117 loc) · 3.28 KB
/
Gcommand.java
File metadata and controls
132 lines (117 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import java.util.List;
import java.util.Objects;
/**
* Gcommand
*/
public class Gcommand implements Comparable {
private double x, y, z;
private int material;
private int usecs;
private String nameID;
/**
* Creates a Gcode command object with x,y,z coords, material, usecs, and associated file name
*/
public Gcommand(double x, double y, double z, int mat, int us, String n) {
this.x = x;
this.y = y;
this.z = z;
this.material = mat;
this.usecs = us;
this.nameID = n;
}
/**
* Creates a dummy Gcode command object
*/
public Gcommand(double x, double y) {
this.x = x;
this.y = y;
this.z = -999;
this.material = -999;
this.usecs = -999;
this.nameID = null;
}
/** Returns the X coordinate */
public double getX() {
return x;
}
/** Returns the Y coordinate */
public double getY() {
return y;
}
/** Returns the Z coordinate */
public double getZ() {
return z;
}
/** Returns the material type */
public int getMaterial() {
return material;
}
/** Returns the pulse time in usecs */
public int getUsecs() {
return usecs;
}
/** Returns the associated file name */
public String getNameID() {
return nameID;
}
/**
* Returns the euclidean distance
* @param a Gcommand A
* @param b Gcommand B
*/
public static double distance(Gcommand a, Gcommand b) {
double x = a.x - b.x;
double y = a.y - b.y;
return Math.sqrt((x * x) + (y * y));
}
/**
* Returns the euclidean total path distance
* @param list input list of Gcommands
*/
public static double pathDistance(List<Gcommand> list) {
double totalDist = 0;
int length = list.size();
for (int i = 0; i < length - 1; i++) {
totalDist += distance(list.get(i), list.get(i + 1));
}
return totalDist;
}
@Override
public String toString() {
if (material == 0) {
return "T0;\n" +
"G1 X" + x + " Y" + y + "; Material: " + material + "\n" +
"M430 V0 S" + usecs + "; Send pulse\n";
} else if (material == 1) {
return "T1;\n" +
"G1 X" + x + " Y" + y + "; Material: " + material + "\n" +
"M430 V0 S" + usecs + "; Send pulse\n";
} else if (material == 2) {
return "T2;\n" +
"G1 X" + x + " Y" + y + "; Material: " + material + "\n" +
"M430 V0 S" + usecs + "; Send pulse\n";
} else {
throw new Error("Invalid or unknown material");
}
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Gcommand o = (Gcommand) obj;
return x == o.x && y == o.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y, z);
}
@Override
public int compareTo(Object o) {
Gcommand gc = (Gcommand) o;
int diff = Double.compare(y, gc.y);
if (diff == 0) {
diff = Double.compare(x, gc.x);
}
return diff;
}
}