BulletBoxShape.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2015 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 /* Desc: Box shape
18  * Author: Nate Koenig
19  * Date: 14 Oct 2009
20  */
21 
22 #ifndef _BULLETBOXSHAPE_HH_
23 #define _BULLETBOXSHAPE_HH_
24 
29 #include "gazebo/physics/World.hh"
31 #include "gazebo/util/system.hh"
32 
33 namespace gazebo
34 {
35  namespace physics
36  {
40 
43  {
45  public: BulletBoxShape(CollisionPtr _parent) : BoxShape(_parent) {}
46 
48  public: virtual ~BulletBoxShape() {}
49 
51  public: void SetSize(const math::Vector3 &_size)
52  {
53  if (_size.x < 0 || _size.y < 0 || _size.z < 0)
54  {
55  gzerr << "Box shape does not support negative size\n";
56  return;
57  }
58  math::Vector3 size = _size;
59  if (math::equal(size.x, 0.0))
60  {
61  // Warn user, but still create shape with very small value
62  // otherwise later resize operations using setLocalScaling
63  // will not be possible
64  gzwarn << "Setting box shape's x to zero \n";
65  size.x = 1e-4;
66  }
67  if (math::equal(size.y, 0.0))
68  {
69  gzwarn << "Setting box shape's y to zero \n";
70  size.y = 1e-4;
71  }
72  if (math::equal(size.z, 0.0))
73  {
74  gzwarn << "Setting box shape's z to zero \n";
75  size.z = 1e-4;
76  }
77 
78  BoxShape::SetSize(size);
79  BulletCollisionPtr bParent;
80  bParent = boost::dynamic_pointer_cast<BulletCollision>(
81  this->collisionParent);
82 
84  btCollisionShape *shape = bParent->GetCollisionShape();
85  if (!shape)
86  {
87  this->initialSize = size;
88  bParent->SetCollisionShape(new btBoxShape(
89  btVector3(size.x*0.5, size.y*0.5, size.z*0.5)));
90  }
91  else
92  {
93  btVector3 boxScale = shape->getLocalScaling();
94  boxScale.setX(size.x / this->initialSize.x);
95  boxScale.setY(size.y / this->initialSize.y);
96  boxScale.setZ(size.z / this->initialSize.z);
97 
98  shape->setLocalScaling(boxScale);
99 
100  // clear bullet cache and re-add the collision shape
101  // otherwise collisions won't work properly after scaling
102  BulletLinkPtr bLink =
103  boost::dynamic_pointer_cast<BulletLink>(
104  bParent->GetLink());
105  bLink->ClearCollisionCache();
106 
107  // remove and add the shape again
108  if (bLink->GetBulletLink()->getCollisionShape()->isCompound())
109  {
110  btCompoundShape *compoundShape =
111  dynamic_cast<btCompoundShape *>(
112  bLink->GetBulletLink()->getCollisionShape());
113 
114  compoundShape->removeChildShape(shape);
115  math::Pose relativePose =
116  this->collisionParent->GetRelativePose();
117  relativePose.pos -= bLink->GetInertial()->GetCoG();
118  compoundShape->addChildShape(
119  BulletTypes::ConvertPose(relativePose), shape);
120  }
121  }
122  }
123 
125  private: math::Vector3 initialSize;
126  };
128  }
129 }
130 #endif
Bullet collisions.
Definition: BulletCollision.hh:53
boost::shared_ptr< BulletLink > BulletLinkPtr
Definition: BulletTypes.hh:44
#define GZ_PHYSICS_VISIBLE
Definition: system.hh:318
virtual ~BulletBoxShape()
Destructor.
Definition: BulletBoxShape.hh:48
Encapsulates a position and rotation in three space.
Definition: Pose.hh:37
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:39
btCollisionShape * GetCollisionShape() const
Get the bullet collision shape.
virtual void SetSize(const math::Vector3 &_size)
Set the size of the box.
double x
X location.
Definition: Vector3.hh:311
#define gzwarn
Output a warning message.
Definition: Console.hh:46
double z
Z location.
Definition: Vector3.hh:317
#define gzerr
Output an error message.
Definition: Console.hh:49
bool equal(const T &_a, const T &_b, const T &_epsilon=1e-6)
check if two values are equal, within a tolerance
Definition: Helpers.hh:171
boost::shared_ptr< Collision > CollisionPtr
Definition: PhysicsTypes.hh:96
Bullet wrapper forward declarations and typedefs.
void SetSize(const math::Vector3 &_size)
Set the size of the box.
Definition: BulletBoxShape.hh:51
Box geometry primitive.
Definition: BoxShape.hh:37
static math::Pose ConvertPose(const btTransform &_bt)
Convert a bullet transform to a gazebo pose.
Definition: BulletTypes.hh:93
Vector3 pos
The position.
Definition: Pose.hh:252
boost::shared_ptr< BulletCollision > BulletCollisionPtr
Definition: BulletTypes.hh:41
Bullet box collision.
Definition: BulletBoxShape.hh:42
BulletBoxShape(CollisionPtr _parent)
Constructor.
Definition: BulletBoxShape.hh:45
double y
Y location.
Definition: Vector3.hh:314