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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QSignalSpy>
#include <QTest>
#include <QtQuick/QQuickItem>
#include <QtQuick3D/private/qquick3dviewport_p.h>
#include <QtQuick3D/private/qquick3dmodel_p.h>
#include <QtQuick3D/private/qquick3dpickresult_p.h>
#include <QtQuick3D/private/qquick3ditem2d_p.h>
#include <QtQuick3DRuntimeRender/private/qssgrenderray_p.h>
#include "../shared/util.h"
class CustomGeometry : public QQuick3DGeometry
{
Q_OBJECT
Q_PROPERTY(int count READ count WRITE setCount NOTIFY countChanged)
QML_NAMED_ELEMENT(CustomGeometry)
public:
CustomGeometry(uint count);
int count() { return m_count; }
void setCount(int count);
signals:
void countChanged();
private:
struct Vertex {
QVector3D position;
QVector3D normal;
QVector2D uv;
};
void updateData();
uint m_count = 500;
};
CustomGeometry::CustomGeometry(uint count)
{
m_count = count;
updateData();
}
void CustomGeometry::setCount(int count)
{
m_count = count;
updateData();
emit countChanged();
}
void CustomGeometry::updateData()
{
QVector<Vertex> vertices;
QVector<QPoint> coords;
int rowCount = m_count;
int columnCount = m_count;
float uvX = 1.0f / float(rowCount - 1);
float uvY = 1.0f / float(columnCount - 1);
for (int i = 0; i < columnCount; i++) {
for (int j = 0; j < rowCount; j++) {
Vertex v;
float x = 4.0f / (columnCount-1) * i - 2.0f;
float z = 4.0f / (rowCount-1) * j - 2.0f;
v.position = QVector3D(x, .0f, z);
v.normal = QVector3D(.0f, .0f, .0f);
v.uv = QVector2D(j * uvX, i * uvY);
vertices.push_back(v);
coords.push_back(QPoint(i, j));
}
}
QVector3D boundsMin = QVector3D(-2.0f, -1.0f, -2.0f);
QVector3D boundsMax = QVector3D(2.0f, 1.0f, 2.0f);
setBounds(boundsMin, boundsMax);
QVector<quint32> indices;
for (int i = 0; i < columnCount - 1; i++) {
for (int j = 0; j < rowCount -1; j++) {
indices.push_back(i * rowCount + j);
indices.push_back(i * rowCount + j + 1);
indices.push_back(i * rowCount + j + rowCount);
indices.push_back(i * rowCount + j + 1);
indices.push_back(i * rowCount + j + 1 + rowCount);
indices.push_back(i * rowCount + j + rowCount);
}
}
setStride(sizeof(Vertex));
setPrimitiveType(QQuick3DGeometry::PrimitiveType::Triangles);
addAttribute(QQuick3DGeometry::Attribute::PositionSemantic,
0,
QQuick3DGeometry::Attribute::F32Type);
addAttribute(QQuick3DGeometry::Attribute::TexCoord0Semantic,
sizeof(QVector3D) * 2,
QQuick3DGeometry::Attribute::F32Type);
addAttribute(QQuick3DGeometry::Attribute::NormalSemantic,
sizeof(QVector3D),
QQuick3DGeometry::Attribute::F32Type);
addAttribute(QQuick3DGeometry::Attribute::IndexSemantic,
0,
QQuick3DGeometry::Attribute::U32Type);
QByteArray vertexBuffer(reinterpret_cast<char *>(vertices.data()), vertices.size() * sizeof(Vertex));
setVertexData(vertexBuffer);
QByteArray indexBuffer(reinterpret_cast<char *>(indices.data()), indices.size() * sizeof(quint32));
setIndexData(indexBuffer);
update();
}
class tst_Picking : public QQuick3DDataTest
{
Q_OBJECT
private Q_SLOTS:
void initTestCase() override;
void test_view_picking();
void test_ray_picking();
void test_object_picking2();
void test_item_picking();
void test_picking_QTBUG_111997();
void test_picking_corner_case();
void test_triangleIntersect();
private:
QQuickItem *find2DChildIn3DNode(QQuickView *view, const QString &objectName, const QString &itemName);
};
void tst_Picking::initTestCase()
{
QQuick3DDataTest::initTestCase();
if (!initialized())
return;
}
QQuickItem *tst_Picking::find2DChildIn3DNode(QQuickView *view, const QString &objectName, const QString &itemName)
{
QQuick3DNode *obj = view->rootObject()->findChild<QQuick3DNode*>(objectName);
if (!obj)
return nullptr;
QQuickItem *subsceneRoot = obj->findChild<QQuickItem *>();
if (!subsceneRoot)
subsceneRoot = obj->findChild<QQuick3DItem2D *>()->contentItem();
if (!subsceneRoot)
return nullptr;
QObject *child = subsceneRoot->findChild<QObject *>(itemName);
return static_cast<QQuickItem *>(child);
}
void tst_Picking::test_view_picking()
{
QScopedPointer<QQuickView> view(createView(QLatin1String("picking.qml"), QSize(400, 400)));
QVERIFY(view);
QVERIFY(QTest::qWaitForWindowExposed(view.data()));
QQuick3DViewport *view3d = view->findChild<QQuick3DViewport *>(QStringLiteral("view"));
QVERIFY(view3d);
QQuick3DModel *model1 = view3d->findChild<QQuick3DModel *>(QStringLiteral("model1"));
QVERIFY(model1);
QQuick3DModel *model2 = view3d->findChild<QQuick3DModel *>(QStringLiteral("model2"));
QVERIFY(model2);
QQuick3DModel *instancedModel = view3d->findChild<QQuick3DModel *>(QStringLiteral("instancedModel"));
QVERIFY(instancedModel);
QQuickItem *item2d = qmlobject_cast<QQuickItem *>(find2DChildIn3DNode(view.data(), "item2dNode", "item2d"));
QVERIFY(item2d);
const qreal dpr = view->devicePixelRatio();
if (dpr != 1.0) {
QSKIP("Test uses window positions to get exact values and those assume DPR of 1.0");
}
// Pick nearest based on viewport position
// Center of model1
auto result = view3d->pick(200, 200);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(result.objectHit(), model1);
QCOMPARE(result.distance(), 550.0f);
QCOMPARE(result.uvPosition(), QVector2D(0.5f, 0.5f));
QCOMPARE(result.scenePosition(), QVector3D(0.0f, 0.0f, 50.0f));
QCOMPARE(result.position(), QVector3D(0.0f, 0.0f, 50.0f));
QCOMPARE(result.sceneNormal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.normal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.instanceIndex(), 0);
QCOMPARE(result.itemHit(), nullptr);
// Upper right corner of model1
result = view3d->pick(250, 150);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(result.objectHit(), model1);
QCOMPARE(result.distance(), 550.0f);
QCOMPARE(result.uvPosition(), QVector2D(1.0f, 1.0f));
QCOMPARE(result.scenePosition(), QVector3D(50.0f, 50.0f, 50.0f));
QCOMPARE(result.position(), QVector3D(50.0f, 50.0f, 50.0f));
QCOMPARE(result.sceneNormal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.normal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.instanceIndex(), 0);
QCOMPARE(result.itemHit(), nullptr);
// Just outside model1's upper right corner, so should hit the model behind (model2)
// This one might cause some floating point grief
result = view3d->pick(251, 151);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(result.objectHit(), model2);
QCOMPARE(result.distance(), 600.0f);
QCOMPARE(result.uvPosition(), QVector2D(0.5099999904632568, 0.4899999797344208));
QCOMPARE(result.scenePosition(), QVector3D(51.0f, 49.0f, -0.000003814697265625));
QCOMPARE(result.position(), QVector3D(1.0f, -1.0f, 49.999996185302734f));
QCOMPARE(result.sceneNormal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.normal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.instanceIndex(), 0);
QCOMPARE(result.itemHit(), nullptr);
// Upper right corner of model2
result = view3d->pick(300, 100);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(result.objectHit(), model2);
QCOMPARE(result.distance(), 600.0f);
QCOMPARE(result.uvPosition(), QVector2D(1.0f, 1.0f));
QCOMPARE(result.scenePosition(), QVector3D(100.0f, 100.0f, 0.0f));
QCOMPARE(result.position(), QVector3D(50.0f, 50.0f, 50.0f));
QCOMPARE(result.sceneNormal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.normal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.instanceIndex(), 0);
QCOMPARE(result.itemHit(), nullptr);
// Just outside model2's upper right corner, so there should be no hit
result = view3d->pick(301, 99);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Null);
QCOMPARE(result.objectHit(), nullptr);
QCOMPARE(result.itemHit(), nullptr);
// Center of the third entry in the instance table
result = view3d->pick(350, 200);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(result.objectHit(), instancedModel);
QCOMPARE(result.distance(), 550.0f);
QCOMPARE(result.uvPosition(), QVector2D(0.0f, 0.5f));
QCOMPARE(result.scenePosition(), QVector3D(150.0f, 0.0f, 50.0f));
QCOMPARE(result.position(), QVector3D(-50.0f, 0.0f, 50.0f));
QCOMPARE(result.sceneNormal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.normal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.instanceIndex(), 2);
QCOMPARE(result.itemHit(), nullptr);
// The bottom right of instancedModel,
// overlapped by model1 and then model2, should hit model1
result = view3d->pick(225, 175);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(result.objectHit(), model1);
QCOMPARE(result.distance(), 550.0f);
QCOMPARE(result.uvPosition(), QVector2D(0.75f, 0.75f));
QCOMPARE(result.scenePosition(), QVector3D(25.0f, 25.0f, 50.0f));
QCOMPARE(result.position(), QVector3D(25.0f, 25.0f, 50.0f));
QCOMPARE(result.sceneNormal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.normal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.instanceIndex(), 0);
QCOMPARE(result.itemHit(), nullptr);
// Pick one specific based on viewport position
// Center of model1, overlapping model2,
// only check for model2, so should hit model2
result = view3d->pick(200, 200, model2);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(result.objectHit(), model2);
QCOMPARE(result.distance(), 600.0f);
QCOMPARE(result.uvPosition(), QVector2D(0.0f, 0.0f));
QCOMPARE(result.scenePosition(), QVector3D(0.0f, 0.0f, 0.0f));
QCOMPARE(result.position(), QVector3D(-50.0f, -50.0f, 50.0f));
QCOMPARE(result.sceneNormal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.normal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.instanceIndex(), 0);
QCOMPARE(result.itemHit(), nullptr);
// The bottom right of instancedModel,
// overlapped model1 and then model2,
// only check for instancedModel, should hit instancedModel
result = view3d->pick(225, 175, instancedModel);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(result.objectHit(), instancedModel);
QCOMPARE(result.distance(), 650.0f);
QCOMPARE(result.uvPosition(), QVector2D(1.0f, 0.0f));
QCOMPARE(result.scenePosition(), QVector3D(25.0f, 25.0f, -50.0f));
QCOMPARE(result.position(), QVector3D(50.0f, -50.0f, 50.0f));
QCOMPARE(result.sceneNormal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.normal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.instanceIndex(), 1);
QCOMPARE(result.itemHit(), nullptr);
// Pick subset based on viewport position
// Upper right corner of model1, center of model2
// Check for model2 only
// Should hit model2, ignore model1
QJSEngine engine;
QJSValue array = engine.newArray(1);
array.setProperty(0, engine.newQObject(model2));
auto resultList = view3d->pickSubset(250, 150, array);
QCOMPARE(resultList.size(), 1);
QCOMPARE(resultList[0].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultList[0].objectHit(), model2);
QCOMPARE(resultList[0].distance(), 600.0f);
QCOMPARE(resultList[0].uvPosition(), QVector2D(0.5f, 0.5f));
QCOMPARE(resultList[0].scenePosition(), QVector3D(50.0f, 50.0f, 0.0f));
QCOMPARE(resultList[0].position(), QVector3D(0.0f, 0.0f, 50.0f));
QCOMPARE(resultList[0].sceneNormal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(resultList[0].normal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(resultList[0].instanceIndex(), 0);
QCOMPARE(resultList[0].itemHit(), nullptr);
// The bottom right of instancedModel,
// overlapped model1 and then model2,
// Check for both model2 and instancedModel
// Should hit both and ignore model1
array = engine.newArray(2);
array.setProperty(0, engine.newQObject(model2));
array.setProperty(1, engine.newQObject(instancedModel));
resultList = view3d->pickSubset(225, 175, array);
QCOMPARE(resultList.size(), 2);
QCOMPARE(resultList[0].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultList[0].objectHit(), model2);
QCOMPARE(resultList[0].distance(), 600.0f);
QCOMPARE(resultList[0].uvPosition(), QVector2D(0.25f, 0.25f));
QCOMPARE(resultList[0].scenePosition(), QVector3D(25.0f, 25.0f, 0.0f));
QCOMPARE(resultList[0].position(), QVector3D(-25.0f, -25.0f, 50.0f));
QCOMPARE(resultList[0].sceneNormal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(resultList[0].normal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(resultList[0].instanceIndex(), 0);
QCOMPARE(resultList[0].itemHit(), nullptr);
QCOMPARE(resultList[1].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultList[1].objectHit(), instancedModel);
QCOMPARE(resultList[1].distance(), 650.0f);
QCOMPARE(resultList[1].uvPosition(), QVector2D(1.0f, 0.0f));
QCOMPARE(resultList[1].scenePosition(), QVector3D(25.0f, 25.0f, -50.0f));
QCOMPARE(resultList[1].position(), QVector3D(50.0f, -50.0f, 50.0f));
QCOMPARE(resultList[1].sceneNormal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(resultList[1].normal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(resultList[1].instanceIndex(), 1);
QCOMPARE(resultList[1].itemHit(), nullptr);
// Enable the 2D item
item2d->setEnabled(true);
QTest::qWait(100);
// Then picking on top of model1 should not pick it anymore
result = view3d->pick(200, 200);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(result.itemHit(), item2d);
QCOMPARE(result.distance(), 400.0f);
QCOMPARE(result.uvPosition(), QVector2D(75.0f, 75.0f));
QCOMPARE(result.scenePosition(), QVector3D(0.0f, 0.0f, 200.0f));
QCOMPARE(result.position(), QVector3D(0.0f, 0.0f, 0.0f));
QCOMPARE(result.sceneNormal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.normal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.instanceIndex(), -1);
QCOMPARE(result.objectHit(), nullptr);
// Hide the 2D item
item2d->setVisible(false);
QTest::qWait(100);
// Then picking on top of model1 should pick it again
result = view3d->pick(200, 200);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(result.objectHit(), model1);
// Pick all based on viewport position
// Center of model1, bottom left corner of model2
resultList = view3d->pickAll(200, 200);
QCOMPARE(resultList.size(), 2);
QCOMPARE(resultList[0].objectHit(), model1);
QCOMPARE(resultList[1].objectHit(), model2);
// Add the 2D item
item2d->setVisible(true);
QTest::qWait(100);
resultList = view3d->pickAll(200, 200);
QCOMPARE(resultList.size(), 3);
QCOMPARE(resultList[0].hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(resultList[0].itemHit(), item2d);
QCOMPARE(resultList[1].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultList[1].objectHit(), model1);
QCOMPARE(resultList[2].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultList[2].objectHit(), model2);
// Hide the 2D item
item2d->setVisible(false);
QTest::qWait(100);
// Top right corner of model1, center of model2
resultList = view3d->pickAll(250, 150);
QCOMPARE(resultList.size(), 2);
QCOMPARE(resultList[0].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultList[0].objectHit(), model1);
QCOMPARE(resultList[1].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultList[1].objectHit(), model2);
// Just outside model1's upper right corner, so should hit the model behind (model2)
resultList = view3d->pickAll(251, 151);
QCOMPARE(resultList.size(), 1);
QCOMPARE(resultList[0].objectHit(), model2);
// The bottom right of the second entry in the instance table,
// overlapping model1 and model2
resultList = view3d->pickAll(225, 175);
QCOMPARE(resultList.size(), 3);
QCOMPARE(resultList[0].objectHit(), model1);
QCOMPARE(resultList[1].objectHit(), model2);
QCOMPARE(resultList[2].objectHit(), instancedModel);
QCOMPARE(resultList[2].instanceIndex(), 1);
// Just outside model2's upper right corner, so there should be no hit
resultList = view3d->pickAll(301, 99);
QVERIFY(resultList.isEmpty());
}
void tst_Picking::test_ray_picking()
{
QScopedPointer<QQuickView> view(createView(QLatin1String("picking.qml"), QSize(400, 400)));
QVERIFY(view);
QVERIFY(QTest::qWaitForWindowExposed(view.data()));
QQuick3DViewport *view3d = view->findChild<QQuick3DViewport *>(QStringLiteral("view"));
QVERIFY(view3d);
QQuick3DModel *model1 = view3d->findChild<QQuick3DModel *>(QStringLiteral("model1"));
QVERIFY(model1);
QQuick3DModel *model2 = view3d->findChild<QQuick3DModel *>(QStringLiteral("model2"));
QVERIFY(model2);
QQuick3DModel *instancedModel = view3d->findChild<QQuick3DModel *>(QStringLiteral("instancedModel"));
QVERIFY(instancedModel);
QQuickItem *item2d = qmlobject_cast<QQuickItem *>(find2DChildIn3DNode(view.data(), "item2dNode", "item2d"));
QVERIFY(item2d);
item2d->setEnabled(true);
item2d->setVisible(true);
QTest::qWait(100);
// Ray based picking one result
// Down the z axis from 0,0,100 (towards 0,0,0)
QVector3D origin(0.0f, 0.0f, 100.0f);
QVector3D direction(0.0f, 0.0f, -1.0f);
auto result = view3d->rayPick(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(result.objectHit(), model1);
QCOMPARE(result.distance(), 50.0f);
QCOMPARE(result.uvPosition(), QVector2D(0.5f, 0.5f));
QCOMPARE(result.scenePosition(), QVector3D(0.0f, 0.0f, 50.0f));
QCOMPARE(result.position(), QVector3D(0.0f, 0.0f, 50.0f));
QCOMPARE(result.sceneNormal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.normal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.instanceIndex(), 0);
QCOMPARE(result.itemHit(), nullptr);
// Down the z axis from 0,0,250 (towards 0,0,0) through the item2d
origin = QVector3D(0.0f, 0.0f, 250.0f);
result = view3d->rayPick(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(result.itemHit(), item2d);
QCOMPARE(result.distance(), 50.0f);
QCOMPARE(result.uvPosition(), QVector2D(75.0f, 75.0f));
QCOMPARE(result.scenePosition(), QVector3D(0.0f, 0.0f, 200.0f));
QCOMPARE(result.position(), QVector3D(0.0f, 0.0f, 0.0f));
QCOMPARE(result.sceneNormal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.normal(), QVector3D(0.0f, 0.0f, 1.0f));
QCOMPARE(result.instanceIndex(), -1);
QCOMPARE(result.objectHit(), nullptr);
// Up the z axis from 0, 0, -101 (towards 0,0,0)
origin = QVector3D(0.0f, 0.0f, -101.0f);
direction = QVector3D(0.0f, 0.0f, 1.0f);
result = view3d->rayPick(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(result.objectHit(), model2);
QCOMPARE(result.distance(), 1.0f);
QCOMPARE(result.uvPosition(), QVector2D(1.0f, 0.0f));
QCOMPARE(result.scenePosition(), QVector3D(0.0f, 0.0f, -100.0f));
QCOMPARE(result.position(), QVector3D(-50.0f, -50.0f, -50.0f));
QCOMPARE(result.sceneNormal(), QVector3D(0.0f, 0.0f, -1.0f));
QCOMPARE(result.normal(), QVector3D(0.0f, 0.0f, -1.0f));
QCOMPARE(result.instanceIndex(), 0);
QCOMPARE(result.itemHit(), nullptr);
// Up the z axis from 0, 0, -100 (towards 0,0,-1000)
origin = QVector3D(0.0f, 0.0f, -100.0f);
direction = QVector3D(0.0f, 0.0f, -1.0f);
result = view3d->rayPick(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Null);
QCOMPARE(result.objectHit(), nullptr);
QCOMPARE(result.itemHit(), nullptr);
// Ray based picking all results
// Down the z axis from 0,0,250 (towards 0,0,0)
origin = QVector3D(0.0f, 0.0f, 250.0f);
direction = QVector3D(0.0f, 0.0f, -1.0f);
auto resultList = view3d->rayPickAll(origin, direction);
QCOMPARE(resultList.size(), 3);
QCOMPARE(resultList[0].hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(resultList[0].itemHit(), item2d);
QCOMPARE(resultList[1].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultList[1].objectHit(), model1);
QCOMPARE(resultList[2].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultList[2].objectHit(), model2);
// Up the z axis from 0, 0, -101 (towards 0,0,0)
origin = QVector3D(0.0f, 0.0f, -101.0f);
direction = QVector3D(0.0f, 0.0f, 1.0f);
resultList = view3d->rayPickAll(origin, direction);
QCOMPARE(resultList.size(), 2);
QCOMPARE(resultList[0].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultList[0].objectHit(), model2);
QCOMPARE(resultList[1].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultList[1].objectHit(), model1);
// Up the z axis from 0, 0, -100 (towards 0,0,-1000)
origin = QVector3D(0.0f, 0.0f, -100.0f);
direction = QVector3D(0.0f, 0.0f, -1.0f);
resultList = view3d->rayPickAll(origin, direction);
QVERIFY(resultList.isEmpty());
}
void tst_Picking::test_object_picking2()
{
QScopedPointer<QQuickView> view(createView(QLatin1String("picking2.qml"), QSize(100, 100)));
QVERIFY(view);
QVERIFY(QTest::qWaitForWindowExposed(view.data()));
const qreal dpr = view->devicePixelRatio();
if (dpr != 1.0) {
QSKIP("Test uses window positions to get exact values and those assume DPR of 1.0");
}
const auto viewSize = view->size();
const float halfWidth = viewSize.width() * 0.5f;
const float halfHeight = viewSize.height() * 0.5f;
const float horizontalPickLine = halfHeight - 35;
QQuick3DViewport *view3d = view->findChild<QQuick3DViewport *>(QStringLiteral("view"));
QVERIFY(view3d);
QQuick3DModel *coneModel = view3d->findChild<QQuick3DModel *>(QStringLiteral("model3"));
QVERIFY(coneModel);
// just left of the cone (model3)
auto result = view3d->pick(halfWidth - 11, horizontalPickLine);
QCOMPARE(result.objectHit(), nullptr);
// center top of the cone (model3)
result = view3d->pick(halfWidth, horizontalPickLine);
QVERIFY(result.objectHit() != nullptr);
QCOMPARE(result.objectHit(), coneModel);
// just right of the cone (model3)
result = view3d->pick(halfWidth + 11, horizontalPickLine);
QCOMPARE(result.objectHit(), nullptr);
}
// Necessary for doing comparisons of normalized vectors
static bool fuzzyCompareVectors(const QVector3D& v1, const QVector3D& v2, float epsilon = 0.000001f) {
return std::abs(v1.x() - v2.x()) < epsilon &&
std::abs(v1.y() - v2.y()) < epsilon &&
std::abs(v1.z() - v2.z()) < epsilon;
}
void tst_Picking::test_item_picking()
{
QScopedPointer<QQuickView> view(createView(QLatin1String("item_picking.qml"), QSize(400, 400)));
QVERIFY(view);
QVERIFY(QTest::qWaitForWindowExposed(view.data()));
QQuick3DViewport *view3d = view->findChild<QQuick3DViewport *>(QStringLiteral("view"));
QVERIFY(view3d);
QQuick3DModel *model1 = view3d->findChild<QQuick3DModel *>(QStringLiteral("model1"));
QVERIFY(model1);
QQuick3DModel *model2 = view3d->findChild<QQuick3DModel *>(QStringLiteral("model2"));
QVERIFY(model2);
QQuickItem *item2dPlusZ = qmlobject_cast<QQuickItem *>(find2DChildIn3DNode(view.data(), "item2dPlusZNode", "item2dPlusZ"));
QVERIFY(item2dPlusZ);
QQuickItem *item2dMinusZ = qmlobject_cast<QQuickItem *>(find2DChildIn3DNode(view.data(), "item2dMinusZNode", "item2dMinusZ"));
QVERIFY(item2dMinusZ);
QQuickItem *item2dPlusX = qmlobject_cast<QQuickItem *>(find2DChildIn3DNode(view.data(), "item2dPlusXNode", "item2dPlusX"));
QVERIFY(item2dPlusX);
QQuickItem *item2dMinusX = qmlobject_cast<QQuickItem *>(find2DChildIn3DNode(view.data(), "item2dMinusXNode", "item2dMinusX"));
QVERIFY(item2dMinusX);
QQuickItem *item2dPlusY = qmlobject_cast<QQuickItem *>(find2DChildIn3DNode(view.data(), "item2dPlusYNode", "item2dPlusY"));
QVERIFY(item2dPlusY);
QQuickItem *item2dMinusY = qmlobject_cast<QQuickItem *>(find2DChildIn3DNode(view.data(), "item2dMinusYNode", "item2dMinusY"));
QVERIFY(item2dMinusY);
QQuickItem *item2dRotation = qmlobject_cast<QQuickItem *>(find2DChildIn3DNode(view.data(), "item2dRotationNode", "item2dRotation"));
QVERIFY(item2dRotation);
QQuickItem *item2dTranslation = qmlobject_cast<QQuickItem *>(find2DChildIn3DNode(view.data(), "item2dTranslationNode", "item2dTranslation"));
QVERIFY(item2dTranslation);
QQuickItem *item2dScale = qmlobject_cast<QQuickItem *>(find2DChildIn3DNode(view.data(), "item2dScaleNode", "item2dScale"));
QVERIFY(item2dScale);
QQuickItem *item2dComplex = qmlobject_cast<QQuickItem *>(find2DChildIn3DNode(view.data(), "item2dComplexNode", "item2dComplex"));
QVERIFY(item2dComplex);
QQuickItem *item2dMadness = qmlobject_cast<QQuickItem *>(find2DChildIn3DNode(view.data(), "item2dMadnessNode", "item2dMadness"));
QVERIFY(item2dMadness);
// These tests intentionally try to cast rays into nothing, to check that item2ds picking stays within the bounds of the items
// This is necessary since items2ds uses plane intersection for ray collision tests
// Z Axes
auto origin = QVector3D(200, 200, 200);
auto direction = QVector3D(0, 0, -1);
auto result = view3d->rayPick(origin, direction);
auto resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Null);
QVERIFY(resultsList.isEmpty());
origin = QVector3D(200, 200, -200);
direction = QVector3D(0, 0, -1);
result = view3d->rayPick(origin, direction);
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Null);
QVERIFY(resultsList.isEmpty());
// X Axes
origin = QVector3D(200, 200, 200);
direction = QVector3D(-1, 0, 0);
result = view3d->rayPick(origin, direction);
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Null);
QVERIFY(resultsList.isEmpty());
origin = QVector3D(-200, 200, 200);
direction = QVector3D(1, 0, 0);
result = view3d->rayPick(origin, direction);
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Null);
QVERIFY(resultsList.isEmpty());
// Y Axes
origin = QVector3D(200, 200, 200);
direction = QVector3D(0, -1, 0);
result = view3d->rayPick(origin, direction);
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Null);
QVERIFY(resultsList.isEmpty());
origin = QVector3D(200, -200, 200);
direction = QVector3D(0, 1, 0);
result = view3d->rayPick(origin, direction);
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Null);
QVERIFY(resultsList.isEmpty());
// These tests all cast a ray down from above
direction = QVector3D (0, -1, 0);
// Rotation
origin = QVector3D(250, 100, -50);
result = view3d->rayPick(origin, direction);
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Null);
QVERIFY(resultsList.isEmpty());
// Translation
origin = QVector3D(450, 100, 10);
result = view3d->rayPick(origin, direction);
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Null);
QVERIFY(resultsList.isEmpty());
// Scale
origin = QVector3D(700, 100, 110);
result = view3d->rayPick(origin, direction);
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Null);
QVERIFY(resultsList.isEmpty());
// Complex
origin = QVector3D(900, 100, 110);
result = view3d->rayPick(origin, direction);
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Null);
QVERIFY(resultsList.isEmpty());
// Madness (fake 3D transforms in 2D)
origin = QVector3D(1150, 100, 90);
result = view3d->rayPick(origin, direction);
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Null);
QVERIFY(resultsList.isEmpty());
// The following tests try to hit the item2ds
// NB: Only a front-face hit of an item2d should count as a hit
// +Z
origin = QVector3D(25, 25, 200);
direction = QVector3D(0, 0, -1);
result = view3d->rayPick(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(result.itemHit(), item2dPlusZ);
QVERIFY(fuzzyCompareVectors(result.sceneNormal(), QVector3D(0, 0, 1)));
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(resultsList.size(), 3);
QCOMPARE(resultsList[0].hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(resultsList[0].itemHit(), item2dPlusZ);
QCOMPARE(resultsList[1].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultsList[1].objectHit(), model1);
QCOMPARE(resultsList[2].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultsList[2].objectHit(), model2);
// -Z
// NB: In this case the distance of collision should be the exact same for both
// model2 and item2dMinusZ. Item2D collision always occurs first, so it should
// be the first in the list.
origin = QVector3D(25, 25, -200);
direction = QVector3D(0, 0, 1);
result = view3d->rayPick(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(result.itemHit(), item2dMinusZ);
QVERIFY(fuzzyCompareVectors(result.sceneNormal(), QVector3D(0, 0, -1)));
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(resultsList.size(), 3);
QCOMPARE(resultsList[0].hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(resultsList[0].itemHit(), item2dMinusZ);
QCOMPARE(resultsList[1].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultsList[1].objectHit(), model2);
QCOMPARE(resultsList[2].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultsList[2].objectHit(), model1);
QCOMPARE(resultsList[0].distance(), resultsList[1].distance());
// +X
// NB: in this case the distance of collision should be the exact same for both
// model2 and item2dPlusX. Item2D collision always occurs first, so it should
// be the first in the list.
origin = QVector3D(200, 25, -25);
direction = QVector3D(-1, 0, 0);
result = view3d->rayPick(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(result.itemHit(), item2dPlusX);
QVERIFY(fuzzyCompareVectors(result.sceneNormal(), QVector3D(1, 0, 0)));
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(resultsList.size(), 3);
QCOMPARE(resultsList[0].hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(resultsList[0].itemHit(), item2dPlusX);
QCOMPARE(resultsList[1].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultsList[1].objectHit(), model2);
QCOMPARE(resultsList[2].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultsList[2].objectHit(), model1);
QCOMPARE(resultsList[0].distance(), resultsList[1].distance());
// -X
origin = QVector3D(-200, 25, -25);
direction = QVector3D(1, 0, 0);
result = view3d->rayPick(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(result.itemHit(), item2dMinusX);
QVERIFY(fuzzyCompareVectors(result.sceneNormal(), QVector3D(-1, 0, 0)));
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(resultsList.size(), 3);
QCOMPARE(resultsList[0].hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(resultsList[0].itemHit(), item2dMinusX);
QCOMPARE(resultsList[1].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultsList[1].objectHit(), model1);
QCOMPARE(resultsList[2].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultsList[2].objectHit(), model2);
// +Y
// NB: in this case the distance of collision should be the exact same for both
// model2 and item2dPlusY. Item2D collision always occurs first, so it should
// be the first in the list.
origin = QVector3D(25, 200, -25);
direction = QVector3D(0, -1, 0);
result = view3d->rayPick(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(result.itemHit(), item2dPlusY);
QVERIFY(fuzzyCompareVectors(result.sceneNormal(), QVector3D(0, 1, 0)));
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(resultsList.size(), 3);
QCOMPARE(resultsList[0].hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(resultsList[0].itemHit(), item2dPlusY);
QCOMPARE(resultsList[1].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultsList[1].objectHit(), model2);
QCOMPARE(resultsList[2].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultsList[2].objectHit(), model1);
QCOMPARE(resultsList[0].distance(), resultsList[1].distance());
// -Y
origin = QVector3D(25, -200, -25);
direction = QVector3D(0, 1, 0);
result = view3d->rayPick(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(result.itemHit(), item2dMinusY);
QVERIFY(fuzzyCompareVectors(result.sceneNormal(), QVector3D(0, -1, 0)));
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(resultsList.size(), 3);
QCOMPARE(resultsList[0].hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(resultsList[0].itemHit(), item2dMinusY);
QCOMPARE(resultsList[1].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultsList[1].objectHit(), model1);
QCOMPARE(resultsList[2].hitType(), QQuick3DPickResultEnums::HitType::Model);
QCOMPARE(resultsList[2].objectHit(), model2);
// The transform tests are aligned to the XZ plane, so all directions
// will be down the Y axis
direction = QVector3D(0, -1, 0);
// Rotation
origin = QVector3D(300, 100, 0);
result = view3d->rayPick(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(result.itemHit(), item2dRotation);
QVERIFY(fuzzyCompareVectors(result.sceneNormal(), QVector3D(0, 1, 0)));
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(resultsList.size(), 1);
QCOMPARE(resultsList[0].hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(resultsList[0].itemHit(), item2dRotation);
// Translate
origin = QVector3D(450, 100, -50);
result = view3d->rayPick(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(result.itemHit(), item2dTranslation);
QVERIFY(fuzzyCompareVectors(result.sceneNormal(), QVector3D(0, 1, 0)));
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(resultsList.size(), 1);
QCOMPARE(resultsList[0].hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(resultsList[0].itemHit(), item2dTranslation);
// Scale
origin = QVector3D(700, 100, 0);
result = view3d->rayPick(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(result.itemHit(), item2dScale);
QVERIFY(fuzzyCompareVectors(result.sceneNormal(), QVector3D(0, 1, 0)));
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(resultsList.size(), 1);
QCOMPARE(resultsList[0].hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(resultsList[0].itemHit(), item2dScale);
// Complex
origin = QVector3D(900, 100, 0);
result = view3d->rayPick(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(result.itemHit(), item2dComplex);
QVERIFY(fuzzyCompareVectors(result.sceneNormal(), QVector3D(0, 1, 0)));
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(resultsList.size(), 1);
QCOMPARE(resultsList[0].hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(resultsList[0].itemHit(), item2dComplex);
// Madness
origin = QVector3D(1150, 100, 50);
result = view3d->rayPick(origin, direction);
QCOMPARE(result.hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(result.itemHit(), item2dMadness);
QVERIFY(fuzzyCompareVectors(result.sceneNormal(), QVector3D(0, 1, 0)));
resultsList = view3d->rayPickAll(origin, direction);
QCOMPARE(resultsList.size(), 1);
QCOMPARE(resultsList[0].hitType(), QQuick3DPickResultEnums::HitType::Item);
QCOMPARE(resultsList[0].itemHit(), item2dMadness);
}
// When triangles become so small the picking algorithm fails because of floating point
// precision. This test generates a quad with count * count subdivisions leading to smaller
// and smaller triangles to pick against. Even if a mesh is very dense, the triangle intersection
// code should still work and return model1 as a hit.
void tst_Picking::test_picking_QTBUG_111997()
{
QScopedPointer<QQuickView> view(createView(QLatin1String("qtbug_111997.qml"), QSize(100, 100)));
QVERIFY(view);
QVERIFY(QTest::qWaitForWindowExposed(view.data()));
QQuick3DViewport *view3d = view->findChild<QQuick3DViewport *>(QStringLiteral("view"));
QVERIFY(view3d);
QQuick3DModel *model1 = view3d->findChild<QQuick3DModel *>(QStringLiteral("model1"));
QVERIFY(model1);
const auto viewSize = view->size();
const float halfWidth = viewSize.width() * 0.5f;
const float halfHeight = viewSize.height() * 0.5f;
QSignalSpy frameSwappedSpy(view.data(), &QQuickWindow::frameSwapped);
// Add custom geometry to the scene
QScopedPointer<CustomGeometry> geometry(new CustomGeometry(20));
geometry->setParent(model1);
geometry->setParentItem(model1);
model1->setGeometry(geometry.data());
// Wait for the next frame (so that geometry is up-to-date
if (!frameSwappedSpy.wait())
QFAIL("frameSwapped() signal not emitted");
// Check picking with 20 subdivisions
auto result = view3d->pick(halfWidth, halfHeight);
QVERIFY(result.objectHit() != nullptr);
QCOMPARE(result.objectHit(), model1);
// check picking with 350 subdivisions
geometry->setCount(350);
if (!frameSwappedSpy.wait())
QFAIL("frameSwapped() signal not emitted");
result = view3d->pick(halfWidth, halfHeight);
QVERIFY(result.objectHit() != nullptr);
QCOMPARE(result.objectHit(), model1);
// check picking with 500 subdivisions
geometry->setCount(500);
if (!frameSwappedSpy.wait())
QFAIL("frameSwapped() signal not emitted");
result = view3d->pick(halfWidth, halfHeight);
QVERIFY(result.objectHit() != nullptr);
QCOMPARE(result.objectHit(), model1);
}
void tst_Picking::test_picking_corner_case()
{
QScopedPointer<QQuickView> view(createView(QLatin1String("corner_case.qml"), QSize(400, 400)));
QVERIFY(view);
QVERIFY(QTest::qWaitForWindowExposed(view.data()));
QQuick3DViewport *view3d = view->findChild<QQuick3DViewport *>(QStringLiteral("view"));
QVERIFY(view3d);
QQuick3DModel *model1 = view3d->findChild<QQuick3DModel *>(QStringLiteral("model1"));
QVERIFY(model1);
const QVector3D direction = QVector3D(0.0f, 0.0f, 1.0f);
{
// bottom right corner (vertex)
const QVector3D origin1 = QVector3D(0.0f, 0.0f, -101.0f);
const QVector3D origin2 = QVector3D(0.0f, 0.0f, -100.0f);
auto result = view3d->rayPick(origin1, direction);
QVERIFY(result.objectHit() != nullptr);
QCOMPARE(result.objectHit(), model1);
QCOMPARE(result.scenePosition(), origin2);
QCOMPARE(result.normal(), QVector3D(0.0f, 0.0f, -1.0f));
QCOMPARE(result.uvPosition(), QVector2D(1.0f, 0.0f));
// The ray doesn't intersect the triangle as the origin
// starts on the triangle
// The ray intersects with the other face of the cube,
// but unless we test for backfaces the test should still
result = view3d->rayPick(origin2, direction);
QVERIFY(result.objectHit() == nullptr);
}
{
// top right corner (vertex)
const QVector3D origin1 = QVector3D(0.0f, 100.0f, -101.0f);
const QVector3D origin2 = QVector3D(0.0f, 100.0f, -100.0f);
auto result = view3d->rayPick(origin1, direction);
QVERIFY(result.objectHit() != nullptr);
QCOMPARE(result.objectHit(), model1);
QCOMPARE(result.scenePosition(), origin2);
QCOMPARE(result.normal(), QVector3D(0.0f, 0.0f, -1.0f));
QCOMPARE(result.uvPosition(), QVector2D(1.0f, 1.0f));
// The ray doesn't intersect the triangle as the origin
// starts on the triangle
// The ray intersects with the other face of the cube,
// but unless we test for backfaces the test should still
result = view3d->rayPick(origin2, direction);
QVERIFY(result.objectHit() == nullptr);
}
{
// bottom left corner (vertex)
const QVector3D origin1 = QVector3D(100.0f, 0.0f, -101.0f);
const QVector3D origin2 = QVector3D(100.0f, 0.0f, -100.0f);
auto result = view3d->rayPick(origin1, direction);
QVERIFY(result.objectHit() != nullptr);
QCOMPARE(result.objectHit(), model1);
QCOMPARE(result.scenePosition(), origin2);
QCOMPARE(result.normal(), QVector3D(0.0f, 0.0f, -1.0f));
QCOMPARE(result.uvPosition(), QVector2D(0.0f, 0.0f));
// The ray doesn't intersect the triangle as the origin
// starts on the triangle
// The ray intersects with the other face of the cube,
// but unless we test for backfaces the test should still
result = view3d->rayPick(origin2, direction);
QVERIFY(result.objectHit() == nullptr);
}
{
// top left corner (vertex)
const QVector3D origin1 = QVector3D(100.0f, 100.0f, -101.0f);
const QVector3D origin2 = QVector3D(100.0f, 100.0f, -100.0f);
auto result = view3d->rayPick(origin1, direction);
QVERIFY(result.objectHit() != nullptr);
QCOMPARE(result.objectHit(), model1);
QCOMPARE(result.scenePosition(), origin2);
QCOMPARE(result.normal(), QVector3D(0.0f, 0.0f, -1.0f));
QCOMPARE(result.uvPosition(), QVector2D(0.0f, 1.0f));
// The ray doesn't intersect the triangle as the origin
// starts on the triangle
// The ray intersects with the other face of the cube,
// but unless we test for backfaces the test should still
result = view3d->rayPick(origin2, direction);
QVERIFY(result.objectHit() == nullptr);
}
{
// Center
const QVector3D origin1 = QVector3D(50.0f, 50.0f, -101.0f);
const QVector3D origin2 = QVector3D(50.0f, 50.0f, -100.0f);
auto result = view3d->rayPick(origin1, direction);
QVERIFY(result.objectHit() != nullptr);
QCOMPARE(result.objectHit(), model1);
QCOMPARE(result.scenePosition(), origin2);
QCOMPARE(result.normal(), QVector3D(0.0f, 0.0f, -1.0f));
QCOMPARE(result.uvPosition(), QVector2D(0.5f, 0.5f));
// The ray doesn't intersect the triangle as the origin
// starts on the triangle
// The ray intersects with the other face of the cube,
// but unless we test for backfaces the test should still
result = view3d->rayPick(origin2, direction);
QVERIFY(result.objectHit() == nullptr);
}
}
void tst_Picking::test_triangleIntersect()
{
const QVector3D direction(0, 0, -1);
const QVector3D expectedNormal(0, 0, 1);
const QVector3D vert0(0, 0, 0);
const QVector3D vert1(-1, -1, 0);
const QVector3D vert2(1, -1, 0);
QVector3D normal;
float u;
float v;
bool intersects;
const QVector3D origin1(0, 0, 2);
const QSSGRenderRay ray1(origin1, direction); // intersection vert0
intersects = QSSGRenderRay::triangleIntersect(ray1,
vert0,
vert1,
vert2,
u,
v,
normal);
QVERIFY(intersects);
QCOMPARE(normal, expectedNormal);
QCOMPARE(u, 0.0f);
QCOMPARE(v, 0.0f);
const QVector3D origin2(-1, -1, 2);
const QSSGRenderRay ray2(origin2, direction); // intersection vert1
intersects = QSSGRenderRay::triangleIntersect(ray2,
vert0,
vert1,
vert2,
u,
v,
normal);
QVERIFY(intersects);
QCOMPARE(normal, expectedNormal);
QCOMPARE(u, 1.0f);
QCOMPARE(v, 0.0f);
const QVector3D origin3(1, -1, 2);
const QSSGRenderRay ray3(origin3, direction); // intersection vert2
intersects = QSSGRenderRay::triangleIntersect(ray3,
vert0,
vert1,
vert2,
u,
v,
normal);
QVERIFY(intersects);
QCOMPARE(normal, expectedNormal);
QCOMPARE(u, 0.0f);
QCOMPARE(v, 1.0f);
}
QTEST_MAIN(tst_Picking)
#include "tst_picking.moc"
|