15 #include "bulletTriangleMesh.h"
18 #include "geomVertexData.h"
19 #include "geomVertexReader.h"
29 BulletTriangleMesh() {
31 _mesh =
new btTriangleMesh();
39 int BulletTriangleMesh::
40 get_num_triangles()
const {
42 return _mesh->getNumTriangles();
50 void BulletTriangleMesh::
51 preallocate(
int num_verts,
int num_indices) {
53 _mesh->preallocateVertices(num_verts);
54 _mesh->preallocateIndices(num_indices);
62 void BulletTriangleMesh::
63 add_triangle(
const LPoint3 &p0,
const LPoint3 &p1,
const LPoint3 &p2,
bool remove_duplicate_vertices) {
70 LVecBase3_to_btVector3(p0),
71 LVecBase3_to_btVector3(p1),
72 LVecBase3_to_btVector3(p2),
73 remove_duplicate_vertices);
81 void BulletTriangleMesh::
82 set_welding_distance(PN_stdfloat distance) {
84 _mesh->m_weldingThreshold = distance;
92 PN_stdfloat BulletTriangleMesh::
93 get_welding_distance()
const {
95 return _mesh->m_weldingThreshold;
103 void BulletTriangleMesh::
104 add_geom(
const Geom *geom,
bool remove_duplicate_vertices,
const TransformState *ts) {
117 while (!reader.is_at_end()) {
118 points.push_back(m.
xform_point(reader.get_data3()));
122 btVector3 *vertices =
new btVector3[points.size()];
126 for (it=points.begin(); it!=points.end(); it++) {
128 vertices[i] = LVecBase3_to_btVector3(v);
136 prim = prim->decompose();
138 for (
int l=0; l<prim->get_num_primitives(); l++) {
140 int s = prim->get_primitive_start(l);
141 int e = prim->get_primitive_end(l);
143 nassertv(e - s == 3);
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)];
149 _mesh->addTriangle(v0, v1, v2, remove_duplicate_vertices);
159 void BulletTriangleMesh::
160 add_array(
const PTA_LVecBase3 &points,
const PTA_int &indices,
bool remove_duplicate_vertices) {
163 btVector3 *vertices =
new btVector3[points.size()];
166 PTA_LVecBase3::const_iterator it;
167 for (it=points.begin(); it!=points.end(); it++) {
169 vertices[i] = LVecBase3_to_btVector3(v);
175 while (j+2 < (
int)indices.size()) {
177 btVector3 v0 = vertices[indices[j++]];
178 btVector3 v1 = vertices[indices[j++]];
179 btVector3 v2 = vertices[indices[j++]];
181 _mesh->addTriangle(v0, v1, v2, remove_duplicate_vertices);
190 void BulletTriangleMesh::
191 output(ostream &out)
const {
193 out << get_type() <<
", " << _mesh->getNumTriangles();
201 void BulletTriangleMesh::
202 write(ostream &out,
int indent_level)
const {
204 indent(out, indent_level) << get_type() <<
":" << endl;
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);
211 indent(out, indent_level + 4) <<
"num triangles:" << meshPart.m_numTriangles << endl;
212 indent(out, indent_level + 4) <<
"num vertices:" << meshPart.m_numVertices << endl;
This is the base class for all three-component vectors and points.
This is an abstract base class for a family of classes that represent the fundamental geometry primit...
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
bool is_nan() const
Returns true if any component of the vector is not-a-number, false otherwise.
This is a 4-by-4 transform matrix.
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
A container for geometry primitives.
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.
LVecBase3f xform_point(const LVecBase3f &v) const
The matrix transforms a 3-component point (including translation component) and returns the result...
int get_num_primitives() const
Returns the number of GeomPrimitive objects stored within the Geom, each of which represents a number...