BulletBoxShape.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef GAZEBO_PHYSICS_BULLET_BULLETBOXSHAPE_HH_
18 #define GAZEBO_PHYSICS_BULLET_BULLETBOXSHAPE_HH_
19 
24 #include "gazebo/physics/World.hh"
26 #include "gazebo/util/system.hh"
27 
28 namespace gazebo
29 {
30  namespace physics
31  {
35 
37  class GZ_PHYSICS_VISIBLE BulletBoxShape : public BoxShape
38  {
40  public: BulletBoxShape(CollisionPtr _parent) : BoxShape(_parent) {}
41 
43  public: virtual ~BulletBoxShape() {}
44 
46  public: void SetSize(const ignition::math::Vector3d &_size)
47  {
48  if (_size.X() < 0 || _size.Y() < 0 || _size.Z() < 0)
49  {
50  gzerr << "Box shape does not support negative size\n";
51  return;
52  }
53  ignition::math::Vector3d size = _size;
54  if (ignition::math::equal(size.X(), 0.0))
55  {
56  // Warn user, but still create shape with very small value
57  // otherwise later resize operations using setLocalScaling
58  // will not be possible
59  gzwarn << "Setting box shape's x to zero \n";
60  size.X() = 1e-4;
61  }
62  if (ignition::math::equal(size.Y(), 0.0))
63  {
64  gzwarn << "Setting box shape's y to zero \n";
65  size.Y() = 1e-4;
66  }
67  if (ignition::math::equal(size.Z(), 0.0))
68  {
69  gzwarn << "Setting box shape's z to zero \n";
70  size.Z() = 1e-4;
71  }
72 
73  BoxShape::SetSize(size);
74  BulletCollisionPtr bParent;
75  bParent = boost::dynamic_pointer_cast<BulletCollision>(
76  this->collisionParent);
77 
79  btCollisionShape *shape = bParent->GetCollisionShape();
80  if (!shape)
81  {
82  this->initialSize = size;
83  bParent->SetCollisionShape(new btBoxShape(
84  btVector3(size.X()*0.5, size.Y()*0.5, size.Z()*0.5)));
85  }
86  else
87  {
88  btVector3 boxScale = shape->getLocalScaling();
89  boxScale.setX(size.X() / this->initialSize.X());
90  boxScale.setY(size.Y() / this->initialSize.Y());
91  boxScale.setZ(size.Z() / this->initialSize.Z());
92 
93  shape->setLocalScaling(boxScale);
94 
95  // clear bullet cache and re-add the collision shape
96  // otherwise collisions won't work properly after scaling
97  BulletLinkPtr bLink =
98  boost::dynamic_pointer_cast<BulletLink>(
99  bParent->GetLink());
100  bLink->ClearCollisionCache();
101 
102  // remove and add the shape again
103  if (bLink->GetBulletLink()->getCollisionShape()->isCompound())
104  {
105  btCompoundShape *compoundShape =
106  dynamic_cast<btCompoundShape *>(
107  bLink->GetBulletLink()->getCollisionShape());
108 
109  compoundShape->removeChildShape(shape);
110  ignition::math::Pose3d relativePose =
111  this->collisionParent->RelativePose()
112  - bLink->GetInertial()->Pose();
113  compoundShape->addChildShape(
114  BulletTypes::ConvertPose(relativePose), shape);
115  }
116  }
117  }
118 
120  private: ignition::math::Vector3d initialSize;
121  };
123  }
124 }
125 #endif
Bullet collisions.
Definition: BulletCollision.hh:43
BulletBoxShape(CollisionPtr _parent)
Constructor.
Definition: BulletBoxShape.hh:40
boost::shared_ptr< BulletCollision > BulletCollisionPtr
Definition: BulletTypes.hh:42
#define gzwarn
Output a warning message.
Definition: Console.hh:47
#define gzerr
Output an error message.
Definition: Console.hh:50
virtual void SetSize(const math::Vector3 &_size) GAZEBO_DEPRECATED(8.0)
Set the size of the box.
Bullet wrapper forward declarations and typedefs.
boost::shared_ptr< BulletLink > BulletLinkPtr
Definition: BulletTypes.hh:45
virtual ~BulletBoxShape()
Destructor.
Definition: BulletBoxShape.hh:43
void SetSize(const ignition::math::Vector3d &_size)
Set the size of the box.
Definition: BulletBoxShape.hh:46
Box geometry primitive.
Definition: BoxShape.hh:34
btCollisionShape * GetCollisionShape() const
Get the bullet collision shape.
static math::Pose ConvertPose(const btTransform &_bt) GAZEBO_DEPRECATED(8.0)
Convert a bullet transform to a gazebo pose.
Definition: BulletTypes.hh:183
Bullet box collision.
Definition: BulletBoxShape.hh:37
boost::shared_ptr< Collision > CollisionPtr
Definition: PhysicsTypes.hh:113