Skip to content

Commit

Permalink
Improved the code according to Nyall's suggessions
Browse files Browse the repository at this point in the history
  • Loading branch information
NEDJIMAbelgacem committed Jul 5, 2020
1 parent 35b9132 commit 94a25d2
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 92 deletions.
2 changes: 1 addition & 1 deletion src/3d/CMakeLists.txt
Expand Up @@ -69,7 +69,7 @@ SET(QGIS_3D_SRCS
mesh/qgsmesh3dmaterial_p.cpp
mesh/qgsmeshterraingenerator.cpp
qgs3dsceneexporter.cpp
qgsexportobject.cpp
qgs3dexportobject.cpp
)

SET(QGIS_3D_HDRS
Expand Down
14 changes: 7 additions & 7 deletions src/3d/qgsexportobject.cpp → src/3d/qgs3dexportobject.cpp
@@ -1,5 +1,5 @@
/***************************************************************************
qgsexportobject.cpp
Qgs3DExportObject.cpp
--------------------------------------
Date : June 2020
Copyright : (C) 2020 by Belgacem Nedjima
Expand All @@ -13,19 +13,19 @@
* *
***************************************************************************/

#include "qgsexportobject.h"
#include "qgs3dexportobject.h"

#include <QVector3D>

QgsExportObject::QgsExportObject( const QString &name, const QString &parentName, QObject *parent )
Qgs3DExportObject::Qgs3DExportObject( const QString &name, const QString &parentName, QObject *parent )
: QObject( parent )
, mName( name )
, mParentName( parentName )
, mSmoothEdges( false )
{
}

void QgsExportObject::setupPositionCoordinates( const QVector<float> &positionsBuffer, float scale, const QVector3D translation )
void Qgs3DExportObject::setupPositionCoordinates( const QVector<float> &positionsBuffer, float scale, const QVector3D translation )
{
for ( int i = 0; i < positionsBuffer.size(); i += 3 )
{
Expand All @@ -41,7 +41,7 @@ void QgsExportObject::setupPositionCoordinates( const QVector<float> &positionsB
}
}

void QgsExportObject::setupPositionCoordinates( const QVector<float> &positionsBuffer, const QVector<unsigned int> &faceIndex, float scale, const QVector3D translation )
void Qgs3DExportObject::setupPositionCoordinates( const QVector<float> &positionsBuffer, const QVector<unsigned int> &faceIndex, float scale, const QVector3D translation )
{
// TODO: delete vertices that are not used
for ( int i = 0; i < positionsBuffer.size(); i += 3 )
Expand All @@ -59,7 +59,7 @@ void QgsExportObject::setupPositionCoordinates( const QVector<float> &positionsB
}
}

void QgsExportObject::objectBounds( float &minX, float &minY, float &minZ, float &maxX, float &maxY, float &maxZ )
void Qgs3DExportObject::objectBounds( float &minX, float &minY, float &minZ, float &maxX, float &maxY, float &maxZ )
{
for ( int vertice : mIndexes )
{
Expand All @@ -73,7 +73,7 @@ void QgsExportObject::objectBounds( float &minX, float &minY, float &minZ, float
}
}

void QgsExportObject::saveTo( QTextStream &out, int scale, const QVector3D &center )
void Qgs3DExportObject::saveTo( QTextStream &out, int scale, const QVector3D &center )
{

// Set object name
Expand Down
24 changes: 13 additions & 11 deletions src/3d/qgsexportobject.h → src/3d/qgs3dexportobject.h
@@ -1,5 +1,5 @@
/***************************************************************************
qgsexportobject.h
Qgs3DExportObject.h
--------------------------------------
Date : June 2020
Copyright : (C) 2020 by Belgacem Nedjima
Expand All @@ -13,36 +13,36 @@
* *
***************************************************************************/

#ifndef QGSEXPORTOBJECT_H
#define QGSEXPORTOBJECT_H
#ifndef Qgs3DExportObject_H
#define Qgs3DExportObject_H

#include <QObject>
#include <QTextStream>
#include <QVector>
#include <QVector3D>

/**
* @brief The QgsExportObject class
* @brief The Qgs3DExportObject class
* Manages the data of each object of the scene (positions, normals, texture coordinates ...) since each object
* \ingroup 3d
* \since QGIS 3.16
*/
class QgsExportObject : public QObject
class Qgs3DExportObject : public QObject
{
Q_OBJECT
public:

/**
* @brief QgsExportObject
* @brief Qgs3DExportObject
* Constructs an export object that will be filled with coordinates later
* @param name
* The name of the object (the user will be able to select each object individually using its name in blender)
* @param parentName
* The name of the parent (Will be useful to define scene hierarchie)
* @param parent
* The parent QObject (we use this to delete the QgsExportObject instance once the exporter instance is deallocated)
* The parent QObject (we use this to delete the Qgs3DExportObject instance once the exporter instance is deallocated)
*/
QgsExportObject( const QString &name, const QString &parentName = QString(), QObject *parent = nullptr );
Qgs3DExportObject( const QString &name, const QString &parentName = QString(), QObject *parent = nullptr );

//! Returns whether object edges will look smooth
bool smoothEdges() { return mSmoothEdges; }
Expand All @@ -53,8 +53,10 @@ class QgsExportObject : public QObject
void setupPositionCoordinates( const QVector<float> &positionsBuffer, float scale = 1.0f, const QVector3D translation = QVector3D( 0, 0, 0 ) );
//! Sets positions coordinates from just one positions buffer and indexes buffer and does the translation and scaling
void setupPositionCoordinates( const QVector<float> &positionsBuffer, const QVector<unsigned int> &facesIndexes, float scale = 1.0f, const QVector3D translation = QVector3D( 0, 0, 0 ) );
//! Updates the box bounds explained with the current object bounds
//! This expands the bounding box if the current object outside the bounds of the already established bounds
/**
* Updates the box bounds explained with the current object bounds
* This expands the bounding box if the current object outside the bounds of the already established bounds
*/
void objectBounds( float &minX, float &minY, float &minZ, float &maxX, float &maxY, float &maxZ );

//! Saves the current object to the output stream while scaling the object and centering it to be visible in exported scene
Expand All @@ -68,4 +70,4 @@ class QgsExportObject : public QObject
bool mSmoothEdges;
};

#endif // QGSEXPORTOBJECT_H
#endif // Qgs3DExportObject_H
10 changes: 0 additions & 10 deletions src/3d/qgs3dmapexportsettings.cpp
Expand Up @@ -14,13 +14,3 @@
***************************************************************************/

#include "qgs3dmapexportsettings.h"

Qgs3DMapExportSettings::Qgs3DMapExportSettings( QObject *parent )
: QObject( parent )
, mSceneName( "Scene" )
, mSceneFolderPath( "~/" )
, mTerrainResolution( 128 )
, mSmoothEdges( false )
{

}
16 changes: 11 additions & 5 deletions src/3d/qgs3dmapexportsettings.h
Expand Up @@ -20,6 +20,7 @@

#include <QString>
#include <QObject>
#include <QDir>

/**
* @brief The Qgs3DMapExportSettings class
Expand All @@ -32,7 +33,7 @@ class _3D_EXPORT Qgs3DMapExportSettings : public QObject
Q_OBJECT
public:
//! Constructor
Qgs3DMapExportSettings( QObject *parent = nullptr );
Qgs3DMapExportSettings( QObject *parent = nullptr ): QObject( parent ) { }

//! Returns the scene name
QString sceneName() const { return mSceneName; }
Expand All @@ -51,11 +52,16 @@ class _3D_EXPORT Qgs3DMapExportSettings : public QObject
void setTerrainResolution( int resolution ) { mTerrainResolution = resolution; }
//! Sets whether triangles edges will look smooth
void setSmoothEdges( bool smoothEdges ) { mSmoothEdges = smoothEdges; }
//! Returns a path to a file that with the "name.extension" in the export folder
QString getFilePath( const QString &name, const QString &extension ) const
{
return QDir( mSceneFolderPath ).filePath( name + "." + extension );
}
private:
QString mSceneName;
QString mSceneFolderPath;
int mTerrainResolution;
bool mSmoothEdges;
QString mSceneName = QString( "Scene" );
QString mSceneFolderPath = QDir::homePath();
int mTerrainResolution = 128;
bool mSmoothEdges = false;
};

#endif // QGS3DMAPEXPORTSETTINGS_H
5 changes: 1 addition & 4 deletions src/3d/qgs3dmapscene.cpp
Expand Up @@ -780,9 +780,6 @@ void Qgs3DMapScene::exportScene( const Qgs3DMapExportSettings &exportSettings )
}
exporter.parseEntity( mTerrain );

// TODO: make the scene .obj file created using a function in Qgs3DMapExportSettings
QString filePath = exportSettings.sceneFolderPath();
if ( !filePath.endsWith( '/' ) ) filePath.push_back( '/' );
filePath += exportSettings.sceneName() + ".obj";
QString filePath = exportSettings.getFilePath( exportSettings.sceneName(), "obj" );
exporter.saveToFile( filePath );
}
14 changes: 7 additions & 7 deletions src/3d/qgs3dsceneexporter.cpp
Expand Up @@ -40,7 +40,7 @@
#include "qgsdemterraingenerator.h"
#include "qgsdemterraintileloader_p.h"
#include "qgsdemterraintilegeometry_p.h"
#include "qgsexportobject.h"
#include "qgs3dexportobject.h"

#include <numeric>

Expand Down Expand Up @@ -403,7 +403,7 @@ void Qgs3DSceneExporter::parseFlatTile( QgsTerrainTileEntity *tileEntity )
QVector<float> positionBuffer = createPlaneVertexData( scale, scale, tileGeometry->resolution() );
QVector<unsigned int> indexesBuffer = createPlaneIndexData( tileGeometry->resolution() );

QgsExportObject *object = new QgsExportObject( "Flat_tile", "", this );
Qgs3DExportObject *object = new Qgs3DExportObject( "Flat_tile", "", this );
mObjects.push_back( object );

object->setSmoothEdges( mSmoothEdges );
Expand Down Expand Up @@ -443,7 +443,7 @@ void Qgs3DSceneExporter::parseDemTile( QgsTerrainTileEntity *tileEntity )
QVector<float> positionBuffer = getAttributeData<float>( tileGeometry->positionAttribute() );
QVector<unsigned int> indexBuffer = getAttributeData<unsigned int>( tileGeometry->indexAttribute() );

QgsExportObject *object = new QgsExportObject( "DEM_tile", "", this );
Qgs3DExportObject *object = new Qgs3DExportObject( "DEM_tile", "", this );
mObjects.push_back( object );

object->setSmoothEdges( mSmoothEdges );
Expand All @@ -454,7 +454,7 @@ void Qgs3DSceneExporter::processAttribute( Qt3DRender::QAttribute *attribute )
{
QVector<float> floatData = getAttributeData<float>( attribute );

QgsExportObject *object = new QgsExportObject( "attribute", "", this );
Qgs3DExportObject *object = new Qgs3DExportObject( "attribute", "", this );
mObjects.push_back( object );

mObjects.push_back( object );
Expand All @@ -470,12 +470,12 @@ void Qgs3DSceneExporter::process( QgsTessellatedPolygonGeometry *geom )
void Qgs3DSceneExporter::saveToFile( const QString &filePath )
{
QFile file( filePath );
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
return;

float maxfloat = std::numeric_limits<float>::max(), minFloat = std::numeric_limits<float>::lowest();
float minX = maxfloat, minY = maxfloat, minZ = maxfloat, maxX = minFloat, maxY = minFloat, maxZ = minFloat;
for ( QgsExportObject *obj : mObjects ) obj->objectBounds( minX, minY, minZ, maxX, maxY, maxZ );
for ( Qgs3DExportObject *obj : mObjects ) obj->objectBounds( minX, minY, minZ, maxX, maxY, maxZ );

float diffX = 1.0f, diffY = 1.0f, diffZ = 1.0f;
diffX = maxX - minX;
Expand All @@ -489,5 +489,5 @@ void Qgs3DSceneExporter::saveToFile( const QString &filePath )
float scale = std::min( diffX, std::min( diffY, diffZ ) );

QTextStream out( &file );
for ( QgsExportObject *obj : mObjects ) obj->saveTo( out, scale, QVector3D( centerX, centerY, centerZ ) );
for ( Qgs3DExportObject *obj : mObjects ) obj->saveTo( out, scale, QVector3D( centerX, centerY, centerZ ) );
}
12 changes: 6 additions & 6 deletions src/3d/qgs3dsceneexporter.h
Expand Up @@ -29,7 +29,7 @@ class Qgs3DMapSettings;
class QgsFlatTerrainGenerator;
class QgsDemTerrainGenerator;
class QgsChunkNode;
class QgsExportObject;
class Qgs3DExportObject;

/**
* @brief The Qgs3DSceneExporter class
Expand Down Expand Up @@ -65,19 +65,19 @@ class Qgs3DSceneExporter : public Qt3DCore::QEntity
int terrainResolution() { return mTerrainResolution; }

private:
//! Processes the attribute directly by taking a position buffer and converting it to QgsExportObject
//! Processes the attribute directly by taking a position buffer and converting it to Qgs3DExportObject
void processAttribute( Qt3DRender::QAttribute *attribute );
//! Processes the tessellated polygons geometry and constructs QgsExportObject from it
//! Processes the tessellated polygons geometry and constructs Qgs3DExportObject from it
void process( QgsTessellatedPolygonGeometry *geom );

//! Returns a tile entity that contains the geometry to be exported and necessary scaling parameters
QgsTerrainTileEntity *getFlatTerrainEntity( QgsTerrainEntity *terrain, QgsChunkNode *node );
//! Returns a tile entity that contains the geometry to be exported and necessary scaling parameters
QgsTerrainTileEntity *getDemTerrainEntity( QgsTerrainEntity *terrain, QgsChunkNode *node );

//! Constructs a QgsExportObject from the DEM tile entity
//! Constructs a Qgs3DExportObject from the DEM tile entity
void parseDemTile( QgsTerrainTileEntity *tileEntity );
//! Constructs a QgsExportObject from the flat tile entity
//! Constructs a Qgs3DExportObject from the flat tile entity
void parseFlatTile( QgsTerrainTileEntity *tileEntity );

/**
Expand All @@ -86,7 +86,7 @@ class Qgs3DSceneExporter : public Qt3DCore::QEntity
*/
QgsTerrainTileEntity *createDEMTileEntity( QgsTerrainEntity *terrain, QgsChunkNode *node );
private:
QVector<QgsExportObject *> mObjects;
QVector<Qgs3DExportObject *> mObjects;

bool mSmoothEdges;
int mTerrainResolution;
Expand Down
14 changes: 4 additions & 10 deletions src/app/3d/qgsmap3dexportwidget.cpp
Expand Up @@ -34,18 +34,12 @@ QgsMap3DExportWidget::QgsMap3DExportWidget( Qgs3DMapScene *scene, Qgs3DMapExport
ui->setupUi( this );

QgsSettings settings;
ui->selectFolderWidget->setStorageMode( QgsFileWidget::StorageMode::GetDirectory );
QString initialPath = settings.value( QStringLiteral( "UI/last3DSceneExportDir" ), QDir::homePath() ).toString();
ui->folderPathLineEdit->setText( initialPath );

connect( ui->selectFolderBtn, &QPushButton::clicked, [ = ]( bool )
{
QString initialPath = ui->folderPathLineEdit->text();
QString outputDir = QFileDialog::getExistingDirectory( this, QString( "Export scane" ), initialPath );
ui->folderPathLineEdit->setText( outputDir );
} );
ui->selectFolderWidget->setDefaultRoot( initialPath );

connect( ui->sceneNameLineEdit, &QLineEdit::textChanged, [ = ]( const QString & ) { settingsChanged(); } );
connect( ui->folderPathLineEdit, &QLineEdit::textChanged, [ = ]( const QString & ) { settingsChanged(); } );
connect( ui->selectFolderWidget, &QgsFileWidget::fileChanged, [ = ]( const QString & ) { settingsChanged(); } );
connect( ui->smoothEdgesCheckBox, &QCheckBox::stateChanged, [ = ]( int ) { settingsChanged(); } );
connect( ui->terrainResolutionSpinBox, qgis::overload<int>::of( &QSpinBox::valueChanged ), [ = ]( int ) { settingsChanged(); } );

Expand All @@ -61,7 +55,7 @@ QgsMap3DExportWidget::~QgsMap3DExportWidget()
void QgsMap3DExportWidget::settingsChanged()
{
mExportSettings->setSceneName( ui->sceneNameLineEdit->text() );
mExportSettings->setSceneFolderPath( ui->folderPathLineEdit->text() );
mExportSettings->setSceneFolderPath( ui->selectFolderWidget->filePath() );
mExportSettings->setTerrainResolution( ui->terrainResolutionSpinBox->value() );
mExportSettings->setSmoothEdges( ui->smoothEdgesCheckBox->isChecked() );
}
Expand Down

0 comments on commit 94a25d2

Please sign in to comment.