1
+ #include < patchwork/patchworkpp.h>
2
+
3
+ #include < iostream>
4
+ #include < fstream>
5
+ #include < open3d/Open3D.h>
6
+
7
+ // for list folder
8
+ #include < experimental/filesystem>
9
+ namespace fs = std::experimental::filesystem;
10
+
11
+ using namespace open3d ;
12
+
13
+ int filename_length = std::string(" demo_visaulize_copied.cpp" ).length();
14
+ std::string file_dir = std::string(__FILE__);
15
+ std::string data_dir = file_dir.substr(0 , file_dir.size()-filename_length) + " ../../data/" ;
16
+
17
+
18
+ void read_bin (std::string bin_path, Eigen::MatrixXf &cloud)
19
+ {
20
+ FILE *file = fopen (bin_path.c_str (), " rb" );
21
+ if (!file) {
22
+ std::cerr << " error: failed to load " << bin_path << std::endl;
23
+ return ;
24
+ }
25
+
26
+ std::vector<float > buffer (1000000 );
27
+ size_t num_points = fread (reinterpret_cast <char *>(buffer.data ()), sizeof (float ), buffer.size (), file) / 4 ;
28
+
29
+ cloud.resize (num_points, 4 );
30
+ for (int i=0 ; i<num_points; i++)
31
+ {
32
+ cloud.row (i) << buffer[i*4 ], buffer[i*4 +1 ], buffer[i*4 +2 ], buffer[i*4 +3 ];
33
+ }
34
+ }
35
+
36
+ void eigen2geo (Eigen::MatrixXf add, std::shared_ptr<geometry::PointCloud> geo)
37
+ {
38
+ for ( int i=0 ; i<add.rows (); i++ ) {
39
+ geo->points_ .push_back (Eigen::Vector3d (add.row (i)(0 ), add.row (i)(1 ), add.row (i)(2 )));
40
+ }
41
+ }
42
+
43
+ void addNormals (Eigen::MatrixXf normals, std::shared_ptr<geometry::PointCloud> geo)
44
+ {
45
+ for (int i=0 ; i<normals.rows (); i++) {
46
+ geo->normals_ .push_back (Eigen::Vector3d (normals.row (i)(0 ), normals.row (i)(1 ), normals.row (i)(2 )));
47
+ }
48
+ }
49
+
50
+
51
+ int main (int argc, char * argv[]) {
52
+
53
+ cout << " Execute" << __FILE__ << endl;
54
+ // Get the dataset
55
+ std::string input_cloud_filepath;
56
+ if (argc < 2 ) {
57
+ // Try out running on the test datasets.
58
+ input_cloud_filepath = data_dir + " 000000.bin" ;
59
+ std::cout << " \033 [1;33mNo point cloud file path specified; defaulting to the test directory. \033 [0m" << std::endl;
60
+ } else {
61
+ input_cloud_filepath = argv[1 ];
62
+ std::cout << " \033 [1;32mLoading point cloud files from " << input_cloud_filepath << " \033 [0m" << std::endl;
63
+ }
64
+ if (!fs::exists (input_cloud_filepath)){
65
+ std::cout << " \033 [1;31mERROR HERE: maybe wrong data file path, please check the path or remove argv to run default one. \033 [0m"
66
+ << " \n The file path you provide is: " << input_cloud_filepath << std::endl;
67
+ return 0 ;
68
+ }
69
+
70
+ // Patchwork++ initialization
71
+ patchwork::Params patchwork_parameters;
72
+ patchwork_parameters.verbose = true ;
73
+
74
+ patchwork::PatchWorkpp Patchworkpp (patchwork_parameters);
75
+
76
+ // Load point cloud
77
+ Eigen::MatrixXf cloud;
78
+ read_bin (input_cloud_filepath, cloud);
79
+
80
+ // Estimate Ground
81
+ Patchworkpp.estimateGround (cloud);
82
+
83
+ // Get Ground and Nonground
84
+ Eigen::MatrixX3f ground = Patchworkpp.getGround ();
85
+ Eigen::MatrixX3f nonground = Patchworkpp.getNonground ();
86
+ double time_taken = Patchworkpp.getTimeTaken ();
87
+
88
+ Eigen::VectorXi ground_idx = Patchworkpp.getGroundIndices ();
89
+ Eigen::VectorXi nonground_idx = Patchworkpp.getNongroundIndices ();
90
+
91
+ // Get centers and normals for patches
92
+ Eigen::MatrixX3f centers = Patchworkpp.getCenters ();
93
+ Eigen::MatrixX3f normals = Patchworkpp.getNormals ();
94
+
95
+ cout << " Origianl Points #: " << cloud.rows () << endl;
96
+ cout << " Ground Points #: " << ground.rows () << endl;
97
+ cout << " Nonground Points #: " << nonground.rows () << endl;
98
+ cout << " Time Taken : " << time_taken / 1000000 << " (sec)" << endl;
99
+ cout << " Press ... \n " << endl;
100
+ cout << " \t H : help" << endl;
101
+ cout << " \t N : visualize the surface normals" << endl;
102
+ cout << " \t ESC : close the Open3D window" << endl;
103
+
104
+ // Visualize
105
+ std::shared_ptr<geometry::PointCloud> geo_ground (new geometry::PointCloud);
106
+ std::shared_ptr<geometry::PointCloud> geo_nonground (new geometry::PointCloud);
107
+ std::shared_ptr<geometry::PointCloud> geo_centers (new geometry::PointCloud);
108
+
109
+ eigen2geo (ground, geo_ground);
110
+ eigen2geo (nonground, geo_nonground);
111
+ eigen2geo (centers, geo_centers);
112
+ addNormals (normals, geo_centers);
113
+
114
+ geo_ground->PaintUniformColor (Eigen::Vector3d (0.0 , 1.0 , 0.0 ));
115
+ geo_nonground->PaintUniformColor (Eigen::Vector3d (1.0 , 0.0 , 0.0 ));
116
+ geo_centers->PaintUniformColor (Eigen::Vector3d (1.0 , 1.0 , 0.0 ));
117
+
118
+ visualization::Visualizer visualizer;
119
+ visualizer.CreateVisualizerWindow (" Open3D" , 1600 , 900 );
120
+ visualizer.AddGeometry (geo_ground);
121
+ visualizer.AddGeometry (geo_nonground);
122
+ visualizer.AddGeometry (geo_centers);
123
+ visualizer.Run ();
124
+ visualizer.DestroyVisualizerWindow ();
125
+ }
0 commit comments