Debian Codegraph @

PathSorter.cpp

raw symbols SCIP
1 /***************************************************************************
2 * This file is part of Robocut. *
3 * Copyright (C) 2010 Markus Schulz <[email protected]> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
20
21 #include "PathSorter.h"
22 #include <cmath>
23 #include <QSvgRenderer>
24 #include <iostream>
25
26 using namespace std;
27
28 PathSorter::PathSorter ( ) {}
29 PathSorter::PathSorter (const QList<QPolygonF> inpaths, qreal mediaheight )
30 {
31 getQList(inpaths);
32 mediaHeight = mediaheight;
33 }
34 PathSorter::~PathSorter ( ) { }
35
36 void PathSorter::getQList (const QList<QPolygonF> inpaths)
37 {
38 pathToSort = QList<QPolygonF>(inpaths);
39 }
40
41 void PathSorter::setMediaHeight(qreal mediaheight)
42 {
43 mediaHeight = mediaheight;
44 }
45
46 QList<QPolygonF> PathSorter::UnSort (const QList<QPolygonF> inpaths)
47 {
48 QList<QPolygonF> outpaths = QList<QPolygonF>(inpaths);
49 return outpaths;
50 }
51
52 bool PathSorter::MyLessThan(const QPolygonF &p1, const QPolygonF &p2)
53 {
54 qreal testx1 = p1[0].x();
55 qreal testx2 = p2[0].x();
56 qreal testy1 = p1[0].y();
57 qreal testy2 = p2[0].y();
58 if(testy1 == testy2) return testx1 > testx2;
59 else return testy1 > testy2;
60 }
61
62 QList<QPolygonF> PathSorter::Sort (const QList<QPolygonF> inpaths)
63 {
64 QList<QPolygonF> outpaths = QList<QPolygonF>(inpaths);
65 qSort(outpaths.begin(), outpaths.end(), MyLessThan);
66 return outpaths;
67 }
68
69 QList<QPolygonF> PathSorter::BestSort (const QList<QPolygonF> inpaths)
70 {
71 qreal inpath = getTotalDistance(inpaths);
72 qreal sortpath = inpath;
73 qreal tsppath = inpath, temppath;
74 qreal tsppath2 = inpath;
75 cout<<"in path: "<< inpath << endl;
76
77 QList<QPolygonF> outpathSort = Sort(QList<QPolygonF>(inpaths));
78 sortpath = getTotalDistance(outpathSort);
79 cout<<"Sort outpath: "<< sortpath << endl;
80
81 int bestpath=1;
82 for (int i = 1; i < inpaths.size()/2; i++)
83 {
84 QList<QPolygonF> outpathTSP = GroupTSP(QList<QPolygonF>(inpaths),i);
85 temppath = getTotalDistance(outpathTSP);
86 //cout<<"xTSPSort outpaths: "<< temppath << " Groups " << i <<endl;
87 if (temppath < tsppath)
88 {
89 bestpath = i;
90 tsppath = temppath;
91 }
92
93 }
94 QList<QPolygonF> outpathTSP = GroupTSP(QList<QPolygonF>(inpaths),bestpath);
95 tsppath = getTotalDistance(outpathTSP);
96 cout<<"TSPSort outpaths: "<< tsppath << " Groups " << bestpath <<endl;
97
98
99 int bestpath2=1;
100 for (int i = 1; i < outpathSort.size()/2; i++)
101 {
102 QList<QPolygonF> outpathTSP2 = GroupTSP(QList<QPolygonF>(outpathSort),i);
103 temppath = getTotalDistance(outpathTSP2);
104 //cout<<"xxTSPSort outpaths: "<< temppath << " Groups " << i <<endl;
105 if (temppath < tsppath2)
106 {
107 bestpath2 = i;
108 tsppath2 = temppath;
109 }
110
111 }
112 QList<QPolygonF> outpathTSP2 = GroupTSP(QList<QPolygonF>(outpathSort),bestpath2);
113 tsppath2 = getTotalDistance(outpathTSP2);
114 cout<<"TSPSort outpath sort: "<< tsppath2 << " Groups " << bestpath2 <<endl;
115
116 if(tsppath < tsppath2 && tsppath < tsppath2) return outpathTSP;
117 if(tsppath2 < tsppath && tsppath2 < tsppath) return outpathTSP2;
118 if(tsppath2 < sortpath && tsppath2 < inpath) return outpathTSP2;
119 if(sortpath < inpath && sortpath < tsppath2) return outpathSort;
120 if(inpath < sortpath && inpath < tsppath2 ) return inpaths;
121 if(tsppath < sortpath && tsppath < inpath) return outpathTSP;
122 if(sortpath < inpath && sortpath < tsppath) return outpathSort;
123 if(inpath < sortpath && inpath < tsppath ) return inpaths;
124 if(tsppath2 < tsppath) return outpathTSP2;
125 if(tsppath < sortpath) return outpathTSP;
126 if(sortpath < tsppath) return outpathSort;
127 return inpaths;
128 }
129
130 QList<QPolygonF> PathSorter::BbSort (const QList<QPolygonF> inpaths)
131 {
132 QList<QPolygonF> outpaths = QList<QPolygonF>(inpaths);
133 for (int i = 0; i < (outpaths.size()-1); i++)
134 {
135 for (int j = (i+1); j < outpaths.size(); j++)
136 {
137 if (outpaths[i].boundingRect().intersects(outpaths[j].boundingRect()))
138 {
139 if (outpaths[i].boundingRect().width() > outpaths[j].boundingRect().width() || outpaths[i].boundingRect().height() > outpaths[j].boundingRect().height())
140 {
141 outpaths.swap(i,j);
142 break;
143 }
144 }
145 }
146 }
147 cout<<"BbSort outpaths: "<< getTotalDistance(outpaths) << endl;
148 return outpaths;
149 }
150
151 QList<QPolygonF> PathSorter::GroupTSP(const QList<QPolygonF> inpaths, int groups)
152 {
153 if (groups > inpaths.size()) groups = (int)((double) inpaths.size()-((double)inpaths.size()*0.2));
154 if (groups < 1) groups = 1;
155 QList<QList< QPolygonF> > listlistpath;
156 QList<QPolygonF> temppaths;
157 int inps = inpaths.size();
158 int inpsparts = inps / groups;
159
160 for (int i = 0; i < inps; i++)
161 {
162 if(i>=inpsparts)
163 {
164 listlistpath.append(temppaths);
165 inpsparts += inps / groups;
166 temppaths = QList<QPolygonF>() ;
167 }
168 temppaths.append(inpaths[i]);
169 }
170 listlistpath.append(temppaths);
171
172 for (int i = 0; i < listlistpath.size(); i++)
173 {
174 listlistpath[i] = MyFakeTSP(listlistpath[i]);
175 }
176
177 QList<QPolygonF> outpaths;
178 for (int i = 0; i < listlistpath.size(); i++)
179 {
180 for (int j = 0; j < listlistpath[i].size(); j++)
181 {
182 outpaths.append(listlistpath[i][j]);
183 }
184 }
185 //cout<<"xTSPSort outpaths: "<< getTotalDistance(outpaths) << " Groups " << groups <<endl;
186 return outpaths;
187 }
188
189 qreal PathSorter::getDistance(const QPolygonF &p1, const QPolygonF &p2)
190 {
191 qreal testx1 = p1.last().x();
192 qreal testy1 = p1.last().y();
193 qreal testx2 = p2.first().x();
194 qreal testy2 = p2.first().y();
195 qreal a = 0.0;
196 qreal b = 0.0;
197 double c = 0.0;
198
199 if(testx1 >= testx2) a = testx1 - testx2;
200 else a = testx2 - testx1;
201 if(testy1 >= testy2) b = testy1 - testy2;
202 else b = testy2 - testy1;
203 c = sqrt((double)(a*a+b*b));
204 return (qreal) c;
205 }
206
207 qreal PathSorter::getTotalDistance(const QList<QPolygonF> inpaths, int maxdepth)
208 {
209 if (maxdepth >= inpaths.size()-1) maxdepth = inpaths.size()-1;
210 if (maxdepth <= 0) maxdepth = inpaths.size()-1;
211 QPolygonF zero = QPolygonF(QRectF(0.0,mediaHeight,0.0,0.0)); // able to change the start point
212 qreal dist = getDistance(zero,inpaths[0]);
213 for (int i = 0; i < maxdepth; ++i)
214 {
215 dist = dist + getDistance(inpaths[i],inpaths[(i+1)]);
216 }
217 return dist;
218 }
219
220 QList<QPolygonF> PathSorter::MyFakeTSP(const QList<QPolygonF> inpaths)
221 {
222 QPolygonF zero = QPolygonF(QRectF(0.0,mediaHeight,0.0,0.0)); // able to change the start point
223
224 QList<QPolygonF> outpaths = QList<QPolygonF>(inpaths);
225
226 // find the shortest path
227 for (int i = 0; i < (outpaths.size()-1); ++i)
228 {
229 if(i == 0) // find good start
230 {
231 qreal dist=10000.0;
232 int bestindex=i;
233 for (int j = (i+1); j < outpaths.size(); ++j)
234 {
235 if (getDistance(zero,outpaths[j]) < dist)
236 {
237 dist = getDistance(zero,outpaths[j]);
238 bestindex = j;
239 }
240
241 }
242 if (dist != 0) outpaths.swap(0,bestindex);
243 }
244 qreal dist=10000.0;
245 int bestindex=i;
246 for (int j = (i+1); j < outpaths.size(); ++j)
247 {
248 if (getDistance(outpaths[i],outpaths[j]) < dist)
249 {
250 dist = getDistance(outpaths[i],outpaths[j]);
251 bestindex = j;
252 }
253 }
254 if (dist != 0) outpaths.swap((i+1),bestindex);
255 }
256 return outpaths;
257 }