examples/hdf5features/Main.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <memory>
3 #include <tuple>
4 #include <stdlib.h>
5 #include <type_traits>
6 #include <utility>
7 
8 #include <boost/optional.hpp>
9 #include <boost/preprocessor/stringize.hpp>
10 #include <opencv2/core.hpp>
11 #include <opencv2/imgproc.hpp>
12 #include <opencv2/highgui/highgui.hpp>
13 
14 // lvr2 includes
17 
19 #include "lvr2/io/hdf5/ArrayIO.hpp"
22 #include "lvr2/io/hdf5/MeshIO.hpp"
25 #include "lvr2/io/hdf5/ImageIO.hpp"
26 
27 
30 
31 cv::Mat generate_image()
32 {
33  cv::Mat img(500, 500, CV_8UC3, cv::Scalar(239,234,224));
34 
35  // outter lines
36  cv::line(img, cv::Point(250, 50), cv::Point(450, 450), cv::Scalar(0, 0, 0), 3, CV_AA );
37  cv::line(img, cv::Point(250, 50), cv::Point(50, 450), cv::Scalar(0, 0, 0), 3, CV_AA );
38  cv::line(img, cv::Point(50, 450), cv::Point(450, 450), cv::Scalar(0, 0, 0), 3, CV_AA );
39 
40  // inner lines
41  cv::line(img, cv::Point(150, 250), cv::Point(350, 250), cv::Scalar(0, 0, 0), 3, CV_AA );
42  cv::line(img, cv::Point(150, 250), cv::Point(250, 450), cv::Scalar(0, 0, 0), 3, CV_AA );
43  cv::line(img, cv::Point(350, 250), cv::Point(250, 450), cv::Scalar(0, 0, 0), 3, CV_AA );
44 
45  return img;
46 }
47 
49 {
50  // Empty IO
51  using BaseHDF5IO = lvr2::Hdf5IO<>;
52 
53  // Extend IO with features (dependencies are automatically fetched)
54  using MyHDF5IO = BaseHDF5IO::AddFeatures<
57  lvr2::hdf5features::PointCloudIO // duplicate test
58  >;
59 
60  // Fast construction
62 
63  // Merge two ios
64  using MergedIO = MyHDF5IO::Merge<MyHDF5IOTest>;
65 
66  // Check if a feature exists in IO Type
67  if(MyHDF5IO::HasFeature<lvr2::hdf5features::PointCloudIO>)
68  {
69  std::cout << "MyHDF5IO has the feature lvr2::hdf5features::PointCloudIO" << std::endl;
70  } else {
71  std::cout << "MyHDF5IO doesnt have feature lvr2::hdf5features::PointCloudIO" << std::endl;
72  }
73 
74  // test duplicate feature
75  using Duplicate = MyHDF5IO::add_feature<lvr2::hdf5features::PointCloudIO>::type;
76  lvr2::Channel<double> channel(1000, 3);
78  pointcloud->add("points", channel);
79  Duplicate io;
80  io.open("gen_test.h5");
81  io.save("apointcloud",pointcloud);
82 
83  if(!io.has<lvr2::hdf5features::ImageIO>())
84  {
85  std::cout << "has feature check on object success" << std::endl;
86  }
87 
88  // cast
89  auto pcl_io = io.scast<lvr2::hdf5features::PointCloudIO>();
90 
91  pcl_io->save("bpointcloud", pointcloud);
92 
93  lvr2::PointBufferPtr bla = pcl_io->load("bpointcloud");
94 
95  auto image_io = io.dcast<lvr2::hdf5features::ImageIO>();
96 
97  if(!image_io)
98  {
99  std::cout << "wrong dynamic cast success" << std::endl;
100  }
101 
102  auto dyn_pcl_io = io.dcast<lvr2::hdf5features::PointCloudIO>();
103  if(dyn_pcl_io)
104  {
105  std::cout << "correct dynamic cast success" << std::endl;
106  dyn_pcl_io->save("cpointcloud", pointcloud);
107  }
108 }
109 
111 {
113  // Create Some Data //
115  size_t num_data = 10000;
116  boost::shared_array<float> data(new float[num_data]);
117  lvr2::Channel<double> channel(1000, 3);
118 
120  // Create an IO //
122 
123  using MyHDF5IO = lvr2::Hdf5IO<
131  >;
132 
133  // NEW: Short initialization respecting all dependencies
135 
136  MyHDF5IO my_io;
137  my_io.open("test.h5");
138 
140  // 1) ArrayIO Example //
142  my_io.save("agroup", "anarray", num_data, data);
143  size_t N;
144  boost::shared_array<float> data_loaded = my_io.ArrayIO::load<float>("agroup", "anarray", N);
145 
146  if(N == num_data)
147  {
148  std::cout << "ArrayIO successful" << std::endl;
149  }
150 
152  // 2) ChannelIO Example //
154  my_io.save("agroup", "achannel", channel);
155  lvr2::ChannelOptional<float> channel_loaded = my_io.ChannelIO::load<float>("agroup", "achannel");
156 
157  // alternative:
158  // channel_loaded = my_io.loadChannel<float>("agroup","achannel");
159 
160  if(channel_loaded)
161  {
162  if(channel_loaded->numElements() == channel.numElements() && channel_loaded->width() == channel.width())
163  {
164  std::cout << "ChannelIO successful" << std::endl;
165  }
166  } else {
167  std::cout << "channel not found" << std::endl;
168  }
169 
171  // 3) PointCloudIO Example //
173  lvr2::PointBufferPtr pointcloud(new lvr2::PointBuffer);
174  pointcloud->add("points", channel);
175  my_io.save("apointcloud", pointcloud);
176  lvr2::PointBufferPtr pointcloud_loaded = my_io.loadPointCloud("apointcloud");
177  if(pointcloud_loaded)
178  {
179  std::cout << "PointCloudIO read success" << std::endl;
180  std::cout << *pointcloud_loaded << std::endl;
181  } else {
182  std::cout << "PointCloudIO read failed" << std::endl;
183  }
184 
186  // 4) VariantChannelIO Example //
188 
189  using VChannel = lvr2::PointBuffer::val_type;
190 
191  auto ovchannel
192  = my_io.loadVariantChannel<VChannel>("apointcloud","points");
193 
194  if(ovchannel) {
195  VChannel vchannel = *ovchannel;
196  std::cout << "succesfully read VariantChannel: " << vchannel << std::endl;
197  } else {
198  std::cout << "could not load point from group apointcloud" << std::endl;
199  }
200 
201  my_io.save("apointcloud", "points2", *ovchannel);
202 
204  // 5) Matrix IO //
206 
207  lvr2::Transformd T = lvr2::Transformd::Identity();
208 
209  std::cout << "saving matrix:" << std::endl;
210  std::cout << T << std::endl;
211 
212  my_io.save("matrices", "amatrix", T);
213 
214  auto T2 = my_io.MatrixIO::template load<lvr2::Transformd>("matrices", "amatrix");
215  if(T2)
216  {
217  std::cout << "succesfully loaded Matrix:" << std::endl;
218  std::cout << *T2 << std::endl;
219  } else {
220  std::cout << "could not load matrix!" << std::endl;
221  }
222 
224  // 5) Image IO //
226 
227 
228 
229 
230 
231 
232  // FALLBACK
233  cv::Mat a = (cv::Mat_<float>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
234  my_io.save("images", "fallback", a);
235  boost::optional<cv::Mat> mat_loaded = my_io.ImageIO::load("images","fallback");
236 
237  if(mat_loaded)
238  {
239  cv::Mat fb_mat = *mat_loaded;
240  std::cout << a << std::endl;
241  std::cout << fb_mat << std::endl;
242  } else {
243  std::cout << "fallback doesnt work" << std::endl;
244  }
245 
246  cv::Mat b = cv::Mat::zeros(50, 50, CV_16SC4);
247  my_io.save("images", "fallback2", b);
248 
249  cv::Mat c = *my_io.loadImage("images","fallback2");
250 
251  if(b.type() == c.type())
252  {
253  std::cout << "fallback success" << std::endl;
254  }
255 
256  // RGB
257  cv::Mat image = generate_image();
258 
259  my_io.save("images", "image", image);
260  boost::optional<cv::Mat> image_loaded = my_io.ImageIO::load("images","image");
261 
262  if(image_loaded)
263  {
264  cv::imshow("loaded image", *image_loaded);
265  cv::waitKey(0);
266  } else {
267  std::cout << "could not load image from hdf5 file." << std::endl;
268  }
269 
270  // GRAYSCALE
271  cv::Mat image_gray;
272  cv::cvtColor(image, image_gray, cv::COLOR_BGR2GRAY);
273  my_io.save("images", "image_gray", image_gray);
274  boost::optional<cv::Mat> image_gray_loaded = my_io.ImageIO::load("images","image_gray");
275 
276 
278  // 6) MeshIO Example //
280 
281  // gen io with dependencies
283 
284  std::cout << "N: " << MeshIO::N << std::endl;
285 
286  MeshIO mesh_io;
287  mesh_io.open("test.h5");
288 
290  mesh->setVertices(lvr2::floatArr(
291  new float[18] {2, 0, 0, 1, 2, 0, 3, 2, 0, 0, 4, 0, 2, 4, 0, 4, 4, 0}
292  ), 6);
293  mesh->setFaceIndices(lvr2::indexArray(
294  new unsigned int[12] {0, 1, 2, 1, 3, 4, 2, 4, 5, 1, 4, 2}
295  ), 4);
296  mesh_io.save("multimesh/amesh", mesh);
297  lvr2::MeshBufferPtr mesh_loaded = mesh_io.loadMesh("multimesh/amesh");
298  if(mesh_loaded)
299  {
300  std::cout << "MeshIO read success" << std::endl;
301  std::cout << *mesh_loaded << std::endl;
302  } else {
303  std::cout << "MeshIO read failed" << std::endl;
304  }
305 }
306 
308 
309  // printing number of features in the io object
310 
311  // number of features: 0
312  using BaseIO = lvr2::Hdf5IO<>;
313  std::cout << BaseIO::N << std::endl;
314 
315  // MeshIO + Dependency tree -> 4
316  using MeshIO = BaseIO::AddFeatures<lvr2::hdf5features::MeshIO>;
317  std::cout << MeshIO::N << std::endl;
318 
319  // No duplicates, ImageIO with no dependencies -> 5
320  using MeshIO2 = MeshIO::AddFeatures<lvr2::hdf5features::MeshIO, lvr2::hdf5features::ImageIO>;
321  std::cout << MeshIO2::N << std::endl;
322 }
323 
324 
325 int main(int argc, char** argv)
326 {
329  update_example();
330  return 0;
331 }
VariantChannel.hpp
lvr2::floatArr
boost::shared_array< float > floatArr
Definition: DataStruct.hpp:133
lvr2::hdf5features::ImageIO
Definition: hdf5/ImageIO.hpp:18
lvr2::Hdf5IO
Manager Class for all Hdf5IO components located in hdf5 directory.
Definition: HDF5FeatureBase.hpp:38
PointCloudIO.hpp
main
int main(int argc, char **argv)
Definition: examples/hdf5features/Main.cpp:325
lvr2::indexArray
boost::shared_array< unsigned int > indexArray
Definition: DataStruct.hpp:128
MatrixIO.hpp
MeshIO.hpp
lvr2::Transformd
Transform< double > Transformd
4x4 double precision transformation matrix
Definition: MatrixTypes.hpp:71
lvr2::PointBufferPtr
std::shared_ptr< PointBuffer > PointBufferPtr
Definition: PointBuffer.hpp:130
lvr2::PointBuffer
A class to handle point information with an arbitrarily large number of attribute channels....
Definition: PointBuffer.hpp:51
generate_image
cv::Mat generate_image()
Definition: examples/hdf5features/Main.cpp:31
hdf5io_gen_example
void hdf5io_gen_example()
Definition: examples/hdf5features/Main.cpp:48
VariantChannelIO.hpp
lvr2::hdf5features::PointCloudIO::save
void save(std::string name, const PointBufferPtr &buffer)
lvr2::MeshBuffer
The MeshBuffer Mesh representation for I/O modules.
Definition: MeshBuffer.hpp:41
lvr2::ChannelOptional
typename Channel< T >::Optional ChannelOptional
Definition: Channel.hpp:85
lvr2::hdf5features::VariantChannelIO
Hdf5IO Feature for handling VariantChannel related IO.
Definition: hdf5/VariantChannelIO.hpp:59
ImageIO.hpp
MatrixTypes.hpp
kfusion::device::Point
float4 Point
Definition: internal.hpp:14
lvr2::hdf5features::PointCloudIO
Hdf5IO Feature for handling PointBuffer related IO.
Definition: hdf5/PointCloudIO.hpp:47
ChannelIO.hpp
HDF5FeatureBase.hpp
argc
int argc
Definition: tests_high_five_parallel.cpp:27
lvr2::Channel
Definition: Channel.hpp:42
hdf5io_usage_example
void hdf5io_usage_example()
Definition: examples/hdf5features/Main.cpp:110
lvr2::Hdf5IO::AddFeatures
typename add_features_with_deps< F... >::type AddFeatures
Definition: HDF5FeatureBase.hpp:120
lvr2::MeshBufferPtr
std::shared_ptr< MeshBuffer > MeshBufferPtr
Definition: MeshBuffer.hpp:217
update_example
void update_example()
Definition: examples/hdf5features/Main.cpp:307
lvr2::Hdf5Build
typename Hdf5Construct< Feature >::type Hdf5Build
Definition: HDF5FeatureBase.hpp:169
lvr2::Channel::width
size_t width() const
lvr2::hdf5features::ImageIO::save
void save(std::string groupName, std::string datasetName, const cv::Mat &img)
lvr2::VariantChannelMap::val_type
VariantChannel< T... > val_type
Definition: VariantChannelMap.hpp:48
mesh
HalfEdgeMesh< Vec > mesh
Definition: src/tools/lvr2_gs_reconstruction/Main.cpp:26
lvr2::hdf5features::ArrayIO
Definition: hdf5/ArrayIO.hpp:13
argv
char ** argv
Definition: tests_high_five_parallel.cpp:28
ArrayIO.hpp
lvr2::hdf5features::ChannelIO
Definition: hdf5/ChannelIO.hpp:20
lvr2::hdf5features::MatrixIO
Definition: hdf5/MatrixIO.hpp:16
lvr2::hdf5features::MeshIO
Hdf5IO Feature for handling MeshBuffer related IO.
Definition: MeshIO.hpp:52
TransformUtils.hpp
lvr2::Channel::numElements
size_t numElements() const


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:24