constraint.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <map>
  10. #include <vector>
  11. #include "expression.h"
  12. #include "shareddata.h"
  13. #include "strength.h"
  14. #include "term.h"
  15. #include "variable.h"
  16. namespace kiwi
  17. {
  18. enum RelationalOperator
  19. {
  20. OP_LE,
  21. OP_GE,
  22. OP_EQ
  23. };
  24. class Constraint
  25. {
  26. public:
  27. Constraint() = default;
  28. Constraint(const Expression &expr,
  29. RelationalOperator op,
  30. double strength = strength::required) : m_data(new ConstraintData(expr, op, strength)) {}
  31. Constraint(const Constraint &other, double strength) : m_data(new ConstraintData(other, strength)) {}
  32. Constraint(const Constraint &) = default;
  33. Constraint(Constraint &&) noexcept = default;
  34. ~Constraint() = default;
  35. const Expression &expression() const
  36. {
  37. return m_data->m_expression;
  38. }
  39. RelationalOperator op() const
  40. {
  41. return m_data->m_op;
  42. }
  43. double strength() const
  44. {
  45. return m_data->m_strength;
  46. }
  47. bool operator!() const
  48. {
  49. return !m_data;
  50. }
  51. Constraint& operator=(const Constraint &) = default;
  52. Constraint& operator=(Constraint &&) noexcept = default;
  53. private:
  54. static Expression reduce(const Expression &expr)
  55. {
  56. std::map<Variable, double> vars;
  57. for (const auto & term : expr.terms())
  58. vars[term.variable()] += term.coefficient();
  59. std::vector<Term> terms(vars.begin(), vars.end());
  60. return Expression(std::move(terms), expr.constant());
  61. }
  62. class ConstraintData : public SharedData
  63. {
  64. public:
  65. ConstraintData(const Expression &expr,
  66. RelationalOperator op,
  67. double strength) : SharedData(),
  68. m_expression(reduce(expr)),
  69. m_strength(strength::clip(strength)),
  70. m_op(op) {}
  71. ConstraintData(const Constraint &other, double strength) : SharedData(),
  72. m_expression(other.expression()),
  73. m_strength(strength::clip(strength)),
  74. m_op(other.op()) {}
  75. ~ConstraintData() = default;
  76. Expression m_expression;
  77. double m_strength;
  78. RelationalOperator m_op;
  79. private:
  80. ConstraintData(const ConstraintData &other);
  81. ConstraintData &operator=(const ConstraintData &other);
  82. };
  83. SharedDataPtr<ConstraintData> m_data;
  84. friend bool operator<(const Constraint &lhs, const Constraint &rhs)
  85. {
  86. return lhs.m_data < rhs.m_data;
  87. }
  88. friend bool operator==(const Constraint &lhs, const Constraint &rhs)
  89. {
  90. return lhs.m_data == rhs.m_data;
  91. }
  92. friend bool operator!=(const Constraint &lhs, const Constraint &rhs)
  93. {
  94. return lhs.m_data != rhs.m_data;
  95. }
  96. };
  97. } // namespace kiwi