summaryrefslogtreecommitdiffstats
path: root/src/plugins/geometryloaders/default/plygeometryloader.cpp
blob: a51c991afc20979bab75efd66a6dab12910f7708 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd and/or its subsidiary(-ies).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "plygeometryloader.h"

#include <QtCore/QDataStream>
#include <QtCore/QLoggingCategory>

QT_BEGIN_NAMESPACE

namespace Qt3DRender {

Q_LOGGING_CATEGORY(PlyGeometryLoaderLog, "Qt3D.PlyGeometryLoader", QtWarningMsg)

namespace {

class PlyDataReader
{
public:
    virtual ~PlyDataReader() {}

    virtual int readIntValue(PlyGeometryLoader::DataType type) = 0;
    virtual float readFloatValue(PlyGeometryLoader::DataType type) = 0;
};

class AsciiPlyDataReader : public PlyDataReader
{
public:
    AsciiPlyDataReader(QIODevice *ioDev)
        : m_stream(ioDev)
    { }

    int readIntValue(PlyGeometryLoader::DataType) Q_DECL_OVERRIDE
    {
        int value;
        m_stream >> value;
        return value;
    }

    float readFloatValue(PlyGeometryLoader::DataType) Q_DECL_OVERRIDE
    {
        float value;
        m_stream >> value;
        return value;
    }

private:
    QTextStream m_stream;
};

class BinaryPlyDataReader : public PlyDataReader
{
public:
    BinaryPlyDataReader(QIODevice *ioDev, QDataStream::ByteOrder byteOrder)
        : m_stream(ioDev)
    {
        ioDev->setTextModeEnabled(false);
        m_stream.setByteOrder(byteOrder);
    }

    int readIntValue(PlyGeometryLoader::DataType type) Q_DECL_OVERRIDE
    {
        return readValue<int>(type);
    }

    float readFloatValue(PlyGeometryLoader::DataType type) Q_DECL_OVERRIDE
    {
        return readValue<float>(type);
    }

private:
    template <typename T>
    T readValue(PlyGeometryLoader::DataType type)
    {
        switch (type) {
        case PlyGeometryLoader::Int8:
        {
            qint8 value;
            m_stream >> value;
            return value;
        }

        case PlyGeometryLoader::Uint8:
        {
            quint8 value;
            m_stream >> value;
            return value;
        }

        case PlyGeometryLoader::Int16:
        {
            qint16 value;
            m_stream >> value;
            return value;
        }

        case PlyGeometryLoader::Uint16:
        {
            quint16 value;
            m_stream >> value;
            return value;
        }

        case PlyGeometryLoader::Int32:
        {
            qint32 value;
            m_stream >> value;
            return value;
        }

        case PlyGeometryLoader::Uint32:
        {
            quint32 value;
            m_stream >> value;
            return value;
        }

        case PlyGeometryLoader::Float32:
        {
            m_stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
            float value;
            m_stream >> value;
            return value;
        }

        case PlyGeometryLoader::Float64:
        {
            m_stream.setFloatingPointPrecision(QDataStream::DoublePrecision);
            double value;
            m_stream >> value;
            return value;
        }

        default:
            break;
        }

        return 0;
    }

    QDataStream m_stream;
};

}

static PlyGeometryLoader::DataType toPlyDataType(const QString &typeName)
{
    if (typeName == QStringLiteral("int8") || typeName == QStringLiteral("char")) {
        return PlyGeometryLoader::Int8;
    } else if (typeName == QStringLiteral("uint8") || typeName == QStringLiteral("uchar")) {
        return PlyGeometryLoader::Uint8;
    } else if (typeName == QStringLiteral("int16") || typeName == QStringLiteral("short")) {
        return PlyGeometryLoader::Int16;
    } else if (typeName == QStringLiteral("uint16") || typeName == QStringLiteral("ushort")) {
        return PlyGeometryLoader::Uint16;
    } else if (typeName == QStringLiteral("int32") || typeName == QStringLiteral("int")) {
        return PlyGeometryLoader::Int32;
    } else if (typeName == QStringLiteral("uint32") || typeName == QStringLiteral("uint")) {
        return PlyGeometryLoader::Uint32;
    } else if (typeName == QStringLiteral("float32") || typeName == QStringLiteral("float")) {
        return PlyGeometryLoader::Float32;
    } else if (typeName == QStringLiteral("float64") || typeName == QStringLiteral("double")) {
        return PlyGeometryLoader::Float64;
    } else if (typeName == QStringLiteral("list")) {
        return PlyGeometryLoader::TypeList;
    } else {
        return PlyGeometryLoader::TypeUnknown;
    }
}

bool PlyGeometryLoader::doLoad(QIODevice *ioDev, const QString &subMesh)
{
    Q_UNUSED(subMesh);

    if (!parseHeader(ioDev))
        return false;

    if (!parseMesh(ioDev))
        return false;

    return true;
}

bool PlyGeometryLoader::parseHeader(QIODevice *ioDev)
{
    Format format = FormatUnknown;

    m_hasNormals = m_hasTexCoords = false;

    while (!ioDev->atEnd()) {
        QByteArray lineBuffer = ioDev->readLine();
        QTextStream textStream(lineBuffer, QIODevice::ReadOnly);

        QString token;
        textStream >> token;

        if (token == QStringLiteral("end_header")) {
            break;
        } else if (token == QStringLiteral("format")) {
            QString formatName;
            textStream >> formatName;

            if (formatName == QStringLiteral("ascii")) {
                m_format = FormatAscii;
            } else if (formatName == QStringLiteral("binary_little_endian")) {
                m_format = FormatBinaryLittleEndian;
            } else if (formatName == QStringLiteral("binary_big_endian")) {
                m_format = FormatBinaryBigEndian;
            } else {
                qCDebug(PlyGeometryLoaderLog) << "Unrecognized PLY file format" << format;
                return false;
            }
        } else if (token == QStringLiteral("element")) {
            Element element;

            QString elementName;
            textStream >> elementName;

            if (elementName == QStringLiteral("vertex")) {
                element.type = ElementVertex;
            } else if (elementName == QStringLiteral("face")) {
                element.type = ElementFace;
            } else {
                element.type = ElementUnknown;
            }

            textStream >> element.count;

            m_elements.append(element);
        } else if (token == QStringLiteral("property")) {
            if (m_elements.isEmpty()) {
                qCDebug(PlyGeometryLoaderLog) << "Misplaced property in header";
                return false;
            }

            Property property;

            QString dataTypeName;
            textStream >> dataTypeName;

            property.dataType = toPlyDataType(dataTypeName);

            if (property.dataType == TypeList) {
                QString listSizeTypeName;
                textStream >> listSizeTypeName;

                property.listSizeType = toPlyDataType(listSizeTypeName);

                QString listElementTypeName;
                textStream >> listElementTypeName;

                property.listElementType = toPlyDataType(listElementTypeName);
            }

            QString propertyName;
            textStream >> propertyName;

            if (propertyName == QStringLiteral("vertex_index")) {
                property.type = PropertyVertexIndex;
            } else if (propertyName == QStringLiteral("x")) {
                property.type = PropertyX;
            } else if (propertyName == QStringLiteral("y")) {
                property.type = PropertyY;
            } else if (propertyName == QStringLiteral("z")) {
                property.type = PropertyZ;
            } else if (propertyName == QStringLiteral("nx")) {
                property.type = PropertyNormalX;
                m_hasNormals = true;
            } else if (propertyName == QStringLiteral("ny")) {
                property.type = PropertyNormalY;
                m_hasNormals = true;
            } else if (propertyName == QStringLiteral("nz")) {
                property.type = PropertyNormalZ;
                m_hasNormals = true;
            } else if (propertyName == QStringLiteral("s")) {
                property.type = PropertyTextureU;
                m_hasTexCoords = true;
            } else if (propertyName == QStringLiteral("t")) {
                property.type = PropertyTextureV;
                m_hasTexCoords = true;
            } else {
                property.type = PropertyUnknown;
            }

            Element &element = m_elements.last();
            element.properties.append(property);
        }
    }

    if (m_format == FormatUnknown) {
        qCDebug(PlyGeometryLoaderLog) << "Missing PLY file format";
        return false;
    }

    return true;
}

bool PlyGeometryLoader::parseMesh(QIODevice *ioDev)
{
    QScopedPointer<PlyDataReader> dataReader;

    switch (m_format) {
    case FormatAscii:
        dataReader.reset(new AsciiPlyDataReader(ioDev));
        break;

    case FormatBinaryLittleEndian:
        dataReader.reset(new BinaryPlyDataReader(ioDev, QDataStream::LittleEndian));
        break;

    default:
        dataReader.reset(new BinaryPlyDataReader(ioDev, QDataStream::BigEndian));
        break;
    }

    for (auto &element : qAsConst(m_elements)) {
        if (element.type == ElementVertex) {
            m_points.reserve(element.count);

            if (m_hasNormals)
                m_normals.reserve(element.count);

            if (m_hasTexCoords)
                m_texCoords.reserve(element.count);
        }

        for (int i = 0; i < element.count; ++i) {
            QVector3D point;
            QVector3D normal;
            QVector2D texCoord;

            QVector<unsigned int> faceIndices;

            for (auto &property : element.properties) {
                if (property.dataType == TypeList) {
                    const int listSize = dataReader->readIntValue(property.listSizeType);

                    if (element.type == ElementFace)
                        faceIndices.reserve(listSize);

                    for (int j = 0; j < listSize; ++j) {
                        const unsigned int value = dataReader->readIntValue(property.listElementType);

                        if (element.type == ElementFace)
                            faceIndices.append(value);
                    }
                } else {
                    float value = dataReader->readFloatValue(property.dataType);

                    if (element.type == ElementVertex) {
                        switch (property.type) {
                        case PropertyX: point.setX(value); break;
                        case PropertyY: point.setY(value); break;
                        case PropertyZ: point.setZ(value); break;
                        case PropertyNormalX: normal.setX(value); break;
                        case PropertyNormalY: normal.setY(value); break;
                        case PropertyNormalZ: normal.setZ(value); break;
                        case PropertyTextureU: texCoord.setX(value); break;
                        case PropertyTextureV: texCoord.setY(value); break;
                        default: break;
                        }
                    }
                }
            }

            if (element.type == ElementVertex) {
                m_points.append(point);

                if (m_hasNormals)
                    m_normals.append(normal);

                if (m_hasTexCoords)
                    m_texCoords.append(texCoord);
            } else if (element.type == ElementFace) {
                if (faceIndices.size() >= 3) {
                    // decompose face into triangle fan

                    for (int j = 1; j < faceIndices.size() - 1; ++j) {
                        m_indices.append(faceIndices[0]);
                        m_indices.append(faceIndices[j]);
                        m_indices.append(faceIndices[j + 1]);
                    }
                }
            }
        }
    }

    return true;
}

} // namespace Qt3DRender

QT_END_NAMESPACE