hpp-fcl  1.4.4
HPP fork of FCL -- The Flexible Collision Library
convex.hxx
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2019, CNRS - LAAS
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Open Source Robotics Foundation nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
38 #ifndef HPP_FCL_SHAPE_CONVEX_HXX
39 #define HPP_FCL_SHAPE_CONVEX_HXX
40 
41 #include <set>
42 #include <vector>
43 
44 namespace hpp
45 {
46 namespace fcl
47 {
48 
49 template <typename PolygonT>
50 Convex<PolygonT>::Convex(bool own_storage, Vec3f* points_, int num_points_,
51  PolygonT* polygons_, int num_polygons_) :
52  ConvexBase(own_storage, points_, num_points_),
53  polygons (polygons_),
54  num_polygons (num_polygons_)
55 {
56  fillNeighbors();
57 }
58 
59 template <typename PolygonT>
61  ConvexBase (other),
62  polygons (other.polygons),
64 {
65  if (own_storage_) {
66  polygons = new PolygonT[num_polygons];
67  memcpy(polygons, other.polygons, sizeof(PolygonT) * num_polygons);
68  }
69 }
70 
71 template <typename PolygonT>
73 {
74  if (own_storage_) delete [] polygons;
75 }
76 
77 template <typename PolygonT>
79 {
80  typedef typename PolygonT::size_type size_type;
81  typedef typename PolygonT::index_type index_type;
82 
83  Matrix3f C = Matrix3f::Zero();
84 
85  Matrix3f C_canonical;
86  C_canonical << 1/60.0, 1/120.0, 1/120.0,
87  1/120.0, 1/60.0, 1/120.0,
88  1/120.0, 1/120.0, 1/60.0;
89 
90  for(int i = 0; i < num_polygons; ++i)
91  {
92  const PolygonT& polygon (polygons[i]);
93 
94  // compute the center of the polygon
95  Vec3f plane_center(0,0,0);
96  for(size_type j = 0; j < polygon.size(); ++j)
97  plane_center += points[polygon[j]];
98  plane_center /= polygon.size();
99 
100  // compute the volume of tetrahedron making by neighboring two points, the plane center and the reference point (zero) of the convex shape
101  const Vec3f& v3 = plane_center;
102  for(size_type j = 0; j < polygon.size(); ++j)
103  {
104  index_type e_first = polygon[j];
105  index_type e_second = polygon[(j+1)%polygon.size()];
106  const Vec3f& v1 = points[e_first];
107  const Vec3f& v2 = points[e_second];
108  Matrix3f A; A << v1.transpose(), v2.transpose(), v3.transpose(); // this is A' in the original document
109  C += A.transpose() * C_canonical * A * (v1.cross(v2)).dot(v3);
110  }
111  }
112 
113  return C.trace() * Matrix3f::Identity() - C;
114 }
115 
116 template <typename PolygonT>
118 {
119  typedef typename PolygonT::size_type size_type;
120  typedef typename PolygonT::index_type index_type;
121 
122  Vec3f com(0,0,0);
123  FCL_REAL vol = 0;
124  for(int i = 0; i < num_polygons; ++i)
125  {
126  const PolygonT& polygon (polygons[i]);
127  // compute the center of the polygon
128  Vec3f plane_center(0,0,0);
129  for(size_type j = 0; j < polygon.size(); ++j)
130  plane_center += points[polygon[j]];
131  plane_center /= polygon.size();
132 
133  // compute the volume of tetrahedron making by neighboring two points, the plane center and the reference point (zero) of the convex shape
134  const Vec3f& v3 = plane_center;
135  for(size_type j = 0; j < polygon.size(); ++j)
136  {
137  index_type e_first = polygon[j];
138  index_type e_second = polygon[(j+1)%polygon.size()];
139  const Vec3f& v1 = points[e_first];
140  const Vec3f& v2 = points[e_second];
141  FCL_REAL d_six_vol = (v1.cross(v2)).dot(v3);
142  vol += d_six_vol;
143  com += (points[e_first] + points[e_second] + plane_center) * d_six_vol;
144  }
145  }
146 
147  return com / (vol * 4); // here we choose zero as the reference
148 }
149 
150 template <typename PolygonT>
152 {
153  typedef typename PolygonT::size_type size_type;
154  typedef typename PolygonT::index_type index_type;
155 
156  FCL_REAL vol = 0;
157  for(int i = 0; i < num_polygons; ++i)
158  {
159  const PolygonT& polygon (polygons[i]);
160 
161  // compute the center of the polygon
162  Vec3f plane_center(0,0,0);
163  for(size_type j = 0; j < polygon.size(); ++j)
164  plane_center += points[polygon[j]];
165  plane_center /= polygon.size();
166 
167  // compute the volume of tetrahedron making by neighboring two points, the plane center and the reference point (zero point) of the convex shape
168  const Vec3f& v3 = plane_center;
169  for(size_type j = 0; j < polygon.size(); ++j)
170  {
171  index_type e_first = polygon[j];
172  index_type e_second = polygon[(j+1)%polygon.size()];
173  const Vec3f& v1 = points[e_first];
174  const Vec3f& v2 = points[e_second];
175  FCL_REAL d_six_vol = (v1.cross(v2)).dot(v3);
176  vol += d_six_vol;
177  }
178  }
179 
180  return vol / 6;
181 }
182 
183 template <typename PolygonT>
185 {
187 
188  typedef typename PolygonT::size_type size_type;
189  typedef typename PolygonT::index_type index_type;
190  std::vector<std::set<index_type> > nneighbors (num_points);
191  unsigned int c_nneighbors = 0;
192 
193  for (int l = 0; l < num_polygons; ++l)
194  {
195  const PolygonT& polygon (polygons[l]);
196  size_type n = polygon.size();
197 
198  for(size_type j = 0; j < polygon.size(); ++j)
199  {
200  size_type i = (j==0 ) ? n-1 : j-1;
201  size_type k = (j==n-1) ? 0 : j+1;
202  index_type pi = polygon[i],
203  pj = polygon[j],
204  pk = polygon[k];
205  // Update neighbors of pj;
206  if (nneighbors[pj].count(pi) == 0) {
207  c_nneighbors++;
208  nneighbors[pj].insert(pi);
209  }
210  if (nneighbors[pj].count(pk) == 0) {
211  c_nneighbors++;
212  nneighbors[pj].insert(pk);
213  }
214  }
215  }
216 
217  nneighbors_ = new unsigned int[c_nneighbors];
218  unsigned int* p_nneighbors = nneighbors_;
219  for (int i = 0; i < num_points; ++i) {
220  Neighbors& n = neighbors[i];
221  if (nneighbors[i].size() >= std::numeric_limits<unsigned char>::max())
222  throw std::logic_error ("Too many neighbors.");
223  n.count_ = (unsigned char)nneighbors[i].size();
224  n.n_ = p_nneighbors;
225  p_nneighbors = std::copy (nneighbors[i].begin(), nneighbors[i].end(), p_nneighbors);
226  }
227  assert (p_nneighbors == nneighbors_ + c_nneighbors);
228 }
229 
230 }
231 
232 } // namespace hpp
233 
234 #endif
FCL_REAL computeVolume() const
compute the volume
Definition: convex.hxx:151
bool own_storage_
Definition: geometric_shapes.h:326
Main namespace.
Definition: AABB.h:43
Definition: geometric_shapes.h:297
Eigen::Matrix< FCL_REAL, 3, 3 > Matrix3f
Definition: data_types.h:74
void fillNeighbors()
Definition: convex.hxx:184
Vec3f computeCOM() const
compute center of mass
Definition: convex.hxx:117
Convex(bool ownStorage, Vec3f *points_, int num_points_, PolygonT *polygons_, int num_polygons_)
Constructing a convex, providing normal and offset of each polytype surface, and the points and shape...
Definition: convex.hxx:50
~Convex()
Definition: convex.hxx:72
PolygonT * polygons
An array of PolygonT object. PolygonT should contains a list of vertices for each polygon...
Definition: convex.h:77
double FCL_REAL
Definition: data_types.h:68
unsigned int * n_
Definition: geometric_shapes.h:300
int num_polygons
Definition: convex.h:78
unsigned char count_
Definition: geometric_shapes.h:299
int num_points
Definition: geometric_shapes.h:295
Base for convex polytope.
Definition: geometric_shapes.h:281
Matrix3f computeMomentofInertia() const
based on http://number-none.com/blow/inertia/bb_inertia.doc
Definition: convex.hxx:78
Eigen::Matrix< FCL_REAL, 3, 1 > Vec3f
Definition: data_types.h:73
Convex polytope.
Definition: convex.h:53
Neighbors * neighbors
Definition: geometric_shapes.h:309
unsigned int * nneighbors_
Definition: geometric_shapes.h:324
Vec3f * points
An array of the points of the polygon.
Definition: geometric_shapes.h:294