Library of Assembled Shared Sources
export_traits_prim.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2025 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44#ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H
45#define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H
46
47#include "python_common.h"
48#include "pyobject_plus.h"
49#include "py_tuple.h"
50
51namespace lass
52{
53namespace python
54{
55namespace impl
56{
57
58template <typename ObjectType, typename ExportTraits, size_t dimension = ObjectType::dimension>
59struct PyExportTraitsVectorPoint
60{
61 static PyObject* build(const ObjectType& v)
62 {
63 PyObject* const tuple = PyTuple_New(dimension);
64 for (size_t i = 0; i < dimension; ++i)
65 {
66 PyTuple_SetItem(tuple, static_cast<Py_ssize_t>(i), pyBuildSimpleObject(v[i]));
67 }
68 return tuple;
69 }
70
71 static int get(PyObject* obj, ObjectType& v)
72 {
73 const TPyObjPtr tuple = impl::checkedFastSequence(obj, dimension);
74 if (!tuple)
75 {
76 impl::addMessageHeader(ExportTraits::className());
77 return 1;
78 }
79 PyObject** objects = PySequence_Fast_ITEMS(tuple.get());
80 ObjectType result;
81 for (size_t k = 0; k < dimension; ++k)
82 {
83 if (!impl::decodeObject(objects[k], static_cast<Py_ssize_t>(k), result[k]))
84 {
85 impl::addMessageHeader(ExportTraits::className());
86 return 1;
87 }
88 }
89 v = result;
90 return 0;
91 }
92};
93
94template < typename AabbType, typename ExportTraits >
95struct PyExportTraitsPrimAabb
96{
97 typedef AabbType TAabb;
98 static PyObject* build(const TAabb& aabb)
99 {
100 return fromSharedPtrToNakedCast(makeTuple(aabb.min(), aabb.max()));
101 }
102 static int get(PyObject* obj, TAabb& aabb)
103 {
104 typename TAabb::TPoint min, max;
105 if (decodeTuple(obj, min, max) != 0)
106 {
107 impl::addMessageHeader(ExportTraits::className());
108 return 1;
109 }
110 try
111 {
112 aabb = TAabb(min, max);
113 }
114 catch (util::Exception& error)
115 {
116 std::ostringstream buffer;
117 buffer << ExportTraits::className() << ": " << error.message();
118 PyErr_SetString(PyExc_ValueError, buffer.str().c_str());
119 return 1;
120 }
121 return 0;
122 }
123};
124
125template < typename LineSegmentType, typename ExportTraits >
126struct PyExportTraitsPrimLineSegment
127{
128 typedef LineSegmentType TLineSegment;
129 static PyObject* build(const TLineSegment& seg)
130 {
131 return fromSharedPtrToNakedCast(makeTuple(seg.tail(), seg.head()));
132 }
133 static int get(PyObject* obj, TLineSegment& seg)
134 {
135 typename TLineSegment::TPoint tail, head;
136 if (decodeTuple(obj, tail, head) != 0)
137 {
138 impl::addMessageHeader(ExportTraits::className());
139 return 1;
140 }
141 seg = TLineSegment(tail, head);
142 return 0;
143 }
144};
145
146template <typename TransformationType, typename ExporTraits>
147struct PyExportTraitsPrimTransformation
148{
149 typedef TransformationType TTransformation;
150 typedef typename TransformationType::TValue TValue;
151 enum { size = TransformationType::dimension + 1 };
152
153 static PyObject* build(const TTransformation& transfo)
154 {
155 const TValue* const values = transfo.matrix();
156 PyObject* const matrix = PyTuple_New(size);
157 for (int i = 0; i < size; ++i)
158 {
159 PyObject* const row = PyTuple_New(size);
160 for (int j = 0; j < size; ++j)
161 {
162 PyTuple_SetItem(row, j, pyBuildSimpleObject(values[i * size + j]));
163 }
164 PyTuple_SetItem(matrix, i, row);
165 }
166 return matrix;
167 }
168
169 static int get(PyObject* obj, TTransformation& transfo)
170 {
171 TPyObjPtr tuple = impl::checkedFastSequence(obj, size);
172 if (!tuple)
173 {
174 impl::addMessageHeader(ExporTraits::className());
175 return 1;
176 }
177 PyObject** rows = PySequence_Fast_ITEMS(tuple.get());
178 TValue values[size * size];
179 for (int i = 0; i < size; ++i)
180 {
181 TPyObjPtr row = impl::checkedFastSequence(rows[i], size);
182 if (!row)
183 {
184 std::ostringstream buffer;
185 buffer << ExporTraits::className() << ": row " << i;
186 impl::addMessageHeader(buffer.str().c_str());
187 return 1;
188 }
189 PyObject** objects = PySequence_Fast_ITEMS(row.get());
190 for (int j = 0; j < size; ++j)
191 {
192 if (pyGetSimpleObject(objects[j], values[size * i + j]) != 0)
193 {
194 std::ostringstream buffer;
195 buffer << ExporTraits::className() << ": row " << i << ", column " << j;
196 impl::addMessageHeader(buffer.str().c_str());
197 return 1;
198 }
199 }
200 }
201 transfo = TTransformation(values, values + size * size);
202 return 0;
203 }
204};
205
206template <typename PolygonType, typename ExportTraits>
207struct PyExportTraitsPrimSimplePolygon
208{
209 typedef PolygonType TPolygon;
210 typedef typename PolygonType::TPoint TPoint;
211 static PyObject* build(const TPolygon& poly)
212 {
213 std::vector<TPoint> points;
214 for (unsigned i = 0; i < poly.size(); ++i)
215 points.push_back(poly[i]);
216 return pyBuildSimpleObject( points );
217 }
218 static int get(PyObject* obj, TPolygon& poly)
219 {
220 std::vector<TPoint> points;
221 if (pyGetSimpleObject(obj, points) != 0)
222 {
223 impl::addMessageHeader(ExportTraits::className());
224 return 1;
225 }
226 poly = TPolygon(points.begin(), points.end());
227 return 0;
228 }
229};
230
231template <typename AxisType, typename ExportTraits>
232struct PyExportTraitsPrimAxis
233{
234 typedef AxisType TAxis;
235 static PyObject* build(const TAxis& axis)
236 {
237 return pyBuildSimpleObject(std::string(1, axis.axis()));
238 }
239 static int get(PyObject* obj, TAxis& axis)
240 {
241 std::string s;
242 if (pyGetSimpleObject(obj, s) != 0)
243 {
244 impl::addMessageHeader(ExportTraits::className());
245 return 1;
246 }
247 try
248 {
249 axis = TAxis(s);
250 }
251 catch (util::Exception& error)
252 {
253 std::ostringstream buffer;
254 buffer << ExportTraits::className() << ": " << error.message();
255 PyErr_SetString(PyExc_ValueError, buffer.str().c_str());
256 return 1;
257 }
258 return 0;
259 }
260};
261
262LASS_PYTHON_DLL PyObject* LASS_CALL buildIndexVertex(size_t vertex, size_t normal, size_t uv);
263LASS_PYTHON_DLL int LASS_CALL getIndexVertex(PyObject* iIndices, size_t& vertex, size_t& normal, size_t& uv);
264
265}
266}
267}
268
269#endif
270
271namespace lass
272{
273namespace python
274{
275
276// --- vectors -------------------------------------------------------------------------------------
277
278#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_VECTOR_2D)
279# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_VECTOR_2D
280# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_VECTOR_2D
281
282/** Maps prim::Vector2D<T> to a Python tuple of two elements of type T.
283 * @ingroup PyExportTraits
284 */
285template <typename T>
286struct PyExportTraits< prim::Vector2D<T> >:
287 public impl::PyExportTraitsVectorPoint< prim::Vector2D<T>, PyExportTraits< prim::Vector2D<T> > >
288{
289 constexpr static const char* py_typing = "_Vector2D[T]";
290 constexpr static const char* py_typing_preamble = "type _Vector2D[T] = tuple[T, T]";
291
292 static const char* className() { return "Vector2D"; }
293};
294
295# endif
296#endif
297
298
299
300#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_VECTOR_3D)
301# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_VECTOR_3D
302# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_VECTOR_3D
303
304/** Maps prim::Vector3D<T> to a Python tuple of three elements of type T.
305 * @ingroup PyExportTraits
306 */
307template <typename T>
308struct PyExportTraits< prim::Vector3D<T> >:
309 public impl::PyExportTraitsVectorPoint< prim::Vector3D<T>, PyExportTraits< prim::Vector3D<T> > >
310{
311 constexpr static const char* py_typing = "_Vector3D[T]";
312 constexpr static const char* py_typing_preamble = "type _Vector3D[T] = tuple[T, T, T]";
313
314 static const char* className() { return "Vector3D"; }
315};
316
317# endif
318#endif
319
320
321#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_VECTOR_4D)
322# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_VECTOR_4D
323# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_VECTOR_4D
324
325/** Maps prim::Vector4D<T> to a Python tuple of four elements of type T.
326 * @ingroup PyExportTraits
327 */
328template <typename T>
329struct PyExportTraits< prim::Vector4D<T> >:
330 public impl::PyExportTraitsVectorPoint< prim::Vector4D<T>, PyExportTraits< prim::Vector4D<T> > >
331{
332 constexpr static const char* py_typing = "_Vector4D[T]";
333 constexpr static const char* py_typing_preamble = "type _Vector4D[T] = tuple[T, T, T, T]";
334
335 static const char* className() { return "Vector4D"; }
336};
337
338# endif
339#endif
340
341
342
343// --- points --------------------------------------------------------------------------------------
344
345#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_POINT_2D)
346# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_POINT_2D
347# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_POINT_2D
348
349/** Maps prim::Point2D<T> to a Python tuple of two elements of type T.
350 * @ingroup PyExportTraits
351 */
352template <typename T>
353struct PyExportTraits< prim::Point2D<T> >:
354 public impl::PyExportTraitsVectorPoint< prim::Point2D<T>, PyExportTraits< prim::Point2D<T> > >
355{
356 constexpr static const char* py_typing = "_Point2D[T]";
357 constexpr static const char* py_typing_preamble = "type _Point2D[T] = tuple[T, T]";
358
359 static const char* className() { return "Point2D"; }
360};
361
362# endif
363#endif
364
365
366
367#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_POINT_3D)
368# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_POINT_3D
369# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_POINT_3D
370
371/** Maps prim::Point3D<T> to a Python tuple of three elements of type T.
372 * @ingroup PyExportTraits
373 */
374template <typename T>
375struct PyExportTraits< prim::Point3D<T> >:
376 public impl::PyExportTraitsVectorPoint< prim::Point3D<T>, PyExportTraits< prim::Point3D<T> > >
377{
378 constexpr static const char* py_typing = "_Point3D[T]";
379 constexpr static const char* py_typing_preamble = "type _Point3D[T] = tuple[T, T, T]";
380
381 static const char* className() { return "Point3D"; }
382};
383
384# endif
385#endif
386
387
388
389// --- boxes ---------------------------------------------------------------------------------------
390
391#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_AABB_2D)
392# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_AABB_2D
393# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_AABB_2D
394
395/** Maps prim::Aabb2D<T> to a Python tuple of two tuples of two elements of type T.
396 * @ingroup PyExportTraits
397 */
398template <typename T, typename MMP>
399struct PyExportTraits< prim::Aabb2D<T, MMP> >:
400 public impl::PyExportTraitsPrimAabb< prim::Aabb2D<T, MMP>, PyExportTraits< prim::Aabb2D<T, MMP> > >
401{
402 constexpr static const char* py_typing = "_Aabb2D[T]";
403 constexpr static const char* py_typing_preamble =
404 "type _Point2D[T] = tuple[T, T]\n"
405 "type _Aabb2D[T] = tuple[_Point2D[T], _Point2D[T]]";
406
407 static const char* className() { return "Aabb2D"; }
408};
409
410# endif
411#endif
412
413#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_AABB_3D)
414# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_AABB_3D
415# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_AABB_3D
416
417/** Maps prim::Aabb3D<T> to a Python tuple of two tuples of three elements of type T.
418 * @ingroup PyExportTraits
419 */
420template <typename T, typename MMP>
421struct PyExportTraits< prim::Aabb3D<T, MMP> >:
422 public impl::PyExportTraitsPrimAabb< prim::Aabb3D<T, MMP>, PyExportTraits< prim::Aabb3D<T, MMP> > >
423{
424 constexpr static const char* py_typing = "_Aabb3D[T]";
425 constexpr static const char* py_typing_preamble =
426 "type _Point3D[T] = tuple[T, T, T]\n"
427 "type _Aabb3D[T] = tuple[_Point3D[T], _Point3D[T]]";
428
429 static const char* className() { return "Aabb3D"; }
430};
431
432# endif
433#endif
434
435
436
437// --- line segments -------------------------------------------------------------------------------
438
439#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_LINE_SEGMENT_2D)
440# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_LINE_SEGMENT_2D
441# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_LINE_SEGMENT_2D
442
443/** Maps prim::LineSegment2D<T> to a Python tuple of two tuples of two elements of type T.
444 * @ingroup PyExportTraits
445 */
446template <typename T, typename PP>
447struct PyExportTraits< prim::LineSegment2D<T, PP> >:
448 public impl::PyExportTraitsPrimLineSegment< prim::LineSegment2D<T, PP>, PyExportTraits< prim::LineSegment2D<T, PP> > >
449{
450 constexpr static const char* py_typing = "_LineSegment2D[T]";
451 constexpr static const char* py_typing_preamble =
452 "type _Point2D[T] = tuple[T, T]\n"
453 "type _LineSegment2D[T] = tuple[_Point2D[T], _Point2D[T]]";
454
455 static const char* className() { return "LineSegment2D"; }
456};
457
458# endif
459#endif
460
461#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_LINE_SEGMENT_3D)
462# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_LINE_SEGMENT_3D
463# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_LINE_SEGMENT_3D
464
465/** Maps prim::LineSegment3D<T> to a Python tuple of two tuples of three elements of type T.
466 * @ingroup PyExportTraits
467 */
468template <typename T, typename PP>
469struct PyExportTraits< prim::LineSegment3D<T, PP> >:
470 public impl::PyExportTraitsPrimLineSegment< prim::LineSegment3D<T, PP>, PyExportTraits< prim::LineSegment3D<T, PP> > >
471{
472 constexpr static const char* py_typing = "_LineSegment3D[T]";
473 constexpr static const char* py_typing_preamble =
474 "type _Point3D[T] = tuple[T, T, T]\n"
475 "type _LineSegment3D[T] = tuple[_Point3D[T], _Point3D[T]]";
476
477 static const char* className() { return "LineSegment3D"; }
478};
479
480# endif
481#endif
482
483
484
485// --- transformations -----------------------------------------------------------------------------
486
487#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_TRANSFORMATION_2D)
488# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_TRANSFORMATION_2D
489# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_TRANSFORMATION_2D
490
491/** Maps prim::Transformation2D<T> to a Python tuple of three tuples of three elements of type T.
492 * @ingroup PyExportTraits
493 */
494template <typename T>
495struct PyExportTraits< prim::Transformation2D<T> >:
496 public impl::PyExportTraitsPrimTransformation< prim::Transformation2D<T>, PyExportTraits< prim::Transformation2D<T> > >
497{
498 constexpr static const char* py_typing = "_Transformation2D[T]";
499 constexpr static const char* py_typing_preamble = "type _Transformation2D[T] = tuple[tuple[T, T, T], tuple[T, T, T], tuple[T, T, T]]";
500
501 static const char* className() { return "Transformation2D"; }
502};
503
504# endif
505#endif
506
507#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_TRANSFORMATION_3D)
508# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_TRANSFORMATION_3D
509# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_TRANSFORMATION_3D
510
511/** Maps prim::Transformation3D<T> to a Python tuple of four tuples of four elements of type T.
512 * @ingroup PyExportTraits
513 */
514template <typename T>
515struct PyExportTraits< prim::Transformation3D<T> >:
516 public impl::PyExportTraitsPrimTransformation< prim::Transformation3D<T>, PyExportTraits< prim::Transformation3D<T> > >
517{
518 constexpr static const char* py_typing = "_Transformation3D[T]";
519 constexpr static const char* py_typing_preamble = "type _Transformation3D[T] = tuple[tuple[T, T, T, T], tuple[T, T, T, T], tuple[T, T, T, T], tuple[T, T, T, T]]";
520
521 static const char* className() { return "Transformation3D"; }
522};
523
524# endif
525#endif
526
527
528
529// --- simplepolygons -----------------------------------------------------------------------------
530
531#ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_SIMPLE_POLYGON
532#define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_SIMPLE_POLYGON
533
534
535#endif
536
537#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_SIMPLE_POLYGON_2D)
538# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_SIMPLE_POLYGON_2D
539# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_SIMPLE_POLYGON_2D
540
541/** Maps prim::SimplePolygon2D<T> to a Python sequence of tuples of two elements of type T.
542 * @ingroup PyExportTraits
543 */
544template <typename T, typename DP>
545struct PyExportTraits< prim::SimplePolygon2D<T, DP> >:
546 public impl::PyExportTraitsPrimSimplePolygon< prim::SimplePolygon2D<T, DP>, PyExportTraits< prim::SimplePolygon2D<T, DP> > >
547{
548 constexpr static const char* py_typing = "_SimplePolygon2D[T]";
549 constexpr static const char* py_typing_preamble =
550 "type _Point2D[T] = tuple[T, T]\n"
551 "type _SimplePolygon2D[T] = Sequence[_Point2D[T]]";
552
553 static const char* className() { return "SimplePolygon2D"; }
554};
555
556# endif
557#endif
558
559
560#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_SIMPLE_POLYGON_3D)
561# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_SIMPLE_POLYGON_3D
562# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_SIMPLE_POLYGON_3D
563
564/** Maps prim::SimplePolygon3D<T> to a Python sequence of tuples of three elements of type T.
565 * @ingroup PyExportTraits
566 */
567template <typename T, typename DP>
568struct PyExportTraits< prim::SimplePolygon3D<T, DP> >:
569 public impl::PyExportTraitsPrimSimplePolygon< prim::SimplePolygon3D<T, DP>, PyExportTraits< prim::SimplePolygon3D<T, DP> > >
570{
571 constexpr static const char* py_typing = "_SimplePolygon3D[T]";
572 constexpr static const char* py_typing_preamble =
573 "type _Point3D[T] = tuple[T, T, T]\n"
574 "type _SimplePolygon3D[T] = Sequence[_Point3D[T]]";
575
576 static const char* className() { return "SimplePolygon3D"; }
577};
578
579# endif
580#endif
581
582// --- axes ----------------------------------------------------------------------------------------
583
584#ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_AXIS
585#define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_AXIS
586
587
588#endif
589
590#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_XY)
591# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_XY
592# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_XY
593
594/** Maps prim::XY to a Python string literal "x" or "y".
595 * @ingroup PyExportTraits
596 */
597template <>
599 public impl::PyExportTraitsPrimAxis<prim::XY, PyExportTraits<prim::XY> >
600{
601 constexpr static const char* py_typing = "Literal['x', 'X', 'y', 'Y']";
602 static const char* className() { return "XY"; }
603};
604
605# endif
606#endif
607
608#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_XYZ)
609# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_XYZ
610# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_XYZ
611
612/** Maps prim::XYZ to a Python string literal "x", "y" or "z".
613 * @ingroup PyExportTraits
614 */
615template <>
616struct PyExportTraits<prim::XYZ>:
617 public impl::PyExportTraitsPrimAxis<prim::XYZ, PyExportTraits<prim::XYZ> >
618{
619 constexpr static const char* py_typing = "Literal['x', 'X', 'y', 'Y', 'z', 'Z']";
620 static const char* className() { return "XYZ"; }
621};
622
623# endif
624#endif
625
626#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_XYZW)
627# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_XYZW
628# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_XYZW
629
630/** Maps prim::XYZW to a Python string literal "x", "y", "z" or "w".
631 * @ingroup PyExportTraits
632 */
633template <>
634struct PyExportTraits<prim::XYZW>:
635 public impl::PyExportTraitsPrimAxis<prim::XYZW, PyExportTraits<prim::XYZW> >
636{
637 constexpr static const char* py_typing = "Literal['x', 'X', 'y', 'Y', 'z', 'Z', 'w', 'W']";
638 static const char* className() { return "XYZW"; }
639};
640
641# endif
642#endif
643
644// --- ColorRGBA -----------------------------------------------------------------------------------
645
646#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_COLOR_RGBA)
647# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_COLOR_RGBA
648# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_COLOR_RGBA
649
650/** Maps prim::ColorRGBA to a Python tuple of four elements of type float.
651 * @ingroup PyExportTraits
652 */
653template <>
654struct PyExportTraits<prim::ColorRGBA>
655{
656 constexpr static const char* py_typing = "_RGBA";
657 constexpr static const char* py_typing_param = "_RGBA | _RGB";
658 constexpr static const char* py_typing_preamble =
659 "type _RGBA = tuple[float, float, float, float]\n"
660 "type _RGB = tuple[float, float, float]";
661
662 static PyObject* build(const prim::ColorRGBA& v)
663 {
664 return pyBuildSimpleObject(v.vector());
665 }
666 static int get(PyObject* obj, prim::ColorRGBA& v)
667 {
668 prim::ColorRGBA x(0, 0, 0, 1);
669 if (decodeTupleMinimum(obj, 3, x.r, x.g, x.b, x.a))
670 {
671 impl::addMessageHeader("ColorRGBA");
672 return 1;
673 }
674 v = x;
675 return 0;
676 }
677};
678
679# endif
680#endif
681
682// --- IndexTriangle -------------------------------------------------------------------------------
683
684#if defined(LASS_PRIM_HAVE_PY_EXPORT_TRAITS_TRIANGLE_MESH_3D)
685# ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_INDEX_TRIANGLE
686# define LASS_GUARDIAN_OF_INCLUSION_PYTHON_EXPORT_TRAITS_PRIM_H_INDEX_TRIANGLE
687
688namespace impl
689{
690}
691
692/** Maps prim::IndexTriangle to a Python tuple of three index vertices.
693 *
694 * An index vertex is a tuple of one, two or three size_t values:
695 * - (vertex,)
696 * - (vertex, normal)
697 * - (vertex, normal, uv)
698 * where 'vertex', 'normal' and 'uv' are indices in the corresponding arrays.
699 * The indices are zero-based.
700 *
701 * The 'normal' and 'uv' indices can be None to indicate that no normal or uv is specified.
702 * The vertex index is mandatory.
703 *
704 * @ingroup PyExportTraits
705 */
706template <>
707struct PyExportTraits<prim::IndexTriangle>
708{
709 constexpr static const char* py_typing = "_IndexTriangle";
710 constexpr static const char* py_typing_preamble =
711 "type _IndexVertex = tuple[int] | tuple[int, int | None] | tuple[int, int | None, int | None]\n"
712 "type _IndexTriangle = tuple[_IndexVertex, _IndexVertex, _IndexVertex]";
713
714 static PyObject* build(const prim::IndexTriangle& iTriangle)
715 {
716 TPyObjPtr triangle(PyTuple_New(3));
717 if (!triangle) return 0;
718 for (int k = 0; k < 3; ++k)
719 {
720 PyObject* vertex = impl::buildIndexVertex(
721 iTriangle.vertices[k], iTriangle.normals[k], iTriangle.uvs[k]);
722 if (!vertex) return 0;
723 if (PyTuple_SetItem(triangle.get(), k, vertex) != 0) return 0;
724 }
725 return fromSharedPtrToNakedCast(triangle);
726 }
727 static int get(PyObject* obj, prim::IndexTriangle& oTriangle)
728 {
729 const TPyObjPtr tuple(impl::checkedFastSequence(obj, 3));
730 if (!tuple)
731 {
732 impl::addMessageHeader("IndexTriangle");
733 return 1;
734 }
735 PyObject** objects = PySequence_Fast_ITEMS(tuple.get());
736 prim::IndexTriangle result = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
737 for (int k = 0; k < 3; ++k)
738 {
739 if (impl::getIndexVertex(objects[k], result.vertices[k], result.normals[k], result.uvs[k]) != 0)
740 {
741 std::ostringstream buffer;
742 buffer << "IndexTriangle: " << (k + 1) << "th vertex";
743 impl::addMessageHeader(buffer.str().c_str());
744 return 1;
745 }
746 }
747 oTriangle = result;
748 return 0;
749 }
750};
751
752# endif
753#endif
754
755}
756}
757
758// EOF
an [0, 1] floating point RGB colour with Alpha channel.
Definition color_rgba.h:62
void addMessageHeader(const char *header)
Prepend a message to the current Python exception value.
PyObjectPtr< PyObject >::Type TPyObjPtr
PyObjectPtr to a PyObject.
PyObject * fromSharedPtrToNakedCast(const util::SharedPtr< T, PyObjectStorage, PyObjectCounter > &object)
fromSharedPtrToNakedCast.
set of geometrical primitives
Definition aabb_2d.h:81
Comprehensive C++ to Python binding library.
Library for Assembled Shared Sources.
Definition config.h:53
by copy, general case assumes shadow type or PyObjectPlus based type.