QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgscopcupdate.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscopcupdate.cpp
3 ---------------------
4 begin : January 2025
5 copyright : (C) 2025 by Martin Dobias
6 email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgscopcupdate.h"
17
18#include <fstream>
19#include <iostream>
20
21#include "lazperf/Extractor.hpp"
22#include "lazperf/filestream.hpp"
23#include "lazperf/header.hpp"
24#include "lazperf/readers.hpp"
25#include "lazperf/vlr.hpp"
26#include "lazperf/writers.hpp"
27#include "qgslazdecoder.h"
28
29#include <QString>
30
31using namespace Qt::StringLiterals;
32
35{
38
44 uint64_t offset;
45
51 int32_t byteSize;
52
58 int32_t pointCount;
59};
60
61typedef QVector<HierarchyEntry> HierarchyEntries;
62
63
64HierarchyEntries getHierarchyPage( std::ifstream &file, uint64_t offset, uint64_t size )
65{
67 std::vector<char> buf( 32 );
68 int numEntries = static_cast<int>( size / 32 );
69 file.seekg( static_cast<int64_t>( offset ) );
70 while ( numEntries-- )
71 {
72 file.read( buf.data(), static_cast<long>( buf.size() ) );
73 lazperf::LeExtractor s( buf.data(), buf.size() );
74
76 int d, x, y, z;
77 s >> d >> x >> y >> z;
78 s >> e.offset >> e.byteSize >> e.pointCount;
79 e.key = QgsPointCloudNodeId( d, x, y, z );
80
81 page.push_back( e );
82 }
83 return page;
84}
85
86
87bool QgsCopcUpdate::write( const QString &outputFilename, const QHash<QgsPointCloudNodeId, UpdatedChunk> &updatedChunks )
88{
89 std::ofstream m_f;
90 m_f.open( QgsLazDecoder::toNativePath( outputFilename ), std::ios::out | std::ios::binary );
91
92 // write header and all VLRs all the way to point offset
93 // (then we patch what we need)
94 mFile.seekg( 0 );
95 std::vector<char> allHeaderData;
96 allHeaderData.resize( mHeader.point_offset );
97 mFile.read( allHeaderData.data(), static_cast<long>( allHeaderData.size() ) );
98 m_f.write( allHeaderData.data(), static_cast<long>( allHeaderData.size() ) );
99
100 m_f.write( "XXXXXXXX", 8 ); // placeholder for chunk table offset
101
102 uint64_t currentChunkOffset = mHeader.point_offset + 8;
103 mFile.seekg( static_cast<long>( currentChunkOffset ) ); // this is where first chunk starts
104
105 // now, let's write chunks:
106 // - iterate through original chunk table, write out chunks
107 // - if chunk is updated, use that instead
108 // - keep updating hierarchy as we go
109 // - keep updating chunk table as we go
110
111 QHash<QgsPointCloudNodeId, uint64_t> voxelToNewOffset;
112
113 int chIndex = 0;
114 for ( lazperf::chunk ch : mChunks )
115 {
116 Q_ASSERT( mOffsetToVoxel.contains( currentChunkOffset ) );
117 QgsPointCloudNodeId n = mOffsetToVoxel[currentChunkOffset];
118
119 uint64_t newOffset = m_f.tellp();
120 voxelToNewOffset[n] = newOffset;
121
122 // check whether the chunk is modified
123 if ( updatedChunks.contains( n ) )
124 {
125 const UpdatedChunk &updatedChunk = updatedChunks[n];
126
127 // use updated one and skip in the original file
128 mFile.seekg( static_cast<long>( mFile.tellg() ) + static_cast<long>( ch.offset ) );
129
130 m_f.write( updatedChunk.chunkData.constData(), updatedChunk.chunkData.size() );
131
132 // update sizes
133 mChunks[chIndex].offset = updatedChunk.chunkData.size();
134 }
135 else
136 {
137 // use as is
138 std::vector<char> originalChunkData;
139 originalChunkData.resize( ch.offset );
140 mFile.read( originalChunkData.data(), static_cast<long>( originalChunkData.size() ) );
141 m_f.write( originalChunkData.data(), static_cast<long>( originalChunkData.size() ) );
142 }
143
144 currentChunkOffset += ch.offset;
145 ++chIndex;
146 }
147
148 // write chunk table: size in bytes + point count of each chunk
149
150 const uint64_t newChunkTableOffset = m_f.tellp();
151
152 m_f.write( "\0\0\0\0", 4 ); // chunk table version
153 m_f.write( reinterpret_cast<const char *>( &mChunkCount ), sizeof( mChunkCount ) );
154
155 lazperf::OutFileStream outStream( m_f );
156 lazperf::compress_chunk_table( outStream.cb(), mChunks, true );
157
158 // update hierarchy
159
160 // NOTE: one big assumption we're doing here is that existing hierarchy pages
161 // are packed one after another, with no gaps. if that's not the case, things
162 // will break apart
163
164 const long hierPositionShift = static_cast<long>( m_f.tellp() ) + 60 - static_cast<long>( mHierarchyOffset );
165
166 HierarchyEntry *oldCopcHierarchyBlobEntries = reinterpret_cast<HierarchyEntry *>( mHierarchyBlob.data() );
167 const int nEntries = static_cast<int>( mHierarchyBlob.size() / 32 );
168 for ( int i = 0; i < nEntries; ++i )
169 {
170 HierarchyEntry &e = oldCopcHierarchyBlobEntries[i];
171 if ( e.pointCount > 0 )
172 {
173 // update entry to new offset
174 Q_ASSERT( voxelToNewOffset.contains( e.key ) );
175 e.offset = voxelToNewOffset[e.key];
176
177 if ( updatedChunks.contains( e.key ) )
178 {
179 uint64_t newByteSize = updatedChunks[e.key].chunkData.size();
180 e.byteSize = static_cast<int>( newByteSize );
181 }
182 }
183 else if ( e.pointCount < 0 )
184 {
185 // move hierarchy pages to new offset
186 e.offset += hierPositionShift;
187 }
188 else // pointCount == 0
189 {
190 // nothing to do - byte size and offset should be zero
191 }
192
193 }
194
195 // write hierarchy eVLR
196
197 const uint64_t newEvlrOffset = m_f.tellp();
198
199 lazperf::evlr_header outCopcHierEvlr;
200 outCopcHierEvlr.reserved = 0;
201 outCopcHierEvlr.user_id = "copc";
202 outCopcHierEvlr.record_id = 1000;
203 outCopcHierEvlr.data_length = mHierarchyBlob.size();
204 outCopcHierEvlr.description = "EPT Hierarchy";
205
206 outCopcHierEvlr.write( m_f );
207 m_f.write( mHierarchyBlob.data(), static_cast<long>( mHierarchyBlob.size() ) );
208
209 // write other eVLRs
210
211 for ( size_t i = 0; i < mEvlrHeaders.size(); ++i )
212 {
213 lazperf::evlr_header evlrHeader = mEvlrHeaders[i];
214 std::vector<char> evlrBody = mEvlrData[i];
215
216 evlrHeader.write( m_f );
217 m_f.write( evlrBody.data(), static_cast<long>( evlrBody.size() ) );
218 }
219
220 // patch header
221
222 m_f.seekp( 235 );
223 m_f.write( reinterpret_cast<const char *>( &newEvlrOffset ), 8 );
224
225 const uint64_t newRootHierOffset = mCopcVlr.root_hier_offset + hierPositionShift;
226 m_f.seekp( 469 );
227 m_f.write( reinterpret_cast<const char *>( &newRootHierOffset ), 8 );
228
229 m_f.seekp( mHeader.point_offset );
230 m_f.write( reinterpret_cast<const char *>( &newChunkTableOffset ), 8 );
231
232 return true;
233}
234
235
236
237bool QgsCopcUpdate::read( const QString &inputFilename )
238{
239 mInputFilename = inputFilename;
240
241 mFile.open( QgsLazDecoder::toNativePath( inputFilename ), std::ios::binary | std::ios::in );
242 if ( mFile.fail() )
243 {
244 mErrorMessage = u"Could not open file for reading: %1"_s.arg( inputFilename );
245 return false;
246 }
247
248 if ( !readHeader() )
249 return false;
250
251 readChunkTable();
252 readHierarchy();
253
254 return true;
255}
256
257
258bool QgsCopcUpdate::readHeader()
259{
260 // read header and COPC VLR
261 mHeader = lazperf::header14::create( mFile );
262 if ( !mFile )
263 {
264 mErrorMessage = u"Error reading COPC header"_s;
265 return false;
266 }
267
268 lazperf::vlr_header vh = lazperf::vlr_header::create( mFile );
269 mCopcVlr = lazperf::copc_info_vlr::create( mFile );
270
271 int baseCount = lazperf::baseCount( mHeader.point_format_id );
272 if ( baseCount == 0 )
273 {
274 mErrorMessage = u"Bad point record format: %1"_s.arg( mHeader.point_format_id );
275 return false;
276 }
277
278 return true;
279}
280
281
282void QgsCopcUpdate::readChunkTable()
283{
284 uint64_t chunkTableOffset;
285
286 mFile.seekg( mHeader.point_offset );
287 mFile.read( reinterpret_cast<char *>( &chunkTableOffset ), sizeof( chunkTableOffset ) );
288 mFile.seekg( static_cast<long>( chunkTableOffset ) + 4 ); // The first 4 bytes are the version, then the chunk count.
289 mFile.read( reinterpret_cast<char *>( &mChunkCount ), sizeof( mChunkCount ) );
290
291 //
292 // read chunk table
293 //
294
295 bool variable = true;
296
297 // TODO: not sure why, but after decompress_chunk_table() the input stream seems to be dead, so we create a temporary one
298 std::ifstream copcFileTmp;
299 copcFileTmp.open( QgsLazDecoder::toNativePath( mInputFilename ), std::ios::binary | std::ios::in );
300 copcFileTmp.seekg( mFile.tellg() );
301 lazperf::InFileStream copcInFileStream( copcFileTmp );
302
303 mChunks = lazperf::decompress_chunk_table( copcInFileStream.cb(), mChunkCount, variable );
304 std::vector<lazperf::chunk> chunksWithAbsoluteOffsets;
305 uint64_t nextChunkOffset = mHeader.point_offset + 8;
306 for ( lazperf::chunk ch : mChunks )
307 {
308 chunksWithAbsoluteOffsets.push_back( {nextChunkOffset, ch.count} );
309 nextChunkOffset += ch.offset;
310 }
311}
312
313
314void QgsCopcUpdate::readHierarchy()
315{
316 // get all hierarchy pages
317
318 HierarchyEntries childEntriesToProcess;
319 childEntriesToProcess.push_back( HierarchyEntry
320 {
321 QgsPointCloudNodeId( 0, 0, 0, 0 ),
322 mCopcVlr.root_hier_offset,
323 static_cast<int32_t>( mCopcVlr.root_hier_size ),
324 -1 } );
325
326 while ( !childEntriesToProcess.empty() )
327 {
328 HierarchyEntry childEntry = childEntriesToProcess.back();
329 childEntriesToProcess.pop_back();
330
331 HierarchyEntries page = getHierarchyPage( mFile, childEntry.offset, childEntry.byteSize );
332
333 for ( const HierarchyEntry &e : page )
334 {
335 if ( e.pointCount > 0 ) // it's a non-empty node
336 {
337 Q_ASSERT( !mOffsetToVoxel.contains( e.offset ) );
338 mOffsetToVoxel[e.offset] = e.key;
339 }
340 else if ( e.pointCount < 0 ) // referring to a child page
341 {
342 childEntriesToProcess.push_back( e );
343 }
344 }
345 }
346
347 lazperf::evlr_header evlr1;
348 mFile.seekg( static_cast<long>( mHeader.evlr_offset ) );
349
350 mHierarchyOffset = 0; // where the hierarchy eVLR payload starts
351
352 for ( uint32_t i = 0; i < mHeader.evlr_count; ++i )
353 {
354 evlr1.read( mFile );
355 if ( evlr1.user_id == "copc" && evlr1.record_id == 1000 )
356 {
357 mHierarchyBlob.resize( evlr1.data_length );
358 mHierarchyOffset = mFile.tellg();
359 mFile.read( mHierarchyBlob.data(), static_cast<long>( evlr1.data_length ) );
360 }
361 else
362 {
363 // keep for later
364 mEvlrHeaders.push_back( evlr1 );
365 std::vector<char> evlrBlob;
366 evlrBlob.resize( evlr1.data_length );
367 mFile.read( evlrBlob.data(), static_cast<long>( evlrBlob.size() ) );
368 mEvlrData.emplace_back( std::move( evlrBlob ) );
369 }
370 }
371
372 Q_ASSERT( !mHierarchyBlob.empty() );
373}
374
375
376bool QgsCopcUpdate::writeUpdatedFile( const QString &inputFilename,
377 const QString &outputFilename,
378 const QHash<QgsPointCloudNodeId, UpdatedChunk> &updatedChunks,
379 QString *errorMessage )
380{
381 QgsCopcUpdate copcUpdate;
382 if ( !copcUpdate.read( inputFilename ) )
383 {
384 if ( errorMessage )
385 *errorMessage = copcUpdate.errorMessage();
386 return false;
387 }
388
389 if ( !copcUpdate.write( outputFilename, updatedChunks ) )
390 {
391 if ( errorMessage )
392 *errorMessage = copcUpdate.errorMessage();
393 return false;
394 }
395
396 return true;
397}
Handles update operations to a COPC file.
QString errorMessage() const
Returns error message.
static bool writeUpdatedFile(const QString &inputFilename, const QString &outputFilename, const QHash< QgsPointCloudNodeId, UpdatedChunk > &updatedChunks, QString *errorMessage=nullptr)
Convenience function to do the whole process in one go: load a COPC file, then write a new COPC file ...
bool read(const QString &inputFilename)
Reads input COPC file and initializes all the members.
bool write(const QString &outputFilename, const QHash< QgsPointCloudNodeId, UpdatedChunk > &updatedChunks)
Writes a COPC file with updated chunks.
Represents an indexed point cloud node's position in octree.
QVector< HierarchyEntry > HierarchyEntries
HierarchyEntries getHierarchyPage(std::ifstream &file, uint64_t offset, uint64_t size)
Keeps one entry of COPC hierarchy.
QgsPointCloudNodeId key
Key of the data to which this entry corresponds.
uint64_t offset
Absolute offset to the data chunk if the pointCount > 0.
int32_t pointCount
If > 0, represents the number of points in the data chunk.
int32_t byteSize
Size of the data chunk in bytes (compressed size) if the pointCount > 0.
Keeps information how points of a single chunk has been modified.
QByteArray chunkData
Data of the chunk (compressed already with LAZ compressor).