QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsraycastingutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsraycastingutils_h.cpp
3 --------------------------------------
4 Date : June 2018
5 Copyright : (C) 2018 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsraycastingutils.h"
17
18#include "qgsaabb.h"
19#include "qgslogger.h"
20#include "qgsray3d.h"
21
22#include <Qt3DCore/QAttribute>
23#include <Qt3DCore/QBuffer>
24#include <Qt3DCore/QGeometry>
25#include <Qt3DRender/QGeometryRenderer>
26
28
29
30namespace QgsRayCastingUtils
31{
32 bool rayBoxIntersection( const QgsRay3D &ray, const QgsAABB &nodeBbox )
33 {
34 // https://tavianator.com/fast-branchless-raybounding-box-intersections/
35 // https://tavianator.com/fast-branchless-raybounding-box-intersections-part-2-nans/
36
37 const QVector3D dirInv = ray.directionInversed();
38
39 double boxXMax = nodeBbox.xMax;
40 double boxYMax = nodeBbox.yMax;
41 double boxZMax = nodeBbox.zMax;
42
43 // intersection() does not like yMin==yMax (excludes borders)
44 if ( boxXMax == nodeBbox.xMin )
45 boxXMax += 0.1;
46 if ( boxYMax == nodeBbox.yMin )
47 boxYMax += 0.1;
48 if ( boxZMax == nodeBbox.zMin )
49 boxZMax += 0.1;
50
51
52 double t1 = ( nodeBbox.xMin - ray.origin().x() ) * dirInv.x();
53 double t2 = ( boxXMax - ray.origin().x() ) * dirInv.x();
54 double tmin = std::min( t1, t2 );
55 double tmax = std::max( t1, t2 );
56
57 t1 = ( nodeBbox.yMin - ray.origin().y() ) * dirInv.y();
58 t2 = ( boxYMax - ray.origin().y() ) * dirInv.y();
59 tmin = std::max( tmin, std::min( std::min( t1, t2 ), tmax ) );
60 tmax = std::min( tmax, std::max( std::max( t1, t2 ), tmin ) );
61
62 t1 = ( nodeBbox.zMin - ray.origin().z() ) * dirInv.z();
63 t2 = ( boxZMax - ray.origin().z() ) * dirInv.z();
64 tmin = std::max( tmin, std::min( std::min( t1, t2 ), tmax ) );
65 tmax = std::min( tmax, std::max( std::max( t1, t2 ), tmin ) );
66
67 return tmax > std::max( tmin, 0.0 );
68 }
69
70
71 // copied from intersectsSegmentTriangle() from qt3d/src/render/backend/triangleboundingvolume.cpp
72 // by KDAB, licensed under the terms of LGPL
73 bool rayTriangleIntersection( const QgsRay3D &ray, float maxDist, const QVector3D &a, const QVector3D &b, const QVector3D &c, QVector3D &uvw, float &t )
74 {
75 // Note: a, b, c in clockwise order
76 // RealTime Collision Detection page 192
77
78 const QVector3D ab = b - a;
79 const QVector3D ac = c - a;
80 const QVector3D qp = ( ray.origin() - ray.point( maxDist ) );
81
82 const QVector3D n = QVector3D::crossProduct( ab, ac );
83 const float d = QVector3D::dotProduct( qp, n );
84
85 if ( d <= 0.0f || std::isnan( d ) )
86 return false;
87
88 const QVector3D ap = ray.origin() - a;
89 t = QVector3D::dotProduct( ap, n );
90
91 if ( t < 0.0f || t > d )
92 return false;
93
94 const QVector3D e = QVector3D::crossProduct( qp, ap );
95 uvw.setY( QVector3D::dotProduct( ac, e ) );
96
97 if ( uvw.y() < 0.0f || uvw.y() > d )
98 return false;
99
100 uvw.setZ( -QVector3D::dotProduct( ab, e ) );
101
102 if ( uvw.z() < 0.0f || uvw.y() + uvw.z() > d )
103 return false;
104
105 const float ood = 1.0f / d;
106 t *= ood;
107 uvw.setY( uvw.y() * ood );
108 uvw.setZ( uvw.z() * ood );
109 uvw.setX( 1.0f - uvw.y() - uvw.z() );
110
111 return true;
112 }
113
114 bool rayMeshIntersection( Qt3DRender::QGeometryRenderer *geometryRenderer, const QgsRay3D &r, float maxDist, const QMatrix4x4 &worldTransform, QVector3D &intPt, int &triangleIndex )
115 {
116 if ( geometryRenderer->primitiveType() != Qt3DRender::QGeometryRenderer::Triangles )
117 {
118 QgsDebugError( QString( "Unsupported primitive type for intersection: " ).arg( geometryRenderer->primitiveType() ) );
119 return false;
120 }
121 if ( geometryRenderer->instanceCount() != 1 || geometryRenderer->indexOffset() != 0 || geometryRenderer->indexBufferByteOffset() != 0 || geometryRenderer->firstVertex() != 0 || geometryRenderer->firstInstance() != 0 )
122 {
123 QgsDebugError( QString( "Unsupported geometry renderer for intersection." ) );
124 return false;
125 }
126
127 Qt3DCore::QGeometry *geometry = geometryRenderer->geometry();
128
129 Qt3DCore::QAttribute *positionAttr = nullptr;
130 Qt3DCore::QAttribute *indexAttr = nullptr;
131 for ( Qt3DCore::QAttribute *attr : geometry->attributes() )
132 {
133 if ( attr->name() == Qt3DCore::QAttribute::defaultPositionAttributeName() )
134 {
135 positionAttr = attr;
136 }
137 else if ( attr->attributeType() == Qt3DCore::QAttribute::IndexAttribute )
138 {
139 indexAttr = attr;
140 }
141 }
142
143 if ( !positionAttr )
144 {
145 QgsDebugError( "Could not find position attribute!" );
146 return false;
147 }
148
149 if ( positionAttr->vertexBaseType() != Qt3DCore::QAttribute::Float || positionAttr->vertexSize() != 3 )
150 {
151 QgsDebugError( QString( "Unsupported position attribute: base type %1, vertex size %2" ).arg( positionAttr->vertexBaseType() ).arg( positionAttr->vertexSize() ) );
152 return false;
153 }
154
155 const QByteArray vertexBuf = positionAttr->buffer()->data();
156 const char *vertexPtr = vertexBuf.constData();
157 vertexPtr += positionAttr->byteOffset();
158 int vertexByteStride = positionAttr->byteStride() == 0 ? 3 * sizeof( float ) : positionAttr->byteStride();
159
160 const uchar *indexPtrUChar = nullptr;
161 const ushort *indexPtrUShort = nullptr;
162 const uint *indexPtrUInt = nullptr;
163 if ( indexAttr )
164 {
165 if ( indexAttr->byteStride() != 0 || indexAttr->vertexSize() != 1 )
166 {
167 QgsDebugError( QString( "Unsupported index attribute: stride %1, vertex size %2" ).arg( indexAttr->byteStride() ).arg( indexAttr->vertexSize() ) );
168 return false;
169 }
170
171 const QByteArray indexBuf = indexAttr->buffer()->data();
172 if ( indexAttr->vertexBaseType() == Qt3DCore::QAttribute::UnsignedByte )
173 {
174 indexPtrUChar = reinterpret_cast<const uchar *>( indexBuf.constData() + indexAttr->byteOffset() );
175 }
176 else if ( indexAttr->vertexBaseType() == Qt3DCore::QAttribute::UnsignedShort )
177 {
178 indexPtrUShort = reinterpret_cast<const ushort *>( indexBuf.constData() + indexAttr->byteOffset() );
179 }
180 else if ( indexAttr->vertexBaseType() == Qt3DCore::QAttribute::UnsignedInt )
181 {
182 indexPtrUInt = reinterpret_cast<const uint *>( indexBuf.constData() + indexAttr->byteOffset() );
183 }
184 else
185 {
186 QgsDebugError( QString( "Unsupported index attribute: base type %1" ).arg( indexAttr->vertexBaseType() ) );
187 return false;
188 }
189 }
190
191 int vertexCount = geometryRenderer->vertexCount();
192 if ( vertexCount == 0 && indexAttr )
193 {
194 vertexCount = indexAttr->count();
195 }
196 if ( vertexCount == 0 )
197 {
198 vertexCount = positionAttr->count();
199 }
200
201 QVector3D intersectionPt, minIntersectionPt;
202 float minDistance = -1;
203
204 for ( int i = 0; i < vertexCount; i += 3 )
205 {
206 int v0index = 0, v1index = 0, v2index = 0;
207 if ( !indexAttr )
208 {
209 v0index = i;
210 v1index = i + 1;
211 v2index = i + 2;
212 }
213 else if ( indexPtrUShort )
214 {
215 v0index = indexPtrUShort[i];
216 v1index = indexPtrUShort[i + 1];
217 v2index = indexPtrUShort[i + 2];
218 }
219 else if ( indexPtrUChar )
220 {
221 v0index = indexPtrUChar[i];
222 v1index = indexPtrUChar[i + 1];
223 v2index = indexPtrUChar[i + 2];
224 }
225 else if ( indexPtrUInt )
226 {
227 v0index = indexPtrUInt[i];
228 v1index = indexPtrUInt[i + 1];
229 v2index = indexPtrUInt[i + 2];
230 }
231 else
232 Q_ASSERT( false );
233
234 const float *v0ptr = reinterpret_cast<const float *>( vertexPtr + v0index * vertexByteStride );
235 const float *v1ptr = reinterpret_cast<const float *>( vertexPtr + v1index * vertexByteStride );
236 const float *v2ptr = reinterpret_cast<const float *>( vertexPtr + v2index * vertexByteStride );
237
238 const QVector3D a( v0ptr[0], v0ptr[1], v0ptr[2] );
239 const QVector3D b( v1ptr[0], v1ptr[1], v1ptr[2] );
240 const QVector3D c( v2ptr[0], v2ptr[1], v2ptr[2] );
241
242 // Currently the worldTransform only has vertical offset, so this could be optimized by applying the transform
243 // to the ray and the resulting intersecting point instead of all triangles
244 // Need to check for potential performance gains.
245 const QVector3D tA = worldTransform * a;
246 const QVector3D tB = worldTransform * b;
247 const QVector3D tC = worldTransform * c;
248
249 QVector3D uvw;
250 float t = 0;
251
252 // We're testing both triangle orientations here and ignoring the culling mode.
253 // We should probably respect the culling mode used for the entity and perform a
254 // single test using the properly oriented triangle.
255 if ( QgsRayCastingUtils::rayTriangleIntersection( r, maxDist, tA, tB, tC, uvw, t ) || QgsRayCastingUtils::rayTriangleIntersection( r, maxDist, tA, tC, tB, uvw, t ) )
256 {
257 intersectionPt = r.point( t * maxDist );
258 const float distance = r.projectedDistance( intersectionPt );
259
260 // we only want the first intersection of the ray with the mesh (closest to the ray origin)
261 if ( minDistance == -1 || distance < minDistance )
262 {
263 triangleIndex = static_cast<int>( i / 3 );
264 minDistance = distance;
265 minIntersectionPt = intersectionPt;
266 }
267 }
268 }
269
270 if ( minDistance != -1 )
271 {
272 intPt = minIntersectionPt;
273 return true;
274 }
275 else
276 return false;
277 }
278} // namespace QgsRayCastingUtils
279
280
float yMax
Definition qgsaabb.h:102
float xMax
Definition qgsaabb.h:101
float xMin
Definition qgsaabb.h:98
float zMax
Definition qgsaabb.h:103
float yMin
Definition qgsaabb.h:99
float zMin
Definition qgsaabb.h:100
float projectedDistance(const QVector3D &point) const
Returns the distance of the projection of a point to the ray.
Definition qgsray3d.cpp:47
QVector3D origin() const
Returns the origin of the ray.
Definition qgsray3d.h:44
QVector3D directionInversed() const
Returns a vector with the direction components inversed ( 1/x, 1/y, 1/z) This can be used as an optim...
Definition qgsray3d.h:57
QVector3D point(float distance) const
Returns the point along the ray with the specified distance from the ray's origin.
Definition qgsray3d.cpp:68
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
#define QgsDebugError(str)
Definition qgslogger.h:59