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