Panda3D
 All Classes Functions Variables Enumerations
bulletTriangleMesh.cxx
1 // Filename: bulletTriangleMesh.cxx
2 // Created by: enn0x (09Feb10)
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 "bulletTriangleMesh.h"
16 
17 #include "pvector.h"
18 #include "geomVertexData.h"
19 #include "geomVertexReader.h"
20 
21 TypeHandle BulletTriangleMesh::_type_handle;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: BulletTriangleMesh::Constructor
25 // Access: Published
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 BulletTriangleMesh::
29 BulletTriangleMesh() {
30 
31  _mesh = new btTriangleMesh();
32 }
33 
34 ////////////////////////////////////////////////////////////////////
35 // Function: BulletTriangleMesh::get_num_triangles
36 // Access: Published
37 // Description:
38 ////////////////////////////////////////////////////////////////////
39 int BulletTriangleMesh::
40 get_num_triangles() const {
41 
42  return _mesh->getNumTriangles();
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: BulletTriangleMesh::preallocate
47 // Access: Published
48 // Description:
49 ////////////////////////////////////////////////////////////////////
50 void BulletTriangleMesh::
51 preallocate(int num_verts, int num_indices) {
52 
53  _mesh->preallocateVertices(num_verts);
54  _mesh->preallocateIndices(num_indices);
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: BulletTriangleMesh::add_triangle
59 // Access: Published
60 // Description:
61 ////////////////////////////////////////////////////////////////////
62 void BulletTriangleMesh::
63 add_triangle(const LPoint3 &p0, const LPoint3 &p1, const LPoint3 &p2, bool remove_duplicate_vertices) {
64 
65  nassertv(!p0.is_nan());
66  nassertv(!p1.is_nan());
67  nassertv(!p2.is_nan());
68 
69  _mesh->addTriangle(
70  LVecBase3_to_btVector3(p0),
71  LVecBase3_to_btVector3(p1),
72  LVecBase3_to_btVector3(p2),
73  remove_duplicate_vertices);
74 }
75 
76 ////////////////////////////////////////////////////////////////////
77 // Function: BulletTriangleMesh::set_welding_distance
78 // Access: Published
79 // Description:
80 ////////////////////////////////////////////////////////////////////
81 void BulletTriangleMesh::
82 set_welding_distance(PN_stdfloat distance) {
83 
84  _mesh->m_weldingThreshold = distance;
85 }
86 
87 ////////////////////////////////////////////////////////////////////
88 // Function: BulletTriangleMesh::get_welding_distance
89 // Access: Published
90 // Description:
91 ////////////////////////////////////////////////////////////////////
92 PN_stdfloat BulletTriangleMesh::
93 get_welding_distance() const {
94 
95  return _mesh->m_weldingThreshold;
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: BulletTriangleMesh::add_geom
100 // Access: Published
101 // Description:
102 ////////////////////////////////////////////////////////////////////
103 void BulletTriangleMesh::
104 add_geom(const Geom *geom, bool remove_duplicate_vertices, const TransformState *ts) {
105 
106  nassertv(geom);
107  nassertv(ts);
108 
109  LMatrix4 m = ts->get_mat();
110 
111  // Collect points
112  pvector<LPoint3> points;
113 
114  CPT(GeomVertexData) vdata = geom->get_vertex_data();
115  GeomVertexReader reader = GeomVertexReader(vdata, InternalName::get_vertex());
116 
117  while (!reader.is_at_end()) {
118  points.push_back(m.xform_point(reader.get_data3()));
119  }
120 
121  // Convert points
122  btVector3 *vertices = new btVector3[points.size()];
123 
124  int i = 0;
126  for (it=points.begin(); it!=points.end(); it++) {
127  LPoint3 v = *it;
128  vertices[i] = LVecBase3_to_btVector3(v);
129  i++;
130  }
131 
132  // Add triangles
133  for (int k=0; k<geom->get_num_primitives(); k++) {
134 
135  CPT(GeomPrimitive) prim = geom->get_primitive(k);
136  prim = prim->decompose();
137 
138  for (int l=0; l<prim->get_num_primitives(); l++) {
139 
140  int s = prim->get_primitive_start(l);
141  int e = prim->get_primitive_end(l);
142 
143  nassertv(e - s == 3);
144 
145  btVector3 v0 = vertices[prim->get_vertex(s)];
146  btVector3 v1 = vertices[prim->get_vertex(s+1)];
147  btVector3 v2 = vertices[prim->get_vertex(s+2)];
148 
149  _mesh->addTriangle(v0, v1, v2, remove_duplicate_vertices);
150  }
151  }
152 }
153 
154 ////////////////////////////////////////////////////////////////////
155 // Function: BulletTriangleMesh::add_array
156 // Access: Published
157 // Description:
158 ////////////////////////////////////////////////////////////////////
159 void BulletTriangleMesh::
160 add_array(const PTA_LVecBase3 &points, const PTA_int &indices, bool remove_duplicate_vertices) {
161 
162  // Convert vertices
163  btVector3 *vertices = new btVector3[points.size()];
164 
165  int i = 0;
166  PTA_LVecBase3::const_iterator it;
167  for (it=points.begin(); it!=points.end(); it++) {
168  LVecBase3 v = *it;
169  vertices[i] = LVecBase3_to_btVector3(v);
170  i++;
171  }
172 
173  // Add triangles
174  int j = 0;
175  while (j+2 < (int)indices.size()) {
176 
177  btVector3 v0 = vertices[indices[j++]];
178  btVector3 v1 = vertices[indices[j++]];
179  btVector3 v2 = vertices[indices[j++]];
180 
181  _mesh->addTriangle(v0, v1, v2, remove_duplicate_vertices);
182  }
183 }
184 
185 ////////////////////////////////////////////////////////////////////
186 // Function: BulletTriangleMesh::output
187 // Access: Published, Virtual
188 // Description:
189 ////////////////////////////////////////////////////////////////////
190 void BulletTriangleMesh::
191 output(ostream &out) const {
192 
193  out << get_type() << ", " << _mesh->getNumTriangles();
194 }
195 
196 ////////////////////////////////////////////////////////////////////
197 // Function: BulletTriangleMesh::write
198 // Access: Published, Virtual
199 // Description:
200 ////////////////////////////////////////////////////////////////////
201 void BulletTriangleMesh::
202 write(ostream &out, int indent_level) const {
203 
204  indent(out, indent_level) << get_type() << ":" << endl;
205 
206  IndexedMeshArray& array = _mesh->getIndexedMeshArray();
207  for (int i=0; i < array.size(); i++) {
208  indent(out, indent_level + 2) << "IndexedMesh " << i << ":" << endl;
209  btIndexedMesh meshPart = array.at(i);
210 
211  indent(out, indent_level + 4) << "num triangles:" << meshPart.m_numTriangles << endl;
212  indent(out, indent_level + 4) << "num vertices:" << meshPart.m_numVertices << endl;
213  }
214 }
215 
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:105
This is an abstract base class for a family of classes that represent the fundamental geometry primit...
Definition: geomPrimitive.h:63
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
bool is_nan() const
Returns true if any component of the vector is not-a-number, false otherwise.
Definition: lvecBase3.h:463
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
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
This object provides a high-level interface for quickly reading a sequence of numeric values from a v...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
LVecBase3f xform_point(const LVecBase3f &v) const
The matrix transforms a 3-component point (including translation component) and returns the result...
Definition: lmatrix.h:1667
int get_num_primitives() const
Returns the number of GeomPrimitive objects stored within the Geom, each of which represents a number...
Definition: geom.I:110