mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
shapes.h
Go to the documentation of this file.
1 // clang: MatousFormat
8 #ifndef SHAPES_H
9 #define SHAPES_H
10 
11 #include <optional>
12 
13 #include <boost/optional.hpp>
14 #include <Eigen/Dense>
15 
16 namespace mrs_lib
17 {
18  namespace geometry
19  {
20 
21  /* class Ray //{ */
22 
26  class Ray
27  {
28  public:
32  Ray();
33 
37  ~Ray();
38 
45  Ray(Eigen::Vector3d p1, Eigen::Vector3d p2);
46 
47  private:
48  Eigen::Vector3d point1;
49  Eigen::Vector3d point2;
50 
51  public:
57  const Eigen::Vector3d p1() const;
63  const Eigen::Vector3d p2() const;
64 
70  const Eigen::Vector3d direction() const;
71 
72  public:
81  static Ray twopointCast(Eigen::Vector3d pointFrom, Eigen::Vector3d pointTo);
82 
91  static Ray directionCast(Eigen::Vector3d origin, Eigen::Vector3d direction);
92  };
93 
94  //}
95 
96  /* class Triangle //{ */
100  class Triangle
101  {
102  public:
106  Triangle();
107 
111  ~Triangle();
112 
120  Triangle(Eigen::Vector3d a, Eigen::Vector3d b, Eigen::Vector3d c);
121 
122  private:
123  Eigen::Vector3d point1;
124  Eigen::Vector3d point2;
125  Eigen::Vector3d point3;
126 
127  public:
133  const Eigen::Vector3d a() const;
134 
140  const Eigen::Vector3d b() const;
141 
147  const Eigen::Vector3d c() const;
148 
154  const Eigen::Vector3d center() const;
155 
162  const Eigen::Vector3d normal() const;
163 
169  const std::vector<Eigen::Vector3d> vertices() const;
170 
171  public:
180  const boost::optional<Eigen::Vector3d> intersectionRay(Ray r, double epsilon = 1e-4) const;
181  };
182  //}
183 
184  /* class Rectangle //{ */
185 
189  class Rectangle
190  {
191  public:
195  Rectangle();
196 
200  ~Rectangle();
201 
207  Rectangle(std::vector<Eigen::Vector3d> points);
208 
217  Rectangle(Eigen::Vector3d a, Eigen::Vector3d b, Eigen::Vector3d c, Eigen::Vector3d d);
218 
219  private:
220  Eigen::Vector3d point1;
221  Eigen::Vector3d point2;
222  Eigen::Vector3d point3;
223  Eigen::Vector3d point4;
224 
225  public:
231  const Eigen::Vector3d a() const;
232 
238  const Eigen::Vector3d b() const;
239 
245  const Eigen::Vector3d c() const;
246 
252  const Eigen::Vector3d d() const;
253 
259  const Eigen::Vector3d center() const;
260 
267  const Eigen::Vector3d normal() const;
268 
274  const std::vector<Eigen::Vector3d> vertices() const;
275 
281  const std::vector<Triangle> triangles() const;
282 
291  const boost::optional<Eigen::Vector3d> intersectionRay(Ray r, double epsilon = 1e-4) const;
292 
300  bool isFacing(Eigen::Vector3d point) const;
301 
302 
310  double solidAngleRelativeTo(Eigen::Vector3d point) const;
311  };
312 
313  //}
314 
315  /* class Cuboid //{ */
319  class Cuboid
320  {
321  public:
325  Cuboid();
326 
330  ~Cuboid();
331 
344  Cuboid(Eigen::Vector3d p0, Eigen::Vector3d p1, Eigen::Vector3d p2, Eigen::Vector3d p3, Eigen::Vector3d p4, Eigen::Vector3d p5, Eigen::Vector3d p6,
345  Eigen::Vector3d p7);
351  Cuboid(std::vector<Eigen::Vector3d> points);
352 
360  Cuboid(Eigen::Vector3d center, Eigen::Vector3d size, Eigen::Quaterniond orientation);
361 
362  private:
366  enum
367  {
368  FRONT = 0,
369  BACK = 1,
370  LEFT = 2,
371  RIGHT = 3,
372  BOTTOM = 4,
373  TOP = 5,
374  };
375 
376  private:
377  std::vector<Eigen::Vector3d> points;
378 
386  std::vector<Eigen::Vector3d> lookupPoints(int face_idx) const;
387 
388  public:
394  const std::vector<Eigen::Vector3d> vertices() const;
395 
401  const Eigen::Vector3d center() const;
402 
410  const Rectangle getRectangle(int face_idx) const;
411 
420  const std::vector<Eigen::Vector3d> intersectionRay(Ray r, double epsilon = 1e-4) const;
421  };
422  //}
423 
424  /* class Ellipse //{ */
428  class Ellipse
429  {
430  public:
434  Ellipse();
435 
439  ~Ellipse();
440 
449  Ellipse(Eigen::Vector3d center, Eigen::Quaterniond orientation, double a, double b);
450 
451  private:
452  double major_semi;
453  double minor_semi;
454  Eigen::Vector3d center_point;
455  Eigen::Quaterniond absolute_orientation;
456 
457  public:
463  double a() const;
464 
470  double b() const;
471 
477  const Eigen::Vector3d center() const;
478 
484  const Eigen::Quaterniond orientation() const;
485  };
486  //}
487 
488  /* class Cylinder //{ */
492  class Cylinder
493  {
494  public:
498  Cylinder();
499 
503  ~Cylinder();
504 
513  Cylinder(Eigen::Vector3d center, double radius, double height, Eigen::Quaterniond orientation);
514 
515  private:
516  Eigen::Vector3d center_point;
517  double radius;
518  double height;
519  Eigen::Quaterniond absolute_orientation;
520 
521  public:
525  enum
526  {
527  BOTTOM = 0,
528  TOP = 1,
529  };
530 
531  public:
537  const Eigen::Vector3d center() const;
538 
544  const Eigen::Quaterniond orientation() const;
545 
551  double r() const;
552 
558  double h() const;
559 
567  const Ellipse getCap(int index) const;
568  };
569  //}
570 
571  /* class Cone //{ */
575  class Cone
576  {
577  public:
581  Cone();
582 
586  ~Cone();
587 
596  Cone(Eigen::Vector3d origin_point, double angle, double height, Eigen::Vector3d orientation);
597 
598  private:
599  Eigen::Vector3d origin_point;
600  double angle;
601  double height;
602  Eigen::Vector3d absolute_direction;
603 
604  public:
610  const Eigen::Vector3d origin() const;
611 
617  const Eigen::Vector3d direction() const;
618 
624  const Eigen::Vector3d center() const;
625 
631  double theta() const;
632 
638  double h() const;
639 
645  const Ellipse getCap() const;
646 
654  const std::optional<Eigen::Vector3d> projectPoint(const Eigen::Vector3d& point) const;
655  };
656  //}
657  } // namespace geometry
658 } // namespace mrs_lib
659 
660 #endif // SHAPES_H
mrs_lib::geometry::Ray::Ray
Ray()
constructor without initialization of internal variables
Definition: shapes.cpp:13
mrs_lib::geometry::Cylinder::~Cylinder
~Cylinder()
destructor
Definition: shapes.cpp:498
mrs_lib::geometry::Cone::getCap
const Ellipse getCap() const
getter for the cap of the cone
Definition: shapes.cpp:600
mrs_lib::geometry::Cylinder::orientation
const Eigen::Quaterniond orientation() const
getter for the orientation
Definition: shapes.cpp:517
mrs_lib::geometry::Ellipse::Ellipse
Ellipse()
constructor for initialization without setting internal variables
Definition: shapes.cpp:449
mrs_lib::geometry::Triangle::intersectionRay
const boost::optional< Eigen::Vector3d > intersectionRay(Ray r, double epsilon=1e-4) const
calculate an intersection of this triangle with a given ray with given tolerance
Definition: shapes.cpp:122
mrs_lib::geometry::Triangle::vertices
const std::vector< Eigen::Vector3d > vertices() const
get a vector of all vertices
Definition: shapes.cpp:111
mrs_lib::geometry::Rectangle::intersectionRay
const boost::optional< Eigen::Vector3d > intersectionRay(Ray r, double epsilon=1e-4) const
calculate an intersection of this rectangle with a given ray with given tolerance
Definition: shapes.cpp:247
mrs_lib::geometry::Cone::~Cone
~Cone()
destructor
Definition: shapes.cpp:561
mrs_lib::geometry::Ellipse::b
double b() const
getter for minor semi-axis
Definition: shapes.cpp:472
mrs_lib::geometry::Cylinder::center
const Eigen::Vector3d center() const
getter for the center point
Definition: shapes.cpp:512
mrs_lib::geometry::Cone::center
const Eigen::Vector3d center() const
getter for the center point. Center point lies in half of the body height
Definition: shapes.cpp:585
mrs_lib::geometry::Triangle::c
const Eigen::Vector3d c() const
getter for third point
Definition: shapes.cpp:94
mrs_lib::geometry::Cylinder::Cylinder
Cylinder()
constructor without setting the internal variables
Definition: shapes.cpp:494
mrs_lib::geometry::Triangle::Triangle
Triangle()
constructor for initialization with default values (points [0,0,0], [1,0,0] and [0,...
Definition: shapes.cpp:64
mrs_lib::geometry::Rectangle::triangles
const std::vector< Triangle > triangles() const
getter for the triangles forming this rectangle
Definition: shapes.cpp:234
mrs_lib::geometry::Ray::p2
const Eigen::Vector3d p2() const
get the end point
Definition: shapes.cpp:36
mrs_lib::geometry::Cone::h
double h() const
getter for body height
Definition: shapes.cpp:595
mrs_lib::geometry::Triangle::center
const Eigen::Vector3d center() const
get position on the triangle center
Definition: shapes.cpp:106
mrs_lib::geometry::Ray
geometric representation of a ray. Instantiate it by two input Vector3. Use static methods for from-t...
Definition: shapes.h:26
mrs_lib::geometry::Ellipse::a
double a() const
getter for major semi-axis
Definition: shapes.cpp:467
mrs_lib::geometry::Cuboid
geometric representation of a cuboid
Definition: shapes.h:319
mrs_lib::geometry::Rectangle::d
const Eigen::Vector3d d() const
getter for the fourth point
Definition: shapes.cpp:207
mrs_lib::geometry::Cuboid::vertices
const std::vector< Eigen::Vector3d > vertices() const
getter for all vertices (vector3) of this cuboid
Definition: shapes.cpp:406
mrs_lib::geometry::Cone::origin
const Eigen::Vector3d origin() const
getter for the tip point
Definition: shapes.cpp:575
mrs_lib::geometry::Cuboid::Cuboid
Cuboid()
constructor for initialization with all points set to [0,0,0]
Definition: shapes.cpp:295
mrs_lib::geometry::Cone::theta
double theta() const
getter for angle between body height and body side
Definition: shapes.cpp:590
mrs_lib::geometry::Rectangle::center
const Eigen::Vector3d center() const
getter for center point
Definition: shapes.cpp:212
mrs_lib::geometry::Rectangle::Rectangle
Rectangle()
constructor for initialization with points set to [0,0,0], [1,0,0], [1,1,0] and [0,...
Definition: shapes.cpp:162
mrs_lib::geometry::Triangle::b
const Eigen::Vector3d b() const
getter for second point
Definition: shapes.cpp:89
mrs_lib::geometry::Triangle::~Triangle
~Triangle()
destructor
Definition: shapes.cpp:78
mrs_lib::geometry::Rectangle::a
const Eigen::Vector3d a() const
getter for first point
Definition: shapes.cpp:192
mrs_lib::geometry::Ray::p1
const Eigen::Vector3d p1() const
get the origin point
Definition: shapes.cpp:31
mrs_lib::geometry::Ellipse
geometric representation of an ellipse
Definition: shapes.h:428
mrs_lib::geometry::Cylinder::getCap
const Ellipse getCap(int index) const
getter for a cap corresponding to a provided index
Definition: shapes.cpp:532
mrs_lib::geometry::Cylinder::r
double r() const
getter for cap radius
Definition: shapes.cpp:522
mrs_lib::geometry::Ellipse::center
const Eigen::Vector3d center() const
getter for the center point
Definition: shapes.cpp:477
mrs_lib::geometry::Rectangle::b
const Eigen::Vector3d b() const
getter for the second point
Definition: shapes.cpp:197
mrs_lib
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition: attitude_converter.h:29
mrs_lib::geometry::Triangle::a
const Eigen::Vector3d a() const
getter for first point
Definition: shapes.cpp:84
mrs_lib::geometry::Cone::Cone
Cone()
constructor without setting the internal variables
Definition: shapes.cpp:557
mrs_lib::geometry::Cuboid::getRectangle
const Rectangle getRectangle(int face_idx) const
getter for one side corresponding to a provided index
Definition: shapes.cpp:411
mrs_lib::geometry::Rectangle
geometric representation of a rectangle (can represent any quadrilateral)
Definition: shapes.h:189
mrs_lib::geometry::Rectangle::vertices
const std::vector< Eigen::Vector3d > vertices() const
getter for all the points of this rectangle provided as std::vector
Definition: shapes.cpp:224
mrs_lib::geometry::Cylinder::h
double h() const
getter for the body height
Definition: shapes.cpp:527
mrs_lib::geometry::Ray::~Ray
~Ray()
destructor
Definition: shapes.cpp:25
mrs_lib::geometry::Rectangle::normal
const Eigen::Vector3d normal() const
getter for the normal vector. It originates in the center of the Rectangle, length is normalized,...
Definition: shapes.cpp:217
mrs_lib::geometry::Cone::direction
const Eigen::Vector3d direction() const
getter for the direction. Normalized direction from origin towards base
Definition: shapes.cpp:580
mrs_lib::geometry::Rectangle::solidAngleRelativeTo
double solidAngleRelativeTo(Eigen::Vector3d point) const
compute the solid angle of this rectangle relative to a given sphere center
Definition: shapes.cpp:271
mrs_lib::geometry::Triangle::normal
const Eigen::Vector3d normal() const
get normal vector of this triangle. The vector origin is placed at triangle center,...
Definition: shapes.cpp:99
mrs_lib::geometry::Ray::twopointCast
static Ray twopointCast(Eigen::Vector3d pointFrom, Eigen::Vector3d pointTo)
static method for generating new rays by raycasting from-to
Definition: shapes.cpp:48
mrs_lib::geometry::Triangle
geometric representation of a triangle. Instantiate a new triangle by providing three vertices
Definition: shapes.h:100
mrs_lib::geometry::Cuboid::center
const Eigen::Vector3d center() const
getter for the center point
Definition: shapes.cpp:416
mrs_lib::geometry::Ellipse::~Ellipse
~Ellipse()
destructor
Definition: shapes.cpp:453
mrs_lib::geometry::Rectangle::~Rectangle
~Rectangle()
destructor
Definition: shapes.cpp:186
mrs_lib::geometry::Ellipse::orientation
const Eigen::Quaterniond orientation() const
getter for the orientation
Definition: shapes.cpp:482
mrs_lib::geometry::Ray::direction
const Eigen::Vector3d direction() const
get the direction of ray (normalized)
Definition: shapes.cpp:41
mrs_lib::geometry::Cone::projectPoint
const std::optional< Eigen::Vector3d > projectPoint(const Eigen::Vector3d &point) const
Project a 3D point orthogonally onto the Cone surface.
Definition: shapes.cpp:609
mrs_lib::geometry::Cylinder
geometric representation of a cylinder
Definition: shapes.h:492
mrs_lib::geometry::Ray::directionCast
static Ray directionCast(Eigen::Vector3d origin, Eigen::Vector3d direction)
static method for generating new rays by raycasting origin-direction
Definition: shapes.cpp:53
mrs_lib::geometry::Cuboid::~Cuboid
~Cuboid()
virtual destructor
Definition: shapes.cpp:353
mrs_lib::geometry::Rectangle::isFacing
bool isFacing(Eigen::Vector3d point) const
check if the normal is facing a given point, i.e. if the point lies in the same half-space as the rec...
Definition: shapes.cpp:261
mrs_lib::geometry::Rectangle::c
const Eigen::Vector3d c() const
getter for the third point
Definition: shapes.cpp:202
mrs_lib::geometry::Cone
geometric representaiton of a cone
Definition: shapes.h:575
mrs_lib::geometry::Cuboid::intersectionRay
const std::vector< Eigen::Vector3d > intersectionRay(Ray r, double epsilon=1e-4) const
calculate the intersection between this cuboid and a provided ray within a given tolerance....
Definition: shapes.cpp:428