Repository navigation
Expand file tree
/
Copy pathSubFunctions.cpp
More file actions
8276 lines (7081 loc) · 292 KB
/
Copy pathSubFunctions.cpp
File metadata and controls
8276 lines (7081 loc) · 292 KB
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
// SubFunctions.cpp
//
//
// Created by Myoung-Jong Noh on 3/30/20.
//
#include "SubFunctions.hpp"
#include "readtiff.hpp"
#include "Jsonread.hpp"
int numcols[7] = {6,5,5,5,4,3,0};
int orientation(Point p, Point q, Point r) {
double val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
if (val == 0) return 0; // Collinear
return (val > 0) ? 1 : 2; // 1 -> Clockwise, 2 -> Counterclockwise
}
int onSegment(Point p, Point q, Point r) {
return (q.x <= (r.x > p.x ? r.x : p.x) && q.x >= (r.x < p.x ? r.x : p.x) &&
q.y <= (r.y > p.y ? r.y : p.y) && q.y >= (r.y < p.y ? r.y : p.y));
}
int doIntersect(Point p1, Point p2, Point p3, Point p4, Point* intersection) {
// Find the four orientations needed for the general and special cases
int o1 = orientation(p1, p2, p3);
int o2 = orientation(p1, p2, p4);
int o3 = orientation(p3, p4, p1);
int o4 = orientation(p3, p4, p2);
// General case: if the two segments straddle each other, they intersect
if (o1 != o2 && o3 != o4)
{
// Find the intersection point (using line equations)
double a1 = p2.y - p1.y;
double b1 = p1.x - p2.x;
double c1 = a1 * p1.x + b1 * p1.y;
double a2 = p4.y - p3.y;
double b2 = p3.x - p4.x;
double c2 = a2 * p3.x + b2 * p3.y;
double det = a1 * b2 - a2 * b1;
if (det == 0) return 0; // Lines are parallel (no intersection)
intersection->x = (b2 * c1 - b1 * c2) / det;
intersection->y = (a1 * c2 - a2 * c1) / det;
intersection->x = ceil(intersection->x);
intersection->y = ceil(intersection->y);
return 1; // They intersect at the point
}
// Special cases: Check for collinearity and overlap
if (o1 == 0 && onSegment(p1, p3, p2)) return 1;
if (o2 == 0 && onSegment(p1, p4, p2)) return 1;
if (o3 == 0 && onSegment(p3, p1, p4)) return 1;
if (o4 == 0 && onSegment(p3, p2, p4)) return 1;
return 0; // No intersection
}
/*
int isInside(Point rect[], Point p) {
double x1 = rect[0].x, y1 = rect[0].y;
double x2 = rect[2].x, y2 = rect[2].y;
return (p.x >= x1 && p.x <= x2 && p.y >= y1 && p.y <= y2);
}
*/
// Function to check if a point is inside the polygon using Ray-Casting
int isInside(Point polygon[], int n, Point p) {
int count = 0, i, next;
// Loop through all edges of the polygon
for (i = 0; i < n; i++) {
next = (i + 1) % n; // Next vertex in the polygon (circular)
// Check if the point p is on the edge of the polygon
if ((p.y > polygon[i].y && p.y <= polygon[next].y) || (p.y > polygon[next].y && p.y <= polygon[i].y)) {
// Calculate the x-coordinate of the intersection point of the ray with the edge
double xIntersection = polygon[i].x + (p.y - polygon[i].y) * (polygon[next].x - polygon[i].x) / (polygon[next].y - polygon[i].y);
// If the intersection point is to the right of the point, increase the count
if (xIntersection > p.x) {
count++;
}
}
}
// If count is odd, the point is inside the polygon, otherwise outside
return count % 2 == 1;
}
double polygonArea(Point *polygon, int n) {
double area = 0;
for (int i = 0; i < n; i++) {
int next = (i + 1) % n;
area += polygon[i].x * polygon[next].y;
area -= polygon[i].y * polygon[next].x;
}
area = fabs(area) / 2.0;
return area;
}
Point findCentroid(Point *points, int n) {
Point centroid = {0, 0};
for (int i = 0; i < n; i++) {
centroid.x += points[i].x;
centroid.y += points[i].y;
}
centroid.x /= n;
centroid.y /= n;
//printf("Centroid: (%f, %f)\n", centroid.x, centroid.y);
return centroid;
}
double computeAngle(Point p, Point centroid) {
double angle = atan2(p.y - centroid.y, p.x - centroid.x)*180/PI;
if(angle < 0)
angle += 360;
return angle;
}
int compareAngles(const void *a, const void *b) {
Point *p1 = (Point *)a;
Point *p2 = (Point *)b;
// Find the centroid (we can compute it once for all points)
Point points[] = {*p1, *p2};
int n = sizeof(points) / sizeof(points[0]);
Point centroid = findCentroid(points, n); // Assume two points are enough for simplicity
//printf("centroid %f\t%f\n",centroid.x,centroid.y);
// Calculate angle of each point with respect to the centroid
double angle1 = atan2(p1->y - centroid.y, p1->x - centroid.x)*180/PI;
if(angle1 < 0)
angle1 += 360;
double angle2 = atan2(p2->y - centroid.y, p2->x - centroid.x)*180/PI;
if(angle2 < 0)
angle2 += 360;
if (angle1 < angle2) return -1;
if (angle1 > angle2) return 1;
return 0;
}
int isDuplicate(vector<Point> &arr, int size, Point p)
{
for (int i = 0; i < size; i++)
{
if (arr[i].x == p.x && arr[i].y == p.y)
{
return 1; // Point is a duplicate
}
}
return 0; // Point is not a duplicate
}
void Overlappoints(Point rect1[], Point rect2[], vector<Point> &intersectionPoints)
{
//vector<Point> intersectionPoints; // Maximum of 8 intersection points
// Loop through the edges of the two rectangles and check for intersections
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
Point intersection;
// Check if the edge (rect1[i], rect1[(i+1)%4]) intersects with (rect2[j], rect2[(j+1)%4])
if (doIntersect(rect1[i], rect1[(i+1)%4], rect2[j], rect2[(j+1)%4], &intersection))
{
// Store the intersection point
//intersectionPoints[numIntersectionPoints++] = intersection;
if (!isDuplicate(intersectionPoints, intersectionPoints.size(), intersection))
{
intersectionPoints.push_back(intersection);
}
//printf("intersection point %f\t%f\n",intersection.x,intersection.y);
}
}
}
for (int i = 0; i < 4; i++) {
if (isInside(rect2, 4, rect1[i])) {
if (!isDuplicate(intersectionPoints, intersectionPoints.size(), rect1[i]))
{
//intersectionPoints[numIntersectionPoints++] = rect1[i];
intersectionPoints.push_back(rect1[i]);
//printf("inside point %f\t%f\n",rect1[i].x,rect1[i].y);
}
}
}
for (int i = 0; i < 4; i++) {
if (isInside(rect1, 4, rect2[i])) {
if (!isDuplicate(intersectionPoints, intersectionPoints.size(), rect2[i]))
{
//intersectionPoints[numIntersectionPoints++] = rect2[i];
intersectionPoints.push_back(rect2[i]);
//printf("inside point %f\t%f\n",rect2[i].x,rect2[i].y);
}
}
}
/*
for (int i = 0; i < intersectionPoints.size(); i++)
{
printf("intersection point %f\t%f\n",intersectionPoints[i].x,intersectionPoints[i].y);
}
*/
}
// Function to sort points in clockwise order using the centroid
void sort_points_clockwise(Point points[], int n, Point centroid) {
// Calculate the angle of each point relative to the centroid
std::vector<double> angles(n);
for (int i = 0; i < n; i++) {
angles[i] = computeAngle(points[i], centroid);
}
// Perform a simple bubble sort based on the angles (clockwise order)
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (angles[j] < angles[j + 1]) {
// Swap points and their corresponding angles
Point temp_point = points[j];
points[j] = points[j + 1];
points[j + 1] = temp_point;
double temp_angle = angles[j];
angles[j] = angles[j + 1];
angles[j + 1] = temp_angle;
}
}
}
}
double findOverlapArea(Point rect1[], Point rect2[], Point refRect[], vector<D2DPOINT> &overpt) {
vector<Point> twoRec_intersectpoint;
vector<Point> ref_intersectpoint;
//printf("two recs intersection\n");
Overlappoints(rect1,rect2,twoRec_intersectpoint);
if(twoRec_intersectpoint.size() >= 3)
{
Point *temp1 = (Point*)calloc(sizeof(Point),twoRec_intersectpoint.size());
for(int i = 0 ; i < twoRec_intersectpoint.size() ; i++)
{
temp1[i].x = twoRec_intersectpoint[i].x;
temp1[i].y = twoRec_intersectpoint[i].y;
}
Point centroid = findCentroid(temp1, twoRec_intersectpoint.size());
sort_points_clockwise(temp1, twoRec_intersectpoint.size(), centroid);
//qsort(temp1, twoRec_intersectpoint.size(), sizeof(Point), compareAngles);
/*
for (int i = 0; i < twoRec_intersectpoint.size(); i++)
{
printf("two sort intersection point %f\t%f\n",temp1[i].x,temp1[i].y);
}
*/
double area1 = polygonArea(temp1, twoRec_intersectpoint.size());
//printf("area1 %f\n",area1);
if(area1 > 1000000)
{
//printf("ref recs intersection\n");
Overlappoints(temp1,refRect,ref_intersectpoint);
if (ref_intersectpoint.size() >= 3)
{
Point *temp2 = (Point*)calloc(sizeof(Point),ref_intersectpoint.size());
for(int i = 0 ; i < ref_intersectpoint.size() ; i++)
{
temp2[i].x = ref_intersectpoint[i].x;
temp2[i].y = ref_intersectpoint[i].y;
}
//qsort(temp2, ref_intersectpoint.size(), sizeof(Point), compareAngles);
Point centroid2 = findCentroid(temp2, ref_intersectpoint.size());
sort_points_clockwise(temp2, ref_intersectpoint.size(), centroid2);
for (int i = 0; i < ref_intersectpoint.size(); i++)
{
//printf("ref recs sort intersection point %f\t%f\n",temp2[i].x,temp2[i].y);
D2DPOINT t_pt(temp2[i].x,temp2[i].y);
overpt.push_back(t_pt);
}
// If there are enough intersection points (at least 3), calculate the area
if (ref_intersectpoint.size() >= 3) {
return polygonArea(temp2, ref_intersectpoint.size());
}
free(temp2);
}
}
free(temp1);
}
// If no valid intersection, return 0 (no overlap)
return 0;
}
double Weightparam_sigmaZ(ProInfo *proinfo, double sigmaZ, double ncc, double ortho_ncc)
{
if(sigmaZ < 5)
{
if(proinfo->check_MPmerge_sigmaZ == 0)
{
if(ncc > ortho_ncc)
return (ncc*100);
else
return (ortho_ncc*100);
}
else if(proinfo->check_MPmerge_sigmaZ == 1)
return sigmaZ;
else
return 100;
}
else //Planet Dove
{
if(ncc > 0 && ortho_ncc > 0)
{
if(ncc > ortho_ncc)
return (exp(1.0/sigmaZ) - 1.0)*1000 + ncc*100/2.0;
else
return (exp(1.0/sigmaZ) - 1.0)*1000 + ortho_ncc*100/2.0;
}
else
return (exp(1.0/sigmaZ) - 1.0)*1000;
}
}
double Weight_from_GC(ProInfo *proinfo, int pair_number, LevelInfo &levelinfo, double CA, double Plane_slop, bool* multimps_check_matched, float* multimps_peak_height, short* multimps_peak_roh, short* multimps_ortho_roh)
{
double W_global;
double sum_ncc = 0;
double sum_oncc = 0;
double sum_count = 0;
#pragma omp parallel for reduction(+:sum_ncc,sum_oncc,sum_count)
for(long int pt_index = 0 ; pt_index < (*levelinfo.Grid_length) ; pt_index++)
{
if(multimps_check_matched[pt_index + pair_number*(*levelinfo.Grid_length)])
{
sum_ncc += SignedCharToDouble_result(multimps_peak_roh[pt_index + pair_number*(*levelinfo.Grid_length)]);
sum_oncc += SignedCharToDouble_result(multimps_ortho_roh[pt_index + pair_number*(*levelinfo.Grid_length)]);
sum_count++;
}
}
const int reference_id = levelinfo.pairinfo->pairs(pair_number).m_X;
const int ti = levelinfo.pairinfo->pairs(pair_number).m_Y;
double ref_gsd = levelinfo.imageinfo[reference_id].GSD.pro_GSD;
double ti_gsd = levelinfo.imageinfo[ti].GSD.pro_GSD;
double dif_gsd = fabs(ref_gsd - ti_gsd)*100;
unsigned char dif_gsd_b;
if(dif_gsd < 255)
dif_gsd_b = (unsigned char)(dif_gsd);
else
dif_gsd_b = 254;
double w_res_d, w_aa_d, w_dist, w_azi; // range = 0 ~ 100
if(dif_gsd_b > 10)
{
w_res_d = (1.0/ pow(ceil(double(dif_gsd_b)/10),2.0) )*100;
}
else
w_res_d = 100;
//w_azi
w_azi = 1.0/pow(ceil(fabs(levelinfo.pairinfo->Azimuth(pair_number))),1.5) * 100;
//w_factor
double w_ca_dist; // range 0 ~ 100;
if(Plane_slop == 0.0)// || proinfo->Plane_slope_th > Plane_slop)
w_ca_dist = 100;//pow(CA,4)/100;
else
{
double dif_slope = Plane_slop;// - proinfo->Plane_slope_th;
double dem = -200*dif_slope*dif_slope + 1;
if(dem > 0)
w_ca_dist = dem*100;//pow(CA,4)*dem/100;
else
w_ca_dist = 0;
}
double w_capoint = pow(CA,4)/100;
//w_ca_dist = pow(CA,4)/100;
//w_sigma
double w_sigma = Weightparam_sigmaZ(proinfo, levelinfo.pairinfo->SigmaZ(pair_number), sum_ncc/sum_count, sum_oncc/sum_count);
double w_AE;
if(levelinfo.pairinfo->AE(pair_number) == 0)
w_AE = 100;
else
w_AE = 1.0/pow(ceil(levelinfo.pairinfo->AE(pair_number)/5.0),1.2)*100;
double w_AE_range, w_BIE_range;
if(levelinfo.pairinfo->Range_AA(pair_number) == 0)
w_AE_range = 1;
else
{
w_AE_range = pow(ceil(levelinfo.pairinfo->Range_AA(pair_number)/0.5),0.4);
if(w_AE_range > 2)
w_AE_range = 2;
}
if(levelinfo.pairinfo->Range_BIE(pair_number) == 0)
w_BIE_range = 1;
else
{
w_BIE_range = pow(ceil(levelinfo.pairinfo->Range_AA(pair_number)/0.2),0.3);
if(w_BIE_range > 2)
w_BIE_range = 2;
}
double w_Plane_angle = 1 + 99*(0.8*w_ca_dist + 10*(w_AE_range-1) + 10*(w_BIE_range - 1))/100;
if(w_Plane_angle > 100)
{
printf("out of PA %f\tvalue %f\t%f\t%f\n",w_Plane_angle,w_ca_dist,w_AE_range,w_BIE_range);
exit(1);
}
//double CA = levelinfo.pairinfo->ConvergenceAngle(pair_number);
double AE = ceil(levelinfo.pairinfo->AE(pair_number)/2.0);
double BIE = levelinfo.pairinfo->BIE(pair_number);
double val = -1.0*(0.8*(10.0-CA)+0.40*(90.0-BIE)+0.20*AE);
double weight = exp(val);
//return W_global = 0.2*w_azi + 0.2*w_ca_dist + 0.2*w_sigma + 0.4*w_capoint;
//return W_global = 0.1*w_azi + 0.7*w_ca_dist + 0.1*w_sigma + 0.1*w_capoint;
return W_global = 0.6*w_ca_dist + 0.1*w_sigma + 30*weight;
//return W_global = 0.05*w_azi + 0.4*w_Plane_angle + 0.05*w_sigma + 0.2*w_capoint + 0.3*w_AE;
}
double dot_product(double* a, double* b) {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
// Function to fit the plane using least squares method
void fit_plane(vector<Point> &points, double* plane_coeffs, double* R2, double *sigma, vector<double> &angle) {
int n = points.size();
GMA_double *A = GMA_double_create(n,3); // Matrix of size n x 4
GMA_double *L = GMA_double_create(n,1); // Vector of size n
// Construct the matrix A and vector L Z = Ax + By + c
for (int i = 0; i < n; i++) {
A->val[i][0] = points[i].x;
A->val[i][1] = points[i].y;
A->val[i][2] = 1.0;
L->val[i][0] = points[i].z;
//printf("%f\t%f\t%f\n",points[i].x,points[i].y,points[i].z);
}
GMA_double *AT = GMA_double_create(3,n);
GMA_double *ATA = GMA_double_create(3,3);
GMA_double *ATAI = GMA_double_create(3,3);
GMA_double *ATL = GMA_double_create(3,1);
GMA_double *X = GMA_double_create(3,1);
GMA_double_Tran(A,AT);
GMA_double_mul(AT,A,ATA);
GMA_double_inv(ATA,ATAI);
GMA_double_mul(AT,L,ATL);
GMA_double_mul(ATAI,ATL,X);
GMA_double *AX = GMA_double_create(n,1);
GMA_double *V = GMA_double_create(n,1);
GMA_double_mul(A,X,AX);
GMA_double_sub(AX,L,V);
double var_sum = 0;
double max_V = -1000;
for (int i = 0; i < n; i++) {
var_sum += (V->val[i][0])*(V->val[i][0]);
if(max_V < fabs(V->val[i][0]))
max_V = fabs(V->val[i][0]);
}
*sigma = sqrt(var_sum/(n - 3));
//printf("adjust sigma %f\t%f\n", sigma,max_V);
// Solve the system of equations A * plane_coeffs = b
//double plane_params[4]; // The plane coefficients A, B, C, D
//gaussian_elimination((double*)A, b, plane_params, 4);
// Copy the result to plane_coeffs
for (int i = 0; i < 3; i++) {
plane_coeffs[i] = X->val[i][0];
}
GMA_double_destroy(A);
GMA_double_destroy(L);
GMA_double_destroy(AT);
GMA_double_destroy(ATA);
GMA_double_destroy(ATAI);
GMA_double_destroy(ATL);
GMA_double_destroy(X);
GMA_double_destroy(AX);
GMA_double_destroy(V);
//plane_coeffs[0] = -0.003;
//plane_coeffs[1] = -0.002;
//plane_coeffs[2] = 4618.813;
// Calculate R^2 (Goodness of Fit)
double residual_sum = 0.0;
double total_sum = 0.0;
double mean_z = 0.0;
for (int i = 0; i < n; i++) {
mean_z += points[i].z;
}
mean_z /= n;
for (int i = 0; i < n; i++) {
//double z_pred = plane_coeffs[0] * points[i].x + plane_coeffs[1] * points[i].y + plane_coeffs[2] * points[i].z + plane_coeffs[3];
double z_pred = plane_coeffs[0] * points[i].x + plane_coeffs[1] * points[i].y + plane_coeffs[2];
double residual = points[i].z - z_pred;
residual_sum += residual * residual;
total_sum += (points[i].z - mean_z) * (points[i].z - mean_z);
}
//printf("residual_sum %f\ttotal_sum %f\n",residual_sum,total_sum);
*R2 = 1.0 - (residual_sum / total_sum);
compute_angle(plane_coeffs,angle);
}
// Function to compute the angle between the fitted plane and the horizontal plane
void compute_angle(double* plane_coeffs, vector<double> &angle)
{
// Normal vector of the fitted plane
//double normal[3] = {plane_coeffs[0], plane_coeffs[1], plane_coeffs[2]};
double normal[3] = {-plane_coeffs[0], -plane_coeffs[1], 1};
// Normal vector of the horizontal plane (z-axis)
double z_axis[3] = {0, 0, 1};
double x_axis[3] = {1, 0, 0};
double y_axis[3] = {0, 1, 0};
// Dot product and magnitudes
double dot = dot_product(normal, x_axis);
double normal_magnitude = sqrt(dot_product(normal, normal));
double z_axis_magnitude = 1.0;
double cos_theta = dot / (normal_magnitude * z_axis_magnitude);
angle.push_back(acos(cos_theta) * (180.0 / M_PI)); // in degrees
dot = dot_product(normal, y_axis);
normal_magnitude = sqrt(dot_product(normal, normal));
z_axis_magnitude = 1.0;
cos_theta = dot / (normal_magnitude * z_axis_magnitude);
angle.push_back(acos(cos_theta) * (180.0 / M_PI)); // in degrees
dot = dot_product(normal, z_axis);
normal_magnitude = sqrt(dot_product(normal, normal));
z_axis_magnitude = 1.0;
cos_theta = dot / (normal_magnitude * z_axis_magnitude);
angle.push_back(acos(cos_theta) * (180.0 / M_PI)); // in degrees
}
double statistic_dH_ref(int pair1, int pair2, GridPairs &Grid_pair, LevelInfo &levelinfo, float* multimps_peak_height, short* multimps_peak_roh, short* multimps_ortho_roh, vector<uint32> &save_ptindex, vector<unsigned char> &save_ptindex_flag, double &sel_count, double &avg, double &std, vector<uint32> &dh_cal_ptindex, vector<float> &dh_cal_height)
{
int level = (*levelinfo.Pyramid_step);
double dH_step = levelinfo.MdH_step;//*(level+1);
double dif_sum_weight = 0;
double sum_weight = 0;
int Hist[9] = {0};
double sum_roh = 0;
double sum_ortho = 0;
double sum_roh2 = 0;
double sum_ortho2 = 0;
for(int cnt = 0 ; cnt < save_ptindex.size() ; cnt++)
{
uint32 pt_index = save_ptindex[cnt];
sum_roh += SignedCharToDouble_result(multimps_peak_roh[pt_index + pair1*(*levelinfo.Grid_length)]);
sum_ortho += SignedCharToDouble_result(multimps_ortho_roh[pt_index + pair1*(*levelinfo.Grid_length)]);
sum_roh2 += SignedCharToDouble_result(multimps_peak_roh[pt_index + pair2*(*levelinfo.Grid_length)]);
sum_ortho2 += SignedCharToDouble_result(multimps_ortho_roh[pt_index + pair2*(*levelinfo.Grid_length)]);
}
double Roh_th = sum_roh/double(save_ptindex.size()) - 0.1;
if(Roh_th < levelinfo.Roh_th)
Roh_th = levelinfo.Roh_th;
double Ortho_th = sum_ortho/double(save_ptindex.size()) - 0.1;
if(Ortho_th < levelinfo.Ortho_th)
Ortho_th = levelinfo.Ortho_th;
double Roh_th2 = sum_roh2/double(save_ptindex.size()) - 0.1;
if(Roh_th2 < levelinfo.Roh_th)
Roh_th2 = levelinfo.Roh_th;
double Ortho_th2 = sum_ortho2/double(save_ptindex.size()) - 0.1;
if(Ortho_th2 < levelinfo.Ortho_th)
Ortho_th2 = levelinfo.Ortho_th;
double temp_count = 0 ;
for(int cnt = 0 ; cnt < save_ptindex.size() ; cnt++)
{
uint32 pt_index = save_ptindex[cnt];
if(SignedCharToDouble_result(multimps_peak_roh[pt_index + pair1*(*levelinfo.Grid_length)]) > Roh_th && SignedCharToDouble_result(multimps_ortho_roh[pt_index + pair1*(*levelinfo.Grid_length)]) > Ortho_th &&
SignedCharToDouble_result(multimps_peak_roh[pt_index + pair2*(*levelinfo.Grid_length)]) > Roh_th2 && SignedCharToDouble_result(multimps_ortho_roh[pt_index + pair2*(*levelinfo.Grid_length)]) > Ortho_th2)
{
float dh = multimps_peak_height[pt_index + pair1*(*levelinfo.Grid_length)] - (multimps_peak_height[pt_index + pair2*(*levelinfo.Grid_length)] - levelinfo.pairinfo->Tz(pair2));
if(dh > - dH_step/2.0 && dh < dH_step/2.0)
Hist[4] ++;
else if(dh > - dH_step/2.0 - dH_step && dh <= - dH_step/2.0)
Hist[3] ++;
else if(dh > - dH_step/2.0 - 2*dH_step && dh <= - dH_step/2.0 - dH_step )
Hist[2] ++;
else if(dh > - dH_step/2.0 - 3*dH_step && dh <= - dH_step/2.0 - 2*dH_step )
Hist[1] ++;
else if(dh <= - dH_step/2.0 - 3*dH_step )
Hist[0] ++;
else if(dh >= dH_step/2.0 && dh <= dH_step/2.0 + dH_step)
Hist[5] ++;
else if(dh >= dH_step/2.0 + dH_step && dh <= dH_step/2.0 + 2*dH_step)
Hist[6] ++;
else if(dh >= dH_step/2.0 + 2*dH_step && dh <= dH_step/2.0 + 3*dH_step)
Hist[7] ++;
else if(dh >= dH_step/2.0 + 3*dH_step)
Hist[8] ++;
temp_count++;
}
}
/*
for(int i = 0 ; i < 8 ; i++)
{
printf("hist %d\n",Hist[i]);
}
*/
int max_hist = 0;
int max_pos = 0;
for(int i = levelinfo.Hist_min ; i < levelinfo.Hist_max ; i++)
{
if(Hist[i] > max_hist)
{
max_hist = Hist[i];
max_pos = i;
}
}
double max_hist_ratio = double(max_hist)/temp_count;
//printf("max_hist %d\tmax_pos %d\n",max_hist, max_pos);
double out_ratio = double(Hist[0] + /*Hist[1] + Hist[7] +*/ Hist[8])/temp_count;
if((double)max_hist/temp_count > levelinfo.COUNT_TH && temp_count > 0)
{
float min_dh, max_dh;
if(max_pos == 4)
{
min_dh = - dH_step/2.0;
max_dh = dH_step/2.0;
}
else if(max_pos == 3)
{
min_dh = - dH_step/2.0 - dH_step ;
max_dh = - dH_step/2.0;
}
else if(max_pos == 2)
{
min_dh = - dH_step/2.0 - 2*dH_step ;
max_dh = - dH_step/2.0 - dH_step;
}
else if(max_pos == 1)
{
min_dh = - dH_step/2.0 - 3*dH_step ;
max_dh = - dH_step/2.0 - 2*dH_step;
}
else if(max_pos == 5)
{
min_dh = dH_step/2.0 ;
max_dh = dH_step/2.0 + dH_step;
}
else if(max_pos == 6)
{
min_dh = dH_step/2.0 + dH_step ;
max_dh = dH_step/2.0 + 2*dH_step;
}
else if(max_pos == 7)
{
min_dh = dH_step/2.0 + 2*dH_step ;
max_dh = dH_step/2.0 + 3*dH_step;
}
/*
if(max_hist_ratio > 0.5)
{
min_dh = - dH_step/2.0 - 3*dH_step;
max_dh = dH_step/2.0 + 3*dH_step;
}
*/
double th_H;
vector<float> selected_dh_ptindex(save_ptindex.size(),-1);
//#pragma omp parallel for reduction(+:dif_sum_weight,sum_weight)
for(int cnt = 0 ; cnt < save_ptindex.size() ; cnt++)
{
uint32 pt_index = save_ptindex[cnt];
if(SignedCharToDouble_result(multimps_peak_roh[pt_index + pair1*(*levelinfo.Grid_length)]) > Roh_th && SignedCharToDouble_result(multimps_ortho_roh[pt_index + pair1*(*levelinfo.Grid_length)]) > Ortho_th &&
SignedCharToDouble_result(multimps_peak_roh[pt_index + pair2*(*levelinfo.Grid_length)]) > Roh_th2 && SignedCharToDouble_result(multimps_ortho_roh[pt_index + pair2*(*levelinfo.Grid_length)]) > Ortho_th2)
{
float dH = multimps_peak_height[pt_index + pair1*(*levelinfo.Grid_length)] - (multimps_peak_height[pt_index + pair2*(*levelinfo.Grid_length)] - levelinfo.pairinfo->Tz(pair2));
//double height_step = UnsignedShortToDouble(levelinfo.pairinfo->pairHeightStep(pair1)) > UnsignedShortToDouble(levelinfo.pairinfo->pairHeightStep(pair2)) ? UnsignedShortToDouble(levelinfo.pairinfo->pairHeightStep(pair1)) : UnsignedShortToDouble(levelinfo.pairinfo->pairHeightStep(pair2));
//th_H = height_step*dH_step;
//if(th_H < TH_height)
// th_H = TH_height;
bool check_dh;
check_dh = dH >= min_dh && dH <= max_dh;
if(check_dh)
{
dh_cal_ptindex.push_back(pt_index);
dh_cal_height.push_back(dH);
save_ptindex_flag[pt_index] = 1;
dif_sum_weight += dH;//*save_difheight_roh[t_pair][cnt];
sum_weight += 1.0;//save_difheight_roh[t_pair][cnt];
selected_dh_ptindex[cnt] = pt_index;
//printf("difH %f\t%f\n",save_difheight[t_pair][cnt],dif_sum);
}
/*
check_dh = dH <= min_dh - min_dh/2.0 || dH >= max_dh + max_dh/2.0;
if(check_dh)
{
save_ptindex_flag[pt_index] = 2;
}*/
}
//else
// save_ptindex_flag[pt_index] = 9;
}
//printf("count %f\n",sum_weight);
//exit(1);
avg = dif_sum_weight/(double)sum_weight;
double sum_var = 0;
#pragma omp parallel for reduction(+:sum_var)
for(int cnt = 0 ; cnt < save_ptindex.size() ; cnt++)
{
if(selected_dh_ptindex[cnt] > -1)
{
uint32 pt_index = selected_dh_ptindex[cnt];
float dH = multimps_peak_height[pt_index + pair1*(*levelinfo.Grid_length)] - (multimps_peak_height[pt_index + pair2*(*levelinfo.Grid_length)] - levelinfo.pairinfo->Tz(pair2));
sum_var += (dH - avg)*(dH - avg);
}
}
std = sqrt(sum_var/(double)sum_weight);
sel_count = sum_weight;
}
else
sel_count = 0;
return out_ratio;
}
double statistic_dH_ref_seed(int pair2, GridPairs &Grid_pair, LevelInfo &levelinfo, UGRID &Grid, float* multimps_peak_height, short* multimps_peak_roh, short* multimps_ortho_roh, vector<uint32> &save_ptindex, vector<unsigned char> &save_ptindex_flag, double &sel_count, double &avg, double &std, vector<uint32> &dh_cal_ptindex, vector<float> &dh_cal_height)
{
int level = (*levelinfo.Pyramid_step);
double dH_step = levelinfo.MdH_step;//*(level+1);
double dif_sum_weight = 0;
double sum_weight = 0;
int Hist[9] = {0};
double sum_roh = 0;
double sum_ortho = 0;
for(int cnt = 0 ; cnt < save_ptindex.size() ; cnt++)
{
uint32 pt_index = save_ptindex[cnt];
sum_roh += SignedCharToDouble_result(multimps_peak_roh[pt_index + pair2*(*levelinfo.Grid_length)]);
sum_ortho += SignedCharToDouble_result(multimps_ortho_roh[pt_index + pair2*(*levelinfo.Grid_length)]);
}
double Roh_th = sum_roh/double(save_ptindex.size()) - 0.1;
if(Roh_th < levelinfo.Roh_th)
Roh_th = levelinfo.Roh_th;
double Ortho_th = sum_ortho/double(save_ptindex.size()) - 0.1;
if(Ortho_th < levelinfo.Ortho_th)
Ortho_th = levelinfo.Ortho_th;
double temp_count = 0;
for(int cnt = 0 ; cnt < save_ptindex.size() ; cnt++)
{
uint32 pt_index = save_ptindex[cnt];
if(SignedCharToDouble_result(multimps_peak_roh[pt_index + pair2*(*levelinfo.Grid_length)]) > Roh_th && SignedCharToDouble_result(multimps_ortho_roh[pt_index + pair2*(*levelinfo.Grid_length)]) > Ortho_th)
{
float dh = Grid.seed_Height(pt_index) - (multimps_peak_height[pt_index + pair2*(*levelinfo.Grid_length)] - levelinfo.pairinfo->Tz(pair2));
if(dh > - dH_step/2.0 && dh < dH_step/2.0)
Hist[4] ++;
else if(dh > - dH_step/2.0 - dH_step && dh <= - dH_step/2.0)
Hist[3] ++;
else if(dh > - dH_step/2.0 - 2*dH_step && dh <= - dH_step/2.0 - dH_step )
Hist[2] ++;
else if(dh > - dH_step/2.0 - 3*dH_step && dh <= - dH_step/2.0 - 2*dH_step )
Hist[1] ++;
else if(dh <= - dH_step/2.0 - 3*dH_step )
Hist[0] ++;
else if(dh >= dH_step/2.0 && dh <= dH_step/2.0 + dH_step)
Hist[5] ++;
else if(dh >= dH_step/2.0 + dH_step && dh <= dH_step/2.0 + 2*dH_step)
Hist[6] ++;
else if(dh >= dH_step/2.0 + 2*dH_step && dh <= dH_step/2.0 + 3*dH_step)
Hist[7] ++;
else if(dh >= dH_step/2.0 + 3*dH_step)
Hist[8] ++;
temp_count++;
}
}
/*
for(int i = 0 ; i < 8 ; i++)
{
printf("hist %d\n",Hist[i]);
}
*/
int max_hist = 0;
int max_pos = 0;
for(int i = levelinfo.Hist_min ; i < levelinfo.Hist_max ; i++)
{
if(Hist[i] > max_hist)
{
max_hist = Hist[i];
max_pos = i;
}
}
double max_hist_ratio = double(max_hist)/temp_count;
//printf("max_hist %d\tmax_pos %d\n",max_hist, max_pos);
double out_ratio = double(Hist[0] + /*Hist[1] + Hist[7] +*/ Hist[8])/temp_count;
if((double)max_hist/temp_count > levelinfo.COUNT_TH && temp_count > 0)
{
//printf("max_pos %d\tratio %f\t%f\t%f\n",max_pos,(double)max_hist/temp_count,levelinfo.COUNT_TH,out_ratio);
float min_dh, max_dh;
if(max_pos == 4)
{
min_dh = - dH_step/2.0;
max_dh = dH_step/2.0;
}
else if(max_pos == 3)
{
min_dh = - dH_step/2.0 - dH_step ;
max_dh = - dH_step/2.0;
}
else if(max_pos == 2)
{
min_dh = - dH_step/2.0 - 2*dH_step ;
max_dh = - dH_step/2.0 - dH_step;
}
else if(max_pos == 1)
{
min_dh = - dH_step/2.0 - 3*dH_step ;
max_dh = - dH_step/2.0 - 2*dH_step;
}
else if(max_pos == 5)
{
min_dh = dH_step/2.0 ;
max_dh = dH_step/2.0 + dH_step;
}
else if(max_pos == 6)
{
min_dh = dH_step/2.0 + dH_step ;
max_dh = dH_step/2.0 + 2*dH_step;
}
else if(max_pos == 7)
{
min_dh = dH_step/2.0 + 2*dH_step ;
max_dh = dH_step/2.0 + 3*dH_step;
}
/*
if(max_hist_ratio > 0.5)
{
min_dh = - dH_step/2.0 - 3*dH_step;
max_dh = dH_step/2.0 + 3*dH_step;
}
*/
double th_H;
vector<float> selected_dh_ptindex(save_ptindex.size(),-1);
//#pragma omp parallel for reduction(+:dif_sum_weight,sum_weight)
for(int cnt = 0 ; cnt < save_ptindex.size() ; cnt++)
{
uint32 pt_index = save_ptindex[cnt];
if(SignedCharToDouble_result(multimps_peak_roh[pt_index + pair2*(*levelinfo.Grid_length)]) > Roh_th && SignedCharToDouble_result(multimps_ortho_roh[pt_index + pair2*(*levelinfo.Grid_length)]) > Ortho_th)
{
float dH = Grid.seed_Height(pt_index) - (multimps_peak_height[pt_index + pair2*(*levelinfo.Grid_length)] - levelinfo.pairinfo->Tz(pair2));
//double height_step = UnsignedShortToDouble(levelinfo.pairinfo->pairHeightStep(pair2));
//th_H = height_step*dH_step;
//if(th_H < TH_height)
// th_H = TH_height;
bool check_dh;
check_dh = dH >= min_dh && dH <= max_dh;
if(check_dh)
{
dh_cal_ptindex.push_back(pt_index);
dh_cal_height.push_back(dH);
save_ptindex_flag[pt_index] = 1;
dif_sum_weight += dH;//*save_difheight_roh[t_pair][cnt];
sum_weight += 1.0;//save_difheight_roh[t_pair][cnt];
selected_dh_ptindex[cnt] = pt_index;
//printf("difH %f\t%f\n",save_difheight[t_pair][cnt],dif_sum);
}
/*
check_dh = dH <= min_dh - min_dh/2.0 || dH >= max_dh + max_dh/2.0;
if(check_dh)
{
save_ptindex_flag[pt_index] = 2;
}*/
}
//else
// save_ptindex_flag[pt_index] = 9;
}
avg = dif_sum_weight/(double)sum_weight;
double sum_var = 0;
#pragma omp parallel for reduction(+:sum_var)
for(int cnt = 0 ; cnt < save_ptindex.size() ; cnt++)
{
if(selected_dh_ptindex[cnt] > -1)
{
uint32 pt_index = selected_dh_ptindex[cnt];
float dH = Grid.seed_Height(pt_index) - (multimps_peak_height[pt_index + pair2*(*levelinfo.Grid_length)] - levelinfo.pairinfo->Tz(pair2));
sum_var += (dH - avg)*(dH - avg);
}
}
std = sqrt(sum_var/(double)sum_weight);
sel_count = sum_weight;
}
else
sel_count = 0;
/*
if(out_ratio > 1.0)
{
printf("out_ratio %f\ttemp_count %f\n",out_ratio,temp_count);
exit(1);
}*/
return out_ratio;
}
double statistic_dH(int pair1, int pair2, int pair_pos, GridPairs &Grid_pair, LevelInfo &levelinfo, float* multimps_peak_height, short* multimps_peak_roh, short* multimps_ortho_roh, uint32 count_ptindex, uint32** save_ptindex, vector<unsigned char> &save_ptindex_flag1, vector<unsigned char> &save_ptindex_flag2, double &sel_count, double &avg, double &std, bool pair1_flag, bool pair2_flag, vector<uint32> &dh_cal_ptindex, vector<float> &dh_cal_height)
{
int level = (*levelinfo.Pyramid_step);
double dH_step = levelinfo.MdH_step;//*(level+1);
double dif_sum_weight = 0;
double sum_weight = 0;
int Hist[9] = {0};
double sum_roh = 0;
double sum_ortho = 0;
double sum_roh2 = 0;
double sum_ortho2 = 0;
for(int cnt = 0 ; cnt < count_ptindex ; cnt++)
{
uint32 pt_index = save_ptindex[pair_pos][cnt];
sum_roh += SignedCharToDouble_result(multimps_peak_roh[pt_index + pair1*(*levelinfo.Grid_length)]);
sum_ortho += SignedCharToDouble_result(multimps_ortho_roh[pt_index + pair1*(*levelinfo.Grid_length)]);
sum_roh2 += SignedCharToDouble_result(multimps_peak_roh[pt_index + pair2*(*levelinfo.Grid_length)]);
sum_ortho2 += SignedCharToDouble_result(multimps_ortho_roh[pt_index + pair2*(*levelinfo.Grid_length)]);
}
double Roh_th = sum_roh/double(count_ptindex) - 0.1;
if(Roh_th < levelinfo.Roh_th)
Roh_th = levelinfo.Roh_th;
double Ortho_th = sum_ortho/double(count_ptindex) - 0.1;
if(Ortho_th < levelinfo.Ortho_th)
Ortho_th = levelinfo.Ortho_th;