Panda3D
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  delete [] vertices;
154 }
155 
156 ////////////////////////////////////////////////////////////////////
157 // Function: BulletTriangleMesh::add_array
158 // Access: Published
159 // Description:
160 ////////////////////////////////////////////////////////////////////
161 void BulletTriangleMesh::
162 add_array(const PTA_LVecBase3 &points, const PTA_int &indices, bool remove_duplicate_vertices) {
163 
164  // Convert vertices
165  btVector3 *vertices = new btVector3[points.size()];
166 
167  int i = 0;
168  PTA_LVecBase3::const_iterator it;
169  for (it=points.begin(); it!=points.end(); it++) {
170  LVecBase3 v = *it;
171  vertices[i] = LVecBase3_to_btVector3(v);
172  i++;
173  }
174 
175  // Add triangles
176  int j = 0;
177  while (j+2 < (int)indices.size()) {
178 
179  btVector3 v0 = vertices[indices[j++]];
180  btVector3 v1 = vertices[indices[j++]];
181  btVector3 v2 = vertices[indices[j++]];
182 
183  _mesh->addTriangle(v0, v1, v2, remove_duplicate_vertices);
184  }
185 
186  delete [] vertices;
187 }
188 
189 ////////////////////////////////////////////////////////////////////
190 // Function: BulletTriangleMesh::output
191 // Access: Published, Virtual
192 // Description:
193 ////////////////////////////////////////////////////////////////////
194 void BulletTriangleMesh::
195 output(ostream &out) const {
196 
197  out << get_type() << ", " << _mesh->getNumTriangles();
198 }
199 
200 ////////////////////////////////////////////////////////////////////
201 // Function: BulletTriangleMesh::write
202 // Access: Published, Virtual
203 // Description:
204 ////////////////////////////////////////////////////////////////////
205 void BulletTriangleMesh::
206 write(ostream &out, int indent_level) const {
207 
208  indent(out, indent_level) << get_type() << ":" << endl;
209 
210  IndexedMeshArray& array = _mesh->getIndexedMeshArray();
211  for (int i=0; i < array.size(); i++) {
212  indent(out, indent_level + 2) << "IndexedMesh " << i << ":" << endl;
213  btIndexedMesh meshPart = array.at(i);
214 
215  indent(out, indent_level + 4) << "num triangles:" << meshPart.m_numTriangles << endl;
216  indent(out, indent_level + 4) << "num vertices:" << meshPart.m_numVertices << endl;
217  }
218 }
219 
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:464
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
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
const LVecBase3 & get_data3()
Returns the data associated with the read row, expressed as a 3-component value, and advances the rea...
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...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85