Skip to content

Commit 7d38e90

Browse files
Limit the use of new operator in tesseract_collision
1 parent aa06be2 commit 7d38e90

File tree

11 files changed

+100
-130
lines changed

11 files changed

+100
-130
lines changed

tesseract/tesseract_collision/examples/box_box_example.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ int main(int /*argc*/, char** /*argv*/)
1919
tesseract_collision_bullet::BulletDiscreteBVHManager checker;
2020

2121
// Add box to checker
22-
CollisionShapePtr box(new Box(1, 1, 1));
22+
CollisionShapePtr box = std::make_shared<Box>(1, 1, 1);
2323
Eigen::Isometry3d box_pose;
2424
box_pose.setIdentity();
2525

@@ -31,7 +31,7 @@ int main(int /*argc*/, char** /*argv*/)
3131
checker.addCollisionObject("box_link", 0, obj1_shapes, obj1_poses);
3232

3333
// Add thin box to checker which is disabled
34-
CollisionShapePtr thin_box(new Box(0.1, 1, 1));
34+
CollisionShapePtr thin_box = std::make_shared<Box>(0.1, 1, 1);
3535
Eigen::Isometry3d thin_box_pose;
3636
thin_box_pose.setIdentity();
3737

@@ -50,10 +50,10 @@ int main(int /*argc*/, char** /*argv*/)
5050
loadSimplePlyFile(std::string(DATA_DIR) + "/box_2m.ply", mesh_vertices, mesh_faces);
5151

5252
// This is required because convex hull cannot have multiple faces on the same plane.
53-
std::shared_ptr<tesseract_common::VectorVector3d> ch_verticies(new tesseract_common::VectorVector3d());
54-
std::shared_ptr<Eigen::VectorXi> ch_faces(new Eigen::VectorXi());
53+
auto ch_verticies = std::make_shared<tesseract_common::VectorVector3d>();
54+
auto ch_faces = std::make_shared<Eigen::VectorXi>();
5555
int ch_num_faces = createConvexHull(*ch_verticies, *ch_faces, mesh_vertices);
56-
second_box.reset(new ConvexMesh(ch_verticies, ch_faces, ch_num_faces));
56+
second_box = std::make_shared<ConvexMesh>(ch_verticies, ch_faces, ch_num_faces);
5757

5858
Eigen::Isometry3d second_box_pose;
5959
second_box_pose.setIdentity();

tesseract/tesseract_collision/include/tesseract_collision/bullet/bullet_utils.h

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,12 @@ class CollisionObjectWrapper : public btCollisionObject
132132
using Ptr = std::shared_ptr<CollisionObjectWrapper>;
133133
using ConstPtr = std::shared_ptr<const CollisionObjectWrapper>;
134134

135+
CollisionObjectWrapper() = default;
135136
CollisionObjectWrapper(std::string name,
136137
const int& type_id,
137138
CollisionShapesConst shapes,
138139
tesseract_common::VectorIsometry3d shape_poses);
139140

140-
/** @brief This is a special constructor used by the clone method */
141-
CollisionObjectWrapper(std::string name,
142-
const int& type_id,
143-
CollisionShapesConst shapes,
144-
tesseract_common::VectorIsometry3d shape_poses,
145-
std::vector<std::shared_ptr<void>> data);
146-
147141
short int m_collisionFilterGroup;
148142
short int m_collisionFilterMask;
149143
bool m_enabled;
@@ -188,7 +182,12 @@ class CollisionObjectWrapper : public btCollisionObject
188182
*/
189183
std::shared_ptr<CollisionObjectWrapper> clone()
190184
{
191-
auto clone_cow = std::make_shared<CollisionObjectWrapper>(m_name, m_type_id, m_shapes, m_shape_poses, m_data);
185+
auto clone_cow = std::make_shared<CollisionObjectWrapper>();
186+
clone_cow->m_name = m_name;
187+
clone_cow->m_type_id = m_type_id;
188+
clone_cow->m_shapes = m_shapes;
189+
clone_cow->m_shape_poses = m_shape_poses;
190+
clone_cow->m_data = m_data;
192191
clone_cow->setCollisionShape(getCollisionShape());
193192
clone_cow->setWorldTransform(getWorldTransform());
194193
clone_cow->m_collisionFilterGroup = m_collisionFilterGroup;
@@ -198,25 +197,16 @@ class CollisionObjectWrapper : public btCollisionObject
198197
return clone_cow;
199198
}
200199

201-
template <class T>
202-
void manage(T* t)
203-
{ // manage memory of this object
204-
m_data.push_back(std::shared_ptr<T>(t));
205-
}
206-
template <class T>
207-
void manage(std::shared_ptr<T> t)
208-
{
209-
m_data.push_back(t);
210-
}
200+
void manage(const std::shared_ptr<btCollisionShape>& t) { m_data.push_back(t); }
211201

212202
protected:
213203
std::string m_name; /**< @brief The name of the collision object */
214204
int m_type_id; /**< @brief A user defined type id */
215205
CollisionShapesConst m_shapes; /**< @brief The shapes that define the collison object */
216206
tesseract_common::VectorIsometry3d m_shape_poses; /**< @brief The shpaes poses information */
217207

218-
std::vector<std::shared_ptr<void>> m_data; /**< @brief This manages the collision shape pointer so they get destroyed
219-
*/
208+
/** @brief This manages the collision shape pointer so they get destroyed */
209+
std::vector<std::shared_ptr<btCollisionShape>> m_data;
220210
};
221211

222212
using COW = CollisionObjectWrapper;
@@ -898,9 +888,9 @@ class TesseractOverlapFilterCallback : public btOverlapFilterCallback
898888
* bullet collision shape by calling getUserIndex function.
899889
* @return Bullet collision shape.
900890
*/
901-
btCollisionShape* createShapePrimitive(const CollisionShapeConstPtr& geom,
902-
CollisionObjectWrapper* cow,
903-
int shape_index);
891+
std::shared_ptr<btCollisionShape> createShapePrimitive(const CollisionShapeConstPtr& geom,
892+
CollisionObjectWrapper* cow,
893+
int shape_index);
904894

905895
/**
906896
* @brief Update a collision objects filters
@@ -939,7 +929,7 @@ inline COW::Ptr createCollisionObject(const std::string& name,
939929
return nullptr;
940930
}
941931

942-
COW::Ptr new_cow(new COW(name, type_id, shapes, shape_poses));
932+
auto new_cow = std::make_shared<COW>(name, type_id, shapes, shape_poses);
943933

944934
new_cow->m_enabled = enabled;
945935
new_cow->setContactProcessingThreshold(BULLET_DEFAULT_CONTACT_DISTANCE);
@@ -1036,17 +1026,18 @@ inline COW::Ptr makeCastCollisionObject(const COW::Ptr& cow)
10361026
assert(convex->getShapeType() != CUSTOM_CONVEX_SHAPE_TYPE); // This checks if the collision object is already a
10371027
// cast collision object
10381028

1039-
auto* shape = new CastHullShape(convex, tf);
1029+
auto shape = std::make_shared<CastHullShape>(convex, tf);
10401030
assert(shape != nullptr);
10411031

10421032
new_cow->manage(shape);
1043-
new_cow->setCollisionShape(shape);
1033+
new_cow->setCollisionShape(shape.get());
10441034
}
10451035
else if (btBroadphaseProxy::isCompound(new_cow->getCollisionShape()->getShapeType()))
10461036
{
10471037
assert(dynamic_cast<btCompoundShape*>(new_cow->getCollisionShape()) != nullptr);
10481038
auto* compound = static_cast<btCompoundShape*>(new_cow->getCollisionShape());
1049-
auto* new_compound = new btCompoundShape(BULLET_COMPOUND_USE_DYNAMIC_AABB, compound->getNumChildShapes());
1039+
auto new_compound =
1040+
std::make_shared<btCompoundShape>(BULLET_COMPOUND_USE_DYNAMIC_AABB, compound->getNumChildShapes());
10501041

10511042
for (int i = 0; i < compound->getNumChildShapes(); ++i)
10521043
{
@@ -1057,18 +1048,18 @@ inline COW::Ptr makeCastCollisionObject(const COW::Ptr& cow)
10571048

10581049
btTransform geomTrans = compound->getChildTransform(i);
10591050

1060-
auto* subshape = new CastHullShape(convex, tf);
1051+
auto subshape = std::make_shared<CastHullShape>(convex, tf);
10611052
assert(subshape != nullptr);
10621053

10631054
new_cow->manage(subshape);
10641055
subshape->setMargin(BULLET_MARGIN);
1065-
new_compound->addChildShape(geomTrans, subshape);
1056+
new_compound->addChildShape(geomTrans, subshape.get());
10661057
}
10671058
else if (btBroadphaseProxy::isCompound(compound->getChildShape(i)->getShapeType()))
10681059
{
10691060
auto* second_compound = static_cast<btCompoundShape*>(compound->getChildShape(i));
1070-
auto* new_second_compound =
1071-
new btCompoundShape(BULLET_COMPOUND_USE_DYNAMIC_AABB, second_compound->getNumChildShapes());
1061+
auto new_second_compound =
1062+
std::make_shared<btCompoundShape>(BULLET_COMPOUND_USE_DYNAMIC_AABB, second_compound->getNumChildShapes());
10721063
for (int j = 0; j < second_compound->getNumChildShapes(); ++j)
10731064
{
10741065
assert(!btBroadphaseProxy::isCompound(second_compound->getChildShape(j)->getShapeType()));
@@ -1079,12 +1070,12 @@ inline COW::Ptr makeCastCollisionObject(const COW::Ptr& cow)
10791070

10801071
btTransform geomTrans = second_compound->getChildTransform(j);
10811072

1082-
auto* subshape = new CastHullShape(convex, tf);
1073+
auto subshape = std::make_shared<CastHullShape>(convex, tf);
10831074
assert(subshape != nullptr);
10841075

10851076
new_cow->manage(subshape);
10861077
subshape->setMargin(BULLET_MARGIN);
1087-
new_second_compound->addChildShape(geomTrans, subshape);
1078+
new_second_compound->addChildShape(geomTrans, subshape.get());
10881079
}
10891080

10901081
btTransform geomTrans = compound->getChildTransform(i);
@@ -1095,7 +1086,7 @@ inline COW::Ptr makeCastCollisionObject(const COW::Ptr& cow)
10951086
// but has an effect when
10961087
// negative
10971088

1098-
new_compound->addChildShape(geomTrans, new_second_compound);
1089+
new_compound->addChildShape(geomTrans, new_second_compound.get());
10991090
}
11001091
else
11011092
{
@@ -1108,7 +1099,7 @@ inline COW::Ptr makeCastCollisionObject(const COW::Ptr& cow)
11081099
// but has an effect when
11091100
// negative
11101101
new_cow->manage(new_compound);
1111-
new_cow->setCollisionShape(new_compound);
1102+
new_cow->setCollisionShape(new_compound.get());
11121103
new_cow->setWorldTransform(cow->getWorldTransform());
11131104
}
11141105
else

tesseract/tesseract_collision/include/tesseract_collision/fcl/fcl_utils.h

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class CollisionObjectWrapper
8484
using Ptr = std::shared_ptr<CollisionObjectWrapper>;
8585
using ConstPtr = std::shared_ptr<const CollisionObjectWrapper>;
8686

87+
CollisionObjectWrapper() = default;
8788
CollisionObjectWrapper(std::string name,
8889
const int& type_id,
8990
CollisionShapesConst shapes,
@@ -137,8 +138,25 @@ class CollisionObjectWrapper
137138
std::vector<CollisionObjectRawPtr>& getCollisionObjectsRaw() { return collision_objects_raw_; }
138139
std::shared_ptr<CollisionObjectWrapper> clone() const
139140
{
140-
CollisionObjectWrapper::Ptr clone_cow(
141-
new CollisionObjectWrapper(name_, type_id_, shapes_, shape_poses_, collision_geometries_, collision_objects_));
141+
auto clone_cow = std::make_shared<CollisionObjectWrapper>();
142+
clone_cow->name_ = name_;
143+
clone_cow->type_id_ = type_id_;
144+
clone_cow->shapes_ = shapes_;
145+
clone_cow->shape_poses_ = shape_poses_;
146+
clone_cow->collision_geometries_ = collision_geometries_;
147+
148+
clone_cow->collision_objects_.reserve(collision_objects_.size());
149+
clone_cow->collision_objects_raw_.reserve(collision_objects_.size());
150+
for (const auto& co : collision_objects_)
151+
{
152+
auto collObj = std::make_shared<FCLCollisionObjectWrapper>(*co);
153+
collObj->setUserData(clone_cow.get());
154+
collObj->setTransform(co->getTransform());
155+
collObj->updateAABB();
156+
clone_cow->collision_objects_.push_back(collObj);
157+
clone_cow->collision_objects_raw_.push_back(collObj.get());
158+
}
159+
142160
clone_cow->m_collisionFilterGroup = m_collisionFilterGroup;
143161
clone_cow->m_collisionFilterMask = m_collisionFilterMask;
144162
clone_cow->m_enabled = m_enabled;
@@ -153,13 +171,6 @@ class CollisionObjectWrapper
153171
int getShapeIndex(const fcl::CollisionObjectd* co) const;
154172

155173
protected:
156-
CollisionObjectWrapper(std::string name,
157-
const int& type_id,
158-
CollisionShapesConst shapes,
159-
tesseract_common::VectorIsometry3d shape_poses,
160-
std::vector<CollisionGeometryPtr> collision_geometries,
161-
const std::vector<CollisionObjectPtr>& collision_objects);
162-
163174
std::string name_; // name of the collision object
164175
int type_id_; // user defined type id
165176
Eigen::Isometry3d world_pose_; /**< @brief Collision Object World Transformation */

tesseract/tesseract_collision/src/bullet/bullet_cast_bvh_manager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ BulletCastBVHManager::~BulletCastBVHManager()
7979

8080
ContinuousContactManager::Ptr BulletCastBVHManager::clone() const
8181
{
82-
BulletCastBVHManager::Ptr manager(new BulletCastBVHManager());
82+
auto manager = std::make_shared<BulletCastBVHManager>();
8383

8484
for (const auto& cow : link2cow_)
8585
{

tesseract/tesseract_collision/src/bullet/bullet_cast_simple_manager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ BulletCastSimpleManager::BulletCastSimpleManager()
6464

6565
ContinuousContactManager::Ptr BulletCastSimpleManager::clone() const
6666
{
67-
BulletCastSimpleManager::Ptr manager(new BulletCastSimpleManager());
67+
auto manager = std::make_shared<BulletCastSimpleManager>();
6868

6969
for (const auto& cow : link2cow_)
7070
{

tesseract/tesseract_collision/src/bullet/bullet_discrete_bvh_manager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ BulletDiscreteBVHManager::~BulletDiscreteBVHManager()
7575

7676
DiscreteContactManager::Ptr BulletDiscreteBVHManager::clone() const
7777
{
78-
BulletDiscreteBVHManager::Ptr manager(new BulletDiscreteBVHManager());
78+
auto manager = std::make_shared<BulletDiscreteBVHManager>();
7979

8080
for (const auto& cow : link2cow_)
8181
{

tesseract/tesseract_collision/src/bullet/bullet_discrete_simple_manager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ BulletDiscreteSimpleManager::BulletDiscreteSimpleManager()
6565

6666
DiscreteContactManager::Ptr BulletDiscreteSimpleManager::clone() const
6767
{
68-
BulletDiscreteSimpleManager::Ptr manager(new BulletDiscreteSimpleManager());
68+
auto manager = std::make_shared<BulletDiscreteSimpleManager>();
6969

7070
for (const auto& cow : link2cow_)
7171
{

0 commit comments

Comments
 (0)