/*************************************************************************** * This file is part of Robocut. * * Copyright (C) 2010 Markus Schulz * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 3 of the License, or * * (at your option) any later version. * * * * This program 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 General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ #include "PathSorter.h" #include #include #include using namespace std; PathSorter::PathSorter ( ) {} PathSorter::PathSorter (const QList inpaths, qreal mediaheight ) { getQList(inpaths); mediaHeight = mediaheight; } PathSorter::~PathSorter ( ) { } void PathSorter::getQList (const QList inpaths) { pathToSort = QList(inpaths); } void PathSorter::setMediaHeight(qreal mediaheight) { mediaHeight = mediaheight; } QList PathSorter::UnSort (const QList inpaths) { QList outpaths = QList(inpaths); return outpaths; } bool PathSorter::MyLessThan(const QPolygonF &p1, const QPolygonF &p2) { qreal testx1 = p1[0].x(); qreal testx2 = p2[0].x(); qreal testy1 = p1[0].y(); qreal testy2 = p2[0].y(); if(testy1 == testy2) return testx1 > testx2; else return testy1 > testy2; } QList PathSorter::Sort (const QList inpaths) { QList outpaths = QList(inpaths); qSort(outpaths.begin(), outpaths.end(), MyLessThan); return outpaths; } QList PathSorter::BestSort (const QList inpaths) { qreal inpath = getTotalDistance(inpaths); qreal sortpath = inpath; qreal tsppath = inpath, temppath; qreal tsppath2 = inpath; cout<<"in path: "<< inpath << endl; QList outpathSort = Sort(QList(inpaths)); sortpath = getTotalDistance(outpathSort); cout<<"Sort outpath: "<< sortpath << endl; int bestpath=1; for (int i = 1; i < inpaths.size()/2; i++) { QList outpathTSP = GroupTSP(QList(inpaths),i); temppath = getTotalDistance(outpathTSP); //cout<<"xTSPSort outpaths: "<< temppath << " Groups " << i < outpathTSP = GroupTSP(QList(inpaths),bestpath); tsppath = getTotalDistance(outpathTSP); cout<<"TSPSort outpaths: "<< tsppath << " Groups " << bestpath < outpathTSP2 = GroupTSP(QList(outpathSort),i); temppath = getTotalDistance(outpathTSP2); //cout<<"xxTSPSort outpaths: "<< temppath << " Groups " << i < outpathTSP2 = GroupTSP(QList(outpathSort),bestpath2); tsppath2 = getTotalDistance(outpathTSP2); cout<<"TSPSort outpath sort: "<< tsppath2 << " Groups " << bestpath2 < PathSorter::BbSort (const QList inpaths) { QList outpaths = QList(inpaths); for (int i = 0; i < (outpaths.size()-1); i++) { for (int j = (i+1); j < outpaths.size(); j++) { if (outpaths[i].boundingRect().intersects(outpaths[j].boundingRect())) { if (outpaths[i].boundingRect().width() > outpaths[j].boundingRect().width() || outpaths[i].boundingRect().height() > outpaths[j].boundingRect().height()) { outpaths.swap(i,j); break; } } } } cout<<"BbSort outpaths: "<< getTotalDistance(outpaths) << endl; return outpaths; } QList PathSorter::GroupTSP(const QList inpaths, int groups) { if (groups > inpaths.size()) groups = (int)((double) inpaths.size()-((double)inpaths.size()*0.2)); if (groups < 1) groups = 1; QList > listlistpath; QList temppaths; int inps = inpaths.size(); int inpsparts = inps / groups; for (int i = 0; i < inps; i++) { if(i>=inpsparts) { listlistpath.append(temppaths); inpsparts += inps / groups; temppaths = QList() ; } temppaths.append(inpaths[i]); } listlistpath.append(temppaths); for (int i = 0; i < listlistpath.size(); i++) { listlistpath[i] = MyFakeTSP(listlistpath[i]); } QList outpaths; for (int i = 0; i < listlistpath.size(); i++) { for (int j = 0; j < listlistpath[i].size(); j++) { outpaths.append(listlistpath[i][j]); } } //cout<<"xTSPSort outpaths: "<< getTotalDistance(outpaths) << " Groups " << groups <= testx2) a = testx1 - testx2; else a = testx2 - testx1; if(testy1 >= testy2) b = testy1 - testy2; else b = testy2 - testy1; c = sqrt((double)(a*a+b*b)); return (qreal) c; } qreal PathSorter::getTotalDistance(const QList inpaths, int maxdepth) { if (maxdepth >= inpaths.size()-1) maxdepth = inpaths.size()-1; if (maxdepth <= 0) maxdepth = inpaths.size()-1; QPolygonF zero = QPolygonF(QRectF(0.0,mediaHeight,0.0,0.0)); // able to change the start point qreal dist = getDistance(zero,inpaths[0]); for (int i = 0; i < maxdepth; ++i) { dist = dist + getDistance(inpaths[i],inpaths[(i+1)]); } return dist; } QList PathSorter::MyFakeTSP(const QList inpaths) { QPolygonF zero = QPolygonF(QRectF(0.0,mediaHeight,0.0,0.0)); // able to change the start point QList outpaths = QList(inpaths); // find the shortest path for (int i = 0; i < (outpaths.size()-1); ++i) { if(i == 0) // find good start { qreal dist=10000.0; int bestindex=i; for (int j = (i+1); j < outpaths.size(); ++j) { if (getDistance(zero,outpaths[j]) < dist) { dist = getDistance(zero,outpaths[j]); bestindex = j; } } if (dist != 0) outpaths.swap(0,bestindex); } qreal dist=10000.0; int bestindex=i; for (int j = (i+1); j < outpaths.size(); ++j) { if (getDistance(outpaths[i],outpaths[j]) < dist) { dist = getDistance(outpaths[i],outpaths[j]); bestindex = j; } } if (dist != 0) outpaths.swap((i+1),bestindex); } return outpaths; }