QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgspointcloudattribute.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspointcloudattribute.cpp
3 -----------------------
4 begin : October 2020
5 copyright : (C) 2020 by Peter Petrik
6 email : zilolv at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgis.h"
21
23
25 : mName( name )
26 , mType( type )
27{
28 updateSize();
29}
30
32{
33 switch ( mType )
34 {
35 case DataType::UChar:
36 case DataType::Char:
37 case DataType::Short:
40 case DataType::Int32:
41 return QMetaType::Type::Int;
42
44 case DataType::Int64:
45 return QMetaType::Type::LongLong;
46 case DataType::Float:
48 return QMetaType::Type::Double;
49 }
50 return QMetaType::Type::UnknownType;
51}
52
54{
55 switch ( mType )
56 {
57 case DataType::UChar:
58 return QObject::tr( "Unsigned Character" );
59 case DataType::Char:
60 return QObject::tr( "Character" );
61 case DataType::Short:
62 return QObject::tr( "Short" );
64 return QObject::tr( "Unsigned Short" );
65 case DataType::Float:
66 return QObject::tr( "Float" );
67 case DataType::Int32:
68 return QObject::tr( "Integer" );
70 return QObject::tr( "Unsigned Integer" );
71 case DataType::Int64:
72 return QObject::tr( "Long Integer" );
74 return QObject::tr( "Unsigned Long Integer" );
76 return QObject::tr( "Double" );
77 }
78 return QString();
79}
80
82{
83 switch ( type )
84 {
85 case DataType::UChar:
86 case DataType::Char:
87 return false;
88 case DataType::Short:
90 case DataType::Int32:
92 case DataType::Int64:
94 case DataType::Float:
96 return true;
97 }
98 return false;
99}
100
101void QgsPointCloudAttribute::updateSize()
102{
103 switch ( mType )
104 {
105 case DataType::UChar:
106 case DataType::Char:
107 mSize = 1;
108 break;
109 case DataType::Short:
110 case DataType::UShort:
111 mSize = 2;
112 break;
113 case DataType::Float:
114 mSize = 4;
115 break;
116 case DataType::Int32:
117 case DataType::UInt32:
118 mSize = 4;
119 break;
120 case DataType::Int64:
121 case DataType::UInt64:
122 mSize = 8;
123 break;
124 case DataType::Double:
125 mSize = 8;
126 break;
127 }
128}
129
130// //////////////////
131
133
135{
136 mAttributes.reserve( attributes.size() );
137 for ( const QgsPointCloudAttribute &attribute : attributes )
138 {
139 push_back( attribute );
140 }
141}
142
144{
145 mCachedAttributes.insert( attribute.name().toUpper(), CachedAttributeData( mAttributes.size(), mSize ) );
146 mAttributes.push_back( attribute );
147 mSize += attribute.size();
148}
149
150void QgsPointCloudAttributeCollection::extend( const QgsPointCloudAttributeCollection &otherCollection, const QSet<QString> &matchingNames )
151{
152 for ( const auto &attributeName : matchingNames )
153 {
154 if ( indexOf( attributeName ) == -1 )
155 {
156 int offset;
157 const auto attr = otherCollection.find( attributeName, offset );
158 if ( attr )
159 push_back( *attr );
160 }
161 }
162}
163
164QVector<QgsPointCloudAttribute> QgsPointCloudAttributeCollection::attributes() const
165{
166 return mAttributes;
167}
168
169const QgsPointCloudAttribute *QgsPointCloudAttributeCollection::find( const QString &attributeName, int &offset ) const
170{
171 const auto it = mCachedAttributes.constFind( attributeName.toUpper() );
172 if ( it != mCachedAttributes.constEnd() )
173 {
174 offset = it->offset;
175 return &mAttributes.at( it->index );
176 }
177
178 // not found
179 return nullptr;
180}
181
182int QgsPointCloudAttributeCollection::indexOf( const QString &name ) const
183{
184 const auto it = mCachedAttributes.constFind( name.toUpper() );
185 if ( it != mCachedAttributes.constEnd() )
186 {
187 return it->index;
188 }
189
190 // not found
191 return -1;
192}
193
195{
196 QgsFields fields;
197 for ( const QgsPointCloudAttribute &attribute : mAttributes )
198 {
199 fields.append( QgsField( attribute.name(), attribute.variantType(), attribute.displayType() ) );
200 }
201 return fields;
202}
203
204template<typename T> void _attribute( const char *data, std::size_t offset, QgsPointCloudAttribute::DataType type, T &value )
205{
206 switch ( type )
207 {
209 value = *reinterpret_cast< const unsigned char * >( data + offset );
210 return;
211
213 value = *( data + offset );
214 return;
215
217 value = *reinterpret_cast< const quint32 * >( data + offset );
218 return;
219
221 value = *reinterpret_cast< const qint32 * >( data + offset );
222 return;
223
225 value = *reinterpret_cast< const quint64 * >( data + offset );
226 return;
227
229 value = *reinterpret_cast< const qint64 * >( data + offset );
230 return;
231
233 value = *reinterpret_cast< const short * >( data + offset );
234 return;
235
237 value = *reinterpret_cast< const unsigned short * >( data + offset );
238 return;
239
241 value = static_cast< T >( *reinterpret_cast< const float * >( data + offset ) );
242 return;
243
245 value = *reinterpret_cast< const double * >( data + offset );
246 return;
247 }
249}
250
251double QgsPointCloudAttribute::convertValueToDouble( const char *ptr ) const
252{
253 double val;
254 _attribute( ptr, 0, mType, val );
255 return val;
256}
257
259 const char *ptr,
260 int i,
261 std::size_t pointRecordSize,
262 int xOffset,
264 int yOffset,
266 int zOffset,
268 const QgsVector3D &indexScale,
269 const QgsVector3D &indexOffset,
270 double &x,
271 double &y,
272 double &z
273)
274{
275 _attribute( ptr, i * pointRecordSize + xOffset, xType, x );
276 x = indexOffset.x() + indexScale.x() * x;
277
278 _attribute( ptr, i * pointRecordSize + yOffset, yType, y );
279 y = indexOffset.y() + indexScale.y() * y;
280
281 _attribute( ptr, i * pointRecordSize + zOffset, zType, z );
282 z = indexOffset.z() + indexScale.z() * z;
283}
284
285QVariantMap QgsPointCloudAttribute::getAttributeMap( const char *data, std::size_t recordOffset, const QgsPointCloudAttributeCollection &attributeCollection )
286{
287 QVariantMap map;
288 const QVector<QgsPointCloudAttribute> attributes = attributeCollection.attributes();
289 for ( const QgsPointCloudAttribute &attr : attributes )
290 {
291 const QString attributeName = attr.name();
292 int attributeOffset;
293 attributeCollection.find( attributeName, attributeOffset );
294 switch ( attr.type() )
295 {
297 {
298 const unsigned char value = *reinterpret_cast< const unsigned char * >( data + recordOffset + attributeOffset );
299 map[attributeName] = value;
300 }
301 break;
302
304 {
305 const char value = *( data + recordOffset + attributeOffset );
306 map[attributeName] = value;
307 }
308 break;
309
311 {
312 const quint32 value = *reinterpret_cast< const quint32 * >( data + recordOffset + attributeOffset );
313 map[attributeName] = value;
314 }
315 break;
317 {
318 const qint32 value = *reinterpret_cast< const qint32 * >( data + recordOffset + attributeOffset );
319 map[attributeName] = value;
320 }
321 break;
322
324 {
325 const quint64 value = *reinterpret_cast< const quint64 * >( data + recordOffset + attributeOffset );
326 map[attributeName] = value;
327 }
328 break;
330 {
331 const qint64 value = *reinterpret_cast< const qint64 * >( data + recordOffset + attributeOffset );
332 map[attributeName] = value;
333 }
334 break;
335
337 {
338 const short value = *reinterpret_cast< const short * >( data + recordOffset + attributeOffset );
339 map[attributeName] = value;
340 }
341 break;
342
344 {
345 const unsigned short value = *reinterpret_cast< const unsigned short * >( data + recordOffset + attributeOffset );
346 map[attributeName] = value;
347 }
348 break;
349
351 {
352 const float value = *reinterpret_cast< const float * >( data + recordOffset + attributeOffset );
353 map[attributeName] = value;
354 }
355 break;
356
358 {
359 const double value = *reinterpret_cast< const double * >( data + recordOffset + attributeOffset );
360 map[attributeName] = value;
361 }
362 break;
363 }
364 }
365 return map;
366}
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:75
A collection of point cloud attributes.
void push_back(const QgsPointCloudAttribute &attribute)
Adds extra attribute.
const QgsPointCloudAttribute * find(const QString &attributeName, int &offset) const
Finds the attribute with the name.
QgsFields toFields() const
Converts the attribute collection to an equivalent QgsFields collection.
void extend(const QgsPointCloudAttributeCollection &otherCollection, const QSet< QString > &matchingNames)
Adds specific missing attributes from another QgsPointCloudAttributeCollection.
QVector< QgsPointCloudAttribute > attributes() const
Returns all attributes.
int indexOf(const QString &name) const
Returns the index of the attribute with the specified name.
Attribute for point cloud data pair of name and size in bytes.
QMetaType::Type variantType() const
Returns the most suitable equivalent QVariant data type to this attribute type.
DataType
Systems of unit measurement.
@ UShort
Unsigned short int 2 bytes.
@ UChar
Unsigned char 1 byte.
@ UInt32
Unsigned int32 4 bytes.
@ UInt64
Unsigned int64 8 bytes.
static void getPointXYZ(const char *ptr, int i, std::size_t pointRecordSize, int xOffset, QgsPointCloudAttribute::DataType xType, int yOffset, QgsPointCloudAttribute::DataType yType, int zOffset, QgsPointCloudAttribute::DataType zType, const QgsVector3D &indexScale, const QgsVector3D &indexOffset, double &x, double &y, double &z)
Retrieves the x, y, z values for the point at index i.
static bool isNumeric(DataType type)
Returns true if the specified data type is numeric.
static QVariantMap getAttributeMap(const char *data, std::size_t recordOffset, const QgsPointCloudAttributeCollection &attributeCollection)
Retrieves all the attributes of a point.
QString displayType() const
Returns the type to use when displaying this field.
int size() const
Returns size of the attribute in bytes.
QString name() const
Returns name of the attribute.
DataType type() const
Returns the data type.
double convertValueToDouble(const char *ptr) const
Returns the attribute's value as a double for data pointed to by ptr.
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
Definition qgsvector3d.h:33
double y() const
Returns Y coordinate.
Definition qgsvector3d.h:60
double z() const
Returns Z coordinate.
Definition qgsvector3d.h:62
double x() const
Returns X coordinate.
Definition qgsvector3d.h:58
#define BUILTIN_UNREACHABLE
Definition qgis.h:7540
void _attribute(const char *data, std::size_t offset, QgsPointCloudAttribute::DataType type, T &value)