nodePathCollection_ext.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Filename: nodePathCollection_ext.cxx
  2. // Created by: rdb (09Dec13)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #include "nodePathCollection_ext.h"
  15. #ifdef HAVE_PYTHON
  16. #ifndef CPPPARSER
  17. extern struct Dtool_PyTypedObject Dtool_NodePath;
  18. #ifdef STDFLOAT_DOUBLE
  19. extern struct Dtool_PyTypedObject Dtool_LPoint3d;
  20. #else
  21. extern struct Dtool_PyTypedObject Dtool_LPoint3f;
  22. #endif
  23. #endif
  24. ////////////////////////////////////////////////////////////////////
  25. // Function: NodePathCollection::__init__
  26. // Access: Published
  27. // Description: This special constructor accepts a Python list of
  28. // NodePaths. Since this constructor accepts a generic
  29. // PyObject *, it should be the last constructor listed
  30. // in the class record.
  31. ////////////////////////////////////////////////////////////////////
  32. void Extension<NodePathCollection>::
  33. __init__(PyObject *self, PyObject *sequence) {
  34. PyObject *fast = PySequence_Fast(sequence, "NodePathCollection constructor requires a sequence");
  35. if (fast == NULL) {
  36. return;
  37. }
  38. Py_ssize_t size = PySequence_Fast_GET_SIZE(fast);
  39. _this->reserve(size);
  40. for (int i = 0; i < size; ++i) {
  41. PyObject *item = PySequence_Fast_GET_ITEM(fast, i);
  42. if (item == NULL) {
  43. return;
  44. }
  45. NodePath *path;
  46. DTOOL_Call_ExtractThisPointerForType(item, &Dtool_NodePath, (void **)&path);
  47. if (path == (NodePath *)NULL) {
  48. // Unable to add item--probably it wasn't of the appropriate type.
  49. ostringstream stream;
  50. stream << "Element " << i << " in sequence passed to NodePathCollection constructor is not a NodePath";
  51. string str = stream.str();
  52. PyErr_SetString(PyExc_TypeError, str.c_str());
  53. Py_DECREF(fast);
  54. return;
  55. } else {
  56. _this->add_path(*path);
  57. }
  58. }
  59. Py_DECREF(fast);
  60. }
  61. ////////////////////////////////////////////////////////////////////
  62. // Function: NodePathCollection::__reduce__
  63. // Access: Published
  64. // Description: This special Python method is implement to provide
  65. // support for the pickle module.
  66. ////////////////////////////////////////////////////////////////////
  67. PyObject *Extension<NodePathCollection>::
  68. __reduce__(PyObject *self) const {
  69. // Here we will return a 4-tuple: (Class, (args), None, iterator),
  70. // where iterator is an iterator that will yield successive
  71. // NodePaths.
  72. // We should return at least a 2-tuple, (Class, (args)): the
  73. // necessary class object whose constructor we should call
  74. // (e.g. this), and the arguments necessary to reconstruct this
  75. // object.
  76. PyObject *this_class = PyObject_Type(self);
  77. if (this_class == NULL) {
  78. return NULL;
  79. }
  80. // Since a NodePathCollection is itself an iterator, we can simply
  81. // pass it as the fourth tuple component.
  82. PyObject *result = Py_BuildValue("(O()OO)", this_class, Py_None, self);
  83. Py_DECREF(this_class);
  84. return result;
  85. }
  86. ////////////////////////////////////////////////////////////////////
  87. // Function: Extension<NodePathCollection>::get_tight_bounds
  88. // Access: Published
  89. // Description: Returns the tight bounds as a 2-tuple of LPoint3
  90. // objects. This is a convenience function for Python
  91. // users, among which the use of calc_tight_bounds
  92. // may be confusing.
  93. // Returns None if calc_tight_bounds returned false.
  94. ////////////////////////////////////////////////////////////////////
  95. PyObject *Extension<NodePathCollection>::
  96. get_tight_bounds() const {
  97. LPoint3 *min_point = new LPoint3;
  98. LPoint3 *max_point = new LPoint3;
  99. if (_this->calc_tight_bounds(*min_point, *max_point)) {
  100. #ifdef STDFLOAT_DOUBLE
  101. PyObject *min_inst = DTool_CreatePyInstance((void*) min_point, Dtool_LPoint3d, true, false);
  102. PyObject *max_inst = DTool_CreatePyInstance((void*) max_point, Dtool_LPoint3d, true, false);
  103. #else
  104. PyObject *min_inst = DTool_CreatePyInstance((void*) min_point, Dtool_LPoint3f, true, false);
  105. PyObject *max_inst = DTool_CreatePyInstance((void*) max_point, Dtool_LPoint3f, true, false);
  106. #endif
  107. return Py_BuildValue("NN", min_inst, max_inst);
  108. } else {
  109. Py_INCREF(Py_None);
  110. return Py_None;
  111. }
  112. }
  113. #endif // HAVE_PYTHON