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