create_convex_hull.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 
36 
37 namespace
38 {
39 const size_t ERROR_IN_COMMAND_LINE = 1;
40 const size_t SUCCESS = 0;
41 const size_t ERROR_UNHANDLED_EXCEPTION = 2;
42 
43 } // namespace
44 
45 int main(int argc, char** argv)
46 {
47  std::string input;
48  std::string output;
49  double shrink = -1.0;
50  double clamp = -1.0;
51 
52  namespace po = boost::program_options;
53  po::options_description desc("Options");
54  desc.add_options()("help,h", "Print help messages")(
55  "input,i", po::value<std::string>(&input)->required(), "File path to mesh used to create a convex hull.")(
56  "output,o", po::value<std::string>(&output)->required(), "File path to save the generated convex hull as a ply.")(
57  "shrink,s",
58  po::value<double>(&shrink),
59  "If positive, the convex hull is shrunken by that amount (each face is moved by 'shrink' length units towards "
60  "the center along its normal).")("clamp,c",
61  po::value<double>(&clamp),
62  "If positive, 'shrink' is clamped to not exceed 'clamp * innerRadius', where "
63  "'innerRadius' is the minimum distance of a face to the center of the convex "
64  "hull.");
65 
66  po::variables_map vm;
67  try
68  {
69  po::store(po::parse_command_line(argc, argv, desc), vm); // can throw
70 
72  if (vm.count("help") != 0U)
73  {
74  std::cout << "Basic Command Line Parameter App\n" << desc << "\n";
75  return SUCCESS;
76  }
77 
78  po::notify(vm); // throws on error, so do after help in case
79  // there are any problems
80  }
81  catch (po::error& e)
82  {
83  std::cerr << "ERROR: " << e.what() << "\n\n";
84  std::cerr << desc << "\n";
85  return ERROR_IN_COMMAND_LINE;
86  }
87 
88  std::ifstream file(input, std::ios::binary | std::ios::ate);
89  std::streamsize size = file.tellg();
90  if (size < 0)
91  {
92  CONSOLE_BRIDGE_logError("Failed to locate input file!");
94  }
95 
97  Eigen::VectorXi mesh_faces;
98  int num_faces = tesseract_collision::loadSimplePlyFile(input, mesh_vertices, mesh_faces);
99  if (num_faces < 0)
100  {
101  CONSOLE_BRIDGE_logError("Failed to read mesh from file!");
103  }
104 
106  Eigen::VectorXi ch_faces;
107  int ch_num_faces = tesseract_collision::createConvexHull(ch_vertices, ch_faces, mesh_vertices);
108 
109  if (ch_num_faces < 0)
110  {
111  CONSOLE_BRIDGE_logError("Failed to create convex hull!");
113  }
114 
115  if (!tesseract_collision::writeSimplePlyFile(output, ch_vertices, ch_faces, ch_num_faces))
116  {
117  CONSOLE_BRIDGE_logError("Failed to write convex hull to file!");
119  }
120 
121  return 0;
122 }
tesseract_collision::createConvexHull
int createConvexHull(tesseract_common::VectorVector3d &vertices, Eigen::VectorXi &faces, const tesseract_common::VectorVector3d &input, double shrink=-1, double shrinkClamp=-1)
Create a convex hull from vertices using Bullet Convex Hull Computer.
Definition: convex_hull_utils.cpp:37
TESSERACT_COMMON_IGNORE_WARNINGS_POP::SUCCESS
const size_t SUCCESS
Definition: create_convex_hull.cpp:40
main
int main(int argc, char **argv)
Definition: create_convex_hull.cpp:45
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
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
convex_hull_utils.h
This is a collection of common methods.
tesseract_common::VectorVector3d
std::vector< Eigen::Vector3d > VectorVector3d
TESSERACT_COMMON_IGNORE_WARNINGS_POP
Definition: create_convex_hull.cpp:37
TESSERACT_COMMON_IGNORE_WARNINGS_POP::ERROR_IN_COMMAND_LINE
const size_t ERROR_IN_COMMAND_LINE
Definition: create_convex_hull.cpp:39
common.h
This is a collection of common methods.
TESSERACT_COMMON_IGNORE_WARNINGS_POP::ERROR_UNHANDLED_EXCEPTION
const size_t ERROR_UNHANDLED_EXCEPTION
Definition: create_convex_hull.cpp:41
macros.h
VHACD::clamp
T clamp(const T &v, const T &lo, const T &hi)
Definition: VHACD.h:506
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
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:01:52