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