22#include <Qt3DRender/QGeometryRenderer>
24#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
25#include <Qt3DRender/QAttribute>
26#include <Qt3DRender/QBuffer>
27#include <Qt3DRender/QGeometry>
32#include <Qt3DCore/QAttribute>
33#include <Qt3DCore/QBuffer>
34#include <Qt3DCore/QGeometry>
52 double boxXMax = nodeBbox.
xMax;
53 double boxYMax = nodeBbox.
yMax;
54 double boxZMax = nodeBbox.
zMax;
57 if ( boxXMax == nodeBbox.
xMin )
59 if ( boxYMax == nodeBbox.
yMin )
61 if ( boxZMax == nodeBbox.
zMin )
65 double t1 = ( nodeBbox.
xMin - ray.
origin().x() ) * dirInv.x();
66 double t2 = ( boxXMax - ray.
origin().x() ) * dirInv.x();
67 double tmin = std::min( t1, t2 );
68 double tmax = std::max( t1, t2 );
70 t1 = ( nodeBbox.
yMin - ray.
origin().y() ) * dirInv.y();
71 t2 = ( boxYMax - ray.
origin().y() ) * dirInv.y();
72 tmin = std::max( tmin, std::min( std::min( t1, t2 ), tmax ) );
73 tmax = std::min( tmax, std::max( std::max( t1, t2 ), tmin ) );
75 t1 = ( nodeBbox.
zMin - ray.
origin().z() ) * dirInv.z();
76 t2 = ( boxZMax - ray.
origin().z() ) * dirInv.z();
77 tmin = std::max( tmin, std::min( std::min( t1, t2 ), tmax ) );
78 tmax = std::min( tmax, std::max( std::max( t1, t2 ), tmin ) );
80 return tmax > std::max( tmin, 0.0 );
86 bool rayTriangleIntersection(
const QgsRay3D &ray,
float maxDist,
const QVector3D &a,
const QVector3D &b,
const QVector3D &
c, QVector3D &uvw,
float &t )
91 const QVector3D ab = b - a;
92 const QVector3D ac =
c - a;
93 const QVector3D qp = ( ray.
origin() - ray.
point( maxDist ) );
95 const QVector3D n = QVector3D::crossProduct( ab, ac );
96 const float d = QVector3D::dotProduct( qp, n );
98 if ( d <= 0.0f || std::isnan( d ) )
101 const QVector3D ap = ray.
origin() - a;
102 t = QVector3D::dotProduct( ap, n );
104 if ( t < 0.0f || t > d )
107 const QVector3D e = QVector3D::crossProduct( qp, ap );
108 uvw.setY( QVector3D::dotProduct( ac, e ) );
110 if ( uvw.y() < 0.0f || uvw.y() > d )
113 uvw.setZ( -QVector3D::dotProduct( ab, e ) );
115 if ( uvw.z() < 0.0f || uvw.y() + uvw.z() > d )
118 const float ood = 1.0f / d;
120 uvw.setY( uvw.y() * ood );
121 uvw.setZ( uvw.z() * ood );
122 uvw.setX( 1.0f - uvw.y() - uvw.z() );
127 bool rayMeshIntersection( Qt3DRender::QGeometryRenderer *geometryRenderer,
const QgsRay3D &r,
float maxDist,
const QMatrix4x4 &worldTransform, QVector3D &intPt,
int &triangleIndex )
129 if ( geometryRenderer->primitiveType() != Qt3DRender::QGeometryRenderer::Triangles )
131 QgsDebugError( QString(
"Unsupported primitive type for intersection: " ).arg( geometryRenderer->primitiveType() ) );
134 if ( geometryRenderer->instanceCount() != 1 || geometryRenderer->indexOffset() != 0 || geometryRenderer->indexBufferByteOffset() != 0 || geometryRenderer->firstVertex() != 0 || geometryRenderer->firstInstance() != 0 )
136 QgsDebugError( QString(
"Unsupported geometry renderer for intersection." ) );
146 if ( attr->name() == Qt3DQAttribute::defaultPositionAttributeName() )
150 else if ( attr->attributeType() == Qt3DQAttribute::IndexAttribute )
162 if ( positionAttr->vertexBaseType() != Qt3DQAttribute::Float || positionAttr->vertexSize() != 3 )
164 QgsDebugError( QString(
"Unsupported position attribute: base type %1, vertex size %2" ).arg( positionAttr->vertexBaseType() ).arg( positionAttr->vertexSize() ) );
168 const QByteArray vertexBuf = positionAttr->buffer()->data();
169 const char *vertexPtr = vertexBuf.constData();
170 vertexPtr += positionAttr->byteOffset();
171 int vertexByteStride = positionAttr->byteStride() == 0 ? 3 *
sizeof( float ) : positionAttr->byteStride();
173 const uchar *indexPtrUChar =
nullptr;
174 const ushort *indexPtrUShort =
nullptr;
175 const uint *indexPtrUInt =
nullptr;
178 if ( indexAttr->byteStride() != 0 || indexAttr->vertexSize() != 1 )
180 QgsDebugError( QString(
"Unsupported index attribute: stride %1, vertex size %2" ).arg( indexAttr->byteStride() ).arg( indexAttr->vertexSize() ) );
184 const QByteArray indexBuf = indexAttr->buffer()->data();
185 if ( indexAttr->vertexBaseType() == Qt3DQAttribute::UnsignedByte )
187 indexPtrUChar =
reinterpret_cast<const uchar *
>( indexBuf.constData() + indexAttr->byteOffset() );
189 else if ( indexAttr->vertexBaseType() == Qt3DQAttribute::UnsignedShort )
191 indexPtrUShort =
reinterpret_cast<const ushort *
>( indexBuf.constData() + indexAttr->byteOffset() );
193 else if ( indexAttr->vertexBaseType() == Qt3DQAttribute::UnsignedInt )
195 indexPtrUInt =
reinterpret_cast<const uint *
>( indexBuf.constData() + indexAttr->byteOffset() );
199 QgsDebugError( QString(
"Unsupported index attribute: base type %1" ).arg( indexAttr->vertexBaseType() ) );
204 int vertexCount = geometryRenderer->vertexCount();
205 if ( vertexCount == 0 && indexAttr )
207 vertexCount = indexAttr->count();
209 if ( vertexCount == 0 )
211 vertexCount = positionAttr->count();
214 QVector3D intersectionPt, minIntersectionPt;
215 float minDistance = -1;
217 for (
int i = 0; i < vertexCount; i += 3 )
219 int v0index = 0, v1index = 0, v2index = 0;
226 else if ( indexPtrUShort )
228 v0index = indexPtrUShort[i];
229 v1index = indexPtrUShort[i + 1];
230 v2index = indexPtrUShort[i + 2];
232 else if ( indexPtrUChar )
234 v0index = indexPtrUChar[i];
235 v1index = indexPtrUChar[i + 1];
236 v2index = indexPtrUChar[i + 2];
238 else if ( indexPtrUInt )
240 v0index = indexPtrUInt[i];
241 v1index = indexPtrUInt[i + 1];
242 v2index = indexPtrUInt[i + 2];
247 const float *v0ptr =
reinterpret_cast<const float *
>( vertexPtr + v0index * vertexByteStride );
248 const float *v1ptr =
reinterpret_cast<const float *
>( vertexPtr + v1index * vertexByteStride );
249 const float *v2ptr =
reinterpret_cast<const float *
>( vertexPtr + v2index * vertexByteStride );
251 const QVector3D a( v0ptr[0], v0ptr[1], v0ptr[2] );
252 const QVector3D b( v1ptr[0], v1ptr[1], v1ptr[2] );
253 const QVector3D
c( v2ptr[0], v2ptr[1], v2ptr[2] );
258 const QVector3D tA = worldTransform * a;
259 const QVector3D tB = worldTransform * b;
260 const QVector3D tC = worldTransform *
c;
268 if (
QgsRayCastingUtils::rayTriangleIntersection( r, maxDist, tA, tB, tC, uvw, t ) ||
QgsRayCastingUtils::rayTriangleIntersection( r, maxDist, tA, tC, tB, uvw, t ) )
270 intersectionPt = r.
point( t * maxDist );
274 if ( minDistance == -1 || distance < minDistance )
276 triangleIndex =
static_cast<int>( i / 3 );
277 minDistance = distance;
278 minIntersectionPt = intersectionPt;
283 if ( minDistance != -1 )
285 intPt = minIntersectionPt;
float projectedDistance(const QVector3D &point) const
Returns the distance of the projection of a point to the ray.
QVector3D origin() const
Returns the origin of the ray.
QVector3D directionInversed() const
Returns a vector with the direction components inversed ( 1/x, 1/y, 1/z) This can be used as an optim...
QVector3D point(float distance) const
Returns the point along the ray with the specified distance from the ray's origin.
bool rayBoxIntersection(const QgsRay3D &ray, const QgsAABB &nodeBbox)
Tests whether an axis aligned box is intersected by a ray.
bool rayMeshIntersection(Qt3DRender::QGeometryRenderer *geometryRenderer, const QgsRay3D &r, float maxDist, const QMatrix4x4 &worldTransform, QVector3D &intPt, int &triangleIndex)
Tests whether a triangular mesh is intersected by a ray.
bool rayTriangleIntersection(const QgsRay3D &ray, float maxDist, const QVector3D &a, const QVector3D &b, const QVector3D &c, QVector3D &uvw, float &t)
Tests whether a triangle is intersected by a ray.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry
#define QgsDebugError(str)