Main Page | Namespace List | Class Hierarchy | Compound List | File List | Namespace Members | Compound Members | File Members

PlToSP.cxx

Go to the documentation of this file.
00001 /**************************************************************************
00002 ***    
00003 *** Copyright (c) 1995-2000 Regents of the University of California,
00004 ***               Andrew E. Caldwell, Andrew B. Kahng and Igor L. Markov
00005 *** Copyright (c) 2000-2004 Regents of the University of Michigan,
00006 ***               Saurabh N. Adya, Jarrod A. Roy and Igor L. Markov
00007 ***
00008 ***  Contact author(s): abk@cs.ucsd.edu, imarkov@umich.edu
00009 ***  Original Affiliation:   UCLA, Computer Science Department,
00010 ***                          Los Angeles, CA 90095-1596 USA
00011 ***
00012 ***  Permission is hereby granted, free of charge, to any person obtaining 
00013 ***  a copy of this software and associated documentation files (the
00014 ***  "Software"), to deal in the Software without restriction, including
00015 ***  without limitation 
00016 ***  the rights to use, copy, modify, merge, publish, distribute, sublicense, 
00017 ***  and/or sell copies of the Software, and to permit persons to whom the 
00018 ***  Software is furnished to do so, subject to the following conditions:
00019 ***
00020 ***  The above copyright notice and this permission notice shall be included
00021 ***  in all copies or substantial portions of the Software.
00022 ***
00023 *** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
00024 *** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00025 *** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
00026 *** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
00027 *** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
00028 *** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
00029 *** THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00030 ***
00031 ***
00032 ***************************************************************************/
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 
00051 #include "FPcommon.h"
00052 #include "PlToSP.h"
00053 using namespace parquetfp;
00054 
00055 Pl2SP::Pl2SP(vector<double>& xloc, vector<double>& yloc, 
00056              vector<double>& widths, vector<double>& heights, 
00057              PL2SP_ALGO whichAlgo) : _xloc(xloc), _yloc(yloc), 
00058                _widths(widths), _heights(heights)
00059 {
00060   unsigned size = _xloc.size();
00061   if(_yloc.size() != size || _widths.size() != size || _heights.size() != size)
00062     cout<<"ERROR: in Pl2SP. Sizes do not match"<<endl;
00063 
00064   switch(whichAlgo)
00065     {
00066     case NAIVE_ALGO :
00067       naiveAlgo();
00068       break;
00069     case TCG_ALGO:
00070       TCGAlgo();
00071       break;
00072     }
00073 }
00074 
00075 void Pl2SP::naiveAlgo(void)
00076 {
00077   unsigned size = _xloc.size();
00078   double rowHeight = 1e100;
00079   double maxYLoc = -1e100;
00080   
00081   for(unsigned i=0; i<size; ++i)
00082    {
00083      if(rowHeight > _heights[i])
00084        rowHeight = _heights[i];
00085        
00086      if(maxYLoc < _yloc[i])
00087        maxYLoc = _yloc[i];
00088    }
00089 
00090   unsigned numRows = unsigned(ceil(maxYLoc/rowHeight)+1);
00091 
00092   //snap to y grid here
00093   for(unsigned i=0; i<size; ++i)
00094    {
00095      unsigned reqdRow = static_cast<unsigned>((_yloc[i]/rowHeight)+0.5);
00096      //unsigned reqdRow = unsigned(rint(_yloc[i]/rowHeight));
00097      _yloc[i] = reqdRow*rowHeight;
00098    }
00099   
00100   vector< vector<RowElem> > rows;
00101   vector<RowElem> singleRow;
00102   RowElem tempRE;
00103   
00104   double currHeight = 0;
00105   
00106   for(unsigned i=0; i<numRows; ++i)
00107    {
00108      for(unsigned j=0; j<size; ++j)
00109        { 
00110          if(fabs(_yloc[j]-currHeight)<0.0001)
00111            {
00112              tempRE.index = j;
00113              tempRE.xloc = _xloc[j];
00114              singleRow.push_back(tempRE);
00115            }
00116        }
00117        
00118      currHeight += rowHeight;
00119 
00120      std::stable_sort(singleRow.begin(), singleRow.end(), less_mag());
00121      rows.push_back(singleRow);
00122      singleRow.clear();
00123    }
00124 
00125    //form the X and Y sequence pairs now
00126    for(unsigned i=0; i<rows.size(); ++i)
00127     {
00128       for(unsigned j=0; j<rows[i].size(); ++j)
00129         {
00130           _YY.push_back(rows[i][j].index);
00131         }
00132     }
00133    for(int i=rows.size()-1; i>=0; --i)
00134     {
00135       for(unsigned j=0; j<rows[i].size(); ++j)
00136         {
00137           _XX.push_back(rows[i][j].index);
00138         }
00139     }
00140 
00141   if(_XX.size() != _xloc.size() || _YY.size() != _xloc.size())
00142    {
00143     cout<<"ERROR: generated sequence pair of not correct sizes "<<_XX.size()<<" & "<<_YY.size()<<" vs "<<size<<endl;
00144     print();
00145    }
00146     
00147 }
00148 
00149 void Pl2SP::TCGAlgo(void)
00150 {
00151   unsigned size = _xloc.size();
00152 
00153   vector< vector<bool> > TCGMatrixVert;
00154   vector< vector<bool> > TCGMatrixHoriz;
00155 
00156   TCGMatrixVert.resize(size);
00157   TCGMatrixHoriz.resize(size);
00158 
00159   for(unsigned i=0; i<size; ++i)
00160     {
00161       TCGMatrixVert[i].resize(size, false);
00162       TCGMatrixHoriz[i].resize(size, false);
00163     }
00164 
00165   double ebsilon = 1e100;
00166   for(unsigned i=0; i<size; ++i) //pick min dimension
00167     {
00168       if(ebsilon > _widths[i])
00169         ebsilon = _widths[i];
00170       if(ebsilon > _heights[i])
00171         ebsilon = _heights[i];
00172     }
00173   ebsilon /= 1000;
00174 
00175   //set up the immediate constraints
00176   for(unsigned i=0; i<size; ++i)
00177     {
00178       for(unsigned j=0; j<=i; ++j)
00179         {
00180           double iXStart, iXEnd, jXStart, jXEnd;
00181           double iYStart, iYEnd, jYStart, jYEnd;
00182 
00183           double horizOverlap = 0;
00184           double vertOverlap = 0;
00185           unsigned vertOverlapDir = 0;
00186           unsigned horizOverlapDir = 0;
00187 
00188 
00189           if (i == j)
00190             {
00191               TCGMatrixHoriz[i][j] = 1;
00192               TCGMatrixVert[i][j] = 1;
00193               continue;
00194             }
00195 
00196           TCGMatrixHoriz[i][j] = 0;
00197           TCGMatrixVert[i][j] = 0;
00198           TCGMatrixHoriz[j][i] = 0;
00199           TCGMatrixVert[j][i] = 0;
00200 
00201           iXStart = _xloc[i];
00202           iXEnd = _xloc[i] + _widths[i];
00203           jXStart = _xloc[j];
00204           jXEnd = _xloc[j] + _widths[j];
00205 
00206           iYStart = _yloc[i];
00207           iYEnd = _yloc[i] + _heights[i];
00208           jYStart = _yloc[j];
00209           jYEnd = _yloc[j] + _heights[j];
00210 
00211           //horizontal constraint
00212           if(jYStart < iYStart + ebsilon && jYEnd < iYEnd + ebsilon &&
00213              jYEnd > iYStart + ebsilon)
00214             {
00215               vertOverlap = jYEnd - iYStart; //lower overlap
00216               vertOverlapDir = 0; 
00217             }
00218           else if(jYStart > iYStart + ebsilon && jYEnd < iYEnd + ebsilon)
00219             {
00220               vertOverlap = jYEnd - jYStart; //inner overlap
00221               if(iYEnd-jYEnd > jYStart-iYStart)
00222                 vertOverlapDir = 0;
00223               else
00224                 vertOverlapDir = 1;
00225             }
00226           else if(jYStart > iYStart + ebsilon && jYStart < iYEnd + ebsilon &&
00227                   jYEnd > iYEnd + ebsilon)
00228             {
00229               vertOverlap = iYEnd - jYStart; //upper overlap
00230               vertOverlapDir = 1;
00231             }
00232           else if(jYStart < iYStart + ebsilon && jYEnd > iYEnd + ebsilon)
00233             {
00234               vertOverlap = iYEnd - iYStart; //outer overlap
00235               if(jYEnd-iYEnd  > iYStart-jYStart)
00236                 vertOverlapDir = 1;
00237               else
00238                 vertOverlapDir = 0;
00239             }
00240           else
00241             TCGMatrixHoriz[i][j] = 0;
00242 
00243 
00244           //vertical constraint
00245           if(jXStart < iXStart + ebsilon && jXEnd < iXEnd + ebsilon &&
00246              jXEnd > iXStart + ebsilon)
00247             {
00248               horizOverlap = jXEnd - iXStart; //right overlap
00249               horizOverlapDir = 0; 
00250             }
00251           else if(jXStart > iXStart + ebsilon && jXEnd < iXEnd + ebsilon)
00252             {
00253               horizOverlap = jXEnd - jXStart; //inner overlap
00254               if(iXEnd-jXEnd > jXStart-iXStart)
00255                 horizOverlapDir = 0;
00256               else
00257                 horizOverlapDir = 1;
00258             }
00259           else if(jXStart > iXStart + ebsilon && jXStart < iXEnd + ebsilon &&
00260                   jXEnd > iXEnd + ebsilon)
00261             {
00262               horizOverlap = iXEnd - jXStart; //left overlap
00263               horizOverlapDir = 1;
00264             }
00265           else if(jXStart < iXStart + ebsilon && jXEnd > iXEnd + ebsilon)
00266             {
00267               horizOverlap = iXEnd - iXStart; //outer overlap
00268               if(jXEnd-iXEnd  > iXStart-jXStart )
00269                 horizOverlapDir = 1;
00270               else
00271                 horizOverlapDir = 0;
00272             }
00273           else
00274             TCGMatrixVert[i][j] = 0;
00275 
00276           if(vertOverlap > ebsilon && horizOverlap <= ebsilon)
00277             {
00278               if(iXStart <= jXStart)
00279                 TCGMatrixHoriz[i][j] = 1;
00280               else
00281                 TCGMatrixHoriz[j][i] = 1;
00282             }
00283           else if(horizOverlap > ebsilon && vertOverlap <= ebsilon)
00284             {
00285               if(iYStart <= jYStart)
00286                 TCGMatrixVert[i][j] = 1;
00287               else
00288                 TCGMatrixVert[j][i] = 1;
00289             }
00290           //overlapping
00291           else if(horizOverlap > ebsilon && vertOverlap > ebsilon)
00292             {
00293               if(vertOverlap >= horizOverlap)
00294                 {
00295                   if(horizOverlapDir == 1)
00296                     TCGMatrixHoriz[i][j] = 1;
00297                   else
00298                     TCGMatrixHoriz[j][i] = 1;
00299                 }
00300               else
00301                 {
00302                   if(vertOverlapDir == 1)
00303                     TCGMatrixVert[i][j] = 1;
00304                   else
00305                     TCGMatrixVert[j][i] = 1;
00306                 }
00307             }
00308         }
00309     }
00310 
00311   //floyd marshal to find transitive closure
00312   //TCG_FM(TCGMatrixHoriz, TCGMatrixVert);
00313 
00314   //dynamic programming DFS algo to find transitive closure
00315   TCG_DP(TCGMatrixHoriz, TCGMatrixVert);
00316 
00317   //find ties and break them
00318   for(unsigned i=0; i<size; ++i)
00319     {
00320       for(unsigned j=0; j<i; ++j)
00321         {
00322           if(i==j)
00323             continue;
00324           if(TCGMatrixHoriz[i][j] == 1 && TCGMatrixHoriz[j][i] == 1)
00325             {
00326               cout<<"ERROR in TCG 1 "<<i<<"\t"<<j<<endl;
00327             }
00328           if(TCGMatrixVert[i][j] == 1 && TCGMatrixVert[j][i] == 1)
00329             {
00330               cout<<"ERROR in TCG 2 "<<i<<"\t"<<j<<endl;
00331             }
00332           unsigned ctr = 0;
00333           if(TCGMatrixHoriz[i][j] == 1)
00334             ++ctr;
00335           if(TCGMatrixHoriz[j][i] == 1)
00336             ++ctr;
00337           if(TCGMatrixVert[i][j] == 1)
00338             ++ctr;
00339           if(TCGMatrixVert[j][i] == 1)
00340             ++ctr;
00341 
00342           if(ctr > 1)
00343             {
00344               unsigned dir = rand()%2;
00345               if(dir == 0) //H constraint
00346                 {
00347                   TCGMatrixVert[i][j] = 0;
00348                   TCGMatrixVert[j][i] = 0;
00349                 }
00350               else //V constraint
00351                 {
00352                   TCGMatrixHoriz[i][j] = 0;
00353                   TCGMatrixHoriz[j][i] = 0;
00354                 }
00355               /*
00356               cout<<"ERROR in TCG 3 "<<i<<"\t"<<j<<"\t";
00357               if(TCGMatrixHoriz[i][j] == 1)
00358                 cout<<"H\t";
00359               if(TCGMatrixHoriz[j][i] == 1)
00360                 cout<<"InvH\t";
00361               if(TCGMatrixVert[i][j] == 1)
00362                 cout<<"V\t";
00363               if(TCGMatrixVert[j][i] == 1)
00364                 cout<<"InvV\t";
00365               cout<<endl;
00366               */
00367             }
00368           
00369           //no constraint between the blocks
00370           if(TCGMatrixHoriz[i][j] == 0 && TCGMatrixHoriz[j][i] == 0 &&
00371              TCGMatrixVert[i][j] == 0 && TCGMatrixVert[j][i] == 0)
00372             {
00373               if(_xloc[i] < _xloc[j])
00374                 TCGMatrixHoriz[i][j] = 1;
00375               else
00376                 TCGMatrixHoriz[j][i] = 1;
00377 
00378               /*
00379                 unsigned dir = rand()%2;
00380                 if(dir == 0) //H constraint
00381                 {
00382                 if(_xloc[i] < _xloc[j])
00383                 TCGMatrixHoriz[i][j] = 1;
00384                 else
00385                 TCGMatrixHoriz[j][i] = 1;
00386                 }
00387                 else //V constraint
00388                 {
00389                 if(_yloc[i] < _yloc[j])
00390                 TCGMatrixVert[i][j] = 1;
00391                 else
00392                 TCGMatrixVert[j][i] = 1;
00393                 }
00394               */
00395             }
00396         }
00397     }
00398   //get the sequence pair now
00399   _XX.resize(size);
00400   _YY.resize(size);
00401   for(unsigned i=0; i<size; ++i)
00402     {
00403       _XX[i] = i;
00404       _YY[i] = i;
00405     }
00406   
00407   SPXRelation SPX(TCGMatrixHoriz, TCGMatrixVert);
00408   SPYRelation SPY(TCGMatrixHoriz, TCGMatrixVert);
00409 
00410   std::sort(_XX.begin(), _XX.end(), SPX);
00411   std::sort(_YY.begin(), _YY.end(), SPY);
00412 
00413   /*
00414   cout<<"TCGMatrixHoriz"<<endl;
00415   cout<<"\t";
00416   for(unsigned i=0; i<size; ++i)
00417     cout<<i<<" ";
00418   cout<<endl;
00419   for(unsigned i=0; i<size; ++i)
00420     {
00421       cout<<i<<"\t";
00422       for(unsigned j=0; j<size; ++j)
00423         {
00424           cout<<TCGMatrixHoriz[i][j]<<" ";
00425         }
00426       cout<<endl;
00427     }
00428   cout<<"TCGMatrixVert"<<endl;
00429   cout<<"\t";
00430   for(unsigned i=0; i<size; ++i)
00431     cout<<i<<" ";
00432   cout<<endl;
00433   for(unsigned i=0; i<size; ++i)
00434     {
00435       cout<<i<<"\t";
00436       for(unsigned j=0; j<size; ++j)
00437         {
00438           cout<<TCGMatrixVert[i][j]<<" ";
00439         }
00440       cout<<endl;
00441     }
00442   print();
00443   */
00444 }
00445 
00446 
00447 void Pl2SP::print() const
00448 {
00449   cout<<"XSequence Pair"<<endl;
00450   for(unsigned i=0; i<_XX.size(); ++i)
00451     cout<<_XX[i]<<"  ";
00452   cout<<endl<<"YSequence Pair"<<endl;
00453   for(unsigned i=0; i<_YY.size(); ++i)
00454     cout<<_YY[i]<<"  ";
00455   cout<<endl;
00456 }
00457 
00458 void Pl2SP::TCG_FM(vector< vector <bool> >& TCGMatrixHoriz, 
00459                    vector< vector <bool> >& TCGMatrixVert)
00460 {
00461   unsigned size = TCGMatrixHoriz[0].size();
00462   for(unsigned k=0; k<size; ++k)
00463     {
00464       for(unsigned i=0; i<size; ++i)
00465         {
00466           for(unsigned j = 0; j<size; ++j)
00467             {
00468               TCGMatrixHoriz[i][j] = TCGMatrixHoriz[i][j] | 
00469                 (TCGMatrixHoriz[i][k] & TCGMatrixHoriz[k][j]);
00470               TCGMatrixVert[i][j] = TCGMatrixVert[i][j] | 
00471                 (TCGMatrixVert[i][k] & TCGMatrixVert[k][j]);
00472             }
00473         }
00474     }
00475 }
00476 
00477 void Pl2SP::TCG_DP(vector< vector <bool> >& TCGMatrixHoriz, 
00478                    vector< vector <bool> >& TCGMatrixVert)
00479 {
00480   int size = TCGMatrixHoriz[0].size();
00481   vector< vector <bool> > adjMatrixHoriz = TCGMatrixHoriz;
00482   vector< vector <bool> > adjMatrixVert = TCGMatrixVert;
00483   vector<int> pre(size, -1);
00484   
00485   int i;
00486   for(i=0; i<size; ++i)
00487     {
00488       fill(TCGMatrixHoriz[i].begin(), TCGMatrixHoriz[i].end(), false);
00489       fill(TCGMatrixVert[i].begin(), TCGMatrixVert[i].end(), false);
00490     }
00491 
00492   _cnt = 0;
00493   for(i=0; i<size; ++i)
00494     if(pre[i] == -1)
00495       TCGDfs(TCGMatrixHoriz, adjMatrixHoriz, i, pre);
00496 
00497   _cnt = 0;
00498   fill(pre.begin(), pre.end(), -1);
00499   for(i=0; i<size; ++i)
00500     if(pre[i] == -1)
00501       TCGDfs(TCGMatrixVert, adjMatrixVert, i, pre);
00502 }
00503 
00504 void Pl2SP::TCGDfs(vector< vector <bool> >& TCGMatrix, 
00505                    vector< vector <bool> >& adjMatrix, int v, vector<int>& pre)
00506 {
00507   int u, i;
00508   pre[v] = _cnt++;
00509   int size = adjMatrix[0].size();
00510 
00511   for(u=0; u<size; ++u)
00512     {
00513       if(adjMatrix[v][u])
00514         {
00515           TCGMatrix[v][u] = true;
00516           if(pre[u] > pre[v]) continue;
00517 
00518           if(pre[u] == -1)
00519             TCGDfs(TCGMatrix, adjMatrix, u, pre);
00520 
00521           for(i=0; i<size; ++i)
00522             if(TCGMatrix[u][i] == 1)
00523               TCGMatrix[v][i] = 1;
00524         }
00525     }
00526 }
00527 

Generated on Mon Apr 25 01:09:25 2005 for Parquete by doxygen 1.3.2