-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjectExtractor.cpp
136 lines (117 loc) · 3.9 KB
/
objectExtractor.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <fstream>
#include <iostream>
#include <string>
#include <ios>
#include <sstream>
#include <boost/property_tree/xml_parser.hpp>
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <boost/property_tree/ptree.hpp>
#include <pcl/filters/passthrough.h>
using namespace boost::property_tree;
static const std::string TAG_SCENARIO = "scenario";
static const std::string TAG_NAME = "name";
static const std::string TAG_OBJECT = "object";
static const std::string TAG_NOBJECTS = "numberOfObjects";
static const std::string TAG_ALLOBJECTS = "allObjects";
static const std::string TAG_POSE = "pose";
static const std::string TAG_DIMENSIONS = "dimensions";
static const std::string TAG_LENGTH = "length";
static const std::string TAG_WIDTH = "width";
static const std::string TAG_HEIGHT = "height";
static const std::string TAG_COLOR = "color";
static const std::string TAG_INDICES = "indices";
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr scene(new pcl::PointCloud<pcl::PointXYZRGBA>());
pcl::PCDReader reader;
pcl::PCDWriter writer;
class object
{
public:
std::string name;
std::string color;
pcl::PointIndices indices;
};
std::vector<object> _objectList;
void parseObject(ptree &parent){
object newObject;
// Name and color
newObject.name = parent.get<std::string>(TAG_NAME);
newObject.color = parent.get<std::string>(TAG_COLOR);
// Get indices
std::istringstream str(parent.get<std::string>(TAG_INDICES));
newObject.indices.indices.clear();
int i;
while(str >> i){
newObject.indices.indices.push_back(i);
}
_objectList.push_back(newObject);
}
std::string importObjectsInformation(char *xmlFile)
{
ptree root;
read_xml(xmlFile, root);
std::string scenarioType = root.get<std::string>(TAG_SCENARIO + "." + "type");
ptree& tableDimensions = root.get_child(TAG_SCENARIO + "." + TAG_DIMENSIONS);
ptree& allObjects = root.get_child(TAG_SCENARIO + "." + TAG_ALLOBJECTS);
ptree::iterator it = allObjects.begin();
it++;
for(; it != allObjects.end(); it++){
parseObject(it->second);
}
return scenarioType;
}
void displayObjects()
{
for (std::vector<object>::const_iterator it = _objectList.begin (); it != _objectList.end (); ++it)
{
std::cout << it->name << std::endl;
}
}
std::string createDir(std::string filename)
{
filename = filename.erase(filename.find_last_of("."), 4);
const int dir_err = mkdir(filename.c_str(), ACCESSPERMS);
if (dir_err == -1)
{
printf("Error creating directory!");
exit(0);
}
return filename;
}
void extractPCD(std::vector<object> objList, std::string folder)
{
for(int i = 0; i < objList.size(); i++)
{
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZRGBA>);
for (std::vector<int>::const_iterator pit = objList[i].indices.indices.begin (); pit != objList[i].indices.indices.end (); ++pit)
//for(int j = 0; j < objList[i].indices.indices.size(); j++)
cloud_cluster->points.push_back (scene->points[*pit]); //*
cloud_cluster->width = cloud_cluster->points.size ();
cloud_cluster->height = 1;
cloud_cluster->is_dense = true;
std::cout << "PointCloud representing the Cluster: " << cloud_cluster->points.size () << " data points." << std::endl;
std::stringstream ss;
ss << folder << "/"<< objList[i].name << ".pcd";
writer.write<pcl::PointXYZRGBA> (ss.str (), *cloud_cluster, false); //*
}
}
int
main (int argc, char *argv[])
{
if ( argc == 3 )
{
if (reader.read (argv[1], *scene) < 0)
{
std::cout << "Error loading scene cloud." << std::endl;
exit (0);
}
std::string fileName(argv[1]);
importObjectsInformation(argv[2]);
displayObjects();
std::string folderName = createDir(fileName);
extractPCD(_objectList, folderName);
}
else
std::cout<<"Usage: " << argv[0] << " <filename>.pcd <filename>.xml" << std::endl;
}