% Areas of interest:
% AOI 1 origin: 0.0, 0.0 width: 2.480 height 3.799
% AOI 2 origin: 7.130, 3.799 width: 3.299 height 3.299
% AOI 3 origin: 2.480, 3.799 width: 4.644 height 3.849
% AOI 4 origin: 0.0, 3.799 width: 2.480 height 3.700
% AOI 5 origin: 1.100, -2.980 width: 9.579 height 2.680
% AOI 11 origin: 0.949, 2.349 width: 0.899 height 0.800
% AOI 21 origin: 7.130, 5.519 width: 1.019 height 1.580
% AOI 31 origin: 3.579, 3.799 width: 2.299 height 1.600
% AOI 41 origin: 0.0, 6.130 width: 1.149 height 1.370
% AOI 6 origin: 7.829, -0.5 width: 1.669 height 1.0

AoI{1} = [0,0; 0, 3.80; 2.48, 3.80; 2.48, 0];
AoI{11} = [0.95,2.35; 0.95, 2.35+0.8;0.95+0.9,2.35+0.8; 0.95+0.9, 2.35];
AoI{2} = [7.13,3.8; 7.13, 3.80+3.3;7.13+3.3, 3.80+3.3; 7.13+3.3, 3.8] ;
AoI{21} = [7.13,5.52; 7.13, 5.52+1.58;7.13+1.02,5.52+1.58; 7.13+1.02, 5.52];
AoI{3} = [2.48,3.80; 2.48, 3.80+3.85;2.48+4.65, 3.80+3.85; 2.48+4.65, 3.80];
AoI{31} = [3.58,3.8; 3.58, 3.8+1.6;3.58+2.3,3.8+1.6; 3.58+2.3, 3.8];
AoI{4} = [0,3.80; 0, 3.80+3.7;0+2.48, 3.80+3.7; 2.48, 3.80];
AoI{41} = [0,6.13; 0, 6.13+1.37;0+1.15,6.13+1.37; 0+1.15, 6.13];
AoI{5} = [1.1,-2.98; 1.1, -2.98+2.68;1.1+9.58,-2.98+2.68; 1.1+9.58, -2.98];
AoI{6} = [7.83,-0.5; 7.83, -0.5+1;7.83+1.7,-0.5+1; 7.83+1.7, -0.5];

refpath = {
[					# path 1
10.340	1.149
10.131	1.287
9.713	1.562
9.296	1.837
8.878	2.112
8.464	2.385
8.075	2.654
7.756	3.001
7.490	3.424
7.223	3.847
6.957	4.270
6.702	4.676
6.454	5.029
6.136	5.348
5.750	5.665
5.363	5.982
4.976	6.299
4.611	6.599
4.240	6.679
3.812	6.517
3.356	6.311
2.919	6.114
2.414	5.920
1.875	5.766
1.400	5.661
0.989	5.560
0.810	5.510
0.810	5.510
0.810	5.510
0.810	5.510
0.810	5.510
0.810	5.510
0.830	5.260
0.870	4.785
1.055	4.639
1.220	4.719
1.220	4.719
1.220	4.719
1.220	4.719
1.220	4.719
1.220	4.719
1.407	4.885
1.782	5.216
2.149	5.540
2.625	5.634
3.150	5.474
3.612	5.282
4.056	5.098
4.269	5.010
4.269	5.010
4.269	5.010
4.269	5.010
4.269	5.010
4.269	5.010
4.269	5.010
], [					# path 2
9.789	6.150
9.822	6.397
9.887	6.887
10.045	7.269
10.155	7.160
10.127	6.661
10.099	6.161
10.071	5.662
10.043	5.171
9.989	4.694
9.724	4.351
9.274	4.133
8.824	3.915
8.599	3.806
8.599	3.806
8.599	3.806
8.599	3.806
8.599	3.806
8.599	3.806
8.374	3.697
7.934	3.484
7.564	3.245
7.233	2.932
6.881	2.578
6.528	2.223
6.176	1.869
5.839	1.530
5.519	1.25
5.132	1.025
4.678	0.817
4.223	0.608
3.773	0.402
3.304	0.245
2.811	0.217
2.314	0.271
1.827	0.324
1.390	0.364
1.190	0.629
1.190	1.129
1.190	1.379
1.190	1.379
1.190	1.379
1.190	1.379
1.190	1.379
1.190	1.379
1.190	1.629
1.190	2.129
1.190	2.629
1.190	2.879
1.190	2.879
1.190	2.879
1.190	2.879
1.190	2.879
1.190	2.879
1.405	2.859
1.869	2.832
2.354	2.817
2.829	2.875
3.074	3.189
3.082	3.689
3.090	4.189
3.098	4.689
3.106	5.174
2.884	5.519
2.443	5.755
2.011	6.007
1.579	6.258
1.147	6.510
0.730	6.753
0.529	6.869
0.529	6.869
0.529	6.869
0.529	6.869
0.529	6.869
0.529	6.869
0.689	6.664
0.814	6.212
0.744	5.722
0.849	5.234
1.204	5.117
1.634	5.373
2.049	5.620
2.549	5.709
3.092	5.620
3.578	5.500
4.063	5.380
4.548	5.260
5.015	5.145
5.239	5.090
5.239	5.090
5.239	5.090
5.239	5.090
5.239	5.090
5.239	5.090
5.239	5.090
], [					# path 3
9.920	0.910
9.676	0.967
9.190	1.083
8.703	1.198
8.217	1.314
7.747	1.426
7.305	1.295
6.887	0.963
6.492	0.678
6.465	0.515
6.876	0.447
7.369	0.363
7.862	0.278
8.354	0.194
8.840	0.111
9.079	0.070
9.079	0.070
9.079	0.070
9.079	0.070
9.079	0.070
9.079	0.070
9.199	-0.210
9.177	-0.695
8.892	-1.106
8.607	-1.517
8.323	-1.928
8.050	-2.321
7.725	-2.539
7.281	-2.539
6.785	-2.478
6.289	-2.418
5.792	-2.357
5.312	-2.298
4.815	-2.365
4.550	-2.460
4.550	-2.460
4.550	-2.460
4.550	-2.460
4.550	-2.460
4.550	-2.460
4.399	-2.260
4.098	-1.861
3.803	-1.470
3.955	-1.089
4.498	-0.922
4.996	-0.967
5.494	-1.012
5.992	-1.057
6.465	-1.099
6.954	-1.060
7.442	-0.886
7.888	-0.660
8.285	-0.458
8.485	-0.060
8.314	0.405
7.922	0.716
7.531	1.027
7.140	1.338
6.752	1.647
6.309	1.884
5.818	2.032
5.334	2.158
4.850	2.284
4.608	2.347
4.608	2.347
4.608	2.347
4.608	2.347
4.608	2.347
4.608	2.347
4.366	2.410
3.882	2.536
3.415	2.599
2.940	2.620
2.460	2.660
2.230	2.680
2.230	2.680
2.230	2.680
2.230	2.680
2.230	2.680
2.230	2.680
2.230	2.680
], [					# path 4 (AoI)
2.230	2.680
2.230	2.680
2.230	2.680
2.230	2.680
2.230	2.680
2.230	2.680
2.479	2.659
2.959	2.619
3.415	2.599
3.881	2.537
4.365	2.411
4.607	2.348
4.607	2.348
4.607	2.348
4.607	2.348
4.607	2.348
4.607	2.348
4.849	2.285
5.333	2.159
5.817	2.033
6.309	1.885
6.755	1.644
7.147	1.333
7.538	1.022
7.929	0.711
8.317	0.402
8.485	-0.060
8.237	-0.483
7.791	-0.709
7.394	-0.911
6.954	-1.060
6.441	-1.097
5.943	-1.052
5.445	-1.007
4.947	-0.962
4.474	-0.920
3.955	-1.089
3.810	-1.479
4.111	-1.878
4.406	-2.269
4.550	-2.460
4.550	-2.460
4.550	-2.460
4.550	-2.460
4.550	-2.460
4.550	-2.460
4.815	-2.365
5.328	-2.300
5.824	-2.361
6.320	-2.421
6.817	-2.482
7.297	-2.541
7.725	-2.539
8.062	-2.304
8.347	-1.893
8.632	-1.482
8.916	-1.071
9.189	-0.678
9.199	-0.210
9.079	0.070
9.079	0.070
9.079	0.070
9.079	0.070
9.079	0.070
9.079	0.070
8.833	0.112
8.340	0.196
7.847	0.281
7.355	0.365
6.869	0.448
6.465	0.515
6.502	0.686
6.897	0.971
7.305	1.295
7.763	1.422
8.249	1.306
8.736	1.191
9.222	1.075
9.692	0.963
10.130	1.030
10.131	1.287
9.713	1.562
9.296	1.837
8.878	2.112
8.464	2.385
8.075	2.654
7.756	3.001
7.490	3.424
7.223	3.847
6.957	4.270
6.702	4.676
6.454	5.029
6.136	5.348
5.750	5.665
5.363	5.982
4.976	6.299
4.611	6.599
4.240	6.679
3.812	6.517
3.356	6.311
2.919	6.114
2.414	5.920
1.875	5.766
1.400	5.661
0.989	5.560
0.810	5.510
0.810	5.510
0.810	5.510
0.810	5.510
0.810	5.510
0.810	5.510
0.830	5.260
0.870	4.785
1.055	4.639
1.220	4.719
1.220	4.719
1.220	4.719
1.220	4.719
1.220	4.719
1.220	4.719
1.407	4.885
1.782	5.216
2.149	5.540
2.625	5.634
3.150	5.474
3.612	5.282
4.056	5.098
4.269	5.010
4.269	5.010
4.269	5.010
4.269	5.010
4.269	5.010
4.269	5.010
4.754	5.050
5.239	5.090
5.239	5.090
5.239	5.090
5.239	5.090
5.239	5.090
5.239	5.090
4.997	5.149
4.511	5.269
4.026	5.389
3.541	5.509
3.074	5.624
2.549	5.709
2.035	5.612
1.605	5.356
1.190	5.109
0.849	5.234
0.745	5.727
0.815	6.217
0.689	6.664
0.529	6.869
0.529	6.869
0.529	6.869
0.529	6.869
0.529	6.869
0.529	6.869
0.746	6.744
1.178	6.492
1.610	6.241
2.042	5.989
2.459	5.746
2.884	5.519
3.105	5.160
3.097	4.660
3.089	4.160
3.081	3.660
3.073	3.175
2.829	2.875
2.340	2.817
1.855	2.832
1.405	2.870
1.190	2.900
1.190	2.900
1.190	2.900
1.190	2.900
1.190	2.900
1.190	2.900
1.190	2.650
1.190	2.150
1.190	1.650
1.190	1.400
1.190	1.400
1.190	1.400
1.190	1.400
1.190	1.400
1.190	1.400
1.190	1.150
1.190	0.650
1.390	0.375
1.838	0.322
2.335	0.268
2.822	0.215
3.304	0.245
3.777	0.404
4.231	0.612
4.686	0.821
5.136	1.027
5.519	1.25
5.856	1.547
6.208	1.901
6.561	2.256
6.913	2.610
7.250	2.949
7.564	3.245
7.945	3.488
8.395	3.706
8.620	3.815
8.620	3.815
8.620	3.815
8.620	3.815
8.620	3.815
8.620	3.815
8.845	3.924
9.295	4.142
9.735	4.355
9.989	4.694
10.044	5.179
10.072	5.678
10.100	6.178
10.128	6.677
10.156	7.168
10.045	7.269
9.887	6.882
9.822	6.392
9.789	6.150
]
};

l = [0 10.43 0 7.65];		# minX, maxX, minY, maxY
scorefuncx = ([0,.5,2,4,Inf]);		# ordinates of score function
scorefuncy = ([10,10,4,0,0]);		# abscissae of score function
N = 10000;				# number of repetitions


path = cell (size(refpath));

## For Matlab, need bsxfun where Octave broadcasting is used
function aoi = aoifromp (p, AoI)
  persistent aoill aoitr
  if (isempty(aoill))
    aoi = [AoI{1:6}]([1 3],:)';		# keep the lower-left and the top-right corners
    aoi = [aoi(1:2:end,:) aoi(2:2:end,:)];
    aoill = aoi(:,[1 3]);	 # lower-left corner coordinates of AoIs
    aoitr = aoi(:,[2 4]);	 # top-right corner coordinates of AoIs
  endif
  ws = "Octave:broadcast";
  oldw = warning("query",ws);		# query state of broadcast warnings
  #warning("off",ws);			# disbale broadcast warnings
  p = permute(p,[1 3 2]);		# permute X Y positions
  in = cat(3,p>=permute(aoill,[3 1 2]),	# broadcast- point greater than ll corner
	   p<=permute(aoitr,[3 1 2])); 	# broadcast- point less than tr corner
  in = all(in,3);	  # one true val in a row means we are in an AoI
  aoi = sum(in.*[1:6],2); # broadcast- AoI No.: areas do not overlap apart from 5,6 which give 11
  #warning(oldw,ws);	  # restore broadcast warnings
endfunction

for pi = 1:4		      # make a path by interpolating the refpath
  p = refpath{pi};
  len = length(p);
  rpath{pi} = interp1(0:len-1,p,0:0.5:len-1);
endfor
raoi = aoifromp(rpath{4},AoI);
size(raoi)

for pi = 1:4
  p = rpath{pi};
  len = rows(p);
  rp = [unifrnd(l(1),l(2),[N*len 1]), ... # random X coordinates
	unifrnd(l(3),l(4),[N*len 1])];	  # random Y coordinates
  if (pi == 4)				  # AoI
    aoi = aoifromp(rp,AoI);
    raoi = repmat(raoi,N,1);		# repeat the same path N times
    err = (aoi == raoi);		# correct AoI detections
    err = reshape(err,len,N);		  # one column per N repetitions
    score = 10 * mean(err);		  # N scores
    printf("AoI: ");
  else					  # paths
    p = repmat(p,N,1);			  # repeat the same path N times
    err = sqrt(sumsq(p-rp,2));		  # errors (Euclidean distances)
    err = reshape(err,len,N);		  # one column per N repetitions
    ## In Matlab, one should use this instead:
    ## function q=thirdquartile(x) x=sort(x);q=x(floor(rows(x)*3/4)+1,:);end
    tqerr = quantile(err,3/4,1,1);	# third quartile of error: use method 1
    score = interp1(scorefuncx,scorefuncy,tqerr); # N scores
    printf("Path N.%d: mean third quartile is %3.1f, ",pi,mean(tqerr));
  endif
  printf("average score %3.1f, score standard dev %3.1f\n",mean(score),std(score));
endfor
