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
|
// 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_IO_POINT_CLOUD_IO_H_
#define DRACO_IO_POINT_CLOUD_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 WritePointCloudIntoStream(const PointCloud *pc, OutStreamT &&os,
PointCloudEncodingMethod method,
const EncoderOptions &options) {
EncoderBuffer buffer;
EncoderOptions local_options = options;
ExpertEncoder encoder(*pc);
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 WritePointCloudIntoStream(const PointCloud *pc, OutStreamT &&os,
PointCloudEncodingMethod method) {
const EncoderOptions options = EncoderOptions::CreateDefaultOptions();
return WritePointCloudIntoStream(pc, os, method, options);
}
template <typename OutStreamT>
OutStreamT &WritePointCloudIntoStream(const PointCloud *pc, OutStreamT &&os) {
return WritePointCloudIntoStream(pc, os, POINT_CLOUD_SEQUENTIAL_ENCODING);
}
template <typename InStreamT>
InStreamT &ReadPointCloudFromStream(std::unique_ptr<PointCloud> *point_cloud,
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 point cloud from that data.
DecoderBuffer buffer;
buffer.Init(&data[0], data.size());
Decoder decoder;
auto statusor = decoder.DecodePointCloudFromBuffer(&buffer);
*point_cloud = std::move(statusor).value();
if (!statusor.ok() || *point_cloud == nullptr) {
is.setstate(std::ios_base::badbit);
}
return is;
}
// Reads a point cloud 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::PointCloudDecoder.
// Returns nullptr with an error status if the decoding failed.
StatusOr<std::unique_ptr<PointCloud>> ReadPointCloudFromFile(
const std::string &file_name);
} // namespace draco
#endif // DRACO_IO_POINT_CLOUD_IO_H_
|