Quellcode durchsuchen

bullet: Cleanup copying of shapes

        - Fixed copy constructors
        - Dropped operator= from shapes

Signed-off-by: deflected <[email protected]>

Closes #283
deflected vor 7 Jahren
Ursprung
Commit
3a88308f45

+ 3 - 11
panda/src/bullet/bulletBoxShape.cxx

@@ -35,19 +35,11 @@ BulletBoxShape::
 BulletBoxShape(const BulletBoxShape &copy) {
   LightMutexHolder holder(BulletWorld::get_global_lock());
 
-  _shape = copy._shape;
   _half_extents = copy._half_extents;
-}
-
-/**
- *
- */
-void BulletBoxShape::
-operator = (const BulletBoxShape &copy) {
-  LightMutexHolder holder(BulletWorld::get_global_lock());
+  btVector3 btHalfExtents = LVecBase3_to_btVector3(_half_extents);
 
-  _shape = copy._shape;
-  _half_extents = copy._half_extents;
+  _shape = new btBoxShape(btHalfExtents);
+  _shape->setUserPointer(this);
 }
 
 /**

+ 0 - 1
panda/src/bullet/bulletBoxShape.h

@@ -34,7 +34,6 @@ private:
 PUBLISHED:
   explicit BulletBoxShape(const LVecBase3 &halfExtents);
   BulletBoxShape(const BulletBoxShape &copy);
-  void operator = (const BulletBoxShape &copy);
   INLINE ~BulletBoxShape();
 
   LVecBase3 get_half_extents_without_margin() const;

+ 2 - 1
panda/src/bullet/bulletCapsuleShape.I

@@ -18,7 +18,8 @@ INLINE BulletCapsuleShape::
 BulletCapsuleShape() :
   _shape(nullptr),
   _radius(0),
-  _height(0) {
+  _height(0),
+  _up(X_up) {
 }
 
 /**

+ 22 - 16
panda/src/bullet/bulletCapsuleShape.cxx

@@ -21,7 +21,8 @@ TypeHandle BulletCapsuleShape::_type_handle;
 BulletCapsuleShape::
 BulletCapsuleShape(PN_stdfloat radius, PN_stdfloat height, BulletUpAxis up) :
   _radius(radius),
-  _height(height) {
+  _height(height),
+  _up(up) {
 
   switch (up) {
   case X_up:
@@ -49,24 +50,29 @@ BulletCapsuleShape::
 BulletCapsuleShape(const BulletCapsuleShape &copy) {
   LightMutexHolder holder(BulletWorld::get_global_lock());
 
-  _shape = copy._shape;
   _radius = copy._radius;
   _height = copy._height;
-}
+  _up = copy._up;
 
-/**
- *
- */
-void BulletCapsuleShape::
-operator = (const BulletCapsuleShape &copy) {
-  LightMutexHolder holder(BulletWorld::get_global_lock());
+  switch (_up) {
+  case X_up:
+    _shape = new btCapsuleShapeX(_radius, _height);
+    break;
+  case Y_up:
+    _shape = new btCapsuleShape(_radius, _height);
+    break;
+  case Z_up:
+    _shape = new btCapsuleShapeZ(_radius, _height);
+    break;
+  default:
+    bullet_cat.error() << "invalid up-axis:" << _up << endl;
+    break;
+  }
 
-  _shape = copy._shape;
-  _radius = copy._radius;
-  _height = copy._height;
+  nassertv(_shape);
+  _shape->setUserPointer(this);
 }
 
-
 /**
  *
  */
@@ -131,9 +137,9 @@ fillin(DatagramIterator &scan, BamReader *manager) {
   // parameters to serialize: radius, height, up
   _radius = scan.get_stdfloat();
   _height = scan.get_stdfloat();
-  int up = (int) scan.get_int8();
+  _up = (BulletUpAxis) scan.get_int8();
 
-  switch (up) {
+  switch (_up) {
   case X_up:
     _shape = new btCapsuleShapeX(_radius, _height);
     break;
@@ -144,7 +150,7 @@ fillin(DatagramIterator &scan, BamReader *manager) {
     _shape = new btCapsuleShapeZ(_radius, _height);
     break;
   default:
-    bullet_cat.error() << "invalid up-axis:" << up << endl;
+    bullet_cat.error() << "invalid up-axis:" << _up << endl;
     break;
   }
 

+ 1 - 1
panda/src/bullet/bulletCapsuleShape.h

@@ -31,7 +31,6 @@ private:
 PUBLISHED:
   explicit BulletCapsuleShape(PN_stdfloat radius, PN_stdfloat height, BulletUpAxis up=Z_up);
   BulletCapsuleShape(const BulletCapsuleShape &copy);
-  void operator = (const BulletCapsuleShape &copy);
   INLINE ~BulletCapsuleShape();
 
   INLINE PN_stdfloat get_radius() const;
@@ -50,6 +49,7 @@ private:
   btCapsuleShape *_shape;
   PN_stdfloat _radius;
   PN_stdfloat _height;
+  BulletUpAxis _up;
 
 public:
   static void register_with_read_factory();

+ 2 - 1
panda/src/bullet/bulletConeShape.I

@@ -18,7 +18,8 @@ INLINE BulletConeShape::
 BulletConeShape() :
   _shape(nullptr),
   _radius(0),
-  _height(0) {
+  _height(0),
+  _up(X_up) {
 }
 
 /**

+ 23 - 16
panda/src/bullet/bulletConeShape.cxx

@@ -21,7 +21,8 @@ TypeHandle BulletConeShape::_type_handle;
 BulletConeShape::
 BulletConeShape(PN_stdfloat radius, PN_stdfloat height, BulletUpAxis up) :
   _radius(radius),
-  _height(height) {
+  _height(height),
+  _up(up) {
 
   switch (up) {
   case X_up:
@@ -49,21 +50,27 @@ BulletConeShape::
 BulletConeShape(const BulletConeShape &copy) {
   LightMutexHolder holder(BulletWorld::get_global_lock());
 
-  _shape = copy._shape;
+  _up = copy._up;
   _radius = copy._radius;
   _height = copy._height;
-}
 
-/**
- *
- */
-void BulletConeShape::
-operator = (const BulletConeShape &copy) {
-  LightMutexHolder holder(BulletWorld::get_global_lock());
+  switch (_up) {
+  case X_up:
+    _shape = new btConeShapeX((btScalar)_radius, (btScalar)_height);
+    break;
+  case Y_up:
+    _shape = new btConeShape((btScalar)_radius, (btScalar)_height);
+    break;
+  case Z_up:
+    _shape = new btConeShapeZ((btScalar)_radius, (btScalar)_height);
+    break;
+  default:
+    bullet_cat.error() << "invalid up-axis:" << _up << endl;
+    break;
+  }
 
-  _shape = copy._shape;
-  _radius = copy._radius;
-  _height = copy._height;
+  nassertv(_shape);
+  _shape->setUserPointer(this);
 }
 
 /**
@@ -95,7 +102,7 @@ write_datagram(BamWriter *manager, Datagram &dg) {
   // parameters to serialize: radius, height, upIndex
   dg.add_stdfloat(_radius);
   dg.add_stdfloat(_height);
-  dg.add_int8((int8_t)_shape->getConeUpIndex());
+  dg.add_int8((int8_t)_up);
 }
 
 /**
@@ -130,9 +137,9 @@ fillin(DatagramIterator &scan, BamReader *manager) {
   // parameters to serialize: radius, height, up
   _radius = scan.get_stdfloat();
   _height = scan.get_stdfloat();
+  _up = (BulletUpAxis) scan.get_int8();
 
-  int up_index = (int) scan.get_int8();
-  switch (up_index) {
+  switch (_up) {
   case 0:
     _shape = new btConeShapeX((btScalar)_radius, (btScalar)_height);
     break;
@@ -143,7 +150,7 @@ fillin(DatagramIterator &scan, BamReader *manager) {
     _shape = new btConeShapeZ((btScalar)_radius, (btScalar)_height);
     break;
   default:
-    bullet_cat.error() << "invalid up-axis:" << up_index << endl;
+    bullet_cat.error() << "invalid up-axis:" << _up << endl;
     break;
   }
 

+ 1 - 1
panda/src/bullet/bulletConeShape.h

@@ -31,7 +31,6 @@ private:
 PUBLISHED:
   explicit BulletConeShape(PN_stdfloat radius, PN_stdfloat height, BulletUpAxis up=Z_up);
   BulletConeShape(const BulletConeShape &copy);
-  void operator = (const BulletConeShape &copy);
   INLINE ~BulletConeShape();
 
   INLINE PN_stdfloat get_radius() const;
@@ -47,6 +46,7 @@ private:
   btConeShape *_shape;
   PN_stdfloat _radius;
   PN_stdfloat _height;
+  BulletUpAxis _up;
 
 public:
   static void register_with_read_factory();

+ 8 - 9
panda/src/bullet/bulletConvexHullShape.cxx

@@ -36,17 +36,13 @@ BulletConvexHullShape::
 BulletConvexHullShape(const BulletConvexHullShape &copy) {
   LightMutexHolder holder(BulletWorld::get_global_lock());
 
-  _shape = copy._shape;
-}
+  _shape = new btConvexHullShape(NULL, 0);
+  _shape->setUserPointer(this);
 
-/**
- *
- */
-void BulletConvexHullShape::
-operator = (const BulletConvexHullShape &copy) {
-  LightMutexHolder holder(BulletWorld::get_global_lock());
+  for(int i = 0; i < copy._shape->getNumPoints(); i++)
+      _shape->addPoint(copy._shape->getUnscaledPoints()[i], false);
 
-  _shape = copy._shape;
+  _shape->recalcLocalAabb();
 }
 
 /**
@@ -117,6 +113,9 @@ add_geom(const Geom *geom, const TransformState *ts) {
     points.push_back(m.xform_point(reader.get_data3()));
   }
 
+  if (_shape)
+      delete _shape;
+
   // Create shape
   _shape = new btConvexHullShape(NULL, 0);
   _shape->setUserPointer(this);

+ 0 - 1
panda/src/bullet/bulletConvexHullShape.h

@@ -30,7 +30,6 @@ class EXPCL_PANDABULLET BulletConvexHullShape : public BulletShape {
 PUBLISHED:
   BulletConvexHullShape();
   BulletConvexHullShape(const BulletConvexHullShape &copy);
-  void operator = (const BulletConvexHullShape &copy);
   INLINE ~BulletConvexHullShape();
 
   void add_point(const LPoint3 &p);

+ 5 - 10
panda/src/bullet/bulletConvexPointCloudShape.cxx

@@ -92,18 +92,13 @@ BulletConvexPointCloudShape(const BulletConvexPointCloudShape &copy) {
   LightMutexHolder holder(BulletWorld::get_global_lock());
 
   _scale = copy._scale;
-  _shape = copy._shape;
-}
 
-/**
- *
- */
-void BulletConvexPointCloudShape::
-operator = (const BulletConvexPointCloudShape &copy) {
-  LightMutexHolder holder(BulletWorld::get_global_lock());
+  btVector3 *btPoints = copy._shape->getUnscaledPoints();
+  int numPoints = copy._shape->getNumPoints();
+  btVector3 btScale = LVecBase3_to_btVector3(_scale);
 
-  _scale = copy._scale;
-  _shape = copy._shape;
+  _shape = new btConvexPointCloudShape(btPoints, numPoints, btScale);
+  _shape->setUserPointer(this);
 }
 
 /**

+ 0 - 1
panda/src/bullet/bulletConvexPointCloudShape.h

@@ -34,7 +34,6 @@ PUBLISHED:
   explicit BulletConvexPointCloudShape(const PTA_LVecBase3 &points, LVecBase3 scale=LVecBase3(1.));
   explicit BulletConvexPointCloudShape(const Geom *geom, LVecBase3 scale=LVecBase3(1.));
   BulletConvexPointCloudShape(const BulletConvexPointCloudShape &copy);
-  void operator = (const BulletConvexPointCloudShape &copy);
   INLINE ~BulletConvexPointCloudShape();
 
   int get_num_points() const;

+ 2 - 1
panda/src/bullet/bulletCylinderShape.I

@@ -17,7 +17,8 @@
 INLINE BulletCylinderShape::
 BulletCylinderShape() :
   _half_extents(LVector3::zero()),
-  _shape(nullptr) {
+  _shape(nullptr),
+  _up(X_up) {
 }
 
 /**

+ 26 - 15
panda/src/bullet/bulletCylinderShape.cxx

@@ -20,7 +20,8 @@ TypeHandle BulletCylinderShape::_type_handle;
  */
 BulletCylinderShape::
 BulletCylinderShape(const LVector3 &half_extents, BulletUpAxis up) :
-  _half_extents(half_extents){
+  _half_extents(half_extents),
+  _up(up) {
 
   btVector3 btHalfExtents = LVecBase3_to_btVector3(half_extents);
 
@@ -47,7 +48,8 @@ BulletCylinderShape(const LVector3 &half_extents, BulletUpAxis up) :
  *
  */
 BulletCylinderShape::
-BulletCylinderShape(PN_stdfloat radius, PN_stdfloat height, BulletUpAxis up) {
+BulletCylinderShape(PN_stdfloat radius, PN_stdfloat height, BulletUpAxis up) :
+  _up(up) {
 
   switch (up) {
   case X_up:
@@ -78,19 +80,28 @@ BulletCylinderShape::
 BulletCylinderShape(const BulletCylinderShape &copy) {
   LightMutexHolder holder(BulletWorld::get_global_lock());
 
-  _shape = copy._shape;
+  _up = copy._up;
   _half_extents = copy._half_extents;
-}
 
-/**
- *
- */
-void BulletCylinderShape::
-operator = (const BulletCylinderShape &copy) {
-  LightMutexHolder holder(BulletWorld::get_global_lock());
+  btVector3 btHalfExtents = LVecBase3_to_btVector3(_half_extents);
 
-  _shape = copy._shape;
-  _half_extents = copy._half_extents;
+  switch (_up) {
+  case X_up:
+    _shape = new btCylinderShapeX(btHalfExtents);
+    break;
+  case Y_up:
+    _shape = new btCylinderShape(btHalfExtents);
+    break;
+  case Z_up:
+    _shape = new btCylinderShapeZ(btHalfExtents);
+    break;
+  default:
+    bullet_cat.error() << "invalid up-axis:" << _up << endl;
+    break;
+  }
+
+  nassertv(_shape);
+  _shape->setUserPointer(this);
 }
 
 /**
@@ -185,11 +196,11 @@ fillin(DatagramIterator &scan, BamReader *manager) {
 
   // parameters to serialize: radius, height, up
   _half_extents.read_datagram(scan);
-  int up = (int) scan.get_int8();
+  _up = (BulletUpAxis) scan.get_int8();
 
   btVector3 btHalfExtents = LVecBase3_to_btVector3(_half_extents);
 
-  switch (up) {
+  switch (_up) {
   case X_up:
     _shape = new btCylinderShapeX(btHalfExtents);
     break;
@@ -200,7 +211,7 @@ fillin(DatagramIterator &scan, BamReader *manager) {
     _shape = new btCylinderShapeZ(btHalfExtents);
     break;
   default:
-    bullet_cat.error() << "invalid up-axis:" << up << endl;
+    bullet_cat.error() << "invalid up-axis:" << _up << endl;
     break;
   }
 

+ 1 - 1
panda/src/bullet/bulletCylinderShape.h

@@ -32,7 +32,6 @@ PUBLISHED:
   explicit BulletCylinderShape(PN_stdfloat radius, PN_stdfloat height, BulletUpAxis up=Z_up);
   explicit BulletCylinderShape(const LVector3 &half_extents, BulletUpAxis up=Z_up);
   BulletCylinderShape(const BulletCylinderShape &copy);
-  void operator = (const BulletCylinderShape &copy);
   INLINE ~BulletCylinderShape();
 
   PN_stdfloat get_radius() const;
@@ -49,6 +48,7 @@ public:
 private:
   LVector3 _half_extents;
   btCylinderShape *_shape;
+  BulletUpAxis _up;
 
 public:
   static void register_with_read_factory();

+ 7 - 18
panda/src/bullet/bulletHeightfieldShape.cxx

@@ -112,7 +112,6 @@ BulletHeightfieldShape::
 BulletHeightfieldShape(const BulletHeightfieldShape &copy) {
   LightMutexHolder holder(BulletWorld::get_global_lock());
 
-  _shape = copy._shape;
   _num_rows = copy._num_rows;
   _num_cols = copy._num_cols;
   _max_height = copy._max_height;
@@ -121,24 +120,14 @@ BulletHeightfieldShape(const BulletHeightfieldShape &copy) {
   size_t size = (size_t)_num_rows * (size_t)_num_cols;
   _data = new btScalar[size];
   memcpy(_data, copy._data, size * sizeof(btScalar));
-}
-
-/**
- *
- */
-void BulletHeightfieldShape::
-operator = (const BulletHeightfieldShape &copy) {
-  LightMutexHolder holder(BulletWorld::get_global_lock());
-
-  _shape = copy._shape;
-  _num_rows = copy._num_rows;
-  _num_cols = copy._num_cols;
-  _max_height = copy._max_height;
-  _up = copy._up;
 
-  size_t size = (size_t)_num_rows * (size_t)_num_cols;
-  _data = new btScalar[size];
-  memcpy(_data, copy._data, size * sizeof(btScalar));
+  _shape = new btHeightfieldTerrainShape(_num_rows,
+                                         _num_cols,
+                                         _data,
+                                         _max_height,
+                                         _up,
+                                         true, false);
+  _shape->setUserPointer(this);
 }
 
 /**

+ 0 - 1
panda/src/bullet/bulletHeightfieldShape.h

@@ -35,7 +35,6 @@ PUBLISHED:
   explicit BulletHeightfieldShape(const PNMImage &image, PN_stdfloat max_height, BulletUpAxis up=Z_up);
   explicit BulletHeightfieldShape(Texture *tex, PN_stdfloat max_height, BulletUpAxis up=Z_up);
   BulletHeightfieldShape(const BulletHeightfieldShape &copy);
-  void operator = (const BulletHeightfieldShape &copy);
   INLINE ~BulletHeightfieldShape();
 
   void set_use_diamond_subdivision(bool flag=true);

+ 5 - 12
panda/src/bullet/bulletMinkowskiSumShape.cxx

@@ -40,21 +40,14 @@ BulletMinkowskiSumShape::
 BulletMinkowskiSumShape(const BulletMinkowskiSumShape &copy) {
   LightMutexHolder holder(BulletWorld::get_global_lock());
 
-  _shape = copy._shape;
   _shape_a = copy._shape_a;
   _shape_b = copy._shape_b;
-}
-
-/**
- *
- */
-void BulletMinkowskiSumShape::
-operator = (const BulletMinkowskiSumShape &copy) {
-  LightMutexHolder holder(BulletWorld::get_global_lock());
+  
+  const btConvexShape *ptr_a = (const btConvexShape *)_shape_a->ptr();
+  const btConvexShape *ptr_b = (const btConvexShape *)_shape_b->ptr();
 
-  _shape = copy._shape;
-  _shape_a = copy._shape_a;
-  _shape_b = copy._shape_b;
+  _shape = new btMinkowskiSumShape(ptr_a, ptr_b);
+  _shape->setUserPointer(this);
 }
 
 /**

+ 0 - 1
panda/src/bullet/bulletMinkowskiSumShape.h

@@ -33,7 +33,6 @@ private:
 PUBLISHED:
   explicit BulletMinkowskiSumShape(const BulletShape *shape_a, const BulletShape *shape_b);
   BulletMinkowskiSumShape(const BulletMinkowskiSumShape &copy);
-  void operator = (const BulletMinkowskiSumShape &copy);
   INLINE ~BulletMinkowskiSumShape();
 
   void set_transform_a(const TransformState *ts);

+ 4 - 10
panda/src/bullet/bulletPlaneShape.cxx

@@ -34,17 +34,11 @@ BulletPlaneShape::
 BulletPlaneShape(const BulletPlaneShape &copy) {
   LightMutexHolder holder(BulletWorld::get_global_lock());
 
-  _shape = copy._shape;
-}
+  btVector3 btNormal = copy._shape->getPlaneNormal();
+  PN_stdfloat constant = (PN_stdfloat)_shape->getPlaneConstant();
 
-/**
- *
- */
-void BulletPlaneShape::
-operator = (const BulletPlaneShape &copy) {
-  LightMutexHolder holder(BulletWorld::get_global_lock());
-
-  _shape = copy._shape;
+  _shape = new btStaticPlaneShape(btNormal, constant);
+  _shape->setUserPointer(this);
 }
 
 /**

+ 0 - 1
panda/src/bullet/bulletPlaneShape.h

@@ -34,7 +34,6 @@ private:
 PUBLISHED:
   explicit BulletPlaneShape(const LVector3 &normal, PN_stdfloat constant);
   BulletPlaneShape(const BulletPlaneShape &copy);
-  void operator = (const BulletPlaneShape &copy);
   INLINE ~BulletPlaneShape();
 
   LVector3 get_plane_normal() const;

+ 2 - 11
panda/src/bullet/bulletSphereShape.cxx

@@ -32,19 +32,10 @@ BulletSphereShape::
 BulletSphereShape(const BulletSphereShape &copy) {
   LightMutexHolder holder(BulletWorld::get_global_lock());
 
-  _shape = copy._shape;
   _radius = copy._radius;
-}
-
-/**
- *
- */
-void BulletSphereShape::
-operator = (const BulletSphereShape &copy) {
-  LightMutexHolder holder(BulletWorld::get_global_lock());
 
-  _shape = copy._shape;
-  _radius = copy._radius;
+  _shape = new btSphereShape(_radius);
+  _shape->setUserPointer(this);
 }
 
 

+ 0 - 1
panda/src/bullet/bulletSphereShape.h

@@ -33,7 +33,6 @@ private:
 PUBLISHED:
   explicit BulletSphereShape(PN_stdfloat radius);
   BulletSphereShape(const BulletSphereShape &copy);
-  void operator = (const BulletSphereShape &copy);
   INLINE ~BulletSphereShape();
 
   INLINE PN_stdfloat get_radius() const;

+ 14 - 14
panda/src/bullet/bulletTriangleMeshShape.cxx

@@ -86,21 +86,21 @@ BulletTriangleMeshShape::
 BulletTriangleMeshShape(const BulletTriangleMeshShape &copy) {
   LightMutexHolder holder(BulletWorld::get_global_lock());
 
-  _bvh_shape = copy._bvh_shape;
-  _gimpact_shape = copy._gimpact_shape;
-  _mesh = copy._mesh;
-}
-
-/**
- *
- */
-void BulletTriangleMeshShape::
-operator = (const BulletTriangleMeshShape &copy) {
-  LightMutexHolder holder(BulletWorld::get_global_lock());
-
-  _bvh_shape = copy._bvh_shape;
-  _gimpact_shape = copy._gimpact_shape;
+  _dynamic = copy._dynamic;
+  _compress = copy._compress;
+  _bvh = copy._bvh;
   _mesh = copy._mesh;
+  
+  if (_dynamic) {
+    _gimpact_shape = new btGImpactMeshShape(_mesh->ptr());
+    _gimpact_shape->updateBound();
+    _gimpact_shape->setUserPointer(this);
+    _bvh_shape = NULL;
+  } else {
+    _bvh_shape = new btBvhTriangleMeshShape(_mesh->ptr(), _compress, _bvh);
+    _bvh_shape->setUserPointer(this);
+    _gimpact_shape = NULL;
+  }
 }
 
 /**

+ 0 - 1
panda/src/bullet/bulletTriangleMeshShape.h

@@ -33,7 +33,6 @@ private:
 PUBLISHED:
   explicit BulletTriangleMeshShape(BulletTriangleMesh *mesh, bool dynamic, bool compress=true, bool bvh=true);
   BulletTriangleMeshShape(const BulletTriangleMeshShape &copy);
-  void operator = (const BulletTriangleMeshShape &copy);
   INLINE ~BulletTriangleMeshShape();
 
   void refit_tree(const LPoint3 &aabb_min, const LPoint3 &aabb_max);