28#include <Qt3DCore/QAttribute>
29#include <Qt3DCore/QBuffer>
31using namespace Qt::StringLiterals;
33void Qgs3DExportObject::setupPositionCoordinates(
const QVector<float> &positionsBuffer,
const QMatrix4x4 &transform )
35 mVertexPosition.clear();
36 for (
int i = 0; i < positionsBuffer.size(); i += 3 )
38 const QVector3D position( positionsBuffer[i], positionsBuffer[i + 1], positionsBuffer[i + 2] );
39 const QVector3D positionFinal = transform.map( position );
40 mVertexPosition << positionFinal.x() << positionFinal.y() << positionFinal.z();
47 setupPositionCoordinates( positionsBuffer, transform );
51 for (
int i = 0; i < facesIndexes.size(); i += 3 )
53 if ( i + 2 >= facesIndexes.size() )
56 if ( facesIndexes[i] == facesIndexes[i + 1] || facesIndexes[i + 1] == facesIndexes[i + 2] || facesIndexes[i] == facesIndexes[i + 2] )
58 for (
int j = 0; j < 3; ++j )
59 mIndexes << facesIndexes[i + j];
66 setupPositionCoordinates( positionsBuffer );
70 for (
int i = 0; i < mVertexPosition.size(); i += 3 )
71 mIndexes << i / 3 + 1;
77 setupPositionCoordinates( positionsBuffer );
85 QMatrix3x3 normal3x3 = transform.normalMatrix();
87 QMatrix4x4 normal4x4( normal3x3( 0, 0 ), normal3x3( 0, 1 ), normal3x3( 0, 2 ), 0,
88 normal3x3( 1, 0 ), normal3x3( 1, 1 ), normal3x3( 1, 2 ), 0,
89 normal3x3( 2, 0 ), normal3x3( 2, 1 ), normal3x3( 2, 2 ), 0,
93 for (
int i = 0; i < normalsBuffer.size(); i += 3 )
95 const QVector3D normalVector( normalsBuffer[i], normalsBuffer[i + 1], normalsBuffer[i + 2] );
96 QVector3D v = normal4x4.mapVector( normalVector );
104 mNormals << v.x() << v.y() << v.z();
111 mTexturesUV << texturesBuffer;
116 mMaterialParameters.clear();
118 for (
auto it = parameters.begin(); it != parameters.end(); ++it )
120 mMaterialParameters[it.key()] = it.value();
128 for (
const unsigned int vertice : std::as_const( mIndexes ) )
130 const int heightIndex =
static_cast<int>( vertice ) * 3 + 1;
131 minX = std::min( minX, mVertexPosition[heightIndex - 1] );
132 maxX = std::max( maxX, mVertexPosition[heightIndex - 1] );
133 minY = std::min( minY, mVertexPosition[heightIndex] );
134 maxY = std::max( maxY, mVertexPosition[heightIndex] );
135 minZ = std::min( minZ, mVertexPosition[heightIndex + 1] );
136 maxZ = std::max( maxZ, mVertexPosition[heightIndex + 1] );
142 switch ( exportFormat )
145 saveToObj( out, scale, center, precision, materialName );
148 saveToStl( out, scale, center, precision );
155void Qgs3DExportObject::saveToObj( QTextStream &out,
float scale,
const QVector3D ¢er,
int precision, QString materialName )
const
158 out <<
"o " << mName <<
"\n";
161 if ( !materialName.isEmpty() )
162 out <<
"usemtl " << materialName <<
"\n";
166 out << qSetRealNumberPrecision( precision );
177 for (
const unsigned int vertice : std::as_const( mIndexes ) )
179 const int i =
static_cast<int>( vertice * 3 );
182 out << ( mVertexPosition[i] - center.x() ) / scale <<
" ";
183 out << ( mVertexPosition[i + 1] - center.y() ) / scale <<
" ";
184 out << ( mVertexPosition[i + 2] - center.z() ) / scale <<
"\n";
185 if ( i + 3 <= mNormals.size() )
187 out <<
"vn " << mNormals[i] <<
" " << mNormals[i + 1] <<
" " << mNormals[i + 2] <<
"\n";
189 const int u_index = i / 3 * 2;
190 if ( u_index + 1 < mTexturesUV.size() )
193 out <<
"vt " << mTexturesUV[u_index] <<
" " << 1.0f - mTexturesUV[u_index + 1] <<
"\n";
197 bool hasTextures = mTexturesUV.size() == mVertexPosition.size() / 3 * 2;
199 bool hasNormals = mNormals.size() == mVertexPosition.size();
201 if ( !hasNormals && !mNormals.empty() )
203 QgsDebugError(
"Vertex normals count and vertex positions count are different" );
205 const unsigned int verticesCount = mIndexes.size();
209 auto getVertexIndex = [&](
unsigned int i ) -> QString {
210 const int negativeIndex =
static_cast<int>( i - verticesCount );
211 if ( hasNormals && !hasTextures )
212 return u
"%1//%2"_s.arg( negativeIndex ).arg( negativeIndex );
213 if ( !hasNormals && hasTextures )
214 return u
"%1/%2"_s.arg( negativeIndex ).arg( negativeIndex );
215 if ( hasNormals && hasTextures )
216 return u
"%1/%2/%3"_s.arg( negativeIndex ).arg( negativeIndex ).arg( negativeIndex );
217 return QString::number( negativeIndex );
225 for (
int i = 0; i < mIndexes.size(); i += 3 )
227 out <<
"f " << getVertexIndex( i );
228 out <<
" " << getVertexIndex( i + 1 );
229 out <<
" " << getVertexIndex( i + 2 );
236 for (
const unsigned int i : std::as_const( mIndexes ) )
237 out <<
" " << getVertexIndex( i );
240 else if ( mType ==
Points )
243 for (
const unsigned int i : std::as_const( mIndexes ) )
244 out <<
" " << getVertexIndex( i );
249void Qgs3DExportObject::saveToStl( QTextStream &out,
float scale,
const QVector3D ¢er,
int precision )
const
253 QgsDebugMsgLevel( u
"Cannot export object %s in %s type. Only triangular type is handled by STL export"_s.arg( mName ).arg( mType ), 3 );
258 out <<
"solid " << mName <<
"\n";
260 out << qSetRealNumberPrecision( precision );
262 for (
int i = 0; i < mIndexes.size(); i += 3 )
265 unsigned int i0 = mIndexes[i] * 3;
266 unsigned int i1 = mIndexes[i + 1] * 3;
267 unsigned int i2 = mIndexes[i + 2] * 3;
269 QVector3D v0( ( mVertexPosition[i0] - center.x() ) / scale, ( mVertexPosition[i0 + 1] - center.y() ) / scale, ( mVertexPosition[i0 + 2] - center.z() ) / scale );
270 QVector3D v1( ( mVertexPosition[i1] - center.x() ) / scale, ( mVertexPosition[i1 + 1] - center.y() ) / scale, ( mVertexPosition[i1 + 2] - center.z() ) / scale );
271 QVector3D v2( ( mVertexPosition[i2] - center.x() ) / scale, ( mVertexPosition[i2 + 1] - center.y() ) / scale, ( mVertexPosition[i2 + 2] - center.z() ) / scale );
273 QVector3D normal = QVector3D::crossProduct( v1 - v0, v2 - v0 ).normalized();
275 out <<
" facet normal " << normal.x() <<
" " << normal.y() <<
" " << normal.z() <<
"\n";
276 out <<
" outer loop\n";
277 out <<
" vertex " << v0.x() <<
" " << v0.y() <<
" " << v0.z() <<
"\n";
278 out <<
" vertex " << v1.x() <<
" " << v1.y() <<
" " << v1.z() <<
"\n";
279 out <<
" vertex " << v2.x() <<
" " << v2.y() <<
" " << v2.z() <<
"\n";
281 out <<
" endfacet\n";
284 out <<
"endsolid " << mName <<
"\n";
289 QString materialName = mName +
"_material";
290 if ( mMaterialParameters.size() == 0 && ( mTexturesUV.size() == 0 || mTextureImage.isNull() ) )
292 mtlOut <<
"newmtl " << materialName <<
"\n";
293 if ( mTexturesUV.size() != 0 && !mTextureImage.isNull() )
295 const QString filePath = QDir( folderPath ).filePath( materialName +
".jpg" );
296 mTextureImage.save( filePath,
"JPG" );
297 mtlOut <<
"\tmap_Kd " << materialName <<
".jpg" <<
"\n";
299 for (
auto it = mMaterialParameters.constBegin(); it != mMaterialParameters.constEnd(); it++ )
301 mtlOut <<
"\t" << it.key() <<
" " << it.value() <<
"\n";
303 mtlOut <<
"\tillum 2\n";
Export3DSceneFormat
The file format used when exporting a 3D scene.
@ StlAscii
STL ascii format.
@ Obj
Wavefront OBJ format.
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 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.
void saveTo(QTextStream &out, float scale, const QVector3D ¢er, const Qgis::Export3DSceneFormat &exportFormat=Qgis::Export3DSceneFormat::Obj, int precision=6, QString materialName=QString()) const
Exports the 3D object to an OBJ or STL compatible output stream.
static QMap< QString, QString > toMaterialExportParameters(const QgsAbstractMaterialSettings *settings)
Returns the parameters to be exported to .mtl file.
Abstract base class for material settings.
#define BUILTIN_UNREACHABLE
bool qgsFloatNear(float a, float b, float epsilon=4 *FLT_EPSILON)
Compare two floats (but allow some difference).
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)