1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.optim.nonlinear.vector.leastsquares;
23
24 import java.util.ArrayList;
25
26 import org.hipparchus.analysis.MultivariateMatrixFunction;
27 import org.hipparchus.analysis.MultivariateVectorFunction;
28 import org.hipparchus.util.FastMath;
29 import org.hipparchus.util.MathUtils;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 class CircleProblem {
46
47 private final ArrayList<double[]> points;
48
49 private final double xSigma;
50
51 private final double ySigma;
52
53
54 private final int resolution;
55
56
57
58
59
60
61
62 public CircleProblem(double xError,
63 double yError,
64 int searchResolution) {
65 points = new ArrayList<double[]>();
66 xSigma = xError;
67 ySigma = yError;
68 resolution = searchResolution;
69 }
70
71
72
73
74
75 public CircleProblem(double xError,
76 double yError) {
77 this(xError, yError, 500);
78 }
79
80 public void addPoint(double px, double py) {
81 points.add(new double[] { px, py });
82 }
83
84 public double[] target() {
85 final double[] t = new double[points.size() * 2];
86 for (int i = 0; i < points.size(); i++) {
87 final double[] p = points.get(i);
88 final int index = i * 2;
89 t[index] = p[0];
90 t[index + 1] = p[1];
91 }
92
93 return t;
94 }
95
96 public double[] weight() {
97 final double wX = 1 / (xSigma * xSigma);
98 final double wY = 1 / (ySigma * ySigma);
99 final double[] w = new double[points.size() * 2];
100 for (int i = 0; i < points.size(); i++) {
101 final int index = i * 2;
102 w[index] = wX;
103 w[index + 1] = wY;
104 }
105
106 return w;
107 }
108
109 public MultivariateVectorFunction getModelFunction() {
110 return new MultivariateVectorFunction() {
111 public double[] value(double[] params) {
112 final double cx = params[0];
113 final double cy = params[1];
114 final double r = params[2];
115
116 final double[] model = new double[points.size() * 2];
117
118 final double deltaTheta = MathUtils.TWO_PI / resolution;
119 for (int i = 0; i < points.size(); i++) {
120 final double[] p = points.get(i);
121 final double px = p[0];
122 final double py = p[1];
123
124 double bestX = 0;
125 double bestY = 0;
126 double dMin = Double.POSITIVE_INFINITY;
127
128
129
130
131 for (double theta = 0; theta <= MathUtils.TWO_PI; theta += deltaTheta) {
132 final double currentX = cx + r * FastMath.cos(theta);
133 final double currentY = cy + r * FastMath.sin(theta);
134 final double dX = currentX - px;
135 final double dY = currentY - py;
136 final double d = dX * dX + dY * dY;
137 if (d < dMin) {
138 dMin = d;
139 bestX = currentX;
140 bestY = currentY;
141 }
142 }
143
144 final int index = i * 2;
145 model[index] = bestX;
146 model[index + 1] = bestY;
147 }
148
149 return model;
150 }
151 };
152 }
153
154 public MultivariateMatrixFunction getModelFunctionJacobian() {
155 return new MultivariateMatrixFunction() {
156 public double[][] value(double[] point) {
157 return jacobian(point);
158 }
159 };
160 }
161
162 private double[][] jacobian(double[] params) {
163 final double[][] jacobian = new double[points.size() * 2][3];
164
165 for (int i = 0; i < points.size(); i++) {
166 final int index = i * 2;
167
168 jacobian[index][0] = 1;
169 jacobian[index + 1][0] = 0;
170
171 jacobian[index][1] = 0;
172 jacobian[index + 1][1] = 1;
173
174 final double[] p = points.get(i);
175 jacobian[index][2] = (p[0] - params[0]) / params[2];
176 jacobian[index + 1][2] = (p[1] - params[1]) / params[2];
177 }
178
179 return jacobian;
180 }
181 }