Panda3D
physxCcdSkeletonDesc.cxx
1 // Filename: physxCcdSkeletonDesc.cxx
2 // Created by: enn0x (01May12)
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 
15 #include "physxCcdSkeletonDesc.h"
16 #include "physxManager.h"
17 
18 #include "nodePathCollection.h"
19 #include "geomNode.h"
20 #include "geomVertexReader.h"
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: PhysxCcdSkeletonDesc::set_num_vertices
24 // Access: Published
25 // Description: Sets the number of vertices to be stored within
26 // this triangle mesh. The function allocates memory
27 // for the vertices, but it does not set any vertices.
28 //
29 // This method must be called before any calls to
30 // set_vertex are done!
31 ////////////////////////////////////////////////////////////////////
33 set_num_vertices(unsigned int numVertices) {
34 
35  if (_desc.points) {
36  delete [] _vertices;
37  }
38 
39  _vertices = new NxVec3[numVertices];
40 
41  _desc.numVertices = numVertices;
42  _desc.points = _vertices;
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: PhysxCcdSkeletonDesc::set_vertex
47 // Access: Published
48 // Description: Sets a single vertex. You have to call the function
49 // set_num_vertices before you can call this function.
50 ////////////////////////////////////////////////////////////////////
52 set_vertex(unsigned int idx, const LPoint3f &vert) {
53 
54  nassertv(_desc.numVertices > idx);
55  _vertices[idx] = PhysxManager::point3_to_nxVec3(vert);
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: PhysxCcdSkeletonDesc::set_num_triangles
60 // Access: Published
61 // Description: Sets the number of triangles to be stored in this
62 // triangle mesh.
63 //
64 // This method must be called before any calls to
65 // set_triangle are done!
66 ////////////////////////////////////////////////////////////////////
68 set_num_triangles(unsigned int numTriangles) {
69 
70  if (_desc.triangles) {
71  delete [] _triangles;
72  }
73 
74  _triangles = new NxU32[3 * numTriangles];
75 
76  _desc.numTriangles = numTriangles;
77  _desc.triangles = _triangles;
78 }
79 
80 ////////////////////////////////////////////////////////////////////
81 // Function: PhysxCcdSkeletonDesc::set_triangles
82 // Access: Published
83 // Description: Sets a single triangle, by providing the three
84 // indices i1, i2, i3.
85 ////////////////////////////////////////////////////////////////////
87 set_triangle(unsigned int idx,
88  unsigned int i1, unsigned int i2, unsigned int i3) {
89 
90  nassertv(_desc.numTriangles > idx);
91 
92  idx = 3 * idx;
93  _triangles[idx] = i1;
94  _triangles[idx + 1] = i2;
95  _triangles[idx + 2] = i3;
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: PhysxCcdSkeletonDesc::get_desc
100 // Access: Public
101 // Description:
102 ////////////////////////////////////////////////////////////////////
103 const NxSimpleTriangleMesh &PhysxCcdSkeletonDesc::
104 get_desc() const {
105 
106  return _desc;
107 }
108 
109 ////////////////////////////////////////////////////////////////////
110 // Function: PhysxCcdSkeletonDesc::set_from_node_path
111 // Access: Published
112 // Description: A convenience method to set the mesh data from
113 // a NodePath in a single call. The method iterates
114 // over the NodePath geoms and collects data for
115 // the triangle mesh.
116 //
117 // Do not use the following function when using this
118 // one:
119 // - set_num_vertices
120 // - set_vertex
121 // - set_num_triangles
122 // - set_triangle
123 ////////////////////////////////////////////////////////////////////
126 
127  pvector<LPoint3f> dataVertices;
128  pvector<int> dataIndices;
129 
130  // Collect data from NodePath
131  NodePathCollection npc = np.find_all_matches( "**/+GeomNode" );
132  for (int i=0; i<npc.get_num_paths(); i++) {
133  NodePath gnp = npc.get_path(i);
134  GeomNode *gnode = DCAST(GeomNode, gnp.node());
135 
136  for (int j=0; j<gnode->get_num_geoms(); j++) {
137  CPT(Geom) geom = gnode->get_geom(j);
138 
139  // Vertices
140  CPT(GeomVertexData) vdata = geom->get_vertex_data();
141  GeomVertexReader reader = GeomVertexReader(vdata, InternalName::get_vertex());
142 
143  while (!reader.is_at_end()) {
144  dataVertices.push_back(reader.get_data3f());
145  }
146 
147  // Indices
148  for (int k=0; k<geom->get_num_primitives(); k++) {
149 
150  CPT(GeomPrimitive) prim = geom->get_primitive(k);
151  prim = prim->decompose();
152 
153  for (int l=0; l<prim->get_num_primitives(); l++) {
154 
155  int s = prim->get_primitive_start(l);
156  int e = prim->get_primitive_end(l);
157 
158  for (int l=s; l<e; l++) {
159  dataIndices.push_back(prim->get_vertex(l));
160  }
161  }
162  }
163  }
164  }
165 
166  // Set descriptor members
167  int i;
168 
169  NxU32 numVertices = dataVertices.size();
170  NxU32 numTriangles = dataIndices.size() / 3;
171 
172  _vertices = new NxVec3[numVertices];
173  _triangles = new NxU32[3 * numTriangles];
174 
175  i = 0;
177  for (vit=dataVertices.begin(); vit!=dataVertices.end(); vit++) {
178  LPoint3f v = *vit;
179 
180  _vertices[i].x = v.get_x();
181  _vertices[i].y = v.get_y();
182  _vertices[i].z = v.get_z();
183  i++;
184  }
185 
186  i = 0;
188  for(iit=dataIndices.begin(); iit!=dataIndices.end(); iit++) {
189  NxU32 idx = *iit;
190 
191  _triangles[i] = idx;
192  i++;
193  }
194 
195  _desc.numVertices = numVertices;
196  _desc.points = _vertices;
197  _desc.numTriangles = numTriangles;
198  _desc.triangles = _triangles;
199 }
200 
void set_num_triangles(unsigned int n)
Sets the number of triangles to be stored in this triangle mesh.
void set_from_node_path(const NodePath &np)
A convenience method to set the mesh data from a NodePath in a single call.
static NxVec3 point3_to_nxVec3(const LPoint3f &p)
Converts from LPoint3f to NxVec3.
Definition: physxManager.I:77
This is an abstract base class for a family of classes that represent the fundamental geometry primit...
Definition: geomPrimitive.h:63
const LVecBase3f & get_data3f()
Returns the data associated with the read row, expressed as a 3-component value, and advances the rea...
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
int get_num_geoms() const
Returns the number of geoms in the node.
Definition: geomNode.I:46
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
A container for geometry primitives.
Definition: geom.h:58
void set_triangle(unsigned int idx, unsigned int i1, unsigned int i2, unsigned int i3)
Sets a single triangle, by providing the three indices i1, i2, i3.
NodePath get_path(int index) const
Returns the nth NodePath in the collection.
int get_num_paths() const
Returns the number of NodePaths in the collection.
PandaNode * node() const
Returns the referenced node of the path.
Definition: nodePath.I:284
void set_num_vertices(unsigned int n)
Sets the number of vertices to be stored within this triangle mesh.
This object provides a high-level interface for quickly reading a sequence of numeric values from a v...
bool is_at_end() const
Returns true if the reader is currently at the end of the list of vertices, false otherwise...
void set_vertex(unsigned int idx, const LPoint3f &vert)
Sets a single vertex.
NodePathCollection find_all_matches(const string &path) const
Returns the complete set of all NodePaths that begin with this NodePath and can be extended by path...
Definition: nodePath.cxx:480
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165
A node that holds Geom objects, renderable pieces of geometry.
Definition: geomNode.h:37
This is a set of zero or more NodePaths.