summaryrefslogtreecommitdiffstats
path: root/tests/auto/threed/qplane3d/tst_qplane3d.cpp
blob: 17f14164d9c4c98c6aa919b14b9905b6779e6226 (plain)
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
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtQuick3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtTest/QtTest>

#include "qplane3d.h"
#include "qray3d.h"

class tst_QPlane3D : public QObject
{
    Q_OBJECT
public:
    tst_QPlane3D() {}
    ~tst_QPlane3D() {}

private slots:
    void create_data();
    void create();
    void intersection_data();
    void intersection();
    void noIntersection_data();
    void noIntersection();
    void contains_data();
    void contains();
    void distanceTo_data();
    void distanceTo();
    void compare();
    void transform_data();
    void transform();
    void dataStream();
    void properties();
    void metaTypes();
};

// since all calculations involved QVector3D are producing values with only
// float precision those calculations can at best be float precision
// if you assign the results of the calculation to a qreal then qFuzzyCompare
// will quite happily use a much higher standard of precision than it is
// possible to acheive - hence redefine it here to always use the float
// Also while on the job fix the problem where a compared value happens
// to be zero (and you cannot always predict this, and should not predict it
// since then you produce self-fulling prophecies instead of tests).
// In that case qFuzzyCompare has a completely strict criterion since
// it finds the "fudge factor" by multiplying by zero...
static inline bool fuzzyCompare(qreal p1, qreal p2)
{
    float fac = qMin(qAbs(p1), qAbs(p2));
    return (qAbs(p1 - p2) <= (qIsNull(fac) ? 0.00001f : 0.00001f * fac));
}

static inline bool fuzzyCompare(const QVector3D &lhs, const QVector3D &rhs)
{
    if (fuzzyCompare(lhs.x(), rhs.x()) &&
            fuzzyCompare(lhs.y(), rhs.y()) &&
            fuzzyCompare(lhs.z(), rhs.z()))
        return true;
#ifndef QT_NO_DEBUG_STREAM
    qWarning() << "actual:" << lhs;
    qWarning() << "expected:" << rhs;
#endif
    return false;
}

static inline bool fuzzyIsNull(const QVector3D &v)
{
    return fuzzyCompare(v.x(), 0.0f) &&
           fuzzyCompare(v.y(), 0.0f) &&
           fuzzyCompare(v.z(), 0.0f);
}

void tst_QPlane3D::create_data()
{
    QTest::addColumn<QVector3D>("point");
    QTest::addColumn<QVector3D>("normal");

    // normalized vectors for the normals.
    QTest::newRow("line on x-axis from origin")
            << QVector3D()
            << QVector3D(1.0f, 0.0f, 0.0f);

    QTest::newRow("line paralell -z-axis from 3,3,3")
            << QVector3D(3.0f, 3.0f, 3.0f)
            << QVector3D(0.0f, 0.0f, -1.0f);

    QTest::newRow("vertical line (paralell to y-axis)")
            << QVector3D(0.5f, 0.0f, 0.5f)
            << QVector3D(0.0f, 1.0f, 0.0f);

    QTest::newRow("equidistant from all 3 axes")
            << QVector3D(0.5f, 0.0f, 0.5f)
            << QVector3D(0.57735026919f, 0.57735026919f, 0.57735026919f);

    // Unnormalized vectors for the normals.
    QTest::newRow("line on x-axis from origin")
            << QVector3D()
            << QVector3D(2.0f, 0.0f, 0.0f);

    QTest::newRow("line paralell -z-axis from 3,3,3")
            << QVector3D(3.0f, 3.0f, 3.0f)
            << QVector3D(0.0f, 0.0f, -0.7f);

    QTest::newRow("vertical line (paralell to y-axis)")
            << QVector3D(0.5f, 0.0f, 0.5f)
            << QVector3D(0.0f, 5.3f, 0.0f);

    QTest::newRow("equidistant from all 3 axes")
            << QVector3D(0.5f, 0.0f, 0.5f)
            << QVector3D(1.0f, 1.0f, 1.0f);

    QTest::newRow("negative direction")
            << QVector3D(-3.0f, -3.0f, -3.0f)
            << QVector3D(-1.2f, -1.8f, -2.4f);
}

void tst_QPlane3D::create()
{
    QFETCH(QVector3D, point);
    QFETCH(QVector3D, normal);
    QPlane3D plane(point, normal);
    QVERIFY(fuzzyCompare(plane.normal(), normal));
    QVERIFY(fuzzyCompare(plane.origin(), point));

    QPlane3D plane2;
    QVERIFY(plane2.origin() == QVector3D(0, 0, 0));
    QVERIFY(plane2.normal() == QVector3D(1, 0, 0));
    plane2.setOrigin(point);
    plane2.setNormal(normal);
    QVERIFY(fuzzyCompare(plane2.normal(), normal));
    QVERIFY(fuzzyCompare(plane2.origin(), point));
}

void tst_QPlane3D::intersection_data()
{
    // Line
    QTest::addColumn<QVector3D>("point1");
    QTest::addColumn<QVector3D>("direction");

    // Plane
    QTest::addColumn<QVector3D>("point2");
    QTest::addColumn<QVector3D>("normal");

    // Resulting intersection
    QTest::addColumn<QVector3D>("intersection");

    // These direction vectors will get normalized
    QTest::newRow("line on x-axis, plane in z-y")
            << QVector3D()
            << QVector3D(2.0f, 0.0f, 0.0f)
            << QVector3D(4.0f, 1.0f, 1.0f)
            << QVector3D(1.0f, 0.0f, 0.0f)
            << QVector3D(4.0f, 0.0f, 0.0f);

    QTest::newRow("line -z-axis, plane in x-y")
            << QVector3D(3.0f, 3.0f, 3.0f)
            << QVector3D(0.0f, 0.0f, -0.7f)
            << QVector3D(1.0f, 1.0f, -2.4f)
            << QVector3D(0.0f, 0.0f, -1.0f)
            << QVector3D(3.0f, 3.0f, -2.4f);

    QTest::newRow("line y-axis, plane in x-z")
            << QVector3D(0.5f, 0.0f, 0.5f)
            << QVector3D(0.0f, 5.3f, 0.0f)
            << QVector3D(1.5f, 0.6f, 1.5f)
            << QVector3D(0.0f, -7.2f, 0.0f)
            << QVector3D(0.5f, 0.6f, 0.5f);

    QTest::newRow("line equidistant from axes, plane in y-z")
            << QVector3D(0.5f, 0.0f, 0.5f)
            << QVector3D(1.0f, 1.0f, 1.0f)
            << QVector3D(5.0f, 3.0f, 3.0f)
            << QVector3D(1.0f, 1.0f, 1.0f)
            << QVector3D(3.8333332539f, 3.3333332539f, 3.8333332539f);

    QTest::newRow("negative direction")
            << QVector3D(-3.0f, -3.0f, -3.0f)
            << QVector3D(-1.2f, -1.8f, -2.4f)
            << QVector3D(5.0f, 3.0f, 3.0f)
            << QVector3D(1.0f, 1.0f, 1.0f)
            << QVector3D( 1.4444446564f, 3.6666665077f, 5.8888893127f);
}

void tst_QPlane3D::intersection()
{
    QFETCH(QVector3D, point1);
    QFETCH(QVector3D, direction);
    QFETCH(QVector3D, point2);
    QFETCH(QVector3D, normal);
    QFETCH(QVector3D, intersection);

    QRay3D line(point1, direction);
    QPlane3D plane(point2, normal);

    qreal t = plane.intersection(line);
    QVERIFY(!qIsNaN(t));
    QVERIFY(fuzzyCompare(line.point(t), intersection));
    QVERIFY(plane.intersects(line));
}

void tst_QPlane3D::noIntersection_data()
{
    QTest::addColumn<QVector3D>("point1");
    QTest::addColumn<QVector3D>("normal");
    QTest::addColumn<QVector3D>("point2");
    QTest::addColumn<QVector3D>("direction");
    QTest::addColumn<QVector3D>("intersection");

    // These direction vectors will get normalized
    QTest::newRow("line on x-axis, plane in z-x")
            << QVector3D()
            << QVector3D(2.0f, 0.0f, 0.0f)
            << QVector3D(4.0f, 1.0f, 1.0f)
            << QVector3D(0.0f, 1.0f, 0.0f);

    QTest::newRow("line -z-axis, lies on plane in z-x")
            << QVector3D(3.0f, 3.0f, 3.0f)
            << QVector3D(0.0f, 0.0f, -0.7f)
            << QVector3D(1.0f, 3.0f, 1.0f)
            << QVector3D(0.0f, -0.7f, 0.0f);

    QTest::newRow("line on an angle, never meets plane on angle")
            << QVector3D(3.0f, 3.0f, 3.0f)
            << QVector3D(0.1f, 0.0f, 0.0f)
            << QVector3D(1.0f, 1.0f, 3.0f)
            << QVector3D(0.0f, 1.0f, 0.0f);
}

void tst_QPlane3D::noIntersection()
{
    QFETCH(QVector3D, point1);
    QFETCH(QVector3D, direction);
    QFETCH(QVector3D, point2);
    QFETCH(QVector3D, normal);

    QPlane3D plane(point1, normal);
    QRay3D line(point2, direction);

    qreal t = plane.intersection(line);
    QVERIFY(qIsNaN(t));
    QVERIFY(!plane.intersects(line));
}

// Find a vector that lies perpendicular to the normal, and in the plane.
static QVector3D vectorInPlane(const QPlane3D &plane)
{
    QVector3D v = QVector3D::crossProduct(plane.normal(), QVector3D(1, 0, 0));
    if (fuzzyIsNull(v))
        v = QVector3D::crossProduct(plane.normal(), QVector3D(0, 1, 0));
    if (fuzzyIsNull(v))
        v = QVector3D::crossProduct(plane.normal(), QVector3D(0, 0, 1));
    v = QVector3D::dotProduct(v, plane.normal()) * plane.normal() /
                plane.normal().lengthSquared();
    Q_ASSERT(fuzzyCompare(QVector3D::dotProduct(v, plane.normal()), 0.0f));
    return v;
}

void tst_QPlane3D::contains_data()
{
    create_data();
}

void tst_QPlane3D::contains()
{
    QFETCH(QVector3D, point);
    QFETCH(QVector3D, normal);
    QPlane3D plane(point, normal);

    QVERIFY(plane.contains(point));
    QVERIFY(!plane.contains(point + normal));
    QVERIFY(!plane.contains(point - normal));

    QVector3D v = vectorInPlane(plane);
    QVERIFY(plane.contains(QRay3D(point, v)));
    QVERIFY(plane.contains(QRay3D(point - v, v)));
    QVERIFY(!plane.contains(QRay3D(point + normal, v)));
    QVERIFY(!plane.contains(QRay3D(point, normal)));
}

void tst_QPlane3D::distanceTo_data()
{
    create_data();
}

void tst_QPlane3D::distanceTo()
{
    QFETCH(QVector3D, point);
    QFETCH(QVector3D, normal);
    QPlane3D plane(point, normal);

    QVERIFY(fuzzyCompare(plane.distanceTo(point), 0.0f));
    QVERIFY(fuzzyCompare(plane.distanceTo(point + normal), normal.length()));
    QVERIFY(fuzzyCompare(plane.distanceTo(point - normal), -normal.length()));

    QVector3D v = vectorInPlane(plane);
    QVERIFY(fuzzyCompare(plane.distanceTo(point + v), 0.0f));
    QVERIFY(fuzzyCompare(plane.distanceTo(point + normal + v), normal.length()));
    QVERIFY(fuzzyCompare(plane.distanceTo(point - normal + v), -normal.length()));
}

void tst_QPlane3D::compare()
{
    QPlane3D plane1(QVector3D(10, 20, 30), QVector3D(-1, 2, 4));
    QPlane3D plane2(QVector3D(10, 20, 30), QVector3D(1, -2, -4));
    QPlane3D plane3(QVector3D(0, 20, 30), QVector3D(-1, 2, 4));
    QVERIFY(plane1 == plane1);
    QVERIFY(!(plane1 != plane1));
    QVERIFY(qFuzzyCompare(plane1, plane1));
    QVERIFY(plane1 != plane2);
    QVERIFY(!(plane1 == plane2));
    QVERIFY(!qFuzzyCompare(plane1, plane2));
    QVERIFY(plane1 != plane3);
    QVERIFY(!(plane1 == plane3));
    QVERIFY(!qFuzzyCompare(plane1, plane3));
}

void tst_QPlane3D::transform_data()
{
    create_data();
}

void tst_QPlane3D::transform()
{
    QFETCH(QVector3D, point);
    QFETCH(QVector3D, normal);

    QMatrix4x4 m;
    m.translate(-1.0f, 2.5f, 5.0f);
    m.rotate(45.0f, 1.0f, 1.0f, 1.0f);
    m.scale(23.5f);

    QPlane3D plane1(point, normal);
    QPlane3D plane2(plane1);
    QPlane3D plane3;

    plane1.transform(m);
    plane3 = plane2.transformed(m);

    QVERIFY(fuzzyCompare(plane1.origin(), plane3.origin()));
    QVERIFY(fuzzyCompare(plane1.normal(), plane3.normal()));

    QVERIFY(fuzzyCompare(plane1.origin(), m * point));
    QVERIFY(fuzzyCompare(plane1.normal(), m.mapVector(normal)));
}

void tst_QPlane3D::dataStream()
{
#ifndef QT_NO_DATASTREAM
    QPlane3D plane(QVector3D(1.0f, 2.0f, 3.0f),
                   QVector3D(4.0f, 5.0f, 6.0f));

    QByteArray data;
    {
        QDataStream stream(&data, QIODevice::WriteOnly);
        stream << plane;
    }

    QPlane3D plane2;
    {
        QDataStream stream2(data);
        stream2 >> plane2;
    }

    QVERIFY(plane == plane2);
#endif
}

class tst_QPlane3DProperties : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QPlane3D plane READ plane WRITE setPlane)
public:
    tst_QPlane3DProperties(QObject *parent = 0) : QObject(parent) {}

    QPlane3D plane() const { return p; }
    void setPlane(const QPlane3D& value) { p = value; }

private:
    QPlane3D p;
};

// Test getting and setting properties via the metaobject system.
void tst_QPlane3D::properties()
{
    tst_QPlane3DProperties obj;

    qRegisterMetaType<QPlane3D>();

    obj.setPlane(QPlane3D(QVector3D(1, 2, 3), QVector3D(4, 5, 6)));

    QPlane3D p = qVariantValue<QPlane3D>(obj.property("plane"));
    QCOMPARE(p.origin(), QVector3D(1, 2, 3));
    QCOMPARE(p.normal(), QVector3D(4, 5, 6));

    obj.setProperty("plane",
                    qVariantFromValue
                        (QPlane3D(QVector3D(-1, -2, -3), QVector3D(-4, -5, -6))));

    p = qVariantValue<QPlane3D>(obj.property("plane"));
    QCOMPARE(p.origin(), QVector3D(-1, -2, -3));
    QCOMPARE(p.normal(), QVector3D(-4, -5, -6));
}

void tst_QPlane3D::metaTypes()
{
    int id = qMetaTypeId<QPlane3D>();
    QVERIFY(QMetaType::type("QPlane3D") == id);
    QCOMPARE(QByteArray(QMetaType::typeName(id)), QByteArray("QPlane3D"));
    QVERIFY(QMetaType::isRegistered(id));
}

QTEST_APPLESS_MAIN(tst_QPlane3D)

#include "tst_qplane3d.moc"