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
|
// Copyright 2016 The Draco Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef DRACO_MESH_MESH_IO_H_
#define DRACO_MESH_MESH_IO_H_
#include "draco/compression/config/compression_shared.h"
#include "draco/compression/decode.h"
#include "draco/compression/expert_encode.h"
namespace draco {
template <typename OutStreamT>
OutStreamT WriteMeshIntoStream(const Mesh *mesh, OutStreamT &&os,
MeshEncoderMethod method,
const EncoderOptions &options) {
EncoderBuffer buffer;
EncoderOptions local_options = options;
ExpertEncoder encoder(*mesh);
encoder.Reset(local_options);
encoder.SetEncodingMethod(method);
if (!encoder.EncodeToBuffer(&buffer).ok()) {
os.setstate(std::ios_base::badbit);
return os;
}
os.write(static_cast<const char *>(buffer.data()), buffer.size());
return os;
}
template <typename OutStreamT>
OutStreamT WriteMeshIntoStream(const Mesh *mesh, OutStreamT &&os,
MeshEncoderMethod method) {
const EncoderOptions options = EncoderOptions::CreateDefaultOptions();
return WriteMeshIntoStream(mesh, os, method, options);
}
template <typename OutStreamT>
OutStreamT &WriteMeshIntoStream(const Mesh *mesh, OutStreamT &&os) {
return WriteMeshIntoStream(mesh, os, MESH_EDGEBREAKER_ENCODING);
}
template <typename InStreamT>
InStreamT &ReadMeshFromStream(std::unique_ptr<Mesh> *mesh, InStreamT &&is) {
// Determine size of stream and write into a vector
const auto start_pos = is.tellg();
is.seekg(0, std::ios::end);
const std::streampos is_size = is.tellg() - start_pos;
is.seekg(start_pos);
std::vector<char> data(is_size);
is.read(&data[0], is_size);
// Create a mesh from that data.
DecoderBuffer buffer;
buffer.Init(&data[0], data.size());
Decoder decoder;
auto statusor = decoder.DecodeMeshFromBuffer(&buffer);
*mesh = std::move(statusor).value();
if (!statusor.ok() || *mesh == nullptr) {
is.setstate(std::ios_base::badbit);
}
return is;
}
// Reads a mesh from a file. The function automatically chooses the correct
// decoder based on the extension of the files. Currently, .obj and .ply files
// are supported. Other file extensions are processed by the default
// draco::MeshDecoder.
// Returns nullptr with an error status if the decoding failed.
StatusOr<std::unique_ptr<Mesh>> ReadMeshFromFile(const std::string &file_name);
// Reads a mesh from a file. The function does the same thing as the previous
// one except using metadata to encode additional information when
// |use_metadata| is set to true.
// Returns nullptr with an error status if the decoding failed.
StatusOr<std::unique_ptr<Mesh>> ReadMeshFromFile(const std::string &file_name,
bool use_metadata);
} // namespace draco
#endif // DRACO_MESH_MESH_IO_H_
|