-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh_filter.cpp
79 lines (72 loc) · 2.31 KB
/
mesh_filter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// Created by Harold on 2021/8/20.
//
#include <open3d/Open3D.h>
#include <fstream>
#include <iostream>
#include <map>
#include "matheval.hpp"
#include "include/io/arg_parser.h"
#include "include/stopwatch.h"
#include <omp.h>
// -i: input stl filepath (.ply/.stl/.obj)
// -o: output stl filepath (.ply/.stl/.obj)
// -f: filter condition ("z > 0.1 && x < 0.5")
int main(int argc, char* argv[]) {
// mesh file io
auto in_file = M_ARG_PARSER::ParseAsString(argc, argv, "-i", "");
if (in_file.empty()) {
std::cerr << "invalid input filename: " << in_file << std::endl;
exit(1);
}
auto out_file = M_ARG_PARSER::ParseAsString(argc, argv, "-o", "");
if (out_file.empty()) {
std::cerr << "invalid output filename: " << in_file << std::endl;
exit(1);
}
std::shared_ptr<open3d::geometry::TriangleMesh> p_source = nullptr;
{
TIME_BLOCK("-read file");
p_source = open3d::io::CreateMeshFromFile(in_file);
if (!p_source) {
std::cerr << "can not read in file: " << in_file << std::endl;
exit(1);
}
}
// read in filter condition
auto f_con = M_ARG_PARSER::ParseAsString(argc, argv, "-f", "");
// filter
std::shared_ptr<open3d::geometry::TriangleMesh> mesh = nullptr;
{
TIME_BLOCK("-filter");
auto const& pts = p_source->vertices_;
std::vector<size_t> indices;
indices.reserve(pts.size());
matheval::Parser parser;
parser.parse(f_con);
// MSVC only support omp 2.0 where only signed int is supported in loop, so disable omp in MSVC if the size is out of int range
#ifndef _WIN32
#pragma omp parallel for
#endif
for (size_t i = 0; i < pts.size(); i++) {
std::map<std::string, double> symtab = {
{ "x", pts[i].x() },
{ "y", pts[i].y() },
{ "z", pts[i].z() }
};
if (parser.evaluate(symtab))
indices.emplace_back(i);
}
mesh = p_source->SelectByIndex(indices);
}
// write output mesh file
{
TIME_BLOCK("-write file");
auto ret = open3d::io::WriteTriangleMesh(out_file, *mesh);
if (!ret) {
std::cerr << "fail to write output file: " << out_file << std::endl;
exit(1);
}
}
return 0;
}