Przeglądaj źródła

add methods to directly access vertex and texture data from Python

David Rose 18 lat temu
rodzic
commit
18fab235c9

+ 6 - 0
panda/src/express/config_express.N

@@ -33,3 +33,9 @@ forcetype ConfigVariableString
 forcetype istream
 forcetype istream
 forcetype ostream
 forcetype ostream
 forcetype iostream
 forcetype iostream
+
+forcetype PointerToArray<unsigned char>
+forcetype ConstPointerToArray<unsigned char>
+
+renametype PointerToArray<unsigned char> PTAUchar
+renametype ConstPointerToArray<unsigned char> CPTAUchar

+ 183 - 61
panda/src/express/pointerToArray.I

@@ -26,7 +26,7 @@ pvector<Element> ConstPointerToArray<Element>::_empty_array;
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: PointerToArray::Constructor
 //     Function: PointerToArray::Constructor
-//       Access: Published
+//       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 template<class Element>
 template<class Element>
@@ -39,7 +39,7 @@ PointerToArray(TypeHandle type_handle) :
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: PointerToArray::empty_array
 //     Function: PointerToArray::empty_array
-//       Access: Published, Static
+//       Access: Public, Static
 //  Description: Return an empty array of size n
 //  Description: Return an empty array of size n
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 template<class Element>
 template<class Element>
@@ -55,7 +55,7 @@ PointerToArray<Element>::empty_array(size_type n, TypeHandle type_handle) {
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: PointerToArray::Constructor
 //     Function: PointerToArray::Constructor
-//       Access: Published
+//       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 template<class Element>
 template<class Element>
@@ -70,7 +70,7 @@ PointerToArray(size_type n, const Element &value, TypeHandle type_handle) :
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: PointerToArray::Copy Constructor
 //     Function: PointerToArray::Copy Constructor
-//       Access: Published
+//       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 template<class Element>
 template<class Element>
@@ -312,7 +312,7 @@ erase(iterator first, iterator last) {
 #if !defined(WIN32_VC)
 #if !defined(WIN32_VC)
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: PointerToArray::Indexing operator
 //     Function: PointerToArray::Indexing operator
-//       Access: Published
+//       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 template<class Element>
 template<class Element>
@@ -330,7 +330,7 @@ operator [](size_type n) const {
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: PointerToArray::Indexing operator
 //     Function: PointerToArray::Indexing operator
-//       Access: Published
+//       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 template<class Element>
 template<class Element>
@@ -340,40 +340,9 @@ operator [](int n) const {
 }
 }
 #endif
 #endif
 
 
-////////////////////////////////////////////////////////////////////
-//     Function: PointerToArray::get_element
-//       Access: Published
-//  Description: This method exists mainly to access the elements of
-//               the array easily from a high-level language such as
-//               Python, especially on Windows, where the above index
-//               element accessor methods can't be defined because of
-//               a confusion with the pointer typecast operator.
-////////////////////////////////////////////////////////////////////
-template<class Element>
-INLINE const Element &PointerToArray<Element>::
-get_element(size_type n) const {
-  return (*this)[n];
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: PointerToArray::set_element
-//       Access: Published
-//  Description: This method exists mainly to access the elements of
-//               the array easily from a high-level language such as
-//               Python, especially on Windows, where the above index
-//               element accessor methods can't be defined because of
-//               a confusion with the pointer typecast operator.
-////////////////////////////////////////////////////////////////////
-template<class Element>
-INLINE void PointerToArray<Element>::
-set_element(size_type n, const Element &value) {
-  nassertv(n < ((To *)(this->_void_ptr))->size());
-  (*this)[n] = value;
-}
-
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: PointerToArray::push_back
 //     Function: PointerToArray::push_back
-//       Access: Published
+//       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 template<class Element>
 template<class Element>
@@ -387,7 +356,7 @@ push_back(const Element &x) {
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: PointerToArray::pop_back
 //     Function: PointerToArray::pop_back
-//       Access: Published
+//       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 template<class Element>
 template<class Element>
@@ -402,7 +371,7 @@ pop_back() {
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: PointerToArray::make_empty
 //     Function: PointerToArray::make_empty
-//       Access: Published
+//       Access: Public
 //  Description: Empties the array pointed to.  This is different from
 //  Description: Empties the array pointed to.  This is different from
 //               clear(), which reassigns the pointer to a NULL
 //               clear(), which reassigns the pointer to a NULL
 //               pointer.
 //               pointer.
@@ -462,6 +431,123 @@ v() const {
   return *((To *)(this->_void_ptr));
   return *((To *)(this->_void_ptr));
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: PointerToArray::get_element
+//       Access: Published
+//  Description: This method exists mainly to access the elements of
+//               the array easily from a high-level language such as
+//               Python, especially on Windows, where the above index
+//               element accessor methods can't be defined because of
+//               a confusion with the pointer typecast operator.
+////////////////////////////////////////////////////////////////////
+template<class Element>
+INLINE const Element &PointerToArray<Element>::
+get_element(size_type n) const {
+  return (*this)[n];
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PointerToArray::set_element
+//       Access: Published
+//  Description: This method exists mainly to access the elements of
+//               the array easily from a high-level language such as
+//               Python, especially on Windows, where the above index
+//               element accessor methods can't be defined because of
+//               a confusion with the pointer typecast operator.
+////////////////////////////////////////////////////////////////////
+template<class Element>
+INLINE void PointerToArray<Element>::
+set_element(size_type n, const Element &value) {
+  nassertv(n < ((To *)(this->_void_ptr))->size());
+  (*this)[n] = value;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PointerToArray::get_data
+//       Access: Published
+//  Description: This method exists mainly to access the data of
+//               the array easily from a high-level language such as
+//               Python.
+//
+//               It returns the entire contents of the vector as a
+//               block of raw data in a string.
+////////////////////////////////////////////////////////////////////
+template<class Element>
+INLINE string PointerToArray<Element>::
+get_data() const {
+  return get_subdata(0, size());
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PointerToArray::set_data
+//       Access: Published
+//  Description: This method exists mainly to access the data of
+//               the array easily from a high-level language such as
+//               Python.
+//
+//               It replaces the entire contents of the vector from a
+//               block of raw data in a string.
+////////////////////////////////////////////////////////////////////
+template<class Element>
+INLINE void PointerToArray<Element>::
+set_data(const string &data) {
+  set_subdata(0, size(), data);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PointerToArray::get_subdata
+//       Access: Published
+//  Description: This method exists mainly to access the data of
+//               the array easily from a high-level language such as
+//               Python.
+//
+//               It returns the contents of a portion of the
+//               vector--from element (n) through element (n + count -
+//               1)--as a block of raw data in a string.
+////////////////////////////////////////////////////////////////////
+template<class Element>
+INLINE string PointerToArray<Element>::
+get_subdata(size_type n, size_type count) const {
+  n = min(n, size());
+  count = max(count, n);
+  count = min(count, size() - n);
+  return string((const char *)(p() + n), sizeof(Element) * count);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PointerToArray::set_subdata
+//       Access: Published
+//  Description: This method exists mainly to access the data of
+//               the array easily from a high-level language such as
+//               Python.
+//
+//               It replaces the contents of a portion of the
+//               vector--from element (n) through element (n + count -
+//               1)--as a block of raw data in a string.  The length
+//               of the string must be an even multiple of Element
+//               size bytes.  The array may be expanded or truncated
+//               if the length of the string does not correspond to
+//               exactly count elements.
+////////////////////////////////////////////////////////////////////
+template<class Element>
+INLINE void PointerToArray<Element>::
+set_subdata(size_type n, size_type count, const string &data) {
+  nassertv((data.length() % sizeof(Element)) == 0);
+  size_type ncount = data.length() / sizeof(Element);
+  if (ncount < count) {
+    // Reduce the array.
+    erase(begin() + n + ncount, begin() + n + count);
+  } else if (count < ncount) {
+    // Expand the array.
+    insert(begin() + n + count, ncount - count, Element());
+  }
+
+  // Now boldly replace the data.  Hope there aren't any constructors
+  // or destructors involved here.  The user better know what she is
+  // doing.
+  memcpy(p() + n, data.data(), sizeof(Element) * ncount);
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: PointerToArray::get(this->_void_ptr)
 //     Function: PointerToArray::get(this->_void_ptr)
 //       Access: Public
 //       Access: Public
@@ -575,7 +661,7 @@ clear() {
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: ConstPointerToArray::Constructor
 //     Function: ConstPointerToArray::Constructor
-//       Access: Published
+//       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 template<class Element>
 template<class Element>
@@ -588,7 +674,7 @@ ConstPointerToArray(TypeHandle type_handle) :
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: ConstPointerToArray::Copy Constructor
 //     Function: ConstPointerToArray::Copy Constructor
-//       Access: Published
+//       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 template<class Element>
 template<class Element>
@@ -601,7 +687,7 @@ ConstPointerToArray(const PointerToArray<Element> &copy) :
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: ConstPointerToArray::Copy Constructor
 //     Function: ConstPointerToArray::Copy Constructor
-//       Access: Published
+//       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 template<class Element>
 template<class Element>
@@ -670,7 +756,7 @@ rend() const {
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: ConstPointerToArray::size
 //     Function: ConstPointerToArray::size
-//       Access: Published
+//       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 template<class Element>
 template<class Element>
@@ -755,7 +841,7 @@ back() const {
 #ifndef WIN32_VC
 #ifndef WIN32_VC
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: ConstPointerToArray::Indexing operator
 //     Function: ConstPointerToArray::Indexing operator
-//       Access: Published
+//       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 template<class Element>
 template<class Element>
@@ -773,7 +859,7 @@ operator [](size_type n) const {
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: ConstPointerToArray::Indexing operator
 //     Function: ConstPointerToArray::Indexing operator
-//       Access: Published
+//       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 template<class Element>
 template<class Element>
@@ -783,21 +869,6 @@ operator [](int n) const {
 }
 }
 #endif
 #endif
 
 
-////////////////////////////////////////////////////////////////////
-//     Function: ConstPointerToArray::get_element
-//       Access: Published
-//  Description: This method exists mainly to access the elements of
-//               the array easily from a high-level language such as
-//               Python, especially on Windows, where the above index
-//               element accessor methods can't be defined because of
-//               a confusion with the pointer typecast operator.
-////////////////////////////////////////////////////////////////////
-template<class Element>
-INLINE const Element &ConstPointerToArray<Element>::
-get_element(size_type n) const {
-  return (*this)[n];
-}
-
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: ConstPointerToArray::Typecast operator
 //     Function: ConstPointerToArray::Typecast operator
 //       Access: Public
 //       Access: Public
@@ -843,6 +914,57 @@ v() const {
   return *(To *)(this->_void_ptr);
   return *(To *)(this->_void_ptr);
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: ConstPointerToArray::get_element
+//       Access: Published
+//  Description: This method exists mainly to access the elements of
+//               the array easily from a high-level language such as
+//               Python, especially on Windows, where the above index
+//               element accessor methods can't be defined because of
+//               a confusion with the pointer typecast operator.
+////////////////////////////////////////////////////////////////////
+template<class Element>
+INLINE const Element &ConstPointerToArray<Element>::
+get_element(size_type n) const {
+  return (*this)[n];
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: ConstPointerToArray::get_data
+//       Access: Published
+//  Description: This method exists mainly to access the data of
+//               the array easily from a high-level language such as
+//               Python.
+//
+//               It returns the entire contents of the vector as a
+//               block of raw data in a string.
+////////////////////////////////////////////////////////////////////
+template<class Element>
+INLINE string ConstPointerToArray<Element>::
+get_data() const {
+  return get_subdata(0, size());
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: ConstPointerToArray::get_subdata
+//       Access: Published
+//  Description: This method exists mainly to access the data of
+//               the array easily from a high-level language such as
+//               Python.
+//
+//               It returns the contents of a portion of the
+//               vector--from element (n) through element (n + count -
+//               1)--as a block of raw data in a string.
+////////////////////////////////////////////////////////////////////
+template<class Element>
+INLINE string ConstPointerToArray<Element>::
+get_subdata(size_type n, size_type count) const {
+  n = min(n, size());
+  count = max(count, n);
+  count = min(count, size() - n);
+  return string((const char *)(p() + n), sizeof(Element) * count);
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: ConstPointerToArray::get_ref_count
 //     Function: ConstPointerToArray::get_ref_count
 //       Access: Public
 //       Access: Public

+ 46 - 9
panda/src/express/pointerToArray.h

@@ -102,9 +102,25 @@ class ConstPointerToArray;
 template <class Element>
 template <class Element>
 class PointerToArray : public PointerToArrayBase<Element> {
 class PointerToArray : public PointerToArrayBase<Element> {
 public:
 public:
-  // By hiding this template from interrogate, we improve compile-time
-  // speed and memory utilization.
-#ifndef CPPPARSER
+  // By hiding this template from interrogate, we would improve
+  // compile-time speed and memory utilization.  However, we do want
+  // to export a minimal subset of this class.  So we define just the
+  // exportable interface here.
+#ifdef CPPPARSER
+PUBLISHED:
+  typedef TYPENAME pvector<Element>::size_type size_type;
+  INLINE PointerToArray(TypeHandle type_handle = get_type_handle(Element));
+  INLINE PointerToArray(const PointerToArray<Element> &copy);
+  INLINE size_type size() const;
+  INLINE const Element &get_element(size_type n) const;
+  INLINE void set_element(size_type n, const Element &value);
+  INLINE string get_data() const;
+  INLINE void set_data(const string &data);
+  INLINE string get_subdata(size_type n, size_type count) const;
+  INLINE void set_subdata(size_type n, size_type count, const string &data);
+
+#else  // CPPPARSER
+  // This is the actual, complete interface.
   typedef TYPENAME PointerToArrayBase<Element>::To To;
   typedef TYPENAME PointerToArrayBase<Element>::To To;
   typedef TYPENAME pvector<Element>::value_type value_type;
   typedef TYPENAME pvector<Element>::value_type value_type;
   typedef TYPENAME pvector<Element>::reference reference;
   typedef TYPENAME pvector<Element>::reference reference;
@@ -162,8 +178,6 @@ public:
   INLINE reference operator [](size_type n) const;
   INLINE reference operator [](size_type n) const;
   INLINE reference operator [](int n) const;
   INLINE reference operator [](int n) const;
 #endif
 #endif
-  INLINE const Element &get_element(size_type n) const;
-  INLINE void set_element(size_type n, const Element &value);
 
 
   INLINE void push_back(const Element &x);
   INLINE void push_back(const Element &x);
   INLINE void pop_back();
   INLINE void pop_back();
@@ -173,6 +187,14 @@ public:
   INLINE Element *p() const;
   INLINE Element *p() const;
   INLINE pvector<Element> &v() const;
   INLINE pvector<Element> &v() const;
 
 
+  // Methods to help out Python and other high-level languages.
+  INLINE const Element &get_element(size_type n) const;
+  INLINE void set_element(size_type n, const Element &value);
+  INLINE string get_data() const;
+  INLINE void set_data(const string &data);
+  INLINE string get_subdata(size_type n, size_type count) const;
+  INLINE void set_subdata(size_type n, size_type count, const string &data);
+
   //These functions are only to be used in Reading through BamReader.
   //These functions are only to be used in Reading through BamReader.
   //They are designed to work in pairs, so that you register what is
   //They are designed to work in pairs, so that you register what is
   //returned by get_void_ptr with BamReader and when you are setting
   //returned by get_void_ptr with BamReader and when you are setting
@@ -218,9 +240,20 @@ private:
 template <class Element>
 template <class Element>
 class ConstPointerToArray : public PointerToArrayBase<Element> {
 class ConstPointerToArray : public PointerToArrayBase<Element> {
 public:
 public:
-  // By hiding this template from interrogate, we improve compile-time
-  // speed and memory utilization.
-#ifndef CPPPARSER
+  // By hiding this template from interrogate, we would improve
+  // compile-time speed and memory utilization.  However, we do want
+  // to export a minimal subset of this class.  So we define just the
+  // exportable interface here.
+#ifdef CPPPARSER
+PUBLISHED:
+  typedef TYPENAME pvector<Element>::size_type size_type;
+  INLINE size_type size() const;
+  INLINE const Element &get_element(size_type n) const;
+  INLINE string get_data() const;
+  INLINE string get_subdata(size_type n, size_type count) const;
+
+#else  // CPPPARSER
+  // This is the actual, complete interface.
   typedef TYPENAME PointerToArrayBase<Element>::To To;
   typedef TYPENAME PointerToArrayBase<Element>::To To;
   typedef TYPENAME pvector<Element>::value_type value_type;
   typedef TYPENAME pvector<Element>::value_type value_type;
   typedef TYPENAME pvector<Element>::const_reference reference;
   typedef TYPENAME pvector<Element>::const_reference reference;
@@ -264,12 +297,16 @@ public:
   INLINE reference operator [](size_type n) const;
   INLINE reference operator [](size_type n) const;
   INLINE reference operator [](int n) const;
   INLINE reference operator [](int n) const;
 #endif
 #endif
-  INLINE const Element &get_element(size_type n) const;
 
 
   INLINE operator const Element *() const;
   INLINE operator const Element *() const;
   INLINE const Element *p() const;
   INLINE const Element *p() const;
   INLINE const pvector<Element> &v() const;
   INLINE const pvector<Element> &v() const;
 
 
+  // Methods to help out Python and other high-level languages.
+  INLINE const Element &get_element(size_type n) const;
+  INLINE string get_data() const;
+  INLINE string get_subdata(size_type n, size_type count) const;
+
   INLINE int get_ref_count() const;
   INLINE int get_ref_count() const;
 
 
   INLINE int get_node_ref_count() const;
   INLINE int get_node_ref_count() const;