create_convex_decomposition.cpp
Go to the documentation of this file.
1 
28 #include <console_bridge/console.h>
29 #include <boost/program_options.hpp>
30 #include <iostream>
31 #include <fstream>
33 
37 
38 namespace
39 {
40 const size_t ERROR_IN_COMMAND_LINE = 1;
41 const size_t SUCCESS = 0;
42 const size_t ERROR_UNHANDLED_EXCEPTION = 2;
43 
44 } // namespace
45 
46 template <typename T>
47 void check_range(const T& value, const T& min, const T& max)
48 {
49  if (value < min || value > max)
50  {
51  std::stringstream ss;
52  ss << "Value " << value << " is not in valid range [" << min << ", " << max << "]";
53  throw std::runtime_error(ss.str());
54  }
55 }
56 
57 int main(int argc, char** argv)
58 {
59  std::string input;
60  std::string output;
62 
63  // clang-format off
64  namespace po = boost::program_options;
65  po::options_description desc("Options");
66  desc.add_options()
67  (
68  "help,h",
69  "Print help messages"
70  )
71  (
72  "input,i",
73  po::value<std::string>(&input)->required(),
74  "File path to mesh used to create a convex hull."
75  )
76  (
77  "output,o",
78  po::value<std::string>(&output)->required(),
79  "File path to save the generated convex hull as a ply."
80  )
81  (
82  "max_convex_hulls,n",
83  po::value<unsigned>(&(params.max_convex_hulls))->notifier([](const unsigned& val){check_range(val, 1u, 32u);}),
84  "Maximum number of convex hulls"
85  )
86  (
87  "resolution,r",
88  po::value<unsigned>(&(params.resolution))->notifier([](const unsigned& val){check_range(val, 1u, std::numeric_limits<unsigned>::max());}),
89  "Number of voxels to use to represent the shape"
90  )
91  (
92  "min_volume_percent_error,e",
93  po::value<double>(&(params.minimum_volume_percent_error_allowed))->notifier([](const double& val){check_range(val, 0.001, 10.0);}),
94  "If the voxels are within this threshold percentage of the volume of the hull, we consider this a close enough approximation"
95  )
96  (
97  "max_recursion_depth,d",
98  po::value<unsigned>(&(params.max_recursion_depth)),
99  "Maximum recursion depth for convex decomposition improvement"
100  )
101  (
102  "shrinkwrap,s",
103  po::value<bool>(&(params.shrinkwrap)),
104  "Shrinkwrap the voxel positions to the source mesh on output"
105  )
106  (
107  "max_num_vertices,v",
108  po::value<unsigned>(&(params.max_num_vertices_per_ch))->notifier([](const unsigned& val){check_range(val, 4u, std::numeric_limits<unsigned>::max());}),
109  "Maximum number of vertices per convex hull"
110  )
111  (
112  "min_edge_length,l",
113  po::value<unsigned>(&(params.min_edge_length))->notifier([](const unsigned& val){check_range(val, 1u, std::numeric_limits<unsigned>::max());}),
114  "Once a voxel patch has an edge length of less than this value in all 3 dimensions, stop recursing"
115  )
116  (
117  "find_best_plane,p",
118  po::value<bool>(&(params.find_best_plane)),
119  "Flag for attempting to split planes along best location (experimental)"
120  );
121  // clang-format on
122 
123  po::variables_map vm;
124  try
125  {
126  po::store(po::parse_command_line(argc, argv, desc), vm); // can throw
127 
129  if (vm.count("help") != 0U)
130  {
131  std::cout << "Basic Command Line Parameter App\n" << desc << "\n";
132  return SUCCESS;
133  }
134 
135  po::notify(vm); // throws on error, so do after help in case
136  // there are any problems
137  }
138  catch (po::error& e)
139  {
140  std::cerr << "ERROR: " << e.what() << "\n\n";
141  std::cerr << desc << "\n";
142  return ERROR_IN_COMMAND_LINE;
143  }
144 
145  std::ifstream file(input, std::ios::binary | std::ios::ate);
146  std::streamsize size = file.tellg();
147  if (size < 0)
148  {
149  CONSOLE_BRIDGE_logError("Failed to locate input file!");
151  }
152 
153  tesseract_common::VectorVector3d mesh_vertices;
154  Eigen::VectorXi mesh_faces;
155  int num_faces = tesseract_collision::loadSimplePlyFile(input, mesh_vertices, mesh_faces, true);
156  if (num_faces < 0)
157  {
158  CONSOLE_BRIDGE_logError("Failed to read mesh from file!");
160  }
161 
162  tesseract_collision::ConvexDecompositionVHACD convex_decomp(params);
163  std::vector<std::shared_ptr<tesseract_geometry::ConvexMesh>> convex_hulls =
164  convex_decomp.compute(mesh_vertices, mesh_faces);
165 
166  if (convex_hulls.empty())
167  {
168  CONSOLE_BRIDGE_logError("Failed to create convex decomposition!");
170  }
171 
172  for (std::size_t i = 0; i < convex_hulls.size(); ++i)
173  {
174  auto ch = convex_hulls[i];
176  std::to_string(i) + "_" + output, *(ch->getVertices()), *(ch->getFaces()), ch->getFaceCount()))
177  {
178  CONSOLE_BRIDGE_logError("Failed to write convex hull to file!");
180  }
181  }
182 
183  return 0;
184 }
tesseract_collision::VHACDParameters::min_edge_length
uint32_t min_edge_length
Once a voxel patch has an edge length of less than 4 on all 3 sides, we don't keep recursing.
Definition: convex_decomposition_vhacd.h:59
tesseract_collision::VHACDParameters::minimum_volume_percent_error_allowed
double minimum_volume_percent_error_allowed
if the voxels are within 1% of the volume of the hull, we consider this a close enough approximation
Definition: convex_decomposition_vhacd.h:47
TESSERACT_COMMON_IGNORE_WARNINGS_POP::SUCCESS
const size_t SUCCESS
Definition: create_convex_hull.cpp:40
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
convex_decomposition_vhacd.h
Convex decomposition VHACD implementation.
check_range
void check_range(const T &value, const T &min, const T &max)
Definition: create_convex_decomposition.cpp:47
tesseract_collision::loadSimplePlyFile
int loadSimplePlyFile(const std::string &path, tesseract_common::VectorVector3d &vertices, Eigen::VectorXi &faces, bool triangles_only=false)
Loads a simple ply file given a path.
Definition: common.cpp:289
tesseract_collision::ConvexDecompositionVHACD
Definition: convex_decomposition_vhacd.h:66
tesseract_collision::VHACDParameters::max_convex_hulls
uint32_t max_convex_hulls
The maximum number of convex hulls to produce.
Definition: convex_decomposition_vhacd.h:43
tesseract_collision::VHACDParameters::find_best_plane
bool find_best_plane
Whether or not to attempt to split planes along the best location. Experimental feature....
Definition: convex_decomposition_vhacd.h:61
tesseract_common::VectorVector3d
std::vector< Eigen::Vector3d > VectorVector3d
TESSERACT_COMMON_IGNORE_WARNINGS_POP
Definition: create_convex_hull.cpp:37
tesseract_collision::VHACDParameters::resolution
uint32_t resolution
The voxel resolution to use.
Definition: convex_decomposition_vhacd.h:45
TESSERACT_COMMON_IGNORE_WARNINGS_POP::ERROR_IN_COMMAND_LINE
const size_t ERROR_IN_COMMAND_LINE
Definition: create_convex_hull.cpp:39
tesseract_collision::VHACDParameters::max_recursion_depth
uint32_t max_recursion_depth
The maximum recursion depth.
Definition: convex_decomposition_vhacd.h:49
common.h
This is a collection of common methods.
convex_mesh.h
tesseract_collision::VHACDParameters
Definition: convex_decomposition_vhacd.h:40
TESSERACT_COMMON_IGNORE_WARNINGS_POP::ERROR_UNHANDLED_EXCEPTION
const size_t ERROR_UNHANDLED_EXCEPTION
Definition: create_convex_hull.cpp:41
macros.h
tesseract_collision::ConvexDecompositionVHACD::compute
std::vector< std::shared_ptr< tesseract_geometry::ConvexMesh > > compute(const tesseract_common::VectorVector3d &vertices, const Eigen::VectorXi &faces) const override
Run convex decomposition algorithm.
Definition: convex_decomposition_vhacd.cpp:37
main
int main(int argc, char **argv)
Definition: create_convex_decomposition.cpp:57
tesseract_collision::VHACDParameters::max_num_vertices_per_ch
uint32_t max_num_vertices_per_ch
The maximum number of vertices allowed in any output convex hull.
Definition: convex_decomposition_vhacd.h:55
tesseract_collision::writeSimplePlyFile
bool writeSimplePlyFile(const std::string &path, const tesseract_common::VectorVector3d &vertices, const std::vector< Eigen::Vector3i > &vectices_color, const Eigen::VectorXi &faces, int num_faces)
Write a simple ply file given vertices and faces.
Definition: common.cpp:174
tesseract_collision::VHACDParameters::shrinkwrap
bool shrinkwrap
Whether or not to shrinkwrap the voxel positions to the source mesh on output.
Definition: convex_decomposition_vhacd.h:51


tesseract_collision
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:01:52