1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 */
16
17 /** \file
18 * \ingroup freestyle
19 */
20
21 #include "BPy_SShape.h"
22
23 #include "BPy_BBox.h"
24 #include "BPy_Convert.h"
25 #include "BPy_Id.h"
26 #include "Interface0D/BPy_SVertex.h"
27 #include "Interface1D/BPy_FEdge.h"
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 ///////////////////////////////////////////////////////////////////////////////////////////
34
35 //-------------------MODULE INITIALIZATION--------------------------------
SShape_Init(PyObject * module)36 int SShape_Init(PyObject *module)
37 {
38 if (module == NULL) {
39 return -1;
40 }
41
42 if (PyType_Ready(&SShape_Type) < 0) {
43 return -1;
44 }
45 Py_INCREF(&SShape_Type);
46 PyModule_AddObject(module, "SShape", (PyObject *)&SShape_Type);
47
48 return 0;
49 }
50
51 /*----------------------SShape methods ----------------------------*/
52
53 PyDoc_STRVAR(
54 SShape_doc,
55 "Class to define a feature shape. It is the gathering of feature\n"
56 "elements from an identified input shape.\n"
57 "\n"
58 ".. method:: __init__()\n"
59 " __init__(brother)\n"
60 "\n"
61 " Creates a :class:`SShape` class using either a default constructor or copy constructor.\n"
62 "\n"
63 " :arg brother: An SShape object.\n"
64 " :type brother: :class:`SShape`");
65
SShape_init(BPy_SShape * self,PyObject * args,PyObject * kwds)66 static int SShape_init(BPy_SShape *self, PyObject *args, PyObject *kwds)
67 {
68 static const char *kwlist[] = {"brother", NULL};
69 PyObject *brother = 0;
70
71 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O!", (char **)kwlist, &SShape_Type, &brother)) {
72 return -1;
73 }
74 if (!brother) {
75 self->ss = new SShape();
76 }
77 else {
78 self->ss = new SShape(*(((BPy_SShape *)brother)->ss));
79 }
80 self->borrowed = false;
81 return 0;
82 }
83
SShape_dealloc(BPy_SShape * self)84 static void SShape_dealloc(BPy_SShape *self)
85 {
86 if (self->ss && !self->borrowed) {
87 delete self->ss;
88 }
89 Py_TYPE(self)->tp_free((PyObject *)self);
90 }
91
SShape_repr(BPy_SShape * self)92 static PyObject *SShape_repr(BPy_SShape *self)
93 {
94 return PyUnicode_FromFormat("SShape - address: %p", self->ss);
95 }
96
97 static char SShape_add_edge_doc[] =
98 ".. method:: add_edge(edge)\n"
99 "\n"
100 " Adds an FEdge to the list of FEdges.\n"
101 "\n"
102 " :arg edge: An FEdge object.\n"
103 " :type edge: :class:`FEdge`\n";
104
SShape_add_edge(BPy_SShape * self,PyObject * args,PyObject * kwds)105 static PyObject *SShape_add_edge(BPy_SShape *self, PyObject *args, PyObject *kwds)
106 {
107 static const char *kwlist[] = {"edge", NULL};
108 PyObject *py_fe = 0;
109
110 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!", (char **)kwlist, &FEdge_Type, &py_fe)) {
111 return NULL;
112 }
113 self->ss->AddEdge(((BPy_FEdge *)py_fe)->fe);
114 Py_RETURN_NONE;
115 }
116
117 PyDoc_STRVAR(SShape_add_vertex_doc,
118 ".. method:: add_vertex(vertex)\n"
119 "\n"
120 " Adds an SVertex to the list of SVertex of this Shape. The SShape\n"
121 " attribute of the SVertex is also set to this SShape.\n"
122 "\n"
123 " :arg vertex: An SVertex object.\n"
124 " :type vertex: :class:`SVertex`");
125
SShape_add_vertex(BPy_SShape * self,PyObject * args,PyObject * kwds)126 static PyObject *SShape_add_vertex(BPy_SShape *self, PyObject *args, PyObject *kwds)
127 {
128 static const char *kwlist[] = {"edge", NULL};
129 PyObject *py_sv = 0;
130
131 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!", (char **)kwlist, &SVertex_Type, &py_sv)) {
132 return NULL;
133 }
134 self->ss->AddNewVertex(((BPy_SVertex *)py_sv)->sv);
135 Py_RETURN_NONE;
136 }
137
138 PyDoc_STRVAR(SShape_compute_bbox_doc,
139 ".. method:: compute_bbox()\n"
140 "\n"
141 " Compute the bbox of the SShape.");
142
SShape_compute_bbox(BPy_SShape * self)143 static PyObject *SShape_compute_bbox(BPy_SShape *self)
144 {
145 self->ss->ComputeBBox();
146 Py_RETURN_NONE;
147 }
148
149 // const Material & material (unsigned i) const
150 // const vector< Material > & materials () const
151 // void SetMaterials (const vector< Material > &iMaterials)
152
153 static PyMethodDef BPy_SShape_methods[] = {
154 {"add_edge", (PyCFunction)SShape_add_edge, METH_VARARGS | METH_KEYWORDS, SShape_add_edge_doc},
155 {"add_vertex",
156 (PyCFunction)SShape_add_vertex,
157 METH_VARARGS | METH_KEYWORDS,
158 SShape_add_vertex_doc},
159 {"compute_bbox", (PyCFunction)SShape_compute_bbox, METH_NOARGS, SShape_compute_bbox_doc},
160 {NULL, NULL, 0, NULL},
161 };
162
163 /*----------------------SShape get/setters ----------------------------*/
164
165 PyDoc_STRVAR(SShape_id_doc,
166 "The Id of this SShape.\n"
167 "\n"
168 ":type: :class:`Id`");
169
SShape_id_get(BPy_SShape * self,void * UNUSED (closure))170 static PyObject *SShape_id_get(BPy_SShape *self, void *UNUSED(closure))
171 {
172 Id id(self->ss->getId());
173 return BPy_Id_from_Id(id); // return a copy
174 }
175
SShape_id_set(BPy_SShape * self,PyObject * value,void * UNUSED (closure))176 static int SShape_id_set(BPy_SShape *self, PyObject *value, void *UNUSED(closure))
177 {
178 if (!BPy_Id_Check(value)) {
179 PyErr_SetString(PyExc_TypeError, "value must be an Id");
180 return -1;
181 }
182 self->ss->setId(*(((BPy_Id *)value)->id));
183 return 0;
184 }
185
186 PyDoc_STRVAR(SShape_name_doc,
187 "The name of the SShape.\n"
188 "\n"
189 ":type: str");
190
SShape_name_get(BPy_SShape * self,void * UNUSED (closure))191 static PyObject *SShape_name_get(BPy_SShape *self, void *UNUSED(closure))
192 {
193 return PyUnicode_FromString(self->ss->getName().c_str());
194 }
195
SShape_name_set(BPy_SShape * self,PyObject * value,void * UNUSED (closure))196 static int SShape_name_set(BPy_SShape *self, PyObject *value, void *UNUSED(closure))
197 {
198 if (!PyUnicode_Check(value)) {
199 PyErr_SetString(PyExc_TypeError, "value must be a string");
200 return -1;
201 }
202 const char *name = _PyUnicode_AsString(value);
203 self->ss->setName(name);
204 return 0;
205 }
206
207 PyDoc_STRVAR(SShape_bbox_doc,
208 "The bounding box of the SShape.\n"
209 "\n"
210 ":type: :class:`BBox`");
211
SShape_bbox_get(BPy_SShape * self,void * UNUSED (closure))212 static PyObject *SShape_bbox_get(BPy_SShape *self, void *UNUSED(closure))
213 {
214 BBox<Vec3r> bb(self->ss->bbox());
215 return BPy_BBox_from_BBox(bb); // return a copy
216 }
217
SShape_bbox_set(BPy_SShape * self,PyObject * value,void * UNUSED (closure))218 static int SShape_bbox_set(BPy_SShape *self, PyObject *value, void *UNUSED(closure))
219 {
220 if (!BPy_BBox_Check(value)) {
221 PyErr_SetString(PyExc_TypeError, "value must be a BBox");
222 return -1;
223 }
224 self->ss->setBBox(*(((BPy_BBox *)value)->bb));
225 return 0;
226 }
227
228 PyDoc_STRVAR(SShape_vertices_doc,
229 "The list of vertices constituting this SShape.\n"
230 "\n"
231 ":type: List of :class:`SVertex` objects");
232
SShape_vertices_get(BPy_SShape * self,void * UNUSED (closure))233 static PyObject *SShape_vertices_get(BPy_SShape *self, void *UNUSED(closure))
234 {
235
236 vector<SVertex *> vertices = self->ss->getVertexList();
237 vector<SVertex *>::iterator it;
238 PyObject *py_vertices = PyList_New(vertices.size());
239 unsigned int i = 0;
240
241 for (it = vertices.begin(); it != vertices.end(); it++) {
242 PyList_SET_ITEM(py_vertices, i++, BPy_SVertex_from_SVertex(*(*it)));
243 }
244
245 return py_vertices;
246 }
247
248 PyDoc_STRVAR(SShape_edges_doc,
249 "The list of edges constituting this SShape.\n"
250 "\n"
251 ":type: List of :class:`FEdge` objects");
252
SShape_edges_get(BPy_SShape * self,void * UNUSED (closure))253 static PyObject *SShape_edges_get(BPy_SShape *self, void *UNUSED(closure))
254 {
255
256 vector<FEdge *> edges = self->ss->getEdgeList();
257 vector<FEdge *>::iterator it;
258 PyObject *py_edges = PyList_New(edges.size());
259 unsigned int i = 0;
260
261 for (it = edges.begin(); it != edges.end(); it++) {
262 PyList_SET_ITEM(py_edges, i++, Any_BPy_FEdge_from_FEdge(*(*it)));
263 }
264
265 return py_edges;
266 }
267
268 static PyGetSetDef BPy_SShape_getseters[] = {
269 {"id", (getter)SShape_id_get, (setter)SShape_id_set, SShape_id_doc, NULL},
270 {"name", (getter)SShape_name_get, (setter)SShape_name_set, SShape_name_doc, NULL},
271 {"bbox", (getter)SShape_bbox_get, (setter)SShape_bbox_set, SShape_bbox_doc, NULL},
272 {"edges", (getter)SShape_edges_get, (setter)NULL, SShape_edges_doc, NULL},
273 {"vertices", (getter)SShape_vertices_get, (setter)NULL, SShape_vertices_doc, NULL},
274 {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
275 };
276
277 /*-----------------------BPy_SShape type definition ------------------------------*/
278
279 PyTypeObject SShape_Type = {
280 PyVarObject_HEAD_INIT(NULL, 0) "SShape", /* tp_name */
281 sizeof(BPy_SShape), /* tp_basicsize */
282 0, /* tp_itemsize */
283 (destructor)SShape_dealloc, /* tp_dealloc */
284 0, /* tp_print */
285 0, /* tp_getattr */
286 0, /* tp_setattr */
287 0, /* tp_reserved */
288 (reprfunc)SShape_repr, /* tp_repr */
289 0, /* tp_as_number */
290 0, /* tp_as_sequence */
291 0, /* tp_as_mapping */
292 0, /* tp_hash */
293 0, /* tp_call */
294 0, /* tp_str */
295 0, /* tp_getattro */
296 0, /* tp_setattro */
297 0, /* tp_as_buffer */
298 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
299 SShape_doc, /* tp_doc */
300 0, /* tp_traverse */
301 0, /* tp_clear */
302 0, /* tp_richcompare */
303 0, /* tp_weaklistoffset */
304 0, /* tp_iter */
305 0, /* tp_iternext */
306 BPy_SShape_methods, /* tp_methods */
307 0, /* tp_members */
308 BPy_SShape_getseters, /* tp_getset */
309 0, /* tp_base */
310 0, /* tp_dict */
311 0, /* tp_descr_get */
312 0, /* tp_descr_set */
313 0, /* tp_dictoffset */
314 (initproc)SShape_init, /* tp_init */
315 0, /* tp_alloc */
316 PyType_GenericNew, /* tp_new */
317 };
318
319 ///////////////////////////////////////////////////////////////////////////////////////////
320
321 #ifdef __cplusplus
322 }
323 #endif
324