QGIS API Documentation 3.99.0-Master (8e76e220402)
Loading...
Searching...
No Matches
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
17
19#include "qgseventtracing.h"
21#include "qgspointcloudlayer.h"
23
24#include <QString>
25#include <QtConcurrentMap>
26
27using namespace Qt::StringLiterals;
28
32
35 , mAttribute( attribute )
36 , mNewValue( value )
37{
38 QgsEventTracing::ScopedEvent _trace( u"PointCloud"_s, u"QgsPointCloudLayerUndoCommand constructor"_s );
39
40 QList<NodeProcessData> processData;
41 for ( auto it = mappedPoints.constBegin(); it != mappedPoints.constEnd(); ++it )
42 {
43 const int position = it.key();
45 if ( position < 0 )
46 index = mLayer->index();
47 else
48 index = mLayer->subIndexes().at( position ).index();
49 const auto &nodes = it.value();
50 for ( auto nodeIt = nodes.constBegin(); nodeIt != nodes.constEnd(); ++nodeIt )
51 {
52 processData.append( { position, index, nodeIt.key(), nodeIt.value() } );
53 }
54 }
55
56 std::function mapFn = [attribute]( const NodeProcessData & data )
57 {
58 QgsPointCloudIndex index = data.index;
59 QgsPointCloudEditingIndex *editIndex = static_cast<QgsPointCloudEditingIndex *>( index.get() );
60 QgsPointCloudNodeId n = data.nodeId;
61 const QVector<int> &points = data.points;
62
63 PerNodeData perNodeData;
64
65 if ( editIndex->isNodeModified( n ) )
66 {
67 const QgsPointCloudAttributeCollection allAttributes = index.attributes();
69 req.setAttributes( allAttributes );
70 // we want to iterate all points so we have the correct point indexes within the node
72 std::unique_ptr<QgsPointCloudBlock> block = index.nodeData( n, req );
73 const char *ptr = block->data();
74 block->attributes().find( attribute.name(), perNodeData.attributeOffset );
75 const int size = block->pointRecordSize();
76 for ( const int point : points )
77 {
78 const int offset = point * size + perNodeData.attributeOffset;
79 const double oldValue = attribute.convertValueToDouble( ptr + offset );
80 perNodeData.oldPointValues[point] = oldValue;
81 }
82 }
83 else
84 {
85 // 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
86 // 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
87 perNodeData.firstEdit = true;
88 for ( const int point : points )
89 {
90 perNodeData.oldPointValues[point] = std::numeric_limits<double>::quiet_NaN();
91 }
92 }
93
94 return std::pair { data.position, std::pair { n, perNodeData } };
95 };
96
97 std::function reduceFn = []( QMap<int, QHash<QgsPointCloudNodeId, PerNodeData>> &res, const std::pair<int, std::pair<QgsPointCloudNodeId, PerNodeData>> &pair )
98 {
99 res[pair.first][pair.second.first] = pair.second.second;
100 };
101
102 mPerNodeData = QtConcurrent::blockingMappedReduced<QMap<int, QHash<QgsPointCloudNodeId, PerNodeData>>>(
103 processData,
104 std::move( mapFn ), std::move( reduceFn )
105 );
106}
107
109{
110 undoRedoPrivate( true );
111}
112
114{
115 undoRedoPrivate( false );
116}
117
118void QgsPointCloudLayerUndoCommandChangeAttribute::undoRedoPrivate( bool isUndo )
119{
120 QgsEventTracing::ScopedEvent _trace( u"PointCloud"_s, u"QgsPointCloudLayerUndoCommand::undoRedoPrivate"_s );
121
122 QgsPointCloudAttribute attribute = mAttribute;
123 double newValue = mNewValue;
124
125 for ( auto it = mPerNodeData.begin(); it != mPerNodeData.end(); ++it )
126 {
127 const int position = it.key();
128 QHash<QgsPointCloudNodeId, PerNodeData> &nodesData = it.value();
129
130 QgsPointCloudIndex index;
131 if ( position < 0 )
132 index = mLayer->index();
133 else
134 index = mLayer->subIndexes().at( position ).index();
135 QgsPointCloudEditingIndex *editIndex = dynamic_cast<QgsPointCloudEditingIndex *>( index.get() );
136 QgsCopcPointCloudIndex *copcIndex = dynamic_cast<QgsCopcPointCloudIndex *>( editIndex->backingIndex().get() );
137
138 QtConcurrent::blockingMap(
139 nodesData.keyValueBegin(),
140 nodesData.keyValueEnd(),
141 [editIndex, copcIndex, isUndo, attribute, newValue](
142 std::pair<const QgsPointCloudNodeId &, PerNodeData &> pair
143 )
144 {
145 QgsPointCloudNodeId node = pair.first;
146 PerNodeData &perNodeData = pair.second;
147
148 QByteArray chunkData = editIndex->rawEditedNodeData( node );
149 if ( chunkData.isEmpty() ) // Not edited yet
150 chunkData = copcIndex->rawNodeData( node );
151
152 QByteArray data;
153 if ( isUndo && perNodeData.firstEdit )
154 {
155 editIndex->resetNodeEdits( node );
156 }
157 else if ( isUndo )
158 {
159 data = QgsPointCloudLayerEditUtils::updateChunkValues( copcIndex, chunkData, attribute, node, perNodeData.oldPointValues );
160 editIndex->updateNodeData( { { node, data } } );
161 }
162 else
163 {
164 data = QgsPointCloudLayerEditUtils::updateChunkValues( copcIndex, chunkData, attribute, node, perNodeData.oldPointValues, newValue );
165 editIndex->updateNodeData( { { node, data } } );
166 }
167 }
168 );
169
170 for ( auto itNode = nodesData.constBegin(); itNode != nodesData.constEnd(); itNode++ )
171 {
172 emit mLayer->chunkAttributeValuesChanged( itNode.key(), position );
173 }
174 }
175}
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< int, QHash< QgsPointCloudNodeId, QVector< int > > > &mappedPoints, const QgsPointCloudAttribute &attribute, double value)
Constructor for QgsPointCloudLayerUndoCommandChangeAttribute.
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.
QVector< QgsPointCloudSubIndex > subIndexes() const
Returns point cloud indexes associated with the layer (only if the layer has a virtual point cloud da...
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.