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_), min_z_(other.min_z_), max_z_(other.max_z_), horizontal_frame_(other.horizontal_frame_),
75 vertical_frame_(other.vertical_frame_), subscribers_() // Don't copy observers
76 {
77 }
78
79 // Move constructor
80 Prism(Prism&& other) noexcept
81 : polygon_(std::move(other.polygon_)), min_z_(other.min_z_), max_z_(other.max_z_), horizontal_frame_(other.horizontal_frame_),
82 vertical_frame_(other.vertical_frame_), subscribers_(std::move(other.subscribers_))
83 {
84 }
85
86 // Move assignment operator
87 Prism& operator=(Prism&& other) noexcept
88 {
89 if (this != &other)
90 {
91 polygon_ = std::move(other.polygon_);
92 min_z_ = other.min_z_;
93 max_z_ = other.max_z_;
94 horizontal_frame_ = other.horizontal_frame_;
95 vertical_frame_ = other.vertical_frame_;
96 subscribers_ = std::move(other.subscribers_);
97 }
98 return *this;
99 }
100
101 ~Prism();
102
103 void subscribe(Subscriber* entity);
104 void unsubscribe(Subscriber* entity);
105
106 std::vector<Point2d> getPoints();
107 double getMaxZ() const;
108 double getMinZ() const;
109 std::string getHorizontalFrame() const;
110 std::string getVerticalFrame() const;
111
112 Polygon2D getPolygon() const;
113
114 // Returns number of vertices in the polygon of the prism.
115 unsigned int getNumVertices() const;
116
117 // Returns the centroid of the polygon of the prism.
118 Point2d getCenter() const;
119
120 void setMaxZ(const double value);
121
122 void setMinZ(const double value);
123
124 // Tries to change the coordinates of given vertex.
125 // returns true if succeeded
126 // returns false otherwise
127 bool setVertex(const Point2d& vertex, const unsigned int index);
128
129 // Tries to change the coordinates of given vertices.
130 // Only notifies subsribers once in case of success
131 // returns true if succeeded
132 // returns false otherwise
133 bool setVertices(const std::vector<Point2d>& vertices, const std::vector<unsigned int>& indices);
134
135 // Adds new vertex in the middle of the neighboring verge
136 void addVertexClockwise(const unsigned int index);
137
138 // Adds new vertex in the middle of the neighboring verge
139 void addVertexCounterclockwise(const unsigned int index);
140
141 void move(const Point3d& adjustment);
142
143 // Takes angle in radians, rotates clockwise (counter-clockwise if alpha < 0)
144 void rotate(const double alpha);
145
146 // Deletes the vertex only if vertex_count > 3
147 void deleteVertex(const unsigned int index);
148
149 // Overload with Point3d
150 bool isPointIn(const Point3d& point) const;
151
152 // Overload with 3D point (double,double,double)
153 bool isPointIn(const double x, const double y, const double z) const;
154
155 // Overload with Point2d
156 bool isPointIn(const Point2d& point) const;
157
158 // Overload with 2D point (double,double)
159 bool isPointIn(const double x, const double y) const;
160
161 // Helper method for text representation
162 // void accept(Visitor &visitor);
163 };
164
165 } // namespace safety_zone
166} // namespace mrs_lib
167
168#endif
Definition prism.h:33
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