Newer
Older
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
/**
* This file is part of LibLaserCut.
* Copyright (C) 2011 - 2013 Thomas Oster <thomas.oster@rwth-aachen.de>
* RWTH Aachen University - 52062 Aachen, Germany
*
* LibLaserCut is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LibLaserCut is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with LibLaserCut. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.t_oster.liblasercut.vectoroptimizers;
import com.t_oster.liblasercut.LaserProperty;
import com.t_oster.liblasercut.VectorCommand;
import com.t_oster.liblasercut.VectorPart;
import com.t_oster.liblasercut.platform.Point;
import com.t_oster.liblasercut.platform.Rectangle;
import java.util.LinkedList;
import java.util.List;
/**
*
* @author Thomas Oster <thomas.oster@rwth-aachen.de>
*/
public abstract class VectorOptimizer
{
public enum OrderStrategy
{
FILE,
NEAREST,
INNER_FIRST,
SMALLEST_FIRST
}
protected class Element
{
LaserProperty prop;
Point start;
List<Point> moves = new LinkedList<Point>();
void invert()
{
if (!moves.isEmpty())
{
moves.add(0, start);
start = moves.remove(moves.size() - 1);
List<Point> inv = new LinkedList<Point>();
while (!moves.isEmpty())
{
inv.add(moves.remove(moves.size() - 1));
}
moves = inv;
}
}
Point getEnd()
{
return moves.isEmpty() ? start : moves.get(moves.size() - 1);
}
/**
* compute bounding box of moves, including start point
*
* @return Rectangle
*/
Rectangle boundingBox()
{
if (start == null)
{ // TODO may this happen?
return null;
}
Rectangle bb = new Rectangle(start.x, start.y, start.x, start.y);
for (Point p : moves)
{
bb.add(p);
}
return bb;
}
/**
* test if this Element represents a closed path (polygon)
*
* @return true if start equals end, false otherwise
*/
boolean isClosedPath()
{
if ((start == null) || moves.isEmpty())
{
return false;
}
return getEnd().equals(start);
}
}
public static VectorOptimizer create(OrderStrategy s)
{
switch (s)
{
case FILE:
return new FileVectorOptimizer();
case NEAREST:
return new NearestVectorOptimizer();
case INNER_FIRST:
return new InnerFirstVectorOptimizer();
case SMALLEST_FIRST:
return new SmallestFirstVectorOptimizer();
}
throw new IllegalArgumentException("Unknown Order Strategy: " + s);
}
protected List<Element> divide(VectorPart vp)
{
List<Element> result = new LinkedList<Element>();
Element cur = null;
Point lastMove = null;
LaserProperty lastProp = null;
boolean stop = false;
for (VectorCommand cmd : vp.getCommandList())
{
switch (cmd.getType())
{
case MOVETO:
{
lastMove = new Point(cmd.getX(), cmd.getY());
stop = true;
break;
}
case LINETO:
{
if (stop)
{
stop = false;
if (cur != null)
{
result.add(cur);
}
cur = new Element();
cur.start = lastMove;
cur.prop = lastProp;
}
cur.moves.add(new Point(cmd.getX(), cmd.getY()));
break;
}
case SETPROPERTY:
{
lastProp = cmd.getProperty();
stop = true;
break;
}
}
}
if (cur != null)
{
result.add(cur);
}
return result;
}
protected double dist(Point a, Point b)
{
return Math.sqrt((a.y - b.y) * (a.y - b.y) + (a.x - b.x) * (a.x - b.x));
}
protected abstract List<Element> sort(List<Element> e);
public VectorPart optimize(VectorPart vp)
{
List<Element> opt = this.sort(this.divide(vp));
LaserProperty cp = opt.isEmpty() ? vp.getCurrentCuttingProperty() : opt.get(0).prop;
VectorPart result = new VectorPart(cp, vp.getDPI());
for (Element e : opt)
{
if (!e.prop.equals(cp))
{
result.setProperty(e.prop);
cp = e.prop;
}
result.moveto(e.start.x, e.start.y);
for (Point p : e.moves)
{
result.lineto(p.x, p.y);
}
}
return result;
}
}