Skip to content

Commit

Permalink
remove code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
NEDJIMAbelgacem authored and wonder-sk committed Jan 13, 2021
1 parent 0167d07 commit ec2fe64
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 226 deletions.
Expand Up @@ -9,6 +9,8 @@





class QgsPointCloudAttribute
{
%Docstring
Expand Down Expand Up @@ -77,6 +79,19 @@ Returns ``True`` if the specified data ``type`` is numeric.
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

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 );
%Docstring
Retrieves the x, y, z values for the point at index ``i``.
%End

static QVariantMap getAttributeMap( const char *data, std::size_t recordOffset, const QgsPointCloudAttributeCollection &attributeCollection );
%Docstring
Retrieves all the attributes of a point
%End

};

class QgsPointCloudAttributeCollection
Expand Down
115 changes: 2 additions & 113 deletions src/app/3d/qgs3dmaptoolidentify.cpp
Expand Up @@ -86,117 +86,6 @@ void Qgs3DMapToolIdentify::mousePressEvent( QMouseEvent *event )
identifyTool2D->clearResults();
}

template <typename T>
void _attribute( const char *data, std::size_t offset, QgsPointCloudAttribute::DataType type, T &value )
{
switch ( type )
{
case QgsPointCloudAttribute::Char:
value = *( data + offset );
break;

case QgsPointCloudAttribute::Int32:
value = *reinterpret_cast< const qint32 * >( data + offset );
break;

case QgsPointCloudAttribute::Short:
{
value = *reinterpret_cast< const short * >( data + offset );
}
break;

case QgsPointCloudAttribute::UShort:
value = *reinterpret_cast< const unsigned short * >( data + offset );
break;

case QgsPointCloudAttribute::Float:
value = static_cast< T >( *reinterpret_cast< const float * >( data + offset ) );
break;

case QgsPointCloudAttribute::Double:
value = *reinterpret_cast< const double * >( data + offset );
break;
}
}

/**
* Retrieves the x, y, z values for the point at index \a i.
*/
static void _pointXYZ( 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 )
{
_attribute( ptr, i * pointRecordSize + xOffset, xType, x );
x = indexOffset.x() + indexScale.x() * x;

_attribute( ptr, i * pointRecordSize + yOffset, yType, y );
y = indexOffset.y() + indexScale.y() * y;

_attribute( ptr, i * pointRecordSize + zOffset, zType, z );
z = indexOffset.z() + indexScale.z() * z;
}

/**
* Retrieves all the attributes of a point
*/
QVariantMap _attributeMap( const char *data, std::size_t recordOffset, const QgsPointCloudAttributeCollection &attributeCollection )
{
QVariantMap map;
const QVector<QgsPointCloudAttribute> attributes = attributeCollection.attributes();
for ( const QgsPointCloudAttribute &attr : attributes )
{
QString attributeName = attr.name();
int attributeOffset;
attributeCollection.find( attributeName, attributeOffset );
switch ( attr.type() )
{
case QgsPointCloudAttribute::Char:
{
const char value = *( data + recordOffset + attributeOffset );
map[ attributeName ] = value;
}
break;

case QgsPointCloudAttribute::Int32:
{
const qint32 value = *reinterpret_cast< const qint32 * >( data + recordOffset + attributeOffset );
map[ attributeName ] = value;
}
break;

case QgsPointCloudAttribute::Short:
{
const short value = *reinterpret_cast< const short * >( data + recordOffset + attributeOffset );
map[ attributeName ] = value;
}
break;

case QgsPointCloudAttribute::UShort:
{
const unsigned short value = *reinterpret_cast< const unsigned short * >( data + recordOffset + attributeOffset );
map[ attributeName ] = value;
}
break;

case QgsPointCloudAttribute::Float:
{
const float value = *reinterpret_cast< const float * >( data + recordOffset + attributeOffset );
map[ attributeName ] = value;
}
break;

case QgsPointCloudAttribute::Double:
{
const double value = *reinterpret_cast< const double * >( data + recordOffset + attributeOffset );
map[ attributeName ] = value;
}
break;
}
}
return map;
}

void Qgs3DMapToolIdentify::mouseReleaseEvent( QMouseEvent *event )
{
if ( event->button() != Qt::MouseButton::LeftButton )
Expand Down Expand Up @@ -278,7 +167,7 @@ void Qgs3DMapToolIdentify::mouseReleaseEvent( QMouseEvent *event )
for ( int i = 0; i < block->pointCount(); ++i )
{
double x, y, z;
_pointXYZ( ptr, i, recordSize, xOffset, xType, yOffset, yType, zOffset, zType, index->scale(), index->offset(), x, y, z );
QgsPointCloudAttribute::getPointXYZ( ptr, i, recordSize, xOffset, xType, yOffset, yType, zOffset, zType, index->scale(), index->offset(), x, y, z );
QVector3D point( x, y, z );

// check whether point is in front of the ray
Expand All @@ -290,7 +179,7 @@ void Qgs3DMapToolIdentify::mouseReleaseEvent( QMouseEvent *event )
continue;

// Note : applying elevation properties is done in fromPointCloudIdentificationToIdentifyResults
QVariantMap pointAttr = _attributeMap( ptr, i * recordSize, blockAttributes );
QVariantMap pointAttr = QgsPointCloudAttribute::getAttributeMap( ptr, i * recordSize, blockAttributes );
pointAttr[ QStringLiteral( "X" ) ] = x;
pointAttr[ QStringLiteral( "Y" ) ] = y;
pointAttr[ QStringLiteral( "Z" ) ] = z;
Expand Down
105 changes: 105 additions & 0 deletions src/core/pointcloud/qgspointcloudattribute.cpp
Expand Up @@ -135,3 +135,108 @@ int QgsPointCloudAttributeCollection::indexOf( const QString &name ) const
// not found
return -1;
}

template <typename T>
void _attribute( const char *data, std::size_t offset, QgsPointCloudAttribute::DataType type, T &value )
{
switch ( type )
{
case QgsPointCloudAttribute::Char:
value = *( data + offset );
break;

case QgsPointCloudAttribute::Int32:
value = *reinterpret_cast< const qint32 * >( data + offset );
break;

case QgsPointCloudAttribute::Short:
{
value = *reinterpret_cast< const short * >( data + offset );
}
break;

case QgsPointCloudAttribute::UShort:
value = *reinterpret_cast< const unsigned short * >( data + offset );
break;

case QgsPointCloudAttribute::Float:
value = static_cast< T >( *reinterpret_cast< const float * >( data + offset ) );
break;

case QgsPointCloudAttribute::Double:
value = *reinterpret_cast< const double * >( data + offset );
break;
}
}

void QgsPointCloudAttribute::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 )
{
_attribute( ptr, i * pointRecordSize + xOffset, xType, x );
x = indexOffset.x() + indexScale.x() * x;

_attribute( ptr, i * pointRecordSize + yOffset, yType, y );
y = indexOffset.y() + indexScale.y() * y;

_attribute( ptr, i * pointRecordSize + zOffset, zType, z );
z = indexOffset.z() + indexScale.z() * z;
}

QVariantMap QgsPointCloudAttribute::getAttributeMap( const char *data, std::size_t recordOffset, const QgsPointCloudAttributeCollection &attributeCollection )
{
QVariantMap map;
const QVector<QgsPointCloudAttribute> attributes = attributeCollection.attributes();
for ( const QgsPointCloudAttribute &attr : attributes )
{
QString attributeName = attr.name();
int attributeOffset;
attributeCollection.find( attributeName, attributeOffset );
switch ( attr.type() )
{
case QgsPointCloudAttribute::Char:
{
const char value = *( data + recordOffset + attributeOffset );
map[ attributeName ] = value;
}
break;

case QgsPointCloudAttribute::Int32:
{
const qint32 value = *reinterpret_cast< const qint32 * >( data + recordOffset + attributeOffset );
map[ attributeName ] = value;
}
break;

case QgsPointCloudAttribute::Short:
{
const short value = *reinterpret_cast< const short * >( data + recordOffset + attributeOffset );
map[ attributeName ] = value;
}
break;

case QgsPointCloudAttribute::UShort:
{
const unsigned short value = *reinterpret_cast< const unsigned short * >( data + recordOffset + attributeOffset );
map[ attributeName ] = value;
}
break;

case QgsPointCloudAttribute::Float:
{
const float value = *reinterpret_cast< const float * >( data + recordOffset + attributeOffset );
map[ attributeName ] = value;
}
break;

case QgsPointCloudAttribute::Double:
{
const double value = *reinterpret_cast< const double * >( data + recordOffset + attributeOffset );
map[ attributeName ] = value;
}
break;
}
}
return map;
}
17 changes: 17 additions & 0 deletions src/core/pointcloud/qgspointcloudattribute.h
Expand Up @@ -23,6 +23,10 @@
#include <QString>
#include <QVector>

#include "qgsvector3d.h"

class QgsPointCloudAttributeCollection;

/**
* \ingroup core
*
Expand Down Expand Up @@ -81,6 +85,19 @@ class CORE_EXPORT QgsPointCloudAttribute
% End
#endif

/**
* Retrieves the x, y, z values for the point at index \a i.
*/
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 all the attributes of a point
*/
static QVariantMap getAttributeMap( const char *data, std::size_t recordOffset, const QgsPointCloudAttributeCollection &attributeCollection );

private:
void updateSize();

Expand Down

0 comments on commit ec2fe64

Please sign in to comment.