37 #ifndef COAL_SHAPE_CONVEX_HXX
38 #define COAL_SHAPE_CONVEX_HXX
48 template <
typename PolygonT>
50 unsigned int num_points_,
51 std::shared_ptr<std::vector<PolygonT>> polygons_,
52 unsigned int num_polygons_)
53 :
Base(), polygons(polygons_), num_polygons(num_polygons_) {
59 template <
typename PolygonT>
63 this->base() = other.
base();
73 template <
typename PolygonT>
74 template <
typename OtherPolygonT>
77 if (source ==
nullptr || copy ==
nullptr) {
82 Base::deepcopy(source, copy);
85 typedef typename OtherPolygonT::IndexType OtherIndexType;
88 const std::vector<PolygonT>& source_polygons = *(source->
polygons);
90 new std::vector<OtherPolygonT>(source_polygons.size()));
91 std::vector<OtherPolygonT>& copy_polygons = *(copy->
polygons);
92 for (std::size_t i = 0; i < source_polygons.size(); ++i) {
93 copy_polygons[i] = source_polygons[i].template cast<OtherIndexType>();
100 template <
typename PolygonT>
102 unsigned int num_points_,
103 std::shared_ptr<std::vector<PolygonT>> polygons_,
104 unsigned int num_polygons_) {
105 Base::set(points_, num_points_);
107 this->num_polygons = num_polygons_;
108 this->polygons = polygons_;
110 this->fillNeighbors();
111 this->buildSupportWarmStart();
114 template <
typename PolygonT>
116 typedef typename PolygonT::size_type size_type;
117 typedef typename PolygonT::IndexType
IndexType;
122 C_canonical <<
Scalar(1 / 60.0),
132 if (!(points.get())) {
133 std::cerr <<
"Error in `ConvexTpl::computeMomentofInertia`! ConvexTpl has "
138 const std::vector<Vec3s>& points_ = *points;
139 if (!(polygons.get())) {
140 std::cerr <<
"Error in `ConvexTpl::computeMomentofInertia`! ConvexTpl has "
145 const std::vector<PolygonT>& polygons_ = *polygons;
146 for (
unsigned int i = 0; i < num_polygons; ++i) {
147 const PolygonT& polygon = polygons_[i];
150 Vec3s plane_center(0, 0, 0);
151 for (size_type j = 0; j < polygon.size(); ++j)
152 plane_center += points_[polygon[(IndexType)j]];
153 plane_center /=
Scalar(polygon.size());
157 const Vec3s& v3 = plane_center;
158 for (size_type j = 0; j < polygon.size(); ++j) {
159 IndexType e_first = polygon[
static_cast<IndexType
>(j)];
161 polygon[
static_cast<IndexType
>((j + 1) % polygon.size())];
162 const Vec3s& v1 = points_[e_first];
163 const Vec3s& v2 = points_[e_second];
165 A << v1.transpose(), v2.transpose(),
167 C += A.transpose() * C_canonical * A * (v1.cross(v2)).dot(v3);
171 return C.trace() * Matrix3s::Identity() - C;
174 template <
typename PolygonT>
176 typedef typename PolygonT::size_type size_type;
177 typedef typename PolygonT::IndexType
IndexType;
181 if (!(points.get())) {
182 std::cerr <<
"Error in `ConvexTpl::computeCOM`! ConvexTpl has no vertices."
186 const std::vector<Vec3s>& points_ = *points;
187 if (!(polygons.get())) {
188 std::cerr <<
"Error in `ConvexTpl::computeCOM`! ConvexTpl has no polygons."
192 const std::vector<PolygonT>& polygons_ = *polygons;
193 for (
unsigned int i = 0; i < num_polygons; ++i) {
194 const PolygonT& polygon = polygons_[i];
196 Vec3s plane_center(0, 0, 0);
197 for (size_type j = 0; j < polygon.size(); ++j)
198 plane_center += points_[polygon[(
IndexType)j]];
199 plane_center /=
Scalar(polygon.size());
203 const Vec3s& v3 = plane_center;
204 for (size_type j = 0; j < polygon.size(); ++j) {
207 polygon[
static_cast<IndexType>((j + 1) % polygon.size())];
208 const Vec3s& v1 = points_[e_first];
209 const Vec3s& v2 = points_[e_second];
210 Scalar d_six_vol = (v1.cross(v2)).dot(v3);
212 com += (points_[e_first] + points_[e_second] + plane_center) * d_six_vol;
216 return com / (vol * 4);
219 template <
typename PolygonT>
221 typedef typename PolygonT::size_type size_type;
222 typedef typename PolygonT::IndexType
IndexType;
225 if (!(points.get())) {
227 <<
"Error in `ConvexTpl::computeVolume`! ConvexTpl has no vertices."
231 const std::vector<Vec3s>& points_ = *points;
232 if (!(polygons.get())) {
234 <<
"Error in `ConvexTpl::computeVolume`! ConvexTpl has no polygons."
238 const std::vector<PolygonT>& polygons_ = *polygons;
239 for (
unsigned int i = 0; i < num_polygons; ++i) {
240 const PolygonT& polygon = polygons_[i];
243 Vec3s plane_center(0, 0, 0);
244 for (size_type j = 0; j < polygon.size(); ++j)
245 plane_center += points_[polygon[(
IndexType)j]];
246 plane_center /=
Scalar(polygon.size());
250 const Vec3s& v3 = plane_center;
251 for (size_type j = 0; j < polygon.size(); ++j) {
254 polygon[
static_cast<IndexType>((j + 1) % polygon.size())];
255 const Vec3s& v1 = points_[e_first];
256 const Vec3s& v2 = points_[e_second];
257 Scalar d_six_vol = (v1.cross(v2)).dot(v3);
265 template <
typename PolygonT>
267 neighbors.reset(
new std::vector<Neighbors>(num_points));
269 typedef typename PolygonT::size_type size_type;
270 typedef typename PolygonT::IndexType
IndexType;
272 std::vector<std::set<IndexType>> nneighbors(num_points);
273 unsigned int c_nneighbors = 0;
275 if (!(polygons.get())) {
277 <<
"Error in `ConvexTpl::fillNeighbors`! ConvexTpl has no polygons."
280 const std::vector<PolygonT>& polygons_ = *polygons;
281 for (
unsigned int l = 0; l < num_polygons; ++l) {
282 const PolygonT& polygon = polygons_[l];
283 const size_type n = polygon.size();
285 for (size_type j = 0; j < polygon.size(); ++j) {
286 size_type i = (j == 0) ? n - 1 : j - 1;
287 size_type k = (j == n - 1) ? 0 : j + 1;
291 if (nneighbors[pj].count(pi) == 0) {
293 nneighbors[pj].insert(pi);
295 if (nneighbors[pj].count(pk) == 0) {
297 nneighbors[pj].insert(pk);
302 nneighbors_.reset(
new std::vector<IndexType>(c_nneighbors));
304 std::vector<Neighbors>& neighbors_ = *neighbors;
305 std::vector<IndexType>& nneighbors__ = *(nneighbors_);
307 for (
unsigned int i = 0; i < num_points; ++i) {
309 if (nneighbors[i].size() >= (std::numeric_limits<unsigned char>::max)())
311 n.
count = (
unsigned char)nneighbors[i].size();
void buildSupportWarmStart()
Build the support points warm starts.
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.
Definition: geometric_shapes.hxx:67
Convex polytope.
Definition: convex.h:50
Base & base()
Cast Convex to ConvexBaseTpl. This method should never be marked as virtual.
Definition: convex.h:79
virtual Matrix3s computeMomentofInertia() const override
based on http://number-none.com/blow/inertia/bb_inertia.doc
Definition: convex.hxx:115
ConvexTpl()
Construct an uninitialized convex object.
Definition: convex.h:61
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:101
ConvexTpl & operator=(const ConvexTpl &other)
Copy operator. The copy operator only shallow copies the data (it copies the shared pointers but does...
Definition: convex.hxx:60
void fillNeighbors()
Definition: convex.hxx:266
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:139
virtual Scalar computeVolume() const override
compute the volume
Definition: convex.hxx:220
ConvexTpl * deepcopy() const override
Deep copy of the ConvexBaseTpl. This method deep copies every field of the class.
Definition: convex.h:101
PolygonT::IndexType IndexType
Definition: convex.h:52
virtual Vec3s computeCOM() const override
compute center of mass
Definition: convex.hxx:175
unsigned int num_polygons
Definition: convex.h:140
#define COAL_THROW_PRETTY(message, exception)
Definition: fwd.hh:64
Main namespace.
Definition: broadphase_bruteforce.h:44
Eigen::Matrix< Scalar, 3, 1 > Vec3s
Definition: data_types.h:70
double Scalar
Definition: data_types.h:68
Eigen::Matrix< Scalar, 3, 3 > Matrix3s
Definition: data_types.h:74
Definition: geometric_shapes.h:638
unsigned char count
Definition: geometric_shapes.h:641
IndexType begin_id
Definition: geometric_shapes.h:642