solver.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) 2013-2017, Nucleic Development Team.
  3. |
  4. | Distributed under the terms of the Modified BSD License.
  5. |
  6. | The full license is in the file LICENSE, distributed with this software.
  7. |----------------------------------------------------------------------------*/
  8. #pragma once
  9. #include "constraint.h"
  10. #include "debug.h"
  11. #include "solverimpl.h"
  12. #include "strength.h"
  13. #include "variable.h"
  14. namespace kiwi
  15. {
  16. class Solver
  17. {
  18. public:
  19. Solver() = default;
  20. ~Solver() = default;
  21. /* Add a constraint to the solver.
  22. Throws
  23. ------
  24. DuplicateConstraint
  25. The given constraint has already been added to the solver.
  26. UnsatisfiableConstraint
  27. The given constraint is required and cannot be satisfied.
  28. */
  29. void addConstraint( const Constraint& constraint )
  30. {
  31. m_impl.addConstraint( constraint );
  32. }
  33. /* Remove a constraint from the solver.
  34. Throws
  35. ------
  36. UnknownConstraint
  37. The given constraint has not been added to the solver.
  38. */
  39. void removeConstraint( const Constraint& constraint )
  40. {
  41. m_impl.removeConstraint( constraint );
  42. }
  43. /* Test whether a constraint has been added to the solver.
  44. */
  45. bool hasConstraint( const Constraint& constraint ) const
  46. {
  47. return m_impl.hasConstraint( constraint );
  48. }
  49. /* Add an edit variable to the solver.
  50. This method should be called before the `suggestValue` method is
  51. used to supply a suggested value for the given edit variable.
  52. Throws
  53. ------
  54. DuplicateEditVariable
  55. The given edit variable has already been added to the solver.
  56. BadRequiredStrength
  57. The given strength is >= required.
  58. */
  59. void addEditVariable( const Variable& variable, double strength )
  60. {
  61. m_impl.addEditVariable( variable, strength );
  62. }
  63. /* Remove an edit variable from the solver.
  64. Throws
  65. ------
  66. UnknownEditVariable
  67. The given edit variable has not been added to the solver.
  68. */
  69. void removeEditVariable( const Variable& variable )
  70. {
  71. m_impl.removeEditVariable( variable );
  72. }
  73. /* Test whether an edit variable has been added to the solver.
  74. */
  75. bool hasEditVariable( const Variable& variable ) const
  76. {
  77. return m_impl.hasEditVariable( variable );
  78. }
  79. /* Suggest a value for the given edit variable.
  80. This method should be used after an edit variable as been added to
  81. the solver in order to suggest the value for that variable. After
  82. all suggestions have been made, the `solve` method can be used to
  83. update the values of all variables.
  84. Throws
  85. ------
  86. UnknownEditVariable
  87. The given edit variable has not been added to the solver.
  88. */
  89. void suggestValue( const Variable& variable, double value )
  90. {
  91. m_impl.suggestValue( variable, value );
  92. }
  93. /* Update the values of the external solver variables.
  94. */
  95. void updateVariables()
  96. {
  97. m_impl.updateVariables();
  98. }
  99. /* Reset the solver to the empty starting condition.
  100. This method resets the internal solver state to the empty starting
  101. condition, as if no constraints or edit variables have been added.
  102. This can be faster than deleting the solver and creating a new one
  103. when the entire system must change, since it can avoid unecessary
  104. heap (de)allocations.
  105. */
  106. void reset()
  107. {
  108. m_impl.reset();
  109. }
  110. /* Dump a representation of the solver internals to stdout.
  111. */
  112. void dump()
  113. {
  114. debug::dump( m_impl );
  115. }
  116. /* Dump a representation of the solver internals to a stream.
  117. */
  118. void dump( std::ostream& out )
  119. {
  120. debug::dump( m_impl, out );
  121. }
  122. /* Dump a representation of the solver internals to a string.
  123. */
  124. std::string dumps()
  125. {
  126. return debug::dumps( m_impl );
  127. }
  128. private:
  129. Solver( const Solver& );
  130. Solver& operator=( const Solver& );
  131. impl::SolverImpl m_impl;
  132. };
  133. } // namespace kiwi