QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgs3dexportobject.cpp
Go to the documentation of this file.
1/***************************************************************************
2 Qgs3DExportObject.cpp
3 --------------------------------------
4 Date : June 2020
5 Copyright : (C) 2020 by Belgacem Nedjima
6 Email : gb underscore nedjima at esi dot dz
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 "qgs3dexportobject.h"
17
18#include <QVector3D>
19#include <QDir>
20#include <QImage>
21#include <QMatrix4x4>
22
23#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
24#include <Qt3DRender/QAttribute>
25#include <Qt3DRender/QBuffer>
26typedef Qt3DRender::QAttribute Qt3DQAttribute;
27typedef Qt3DRender::QBuffer Qt3DQBuffer;
28#else
29#include <Qt3DCore/QAttribute>
30#include <Qt3DCore/QBuffer>
31typedef Qt3DCore::QAttribute Qt3DQAttribute;
32typedef Qt3DCore::QBuffer Qt3DQBuffer;
33#endif
34
35#include "qgslogger.h"
37
38
39template<typename T>
40void insertIndexData( QVector<uint> &vertexIndex, const QVector<T> &faceIndex )
41{
42 for ( int i = 0; i < faceIndex.size(); i += 3 )
43 {
44 if ( i + 2 >= faceIndex.size() ) continue;
45 // skip invalid triangles
46 if ( faceIndex[i] == faceIndex[i + 1] || faceIndex[i + 1] == faceIndex[i + 2] || faceIndex[i] == faceIndex[i + 2] )
47 continue;
48 for ( int j = 0; j < 3; ++j )
49 vertexIndex << faceIndex[i + j];
50 }
51}
52
53void Qgs3DExportObject::setupPositionCoordinates( const QVector<float> &positionsBuffer, const QMatrix4x4 &transform )
54{
55 for ( int i = 0; i < positionsBuffer.size(); i += 3 )
56 {
57 const QVector3D position( positionsBuffer[i], positionsBuffer[i + 1], positionsBuffer[i + 2] );
58 const QVector3D positionFinal = transform.map( position );
59 mVertexPosition << positionFinal.x() << positionFinal.y() << positionFinal.z();
60 }
61}
62
63void Qgs3DExportObject::setupFaces( const QVector<uint> &facesIndexes )
64{
65 insertIndexData<uint>( mIndexes, facesIndexes );
66}
67
68void Qgs3DExportObject::setupLine( const QVector<uint> &lineIndexes )
69{
70 Q_UNUSED( lineIndexes );
71 for ( int i = 0; i < mVertexPosition.size(); i += 3 ) mIndexes << i / 3 + 1;
72}
73
74void Qgs3DExportObject::setupNormalCoordinates( const QVector<float> &normalsBuffer, const QMatrix4x4 &transform )
75{
76 // Qt does not provide QMatrix3x3 * QVector3D multiplication so we use QMatrix4x4
77 QMatrix3x3 normal3x3 = transform.normalMatrix();
78 QMatrix4x4 normal4x4( normal3x3( 0, 0 ), normal3x3( 0, 1 ), normal3x3( 0, 2 ), 0,
79 normal3x3( 1, 0 ), normal3x3( 1, 1 ), normal3x3( 1, 2 ), 0,
80 normal3x3( 2, 0 ), normal3x3( 2, 1 ), normal3x3( 2, 2 ), 0,
81 0, 0, 0, 1 );
82
83 for ( int i = 0; i < normalsBuffer.size(); i += 3 )
84 {
85 const QVector3D normalVector( normalsBuffer[i], normalsBuffer[i + 1], normalsBuffer[i + 2] );
86 QVector3D v = normal4x4.mapVector( normalVector );
87 // round numbers very close to zero to avoid tiny numbers like 6e-8 in export
88 if ( qgsFloatNear( v.x(), 0 ) ) v.setX( 0 );
89 if ( qgsFloatNear( v.y(), 0 ) ) v.setY( 0 );
90 if ( qgsFloatNear( v.z(), 0 ) ) v.setZ( 0 );
91 mNormals << v.x() << v.y() << v.z();
92 }
93}
94
95void Qgs3DExportObject::setupTextureCoordinates( const QVector<float> &texturesBuffer )
96{
97 mTexturesUV << texturesBuffer;
98}
99
101{
102 QMap<QString, QString> parameters = material->toExportParameters();
103 for ( auto it = parameters.begin(); it != parameters.end(); ++it )
104 {
105 setMaterialParameter( it.key(), it.value() );
106 }
107}
108
109void Qgs3DExportObject::objectBounds( float &minX, float &minY, float &minZ, float &maxX, float &maxY, float &maxZ )
110{
111 if ( mType != TriangularFaces ) return;
112 for ( const unsigned int vertice : qAsConst( mIndexes ) )
113 {
114 const int heightIndex = static_cast<int>( vertice ) * 3 + 1;
115 minX = std::min( minX, mVertexPosition[heightIndex - 1] );
116 maxX = std::max( maxX, mVertexPosition[heightIndex - 1] );
117 minY = std::min( minY, mVertexPosition[heightIndex] );
118 maxY = std::max( maxY, mVertexPosition[heightIndex] );
119 minZ = std::min( minZ, mVertexPosition[heightIndex + 1] );
120 maxZ = std::max( maxZ, mVertexPosition[heightIndex + 1] );
121 }
122}
123
124void Qgs3DExportObject::saveTo( QTextStream &out, float scale, const QVector3D &center, int precision )
125{
126 // Set groups
127 // turns out grouping doest work as expected in blender
128 out << qSetRealNumberPrecision( precision );
129
130 // smoothen edges
131 if ( mSmoothEdges )
132 out << "s on\n";
133 else
134 out << "s off\n";
135
136 // Construct vertices
137 // As we can have holes in the face list and we only write vertices from these faces
138 // then the vertex list in the obj is not the whole from mVertexPosition!
139 for ( const unsigned int vertice : qAsConst( mIndexes ) )
140 {
141 const int i = static_cast<int>( vertice * 3 );
142 // for now just ignore wrong vertex positions
143 out << "v ";
144 out << ( mVertexPosition[i] - center.x() ) / scale << " ";
145 out << ( mVertexPosition[i + 1] - center.y() ) / scale << " ";
146 out << ( mVertexPosition[i + 2] - center.z() ) / scale << "\n";
147 if ( i + 3 <= mNormals.size() )
148 {
149 out << "vn " << mNormals[i] << " " << mNormals[i + 1] << " " << mNormals[i + 2] << "\n";
150 }
151 const int u_index = i / 3 * 2;
152 if ( u_index + 1 < mTexturesUV.size() )
153 {
154 // TODO: flip texture in a more appropriate way (for repeated textures)
155 out << "vt " << mTexturesUV[u_index] << " " << 1.0f - mTexturesUV[u_index + 1] << "\n";
156 }
157 }
158
159 bool hasTextures = mTexturesUV.size() == mVertexPosition.size() / 3 * 2;
160 // if the object has normals then the normals and positions buffers should be the same size
161 bool hasNormals = mNormals.size() == mVertexPosition.size();
162
163 if ( !hasNormals && !mNormals.empty() )
164 {
165 QgsDebugError( "Vertex normals count and vertex positions count are different" );
166 }
167 const int verticesCount = mIndexes.size();
168
169 // we use negative indexes as this is the way to use relative values to reference vertex positions
170 // Positive values are absolute vertex position from the beginning of the file.
171 auto getVertexIndex = [&]( unsigned int i ) -> QString
172 {
173 const int negativeIndex = static_cast<int>( i - verticesCount );
174 if ( hasNormals && !hasTextures )
175 return QStringLiteral( "%1//%2" ).arg( negativeIndex ).arg( negativeIndex );
176 if ( !hasNormals && hasTextures )
177 return QStringLiteral( "%1/%2" ).arg( negativeIndex ).arg( negativeIndex );
178 if ( hasNormals && hasTextures )
179 return QStringLiteral( "%1/%2/%3" ).arg( negativeIndex ).arg( negativeIndex ).arg( negativeIndex );
180 return QString::number( negativeIndex );
181 };
182
183 if ( mType == TriangularFaces )
184 {
185 // Construct triangular faces
186 // As we have "compressed" the vertex/normal section above by using only the vertices referenced by the faces
187 // we do not need to the 'mIndexes[i]' value but only the 'i' value.
188 for ( int i = 0; i < mIndexes.size(); i += 3 )
189 {
190 out << "f " << getVertexIndex( i );
191 out << " " << getVertexIndex( i + 1 );
192 out << " " << getVertexIndex( i + 2 );
193 out << "\n";
194 }
195 }
196 else if ( mType == LineStrip )
197 {
198 out << "l";
199 for ( const unsigned int i : qAsConst( mIndexes ) )
200 out << " " << getVertexIndex( i );
201 out << "\n";
202 }
203 else if ( mType == Points )
204 {
205 out << "p";
206 for ( const unsigned int i : qAsConst( mIndexes ) )
207 out << " " << getVertexIndex( i );
208 out << "\n";
209 }
210}
211
212QString Qgs3DExportObject::saveMaterial( QTextStream &mtlOut, const QString &folderPath )
213{
214 QString materialName = mName + "_material";
215 if ( mMaterialParameters.size() == 0 && ( mTexturesUV.size() == 0 || mTextureImage.isNull() ) ) return QString();
216 mtlOut << "newmtl " << materialName << "\n";
217 if ( mTexturesUV.size() != 0 && !mTextureImage.isNull() )
218 {
219 const QString filePath = QDir( folderPath ).filePath( materialName + ".jpg" );
220 mTextureImage.save( filePath, "JPG" );
221 mtlOut << "\tmap_Kd " << materialName << ".jpg" << "\n";
222 }
223 for ( auto it = mMaterialParameters.constBegin(); it != mMaterialParameters.constEnd(); it++ )
224 {
225 mtlOut << "\t" << it.key() << " " << it.value() << "\n";
226 }
227 mtlOut << "\tillum 2\n";
228 return materialName;
229}
void setMaterialParameter(const QString &parameter, const QString &value)
Sets a material parameter to be exported in the .mtl file.
void setupPositionCoordinates(const QVector< float > &positionsBuffer, const QMatrix4x4 &transform)
Sets positions coordinates and does the translation, rotation and scaling.
void objectBounds(float &minX, float &minY, float &minZ, float &maxX, float &maxY, float &maxZ)
Updates the box bounds explained with the current object bounds This expands the bounding box if the ...
void saveTo(QTextStream &out, float scale, const QVector3D &center, int precision=6)
Saves the current object to the output stream while scaling the object and centering it to be visible...
void setupMaterial(QgsAbstractMaterialSettings *material)
Sets the material parameters (diffuse color, shininess...) from phong material.
void setupNormalCoordinates(const QVector< float > &normalsBuffer, const QMatrix4x4 &transform)
Sets normal coordinates for each vertex.
QString saveMaterial(QTextStream &mtlOut, const QString &folder)
saves the texture of the object and material information
void setupLine(const QVector< uint > &facesIndexes)
sets line vertex indexes
void setupFaces(const QVector< uint > &facesIndexes)
Sets the faces in facesIndexes to the faces in the object.
void setupTextureCoordinates(const QVector< float > &texturesBuffer)
Sets texture coordinates for each vertex.
virtual QMap< QString, QString > toExportParameters() const =0
Returns the parameters to be exported to .mtl file.
bool qgsFloatNear(float a, float b, float epsilon=4 *FLT_EPSILON)
Compare two floats (but allow some difference)
Definition qgis.h:5928
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
void insertIndexData(QVector< uint > &vertexIndex, const QVector< T > &faceIndex)
#define QgsDebugError(str)
Definition qgslogger.h:38
int precision