| 1 |
/** |
| 2 |
* \brief Bridge for C programs to access solve_VPSC (which is in C++) |
| 3 |
* |
| 4 |
* Authors: |
| 5 |
* Tim Dwyer <[email protected]> |
| 6 |
* |
| 7 |
* Copyright (C) 2005 Authors |
| 8 |
* |
| 9 |
* This version is released under the CPL (Common Public License) with |
| 10 |
* the Graphviz distribution. |
| 11 |
* A version is also available under the LGPL as part of the Adaptagrams |
| 12 |
* project: https://github.com/mjwybrow/adaptagrams. |
| 13 |
* If you make improvements or bug fixes to this code it would be much |
| 14 |
* appreciated if you could also contribute those changes back to the |
| 15 |
* Adaptagrams repository. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "config.h" |
| 19 |
|
| 20 |
#include <iostream> |
| 21 |
#include <vpsc/variable.h> |
| 22 |
#include <vpsc/constraint.h> |
| 23 |
#include <vpsc/generate-constraints.h> |
| 24 |
#include <vpsc/solve_VPSC.h> |
| 25 |
#include <cstdlib> |
| 26 |
#include <cassert> |
| 27 |
#include <vector> |
| 28 |
#include <vpsc/csolve_VPSC.h> |
| 29 |
Variable* newVariable(int id, double desiredPos, double weight) { |
| 30 |
return new Variable(id,desiredPos,weight); |
| 31 |
} |
| 32 |
Constraint* newConstraint(Variable* left, Variable* right, double gap) { |
| 33 |
return new Constraint(left,right,gap); |
| 34 |
} |
| 35 |
VPSC* newIncVPSC(int n, Variable* vs[], int m, Constraint* cs[]) { |
| 36 |
return new IncVPSC(n,vs,m,cs); |
| 37 |
} |
| 38 |
|
| 39 |
int genXConstraints(int n, boxf *bb, Variable **vs, Constraint ***cs, |
| 40 |
bool transitiveClosure) { |
| 41 |
std::vector<Rectangle> rs; |
| 42 |
for(int i=0;i<n;i++) { |
| 43 |
rs.emplace_back(bb[i].LL.x,bb[i].UR.x,bb[i].LL.y,bb[i].UR.y); |
| 44 |
} |
| 45 |
const int m = generateXConstraints(rs, vs, *cs, transitiveClosure); |
| 46 |
return m; |
| 47 |
} |
| 48 |
int genYConstraints(int n, boxf* bb, Variable** vs, Constraint*** cs) { |
| 49 |
std::vector<Rectangle> rs; |
| 50 |
for(int i=0;i<n;i++) { |
| 51 |
rs.emplace_back(bb[i].LL.x,bb[i].UR.x,bb[i].LL.y,bb[i].UR.y); |
| 52 |
} |
| 53 |
const int m = generateYConstraints(rs, vs, *cs); |
| 54 |
return m; |
| 55 |
} |
| 56 |
|
| 57 |
Constraint** newConstraints(int m) { |
| 58 |
return new Constraint*[m]; |
| 59 |
} |
| 60 |
void deleteConstraints(int m, Constraint **cs) { |
| 61 |
for(int i=0;i<m;i++) { |
| 62 |
delete cs[i]; |
| 63 |
} |
| 64 |
delete [] cs; |
| 65 |
} |
| 66 |
void deleteConstraint(Constraint* c) { |
| 67 |
delete c; |
| 68 |
} |
| 69 |
void deleteVariable(Variable* v) { |
| 70 |
delete v; |
| 71 |
} |
| 72 |
void satisfyVPSC(VPSC* vpsc) { |
| 73 |
try { |
| 74 |
vpsc->satisfy(); |
| 75 |
} catch(const char *e) { |
| 76 |
std::cerr << e << "\n"; |
| 77 |
std::exit(1); |
| 78 |
} |
| 79 |
} |
| 80 |
void deleteVPSC(VPSC *vpsc) { |
| 81 |
assert(vpsc!=nullptr); |
| 82 |
delete vpsc; |
| 83 |
} |
| 84 |
void solveVPSC(VPSC* vpsc) { |
| 85 |
vpsc->solve(); |
| 86 |
} |
| 87 |
void setVariableDesiredPos(Variable *v, double desiredPos) { |
| 88 |
v->desiredPosition = desiredPos; |
| 89 |
} |
| 90 |
double getVariablePos(const Variable *v) { |
| 91 |
return v->position(); |
| 92 |
} |
| 93 |
void remapInConstraints(Variable *u, Variable *v, double dgap) { |
| 94 |
for (Constraint *c : u->in) { |
| 95 |
c->right=v; |
| 96 |
c->gap+=dgap; |
| 97 |
v->in.push_back(c); |
| 98 |
} |
| 99 |
u->in.clear(); |
| 100 |
} |
| 101 |
void remapOutConstraints(Variable *u, Variable *v, double dgap) { |
| 102 |
for (Constraint *c : u->out) { |
| 103 |
c->left=v; |
| 104 |
c->gap+=dgap; |
| 105 |
v->out.push_back(c); |
| 106 |
} |
| 107 |
u->out.clear(); |
| 108 |
} |