Panda3D
bulletConvexPointCloudShape.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 bulletConvexPointCloudShape.cxx
10  * @author enn0x
11  * @date 2010-01-30
12  */
13 
15 
16 #include "bulletWorld.h"
17 
18 #include "bullet_utils.h"
19 
20 #include "geomVertexReader.h"
21 
22 TypeHandle BulletConvexPointCloudShape::_type_handle;
23 
24 /**
25  *
26  */
27 BulletConvexPointCloudShape::
28 BulletConvexPointCloudShape(const PTA_LVecBase3 &points, LVecBase3 scale) :
29  _scale(scale) {
30 
31  btVector3 btScale = LVecBase3_to_btVector3(scale);
32 
33  // Convert points
34  btVector3 *btPoints = new btVector3[points.size()];
35 
36  int i = 0;
37  PTA_LVecBase3::const_iterator it;
38  for (it=points.begin(); it!=points.end(); it++) {
39  btPoints[i] = LVecBase3_to_btVector3(*it);
40  i++;
41  }
42 
43  // Create shape
44  _shape = new btConvexPointCloudShape(btPoints, points.size(), btScale);
45  _shape->setUserPointer(this);
46 }
47 
48 /**
49  *
50  */
51 btCollisionShape *BulletConvexPointCloudShape::
52 ptr() const {
53 
54  return _shape;
55 }
56 
57 /**
58  *
59  */
60 BulletConvexPointCloudShape::
61 BulletConvexPointCloudShape(const Geom *geom, LVecBase3 scale) {
62 
63  btVector3 btScale = LVecBase3_to_btVector3(scale);
64  _scale = scale;
65 
66  // Collect points
67  pvector<LPoint3> points;
68 
69  CPT(GeomVertexData) vdata = geom->get_vertex_data();
70  GeomVertexReader reader = GeomVertexReader(vdata, InternalName::get_vertex());
71 
72  while (!reader.is_at_end()) {
73  points.push_back(reader.get_data3());
74  }
75 
76  // Convert points
77  btVector3 *btPoints = new btVector3[points.size()];
78 
79  int i = 0;
81  for (it=points.begin(); it!=points.end(); it++) {
82  btPoints[i] = LVecBase3_to_btVector3(*it);
83  i++;
84  }
85 
86  // Create
87  _shape = new btConvexPointCloudShape(btPoints, points.size(), btScale);
88  _shape->setUserPointer(this);
89 }
90 
91 /**
92  *
93  */
94 BulletConvexPointCloudShape::
95 BulletConvexPointCloudShape(const BulletConvexPointCloudShape &copy) {
96  LightMutexHolder holder(BulletWorld::get_global_lock());
97 
98  _scale = copy._scale;
99 
100  btVector3 *btPoints = copy._shape->getUnscaledPoints();
101  int numPoints = copy._shape->getNumPoints();
102  btVector3 btScale = LVecBase3_to_btVector3(_scale);
103 
104  _shape = new btConvexPointCloudShape(btPoints, numPoints, btScale);
105  _shape->setUserPointer(this);
106 }
107 
108 /**
109  *
110  */
111 int BulletConvexPointCloudShape::
112 get_num_points() const {
113  LightMutexHolder holder(BulletWorld::get_global_lock());
114 
115  return _shape->getNumPoints();
116 }
117 
118 /**
119  * Tells the BamReader how to create objects of type BulletShape.
120  */
123  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
124 }
125 
126 /**
127  * Writes the contents of this object to the datagram for shipping out to a
128  * Bam file.
129  */
132  BulletShape::write_datagram(manager, dg);
133 
134  // parameters to serialize: num points, points, scale
135  _scale.write_datagram(dg);
136 
137  dg.add_int32(get_num_points());
138  for (int i = 0; i < get_num_points(); ++i){
139  btVector3_to_LVector3(_shape->getUnscaledPoints()[i]).write_datagram(dg);
140  }
141 }
142 
143 /**
144  * This function is called by the BamReader's factory when a new object of
145  * type BulletShape is encountered in the Bam file. It should create the
146  * BulletShape and extract its information from the file.
147  */
148 TypedWritable *BulletConvexPointCloudShape::
149 make_from_bam(const FactoryParams &params) {
150  // create a default BulletConvexPointCloudShape
152  DatagramIterator scan;
153  BamReader *manager;
154 
155  parse_params(params, scan, manager);
156  param->fillin(scan, manager);
157 
158  return param;
159 }
160 
161 /**
162  * This internal function is called by make_from_bam to read in all of the
163  * relevant data from the BamFile for the new BulletShape.
164  */
165 void BulletConvexPointCloudShape::
166 fillin(DatagramIterator &scan, BamReader *manager) {
167  BulletShape::fillin(scan, manager);
168 
169  // parameters to serialize: num points, points, scale
170  _scale.read_datagram(scan);
171 
172  unsigned int num_points = scan.get_uint32();
173 
174  btVector3 *btPoints = new btVector3[num_points];
175  for (unsigned int i = 0; i < num_points; ++i) {
176  LPoint3 point;
177  point.read_datagram(scan);
178  btPoints[i] = LVecBase3_to_btVector3(point);
179  }
180 
181  // Create shape
182  _shape = new btConvexPointCloudShape(btPoints, num_points, LVecBase3_to_btVector3(_scale));
183  _shape->setUserPointer(this);
184 }
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
virtual void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is intended to be called by each class's make_from_bam() method to read in all...
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
void parse_params(const FactoryParams &params, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
Definition: bamReader.I:275
Similar to MutexHolder, but for a light mutex.
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:54
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
uint32_t get_uint32()
Extracts an unsigned 32-bit integer.
const LVecBase3 & get_data3()
Returns the data associated with the read row, expressed as a 3-component value, and advances the rea...
void register_factory(TypeHandle handle, CreateFunc *func, void *user_data=nullptr)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:73
static void register_with_read_factory()
Tells the BamReader how to create objects of type BulletShape.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void add_int32(int32_t value)
Adds a signed 32-bit integer to the datagram.
Definition: datagram.I:67
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A class to retrieve the individual data elements previously stored in a Datagram.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38