csolve_VPSC.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include <iostream>
  18. #include <vpsc/variable.h>
  19. #include <vpsc/constraint.h>
  20. #include <vpsc/generate-constraints.h>
  21. #include <vpsc/solve_VPSC.h>
  22. #include <cstdlib>
  23. #include <cassert>
  24. #include <vector>
  25. #include <vpsc/csolve_VPSC.h>
  26. Variable* newVariable(int id, double desiredPos, double weight) {
  27. return new Variable(id,desiredPos,weight);
  28. }
  29. Constraint* newConstraint(Variable* left, Variable* right, double gap) {
  30. return new Constraint(left,right,gap);
  31. }
  32. VPSC* newIncVPSC(int n, Variable* vs[], int m, Constraint* cs[]) {
  33. return new IncVPSC(n,vs,m,cs);
  34. }
  35. int genXConstraints(int n, boxf *bb, Variable **vs, Constraint ***cs,
  36. bool transitiveClosure) {
  37. std::vector<Rectangle> rs;
  38. for(int i=0;i<n;i++) {
  39. rs.emplace_back(bb[i].LL.x,bb[i].UR.x,bb[i].LL.y,bb[i].UR.y);
  40. }
  41. const int m = generateXConstraints(rs, vs, *cs, transitiveClosure);
  42. return m;
  43. }
  44. int genYConstraints(int n, boxf* bb, Variable** vs, Constraint*** cs) {
  45. std::vector<Rectangle> rs;
  46. for(int i=0;i<n;i++) {
  47. rs.emplace_back(bb[i].LL.x,bb[i].UR.x,bb[i].LL.y,bb[i].UR.y);
  48. }
  49. const int m = generateYConstraints(rs, vs, *cs);
  50. return m;
  51. }
  52. Constraint** newConstraints(int m) {
  53. return new Constraint*[m];
  54. }
  55. void deleteConstraints(int m, Constraint **cs) {
  56. for(int i=0;i<m;i++) {
  57. delete cs[i];
  58. }
  59. delete [] cs;
  60. }
  61. void deleteConstraint(Constraint* c) {
  62. delete c;
  63. }
  64. void deleteVariable(Variable* v) {
  65. delete v;
  66. }
  67. void satisfyVPSC(VPSC* vpsc) {
  68. try {
  69. vpsc->satisfy();
  70. } catch(const char *e) {
  71. std::cerr << e << "\n";
  72. std::exit(1);
  73. }
  74. }
  75. void deleteVPSC(VPSC *vpsc) {
  76. assert(vpsc!=nullptr);
  77. delete vpsc;
  78. }
  79. void solveVPSC(VPSC* vpsc) {
  80. vpsc->solve();
  81. }
  82. void setVariableDesiredPos(Variable *v, double desiredPos) {
  83. v->desiredPosition = desiredPos;
  84. }
  85. double getVariablePos(const Variable *v) {
  86. return v->position();
  87. }
  88. void remapInConstraints(Variable *u, Variable *v, double dgap) {
  89. for (Constraint *c : u->in) {
  90. c->right=v;
  91. c->gap+=dgap;
  92. v->in.push_back(c);
  93. }
  94. u->in.clear();
  95. }
  96. void remapOutConstraints(Variable *u, Variable *v, double dgap) {
  97. for (Constraint *c : u->out) {
  98. c->left=v;
  99. c->gap+=dgap;
  100. v->out.push_back(c);
  101. }
  102. u->out.clear();
  103. }