1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtTest>
#include <QtQuick3DRuntimeRender/private/qssgrenderray_p.h>
class intersection : public QObject
{
Q_OBJECT
public:
intersection() = default;
~intersection() = default;
private slots:
void test_aabbIntersectionv2();
void test_aabbIntersectionScaledv2();
void test_aabbIntersectionTranslatedv2();
void test_aabbIntersectionRotatedv2();
private:
static QSSGRenderRay::IntersectionResult intersectWithAABBv2_proxy(const QMatrix4x4 &inGlobalTransform,
const QSSGBounds3 &inBounds,
const QSSGRenderRay &ray)
{
QSSGRenderRay::RayData data = QSSGRenderRay::createRayData(inGlobalTransform, ray);
const auto hit = QSSGRenderRay::intersectWithAABBv2(data, inBounds);
return hit.intersects() ? QSSGRenderRay::createIntersectionResult(data, hit)
: QSSGRenderRay::IntersectionResult();
}
using IntersectionFunc = std::function<QSSGRenderRay::IntersectionResult(const QMatrix4x4 &, const QSSGBounds3 &, const QSSGRenderRay &)>;
void aabbIntersection(const IntersectionFunc &intersectFunc);
void aabbIntersectionScaled(const IntersectionFunc &intersectFunc);
void aabbIntersectionTranslated(const IntersectionFunc &intersectFunc);
void aabbIntersectionRotated(const IntersectionFunc &intersectFunc);
};
void intersection::test_aabbIntersectionv2()
{
aabbIntersection(&intersection::intersectWithAABBv2_proxy);
}
void intersection::test_aabbIntersectionScaledv2()
{
aabbIntersectionScaled(&intersection::intersectWithAABBv2_proxy);
}
void intersection::test_aabbIntersectionTranslatedv2()
{
aabbIntersectionTranslated(&intersection::intersectWithAABBv2_proxy);
}
void intersection::test_aabbIntersectionRotatedv2()
{
aabbIntersectionRotated(&intersection::intersectWithAABBv2_proxy);
}
void intersection::aabbIntersection(const IntersectionFunc &intersectFunc)
{
QSSGRenderRay::IntersectionResult res;
QMatrix4x4 globalTransform; // Identity
// Bounding box of size 1, placed at origin
QSSGBounds3 bounds { /*min=*/ QVector3D{ -0.5f, -0.5f, -0.5f }, /*max=*/ QVector3D{ 0.5f, 0.5f, 0.5f } };
// Pick ray goes directly down the z-axis and should hit directly in the middle of the box
auto pickRay = QSSGRenderRay(/*Origin=*/{0.0f, 0.0f, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, true);
{ // Test against a flat bounding volume
QSSGBounds3 boundsFlat { /*min=*/ QVector3D{ -1.0f, 0.0f, -1.0f }, /*max=*/ QVector3D{ 1.0f, 0.0f, 1.0f } };
res = intersectFunc(globalTransform, boundsFlat, QSSGRenderRay(/*Origin=*/{0.0f, 10.0f, 0.0f}, /*Direction=*/{0.0f, -1.0f, 0.0f}));
QCOMPARE(res.intersects, true);
}
{ // horizontal scan
const int steps = 10;
const float stepping = (bounds.maximum.x() - bounds.minimum.x()) / float(steps);
float rayX = bounds.minimum.x() - stepping;
const float rayY = (bounds.maximum.y() - bounds.minimum.y()) / 2.0f;
// First pick should fail, as it's just outside the bound's x extents
QVERIFY(rayX < bounds.maximum.x());
pickRay = QSSGRenderRay(/*Origin=*/{rayX, rayY, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, false);
// scan through the bounding box along the x-axis
rayX = bounds.minimum.x();
for (int i = 0; i != steps; ++i, rayX += stepping) {
pickRay = QSSGRenderRay(/*Origin=*/{rayX, rayY, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, true);
}
// Do one more step to go outside the box and test that we don't hit.
rayX += stepping;
pickRay = QSSGRenderRay(/*Origin=*/{rayX, rayY, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, false);
}
{ // vertical scan
const int steps = 10;
const float stepping = (bounds.maximum.y() - bounds.minimum.y()) / float(steps);
float rayY = bounds.minimum.y() - stepping;
const float rayX = (bounds.maximum.x() - bounds.minimum.x()) / 2.0f;
// First pick should fail, as it's just outside the bound's y extents
QVERIFY(rayY < bounds.maximum.y());
pickRay = QSSGRenderRay(/*Origin=*/{rayY, rayX, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, false);
// scan through the bounding box along the y-axis
rayY = bounds.minimum.y();
for (int i = 0; i != steps; ++i, rayY += stepping) {
pickRay = QSSGRenderRay(/*Origin=*/{rayY, rayX, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, true);
}
// Do one more step to go outside the box and test that we don't hit.
rayY += stepping;
pickRay = QSSGRenderRay(/*Origin=*/{rayY, rayX, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, false);
}
}
void intersection::aabbIntersectionScaled(const IntersectionFunc &intersectFunc)
{
QSSGRenderRay::IntersectionResult res;
QMatrix4x4 globalTransform; // Identity
// Bounding box of size 1, placed at origin
QSSGBounds3 bounds { /*min=*/ QVector3D{ -0.5f, -0.5f, -0.5f }, /*max=*/ QVector3D{ 0.5f, 0.5f, 0.5f } };
// Pick ray goes directly down the z-axis and should hit directly in the middle of the box
auto pickRay = QSSGRenderRay(/*Origin=*/{0.0f, 0.0f, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, true);
{ // Scale +
globalTransform.setToIdentity();
const float scale = 2.0f;
globalTransform.scale(scale);
const float rayY = bounds.minimum.y() * scale;
const float rayX = bounds.minimum.x() * scale;
pickRay = QSSGRenderRay(/*Origin=*/{rayY, rayX, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, true);
}
{ // Scale -
globalTransform.setToIdentity();
const float scale = -2.0f;
globalTransform.scale(scale);
const float rayY = bounds.maximum.y() * scale;
const float rayX = bounds.maximum.x() * scale;
pickRay = QSSGRenderRay(/*Origin=*/{rayY, rayX, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, true);
}
}
void intersection::aabbIntersectionTranslated(const IntersectionFunc &intersectFunc)
{
QSSGRenderRay::IntersectionResult res;
QMatrix4x4 globalTransform; // Identity
// Bounding box of size 1, placed at origin
QSSGBounds3 bounds { /*min=*/ QVector3D{ -0.5f, -0.5f, -0.5f }, /*max=*/ QVector3D{ 0.5f, 0.5f, 0.5f } };
// Pick ray goes directly down the z-axis and should hit directly in the middle of the box
auto pickRay = QSSGRenderRay(/*Origin=*/{0.0f, 0.0f, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, true);
{ // Translate 1
globalTransform.setToIdentity();
const float uniformTranslation = 1.0f;
globalTransform.translate(uniformTranslation, uniformTranslation);
// Verify that we can get a hit in the center of the bounding box
pickRay = QSSGRenderRay(/*Origin=*/{0.0f, 0.0f, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, false);
const float rayY = bounds.minimum.y() + uniformTranslation;
const float rayX = bounds.minimum.x() + uniformTranslation;
pickRay = QSSGRenderRay(/*Origin=*/{rayX, rayY, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, true);
}
{ // Translate 2
globalTransform.setToIdentity();
const float translationX = -1.0f;
const float translationY = 1.0f;
globalTransform.translate(translationX, translationY);
// Verify that we can get a hit in the center of the bounding box
pickRay = QSSGRenderRay(/*Origin=*/{0.0f, 0.0f, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, false);
const float rayY = bounds.minimum.y() + translationY;
const float rayX = bounds.minimum.x() + translationX;
pickRay = QSSGRenderRay(/*Origin=*/{rayX, rayY, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, true);
}
{ // Translate 3
globalTransform.setToIdentity();
const float uniformTranslation = -1.0f;
globalTransform.translate(uniformTranslation, uniformTranslation);
// Verify that we can get a hit in the center of the bounding box
pickRay = QSSGRenderRay(/*Origin=*/{0.0f, 0.0f, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, false);
const float rayY = bounds.maximum.y() + uniformTranslation;
const float rayX = bounds.maximum.x() + uniformTranslation;
pickRay = QSSGRenderRay(/*Origin=*/{rayX, rayY, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, true);
}
{ // Translate 4
globalTransform.setToIdentity();
const float translationX = 1.0f;
const float translationY = -1.0f;
globalTransform.translate(translationX, translationY);
// Verify that we can get a hit in the center of the bounding box
pickRay = QSSGRenderRay(/*Origin=*/{0.0f, 0.0f, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, false);
const float rayY = bounds.minimum.y() + translationY;
const float rayX = bounds.minimum.x() + translationX;
pickRay = QSSGRenderRay(/*Origin=*/{rayX, rayY, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, true);
}
}
void intersection::aabbIntersectionRotated(const IntersectionFunc &intersectFunc)
{
QSSGRenderRay::IntersectionResult res;
QMatrix4x4 globalTransform; // Identity
// Bounding box of size 1, placed at origin
QSSGBounds3 bounds { /*min=*/ QVector3D{ -0.5f, -0.5f, -0.5f }, /*max=*/ QVector3D{ 0.5f, 0.5f, 0.5f } };
// Pick ray goes directly down the z-axis and should hit directly in the middle of the box
auto pickRay = QSSGRenderRay(/*Origin=*/{0.0f, 0.0f, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, true);
{ // Rotate
globalTransform.setToIdentity();
const float rotation = 45.0f;
globalTransform.rotate(rotation, {0.0f, 0.0f, 1.0f});
// Verify that we can get a hit in the center of the bounding box
pickRay = QSSGRenderRay(/*Origin=*/{0.0f, 0.0f, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, true);
float rayX = bounds.minimum.x();
float rayY = bounds.minimum.y();
pickRay = QSSGRenderRay(/*Origin=*/{rayX, rayY, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, false);
rayX = bounds.maximum.x();
rayY = bounds.maximum.y();
pickRay = QSSGRenderRay(/*Origin=*/{rayX, rayY, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, false);
const auto center = bounds.center();
rayX = center.x();
rayY = center.y() + std::sqrt(bounds.dimensions().x() + bounds.dimensions().y()) * 0.5f;
pickRay = QSSGRenderRay(/*Origin=*/{rayX, rayY, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, true);
rayX = center.x() + std::sqrt(bounds.dimensions().x() + bounds.dimensions().y()) * 0.5f;
rayY = center.y();
pickRay = QSSGRenderRay(/*Origin=*/{rayX, rayY, 10.0f}, /*Direction=*/{0.0f, 0.0f, -1.0f});
res = intersectFunc(globalTransform, bounds, pickRay);
QCOMPARE(res.intersects, true);
}
}
QTEST_APPLESS_MAIN(intersection)
#include "tst_intersection.moc"
|