-
Notifications
You must be signed in to change notification settings - Fork 432
/
BpfWriter.cpp
299 lines (258 loc) · 9.14 KB
/
BpfWriter.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/******************************************************************************
* Copyright (c) 2015, Hobu Inc., hobu@hobu.co
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
****************************************************************************/
#include <pdal/drivers/bpf/BpfWriter.hpp>
#include <pdal/pdal_internal.hpp>
#include <zlib.h>
#include <pdal/Charbuf.hpp>
#include <pdal/Options.hpp>
#include "BpfCompressor.hpp"
namespace pdal
{
Options BpfWriter::getDefaultOptions()
{
Options ops;
ops.add("filename", "", "Filename for BPF output");
ops.add("compression", false, "Whether zlib compression should be used");
ops.add("format", "dimension", "Point output format: "
"non-interleaved(\"dimension\"), interleaved(\"point\") or "
"byte-segregated(\"byte\")");
ops.add("coord_id", 0, "Coordinate ID (UTM zone).");
return ops;
}
void BpfWriter::processOptions(const Options& options)
{
m_filename = options.getValueOrThrow<std::string>("filename");
bool compression = options.getValueOrDefault("compression", false);
m_header.m_compression = compression ? BpfCompression::Zlib :
BpfCompression::None;
std::string fileFormat =
options.getValueOrDefault<std::string>("format", "POINT");
std::transform(fileFormat.begin(), fileFormat.end(), fileFormat.begin(),
::toupper);
if (fileFormat.find("POINT") != std::string::npos)
m_header.m_pointFormat = BpfFormat::PointMajor;
else if (fileFormat.find("BYTE") != std::string::npos)
m_header.m_pointFormat = BpfFormat::ByteMajor;
else
m_header.m_pointFormat = BpfFormat::DimMajor;
if (options.hasOption("identity_mueller"))
m_writeIdentity_xform = options.getValueOrThrow<bool>("identity_mueller");
else
m_writeIdentity_xform = false;
if (options.hasOption("coord_id"))
{
m_header.m_coordType = BpfCoordType::UTM;
m_header.m_coordId = options.getValueOrThrow<int>("coord_id");
}
else
{
m_header.m_coordType = BpfCoordType::None;
m_header.m_coordId = 0;
}
m_header.setLog(log());
}
void BpfWriter::ready(PointContext ctx)
{
loadBpfDimensions(ctx);
m_stream = FileUtils::createFile(m_filename, true);
m_header.m_version = 3;
m_header.m_numDim = m_dims.size();
// We will re-write the header and dimensions to account for the point
// count and dimension min/max.
m_header.write(m_stream);
m_header.writeDimensions(m_stream, m_dims);
m_header.m_len = m_stream.position();
}
void BpfWriter::loadBpfDimensions(PointContext ctx)
{
Dimension *dim;
// Verify that we have X, Y and Z and that they're the first three
// dimensions.
m_schema = ctx.schema();
auto addDimension = [this](Dimension *d)
{
BpfDimension dim;
dim.m_label = d->getName();
dim.m_offset = d->getNumericOffset();
dim.m_dim = d;
m_dims.push_back(dim);
};
dim = m_schema->getDimensionPtr("X");
if (!dim)
throw pdal_error("Couldn't find required X dimension for BPF output.");
addDimension(dim);
if (!m_writeIdentity_xform)
m_header.m_xform.m_vals[0]= dim->getNumericScale();
dim = m_schema->getDimensionPtr("Y");
if (!dim)
throw pdal_error("Couldn't find required Y dimension for BPF output.");
addDimension(dim);
if (!m_writeIdentity_xform)
m_header.m_xform.m_vals[5] = dim->getNumericScale();
dim = m_schema->getDimensionPtr("Z");
if (!dim)
throw pdal_error("Couldn't find required Z dimension for BPF output.");
addDimension(dim);
if (!m_writeIdentity_xform)
m_header.m_xform.m_vals[10] = dim->getNumericScale();
size_t numDims = m_schema->numDimensions();
for (size_t d = 0; d < numDims; ++d)
{
Dimension *dim = m_schema->getDimensionPtr(d);
if (dim->getName() != "X" && dim->getName() != "Y" &&
dim->getName() != "Z")
{
addDimension(dim);
}
}
}
void BpfWriter::write(const PointBuffer& data)
{
switch (m_header.m_pointFormat)
{
case BpfFormat::PointMajor:
writePointMajor(data);
break;
case BpfFormat::DimMajor:
writeDimMajor(data);
break;
case BpfFormat::ByteMajor:
writeByteMajor(data);
break;
}
m_header.m_numPts += data.size();
}
void BpfWriter::writePointMajor(const PointBuffer& data)
{
// Blocks of 10,000 points will ensure that we're under 16MB, even
// for 255 dimensions.
size_t blockpoints = std::min(10000UL, data.size());
// For compression we're going to write to a buffer so that it can be
// compressed before it's written to the file stream.
BpfCompressor compressor(m_stream,
blockpoints * sizeof(float) * m_dims.size());
PointId idx = 0;
while (idx < data.size())
{
if (m_header.m_compression)
compressor.startBlock();
size_t blockId;
for (blockId = 0; idx < data.size() && blockId < blockpoints;
++idx, ++blockId)
{
for (auto& bpfDim : m_dims)
{
double d = data.getFieldAs<double>(*bpfDim.m_dim, idx);
bpfDim.m_min = std::min(bpfDim.m_min, d);
bpfDim.m_max = std::max(bpfDim.m_max, d);
float f = data.getFieldAs<float>(*bpfDim.m_dim, idx, false);
m_stream << f;
}
}
if (m_header.m_compression)
{
compressor.compress();
compressor.finish();
}
}
}
void BpfWriter::writeDimMajor(const PointBuffer& data)
{
// We're going to pretend for now that we only even have one point buffer.
BpfCompressor compressor(m_stream, data.size() * sizeof(float));
for (auto & bpfDim : m_dims)
{
if (m_header.m_compression)
compressor.startBlock();
for (PointId idx = 0; idx < data.size(); ++idx)
{
double d = data.getFieldAs<double>(*bpfDim.m_dim, idx);
bpfDim.m_min = std::min(bpfDim.m_min, d);
bpfDim.m_max = std::max(bpfDim.m_max, d);
float f = data.getFieldAs<float>(*bpfDim.m_dim, idx, false);
m_stream << f;
}
if (m_header.m_compression)
{
compressor.compress();
compressor.finish();
}
}
}
void BpfWriter::writeByteMajor(const PointBuffer& data)
{
union
{
float f;
uint32_t u32;
} uu;
// We're going to pretend for now that we only even have one point buffer.
BpfCompressor compressor(m_stream,
data.size() * sizeof(float) * m_dims.size());
if (m_header.m_compression)
compressor.startBlock();
for (auto & bpfDim : m_dims)
{
for (size_t b = 0; b < sizeof(float); b++)
{
for (PointId idx = 0; idx < data.size(); ++idx)
{
if (b == 0)
{
double d = data.getFieldAs<double>(*bpfDim.m_dim, idx);
bpfDim.m_min = std::min(bpfDim.m_min, d);
bpfDim.m_max = std::max(bpfDim.m_max, d);
}
uu.f = data.getFieldAs<float>(*bpfDim.m_dim, idx, false);
uint8_t u8 = (uint8_t)(uu.u32 >> (b * CHAR_BIT));
m_stream << u8;
}
}
}
if (m_header.m_compression)
{
compressor.compress();
compressor.finish();
}
}
void BpfWriter::done(PointContext)
{
// Rewrite the header to update the the correct number of points and
// statistics.
m_stream.seek(0);
m_header.write(m_stream);
m_header.writeDimensions(m_stream, m_dims);
m_stream.flush();
}
} //namespace pdal