| 1 | // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qaxisalignedboundingbox_p.h" |
| 5 | |
| 6 | #include <QDebug> |
| 7 | #include <QMatrix4x4> |
| 8 | |
| 9 | QT_BEGIN_NAMESPACE |
| 10 | |
| 11 | namespace Qt3DRender { |
| 12 | |
| 13 | void QAxisAlignedBoundingBox::update(const std::vector<QVector3D> &points) |
| 14 | { |
| 15 | if (points.empty()) { |
| 16 | m_center = QVector3D(); |
| 17 | m_radii = QVector3D(); |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | QVector3D minPoint = points.at( n: 0 ); |
| 22 | QVector3D maxPoint = points.at( n: 0 ); |
| 23 | |
| 24 | for (size_t i = 1; i < points.size(); ++i) { |
| 25 | const QVector3D &point = points.at(n: i); |
| 26 | if (point.x() > maxPoint.x()) |
| 27 | maxPoint.setX(point.x()); |
| 28 | if (point.y() > maxPoint.y()) |
| 29 | maxPoint.setY(point.y()); |
| 30 | if (point.z() > maxPoint.z()) |
| 31 | maxPoint.setZ(point.z()); |
| 32 | if (point.x() < minPoint.x()) |
| 33 | minPoint.setX(point.x()); |
| 34 | if (point.y() < minPoint.y()) |
| 35 | minPoint.setY(point.y()); |
| 36 | if (point.z() < minPoint.z()) |
| 37 | minPoint.setZ(point.z()); |
| 38 | } |
| 39 | |
| 40 | m_center = 0.5 * (minPoint + maxPoint); |
| 41 | m_radii = 0.5 * (maxPoint - minPoint); |
| 42 | #if 0 |
| 43 | qDebug() << "AABB:"; |
| 44 | qDebug() << " min ="<< minPoint; |
| 45 | qDebug() << " max ="<< maxPoint; |
| 46 | qDebug() << " center ="<< m_center; |
| 47 | qDebug() << " radii ="<< m_radii; |
| 48 | #endif |
| 49 | } |
| 50 | |
| 51 | QDebug operator<<(QDebug dbg, const QAxisAlignedBoundingBox &c) |
| 52 | { |
| 53 | QDebugStateSaver saver(dbg); |
| 54 | dbg.nospace() << "AABB ( min:"<< c.minPoint() << ", max:"<< c.maxPoint() << ')'; |
| 55 | return dbg; |
| 56 | } |
| 57 | |
| 58 | } //namespace Qt3DRender |
| 59 | |
| 60 | QT_END_NAMESPACE |
| 61 |
