11 const Eigen::MatrixXd &subject_polygon,
12 const Eigen::MatrixXd &clipping_polygon)
34 using namespace PolyClipper;
37 std::vector<Vertex2d<>> poly;
39 std::vector<Vector2d> positions;
40 positions.reserve(subject_polygon.rows());
41 std::vector<std::vector<int>> neighbors;
42 neighbors.reserve(subject_polygon.rows());
43 for (
int i = 0; i < subject_polygon.rows(); i++)
46 neighbors.emplace_back(2);
48 neighbors.back()[0] = (i + 1) % subject_polygon.rows();
50 neighbors.back()[1] = i == 0 ? (subject_polygon.rows() - 1) : (i - 1);
53 initializePolygon(poly, positions, neighbors);
59 std::vector<Plane<internal::VectorAdapter<Vector2d>>> planes;
60 for (
int i = 0; i < clipping_polygon.rows(); i++)
63 const Vector2d e1 =
toPolyClipperVector(clipping_polygon.row((i + 1) % clipping_polygon.rows()));
64 const Vector2d e = e1 - e0;
65 planes.emplace_back(e0, Vector2d(e.y, -e.x).unitVector());
68 clipPolygon(poly, planes);
72 std::vector<Eigen::MatrixXd> result;
74 std::vector<std::vector<Eigen::Vector2d>> polygons;
76 std::vector<bool> found_vertex(poly.size(),
false);
77 for (
int i = 0; i < poly.size(); i++)
80 polygons.emplace_back();
83 while (!found_vertex[j])
85 found_vertex[j] =
true;
87 j = poly[j].neighbors.first;
91 for (
const std::vector<Eigen::Vector2d> &polygon : polygons)
93 result.emplace_back(polygon.size(), 2);
94 for (
int i = 0; i < polygon.size(); i++)
95 result.back().row(i) = polygon[i];
104 const Eigen::MatrixXd &subject_triangle,
105 const Eigen::MatrixXd &clipping_triangle)
107 const std::vector<Eigen::MatrixXd> overlap =
111 assert(overlap.size() <= 1);
113 if (overlap.empty() || overlap[0].rows() < 3)
114 return std::vector<Eigen::MatrixXd>();
120 const Eigen::MatrixXd &subject_tet,
121 const Eigen::MatrixXd &clipping_tet)
123 using namespace PolyClipper;
124 assert(subject_tet.rows() == 4 && subject_tet.cols() == 3);
125 assert(clipping_tet.rows() == 4 && clipping_tet.cols() == 3);
130 std::vector<Vertex3d<>> poly;
132 std::vector<Vector3d> positions(subject_tet.rows());
133 for (
int i = 0; i < subject_tet.rows(); i++)
136 std::vector<std::vector<int>> neighbors = {{1, 3, 2}, {2, 3, 0}, {0, 3, 1}, {0, 1, 2}};
138 initializePolyhedron(poly, positions, neighbors);
149 std::vector<Plane<internal::VectorAdapter<Vector3d>>> planes;
150 planes.emplace_back(t0, (t1 - t0).
cross(t2 - t0).unitVector());
151 planes.emplace_back(t0, (t2 - t0).
cross(t3 - t0).unitVector());
152 planes.emplace_back(t0, (t3 - t0).
cross(t1 - t0).unitVector());
153 planes.emplace_back(t1, (t3 - t1).
cross(t2 - t1).unitVector());
155 clipPolyhedron(poly, planes);
159 const std::vector<std::vector<int>> T = splitIntoTetrahedra(poly);
160 std::vector<Eigen::MatrixXd> result(T.size(), Eigen::MatrixXd(4, 3));
161 for (
int i = 0; i < T.size(); i++)
163 for (
int j = 0; j < 4; j++)
Eigen::Matrix< double, dim, 1 > cross(const Eigen::Matrix< double, dim, 1 > &x, const Eigen::Matrix< double, dim, 1 > &y)
double tetrahedron_volume(const Eigen::Vector3d &a, const Eigen::Vector3d &b, const Eigen::Vector3d &c, const Eigen::Vector3d &d)
Compute the signed volume of a tetrahedron defined by four points.