37 #ifndef HPP_FCL_SHAPE_CONVEX_HXX
38 #define HPP_FCL_SHAPE_CONVEX_HXX
46 template <
typename PolygonT>
48 unsigned int num_points_, PolygonT* polygons_,
49 unsigned int num_polygons_)
50 :
ConvexBase(), polygons(polygons_), num_polygons(num_polygons_) {
55 template <
typename PolygonT>
58 polygons(other.polygons),
59 num_polygons(other.num_polygons) {
66 template <
typename PolygonT>
68 if (own_storage_)
delete[] polygons;
71 template <
typename PolygonT>
73 unsigned int num_points_, PolygonT* polygons_,
74 unsigned int num_polygons_) {
75 if (own_storage_)
delete[] polygons;
78 num_polygons = num_polygons_;
84 template <
typename PolygonT>
87 std::copy(points, points + num_points, cloned_points);
89 PolygonT* cloned_polygons =
new PolygonT[num_polygons];
90 std::copy(polygons, polygons + num_polygons, cloned_polygons);
92 Convex* copy_ptr =
new Convex(
true, cloned_points, num_points,
93 cloned_polygons, num_polygons);
95 copy_ptr->ShapeBase::operator=(*this);
99 template <
typename PolygonT>
101 typedef typename PolygonT::size_type size_type;
102 typedef typename PolygonT::index_type index_type;
107 C_canonical << 1 / 60.0, 1 / 120.0, 1 / 120.0, 1 / 120.0, 1 / 60.0, 1 / 120.0,
108 1 / 120.0, 1 / 120.0, 1 / 60.0;
110 for (
unsigned int i = 0; i < num_polygons; ++i) {
111 const PolygonT& polygon = polygons[i];
114 Vec3f plane_center(0, 0, 0);
115 for (size_type j = 0; j < polygon.size(); ++j)
116 plane_center += points[polygon[(index_type)j]];
117 plane_center /= polygon.size();
121 const Vec3f& v3 = plane_center;
122 for (size_type j = 0; j < polygon.size(); ++j) {
123 index_type e_first = polygon[
static_cast<index_type
>(j)];
124 index_type e_second =
125 polygon[
static_cast<index_type
>((j + 1) % polygon.size())];
126 const Vec3f& v1 = points[e_first];
127 const Vec3f& v2 = points[e_second];
129 A << v1.transpose(), v2.transpose(),
131 C += A.transpose() * C_canonical * A * (v1.cross(v2)).dot(v3);
135 return C.trace() * Matrix3f::Identity() - C;
138 template <
typename PolygonT>
140 typedef typename PolygonT::size_type size_type;
141 typedef typename PolygonT::index_type index_type;
145 for (
unsigned int i = 0; i < num_polygons; ++i) {
146 const PolygonT& polygon = polygons[i];
148 Vec3f plane_center(0, 0, 0);
149 for (size_type j = 0; j < polygon.size(); ++j)
150 plane_center += points[polygon[(index_type)j]];
151 plane_center /= polygon.size();
155 const Vec3f& v3 = plane_center;
156 for (size_type j = 0; j < polygon.size(); ++j) {
157 index_type e_first = polygon[
static_cast<index_type
>(j)];
158 index_type e_second =
159 polygon[
static_cast<index_type
>((j + 1) % polygon.size())];
160 const Vec3f& v1 = points[e_first];
161 const Vec3f& v2 = points[e_second];
162 FCL_REAL d_six_vol = (v1.cross(v2)).dot(v3);
164 com += (points[e_first] + points[e_second] + plane_center) * d_six_vol;
168 return com / (vol * 4);
171 template <
typename PolygonT>
173 typedef typename PolygonT::size_type size_type;
174 typedef typename PolygonT::index_type index_type;
177 for (
unsigned int i = 0; i < num_polygons; ++i) {
178 const PolygonT& polygon = polygons[i];
181 Vec3f plane_center(0, 0, 0);
182 for (size_type j = 0; j < polygon.size(); ++j)
183 plane_center += points[polygon[(index_type)j]];
184 plane_center /= polygon.size();
188 const Vec3f& v3 = plane_center;
189 for (size_type j = 0; j < polygon.size(); ++j) {
190 index_type e_first = polygon[
static_cast<index_type
>(j)];
191 index_type e_second =
192 polygon[
static_cast<index_type
>((j + 1) % polygon.size())];
193 const Vec3f& v1 = points[e_first];
194 const Vec3f& v2 = points[e_second];
195 FCL_REAL d_six_vol = (v1.cross(v2)).dot(v3);
203 template <
typename PolygonT>
205 if (neighbors)
delete[] neighbors;
208 typedef typename PolygonT::size_type size_type;
209 typedef typename PolygonT::index_type index_type;
210 std::vector<std::set<index_type> > nneighbors(num_points);
211 unsigned int c_nneighbors = 0;
213 for (
unsigned int l = 0; l < num_polygons; ++l) {
214 const PolygonT& polygon = polygons[l];
215 const size_type n = polygon.size();
217 for (size_type j = 0; j < polygon.size(); ++j) {
218 size_type i = (j == 0) ? n - 1 : j - 1;
219 size_type k = (j == n - 1) ? 0 : j + 1;
220 index_type pi = polygon[(index_type)i], pj = polygon[(index_type)j],
221 pk = polygon[(index_type)k];
223 if (nneighbors[pj].count(pi) == 0) {
225 nneighbors[pj].insert(pi);
227 if (nneighbors[pj].count(pk) == 0) {
229 nneighbors[pj].insert(pk);
234 if (nneighbors_)
delete[] nneighbors_;
235 nneighbors_ =
new unsigned int[c_nneighbors];
237 unsigned int* p_nneighbors = nneighbors_;
238 for (
unsigned int i = 0; i < num_points; ++i) {
240 if (nneighbors[i].size() >= (std::numeric_limits<unsigned char>::max)())
242 n.
count_ = (
unsigned char)nneighbors[i].size();
245 std::copy(nneighbors[i].begin(), nneighbors[i].end(), p_nneighbors);
247 assert(p_nneighbors == nneighbors_ + c_nneighbors);