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_Convert.h"
22 
23 #include "BPy_BBox.h"
24 #include "BPy_FrsMaterial.h"
25 #include "BPy_Id.h"
26 #include "BPy_IntegrationType.h"
27 #include "BPy_Interface0D.h"
28 #include "BPy_Interface1D.h"
29 #include "BPy_MediumType.h"
30 #include "BPy_Nature.h"
31 #include "BPy_SShape.h"
32 #include "BPy_StrokeAttribute.h"
33 #include "BPy_ViewShape.h"
34 #include "Interface0D/BPy_CurvePoint.h"
35 #include "Interface0D/BPy_SVertex.h"
36 #include "Interface0D/BPy_ViewVertex.h"
37 #include "Interface0D/CurvePoint/BPy_StrokeVertex.h"
38 #include "Interface0D/ViewVertex/BPy_NonTVertex.h"
39 #include "Interface0D/ViewVertex/BPy_TVertex.h"
40 #include "Interface1D/BPy_FEdge.h"
41 #include "Interface1D/BPy_Stroke.h"
42 #include "Interface1D/BPy_ViewEdge.h"
43 #include "Interface1D/Curve/BPy_Chain.h"
44 #include "Interface1D/FEdge/BPy_FEdgeSharp.h"
45 #include "Interface1D/FEdge/BPy_FEdgeSmooth.h"
46 
47 #include "Iterator/BPy_AdjacencyIterator.h"
48 #include "Iterator/BPy_ChainPredicateIterator.h"
49 #include "Iterator/BPy_ChainSilhouetteIterator.h"
50 #include "Iterator/BPy_ChainingIterator.h"
51 #include "Iterator/BPy_CurvePointIterator.h"
52 #include "Iterator/BPy_Interface0DIterator.h"
53 #include "Iterator/BPy_SVertexIterator.h"
54 #include "Iterator/BPy_StrokeVertexIterator.h"
55 #include "Iterator/BPy_ViewEdgeIterator.h"
56 #include "Iterator/BPy_orientedViewEdgeIterator.h"
57 
58 #include "../stroke/StrokeRep.h"
59 
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63 
64 ///////////////////////////////////////////////////////////////////////////////////////////
65 
66 //==============================
67 // C++ => Python
68 //==============================
69 
PyBool_from_bool(bool b)70 PyObject *PyBool_from_bool(bool b)
71 {
72   return PyBool_FromLong(b ? 1 : 0);
73 }
74 
Vector_from_Vec2f(Vec2f & vec)75 PyObject *Vector_from_Vec2f(Vec2f &vec)
76 {
77   float vec_data[2];  // because vec->_coord is protected
78   vec_data[0] = vec.x();
79   vec_data[1] = vec.y();
80   return Vector_CreatePyObject(vec_data, 2, NULL);
81 }
82 
Vector_from_Vec3f(Vec3f & vec)83 PyObject *Vector_from_Vec3f(Vec3f &vec)
84 {
85   float vec_data[3];  // because vec->_coord is protected
86   vec_data[0] = vec.x();
87   vec_data[1] = vec.y();
88   vec_data[2] = vec.z();
89   return Vector_CreatePyObject(vec_data, 3, NULL);
90 }
91 
Vector_from_Vec3r(Vec3r & vec)92 PyObject *Vector_from_Vec3r(Vec3r &vec)
93 {
94   float vec_data[3];  // because vec->_coord is protected
95   vec_data[0] = vec.x();
96   vec_data[1] = vec.y();
97   vec_data[2] = vec.z();
98   return Vector_CreatePyObject(vec_data, 3, NULL);
99 }
100 
BPy_Id_from_Id(Id & id)101 PyObject *BPy_Id_from_Id(Id &id)
102 {
103   PyObject *py_id = Id_Type.tp_new(&Id_Type, 0, 0);
104   ((BPy_Id *)py_id)->id = new Id(id.getFirst(), id.getSecond());
105   return py_id;
106 }
107 
Any_BPy_Interface0D_from_Interface0D(Interface0D & if0D)108 PyObject *Any_BPy_Interface0D_from_Interface0D(Interface0D &if0D)
109 {
110   if (typeid(if0D) == typeid(CurvePoint)) {
111     return BPy_CurvePoint_from_CurvePoint(dynamic_cast<CurvePoint &>(if0D));
112   }
113   if (typeid(if0D) == typeid(StrokeVertex)) {
114     return BPy_StrokeVertex_from_StrokeVertex(dynamic_cast<StrokeVertex &>(if0D));
115   }
116   if (typeid(if0D) == typeid(SVertex)) {
117     return BPy_SVertex_from_SVertex(dynamic_cast<SVertex &>(if0D));
118   }
119   if (typeid(if0D) == typeid(ViewVertex)) {
120     return BPy_ViewVertex_from_ViewVertex(dynamic_cast<ViewVertex &>(if0D));
121   }
122   if (typeid(if0D) == typeid(NonTVertex)) {
123     return BPy_NonTVertex_from_NonTVertex(dynamic_cast<NonTVertex &>(if0D));
124   }
125   if (typeid(if0D) == typeid(TVertex)) {
126     return BPy_TVertex_from_TVertex(dynamic_cast<TVertex &>(if0D));
127   }
128   if (typeid(if0D) == typeid(Interface0D)) {
129     return BPy_Interface0D_from_Interface0D(if0D);
130   }
131   string msg("unexpected type: " + if0D.getExactTypeName());
132   PyErr_SetString(PyExc_TypeError, msg.c_str());
133   return NULL;
134 }
135 
Any_BPy_Interface1D_from_Interface1D(Interface1D & if1D)136 PyObject *Any_BPy_Interface1D_from_Interface1D(Interface1D &if1D)
137 {
138   if (typeid(if1D) == typeid(ViewEdge)) {
139     return BPy_ViewEdge_from_ViewEdge(dynamic_cast<ViewEdge &>(if1D));
140   }
141   if (typeid(if1D) == typeid(Chain)) {
142     return BPy_Chain_from_Chain(dynamic_cast<Chain &>(if1D));
143   }
144   if (typeid(if1D) == typeid(Stroke)) {
145     return BPy_Stroke_from_Stroke(dynamic_cast<Stroke &>(if1D));
146   }
147   if (typeid(if1D) == typeid(FEdgeSharp)) {
148     return BPy_FEdgeSharp_from_FEdgeSharp(dynamic_cast<FEdgeSharp &>(if1D));
149   }
150   if (typeid(if1D) == typeid(FEdgeSmooth)) {
151     return BPy_FEdgeSmooth_from_FEdgeSmooth(dynamic_cast<FEdgeSmooth &>(if1D));
152   }
153   if (typeid(if1D) == typeid(FEdge)) {
154     return BPy_FEdge_from_FEdge(dynamic_cast<FEdge &>(if1D));
155   }
156   if (typeid(if1D) == typeid(Interface1D)) {
157     return BPy_Interface1D_from_Interface1D(if1D);
158   }
159   string msg("unexpected type: " + if1D.getExactTypeName());
160   PyErr_SetString(PyExc_TypeError, msg.c_str());
161   return NULL;
162 }
163 
Any_BPy_FEdge_from_FEdge(FEdge & fe)164 PyObject *Any_BPy_FEdge_from_FEdge(FEdge &fe)
165 {
166   if (typeid(fe) == typeid(FEdgeSharp)) {
167     return BPy_FEdgeSharp_from_FEdgeSharp(dynamic_cast<FEdgeSharp &>(fe));
168   }
169   if (typeid(fe) == typeid(FEdgeSmooth)) {
170     return BPy_FEdgeSmooth_from_FEdgeSmooth(dynamic_cast<FEdgeSmooth &>(fe));
171   }
172   if (typeid(fe) == typeid(FEdge)) {
173     return BPy_FEdge_from_FEdge(fe);
174   }
175   string msg("unexpected type: " + fe.getExactTypeName());
176   PyErr_SetString(PyExc_TypeError, msg.c_str());
177   return NULL;
178 }
179 
Any_BPy_ViewVertex_from_ViewVertex(ViewVertex & vv)180 PyObject *Any_BPy_ViewVertex_from_ViewVertex(ViewVertex &vv)
181 {
182   if (typeid(vv) == typeid(NonTVertex)) {
183     return BPy_NonTVertex_from_NonTVertex(dynamic_cast<NonTVertex &>(vv));
184   }
185   if (typeid(vv) == typeid(TVertex)) {
186     return BPy_TVertex_from_TVertex(dynamic_cast<TVertex &>(vv));
187   }
188   if (typeid(vv) == typeid(ViewVertex)) {
189     return BPy_ViewVertex_from_ViewVertex(vv);
190   }
191   string msg("unexpected type: " + vv.getExactTypeName());
192   PyErr_SetString(PyExc_TypeError, msg.c_str());
193   return NULL;
194 }
195 
BPy_Interface0D_from_Interface0D(Interface0D & if0D)196 PyObject *BPy_Interface0D_from_Interface0D(Interface0D &if0D)
197 {
198   PyObject *py_if0D = Interface0D_Type.tp_new(&Interface0D_Type, 0, 0);
199   ((BPy_Interface0D *)py_if0D)->if0D = &if0D;
200   ((BPy_Interface0D *)py_if0D)->borrowed = true;
201   return py_if0D;
202 }
203 
BPy_Interface1D_from_Interface1D(Interface1D & if1D)204 PyObject *BPy_Interface1D_from_Interface1D(Interface1D &if1D)
205 {
206   PyObject *py_if1D = Interface1D_Type.tp_new(&Interface1D_Type, 0, 0);
207   ((BPy_Interface1D *)py_if1D)->if1D = &if1D;
208   ((BPy_Interface1D *)py_if1D)->borrowed = true;
209   return py_if1D;
210 }
211 
BPy_SVertex_from_SVertex(SVertex & sv)212 PyObject *BPy_SVertex_from_SVertex(SVertex &sv)
213 {
214   PyObject *py_sv = SVertex_Type.tp_new(&SVertex_Type, 0, 0);
215   ((BPy_SVertex *)py_sv)->sv = &sv;
216   ((BPy_SVertex *)py_sv)->py_if0D.if0D = ((BPy_SVertex *)py_sv)->sv;
217   ((BPy_SVertex *)py_sv)->py_if0D.borrowed = true;
218   return py_sv;
219 }
220 
BPy_FEdgeSharp_from_FEdgeSharp(FEdgeSharp & fes)221 PyObject *BPy_FEdgeSharp_from_FEdgeSharp(FEdgeSharp &fes)
222 {
223   PyObject *py_fe = FEdgeSharp_Type.tp_new(&FEdgeSharp_Type, 0, 0);
224   ((BPy_FEdgeSharp *)py_fe)->fes = &fes;
225   ((BPy_FEdgeSharp *)py_fe)->py_fe.fe = ((BPy_FEdgeSharp *)py_fe)->fes;
226   ((BPy_FEdgeSharp *)py_fe)->py_fe.py_if1D.if1D = ((BPy_FEdgeSharp *)py_fe)->fes;
227   ((BPy_FEdgeSharp *)py_fe)->py_fe.py_if1D.borrowed = true;
228   return py_fe;
229 }
230 
BPy_FEdgeSmooth_from_FEdgeSmooth(FEdgeSmooth & fes)231 PyObject *BPy_FEdgeSmooth_from_FEdgeSmooth(FEdgeSmooth &fes)
232 {
233   PyObject *py_fe = FEdgeSmooth_Type.tp_new(&FEdgeSmooth_Type, 0, 0);
234   ((BPy_FEdgeSmooth *)py_fe)->fes = &fes;
235   ((BPy_FEdgeSmooth *)py_fe)->py_fe.fe = ((BPy_FEdgeSmooth *)py_fe)->fes;
236   ((BPy_FEdgeSmooth *)py_fe)->py_fe.py_if1D.if1D = ((BPy_FEdgeSmooth *)py_fe)->fes;
237   ((BPy_FEdgeSmooth *)py_fe)->py_fe.py_if1D.borrowed = true;
238   return py_fe;
239 }
240 
BPy_FEdge_from_FEdge(FEdge & fe)241 PyObject *BPy_FEdge_from_FEdge(FEdge &fe)
242 {
243   PyObject *py_fe = FEdge_Type.tp_new(&FEdge_Type, 0, 0);
244   ((BPy_FEdge *)py_fe)->fe = &fe;
245   ((BPy_FEdge *)py_fe)->py_if1D.if1D = ((BPy_FEdge *)py_fe)->fe;
246   ((BPy_FEdge *)py_fe)->py_if1D.borrowed = true;
247   return py_fe;
248 }
249 
BPy_Nature_from_Nature(unsigned short n)250 PyObject *BPy_Nature_from_Nature(unsigned short n)
251 {
252   PyObject *args = PyTuple_New(1);
253   PyTuple_SET_ITEM(args, 0, PyLong_FromLong(n));
254   PyObject *py_n = Nature_Type.tp_new(&Nature_Type, args, NULL);
255   Py_DECREF(args);
256   return py_n;
257 }
258 
BPy_Stroke_from_Stroke(Stroke & s)259 PyObject *BPy_Stroke_from_Stroke(Stroke &s)
260 {
261   PyObject *py_s = Stroke_Type.tp_new(&Stroke_Type, 0, 0);
262   ((BPy_Stroke *)py_s)->s = &s;
263   ((BPy_Stroke *)py_s)->py_if1D.if1D = ((BPy_Stroke *)py_s)->s;
264   ((BPy_Stroke *)py_s)->py_if1D.borrowed = true;
265   return py_s;
266 }
267 
BPy_StrokeAttribute_from_StrokeAttribute(StrokeAttribute & sa)268 PyObject *BPy_StrokeAttribute_from_StrokeAttribute(StrokeAttribute &sa)
269 {
270   PyObject *py_sa = StrokeAttribute_Type.tp_new(&StrokeAttribute_Type, 0, 0);
271   ((BPy_StrokeAttribute *)py_sa)->sa = &sa;
272   ((BPy_StrokeAttribute *)py_sa)->borrowed = true;
273   return py_sa;
274 }
275 
BPy_MediumType_from_MediumType(Stroke::MediumType n)276 PyObject *BPy_MediumType_from_MediumType(Stroke::MediumType n)
277 {
278   PyObject *args = PyTuple_New(1);
279   PyTuple_SET_ITEM(args, 0, PyLong_FromLong(n));
280   PyObject *py_mt = MediumType_Type.tp_new(&MediumType_Type, args, NULL);
281   Py_DECREF(args);
282   return py_mt;
283 }
284 
BPy_StrokeVertex_from_StrokeVertex(StrokeVertex & sv)285 PyObject *BPy_StrokeVertex_from_StrokeVertex(StrokeVertex &sv)
286 {
287   PyObject *py_sv = StrokeVertex_Type.tp_new(&StrokeVertex_Type, 0, 0);
288   ((BPy_StrokeVertex *)py_sv)->sv = &sv;
289   ((BPy_StrokeVertex *)py_sv)->py_cp.cp = ((BPy_StrokeVertex *)py_sv)->sv;
290   ((BPy_StrokeVertex *)py_sv)->py_cp.py_if0D.if0D = ((BPy_StrokeVertex *)py_sv)->sv;
291   ((BPy_StrokeVertex *)py_sv)->py_cp.py_if0D.borrowed = true;
292   return py_sv;
293 }
294 
BPy_ViewVertex_from_ViewVertex(ViewVertex & vv)295 PyObject *BPy_ViewVertex_from_ViewVertex(ViewVertex &vv)
296 {
297   PyObject *py_vv = ViewVertex_Type.tp_new(&ViewVertex_Type, 0, 0);
298   ((BPy_ViewVertex *)py_vv)->vv = &vv;
299   ((BPy_ViewVertex *)py_vv)->py_if0D.if0D = ((BPy_ViewVertex *)py_vv)->vv;
300   ((BPy_ViewVertex *)py_vv)->py_if0D.borrowed = true;
301   return py_vv;
302 }
303 
BPy_NonTVertex_from_NonTVertex(NonTVertex & ntv)304 PyObject *BPy_NonTVertex_from_NonTVertex(NonTVertex &ntv)
305 {
306   PyObject *py_ntv = NonTVertex_Type.tp_new(&NonTVertex_Type, 0, 0);
307   ((BPy_NonTVertex *)py_ntv)->ntv = &ntv;
308   ((BPy_NonTVertex *)py_ntv)->py_vv.vv = ((BPy_NonTVertex *)py_ntv)->ntv;
309   ((BPy_NonTVertex *)py_ntv)->py_vv.py_if0D.if0D = ((BPy_NonTVertex *)py_ntv)->ntv;
310   ((BPy_NonTVertex *)py_ntv)->py_vv.py_if0D.borrowed = true;
311   return py_ntv;
312 }
313 
BPy_TVertex_from_TVertex(TVertex & tv)314 PyObject *BPy_TVertex_from_TVertex(TVertex &tv)
315 {
316   PyObject *py_tv = TVertex_Type.tp_new(&TVertex_Type, 0, 0);
317   ((BPy_TVertex *)py_tv)->tv = &tv;
318   ((BPy_TVertex *)py_tv)->py_vv.vv = ((BPy_TVertex *)py_tv)->tv;
319   ((BPy_TVertex *)py_tv)->py_vv.py_if0D.if0D = ((BPy_TVertex *)py_tv)->tv;
320   ((BPy_TVertex *)py_tv)->py_vv.py_if0D.borrowed = true;
321   return py_tv;
322 }
323 
BPy_BBox_from_BBox(const BBox<Vec3r> & bb)324 PyObject *BPy_BBox_from_BBox(const BBox<Vec3r> &bb)
325 {
326   PyObject *py_bb = BBox_Type.tp_new(&BBox_Type, 0, 0);
327   ((BPy_BBox *)py_bb)->bb = new BBox<Vec3r>(bb);
328   return py_bb;
329 }
330 
BPy_ViewEdge_from_ViewEdge(ViewEdge & ve)331 PyObject *BPy_ViewEdge_from_ViewEdge(ViewEdge &ve)
332 {
333   PyObject *py_ve = ViewEdge_Type.tp_new(&ViewEdge_Type, 0, 0);
334   ((BPy_ViewEdge *)py_ve)->ve = &ve;
335   ((BPy_ViewEdge *)py_ve)->py_if1D.if1D = ((BPy_ViewEdge *)py_ve)->ve;
336   ((BPy_ViewEdge *)py_ve)->py_if1D.borrowed = true;
337   return py_ve;
338 }
339 
BPy_Chain_from_Chain(Chain & c)340 PyObject *BPy_Chain_from_Chain(Chain &c)
341 {
342   PyObject *py_c = Chain_Type.tp_new(&Chain_Type, 0, 0);
343   ((BPy_Chain *)py_c)->c = &c;
344   ((BPy_Chain *)py_c)->py_c.c = ((BPy_Chain *)py_c)->c;
345   ((BPy_Chain *)py_c)->py_c.py_if1D.if1D = ((BPy_Chain *)py_c)->c;
346   ((BPy_Chain *)py_c)->py_c.py_if1D.borrowed = true;
347   return py_c;
348 }
349 
BPy_SShape_from_SShape(SShape & ss)350 PyObject *BPy_SShape_from_SShape(SShape &ss)
351 {
352   PyObject *py_ss = SShape_Type.tp_new(&SShape_Type, 0, 0);
353   ((BPy_SShape *)py_ss)->ss = &ss;
354   ((BPy_SShape *)py_ss)->borrowed = true;
355   return py_ss;
356 }
357 
BPy_ViewShape_from_ViewShape(ViewShape & vs)358 PyObject *BPy_ViewShape_from_ViewShape(ViewShape &vs)
359 {
360   PyObject *py_vs = ViewShape_Type.tp_new(&ViewShape_Type, 0, 0);
361   ((BPy_ViewShape *)py_vs)->vs = &vs;
362   ((BPy_ViewShape *)py_vs)->borrowed = true;
363   ((BPy_ViewShape *)py_vs)->py_ss = NULL;
364   return py_vs;
365 }
366 
BPy_FrsMaterial_from_FrsMaterial(const FrsMaterial & m)367 PyObject *BPy_FrsMaterial_from_FrsMaterial(const FrsMaterial &m)
368 {
369   PyObject *py_m = FrsMaterial_Type.tp_new(&FrsMaterial_Type, 0, 0);
370   ((BPy_FrsMaterial *)py_m)->m = new FrsMaterial(m);
371   return py_m;
372 }
373 
BPy_IntegrationType_from_IntegrationType(IntegrationType i)374 PyObject *BPy_IntegrationType_from_IntegrationType(IntegrationType i)
375 {
376   PyObject *args = PyTuple_New(1);
377   PyTuple_SET_ITEM(args, 0, PyLong_FromLong(i));
378   PyObject *py_it = IntegrationType_Type.tp_new(&IntegrationType_Type, args, NULL);
379   Py_DECREF(args);
380   return py_it;
381 }
382 
BPy_CurvePoint_from_CurvePoint(CurvePoint & cp)383 PyObject *BPy_CurvePoint_from_CurvePoint(CurvePoint &cp)
384 {
385   PyObject *py_cp = CurvePoint_Type.tp_new(&CurvePoint_Type, 0, 0);
386   // CurvePointIterator::operator*() returns a reference of a class data
387   // member whose value is mutable upon iteration over different CurvePoints.
388   // It is likely that such a mutable reference is passed to this function,
389   // so that a new allocated CurvePoint instance is created here to avoid
390   // nasty bugs (cf. T41464).
391   ((BPy_CurvePoint *)py_cp)->cp = new CurvePoint(cp);
392   ((BPy_CurvePoint *)py_cp)->py_if0D.if0D = ((BPy_CurvePoint *)py_cp)->cp;
393   ((BPy_CurvePoint *)py_cp)->py_if0D.borrowed = false;
394   return py_cp;
395 }
396 
BPy_directedViewEdge_from_directedViewEdge(ViewVertex::directedViewEdge & dve)397 PyObject *BPy_directedViewEdge_from_directedViewEdge(ViewVertex::directedViewEdge &dve)
398 {
399   PyObject *py_dve = PyTuple_New(2);
400   PyTuple_SET_ITEMS(
401       py_dve, BPy_ViewEdge_from_ViewEdge(*(dve.first)), PyBool_from_bool(dve.second));
402   return py_dve;
403 }
404 
405 //==============================
406 // Iterators
407 //==============================
408 
BPy_AdjacencyIterator_from_AdjacencyIterator(AdjacencyIterator & a_it)409 PyObject *BPy_AdjacencyIterator_from_AdjacencyIterator(AdjacencyIterator &a_it)
410 {
411   PyObject *py_a_it = AdjacencyIterator_Type.tp_new(&AdjacencyIterator_Type, 0, 0);
412   ((BPy_AdjacencyIterator *)py_a_it)->a_it = new AdjacencyIterator(a_it);
413   ((BPy_AdjacencyIterator *)py_a_it)->py_it.it = ((BPy_AdjacencyIterator *)py_a_it)->a_it;
414   ((BPy_AdjacencyIterator *)py_a_it)->at_start = true;
415   return py_a_it;
416 }
417 
BPy_Interface0DIterator_from_Interface0DIterator(Interface0DIterator & if0D_it,bool reversed)418 PyObject *BPy_Interface0DIterator_from_Interface0DIterator(Interface0DIterator &if0D_it,
419                                                            bool reversed)
420 {
421   PyObject *py_if0D_it = Interface0DIterator_Type.tp_new(&Interface0DIterator_Type, 0, 0);
422   ((BPy_Interface0DIterator *)py_if0D_it)->if0D_it = new Interface0DIterator(if0D_it);
423   ((BPy_Interface0DIterator *)py_if0D_it)->py_it.it =
424       ((BPy_Interface0DIterator *)py_if0D_it)->if0D_it;
425   ((BPy_Interface0DIterator *)py_if0D_it)->at_start = true;
426   ((BPy_Interface0DIterator *)py_if0D_it)->reversed = reversed;
427   return py_if0D_it;
428 }
429 
BPy_CurvePointIterator_from_CurvePointIterator(CurveInternal::CurvePointIterator & cp_it)430 PyObject *BPy_CurvePointIterator_from_CurvePointIterator(CurveInternal::CurvePointIterator &cp_it)
431 {
432   PyObject *py_cp_it = CurvePointIterator_Type.tp_new(&CurvePointIterator_Type, 0, 0);
433   ((BPy_CurvePointIterator *)py_cp_it)->cp_it = new CurveInternal::CurvePointIterator(cp_it);
434   ((BPy_CurvePointIterator *)py_cp_it)->py_it.it = ((BPy_CurvePointIterator *)py_cp_it)->cp_it;
435   return py_cp_it;
436 }
437 
BPy_StrokeVertexIterator_from_StrokeVertexIterator(StrokeInternal::StrokeVertexIterator & sv_it,bool reversed)438 PyObject *BPy_StrokeVertexIterator_from_StrokeVertexIterator(
439     StrokeInternal::StrokeVertexIterator &sv_it, bool reversed)
440 {
441   PyObject *py_sv_it = StrokeVertexIterator_Type.tp_new(&StrokeVertexIterator_Type, 0, 0);
442   ((BPy_StrokeVertexIterator *)py_sv_it)->sv_it = new StrokeInternal::StrokeVertexIterator(sv_it);
443   ((BPy_StrokeVertexIterator *)py_sv_it)->py_it.it = ((BPy_StrokeVertexIterator *)py_sv_it)->sv_it;
444   ((BPy_StrokeVertexIterator *)py_sv_it)->at_start = true;
445   ((BPy_StrokeVertexIterator *)py_sv_it)->reversed = reversed;
446   return py_sv_it;
447 }
448 
BPy_SVertexIterator_from_SVertexIterator(ViewEdgeInternal::SVertexIterator & sv_it)449 PyObject *BPy_SVertexIterator_from_SVertexIterator(ViewEdgeInternal::SVertexIterator &sv_it)
450 {
451   PyObject *py_sv_it = SVertexIterator_Type.tp_new(&SVertexIterator_Type, 0, 0);
452   ((BPy_SVertexIterator *)py_sv_it)->sv_it = new ViewEdgeInternal::SVertexIterator(sv_it);
453   ((BPy_SVertexIterator *)py_sv_it)->py_it.it = ((BPy_SVertexIterator *)py_sv_it)->sv_it;
454   return py_sv_it;
455 }
456 
BPy_orientedViewEdgeIterator_from_orientedViewEdgeIterator(ViewVertexInternal::orientedViewEdgeIterator & ove_it,bool reversed)457 PyObject *BPy_orientedViewEdgeIterator_from_orientedViewEdgeIterator(
458     ViewVertexInternal::orientedViewEdgeIterator &ove_it, bool reversed)
459 {
460   PyObject *py_ove_it = orientedViewEdgeIterator_Type.tp_new(&orientedViewEdgeIterator_Type, 0, 0);
461   ((BPy_orientedViewEdgeIterator *)py_ove_it)->ove_it =
462       new ViewVertexInternal::orientedViewEdgeIterator(ove_it);
463   ((BPy_orientedViewEdgeIterator *)py_ove_it)->py_it.it =
464       ((BPy_orientedViewEdgeIterator *)py_ove_it)->ove_it;
465   ((BPy_orientedViewEdgeIterator *)py_ove_it)->at_start = true;
466   ((BPy_orientedViewEdgeIterator *)py_ove_it)->reversed = reversed;
467   return py_ove_it;
468 }
469 
BPy_ViewEdgeIterator_from_ViewEdgeIterator(ViewEdgeInternal::ViewEdgeIterator & ve_it)470 PyObject *BPy_ViewEdgeIterator_from_ViewEdgeIterator(ViewEdgeInternal::ViewEdgeIterator &ve_it)
471 {
472   PyObject *py_ve_it = ViewEdgeIterator_Type.tp_new(&ViewEdgeIterator_Type, 0, 0);
473   ((BPy_ViewEdgeIterator *)py_ve_it)->ve_it = new ViewEdgeInternal::ViewEdgeIterator(ve_it);
474   ((BPy_ViewEdgeIterator *)py_ve_it)->py_it.it = ((BPy_ViewEdgeIterator *)py_ve_it)->ve_it;
475   return py_ve_it;
476 }
477 
BPy_ChainingIterator_from_ChainingIterator(ChainingIterator & c_it)478 PyObject *BPy_ChainingIterator_from_ChainingIterator(ChainingIterator &c_it)
479 {
480   PyObject *py_c_it = ChainingIterator_Type.tp_new(&ChainingIterator_Type, 0, 0);
481   ((BPy_ChainingIterator *)py_c_it)->c_it = new ChainingIterator(c_it);
482   ((BPy_ChainingIterator *)py_c_it)->py_ve_it.py_it.it = ((BPy_ChainingIterator *)py_c_it)->c_it;
483   return py_c_it;
484 }
485 
BPy_ChainPredicateIterator_from_ChainPredicateIterator(ChainPredicateIterator & cp_it)486 PyObject *BPy_ChainPredicateIterator_from_ChainPredicateIterator(ChainPredicateIterator &cp_it)
487 {
488   PyObject *py_cp_it = ChainPredicateIterator_Type.tp_new(&ChainPredicateIterator_Type, 0, 0);
489   ((BPy_ChainPredicateIterator *)py_cp_it)->cp_it = new ChainPredicateIterator(cp_it);
490   ((BPy_ChainPredicateIterator *)py_cp_it)->py_c_it.py_ve_it.py_it.it =
491       ((BPy_ChainPredicateIterator *)py_cp_it)->cp_it;
492   return py_cp_it;
493 }
494 
BPy_ChainSilhouetteIterator_from_ChainSilhouetteIterator(ChainSilhouetteIterator & cs_it)495 PyObject *BPy_ChainSilhouetteIterator_from_ChainSilhouetteIterator(ChainSilhouetteIterator &cs_it)
496 {
497   PyObject *py_cs_it = ChainSilhouetteIterator_Type.tp_new(&ChainSilhouetteIterator_Type, 0, 0);
498   ((BPy_ChainSilhouetteIterator *)py_cs_it)->cs_it = new ChainSilhouetteIterator(cs_it);
499   ((BPy_ChainSilhouetteIterator *)py_cs_it)->py_c_it.py_ve_it.py_it.it =
500       ((BPy_ChainSilhouetteIterator *)py_cs_it)->cs_it;
501   return py_cs_it;
502 }
503 
504 //==============================
505 // Python => C++
506 //==============================
507 
bool_from_PyBool(PyObject * b)508 bool bool_from_PyBool(PyObject *b)
509 {
510   return PyObject_IsTrue(b) != 0;
511 }
512 
IntegrationType_from_BPy_IntegrationType(PyObject * obj)513 IntegrationType IntegrationType_from_BPy_IntegrationType(PyObject *obj)
514 {
515   return static_cast<IntegrationType>(PyLong_AsLong(obj));
516 }
517 
MediumType_from_BPy_MediumType(PyObject * obj)518 Stroke::MediumType MediumType_from_BPy_MediumType(PyObject *obj)
519 {
520   return static_cast<Stroke::MediumType>(PyLong_AsLong(obj));
521 }
522 
EdgeNature_from_BPy_Nature(PyObject * obj)523 Nature::EdgeNature EdgeNature_from_BPy_Nature(PyObject *obj)
524 {
525   return static_cast<Nature::EdgeNature>(PyLong_AsLong(obj));
526 }
527 
Vec2f_ptr_from_PyObject(PyObject * obj,Vec2f & vec)528 bool Vec2f_ptr_from_PyObject(PyObject *obj, Vec2f &vec)
529 {
530   if (Vec2f_ptr_from_Vector(obj, vec)) {
531     return true;
532   }
533   if (Vec2f_ptr_from_PyList(obj, vec)) {
534     return true;
535   }
536   if (Vec2f_ptr_from_PyTuple(obj, vec)) {
537     return true;
538   }
539   return false;
540 }
541 
Vec3f_ptr_from_PyObject(PyObject * obj,Vec3f & vec)542 bool Vec3f_ptr_from_PyObject(PyObject *obj, Vec3f &vec)
543 {
544   if (Vec3f_ptr_from_Vector(obj, vec)) {
545     return true;
546   }
547   if (Vec3f_ptr_from_Color(obj, vec)) {
548     return true;
549   }
550   if (Vec3f_ptr_from_PyList(obj, vec)) {
551     return true;
552   }
553   if (Vec3f_ptr_from_PyTuple(obj, vec)) {
554     return true;
555   }
556   return false;
557 }
558 
Vec3r_ptr_from_PyObject(PyObject * obj,Vec3r & vec)559 bool Vec3r_ptr_from_PyObject(PyObject *obj, Vec3r &vec)
560 {
561   if (Vec3r_ptr_from_Vector(obj, vec)) {
562     return true;
563   }
564   if (Vec3r_ptr_from_Color(obj, vec)) {
565     return true;
566   }
567   if (Vec3r_ptr_from_PyList(obj, vec)) {
568     return true;
569   }
570   if (Vec3r_ptr_from_PyTuple(obj, vec)) {
571     return true;
572   }
573   return false;
574 }
575 
Vec2f_ptr_from_Vector(PyObject * obj,Vec2f & vec)576 bool Vec2f_ptr_from_Vector(PyObject *obj, Vec2f &vec)
577 {
578   if (!VectorObject_Check(obj) || ((VectorObject *)obj)->size != 2) {
579     return false;
580   }
581   if (BaseMath_ReadCallback((BaseMathObject *)obj) == -1) {
582     return false;
583   }
584   vec[0] = ((VectorObject *)obj)->vec[0];
585   vec[1] = ((VectorObject *)obj)->vec[1];
586   return true;
587 }
588 
Vec3f_ptr_from_Vector(PyObject * obj,Vec3f & vec)589 bool Vec3f_ptr_from_Vector(PyObject *obj, Vec3f &vec)
590 {
591   if (!VectorObject_Check(obj) || ((VectorObject *)obj)->size != 3) {
592     return false;
593   }
594   if (BaseMath_ReadCallback((BaseMathObject *)obj) == -1) {
595     return false;
596   }
597   vec[0] = ((VectorObject *)obj)->vec[0];
598   vec[1] = ((VectorObject *)obj)->vec[1];
599   vec[2] = ((VectorObject *)obj)->vec[2];
600   return true;
601 }
602 
Vec3r_ptr_from_Vector(PyObject * obj,Vec3r & vec)603 bool Vec3r_ptr_from_Vector(PyObject *obj, Vec3r &vec)
604 {
605   if (!VectorObject_Check(obj) || ((VectorObject *)obj)->size != 3) {
606     return false;
607   }
608   if (BaseMath_ReadCallback((BaseMathObject *)obj) == -1) {
609     return false;
610   }
611   vec[0] = ((VectorObject *)obj)->vec[0];
612   vec[1] = ((VectorObject *)obj)->vec[1];
613   vec[2] = ((VectorObject *)obj)->vec[2];
614   return true;
615 }
616 
Vec3f_ptr_from_Color(PyObject * obj,Vec3f & vec)617 bool Vec3f_ptr_from_Color(PyObject *obj, Vec3f &vec)
618 {
619   if (!ColorObject_Check(obj)) {
620     return false;
621   }
622   if (BaseMath_ReadCallback((BaseMathObject *)obj) == -1) {
623     return false;
624   }
625   vec[0] = ((ColorObject *)obj)->col[0];
626   vec[1] = ((ColorObject *)obj)->col[1];
627   vec[2] = ((ColorObject *)obj)->col[2];
628   return true;
629 }
630 
Vec3r_ptr_from_Color(PyObject * obj,Vec3r & vec)631 bool Vec3r_ptr_from_Color(PyObject *obj, Vec3r &vec)
632 {
633   if (!ColorObject_Check(obj)) {
634     return false;
635   }
636   if (BaseMath_ReadCallback((BaseMathObject *)obj) == -1) {
637     return false;
638   }
639   vec[0] = ((ColorObject *)obj)->col[0];
640   vec[1] = ((ColorObject *)obj)->col[1];
641   vec[2] = ((ColorObject *)obj)->col[2];
642   return true;
643 }
644 
float_array_from_PyList(PyObject * obj,float * v,int n)645 static bool float_array_from_PyList(PyObject *obj, float *v, int n)
646 {
647   for (int i = 0; i < n; i++) {
648     v[i] = PyFloat_AsDouble(PyList_GET_ITEM(obj, i));
649     if (v[i] == -1.0f && PyErr_Occurred()) {
650       PyErr_SetString(PyExc_TypeError, "list elements must be a number");
651       return 0;
652     }
653   }
654   return 1;
655 }
656 
Vec2f_ptr_from_PyList(PyObject * obj,Vec2f & vec)657 bool Vec2f_ptr_from_PyList(PyObject *obj, Vec2f &vec)
658 {
659   float v[2];
660 
661   if (!PyList_Check(obj) || PyList_GET_SIZE(obj) != 2) {
662     return false;
663   }
664   if (!float_array_from_PyList(obj, v, 2)) {
665     return false;
666   }
667   vec[0] = v[0];
668   vec[1] = v[1];
669   return true;
670 }
671 
Vec3f_ptr_from_PyList(PyObject * obj,Vec3f & vec)672 bool Vec3f_ptr_from_PyList(PyObject *obj, Vec3f &vec)
673 {
674   float v[3];
675 
676   if (!PyList_Check(obj) || PyList_GET_SIZE(obj) != 3) {
677     return false;
678   }
679   if (!float_array_from_PyList(obj, v, 3)) {
680     return false;
681   }
682   vec[0] = v[0];
683   vec[1] = v[1];
684   vec[2] = v[2];
685   return true;
686 }
687 
Vec3r_ptr_from_PyList(PyObject * obj,Vec3r & vec)688 bool Vec3r_ptr_from_PyList(PyObject *obj, Vec3r &vec)
689 {
690   float v[3];
691 
692   if (!PyList_Check(obj) || PyList_GET_SIZE(obj) != 3) {
693     return false;
694   }
695   if (!float_array_from_PyList(obj, v, 3)) {
696     return false;
697   }
698   vec[0] = v[0];
699   vec[1] = v[1];
700   vec[2] = v[2];
701   return true;
702 }
703 
float_array_from_PyTuple(PyObject * obj,float * v,int n)704 static bool float_array_from_PyTuple(PyObject *obj, float *v, int n)
705 {
706   for (int i = 0; i < n; i++) {
707     v[i] = PyFloat_AsDouble(PyTuple_GET_ITEM(obj, i));
708     if (v[i] == -1.0f && PyErr_Occurred()) {
709       PyErr_SetString(PyExc_TypeError, "tuple elements must be a number");
710       return 0;
711     }
712   }
713   return 1;
714 }
715 
Vec2f_ptr_from_PyTuple(PyObject * obj,Vec2f & vec)716 bool Vec2f_ptr_from_PyTuple(PyObject *obj, Vec2f &vec)
717 {
718   float v[2];
719 
720   if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2) {
721     return false;
722   }
723   if (!float_array_from_PyTuple(obj, v, 2)) {
724     return false;
725   }
726   vec[0] = v[0];
727   vec[1] = v[1];
728   return true;
729 }
730 
Vec3f_ptr_from_PyTuple(PyObject * obj,Vec3f & vec)731 bool Vec3f_ptr_from_PyTuple(PyObject *obj, Vec3f &vec)
732 {
733   float v[3];
734 
735   if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 3) {
736     return false;
737   }
738   if (!float_array_from_PyTuple(obj, v, 3)) {
739     return false;
740   }
741   vec[0] = v[0];
742   vec[1] = v[1];
743   vec[2] = v[2];
744   return true;
745 }
746 
Vec3r_ptr_from_PyTuple(PyObject * obj,Vec3r & vec)747 bool Vec3r_ptr_from_PyTuple(PyObject *obj, Vec3r &vec)
748 {
749   float v[3];
750 
751   if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 3) {
752     return false;
753   }
754   if (!float_array_from_PyTuple(obj, v, 3)) {
755     return false;
756   }
757   vec[0] = v[0];
758   vec[1] = v[1];
759   vec[2] = v[2];
760   return true;
761 }
762 
763 // helpers for argument parsing
764 
float_array_from_PyObject(PyObject * obj,float * v,int n)765 bool float_array_from_PyObject(PyObject *obj, float *v, int n)
766 {
767   if (VectorObject_Check(obj) && ((VectorObject *)obj)->size == n) {
768     if (BaseMath_ReadCallback((BaseMathObject *)obj) == -1) {
769       return 0;
770     }
771     for (int i = 0; i < n; i++) {
772       v[i] = ((VectorObject *)obj)->vec[i];
773     }
774     return 1;
775   }
776   if (ColorObject_Check(obj) && n == 3) {
777     if (BaseMath_ReadCallback((BaseMathObject *)obj) == -1) {
778       return 0;
779     }
780     for (int i = 0; i < n; i++) {
781       v[i] = ((ColorObject *)obj)->col[i];
782     }
783     return 1;
784   }
785   if (PyList_Check(obj) && PyList_GET_SIZE(obj) == n) {
786     return float_array_from_PyList(obj, v, n);
787   }
788   if (PyTuple_Check(obj) && PyTuple_GET_SIZE(obj) == n) {
789     return float_array_from_PyTuple(obj, v, n);
790   }
791   return 0;
792 }
793 
convert_v4(PyObject * obj,void * v)794 int convert_v4(PyObject *obj, void *v)
795 {
796   return mathutils_array_parse((float *)v, 4, 4, obj, "Error parsing 4D vector");
797 }
798 
convert_v3(PyObject * obj,void * v)799 int convert_v3(PyObject *obj, void *v)
800 {
801   return mathutils_array_parse((float *)v, 3, 3, obj, "Error parsing 3D vector");
802 }
803 
convert_v2(PyObject * obj,void * v)804 int convert_v2(PyObject *obj, void *v)
805 {
806   return mathutils_array_parse((float *)v, 2, 2, obj, "Error parsing 2D vector");
807 }
808 
809 ///////////////////////////////////////////////////////////////////////////////////////////
810 
811 #ifdef __cplusplus
812 }
813 #endif
814