QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
122 || geometryRenderer->indexOffset() != 0
123 || geometryRenderer->indexBufferByteOffset() != 0
124 || geometryRenderer->firstVertex() != 0
125 || geometryRenderer->firstInstance() != 0 )
126 {
127 QgsDebugError( QString( "Unsupported geometry renderer for intersection." ) );
128 return false;
129 }
130
131 Qt3DCore::QGeometry *geometry = geometryRenderer->geometry();
132
133 Qt3DCore::QAttribute *positionAttr = nullptr;
134 Qt3DCore::QAttribute *indexAttr = nullptr;
135 for ( Qt3DCore::QAttribute *attr : geometry->attributes() )
136 {
137 if ( attr->name() == Qt3DCore::QAttribute::defaultPositionAttributeName() )
138 {
139 positionAttr = attr;
140 }
141 else if ( attr->attributeType() == Qt3DCore::QAttribute::IndexAttribute )
142 {
143 indexAttr = attr;
144 }
145 }
146
147 if ( !positionAttr )
148 {
149 QgsDebugError( "Could not find position attribute!" );
150 return false;
151 }
152
153 if ( positionAttr->vertexBaseType() != Qt3DCore::QAttribute::Float || positionAttr->vertexSize() != 3 )
154 {
155 QgsDebugError( QString( "Unsupported position attribute: base type %1, vertex size %2" ).arg( positionAttr->vertexBaseType() ).arg( positionAttr->vertexSize() ) );
156 return false;
157 }
158
159 const QByteArray vertexBuf = positionAttr->buffer()->data();
160 const char *vertexPtr = vertexBuf.constData();
161 vertexPtr += positionAttr->byteOffset();
162 int vertexByteStride = positionAttr->byteStride() == 0 ? 3 * sizeof( float ) : positionAttr->byteStride();
163
164 const uchar *indexPtrUChar = nullptr;
165 const ushort *indexPtrUShort = nullptr;
166 const uint *indexPtrUInt = nullptr;
167 if ( indexAttr )
168 {
169 if ( indexAttr->byteStride() != 0 || indexAttr->vertexSize() != 1 )
170 {
171 QgsDebugError( QString( "Unsupported index attribute: stride %1, vertex size %2" ).arg( indexAttr->byteStride() ).arg( indexAttr->vertexSize() ) );
172 return false;
173 }
174
175 const QByteArray indexBuf = indexAttr->buffer()->data();
176 if ( indexAttr->vertexBaseType() == Qt3DCore::QAttribute::UnsignedByte )
177 {
178 indexPtrUChar = reinterpret_cast<const uchar *>( indexBuf.constData() + indexAttr->byteOffset() );
179 }
180 else if ( indexAttr->vertexBaseType() == Qt3DCore::QAttribute::UnsignedShort )
181 {
182 indexPtrUShort = reinterpret_cast<const ushort *>( indexBuf.constData() + indexAttr->byteOffset() );
183 }
184 else if ( indexAttr->vertexBaseType() == Qt3DCore::QAttribute::UnsignedInt )
185 {
186 indexPtrUInt = reinterpret_cast<const uint *>( indexBuf.constData() + indexAttr->byteOffset() );
187 }
188 else
189 {
190 QgsDebugError( QString( "Unsupported index attribute: base type %1" ).arg( indexAttr->vertexBaseType() ) );
191 return false;
192 }
193 }
194
195 int vertexCount = geometryRenderer->vertexCount();
196 if ( vertexCount == 0 && indexAttr )
197 {
198 vertexCount = indexAttr->count();
199 }
200 if ( vertexCount == 0 )
201 {
202 vertexCount = positionAttr->count();
203 }
204
205 QVector3D intersectionPt, minIntersectionPt;
206 float minDistance = -1;
207
208 for ( int i = 0; i < vertexCount; i += 3 )
209 {
210 int v0index = 0, v1index = 0, v2index = 0;
211 if ( !indexAttr )
212 {
213 v0index = i;
214 v1index = i + 1;
215 v2index = i + 2;
216 }
217 else if ( indexPtrUShort )
218 {
219 v0index = indexPtrUShort[i];
220 v1index = indexPtrUShort[i + 1];
221 v2index = indexPtrUShort[i + 2];
222 }
223 else if ( indexPtrUChar )
224 {
225 v0index = indexPtrUChar[i];
226 v1index = indexPtrUChar[i + 1];
227 v2index = indexPtrUChar[i + 2];
228 }
229 else if ( indexPtrUInt )
230 {
231 v0index = indexPtrUInt[i];
232 v1index = indexPtrUInt[i + 1];
233 v2index = indexPtrUInt[i + 2];
234 }
235 else
236 Q_ASSERT( false );
237
238 const float *v0ptr = reinterpret_cast<const float *>( vertexPtr + v0index * vertexByteStride );
239 const float *v1ptr = reinterpret_cast<const float *>( vertexPtr + v1index * vertexByteStride );
240 const float *v2ptr = reinterpret_cast<const float *>( vertexPtr + v2index * vertexByteStride );
241
242 const QVector3D a( v0ptr[0], v0ptr[1], v0ptr[2] );
243 const QVector3D b( v1ptr[0], v1ptr[1], v1ptr[2] );
244 const QVector3D c( v2ptr[0], v2ptr[1], v2ptr[2] );
245
246 // Currently the worldTransform only has vertical offset, so this could be optimized by applying the transform
247 // to the ray and the resulting intersecting point instead of all triangles
248 // Need to check for potential performance gains.
249 const QVector3D tA = worldTransform * a;
250 const QVector3D tB = worldTransform * b;
251 const QVector3D tC = worldTransform * c;
252
253 QVector3D uvw;
254 float t = 0;
255
256 // We're testing both triangle orientations here and ignoring the culling mode.
257 // We should probably respect the culling mode used for the entity and perform a
258 // single test using the properly oriented triangle.
259 if ( QgsRayCastingUtils::rayTriangleIntersection( r, maxDist, tA, tB, tC, uvw, t ) || QgsRayCastingUtils::rayTriangleIntersection( r, maxDist, tA, tC, tB, uvw, t ) )
260 {
261 intersectionPt = r.point( t * maxDist );
262 const float distance = r.projectedDistance( intersectionPt );
263
264 // we only want the first intersection of the ray with the mesh (closest to the ray origin)
265 if ( minDistance == -1 || distance < minDistance )
266 {
267 triangleIndex = static_cast<int>( i / 3 );
268 minDistance = distance;
269 minIntersectionPt = intersectionPt;
270 }
271 }
272 }
273
274 if ( minDistance != -1 )
275 {
276 intPt = minIntersectionPt;
277 return true;
278 }
279 else
280 return false;
281 }
282} // namespace QgsRayCastingUtils
283
284
float yMax
Definition qgsaabb.h:100
float xMax
Definition qgsaabb.h:99
float xMin
Definition qgsaabb.h:96
float zMax
Definition qgsaabb.h:101
float yMin
Definition qgsaabb.h:97
float zMin
Definition qgsaabb.h:98
float projectedDistance(const QVector3D &point) const
Returns the distance of the projection of a point to the ray.
Definition qgsray3d.cpp:41
QVector3D origin() const
Returns the origin of the ray.
Definition qgsray3d.h:43
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:56
QVector3D point(float distance) const
Returns the point along the ray with the specified distance from the ray's origin.
Definition qgsray3d.cpp:62
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