-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlanTrajectory.java
355 lines (298 loc) · 8.18 KB
/
PlanTrajectory.java
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import javax.swing.*;
import javax.imageio.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
public class PlanTrajectory extends FigurePanel implements Mover {
GraphicalPoint p0, p1, p2, p3;
GraphicalPath path;
ArrayList<GraphicalPoint> knotPoints;
ArrayList<GraphicalBezier> beziers;
double[][][] bezier;
public static JLabel modeLabel;
static PlanTrajectory panel;
static final int MOVE = 0;
static final int ADD = 1;
static final int REMOVE = 2;
static int state = MOVE;
boolean addRobot;
static BufferedImage field = null;
public PlanTrajectory() {
this(false, null);
}
public PlanTrajectory(boolean addRobot, String filename) {
super(0, 0, 54*12, 27*12, field);
this.addRobot = addRobot;
knotPoints = new ArrayList<GraphicalPoint>();
if (filename == null) {
p0 = new GraphicalPoint(Robot.width/2.0, 70);
p1 = new GraphicalPoint(40, 30);
p2 = new GraphicalPoint(100, 10);
p3 = new GraphicalPoint(200, 30);
knotPoints.add(p0);
knotPoints.add(p1);
knotPoints.add(p2);
knotPoints.add(p3);
} else {
CSVReader reader = new CSVReader(filename);
double[][] points = reader.parseCSV();
for (int i = 0; i < points.length; i++) {
GraphicalPoint gp =
new GraphicalPoint(points[i][0], points[i][1]);
knotPoints.add(gp);
}
}
double[] point = knotPoints.get(0).getPoint();
if (addRobot) {
Robot robot = new Robot(point[0]-Robot.width/2.0,
point[1]+Robot.height/2.0);
add(robot);
addMoveable(robot,this);
}
bezier = buildTrajectory();
createBeziers();
for (int i = 0; i < knotPoints.size(); i++) {
GraphicalPoint p = knotPoints.get(i);
p.setSize(4);
p.setColor(Color.red);
add(p);
addMoveable(p, this);
}
}
public void loadFile(File file) {
CSVReader reader = new CSVReader(file);
double[][] points = reader.parseCSV();
removeAll();
knotPoints = new ArrayList<GraphicalPoint>();
for (int i = 0; i < points.length; i++) {
GraphicalPoint gp =
new GraphicalPoint(points[i][0], points[i][1]);
knotPoints.add(gp);
}
double[] point = knotPoints.get(0).getPoint();
if (addRobot) {
Robot robot = new Robot(point[0]-Robot.width/2.0,
point[1]+Robot.height/2.0);
add(robot);
addMoveable(robot,this);
}
bezier = buildTrajectory();
createBeziers();
for (int i = 0; i < knotPoints.size(); i++) {
GraphicalPoint p = knotPoints.get(i);
p.setSize(4);
p.setColor(Color.red);
add(p);
addMoveable(p, this);
}
repaint();
}
public void createBeziers() {
if (beziers != null) {
for (int i = 0; i < beziers.size(); i++) {
remove(beziers.get(i));
removeMoveable(beziers.get(i));
}
}
for (int i = 0; i < knotPoints.size(); i++) {
remove(knotPoints.get(i));
removeMoveable(knotPoints.get(i));
}
beziers = new ArrayList<GraphicalBezier>();
for (int i = 0; i < bezier.length; i++) {
GraphicalBezier curve = new GraphicalBezier();
curve.setControlPoints(bezier[i]);
curve.setColor(Color.blue);
beziers.add(curve);
add(curve);
addMoveable(curve, this);
}
for (int i = 0; i < knotPoints.size(); i++) {
add(knotPoints.get(i));
addMoveable(knotPoints.get(i), this);
}
}
public void setPath() {
for (int i = 0; i < bezier.length; i++) {
beziers.get(i).setControlPoints(bezier[i]);
}
}
public double[][][] buildTrajectory() {
double[][] points = new double[knotPoints.size()][2];
for (int i = 0; i < knotPoints.size(); i++) {
points[i] = knotPoints.get(i).getPoint();
}
Spline spline = new Spline(points);
return spline.buildTrajectory();
}
public static void printPoints() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("./profiles"));
FileNameExtensionFilter filter =
new FileNameExtensionFilter("CSV", "csv");
chooser.addChoosableFileFilter(filter);
int v = chooser.showOpenDialog(panel);
if (v == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
FileWriter writer;
try {
writer = new FileWriter(file);
for (int i = 0; i < panel.knotPoints.size(); i++) {
GraphicalPoint p = panel.knotPoints.get(i);
double[] point = p.getPoint();
writer.append(point[0] + ", " + point[1] + "\n");
}
writer.flush();
writer.close();
} catch(IOException ex) {
System.out.println(ex);
return;
}
}
}
public static void load() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("./profiles"));
FileNameExtensionFilter filter =
new FileNameExtensionFilter("CSV", "csv");
chooser.addChoosableFileFilter(filter);
int v = chooser.showOpenDialog(panel);
if (v == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
panel.loadFile(file);
}
}
public void move(Moveable m, double x, double y) {
if (state == ADD) {
if (m instanceof GraphicalBezier) {
for (int i = 0; i < beziers.size(); i++) {
if (m == beziers.get(i)) {
GraphicalPoint gp = new GraphicalPoint(x,y);
gp.setSize(4);
gp.setColor(Color.red);
knotPoints.add(i+1, gp);
add(gp);
addMoveable(gp, this);
}
}
bezier = buildTrajectory();
createBeziers();
setPath();
}
return;
}
if (state == REMOVE) {
if (m instanceof GraphicalPoint) {
if (knotPoints.size() <= 3) return;
knotPoints.remove(m);
remove((GraphicalPoint) m);
removeMoveable(m);
bezier = buildTrajectory();
createBeziers();
}
return;
}
if (state == MOVE) {
if (m instanceof Robot) {
System.out.println(y);
if (y > 295 || y < 55) return;
((Robot) m).setPoint(0,y);
knotPoints.get(0).setPoint(Robot.width/2.0,
y-Robot.height/2.0);
bezier = buildTrajectory();
setPath();
}
if (m instanceof GraphicalPoint == false) return;
((GraphicalPoint) m).setPoint(x,y);
bezier = buildTrajectory();
setPath();
}
}
public static void setMode(int mode) {
state = mode;
switch(state) {
case MOVE:
panel.modeLabel.setText("Move");
return;
case ADD:
panel.modeLabel.setText("Add");
return;
case REMOVE:
panel.modeLabel.setText("Remove");
return;
}
}
public static void main(String[] args) {
try {
field = ImageIO.read(new File("field.cropped.png"));
} catch(IOException ex) {
System.out.println(ex);
}
DrawFrame frame = new DrawFrame();
switch(args.length) {
case 0: {
panel = new PlanTrajectory();
break;
}
case 1: {
if (args[0].equals("-r")) panel = new PlanTrajectory(true, null);
else panel = new PlanTrajectory(false, args[0]);
break;
}
case 2: {
panel = new PlanTrajectory(true, args[1]);
break;
}
default:
System.out.println("Usage: java PlanTrajectory -r filename");
return;
}
frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setPreferredSize(new Dimension(1199, 611));
JPanel buttonPanel = new JPanel();
JButton add = new JButton("Add");
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
setMode(ADD);
}
});
JButton remove = new JButton("Remove");
remove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
setMode(REMOVE);
}
});
JButton move = new JButton("Move");
move.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
setMode(MOVE);
}
});
JButton print = new JButton("Save");
print.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
printPoints();
}
});
JButton load = new JButton("Load");
load.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
load();
}
});
modeLabel = new JLabel("Move");
buttonPanel.add(add);
buttonPanel.add(remove);
buttonPanel.add(move);
buttonPanel.add(load);
buttonPanel.add(print);
buttonPanel.add(modeLabel);
frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
frame.pack();
frame.show();
}
}