00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "plsptobtree.h"
00036
00037 #include "btree.h"
00038
00039 #include <vector>
00040 #include <cmath>
00041 #include <cfloat>
00042 #include <algorithm>
00043 using namespace std;
00044
00045 const double PlSP2BTree::INFTY = basepacking_h::Dimension::INFTY;
00046 const int PlSP2BTree::UNDEFINED = BTree::UNDEFINED;
00047
00048 PlSP2BTree::PlSP2BTree(const vector<double>& n_xloc,
00049 const vector<double>& n_yloc,
00050 const vector<double>& n_widths,
00051 const vector<double>& n_heights,
00052 const vector<int>& XX,
00053 const vector<int>& YY)
00054 : xloc(n_xloc),
00055 yloc(n_yloc),
00056 widths(n_widths),
00057 heights(n_heights),
00058 _blocknum(n_xloc.size()),
00059 _XX(XX),
00060 _YY(YY),
00061 _XXinverse(_blocknum),
00062 _YYinverse(_blocknum),
00063 _btree(_blocknum+2)
00064 {
00065 constructor_core();
00066 }
00067
00068 PlSP2BTree::PlSP2BTree(const vector<double>& n_xloc,
00069 const vector<double>& n_yloc,
00070 const vector<double>& n_widths,
00071 const vector<double>& n_heights,
00072 const vector<unsigned int>& XX,
00073 const vector<unsigned int>& YY)
00074 : xloc(n_xloc),
00075 yloc(n_yloc),
00076 widths(n_widths),
00077 heights(n_heights),
00078 _blocknum(n_xloc.size()),
00079 _XX(_blocknum),
00080 _YY(_blocknum),
00081 _XXinverse(_blocknum),
00082 _YYinverse(_blocknum),
00083 _btree(_blocknum+2)
00084 {
00085 for (int i = 0; i < _blocknum; i++)
00086 {
00087 _XX[i] = int(XX[i]);
00088 _YY[i] = int(YY[i]);
00089 }
00090 constructor_core();
00091 }
00092
00093 void PlSP2BTree::build_tree()
00094 {
00095 for (int i = 0; i < _blocknum; i++)
00096 {
00097 int currBlock = _YY[i];
00098 int otree_parent = _blocknum;
00099
00100 double currXStart = xloc[currBlock];
00101 double minDistance = currXStart;
00102 int parentMaxIndex = _blocknum;
00103 for (int j = i-1; j >= 0; j--)
00104 {
00105 int tempBlock = _YY[j];
00106 if (SPleftof(tempBlock, currBlock))
00107 {
00108 if (_XXinverse[tempBlock] < parentMaxIndex)
00109 {
00110 double tempDistance =
00111 currXStart - xloc[tempBlock] - widths[tempBlock];
00112 if (tempDistance < minDistance)
00113 {
00114 minDistance = tempDistance;
00115 otree_parent = tempBlock;
00116 }
00117 }
00118 }
00119 else
00120 parentMaxIndex = min(parentMaxIndex, _XXinverse[tempBlock]);
00121 }
00122 build_tree_add_block(currBlock, otree_parent);
00123 }
00124 }
00125
00126 void PlSP2BTree::build_tree_add_block(int currBlock,
00127 int otree_parent)
00128 {
00129
00130
00131
00132 if (_btree[otree_parent].left == UNDEFINED)
00133 {
00134 _btree[otree_parent].left = currBlock;
00135 _btree[currBlock].parent = otree_parent;
00136 }
00137 else
00138 {
00139 int tree_prev = otree_parent;
00140 int tree_curr = _btree[otree_parent].left;
00141 while ((tree_curr != UNDEFINED) &&
00142 (yloc[tree_curr] < yloc[currBlock]))
00143 {
00144 tree_prev = tree_curr;
00145 tree_curr = _btree[tree_curr].right;
00146 }
00147
00148
00149
00150
00151
00152
00153 if ((tree_curr != UNDEFINED) &&
00154 (tree_curr == _btree[tree_prev].left))
00155 _btree[tree_prev].left = currBlock;
00156 else
00157 _btree[tree_prev].right = currBlock;
00158 _btree[currBlock].parent = tree_prev;
00159
00160 _btree[currBlock].right = tree_curr;
00161 if (tree_curr != UNDEFINED)
00162 _btree[tree_curr].parent = currBlock;
00163 }
00164 }
00165
00166 void PlSP2BTree::constructor_core()
00167 {
00168 initializeTree();
00169 for (int i = 0; i < _blocknum; i++)
00170 {
00171 _XXinverse[_XX[i]] = i;
00172 _YYinverse[_YY[i]] = i;
00173 }
00174 build_tree();
00175 }
00176
00177 void PlSP2BTree::initializeTree()
00178 {
00179 int vec_size = int(_btree.size());
00180 for (int i = 0; i < vec_size; i++)
00181 {
00182 _btree[i].parent = _blocknum;
00183 _btree[i].left = UNDEFINED;
00184 _btree[i].right = UNDEFINED;
00185 _btree[i].block_index = i;
00186 _btree[i].orient = 0;
00187 }
00188
00189 _btree[_blocknum].parent = UNDEFINED;
00190 _btree[_blocknum].left = UNDEFINED;
00191 _btree[_blocknum].right = UNDEFINED;
00192 _btree[_blocknum].block_index = _blocknum;
00193 _btree[_blocknum].orient = UNDEFINED;
00194
00195 _btree[_blocknum+1].parent = _blocknum;
00196 _btree[_blocknum+1].left = UNDEFINED;
00197 _btree[_blocknum+1].right = UNDEFINED;
00198 _btree[_blocknum+1].block_index = _blocknum+1;
00199 _btree[_blocknum+1].orient = UNDEFINED;
00200 }
00201
00202
00203
00204