coal  3.0.1
Coal, The Collision Detection Library. Previously known as HPP-FCL, fork of FCL -- The Flexible Collision Library
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 
46 #include "coal/collision_object.h"
47 #include "coal/data_types.h"
48 
49 #ifdef COAL_HAS_QHULL
50 namespace orgQhull {
51 class Qhull;
52 }
53 #endif
54 
55 namespace coal {
56 
59  public:
60  ShapeBase() {}
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:
110  TriangleP() {};
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 
124  NODE_TYPE getNodeType() const { return GEOM_TRIANGLE; }
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 
164 class 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 
238 class COAL_DLLAPI Sphere : public ShapeBase {
239  public:
241  Sphere() {}
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 
257  NODE_TYPE getNodeType() const { return GEOM_SPHERE; }
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 
381 class COAL_DLLAPI Capsule : public ShapeBase {
382  public:
384  Capsule() {}
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 
406  NODE_TYPE getNodeType() const { return GEOM_CAPSULE; }
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 
466 class 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:
559  Cylinder() {}
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 
589  NODE_TYPE getNodeType() const { return GEOM_CYLINDER; }
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 
640  public:
653  static ConvexBase* convexHull(std::shared_ptr<std::vector<Vec3s>>& points,
654  unsigned int num_points, bool keepTriangles,
655  const char* qhullCommand = NULL);
656 
657  // TODO(louis): put this method in private sometime in the future.
659  const Vec3s* points, unsigned int num_points, bool keepTriangles,
660  const char* qhullCommand = NULL);
661 
662  virtual ~ConvexBase();
663 
667  virtual ConvexBase* clone() const { return new ConvexBase(*this); }
668 
671 
673  NODE_TYPE getNodeType() const { return GEOM_CONVEX; }
674 
675 #ifdef COAL_HAS_QHULL
678  void buildDoubleDescription();
679 #endif
680 
682  unsigned char count_;
683  unsigned int* n_;
684 
685  unsigned char const& count() const { return count_; }
686  unsigned int& operator[](int i) {
687  assert(i < count_);
688  return n_[i];
689  }
690  unsigned int const& operator[](int i) const {
691  assert(i < count_);
692  return n_[i];
693  }
694 
695  bool operator==(const Neighbors& other) const {
696  if (count_ != other.count_) return false;
697 
698  for (int i = 0; i < count_; ++i) {
699  if (n_[i] != other.n_[i]) return false;
700  }
701 
702  return true;
703  }
704 
705  bool operator!=(const Neighbors& other) const { return !(*this == other); }
706  };
707 
710  static constexpr size_t num_vertices_large_convex_threshold = 32;
711 
713  std::shared_ptr<std::vector<Vec3s>> points;
714  unsigned int num_points;
715 
717  std::shared_ptr<std::vector<Vec3s>> normals;
720  std::shared_ptr<std::vector<Scalar>> offsets;
722 
726  std::shared_ptr<std::vector<Neighbors>> neighbors;
727 
731 
739  std::vector<Vec3s> points;
740 
744  std::vector<int> indices;
745  };
746 
748  static constexpr size_t num_support_warm_starts = 14;
749 
752 
753  protected:
757  : ShapeBase(),
758  num_points(0),
759  num_normals_and_offsets(0),
760  center(Vec3s::Zero()) {}
761 
768  void initialize(std::shared_ptr<std::vector<Vec3s>> points_,
769  unsigned int num_points_);
770 
776  void set(std::shared_ptr<std::vector<Vec3s>> points_,
777  unsigned int num_points_);
778 
781  ConvexBase(const ConvexBase& other);
782 
783 #ifdef COAL_HAS_QHULL
784  void buildDoubleDescriptionFromQHullResult(const orgQhull::Qhull& qh);
785 #endif
786 
789 
795  std::shared_ptr<std::vector<unsigned int>> nneighbors_;
796 
797  private:
798  void computeCenter();
799 
800  virtual bool isEqual(const CollisionGeometry& _other) const {
801  const ConvexBase* other_ptr = dynamic_cast<const ConvexBase*>(&_other);
802  if (other_ptr == nullptr) return false;
803  const ConvexBase& other = *other_ptr;
804 
805  if (num_points != other.num_points) return false;
806 
807  if ((!(points.get()) && other.points.get()) ||
808  (points.get() && !(other.points.get())))
809  return false;
810  if (points.get() && other.points.get()) {
811  const std::vector<Vec3s>& points_ = *points;
812  const std::vector<Vec3s>& other_points_ = *(other.points);
813  for (unsigned int i = 0; i < num_points; ++i) {
814  if (points_[i] != (other_points_)[i]) return false;
815  }
816  }
817 
818  if ((!(neighbors.get()) && other.neighbors.get()) ||
819  (neighbors.get() && !(other.neighbors.get())))
820  return false;
821  if (neighbors.get() && other.neighbors.get()) {
822  const std::vector<Neighbors>& neighbors_ = *neighbors;
823  const std::vector<Neighbors>& other_neighbors_ = *(other.neighbors);
824  for (unsigned int i = 0; i < num_points; ++i) {
825  if (neighbors_[i] != other_neighbors_[i]) return false;
826  }
827  }
828 
829  if ((!(normals.get()) && other.normals.get()) ||
830  (normals.get() && !(other.normals.get())))
831  return false;
832  if (normals.get() && other.normals.get()) {
833  const std::vector<Vec3s>& normals_ = *normals;
834  const std::vector<Vec3s>& other_normals_ = *(other.normals);
835  for (unsigned int i = 0; i < num_normals_and_offsets; ++i) {
836  if (normals_[i] != other_normals_[i]) return false;
837  }
838  }
839 
840  if ((!(offsets.get()) && other.offsets.get()) ||
841  (offsets.get() && !(other.offsets.get())))
842  return false;
843  if (offsets.get() && other.offsets.get()) {
844  const std::vector<Scalar>& offsets_ = *offsets;
845  const std::vector<Scalar>& other_offsets_ = *(other.offsets);
846  for (unsigned int i = 0; i < num_normals_and_offsets; ++i) {
847  if (offsets_[i] != other_offsets_[i]) return false;
848  }
849  }
850 
851  if (this->support_warm_starts.points.size() !=
852  other.support_warm_starts.points.size() ||
853  this->support_warm_starts.indices.size() !=
854  other.support_warm_starts.indices.size()) {
855  return false;
856  }
857 
858  for (size_t i = 0; i < this->support_warm_starts.points.size(); ++i) {
859  if (this->support_warm_starts.points[i] !=
860  other.support_warm_starts.points[i] ||
861  this->support_warm_starts.indices[i] !=
862  other.support_warm_starts.indices[i]) {
863  return false;
864  }
865  }
866 
867  return center == other.center &&
868  getSweptSphereRadius() == other.getSweptSphereRadius();
869  }
870 
871  public:
872  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
873 };
874 
875 template <typename PolygonT>
876 class Convex;
877 
887  public:
889  Halfspace(const Vec3s& n_, Scalar d_) : ShapeBase(), n(n_), d(d_) {
890  unitNormalTest();
891  }
892 
895  : ShapeBase(), n(a, b, c), d(d_) {
896  unitNormalTest();
897  }
898 
899  Halfspace() : ShapeBase(), n(1, 0, 0), d(0) {}
900 
901  Halfspace(const Halfspace& other)
902  : ShapeBase(other), n(other.n), d(other.d) {}
903 
905  Halfspace& operator=(const Halfspace& other) {
906  n = other.n;
907  d = other.d;
908  return *this;
909  }
910 
912  virtual Halfspace* clone() const { return new Halfspace(*this); };
913 
914  Scalar signedDistance(const Vec3s& p) const {
915  return n.dot(p) - (d + this->getSweptSphereRadius());
916  }
917 
918  Scalar distance(const Vec3s& p) const {
919  return std::abs(this->signedDistance(p));
920  }
921 
924 
927 
929  return std::numeric_limits<Scalar>::lowest();
930  }
931 
940  std::pair<Halfspace, Transform3s> inflated(const Scalar value) const {
941  if (value <= minInflationValue())
942  COAL_THROW_PRETTY("value (" << value
943  << ") is two small. It should be at least: "
944  << minInflationValue(),
945  std::invalid_argument);
946  return std::make_pair(Halfspace(n, d + value), Transform3s());
947  }
948 
951 
954 
955  protected:
958 
959  private:
960  virtual bool isEqual(const CollisionGeometry& _other) const {
961  const Halfspace* other_ptr = dynamic_cast<const Halfspace*>(&_other);
962  if (other_ptr == nullptr) return false;
963  const Halfspace& other = *other_ptr;
964 
965  return n == other.n && d == other.d &&
966  getSweptSphereRadius() == other.getSweptSphereRadius();
967  }
968 
969  public:
970  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
971 };
972 
977 class COAL_DLLAPI Plane : public ShapeBase {
978  public:
980  Plane(const Vec3s& n_, Scalar d_) : ShapeBase(), n(n_), d(d_) {
981  unitNormalTest();
982  }
983 
986  : ShapeBase(), n(a, b, c), d(d_) {
987  unitNormalTest();
988  }
989 
990  Plane() : ShapeBase(), n(1, 0, 0), d(0) {}
991 
992  Plane(const Plane& other) : ShapeBase(other), n(other.n), d(other.d) {}
993 
995  Plane& operator=(const Plane& other) {
996  n = other.n;
997  d = other.d;
998  return *this;
999  }
1000 
1002  virtual Plane* clone() const { return new Plane(*this); };
1003 
1004  Scalar signedDistance(const Vec3s& p) const {
1005  const Scalar dist = n.dot(p) - d;
1006  Scalar signed_dist = std::abs(n.dot(p) - d) - this->getSweptSphereRadius();
1007  if (dist >= 0) {
1008  return signed_dist;
1009  }
1010  if (signed_dist >= 0) {
1011  return -signed_dist;
1012  }
1013  return signed_dist;
1014  }
1015 
1016  Scalar distance(const Vec3s& p) const {
1017  return std::abs(std::abs(n.dot(p) - d) - this->getSweptSphereRadius());
1018  }
1019 
1022 
1024  NODE_TYPE getNodeType() const { return GEOM_PLANE; }
1025 
1028 
1031 
1032  protected:
1035 
1036  private:
1037  virtual bool isEqual(const CollisionGeometry& _other) const {
1038  const Plane* other_ptr = dynamic_cast<const Plane*>(&_other);
1039  if (other_ptr == nullptr) return false;
1040  const Plane& 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 
1050 } // namespace coal
1051 
1052 #endif
Center at zero point, axis aligned box.
Definition: geometric_shapes.h:164
Capsule It is where is the distance between the point x and the capsule segment AB,...
Definition: geometric_shapes.h:381
The geometry for the object for collision or distance computation.
Definition: collision_object.h:94
Cone The base of the cone is at and the top is at .
Definition: geometric_shapes.h:466
Base for convex polytope.
Definition: geometric_shapes.h:639
Cylinder along Z axis. The cylinder is defined at its centroid.
Definition: geometric_shapes.h:556
Ellipsoid centered at point zero.
Definition: geometric_shapes.h:303
Half Space: this is equivalent to the Plane in ODE. A Half space has a priviledged direction: the dir...
Definition: geometric_shapes.h:886
Infinite plane. A plane can be viewed as two half spaces; it has no priviledged direction....
Definition: geometric_shapes.h:977
Base class for all basic geometric shapes.
Definition: geometric_shapes.h:58
Center at zero point sphere.
Definition: geometric_shapes.h:238
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
#define COAL_DLLAPI
Definition: config.hh:88
#define COAL_DEPRECATED
Definition: deprecated.hh:37
#define COAL_THROW_PRETTY(message, exception)
Definition: fwd.hh:64
@ GEOM_CONE
Definition: collision_object.h:77
@ GEOM_TRIANGLE
Definition: collision_object.h:82
@ 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:84
@ GEOM_HALFSPACE
Definition: collision_object.h:81
@ GEOM_PLANE
Definition: collision_object.h:80
@ GEOM_CONVEX
Definition: collision_object.h:79
@ OT_GEOM
Definition: collision_object.h:55
Scalar minInflationValue() const
Definition: geometric_shapes.h:429
Matrix3s computeMomentofInertia() const
compute the inertia matrix, related to the origin
Definition: geometric_shapes.h:596
virtual ~ConvexBase()
Matrix3s computeMomentofInertia() const
compute the inertia matrix, related to the origin
Definition: geometric_shapes.h:413
ShapeBase & operator=(const ShapeBase &other)=default
virtual Capsule * clone() const
Clone *this into a new Capsule.
Definition: geometric_shapes.h:394
Scalar minInflationValue() const
Definition: geometric_shapes.h:269
virtual ~ShapeBase()
Definition: geometric_shapes.h:69
ShapeBase(const ShapeBase &other)
&#160;
Definition: geometric_shapes.h:63
Scalar minInflationValue() const
Definition: geometric_shapes.h:928
Scalar halfLength
Half Length along z axis.
Definition: geometric_shapes.h:400
Scalar distance(const Vec3s &p) const
Definition: geometric_shapes.h:1016
Vec3s n
Plane normal.
Definition: geometric_shapes.h:950
Scalar radius
Radius of the cylinder.
Definition: geometric_shapes.h:577
std::shared_ptr< std::vector< unsigned int > > 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:795
unsigned int * n_
Definition: geometric_shapes.h:683
Scalar computeVolume() const
compute the volume
Definition: geometric_shapes.h:591
bool operator==(const Neighbors &other) const
Definition: geometric_shapes.h:695
Plane()
Definition: geometric_shapes.h:990
Capsule(Scalar radius_, Scalar lz_)
Definition: geometric_shapes.h:386
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
Box & operator=(const Box &other)
Definition: geometric_shapes.h:173
Capsule(const Capsule &other)
Definition: geometric_shapes.h:390
Cylinder(Scalar radius_, Scalar lz_)
Definition: geometric_shapes.h:561
unsigned int const & operator[](int i) const
Definition: geometric_shapes.h:690
Halfspace & operator=(const Halfspace &other)
operator =
Definition: geometric_shapes.h:905
Scalar d
Plane offset.
Definition: geometric_shapes.h:953
bool operator!=(const Neighbors &other) const
Definition: geometric_shapes.h:705
Vec3s n
Plane normal.
Definition: geometric_shapes.h:1027
virtual Box * clone() const
Clone *this into a new Box.
Definition: geometric_shapes.h:181
Matrix3s computeMomentofInertia() const
compute the inertia matrix, related to the origin
Definition: geometric_shapes.h:259
Box()
Default constructor.
Definition: geometric_shapes.h:184
void setSweptSphereRadius(Scalar radius)
Set radius of sphere swept around the shape. Must be >= 0.
Definition: geometric_shapes.h:76
Sphere(const Sphere &other)
Definition: geometric_shapes.h:245
void computeLocalAABB()
Compute AABB.
virtual TriangleP * clone() const
Clone *this into a new TriangleP.
Definition: geometric_shapes.h:119
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
Scalar halfLength
Half Length along z axis.
Definition: geometric_shapes.h:485
unsigned char const & count() const
Definition: geometric_shapes.h:685
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
virtual Plane * clone() const
Clone *this into a new Plane.
Definition: geometric_shapes.h:1002
Cone(Scalar radius_, Scalar lz_)
Definition: geometric_shapes.h:471
std::shared_ptr< std::vector< Vec3s > > normals
An array of the normals of the polygon.
Definition: geometric_shapes.h:717
Ellipsoid(const Ellipsoid &other)
Definition: geometric_shapes.h:312
Scalar distance(const Vec3s &p) const
Definition: geometric_shapes.h:918
NODE_TYPE getNodeType() const
get the node type
Definition: geometric_shapes.h:124
TriangleP(const TriangleP &other)
Definition: geometric_shapes.h:115
Cylinder()
Default constructor.
Definition: geometric_shapes.h:559
void computeLocalAABB()
Compute AABB.
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:940
Ellipsoid()
Default constructor.
Definition: geometric_shapes.h:306
unsigned int num_points
Definition: geometric_shapes.h:714
void computeLocalAABB()
Compute AABB.
Plane(const Vec3s &n_, Scalar d_)
Construct a plane with normal direction and offset.
Definition: geometric_shapes.h:980
Plane(const Plane &other)
Definition: geometric_shapes.h:992
Box(Scalar x, Scalar y, Scalar z)
Definition: geometric_shapes.h:166
Scalar minInflationValue() const
Definition: geometric_shapes.h:603
void buildSupportWarmStart()
Build the support points warm starts.
Matrix3s computeMomentofInertia() const
compute the inertia matrix, related to the origin
Definition: geometric_shapes.h:327
Halfspace(const Halfspace &other)
Definition: geometric_shapes.h:901
Halfspace(Scalar a, Scalar b, Scalar c, Scalar d_)
Construct a plane with normal direction and offset.
Definition: geometric_shapes.h:894
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
NODE_TYPE getNodeType() const
Get node type: an ellipsoid.
Definition: geometric_shapes.h:325
NODE_TYPE getNodeType() const
Get node type: a cone.
Definition: geometric_shapes.h:491
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:726
Ellipsoid(const Vec3s &radii)
Definition: geometric_shapes.h:310
virtual ConvexBase * clone() const
Clone (deep copy). This method is consistent with BVHModel clone method. The copy constructor is call...
Definition: geometric_shapes.h:667
Scalar minInflationValue() const
Definition: geometric_shapes.h:509
void computeLocalAABB()
Compute AABB.
Vec3s halfSide
box side half-length
Definition: geometric_shapes.h:187
Halfspace()
Definition: geometric_shapes.h:899
void set(std::shared_ptr< std::vector< Vec3s >> points_, unsigned int num_points_)
Set the points of the convex shape.
Sphere()
Default constructor.
Definition: geometric_shapes.h:241
Scalar minInflationValue() const
Definition: geometric_shapes.h:203
NODE_TYPE getNodeType() const
Get node type: a convex polytope.
Definition: geometric_shapes.h:673
Scalar computeVolume() const
compute the volume
Definition: geometric_shapes.h:408
Scalar computeVolume() const
compute the volume
Definition: geometric_shapes.h:195
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.
Matrix3s computeMomentofInertia() const
compute the inertia matrix, related to the origin
Definition: geometric_shapes.h:197
Vec3s a
Definition: geometric_shapes.h:147
Halfspace(const Vec3s &n_, Scalar d_)
Construct a half space with normal direction and offset.
Definition: geometric_shapes.h:889
Scalar signedDistance(const Vec3s &p) const
Definition: geometric_shapes.h:914
OBJECT_TYPE getObjectType() const
Get object type: a geometric shape.
Definition: geometric_shapes.h:72
void computeLocalAABB()
Compute AABB.
Vec3s b
Definition: geometric_shapes.h:147
void computeLocalAABB()
Compute AABB.
Scalar computeVolume() const
compute the volume
Definition: geometric_shapes.h:338
virtual Ellipsoid * clone() const
Clone *this into a new Ellipsoid.
Definition: geometric_shapes.h:315
Scalar radius
Radius of the cone.
Definition: geometric_shapes.h:479
void unitNormalTest()
Turn non-unit normal into unit.
void computeLocalAABB()
Compute AABB.
Scalar radius
Radius of the sphere.
Definition: geometric_shapes.h:248
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
void computeLocalAABB()
Compute AABB.
Scalar halfLength
Half Length along z axis.
Definition: geometric_shapes.h:583
unsigned int & operator[](int i)
Definition: geometric_shapes.h:686
Vec3s computeCOM() const
compute center of mass
Definition: geometric_shapes.h:507
Scalar getSweptSphereRadius() const
Get radius of sphere swept around the shape. This radius is always >= 0.
Definition: geometric_shapes.h:86
static ConvexBase * convexHull(const Vec3s *points, unsigned int num_points, bool keepTriangles, const char *qhullCommand=NULL)
std::vector< int > indices
Indices of the support points warm starts. These are the indices of the real convex,...
Definition: geometric_shapes.h:744
Vec3s radii
Radii of the Ellipsoid (such that on boundary: x^2/rx^2 + y^2/ry^2.
Definition: geometric_shapes.h:315
Scalar minInflationValue() const
Definition: geometric_shapes.h:343
Cylinder(const Cylinder &other)
Definition: geometric_shapes.h:565
Scalar computeVolume() const
compute the volume
Definition: geometric_shapes.h:493
Vec3s c
Definition: geometric_shapes.h:147
NODE_TYPE getNodeType() const
Get node type: a sphere.
Definition: geometric_shapes.h:257
virtual Sphere * clone() const
Clone *this into a new Sphere.
Definition: geometric_shapes.h:248
Plane & operator=(const Plane &other)
operator =
Definition: geometric_shapes.h:995
std::vector< Vec3s > points
Array of support points to warm start the support function computation.
Definition: geometric_shapes.h:739
virtual Cone * clone() const
Clone *this into a new Cone.
Definition: geometric_shapes.h:479
unsigned int num_normals_and_offsets
Definition: geometric_shapes.h:721
Sphere(Scalar radius_)
Definition: geometric_shapes.h:243
Box(const Vec3s &side_)
Definition: geometric_shapes.h:169
void unitNormalTest()
Turn non-unit normal into unit.
Vec3s center
center of the convex polytope, this is used for collision: center is guaranteed in the internal of th...
Definition: geometric_shapes.h:730
unsigned char count_
Definition: geometric_shapes.h:682
ConvexBase()
Construct an uninitialized convex object Initialization is done with ConvexBase::initialize.
Definition: geometric_shapes.h:756
void computeLocalAABB()
virtual function of compute AABB in local coordinate
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
NODE_TYPE getNodeType() const
Get node type: a cylinder.
Definition: geometric_shapes.h:589
ShapeBase()
Definition: geometric_shapes.h:60
Matrix3s computeMomentofInertia() const
compute the inertia matrix, related to the origin
Definition: geometric_shapes.h:498
Ellipsoid(Scalar rx, Scalar ry, Scalar rz)
Definition: geometric_shapes.h:308
Box(const Box &other)
Definition: geometric_shapes.h:171
TriangleP()
Definition: geometric_shapes.h:110
std::shared_ptr< std::vector< Vec3s > > points
An array of the points of the polygon.
Definition: geometric_shapes.h:713
Capsule()
Default constructor.
Definition: geometric_shapes.h:384
Scalar radius
Radius of capsule.
Definition: geometric_shapes.h:394
Scalar signedDistance(const Vec3s &p) const
Definition: geometric_shapes.h:1004
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:720
static ConvexBase * 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.
NODE_TYPE getNodeType() const
Get node type: a half space.
Definition: geometric_shapes.h:926
TriangleP(const Vec3s &a_, const Vec3s &b_, const Vec3s &c_)
Definition: geometric_shapes.h:112
virtual Cylinder * clone() const
Clone *this into a new Cylinder.
Definition: geometric_shapes.h:577
Cone()
Default constructor.
Definition: geometric_shapes.h:469
Cylinder & operator=(const Cylinder &other)
Definition: geometric_shapes.h:568
ConvexBase(const ConvexBase &other)
Copy constructor Only the list of neighbors is copied.
Scalar computeVolume() const
compute the volume
Definition: geometric_shapes.h:264
NODE_TYPE getNodeType() const
Get node type: a plane.
Definition: geometric_shapes.h:1024
SupportWarmStartPolytope support_warm_starts
Support warm start polytopes.
Definition: geometric_shapes.h:751
Cone(const Cone &other)
Definition: geometric_shapes.h:475
NODE_TYPE getNodeType() const
Get node type: a box.
Definition: geometric_shapes.h:193
virtual Halfspace * clone() const
Clone *this into a new Halfspace.
Definition: geometric_shapes.h:912
void computeLocalAABB()
Compute AABB.
NODE_TYPE getNodeType() const
Get node type: a capsule.
Definition: geometric_shapes.h:406
Scalar d
Plane offset.
Definition: geometric_shapes.h:1030
Plane(Scalar a, Scalar b, Scalar c, Scalar d_)
Construct a plane with normal direction and offset.
Definition: geometric_shapes.h:985
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:681
The support warm start polytope contains certain points of this which are support points in specific ...
Definition: geometric_shapes.h:736