QGIS/external/untwine/epf/FileProcessor.cpp
Martin Dobias d76ec1cecb Update untwine to match the current hobu/untwine main branch
Added a minor update to the untwine_to_qgis.bash script as it was
getting confused about the "untwine" subfolders and copying sources
one level deeper as supposed to
2020-12-16 16:16:55 +01:00

103 lines
3.6 KiB
C++

/*****************************************************************************
* Copyright (c) 2020, Hobu, Inc. (info@hobu.co) *
* *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
****************************************************************************/
#include "FileProcessor.hpp"
#include "../untwine/ProgressWriter.hpp"
#include <pdal/StageFactory.hpp>
#include <pdal/filters/StreamCallbackFilter.hpp>
namespace untwine
{
namespace epf
{
FileProcessor::FileProcessor(const FileInfo& fi, size_t pointSize, const Grid& grid,
Writer *writer, ProgressWriter& progress) :
m_fi(fi), m_cellMgr(pointSize, writer), m_grid(grid), m_progress(progress)
{}
void FileProcessor::run()
{
pdal::Options opts;
opts.add("filename", m_fi.filename);
pdal::StageFactory factory;
pdal::Stage *s = factory.createStage(m_fi.driver);
s->setOptions(opts);
pdal::StreamCallbackFilter f;
const PointCount CountIncrement = 100000;
PointCount count = 0;
PointCount limit = CountIncrement;
// We need to move the data from the PointRef to some output buffer. We copy the data
// to the end of the *last* output buffer we used in hopes that it's the right one.
// If it's not we lose and we're forced to move that data to the another cell,
// which then becomes the active cell.
// This is some random cell that ultimately won't get used, but it contains a buffer
// into which we can write data.
Cell *cell = m_cellMgr.get(VoxelKey());
f.setCallback([this, &count, &limit, &cell](pdal::PointRef& point)
{
// Write the data into the point buffer in the cell. This is the *last*
// cell buffer that we used. We're hoping that it's the right one.
Point p = cell->point();
for (const FileDimInfo& fdi : m_fi.dimInfo)
point.getField(reinterpret_cast<char *>(p.data() + fdi.offset),
fdi.dim, fdi.type);
// Find the actual cell that this point belongs in. If it's not the one
// we chose, copy the data to the correct cell.
VoxelKey cellIndex = m_grid.key(p.x(), p.y(), p.z());
if (cellIndex != cell->key())
{
cell = m_cellMgr.get(cellIndex);
cell->copyPoint(p);
}
// Advance the cell - move the buffer pointer so when refer to the cell's
// point, we're referring to the next location in the cell's buffer.
cell->advance();
count++;
if (count == limit)
{
m_progress.update(CountIncrement);
limit += CountIncrement;
}
return true;
}
);
f.setInput(*s);
pdal::FixedPointTable t(1000);
try
{
f.prepare(t);
f.execute(t);
}
catch (const pdal::pdal_error& err)
{
fatal(err.what());
}
m_progress.update(count % CountIncrement);
}
} // namespace epf
} // namespace untwine