4#include <igl/predicates/exactinit.h>
5#include <igl/predicates/orient2d.h>
21 inline Sign point_is_in_half_plane(
22 const Eigen::RowVector2d &p,
const Eigen::RowVector2d &q1,
const Eigen::RowVector2d &q2)
24 igl::predicates::exactinit();
25 return Sign(
static_cast<int>(igl::predicates::orient2d(q1, q2, p)));
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)
34 Eigen::RowVector2d Vp = p2 - p1;
35 Eigen::RowVector2d Vq = q2 - q1;
36 Eigen::RowVector2d pq = q1 - p1;
43 double delta = a * d -
b * c;
49 double tp = (d * pq(0) -
b * pq(1)) / delta;
51 result << (1.0 - tp) * p1(0) + tp * p2(0),
52 (1.0 - tp) * p1(1) + tp * p2(1);
62 const Eigen::RowVector2d &q1,
const Eigen::RowVector2d &q2, Eigen::MatrixXd &P_out)
64 assert(P_in.cols() == 2);
65 std::vector<Eigen::RowVector2d> result;
75 if (point_is_in_half_plane(P_in.row(0), q1, q2))
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);
90 for (
unsigned int i = 0; i < P_in.rows(); ++i)
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)
96 Eigen::RowVector2d intersect;
97 if (intersect_segments(prev_p, p, q1, q2, intersect))
99 result.push_back(intersect);
118 prev_status = status;
121 P_out.resize((
int)result.size(), 2);
122 for (
size_t i = 0; i < result.size(); ++i)
124 P_out.row((
int)i) = result[i];
132 assert(IV.cols() == 2 || IV.cols() == 3);
135 Eigen::MatrixXd src, dst;
136 const auto &minV = IV.colwise().minCoeff().array();
137 const auto &maxV = IV.colwise().maxCoeff().array();
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);
145 for (
unsigned int i = 0; i < IV.rows(); ++i)
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>();
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)
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();
178 moment +=
cross * (a + b);
183 bary.head<2>() = origin + moment / (3 * twice_area);
185 bary.head<2>() = OV.colwise().mean();
194#ifdef POLYFEM_WITH_CLIPPER
195 using namespace ClipperLib;
200 co.AddPath(PolygonClipping::toClipperPolygon(IV), jtSquare, etClosedPolygon);
204 co.Execute(solution, cInt(eps * DOUBLE_TO_INT_SCALE_FACTOR));
205 assert(solution.size() == 1);
208 OV = PolygonClipping::fromClipperPolygon(solution.front());
210 throw std::runtime_error(
"Compile with clipper!");
219 typedef Eigen::RowVector2d Vec2d;
221 double inline det(Vec2d u, Vec2d v)
223 return u[0] * v[1] - u[1] * v[0];
227 bool intersect_segment(
const Vec2d &a,
const Vec2d &b, Vec2d c, Vec2d d, Vec2d &ans)
229 const double eps = 1e-10;
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)
235 ans = c + (d - c) *
y /
z;
239 bool is_point_inside(
const Eigen::MatrixXd &poly,
const Vec2d &outside,
const Vec2d &query)
242 bool tmp, ans =
false;
243 for (
long i = 0; i < poly.rows(); ++i)
246 tmp = intersect_segment(query, outside, poly.row(i), poly.row((i + 1) % n), m);
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());
265 for (
long i = 0; i < Q.rows(); ++i)
267 inside[i] = is_point_inside(IV, outside, Q.row(i));
280 assert(poly.rows() >= 2);
283 auto length = [&](
int i) {
284 return (poly.row(i) - poly.row((i + 1) % n)).norm();
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)
294 total_length += length(i);
295 for (
int d = 0; d < poly.cols(); ++d)
297 const double difference = poly(i, d) - poly(i0, d);
298 if (std::abs(difference) <= coordinate_tolerance)
307 S.resize(num_samples, poly.cols());
308 double spacing = total_length / num_samples;
309 double offset = spacing / 2.0;
310 double distance_to_next = length(i0);
311 for (
int s = 0, i = i0; s < num_samples; ++s)
313 double distance_to_sample = s * spacing + offset;
314 while (distance_to_sample > distance_to_next)
317 distance_to_next += length(i);
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);
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.