qmath3d/0000775000175000017500000000000013417763433012050 5ustar wookeywookeyqmath3d/QRay3D0000664000175000017500000000002412760160064013021 0ustar wookeywookey#include "qray3d.h" qmath3d/qray3d.h0000664000175000017500000001101112760160064013405 0ustar wookeywookey/**************************************************************************** ** ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** 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 Digia. For licensing terms and ** conditions see http://qt.digia.com/licensing. For further information ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Digia gives you certain additional ** rights. These rights are described in the Digia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QRAY3D_H #define QRAY3D_H #include "smallqt3d_global.h" #include #include QT_BEGIN_NAMESPACE class Q_MATH_3D_EXPORT QRay3D { public: QRay3D(); QRay3D(const QVector3D &origin, const QVector3D &direction); QVector3D origin() const; void setOrigin(const QVector3D & value); QVector3D direction() const; void setDirection(const QVector3D & value); bool contains(const QVector3D &point) const; bool contains(const QRay3D &ray) const; QVector3D point(float t) const; float projectedDistance(const QVector3D &point) const; QVector3D project(const QVector3D &vector) const; float distance(const QVector3D &point) const; void transform(const QMatrix4x4 &matrix); QRay3D transformed(const QMatrix4x4 &matrix) const; bool operator==(const QRay3D &other); bool operator!=(const QRay3D &other); private: QVector3D m_origin; QVector3D m_direction; }; inline QRay3D::QRay3D() : m_direction(1.0f, 0.0f, 0.0f) {} inline QRay3D::QRay3D(const QVector3D &origin_, const QVector3D &direction_) : m_origin(origin_) , m_direction(direction_) { } inline QVector3D QRay3D::origin() const { return m_origin; } inline void QRay3D::setOrigin(const QVector3D &value) { m_origin = value; } inline QVector3D QRay3D::direction() const { return m_direction; } inline void QRay3D::setDirection(const QVector3D & value) { m_direction = value; } inline QVector3D QRay3D::point(float t) const { return m_origin + t * m_direction; } inline void QRay3D::transform(const QMatrix4x4 &matrix) { m_origin = matrix * m_origin; m_direction = matrix.mapVector(m_direction); } inline QRay3D QRay3D::transformed(const QMatrix4x4 &matrix) const { return QRay3D(matrix * m_origin, matrix.mapVector(m_direction)); } inline bool QRay3D::operator==(const QRay3D &other) { return m_origin == other.origin() && m_direction == other.direction(); } inline bool QRay3D::operator!=(const QRay3D &other) { return m_origin != other.origin() || m_direction != other.direction(); } inline bool qFuzzyCompare(const QRay3D &ray1, const QRay3D &ray2) { return qFuzzyCompare(ray1.origin(), ray2.origin()) && qFuzzyCompare(ray1.direction(), ray2.direction()); } #ifndef QT_NO_DEBUG_STREAM Q_MATH_3D_EXPORT QDebug operator<<(QDebug dbg, const QRay3D &ray); #endif #ifndef QT_NO_DATASTREAM Q_MATH_3D_EXPORT QDataStream &operator<<(QDataStream &stream, const QRay3D &ray); Q_MATH_3D_EXPORT QDataStream &operator>>(QDataStream &stream, QRay3D &ray); #endif QT_END_NAMESPACE Q_DECLARE_METATYPE(QRay3D) #endif qmath3d/QPlane3D0000664000175000017500000000002612760160064013327 0ustar wookeywookey#include "qplane3d.h" qmath3d/qsphere3d.h0000664000175000017500000001130512760160064014106 0ustar wookeywookey/**************************************************************************** ** ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** 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 Digia. For licensing terms and ** conditions see http://qt.digia.com/licensing. For further information ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Digia gives you certain additional ** rights. These rights are described in the Digia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QSPHERE3D_H #define QSPHERE3D_H #include "smallqt3d_global.h" #include QT_BEGIN_NAMESPACE class QMatrix4x4; class QRay3D; class QBox3D; class QPlane3D; class Q_MATH_3D_EXPORT QSphere3D { public: QSphere3D(); QSphere3D(const QVector3D ¢er, float radius); QVector3D center() const; void setCenter(const QVector3D ¢er); float radius() const; void setRadius(float radius); bool contains(const QVector3D &point) const; bool intersects(const QRay3D &ray) const; bool intersects(const QSphere3D &sphere) const; bool intersects(const QBox3D &box) const; bool intersects(const QPlane3D &plane) const; bool intersection(const QRay3D &ray, float *minimum_t, float *maximum_t) const; float intersection(const QRay3D &ray) const; void transform(const QMatrix4x4 &matrix); QSphere3D transformed(const QMatrix4x4 &matrix) const; bool operator==(const QSphere3D &sphere) const; bool operator!=(const QSphere3D &sphere) const; private: QVector3D m_center; float m_radius; }; inline QSphere3D::QSphere3D() : m_radius(1.0f) {} inline QSphere3D::QSphere3D(const QVector3D ¢er_, float radius_) : m_center(center_), m_radius(radius_) {} inline QVector3D QSphere3D::center() const { return m_center; } inline void QSphere3D::setCenter(const QVector3D ¢er_) { m_center = center_; } inline float QSphere3D::radius() const { return m_radius; } inline void QSphere3D::setRadius(float radius_) { m_radius = radius_; } inline bool QSphere3D::contains(const QVector3D &point) const { return (point - m_center).lengthSquared() <= (m_radius * m_radius); } inline bool QSphere3D::intersects(const QSphere3D &sphere) const { float radsum = sphere.radius() + m_radius; return (sphere.center() - m_center).lengthSquared() <= (radsum * radsum); } inline void QSphere3D::transform(const QMatrix4x4 &matrix) { *this = transformed(matrix); } inline bool QSphere3D::operator==(const QSphere3D &sphere) const { return m_center == sphere.m_center && m_radius == sphere.m_radius; } inline bool QSphere3D::operator!=(const QSphere3D &sphere) const { return m_center != sphere.m_center || m_radius != sphere.m_radius; } inline bool qFuzzyCompare(const QSphere3D &sphere1, const QSphere3D &sphere2) { return qFuzzyCompare(sphere1.center(), sphere2.center()) && qFuzzyCompare(sphere1.radius(), sphere2.radius()); } #ifndef QT_NO_DEBUG_STREAM Q_MATH_3D_EXPORT QDebug operator<<(QDebug dbg, const QSphere3D &sphere); #endif #ifndef QT_NO_DATASTREAM Q_MATH_3D_EXPORT QDataStream &operator<<(QDataStream &stream, const QSphere3D &sphere); Q_MATH_3D_EXPORT QDataStream &operator>>(QDataStream &stream, QSphere3D &sphere); #endif QT_END_NAMESPACE Q_DECLARE_METATYPE(QSphere3D) #endif qmath3d/QSphere3D0000664000175000017500000000002712760160064013517 0ustar wookeywookey#include "qsphere3d.h" qmath3d/qray3d.cpp0000664000175000017500000002143612760160064013754 0ustar wookeywookey/**************************************************************************** ** ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** 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 Digia. For licensing terms and ** conditions see http://qt.digia.com/licensing. For further information ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Digia gives you certain additional ** rights. These rights are described in the Digia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "qray3d.h" #include QT_BEGIN_NAMESPACE /*! \class QRay3D \brief The QRay3D class defines a directional line in 3D space extending through an origin point. \since 4.8 \ingroup qt3d \ingroup qt3d::math A ray is defined by the origin() point and the direction() vector. Rays are infinite in length, extending out from origin() in both directions. If the direction() is zero length, then the behavior of the class is undefined. A ray can be thought of as a one-dimensional co-ordinate system. If the co-ordinate is \b t then the origin() point is at \b t = 0, the point origin() + direction() is at \b t = 1, and the point origin() - direction() is at \b t = -1. The point() method can be used to obtain the position of a point within this one-dimensional co-ordinate system. The projectedDistance() method can be used to convert a point into a value in this one-dimensional co-ordinate system. */ /*! \fn QRay3D::QRay3D() Construct a default ray with an origin() of (0, 0, 0) and a direction() of (1, 0, 0). */ /*! \fn QRay3D::QRay3D(const QVector3D &origin, const QVector3D &direction) Construct a ray given its defining \a origin and \a direction. The \a direction does not need to be normalized. To construct a ray that passes through two points, use the following: \code QRay3D thruAB(pointA, pointB - pointA); \endcode */ /*! \fn QVector3D QRay3D::origin() const Returns the origin of this ray. The default value is (0, 0, 0). \sa setOrigin(), direction() */ /*! \fn void QRay3D::setOrigin(const QVector3D &value) Sets the origin point of this ray to \a value. \sa origin(), setDirection() */ /*! \fn QVector3D QRay3D::direction() const Returns the direction vector of this ray. The default value is (1, 0, 0). \sa setDirection(), origin() */ /*! \fn void QRay3D::setDirection(const QVector3D &direction) Sets the direction vector of this ray to \a direction. \sa direction(), setOrigin() */ /*! Returns true if \a point lies on this ray; false otherwise. */ bool QRay3D::contains(const QVector3D &point) const { QVector3D ppVec(point - m_origin); if (ppVec.isNull()) // point coincides with origin return true; const float dot = QVector3D::dotProduct(ppVec, m_direction); if (qFuzzyIsNull(dot)) return false; return qFuzzyCompare(dot*dot, ppVec.lengthSquared() * m_direction.lengthSquared()); } /*! Returns true if \a ray lies on this ray; false otherwise. If true, this implies that the two rays are actually the same, but with different origin() points or an inverted direction(). */ bool QRay3D::contains(const QRay3D &ray) const { const float dot = QVector3D::dotProduct(m_direction, ray.direction()); if (!qFuzzyCompare(dot*dot, m_direction.lengthSquared() * ray.direction().lengthSquared())) return false; return contains(ray.origin()); } /*! \fn QVector3D QRay3D::point(float t) const Returns the point on the ray defined by moving \a t units along the ray in the direction of the direction() vector. Note that \a t may be negative in which case the point returned will lie behind the origin() point with respect to the direction() vector. The units for \a t are defined by direction(). The return value is precisely origin() + t * direction(). \sa projectedDistance(), distance() */ /*! Returns the number of direction() units along the ray from origin() to \a point. Essentially, this function computes the value t, where \a point = origin() + t * direction(). If \a point is not on the ray, then the closest point that is on the ray will be used instead. If the return value is positive, then \a point lies in front of the origin() with respect to the direction() vector. If the return value is negative, then \a point lies behind the origin() with respect to the direction() vector. \sa point(), project() */ float QRay3D::projectedDistance(const QVector3D &point) const { return QVector3D::dotProduct(point - m_origin, m_direction) / m_direction.lengthSquared(); } /*! Returns the projection of \a vector onto this ray. In the following diagram, the dotted line is the ray, and V is the \a vector. The return value will be the vector V': \image qray3d-project.png \sa projectedDistance() */ QVector3D QRay3D::project(const QVector3D &vector) const { QVector3D norm = m_direction.normalized(); return QVector3D::dotProduct(vector, norm) * norm; } /*! Returns the minimum distance from this ray to \a point, or equivalently the length of a line perpendicular to this ray which passes through \a point. If \a point is on the ray, then this function will return zero. \sa point() */ float QRay3D::distance(const QVector3D &point) const { float t = projectedDistance(point); return (point - (m_origin + t * m_direction)).length(); } /*! \fn void QRay3D::transform(const QMatrix4x4 &matrix) Transforms this ray using \a matrix, replacing origin() and direction() with the transformed versions. \sa transformed() */ /*! \fn QRay3D QRay3D::transformed(const QMatrix4x4 &matrix) const Returns a new ray that is formed by transforming origin() and direction() using \a matrix. \sa transform() */ /*! \fn bool QRay3D::operator==(const QRay3D &other) Returns true if this ray is the same as \a other; false otherwise. \sa operator!=() */ /*! \fn bool QRay3D::operator!=(const QRay3D &other) Returns true if this ray is not the same as \a other; false otherwise. \sa operator==() */ /*! \fn bool qFuzzyCompare(const QRay3D &ray1, const QRay3D &ray2) \relates QRay3D Returns true if \a ray1 and \a ray2 are almost equal; false otherwise. */ #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug dbg, const QRay3D &ray) { dbg.nospace() << "QRay3D(origin(" << ray.origin().x() << ", " << ray.origin().y() << ", " << ray.origin().z() << ") - direction(" << ray.direction().x() << ", " << ray.direction().y() << ", " << ray.direction().z() << "))"; return dbg.space(); } #endif #ifndef QT_NO_DATASTREAM /*! \relates QRay3D Writes the given \a ray to the given \a stream and returns a reference to the stream. */ QDataStream &operator<<(QDataStream &stream, const QRay3D &ray) { stream << ray.origin(); stream << ray.direction(); return stream; } /*! \relates QRay3D Reads a 3D ray from the given \a stream into the given \a ray and returns a reference to the stream. */ QDataStream &operator>>(QDataStream &stream, QRay3D &ray) { QVector3D origin, direction; stream >> origin; stream >> direction; ray = QRay3D(origin, direction); return stream; } #endif // QT_NO_DATASTREAM QT_END_NAMESPACE qmath3d/qbox3d.cpp0000664000175000017500000004663512760160064013761 0ustar wookeywookey/**************************************************************************** ** ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** 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 Digia. For licensing terms and ** conditions see http://qt.digia.com/licensing. For further information ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Digia gives you certain additional ** rights. These rights are described in the Digia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "qbox3d.h" #include "qplane3d.h" #include #include QT_BEGIN_NAMESPACE /*! \class QBox3D \brief The QBox3D class represents an axis-aligned box in 3D space. \since 4.8 \ingroup qt3d \ingroup qt3d::math QBox3D can be used to represent the bounding box of objects in a 3D scene so that they can be easily culled if they are out of view. The sides of the box are always aligned with the x, y, and z axes of the world co-ordinate system. Transforming a box with transformed() will result in the smallest axis-aligned bounding box that contains the transformed box. Boxes may be null, finite, or infinite. A null box does not occupy any space and does not intersect with any other box. A finite box consists of a minimum() and maximum() extent in 3D space. An infinite box encompasses all points in 3D space. The extents of a finite box are also included within the box. A box with minimum() and maximum() set to the same value contains a single point. */ /*! \fn QBox3D::QBox3D() Constructs a null box in 3D space. \sa isNull() */ /*! \fn QBox3D::QBox3D(const QVector3D& corner1, const QVector3D& corner2) Constructs a finite box in 3D space from \a corner1 to \a corner2. The minimum() and maximum() co-ordinates of the new box are set to the minimum and maximum x, y, and z values from \a corner1 and \a corner2. The \a corner1 and \a corner2 values can be any two opposite corners that define the box. \sa isFinite(), minimum(), maximum() */ /*! \fn bool QBox3D::isNull() const Returns true if this box is null; false otherwise. \sa isFinite(), isInfinite(), setToNull() */ /*! \fn bool QBox3D::isFinite() const Returns true if this box is finite in size; false otherwise. \sa isNull(), isInfinite(), setExtents() */ /*! \fn bool QBox3D::isInfinite() const Returns true if this box is infinite in size; false otherwise. \sa isNull(), isFinite(), setToInfinite() */ /*! \fn QVector3D QBox3D::minimum() const Returns the minimum corner of this box. \sa maximum(), setExtents() */ /*! \fn QVector3D QBox3D::maximum() const Returns the maximum corner of this box. \sa minimum(), setExtents() */ /*! \fn void QBox3D::setExtents(const QVector3D& corner1, const QVector3D& corner2) Sets the extents of this box to a finite region from \a corner1 to \a corner2. The minimum() and maximum() co-ordinates of the box are set to the minimum and maximum x, y, and z values from \a corner1 and \a corner2. The \a corner1 and \a corner2 values can be any two opposite corners that define the box. \sa minimum(), maximum() */ /*! \fn void QBox3D::setToNull() Sets this box to null. \sa isNull() */ /*! \fn void QBox3D::setToInfinite() Sets this box to be infinite in size. \sa isInfinite() */ /*! \fn QVector3D QBox3D::size() const Returns the finite size of this box. If this box is null or infinite, the returned value will be zero. \sa center(), isNull(), isInfinite() */ /*! \fn QVector3D QBox3D::center() const Returns the finite center of this box. If this box is null or infinite, the returned value will be zero. \sa size(), isNull(), isInfinite() */ /*! \fn bool QBox3D::contains(const QVector3D& point) const Returns true if this box contains \a point; false otherwise. Null boxes do not contain any points and infinite boxes contain all points. Containment is not a strict test: the point is contained if it lies on one of the faces of the box. \sa intersects() */ /*! \fn bool QBox3D::contains(const QBox3D& box) const Returns true if this box completely contains \a box. If this box is null, then it will not contain \a box. If this box is infinite, and \a box is not null, then \a box will be contained within this box. If \a box is infinite, then this box must also be infinite to contain it. \sa intersects() */ /*! Returns true if \a box intersects this box; false otherwise. \sa intersect(), intersected(), contains() */ bool QBox3D::intersects(const QBox3D& box) const { if (boxtype == Null) return false; else if (boxtype == Infinite) return box.boxtype != Null; else if (box.boxtype == Null) return false; else if (box.boxtype == Infinite) return true; if (maxcorner.x() < box.mincorner.x()) return false; if (mincorner.x() > box.maxcorner.x()) return false; if (maxcorner.y() < box.mincorner.y()) return false; if (mincorner.y() > box.maxcorner.y()) return false; if (maxcorner.z() < box.mincorner.z()) return false; if (mincorner.z() > box.maxcorner.z()) return false; return true; } /*! Returns true if \a ray intersects this box; false otherwise. \sa intersection() */ bool QBox3D::intersects(const QRay3D &ray) const { float minimum_t, maximum_t; return intersection(ray, &minimum_t, &maximum_t); } static inline void trackIntersectionX (const QBox3D &box, const QRay3D &ray, float t, float *minimum_t, float *maximum_t, bool *found) { QVector3D point = ray.point(t); if (point.y() < box.minimum().y() || point.y() > box.maximum().y()) return; if (point.z() < box.minimum().z() || point.z() > box.maximum().z()) return; if (!(*found)) { *minimum_t = *maximum_t = t; *found = true; } else { if (t < *minimum_t) *minimum_t = t; if (t > *maximum_t) *maximum_t = t; } } static inline void trackIntersectionY (const QBox3D &box, const QRay3D &ray, float t, float *minimum_t, float *maximum_t, bool *found) { QVector3D point = ray.point(t); if (point.x() < box.minimum().x() || point.x() > box.maximum().x()) return; if (point.z() < box.minimum().z() || point.z() > box.maximum().z()) return; if (!(*found)) { *minimum_t = *maximum_t = t; *found = true; } else { if (t < *minimum_t) *minimum_t = t; if (t > *maximum_t) *maximum_t = t; } } static inline void trackIntersectionZ (const QBox3D &box, const QRay3D &ray, float t, float *minimum_t, float *maximum_t, bool *found) { QVector3D point = ray.point(t); if (point.x() < box.minimum().x() || point.x() > box.maximum().x()) return; if (point.y() < box.minimum().y() || point.y() > box.maximum().y()) return; if (!(*found)) { *minimum_t = *maximum_t = t; *found = true; } else { if (t < *minimum_t) *minimum_t = t; if (t > *maximum_t) *maximum_t = t; } } /*! Finds the \a minimum_t and \a maximum_t values where \a ray intersects this box. Returns true if intersections were found; or false if there is no intersection. If \a minimum_t and \a maximum_t are set to the same value, then the intersection is at a corner or the volume of the box is zero. If the t values are negative, then the intersection occurs before the ray's origin point in the reverse direction of the ray. The \a minimum_t and \a maximum_t values can be passed to QRay3D::point() to determine the actual intersection points, as shown in the following example: \code float minimum_t, maximum_t; if (box.intersection(ray, &minimum_t, &maximum_t)) { qDebug() << "intersections at" << ray.point(minimum_t) << "and" << ray.point(maximum_t); } \endcode \sa intersects(), QRay3D::point() */ bool QBox3D::intersection(const QRay3D &ray, float *minimum_t, float *maximum_t) const { bool found = false; QVector3D origin = ray.origin(); QVector3D direction = ray.direction(); *minimum_t = *maximum_t = qSNaN(); if (boxtype == Finite) { if (direction.x() != 0.0f) { trackIntersectionX (*this, ray, (mincorner.x() - origin.x()) / direction.x(), minimum_t, maximum_t, &found); trackIntersectionX (*this, ray, (maxcorner.x() - origin.x()) / direction.x(), minimum_t, maximum_t, &found); } if (direction.y() != 0.0f) { trackIntersectionY (*this, ray, (mincorner.y() - origin.y()) / direction.y(), minimum_t, maximum_t, &found); trackIntersectionY (*this, ray, (maxcorner.y() - origin.y()) / direction.y(), minimum_t, maximum_t, &found); } if (direction.z() != 0.0f) { trackIntersectionZ (*this, ray, (mincorner.z() - origin.z()) / direction.z(), minimum_t, maximum_t, &found); trackIntersectionZ (*this, ray, (maxcorner.z() - origin.z()) / direction.z(), minimum_t, maximum_t, &found); } } return found; } /*! Returns the t value at which \a ray first intersects the sides of this box, or not-a-number if there is no intersection. When the \a ray intersects this box, the return value is a parametric value that can be passed to QRay3D::point() to determine the actual intersection point, as shown in the following example: \code float t = box.intersection(ray); QVector3D pt; if (qIsNaN(t)) { qWarning("no intersection occurred"); else pt = ray.point(t); \endcode The \a ray might intersect at two points - as the ray passes through the box - one on the near side, one on the far side; where near and far are relative to the origin point of the ray. This function only returns the near intersection point. Only positive values on the ray are considered. This means that if the origin point of the ray is inside the box, there is only one solution, not two. To get the other solution, simply change the sign of the ray's direction vector. If the origin point of the ray is outside the box, and the direction points away from the box, then there will be no intersection. When the ray does not intersect the box in the positive direction, or the box is not finite, then the return value is not-a-number. \sa intersects(), QRay3D::point() */ float QBox3D::intersection(const QRay3D &ray) const { float minimum_t, maximum_t; if (intersection(ray, &minimum_t, &maximum_t)) { if (minimum_t >= 0.0f) return minimum_t; else if (maximum_t >= 0.0f) return maximum_t; else return qSNaN(); } else { return qSNaN(); } } /*! Intersects this box with \a box. \sa intersected(), intersects(), unite() */ void QBox3D::intersect(const QBox3D& box) { // Handle the simple cases first. if (boxtype == Null) { // Null intersected with anything is null. return; } else if (boxtype == Infinite) { // Infinity intersected with a box is that box. *this = box; return; } else if (box.boxtype == Null) { // Anything intersected with null is null. setToNull(); return; } else if (box.boxtype == Infinite) { // Box intersected with infinity is the box. return; } // Intersect two finite boxes. QVector3D min1 = mincorner; QVector3D max1 = maxcorner; QVector3D min2 = box.mincorner; QVector3D max2 = box.maxcorner; if (min2.x() > min1.x()) min1.setX(min2.x()); if (min2.y() > min1.y()) min1.setY(min2.y()); if (min2.z() > min1.z()) min1.setZ(min2.z()); if (max2.x() < max1.x()) max1.setX(max2.x()); if (max2.y() < max1.y()) max1.setY(max2.y()); if (max2.z() < max1.z()) max1.setZ(max2.z()); if (min1.x() > max1.x() || min1.y() > max1.y() || min1.z() > max1.z()) { setToNull(); } else { mincorner = min1; maxcorner = max1; } } /*! Returns a new box which is the intersection of this box with \a box. \sa intersect(), intersects(), united() */ QBox3D QBox3D::intersected(const QBox3D& box) const { QBox3D result(*this); result.intersect(box); return result; } /*! Unites this box with \a point by expanding it to encompass \a point. If \a point is already contained within this box, then this box will be unchanged. \sa united(), intersect() */ void QBox3D::unite(const QVector3D& point) { if (boxtype == Finite) { if (point.x() < mincorner.x()) mincorner.setX(point.x()); else if (point.x() > maxcorner.x()) maxcorner.setX(point.x()); if (point.y() < mincorner.y()) mincorner.setY(point.y()); else if (point.y() > maxcorner.y()) maxcorner.setY(point.y()); if (point.z() < mincorner.z()) mincorner.setZ(point.z()); else if (point.z() > maxcorner.z()) maxcorner.setZ(point.z()); } else if (boxtype == Null) { boxtype = Finite; mincorner = point; maxcorner = point; } } /*! Unites this box with \a box by expanding this box to encompass the region defined by \a box. If \a box is already contained within this box, then this box will be unchanged. \sa united(), intersect() */ void QBox3D::unite(const QBox3D& box) { if (box.boxtype == Finite) { unite(box.minimum()); unite(box.maximum()); } else if (box.boxtype == Infinite) { setToInfinite(); } } /*! Returns a new box which unites this box with \a point. The returned value will be the smallest box that contains both this box and \a point. \sa unite(), intersected() */ QBox3D QBox3D::united(const QVector3D& point) const { if (boxtype == Finite) { QBox3D result(*this); result.unite(point); return result; } else if (boxtype == Null) { return QBox3D(point, point); } else { return *this; } } /*! Returns a new box which unites this box with \a box. The returned value will be the smallest box that contains both this box and \a box. \sa unite(), intersected() */ QBox3D QBox3D::united(const QBox3D& box) const { if (boxtype == Finite) { QBox3D result(*this); result.unite(box); return result; } else if (boxtype == Null) { return box; } else { return *this; } } /*! Transforms this box according to \a matrix. Each of the 8 box corners are transformed and then a new box that encompasses all of the transformed corner values is created. \sa transformed() */ void QBox3D::transform(const QMatrix4x4& matrix) { *this = transformed(matrix); } /*! Returns this box transformed by \a matrix. Each of the 8 box corners are transformed and then a new box that encompasses all of the transformed corner values is returned. \sa transform() */ QBox3D QBox3D::transformed(const QMatrix4x4& matrix) const { if (boxtype != Finite) return *this; QBox3D result; result.unite(matrix * mincorner); result.unite(matrix * QVector3D(mincorner.x(), mincorner.y(), maxcorner.z())); result.unite(matrix * QVector3D(mincorner.x(), maxcorner.y(), maxcorner.z())); result.unite(matrix * QVector3D(mincorner.x(), maxcorner.y(), mincorner.z())); result.unite(matrix * QVector3D(maxcorner.x(), mincorner.y(), mincorner.z())); result.unite(matrix * QVector3D(maxcorner.x(), maxcorner.y(), mincorner.z())); result.unite(matrix * QVector3D(maxcorner.x(), mincorner.y(), maxcorner.z())); result.unite(matrix * maxcorner); return result; } /*! \fn bool QBox3D::operator==(const QBox3D& box) const Returns true if this box is identical to \a box. */ /*! \fn bool QBox3D::operator!=(const QBox3D& box) const Returns true if this box is not identical to \a box. */ /*! \fn bool qFuzzyCompare(const QBox3D& box1, const QBox3D& box2) \relates QBox3D Returns true if \a box1 and \a box2 are almost equal; false otherwise. */ #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug dbg, const QBox3D &box) { if (box.isFinite()) { dbg.nospace() << "QBox3D((" << box.minimum().x() << ", " << box.minimum().y() << ", " << box.minimum().z() << ") - (" << box.maximum().x() << ", " << box.maximum().y() << ", " << box.maximum().z() << "))"; return dbg.space(); } else if (box.isNull()) { dbg << "QBox3D(null)"; return dbg; } else { dbg << "QBox3D(infinite)"; return dbg; } } #endif #ifndef QT_NO_DATASTREAM /*! \relates QBox3D Writes the given \a box to the given \a stream and returns a reference to the stream. */ QDataStream &operator<<(QDataStream &stream, const QBox3D &box) { if (box.isNull()) { stream << int(0); } else if (box.isInfinite()) { stream << int(2); } else { stream << int(1); stream << box.minimum(); stream << box.maximum(); } return stream; } /*! \relates QBox3D Reads a 3D box from the given \a stream into the given \a box and returns a reference to the stream. */ QDataStream &operator>>(QDataStream &stream, QBox3D &box) { int type; stream >> type; if (type == 1) { QVector3D minimum, maximum; stream >> minimum; stream >> maximum; box = QBox3D(minimum, maximum); } else if (type == 2) { box.setToInfinite(); } else { box.setToNull(); } return stream; } #endif // QT_NO_DATASTREAM QT_END_NAMESPACE qmath3d/README.md0000664000175000017500000000005212760160064013313 0ustar wookeywookeyQMath3d ======= Useful QMath3d Functions qmath3d/qplane3d.h0000664000175000017500000001114412760160064013720 0ustar wookeywookey/**************************************************************************** ** ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** 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 Digia. For licensing terms and ** conditions see http://qt.digia.com/licensing. For further information ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Digia gives you certain additional ** rights. These rights are described in the Digia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QPLANE3D_H #define QPLANE3D_H #include "qray3d.h" #include #include QT_BEGIN_NAMESPACE class Q_MATH_3D_EXPORT QPlane3D { public: QPlane3D(); QPlane3D(const QVector3D &point, const QVector3D &normal); QPlane3D(const QVector3D &p, const QVector3D &q, const QVector3D &r); QVector3D origin() const; void setOrigin(const QVector3D& value); QVector3D normal() const; void setNormal(const QVector3D& value); bool contains(const QVector3D &point) const; bool contains(const QRay3D &ray) const; bool intersects(const QRay3D &ray) const; float intersection(const QRay3D &ray) const; float distance(const QVector3D &point) const; void transform(const QMatrix4x4 &matrix); QPlane3D transformed(const QMatrix4x4 &matrix) const; bool operator==(const QPlane3D &other); bool operator!=(const QPlane3D &other); private: QVector3D m_origin; QVector3D m_normal; }; inline QPlane3D::QPlane3D() : m_normal(1.0f, 0.0f, 0.0f) {} inline QPlane3D::QPlane3D(const QVector3D &point, const QVector3D &normal_) : m_origin(point), m_normal(normal_) { } inline QPlane3D::QPlane3D(const QVector3D &p, const QVector3D &q, const QVector3D &r) : m_origin(p), m_normal(QVector3D::crossProduct(q - p, r - q)) { } inline QVector3D QPlane3D::origin() const { return m_origin; } inline void QPlane3D::setOrigin(const QVector3D &value) { m_origin = value; } inline QVector3D QPlane3D::normal() const { return m_normal; } inline void QPlane3D::setNormal(const QVector3D& value) { m_normal = value; } inline void QPlane3D::transform(const QMatrix4x4 &matrix) { m_origin = matrix * m_origin; m_normal = matrix.mapVector(m_normal); } inline QPlane3D QPlane3D::transformed(const QMatrix4x4 &matrix) const { return QPlane3D(matrix * m_origin, matrix.mapVector(m_normal)); } inline bool QPlane3D::operator==(const QPlane3D &other) { return m_origin == other.origin() && m_normal == other.normal(); } inline bool QPlane3D::operator!=(const QPlane3D &other) { return m_origin != other.origin() || m_normal != other.normal(); } inline bool qFuzzyCompare(const QPlane3D &plane1, const QPlane3D &plane2) { return qFuzzyCompare(plane1.origin(), plane2.origin()) && qFuzzyCompare(plane1.normal(), plane2.normal()); } #ifndef QT_NO_DEBUG_STREAM Q_MATH_3D_EXPORT QDebug operator<<(QDebug dbg, const QPlane3D &plane); #endif #ifndef QT_NO_DATASTREAM Q_MATH_3D_EXPORT QDataStream &operator<<(QDataStream &stream, const QPlane3D &plane); Q_MATH_3D_EXPORT QDataStream &operator>>(QDataStream &stream, QPlane3D &plane); #endif QT_END_NAMESPACE Q_DECLARE_METATYPE(QPlane3D) #endif // QPLANE3D_H qmath3d/QBox3D0000664000175000017500000000002412760160064013016 0ustar wookeywookey#include "qbox3d.h" qmath3d/math3d.pri0000664000175000017500000000034212760160064013732 0ustar wookeywookeyINCLUDEPATH += $$PWD VPATH += $$PWD HEADERS += qbox3d.h \ qplane3d.h \ qray3d.h \ qsphere3d.h \ qtriangle3d.h SOURCES += qbox3d.cpp \ qplane3d.cpp \ qray3d.cpp \ qsphere3d.cpp \ qtriangle3d.cpp qmath3d/qtriangle3d.cpp0000664000175000017500000002546512760160064014774 0ustar wookeywookey/**************************************************************************** ** ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** 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 Digia. For licensing terms and ** conditions see http://qt.digia.com/licensing. For further information ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Digia gives you certain additional ** rights. These rights are described in the Digia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "qtriangle3d.h" #include #include QT_BEGIN_NAMESPACE /*! \class QTriangle3D \brief The QTriangle3D class represents a triangle as three points in 3D space. \since 4.8 \ingroup qt3d \ingroup qt3d::math A triangle is defined by 3 points in 3D space. Since any 3 points define a plane, the triangle can be thought of as defining a plane, and forming a geometric region in that plane. If you need a simple plane, with no particular geometry, then QPlane3D is a more compact and mathematically sufficient class. The three points are labelled p(), q() and r() for consistency with textbook treatments. It is recommended that the points be supplied in counter-clockwise order for correct orientation of the triangle's plane(). \sa QPlane3D */ /*! \fn QTriangle3D::QTriangle3D() Constructs a default triangle which lies in the x-z plane, with the three vertices (0, 0, 0), (1, 0, 0), and (0, 1, 0). */ /*! \fn QTriangle3D::QTriangle3D(const QVector3D &p, const QVector3D &q, const QVector3D &r) Constructs a triangle with the supplied \a p, \a q and \a r vertices. */ /*! \fn QVector3D QTriangle3D::p() const Returns the value of the P vertex on the triangle. \sa q(), r(), setP() */ /*! \fn void QTriangle3D::setP(const QVector3D &point) Sets the value of the P vertex on the triangle to \a point. \sa setQ(), setR(), p() */ /*! \fn QVector3D QTriangle3D::q() const Returns the value of the Q vertex on the triangle. \sa p(), r(), setQ() */ /*! \fn void QTriangle3D::setQ(const QVector3D &point) Sets the value of the Q vertex on the triangle \a point. \sa setP(), setR(), q() */ /*! \fn QVector3D QTriangle3D::r() const Returns the value of the R vertex on the triangle. \sa p(), q(), setR() */ /*! \fn void QTriangle3D::setR(const QVector3D &point) Sets the value of the R vertex on the triangle \a point. \sa setP(), setQ(), r() */ /*! \fn QPlane3D QTriangle3D::plane() const Returns the plane in which the triangle lies. \sa QPlane3D */ /*! \fn QVector3D QTriangle3D::center() const Returns the center of the triangle, which is the geometric average of the three vertices. */ /*! \fn QVector3D QTriangle3D::faceNormal() const Returns the vector normal to this triangle, computed from the cross-product of P-Q and Q-R. The result is not normalized. */ static inline bool uvInTriangle(const QVector2D &c) { if (c.x() < 0.0f || c.x() > 1.0f) return false; if (c.y() < 0.0f || c.y() > 1.0f) return false; if ((c.x() + c.y()) > 1.0f) return false; return true; } /*! Returns true if this triangle contains \a point; false otherwise. To contain the \a point means that: \list \li the point lies on the same plane as the triangle, and \li the point \list \li lies either wholly within the triangle, or \li lies on one of the sides, or \li coincides with one of the 3 vertices \endlist \endlist \sa intersects() */ bool QTriangle3D::contains(const QVector3D &point) const { // Check if the point is on the triangle's plane first. QVector3D normal = QVector3D::crossProduct(m_q - m_p, m_r - m_q); if (!qFuzzyIsNull(float(QVector3D::dotProduct(normal.normalized(), m_p - point)))) return false; // Compute the barycentric co-ordinates and use them to determine // if the point is within the triangle. return uvInTriangle(uv(point)); } /*! Returns true if the \a ray intersects this triangle; false otherwise. This function will return false if the triangle is degenerate. \sa contains(), intersection() */ bool QTriangle3D::intersects(const QRay3D &ray) const { float t = plane().intersection(ray); if (qIsNaN(t)) return false; return uvInTriangle(uv(ray.point(t))); } /*! Returns the t value at which \a ray intersects this triangle, or not-a-number if there is no intersection. When the \a ray intersects this triangle, the return value is a parametric value that can be passed to QRay3D::point() to determine the actual intersection point, as shown in the following example: \code float t = triangle.intersection(ray); QVector3D pt; if (qIsNaN(t)) { qWarning("no intersection occurred"); else pt = ray.point(t); \endcode \sa intersects(), contains(), QRay3D::point() */ float QTriangle3D::intersection(const QRay3D &ray) const { float t = plane().intersection(ray); if (qIsNaN(t) || contains(ray.point(t))) return t; return qSNaN(); } /*! Transforms the points of this triangle according to \a matrix. \sa transformed() */ void QTriangle3D::transform(const QMatrix4x4 &matrix) { m_p = matrix * m_p; m_q = matrix * m_q; m_r = matrix * m_r; } /*! Returns a new triangle that results from transforming this one using \a matrix. \sa transform() */ QTriangle3D QTriangle3D::transformed(const QMatrix4x4 &matrix) const { return QTriangle3D(matrix * m_p, matrix * m_q, matrix * m_r); } /*! Returns the (u, v) barycentric co-ordinates of \a point within this triangle. The returned barycentric co-ordinates will be (1, 0) at p(), (0, 1) at q(), and (0, 0) at r(). Technically, barycentric co-ordinates have three components with the corners at (1, 0, 0), (0, 1, 0), and (0, 0, 1). However, the third component is always equal to (1 - u - v) so we do not return it. The typical use case for this function is to convert an intersection point on a triangle into the texture co-ordinate corresponding to that point. If \c p, \c q, and \c r are the points on the triangle, with corresponding texture co-ordinates \c tp, \c tq, and \c tr, then the texture co-ordinate \c tc of \a point can be determined by the following code: \code QTriangle3D triangle(p, q, r); QVector2D uv = triangle.uv(point); QVector2D tc = uv.x() * tp + uv.y() * tq + (1 - uv.x() - uv.y()) * tr; \endcode \sa contains(), intersection() */ QVector2D QTriangle3D::uv(const QVector3D &point) const { // Algorithm from: http://www.blackpawn.com/texts/pointinpoly/default.html // More: http://en.wikipedia.org/wiki/Barycentric_coordinates_(mathematics) QVector3D rq = m_q - m_r; QVector3D rp = m_p - m_r; QVector3D pp = point - m_r; float dot_rq_rq = QVector3D::dotProduct(rq, rq); float dot_rq_rp = QVector3D::dotProduct(rq, rp); float dot_rq_pp = QVector3D::dotProduct(rq, pp); float dot_rp_rp = QVector3D::dotProduct(rp, rp); float dot_rp_pp = QVector3D::dotProduct(rp, pp); float det = dot_rq_rq * dot_rp_rp - dot_rq_rp * dot_rq_rp; if (qFuzzyIsNull(float(det))) { // The point is probably not in the triangle, or the triangle // is degenerate. Return an out of range value for (u, v) so // that contains() will fail when this case happens. return QVector2D(-1.0f, -1.0f); } return QVector2D((dot_rq_rq * dot_rp_pp - dot_rq_rp * dot_rq_pp) / det, (dot_rp_rp * dot_rq_pp - dot_rq_rp * dot_rp_pp) / det); } /*! \fn bool QTriangle3D::operator==(const QTriangle3D &other) Returns true if this triangle is the same as \a other; false otherwise. \sa operator!=() */ /*! \fn bool QTriangle3D::operator!=(const QTriangle3D &other) Returns true if this triangle is not the same as \a other; false otherwise. \sa operator==() */ /*! \fn bool qFuzzyCompare(const QTriangle3D &triangle1, const QTriangle3D &triangle2) \relates QTriangle3D Returns true if \a triangle1 and \a triangle2 are almost equal; false otherwise. */ #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug dbg, const QTriangle3D &triangle) { dbg.nospace() << "QTriangle3D((" << triangle.p().x() << ", " << triangle.p().y() << ", " << triangle.p().z() << "), (" << triangle.q().x() << ", " << triangle.q().y() << ", " << triangle.q().z() << "), (" << triangle.r().x() << ", " << triangle.r().y() << ", " << triangle.r().z() << "))"; return dbg.space(); } #endif #ifndef QT_NO_DATASTREAM /*! \relates QTriangle3D Writes the given \a triangle to the given \a stream and returns a reference to the stream. */ QDataStream &operator<<(QDataStream &stream, const QTriangle3D &triangle) { stream << triangle.p(); stream << triangle.q(); stream << triangle.r(); return stream; } /*! \relates QTriangle3D Reads a 3D triangle from the given \a stream into the given \a triangle and returns a reference to the stream. */ QDataStream &operator>>(QDataStream &stream, QTriangle3D &triangle) { QVector3D p, q, r; stream >> p; stream >> q; stream >> r; triangle = QTriangle3D(p, q, r); return stream; } #endif // QT_NO_DATASTREAM QT_END_NAMESPACE qmath3d/qbox3d.h0000664000175000017500000001530312760160064013412 0ustar wookeywookey/**************************************************************************** ** ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** 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 Digia. For licensing terms and ** conditions see http://qt.digia.com/licensing. For further information ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Digia gives you certain additional ** rights. These rights are described in the Digia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QBOX3D_H #define QBOX3D_H #include "smallqt3d_global.h" #include "qray3d.h" QT_BEGIN_NAMESPACE class QMatrix4x4; class Q_MATH_3D_EXPORT QBox3D { public: QBox3D(); QBox3D(const QVector3D& corner1, const QVector3D& corner2); bool isNull() const; bool isFinite() const; bool isInfinite() const; QVector3D minimum() const; QVector3D maximum() const; void setExtents(const QVector3D& corner1, const QVector3D& corner2); void setToNull(); void setToInfinite(); QVector3D size() const; QVector3D center() const; bool contains(const QVector3D& point) const; bool contains(const QBox3D& box) const; bool intersects(const QRay3D& ray) const; bool intersection(const QRay3D &ray, float *minimum_t, float *maximum_t) const; float intersection(const QRay3D& ray) const; bool intersects(const QBox3D& box) const; void intersect(const QBox3D& box); QBox3D intersected(const QBox3D& box) const; void unite(const QVector3D& point); void unite(const QBox3D& box); QBox3D united(const QVector3D& point) const; QBox3D united(const QBox3D& box) const; void transform(const QMatrix4x4& matrix); QBox3D transformed(const QMatrix4x4& matrix) const; bool operator==(const QBox3D& box) const; bool operator!=(const QBox3D& box) const; friend bool qFuzzyCompare(const QBox3D& box1, const QBox3D& box2); private: enum Type { Null, Finite, Infinite }; QBox3D::Type boxtype; QVector3D mincorner, maxcorner; }; inline QBox3D::QBox3D() : boxtype(Null), mincorner(0, 0, 0), maxcorner(0, 0, 0) {} inline QBox3D::QBox3D(const QVector3D& corner1, const QVector3D& corner2) : boxtype(Finite), mincorner(qMin(corner1.x(), corner2.x()), qMin(corner1.y(), corner2.y()), qMin(corner1.z(), corner2.z())), maxcorner(qMax(corner1.x(), corner2.x()), qMax(corner1.y(), corner2.y()), qMax(corner1.z(), corner2.z())) {} inline bool QBox3D::isNull() const { return (boxtype == Null); } inline bool QBox3D::isFinite() const { return (boxtype == Finite); } inline bool QBox3D::isInfinite() const { return (boxtype == Infinite); } inline QVector3D QBox3D::minimum() const { return mincorner; } inline QVector3D QBox3D::maximum() const { return maxcorner; } inline void QBox3D::setExtents(const QVector3D& corner1, const QVector3D& corner2) { boxtype = Finite; mincorner = QVector3D(qMin(corner1.x(), corner2.x()), qMin(corner1.y(), corner2.y()), qMin(corner1.z(), corner2.z())); maxcorner = QVector3D(qMax(corner1.x(), corner2.x()), qMax(corner1.y(), corner2.y()), qMax(corner1.z(), corner2.z())); } inline void QBox3D::setToNull() { boxtype = Null; mincorner = QVector3D(0, 0, 0); maxcorner = QVector3D(0, 0, 0); } inline void QBox3D::setToInfinite() { boxtype = Infinite; mincorner = QVector3D(0, 0, 0); maxcorner = QVector3D(0, 0, 0); } inline QVector3D QBox3D::size() const { return maxcorner - mincorner; } inline QVector3D QBox3D::center() const { return (mincorner + maxcorner) * 0.5f; } inline bool QBox3D::contains(const QVector3D& point) const { if (boxtype == Finite) { return (point.x() >= mincorner.x() && point.x() <= maxcorner.x() && point.y() >= mincorner.y() && point.y() <= maxcorner.y() && point.z() >= mincorner.z() && point.z() <= maxcorner.z()); } else if (boxtype == Infinite) { return true; } else { return false; } } inline bool QBox3D::contains(const QBox3D& box) const { if (box.boxtype == Finite) return contains(box.mincorner) && contains(box.maxcorner); else if (box.boxtype == Infinite) return (boxtype == Infinite); else return false; } inline bool QBox3D::operator==(const QBox3D& box) const { return (boxtype == box.boxtype && mincorner == box.mincorner && maxcorner == box.maxcorner); } inline bool QBox3D::operator!=(const QBox3D& box) const { return (boxtype != box.boxtype || mincorner != box.mincorner || maxcorner != box.maxcorner); } inline bool qFuzzyCompare(const QBox3D& box1, const QBox3D& box2) { return box1.boxtype == box2.boxtype && qFuzzyCompare(box1.mincorner, box2.mincorner) && qFuzzyCompare(box1.maxcorner, box2.maxcorner); } #ifndef QT_NO_DEBUG_STREAM Q_MATH_3D_EXPORT QDebug operator<<(QDebug dbg, const QBox3D &box); #endif #ifndef QT_NO_DATASTREAM Q_MATH_3D_EXPORT QDataStream &operator<<(QDataStream &stream, const QBox3D &box); Q_MATH_3D_EXPORT QDataStream &operator>>(QDataStream &stream, QBox3D &box); #endif QT_END_NAMESPACE Q_DECLARE_METATYPE(QBox3D) #endif qmath3d/QMath3d.qbs0000664000175000017500000000262512760160064014014 0ustar wookeywookeyimport qbs.base 1.0 DynamicLibrary { name: "QMath3d" //For mac os x we need to build dylib instead of framework bundle. When running //macdepolyqt for release, with a framework, an extra "lib" is added to the //path which prevents macdeployqt from finding the correct library's location consoleApplication: true cpp.cxxLanguageVersion: (Qt.core.versionMajor >= 5 && Qt.core.versionMinor >= 7 ? "c++11" : "c++98"); readonly property string rpath: buildDirectory Depends { name: "cpp" } Depends { name: "Qt"; submodules: [ "core", "gui"] } Export { Depends { name: "cpp" } cpp.includePaths: ["."] cpp.rpaths: product.rpath cpp.cxxLanguageVersion: (Qt.core.versionMajor >= 5 && Qt.core.versionMinor >= 7 ? "c++11" : "c++98"); } Group { fileTagsFilter: ["dynamiclibrary"] qbs.install: qbs.targetOS.contains("windows") } cpp.rpaths: [Qt.core.libPath] files: [ "qbox3d.h", "qplane3d.h", "qray3d.h", "qsphere3d.h", "qtriangle3d.h", "qbox3d.cpp", "qplane3d.cpp", "qray3d.cpp", "qsphere3d.cpp", "qtriangle3d.cpp", "smallqt3d_global.h" ] cpp.includePaths: [ "." ] cpp.installNamePrefix: "@rpath" Properties { condition: qbs.targetOS.contains("windows") cpp.defines: ["Q_MATH_3D"] } } qmath3d/QTriangle3D0000664000175000017500000000003112760160064014031 0ustar wookeywookey#include "qtriangle3d.h" qmath3d/qtriangle3d.h0000664000175000017500000001153012760160064014425 0ustar wookeywookey/**************************************************************************** ** ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** 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 Digia. For licensing terms and ** conditions see http://qt.digia.com/licensing. For further information ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Digia gives you certain additional ** rights. These rights are described in the Digia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QTRIANGLE3D_H #define QTRIANGLE3D_H #include "smallqt3d_global.h" #include "qplane3d.h" #include "qray3d.h" #include #include QT_BEGIN_NAMESPACE class QMatrix4x4; class Q_MATH_3D_EXPORT QTriangle3D { public: QTriangle3D(); QTriangle3D(const QVector3D &p, const QVector3D &q, const QVector3D &r); QVector3D p() const; void setP(const QVector3D & point); QVector3D q() const; void setQ(const QVector3D & point); QVector3D r() const; void setR(const QVector3D & point); QPlane3D plane() const; QVector3D center() const; QVector3D faceNormal() const; bool contains(const QVector3D &point) const; bool intersects(const QRay3D &ray) const; float intersection(const QRay3D &ray) const; void transform(const QMatrix4x4 &matrix); QTriangle3D transformed(const QMatrix4x4 &matrix) const; QVector2D uv(const QVector3D &point) const; bool operator==(const QTriangle3D &other); bool operator!=(const QTriangle3D &other); private: QVector3D m_p, m_q, m_r; }; inline QTriangle3D::QTriangle3D() : m_p(0.0f, 0.0f, 0.0f) , m_q(1.0f, 0.0f, 0.0f) , m_r(0.0f, 1.0f, 0.0f) {} inline QTriangle3D::QTriangle3D(const QVector3D &p_, const QVector3D &q_, const QVector3D &r_) : m_p(p_) , m_q(q_) , m_r(r_) {} inline QVector3D QTriangle3D::p() const { return m_p; } inline void QTriangle3D::setP(const QVector3D &point) { m_p = point; } inline QVector3D QTriangle3D::q() const { return m_q; } inline void QTriangle3D::setQ(const QVector3D &point) { m_q = point; } inline QVector3D QTriangle3D::r() const { return m_r; } inline void QTriangle3D::setR(const QVector3D &point) { m_r = point; } inline QPlane3D QTriangle3D::plane() const { return QPlane3D(m_p, m_q, m_r); } inline QVector3D QTriangle3D::center() const { return (m_p + m_q + m_r) / 3.0f; } inline QVector3D QTriangle3D::faceNormal() const { return QVector3D::crossProduct(m_q - m_p, m_r - m_q); } inline bool QTriangle3D::operator==(const QTriangle3D &other) { return m_p == other.m_p && m_q == other.m_q && m_r == other.m_r; } inline bool QTriangle3D::operator!=(const QTriangle3D &other) { return m_p != other.m_p || m_q != other.m_q || m_r != other.m_r; } inline bool qFuzzyCompare (const QTriangle3D &triangle1, const QTriangle3D &triangle2) { return qFuzzyCompare(triangle1.p(), triangle2.p()) && qFuzzyCompare(triangle1.q(), triangle2.q()) && qFuzzyCompare(triangle1.r(), triangle2.r()); } #ifndef QT_NO_DEBUG_STREAM Q_MATH_3D_EXPORT QDebug operator<<(QDebug dbg, const QTriangle3D &triangle); #endif #ifndef QT_NO_DATASTREAM Q_MATH_3D_EXPORT QDataStream &operator<<(QDataStream &stream, const QTriangle3D &triangle); Q_MATH_3D_EXPORT QDataStream &operator>>(QDataStream &stream, QTriangle3D &triangle); #endif QT_END_NAMESPACE Q_DECLARE_METATYPE(QTriangle3D) #endif // QTRIANGLE3D_H qmath3d/qplane3d.cpp0000664000175000017500000002121312760160064014251 0ustar wookeywookey/**************************************************************************** ** ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** 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 Digia. For licensing terms and ** conditions see http://qt.digia.com/licensing. For further information ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Digia gives you certain additional ** rights. These rights are described in the Digia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "qplane3d.h" #include #include QT_BEGIN_NAMESPACE /*! \class QPlane3D \brief The QPlane3D class models the mathematics of planes in 3D space. \since 4.8 \ingroup qt3d \ingroup qt3d::math A plane is defined by an origin() point lying on the plane, and a normal() vector, which is perpendicular to the surface of the plane. The normal() vector does not need to be normalized. QPlane3D is an infinite surface, from which the normal() vector points out perpendicular from the origin() point. \sa QRay3D */ /*! \fn QPlane3D::QPlane3D() Constructs a default plane object. The defining origin() of the plane is set to (0, 0, 0) and the normal() vector is to (1, 0, 0). */ /*! \fn QPlane3D::QPlane3D(const QVector3D &point, const QVector3D &normal) Constructs a new plane, where \a point lies on the plane, and \a normal is perpendicular to it. The \a normal does not have to be normalized. If \a normal is zero, the behavior of the plane is undefined. */ /*! \fn QPlane3D::QPlane3D(const QVector3D &p, const QVector3D &q, const QVector3D &r) Constructs a new plane defined by the three points \a p, \a q, and \a r. The point \a p is used as the plane's origin() point, and a normal() is constructed from the cross-product of \a q - \a p and \a r - \a q. */ /*! \fn QVector3D QPlane3D::origin() const Returns this plane's defining origin point. The default value is (0, 0, 0). \sa setOrigin(), normal() */ /*! \fn void QPlane3D::setOrigin(const QVector3D& value) Set this plane's defining origin point to \a value. \sa origin(), setNormal() */ /*! \fn QVector3D QPlane3D::normal() const Returns this plane's normal vector. The default value is (1, 0, 0). \sa setNormal(), origin() */ /*! \fn void QPlane3D::setNormal(const QVector3D& value) Set this plane's normal vector to \a value. The \a value does not have to be normalized. If \a value is zero, the behavior of the plane is undefined. \sa normal(), setOrigin() */ /*! Returns true if \a point lies in this plane; false otherwise. */ bool QPlane3D::contains(const QVector3D &point) const { return qFuzzyIsNull (float(QVector3D::dotProduct(m_normal, m_origin - point))); } /*! Returns true if all of the points on \a ray lie in this plane; false otherwise. */ bool QPlane3D::contains(const QRay3D &ray) const { return qFuzzyIsNull (float(QVector3D::dotProduct(m_normal, ray.direction()))) && contains(ray.origin()); } /*! Returns true if an intersection of \a ray with this plane exists; false otherwise. \sa intersection() */ bool QPlane3D::intersects(const QRay3D &ray) const { return !qFuzzyIsNull (float(QVector3D::dotProduct(m_normal, ray.direction()))); } /*! Returns the t value at which \a ray intersects this plane, or not-a-number if there is no intersection. When the \a ray intersects this plane, the return value is a parametric value that can be passed to QRay3D::point() to determine the actual intersection point, as shown in the following example: \code float t = plane.intersection(ray); QVector3D pt; if (qIsNaN(t)) { qWarning("no intersection occurred"); else pt = ray.point(t); \endcode If the return value is positive, then the QRay3D::origin() of \a ray begins below the plane and then extends through it. If the return value is negative, then the origin begins above the plane. There are two failure cases where no single intersection point exists: \list \li when the ray is parallel to the plane (but does not lie on it) \li the ray lies entirely in the plane (thus every point "intersects") \endlist This method does not distinguish between these two failure cases and simply returns not-a-number for both. \sa intersects() */ float QPlane3D::intersection(const QRay3D& ray) const { float dotLineAndPlane = QVector3D::dotProduct(m_normal, ray.direction()); if (qFuzzyIsNull(float(dotLineAndPlane))) { // degenerate case - ray & plane-normal vectors are at // 90 degrees to each other, so either plane and ray never meet // or the ray lies in the plane - return failure case. return qSNaN(); } return QVector3D::dotProduct(m_origin - ray.origin(), m_normal) / dotLineAndPlane; } /*! Returns the distance from this plane to \a point. The value will be positive if \a point is above the plane in the direction of normal(), and negative if \a point is below the plane. */ float QPlane3D::distance(const QVector3D &point) const { return QVector3D::dotProduct(point - m_origin, m_normal) / m_normal.length(); } /*! \fn void QPlane3D::transform(const QMatrix4x4 &matrix) Transforms this plane using \a matrix, replacing origin() and normal() with the transformed versions. \sa transformed() */ /*! \fn QPlane3D QPlane3D::transformed(const QMatrix4x4 &matrix) const Returns a new plane that is formed by transforming origin() and normal() using \a matrix. \sa transform() */ /*! \fn bool QPlane3D::operator==(const QPlane3D &other) Returns true if this plane is the same as \a other; false otherwise. \sa operator!=() */ /*! \fn bool QPlane3D::operator!=(const QPlane3D &other) Returns true if this plane is not the same as \a other; false otherwise. \sa operator==() */ /*! \fn bool qFuzzyCompare(const QPlane3D &plane1, const QPlane3D &plane2) \relates QPlane3D Returns true if \a plane1 and \a plane2 are almost equal; false otherwise. */ #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug dbg, const QPlane3D &plane) { dbg.nospace() << "QPlane3D(origin(" << plane.origin().x() << ", " << plane.origin().y() << ", " << plane.origin().z() << ") - normal(" << plane.normal().x() << ", " << plane.normal().y() << ", " << plane.normal().z() << "))"; return dbg.space(); } #endif #ifndef QT_NO_DATASTREAM /*! \relates QPlane3D Writes the given \a plane to the given \a stream and returns a reference to the stream. */ QDataStream &operator<<(QDataStream &stream, const QPlane3D &plane) { stream << plane.origin(); stream << plane.normal(); return stream; } /*! \relates QPlane3D Reads a 3D plane from the given \a stream into the given \a plane and returns a reference to the stream. */ QDataStream &operator>>(QDataStream &stream, QPlane3D &plane) { QVector3D origin, normal; stream >> origin; stream >> normal; plane = QPlane3D(origin, normal); return stream; } #endif // QT_NO_DATASTREAM QT_END_NAMESPACE qmath3d/smallqt3d_global.h0000664000175000017500000000071612760160064015440 0ustar wookeywookey/************************************************************************** ** ** Copyright (C) 2013 by Philip Schuchardt ** www.cavewhere.com ** **************************************************************************/ #ifndef SMALLQT3D_GLOBAL_H #define SMALLQT3D_GLOBAL_H #include #if defined(Q_MATH_3D) # define Q_MATH_3D_EXPORT Q_DECL_EXPORT #else # define Q_MATH_3D_EXPORT Q_DECL_IMPORT #endif #endif // SMALLQT3D_GLOBAL_H qmath3d/LICENSE0000664000175000017500000006351612760160064013057 0ustar wookeywookeyGNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999 Copyright (C) 1991, 1999 Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. [This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public Licenses are intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This license, the Lesser General Public License, applies to some specially designated software packages--typically libraries--of the Free Software Foundation and other authors who decide to use it. You can use it too, but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular case, based on the explanations below. When we speak of free software, we are referring to freedom of use, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish); that you receive source code or can get it if you want it; that you can change the software and use pieces of it in new free programs; and that you are informed that you can do these things. To protect your rights, we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights. These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it. For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that we gave you. You must make sure that they, too, receive or can get the source code. If you link other code with the library, you must provide complete object files to the recipients, so that they can relink them with the library after making changes to the library and recompiling it. And you must show them these terms so they know their rights. We protect your rights with a two-step method: (1) we copyright the library, and (2) we offer you this license, which gives you legal permission to copy, distribute and/or modify the library. To protect each distributor, we want to make it very clear that there is no warranty for the free library. Also, if the library is modified by someone else and passed on, the recipients should know that what they have is not the original version, so that the original author's reputation will not be affected by problems that might be introduced by others. Finally, software patents pose a constant threat to the existence of any free program. We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a restrictive license from a patent holder. Therefore, we insist that any patent license obtained for a version of the library must be consistent with the full freedom of use specified in this license. Most GNU software, including some libraries, is covered by the ordinary GNU General Public License. This license, the GNU Lesser General Public License, applies to certain designated libraries, and is quite different from the ordinary General Public License. We use this license for certain libraries in order to permit linking those libraries into non-free programs. When a program is linked with a library, whether statically or using a shared library, the combination of the two is legally speaking a combined work, a derivative of the original library. The ordinary General Public License therefore permits such linking only if the entire combination fits its criteria of freedom. The Lesser General Public License permits more lax criteria for linking other code with the library. We call this license the "Lesser" General Public License because it does Less to protect the user's freedom than the ordinary General Public License. It also provides other free software developers Less of an advantage over competing non-free programs. These disadvantages are the reason we use the ordinary General Public License for many libraries. However, the Lesser license provides advantages in certain special circumstances. For example, on rare occasions, there may be a special need to encourage the widest possible use of a certain library, so that it becomes a de-facto standard. To achieve this, non-free programs must be allowed to use the library. A more frequent case is that a free library does the same job as widely used non-free libraries. In this case, there is little to gain by limiting the free library to free software only, so we use the Lesser General Public License. In other cases, permission to use a particular library in non-free programs enables a greater number of people to use a large body of free software. For example, permission to use the GNU C Library in non-free programs enables many more people to use the whole GNU operating system, as well as its variant, the GNU/Linux operating system. Although the Lesser General Public License is Less protective of the users' freedom, it does ensure that the user of a program that is linked with the Library has the freedom and the wherewithal to run that program using a modified version of the Library. The precise terms and conditions for copying, distribution and modification follow. Pay close attention to the difference between a "work based on the library" and a "work that uses the library". The former contains code derived from the library, whereas the latter must be combined with the library in order to run. GNU LESSER GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any software library or other program which contains a notice placed by the copyright holder or other authorized party saying it may be distributed under the terms of this Lesser General Public License (also called "this License"). Each licensee is addressed as "you". A "library" means a collection of software functions and/or data prepared so as to be conveniently linked with application programs (which use some of those functions and data) to form executables. The "Library", below, refers to any such software library or work which has been distributed under these terms. A "work based on the Library" means either the Library or any derivative work under copyright law: that is to say, a work containing the Library or a portion of it, either verbatim or with modifications and/or translated straightforwardly into another language. (Hereinafter, translation is included without limitation in the term "modification".) "Source code" for a work means the preferred form of the work for making modifications to it. For a library, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the library. Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running a program using the Library is not restricted, and output from such a program is covered only if its contents constitute a work based on the Library (independent of the use of the Library in a tool for writing it). Whether that is true depends on what the Library does and what the program that uses the Library does. 1. You may copy and distribute verbatim copies of the Library's complete source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and distribute a copy of this License along with the Library. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Library or any portion of it, thus forming a work based on the Library, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) The modified work must itself be a software library. b) You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change. c) You must cause the whole of the work to be licensed at no charge to all third parties under the terms of this License. d) If a facility in the modified Library refers to a function or a table of data to be supplied by an application program that uses the facility, other than as an argument passed when the facility is invoked, then you must make a good faith effort to ensure that, in the event an application does not supply such function or table, the facility still operates, and performs whatever part of its purpose remains meaningful. (For example, a function in a library to compute square roots has a purpose that is entirely well-defined independent of the application. Therefore, Subsection 2d requires that any application-supplied function or table used by this function must be optional: if the application does not supply it, the square root function must still compute square roots.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Library, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Library, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Library. In addition, mere aggregation of another work not based on the Library with the Library (or with a work based on the Library) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may opt to apply the terms of the ordinary GNU General Public License instead of this License to a given copy of the Library. To do this, you must alter all the notices that refer to this License, so that they refer to the ordinary GNU General Public License, version 2, instead of to this License. (If a newer version than version 2 of the ordinary GNU General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU General Public License applies to all subsequent copies and derivative works made from that copy. This option is useful when you wish to copy part of the code of the Library into a program that is not a library. 4. You may copy and distribute the Library (or a portion or derivative of it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange. If distribution of object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place satisfies the requirement to distribute the source code, even though third parties are not compelled to copy the source along with the object code. 5. A program that contains no derivative of any portion of the Library, but is designed to work with the Library by being compiled or linked with it, is called a "work that uses the Library". Such a work, in isolation, is not a derivative work of the Library, and therefore falls outside the scope of this License. However, linking a "work that uses the Library" with the Library creates an executable that is a derivative of the Library (because it contains portions of the Library), rather than a "work that uses the library". The executable is therefore covered by this License. Section 6 states terms for distribution of such executables. When a "work that uses the Library" uses material from a header file that is part of the Library, the object code for the work may be a derivative work of the Library even though the source code is not. Whether this is true is especially significant if the work can be linked without the Library, or if the work is itself a library. The threshold for this to be true is not precisely defined by law. If such an object file uses only numerical parameters, data structure layouts and accessors, and small macros and small inline functions (ten lines or less in length), then the use of the object file is unrestricted, regardless of whether it is legally a derivative work. (Executables containing this object code plus portions of the Library will still fall under Section 6.) Otherwise, if the work is a derivative of the Library, you may distribute the object code for the work under the terms of Section 6. Any executables containing that work also fall under Section 6, whether or not they are linked directly with the Library itself. 6. As an exception to the Sections above, you may also combine or link a "work that uses the Library" with the Library to produce a work containing portions of the Library, and distribute that work under terms of your choice, provided that the terms permit modification of the work for the customer's own use and reverse engineering for debugging such modifications. You must give prominent notice with each copy of the work that the Library is used in it and that the Library and its use are covered by this License. You must supply a copy of this License. If the work during execution displays copyright notices, you must include the copyright notice for the Library among them, as well as a reference directing the user to the copy of this License. Also, you must do one of these things: a) Accompany the work with the complete corresponding machine-readable source code for the Library including whatever changes were used in the work (which must be distributed under Sections 1 and 2 above); and, if the work is an executable linked with the Library, with the complete machine-readable "work that uses the Library", as object code and/or source code, so that the user can modify the Library and then relink to produce a modified executable containing the modified Library. (It is understood that the user who changes the contents of definitions files in the Library will not necessarily be able to recompile the application to use the modified definitions.) b) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (1) uses at run time a copy of the library already present on the user's computer system, rather than copying library functions into the executable, and (2) will operate properly with a modified version of the library, if the user installs one, as long as the modified version is interface-compatible with the version that the work was made with. c) Accompany the work with a written offer, valid for at least three years, to give the same user the materials specified in Subsection 6a, above, for a charge no more than the cost of performing this distribution. d) If distribution of the work is made by offering access to copy from a designated place, offer equivalent access to copy the above specified materials from the same place. e) Verify that the user has already received a copy of these materials or that you have already sent this user a copy. For an executable, the required form of the "work that uses the Library" must include any data and utility programs needed for reproducing the executable from it. However, as a special exception, the materials to be distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. It may happen that this requirement contradicts the license restrictions of other proprietary libraries that do not normally accompany the operating system. Such a contradiction means you cannot use both them and the Library together in an executable that you distribute. 7. You may place library facilities that are a work based on the Library side-by-side in a single library together with other library facilities not covered by this License, and distribute such a combined library, provided that the separate distribution of the work based on the Library and of the other library facilities is otherwise permitted, and provided that you do these two things: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities. This must be distributed under the terms of the Sections above. b) Give prominent notice with the combined library of the fact that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 8. You may not copy, modify, sublicense, link with, or distribute the Library except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, link with, or distribute the Library is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 9. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Library or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Library (or any work based on the Library), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Library or works based on it. 10. Each time you redistribute the Library (or any work based on the Library), the recipient automatically receives a license from the original licensor to copy, distribute, link with or modify the Library subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties with this License. 11. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Library at all. For example, if a patent license would not permit royalty-free redistribution of the Library by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Library. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply, and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 12. If the distribution and/or use of the Library is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Library under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 13. The Free Software Foundation may publish revised and/or new versions of the Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Library does not specify a license version number, you may choose any version ever published by the Free Software Foundation. 14. If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incompatible with these, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Libraries If you develop a new library, and you want it to be of the greatest possible use to the public, we recommend making it free software that everyone can redistribute and change. You can do so by permitting redistribution under these terms (or, alternatively, under the terms of the ordinary General Public License). To apply these terms, attach the following notices to the library. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Useful QMath3d Functions Copyright (C) 2013 vpicaver This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the library, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the library `Frob' (a library for tweaking knobs) written by James Random Hacker. {signature of Ty Coon}, 1 April 1990 Ty Coon, President of Vice That's all there is to it! qmath3d/qsphere3d.cpp0000664000175000017500000002640112760160064014444 0ustar wookeywookey/**************************************************************************** ** ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** 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 Digia. For licensing terms and ** conditions see http://qt.digia.com/licensing. For further information ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Digia gives you certain additional ** rights. These rights are described in the Digia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "qsphere3d.h" #include "qray3d.h" #include "qbox3d.h" #include "qplane3d.h" #include #include QT_BEGIN_NAMESPACE /*! \class QSphere3D \brief The QSphere3D class represents a mathematical sphere in 3D space. \since 4.8 \ingroup qt3d \ingroup qt3d::math QSphere3D can be used to represent the bounding regions of objects in a 3D scene so that they can be easily culled if they are out of view. It can also be used to assist with collision testing. \sa QBox3D */ /*! \fn QSphere3D::QSphere3D() Constructs a default sphere with a center() of (0, 0, 0) and radius() of 1. */ /*! \fn QSphere3D::QSphere3D(const QVector3D ¢er, float radius) Constructs a sphere with the specified \a center and \a radius. */ /*! \fn QVector3D QSphere3D::center() const Returns the center of this sphere. \sa setCenter(), radius() */ /*! \fn void QSphere3D::setCenter(const QVector3D ¢er) Sets the \a center of this sphere. \sa center(), setRadius() */ /*! \fn float QSphere3D::radius() const Returns the radius of this sphere. \sa setRadius(), center() */ /*! \fn void QSphere3D::setRadius(float radius) Sets the \a radius of this sphere. \sa radius(), setCenter() */ /*! \fn bool QSphere3D::contains(const QVector3D &point) const Returns true if \a point is contained within the bounds of this sphere; false otherwise. */ /*! Returns true if this sphere intersects \a ray; false otherwise. \sa intersection() */ bool QSphere3D::intersects(const QRay3D &ray) const { QVector3D centerToOrigin = ray.origin() - m_center; float term1 = ray.direction().lengthSquared(); float term2 = 2.0f * QVector3D::dotProduct(centerToOrigin, ray.direction()); float term3 = centerToOrigin.lengthSquared() - m_radius * m_radius; float det = term2 * term2 - (4.0f * term1 * term3); return term1 != 0.0f && det >= 0.0f; } /*! \fn bool QSphere3D::intersects(const QSphere3D &sphere) const Returns true if this sphere intersects \a sphere; false otherwise. \sa contains() */ /*! Returns true if this sphere intersects \a box; false otherwise. */ bool QSphere3D::intersects(const QBox3D &box) const { if (box.isFinite()) { // Use Arvo's Algorithm to determine if we have an intersection. float dist = 0.0f; float center = m_center.x(); float minval = box.minimum().x(); float maxval = box.maximum().x(); if (center < minval) dist += (center - minval) * (center - minval); else if (center > maxval) dist += (center - maxval) * (center - maxval); center = m_center.y(); minval = box.minimum().y(); maxval = box.maximum().y(); if (center < minval) dist += (center - minval) * (center - minval); else if (center > maxval) dist += (center - maxval) * (center - maxval); center = m_center.z(); minval = box.minimum().z(); maxval = box.maximum().z(); if (center < minval) dist += (center - minval) * (center - minval); else if (center > maxval) dist += (center - maxval) * (center - maxval); return dist <= (m_radius * m_radius); } else { return box.isInfinite(); } } /*! Returns true if this sphere intersects \a plane; false otherwise. */ bool QSphere3D::intersects(const QPlane3D &plane) const { return qAbs(plane.distance(m_center)) <= m_radius; } /*! Finds the \a minimum_t and \a maximum_t values where \a ray intersects this sphere. Returns true if intersections were found; or false if there is no intersection. If \a minimum_t and \a maximum_t are set to the same value, then \a ray touches the surface of the sphere at a single point. If the t values are negative, then the intersection occurs before the ray's origin point in the reverse direction of the ray. The \a minimum_t and \a maximum_t values can be passed to QRay3D::point() to determine the actual intersection points, as shown in the following example: \code float minimum_t, maximum_t; if (sphere.intersection(ray, &minimum_t, &maximum_t)) { qDebug() << "intersections at" << ray.point(minimum_t) << "and" << ray.point(maximum_t); } \endcode \sa intersects(), QRay3D::point() */ bool QSphere3D::intersection(const QRay3D &ray, float *minimum_t, float *maximum_t) const { QVector3D centerToOrigin = ray.origin() - m_center; float term1 = ray.direction().lengthSquared(); float term2 = 2.0f * QVector3D::dotProduct(centerToOrigin, ray.direction()); float term3 = centerToOrigin.lengthSquared() - m_radius * m_radius; float det = term2 * term2 - (4.0f * term1 * term3); if (term1 == 0.0f || det < 0.0f) { *minimum_t = qSNaN(); *maximum_t = qSNaN(); return false; } else if (det == 0.0f) { *minimum_t = *maximum_t = -term2 / (2.0f * term1); } else { float sqrtDet = sqrtf(det); float t1 = (-term2 - sqrtDet) / (2.0f * term1); float t2 = (-term2 + sqrtDet) / (2.0f * term1); if (t1 < t2) { *minimum_t = t1; *maximum_t = t2; } else { *minimum_t = t2; *maximum_t = t1; } } return true; } /*! Returns the t value at which \a ray first intersects the surface of this sphere, or not-a-number if there is no intersection. When the \a ray intersects this sphere, the return value is a parametric value that can be passed to QRay3D::point() to determine the actual intersection point, as shown in the following example: \code float t = sphere.intersection(ray); QVector3D pt; if (qIsNaN(t)) { qWarning("no intersection occurred"); else pt = ray.point(t); \endcode The \a ray might intersect at two points - as the ray passes through the sphere - one on the near side, one on the far side; where near and far are relative to the origin point of the ray. This function only returns the near intersection point. Only positive values on the ray are considered. This means that if the origin point of the ray is inside the sphere, there is only one solution, not two. To get the other solution, simply change the sign of the ray's direction vector. If the origin point of the ray is outside the sphere, and the direction points away from the sphere, then there will be no intersection. When the ray does not intersect the sphere in the positive direction, then the return value is not-a-number. \sa intersects(), QRay3D::point() */ float QSphere3D::intersection(const QRay3D &ray) const { float minimum_t, maximum_t; if (intersection(ray, &minimum_t, &maximum_t)) { if (minimum_t >= 0.0f) return minimum_t; else if (maximum_t >= 0.0f) return maximum_t; else return qSNaN(); } else { return qSNaN(); } } /*! \fn void QSphere3D::transform(const QMatrix4x4 &matrix) Transforms this sphere's center() and radius() according to \a matrix. It is assumed that \a matrix contains a uniform scale factor in the x, y, and z directions. Otherwise the radius() in the result is undefined. \sa transformed() */ /*! Returns the result of transforming this sphere's center() and radius() according to \a matrix. It is assumed that \a matrix contains a uniform scale factor in the x, y, and z directions. Otherwise the radius() in the result is undefined. \sa transform() */ QSphere3D QSphere3D::transformed(const QMatrix4x4 &matrix) const { return QSphere3D(matrix * m_center, matrix.mapVector(QVector3D(m_radius, 0, 0)).length()); } /*! \fn bool QSphere3D::operator==(const QSphere3D &sphere) const Returns true if this sphere is the same as \a sphere; false otherwise. \sa operator!=() */ /*! \fn bool QSphere3D::operator!=(const QSphere3D &sphere) const Returns true if this sphere is not the same as \a sphere; false otherwise. \sa operator==() */ /*! \fn bool qFuzzyCompare(const QSphere3D &sphere1, const QSphere3D &sphere2) \relates QSphere3D Returns true if \a sphere1 and \a sphere2 are almost equal; false otherwise. */ #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug dbg, const QSphere3D &sphere) { dbg.nospace() << "QSphere3D(center=(" << sphere.center().x() << ", " << sphere.center().y() << ", " << sphere.center().z() << "), radius=" << sphere.radius() << ')'; return dbg.space(); } #endif #ifndef QT_NO_DATASTREAM /*! \relates QSphere3D Writes the given \a sphere to the given \a stream and returns a reference to the stream. */ QDataStream &operator<<(QDataStream &stream, const QSphere3D &sphere) { stream << sphere.center(); stream << double(sphere.radius()); return stream; } /*! \relates QSphere3D Reads a 3D sphere from the given \a stream into the given \a sphere and returns a reference to the stream. */ QDataStream &operator>>(QDataStream &stream, QSphere3D &sphere) { QVector3D center; double radius; stream >> center; stream >> radius; sphere.setCenter(center); sphere.setRadius(float(radius)); return stream; } #endif QT_END_NAMESPACE qmath3d/QMath3d.txt0000664000175000017500000000000012760160064014027 0ustar wookeywookey