QGIS API Documentation 3.43.0-Master (32433f7016e)
qgspointcloudlayerundocommand.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspointcloudlayerundocommand.cpp
3 ---------------------
4 begin : January 2025
5 copyright : (C) 2025 by Stefanos Natsis
6 email : uclaros 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 "qgseventtracing.h"
20#include "qgspointcloudlayer.h"
22
23#include <QtConcurrentMap>
24
25
29
32 , mAttribute( attribute )
33 , mNewValue( value )
34{
35 QgsEventTracing::ScopedEvent _trace( QStringLiteral( "PointCloud" ), QStringLiteral( "QgsPointCloudLayerUndoCommand constructor" ) );
36
38 QgsPointCloudEditingIndex *editIndex = static_cast<QgsPointCloudEditingIndex *>( index.get() );
39
40 std::function mapFn = [editIndex, attribute, index = std::move( index )]( std::pair<const QgsPointCloudNodeId &, const QVector<int> &> pair )
41 {
42 QgsPointCloudNodeId n = pair.first;
43 auto &points = pair.second;
44 QgsPointCloudIndex index2 = index; // Copy to remove const
45
46 PerNodeData perNodeData;
47
48 if ( editIndex->isNodeModified( n ) )
49 {
50 const QgsPointCloudAttributeCollection allAttributes = index.attributes();
52 req.setAttributes( allAttributes );
53 // we want to iterate all points so we have the correct point indexes within the node
55 std::unique_ptr<QgsPointCloudBlock> block = index2.nodeData( n, req );
56 const char *ptr = block->data();
57 block->attributes().find( attribute.name(), perNodeData.attributeOffset );
58 const int size = block->pointRecordSize();
59 for ( const int point : points )
60 {
61 const int offset = point * size + perNodeData.attributeOffset;
62 const double oldValue = attribute.convertValueToDouble( ptr + offset );
63 perNodeData.oldPointValues[point] = oldValue;
64 }
65 }
66 else
67 {
68 // If this is the first time this node is edited, we don't need the previous values, we will just discard the node from the edit index when undoing
69 // we still need the keys in mPointValues though as they are the points to be modified in the Redo stage, so we populate them with some NaNs
70 perNodeData.firstEdit = true;
71 for ( const int point : points )
72 {
73 perNodeData.oldPointValues[point] = std::numeric_limits<double>::quiet_NaN();
74 }
75 }
76
77 return std::pair { n, perNodeData };
78 };
79
80 std::function reduceFn = []( QHash<QgsPointCloudNodeId, PerNodeData> &res, const std::pair<QgsPointCloudNodeId, PerNodeData> &pair )
81 {
82 res[pair.first] = pair.second;
83 };
84
85 mPerNodeData = QtConcurrent::blockingMappedReduced<QHash<QgsPointCloudNodeId, PerNodeData>>(
86 nodesAndPoints.keyValueBegin(), nodesAndPoints.keyValueEnd(),
87 std::move( mapFn ), std::move( reduceFn )
88 );
89}
90
92{
93 undoRedoPrivate( true );
94}
95
97{
98 undoRedoPrivate( false );
99}
100
101void QgsPointCloudLayerUndoCommandChangeAttribute::undoRedoPrivate( bool isUndo )
102{
103 QgsEventTracing::ScopedEvent _trace( QStringLiteral( "PointCloud" ), QStringLiteral( "QgsPointCloudLayerUndoCommand::undoRedoPrivate" ) );
104 QgsPointCloudEditingIndex *editIndex = dynamic_cast<QgsPointCloudEditingIndex *>( mLayer->index().get() );
105 QgsCopcPointCloudIndex *copcIndex = dynamic_cast<QgsCopcPointCloudIndex *>( editIndex->backingIndex().get() );
106 QgsPointCloudAttribute attribute = mAttribute;
107
108 QtConcurrent::blockingMap(
109 mPerNodeData.keyValueBegin(),
110 mPerNodeData.keyValueEnd(),
111 [editIndex, copcIndex, isUndo, attribute = mAttribute, newValue = mNewValue](
112 std::pair<const QgsPointCloudNodeId &, PerNodeData &> pair
113 )
114 {
115 QgsPointCloudNodeId node = pair.first;
116 PerNodeData &perNodeData = pair.second;
117
118 QByteArray chunkData = editIndex->rawEditedNodeData( node );
119 if ( chunkData.isEmpty() ) // Not edited yet
120 chunkData = copcIndex->rawNodeData( node );
121
122 QByteArray data;
123 if ( isUndo && perNodeData.firstEdit )
124 {
125 editIndex->resetNodeEdits( node );
126 }
127 else if ( isUndo )
128 {
129 data = QgsPointCloudLayerEditUtils::updateChunkValues( copcIndex, chunkData, attribute, node, perNodeData.oldPointValues );
130 editIndex->updateNodeData( { { node, data } } );
131 }
132 else
133 {
134 data = QgsPointCloudLayerEditUtils::updateChunkValues( copcIndex, chunkData, attribute, node, perNodeData.oldPointValues, newValue );
135 editIndex->updateNodeData( { { node, data } } );
136 }
137 }
138 );
139
140 for ( auto it = mPerNodeData.constBegin(); it != mPerNodeData.constEnd(); it++ )
141 {
142 emit mLayer->chunkAttributeValuesChanged( it.key() );
143 }
144}
A collection of point cloud attributes.
Attribute for point cloud data pair of name and size in bytes.
QString name() const
Returns name of the attribute.
double convertValueToDouble(const char *ptr) const
Returns the attribute's value as a double for data pointed to by ptr.
A QgsPointCloudIndex that is used as an editing buffer when editing point cloud data.
QgsPointCloudIndex backingIndex() const
Returns index for the underlying non-edited data.
bool isNodeModified(QgsPointCloudNodeId n) const
Returns true if this node was modified.
bool updateNodeData(const QHash< QgsPointCloudNodeId, QByteArray > &data) override
Tries to update the data for the specified nodes.
Smart pointer for QgsAbstractPointCloudIndex.
std::unique_ptr< QgsPointCloudBlock > nodeData(const QgsPointCloudNodeId &n, const QgsPointCloudRequest &request)
Returns node data block.
QgsAbstractPointCloudIndex * get()
Returns pointer to the implementation class.
QgsPointCloudAttributeCollection attributes() const
Returns all attributes that are stored in the file.
static QByteArray updateChunkValues(QgsCopcPointCloudIndex *copcIndex, const QByteArray &chunkData, const QgsPointCloudAttribute &attribute, const QgsPointCloudNodeId &n, const QHash< int, double > &pointValues, std::optional< double > newValue=std::nullopt)
Sets new classification value for the given points in voxel and return updated chunk data.
QgsPointCloudLayerUndoCommandChangeAttribute(QgsPointCloudLayer *layer, const QHash< QgsPointCloudNodeId, QVector< int > > &nodesAndPoints, const QgsPointCloudAttribute &attribute, double value)
Constructor for QgsPointCloudLayerUndoCommandChangeAttribute.
Base class for undo/redo command for point cloud editing.
QgsPointCloudLayerUndoCommand(QgsPointCloudLayer *layer)
Ctor.
Represents a map layer supporting display of point clouds.
QgsPointCloudIndex index() const
Returns the point cloud index associated with the layer.
Represents an indexed point cloud node's position in octree.
Point cloud data request.
void setIgnoreIndexFilterEnabled(bool enable)
When enable is true, the request will ignore the point cloud index's filter expression and use an emp...
void setAttributes(const QgsPointCloudAttributeCollection &attributes)
Set attributes filter in the request.