mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
prism.h
1#ifndef MRS_PRISM_H
2#define MRS_PRISM_H
3
4#include <boost/geometry.hpp>
5#include <boost/geometry/algorithms/centroid.hpp>
6#include <boost/geometry/geometries/point.hpp>
7#include <boost/geometry/geometries/polygon.hpp>
8
9namespace mrs_lib
10{
11
12 namespace safety_zone
13 {
14
15 typedef boost::geometry::model::point<double, 2, boost::geometry::cs::cartesian> Point2d;
16 typedef boost::geometry::model::point<double, 3, boost::geometry::cs::cartesian> Point3d;
17 typedef boost::geometry::model::polygon<Point2d> Polygon2D;
18
20 {
21 protected:
22 bool is_active_ = true;
23
24 public:
25 // Called every time a change has been made to the prism
26 virtual void update() = 0;
27
28 // Called once upon deleting the prism
29 virtual void cleanup() = 0;
30 };
31
32 class Prism
33 {
34 private:
35 Polygon2D polygon_;
36 double min_z_;
37 double max_z_;
38 std::string horizontal_frame_; // frame in which the polygon is defined
39 std::string vertical_frame_; // frame in which the z limits are defined
40
41 std::set<Subscriber*> subscribers_;
42
43 void notifySubscribers();
44 void cleanSubscribers();
45
46 public:
47 // Empty constructor
48 Prism() : polygon_(), min_z_(0.0), max_z_(0.0), horizontal_frame_("world_origin"), vertical_frame_("world_origin")
49 {
50 }
51 // If max_z < min_z, automatically swaps them
52 // Throws std::invalid argument if polygon is invalid
53 Prism(const std::vector<Point2d>& points, const double max_z, const double min_z);
54
55 Prism(const std::vector<Point2d>& points, const double max_z, const double min_z, const std::string& horizontal_frame, const std::string& vertical_frame);
56
57 // Copy assignment operator
58 Prism& operator=(const Prism& other)
59 {
60 if (this != &other)
61 {
62 polygon_ = other.polygon_;
63 min_z_ = other.min_z_;
64 max_z_ = other.max_z_;
65 horizontal_frame_ = other.horizontal_frame_;
66 vertical_frame_ = other.vertical_frame_;
67 // Keep existing subscribers, don't copy them
68 }
69 return *this;
70 }
71
72 // Copy constructor
73 Prism(const Prism& other)
74 : polygon_(other.polygon_),
75 min_z_(other.min_z_),
76 max_z_(other.max_z_),
77 horizontal_frame_(other.horizontal_frame_),
78 vertical_frame_(other.vertical_frame_),
79 subscribers_() // Don't copy observers
80 {
81 }
82
83 // Move constructor
84 Prism(Prism&& other) noexcept
85 : polygon_(std::move(other.polygon_)),
86 min_z_(other.min_z_),
87 max_z_(other.max_z_),
88 horizontal_frame_(other.horizontal_frame_),
89 vertical_frame_(other.vertical_frame_),
90 subscribers_(std::move(other.subscribers_))
91 {
92 }
93
94 // Move assignment operator
95 Prism& operator=(Prism&& other) noexcept
96 {
97 if (this != &other)
98 {
99 polygon_ = std::move(other.polygon_);
100 min_z_ = other.min_z_;
101 max_z_ = other.max_z_;
102 horizontal_frame_ = other.horizontal_frame_;
103 vertical_frame_ = other.vertical_frame_;
104 subscribers_ = std::move(other.subscribers_);
105 }
106 return *this;
107 }
108
109 ~Prism();
110
111 void subscribe(Subscriber* entity);
112 void unsubscribe(Subscriber* entity);
113
114 std::vector<Point2d> getPoints() const;
115 double getMaxZ() const;
116 double getMinZ() const;
117 std::string getHorizontalFrame() const;
118 std::string getVerticalFrame() const;
119
120 Polygon2D getPolygon() const;
121
122 // Returns number of vertices in the polygon of the prism.
123 unsigned int getNumVertices() const;
124
125 // Returns the centroid of the polygon of the prism.
126 Point2d getCenter() const;
127
128 void setMaxZ(const double value);
129
130 void setMinZ(const double value);
131
132 // Tries to change the coordinates of given vertex.
133 // returns true if succeeded
134 // returns false otherwise
135 bool setVertex(const Point2d& vertex, const unsigned int index);
136
137 // Tries to change the coordinates of given vertices.
138 // Only notifies subsribers once in case of success
139 // returns true if succeeded
140 // returns false otherwise
141 bool setVertices(const std::vector<Point2d>& vertices, const std::vector<unsigned int>& indices);
142
143 // Adds new vertex in the middle of the neighboring verge
144 void addVertexClockwise(const unsigned int index);
145
146 // Adds new vertex in the middle of the neighboring verge
147 void addVertexCounterclockwise(const unsigned int index);
148
149 void move(const Point3d& adjustment);
150
151 // Takes angle in radians, rotates clockwise (counter-clockwise if alpha < 0)
152 void rotate(const double alpha);
153
154 // Deletes the vertex only if vertex_count > 3
155 void deleteVertex(const unsigned int index);
156
157 // Overload with Point3d
158 bool isPointIn(const Point3d& point) const;
159
160 // Overload with 3D point (double,double,double)
161 bool isPointIn(const double x, const double y, const double z) const;
162
163 // Overload with Point2d
164 bool isPointIn(const Point2d& point) const;
165
166 // Overload with 2D point (double,double)
167 bool isPointIn(const double x, const double y) const;
168
169 // Helper method for text representation
170 // void accept(Visitor &visitor);
171 };
172
173 } // namespace safety_zone
174} // namespace mrs_lib
175
176#endif
std::string getVerticalFrame() const
Definition prism.cpp:437
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24