7 #ifndef INTERSECT_POLYS_R3D_H 8 #define INTERSECT_POLYS_R3D_H 16 #include "wonton/intersect/r3d/r3d.h" 20 #include "tangram/support/MatPoly.h" 25 #include "wonton/support/Point.h" 26 #include "wonton/support/Vector.h" 51 facetedpoly_t get_faceted_matpoly(
const Tangram::MatPoly<3>& matpoly) {
53 Tangram::MatPoly<3> faceted_matpoly;
54 matpoly.faceted_matpoly(&faceted_matpoly);
60 for (
auto p : faceted_matpoly.points()) result.
points.push_back(Point<3>(p));
73 const std::vector<std::array<Point<3>, 4>> &target_tet_coords,
82 std::vector<double> source_cell_bounds = {1e99, -1e99, 1e99, -1e99,
88 int num_verts = srcpoly.
points.size();
89 r3d_rvec3 *verts =
new r3d_rvec3[num_verts];
90 for (
int i = 0; i < num_verts; i++) {
91 for (
int j = 0; j < 3; j++) {
92 verts[i].xyz[j] = srcpoly.
points[i][j];
94 if (source_cell_bounds[2*j] > verts[i].xyz[j])
95 source_cell_bounds[2*j] = verts[i].xyz[j];
96 if (source_cell_bounds[2*j+1] < verts[i].xyz[j])
97 source_cell_bounds[2*j+1] = verts[i].xyz[j];
105 r3d_int *face_num_verts =
new r3d_int[num_faces];
106 for (
int i = 0; i < num_faces; i++)
109 r3d_int **face_vert_ids =
new r3d_int *[num_faces];
110 for (
int i = 0; i < num_faces; i++)
111 face_vert_ids[i] =
new r3d_int[face_num_verts[i]];
113 for (
int i = 0; i < num_faces; i++)
122 Point<3> cen(0.0, 0.0, 0.0);
123 for (
int i = 0; i < num_verts; i++)
128 double polyvol = 0.0;
129 for (
int i = 0; i < num_faces; i++) {
136 Vector<3> v0 = p1-p0;
137 Vector<3> v1 = p2-p0;
138 Vector<3> outnormal = cross(v0, v1);
139 Vector<3> v2 = cen-p0;
140 double tetvol = -dot(v2, outnormal)/6.0;
142 std::cerr <<
"Wrong orientation of facets or non-convex polyhedron" <<
147 throw std::runtime_error(
"Source polyhedron has negative volume");
150 r3d_init_poly(&src_r3dpoly, verts, num_verts, face_vert_ids, face_num_verts,
155 std::vector<double> moments(4, 0);
156 for (
auto const & target_cell_tet : target_tet_coords) {
157 std::vector<r3d_plane> faces(4);
159 std::vector<double> target_tet_bounds = {1e99, -1e99, 1e99,
162 for (
int i = 0; i < 4; i++)
163 for (
int j = 0; j < 3; j++) {
164 verts2[i].xyz[j] = target_cell_tet[i][j];
166 if (target_tet_bounds[2*j] > verts2[i].xyz[j])
167 target_tet_bounds[2*j] = verts2[i].xyz[j];
168 if (target_tet_bounds[2*j+1] < verts2[i].xyz[j])
169 target_tet_bounds[2*j+1] = verts2[i].xyz[j];
176 bool check_bb =
true;
178 bool disjoint =
false;
179 for (
int j = 0; j < 3 && !disjoint; ++j)
180 disjoint = (target_tet_bounds[2*j] > source_cell_bounds[2*j+1]+bbeps ||
181 target_tet_bounds[2*j+1] < source_cell_bounds[2*j]-bbeps);
182 if (disjoint)
continue;
186 if (r3d_orient(verts2) < 0)
187 throw std::runtime_error(
"target_wedge has negative volume");
190 r3d_tet_faces_from_verts(&faces[0], verts2);
195 r3d_poly src_r3dpoly_copy = src_r3dpoly;
196 r3d_clip(&src_r3dpoly_copy, &faces[0], 4);
199 const int POLY_ORDER = 1;
200 r3d_real om[R3D_NUM_MOMENTS(POLY_ORDER)];
201 r3d_reduce(&src_r3dpoly_copy, om, POLY_ORDER);
208 throw std::runtime_error(
"Negative volume");
211 for (
int i = 0; i < 4; i++)
216 delete [] face_num_verts;
217 for (
int i = 0; i < num_faces; i++)
218 delete [] face_vert_ids[i];
219 delete [] face_vert_ids;
226 #endif // INTERSECT_POLYS_R3D_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
Definition: intersect_polys_r3d.h:34
std::vector< double > intersect_polys_r3d(const facetedpoly_t &srcpoly, const std::vector< std::array< Point< 3 >, 4 >> &target_tet_coords, NumericTolerances_t num_tols)
Definition: intersect_polys_r3d.h:72
Definition: coredriver.h:42
std::vector< Point< 3 > > points
Definition: intersect_polys_r3d.h:41
std::vector< std::vector< int > > facetpoints
Definition: intersect_polys_r3d.h:36
struct Portage::facetedpoly facetedpoly_t