37 #ifndef HPP_FCL_SHAPE_CONVEX_HXX
38 #define HPP_FCL_SHAPE_CONVEX_HXX
47 template <
typename PolygonT>
49 unsigned int num_points_,
50 std::shared_ptr<std::vector<PolygonT>> polygons_,
51 unsigned int num_polygons_)
52 :
ConvexBase(), polygons(polygons_), num_polygons(num_polygons_) {
58 template <
typename PolygonT>
60 :
ConvexBase(other), num_polygons(other.num_polygons) {
67 template <
typename PolygonT>
70 template <
typename PolygonT>
72 unsigned int num_points_,
73 std::shared_ptr<std::vector<PolygonT>> polygons_,
74 unsigned int num_polygons_) {
77 this->num_polygons = num_polygons_;
78 this->polygons = polygons_;
80 this->fillNeighbors();
81 this->buildSupportWarmStart();
84 template <
typename PolygonT>
89 template <
typename PolygonT>
91 typedef typename PolygonT::size_type size_type;
92 typedef typename PolygonT::index_type index_type;
97 C_canonical << 1 / 60.0, 1 / 120.0, 1 / 120.0, 1 / 120.0, 1 / 60.0, 1 / 120.0,
98 1 / 120.0, 1 / 120.0, 1 / 60.0;
100 if (!(points.get())) {
102 <<
"Error in `Convex::computeMomentofInertia`! Convex has no vertices."
106 const std::vector<Vec3f>& points_ = *points;
107 if (!(polygons.get())) {
109 <<
"Error in `Convex::computeMomentofInertia`! Convex has no polygons."
113 const std::vector<PolygonT>& polygons_ = *polygons;
114 for (
unsigned int i = 0; i < num_polygons; ++i) {
115 const PolygonT& polygon = polygons_[i];
118 Vec3f plane_center(0, 0, 0);
119 for (size_type j = 0; j < polygon.size(); ++j)
120 plane_center += points_[polygon[(index_type)j]];
121 plane_center /= polygon.size();
125 const Vec3f& v3 = plane_center;
126 for (size_type j = 0; j < polygon.size(); ++j) {
127 index_type e_first = polygon[
static_cast<index_type
>(j)];
128 index_type e_second =
129 polygon[
static_cast<index_type
>((j + 1) % polygon.size())];
130 const Vec3f& v1 = points_[e_first];
131 const Vec3f& v2 = points_[e_second];
133 A << v1.transpose(), v2.transpose(),
135 C += A.transpose() * C_canonical * A * (v1.cross(v2)).dot(v3);
139 return C.trace() * Matrix3f::Identity() - C;
142 template <
typename PolygonT>
144 typedef typename PolygonT::size_type size_type;
145 typedef typename PolygonT::index_type index_type;
149 if (!(points.get())) {
150 std::cerr <<
"Error in `Convex::computeCOM`! Convex has no vertices."
154 const std::vector<Vec3f>& points_ = *points;
155 if (!(polygons.get())) {
156 std::cerr <<
"Error in `Convex::computeCOM`! Convex has no polygons."
160 const std::vector<PolygonT>& polygons_ = *polygons;
161 for (
unsigned int i = 0; i < num_polygons; ++i) {
162 const PolygonT& polygon = polygons_[i];
164 Vec3f plane_center(0, 0, 0);
165 for (size_type j = 0; j < polygon.size(); ++j)
166 plane_center += points_[polygon[(index_type)j]];
167 plane_center /= polygon.size();
171 const Vec3f& v3 = plane_center;
172 for (size_type j = 0; j < polygon.size(); ++j) {
173 index_type e_first = polygon[
static_cast<index_type
>(j)];
174 index_type e_second =
175 polygon[
static_cast<index_type
>((j + 1) % polygon.size())];
176 const Vec3f& v1 = points_[e_first];
177 const Vec3f& v2 = points_[e_second];
178 FCL_REAL d_six_vol = (v1.cross(v2)).dot(v3);
180 com += (points_[e_first] + points_[e_second] + plane_center) * d_six_vol;
184 return com / (vol * 4);
187 template <
typename PolygonT>
189 typedef typename PolygonT::size_type size_type;
190 typedef typename PolygonT::index_type index_type;
193 if (!(points.get())) {
194 std::cerr <<
"Error in `Convex::computeVolume`! Convex has no vertices."
198 const std::vector<Vec3f>& points_ = *points;
199 if (!(polygons.get())) {
200 std::cerr <<
"Error in `Convex::computeVolume`! Convex has no polygons."
204 const std::vector<PolygonT>& polygons_ = *polygons;
205 for (
unsigned int i = 0; i < num_polygons; ++i) {
206 const PolygonT& polygon = polygons_[i];
209 Vec3f plane_center(0, 0, 0);
210 for (size_type j = 0; j < polygon.size(); ++j)
211 plane_center += points_[polygon[(index_type)j]];
212 plane_center /= polygon.size();
216 const Vec3f& v3 = plane_center;
217 for (size_type j = 0; j < polygon.size(); ++j) {
218 index_type e_first = polygon[
static_cast<index_type
>(j)];
219 index_type e_second =
220 polygon[
static_cast<index_type
>((j + 1) % polygon.size())];
221 const Vec3f& v1 = points_[e_first];
222 const Vec3f& v2 = points_[e_second];
223 FCL_REAL d_six_vol = (v1.cross(v2)).dot(v3);
231 template <
typename PolygonT>
233 neighbors.reset(
new std::vector<Neighbors>(num_points));
235 typedef typename PolygonT::size_type size_type;
236 typedef typename PolygonT::index_type index_type;
237 std::vector<std::set<index_type>> nneighbors(num_points);
238 unsigned int c_nneighbors = 0;
240 if (!(polygons.get())) {
241 std::cerr <<
"Error in `Convex::fillNeighbors`! Convex has no polygons."
244 const std::vector<PolygonT>& polygons_ = *polygons;
245 for (
unsigned int l = 0; l < num_polygons; ++l) {
246 const PolygonT& polygon = polygons_[l];
247 const size_type n = polygon.size();
249 for (size_type j = 0; j < polygon.size(); ++j) {
250 size_type i = (j == 0) ? n - 1 : j - 1;
251 size_type k = (j == n - 1) ? 0 : j + 1;
252 index_type pi = polygon[(index_type)i], pj = polygon[(index_type)j],
253 pk = polygon[(index_type)k];
255 if (nneighbors[pj].count(pi) == 0) {
257 nneighbors[pj].insert(pi);
259 if (nneighbors[pj].count(pk) == 0) {
261 nneighbors[pj].insert(pk);
266 nneighbors_.reset(
new std::vector<unsigned int>(c_nneighbors));
268 unsigned int* p_nneighbors = nneighbors_->data();
269 std::vector<Neighbors>& neighbors_ = *neighbors;
270 for (
unsigned int i = 0; i < num_points; ++i) {
272 if (nneighbors[i].size() >= (std::numeric_limits<unsigned char>::max)())
274 n.
count_ = (
unsigned char)nneighbors[i].size();
277 std::copy(nneighbors[i].begin(), nneighbors[i].end(), p_nneighbors);
279 assert(p_nneighbors == nneighbors_->data() + c_nneighbors);
Base for convex polytope.
Definition: geometric_shapes.h:638
Convex polytope.
Definition: convex.h:50
virtual Convex< PolygonT > * clone() const
Clone (deep copy)
Definition: convex.hxx:85
Convex()
Construct an uninitialized convex object.
Definition: convex.h:53
~Convex()
Definition: convex.hxx:68
std::shared_ptr< std::vector< PolygonT > > polygons
An array of PolygonT object. PolygonT should contains a list of vertices for each polygon,...
Definition: convex.h:102
void set(std::shared_ptr< std::vector< Vec3f >> points, unsigned int num_points, std::shared_ptr< std::vector< PolygonT >> polygons, unsigned int num_polygons)
Set the current Convex from a list of points and polygons.
Definition: convex.hxx:71
Vec3f computeCOM() const
compute center of mass
Definition: convex.hxx:143
FCL_REAL computeVolume() const
compute the volume
Definition: convex.hxx:188
void fillNeighbors()
Definition: convex.hxx:232
Matrix3f computeMomentofInertia() const
based on http://number-none.com/blow/inertia/bb_inertia.doc
Definition: convex.hxx:90
#define HPP_FCL_THROW_PRETTY(message, exception)
Definition: fwd.hh:64
void set(std::shared_ptr< std::vector< Vec3f >> points_, unsigned int num_points_)
Set the points of the convex shape.
unsigned int * n_
Definition: geometric_shapes.h:682
void buildSupportWarmStart()
Build the support points warm starts.
void initialize(std::shared_ptr< std::vector< Vec3f >> points_, unsigned int num_points_)
Initialize the points of the convex shape This also initializes the ConvexBase::center.
unsigned char count_
Definition: geometric_shapes.h:681
Eigen::Matrix< FCL_REAL, 3, 3 > Matrix3f
Definition: data_types.h:71
Eigen::Matrix< FCL_REAL, 3, 1 > Vec3f
Definition: data_types.h:67
double FCL_REAL
Definition: data_types.h:66
Main namespace.
Definition: broadphase_bruteforce.h:44
Definition: geometric_shapes.h:680