1.1 --- a/mapeditor.cpp Mon Sep 07 15:36:57 2009 +0000
1.2 +++ b/mapeditor.cpp Tue Sep 08 12:15:39 2009 +0000
1.3 @@ -7,6 +7,7 @@
1.4 #include <QObject>
1.5
1.6 #include "branchitem.h"
1.7 +#include "geometry.h"
1.8 #include "mainwindow.h"
1.9 #include "misc.h"
1.10 #include "warningdialog.h"
1.11 @@ -545,10 +546,6 @@
1.12 }
1.13 cout <<" hidemode="<<hidemode<<endl;
1.14 */
1.15 -}
1.16 -
1.17 -void MapEditor::testFunction2()
1.18 -{
1.19
1.20 /*
1.21 // Toggle hidemode
1.22 @@ -557,6 +554,73 @@
1.23 else
1.24 setHideTmpMode (HideExport);
1.25 */
1.26 +
1.27 +}
1.28 +
1.29 +void MapEditor::testFunction2()
1.30 +{
1.31 + // Create list with all bounding polygons
1.32 + QList <MapObj*> mapobjects;
1.33 + QList <ConvexPolygon> polys;
1.34 + QList <Vector> vectors;
1.35 + QList <Vector> orgpos;
1.36 + BranchItem *bi;
1.37 + BranchObj *bo;
1.38 + TreeItem *ri=model->getRootItem();
1.39 + for (int i=0;i<ri->branchCount();++i)
1.40 + {
1.41 + bi=ri->getBranchNum (i);
1.42 + bo=(BranchObj*)bi->getLMO();
1.43 + if (bo)
1.44 + {
1.45 + mapobjects.append (bo);
1.46 + polys.append(bo->getBoundingPolygon());
1.47 + polys[i].calcCentroid();
1.48 + vectors.append (QPointF(0,0));
1.49 + orgpos.append (polys[i].at(0));
1.50 + }
1.51 + }
1.52 +
1.53 + // Iterate moving bounding polygons until we have no more collisions
1.54 + int collisions=1;
1.55 + while (collisions>0)
1.56 + {
1.57 + collisions=0;
1.58 + for (int i=0; i<polys.size()-1; ++i)
1.59 + {
1.60 + for (int j=i+1; j<polys.size();++j)
1.61 + {
1.62 + if (polygonCollision (polys.at(i),polys.at(j), QPointF(0,0)).intersect )
1.63 + {
1.64 + collisions++;
1.65 + Vector v=polys.at(j).centroid()-polys.at(i).centroid();
1.66 + // Move also away if centroids are identical
1.67 + if (v.isNull())
1.68 + {
1.69 + v.setX (rand()%200 -100);
1.70 + v.setY (rand()%200 -100);
1.71 + }
1.72 + v.normalize();
1.73 + cout << "v="<<v<<endl;
1.74 + v.scale (2);
1.75 + vectors[j]=v;
1.76 + vectors[i]=v;
1.77 + vectors[i].invert();
1.78 + }
1.79 + }
1.80 + }
1.81 + for (int i=0;i<vectors.size();i++)
1.82 + polys[i].translate (vectors[i]);
1.83 + //cout << "Collisions: "<<collisions<<endl;
1.84 + }
1.85 +
1.86 + // Finally move the real objects and update
1.87 + for (int i=0;i<polys.size();i++)
1.88 + {
1.89 + Vector v=polys[i].at(0)-orgpos[i];
1.90 + mapobjects[i]->moveBy(v.x(),v.y() );
1.91 + }
1.92 + model->reposition();
1.93 }
1.94
1.95 BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
1.96 @@ -1050,8 +1114,8 @@
1.97 if ( e->modifiers()==Qt::ShiftModifier && dsti && dsti != seli->parent() )
1.98 {
1.99 // Also save the move which was done so far
1.100 - QString pold=qpointfToString(movingObj_orgRelPos);
1.101 - QString pnow=qpointfToString(fio->getRelPos());
1.102 + QString pold=qpointFToString(movingObj_orgRelPos);
1.103 + QString pnow=qpointFToString(fio->getRelPos());
1.104 model->saveState(
1.105 seli,
1.106 "moveRel "+pold,
1.107 @@ -1205,8 +1269,8 @@
1.108 if(fio)
1.109 {
1.110 // Moved FloatObj. Maybe we need to reposition
1.111 - QString pold=qpointfToString(movingObj_orgRelPos);
1.112 - QString pnow=qpointfToString(fio->getRelPos());
1.113 + QString pold=qpointFToString(movingObj_orgRelPos);
1.114 + QString pnow=qpointFToString(fio->getRelPos());
1.115 model->saveState(
1.116 seli,
1.117 "moveRel "+pold,
1.118 @@ -1225,8 +1289,8 @@
1.119 {
1.120 if (movingObj_orgPos != bi->getBranchObj()->getAbsPos()) // FIXME-3 check getBO here...
1.121 {
1.122 - QString pold=qpointfToString(movingObj_orgPos);
1.123 - QString pnow=qpointfToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1.124 + QString pold=qpointFToString(movingObj_orgPos);
1.125 + QString pnow=qpointFToString(bi->getBranchObj()->getAbsPos()); // FIXME-3 check getBO here...
1.126
1.127 model->saveState(
1.128 bi,
1.129 @@ -1311,9 +1375,9 @@
1.130 QPointF rp(lmosel->getRelPos());
1.131 if (rp != movingObj_orgRelPos)
1.132 {
1.133 - QString ps=qpointfToString(rp);
1.134 + QString ps=qpointFToString(rp);
1.135 model->saveState(
1.136 - model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos),
1.137 + model->getSelectString(lmosel), "moveRel "+qpointFToString(movingObj_orgRelPos),
1.138 preSelStr, "moveRel "+ps,
1.139 QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
1.140 }