summaryrefslogtreecommitdiffstats
path: root/src/datavisualization/engine/q3dcamera.cpp
blob: 8a02d3aebd03d43d56449a6aa9bf45501712f92d (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
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
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com
**
** This file is part of the QtDataVisualization module.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com
**
****************************************************************************/

#include "q3dcamera.h"
#include "q3dcamera_p.h"
#include "q3dscene.h"
#include "q3dbox.h"
#include "q3dobject.h"
#include "utils_p.h"

#include <qmath.h>
#include <QVector3D>

QT_BEGIN_NAMESPACE_DATAVISUALIZATION

/*!
 * \class Q3DCamera
 * \inmodule QtDataVisualization
 * \brief Representation of a camera in 3D space.
 * \since Qt Data Visualization 1.0
 *
 * Q3DCamera represents a basic orbit around centerpoint 3D camera that is used when rendering the
 * data visualization. The class offers simple methods for setting the orbit point in rotations,
 * but allows also setting the 4x4 view matrix directly in case a more customized camera behavior is
 * needed.
 */

/*!
 * \enum Q3DCamera::CameraPreset
 *
 * Predefined positions for camera.
 *
 * \value CameraPresetNone
 *        Used to indicate a preset has not been set, or the scene has been rotated freely.
 * \value CameraPresetFrontLow
 * \value CameraPresetFront
 * \value CameraPresetFrontHigh
 * \value CameraPresetLeftLow
 * \value CameraPresetLeft
 * \value CameraPresetLeftHigh
 * \value CameraPresetRightLow
 * \value CameraPresetRight
 * \value CameraPresetRightHigh
 * \value CameraPresetBehindLow
 * \value CameraPresetBehind
 * \value CameraPresetBehindHigh
 * \value CameraPresetIsometricLeft
 * \value CameraPresetIsometricLeftHigh
 * \value CameraPresetIsometricRight
 * \value CameraPresetIsometricRightHigh
 * \value CameraPresetDirectlyAbove
 * \value CameraPresetDirectlyAboveCW45
 * \value CameraPresetDirectlyAboveCCW45
 * \value CameraPresetFrontBelow
 *        In Q3DBars from CameraPresetFrontBelow onward these only work for graphs including negative
 *        values. They act as Preset...Low for positive-only values.
 * \value CameraPresetLeftBelow
 * \value CameraPresetRightBelow
 * \value CameraPresetBehindBelow
 * \value CameraPresetDirectlyBelow
 *        Acts as CameraPresetFrontLow for positive-only bars.
 */

/*!
 * \qmltype Camera3D
 * \inqmlmodule QtDataVisualization
 * \since QtDataVisualization 1.0
 * \ingroup datavisualization_qml
 * \instantiates Q3DCamera
 * \brief Representation of a camera in 3D space.
 *
 * Camera3D represents a basic orbit around centerpoint 3D camera that is used when rendering the
 * data visualization. The class offers simple methods for setting the orbit point in rotations,
 * but allows also setting the 4x4 view matrix directly in case a more customized camera behavior is
 * needed.
 */

/*!
 * \qmlproperty float Camera3D::xRotation
 *
 * This property contains the X-rotation angle of the camera around the target point in degrees
 * starting from the current base position set by the setBaseOrientation method.
 */

/*!
 * \qmlproperty float Camera3D::yRotation
 *
 * This property contains the Y-rotation angle of the camera around the target point in degrees
 * starting from the current base position set by the setBaseOrientation method.
 */

/*!
 * \qmlproperty float Camera3D::minXRotation
 *
 * This property contains the current minimum X-rotation for the camera.
 * The full circle range is \c{[-180, 180]} and the minimum value is limited to \c -180.
 * Also the value can't be higher than the maximum, and is adjusted if necessary.
 *
 * \sa wrapXRotation, maxXRotation
 */

/*!
 * \qmlproperty float Camera3D::minYRotation
 *
 * This property contains the current minimum Y-rotation for the camera.
 * The full Y angle range is \c{[-90, 90]} and the minimum value is limited to \c -90.
 * Also the value can't be higher than the maximum, and is adjusted if necessary.
 *
 * \sa wrapYRotation, maxYRotation
 */

/*!
 * \qmlproperty float Camera3D::maxXRotation
 *
 * This property contains the current maximum X-rotation for the camera.
 * The full circle range is \c{[-180, 180]} and the maximum value is limited to \c 180.
 * Also the value can't be lower than the minimum, and is adjusted if necessary.
 *
 * \sa wrapXRotation, minXRotation
 */

/*!
 * \qmlproperty float Camera3D::maxYRotation
 *
 * This property contains the current maximum Y-rotation for the camera.
 * The full Y angle range is \c{[-90, 90]} and the maximum value is limited to \c 90.
 * Also the value can't be lower than the minimum, and is adjusted if necessary.
 *
 * \sa wrapYRotation, minYRotation
 */

/*!
 * \qmlmethod void Camera3D::setBaseOrientation(vector3d basePosition, vector3d target, vector3d baseUp)
 *
 * Sets the base values for the camera that are used when calculating the camera position using the
 * rotation values. The base position of the camera is defined by \a basePosition, expectation is
 * that the x and y values are 0. Look at target point is defined by \a target and the camera
 * rotates around it. Up direction for the camera is defined by \a baseUp, normally this is a
 * vector with only y value set to 1.
 */

/*!
 * \qmlproperty matrix4x4 Camera3D::viewMatrix
 *
 * This property contains the view matrix used in the 3D calculations. When the default orbiting
 * camera behavior is sufficient, there is no need to touch this property. If the default
 * behavior is insufficient, the view matrix can be set directly.
 * \note When setting the view matrix directly remember to set viewMatrixAutoUpdateEnabled
 * property to \c false.
 */

/*!
 * \qmlproperty bool Camera3D::viewMatrixAutoUpdateEnabled
 *
 * This property determines if view matrix is automatically updated each render cycle using the
 * current base orientation and rotations. If set to \c false, no automatic recalculation is done and
 * the view matrix can be set using the viewMatrix property.
 */

/*!
 * \qmlproperty Camera3D.CameraPreset Camera3D::cameraPreset
 *
 * This property contains the currently active camera preset, which is one of
 * \l{Q3DCamera::CameraPreset}{Camera3D.CameraPreset}. If no preset is active the value
 * is \l{Q3DCamera::CameraPresetNone}{Camera3D.CameraPresetNone}.
 * \note The base camera orientation set by setBaseOrientation will affect the presets as all
 * calculations are based on those values.
 */

/*!
 * \qmlproperty float Camera3D::zoomLevel
 *
 * This property contains the the camera zoom level in percentage. 100.0 means there is no zoom
 * in or out set in the camera.
 */

/*!
 * \qmlproperty bool Camera3D::wrapXRotation
 *
 * This property determines the behavior of the minimum and maximum limits in the X-rotation.
 * By default the X-rotation wraps from minimum value to maximum and from maximum to minimum.
 *
 * If set to \c true the X-rotation of the camera is wrapped from minimum to maximum and from maximum
 * to minimum. If set to \c false the X-rotation of the camera is limited to the sector determined by
 * minimum and maximum values.
 */

/*!
 * \qmlproperty bool Camera3D::wrapYRotation
 *
 * This property determines the behavior of the minimum and maximum limits in the Y-rotation.
 * By default the Y-rotation is limited between the minimum and maximum values without any wrapping.
 *
 * If \c true the Y-rotation of the camera is wrapped from minimum to maximum and from maximum
 * to minimum. If \c false the Y-rotation of the camera is limited to the sector determined by minimum
 * and maximum values.
 */

/*!
 * Constructs a new 3D camera with position set to origin, up direction facing towards the Y-axis
 * and looking at origin by default. An optional \a parent parameter can be given and is then passed
 * to QObject constructor.
 */
Q3DCamera::Q3DCamera(QObject *parent) :
    Q3DObject(parent),
    d_ptr(new Q3DCameraPrivate(this))
{
}

/*!
 *  Destroys the camera object.
 */
Q3DCamera::~Q3DCamera()
{
}

/*!
 * Copies the 3D camera's properties from the given source camera.
 * Values are copied from the \a source to this object.
 */
void Q3DCamera::copyValuesFrom(const Q3DObject &source)
{
    Q3DObject::copyValuesFrom(source);

    const Q3DCamera &sourceCamera = static_cast<const Q3DCamera &>(source);

    d_ptr->m_target.setX(sourceCamera.d_ptr->m_target.x());
    d_ptr->m_target.setY(sourceCamera.d_ptr->m_target.y());
    d_ptr->m_target.setZ(sourceCamera.d_ptr->m_target.z());

    d_ptr->m_up.setX(sourceCamera.d_ptr->m_up.x());
    d_ptr->m_up.setY(sourceCamera.d_ptr->m_up.y());
    d_ptr->m_up.setZ(sourceCamera.d_ptr->m_up.z());

    float *values = new float[16];
    sourceCamera.d_ptr->m_viewMatrix.copyDataTo(values);
    d_ptr->m_viewMatrix = QMatrix4x4(values);
    delete[] values;

    d_ptr->m_xRotation = sourceCamera.d_ptr->m_xRotation;
    d_ptr->m_yRotation = sourceCamera.d_ptr->m_yRotation;

    d_ptr->m_minXRotation = sourceCamera.d_ptr->m_minXRotation;
    d_ptr->m_minYRotation = sourceCamera.d_ptr->m_minYRotation;
    d_ptr->m_maxXRotation = sourceCamera.d_ptr->m_maxXRotation;
    d_ptr->m_maxYRotation = sourceCamera.d_ptr->m_maxYRotation;

    d_ptr->m_wrapXRotation = sourceCamera.d_ptr->m_wrapXRotation;
    d_ptr->m_wrapYRotation = sourceCamera.d_ptr->m_wrapYRotation;

    d_ptr->m_zoomLevel = sourceCamera.d_ptr->m_zoomLevel;
    d_ptr->m_activePreset = sourceCamera.d_ptr->m_activePreset;
}

/*!
 * \property Q3DCamera::xRotation
 *
 * This property contains the X-rotation angle of the camera around the target point in degrees
 * starting from the current base position set by the setBaseOrientation() method.
 */
float Q3DCamera::xRotation() const {
    return d_ptr->m_xRotation;
}

void Q3DCamera::setXRotation(float rotation)
{
    if (d_ptr->m_wrapXRotation)
        rotation = Utils::wrapValue(rotation, d_ptr->m_minXRotation, d_ptr->m_maxXRotation);
    else
        rotation = qBound(float(d_ptr->m_minXRotation), float(rotation), float(d_ptr->m_maxXRotation));

    if (d_ptr->m_xRotation != rotation) {
        d_ptr->setXRotation(rotation);
        if (d_ptr->m_activePreset != CameraPresetNone) {
            d_ptr->m_activePreset = CameraPresetNone;
            setDirty(true);
        }

        emit xRotationChanged(d_ptr->m_xRotation);
    }
}

/*!
 * \property Q3DCamera::yRotation
 *
 * This property contains the Y-rotation angle of the camera around the target point in degrees
 * starting from the current base position set by the setBaseOrientation() method.
 */
float Q3DCamera::yRotation() const {
    return d_ptr->m_yRotation;
}

void Q3DCamera::setYRotation(float rotation)
{
    if (d_ptr->m_wrapYRotation)
        rotation = Utils::wrapValue(rotation, d_ptr->m_minYRotation, d_ptr->m_maxYRotation);
    else
        rotation = qBound(float(d_ptr->m_minYRotation), float(rotation), float(d_ptr->m_maxYRotation));

    if (d_ptr->m_yRotation != rotation) {
        d_ptr->setYRotation(rotation);
        if (d_ptr->m_activePreset != CameraPresetNone) {
            d_ptr->m_activePreset = CameraPresetNone;
            setDirty(true);
        }

        emit yRotationChanged(d_ptr->m_yRotation);
    }
}

/*!
 * \property Q3DCamera::minXRotation
 *
 * This property contains the current minimum X-rotation for the camera.
 * The full circle range is \c{[-180, 180]} and the minimum value is limited to \c -180.
 * Also the value can't be higher than the maximum, and is adjusted if necessary.
 *
 * \sa wrapXRotation, maxXRotation
 */
float Q3DCamera::minXRotation() const
{
    return d_ptr->m_minXRotation;
}

void Q3DCamera::setMinXRotation(float minRotation)
{
    minRotation = qBound(-180.0f, minRotation, 180.0f);
    if (minRotation > d_ptr->m_maxXRotation)
        minRotation = d_ptr->m_maxXRotation;

    if (d_ptr->m_minXRotation != minRotation) {
        d_ptr->m_minXRotation = minRotation;
        emit minXRotationChanged(minRotation);

        if (d_ptr->m_xRotation < d_ptr->m_minXRotation)
            setXRotation(d_ptr->m_xRotation);
    }
}

/*!
 * \property Q3DCamera::minYRotation
 *
 * This property contains the current minimum Y-rotation for the camera.
 * The full Y angle range is \c{[-90, 90]} and the minimum value is limited to \c -90.
 * Also the value can't be higher than the maximum, and is adjusted if necessary.
 *
 * \sa wrapYRotation, maxYRotation
 */
float Q3DCamera::minYRotation() const
{
    return d_ptr->m_minYRotation;
}

void Q3DCamera::setMinYRotation(float minRotation)
{
    minRotation = qBound(-90.0f, minRotation, 90.0f);
    if (minRotation > d_ptr->m_maxYRotation)
        minRotation = d_ptr->m_maxYRotation;

    if (d_ptr->m_minYRotation != minRotation) {
        d_ptr->m_minYRotation = minRotation;
        emit minYRotationChanged(minRotation);

        if (d_ptr->m_yRotation < d_ptr->m_minYRotation)
            setYRotation(d_ptr->m_yRotation);
    }
}

/*!
 * \property Q3DCamera::maxXRotation
 *
 * This property contains the current maximum X-rotation for the camera.
 * The full circle range is \c{[-180, 180]} and the maximum value is limited to \c 180.
 * Also the value can't be lower than the minimum, and is adjusted if necessary.
 *
 * \sa wrapXRotation, minXRotation
 */
float Q3DCamera::maxXRotation() const
{
    return d_ptr->m_maxXRotation;
}

void Q3DCamera::setMaxXRotation(float maxRotation)
{
    maxRotation = qBound(-180.0f, maxRotation, 180.0f);

    if (maxRotation < d_ptr->m_minXRotation)
        maxRotation = d_ptr->m_minXRotation;

    if (d_ptr->m_maxXRotation != maxRotation) {
        d_ptr->m_maxXRotation = maxRotation;
        emit maxXRotationChanged(maxRotation);

        if (d_ptr->m_xRotation > d_ptr->m_maxXRotation)
            setXRotation(d_ptr->m_xRotation);
    }
}

/*!
 * \property Q3DCamera::maxYRotation
 *
 * This property contains the current maximum Y-rotation for the camera.
 * The full Y angle range is \c{[-90, 90]} and the maximum value is limited to \c 90.
 * Also the value can't be lower than the minimum, and is adjusted if necessary.
 *
 * \sa wrapYRotation, minYRotation
 */
float Q3DCamera::maxYRotation() const
{
    return d_ptr->m_maxYRotation;
}

void Q3DCamera::setMaxYRotation(float maxRotation)
{
    maxRotation = qBound(-90.0f, maxRotation, 90.0f);

    if (maxRotation < d_ptr->m_minYRotation)
        maxRotation = d_ptr->m_minYRotation;

    if (d_ptr->m_maxYRotation != maxRotation) {
        d_ptr->m_maxYRotation = maxRotation;
        emit maxYRotationChanged(maxRotation);

        if (d_ptr->m_yRotation > d_ptr->m_maxYRotation)
            setYRotation(d_ptr->m_yRotation);
    }
}

/*!
 * Sets the base values for the camera that are used when calculating the camera position using the
 * rotation values. The base position of the camera is defined by \a basePosition, expectation is
 * that the x and y values are 0. Look at target point is defined by \a target and the camera
 * rotates around it. Up direction for the camera is defined by \a baseUp, normally this is a
 * vector with only y value set to 1.
 */
void Q3DCamera::setBaseOrientation(const QVector3D &basePosition,
                                   const QVector3D &target,
                                   const QVector3D &baseUp)
{
    if (position() != basePosition
            || d_ptr->m_target != target
            || d_ptr->m_up != baseUp) {
        setPosition(basePosition);
        d_ptr->m_target   = target;
        d_ptr->m_up       = baseUp;
        setDirty(true);
    }
}

/*!
 * \property Q3DCamera::viewMatrix
 *
 * This property contains the view matrix used in the 3D calculations. When the default orbiting
 * camera behavior is sufficient, there is no need to touch this property. If the default
 * behavior is insufficient, the view matrix can be set directly.
 * \note When setting the view matrix directly remember to set viewMatrixAutoUpdateEnabled to
 * \c false.
 */
QMatrix4x4 Q3DCamera::viewMatrix() const
{
    return d_ptr->m_viewMatrix;
}

void Q3DCamera::setViewMatrix(const QMatrix4x4 &viewMatrix)
{
    if (d_ptr->m_viewMatrix != viewMatrix) {
        d_ptr->m_viewMatrix = viewMatrix;
        setDirty(true);
        emit viewMatrixChanged(d_ptr->m_viewMatrix);
    }
}

/*!
 * \property Q3DCamera::viewMatrixAutoUpdateEnabled
 *
 * This property determines if view matrix is automatically updated each render cycle using the
 * current base orientation and rotations. If set to \c false, no automatic recalculation is done
 * and the view matrix can be set using the viewMatrix property.
 */
bool Q3DCamera::isViewMatrixAutoUpdateEnabled() const
{
    return d_ptr->m_isViewMatrixUpdateActive;
}

void Q3DCamera::setViewMatrixAutoUpdateEnabled(bool isEnabled)
{
    d_ptr->m_isViewMatrixUpdateActive = isEnabled;
    emit viewMatrixAutoUpdateChanged(isEnabled);
}

/*!
 * \property Q3DCamera::cameraPreset
 *
 * This property contains the currently active camera preset, if no preset is active the value
 * is CameraPresetNone.
 * \note The base camera orientation set by setBaseOrientation() will affect
 * the presets as all calculations are based on those values.
 */
Q3DCamera::CameraPreset Q3DCamera::cameraPreset() const
{
    return d_ptr->m_activePreset;
}

void Q3DCamera::setCameraPreset(CameraPreset preset)
{
    switch (preset) {
    case CameraPresetFrontLow: {
        setXRotation(0.0f);
        setYRotation(0.0f);
        break;
    }
    case CameraPresetFront: {
        setXRotation(0.0f);
        setYRotation(22.5f);
        break;
    }
    case CameraPresetFrontHigh: {
        setXRotation(0.0f);
        setYRotation(45.0f);
        break;
    }
    case CameraPresetLeftLow: {
        setXRotation(90.0f);
        setYRotation(0.0f);
        break;
    }
    case CameraPresetLeft: {
        setXRotation(90.0f);
        setYRotation(22.5f);
        break;
    }
    case CameraPresetLeftHigh: {
        setXRotation(90.0f);
        setYRotation(45.0f);
        break;
    }
    case CameraPresetRightLow: {
        setXRotation(-90.0f);
        setYRotation(0.0f);
        break;
    }
    case CameraPresetRight: {
        setXRotation(-90.0f);
        setYRotation(22.5f);
        break;
    }
    case CameraPresetRightHigh: {
        setXRotation(-90.0f);
        setYRotation(45.0f);
        break;
    }
    case CameraPresetBehindLow: {
        setXRotation(180.0f);
        setYRotation(0.0f);
        break;
    }
    case CameraPresetBehind: {
        setXRotation(180.0f);
        setYRotation(22.5f);
        break;
    }
    case CameraPresetBehindHigh: {
        setXRotation(180.0f);
        setYRotation(45.0f);
        break;
    }
    case CameraPresetIsometricLeft: {
        setXRotation(45.0f);
        setYRotation(22.5f);
        break;
    }
    case CameraPresetIsometricLeftHigh: {
        setXRotation(45.0f);
        setYRotation(45.0f);
        break;
    }
    case CameraPresetIsometricRight: {
        setXRotation(-45.0f);
        setYRotation(22.5f);
        break;
    }
    case CameraPresetIsometricRightHigh: {
        setXRotation(-45.0f);
        setYRotation(45.0f);
        break;
    }
    case CameraPresetDirectlyAbove: {
        setXRotation(0.0f);
        setYRotation(90.0f);
        break;
    }
    case CameraPresetDirectlyAboveCW45: {
        setXRotation(-45.0f);
        setYRotation(90.0f);
        break;
    }
    case CameraPresetDirectlyAboveCCW45: {
        setXRotation(45.0f);
        setYRotation(90.0f);
        break;
    }
    case CameraPresetFrontBelow: {
        setXRotation(0.0f);
        setYRotation(-45.0f);
        break;
    }
    case CameraPresetLeftBelow: {
        setXRotation(90.0f);
        setYRotation(-45.0f);
        break;
    }
    case CameraPresetRightBelow: {
        setXRotation(-90.0f);
        setYRotation(-45.0f);
        break;
    }
    case CameraPresetBehindBelow: {
        setXRotation(180.0f);
        setYRotation(-45.0f);
        break;
    }
    case CameraPresetDirectlyBelow: {
        setXRotation(0.0f);
        setYRotation(-90.0f);
        break;
    }
    default:
        preset = CameraPresetNone;
        break;
    }

    if (d_ptr->m_activePreset != preset) {
        d_ptr->m_activePreset = preset;
        setDirty(true);
        emit cameraPresetChanged(preset);
    }
}

/*!
 * \property Q3DCamera::zoomLevel
 *
 * This property contains the the camera zoom level in percentage. \c 100.0f means there is no zoom
 * in or out set in the camera.
 */
int Q3DCamera::zoomLevel() const
{
    return d_ptr->m_zoomLevel;
}

void Q3DCamera::setZoomLevel(int zoomLevel)
{
    if (d_ptr->m_zoomLevel != zoomLevel) {
        d_ptr->m_zoomLevel = zoomLevel;
        setDirty(true);
        emit zoomLevelChanged(zoomLevel);
    }
}

/*!
 * Calculates and returns a position relative to the camera using the given parameters
 * and the current camera viewMatrix property.
 * The relative 3D offset to the current camera position is defined in \a relativePosition.
 * An optional fixed rotation of the calculated point around the data visualization area can be
 * given in \a fixedRotation. The rotation is given in degrees.
 * An optional \a distanceModifier modifies the distance of the calculated point from the data
 * visualization.
 * \return calculated position relative to this camera's position.
 */
QVector3D Q3DCamera::calculatePositionRelativeToCamera(const QVector3D &relativePosition,
                                                       float fixedRotation,
                                                       float distanceModifier) const
{
    // Move the position with camera
    GLfloat radiusFactor = cameraDistance * (1.5f + distanceModifier);
    GLfloat xAngle;
    GLfloat yAngle;
    if (!fixedRotation) {
        xAngle = qDegreesToRadians(d_ptr->m_xRotation);
        yAngle = qDegreesToRadians(d_ptr->m_yRotation);
    } else {
        xAngle = qDegreesToRadians(fixedRotation);
        yAngle = 0;
    }
    GLfloat radius = (radiusFactor + relativePosition.y()); // set radius to match the highest height of the position
    GLfloat zPos = radius * qCos(xAngle) * qCos(yAngle);
    GLfloat xPos = radius * qSin(xAngle) * qCos(yAngle);
    GLfloat yPos = (radiusFactor + relativePosition.y()) * qSin(yAngle);

    // Keep in the set position in relation to camera
    return QVector3D(-xPos + relativePosition.x(),
                     yPos + relativePosition.y(),
                     zPos + relativePosition.z());
}

/*!
 * \property Q3DCamera::wrapXRotation
 *
 * This property determines the behavior of the minimum and maximum limits in the X-rotation.
 * By default the X-rotation wraps from minimum value to maximum and from maximum to minimum.
 *
 * If set to \c true the X-rotation of the camera is wrapped from minimum to maximum and from
 * maximum to minimum. If set to \c false the X-rotation of the camera is limited to the sector
 * determined by minimum and maximum values.
 */
bool Q3DCamera::wrapXRotation() const
{
    return d_ptr->m_wrapXRotation;
}

void Q3DCamera::setWrapXRotation(bool isEnabled)
{
    d_ptr->m_wrapXRotation = isEnabled;
}

/*!
 * \property Q3DCamera::wrapYRotation
 *
 * This property determines the behavior of the minimum and maximum limits in the Y-rotation.
 * By default the Y-rotation is limited between the minimum and maximum values without any wrapping.
 *
 * If \c true the Y-rotation of the camera is wrapped from minimum to maximum and from maximum to
 * minimum. If \c false the Y-rotation of the camera is limited to the sector determined by minimum
 * and maximum values.
 */
bool Q3DCamera::wrapYRotation() const
{
    return d_ptr->m_wrapYRotation;
}

void Q3DCamera::setWrapYRotation(bool isEnabled)
{
    d_ptr->m_wrapYRotation = isEnabled;
}

/*!
 * Utility function that sets the camera rotations and distance.\a horizontal and \a vertical
 * define the camera rotations to be used.
 * Optional \a zoom parameter can be given to set the zoom percentage of the camera in range of
 * \c{10.0f - 500.0f}.
 */
void Q3DCamera::setCameraPosition(float horizontal, float vertical, float zoom)
{
    setZoomLevel(qBound(10.0f, zoom, 500.0f));
    setXRotation(horizontal);
    setYRotation(vertical);
}

Q3DCameraPrivate::Q3DCameraPrivate(Q3DCamera *q) :
    q_ptr(q),
    m_isViewMatrixUpdateActive(true),
    m_xRotation(0.0f),
    m_yRotation(0.0f),
    m_minXRotation(-180.0f),
    m_minYRotation(0.0f),
    m_maxXRotation(180.0f),
    m_maxYRotation(90.0f),
    m_wrapXRotation(true),
    m_wrapYRotation(false),
    m_zoomLevel(100),
    m_activePreset(Q3DCamera::CameraPresetNone)
{
}

Q3DCameraPrivate::~Q3DCameraPrivate()
{
}

// Copies changed values from this camera to the other camera. If the other camera had same changes,
// those changes are discarded.
void Q3DCameraPrivate::sync(Q3DCamera &other)
{
    if (q_ptr->isDirty()) {
        other.copyValuesFrom(*q_ptr);
        q_ptr->setDirty(false);
        other.setDirty(false);
    }
}

void Q3DCameraPrivate::setXRotation(const float rotation)
{
    if (m_xRotation != rotation) {
        m_xRotation = rotation;
        q_ptr->setDirty(true);
    }
}

void Q3DCameraPrivate::setYRotation(const float rotation)
{
    if (m_yRotation != rotation) {
        m_yRotation = rotation;
        q_ptr->setDirty(true);
    }
}

void Q3DCameraPrivate::setMinXRotation(const float rotation)
{
    if (m_minXRotation != rotation) {
        m_minXRotation = rotation;
        q_ptr->setDirty(true);
    }
}

void Q3DCameraPrivate::setMinYRotation(const float rotation)
{
    if (m_minYRotation != rotation) {
        m_minYRotation = rotation;
        q_ptr->setDirty(true);
    }
}

void Q3DCameraPrivate::setMaxXRotation(const float rotation)
{
    if (m_maxXRotation != rotation) {
        m_maxXRotation = rotation;
        q_ptr->setDirty(true);
    }
}

void Q3DCameraPrivate::setMaxYRotation(const float rotation)
{
    if (m_maxYRotation != rotation) {
        m_maxYRotation = rotation;
        q_ptr->setDirty(true);
    }
}

// Recalculates the view matrix based on the currently set base orientation, rotation and zoom level values.
//  zoomAdjustment is adjustment to ensure that the 3D visualization stays inside the view area in the 100% zoom.
void Q3DCameraPrivate::updateViewMatrix(float zoomAdjustment)
{
    if (!m_isViewMatrixUpdateActive)
        return;

    int zoom = m_zoomLevel * zoomAdjustment;
    QMatrix4x4 viewMatrix;

    // Apply to view matrix
    viewMatrix.lookAt(q_ptr->position(), m_target, m_up);
    // Compensate for translation (if d_ptr->m_target is off origin)
    viewMatrix.translate(m_target.x(), m_target.y(), m_target.z());
    // Apply rotations
    // Handle x and z rotation when y -angle is other than 0
    viewMatrix.rotate(m_xRotation, 0, qCos(qDegreesToRadians(m_yRotation)),
                      qSin(qDegreesToRadians(m_yRotation)));
    // y rotation is always "clean"
    viewMatrix.rotate(m_yRotation, 1.0f, 0.0f, 0.0f);
    // handle zoom by scaling
    viewMatrix.scale((GLfloat)zoom / 100.0f);
    // Compensate for translation (if d_ptr->m_target is off origin)
    viewMatrix.translate(-m_target.x(), -m_target.y(), -m_target.z());

    q_ptr->setViewMatrix(viewMatrix);
}

QT_END_NAMESPACE_DATAVISUALIZATION