hdf5_map_io.cpp
Go to the documentation of this file.
2 #include <hdf5_hl.h>
3 #include <unistd.h>
4 
5 namespace hdf5_map_io
6 {
7 
9 {
12  else
14 
17  else
19 
22  else
24 
27  else
29 }
30 
31 HDF5MapIO::HDF5MapIO(std::string filename)
32  : m_file(filename, hf::File::ReadWrite)
33 {
35 }
36 
38  std::string filename,
39  const std::vector<float>& vertices,
40  const std::vector<uint32_t>& face_ids
41 )
42  : m_file(filename, hf::File::ReadWrite | hf::File::Create | hf::File::Truncate)
43 {
44 
45  if (!m_file.isValid())
46  throw "Could not open file.";
47 
49 
50  // Create geometry data sets
52  .createDataSet<float>("vertices", hf::DataSpace::From(vertices))
53  .write(vertices);
55  .createDataSet<uint32_t>("face_indices", hf::DataSpace::From(face_ids))
56  .write(face_ids);
57 }
58 
60 {
61  if (!m_file.isValid())
62  {
63  // do nothing if file is not valid, i.e. already closed
64  return;
65  }
66 
67  H5Gclose(m_channelsGroup.getId());
68  H5Gclose(m_clusterSetsGroup.getId());
69  H5Gclose(m_texturesGroup.getId());
70  H5Gclose(m_labelsGroup.getId());
71  H5Fclose(m_file.getId());
72 }
73 
75 {
76  auto dimensions = data_set.getSpace().getDimensions();
77  return dimensions[0] * dimensions[1];
78 }
79 
80 std::vector<float> HDF5MapIO::getVertices()
81 {
82  std::vector<float> vertices;
83  if (!m_channelsGroup.exist("vertices"))
84  return vertices;
85  auto dataset = m_channelsGroup.getDataSet("vertices");
86  vertices.resize(getSize(dataset));
87  dataset.read(vertices.data());
88  return vertices;
89 }
90 
91 std::vector<uint32_t> HDF5MapIO::getFaceIds()
92 {
93  std::vector<uint32_t> indices;
94  if (!m_channelsGroup.exist("face_indices"))
95  return indices;
96  auto dataset = m_channelsGroup.getDataSet("face_indices");
97  indices.resize(getSize(dataset));
98  dataset.read(indices.data());
99  return indices;
100 }
101 
102 std::vector<float> HDF5MapIO::getVertexNormals()
103 {
104  std::vector<float> normals;
105  if (!m_channelsGroup.exist("vertex_normals"))
106  return normals;
107  auto dataset = m_channelsGroup.getDataSet("vertex_normals");
108  normals.resize(getSize(dataset));
109  dataset.read(normals.data());
110  return normals;
111 }
112 
113 std::vector<uint8_t> HDF5MapIO::getVertexColors()
114 {
115  std::vector<uint8_t> colors;
116  if (!m_channelsGroup.exist("vertex_colors"))
117  return colors;
118 
119  auto dataset = m_channelsGroup.getDataSet("vertex_colors");
120  colors.resize(getSize(dataset));
121  dataset.read(colors.data());
122  return colors;
123 }
124 
125 std::vector<MapImage> HDF5MapIO::getTextures()
126 {
127  std::vector<MapImage> textures;
128  if (!m_texturesGroup.exist("images"))
129  {
130  return textures;
131  }
132 
133  const hf::Group& imagesGroup = m_texturesGroup.getGroup("images");
134  for (auto setName: imagesGroup.listObjectNames())
135  {
136  textures.push_back(getImage(imagesGroup, setName));
137  }
138 
139  return textures;
140 }
141 
142 std::unordered_map<MapVertex, std::vector<float>> HDF5MapIO::getFeatures()
143 {
144  std::unordered_map<MapVertex, std::vector<float>> features;
145 
146  if (!m_channelsGroup.exist("texture_features"))
147  {
148  return features;
149  }
150 
151  const auto& featuresGroup = m_channelsGroup.getGroup("texture_features");
152  features.reserve(featuresGroup.getNumberObjects());
153 
154  for (auto name : featuresGroup.listObjectNames())
155  {
156  // fill vector with descriptor
157  std::vector<float> descriptor;
158  auto dataset = featuresGroup.getDataSet(name);
159  dataset.read(descriptor);
160 
161  // read vector attribute with xyz coords
162  MapVertex v;
163  std::vector<float> xyz(3);
164  auto vector_attr = dataset.getAttribute("vector");
165  vector_attr.read(xyz);
166 
167  v.x = xyz[0];
168  v.y = xyz[1];
169  v.z = xyz[2];
170 
171  features.insert({v, descriptor});
172  }
173 
174  return features;
175 }
176 
177 std::vector<MapMaterial> HDF5MapIO::getMaterials()
178 {
179  std::vector<MapMaterial> materials;
180 
181  if (!m_texturesGroup.exist("materials"))
182  {
183  return materials;
184  }
185 
186  m_texturesGroup.getDataSet("materials")
187  .read(materials);
188 
189  return materials;
190 }
191 
192 std::vector<uint32_t> HDF5MapIO::getMaterialFaceIndices()
193 {
194  std::vector<uint32_t> matFaceIndices;
195 
196  if (!m_texturesGroup.exist("mat_face_indices"))
197  {
198  return matFaceIndices;
199  }
200 
201  m_texturesGroup.getDataSet("mat_face_indices")
202  .read(matFaceIndices);
203 
204  return matFaceIndices;
205 }
206 
208 {
209  std::vector<float> coords;
210 
211  if (!m_texturesGroup.exist("coords"))
212  {
213  return coords;
214  }
215 
216  m_texturesGroup.getDataSet("coords")
217  .read(coords);
218 
219  return coords;
220 }
221 
222 std::vector<std::string> HDF5MapIO::getLabelGroups()
223 {
225 }
226 
227 std::vector<std::string> HDF5MapIO::getAllLabelsOfGroup(std::string groupName)
228 {
229  if (!m_labelsGroup.exist(groupName))
230  {
231  return std::vector<std::string>();
232  }
233 
234  return m_labelsGroup.getGroup(groupName).listObjectNames();
235 }
236 
237 std::vector<uint32_t> HDF5MapIO::getFaceIdsOfLabel(std::string groupName, std::string labelName)
238 {
239  std::vector<uint32_t> faceIds;
240 
241  if (!m_labelsGroup.exist(groupName))
242  {
243  return faceIds;
244  }
245 
246  auto lg = m_labelsGroup.getGroup(groupName);
247 
248  if (!lg.exist(labelName))
249  {
250  return faceIds;
251  }
252 
253  lg.getDataSet(labelName).read(faceIds);
254 
255  return faceIds;
256 }
257 
258 std::vector<float> HDF5MapIO::getRoughness()
259 {
260  return getVertexCosts("roughness");
261 }
262 
263 std::vector<float> HDF5MapIO::getHeightDifference()
264 {
265  return getVertexCosts("height_diff");
266 }
267 
268 std::vector<float> HDF5MapIO::getVertexCosts(std::string costlayer)
269 {
270  std::vector<float> costs;
271 
272  if (!m_channelsGroup.exist(costlayer))
273  {
274  return costs;
275  }
276 
277  m_channelsGroup.getDataSet(costlayer)
278  .read(costs);
279 
280  return costs;
281 }
282 
283 std::vector<std::string> HDF5MapIO::getCostLayers()
284 {
286 }
287 
288 MapImage HDF5MapIO::getImage(hf::Group group, std::string name)
289 {
290  MapImage t;
291 
292  if (!group.exist(name))
293  {
294  return t;
295  }
296 
297  hsize_t width;
298  hsize_t height;
299  hsize_t pixel_size;
300  char interlace[20];
301  hssize_t npals;
302 
303  H5IMget_image_info(group.getId(), name.c_str(), &width, &height, &pixel_size, interlace, &npals);
304 
305  auto bufSize = width * height * pixel_size;
306  std::vector<unsigned char> buf;
307  buf.resize(bufSize);
308  H5IMread_image(group.getId(), name.c_str(), buf.data());
309 
310  t.name = name;
311  t.width = width;
312  t.height = height;
313  t.channels = pixel_size;
314  t.data = buf;
315 
316  return t;
317 }
318 
319 hf::DataSet HDF5MapIO::addVertexNormals(std::vector<float>& normals)
320 {
321  // TODO make more versatile to add and/or overwrite normals in file
322  auto dataSet = m_channelsGroup.createDataSet<float>("vertex_normals", hf::DataSpace::From(normals));
323  dataSet.write(normals);
324 
325  return dataSet;
326 }
327 
328 hf::DataSet HDF5MapIO::addVertexColors(std::vector<uint8_t>& colors)
329 {
330  auto dataSet = m_channelsGroup.createDataSet<uint8_t>("vertex_colors", hf::DataSpace::From(colors));
331  dataSet.write(colors);
332 
333  return dataSet;
334 }
335 
336 void HDF5MapIO::addTexture(int index, uint32_t width, uint32_t height, uint8_t *data)
337 {
338  if (!m_texturesGroup.exist("images"))
339  {
340  m_texturesGroup.createGroup("images");
341  }
342 
343  auto imagesGroup = m_texturesGroup.getGroup("images");
344  const std::string& name = std::to_string(index);
345 
346  if (imagesGroup.exist(name))
347  {
348  return;
349  }
350 
351  addImage(imagesGroup, name, width, height, data);
352 }
353 
354 void HDF5MapIO::addMaterials(std::vector<MapMaterial>& materials, std::vector<uint32_t>& matFaceIndices)
355 {
357  .createDataSet<MapMaterial>("materials", hf::DataSpace::From(materials))
358  .write(materials);
359 
361  .createDataSet<uint32_t>("mat_face_indices", hf::DataSpace::From(matFaceIndices))
362  .write(matFaceIndices);
363 }
364 
365 void HDF5MapIO::addVertexTextureCoords(std::vector<float>& coords)
366 {
368  .createDataSet<float>("coords", hf::DataSpace::From(coords))
369  .write(coords);
370 }
371 
372 void HDF5MapIO::addOrUpdateLabel(std::string groupName, std::string labelName, std::vector<uint32_t>& faceIds)
373 {
374  std::cout << "Add or update label" << std::endl;
375  if (!m_labelsGroup.exist(groupName))
376  {
377  m_labelsGroup.createGroup(groupName);
378  }
379 
380  auto group = m_labelsGroup.getGroup(groupName);
381  if(group.exist(labelName))
382  {
383  std::cout << "write to existing label" << std::endl;
384  auto dataset = group.getDataSet(labelName);
385  dataset.write(faceIds);
386  }
387  else
388  {
389  std::cout << "write to new label" << std::endl;
390  auto dataset = group.createDataSet<uint32_t>(labelName, hf::DataSpace::From(faceIds));
391  dataset.write(faceIds);
392  }
393 }
394 
395 void HDF5MapIO::addLabel(std::string groupName, std::string labelName, std::vector<uint32_t>& faceIds)
396 {
397  if (!m_labelsGroup.exist(groupName))
398  {
399  m_labelsGroup.createGroup(groupName);
400  }
401 
402  m_labelsGroup.getGroup(groupName)
403  .createDataSet<uint32_t>(labelName, hf::DataSpace::From(faceIds))
404  .write(faceIds);
405 }
406 
407 void HDF5MapIO::addTextureKeypointsMap(std::unordered_map<MapVertex, std::vector<float>>& keypoints_map)
408 {
409  if (!m_channelsGroup.exist("texture_features"))
410  {
411  m_channelsGroup.createGroup("texture_features");
412  }
413 
414  auto tf = m_channelsGroup.getGroup("texture_features");
415 
416  size_t i = 0;
417  for (const auto& keypoint_features : keypoints_map)
418  {
419  auto dataset = tf.createDataSet<float>(std::to_string(i), hf::DataSpace::From(keypoint_features.second));
420  dataset.write(keypoint_features.second);
421 
422  std::vector<float> v = {keypoint_features.first.x, keypoint_features.first.y, keypoint_features.first.z};
423  dataset.template createAttribute<float>("vector", hf::DataSpace::From(v))
424  .write(v);
425 
426  i++;
427  }
428 }
429 
430 void HDF5MapIO::addRoughness(std::vector<float>& roughness)
431 {
432  m_channelsGroup.createDataSet<float>("roughness", hf::DataSpace::From(roughness))
433  .write(roughness);
434 }
435 
436 void HDF5MapIO::addHeightDifference(std::vector<float>& diff)
437 {
438  m_channelsGroup.createDataSet<float>("height_diff", hf::DataSpace::From(diff))
439  .write(diff);
440 }
441 
442 void HDF5MapIO::addImage(hf::Group group, std::string name, const uint32_t width, const uint32_t height,
443  const uint8_t *pixelBuffer)
444 {
445  H5IMmake_image_24bit(group.getId(), name.c_str(), width, height, "INTERLACE_PIXEL", pixelBuffer);
446 }
447 
449 {
450  bool result = true;
451  for (std::string name : m_labelsGroup.listObjectNames())
452  {
453  std::string fullPath = std::string(LABELS_GROUP) + "/" + name;
454  result = H5Ldelete(m_file.getId(), fullPath.data(), H5P_DEFAULT) > 0;
455  }
456 
457  return result;
458 }
459 
461 {
462  m_file.flush();
463 }
464 
465 } // namespace hdf5_map_io
466 
hdf5_map_io::HDF5MapIO::getMaterials
std::vector< MapMaterial > getMaterials()
Returns materials as MapMaterial.
Definition: hdf5_map_io.cpp:177
hdf5_map_io::HDF5MapIO::getLabelGroups
std::vector< std::string > getLabelGroups()
Returns all available label groups.
Definition: hdf5_map_io.cpp:222
hdf5_map_io::HDF5MapIO::getMaterialFaceIndices
std::vector< uint32_t > getMaterialFaceIndices()
Returns material <-> face indices.
Definition: hdf5_map_io.cpp:192
hdf5_map_io::HDF5MapIO::getHeightDifference
std::vector< float > getHeightDifference()
Returns the height difference as float vector.
Definition: hdf5_map_io.cpp:263
hdf5_map_io::HDF5MapIO::m_file
hf::File m_file
Definition: hdf5_map_io.h:247
hdf5_map_io::HDF5MapIO::removeAllLabels
bool removeAllLabels()
Definition: hdf5_map_io.cpp:448
hdf5_map_io::HDF5MapIO::getImage
MapImage getImage(hf::Group group, std::string name)
Returns the image in the group, if it exists. If not an empty struct is returned.
Definition: hdf5_map_io.cpp:288
hdf5_map_io::HDF5MapIO::HDF5MapIO
HDF5MapIO(std::string filename)
Opens a map file for reading and writing.
Definition: hdf5_map_io.cpp:31
hdf5_map_io::MapVertex::z
float z
Definition: hdf5_map_io.h:23
hdf5_map_io::HDF5MapIO::~HDF5MapIO
~HDF5MapIO()
Closes main groups and makes sure all buffers are flushed to the file on disc.
Definition: hdf5_map_io.cpp:59
hdf5_map_io::HDF5MapIO::getFaceIds
std::vector< uint32_t > getFaceIds()
Returns face ids vector.
Definition: hdf5_map_io.cpp:91
NodeTraits< File >::createGroup
Group createGroup(const std::string &group_name)
hdf5_map_io::HDF5MapIO::getAllLabelsOfGroup
std::vector< std::string > getAllLabelsOfGroup(std::string groupName)
Returns all labels inside the given group.
Definition: hdf5_map_io.cpp:227
HighFive::DataSpace::getDimensions
std::vector< size_t > getDimensions() const
hdf5_map_io::MapVertex::x
float x
Definition: hdf5_map_io.h:21
hdf5_map_io::HDF5MapIO::addHeightDifference
void addHeightDifference(std::vector< float > &diff)
Adds the height difference to the attributes group.
Definition: hdf5_map_io.cpp:436
NodeTraits< Group >::getDataSet
DataSet getDataSet(const std::string &dataset_name, const DataSetAccessProps &accessProps=DataSetAccessProps()) const
hdf5_map_io::HDF5MapIO::addRoughness
void addRoughness(std::vector< float > &roughness)
Adds the roughness to the attributes group.
Definition: hdf5_map_io.cpp:430
hdf5_map_io::HDF5MapIO::getFaceIdsOfLabel
std::vector< uint32_t > getFaceIdsOfLabel(std::string groupName, std::string labelName)
Returns face ids for the given label inside the group. E.g: label=tree_1 -> groupName=tree; labelName...
Definition: hdf5_map_io.cpp:237
hdf5_map_io::MapVertex
Definition: hdf5_map_io.h:20
hdf5_map_io::HDF5MapIO::getFeatures
std::unordered_map< MapVertex, std::vector< float > > getFeatures()
Returns an map which keys are representing the features point in space and the values are an vector o...
Definition: hdf5_map_io.cpp:142
HighFive::Object::getId
hid_t getId() const
hdf5_map_io::HDF5MapIO::getCostLayers
std::vector< std::string > getCostLayers()
returns the names of all available costlayers
Definition: hdf5_map_io.cpp:283
hdf5_map_io::HDF5MapIO::m_labelsGroup
hf::Group m_labelsGroup
Definition: hdf5_map_io.h:262
hdf5_map_io::MapImage::name
std::string name
Definition: hdf5_map_io.h:31
hdf5_map_io::HDF5MapIO::getRoughness
std::vector< float > getRoughness()
Returns the roughness as float vector.
Definition: hdf5_map_io.cpp:258
HighFive::DataSpace::From
static DataSpace From(const ScalarValue &scalar_value)
hdf5_map_io::HDF5MapIO::addTexture
void addTexture(int index, uint32_t width, uint32_t height, uint8_t *data)
Definition: hdf5_map_io.cpp:336
hdf5_map_io::HDF5MapIO::getVertexCosts
std::vector< float > getVertexCosts(std::string costlayer)
Returns one costlayer as float vector.
Definition: hdf5_map_io.cpp:268
hdf5_map_io::HDF5MapIO::addVertexTextureCoords
void addVertexTextureCoords(std::vector< float > &coords)
Add vertex texture coordinates to the textures group.
Definition: hdf5_map_io.cpp:365
hdf5_map_io::HDF5MapIO::CHANNELS_GROUP
static constexpr const char * CHANNELS_GROUP
Definition: hdf5_map_io.h:253
hdf5_map_io::HDF5MapIO::getVertexNormals
std::vector< float > getVertexNormals()
Returns vertex normals vector.
Definition: hdf5_map_io.cpp:102
hdf5_map_io.h
HighFive::DataSet::getSpace
DataSpace getSpace() const
hdf5_map_io::HDF5MapIO::getVertexTextureCoords
std::vector< float > getVertexTextureCoords()
Returns vertex texture coordinates.
Definition: hdf5_map_io.cpp:207
hdf5_map_io::HDF5MapIO::addTextureKeypointsMap
void addTextureKeypointsMap(std::unordered_map< MapVertex, std::vector< float >> &keypoints_map)
Adds the keypoints with their corresponding positions to the attributes_group. The position is saved ...
Definition: hdf5_map_io.cpp:407
hdf5_map_io::HDF5MapIO::LABELS_GROUP
static constexpr const char * LABELS_GROUP
Definition: hdf5_map_io.h:256
hdf5_map_io::HDF5MapIO::getTextures
std::vector< MapImage > getTextures()
Returns textures vector.
Definition: hdf5_map_io.cpp:125
HighFive::Group
filename
filename
hdf5_map_io::HDF5MapIO::TEXTURES_GROUP
static constexpr const char * TEXTURES_GROUP
Definition: hdf5_map_io.h:255
NodeTraits< Group >::createDataSet
DataSet createDataSet(const std::string &dataset_name, const DataSpace &space, const DataSetCreateProps &createProps=DataSetCreateProps(), const DataSetAccessProps &accessProps=DataSetAccessProps())
hdf5_map_io::HDF5MapIO::addVertexNormals
hf::DataSet addVertexNormals(std::vector< float > &normals)
Add normals to the attributes group.
Definition: hdf5_map_io.cpp:319
hdf5_map_io::HDF5MapIO::addImage
void addImage(hf::Group group, std::string name, const uint32_t width, const uint32_t height, const uint8_t *pixelBuffer)
Adds an image with given data set name to the given group.
Definition: hdf5_map_io.cpp:442
NodeTraits< File >::getGroup
Group getGroup(const std::string &group_name) const
hdf5_map_io::MapImage::channels
uint32_t channels
Definition: hdf5_map_io.h:34
hdf5_map_io::HDF5MapIO::addMaterials
void addMaterials(std::vector< MapMaterial > &materials, std::vector< uint32_t > &matFaceIndices)
Add materials as MapMaterial and the corresponding material <-> face indices.
Definition: hdf5_map_io.cpp:354
colors
colors
hdf5_map_io::MapImage
Definition: hdf5_map_io.h:30
hdf5_map_io::HDF5MapIO::m_channelsGroup
hf::Group m_channelsGroup
Definition: hdf5_map_io.h:259
hdf5_map_io::HDF5MapIO::getVertexColors
std::vector< uint8_t > getVertexColors()
Returns vertex colors vector.
Definition: hdf5_map_io.cpp:113
hdf5_map_io::HDF5MapIO::CLUSTERSETS_GROUP
static constexpr const char * CLUSTERSETS_GROUP
Definition: hdf5_map_io.h:254
hdf5_map_io::HDF5MapIO::flush
void flush()
Flushes the file. All opened buffers are saved to disc.
Definition: hdf5_map_io.cpp:460
HighFive::Object::isValid
bool isValid() const
hdf5_map_io::HDF5MapIO::getVertices
std::vector< float > getVertices()
Returns vertices vector.
Definition: hdf5_map_io.cpp:80
hdf5_map_io::MapImage::height
uint32_t height
Definition: hdf5_map_io.h:33
hdf5_map_io::MapImage::width
uint32_t width
Definition: hdf5_map_io.h:32
hdf5_map_io::HDF5MapIO::addLabel
void addLabel(std::string groupName, std::string labelName, std::vector< uint32_t > &faceIds)
Adds the label (labelName) to the label group with the given faces. E.g.: tree_1 -> groupName=tree; l...
Definition: hdf5_map_io.cpp:395
HighFive::File::flush
void flush()
NodeTraits< File >::exist
bool exist(const std::string &node_name) const
hdf5_map_io::HDF5MapIO::m_texturesGroup
hf::Group m_texturesGroup
Definition: hdf5_map_io.h:261
hdf5_map_io::HDF5MapIO::getSize
size_t getSize(hf::DataSet &data_set)
Definition: hdf5_map_io.cpp:74
hdf5_map_io::HDF5MapIO::creatOrGetGroups
void creatOrGetGroups()
Definition: hdf5_map_io.cpp:8
hdf5_map_io
Definition: hdf5_map_io.h:14
hdf5_map_io::HDF5MapIO::m_clusterSetsGroup
hf::Group m_clusterSetsGroup
Definition: hdf5_map_io.h:260
hdf5_map_io::MapMaterial
Definition: hdf5_map_io.h:43
HighFive::DataSet
HighFive
hdf5_map_io::HDF5MapIO::addOrUpdateLabel
void addOrUpdateLabel(std::string groupName, std::string labelName, std::vector< uint32_t > &faceIds)
Adds or updates the label (labelName) to the label group with the given faces. E.g....
Definition: hdf5_map_io.cpp:372
hdf5_map_io::MapImage::data
std::vector< uint8_t > data
Definition: hdf5_map_io.h:35
hdf5_map_io::MapVertex::y
float y
Definition: hdf5_map_io.h:22
NodeTraits< Group >::listObjectNames
std::vector< std::string > listObjectNames() const
hdf5_map_io::HDF5MapIO::addVertexColors
hf::DataSet addVertexColors(std::vector< uint8_t > &colors)
Add vertex colors to the attributes group.
Definition: hdf5_map_io.cpp:328


hdf5_map_io
Author(s): Sebastian Pütz
autogenerated on Sun Jan 21 2024 04:06:12