forked from githubdoe/DFTFringe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcirclefit.cpp
833 lines (624 loc) · 22.9 KB
/
circlefit.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
#include "circleutils.h"
#include "circle.h"
/************************************************************************
BODY OF THE MEMBER ROUTINES
************************************************************************/
// Default constructor
Circle::Circle()
{
a=0.; b=0.; r=1.; s=0.; i=0; j=0;
}
// Constructor with assignment of the circle parameters only
Circle::Circle(double aa, double bb, double rr)
{
a=aa; b=bb; r=rr;
}
// Printing routine
void Circle::print(void)
{
std::cout << std::endl;
std::cout << std::setprecision(10) << "center (" <<a <<","<< b <<") radius "
<< r << " sigma " << s << " gradient " << g << " iter "<< i << " inner " << j << std::endl;
}
//****************** Sigma ************************************
//
// estimate of Sigma = square root of RSS divided by N
// gives the root-mean-square error of the geometric circle fit
//
// data.h
//
/************************************************************************
DECLARATION OF THE CLASS DATA
************************************************************************/
// Class for Data
// A data has 5 fields:
// n (of type int), the number of data points
// X and Y (arrays of type double), arrays of x- and y-coordinates
// meanX and meanY (of type double), coordinates of the centroid (x and y sample means)
/************************************************************************
BODY OF THE MEMBER ROUTINES
************************************************************************/
// Default constructor
CircleData::CircleData()
{
n=0;
X = new double[n];
Y = new double[n];
for (int i=0; i<n; i++)
{
X[i]=0.;
Y[i]=0.;
}
}
// Constructor with assignment of the field N
CircleData::CircleData(int N)
{
n=N;
X = new double[n];
Y = new double[n];
for (int i=0; i<n; i++)
{
X[i]=0.;
Y[i]=0.;
}
}
// Constructor with assignment of each field
CircleData::CircleData(int N, const double dataX[], const double dataY[])
{
n=N;
X = new double[n];
Y = new double[n];
for (int i=0; i<n; i++)
{
X[i]=dataX[i];
Y[i]=dataY[i];
}
}
// Routine that computes the x- and y- sample means (the coordinates of the centeroid)
void CircleData::means(void)
{
meanX=0.; meanY=0.;
for (int i=0; i<n; i++)
{
meanX += X[i];
meanY += Y[i];
}
meanX /= n;
meanY /= n;
}
// Routine that centers the data set (shifts the coordinates to the centeroid)
void CircleData::center(void)
{
double sX=0.,sY=0.;
int i;
for (i=0; i<n; i++)
{
sX += X[i];
sY += Y[i];
}
sX /= n;
sY /= n;
for (i=0; i<n; i++)
{
X[i] -= sX;
Y[i] -= sY;
}
meanX = 0.;
meanY = 0.;
}
// Routine that scales the coordinates (makes them of order one)
void CircleData::scale(void)
{
double sXX=0.,sYY=0.,scaling;
int i;
for (i=0; i<n; i++)
{
sXX += X[i]*X[i];
sYY += Y[i]*Y[i];
}
scaling = sqrt((sXX+sYY)/n/Two);
for (i=0; i<n; i++)
{
X[i] /= scaling;
Y[i] /= scaling;
}
}
// Destructor
CircleData::~CircleData()
{
delete[] X;
delete[] Y;
}
double Sigma (const CircleData& data, const Circle& circle)
{
double sum=0.,dx,dy;
for (int i=0; i<data.n; i++)
{
dx = data.X[i] - circle.a;
dy = data.Y[i] - circle.b;
sum += SQR(std::sqrt(dx*dx+dy*dy) - circle.r);
}
return std::sqrt(sum/data.n);
}
Circle CircleFitByHyper (CircleData& data)
/*
Circle fit to a given set of data points (in 2D)
This is an algebraic fit based on the journal article
A. Al-Sharadqah and N. Chernov, "Error analysis for circle fitting algorithms",
Electronic Journal of Statistics, Vol. 3, pages 886-911, (2009)
It is an algebraic circle fit with "hyperaccuracy" (with zero essential bias).
The term "hyperaccuracy" first appeared in papers by Kenichi Kanatani around 2006
Input: data - the class of data (contains the given points):
data.n - the number of data points
data.X[] - the array of X-coordinates
data.Y[] - the array of Y-coordinates
Output:
circle - parameters of the fitting circle:
circle.a - the X-coordinate of the center of the fitting circle
circle.b - the Y-coordinate of the center of the fitting circle
circle.r - the radius of the fitting circle
circle.s - the root mean square error (the estimate of sigma)
circle.j - the total number of iterations
This method combines the Pratt and Taubin fits to eliminate the essential bias.
It works well whether data points are sampled along an entire circle or
along a small arc.
Its statistical accuracy is theoretically higher than that of the Pratt fit
and Taubin fit, but practically they all return almost identical circles
(unlike the Kasa fit that may be grossly inaccurate).
It provides a very good initial guess for a subsequent geometric fit.
Nikolai Chernov (September 2012)
*/
{
int i,iter,IterMAX=99;
double Xi,Yi,Zi;
double Mz,Mxy,Mxx,Myy,Mxz,Myz,Mzz,Cov_xy,Var_z;
double A0,A1,A2,A22;
double Dy,xnew,x,ynew,y;
double DET,Xcenter,Ycenter;
Circle circle;
data.means(); // Compute x- and y- sample means (via a function in the class "data")
// computing moments
Mxx=Myy=Mxy=Mxz=Myz=Mzz=0.;
for (i=0; i<data.n; i++)
{
Xi = data.X[i] - data.meanX; // centered x-coordinates
Yi = data.Y[i] - data.meanY; // centered y-coordinates
Zi = Xi*Xi + Yi*Yi;
Mxy += Xi*Yi;
Mxx += Xi*Xi;
Myy += Yi*Yi;
Mxz += Xi*Zi;
Myz += Yi*Zi;
Mzz += Zi*Zi;
}
Mxx /= data.n;
Myy /= data.n;
Mxy /= data.n;
Mxz /= data.n;
Myz /= data.n;
Mzz /= data.n;
// computing the coefficients of the characteristic polynomial
Mz = Mxx + Myy;
Cov_xy = Mxx*Myy - Mxy*Mxy;
Var_z = Mzz - Mz*Mz;
A2 = Four*Cov_xy - Three*Mz*Mz - Mzz;
A1 = Var_z*Mz + Four*Cov_xy*Mz - Mxz*Mxz - Myz*Myz;
A0 = Mxz*(Mxz*Myy - Myz*Mxy) + Myz*(Myz*Mxx - Mxz*Mxy) - Var_z*Cov_xy;
A22 = A2 + A2;
// finding the root of the characteristic polynomial
// using Newton's method starting at x=0
// (it is guaranteed to converge to the right root)
for (x=0.,y=A0,iter=0; iter<IterMAX; iter++) // usually, 4-6 iterations are enough
{
Dy = A1 + x*(A22 + 16.*x*x);
xnew = x - y/Dy;
if ((xnew == x)||(!std::isfinite(xnew))) break;
ynew = A0 + xnew*(A1 + xnew*(A2 + Four*xnew*xnew));
if (std::abs(ynew)>=std::abs(y)) break;
x = xnew; y = ynew;
}
// computing paramters of the fitting circle
DET = x*x - x*Mz + Cov_xy;
Xcenter = (Mxz*(Myy - x) - Myz*Mxy)/DET/Two;
Ycenter = (Myz*(Mxx - x) - Mxz*Mxy)/DET/Two;
// assembling the output
circle.a = Xcenter + data.meanX;
circle.b = Ycenter + data.meanY;
circle.r = std::sqrt(Xcenter*Xcenter + Ycenter*Ycenter + Mz - x - x);
circle.s = Sigma(data,circle);
circle.i = 0;
circle.j = iter; // return the number of iterations, too
return circle;
}
Circle CircleFitByKasa (CircleData &data)
/*
Circle fit to a given set of data points (in 2D)
This is an algebraic fit, disovered and rediscovered by many people.
One of the earliest publications is due to Kasa:
I. Kasa, "A curve fitting procedure and its error analysis",
IEEE Trans. Inst. Meas., Vol. 25, pages 8-14, (1976)
Input: data - the class of data (contains the given points):
data.n - the number of data points
data.X[] - the array of X-coordinates
data.Y[] - the array of Y-coordinates
Output:
circle - parameters of the fitting circle:
circle.a - the X-coordinate of the center of the fitting circle
circle.b - the Y-coordinate of the center of the fitting circle
circle.r - the radius of the fitting circle
circle.s - the root mean square error (the estimate of sigma)
circle.j - the total number of iterations
The method is based on the minimization of the function
F = sum [(x-a)^2 + (y-b)^2 - R^2]^2
This is perhaps the simplest and fastest circle fit.
It works well when data points are sampled along an entire circle
or a large part of it (at least half circle).
It does not work well when data points are sampled along a small arc
of a circle. In that case the method is heavily biased, it returns
circles that are too often too small.
It is the oldest algebraic circle fit (first published in 1972?).
For 20-30 years it has been the most popular circle fit, at least
until the more robust Pratt fit (1987) and Taubin fit (1991) were invented.
Nikolai Chernov (September 2012)
*/
{
int i;
double Xi,Yi,Zi;
double Mxy,Mxx,Myy,Mxz,Myz;
double B,C,G11,G12,G22,D1,D2;
Circle circle;
data.means(); // Compute x- and y- sample means (via a function in the class "data")
// computing moments
Mxx=Myy=Mxy=Mxz=Myz=0.;
for (i=0; i<data.n; i++)
{
Xi = data.X[i] - data.meanX; // centered x-coordinates
Yi = data.Y[i] - data.meanY; // centered y-coordinates
Zi = Xi*Xi + Yi*Yi;
Mxx += Xi*Xi;
Myy += Yi*Yi;
Mxy += Xi*Yi;
Mxz += Xi*Zi;
Myz += Yi*Zi;
}
Mxx /= data.n;
Myy /= data.n;
Mxy /= data.n;
Mxz /= data.n;
Myz /= data.n;
// solving system of equations by Cholesky factorization
G11 = std::sqrt(Mxx);
G12 = Mxy/G11;
G22 = std::sqrt(Myy - G12*G12);
D1 = Mxz/G11;
D2 = (Myz - D1*G12)/G22;
// computing paramters of the fitting circle
C = D2/G22/Two;
B = (D1 - G12*C)/G11/Two;
// assembling the output
circle.a = B + data.meanX;
circle.b = C + data.meanY;
circle.r = std::sqrt(B*B + C*C + Mxx + Myy);
circle.s = Sigma(data,circle);
circle.i = 0;
circle.j = 0;
return circle;
}
Circle CircleFitByPratt (CircleData &data)
/*
Circle fit to a given set of data points (in 2D)
This is an algebraic fit, due to Pratt, based on the journal article
V. Pratt, "Direct least-squares fitting of algebraic surfaces",
Computer Graphics, Vol. 21, pages 145-152 (1987)
Input: data - the class of data (contains the given points):
data.n - the number of data points
data.X[] - the array of X-coordinates
data.Y[] - the array of Y-coordinates
Output:
circle - parameters of the fitting circle:
circle.a - the X-coordinate of the center of the fitting circle
circle.b - the Y-coordinate of the center of the fitting circle
circle.r - the radius of the fitting circle
circle.s - the root mean square error (the estimate of sigma)
circle.j - the total number of iterations
The method is based on the minimization of the function
F = sum [(x-a)^2 + (y-b)^2 - R^2]^2 / R^2
This method is more balanced than the simple Kasa fit.
It works well whether data points are sampled along an entire circle or
along a small arc.
It still has a small bias and its statistical accuracy is slightly
lower than that of the geometric fit (minimizing geometric distances).
It provides a good initial guess for a subsequent geometric fit.
Nikolai Chernov (September 2012)
*/
{
int i,iter,IterMAX=99;
double Xi,Yi,Zi;
double Mz,Mxy,Mxx,Myy,Mxz,Myz,Mzz,Cov_xy,Var_z;
double A0,A1,A2,A22;
double Dy,xnew,x,ynew,y;
double DET,Xcenter,Ycenter;
Circle circle;
data.means(); // Compute x- and y- sample means (via a function in the class "data")
// computing moments
Mxx=Myy=Mxy=Mxz=Myz=Mzz=0.;
for (i=0; i<data.n; i++)
{
Xi = data.X[i] - data.meanX; // centered x-coordinates
Yi = data.Y[i] - data.meanY; // centered y-coordinates
Zi = Xi*Xi + Yi*Yi;
Mxy += Xi*Yi;
Mxx += Xi*Xi;
Myy += Yi*Yi;
Mxz += Xi*Zi;
Myz += Yi*Zi;
Mzz += Zi*Zi;
}
Mxx /= data.n;
Myy /= data.n;
Mxy /= data.n;
Mxz /= data.n;
Myz /= data.n;
Mzz /= data.n;
// computing coefficients of the characteristic polynomial
Mz = Mxx + Myy;
Cov_xy = Mxx*Myy - Mxy*Mxy;
Var_z = Mzz - Mz*Mz;
A2 = Four*Cov_xy - Three*Mz*Mz - Mzz;
A1 = Var_z*Mz + Four*Cov_xy*Mz - Mxz*Mxz - Myz*Myz;
A0 = Mxz*(Mxz*Myy - Myz*Mxy) + Myz*(Myz*Mxx - Mxz*Mxy) - Var_z*Cov_xy;
A22 = A2 + A2;
// finding the root of the characteristic polynomial
// using Newton's method starting at x=0
// (it is guaranteed to converge to the right root)
for (x=0.,y=A0,iter=0; iter<IterMAX; iter++) // usually, 4-6 iterations are enough
{
Dy = A1 + x*(A22 + 16.*x*x);
xnew = x - y/Dy;
if ((xnew == x)||(!std::isfinite(xnew))) break;
ynew = A0 + xnew*(A1 + xnew*(A2 + Four*xnew*xnew));
if (std::abs(ynew)>=std::abs(y)) break;
x = xnew; y = ynew;
}
// computing paramters of the fitting circle
DET = x*x - x*Mz + Cov_xy;
Xcenter = (Mxz*(Myy - x) - Myz*Mxy)/DET/Two;
Ycenter = (Myz*(Mxx - x) - Mxz*Mxy)/DET/Two;
// assembling the output
circle.a = Xcenter + data.meanX;
circle.b = Ycenter + data.meanY;
circle.r = std::sqrt(Xcenter*Xcenter + Ycenter*Ycenter + Mz + x + x);
circle.s = Sigma(data,circle);
circle.i = 0;
circle.j = iter; // return the number of iterations, too
return circle;
}
Circle CircleFitByTaubin (CircleData& data)
/*
Circle fit to a given set of data points (in 2D)
This is an algebraic fit, due to Taubin, based on the journal article
G. Taubin, "Estimation Of Planar Curves, Surfaces And Nonplanar
Space Curves Defined By Implicit Equations, With
Applications To Edge And Range Image Segmentation",
IEEE Trans. PAMI, Vol. 13, pages 1115-1138, (1991)
Input: data - the class of data (contains the given points):
data.n - the number of data points
data.X[] - the array of X-coordinates
data.Y[] - the array of Y-coordinates
Output:
circle - parameters of the fitting circle:
circle.a - the X-coordinate of the center of the fitting circle
circle.b - the Y-coordinate of the center of the fitting circle
circle.r - the radius of the fitting circle
circle.s - the root mean square error (the estimate of sigma)
circle.j - the total number of iterations
The method is based on the minimization of the function
sum [(x-a)^2 + (y-b)^2 - R^2]^2
F = -------------------------------
sum [(x-a)^2 + (y-b)^2]
This method is more balanced than the simple Kasa fit.
It works well whether data points are sampled along an entire circle or
along a small arc.
It still has a small bias and its statistical accuracy is slightly
lower than that of the geometric fit (minimizing geometric distances),
but slightly higher than that of the very similar Pratt fit.
Besides, the Taubin fit is slightly simpler than the Pratt fit
It provides a very good initial guess for a subsequent geometric fit.
Nikolai Chernov (September 2012)
*/
{
int i,iter,IterMAX=99;
double Xi,Yi,Zi;
double Mz,Mxy,Mxx,Myy,Mxz,Myz,Mzz,Cov_xy,Var_z;
double A0,A1,A2,A22,A3,A33;
double Dy,xnew,x,ynew,y;
double DET,Xcenter,Ycenter;
Circle circle;
data.means(); // Compute x- and y- sample means (via a function in the class "data")
// computing moments
Mxx=Myy=Mxy=Mxz=Myz=Mzz=0.;
for (i=0; i<data.n; i++)
{
Xi = data.X[i] - data.meanX; // centered x-coordinates
Yi = data.Y[i] - data.meanY; // centered y-coordinates
Zi = Xi*Xi + Yi*Yi;
Mxy += Xi*Yi;
Mxx += Xi*Xi;
Myy += Yi*Yi;
Mxz += Xi*Zi;
Myz += Yi*Zi;
Mzz += Zi*Zi;
}
Mxx /= data.n;
Myy /= data.n;
Mxy /= data.n;
Mxz /= data.n;
Myz /= data.n;
Mzz /= data.n;
// computing coefficients of the characteristic polynomial
Mz = Mxx + Myy;
Cov_xy = Mxx*Myy - Mxy*Mxy;
Var_z = Mzz - Mz*Mz;
A3 = Four*Mz;
A2 = -Three*Mz*Mz - Mzz;
A1 = Var_z*Mz + Four*Cov_xy*Mz - Mxz*Mxz - Myz*Myz;
A0 = Mxz*(Mxz*Myy - Myz*Mxy) + Myz*(Myz*Mxx - Mxz*Mxy) - Var_z*Cov_xy;
A22 = A2 + A2;
A33 = A3 + A3 + A3;
// finding the root of the characteristic polynomial
// using Newton's method starting at x=0
// (it is guaranteed to converge to the right root)
for (x=0.,y=A0,iter=0; iter<IterMAX; iter++) // usually, 4-6 iterations are enough
{
Dy = A1 + x*(A22 + A33*x);
xnew = x - y/Dy;
if ((xnew == x)||(!std::isfinite(xnew))) break;
ynew = A0 + xnew*(A1 + xnew*(A2 + xnew*A3));
if (std::abs(ynew)>=std::abs(y)) break;
x = xnew; y = ynew;
}
// computing paramters of the fitting circle
DET = x*x - x*Mz + Cov_xy;
Xcenter = (Mxz*(Myy - x) - Myz*Mxy)/DET/Two;
Ycenter = (Myz*(Mxx - x) - Mxz*Mxy)/DET/Two;
// assembling the output
circle.a = Xcenter + data.meanX;
circle.b = Ycenter + data.meanY;
circle.r = std::sqrt(Xcenter*Xcenter + Ycenter*Ycenter + Mz);
circle.s = Sigma(data,circle);
circle.i = 0;
circle.j = iter; // return the number of iterations, too
return circle;
}
int CircleFitByLevenbergMarquardtFull (const CircleData& data, const Circle& circleIni, double LambdaIni, Circle& circle)
/* <---------------------------- Input ----------------------------> <-- Output -->
Geometric circle fit to a given set of data points (in 2D)
Input: data - the class of data (contains the given points):
data.n - the number of data points
data.X[] - the array of X-coordinates
data.Y[] - the array of Y-coordinates
circleIni - parameters of the initial circle ("initial guess")
circleIni.a - the X-coordinate of the center of the initial circle
circleIni.b - the Y-coordinate of the center of the initial circle
circleIni.r - the radius of the initial circle
LambdaIni - the initial value of the control parameter "lambda"
for the Levenberg-Marquardt procedure
(common choice is a small positive number, e.g. 0.001)
Output:
integer function value is a code:
0: normal termination, the best fitting circle is
successfully found
1: the number of outer iterations exceeds the limit (99)
(indicator of a possible divergence)
2: the number of inner iterations exceeds the limit (99)
(another indicator of a possible divergence)
3: the coordinates of the center are too large
(a strong indicator of divergence)
circle - parameters of the fitting circle ("best fit")
circle.a - the X-coordinate of the center of the fitting circle
circle.b - the Y-coordinate of the center of the fitting circle
circle.r - the radius of the fitting circle
circle.s - the root mean square error (the estimate of sigma)
circle.i - the total number of outer iterations (updating the parameters)
circle.j - the total number of inner iterations (adjusting lambda)
Algorithm: Levenberg-Marquardt running over the full parameter space (a,b,r)
See a detailed description in Section 4.5 of the book by Nikolai Chernov:
"Circular and linear regression: Fitting circles and lines by least squares"
Chapman & Hall/CRC, Monographs on Statistics and Applied Probability, volume 117, 2010.
Nikolai Chernov, February 2014
*/
{
int code,i,iter,inner,IterMAX=99;
double factorUp=10.,factorDown=0.04,lambda,ParLimit=1.e+6;
double dx,dy,ri,u,v;
double Mu,Mv,Muu,Mvv,Muv,Mr,UUl,VVl,Nl,F1,F2,F3,dX,dY,dR;
double epsilon=3.e-8;
double G11,G22,G33,G12,G13,G23,D1,D2,D3;
Circle Old,New;
// starting with the given initial circle (initial guess)
New = circleIni;
// compute the root-mean-square error via function Sigma; see Utilities.cpp
New.s = Sigma(data,New);
// initializing lambda, iteration counters, and the exit code
lambda = LambdaIni;
iter = inner = code = 0;
NextIteration:
Old = New;
if (++iter > IterMAX) {code = 1; goto enough;}
// computing moments
Mu=Mv=Muu=Mvv=Muv=Mr=0.;
for (i=0; i<data.n; i++)
{
dx = data.X[i] - Old.a;
dy = data.Y[i] - Old.b;
ri = std::sqrt(dx*dx + dy*dy);
u = dx/ri;
v = dy/ri;
Mu += u;
Mv += v;
Muu += u*u;
Mvv += v*v;
Muv += u*v;
Mr += ri;
}
Mu /= data.n;
Mv /= data.n;
Muu /= data.n;
Mvv /= data.n;
Muv /= data.n;
Mr /= data.n;
// computing matrices
F1 = Old.a + Old.r*Mu - data.meanX;
F2 = Old.b + Old.r*Mv - data.meanY;
F3 = Old.r - Mr;
Old.g = New.g = std::sqrt(F1*F1 + F2*F2 + F3*F3);
try_again:
UUl = Muu + lambda;
VVl = Mvv + lambda;
Nl = One + lambda;
// Cholesly decomposition
G11 = std::sqrt(UUl);
G12 = Muv/G11;
G13 = Mu/G11;
G22 = std::sqrt(VVl - G12*G12);
G23 = (Mv - G12*G13)/G22;
G33 = std::sqrt(Nl - G13*G13 - G23*G23);
D1 = F1/G11;
D2 = (F2 - G12*D1)/G22;
D3 = (F3 - G13*D1 - G23*D2)/G33;
dR = D3/G33;
dY = (D2 - G23*dR)/G22;
dX = (D1 - G12*dY - G13*dR)/G11;
if ((std::abs(dR)+std::abs(dX)+std::abs(dY))/(One+Old.r) < epsilon) goto enough;
// updating the parameters
New.a = Old.a - dX;
New.b = Old.b - dY;
if (std::abs(New.a)>ParLimit || std::abs(New.b)>ParLimit) {code = 3; goto enough;}
New.r = Old.r - dR;
if (New.r <= 0.)
{
lambda *= factorUp;
if (++inner > IterMAX) {code = 2; goto enough;}
goto try_again;
}
// compute the root-mean-square error via function Sigma; see Utilities.cpp
New.s = Sigma(data,New);
// check if improvement is gained
if (New.s < Old.s) // yes, improvement
{
lambda *= factorDown;
goto NextIteration;
}
else // no improvement
{
if (++inner > IterMAX) {code = 2; goto enough;}
lambda *= factorUp;
goto try_again;
}
// exit
enough:
Old.i = iter; // total number of outer iterations (updating the parameters)
Old.j = inner; // total number of inner iterations (adjusting lambda)
circle = Old;
return code;
}