Panda3D

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