37 #ifndef COAL_SHAPE_CONVEX_HXX
38 #define COAL_SHAPE_CONVEX_HXX
46 template <
typename PolygonT>
48 unsigned int num_points_,
49 std::shared_ptr<std::vector<PolygonT>> polygons_,
50 unsigned int num_polygons_)
51 :
ConvexBase(), polygons(polygons_), num_polygons(num_polygons_) {
57 template <
typename PolygonT>
59 :
ConvexBase(other), num_polygons(other.num_polygons) {
66 template <
typename PolygonT>
69 template <
typename PolygonT>
71 unsigned int num_points_,
72 std::shared_ptr<std::vector<PolygonT>> polygons_,
73 unsigned int num_polygons_) {
76 this->num_polygons = num_polygons_;
77 this->polygons = polygons_;
79 this->fillNeighbors();
80 this->buildSupportWarmStart();
83 template <
typename PolygonT>
88 template <
typename PolygonT>
90 typedef typename PolygonT::size_type size_type;
91 typedef typename PolygonT::index_type index_type;
96 C_canonical << 1 / 60.0, 1 / 120.0, 1 / 120.0, 1 / 120.0, 1 / 60.0, 1 / 120.0,
97 1 / 120.0, 1 / 120.0, 1 / 60.0;
99 if (!(points.get())) {
101 <<
"Error in `Convex::computeMomentofInertia`! Convex has no vertices."
105 const std::vector<Vec3s>& points_ = *points;
106 if (!(polygons.get())) {
108 <<
"Error in `Convex::computeMomentofInertia`! Convex has no polygons."
112 const std::vector<PolygonT>& polygons_ = *polygons;
113 for (
unsigned int i = 0; i < num_polygons; ++i) {
114 const PolygonT& polygon = polygons_[i];
117 Vec3s plane_center(0, 0, 0);
118 for (size_type j = 0; j < polygon.size(); ++j)
119 plane_center += points_[polygon[(index_type)j]];
120 plane_center /= polygon.size();
124 const Vec3s& v3 = plane_center;
125 for (size_type j = 0; j < polygon.size(); ++j) {
126 index_type e_first = polygon[
static_cast<index_type
>(j)];
127 index_type e_second =
128 polygon[
static_cast<index_type
>((j + 1) % polygon.size())];
129 const Vec3s& v1 = points_[e_first];
130 const Vec3s& v2 = points_[e_second];
132 A << v1.transpose(), v2.transpose(),
134 C += A.transpose() * C_canonical * A * (v1.cross(v2)).dot(v3);
138 return C.trace() * Matrix3s::Identity() - C;
141 template <
typename PolygonT>
143 typedef typename PolygonT::size_type size_type;
144 typedef typename PolygonT::index_type index_type;
148 if (!(points.get())) {
149 std::cerr <<
"Error in `Convex::computeCOM`! Convex has no vertices."
153 const std::vector<Vec3s>& points_ = *points;
154 if (!(polygons.get())) {
155 std::cerr <<
"Error in `Convex::computeCOM`! Convex has no polygons."
159 const std::vector<PolygonT>& polygons_ = *polygons;
160 for (
unsigned int i = 0; i < num_polygons; ++i) {
161 const PolygonT& polygon = polygons_[i];
163 Vec3s plane_center(0, 0, 0);
164 for (size_type j = 0; j < polygon.size(); ++j)
165 plane_center += points_[polygon[(index_type)j]];
166 plane_center /= polygon.size();
170 const Vec3s& v3 = plane_center;
171 for (size_type j = 0; j < polygon.size(); ++j) {
172 index_type e_first = polygon[
static_cast<index_type
>(j)];
173 index_type e_second =
174 polygon[
static_cast<index_type
>((j + 1) % polygon.size())];
175 const Vec3s& v1 = points_[e_first];
176 const Vec3s& v2 = points_[e_second];
177 CoalScalar d_six_vol = (v1.cross(v2)).dot(v3);
179 com += (points_[e_first] + points_[e_second] + plane_center) * d_six_vol;
183 return com / (vol * 4);
186 template <
typename PolygonT>
188 typedef typename PolygonT::size_type size_type;
189 typedef typename PolygonT::index_type index_type;
192 if (!(points.get())) {
193 std::cerr <<
"Error in `Convex::computeVolume`! Convex has no vertices."
197 const std::vector<Vec3s>& points_ = *points;
198 if (!(polygons.get())) {
199 std::cerr <<
"Error in `Convex::computeVolume`! Convex has no polygons."
203 const std::vector<PolygonT>& polygons_ = *polygons;
204 for (
unsigned int i = 0; i < num_polygons; ++i) {
205 const PolygonT& polygon = polygons_[i];
208 Vec3s plane_center(0, 0, 0);
209 for (size_type j = 0; j < polygon.size(); ++j)
210 plane_center += points_[polygon[(index_type)j]];
211 plane_center /= polygon.size();
215 const Vec3s& v3 = plane_center;
216 for (size_type j = 0; j < polygon.size(); ++j) {
217 index_type e_first = polygon[
static_cast<index_type
>(j)];
218 index_type e_second =
219 polygon[
static_cast<index_type
>((j + 1) % polygon.size())];
220 const Vec3s& v1 = points_[e_first];
221 const Vec3s& v2 = points_[e_second];
222 CoalScalar d_six_vol = (v1.cross(v2)).dot(v3);
230 template <
typename PolygonT>
232 neighbors.reset(
new std::vector<Neighbors>(num_points));
234 typedef typename PolygonT::size_type size_type;
235 typedef typename PolygonT::index_type index_type;
236 std::vector<std::set<index_type>> nneighbors(num_points);
237 unsigned int c_nneighbors = 0;
239 if (!(polygons.get())) {
240 std::cerr <<
"Error in `Convex::fillNeighbors`! Convex has no polygons."
243 const std::vector<PolygonT>& polygons_ = *polygons;
244 for (
unsigned int l = 0; l < num_polygons; ++l) {
245 const PolygonT& polygon = polygons_[l];
246 const size_type n = polygon.size();
248 for (size_type j = 0; j < polygon.size(); ++j) {
249 size_type i = (j == 0) ? n - 1 : j - 1;
250 size_type k = (j == n - 1) ? 0 : j + 1;
251 index_type pi = polygon[(index_type)i], pj = polygon[(index_type)j],
252 pk = polygon[(index_type)k];
254 if (nneighbors[pj].count(pi) == 0) {
256 nneighbors[pj].insert(pi);
258 if (nneighbors[pj].count(pk) == 0) {
260 nneighbors[pj].insert(pk);
265 nneighbors_.reset(
new std::vector<unsigned int>(c_nneighbors));
267 unsigned int* p_nneighbors = nneighbors_->data();
268 std::vector<Neighbors>& neighbors_ = *neighbors;
269 for (
unsigned int i = 0; i < num_points; ++i) {
271 if (nneighbors[i].size() >= (std::numeric_limits<unsigned char>::max)())
273 n.
count_ = (
unsigned char)nneighbors[i].size();
276 std::copy(nneighbors[i].begin(), nneighbors[i].end(), p_nneighbors);
278 assert(p_nneighbors == nneighbors_->data() + c_nneighbors);
Base for convex polytope.
Definition: geometric_shapes.h:645
Convex polytope.
Definition: convex.h:49
virtual Convex< PolygonT > * clone() const
Clone (deep copy)
Definition: convex.hxx:84
Matrix3s computeMomentofInertia() const
based on http://number-none.com/blow/inertia/bb_inertia.doc
Definition: convex.hxx:89
~Convex()
Definition: convex.hxx:67
CoalScalar computeVolume() const
compute the volume
Definition: convex.hxx:187
void fillNeighbors()
Definition: convex.hxx:231
Vec3s computeCOM() const
compute center of mass
Definition: convex.hxx:142
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:101
void set(std::shared_ptr< std::vector< Vec3s >> 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:70
Convex()
Construct an uninitialized convex object.
Definition: convex.h:52
#define COAL_THROW_PRETTY(message, exception)
Definition: fwd.hh:64
unsigned int * n_
Definition: geometric_shapes.h:689
void buildSupportWarmStart()
Build the support points warm starts.
void set(std::shared_ptr< std::vector< Vec3s >> points_, unsigned int num_points_)
Set the points of the convex shape.
void initialize(std::shared_ptr< std::vector< Vec3s >> 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:688
Main namespace.
Definition: broadphase_bruteforce.h:44
Eigen::Matrix< CoalScalar, 3, 3 > Matrix3s
Definition: data_types.h:81
Eigen::Matrix< CoalScalar, 3, 1 > Vec3s
Definition: data_types.h:77
double CoalScalar
Definition: data_types.h:76
Definition: geometric_shapes.h:687