geometric_shapes.cpp 5.1 KB
Newer Older
sachinc's avatar
sachinc committed
1
2
3
/*
 * Software License Agreement (BSD License)
 *
4
5
 *  Copyright (c) 2011-2014, Willow Garage, Inc.
 *  Copyright (c) 2014-2015, Open Source Robotics Foundation
sachinc's avatar
sachinc committed
6
7
8
9
10
11
12
13
14
15
16
17
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
18
 *   * Neither the name of Open Source Robotics Foundation nor the names of its
sachinc's avatar
sachinc committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */

/** \author Jia Pan */


39
#include <hpp/fcl/shape/geometric_shapes.h>
Lucile Remigy's avatar
Lucile Remigy committed
40
#include <hpp/fcl/shape/geometric_shapes_utility.h>
sachinc's avatar
sachinc committed
41

42
43
namespace hpp
{
sachinc's avatar
sachinc committed
44
45
46
namespace fcl
{

47
void ConvexBase::initialize(bool own_storage, Vec3f* points_, int num_points_)
sachinc's avatar
sachinc committed
48
{
49
50
51
  points       = points_;
  num_points   = num_points_;
  own_storage_ = own_storage;
52
53
  computeCenter();
}
sachinc's avatar
sachinc committed
54

55
56
57
58
ConvexBase::ConvexBase(const ConvexBase& other) :
  ShapeBase    (other),
  points       (other.points),
  num_points   (other.num_points),
59
60
  center       (other.center),
  own_storage_ (other.own_storage_)
61
{
62
63
  if (neighbors) delete [] neighbors;
  if (nneighbors_) delete [] nneighbors_;
64
  if (own_storage_) {
65
66
    if (own_storage_ && points) delete [] points;

67
68
69
70
    points = new Vec3f[num_points];
    memcpy(points, other.points, sizeof(Vec3f) * num_points);
  }

71
  neighbors = new Neighbors[num_points];
72
  memcpy(neighbors, other.neighbors, sizeof(Neighbors) * num_points);
sachinc's avatar
sachinc committed
73

74
75
76
77
78
  int c_nneighbors = 0;
  for (int i = 0; i < num_points; ++i) c_nneighbors += neighbors[i].count();
  nneighbors_ = new unsigned int[c_nneighbors];
  memcpy(nneighbors_, other.nneighbors_, sizeof(unsigned int) * c_nneighbors);
}
sachinc's avatar
sachinc committed
79

80
81
ConvexBase::~ConvexBase ()
{
82
83
84
  if (neighbors) delete [] neighbors;
  if (nneighbors_) delete [] nneighbors_;
  if (own_storage_ && points) delete [] points;
85
}
sachinc's avatar
sachinc committed
86

87
88
89
90
91
92
void ConvexBase::computeCenter()
{
  center.setZero();
  for(int i = 0; i < num_points; ++i)
    center += points[i];
  center /= num_points;
sachinc's avatar
sachinc committed
93
94
}

95
96
void Halfspace::unitNormalTest()
{
97
  FCL_REAL l = n.norm();
98
99
100
101
102
103
104
105
  if(l > 0)
  {
    FCL_REAL inv_l = 1.0 / l;
    n *= inv_l;
    d *= inv_l;
  }
  else
  {
106
    n << 1, 0, 0;
107
108
109
110
    d = 0;
  }  
}

sachinc's avatar
sachinc committed
111
112
void Plane::unitNormalTest()
{
113
  FCL_REAL l = n.norm();
sachinc's avatar
sachinc committed
114
115
  if(l > 0)
  {
116
    FCL_REAL inv_l = 1.0 / l;
117
    n *= inv_l;
sachinc's avatar
sachinc committed
118
119
120
121
    d *= inv_l;
  }
  else
  {
122
    n << 1, 0, 0;
sachinc's avatar
sachinc committed
123
124
125
126
    d = 0;
  }
}

127
128
void Box::computeLocalAABB()
{
129
  computeBV<AABB>(*this, Transform3f(), aabb_local);
jpan's avatar
jpan committed
130
  aabb_center = aabb_local.center();
131
  aabb_radius = (aabb_local.min_ - aabb_center).norm();
132
133
134
135
}

void Sphere::computeLocalAABB()
{
136
  computeBV<AABB>(*this, Transform3f(), aabb_local);
jpan's avatar
jpan committed
137
  aabb_center = aabb_local.center();
138
139
140
141
142
  aabb_radius = radius;
}

void Capsule::computeLocalAABB()
{
143
  computeBV<AABB>(*this, Transform3f(), aabb_local);
jpan's avatar
jpan committed
144
  aabb_center = aabb_local.center();
145
  aabb_radius = (aabb_local.min_ - aabb_center).norm();
146
147
148
149
}

void Cone::computeLocalAABB()
{
150
  computeBV<AABB>(*this, Transform3f(), aabb_local);
jpan's avatar
jpan committed
151
  aabb_center = aabb_local.center();
152
  aabb_radius = (aabb_local.min_ - aabb_center).norm();
153
154
155
156
}

void Cylinder::computeLocalAABB()
{
157
  computeBV<AABB>(*this, Transform3f(), aabb_local);
jpan's avatar
jpan committed
158
  aabb_center = aabb_local.center();
159
  aabb_radius = (aabb_local.min_ - aabb_center).norm();
160
161
}

162
void ConvexBase::computeLocalAABB()
163
{
164
  computeBV<AABB>(*this, Transform3f(), aabb_local);
jpan's avatar
jpan committed
165
  aabb_center = aabb_local.center();
166
  aabb_radius = (aabb_local.min_ - aabb_center).norm();
167
168
}

169
170
171
172
void Halfspace::computeLocalAABB()
{
  computeBV<AABB>(*this, Transform3f(), aabb_local);
  aabb_center = aabb_local.center();
173
  aabb_radius = (aabb_local.min_ - aabb_center).norm();
174
175
}

176
177
void Plane::computeLocalAABB()
{
178
  computeBV<AABB>(*this, Transform3f(), aabb_local);
jpan's avatar
jpan committed
179
  aabb_center = aabb_local.center();
180
  aabb_radius = (aabb_local.min_ - aabb_center).norm();
181
182
}

183
void TriangleP::computeLocalAABB()
184
{
185
  computeBV<AABB>(*this, Transform3f(), aabb_local);
jpan's avatar
jpan committed
186
  aabb_center = aabb_local.center();
187
  aabb_radius = (aabb_local.min_ - aabb_center).norm();
188
189
190
}


sachinc's avatar
sachinc committed
191
}
192
193

} // namespace hpp