forked from atenpas/gpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect_grasps.cpp
94 lines (78 loc) · 2.72 KB
/
detect_grasps.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <string>
#include <gpd/grasp_detector.h>
namespace gpd {
namespace apps {
namespace detect_grasps {
bool checkFileExists(const std::string &file_name) {
std::ifstream file;
file.open(file_name.c_str());
if (!file) {
std::cout << "File " + file_name + " could not be found!\n";
return false;
}
file.close();
return true;
}
int DoMain(int argc, char *argv[]) {
// Read arguments from command line.
if (argc < 3) {
std::cout << "Error: Not enough input arguments!\n\n";
std::cout << "Usage: detect_grasps CONFIG_FILE PCD_FILE [NORMALS_FILE]\n\n";
std::cout << "Detect grasp poses for a point cloud, PCD_FILE (*.pcd), "
"using parameters from CONFIG_FILE (*.cfg).\n\n";
std::cout << "[NORMALS_FILE] (optional) contains a surface normal for each "
"point in the cloud (*.csv).\n";
return (-1);
}
std::string config_filename = argv[1];
std::string pcd_filename = argv[2];
if (!checkFileExists(config_filename)) {
printf("Error: config file not found!\n");
return (-1);
}
if (!checkFileExists(pcd_filename)) {
printf("Error: PCD file not found!\n");
return (-1);
}
// Read parameters from configuration file.
util::ConfigFile config_file(config_filename);
config_file.ExtractKeys();
// Set the camera position. Assumes a single camera view.
std::vector<double> camera_position =
config_file.getValueOfKeyAsStdVectorDouble("camera_position",
"0.0 0.0 0.0");
Eigen::Matrix3Xd view_points(3, 1);
view_points << camera_position[0], camera_position[1], camera_position[2];
// Load point cloud from file.
util::Cloud cloud(pcd_filename, view_points);
if (cloud.getCloudOriginal()->size() == 0) {
std::cout << "Error: Input point cloud is empty or does not exist!\n";
return (-1);
}
// Load surface normals from file.
if (argc > 3) {
std::string normals_filename = argv[3];
cloud.setNormalsFromFile(normals_filename);
std::cout << "Loaded surface normals from file: " << normals_filename
<< "\n";
}
GraspDetector detector(config_filename);
// Preprocess the point cloud.
detector.preprocessPointCloud(cloud);
// If the object is centered at the origin, reverse all surface normals.
bool centered_at_origin =
config_file.getValueOfKey<bool>("centered_at_origin", false);
if (centered_at_origin) {
printf("Reversing normal directions ...\n");
cloud.setNormals(cloud.getNormals() * (-1.0));
}
// Detect grasp poses.
detector.detectGrasps(cloud);
return 0;
}
} // namespace detect_grasps
} // namespace apps
} // namespace gpd
int main(int argc, char *argv[]) {
return gpd::apps::detect_grasps::DoMain(argc, argv);
}