coal 3.0.1
Coal, The Collision Detection Library. Previously known as HPP-FCL, fork of FCL -- The Flexible Collision Library
Loading...
Searching...
No Matches
geometric_shapes.h
Go to the documentation of this file.
1/*
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011-2014, Willow Garage, Inc.
5 * Copyright (c) 2014-2015, Open Source Robotics Foundation
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * * Neither the name of Open Source Robotics Foundation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
38#ifndef COAL_GEOMETRIC_SHAPES_H
39#define COAL_GEOMETRIC_SHAPES_H
40
41#include <vector>
42#include <memory>
43
44#include <boost/math/constants/constants.hpp>
45
47#include "coal/data_types.h"
48
49#ifdef COAL_HAS_QHULL
50namespace orgQhull {
51class Qhull;
52}
53#endif
54
55namespace coal {
56
59 public:
61
63 ShapeBase(const ShapeBase& other)
64 : CollisionGeometry(other),
65 m_swept_sphere_radius(other.m_swept_sphere_radius) {}
66
67 ShapeBase& operator=(const ShapeBase& other) = default;
68
69 virtual ~ShapeBase() {};
70
72 OBJECT_TYPE getObjectType() const { return OT_GEOM; }
73
77 if (radius < 0) {
78 COAL_THROW_PRETTY("Swept-sphere radius must be positive.",
79 std::invalid_argument);
80 }
81 this->m_swept_sphere_radius = radius;
82 }
83
86 Scalar getSweptSphereRadius() const { return this->m_swept_sphere_radius; }
87
88 protected:
100 Scalar m_swept_sphere_radius{0};
101};
102
106
109 public:
111
112 TriangleP(const Vec3s& a_, const Vec3s& b_, const Vec3s& c_)
113 : ShapeBase(), a(a_), b(b_), c(c_) {}
114
115 TriangleP(const TriangleP& other)
116 : ShapeBase(other), a(other.a), b(other.b), c(other.c) {}
117
119 virtual TriangleP* clone() const { return new TriangleP(*this); };
120
123
125
126 // std::pair<ShapeBase*, Transform3s> inflated(const Scalar value) const
127 // {
128 // if (value == 0) return std::make_pair(new TriangleP(*this),
129 // Transform3s()); Vec3s AB(b - a), BC(c - b), CA(a - c); AB.normalize();
130 // BC.normalize();
131 // CA.normalize();
132 //
133 // Vec3s new_a(a + value * Vec3s(-AB + CA).normalized());
134 // Vec3s new_b(b + value * Vec3s(-BC + AB).normalized());
135 // Vec3s new_c(c + value * Vec3s(-CA + BC).normalized());
136 //
137 // return std::make_pair(new TriangleP(new_a, new_b, new_c),
138 // Transform3s());
139 // }
140 //
141 // Scalar minInflationValue() const
142 // {
143 // return (std::numeric_limits<Scalar>::max)(); // TODO(jcarpent):
144 // implement
145 // }
146
147 Vec3s a, b, c;
148
149 private:
150 virtual bool isEqual(const CollisionGeometry& _other) const {
151 const TriangleP* other_ptr = dynamic_cast<const TriangleP*>(&_other);
152 if (other_ptr == nullptr) return false;
153 const TriangleP& other = *other_ptr;
154
155 return a == other.a && b == other.b && c == other.c &&
156 getSweptSphereRadius() == other.getSweptSphereRadius();
157 }
158
159 public:
160 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
161};
162
164class COAL_DLLAPI Box : public ShapeBase {
165 public:
167 : ShapeBase(), halfSide(x / 2, y / 2, z / 2) {}
168
169 Box(const Vec3s& side_) : ShapeBase(), halfSide(side_ / 2) {}
170
171 Box(const Box& other) : ShapeBase(other), halfSide(other.halfSide) {}
172
173 Box& operator=(const Box& other) {
174 if (this == &other) return *this;
175
176 this->halfSide = other.halfSide;
177 return *this;
178 }
179
181 virtual Box* clone() const { return new Box(*this); };
182
184 Box() {}
185
188
191
193 NODE_TYPE getNodeType() const { return GEOM_BOX; }
194
195 Scalar computeVolume() const { return 8 * halfSide.prod(); }
196
198 Scalar V = computeVolume();
199 Vec3s s(halfSide.cwiseAbs2() * V);
200 return (Vec3s(s[1] + s[2], s[0] + s[2], s[0] + s[1]) / 3).asDiagonal();
201 }
202
203 Scalar minInflationValue() const { return -halfSide.minCoeff(); }
204
213 std::pair<Box, Transform3s> inflated(const Scalar value) const {
214 if (value <= minInflationValue())
215 COAL_THROW_PRETTY("value (" << value << ") "
216 << "is two small. It should be at least: "
217 << minInflationValue(),
218 std::invalid_argument);
219 return std::make_pair(Box(2 * (halfSide + Vec3s::Constant(value))),
220 Transform3s());
221 }
222
223 private:
224 virtual bool isEqual(const CollisionGeometry& _other) const {
225 const Box* other_ptr = dynamic_cast<const Box*>(&_other);
226 if (other_ptr == nullptr) return false;
227 const Box& other = *other_ptr;
228
229 return halfSide == other.halfSide &&
230 getSweptSphereRadius() == other.getSweptSphereRadius();
231 }
232
233 public:
234 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
235};
236
239 public:
242
243 explicit Sphere(Scalar radius_) : ShapeBase(), radius(radius_) {}
244
245 Sphere(const Sphere& other) : ShapeBase(other), radius(other.radius) {}
246
248 virtual Sphere* clone() const { return new Sphere(*this); };
249
252
255
258
260 Scalar I = Scalar(0.4) * radius * radius * computeVolume();
261 return I * Matrix3s::Identity();
262 }
263
265 return 4 * boost::math::constants::pi<Scalar>() * radius * radius * radius /
266 3;
267 }
268
269 Scalar minInflationValue() const { return -radius; }
270
279 std::pair<Sphere, Transform3s> inflated(const Scalar value) const {
280 if (value <= minInflationValue())
281 COAL_THROW_PRETTY("value (" << value
282 << ") is two small. It should be at least: "
283 << minInflationValue(),
284 std::invalid_argument);
285 return std::make_pair(Sphere(radius + value), Transform3s());
286 }
287
288 private:
289 virtual bool isEqual(const CollisionGeometry& _other) const {
290 const Sphere* other_ptr = dynamic_cast<const Sphere*>(&_other);
291 if (other_ptr == nullptr) return false;
292 const Sphere& other = *other_ptr;
293
294 return radius == other.radius &&
295 getSweptSphereRadius() == other.getSweptSphereRadius();
296 }
297
298 public:
299 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
300};
301
304 public:
307
308 Ellipsoid(Scalar rx, Scalar ry, Scalar rz) : ShapeBase(), radii(rx, ry, rz) {}
309
310 explicit Ellipsoid(const Vec3s& radii) : radii(radii) {}
311
312 Ellipsoid(const Ellipsoid& other) : ShapeBase(other), radii(other.radii) {}
313
315 virtual Ellipsoid* clone() const { return new Ellipsoid(*this); };
316
320
323
326
328 Scalar V = computeVolume();
329 Scalar a2 = V * radii[0] * radii[0];
330 Scalar b2 = V * radii[1] * radii[1];
331 Scalar c2 = V * radii[2] * radii[2];
332 Scalar alpha = Scalar(0.2);
333 return (Matrix3s() << alpha * (b2 + c2), 0, 0, 0, alpha * (a2 + c2), 0, 0,
334 0, alpha * (a2 + b2))
335 .finished();
336 }
337
339 return 4 * boost::math::constants::pi<Scalar>() * radii[0] * radii[1] *
340 radii[2] / 3;
341 }
342
343 Scalar minInflationValue() const { return -radii.minCoeff(); }
344
353 std::pair<Ellipsoid, Transform3s> inflated(const Scalar value) const {
354 if (value <= minInflationValue())
355 COAL_THROW_PRETTY("value (" << value
356 << ") is two small. It should be at least: "
357 << minInflationValue(),
358 std::invalid_argument);
359 return std::make_pair(Ellipsoid(radii + Vec3s::Constant(value)),
360 Transform3s());
361 }
362
363 private:
364 virtual bool isEqual(const CollisionGeometry& _other) const {
365 const Ellipsoid* other_ptr = dynamic_cast<const Ellipsoid*>(&_other);
366 if (other_ptr == nullptr) return false;
367 const Ellipsoid& other = *other_ptr;
368
369 return radii == other.radii &&
370 getSweptSphereRadius() == other.getSweptSphereRadius();
371 }
372
373 public:
374 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
375};
376
382 public:
385
386 Capsule(Scalar radius_, Scalar lz_) : ShapeBase(), radius(radius_) {
387 halfLength = lz_ / 2;
388 }
389
390 Capsule(const Capsule& other)
391 : ShapeBase(other), radius(other.radius), halfLength(other.halfLength) {}
392
394 virtual Capsule* clone() const { return new Capsule(*this); };
395
398
401
404
407
409 return boost::math::constants::pi<Scalar>() * radius * radius *
410 ((halfLength * 2) + radius * 4 / Scalar(3));
411 }
412
414 Scalar v_cyl = radius * radius * (halfLength * 2) *
415 boost::math::constants::pi<Scalar>();
416 Scalar v_sph = radius * radius * radius *
417 boost::math::constants::pi<Scalar>() * 4 / Scalar(3);
418
419 Scalar h2 = halfLength * halfLength;
420 Scalar r2 = radius * radius;
421 Scalar ix =
422 v_cyl * (h2 / Scalar(3) + r2 / Scalar(4)) +
423 v_sph * (Scalar(0.4) * r2 + h2 + Scalar(0.75) * radius * halfLength);
424 Scalar iz = (Scalar(0.5) * v_cyl + Scalar(0.4) * v_sph) * radius * radius;
425
426 return (Matrix3s() << ix, 0, 0, 0, ix, 0, 0, 0, iz).finished();
427 }
428
429 Scalar minInflationValue() const { return -radius; }
430
439 std::pair<Capsule, Transform3s> inflated(const Scalar value) const {
440 if (value <= minInflationValue())
441 COAL_THROW_PRETTY("value (" << value
442 << ") is two small. It should be at least: "
443 << minInflationValue(),
444 std::invalid_argument);
445 return std::make_pair(Capsule(radius + value, 2 * halfLength),
446 Transform3s());
447 }
448
449 private:
450 virtual bool isEqual(const CollisionGeometry& _other) const {
451 const Capsule* other_ptr = dynamic_cast<const Capsule*>(&_other);
452 if (other_ptr == nullptr) return false;
453 const Capsule& other = *other_ptr;
454
455 return radius == other.radius && halfLength == other.halfLength &&
456 getSweptSphereRadius() == other.getSweptSphereRadius();
457 }
458
459 public:
460 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
461};
462
466class COAL_DLLAPI Cone : public ShapeBase {
467 public:
469 Cone() {}
470
471 Cone(Scalar radius_, Scalar lz_) : ShapeBase(), radius(radius_) {
472 halfLength = lz_ / 2;
473 }
474
475 Cone(const Cone& other)
476 : ShapeBase(other), radius(other.radius), halfLength(other.halfLength) {}
477
479 virtual Cone* clone() const { return new Cone(*this); };
480
483
486
489
491 NODE_TYPE getNodeType() const { return GEOM_CONE; }
492
494 return boost::math::constants::pi<Scalar>() * radius * radius *
495 (halfLength * 2) / 3;
496 }
497
499 Scalar V = computeVolume();
500 Scalar ix =
501 V * (Scalar(0.4) * halfLength * halfLength + 3 * radius * radius / 20);
502 Scalar iz = Scalar(0.3) * V * radius * radius;
503
504 return (Matrix3s() << ix, 0, 0, 0, ix, 0, 0, 0, iz).finished();
505 }
506
507 Vec3s computeCOM() const { return Vec3s(0, 0, -Scalar(0.5) * halfLength); }
508
509 Scalar minInflationValue() const { return -(std::min)(radius, halfLength); }
510
519 std::pair<Cone, Transform3s> inflated(const Scalar value) const {
520 if (value <= minInflationValue())
521 COAL_THROW_PRETTY("value (" << value
522 << ") is two small. It should be at least: "
523 << minInflationValue(),
524 std::invalid_argument);
525
526 // tan(alpha) = 2*halfLength/radius;
527 const Scalar tan_alpha = 2 * halfLength / radius;
528 const Scalar sin_alpha = tan_alpha / std::sqrt(1 + tan_alpha * tan_alpha);
529 const Scalar top_inflation = value / sin_alpha;
530 const Scalar bottom_inflation = value;
531
532 const Scalar new_lz = 2 * halfLength + top_inflation + bottom_inflation;
533 const Scalar new_cz = (top_inflation + bottom_inflation) / Scalar(2);
534 const Scalar new_radius = new_lz / tan_alpha;
535
536 return std::make_pair(Cone(new_radius, new_lz),
537 Transform3s(Vec3s(0., 0., new_cz)));
538 }
539
540 private:
541 virtual bool isEqual(const CollisionGeometry& _other) const {
542 const Cone* other_ptr = dynamic_cast<const Cone*>(&_other);
543 if (other_ptr == nullptr) return false;
544 const Cone& other = *other_ptr;
545
546 return radius == other.radius && halfLength == other.halfLength &&
547 getSweptSphereRadius() == other.getSweptSphereRadius();
548 }
549
550 public:
551 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
552};
553
557 public:
560
561 Cylinder(Scalar radius_, Scalar lz_) : ShapeBase(), radius(radius_) {
562 halfLength = lz_ / 2;
563 }
564
565 Cylinder(const Cylinder& other)
566 : ShapeBase(other), radius(other.radius), halfLength(other.halfLength) {}
567
568 Cylinder& operator=(const Cylinder& other) {
569 if (this == &other) return *this;
570
571 this->radius = other.radius;
572 this->halfLength = other.halfLength;
573 return *this;
574 }
575
577 virtual Cylinder* clone() const { return new Cylinder(*this); };
578
581
584
587
590
592 return boost::math::constants::pi<Scalar>() * radius * radius *
593 (halfLength * 2);
594 }
595
597 Scalar V = computeVolume();
598 Scalar ix = V * (radius * radius / 4 + halfLength * halfLength / 3);
599 Scalar iz = V * radius * radius / 2;
600 return (Matrix3s() << ix, 0, 0, 0, ix, 0, 0, 0, iz).finished();
601 }
602
603 Scalar minInflationValue() const { return -(std::min)(radius, halfLength); }
604
613 std::pair<Cylinder, Transform3s> inflated(const Scalar value) const {
614 if (value <= minInflationValue())
615 COAL_THROW_PRETTY("value (" << value
616 << ") is two small. It should be at least: "
617 << minInflationValue(),
618 std::invalid_argument);
619 return std::make_pair(Cylinder(radius + value, 2 * (halfLength + value)),
620 Transform3s());
621 }
622
623 private:
624 virtual bool isEqual(const CollisionGeometry& _other) const {
625 const Cylinder* other_ptr = dynamic_cast<const Cylinder*>(&_other);
626 if (other_ptr == nullptr) return false;
627 const Cylinder& other = *other_ptr;
628
629 return radius == other.radius && halfLength == other.halfLength &&
630 getSweptSphereRadius() == other.getSweptSphereRadius();
631 }
632
633 public:
634 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
635};
636
637template <typename _IndexType>
639 typedef _IndexType IndexType;
640
641 unsigned char count;
643
644 bool operator==(const ConvexBaseTplNeighbors& other) const {
645 if (count != other.count) return false;
646 if (begin_id != other.begin_id) return false;
647
648 return true;
649 }
650
651 bool operator!=(const ConvexBaseTplNeighbors& other) const {
652 return !(*this == other);
653 }
654};
655
656// The support warm start polytope contains certain points of `this`
657// which are support points in specific directions of space.
658// This struct is used to warm start the support function computation for
659// large meshes (`num_points` > 32).
660template <typename _IndexType>
662 typedef _IndexType IndexType;
663
664 // Array of support points to warm start the support function
665 // computation.
666 std::vector<Vec3s> points;
667
668 // Indices of the support points warm starts.
669 // These are the indices of the real convex, not the indices of points in
670 // the warm start polytope.
671 std::vector<IndexType> indices;
672
673 // Cast to a different index type.
674 template <typename OtherIndexType>
677 ResType res;
678 res.points = this->points;
679 res.indices.clear();
680 for (size_t i = 0; i < this->indices.size(); ++i) {
681 res.indices.push_back(OtherIndexType(this->indices[i]));
682 }
683 return res;
684 }
685};
686
690template <typename _IndexType>
691class ConvexBaseTpl : public ShapeBase {
692 public:
693 // clang-format off
695 // clang-format on
698
699 template <typename OtherIndexType>
700 friend class ConvexBaseTpl;
701
715 std::shared_ptr<std::vector<Vec3s>>& points, unsigned int num_points,
716 bool keepTriangles, const char* qhullCommand = NULL);
717
718 // TODO(louis): put this method in private sometime in the future.
720 const Vec3s* points, unsigned int num_points, bool keepTriangles,
721 const char* qhullCommand = NULL);
722
723 virtual ~ConvexBaseTpl() {}
724
727 Base& base() { return static_cast<Base&>(*this); }
728
731 const Base& base() const { return static_cast<const Base&>(*this); }
732
737
742
745 virtual ConvexBaseTpl* clone() const { return this->deepcopy(); }
746
749 virtual ConvexBaseTpl* deepcopy() const {
751 deepcopy(this, copy);
752 return copy;
753 }
754
757 template <typename OtherIndexType>
763
765 void computeLocalAABB();
766
768 NODE_TYPE getNodeType() const;
769
770#ifdef COAL_HAS_QHULL
774#endif
775
777
781 const std::vector<Neighbors>& nns = *neighbors;
782 IndexType begin_id = nns[i].begin_id;
783#ifndef NDEBUG
784 unsigned char count = nns[i].count;
785 assert(j < count);
786#endif
787 const std::vector<IndexType>& nns_vec = *nneighbors_;
788 return nns_vec[begin_id + j];
789 }
790
793 static constexpr size_t num_vertices_large_convex_threshold = 32;
794
796 std::shared_ptr<std::vector<Vec3s>> points;
797 unsigned int num_points;
798
800 std::shared_ptr<std::vector<Vec3s>> normals;
803 std::shared_ptr<std::vector<Scalar>> offsets;
805
809 std::shared_ptr<std::vector<Neighbors>> neighbors;
810
814
817
819 static constexpr size_t num_support_warm_starts = 14;
820
823
824 protected:
832
838 void initialize(std::shared_ptr<std::vector<Vec3s>> points_,
839 unsigned int num_points_);
840
845 void set(std::shared_ptr<std::vector<Vec3s>> points_,
846 unsigned int num_points_);
847
848#ifdef COAL_HAS_QHULL
849 void COAL_DLLAPI
850 buildDoubleDescriptionFromQHullResult(const orgQhull::Qhull& qh);
851#endif
852
855
861 std::shared_ptr<std::vector<IndexType>> nneighbors_;
862
863 protected:
866 template <typename OtherIndexType>
867 static void deepcopy(const ConvexBaseTpl<IndexType>* source,
869
870 void computeCenter();
871
872 virtual bool isEqual(const CollisionGeometry& _other) const {
873 const ConvexBaseTpl* other_ptr =
874 dynamic_cast<const ConvexBaseTpl*>(&_other);
875 if (other_ptr == nullptr) return false;
876 const ConvexBaseTpl& other = *other_ptr;
877
878 if (num_points != other.num_points) return false;
879
880 if ((!(points.get()) && other.points.get()) ||
881 (points.get() && !(other.points.get())))
882 return false;
883 if (points.get() && other.points.get()) {
884 const std::vector<Vec3s>& points_ = *points;
885 const std::vector<Vec3s>& other_points_ = *(other.points);
886 for (unsigned int i = 0; i < num_points; ++i) {
887 if (points_[i] != (other_points_)[i]) return false;
888 }
889 }
890
891 if ((!(neighbors.get()) && other.neighbors.get()) ||
892 (neighbors.get() && !(other.neighbors.get())))
893 return false;
894 if (neighbors.get() && other.neighbors.get()) {
895 const std::vector<Neighbors>& neighbors_ = *neighbors;
896 const std::vector<Neighbors>& other_neighbors_ = *(other.neighbors);
897 for (unsigned int i = 0; i < num_points; ++i) {
898 if (neighbors_[i] != other_neighbors_[i]) return false;
899 }
900 }
901
902 if ((!(normals.get()) && other.normals.get()) ||
903 (normals.get() && !(other.normals.get())))
904 return false;
905 if (normals.get() && other.normals.get()) {
906 const std::vector<Vec3s>& normals_ = *normals;
907 const std::vector<Vec3s>& other_normals_ = *(other.normals);
908 for (unsigned int i = 0; i < num_normals_and_offsets; ++i) {
909 if (normals_[i] != other_normals_[i]) return false;
910 }
911 }
912
913 if ((!(offsets.get()) && other.offsets.get()) ||
914 (offsets.get() && !(other.offsets.get())))
915 return false;
916 if (offsets.get() && other.offsets.get()) {
917 const std::vector<Scalar>& offsets_ = *offsets;
918 const std::vector<Scalar>& other_offsets_ = *(other.offsets);
919 for (unsigned int i = 0; i < num_normals_and_offsets; ++i) {
920 if (offsets_[i] != other_offsets_[i]) return false;
921 }
922 }
923
924 if (this->support_warm_starts.points.size() !=
925 other.support_warm_starts.points.size() ||
926 this->support_warm_starts.indices.size() !=
927 other.support_warm_starts.indices.size()) {
928 return false;
929 }
930
931 for (size_t i = 0; i < this->support_warm_starts.points.size(); ++i) {
932 if (this->support_warm_starts.points[i] !=
933 other.support_warm_starts.points[i] ||
934 this->support_warm_starts.indices[i] !=
935 other.support_warm_starts.indices[i]) {
936 return false;
937 }
938 }
939
940 return center == other.center &&
941 getSweptSphereRadius() == other.getSweptSphereRadius();
942 }
943
944 public:
946};
947
950// typedef ConvexBase32 ConvexBase;
951
952template <typename PolygonT>
953class ConvexTpl;
954
964 public:
966 Halfspace(const Vec3s& n_, Scalar d_) : ShapeBase(), n(n_), d(d_) {
967 unitNormalTest();
968 }
969
972 : ShapeBase(), n(a, b, c), d(d_) {
973 unitNormalTest();
974 }
975
976 Halfspace() : ShapeBase(), n(1, 0, 0), d(0) {}
977
978 Halfspace(const Halfspace& other)
979 : ShapeBase(other), n(other.n), d(other.d) {}
980
983 n = other.n;
984 d = other.d;
985 return *this;
986 }
987
989 virtual Halfspace* clone() const { return new Halfspace(*this); };
990
991 Scalar signedDistance(const Vec3s& p) const {
992 return n.dot(p) - (d + this->getSweptSphereRadius());
993 }
994
995 Scalar distance(const Vec3s& p) const {
996 return std::abs(this->signedDistance(p));
997 }
998
1001
1004
1006 return std::numeric_limits<Scalar>::lowest();
1007 }
1008
1017 std::pair<Halfspace, Transform3s> inflated(const Scalar value) const {
1018 if (value <= minInflationValue())
1019 COAL_THROW_PRETTY("value (" << value
1020 << ") is two small. It should be at least: "
1021 << minInflationValue(),
1022 std::invalid_argument);
1023 return std::make_pair(Halfspace(n, d + value), Transform3s());
1024 }
1025
1028
1031
1032 protected:
1035
1036 private:
1037 virtual bool isEqual(const CollisionGeometry& _other) const {
1038 const Halfspace* other_ptr = dynamic_cast<const Halfspace*>(&_other);
1039 if (other_ptr == nullptr) return false;
1040 const Halfspace& other = *other_ptr;
1041
1042 return n == other.n && d == other.d &&
1043 getSweptSphereRadius() == other.getSweptSphereRadius();
1044 }
1045
1046 public:
1047 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
1048};
1049
1055 public:
1057 Plane(const Vec3s& n_, Scalar d_) : ShapeBase(), n(n_), d(d_) {
1058 unitNormalTest();
1059 }
1060
1063 : ShapeBase(), n(a, b, c), d(d_) {
1064 unitNormalTest();
1065 }
1066
1067 Plane() : ShapeBase(), n(1, 0, 0), d(0) {}
1068
1069 Plane(const Plane& other) : ShapeBase(other), n(other.n), d(other.d) {}
1070
1072 Plane& operator=(const Plane& other) {
1073 n = other.n;
1074 d = other.d;
1075 return *this;
1076 }
1077
1079 virtual Plane* clone() const { return new Plane(*this); };
1080
1081 Scalar signedDistance(const Vec3s& p) const {
1082 const Scalar dist = n.dot(p) - d;
1083 Scalar signed_dist = std::abs(n.dot(p) - d) - this->getSweptSphereRadius();
1084 if (dist >= 0) {
1085 return signed_dist;
1086 }
1087 if (signed_dist >= 0) {
1088 return -signed_dist;
1089 }
1090 return signed_dist;
1091 }
1092
1093 Scalar distance(const Vec3s& p) const {
1094 return std::abs(std::abs(n.dot(p) - d) - this->getSweptSphereRadius());
1095 }
1096
1099
1102
1105
1108
1109 protected:
1112
1113 private:
1114 virtual bool isEqual(const CollisionGeometry& _other) const {
1115 const Plane* other_ptr = dynamic_cast<const Plane*>(&_other);
1116 if (other_ptr == nullptr) return false;
1117 const Plane& other = *other_ptr;
1118
1119 return n == other.n && d == other.d &&
1120 getSweptSphereRadius() == other.getSweptSphereRadius();
1121 }
1122
1123 public:
1124 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
1125};
1126
// end of Geometric_Shapes
1128
1129} // namespace coal
1130
1132
1133#endif
Center at zero point, axis aligned box.
Definition geometric_shapes.h:164
std::pair< Box, Transform3s > inflated(const Scalar value) const
Inflate the box by an amount given by value. This value can be positive or negative but must always >...
Definition geometric_shapes.h:213
Box()
Default constructor.
Definition geometric_shapes.h:184
Box & operator=(const Box &other)
Definition geometric_shapes.h:173
void computeLocalAABB()
Compute AABB.
Box(Scalar x, Scalar y, Scalar z)
Definition geometric_shapes.h:166
Vec3s halfSide
box side half-length
Definition geometric_shapes.h:187
Scalar minInflationValue() const
Definition geometric_shapes.h:203
Scalar computeVolume() const
compute the volume
Definition geometric_shapes.h:195
Matrix3s computeMomentofInertia() const
compute the inertia matrix, related to the origin
Definition geometric_shapes.h:197
virtual Box * clone() const
Clone *this into a new Box.
Definition geometric_shapes.h:181
Box(const Vec3s &side_)
Definition geometric_shapes.h:169
Box(const Box &other)
Definition geometric_shapes.h:171
NODE_TYPE getNodeType() const
Get node type: a box.
Definition geometric_shapes.h:193
Capsule It is where is the distance between the point x and the capsule segment AB,...
Definition geometric_shapes.h:381
Scalar minInflationValue() const
Definition geometric_shapes.h:429
Matrix3s computeMomentofInertia() const
compute the inertia matrix, related to the origin
Definition geometric_shapes.h:413
Scalar halfLength
Half Length along z axis.
Definition geometric_shapes.h:400
Capsule(Scalar radius_, Scalar lz_)
Definition geometric_shapes.h:386
Capsule(const Capsule &other)
Definition geometric_shapes.h:390
void computeLocalAABB()
Compute AABB.
Scalar computeVolume() const
compute the volume
Definition geometric_shapes.h:408
std::pair< Capsule, Transform3s > inflated(const Scalar value) const
Inflate the capsule by an amount given by value. This value can be positive or negative but must alwa...
Definition geometric_shapes.h:439
virtual Capsule * clone() const
Clone *this into a new Capsule.
Definition geometric_shapes.h:394
Capsule()
Default constructor.
Definition geometric_shapes.h:384
Scalar radius
Radius of capsule.
Definition geometric_shapes.h:397
NODE_TYPE getNodeType() const
Get node type: a capsule.
Definition geometric_shapes.h:406
The geometry for the object for collision or distance computation.
Definition collision_object.h:96
Cone The base of the cone is at and the top is at .
Definition geometric_shapes.h:466
Scalar halfLength
Half Length along z axis.
Definition geometric_shapes.h:485
Cone(Scalar radius_, Scalar lz_)
Definition geometric_shapes.h:471
NODE_TYPE getNodeType() const
Get node type: a cone.
Definition geometric_shapes.h:491
Scalar minInflationValue() const
Definition geometric_shapes.h:509
void computeLocalAABB()
Compute AABB.
Scalar radius
Radius of the cone.
Definition geometric_shapes.h:482
std::pair< Cone, Transform3s > inflated(const Scalar value) const
Inflate the cone by an amount given by value. This value can be positive or negative but must always ...
Definition geometric_shapes.h:519
Vec3s computeCOM() const
compute center of mass
Definition geometric_shapes.h:507
Scalar computeVolume() const
compute the volume
Definition geometric_shapes.h:493
virtual Cone * clone() const
Clone *this into a new Cone.
Definition geometric_shapes.h:479
Matrix3s computeMomentofInertia() const
compute the inertia matrix, related to the origin
Definition geometric_shapes.h:498
Cone()
Default constructor.
Definition geometric_shapes.h:469
Cone(const Cone &other)
Definition geometric_shapes.h:475
Base for convex polytope.
Definition geometric_shapes.h:691
Base & base()
Cast ConvexBaseTpl to ShapeBase. This method should never be marked as virtual.
Definition geometric_shapes.h:727
ConvexBaseTpl< OtherIndexType > cast() const
Cast this ConvexBase vertex indices to OtherIndexType. This effectively deep copies this ConvexBaseTp...
Definition geometric_shapes.h:758
unsigned int num_points
Definition geometric_shapes.h:797
ShapeBase Base
Definition geometric_shapes.h:697
std::shared_ptr< std::vector< Vec3s > > normals
An array of the normals of the polygon.
Definition geometric_shapes.h:800
SupportWarmStartPolytope support_warm_starts
Support warm start polytopes.
Definition geometric_shapes.h:822
std::shared_ptr< std::vector< Vec3s > > points
An array of the points of the polygon.
Definition geometric_shapes.h:796
_IndexType IndexType
Definition geometric_shapes.h:696
static ConvexBaseTpl * convexHull(std::shared_ptr< std::vector< Vec3s > > &points, unsigned int num_points, bool keepTriangles, const char *qhullCommand=NULL)
Build a convex hull based on Qhull library and store the vertices and optionally the triangles.
Vec3s center
center of the convex polytope, this is used for collision: center is guaranteed in the internal of th...
Definition geometric_shapes.h:813
virtual ~ConvexBaseTpl()
Definition geometric_shapes.h:723
static constexpr size_t num_vertices_large_convex_threshold
Above this threshold, the convex polytope is considered large. This influcences the way the support f...
Definition geometric_shapes.h:793
void buildSupportWarmStart()
Build the support points warm starts.
void computeCenter()
Definition geometric_shapes.hxx:184
_IndexType index_type
Definition geometric_shapes.h:694
ConvexBaseTpl & operator=(const ConvexBaseTpl &other)
Copy assignment operator. The copy assignment operator shallow copies the data, just as the copy cons...
Definition geometric_shapes.hxx:88
static constexpr size_t num_support_warm_starts
Number of support warm starts.
Definition geometric_shapes.h:819
void computeLocalAABB()
Compute AABB.
Definition geometric_shapes.hxx:197
std::shared_ptr< std::vector< Neighbors > > neighbors
Neighbors of each vertex. It is an array of size num_points. For each vertex, it contains the number ...
Definition geometric_shapes.h:809
const Base & base() const
Const cast ConvexBaseTpl to ShapeBase. This method should never be marked as virtual.
Definition geometric_shapes.h:731
NODE_TYPE getNodeType() const
Get node type: a convex polytope.
Definition geometric_shapes.hxx:50
friend class ConvexBaseTpl
Definition geometric_shapes.h:700
std::shared_ptr< std::vector< Scalar > > offsets
An array of the offsets to the normals of the polygon. Note: there are as many offsets as normals.
Definition geometric_shapes.h:803
virtual bool isEqual(const CollisionGeometry &_other) const
equal operator with another object of derived type.
Definition geometric_shapes.h:872
void set(std::shared_ptr< std::vector< Vec3s > > points_, unsigned int num_points_)
Set the points of the convex shape.
Definition geometric_shapes.hxx:82
static ConvexBaseTpl * convexHull(const Vec3s *points, unsigned int num_points, bool keepTriangles, const char *qhullCommand=NULL)
ConvexBaseTpl()
Construct an uninitialized convex object Initialization is done with ConvexBase::initialize.
Definition geometric_shapes.h:827
virtual ConvexBaseTpl * deepcopy() const
Deep copy of the ConvexBaseTpl. This method deep copies every field of the class.
Definition geometric_shapes.h:749
virtual ConvexBaseTpl * clone() const
Clone (deep copy).
Definition geometric_shapes.h:745
IndexType neighbor(IndexType i, IndexType j) const
Get the index of the j-th neighbor of the i-th vertex.
Definition geometric_shapes.h:779
ConvexBaseTpl(const ConvexBaseTpl &other)
Copy constructor. The copy constructor only shallow copies the data (it copies the shared pointers bu...
Definition geometric_shapes.h:736
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
unsigned int num_normals_and_offsets
Definition geometric_shapes.h:804
std::shared_ptr< std::vector< IndexType > > nneighbors_
Array of indices of the neighbors of each vertex. Since we don't know a priori the number of neighbor...
Definition geometric_shapes.h:861
Convex polytope.
Definition convex.h:50
Cylinder along Z axis. The cylinder is defined at its centroid.
Definition geometric_shapes.h:556
Matrix3s computeMomentofInertia() const
compute the inertia matrix, related to the origin
Definition geometric_shapes.h:596
Scalar radius
Radius of the cylinder.
Definition geometric_shapes.h:580
Scalar computeVolume() const
compute the volume
Definition geometric_shapes.h:591
Cylinder(Scalar radius_, Scalar lz_)
Definition geometric_shapes.h:561
Cylinder()
Default constructor.
Definition geometric_shapes.h:559
Scalar minInflationValue() const
Definition geometric_shapes.h:603
std::pair< Cylinder, Transform3s > inflated(const Scalar value) const
Inflate the cylinder by an amount given by value. This value can be positive or negative but must alw...
Definition geometric_shapes.h:613
void computeLocalAABB()
Compute AABB.
Scalar halfLength
Half Length along z axis.
Definition geometric_shapes.h:583
Cylinder(const Cylinder &other)
Definition geometric_shapes.h:565
virtual Cylinder * clone() const
Clone *this into a new Cylinder.
Definition geometric_shapes.h:577
NODE_TYPE getNodeType() const
Get node type: a cylinder.
Definition geometric_shapes.h:589
Cylinder & operator=(const Cylinder &other)
Definition geometric_shapes.h:568
Ellipsoid centered at point zero.
Definition geometric_shapes.h:303
virtual Ellipsoid * clone() const
Clone *this into a new Ellipsoid.
Definition geometric_shapes.h:315
Ellipsoid(const Ellipsoid &other)
Definition geometric_shapes.h:312
Ellipsoid()
Default constructor.
Definition geometric_shapes.h:306
void computeLocalAABB()
Compute AABB.
Matrix3s computeMomentofInertia() const
compute the inertia matrix, related to the origin
Definition geometric_shapes.h:327
NODE_TYPE getNodeType() const
Get node type: an ellipsoid.
Definition geometric_shapes.h:325
Ellipsoid(const Vec3s &radii)
Definition geometric_shapes.h:310
Scalar computeVolume() const
compute the volume
Definition geometric_shapes.h:338
Vec3s radii
Radii of the Ellipsoid (such that on boundary: x^2/rx^2 + y^2/ry^2.
Definition geometric_shapes.h:319
Scalar minInflationValue() const
Definition geometric_shapes.h:343
Ellipsoid(Scalar rx, Scalar ry, Scalar rz)
Definition geometric_shapes.h:308
std::pair< Ellipsoid, Transform3s > inflated(const Scalar value) const
Inflate the ellipsoid by an amount given by value. This value can be positive or negative but must al...
Definition geometric_shapes.h:353
Half Space: this is equivalent to the Plane in ODE. A Half space has a priviledged direction: the dir...
Definition geometric_shapes.h:963
virtual Halfspace * clone() const
Clone *this into a new Halfspace.
Definition geometric_shapes.h:989
Scalar minInflationValue() const
Definition geometric_shapes.h:1005
Vec3s n
Plane normal.
Definition geometric_shapes.h:1027
Halfspace & operator=(const Halfspace &other)
operator =
Definition geometric_shapes.h:982
Scalar d
Plane offset.
Definition geometric_shapes.h:1030
std::pair< Halfspace, Transform3s > inflated(const Scalar value) const
Inflate the halfspace by an amount given by value. This value can be positive or negative but must al...
Definition geometric_shapes.h:1017
Scalar distance(const Vec3s &p) const
Definition geometric_shapes.h:995
Halfspace(const Halfspace &other)
Definition geometric_shapes.h:978
Halfspace(Scalar a, Scalar b, Scalar c, Scalar d_)
Construct a plane with normal direction and offset.
Definition geometric_shapes.h:971
Halfspace()
Definition geometric_shapes.h:976
Halfspace(const Vec3s &n_, Scalar d_)
Construct a half space with normal direction and offset.
Definition geometric_shapes.h:966
Scalar signedDistance(const Vec3s &p) const
Definition geometric_shapes.h:991
void unitNormalTest()
Turn non-unit normal into unit.
NODE_TYPE getNodeType() const
Get node type: a half space.
Definition geometric_shapes.h:1003
void computeLocalAABB()
Compute AABB.
Infinite plane. A plane can be viewed as two half spaces; it has no priviledged direction....
Definition geometric_shapes.h:1054
virtual Plane * clone() const
Clone *this into a new Plane.
Definition geometric_shapes.h:1079
Scalar distance(const Vec3s &p) const
Definition geometric_shapes.h:1093
Plane()
Definition geometric_shapes.h:1067
Vec3s n
Plane normal.
Definition geometric_shapes.h:1104
Plane & operator=(const Plane &other)
operator =
Definition geometric_shapes.h:1072
Plane(const Vec3s &n_, Scalar d_)
Construct a plane with normal direction and offset.
Definition geometric_shapes.h:1057
Plane(const Plane &other)
Definition geometric_shapes.h:1069
void unitNormalTest()
Turn non-unit normal into unit.
void computeLocalAABB()
Compute AABB.
Scalar signedDistance(const Vec3s &p) const
Definition geometric_shapes.h:1081
NODE_TYPE getNodeType() const
Get node type: a plane.
Definition geometric_shapes.h:1101
Scalar d
Plane offset.
Definition geometric_shapes.h:1107
Plane(Scalar a, Scalar b, Scalar c, Scalar d_)
Construct a plane with normal direction and offset.
Definition geometric_shapes.h:1062
Base class for all basic geometric shapes.
Definition geometric_shapes.h:58
virtual ~ShapeBase()
Definition geometric_shapes.h:69
ShapeBase(const ShapeBase &other)
&#160;
Definition geometric_shapes.h:63
void setSweptSphereRadius(Scalar radius)
Set radius of sphere swept around the shape. Must be >= 0.
Definition geometric_shapes.h:76
OBJECT_TYPE getObjectType() const
Get object type: a geometric shape.
Definition geometric_shapes.h:72
ShapeBase & operator=(const ShapeBase &other)=default
Scalar getSweptSphereRadius() const
Get radius of sphere swept around the shape. This radius is always >= 0.
Definition geometric_shapes.h:86
ShapeBase()
Definition geometric_shapes.h:60
Center at zero point sphere.
Definition geometric_shapes.h:238
Scalar minInflationValue() const
Definition geometric_shapes.h:269
Matrix3s computeMomentofInertia() const
compute the inertia matrix, related to the origin
Definition geometric_shapes.h:259
Sphere(const Sphere &other)
Definition geometric_shapes.h:245
void computeLocalAABB()
Compute AABB.
Sphere()
Default constructor.
Definition geometric_shapes.h:241
Scalar radius
Radius of the sphere.
Definition geometric_shapes.h:251
NODE_TYPE getNodeType() const
Get node type: a sphere.
Definition geometric_shapes.h:257
Sphere(Scalar radius_)
Definition geometric_shapes.h:243
virtual Sphere * clone() const
Clone *this into a new Sphere.
Definition geometric_shapes.h:248
std::pair< Sphere, Transform3s > inflated(const Scalar value) const
Inflate the sphere by an amount given by value. This value can be positive or negative but must alway...
Definition geometric_shapes.h:279
Scalar computeVolume() const
compute the volume
Definition geometric_shapes.h:264
Simple transform class used locally by InterpMotion.
Definition transform.h:55
Triangle stores the points instead of only indices of points.
Definition geometric_shapes.h:108
virtual TriangleP * clone() const
Clone *this into a new TriangleP.
Definition geometric_shapes.h:119
NODE_TYPE getNodeType() const
get the node type
Definition geometric_shapes.h:124
TriangleP(const TriangleP &other)
Definition geometric_shapes.h:115
Vec3s a
Definition geometric_shapes.h:147
Vec3s b
Definition geometric_shapes.h:147
Vec3s c
Definition geometric_shapes.h:147
void computeLocalAABB()
virtual function of compute AABB in local coordinate
TriangleP()
Definition geometric_shapes.h:110
TriangleP(const Vec3s &a_, const Vec3s &b_, const Vec3s &c_)
Definition geometric_shapes.h:112
#define COAL_DLLAPI
Definition config.hh:88
#define COAL_DEPRECATED
Definition deprecated.hh:37
#define COAL_DEPRECATED_MESSAGE(message)
Definition deprecated.hh:38
#define COAL_THROW_PRETTY(message, exception)
Definition fwd.hh:64
@ GEOM_CONE
Definition collision_object.h:77
@ GEOM_TRIANGLE
Definition collision_object.h:84
@ GEOM_BOX
Definition collision_object.h:74
@ GEOM_SPHERE
Definition collision_object.h:75
@ GEOM_CYLINDER
Definition collision_object.h:78
@ GEOM_CAPSULE
Definition collision_object.h:76
@ GEOM_ELLIPSOID
Definition collision_object.h:86
@ GEOM_HALFSPACE
Definition collision_object.h:83
@ GEOM_PLANE
Definition collision_object.h:82
@ OT_GEOM
Definition collision_object.h:55
ConvexBaseTpl< Triangle32::IndexType > ConvexBase32
Definition geometric_shapes.h:949
ConvexBaseTpl< Triangle16::IndexType > ConvexBase16
Definition geometric_shapes.h:948
Main namespace.
Definition broadphase_bruteforce.h:44
NODE_TYPE
traversal node type: bounding volume (AABB, OBB, RSS, kIOS, OBBRSS, KDOP16, KDOP18,...
Definition collision_object.h:64
Eigen::Matrix< Scalar, 3, 1 > Vec3s
Definition data_types.h:70
double Scalar
Definition data_types.h:68
OBJECT_TYPE
object type: BVH (mesh, points), basic geometry, octree
Definition collision_object.h:52
Eigen::Matrix< Scalar, 3, 3 > Matrix3s
Definition data_types.h:74
bool isEqual(const Eigen::MatrixBase< Derived > &lhs, const Eigen::MatrixBase< OtherDerived > &rhs, const Scalar tol=std::numeric_limits< Scalar >::epsilon() *100)
Definition tools.h:204
Definition geometric_shapes.h:638
_IndexType IndexType
Definition geometric_shapes.h:639
unsigned char count
Definition geometric_shapes.h:641
bool operator==(const ConvexBaseTplNeighbors &other) const
Definition geometric_shapes.h:644
bool operator!=(const ConvexBaseTplNeighbors &other) const
Definition geometric_shapes.h:651
IndexType begin_id
Definition geometric_shapes.h:642
Definition geometric_shapes.h:661
std::vector< Vec3s > points
Definition geometric_shapes.h:666
std::vector< IndexType > indices
Definition geometric_shapes.h:671
_IndexType IndexType
Definition geometric_shapes.h:662
ConvexBaseTplSupportWarmStartPolytope< OtherIndexType > cast() const
Definition geometric_shapes.h:675