-
Notifications
You must be signed in to change notification settings - Fork 22
/
formheadache.cpp
1104 lines (916 loc) · 33.4 KB
/
formheadache.cpp
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
#include "formheadache.h"
#include "cheadline.h"
#include "cboundary.h"
#include "cabcurve.h"
#include "cvehicle.h"
#include "glm.h"
#include "qmlutil.h"
#include "ctrack.h"
#include <QMatrix4x4>
#include <QOpenGLFunctions>
#include "glutils.h"
#include "aogproperty.h"
#include "aogrenderer.h"
#include <QTime>
//here for now. Put in another module for use in other places.
void CalculateHeadings(QVector<Vec3> &xList);
void MakePointMinimumSpacing(QVector<Vec3> &xList, double minDistance);
int GetLineIntersection(double p0x, double p0y, double p1x, double p1y,
double p2x, double p2y, double p3x, double p3y,
double &iEast,
double &iNorth);
FormHeadache::FormHeadache(QObject *parent)
: QObject{parent}
{}
void FormHeadache::connect_ui(QObject *headache_designer_instance) {
isA = true; //why is this not initialized?
this->headache_designer_instance = headache_designer_instance;
InterfaceProperty<HeadacheDesigner,int>::set_qml_root(headache_designer_instance);
InterfaceProperty<HeadacheDesigner,bool>::set_qml_root(headache_designer_instance);
InterfaceProperty<HeadacheDesigner,double>::set_qml_root(headache_designer_instance);
InterfaceProperty<HeadacheDesigner,QColor>::set_qml_root(headache_designer_instance);
InterfaceProperty<HeadacheDesigner,QPoint>::set_qml_root(headache_designer_instance);
//connect UI signals
connect(headache_designer_instance,SIGNAL(load()),this,SLOT(load_headline()));
connect(headache_designer_instance,SIGNAL(update_lines()),this,SLOT(update_lines()));
connect(headache_designer_instance,SIGNAL(mouseClicked(int, int)),this,SLOT(clicked(int,int)));
connect(headache_designer_instance,SIGNAL(cycleForward()),this,SLOT(btnCycleForward_Click()));
connect(headache_designer_instance,SIGNAL(cycleBackward()),this,SLOT(btnCycleBackward_Click()));
connect(headache_designer_instance,SIGNAL(deleteCurve()),this,SLOT(btnDeleteCurve_Click()));
connect(headache_designer_instance,SIGNAL(deleteHeadland()), this, SLOT(btnDeleteHeadland_Click()));
connect(headache_designer_instance,SIGNAL(createHeadland()),this,SLOT(btnBndLoop_Click()));
//connect(headache_designer_instance,SIGNAL(deletePoints()),this,SLOT(btnDeletePoints_Click()));
connect(headache_designer_instance,SIGNAL(ashrink()),this,SLOT(btnAShrink_Click()));
connect(headache_designer_instance,SIGNAL(bshrink()),this,SLOT(btnBShrink_Click()));
connect(headache_designer_instance,SIGNAL(alength()),this,SLOT(btnALength_Click()));
connect(headache_designer_instance,SIGNAL(blength()),this,SLOT(btnBLength_Click()));
connect(headache_designer_instance,SIGNAL(headlandOff()),this,SLOT(btnHeadlandOff_Click()));
connect(headache_designer_instance,SIGNAL(cancelTouch()),this,SLOT(btnCancelTouch_Click()));
connect(headache_designer_instance,SIGNAL(isSectionControlled(bool)),this,SLOT(isSectionControlled(bool)));
connect(headache_designer_instance,SIGNAL(save_exit()),this,SLOT(btnExit_Click()));
}
void FormHeadache::setFieldInfo(double maxFieldDistance, double fieldCenterX, double fieldCenterY) {
this->maxFieldDistance = maxFieldDistance;
this->fieldCenterX = fieldCenterX;
this->fieldCenterY = fieldCenterY;
}
QVector3D FormHeadache::mouseClickToField(int mouseX, int mouseY) {
/* returns the field easting and northing position of a
* mouse click
*/
QMatrix4x4 modelview;
QMatrix4x4 projection;
int width = qmlItem(headache_designer_instance, "headacheRenderer")->property("width").toReal();
int height = qmlItem(headache_designer_instance, "headacheRenderer")->property("height").toReal();
projection.setToIdentity();
//to shift, translate projection here. -1,0,0 is far left, 1,0,0 is far right.
//58 degrees view
//projection.viewport(0,0,width,height);
projection.perspective(58, 1.0f, 1.0f, 20000);
modelview.setToIdentity();
//back the camera up
modelview.translate(0, 0, -(double)maxFieldDistance * (double)zoom);
//translate to that spot in the world
modelview.translate(-(double)fieldCenterX + (double)sX * (double)maxFieldDistance,
-(double)fieldCenterY + (double)sY * (double)maxFieldDistance,
0);
float x,y;
x = mouseX;
y = height - mouseY;
//y = mouseY;
//get point on the near plane
QVector3D worldpoint_near = QVector3D( { x, y, 0} ).unproject(modelview,projection,QRect(0,0,width, height));
//get point on the far plane
QVector3D worldpoint_far = QVector3D( { x, y, 1} ).unproject(modelview, projection,QRect(0,0,width, height));
//get direction vector from near to far
QVector3D direction = worldpoint_far - worldpoint_near;
//determine intercept with z=0 plane, and calculate easting and northing
double lambda = -(worldpoint_near.z()) / direction.z();
float mouseEasting = worldpoint_near.x() + lambda * direction.x();
float mouseNorthing = worldpoint_near.y() + lambda * direction.y();
QVector3D fieldCoord = QVector3D( { (float)mouseEasting, (float)mouseNorthing, 0 } );
return fieldCoord;
}
void FormHeadache::load_headline() {
hdl->idx = -1;
headache_designer_instance->setProperty("showa", false);
headache_designer_instance->setProperty("showb", false);
emit loadHeadlines();
//clear headline. Not sure I like that.
update_lines();
update_headland();
}
void FormHeadache::setup_matrices(QMatrix4x4 &modelview, QMatrix4x4 &projection) {
projection.setToIdentity();
//to shift, translate projection here. -1,0,0 is far left, 1,0,0 is far right.
//58 degrees view
projection.perspective(58, 1.0f, 1.0f, 20000);
modelview.setToIdentity();
//back the camera up
modelview.translate(0, 0, -(double)maxFieldDistance * (double)zoom);
//translate to that spot in the world
modelview.translate(-(double)fieldCenterX + (double)sX * (double)maxFieldDistance,
-(double)fieldCenterY + (double)sY * (double)maxFieldDistance,
0);
}
void FormHeadache::update_lines() {
QVariantList lines;
QVariantMap linemap;
QVariantList linepoints;
QPoint linepoint;
QColor color;
QMatrix4x4 modelview;
QMatrix4x4 projection;
QVector3D s;
QVector3D p;
setup_matrices(modelview, projection);
int width = qmlItem(headache_designer_instance, "headacheRenderer")->property("width").toReal();
int height = qmlItem(headache_designer_instance, "headacheRenderer")->property("height").toReal();
for (int j = 0; j < bnd->bndList.count(); j++)
{
if (j == bndSelect)
color = QColor::fromRgbF(0.75f, 0.75f, 0.750f);
else
color = QColor::fromRgbF(0.0f, 0.25f, 0.10f);
linemap.clear();
linemap["index"] = j;
linemap["color"] = color;
linemap["width"] = 4;
linepoints.clear();
for (int i = 0; i < bnd->bndList[j].fenceLine.count(); i++)
{
p = QVector3D (bnd->bndList[j].fenceLine[i].easting,
bnd->bndList[j].fenceLine[i].northing,
0);
s = p.project(modelview, projection, QRect(0,0,width,height));
linepoint = QPoint(s.x(),height - s.y());
linepoints.append(linepoint);
}
linemap["points"] = linepoints;
lines.append(linemap);
}
headache_designer_instance->setProperty("boundaryLines", lines);
update_headlines();
update_ab();
update_headland();
}
void FormHeadache::update_ab() {
QMatrix4x4 modelview, projection;
QVector3D p, s;
setup_matrices(modelview, projection);
int width = qmlItem(headache_designer_instance, "headacheRenderer")->property("width").toReal();
int height = qmlItem(headache_designer_instance, "headacheRenderer")->property("height").toReal();
//draw A and B points
if (start != 99999) {
p = QVector3D(bnd->bndList[bndSelect].fenceLine[start].easting, bnd->bndList[bndSelect].fenceLine[start].northing, 0);
s = p.project(modelview, projection, QRect(0,0,width,height));
showa = true;
apoint = QPoint(s.x(), height - s.y());
} //else {
//showa = false;
//}
if (end != 99999) {
p = QVector3D(bnd->bndList[bndSelect].fenceLine[end].easting, bnd->bndList[bndSelect].fenceLine[end].northing, 0);
s = p.project(modelview, projection, QRect(0,0,width,height));
bpoint = QPoint(s.x(), height - s.y());
showb = true;
}
}
void FormHeadache::update_headland() {
QMatrix4x4 modelview, projection;
QVector3D p, s;
QVariantList line;
QPoint linepoint;
setup_matrices(modelview, projection);
int width = qmlItem(headache_designer_instance, "headacheRenderer")->property("width").toReal();
int height = qmlItem(headache_designer_instance, "headacheRenderer")->property("height").toReal();
//draw headland line if exists
if (bnd->bndList[0].hdLine.count()) {
//color is set in QML
for (int i = 0; i < bnd->bndList[0].hdLine.count(); i++)
{
p = QVector3D (bnd->bndList[0].hdLine[i].easting,
bnd->bndList[0].hdLine[i].northing,
0);
s = p.project(modelview, projection, QRect(0,0,width,height));
linepoint = QPoint(s.x(),height - s.y());
line.append(linepoint);
}
}
headache_designer_instance->setProperty("headlandLine", line);
}
void FormHeadache::update_headlines()
{
QMatrix4x4 modelview, projection;
QVector3D p, s;
QVariantList lines;
QVariantMap linemap;
QVariantList linepoints;
QPoint linepoint;
QColor color;
setup_matrices(modelview, projection);
int width = qmlItem(headache_designer_instance, "headacheRenderer")->property("width").toReal();
int height = qmlItem(headache_designer_instance, "headacheRenderer")->property("height").toReal();
lines.clear();
showa = false;
showb = false;
if (isLinesVisible && hdl->tracksArr.count() > 0)
{
//GL.Enable(EnableCap.LineStipple);
//GL.LineStipple(1, 0x7070);
//GL.PointSize(3);
for (int i = 0; i < hdl->tracksArr.count(); i++)
{
linemap.clear();
if (hdl->tracksArr[i].mode == (int)TrackMode::AB)
{
color = QColor::fromRgbF(0.973f, 0.9f, 0.10f);
}
else
{
color = QColor::fromRgbF(0.3f, 0.99f, 0.20f);
}
linemap["color"] = color;
linepoints.clear();
for (Vec3 item: hdl->tracksArr[i].trackPts)
{
p = QVector3D(item.easting, item.northing, 0);
s = p.project(modelview, projection, QRect(0,0,width,height));
linepoint = QPoint(s.x(), height - s.y());
linepoints.append(linepoint);
}
linemap["points"] = linepoints;
linemap["dashed"] = true;
linemap["width"] = 3;
linemap["index"] = 0; //unused
lines.append(linemap);
}
//GL.Disable(EnableCap.LineStipple);
if (hdl->idx > -1)
{
linemap.clear();
linemap["width"] = 6;
//GL.LineWidth(6);
linemap["color"] = QColor::fromRgbF(1.0f, 0.0f, 1.0f);
linemap["dashed"] = false;
linepoints.clear();
//GL.Begin(PrimitiveType.LineStrip);
for (Vec3 item: hdl->tracksArr[hdl->idx].trackPts)
{
p = QVector3D(item.easting, item.northing, 0);
s = p.project(modelview, projection, QRect(0,0,width,height));
linepoint = QPoint(s.x(), height - s.y());
linepoints.append(linepoint);
}
linemap["points"] = linepoints;
lines.append(linemap);
int cnt = hdl->tracksArr[hdl->idx].trackPts.count() - 1;
p = QVector3D(hdl->tracksArr[hdl->idx].trackPts[0].easting, hdl->tracksArr[hdl->idx].trackPts[0].northing,0);
s = p.project(modelview, projection, QRect(0,0,width,height));
apoint = QPoint(s.x(), height - s.y());
showa = true;
p = QVector3D(hdl->tracksArr[hdl->idx].trackPts[cnt].easting, hdl->tracksArr[hdl->idx].trackPts[cnt].northing,0);
s = p.project(modelview, projection, QRect(0,0,width,height));
bpoint = QPoint(s.x(), height - s.y());
showb = true;
}
}
headache_designer_instance->setProperty("headacheLines", lines);
}
void FormHeadache::FormHeadLine_FormClosing()
{
//hdl
if (hdl->idx == -1)
{
isBtnAutoSteerOn = false;
isYouTurnBtnOn = false;
}
emit saveHeadlines();
if (hdl->tracksArr.count() > 0)
{
hdl->idx = 0;
}
else hdl->idx = -1;
}
void FormHeadache::btnCycleBackward_Click()
{
bnd->bndList[0].hdLine.clear();
if (hdl->tracksArr.count() > 0)
{
hdl->idx++;
if (hdl->idx > (hdl->tracksArr.count() - 1)) hdl->idx = 0;
}
else
{
hdl->idx = -1;
}
update_headlines();
}
void FormHeadache::btnCycleForward_Click()
{
bnd->bndList[0].hdLine.clear();
if (hdl->tracksArr.count() > 0)
{
hdl->idx--;
if (hdl->idx < 0) hdl->idx = (hdl->tracksArr.count() - 1);
}
else
{
hdl->idx = -1;
}
update_headlines();
}
void FormHeadache::clicked(int mouseX, int mouseY) {
QVector3D fieldCoords = mouseClickToField(mouseX, mouseY);
pint.easting = fieldCoords.x();
pint.northing = fieldCoords.y();
bnd->bndList[0].hdLine.clear();
hdl->idx = -1;
headache_designer_instance->setProperty("showa", false);
headache_designer_instance->setProperty("showb", false);
if (isA)
{
double minDistA = glm::DOUBLE_MAX;
start = 99999; end = 99999;
for (int j = 0; j < bnd->bndList.count(); j++)
{
for (int i = 0; i < bnd->bndList[j].fenceLine.count(); i++)
{
double dist = ((pint.easting - bnd->bndList[j].fenceLine[i].easting) * (pint.easting - bnd->bndList[j].fenceLine[i].easting))
+ ((pint.northing - bnd->bndList[j].fenceLine[i].northing) * (pint.northing - bnd->bndList[j].fenceLine[i].northing));
if (dist < minDistA)
{
minDistA = dist;
bndSelect = j;
start = i;
}
}
}
isA = false;
update_ab();
}
else
{
double minDistA = glm::DOUBLE_MAX;
int j = bndSelect;
for (int i = 0; i < bnd->bndList[j].fenceLine.count(); i++)
{
double dist = ((pint.easting - bnd->bndList[j].fenceLine[i].easting) * (pint.easting - bnd->bndList[j].fenceLine[i].easting))
+ ((pint.northing - bnd->bndList[j].fenceLine[i].northing) * (pint.northing - bnd->bndList[j].fenceLine[i].northing));
if (dist < minDistA)
{
minDistA = dist;
end = i;
}
}
isA = true;
if (start == end)
{
start = 99999; end = 99999;
emit timedMessageBox(3000, tr("Line Error"), tr("Start Point = End Point"));
return;
}
//build the lines
if ((bool)curveLine)
{
hdl->tracksArr.append(CHeadPath());
hdl->idx = hdl->tracksArr.count() - 1;
bool isLoop = false;
int limit = end;
if ((abs(start - end)) > (bnd->bndList[bndSelect].fenceLine.count() * 0.5))
{
if (start < end)
{
int tmp = start;
start = end;
end = tmp;
}
isLoop = true;
if (start < end)
{
limit = end;
end = 0;
}
else
{
limit = end;
end = bnd->bndList[bndSelect].fenceLine.count();
}
}
else
{
if (start > end)
{
int tmp = start;
start = end;
end = tmp;
}
}
hdl->tracksArr[hdl->idx].a_point = start;
hdl->tracksArr[hdl->idx].trackPts.clear();
Vec3 pt3;
if (start < end)
{
for (int i = start; i <= end; i++)
{
//calculate the point inside the boundary
pt3 = bnd->bndList[bndSelect].fenceLine[i];
hdl->tracksArr[hdl->idx].trackPts.append(pt3);
if (isLoop && i == bnd->bndList[bndSelect].fenceLine.count() - 1)
{
i = -1;
isLoop = false;
end = limit;
}
}
}
else
{
for (int i = start; i >= end; i--)
{
//calculate the point inside the boundary
pt3 = bnd->bndList[bndSelect].fenceLine[i];
hdl->tracksArr[hdl->idx].trackPts.append(pt3);
if (isLoop && i == 0)
{
i = bnd->bndList[bndSelect].fenceLine.count() - 1;
isLoop = false;
end = limit;
}
}
}
//who knows which way it actually goes
CalculateHeadings(hdl->tracksArr[hdl->idx].trackPts);
int ptCnt = hdl->tracksArr[hdl->idx].trackPts.count() - 1;
for (int i = 1; i < 30; i++)
{
Vec3 pt(hdl->tracksArr[hdl->idx].trackPts[ptCnt]);
pt.easting += (sin(pt.heading) * i);
pt.northing += (cos(pt.heading) * i);
hdl->tracksArr[hdl->idx].trackPts.append(pt);
}
Vec3 stat(hdl->tracksArr[hdl->idx].trackPts[0]);
for (int i = 1; i < 30; i++)
{
Vec3 pt(stat);
pt.easting -= (sin(pt.heading) * i);
pt.northing -= (cos(pt.heading) * i);
hdl->tracksArr[hdl->idx].trackPts.insert(0, pt);
}
//create a name
hdl->tracksArr[hdl->idx].name = QString("%1 Cu %2").arg(hdl->idx).arg(QTime::currentTime().toString("mm:ss"));
hdl->tracksArr[hdl->idx].moveDistance = 0;
hdl->tracksArr[hdl->idx].mode = (int)TrackMode::Curve;
emit saveHeadlines();
//update arrays
start = 99999; end = 99999;
//FixLabelsCurve();
}
else //straight line
{
if ((abs(start - end)) > (bnd->bndList[bndSelect].fenceLine.count() * 0.5))
{
if (start < end)
{
int tmp = start;
start = end;
end = tmp;
}
}
else
{
if (start > end)
{
int tmp = start;
start = end;
end = tmp;
}
}
Vec3 ptA(bnd->bndList[bndSelect].fenceLine[start]);
Vec3 ptB(bnd->bndList[bndSelect].fenceLine[end]);
//calculate the AB Heading
double abHead = atan2(
bnd->bndList[bndSelect].fenceLine[end].easting - bnd->bndList[bndSelect].fenceLine[start].easting,
bnd->bndList[bndSelect].fenceLine[end].northing - bnd->bndList[bndSelect].fenceLine[start].northing);
if (abHead < 0) abHead += glm::twoPI;
if (hdl->idx < hdl->tracksArr.count() - 1)
{
hdl->idx++;
hdl->tracksArr.insert(hdl->idx, CHeadPath());
}
else
{
hdl->tracksArr.append(CHeadPath());
hdl->idx = hdl->tracksArr.count() - 1;
}
hdl->tracksArr[hdl->idx].a_point = start;
hdl->tracksArr[hdl->idx].trackPts.clear();
ptA.heading = abHead;
ptB.heading = abHead;
for (int i = 0; i <= (int)(glm::Distance(ptA, ptB)); i++)
{
Vec3 ptC(
(sin(abHead) * i) + ptA.easting,
(cos(abHead) * i) + ptA.northing,
abHead
);
hdl->tracksArr[hdl->idx].trackPts.append(ptC);
}
int ptCnt = hdl->tracksArr[hdl->idx].trackPts.count() - 1;
for (int i = 1; i < 30; i++)
{
Vec3 pt(hdl->tracksArr[hdl->idx].trackPts[ptCnt]);
pt.easting += (sin(pt.heading) * i);
pt.northing += (cos(pt.heading) * i);
hdl->tracksArr[hdl->idx].trackPts.append(pt);
}
Vec3 stat(hdl->tracksArr[hdl->idx].trackPts[0]);
for (int i = 1; i < 30; i++)
{
Vec3 pt(stat);
pt.easting -= (sin(pt.heading) * i);
pt.northing -= (cos(pt.heading) * i);
hdl->tracksArr[hdl->idx].trackPts.insert(0, pt);
}
//create a name
hdl->tracksArr[hdl->idx].name = QString("%1 AB %2").arg(hdl->idx).arg(QTime::currentTime().toString("mm:ss"));
hdl->tracksArr[hdl->idx].moveDistance = 0;
hdl->tracksArr[hdl->idx].mode = (int)TrackMode::AB;
emit saveHeadlines();
start = 99999; end = 99999;
}
//bnd->bndList[0].hdLine.clear();
hdl->desList.clear();
if (hdl->tracksArr.count() < 1 || hdl->idx == -1) return;
double distAway = (double)lineDistance;
hdl->tracksArr[hdl->idx].moveDistance += distAway;
double distSqAway = (distAway * distAway) - 0.01;
Vec3 point;
int refCount = hdl->tracksArr[hdl->idx].trackPts.count();
for (int i = 0; i < refCount; i++)
{
point = Vec3(
hdl->tracksArr[hdl->idx].trackPts[i].easting - (sin(glm::PIBy2 + hdl->tracksArr[hdl->idx].trackPts[i].heading) * distAway),
hdl->tracksArr[hdl->idx].trackPts[i].northing - (cos(glm::PIBy2 + hdl->tracksArr[hdl->idx].trackPts[i].heading) * distAway),
hdl->tracksArr[hdl->idx].trackPts[i].heading);
bool Add = true;
for (int t = 0; t < refCount; t++)
{
double dist = ((point.easting - hdl->tracksArr[hdl->idx].trackPts[t].easting) * (point.easting - hdl->tracksArr[hdl->idx].trackPts[t].easting))
+ ((point.northing - hdl->tracksArr[hdl->idx].trackPts[t].northing) * (point.northing - hdl->tracksArr[hdl->idx].trackPts[t].northing));
if (dist < distSqAway)
{
Add = false;
break;
}
}
if (Add)
{
if (hdl->desList.count() > 0)
{
double dist = ((point.easting - hdl->desList[hdl->desList.count() - 1].easting) * (point.easting - hdl->desList[hdl->desList.count() - 1].easting))
+ ((point.northing - hdl->desList[hdl->desList.count() - 1].northing) * (point.northing - hdl->desList[hdl->desList.count() - 1].northing));
if (dist > 1)
hdl->desList.append(point);
}
else hdl->desList.append(point);
}
}
hdl->tracksArr[hdl->idx].trackPts.clear();
for (int i = 0; i < hdl->desList.count(); i++)
{
hdl->tracksArr[hdl->idx].trackPts.append(Vec3(hdl->desList[i]));
}
hdl->desList.clear();
update_headlines();
}
}
void FormHeadache::btnDeleteCurve_Click()
{
//bnd->bndList[0].hdLine.clear();
if (hdl->tracksArr.count() > 0 && hdl->idx > -1)
{
hdl->tracksArr.remove(hdl->idx);
hdl->idx--;
}
if (hdl->tracksArr.count() > 0)
{
if (hdl->idx == -1)
{
hdl->idx++;
}
}
else hdl->idx = -1;
update_headlines();
}
void FormHeadache::btnExit_Click() {
emit saveHeadlines();
}
void FormHeadache::isSectionControlled(bool wellIsIt) {
bnd->isSectionControlledByHeadland = wellIsIt;
property_setHeadland_isSectionControlled = wellIsIt;
}
void FormHeadache::btnBndLoop_Click() {
//sort the lines using CHeadPath::operator<
std::sort(hdl->tracksArr.begin(), hdl->tracksArr.end());
emit saveHeadlines();
hdl->idx = -1;
//build the headland
bnd->bndList[0].hdLine.clear();
//int numOfLines = hdl->tracksArr.count();
int nextLine = 0;
crossings.clear();
int isStart = 0;
for (int lineNum = 0; lineNum < hdl->tracksArr.count(); lineNum++)
{
nextLine = lineNum - 1;
if (nextLine < 0) nextLine = hdl->tracksArr.count() - 1;
if (nextLine == lineNum)
{
emit timedMessageBox(2000, tr("Create Error"), tr("Is there maybe only one line?"));
return;
}
for (int i = 0; i < hdl->tracksArr[lineNum].trackPts.count() - 2; i++)
{
for (int k = 0; k < hdl->tracksArr[nextLine].trackPts.count() - 2; k++)
{
int res = GetLineIntersection(
hdl->tracksArr[lineNum].trackPts[i].easting,
hdl->tracksArr[lineNum].trackPts[i].northing,
hdl->tracksArr[lineNum].trackPts[i + 1].easting,
hdl->tracksArr[lineNum].trackPts[i + 1].northing,
hdl->tracksArr[nextLine].trackPts[k].easting,
hdl->tracksArr[nextLine].trackPts[k].northing,
hdl->tracksArr[nextLine].trackPts[k + 1].easting,
hdl->tracksArr[nextLine].trackPts[k + 1].northing,
iE, iN);
if (res == 1)
{
if (isStart == 0) i++;
crossings.append(i);
isStart++;
if (isStart == 2) goto again;
nextLine = lineNum + 1;
if (nextLine > hdl->tracksArr.count() - 1) nextLine = 0;
}
}
}
again:
isStart = 0;
}
if (crossings.count() != hdl->tracksArr.count() * 2)
{
emit timedMessageBox(2000, tr("Crosings Error"), tr("Make sure all ends cross only once"));
bnd->bndList[0].hdLine.clear();
return;
}
for (int i = 0; i < hdl->tracksArr.count(); i++)
{
int low = crossings[i * 2];
int high = crossings[i * 2 + 1];
for (int k = low; k < high; k++)
{
bnd->bndList[0].hdLine.append(hdl->tracksArr[i].trackPts[k]);
}
}
//for (int i = 0; i < hdl->tracksArr.count(); i++)
//{
// hdl->desList.clear();
// int low = crossings[i * 2];
// int high = crossings[i * 2 + 1];
// for (int k = low; k < high; k++)
// {
// hdl->desList.append(hdl->tracksArr[i].trackPts[k]);
// }
// hdl->tracksArr[i].trackPts.clear();
// foreach (var item in hdl->desList)
// {
// hdl->tracksArr[i].trackPts.append(Vec3(item));
// }
//}
QVector<Vec3> hdArr;
if (bnd->bndList[0].hdLine.count() > 0)
{
hdArr = bnd->bndList[0].hdLine;
}
else
{
bnd->bndList[0].hdLine.clear();
return;
}
//middle points
for (int i = 1; i < hdArr.count(); i++)
{
hdArr[i - 1].heading = atan2(hdArr[i - 1].easting - hdArr[i].easting, hdArr[i - 1].northing - hdArr[i].northing);
if (hdArr[i].heading < 0) hdArr[i].heading += glm::twoPI;
}
double delta = 0;
for (int i = 0; i < hdArr.count(); i++)
{
if (i == 0)
{
bnd->bndList[0].hdLine.append(Vec3(hdArr[i].easting, hdArr[i].northing, hdArr[i].heading));
continue;
}
delta += (hdArr[i - 1].heading - hdArr[i].heading);
if (fabs(delta) > 0.005)
{
Vec3 pt(hdArr[i].easting, hdArr[i].northing, hdArr[i].heading);
bnd->bndList[0].hdLine.append(pt);
delta = 0;
}
}
update_headland();
emit saveHeadland();
}
void FormHeadache::btnSlice_Click() {
int startBnd = 0, endBnd = 0, startLine = 0, endLine = 0;
int isStart = 0;
if (sliceArr.count() == 0) return;
//save a backup
backupList.clear();
for (auto item: bnd->bndList[0].hdLine)
{
backupList.append(item);
}
for (int i = 0; i < sliceArr.count() - 2; i++)
{
for (int k = 0; k < bnd->bndList[0].hdLine.count() - 2; k++)
{
int res = GetLineIntersection(
sliceArr[i].easting,
sliceArr[i].northing,
sliceArr[i + 1].easting,
sliceArr[i + 1].northing,
bnd->bndList[0].hdLine[k].easting,
bnd->bndList[0].hdLine[k].northing,
bnd->bndList[0].hdLine[k + 1].easting,
bnd->bndList[0].hdLine[k + 1].northing,
iE, iN);
if (res == 1)
{
if (isStart == 0)
{
startBnd = k + 1;
startLine = i + 1;
}
else
{
endBnd = k + 1;
endLine = i;
}
isStart++;
}
}
}
if (isStart < 2)
{
emit timedMessageBox(2000, tr("Error"), tr("Crossings not Found"));
return;
}
//overlaps start finish
if ((fabs(startBnd - endBnd)) > (bnd->bndList[bndSelect].fenceLine.count() * 0.5))
{
if (startBnd < endBnd)
{
int tmp = startBnd;
startBnd = endBnd;
endBnd = tmp;
}
hdl->desList.clear();
//first bnd segment
for (int i = endBnd; i < startBnd; i++)
{
hdl->desList.append(bnd->bndList[0].hdLine[i]);
}
for (int i = startLine; i < endLine; i++)
{
hdl->desList.append(sliceArr[i]);
}
//build headline from desList
bnd->bndList[0].hdLine.clear();
for (auto item: hdl->desList)
{
bnd->bndList[0].hdLine.append(item);
}
}
// completely in between start finish
else
{
if (startBnd > endBnd)
{
int tmp = startBnd;
startBnd = endBnd;
endBnd = tmp;
}
hdl->desList.clear();
//first bnd segment
for (int i = 0; i < startBnd; i++)
{
hdl->desList.append(bnd->bndList[0].hdLine[i]);
}
//line segment
for (int i = startLine; i < endLine; i++)
{
hdl->desList.append(sliceArr[i]);
}
//final bnd segment
for (int i = endBnd; i < bnd->bndList[0].hdLine.count(); i++)
{
hdl->desList.append(bnd->bndList[0].hdLine[i]);
}
//build headline from desList
bnd->bndList[0].hdLine.clear();