mapeditor.cpp
changeset 794 d922fb6ea482
parent 793 cac93797c580
child 795 6b0a5f4923d3
     1.1 --- a/mapeditor.cpp	Wed Sep 09 12:57:06 2009 +0000
     1.2 +++ b/mapeditor.cpp	Fri Sep 11 12:56:15 2009 +0000
     1.3 @@ -560,7 +560,7 @@
     1.4  void MapEditor::testFunction2()
     1.5  {
     1.6  	// Create list with all bounding polygons
     1.7 -	QList <MapObj*> mapobjects;
     1.8 +	QList <LinkableMapObj*> mapobjects;
     1.9  	QList <ConvexPolygon> polys; 
    1.10  	ConvexPolygon p;
    1.11  	QList <Vector> vectors;
    1.12 @@ -570,25 +570,16 @@
    1.13  	BranchItem *bi;
    1.14  	BranchItem *bi2;
    1.15  	BranchObj *bo;
    1.16 -	BranchItem *ri=model->getRootItem();
    1.17 -	for (int i=0;i<ri->branchCount();++i)
    1.18 +
    1.19 +	// Outer loop: Iterate until we no more changes in orientation 
    1.20 +	bool orientationChanged=true;
    1.21 +	while (orientationChanged)
    1.22  	{
    1.23 -		bi=ri->getBranchNum (i);
    1.24 -		bo=(BranchObj*)bi->getLMO();
    1.25 -		if (bo)
    1.26 +		BranchItem *ri=model->getRootItem();
    1.27 +		for (int i=0;i<ri->branchCount();++i)
    1.28  		{
    1.29 -			mapobjects.append (bo);
    1.30 -			p=bo->getBoundingPolygon();
    1.31 -			p.calcCentroid();
    1.32 -			polys.append(p);
    1.33 -			vectors.append (QPointF(0,0));
    1.34 -			orgpos.append (p.at(0));
    1.35 -			headings.append (bi->getHeading());
    1.36 -		}
    1.37 -		for (int j=0;j<bi->branchCount();++j)
    1.38 -		{
    1.39 -			bi2=bi->getBranchNum (j);
    1.40 -			bo=(BranchObj*)bi2->getLMO();
    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 @@ -597,57 +588,85 @@
    1.47  				polys.append(p);
    1.48  				vectors.append (QPointF(0,0));
    1.49  				orgpos.append (p.at(0));
    1.50 -				headings.append (bi2->getHeading());
    1.51 -			}	
    1.52 -		}
    1.53 -	}
    1.54 -
    1.55 -	// Iterate moving bounding polygons until we have no more collisions
    1.56 -	int collisions=1;
    1.57 -	while (collisions>0)
    1.58 -	{
    1.59 -		collisions=0;
    1.60 -		for (int i=0; i<polys.size()-1; ++i)
    1.61 -		{
    1.62 -			for (int j=i+1; j<polys.size();++j)
    1.63 +				headings.append (bi->getHeading());
    1.64 +			}
    1.65 +			for (int j=0;j<bi->branchCount();++j)
    1.66  			{
    1.67 -				if (polygonCollision (polys.at(i),polys.at(j), QPointF(0,0)).intersect )
    1.68 +				bi2=bi->getBranchNum (j);
    1.69 +				bo=(BranchObj*)bi2->getLMO();
    1.70 +				if (bo)
    1.71  				{
    1.72 -					collisions++;
    1.73 -					v=polys.at(j).centroid()-polys.at(i).centroid();
    1.74 -					// Move also away if centroids are identical
    1.75 -					if (v.isNull()) 
    1.76 -					{
    1.77 -						//cout << "v==0="<<polys[i].centroid()<<polys[j].centroid()<<" "<<v<<"  "<<headings[i].toStdString()<<" - "<<headings[j].toStdString()<<"  ";
    1.78 -						v.setX (rand()%200 -100);
    1.79 -						v.setY (rand()%200 -100);
    1.80 -						//cout << v;
    1.81 -					}
    1.82 -					v.normalize();
    1.83 -					v.scale (2);
    1.84 -					//cout <<  "  "<<v<<endl;
    1.85 -					vectors[j]=v;
    1.86 -					vectors[i]=v;
    1.87 -					vectors[i].invert();
    1.88 -				}  
    1.89 +					mapobjects.append (bo);
    1.90 +					p=bo->getBoundingPolygon();
    1.91 +					p.calcCentroid();
    1.92 +					polys.append(p);
    1.93 +					vectors.append (QPointF(0,0));
    1.94 +					orgpos.append (p.at(0));
    1.95 +					headings.append (bi2->getHeading());
    1.96 +				}	
    1.97  			}
    1.98  		}
    1.99 -		for (int i=0;i<vectors.size();i++)
   1.100 +
   1.101 +		// Iterate moving bounding polygons until we have no more collisions
   1.102 +		int collisions=1;
   1.103 +		while (collisions>0)
   1.104  		{
   1.105 -			//cout << " v="<<vectors[i]<<" "<<headings[i].toStdString()<<endl;
   1.106 -			polys[i].translate (vectors[i]);
   1.107 -		}
   1.108 -		//cout << "Collisions: "<<collisions<<endl;
   1.109 -		//collisions=0;
   1.110 -	}	
   1.111 +			collisions=0;
   1.112 +			for (int i=0; i<polys.size()-1; ++i)
   1.113 +			{
   1.114 +				for (int j=i+1; j<polys.size();++j)
   1.115 +				{
   1.116 +					if (polygonCollision (polys.at(i),polys.at(j), QPointF(0,0)).intersect )
   1.117 +					{
   1.118 +						collisions++;
   1.119 +						//cout << "Collision: "<<headings[i].toStdString()<<" - "<<headings[j].toStdString()<<endl;
   1.120 +						v=polys.at(j).centroid()-polys.at(i).centroid();
   1.121 +						// Move also away if centroids are identical
   1.122 +						if (v.isNull()) 
   1.123 +						{
   1.124 +							//cout << "v==0="<<polys[i].centroid()<<polys[j].centroid()<<" "<<v<<endl;
   1.125 +							v.setX (rand()%200 -100);
   1.126 +							v.setY (rand()%200 -100);
   1.127 +							//cout << v;
   1.128 +						}
   1.129 +						v.normalize();
   1.130 +						v.scale (2);
   1.131 +						//cout <<  "  v="<<v<<endl;
   1.132 +						vectors[j]=v;
   1.133 +						vectors[i]=v;
   1.134 +						vectors[i].invert();
   1.135 +					}  
   1.136 +				}
   1.137 +			}
   1.138 +			for (int i=0;i<vectors.size();i++)
   1.139 +			{
   1.140 +				//cout << " v="<<vectors[i]<<" "<<headings[i].toStdString()<<endl;
   1.141 +				polys[i].translate (vectors[i]);
   1.142 +			}
   1.143 +			cout << "Collisions: "<<collisions<<endl;
   1.144 +			//collisions=0;
   1.145 +		}	
   1.146  
   1.147 -	// Finally move the real objects and update 
   1.148 -	for (int i=0;i<polys.size();i++)
   1.149 -	{
   1.150 -		Vector v=polys[i].at(0)-orgpos[i];
   1.151 -		mapobjects[i]->moveBy(v.x(),v.y() );
   1.152 -	}	
   1.153 -	model->reposition();	
   1.154 +		// Finally move the real objects and update 
   1.155 +		QList <LinkableMapObj::Orientation> orients;
   1.156 +		for (int i=0;i<polys.size();i++)
   1.157 +		{
   1.158 +			Vector v=polys[i].at(0)-orgpos[i];
   1.159 +			orients.append (mapobjects[i]->getOrientation());
   1.160 +			mapobjects[i]->moveBy(v.x(),v.y() );
   1.161 +		}	
   1.162 +		model->reposition();	
   1.163 +		orientationChanged=false;
   1.164 +		for (int i=0;i<polys.size();i++)
   1.165 +			if (orients[i]!=mapobjects[i]->getOrientation())
   1.166 +			{
   1.167 +				orientationChanged=true;
   1.168 +				break;
   1.169 +			}
   1.170 +		cout << "Final: orientChanged="<<orientationChanged<<endl;
   1.171 +		break;
   1.172 +		//orientationChanged=false;
   1.173 +	} // loop if orientation has changed
   1.174  }
   1.175  
   1.176  BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
   1.177 @@ -1190,6 +1209,7 @@
   1.178  					lmosel->setRelPos();	
   1.179  				} 
   1.180  
   1.181 +			} // depth>0
   1.182  				// Maybe we can relink temporary?
   1.183  				if (dsti)
   1.184  				{
   1.185 @@ -1208,7 +1228,6 @@
   1.186  				}		
   1.187  				// reposition subbranch
   1.188  				lmosel->reposition();	
   1.189 -			} // depth>0
   1.190  
   1.191  			QItemSelection sel=model->getSelectionModel()->selection();
   1.192  			updateSelection(sel,sel);	// position has changed
   1.193 @@ -1278,7 +1297,11 @@
   1.194  			tmpXLink->setEnd ( ((BranchItem*)dsti) );
   1.195  			tmpXLink->updateXLink();
   1.196  			tmpXLink->activate(); 
   1.197 -			//FIXME-0 model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );	
   1.198 +			model->saveState(
   1.199 +				tmpXLink,QString("delete ()"),
   1.200 +				dsti,QString("addXLink (\"%1\",\"%2\")").arg(model->getSelectString(tmpXLink->getBegin())).arg(model->getSelectString(dsti)),
   1.201 +				QString("Add xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(dsti)) 
   1.202 +			);	
   1.203  		} else
   1.204  		{
   1.205  			model->deleteItem(tmpXLink);