libalmath  2.8.7.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
shapes3d.h
Go to the documentation of this file.
1 
7 #ifndef LIB_ALMATH_GEOMETRICS_SHAPES3D_H
8 #define LIB_ALMATH_GEOMETRICS_SHAPES3D_H
9 
10 #include <almath/api.h>
11 #include <string>
12 #include <stdexcept>
13 
14 namespace AL {
15 namespace Math {
16 
17 class ALMATH_API Shape3DVisitor;
18 
19 class ALMATH_API Shape3D {
20  public:
21  virtual ~Shape3D() {}
22  virtual void accept(const Shape3DVisitor &v) const = 0;
23 };
24 
25 // Defined by its radius. Centered at (0, 0, 0).
26 class ALMATH_API Sphere : public Shape3D {
27  public:
28  Sphere(float pRadius);
29  friend bool operator==(const Sphere &lhs, const Sphere &rhs)
30  {
31  return lhs.fRadius == rhs.fRadius;
32  }
33  friend bool operator!=(const Sphere &lhs, const Sphere &rhs)
34  {
35  return !(lhs == rhs);
36  }
37  void accept(const Shape3DVisitor &v) const override;
38  float getRadius() const;
39 
40  private:
41  float fRadius;
42 };
43 
44 // Minkowsky sum of a 2D rectangle with a 3D sphere.
45 // Centered. The normal to the plane is : (0, 0, 1).
46 // a.k.a Tab before (like a tactile tablet).
47 class ALMATH_API RoundedRectangle : public Shape3D {
48  public:
49  RoundedRectangle(float pHalfExtentX, float pHalfExtentY, float pRadius);
50  friend bool operator==(const RoundedRectangle &lhs,
51  const RoundedRectangle &rhs)
52  {
53  return lhs.fHalfExtentX == rhs.fHalfExtentX &&
54  lhs.fHalfExtentY == rhs.fHalfExtentY &&
55  lhs.fRadius == rhs.fRadius;
56  }
57  friend bool operator!=(const RoundedRectangle &lhs,
58  const RoundedRectangle &rhs)
59  {
60  return !(lhs == rhs);
61  }
62  void accept(const Shape3DVisitor &v) const override;
63  float getHalfExtentX() const;
64  float getHalfExtentY() const;
65  float getRadius() const;
66 
67  private:
68  float fHalfExtentX;
69  float fHalfExtentY;
70  float fRadius;
71 };
72 
73 // 3D convex hull of two spheres, oriented along z axis.
74 // The centers of the spheres have the coordinates in pill frame :
75 // upSphere(0, 0, fHalfExtent), downSpere(0, 0, -fhalfExtent).
76 class ALMATH_API Pill : public Shape3D {
77  public:
78  Pill(float pHalfExtent, float pRadius);
79  friend bool operator==(const Pill &lhs, const Pill &rhs)
80  {
81  return lhs.fHalfExtent == rhs.fHalfExtent &&
82  lhs.fRadius == rhs.fRadius;
83  }
84  friend bool operator!=(const Pill &lhs, const Pill &rhs)
85  {
86  return !(lhs == rhs);
87  }
88  float getHalfExtent() const;
89  float getRadius() const;
90  void accept(const Shape3DVisitor &v) const override;
91 
92  private:
93  float fHalfExtent;
94  float fRadius;
95 
96 };
97 
98 
99 
100 // Equation z = 0, normal (0, 0, 1).
101 class ALMATH_API Plane : public Shape3D {
102  public:
103  friend bool operator==(const Plane &lhs, const Plane &rhs)
104  {
105  return true;
106  }
107  friend bool operator!=(const Plane &lhs, const Plane &rhs)
108  {
109  return false;
110  }
111  void accept(const Shape3DVisitor &v) const override;
112 };
113 
114 // Equation z <= 0, normal (0, 0, 1). (z > 0 is free space).
115 class ALMATH_API HalfSpace : public Shape3D {
116  public:
117  friend bool operator==(const HalfSpace &lhs, const HalfSpace &rhs)
118  {
119  return true;
120  }
121  friend bool operator!=(const HalfSpace &lhs, const HalfSpace &rhs)
122  {
123  return false;
124  }
125  void accept(const Shape3DVisitor &v) const override;
126 };
127 
128 // Equation z = 0, abs(x) < fHalfExtentX, abs(y) < fHalfExtentY,
129 class ALMATH_API Rectangle : public Shape3D {
130  public:
131  Rectangle(float pHalfExtentX, float pHalfExtentY);
132  friend bool operator==(const Rectangle &lhs, const Rectangle &rhs)
133  {
134  return lhs.fHalfExtentX == rhs.fHalfExtentX &&
135  lhs.fHalfExtentY == rhs.fHalfExtentY;
136  }
137  friend bool operator!=(const Rectangle &lhs, const Rectangle &rhs)
138  {
139  return !(lhs == rhs);
140  }
141  void accept(const Shape3DVisitor &v) const override;
142  float getHalfExtentX() const;
143  float getHalfExtentY() const;
144 
145  private:
146  float fHalfExtentX;
147  float fHalfExtentY;
148 };
149 
150 // Half line oriented along z axis, equation z >= 0, x = 0, y = 0.
151 class ALMATH_API HalfLine : public Shape3D {
152  public:
153  friend bool operator==(const HalfLine &lhs, const HalfLine &rhs)
154  {
155  return true;
156  }
157  friend bool operator!=(const HalfLine &lhs, const HalfLine &rhs)
158  {
159  return false;
160  }
161  void accept(const Shape3DVisitor &v) const override;
162 };
163 
164 class ALMATH_API Shape3DVisitor {
165  public:
166  virtual void visit(const Pill &pShape) const = 0;
167  virtual void visit(const Sphere &pShape) const = 0;
168  virtual void visit(const RoundedRectangle &pShape) const = 0;
169  virtual void visit(const Plane &pShape) const = 0;
170  virtual void visit(const HalfSpace &pShape) const = 0;
171  virtual void visit(const Rectangle &pShape) const = 0;
172  virtual void visit(const HalfLine &pShape) const = 0;
173  virtual ~Shape3DVisitor() = default;
174 };
175 
176 class ALMATH_API NotImplementedShape3DVisitor : public Shape3DVisitor {
177  public:
178  NotImplementedShape3DVisitor(const std::string msg = "not implemented")
179  : fMsg(msg) {}
180  virtual void visit(const Pill &) const override {
181  throw std::runtime_error(fMsg);
182  }
183  virtual void visit(const Sphere &) const override {
184  throw std::runtime_error(fMsg);
185  }
186  virtual void visit(const RoundedRectangle &) const override {
187  throw std::runtime_error(fMsg);
188  }
189  virtual void visit(const Plane &) const override {
190  throw std::runtime_error(fMsg);
191  }
192  virtual void visit(const HalfSpace &) const override {
193  throw std::runtime_error(fMsg);
194  }
195  virtual void visit(const Rectangle &) const override {
196  throw std::runtime_error(fMsg);
197  }
198  virtual void visit(const HalfLine &) const override {
199  throw std::runtime_error(fMsg);
200  }
201  virtual ~NotImplementedShape3DVisitor() = default;
202 
203  private:
204  std::string fMsg;
205 };
206 
207 } // End namespace Math.
208 } // End namespace AL.
209 
210 #endif // LIB_ALMATH_GEOMETRICS_SHAPES3D_H
friend bool operator!=(const Rectangle &lhs, const Rectangle &rhs)
Definition: shapes3d.h:137
friend bool operator==(const HalfSpace &lhs, const HalfSpace &rhs)
Definition: shapes3d.h:117
friend bool operator==(const RoundedRectangle &lhs, const RoundedRectangle &rhs)
Definition: shapes3d.h:50
friend bool operator!=(const RoundedRectangle &lhs, const RoundedRectangle &rhs)
Definition: shapes3d.h:57
virtual void visit(const RoundedRectangle &) const override
Definition: shapes3d.h:186
virtual void visit(const Pill &) const override
Definition: shapes3d.h:180
virtual void visit(const HalfLine &) const override
Definition: shapes3d.h:198
friend bool operator!=(const Plane &lhs, const Plane &rhs)
Definition: shapes3d.h:107
virtual void visit(const Rectangle &) const override
Definition: shapes3d.h:195
friend bool operator!=(const Pill &lhs, const Pill &rhs)
Definition: shapes3d.h:84
friend bool operator!=(const HalfLine &lhs, const HalfLine &rhs)
Definition: shapes3d.h:157
virtual void visit(const Sphere &) const override
Definition: shapes3d.h:183
friend bool operator==(const Sphere &lhs, const Sphere &rhs)
Definition: shapes3d.h:29
virtual void visit(const Plane &) const override
Definition: shapes3d.h:189
NotImplementedShape3DVisitor(const std::string msg="not implemented")
Definition: shapes3d.h:178
friend bool operator==(const Pill &lhs, const Pill &rhs)
Definition: shapes3d.h:79
friend bool operator!=(const HalfSpace &lhs, const HalfSpace &rhs)
Definition: shapes3d.h:121
friend bool operator==(const Rectangle &lhs, const Rectangle &rhs)
Definition: shapes3d.h:132
friend bool operator==(const Plane &lhs, const Plane &rhs)
Definition: shapes3d.h:103
virtual ~Shape3D()
Definition: shapes3d.h:21
friend bool operator==(const HalfLine &lhs, const HalfLine &rhs)
Definition: shapes3d.h:153
virtual void visit(const HalfSpace &) const override
Definition: shapes3d.h:192
friend bool operator!=(const Sphere &lhs, const Sphere &rhs)
Definition: shapes3d.h:33