summaryrefslogtreecommitdiff
path: root/src/graphicsscene.cpp
blob: d7e0bba483aabec32f7755bfb19c9184d1fc1323 (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
#include "graphicsscene.h"

// qt
#include <QGraphicsEllipseItem>
#include <QGraphicsLineItem>
#include <QGraphicsPolygonItem>
#include <QGraphicsTextItem>
#include <QGraphicsView>

namespace orphex
{
namespace constants
{
const QLineF laserPlane{QPointF{-1000, 0}, QPointF{1000, 0}};

const auto laserColor = QColor{Qt::red};
const auto sensorColor = QColor{Qt::red};
}

/*!
 * \brief vFoVDegrees - get "vertical" (YZ projection) Field of View (degrees)
 * \param vSensorPlane - sensor plane (YZ projection)
 * \param vLaserRange - laser range (YZ projection)
 * \return - Field of View angle (degrees)
 */
double vFoVDegrees(const QLineF& vSensorPlane, const QLineF& vLaserRange);

/*!
 * \brief lenseBodyRect - create 2d rectangle representing lense body
 * \param diameterMm - body diameter (mm)
 * \param lengthMm - body length (mm)
 * \return - corresponding 2d body rect
 */
QRectF lenseBodyRect(const double diameterMm, const double lengthMm);

/*!
 * \brief laserBodyRect - create 2d rectangle representing laser body
 * \param diameterMm - body diameter (mm)
 * \param lengthMm - body length (mm)
 * \return - corresponding 2d body rect
 */
QRectF laserBodyRect(const double diameterMm, const double lengthMm);

/*!
 * \brief laserBodyRect - create 2d rectangle representing laser body
 * \param diameterMm - body diameter (mm)
 * \param lengthMm - body length (mm)
 * \return - corresponding 2d body rect
 */

/*!
 * \brief scannerBodyFrontWallRect - create 2d rectangle representing front wall
 * of scanner body
 * \param thicknessMm - wall thickness (mm)
 * \param offsetMm - horizontal offset from lense center (mm)
 * \return - corresponding 2d body rect
 */
QRectF scannerBodyFrontWallRect(
    const double thicknessMm,
    const double offsetMm
);

QList<QPointF> xzLaserAreaPolygon(const double laserAngle);
}  // namespace orphex

GraphicsScene::GraphicsScene(QObject* parent)
    : QGraphicsScene{parent}
{
    setSceneRect(QRectF{QPointF{-1000, -1000}, QPointF{1000, 1000}});

    m_lenseItem = addEllipse(
        QRectF{QPointF{-1, -15}, QPointF{1, 15}},
        QPen{Qt::yellow, 0}
    );
    m_lenseItem->setTransformOriginPoint(m_lenseItem->boundingRect().center());

    m_yzSensorItem = new QGraphicsLineItem{QLineF{}, m_lenseItem};
    m_yzSensorItem->setPen(QPen{orphex::constants::sensorColor, 0});

    m_xzSensorItem = addRect({}, QPen{orphex::constants::sensorColor, 0});

    m_lineOfActionItem = new QGraphicsLineItem{
        QLineF{QPointF{0, -1000}, QPointF{0, 1000}},
        m_lenseItem
    };
    m_lineOfActionItem->setPen(QPen{Qt::green, 0});

    m_opticalAxisItem = new QGraphicsLineItem{
        QLineF{QPointF{-1000, 0}, QPointF{1000, 0}},
        m_lenseItem
    };
    m_opticalAxisItem->setPen(QPen{Qt::gray, 0});

    m_lenseVerticalPlaneItem = addLine(
        QLineF{QPointF{0, -1000}, QPointF{0, 1000}},
        QPen{Qt::black, 0}
    );

    m_lenseBodyItem = addRect(
        QRectF{},
        QPen{m_lenseItem->pen().color(), 0},
        QBrush{m_lenseItem->pen().color(), Qt::DiagCrossPattern}
    );
    m_lenseBodyItem->setParentItem(m_lenseItem);
    m_lenseBodyItem->setOpacity(0.1);

    m_laserBodyItem = addRect(
        QRectF{},
        QPen{Qt::black, 0},
        QBrush{Qt::black, Qt::DiagCrossPattern}
    );

    m_scannerBodyFrontWallItem = addRect(
        QRectF{},
        QPen{Qt::lightGray, 0},
        QBrush{Qt::lightGray, Qt::DiagCrossPattern}
    );

    using namespace orphex::constants;
    m_laserPlaneItem = addLine(laserPlane, QPen{Qt::black, 0});
    m_laserPlaneItem->stackBefore(m_lenseItem);

    m_reverseLaserPlaneItem = new QGraphicsLineItem(QLineF{}, m_lenseItem);
    m_reverseLaserPlaneItem->setPen(QPen{Qt::magenta, 2});
    m_reverseLaserPlaneItem->setVisible(false);

    m_desiredLaserPlaneItem = new QGraphicsLineItem(QLineF{}, m_lenseItem);
    m_desiredLaserPlaneItem->setPen(QPen{Qt::yellow, 0.1});
    m_desiredLaserPlaneItem->setOpacity(0.5);

    m_desiredImagePlaneItem = new QGraphicsLineItem(QLineF{}, m_lenseItem);
    m_desiredImagePlaneItem->setPen(QPen{Qt::yellow, 0.15});
    m_desiredImagePlaneItem->stackBefore(m_yzSensorItem);

    m_desiredRangeAreaItem = new QGraphicsPolygonItem{m_lenseItem};
    m_desiredRangeAreaItem->setPen(
        QPen{m_desiredLaserPlaneItem->pen().color(), 0}
    );
    m_desiredRangeAreaItem->setBrush(
        QBrush{m_desiredLaserPlaneItem->pen().color()}
    );
    m_desiredRangeAreaItem->setOpacity(0.1);

    m_actualRangeItem = new QGraphicsLineItem(QLineF{}, m_lenseItem);
    m_actualRangeItem->setPen(QPen{Qt::red, 0.1});
    m_actualRangeItem->setOpacity(0.5);

    m_actualRangeAreaItem = new QGraphicsPolygonItem{m_lenseItem};
    m_actualRangeAreaItem->setPen(QPen{m_yzSensorItem->pen().color(), 0});
    m_actualRangeAreaItem->setBrush(QBrush{m_yzSensorItem->pen().color()});
    m_actualRangeAreaItem->setOpacity(0.1);

    m_xzActualRangeAreaItem = addPolygon(
        {},
        m_actualRangeItem->pen(),
        m_actualRangeAreaItem->brush()
    );
    m_xzActualRangeAreaItem->setOpacity(0.1);
    // m_xzActualRangeAreaItem->setParentItem(m_lenseItem);

    m_sensorLenseIntersectionItem = new QGraphicsEllipseItem{
        QRectF{QPointF{-1, -1}, QPointF{1, 1}},
        m_lenseItem
    };
    m_sensorLenseIntersectionItem->setPen(QPen{Qt::red, 0.1});

    const auto sensorLenseTextItem = new QGraphicsTextItem{
        tr("Scheimpflug: sensor/lense"),
        m_sensorLenseIntersectionItem
    };

    sensorLenseTextItem->setPos(QPointF{-6, -8});
    auto font = sensorLenseTextItem->font();
    font.setPixelSize(2);
    sensorLenseTextItem->setFont(font);
    sensorLenseTextItem->setDefaultTextColor(
        m_sensorLenseIntersectionItem->pen().color()
    );

    m_lenseLaserIntersectionItem = new QGraphicsEllipseItem{
        QRectF{QPointF{-1, -1}, QPointF{1, 1}},
        m_lenseItem
    };
    m_lenseLaserIntersectionItem->setPen(QPen{Qt::blue, 0});

    const auto lenseLaserTextItem = new QGraphicsTextItem{
        tr("Scheimpflug: lense/laser"),
        m_lenseLaserIntersectionItem
    };

    lenseLaserTextItem->setPos(QPointF{-6, -2});
    lenseLaserTextItem->setFont(font);
    lenseLaserTextItem->setDefaultTextColor(
        m_lenseLaserIntersectionItem->pen().color()
    );

    m_xzHardwareLaserPlaneItem = addPolygon(
        QPolygonF{},
        // QPen{orphex::constants::laserColor, 0.},
        QPen{Qt::black, 0.},
        // QBrush{orphex::constants::laserColor, Qt::DiagCrossPattern}
        QBrush{Qt::black, Qt::DiagCrossPattern}
    );
    m_xzHardwareLaserPlaneItem->setOpacity(0.2);
    m_xzHardwareLaserPlaneItem->setParentItem(m_laserBodyItem);
    m_xzDebugItem = addPolygon(
        QPolygonF{},
        QPen{Qt::green, 0.},
        QBrush{Qt::green, Qt::DiagCrossPattern}
    );
    m_xzDebugItem->setOpacity(0.1);
    m_xzDebugItem->setParentItem(m_laserBodyItem);
}

void GraphicsScene::update(OpticalDesign* design)
{
    qDebug() << "update";
    if (!design)
    {
        qCritical() << Q_FUNC_INFO << "design is nullptr";
        return;
    }

    using namespace orphex::constants;

    const auto F = design->get_focalDistanceMm();
    const auto H = design->get_lenseYPosMm();
    const auto w = design->get_sensorPixelsWidth();
    const auto h = design->get_sensorPixelsHeight();
    const auto wMm = design->get_sensorWidthMm();
    const auto hMm = design->get_sensorHeightMm();
    const auto oAngle = design->get_opticalAxisAngleDegrees();

    m_lenseItem->setRotation(m_lenseItem->rotation());
    m_lenseItem->setRotation(-oAngle);
    m_lenseItem->setPos(QPointF{0, -H});

    const auto objectToImage = [F](const QPointF& objectPoint) -> QPointF {
        // please note that object X is negative
        const auto imagePointX = 1. / (1. / F - 1. / -objectPoint.x());
        const auto magnification = imagePointX / objectPoint.x();
        const auto imagePointY = magnification * objectPoint.y();

        return QPointF{imagePointX, imagePointY};
    };

    const auto imageToObject = [F](const QPointF& imagePoint) -> QPointF {
        const auto objectPointX = 1. / (1. / imagePoint.x() - 1. / F);
        const auto magnification = imagePoint.x() / objectPointX;
        const auto objectPointY = imagePoint.y() / magnification;

        return QPointF{objectPointX, objectPointY};
    };

    // calculate sensor plane
    const auto& oai = m_opticalAxisItem;
    const auto& oail = m_opticalAxisItem->line();
    const QLineF opticalAxisScene{
        oai->mapToScene(oail.p1()),
        oai->mapToScene(oail.p2())
    };

    QPointF intersection{};
    const auto type =
        m_laserPlaneItem->line().intersects(opticalAxisScene, &intersection);

    if (type == QLineF::NoIntersection)
    {
        qCritical() << "no intersection between laser plane and optical axis";
        return;
    }

    if (!qFuzzyCompare(intersection.y(), 0.))
    {
        qCritical() << "laser plane/optical axis intersection point y != 0";
        return;
    }

    // take 2 points - to the left and to the right from intersection point;
    const auto leftP = intersection / 4;
    // const auto rightP = intersection + leftP;
    const auto rightP = intersection * 1.5;

    // create desired line on laser plane
    m_desiredLaserPlaneItem->setLine(
        {m_lenseItem->mapFromScene(QPointF{-design->get_zBaseMm(), 0}),
         m_lenseItem->mapFromScene(
             QPointF{-(design->get_zBaseMm() + design->get_zRangeMm()), 0}
         )}
    );

    // calculate image plane
    const auto debugL = m_desiredLaserPlaneItem->line();
    const QLineF debugImageL{
        objectToImage(debugL.p1()),
        objectToImage(debugL.p2())
    };

    m_desiredImagePlaneItem->setLine(debugImageL);

    // create sensor line on image plane
    QPointF sensorCenterPos{};

    if (m_opticalAxisItem->line().intersects(
            m_desiredImagePlaneItem->line(),
            &sensorCenterPos
        ) == QLineF::NoIntersection)
    {
        qCritical() << "no intersection between sensor plane and optical axis";
        return;
    }

    const auto sensorHalfHeight = design->get_sensorHeightMm() / 2.;
    QLineF sensorLine{QPointF{0, 0}, QPointF{0, sensorHalfHeight}};
    sensorLine.translate(sensorCenterPos);
    sensorLine.setAngle(m_desiredImagePlaneItem->line().angle());
    sensorLine.setP1(sensorCenterPos + (sensorCenterPos - sensorLine.p2()));
    // at this momet sensor is centered and rotated. apply vertical offset
    const auto offsetRatio =
        design->get_sensorVerticalOffsetMm() / sensorLine.length();
    const auto offset = sensorLine.pointAt(1 + offsetRatio) - sensorLine.p2();
    sensorLine.setPoints(sensorLine.p1() + offset, sensorLine.p2() + offset);

    m_yzSensorItem->setLine(sensorLine);

    const auto sensorHalfWidth = design->get_sensorWidthMm() / 2.;
    const QRectF xzSensorRect{
        QPointF{
            // m_yzSensorItem->mapToScene(sensorLine.p1()).x(),
            sensorLine.p1().x(),
            sensorHalfWidth
        },
        QPointF{
            // m_yzSensorItem->mapToScene(sensorLine.p2()).x(),
            sensorLine.p2().x(),
            -sensorHalfWidth
        }
    };
    m_xzSensorItem->setRect(xzSensorRect);

    // const auto yzFoVAngle = QLineF{m_lenseItem->pos(), }
    // const auto axisAngleD = design->get_opticalAxisAngleDegrees();
    // const QLineF sceneAxis{
    //     m_opticalAxisItem->mapToScene(m_opticalAxisItem->line().p1()),
    //     m_opticalAxisItem->mapToScene(m_opticalAxisItem->line().p1())
    // };
    // for (auto& p : xzActualRangePolygon)
    // {
    //     // qDebug() << "AZAZA hyp:" << p.x();
    //     // p.setX(sceneAxis.x1());
    //     // qt_noop();
    //     // p.setX(std::sin(qDegreesToRadians(axisAngleD)) * p.x());
    //     // p.setX(std::cos(qDegreesToRadians(axisAngleD)) * p.x());
    //     // p.setX(p.x() / std::cos(qDegreesToRadians(axisAngleD)));
    //     // p.setX(m_lenseItem->mapFromScene(p).x());
    //     p = QPointF
    // }

    // m_xzActualRangeAreaItem->setRotation(180.);

    const auto p1 = xzSensorRect.topLeft();
    const auto p2 = xzSensorRect.bottomRight();

    // find laser plane range which corresponds to sensor position
    m_actualRangeItem->setLine({
        imageToObject(m_yzSensorItem->line().p1()),
        imageToObject(m_yzSensorItem->line().p2()),
    });

    // note that object area has negative coords
    // const auto tmp = m_actualRangeItem->line();
    // const auto tmp2 = m_actualRangeItem->mapToScene(tmp.p1());

    design->set_actualZBaseMm(
        -m_actualRangeItem->mapToScene(m_actualRangeItem->line().p1()).x() /*+
        design->fullBodyOffset()*/
    );
    design->set_actualZRangeMm(m_actualRangeItem->line().length());

    // convert debug image plane to laser plane just to verify they are the same
    // TODO: check with code and log errors?
    const QLineF reverseLaserPlaneLine{
        imageToObject(m_desiredImagePlaneItem->line().p1()),
        imageToObject(m_desiredImagePlaneItem->line().p2())
    };
    m_reverseLaserPlaneItem->setLine(reverseLaserPlaneLine);

    // fill desired range area
    m_desiredRangeAreaItem->setPolygon(
        QPolygonF{
            {m_desiredImagePlaneItem->line().p1(),
             m_desiredImagePlaneItem->line().p2(),
             m_desiredLaserPlaneItem->line().p2(),
             m_desiredLaserPlaneItem->line().p1()}
        }
    );

    // fill actual range area
    m_actualRangeAreaItem->setPolygon(
        QPolygonF{
            {m_yzSensorItem->line().p1(),
             m_yzSensorItem->line().p2(),
             m_actualRangeItem->line().p2(),
             m_actualRangeItem->line().p1()}
        }
    );

    design->set_vFoVDegrees(
        orphex::vFoVDegrees(m_yzSensorItem->line(), m_actualRangeItem->line())
    );

    design->set_sensorLenseAngleDegrees(
        m_desiredImagePlaneItem->line().angleTo(m_lineOfActionItem->line())
    );

    // TODO: take back focal length into account

    // check Scheimpflug principle
    // TODO: draw lines?
    QPointF sensorLenseIntersection{};

    if (m_yzSensorItem->line().intersects(
            m_lineOfActionItem->line(),
            &sensorLenseIntersection
        ) == QLineF::NoIntersection)
    {
        qWarning() << "no intersection between sensor plane and lense plane";
        return;
    }

    m_sensorLenseIntersectionItem->setPos(sensorLenseIntersection);

    QPointF lenseLaserIntersection{};

    if (m_lineOfActionItem->line().intersects(
            m_actualRangeItem->line(),
            &lenseLaserIntersection
        ) == QLineF::NoIntersection)
    {
        qWarning() << "no intersection between laser plane and lense plane";
        return;
    }

    m_lenseLaserIntersectionItem->setPos(lenseLaserIntersection);

    if (!qFuzzyCompare(sensorLenseIntersection, lenseLaserIntersection))
    {
        qWarning() << "The Scheimpflug principle is not observed:" << Qt::endl
                   << "sensor/lense intersection:" << sensorLenseIntersection
                   << Qt::endl
                   << "lense/laser intersection:" << lenseLaserIntersection;
    }

    // TODO: move to settings
    // doesn't work (doesn't zoom)
    constexpr bool autoScale{false};

    if (autoScale)
    {
        // region of interest
        QRectF ROI{};

        ROI |= m_actualRangeAreaItem->boundingRect();
        ROI |= m_desiredRangeAreaItem->boundingRect();
        ROI |= m_lenseItem->boundingRect();
        ROI |= m_yzSensorItem->boundingRect();
        ROI |= m_sensorLenseIntersectionItem->boundingRect();
        ROI |= m_lenseLaserIntersectionItem->boundingRect();

        for (const auto& view : views())
        {
            // TODO: move to settings
            constexpr double margin{5.};
            view->setSceneRect(ROI);
        }
    }

    // calculate distance between sensor and lense on optical axis
    QPointF sensorAxisIntersection{};

    if (m_yzSensorItem->line().intersects(
            m_opticalAxisItem->line(),
            &sensorAxisIntersection
        ) == QLineF::NoIntersection)
    {
        qCritical() << "no intersection between sensor and optical axis";
        return;
    }

    design->set_lenseSensorDistanceMm(
        QLineF{QPointF{0, 0}, sensorAxisIntersection}.length()
    );

    // calculate depth of field
    QPointF axisLaserIntersection{};

    if (m_opticalAxisItem->line().intersects(
            m_actualRangeItem->line(),
            &axisLaserIntersection
        ) == QLineF::NoIntersection)
    {
        qCritical() << "no intersection between laser and optical axis";
        return;
    }

    // const auto zM = std::min(
    //                     design->get_sensorPixelsHeight(),
    //                     design->get_sensorPixelsWidth()
    //                 ) /
    //                 1000. / 1000.;

    // try z based on sensor cell size
    // const auto zMm = std::min(
    //                      design->get_sensorCellHeightUm(),
    //                      design->get_sensorCellHeightUm()
    //                  ) /
    //                  1000.;
    // try z based on 1/16 sensor cell size (calibration step)
    const auto zMm = std::min(
                         design->get_sensorCellHeightUm(),
                         design->get_sensorCellWidthUm()
                     ) /
                     1000. / 16.;

    // try z based on E. Goldovsky method - 0.2% of sensor height
    // const auto zMm = design->get_sensorHeightMm() * 0.2 / 100.;
    // const auto RM =
    //     QLineF{QPointF{0., 0.}, axisLaserIntersection}.length() * 1000.;
    const auto RMidMm = QLineF{QPointF{0., 0.}, axisLaserIntersection}.length();
    // const auto RFrontMm = QLineF{QPointF{0., 0.},
    // axisLaserIntersection}.length(); const auto fM =
    // design->get_backFocalDistanceMm() * 1000.;

    // Gordijchuk & Pell, 1979, say to use focal distance.
    // Wikipedia, based on the same book, says to use back focal distance. WTF?
    // const auto fMm = design->get_backFocalDistanceMm();
    const auto fMm = design->get_focalDistanceMm();

    const auto K = static_cast<int>(design->get_lenseAperture()) / 10.;

    using FromSharpDistMm = double;
    using BackSharpDistMm = double;
    using DofResult = std::tuple<FromSharpDistMm, BackSharpDistMm>;
    const auto calculateDof = [zMm, fMm, K](const auto RMm) -> DofResult {
        const auto ff = fMm * fMm;
        const auto Rff = RMm * ff;
        const auto Kfz = K * fMm * zMm;
        const auto KRz = K * RMm * zMm;

        const auto R1Mm = Rff / (ff - Kfz + KRz);
        const auto R2Mm = Rff / (ff + Kfz - KRz);

        return {R1Mm, R2Mm};
    };

    const auto ff = fMm * fMm;
    const auto Rff = RMidMm * ff;
    const auto Kfz = K * fMm * zMm;
    const auto KRz = K * RMidMm * zMm;

    // const auto R1M = Rff / (ff - Kfz + KRz);
    // const auto R2M = Rff / (ff + Kfz - KRz);

    // const auto R1Mm = Rff / (ff - Kfz + KRz);
    // const auto R2Mm = Rff / (ff + Kfz - KRz);

    const auto [R1Mm, R2Mm] = calculateDof(RMidMm);
    design->set_frontSharpDistanceMm(R1Mm);
    design->set_backSharpDistanceMm(R2Mm);
    const auto depthOfFieldMm = R2Mm - R1Mm;
    design->set_depthOfFieldMm(depthOfFieldMm);

    if (qFuzzyIsNull(depthOfFieldMm))
    {
        emit design->depthOfFieldMmChanged();
        emit design->depthOfFieldMmChanged(depthOfFieldMm);
    }

    m_lenseBodyItem->setRect(
        orphex::lenseBodyRect(
            design->get_lenseBodyMaxDiameterMm(),
            design->get_lenseBodyLengthMm()
        )
    );

    m_laserBodyItem->setRect(
        orphex::laserBodyRect(
            design->get_laserBodyDiameterMm(),
            design->get_laserBodyLengthMm()
        )
    );

    m_scannerBodyFrontWallItem->setRect(
        orphex::scannerBodyFrontWallRect(
            design->get_scannerBodyWallThicknessMm(),
            design->get_scannerBodyFrontWallOffsetMm()
        )
    );

    {
        m_xzHardwareLaserPlaneItem->setPolygon(
            orphex::xzLaserAreaPolygon(design->get_laserAngleDegrees())
        );
    }

    {
        const auto item = m_actualRangeItem;
        const auto line = item->line();
        const auto x1 = item->mapToScene(line.p1()).x();
        const auto x2 = item->mapToScene(line.p2()).x();
        QPolygonF xzActualRangePolygon{QList<QPointF>{
            QPointF{x1, imageToObject(m_xzSensorItem->rect().topLeft()).y()},
            QPointF{x1, imageToObject(m_xzSensorItem->rect().bottomLeft()).y()},
            QPointF{
                x2,
                imageToObject(m_xzSensorItem->rect().bottomRight()).y()
            },
            QPointF{x2, imageToObject(m_xzSensorItem->rect().topRight()).y()},
        }};
        m_xzActualRangeAreaItem->setPolygon(xzActualRangePolygon);

        m_xzDebugItem->setPolygon(
            QPolygonF{QList<QPointF>{
                QPointF{0., 0.},
                QPointF{
                    x1,
                    imageToObject(m_xzSensorItem->rect().topLeft()).y()
                },
                QPointF{
                    x1,
                    imageToObject(m_xzSensorItem->rect().bottomLeft()).y()
                },
            }}
        );
    }
}

double orphex::vFoVDegrees(
    const QLineF& vSensorPlane,
    const QLineF& vLaserRange
)
{
    const auto sl = vSensorPlane;
    const auto arl = vLaserRange;

    const QLineF l0{sl.p1(), arl.p1()};
    const QLineF l1{sl.p2(), arl.p2()};

    return l1.angleTo(l0);
}

QRectF orphex::lenseBodyRect(const double diameterMm, const double lengthMm)
{
    return QRectF{
        QPointF{-lengthMm, -diameterMm / 2.},
        QSizeF{lengthMm, diameterMm}
    };
}

QRectF orphex::laserBodyRect(const double diameterMm, const double lengthMm)
{
    return QRectF{QPointF{0., -diameterMm / 2.}, QSizeF{lengthMm, diameterMm}};
}

QRectF orphex::scannerBodyFrontWallRect(
    const double thicknessMm,
    const double offsetMm
)
{
    return QRectF{
        QPointF{offsetMm - thicknessMm, -1000.},
        QPointF{offsetMm, 1000.}
    };
}

QList<QPointF> orphex::xzLaserAreaPolygon(const double laserAngle)
{
    QLineF line{QPointF{0., 0.}, QPointF{-1000., 0.}};

    line.setAngle(180. + laserAngle / 2.);

    QList<QPointF> result{QPointF{0., 0.}, line.p2()};

    line.setAngle(180. - laserAngle / 2.);

    result << line.p2();

    return result;
}