Debian Codegraph @

py/src/constraint.cpp

raw
1 /*-----------------------------------------------------------------------------
2 | Copyright (c) 2013-2026, 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 #include <cppy/cppy.h>
9 #include <algorithm>
10 #include <sstream>
11 #include <kiwi/kiwi.h>
12 #include "types.h"
13 #include "util.h"
14
15 namespace kiwisolver
16 {
17
18 namespace
19 {
20
21 PyObject *
22 Constraint_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
23 {
24 static const char *kwlist[] = {"expression", "op", "strength", 0};
25 PyObject *pyexpr;
26 PyObject *pyop;
27 PyObject *pystrength = 0;
28 if (!PyArg_ParseTupleAndKeywords(
29 args, kwargs, "OO|O:__new__", const_cast<char **>(kwlist),
30 &pyexpr, &pyop, &pystrength))
31 return 0;
32 if (!Expression::TypeCheck(pyexpr))
33 return cppy::type_error(pyexpr, "Expression");
34 kiwi::RelationalOperator op;
35 if (!convert_to_relational_op(pyop, op))
36 return 0;
37 double strength = kiwi::strength::required;
38 if (pystrength && !convert_to_strength(pystrength, strength))
39 return 0;
40 cppy::ptr pycn(PyType_GenericNew(type, args, kwargs));
41 if (!pycn)
42 return 0;
43 Constraint *cn = reinterpret_cast<Constraint *>(pycn.get());
44 cn->expression = reduce_expression(pyexpr);
45 if (!cn->expression)
46 return 0;
47
48 ACQUIRE_GLOBAL_LOCK();
49 kiwi::Expression expr(convert_to_kiwi_expression(cn->expression));
50 new (&cn->constraint) kiwi::Constraint(expr, op, strength);
51 RELEASE_GLOBAL_LOCK();
52
53 return pycn.release();
54 }
55
56 void Constraint_clear(Constraint *self)
57 {
58 Py_CLEAR(self->expression);
59 }
60
61 int Constraint_traverse(Constraint *self, visitproc visit, void *arg)
62 {
63 Py_VISIT(self->expression);
64 #if PY_VERSION_HEX >= 0x03090000
65 // This was not needed before Python 3.9 (Python issue 35810 and 40217)
66 Py_VISIT(Py_TYPE(self));
67 #endif
68 return 0;
69 }
70
71 void Constraint_dealloc(Constraint *self)
72 {
73 PyObject_GC_UnTrack(self);
74 Constraint_clear(self);
75 ACQUIRE_GLOBAL_LOCK();
76 self->constraint.~Constraint();
77 RELEASE_GLOBAL_LOCK();
78 Py_TYPE(self)->tp_free(pyobject_cast(self));
79 }
80
81 PyObject *
82 Constraint_repr(Constraint *self)
83 {
84 std::stringstream stream;
85 Expression *expr = reinterpret_cast<Expression *>(self->expression);
86 Py_ssize_t size = PyTuple_GET_SIZE(expr->terms);
87 for (Py_ssize_t i = 0; i < size; ++i)
88 {
89 PyObject *item = PyTuple_GET_ITEM(expr->terms, i);
90 Term *term = reinterpret_cast<Term *>(item);
91 stream << term->coefficient << " * ";
92 ACQUIRE_GLOBAL_LOCK();
93 std::string name = reinterpret_cast<Variable *>(term->variable)->variable.name();
94 RELEASE_GLOBAL_LOCK();
95 stream << name;
96 stream << " + ";
97 }
98 stream << expr->constant;
99
100 ACQUIRE_GLOBAL_LOCK();
101 kiwi::RelationalOperator op = self->constraint.op();
102 double strength = self->constraint.strength();
103 bool violated = self->constraint.violated();
104 RELEASE_GLOBAL_LOCK();
105
106 switch (op)
107 {
108 case kiwi::OP_EQ:
109 stream << " == 0";
110 break;
111 case kiwi::OP_LE:
112 stream << " <= 0";
113 break;
114 case kiwi::OP_GE:
115 stream << " >= 0";
116 break;
117 }
118 stream << " | strength = " << strength;
119 if (violated)
120 {
121 stream << " (VIOLATED)";
122 }
123 return PyUnicode_FromString(stream.str().c_str());
124 }
125
126 PyObject *
127 Constraint_expression(Constraint *self)
128 {
129 return cppy::incref(self->expression);
130 }
131
132 PyObject *
133 Constraint_op(Constraint *self)
134 {
135 PyObject *res = 0;
136
137 ACQUIRE_GLOBAL_LOCK();
138 kiwi::RelationalOperator op = self->constraint.op();
139 RELEASE_GLOBAL_LOCK();
140
141 switch (op)
142 {
143 case kiwi::OP_EQ:
144 res = PyUnicode_FromString("==");
145 break;
146 case kiwi::OP_LE:
147 res = PyUnicode_FromString("<=");
148 break;
149 case kiwi::OP_GE:
150 res = PyUnicode_FromString(">=");
151 break;
152 }
153 return res;
154 }
155
156 PyObject *
157 Constraint_strength(Constraint *self)
158 {
159 ACQUIRE_GLOBAL_LOCK();
160 double strength = self->constraint.strength();
161 RELEASE_GLOBAL_LOCK();
162 return PyFloat_FromDouble(strength);
163 }
164
165 PyObject *
166 Constraint_violated(Constraint *self)
167 {
168 ACQUIRE_GLOBAL_LOCK();
169 bool violated = self->constraint.violated();
170 RELEASE_GLOBAL_LOCK();
171
172 if (violated) {
173 Py_RETURN_TRUE;
174 } else {
175 Py_RETURN_FALSE;
176 }
177 }
178
179 PyObject *
180 Constraint_or(PyObject *pyoldcn, PyObject *value)
181 {
182 if (!Constraint::TypeCheck(pyoldcn))
183 std::swap(pyoldcn, value);
184 double strength;
185 if (!convert_to_strength(value, strength))
186 return 0;
187 PyObject *pynewcn = PyType_GenericNew(Constraint::TypeObject, 0, 0);
188 if (!pynewcn)
189 return 0;
190 Constraint *oldcn = reinterpret_cast<Constraint *>(pyoldcn);
191 Constraint *newcn = reinterpret_cast<Constraint *>(pynewcn);
192 newcn->expression = cppy::incref(oldcn->expression);
193
194 ACQUIRE_GLOBAL_LOCK();
195 new (&newcn->constraint) kiwi::Constraint(oldcn->constraint, strength);
196 RELEASE_GLOBAL_LOCK();
197
198 return pynewcn;
199 }
200
201 static PyMethodDef
202 Constraint_methods[] = {
203 {"expression", (PyCFunction)Constraint_expression, METH_NOARGS,
204 "Get the expression object for the constraint."},
205 {"op", (PyCFunction)Constraint_op, METH_NOARGS,
206 "Get the relational operator for the constraint."},
207 {"strength", (PyCFunction)Constraint_strength, METH_NOARGS,
208 "Get the strength for the constraint."},
209 {"violated", (PyCFunction)Constraint_violated, METH_NOARGS,
210 "Return whether or not the constraint was violated "
211 "during the last solver pass."},
212 {0} // sentinel
213 };
214
215 static PyType_Slot Constraint_Type_slots[] = {
216 {Py_tp_dealloc, void_cast(Constraint_dealloc)}, /* tp_dealloc */
217 {Py_tp_traverse, void_cast(Constraint_traverse)}, /* tp_traverse */
218 {Py_tp_clear, void_cast(Constraint_clear)}, /* tp_clear */
219 {Py_tp_repr, void_cast(Constraint_repr)}, /* tp_repr */
220 {Py_tp_methods, void_cast(Constraint_methods)}, /* tp_methods */
221 {Py_tp_new, void_cast(Constraint_new)}, /* tp_new */
222 {Py_tp_alloc, void_cast(PyType_GenericAlloc)}, /* tp_alloc */
223 {Py_tp_free, void_cast(PyObject_GC_Del)}, /* tp_free */
224 {Py_nb_or, void_cast(Constraint_or)}, /* nb_or */
225 {0, 0},
226 };
227
228 } // namespace
229
230 // Initialize static variables (otherwise the compiler eliminates them)
231 PyTypeObject *Constraint::TypeObject = NULL;
232
233 PyType_Spec Constraint::TypeObject_Spec = {
234 "kiwisolver.Constraint", /* tp_name */
235 sizeof(Constraint), /* tp_basicsize */
236 0, /* tp_itemsize */
237 Py_TPFLAGS_DEFAULT |
238 Py_TPFLAGS_HAVE_GC |
239 Py_TPFLAGS_BASETYPE, /* tp_flags */
240 Constraint_Type_slots /* slots */
241 };
242
243 bool Constraint::Ready()
244 {
245 // The reference will be handled by the module to which we will add the type
246 TypeObject = pytype_cast(PyType_FromSpec(&TypeObject_Spec));
247 if (!TypeObject)
248 {
249 return false;
250 }
251 return true;
252 }
253
254 } // namespace kiwisolver