DrcIO.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2018 Uni Osnabrück
2  * This file is part of the LAS VEGAS Reconstruction Toolkit,
3  *
4  * LAS VEGAS is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * LAS VEGAS is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
17  */
18 
28 #include <fstream>
29 #include <unistd.h>
30 
31 #include "lvr2/io/DracoDecoder.hpp"
32 #include "lvr2/io/DracoEncoder.hpp"
33 #include "lvr2/io/DrcIO.hpp"
34 
35 namespace lvr2
36 {
37 
39 {
40  std::ifstream file(filename, std::ios::binary);
41  if (!file)
42  {
43  std::cerr << "File:"
44  << " " << filename << " "
45  << "could not be read!" << std::endl;
46  return ModelPtr(new Model());
47  }
48 
49  std::streampos file_size = 0;
50  file.seekg(0, std::ios::end);
51  file_size = file.tellg() - file_size;
52  file.seekg(0, std::ios::beg);
53  std::vector<char> data(file_size);
54  file.read(data.data(), file_size);
55 
56  if (data.empty())
57  {
58  std::cerr << "File:"
59  << " " << filename << " "
60  << "is empty!" << std::endl;
61  return ModelPtr(new Model());
62  }
63 
64  draco::DecoderBuffer buffer;
65  buffer.Init(data.data(), data.size());
66 
67  auto type = draco::Decoder::GetEncodedGeometryType(&buffer);
68 
69  if (!type.ok())
70  {
71  std::cerr << "Content in"
72  << " " << filename << " "
73  << "is neither a Mesh nor a PointCloud!" << std::endl;
74  return ModelPtr(new Model());
75  }
76 
77  const draco::EncodedGeometryType geom_type = type.value();
78 
79  // decode
80  ModelPtr modelPtr = decodeDraco(buffer, geom_type);
81  m_model = modelPtr;
82  return modelPtr;
83 }
84 
85 void DrcIO::save(string filename)
86 {
87  // check for validity
88  if (!m_model)
89  {
90  std::cerr << "no model set for export!" << std::endl;
91  return;
92  }
93 
94  if (!m_model->m_mesh && !m_model->m_pointCloud)
95  {
96  std::cerr << "model does not contain geometry data!" << std::endl;
97  return;
98  }
99 
100  // encode
101  std::unique_ptr<draco::EncoderBuffer> buffer =
102  encodeDraco(m_model, (m_model->m_pointCloud ? draco::EncodedGeometryType::POINT_CLOUD
103  : draco::EncodedGeometryType::TRIANGULAR_MESH));
104 
105  if (buffer)
106  {
107  std::ofstream file(filename, std::ios::binary);
108 
109  // write to file
110  file.write(buffer->data(), buffer->size());
111  }
112 }
113 
114 void DrcIO::save(ModelPtr model, string filename)
115 {
116  m_model = model;
117  save(filename);
118 }
119 
120 } /* namespace lvr */
Decodes a draco comptressed file into a lvr model.
virtual void save(string filename)
Save/Compress the loaded elements to a draco file.
Definition: DrcIO.cpp:85
ModelPtr m_model
Definition: BaseIO.hpp:104
IO module for importing and exporting .drc files.
ModelPtr decodeDraco(draco::DecoderBuffer &buffer, draco::EncodedGeometryType type)
delivers ModelPtr from draco DecoderBuffer
FILE * file
std::unique_ptr< draco::EncoderBuffer > encodeDraco(ModelPtr modelPtr, draco::EncodedGeometryType type)
encodes Model to draco EncodeBuffer which contents can be written into a file
std::shared_ptr< Model > ModelPtr
Definition: Model.hpp:80
virtual ModelPtr read(string filename)
Parse the draco and load supported elements.
Definition: DrcIO.cpp:38
Encodes a lvr model into a draco compressed file.


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 Mon Feb 28 2022 22:46:06