in source/render/MeshUtil.h [345:405]
inline void applyMaskToVertexesAndFaces(
Eigen::MatrixXd& vertexes,
Eigen::MatrixXi& faces,
const cv::Mat_<bool> mask) {
const int width = mask.cols;
const int height = mask.rows;
CHECK_EQ(width * height, vertexes.rows());
bool* originalVertexMask = (bool*)mask.data;
// Keep faces only if all vertexes have a non-zero mask
std::vector<int> outputFaceIndexes;
for (int i = 0; i < faces.rows(); ++i) {
const Eigen::Vector3i& face = faces.row(i);
bool allVertexesRetained = true;
for (int j = 0; j < face.size(); ++j) {
if (!originalVertexMask[face(j)]) {
allVertexesRetained = false;
break;
}
}
if (allVertexesRetained) {
outputFaceIndexes.push_back(i);
}
}
// Keep only vertexes of retained faces
std::vector<bool> vertexMask(vertexes.rows(), false);
for (int i : outputFaceIndexes) {
const Eigen::Vector3i& face = faces.row(i);
for (int j = 0; j < face.size(); ++j) {
vertexMask[face(j)] = true;
}
}
// Compute mapping between old and new vertex indices
std::map<int, int> inputToOutputVertexIndexes;
int numOutputVertexes = 0;
for (int i = 0; i < vertexes.rows(); ++i) {
if (vertexMask[i]) {
inputToOutputVertexIndexes[i] = numOutputVertexes++;
}
}
// create output vertex matrix
Eigen::MatrixXd inputVertexes = vertexes;
vertexes = Eigen::MatrixXd(numOutputVertexes, 3);
for (auto iter = inputToOutputVertexIndexes.begin(); iter != inputToOutputVertexIndexes.end();
++iter) {
vertexes.row(iter->second) = inputVertexes.row(iter->first);
}
// Create output face matrix and re-index vertexes
Eigen::MatrixXi inputFaces = faces;
faces = Eigen::MatrixXi(outputFaceIndexes.size(), 3);
for (ssize_t i = 0; i < ssize(outputFaceIndexes); ++i) {
const Eigen::Vector3i& face = inputFaces.row(outputFaceIndexes[i]);
for (int j = 0; j < face.size(); ++j) {
faces(i, j) = inputToOutputVertexIndexes[face(j)];
}
}
}