| 1 |
/** |
| 2 |
* \brief A block structure defined over the variables |
| 3 |
* |
| 4 |
* A block structure defined over the variables such that each block contains |
| 5 |
* 1 or more variables, with the invariant that all constraints inside a block |
| 6 |
* are satisfied by keeping the variables fixed relative to one another |
| 7 |
* |
| 8 |
* Authors: |
| 9 |
* Tim Dwyer <[email protected]> |
| 10 |
* |
| 11 |
* Copyright (C) 2005 Authors |
| 12 |
* |
| 13 |
* This version is released under the CPL (Common Public License) with |
| 14 |
* the Graphviz distribution. |
| 15 |
* A version is also available under the LGPL as part of the Adaptagrams |
| 16 |
* project: https://github.com/mjwybrow/adaptagrams. |
| 17 |
* If you make improvements or bug fixes to this code it would be much |
| 18 |
* appreciated if you could also contribute those changes back to the |
| 19 |
* Adaptagrams repository. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include <vpsc/blocks.h> |
| 25 |
#include <vpsc/block.h> |
| 26 |
#include <vpsc/constraint.h> |
| 27 |
#include <fstream> |
| 28 |
using std::ios; |
| 29 |
using std::ofstream; |
| 30 |
using std::set; |
| 31 |
using std::list; |
| 32 |
|
| 33 |
#ifndef RECTANGLE_OVERLAP_LOGGING |
| 34 |
#define RECTANGLE_OVERLAP_LOGGING 0 |
| 35 |
#endif |
| 36 |
|
| 37 |
long blockTimeCtr; |
| 38 |
|
| 39 |
Blocks::Blocks(const int n, Variable *vs_[]) : vs(vs_), nvs(n) { |
| 40 |
blockTimeCtr=0; |
| 41 |
for(int i=0;i<nvs;i++) { |
| 42 |
insert(new Block(vs[i])); |
| 43 |
} |
| 44 |
} |
| 45 |
Blocks::~Blocks() |
| 46 |
{ |
| 47 |
blockTimeCtr=0; |
| 48 |
for (Block *b : *this) { |
| 49 |
delete b; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* returns a list of variables with total ordering determined by the constraint |
| 55 |
* DAG |
| 56 |
*/ |
| 57 |
list<Variable*> Blocks::totalOrder() { |
| 58 |
list<Variable*> order; |
| 59 |
for(int i=0;i<nvs;i++) { |
| 60 |
vs[i]->visited=false; |
| 61 |
} |
| 62 |
for(int i=0;i<nvs;i++) { |
| 63 |
if(vs[i]->in.empty()) { |
| 64 |
dfsVisit(vs[i],order); |
| 65 |
} |
| 66 |
} |
| 67 |
return order; |
| 68 |
} |
| 69 |
// Recursive depth first search giving total order by pushing nodes in the DAG |
| 70 |
// onto the front of the list when we finish searching them |
| 71 |
void Blocks::dfsVisit(Variable *v, list<Variable*> &order) { |
| 72 |
v->visited=true; |
| 73 |
for (Constraint *c : v->out) { |
| 74 |
if(!c->right->visited) { |
| 75 |
dfsVisit(c->right, order); |
| 76 |
} |
| 77 |
} |
| 78 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 79 |
ofstream f(LOGFILE,ios::app); |
| 80 |
f<<" order="<<*v<<"\n"; |
| 81 |
} |
| 82 |
order.push_front(v); |
| 83 |
} |
| 84 |
/** |
| 85 |
* Processes incoming constraints, most violated to least, merging with the |
| 86 |
* neighbouring (left) block until no more violated constraints are found |
| 87 |
*/ |
| 88 |
void Blocks::mergeLeft(Block *r) { |
| 89 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 90 |
ofstream f(LOGFILE,ios::app); |
| 91 |
f<<"mergeLeft called on "<<*r<<"\n"; |
| 92 |
} |
| 93 |
r->timeStamp=++blockTimeCtr; |
| 94 |
r->setUpInConstraints(); |
| 95 |
Constraint *c=r->findMinInConstraint(); |
| 96 |
while (c != nullptr && c->slack()<0) { |
| 97 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 98 |
ofstream f(LOGFILE,ios::app); |
| 99 |
f<<"mergeLeft on constraint: "<<*c<<"\n"; |
| 100 |
} |
| 101 |
r->deleteMinInConstraint(); |
| 102 |
Block *l = c->left->block; |
| 103 |
if (l->in.empty()) l->setUpInConstraints(); |
| 104 |
double dist = c->right->offset - c->left->offset - c->gap; |
| 105 |
if (r->vars.size() < l->vars.size()) { |
| 106 |
dist=-dist; |
| 107 |
std::swap(l, r); |
| 108 |
} |
| 109 |
blockTimeCtr++; |
| 110 |
r->merge(l, c, dist); |
| 111 |
r->mergeIn(l); |
| 112 |
r->timeStamp=blockTimeCtr; |
| 113 |
removeBlock(l); |
| 114 |
c=r->findMinInConstraint(); |
| 115 |
} |
| 116 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 117 |
ofstream f(LOGFILE,ios::app); |
| 118 |
f<<"merged "<<*r<<"\n"; |
| 119 |
} |
| 120 |
} |
| 121 |
/** |
| 122 |
* Symmetrical to mergeLeft |
| 123 |
*/ |
| 124 |
void Blocks::mergeRight(Block *l) { |
| 125 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 126 |
ofstream f(LOGFILE,ios::app); |
| 127 |
f<<"mergeRight called on "<<*l<<"\n"; |
| 128 |
} |
| 129 |
l->setUpOutConstraints(); |
| 130 |
Constraint *c = l->findMinOutConstraint(); |
| 131 |
while (c != nullptr && c->slack()<0) { |
| 132 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 133 |
ofstream f(LOGFILE,ios::app); |
| 134 |
f<<"mergeRight on constraint: "<<*c<<"\n"; |
| 135 |
} |
| 136 |
l->deleteMinOutConstraint(); |
| 137 |
Block *r = c->right->block; |
| 138 |
r->setUpOutConstraints(); |
| 139 |
double dist = c->left->offset + c->gap - c->right->offset; |
| 140 |
if (l->vars.size() > r->vars.size()) { |
| 141 |
dist=-dist; |
| 142 |
std::swap(l, r); |
| 143 |
} |
| 144 |
l->merge(r, c, dist); |
| 145 |
l->mergeOut(r); |
| 146 |
removeBlock(r); |
| 147 |
c=l->findMinOutConstraint(); |
| 148 |
} |
| 149 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 150 |
ofstream f(LOGFILE,ios::app); |
| 151 |
f<<"merged "<<*l<<"\n"; |
| 152 |
} |
| 153 |
} |
| 154 |
void Blocks::removeBlock(Block *doomed) { |
| 155 |
doomed->deleted=true; |
| 156 |
//erase(doomed); |
| 157 |
} |
| 158 |
void Blocks::cleanup() { |
| 159 |
for (auto i = begin(); i != end();) { |
| 160 |
Block *b=*i; |
| 161 |
if(b->deleted) { |
| 162 |
i = erase(i); |
| 163 |
delete b; |
| 164 |
} else { |
| 165 |
++i; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
/** |
| 170 |
* Splits block b across constraint c into two new blocks, l and r (c's left |
| 171 |
* and right sides respectively) |
| 172 |
*/ |
| 173 |
void Blocks::split(Block *b, Block *&l, Block *&r, Constraint *c) { |
| 174 |
b->split(l,r,c); |
| 175 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 176 |
ofstream f(LOGFILE,ios::app); |
| 177 |
f<<"Split left: "<<*l<<"\n"; |
| 178 |
f<<"Split right: "<<*r<<"\n"; |
| 179 |
} |
| 180 |
r->posn = b->posn; |
| 181 |
r->wposn = r->posn * r->weight; |
| 182 |
mergeLeft(l); |
| 183 |
// r may have been merged! |
| 184 |
r = c->right->block; |
| 185 |
r->wposn = r->desiredWeightedPosition(); |
| 186 |
r->posn = r->wposn / r->weight; |
| 187 |
mergeRight(r); |
| 188 |
removeBlock(b); |
| 189 |
|
| 190 |
insert(l); |
| 191 |
insert(r); |
| 192 |
} |
| 193 |
/** |
| 194 |
* returns the cost total squared distance of variables from their desired |
| 195 |
* positions |
| 196 |
*/ |
| 197 |
double Blocks::cost() { |
| 198 |
double c = 0; |
| 199 |
for (Block *b : *this) { |
| 200 |
c += b->cost(); |
| 201 |
} |
| 202 |
return c; |
| 203 |
} |