7 #ifndef INTERSECT_POLYS_R2D_H 8 #define INTERSECT_POLYS_R2D_H 16 #include "wonton/intersect/r3d/r2d.h" 20 #include "wonton/support/Point.h" 33 std::vector<double> moments(3, 0);
34 bool src_convex =
true;
35 bool trg_convex =
true;
37 const int POLY_ORDER = 1;
41 const int size1 = source_poly.size();
42 const int size2 = target_poly.size();
46 std::vector<r2d_rvec2> verts1(size1);
47 for (
int i = 0; i < size1; ++i) {
48 verts1[i].xy[0] = source_poly[i][0];
49 verts1[i].xy[1] = source_poly[i][1];
53 for (
int i = 1; i < size1-1; ++i)
54 volume1 += r2d_orient(verts1[0], verts1[i], verts1[i+1]);
56 throw std::runtime_error(
"target_poly has negative volume");
60 r2d_init_poly(&srcpoly_r2d, &verts1[0], size1);
63 if (r2d_is_good(&srcpoly_r2d) == 0)
64 throw std::runtime_error(
"source_poly: invalid poly");
70 std::vector<r2d_plane> faces(size2);
71 std::vector<r2d_rvec2> verts2(size2);
72 for (
int i = 0; i < size2; ++i) {
73 verts2[i].xy[0] = target_poly[i][0];
74 verts2[i].xy[1] = target_poly[i][1];
78 for (
int i = 0; i < size2; ++i) {
81 int ifv = i, imv = (i+1)%size2, isv = (i+2)%size2;
82 Wonton::Vector<2> normal( target_poly[isv][1] - target_poly[ifv][1],
83 -target_poly[isv][0] + target_poly[ifv][0]);
85 Wonton::Vector<2> fv2mv = target_poly[imv] - target_poly[ifv];
86 double dst = Wonton::dot(fv2mv, normal);
96 r2d_poly_faces_from_verts(&faces[0], &verts2[0], size2);
99 r2d_clip(&srcpoly_r2d, &faces[0], size2);
102 r2d_real om[R2D_NUM_MOMENTS(POLY_ORDER)];
103 r2d_reduce(&srcpoly_r2d, om, POLY_ORDER);
109 throw std::runtime_error(
"Negative volume");
112 for (
int j = 0; j < 3; ++j)
118 for (
int i = 0; i < size1; ++i) {
121 int ifv = i, imv = (i+1)%size1, isv = (i+2)%size1;
122 Wonton::Vector<2> normal( source_poly[isv][1] - source_poly[ifv][1],
123 -source_poly[isv][0] + source_poly[ifv][0]);
125 Wonton::Vector<2> fv2mv = source_poly[imv] - source_poly[ifv];
126 double dst = Wonton::dot(fv2mv, normal);
144 bool center_point_ok =
true;
145 Wonton::Point<2> cen(0.0, 0.0);
147 cenr2d.xy[0] = 0.0; cenr2d.xy[1] = 0.0;
148 double area_sum = 0.0;
149 for (
int i = 1; i < size2; ++i) {
150 double area = r2d_orient(verts2[0], verts2[i], verts2[(i+1)%size2]);
152 Wonton::Point<2> tricen =
153 (target_poly[0] + target_poly[i] + target_poly[(i+1)%size2])/3.0;
157 cenr2d.xy[0] = cen[0]; cenr2d.xy[1] = cen[1];
159 for (
int i = 0; i < size2; ++i)
160 if (r2d_orient(cenr2d, verts2[i], verts2[(i+1)%size2]) < 0.)
161 center_point_ok =
false;
163 if (!center_point_ok) {
171 r2d_init_poly(&fspoly, &verts2[0], size2);
173 r2d_poly_faces_from_verts(&faces[0], &verts2[0], size2);
175 r2d_clip(&fspoly, &faces[0], size2);
178 if (fspoly.nverts == 0)
179 std::runtime_error(
"Could not find a valid center point to triangulate non-convex polygon");
184 r2d_real fspoly_moments[R2D_NUM_MOMENTS(POLY_ORDER)];
185 r2d_reduce(&fspoly, fspoly_moments, POLY_ORDER);
187 cen[0] = cenr2d.xy[0] = fspoly_moments[1]/fspoly_moments[0];
188 cen[1] = cenr2d.xy[1] = fspoly_moments[2]/fspoly_moments[0];
195 for (
int i = 0; i < size2; ++i)
196 if (r2d_orient(cenr2d, verts2[i], verts2[(i+1)%size2]) < 0.)
197 center_point_ok =
false;
201 if (!center_point_ok)
202 std::runtime_error(
"Could not find a valid center point to triangulate non-convex polygon");
205 for (
int i = 0; i < size2; ++i) {
206 verts2[0].xy[0] = cen[0];
207 verts2[0].xy[1] = cen[1];
208 verts2[1].xy[0] = target_poly[i][0];
209 verts2[1].xy[1] = target_poly[i][1];
210 verts2[2].xy[0] = target_poly[(i+1)%size2][0];
211 verts2[2].xy[1] = target_poly[(i+1)%size2][1];
213 r2d_poly_faces_from_verts(&faces[0], &verts2[0], 3);
215 r2d_poly srcpoly_r2d_copy = srcpoly_r2d;
218 r2d_clip(&srcpoly_r2d_copy, &faces[0], 3);
223 if (R2D_NUM_MOMENTS(POLY_ORDER) != 3)
224 throw std::runtime_error(
"Invalid number of moments");
226 r2d_real om[R2D_NUM_MOMENTS(POLY_ORDER)];
227 r2d_reduce(&srcpoly_r2d_copy, om, POLY_ORDER);
233 throw std::runtime_error(
"Negative volume for triangle of polygon");
236 for (
int j = 0; j < 3; ++j)
247 #endif // INTERSECT_POLYS_R2D_H double min_absolute_distance
Definition: portage.h:165
std::vector< T > vector
Definition: portage.h:238
Intersection and other tolerances to handle tiny values.
Definition: portage.h:152
double minimal_intersection_volume
Definition: portage.h:160
std::vector< double > intersect_polys_r2d(std::vector< Wonton::Point< 2 >> const &source_poly, std::vector< Wonton::Point< 2 >> const &target_poly, NumericTolerances_t num_tols)
Definition: intersect_polys_r2d.h:29
Definition: coredriver.h:42