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