PolyFEM
Loading...
Searching...
No Matches
PolygonUtils.cpp
Go to the documentation of this file.
1
2#include "PolygonUtils.hpp"
4#include <igl/predicates/exactinit.h>
5#include <igl/predicates/orient2d.h>
6#include <cassert>
7#include <limits>
9
10namespace
11{
12
13 enum Sign
14 {
15 NEGATIVE = -1,
16 ZERO = 0,
17 POSITIVE = 1
18 };
19
20 // Exact orientation of (q1, q2, p): POSITIVE if p lies to the left of q1->q2.
21 inline Sign point_is_in_half_plane(
22 const Eigen::RowVector2d &p, const Eigen::RowVector2d &q1, const Eigen::RowVector2d &q2)
23 {
24 igl::predicates::exactinit();
25 return Sign(static_cast<int>(igl::predicates::orient2d(q1, q2, p)));
26 }
27
28 inline bool intersect_segments(
29 const Eigen::RowVector2d &p1, const Eigen::RowVector2d &p2,
30 const Eigen::RowVector2d &q1, const Eigen::RowVector2d &q2,
31 Eigen::RowVector2d &result)
32 {
33
34 Eigen::RowVector2d Vp = p2 - p1;
35 Eigen::RowVector2d Vq = q2 - q1;
36 Eigen::RowVector2d pq = q1 - p1;
37
38 double a = Vp(0);
39 double b = -Vq(0);
40 double c = Vp(1);
41 double d = -Vq(1);
42
43 double delta = a * d - b * c;
44 if (delta == 0.0)
45 {
46 return false;
47 }
48
49 double tp = (d * pq(0) - b * pq(1)) / delta;
50
51 result << (1.0 - tp) * p1(0) + tp * p2(0),
52 (1.0 - tp) * p1(1) + tp * p2(1);
53
54 return true;
55 }
56
57} // anonymous namespace
58
59// -----------------------------------------------------------------------------
60
61void polyfem::mesh::clip_polygon_by_half_plane(const Eigen::MatrixXd &P_in,
62 const Eigen::RowVector2d &q1, const Eigen::RowVector2d &q2, Eigen::MatrixXd &P_out)
63{
64 assert(P_in.cols() == 2);
65 std::vector<Eigen::RowVector2d> result;
66
67 if (P_in.rows() == 0)
68 {
69 P_out.resize(0, 2);
70 return;
71 }
72
73 if (P_in.rows() == 1)
74 {
75 if (point_is_in_half_plane(P_in.row(0), q1, q2))
76 {
77 P_out.resize(1, 2);
78 P_out << P_in.row(0);
79 }
80 else
81 {
82 P_out.resize(0, 2);
83 }
84 return;
85 }
86
87 Eigen::RowVector2d prev_p = P_in.row(P_in.rows() - 1);
88 Sign prev_status = point_is_in_half_plane(prev_p, q1, q2);
89
90 for (unsigned int i = 0; i < P_in.rows(); ++i)
91 {
92 Eigen::RowVector2d p = P_in.row(i);
93 Sign status = point_is_in_half_plane(p, q1, q2);
94 if (status != prev_status && status != ZERO && prev_status != ZERO)
95 {
96 Eigen::RowVector2d intersect;
97 if (intersect_segments(prev_p, p, q1, q2, intersect))
98 {
99 result.push_back(intersect);
100 }
101 }
102
103 switch (status)
104 {
105 case NEGATIVE:
106 break;
107 case ZERO:
108 result.push_back(p);
109 break;
110 case POSITIVE:
111 result.push_back(p);
112 break;
113 default:
114 break;
115 }
116
117 prev_p = p;
118 prev_status = status;
119 }
120
121 P_out.resize((int)result.size(), 2);
122 for (size_t i = 0; i < result.size(); ++i)
123 {
124 P_out.row((int)i) = result[i];
125 }
126}
127
129
130void polyfem::mesh::compute_visibility_kernel(const Eigen::MatrixXd &IV, Eigen::MatrixXd &OV)
131{
132 assert(IV.cols() == 2 || IV.cols() == 3);
133
134 // 1) Start from the bounding box of the input points
135 Eigen::MatrixXd src, dst;
136 const auto &minV = IV.colwise().minCoeff().array();
137 const auto &maxV = IV.colwise().maxCoeff().array();
138 src.resize(4, 2);
139 src.row(0) << minV(0), minV(1);
140 src.row(1) << maxV(0), minV(1);
141 src.row(2) << maxV(0), maxV(1);
142 src.row(3) << minV(0), maxV(1);
143
144 // 2) Clip by half planes until we are left with the kernel
145 for (unsigned int i = 0; i < IV.rows(); ++i)
146 {
147 unsigned int j = ((i + 1) % (int)IV.rows());
148 const Eigen::RowVector2d &q1 = IV.row(i).head<2>();
149 const Eigen::RowVector2d &q2 = IV.row(j).head<2>();
150 clip_polygon_by_half_plane(src, q1, q2, dst);
151 std::swap(src, dst);
152 }
153 OV = src;
154}
155
157
158bool polyfem::mesh::is_star_shaped(const Eigen::MatrixXd &IV, Eigen::RowVector3d &bary)
159{
160 Eigen::MatrixXd OV;
162 if (OV.rows() == 0)
163 {
164 return false;
165 }
166 else
167 {
168 bary.setZero();
169 const Eigen::RowVector2d origin = OV.row(0);
170 Eigen::RowVector2d moment = Eigen::RowVector2d::Zero();
171 double twice_area = 0;
172 for (int i = 1; i + 1 < OV.rows(); ++i)
173 {
174 const Eigen::RowVector2d a = OV.row(i) - origin;
175 const Eigen::RowVector2d b = OV.row(i + 1) - origin;
176 const double cross = a.x() * b.y() - a.y() * b.x();
177 twice_area += cross;
178 moment += cross * (a + b);
179 }
180 // Clipping can repeat corners or insert collinear vertices. Their
181 // multiplicity must not change the point used for refinement.
182 if (twice_area != 0)
183 bary.head<2>() = origin + moment / (3 * twice_area);
184 else
185 bary.head<2>() = OV.colwise().mean();
186 return true;
187 }
188}
189
190// -----------------------------------------------------------------------------
191
192void polyfem::mesh::offset_polygon(const Eigen::MatrixXd &IV, Eigen::MatrixXd &OV, double eps)
193{
194#ifdef POLYFEM_WITH_CLIPPER
195 using namespace ClipperLib;
196 using namespace polyfem::utils;
197
198 // Convert input polygon to integer grid
199 ClipperOffset co;
200 co.AddPath(PolygonClipping::toClipperPolygon(IV), jtSquare, etClosedPolygon);
201
202 // Compute offset in the integer grid
203 Paths solution;
204 co.Execute(solution, cInt(eps * DOUBLE_TO_INT_SCALE_FACTOR));
205 assert(solution.size() == 1);
206
207 // Convert back to double
208 OV = PolygonClipping::fromClipperPolygon(solution.front());
209#else
210 throw std::runtime_error("Compile with clipper!");
211#endif
212}
213
215
216namespace
217{
218
219 typedef Eigen::RowVector2d Vec2d;
220
221 double inline det(Vec2d u, Vec2d v)
222 {
223 return u[0] * v[1] - u[1] * v[0];
224 }
225
226 // Return true iff [a,b] intersects [c,d], and store the intersection in ans
227 bool intersect_segment(const Vec2d &a, const Vec2d &b, Vec2d c, Vec2d d, Vec2d &ans)
228 {
229 const double eps = 1e-10; // small epsilon for numerical precision
230 double x = det(c - a, d - c);
231 double y = det(b - a, a - c);
232 double z = det(b - a, d - c);
233 if (std::abs(z) < eps || x * z < 0 || x * z > z * z || y * z < 0 || y * z > z * z)
234 return false;
235 ans = c + (d - c) * y / z;
236 return true;
237 }
238
239 bool is_point_inside(const Eigen::MatrixXd &poly, const Vec2d &outside, const Vec2d &query)
240 {
241 int n = poly.rows();
242 bool tmp, ans = false;
243 for (long i = 0; i < poly.rows(); ++i)
244 {
245 Vec2d m; // Coordinates of intersection point
246 tmp = intersect_segment(query, outside, poly.row(i), poly.row((i + 1) % n), m);
247 ans = (ans != tmp);
248 }
249 return ans;
250 }
251
252} // anonymous namespace
253
254// -----------------------------------------------------------------------------
255
256int polyfem::mesh::is_inside(const Eigen::MatrixXd &IV, const Eigen::MatrixXd &Q, std::vector<bool> &inside)
257{
258 assert(IV.cols() == 2);
259 Eigen::RowVector2d minV = IV.colwise().minCoeff().array();
260 Eigen::RowVector2d maxV = IV.colwise().maxCoeff().array();
261 Eigen::RowVector2d center = 0.5 * (maxV + minV);
262 Eigen::RowVector2d outside(2.0 * minV(0) - center(0), center(1));
263 inside.resize(Q.rows());
264 int num_inside = 0;
265 for (long i = 0; i < Q.rows(); ++i)
266 {
267 inside[i] = is_point_inside(IV, outside, Q.row(i));
268 if (inside[i])
269 {
270 ++num_inside;
271 }
272 }
273 return num_inside;
274}
275
277
278void polyfem::mesh::sample_polygon(const Eigen::MatrixXd &poly, int num_samples, Eigen::MatrixXd &S)
279{
280 assert(poly.rows() >= 2);
281 int n = poly.rows();
282
283 auto length = [&](int i) {
284 return (poly.row(i) - poly.row((i + 1) % n)).norm();
285 };
286
287 // Start at the lexicographically smallest vertex. A longest-edge midpoint
288 // changes when clipping inserts or removes collinear vertices.
289 int i0 = 0;
290 double total_length = 0;
291 const double coordinate_tolerance = 64 * std::numeric_limits<double>::epsilon() * poly.cwiseAbs().maxCoeff();
292 for (int i = 0; i < n; ++i)
293 {
294 total_length += length(i);
295 for (int d = 0; d < poly.cols(); ++d)
296 {
297 const double difference = poly(i, d) - poly(i0, d);
298 if (std::abs(difference) <= coordinate_tolerance)
299 continue;
300 if (difference < 0)
301 i0 = i;
302 break;
303 }
304 }
305
306 // Place samples at the midpoints of equal arclength intervals.
307 S.resize(num_samples, poly.cols());
308 double spacing = total_length / num_samples; // sampling distance
309 double offset = spacing / 2.0; // distance from first sample to vertex i0
310 double distance_to_next = length(i0);
311 for (int s = 0, i = i0; s < num_samples; ++s)
312 {
313 double distance_to_sample = s * spacing + offset; // next sample length
314 while (distance_to_sample > distance_to_next)
315 {
316 i = (i + 1) % n;
317 distance_to_next += length(i);
318 }
319 double t = (distance_to_next - distance_to_sample) / length(i);
320 S.row(s) = t * poly.row(i) + (1.0 - t) * poly.row((i + 1) % n);
321 }
322}
int y
int z
int x
list tmp
Definition p_bases.py:366
Eigen::Matrix< double, dim, 1 > cross(const Eigen::Matrix< double, dim, 1 > &x, const Eigen::Matrix< double, dim, 1 > &y)
void clip_polygon_by_half_plane(const Eigen::MatrixXd &P, const Eigen::RowVector2d &q1, const Eigen::RowVector2d &q2, Eigen::MatrixXd &result)
Clip a polygon by a half-plane.
void sample_polygon(const Eigen::MatrixXd &IV, int num_samples, Eigen::MatrixXd &S)
Sample equal-arclength interval midpoints, starting at the lexicographically smallest polygon vertex.
void offset_polygon(const Eigen::MatrixXd &IV, Eigen::MatrixXd &OV, double eps)
Compute offset polygon.
void compute_visibility_kernel(const Eigen::MatrixXd &IV, Eigen::MatrixXd &OV)
Determine the kernel of the given polygon.
bool is_star_shaped(const Eigen::MatrixXd &IV, Eigen::RowVector3d &bary)
Determine whether a polygon is star-shaped or not.
int is_inside(const Eigen::MatrixXd &IV, const Eigen::MatrixXd &Q, std::vector< bool > &inside)
Compute whether points are inside a polygon.