1 #ifndef MEDUSA_BITS_DOMAINS_POLYGONSHAPE_HPP_
2 #define MEDUSA_BITS_DOMAINS_POLYGONSHAPE_HPP_
16 template <
typename vec_t>
20 assert_msg(n >= 3,
"At least three points are needed to form a polygon, got %d.", n);
21 for (
int i = 0; i < n; ++i) {
22 int j = (i == n-1) ? 0 : (i+1);
25 assert_msg(std::abs(area) > 1e-10,
"Given polygon has no area.");
32 template <
typename vec_t>
34 base_t::setMargin(margin);
38 template <
typename vec_t>
40 int n = points_.size();
41 points_with_margin_ = points_;
42 for (
int i = 0; i < n; ++i) {
43 int j = (i == n - 1) ? 0 : (i + 1);
44 int k = (i == 0) ? n - 1 : (i - 1);
45 vec_t next_edge = (points_[j] - points_[i]).normalized();
46 vec_t prev_edge = (points_[i] - points_[k]).normalized();
47 vec_t normal = {next_edge[1] + prev_edge[1], -next_edge[0] - prev_edge[0]};
48 points_with_margin_[i] += margin_*normal.normalized();
50 points_with_margin_.push_back(points_with_margin_[0]);
53 template <
typename vec_t>
56 int n = points_.size();
58 auto& pts = points_with_margin_;
59 for (
int i = 0; i < n; i++) {
60 if (pts[i][1] <= point[1]) {
62 if (pts[i+1][1] > point[1] && isLeft(pts[i], pts[i+1], point) > 0) {
67 if (pts[i+1][1] <= point[1] && isLeft(pts[i], pts[i+1], point) < 0) {
75 template <
typename vec_t>
78 int n = points_.size();
79 for (
int i = 0; i < n; ++i) {
80 if (points_[i][0] > maxx) maxx = points_[i][0];
81 if (points_[i][0] < minx) minx = points_[i][0];
82 if (points_[i][1] > maxy) maxy = points_[i][1];
83 if (points_[i][1] < miny) miny = points_[i][1];
85 return {{minx, miny}, {maxx, maxy}};
88 template <
typename vec_t>
92 int n = points_.size();
93 for (
int i = 0; i < n; ++i) {
94 int j = (i == n-1) ? 0 : (i+1);
95 int k = (i == 0) ? n-1 : (i-1);
96 vec_t edge = points_[j] - points_[i];
100 vec_t next_edge_n = edge / len;
101 vec_t prev_edge_n = (points_[i] - points_[k]).normalized();
102 vec_t p_normal = {next_edge_n[1] + prev_edge_n[1], -next_edge_n[0] - prev_edge_n[0]};
103 d.
addBoundaryNode(points_[i], (type == 0) ? -i-1 : type, p_normal.normalized());
106 int count =
iceil(len/step);
107 vec_t e_normal = {edge[1] / len, -edge[0] / len};
108 for (
int c = 1; c < count; ++c) {
110 (type == 0) ? -i-1 : type, e_normal);
116 template <
typename vec_t>
119 const std::function<
scalar_t(vec_t)>& dr,
int type)
const {
121 int n = points_.size();
122 for (
int i = 0; i < n; ++i) {
123 int j = (i == n-1) ? 0 : (i+1);
124 int k = (i == 0) ? n-1 : (i-1);
125 vec_t edge = points_[j] - points_[i];
129 vec_t next_edge_n = edge / len;
130 vec_t prev_edge_n = (points_[i] - points_[k]).normalized();
131 vec_t p_normal = {next_edge_n[1] + prev_edge_n[1], -next_edge_n[0] - prev_edge_n[0]};
132 d.
addBoundaryNode(points_[i], (type == 0) ? -i-1 : type, p_normal.normalized());
135 vec_t e_normal = {points_[j][1] - points_[i][1], -points_[j][0] + points_[i][0]};
136 e_normal.normalize();
137 for (
const auto& p : points) {
146 #endif // MEDUSA_BITS_DOMAINS_POLYGONSHAPE_HPP_