| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 4 | |
| 5 | #include <QtTest/QtTest> |
| 6 | #include <Qt3DRender/private/qray3d_p.h> |
| 7 | |
| 8 | class tst_QRay3D : public QObject |
| 9 | { |
| 10 | Q_OBJECT |
| 11 | public: |
| 12 | tst_QRay3D() {} |
| 13 | ~tst_QRay3D() {} |
| 14 | |
| 15 | private Q_SLOTS: |
| 16 | void create_data(); |
| 17 | void create(); |
| 18 | void projection_data(); |
| 19 | void projection(); |
| 20 | void point_data(); |
| 21 | void point(); |
| 22 | void contains_point_data(); |
| 23 | void contains_point(); |
| 24 | void contains_ray_data(); |
| 25 | void contains_ray(); |
| 26 | void distance_data(); |
| 27 | void distance(); |
| 28 | void compare(); |
| 29 | void dataStream(); |
| 30 | void transform_data(); |
| 31 | void transform(); |
| 32 | void properties(); |
| 33 | void metaTypes(); |
| 34 | void shouldNotAllowNullDirection(); |
| 35 | }; |
| 36 | |
| 37 | // Fix the problem where a compared value happens to be zero (and |
| 38 | // you cannot always predict this, and should not predict it |
| 39 | // since then you produce self-fulling prophecies instead of tests). |
| 40 | // In that case qFuzzyCompare has a completely strict criterion since |
| 41 | // it finds the "fudge factor" by multiplying by zero... |
| 42 | static inline bool fuzzyCompare(float p1, float p2) |
| 43 | { |
| 44 | float fac = qMin(qAbs(p1), qAbs(p2)); |
| 45 | return (qAbs(p1 - p2) <= (qIsNull(fac) ? 0.00001f : 0.00001f * fac)); |
| 46 | } |
| 47 | |
| 48 | static inline bool fuzzyCompare(const Vector3D &lhs, const Vector3D &rhs) |
| 49 | { |
| 50 | if (fuzzyCompare(lhs.x(), rhs.x()) && |
| 51 | fuzzyCompare(lhs.y(), rhs.y()) && |
| 52 | fuzzyCompare(lhs.z(), rhs.z())) |
| 53 | return true; |
| 54 | #ifndef QT_NO_DEBUG_STREAM |
| 55 | qWarning() << "actual:" << lhs; |
| 56 | qWarning() << "expected:" << rhs; |
| 57 | #endif |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | void tst_QRay3D::create_data() |
| 62 | { |
| 63 | QTest::addColumn<Vector3D>("point"); |
| 64 | QTest::addColumn<Vector3D>("direction"); |
| 65 | |
| 66 | // normalized direction vectors |
| 67 | QTest::newRow("line on x-axis from origin") |
| 68 | << Vector3D() |
| 69 | << Vector3D(1.0f, 0.0f, 0.0f); |
| 70 | |
| 71 | QTest::newRow("line parallel -z-axis from 3,3,3") |
| 72 | << Vector3D(3.0f, 3.0f, 3.0f) |
| 73 | << Vector3D(0.0f, 0.0f, -1.0f); |
| 74 | |
| 75 | QTest::newRow("vertical line (parallel to y-axis)") |
| 76 | << Vector3D(0.5f, 0.0f, 0.5f) |
| 77 | << Vector3D(0.0f, 1.0f, 0.0f); |
| 78 | |
| 79 | QTest::newRow("equidistant from all 3 axes") |
| 80 | << Vector3D(0.5f, 0.0f, 0.5f) |
| 81 | << Vector3D(0.57735026919f, 0.57735026919f, 0.57735026919f); |
| 82 | |
| 83 | // non-normalized direction vectors |
| 84 | QTest::newRow("line on x-axis from origin - B") |
| 85 | << Vector3D() |
| 86 | << Vector3D(2.0f, 0.0f, 0.0f).normalized(); |
| 87 | |
| 88 | QTest::newRow("line parallel -z-axis from 3,3,3 - B") |
| 89 | << Vector3D(3.0f, 3.0f, 3.0f) |
| 90 | << Vector3D(0.0f, 0.0f, -0.7f).normalized(); |
| 91 | |
| 92 | QTest::newRow("vertical line (parallel to y-axis) - B") |
| 93 | << Vector3D(0.5f, 0.0f, 0.5f) |
| 94 | << Vector3D(0.0f, 5.3f, 0.0f).normalized(); |
| 95 | |
| 96 | QTest::newRow("equidistant from all 3 axes - B") |
| 97 | << Vector3D(0.5f, 0.0f, 0.5f) |
| 98 | << Vector3D(1.0f, 1.0f, 1.0f).normalized(); |
| 99 | |
| 100 | QTest::newRow("negative direction") |
| 101 | << Vector3D(-3.0f, -3.0f, -3.0f) |
| 102 | << Vector3D(-1.2f, -1.8f, -2.4f).normalized(); |
| 103 | } |
| 104 | |
| 105 | void tst_QRay3D::create() |
| 106 | { |
| 107 | QFETCH(Vector3D, point); |
| 108 | QFETCH(Vector3D, direction); |
| 109 | Qt3DRender::RayCasting::QRay3D ray(point, direction); |
| 110 | QVERIFY(fuzzyCompare(ray.direction(), direction)); |
| 111 | QVERIFY(fuzzyCompare(ray.origin(), point)); |
| 112 | |
| 113 | Qt3DRender::RayCasting::QRay3D ray2; |
| 114 | QCOMPARE(ray2.origin(), Vector3D(0, 0, 0)); |
| 115 | QCOMPARE(ray2.direction(), Vector3D(0, 0, 1)); |
| 116 | ray2.setOrigin(point); |
| 117 | ray2.setDirection(direction); |
| 118 | QVERIFY(fuzzyCompare(ray.direction(), direction)); |
| 119 | QVERIFY(fuzzyCompare(ray.origin(), point)); |
| 120 | } |
| 121 | |
| 122 | void tst_QRay3D::projection_data() |
| 123 | { |
| 124 | QTest::addColumn<Vector3D>("point"); |
| 125 | QTest::addColumn<Vector3D>("direction"); |
| 126 | QTest::addColumn<Vector3D>("vector"); |
| 127 | QTest::addColumn<Vector3D>("expected"); |
| 128 | |
| 129 | QTest::newRow("line on x-axis from origin") |
| 130 | << Vector3D() |
| 131 | << Vector3D(2.0f, 0.0f, 0.0f) |
| 132 | << Vector3D(0.6f, 0.0f, 0.0f) |
| 133 | << Vector3D(0.6f, 0.0f, 0.0f); |
| 134 | |
| 135 | QTest::newRow("line parallel -z-axis from 3,3,3") |
| 136 | << Vector3D(3.0f, 3.0f, 3.0f) |
| 137 | << Vector3D(0.0f, 0.0f, -0.7f) |
| 138 | << Vector3D(3.0f, 3.0f, 2.4f) |
| 139 | << Vector3D(0.0f, 0.0f, 2.4f); |
| 140 | |
| 141 | QTest::newRow("vertical line (parallel to y-axis)") |
| 142 | << Vector3D(0.5f, 0.0f, 0.5f) |
| 143 | << Vector3D(0.0f, 5.3f, 0.0f) |
| 144 | << Vector3D(0.5f, 0.6f, 0.5f) |
| 145 | << Vector3D(0.0f, 0.6f, 0.0f); |
| 146 | |
| 147 | QTest::newRow("equidistant from all 3 axes, project y-axis (with some z & x)") |
| 148 | << Vector3D(0.5f, 0.0f, 0.5f) |
| 149 | << Vector3D(1.0f, 1.0f, 1.0f) |
| 150 | << Vector3D(0.5f, 5.0f, 0.5f) |
| 151 | << Vector3D(2.0f, 2.0f, 2.0f); |
| 152 | |
| 153 | QTest::newRow("negative direction line, project +ve y-axis (with some z & x)") |
| 154 | << Vector3D(-3.0f, -3.0f, -3.0f) |
| 155 | << Vector3D(-1.2f, -1.8f, -2.4f) |
| 156 | << Vector3D(0.5f, 5.0f, 0.5f) |
| 157 | << Vector3D(1.241379261016846f, 1.862068772315979f, 2.48275852203369f); |
| 158 | } |
| 159 | |
| 160 | void tst_QRay3D::projection() |
| 161 | { |
| 162 | QFETCH(Vector3D, point); |
| 163 | QFETCH(Vector3D, direction); |
| 164 | QFETCH(Vector3D, vector); |
| 165 | QFETCH(Vector3D, expected); |
| 166 | Qt3DRender::RayCasting::QRay3D line(point, direction); |
| 167 | Vector3D result = line.project(vector); |
| 168 | QVERIFY(fuzzyCompare(result, expected)); |
| 169 | } |
| 170 | |
| 171 | void tst_QRay3D::point_data() |
| 172 | { |
| 173 | QTest::addColumn<Vector3D>("point"); |
| 174 | QTest::addColumn<Vector3D>("direction"); |
| 175 | QTest::addColumn<Vector3D>("point_on_line_pos_0_6"); |
| 176 | QTest::addColumn<Vector3D>("point_on_line_neg_7_2"); |
| 177 | |
| 178 | QTest::newRow("line on x-axis from origin") |
| 179 | << Vector3D() |
| 180 | << Vector3D(2.0f, 0.0f, 0.0f) |
| 181 | << Vector3D(0.6f, 0.0f, 0.0f) |
| 182 | << Vector3D(-7.2f, 0.0f, 0.0f); |
| 183 | |
| 184 | QTest::newRow("line parallel -z-axis from 3,3,3") |
| 185 | << Vector3D(3.0f, 3.0f, 3.0f) |
| 186 | << Vector3D(0.0f, 0.0f, -0.7f) |
| 187 | << Vector3D(3.0f, 3.0f, 2.4f) |
| 188 | << Vector3D(3.0f, 3.0f, 10.2f); |
| 189 | |
| 190 | QTest::newRow("vertical line (parallel to y-axis)") |
| 191 | << Vector3D(0.5f, 0.0f, 0.5f) |
| 192 | << Vector3D(0.0f, 5.3f, 0.0f) |
| 193 | << Vector3D(0.5f, 0.6f, 0.5f) |
| 194 | << Vector3D(0.5f, -7.2f, 0.5f); |
| 195 | |
| 196 | QTest::newRow("equidistant from all 3 axes") |
| 197 | << Vector3D(0.5f, 0.0f, 0.5f) |
| 198 | << Vector3D(1.0f, 1.0f, 1.0f) |
| 199 | << Vector3D(0.84641f, 0.34641f, 0.84641f) |
| 200 | << Vector3D(-3.65692f, -4.15692f, -3.65692f); |
| 201 | |
| 202 | QTest::newRow("negative direction") |
| 203 | << Vector3D(-3.0f, -3.0f, -3.0f) |
| 204 | << Vector3D(-1.2f, -1.8f, -2.4f) |
| 205 | << Vector3D(-3.22283f, -3.33425f, -3.44567f) |
| 206 | << Vector3D(-0.325987f, 1.01102f, 2.34803f); |
| 207 | } |
| 208 | |
| 209 | void tst_QRay3D::point() |
| 210 | { |
| 211 | QFETCH(Vector3D, point); |
| 212 | QFETCH(Vector3D, direction); |
| 213 | QFETCH(Vector3D, point_on_line_pos_0_6); |
| 214 | QFETCH(Vector3D, point_on_line_neg_7_2); |
| 215 | Qt3DRender::RayCasting::QRay3D line(point, direction); |
| 216 | QVERIFY(fuzzyCompare(line.point(0.6f), point_on_line_pos_0_6)); |
| 217 | QVERIFY(fuzzyCompare(line.point(-7.2f), point_on_line_neg_7_2)); |
| 218 | QVERIFY(fuzzyCompare(line.projectedDistance(point_on_line_pos_0_6), 0.6f)); |
| 219 | QVERIFY(fuzzyCompare(line.projectedDistance(point_on_line_neg_7_2), -7.2f)); |
| 220 | } |
| 221 | |
| 222 | void tst_QRay3D::contains_point_data() |
| 223 | { |
| 224 | QTest::addColumn<Vector3D>("origin"); |
| 225 | QTest::addColumn<Vector3D>("direction"); |
| 226 | QTest::addColumn<Vector3D>("point"); |
| 227 | QTest::addColumn<bool>("contains"); |
| 228 | |
| 229 | QTest::newRow("bogus this line with null direction") |
| 230 | << Vector3D(1.0, 3.0, 3.0) |
| 231 | << Vector3D(0.0, 0.0, 0.0) |
| 232 | << Vector3D(1.0, 2.0, 4.0) |
| 233 | << false; |
| 234 | |
| 235 | QTest::newRow("point at the origin") |
| 236 | << Vector3D(0.0, 0.0, 0.0) |
| 237 | << Vector3D(1.0, 3.0, 3.0) |
| 238 | << Vector3D(0.0, 0.0, 0.0) |
| 239 | << true; |
| 240 | |
| 241 | QTest::newRow("close to the origin") |
| 242 | << Vector3D(1.0, 1.0, 1.0) |
| 243 | << Vector3D(1.0, 3.0, 3.0) |
| 244 | << Vector3D(1.0005f, 1.0005f, 1.0) |
| 245 | << false; |
| 246 | |
| 247 | QTest::newRow("45 line line in plane x=1") |
| 248 | << Vector3D(1.0, 3.0, 3.0) |
| 249 | << Vector3D(0.0, -1.0, -1.0) |
| 250 | << Vector3D(1.0, 4.0, 4.0) |
| 251 | << true; |
| 252 | { |
| 253 | // This is to prove that the constructed approach give the |
| 254 | // same results |
| 255 | Vector3D p(1.0, 3.0, 3.0); |
| 256 | Vector3D v(0.0, -1.0, -1.0); |
| 257 | |
| 258 | QTest::newRow("constructed 45 line line in plane x=1") |
| 259 | << p |
| 260 | << v |
| 261 | << p + v |
| 262 | << true; |
| 263 | } |
| 264 | |
| 265 | QTest::newRow("intersection with negative s in plane z=-1") |
| 266 | << Vector3D(1.0f, 2.0f, -1.0f) |
| 267 | << Vector3D(1.0f, 1.0f, 0.0f) |
| 268 | << Vector3D(2.0f, 1.0f, 0.0f) |
| 269 | << false; |
| 270 | |
| 271 | QTest::newRow("45 angled line") |
| 272 | << Vector3D(3.0f, 0.0f, -1.0f) |
| 273 | << Vector3D(1.0f, -1.0f, 1.0f) |
| 274 | << Vector3D(6.0f, -3.0f, 2.0f) |
| 275 | << true; |
| 276 | |
| 277 | { |
| 278 | Vector3D p(-10.0, 3.0, 3.0); |
| 279 | Vector3D v(0.0, 20.0, -1.0); |
| 280 | QTest::newRow("constructed vector close to axis") |
| 281 | << p |
| 282 | << v |
| 283 | << p + v * 3.0 |
| 284 | << true; |
| 285 | } |
| 286 | |
| 287 | { |
| 288 | Vector3D p(1.0, 3.0, 3.0); |
| 289 | Vector3D v(40.0, 500.0, -1.0); |
| 290 | QTest::newRow("constructed larger values close to axis") |
| 291 | << p |
| 292 | << v |
| 293 | << p + v |
| 294 | << true; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void tst_QRay3D::contains_point() |
| 299 | { |
| 300 | QFETCH(Vector3D, origin); |
| 301 | QFETCH(Vector3D, direction); |
| 302 | QFETCH(Vector3D, point); |
| 303 | QFETCH(bool, contains); |
| 304 | |
| 305 | Qt3DRender::RayCasting::QRay3D line(origin, direction); |
| 306 | QCOMPARE(line.contains(point), contains); |
| 307 | } |
| 308 | |
| 309 | void tst_QRay3D::contains_ray_data() |
| 310 | { |
| 311 | contains_point_data(); |
| 312 | } |
| 313 | |
| 314 | void tst_QRay3D::contains_ray() |
| 315 | { |
| 316 | QFETCH(Vector3D, origin); |
| 317 | QFETCH(Vector3D, direction); |
| 318 | QFETCH(Vector3D, point); |
| 319 | QFETCH(bool, contains); |
| 320 | |
| 321 | Qt3DRender::RayCasting::QRay3D line(origin, direction); |
| 322 | if (contains) { |
| 323 | Qt3DRender::RayCasting::QRay3D line2(point, direction); |
| 324 | QVERIFY(line.contains(line2)); |
| 325 | QVERIFY(line2.contains(line)); |
| 326 | |
| 327 | // Reversed direction is also contained. |
| 328 | Qt3DRender::RayCasting::QRay3D line3(point, -direction); |
| 329 | QVERIFY(line.contains(line2)); |
| 330 | QVERIFY(line2.contains(line)); |
| 331 | |
| 332 | // Different direction. |
| 333 | Qt3DRender::RayCasting::QRay3D line4(point, Vector3D(direction.y(), direction.x(), direction.z())); |
| 334 | QVERIFY(!line.contains(line4)); |
| 335 | QVERIFY(!line4.contains(line)); |
| 336 | } else { |
| 337 | Qt3DRender::RayCasting::QRay3D line2(point, direction); |
| 338 | QVERIFY(!line.contains(line2)); |
| 339 | QVERIFY(!line2.contains(line)); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | void tst_QRay3D::distance_data() |
| 344 | { |
| 345 | QTest::addColumn<Vector3D>("origin"); |
| 346 | QTest::addColumn<Vector3D>("direction"); |
| 347 | QTest::addColumn<Vector3D>("point"); |
| 348 | QTest::addColumn<float>("distance"); |
| 349 | |
| 350 | QTest::newRow("axis-x") |
| 351 | << Vector3D(6.0f, 0.0f, 0.0f) |
| 352 | << Vector3D(1.0f, 0.0f, 0.0f) |
| 353 | << Vector3D(0.0f, 0.0f, 0.0f) |
| 354 | << 0.0f; |
| 355 | |
| 356 | QTest::newRow("axis-x to 1") |
| 357 | << Vector3D(6.0f, 0.0f, 0.0f) |
| 358 | << Vector3D(1.0f, 0.0f, 0.0f) |
| 359 | << Vector3D(0.0f, 1.0f, 0.0f) |
| 360 | << 1.0f; |
| 361 | |
| 362 | QTest::newRow("neg-axis-y") |
| 363 | << Vector3D(0.0f, 6.0f, 0.0f) |
| 364 | << Vector3D(0.0f, -1.5f, 0.0f) |
| 365 | << Vector3D(0.0f, 100.0f, 0.0f) |
| 366 | << 0.0f; |
| 367 | |
| 368 | QTest::newRow("neg-axis-y to 2") |
| 369 | << Vector3D(0.0f, 6.0f, 0.0f) |
| 370 | << Vector3D(0.0f, -1.5f, 0.0f) |
| 371 | << Vector3D(2.0f, 0.0f, 0.0f) |
| 372 | << 2.0f; |
| 373 | } |
| 374 | |
| 375 | void tst_QRay3D::distance() |
| 376 | { |
| 377 | QFETCH(Vector3D, origin); |
| 378 | QFETCH(Vector3D, direction); |
| 379 | QFETCH(Vector3D, point); |
| 380 | QFETCH(float, distance); |
| 381 | |
| 382 | Qt3DRender::RayCasting::QRay3D line(origin, direction); |
| 383 | QCOMPARE(line.distance(point), distance); |
| 384 | } |
| 385 | |
| 386 | void tst_QRay3D::compare() |
| 387 | { |
| 388 | Qt3DRender::RayCasting::QRay3D ray1(Vector3D(10, 20, 30), Vector3D(-3, -4, -5)); |
| 389 | Qt3DRender::RayCasting::QRay3D ray2(Vector3D(10, 20, 30), Vector3D(1.5f, 2.0f, 2.5f)); |
| 390 | Qt3DRender::RayCasting::QRay3D ray3(Vector3D(0, 20, 30), Vector3D(-3, -4, -5)); |
| 391 | QVERIFY(ray1 == ray1); |
| 392 | QVERIFY(!(ray1 != ray1)); |
| 393 | QVERIFY(qFuzzyCompare(ray1, ray1)); |
| 394 | QVERIFY(ray1 != ray2); |
| 395 | QVERIFY(!(ray1 == ray2)); |
| 396 | QVERIFY(!qFuzzyCompare(ray1, ray2)); |
| 397 | QVERIFY(ray1 != ray3); |
| 398 | QVERIFY(!(ray1 == ray3)); |
| 399 | QVERIFY(!qFuzzyCompare(ray1, ray3)); |
| 400 | } |
| 401 | |
| 402 | void tst_QRay3D::dataStream() |
| 403 | { |
| 404 | #ifndef QT_NO_DATASTREAM |
| 405 | Qt3DRender::RayCasting::QRay3D ray(Vector3D(1.0f, 2.0f, 3.0f), Vector3D(4.0f, 5.0f, 6.0f)); |
| 406 | |
| 407 | QByteArray data; |
| 408 | { |
| 409 | QDataStream stream(&data, QIODevice::WriteOnly); |
| 410 | stream << ray; |
| 411 | } |
| 412 | |
| 413 | Qt3DRender::RayCasting::QRay3D ray2; |
| 414 | { |
| 415 | QDataStream stream2(data); |
| 416 | stream2 >> ray2; |
| 417 | } |
| 418 | |
| 419 | QVERIFY(ray == ray2); |
| 420 | #endif |
| 421 | } |
| 422 | |
| 423 | void tst_QRay3D::transform_data() |
| 424 | { |
| 425 | create_data(); |
| 426 | } |
| 427 | |
| 428 | void tst_QRay3D::transform() |
| 429 | { |
| 430 | QFETCH(Vector3D, point); |
| 431 | QFETCH(Vector3D, direction); |
| 432 | |
| 433 | Matrix4x4 m; |
| 434 | { |
| 435 | QMatrix4x4 c; |
| 436 | c.translate(-1.0f, 2.5f, 5.0f); |
| 437 | c.rotate(45.0f, 1.0f, 1.0f, 1.0f); |
| 438 | c.scale(23.5f); |
| 439 | m = Matrix4x4(c); |
| 440 | } |
| 441 | |
| 442 | Qt3DRender::RayCasting::QRay3D ray1(point, direction); |
| 443 | Qt3DRender::RayCasting::QRay3D ray2(ray1); |
| 444 | Qt3DRender::RayCasting::QRay3D ray3; |
| 445 | |
| 446 | ray1.transform(m); |
| 447 | ray3 = ray2.transformed(m); |
| 448 | |
| 449 | QVERIFY(fuzzyCompare(ray1.origin(), ray3.origin())); |
| 450 | QVERIFY(fuzzyCompare(ray1.direction(), ray3.direction())); |
| 451 | |
| 452 | QVERIFY(fuzzyCompare(ray1.origin(), m * point)); |
| 453 | QVERIFY(fuzzyCompare(ray1.direction(), m.mapVector(direction).normalized())); |
| 454 | } |
| 455 | |
| 456 | class tst_QRay3DProperties : public QObject |
| 457 | { |
| 458 | Q_OBJECT |
| 459 | Q_PROPERTY(Qt3DRender::RayCasting::QRay3D ray READ ray WRITE setRay) |
| 460 | public: |
| 461 | tst_QRay3DProperties(QObject *parent = 0) : QObject(parent) {} |
| 462 | |
| 463 | Qt3DRender::RayCasting::QRay3D ray() const { return r; } |
| 464 | void setRay(const Qt3DRender::RayCasting::QRay3D& value) { r = value; } |
| 465 | |
| 466 | private: |
| 467 | Qt3DRender::RayCasting::QRay3D r; |
| 468 | }; |
| 469 | |
| 470 | // Test getting and setting properties via the metaobject system. |
| 471 | void tst_QRay3D::properties() |
| 472 | { |
| 473 | tst_QRay3DProperties obj; |
| 474 | |
| 475 | qRegisterMetaType<Qt3DRender::RayCasting::QRay3D>(); |
| 476 | |
| 477 | obj.setRay(Qt3DRender::RayCasting::QRay3D(Vector3D(1, 2, 3), Vector3D(4, 5, 6))); |
| 478 | |
| 479 | Qt3DRender::RayCasting::QRay3D r = qvariant_cast<Qt3DRender::RayCasting::QRay3D>(obj.property("ray")); |
| 480 | QCOMPARE(r.origin(), Vector3D(1, 2, 3)); |
| 481 | QCOMPARE(r.direction(), Vector3D(4, 5, 6).normalized()); |
| 482 | |
| 483 | obj.setProperty("ray", |
| 484 | QVariant::fromValue |
| 485 | (Qt3DRender::RayCasting::QRay3D(Vector3D(-1, -2, -3), Vector3D(-4, -5, -6)))); |
| 486 | |
| 487 | r = qvariant_cast<Qt3DRender::RayCasting::QRay3D>(obj.property("ray")); |
| 488 | QCOMPARE(r.origin(), Vector3D(-1, -2, -3)); |
| 489 | QCOMPARE(r.direction(), Vector3D(-4, -5, -6).normalized()); |
| 490 | } |
| 491 | |
| 492 | void tst_QRay3D::metaTypes() |
| 493 | { |
| 494 | int id = qMetaTypeId<Qt3DRender::RayCasting::QRay3D>(); |
| 495 | QVERIFY(QMetaType::fromName("Qt3DRender::RayCasting::QRay3D").id() == id); |
| 496 | QCOMPARE(QByteArray(QMetaType(id).name()), QByteArray("Qt3DRender::RayCasting::QRay3D")); |
| 497 | QVERIFY(QMetaType::isRegistered(id)); |
| 498 | } |
| 499 | |
| 500 | void tst_QRay3D::shouldNotAllowNullDirection() |
| 501 | { |
| 502 | // GIVEN |
| 503 | Qt3DRender::RayCasting::QRay3D ray; |
| 504 | |
| 505 | QCOMPARE(ray.origin(), Vector3D(0, 0, 0)); |
| 506 | QCOMPARE(ray.direction(), Vector3D(0, 0, 1)); |
| 507 | |
| 508 | // WHEN |
| 509 | ray.setDirection(Vector3D(0, 0, 0)); |
| 510 | |
| 511 | // THEN |
| 512 | QCOMPARE(ray.direction(), Vector3D(0, 0, 1)); |
| 513 | } |
| 514 | |
| 515 | QTEST_APPLESS_MAIN(tst_QRay3D) |
| 516 | |
| 517 | #include "tst_qray3d.moc" |