1 /**
2 * \brief Bridge for C programs to access solve_VPSC (which is in C++)
3 *
4 * Authors:
5 * Tim Dwyer <tgdwyer@gmail.com>
6 *
7 * Copyright (C) 2005 Authors
8 *
9 * Released under GNU LGPL. Read the file 'COPYING' for more information.
10 */
11 #include <iostream>
12 #include <cassert>
13 #include "variable.h"
14 #include "constraint.h"
15 #include "generate-constraints.h"
16 #include "solve_VPSC.h"
17 #include "csolve_VPSC.h"
18 extern "C" {
19 Variable* newVariable(int id, double desiredPos, double weight) {
20 return new Variable(id,desiredPos,weight);
21 }
22 Constraint* newConstraint(Variable* left, Variable* right, double gap) {
23 return new Constraint(left,right,gap);
24 }
25 VPSC* newVPSC(int n, Variable* vs[], int m, Constraint* cs[]) {
26 return new VPSC(n,vs,m,cs);
27 }
28 VPSC* newIncVPSC(int n, Variable* vs[], int m, Constraint* cs[]) {
29 return (VPSC*)new IncVPSC(n,vs,m,cs);
30 }
32 int genXConstraints(int n, boxf* bb, Variable** vs, Constraint*** cs,int transitiveClosure) {
33 Rectangle* rs[n];
34 for(int i=0;i<n;i++) {
35 rs[i]=new Rectangle(bb[i].LL.x,bb[i].UR.x,bb[i].LL.y,bb[i].UR.y);
36 }
37 int m = generateXConstraints(n,rs,vs,*cs,transitiveClosure);
38 for(int i=0;i<n;i++) {
39 delete rs[i];
40 }
41 return m;
42 }
43 int genYConstraints(int n, boxf* bb, Variable** vs, Constraint*** cs) {
44 Rectangle* rs[n];
45 for(int i=0;i<n;i++) {
46 rs[i]=new Rectangle(bb[i].LL.x,bb[i].UR.x,bb[i].LL.y,bb[i].UR.y);
47 }
48 int m = generateYConstraints(n,rs,vs,*cs);
49 for(int i=0;i<n;i++) {
50 delete rs[i];
51 }
52 return m;
53 }
55 Constraint** newConstraints(int m) {
56 return new Constraint*[m];
57 }
58 void deleteConstraints(int m, Constraint **cs) {
59 for(int i=0;i<m;i++) {
60 delete cs[i];
61 }
62 delete [] cs;
63 }
64 void deleteConstraint(Constraint* c) {
65 delete c;
66 }
67 void deleteVariable(Variable* v) {
68 delete v;
69 }
70 void satisfyVPSC(VPSC* vpsc) {
71 try {
72 vpsc->satisfy();
73 } catch(const char *e) {
74 std::cerr << e << std::endl;
75 exit(1);
76 }
77 }
78 int getSplitCnt(IncVPSC *vpsc) {
79 return vpsc->splitCnt;
80 }
81 void deleteVPSC(VPSC *vpsc) {
82 assert(vpsc!=NULL);
83 delete vpsc;
84 }
85 void solveVPSC(VPSC* vpsc) {
86 vpsc->solve();
87 }
88 void splitIncVPSC(IncVPSC* vpsc) {
89 vpsc->splitBlocks();
90 }
91 void setVariableDesiredPos(Variable *v, double desiredPos) {
92 v->desiredPosition = desiredPos;
93 }
94 double getVariablePos(Variable *v) {
95 return v->position();
96 }
97 void remapInConstraints(Variable *u, Variable *v, double dgap) {
98 for(Constraints::iterator i=u->in.begin();i!=u->in.end();i++) {
99 Constraint* c=*i;
100 c->right=v;
101 c->gap+=dgap;
102 v->in.push_back(c);
103 }
104 u->in.clear();
105 }
106 void remapOutConstraints(Variable *u, Variable *v, double dgap) {
107 for(Constraints::iterator i=u->out.begin();i!=u->out.end();i++) {
108 Constraint* c=*i;
109 c->left=v;
110 c->gap+=dgap;
111 v->out.push_back(c);
112 }
113 u->out.clear();
114 }
115 int getLeftVarID(Constraint *c) {
116 return c->left->id;
117 }
118 int getRightVarID(Constraint *c){
119 return c->right->id;
120 }
121 double getSeparation(Constraint *c){
122 return c->gap;
123 }
124 }