1 /*
2 * Copyright (C) 2015 Alberto Irurueta Carro (alberto@irurueta.com)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.irurueta.geometry.estimators;
17
18 import com.irurueta.geometry.Circle;
19 import com.irurueta.geometry.ColinearPointsException;
20 import com.irurueta.geometry.Point2D;
21 import com.irurueta.numerical.robust.LMedSRobustEstimator;
22 import com.irurueta.numerical.robust.LMedSRobustEstimatorListener;
23 import com.irurueta.numerical.robust.RobustEstimator;
24 import com.irurueta.numerical.robust.RobustEstimatorException;
25 import com.irurueta.numerical.robust.RobustEstimatorMethod;
26
27 import java.util.List;
28
29 /**
30 * Finds the best circle for provided collection of 2D points using LMedS
31 * algorithm.
32 */
33 public class LMedSCircleRobustEstimator extends CircleRobustEstimator {
34
35 /**
36 * Default value to be used for stop threshold. Stop threshold can be used
37 * to keep the algorithm iterating in case that best estimated threshold
38 * using median of residuals is not small enough. Once a solution is found
39 * that generates a threshold below this value, the algorithm will stop.
40 * The stop threshold can be used to prevent the LMedS algorithm iterating
41 * too many times in cases where samples have a very similar accuracy.
42 * For instance, in cases where proportion of outliers is very small (close
43 * to 0%), and samples are very accurate (i.e. 1e-6), the algorithm would
44 * iterate for a long time trying to find the best solution when indeed
45 * there is no need to do that if a reasonable threshold has already been
46 * reached.
47 * Because of this behaviour the stop threshold can be set to a value much
48 * lower than the one typically used in RANSAC, and yet the algorithm could
49 * still produce even smaller thresholds in estimated results.
50 */
51 public static final double DEFAULT_STOP_THRESHOLD = 1e-3;
52
53 /**
54 * Minimum allowed stop threshold value.
55 */
56 public static final double MIN_STOP_THRESHOLD = 0.0;
57
58 /**
59 * Threshold to be used to keep the algorithm iterating in case that best
60 * estimated threshold using median of residuals is not small enough. Once
61 * a solution is found that generates a threshold below this value, the
62 * algorithm will stop.
63 * The stop threshold can be used to prevent the LMedS algorithm iterating
64 * too many times in cases where samples have a very similar accuracy.
65 * For instance, in cases where proportion of outliers is very small (close
66 * to 0%), and samples are very accurate (i.e. 1e-6), the algorithm would
67 * iterate for a long time trying to find the best solution when indeed
68 * there is no need to do that if a reasonable threshold has already been
69 * reached.
70 * Because of this behaviour the stop threshold can be set to a value much
71 * lower than the one typically used in RANSAC, and yet the algorithm could
72 * still produce even smaller thresholds in estimated results.
73 */
74 private double stopThreshold;
75
76 /**
77 * Constructor.
78 */
79 public LMedSCircleRobustEstimator() {
80 super();
81 stopThreshold = DEFAULT_STOP_THRESHOLD;
82 }
83
84 /**
85 * Constructor with points.
86 *
87 * @param points 2D points to estimate a circle.
88 * @throws IllegalArgumentException if provided list of points don't have
89 * a size greater or equal than MINIMUM_SIZE.
90 */
91 public LMedSCircleRobustEstimator(final List<Point2D> points) {
92 super(points);
93 stopThreshold = DEFAULT_STOP_THRESHOLD;
94 }
95
96 /**
97 * Constructor.
98 *
99 * @param listener listener to be notified of events such as when estimation
100 * starts, ends or its progress significantly changes.
101 */
102 public LMedSCircleRobustEstimator(final CircleRobustEstimatorListener listener) {
103 super(listener);
104 stopThreshold = DEFAULT_STOP_THRESHOLD;
105 }
106
107
108 /**
109 * Constructor.
110 *
111 * @param listener listener to be notified of events such as when estimation
112 * starts, ends or its progress significantly changes.
113 * @param points 2D points to estimate a circle.
114 * @throws IllegalArgumentException if provided list of points don't have
115 * a size greater or equal than MINIMUM_SIZE.
116 */
117 public LMedSCircleRobustEstimator(final CircleRobustEstimatorListener listener, final List<Point2D> points) {
118 super(listener, points);
119 stopThreshold = DEFAULT_STOP_THRESHOLD;
120 }
121
122 /**
123 * Returns threshold to be used to keep the algorithm iterating in case that
124 * best estimated threshold using median of residuals is not small enough.
125 * Once a solution is found that generates a threshold below this value, the
126 * algorithm will stop.
127 * The stop threshold can be used to prevent the LMedS algorithm iterating
128 * too many times in cases where samples have a very similar accuracy.
129 * For instance, in cases where proportion of outliers is very small (close
130 * to 0%), and samples are very accurate (i.e. 1e-6), the algorithm would
131 * iterate for a long time trying to find the best solution when indeed
132 * there is no need to do that if a reasonable threshold has already been
133 * reached.
134 * Because of this behaviour the stop threshold can be set to a value much
135 * lower than the one typically used in RANSAC, and yet the algorithm could
136 * still produce even smaller thresholds in estimated results.
137 *
138 * @return stop threshold to stop the algorithm prematurely when a certain
139 * accuracy has been reached.
140 */
141 public double getStopThreshold() {
142 return stopThreshold;
143 }
144
145 /**
146 * Sets threshold to be used to keep the algorithm iterating in case that
147 * best estimated threshold using median of residuals is not small enough.
148 * Once a solution is found that generates a threshold below this value, the
149 * algorithm will stop.
150 * The stop threshold can be used to prevent the LMedS algorithm iterating
151 * too many times in cases where samples have a very similar accuracy.
152 * For instance, in cases where proportion of outliers is very small (close
153 * to 0%), and samples are very accurate (i.e. 1e-6), the algorithm would
154 * iterate for a long time trying to find the best solution when indeed
155 * there is no need to do that if a reasonable threshold has already been
156 * reached.
157 * Because of this behaviour the stop threshold can be set to a value much
158 * lower than the one typically used in RANSAC, and yet the algorithm could
159 * still produce even smaller thresholds in estimated results.
160 *
161 * @param stopThreshold stop threshold to stop the algorithm prematurely
162 * when a certain accuracy has been reached.
163 * @throws IllegalArgumentException if provided value is zero or negative.
164 * @throws LockedException if robust estimator is locked because an
165 * estimation is already in progress.
166 */
167 public void setStopThreshold(final double stopThreshold) throws LockedException {
168 if (isLocked()) {
169 throw new LockedException();
170 }
171 if (stopThreshold <= MIN_STOP_THRESHOLD) {
172 throw new IllegalArgumentException();
173 }
174
175 this.stopThreshold = stopThreshold;
176 }
177
178 /**
179 * Estimates a circle using a robust estimator and the best set of 2D points
180 * that fit into the locus of the estimated circle found using the robust
181 * estimator.
182 *
183 * @return a circle.
184 * @throws LockedException if robust estimator is locked because an
185 * estimation is already in progress.
186 * @throws NotReadyException if provided input data is not enough to start
187 * the estimation.
188 * @throws RobustEstimatorException if estimation fails for any reason
189 * (i.e. numerical instability, no solution available, etc).
190 */
191 @SuppressWarnings("DuplicatedCode")
192 @Override
193 public Circle estimate() throws LockedException, NotReadyException, RobustEstimatorException {
194 if (isLocked()) {
195 throw new LockedException();
196 }
197 if (!isReady()) {
198 throw new NotReadyException();
199 }
200
201 final var innerEstimator = new LMedSRobustEstimator<>(new LMedSRobustEstimatorListener<Circle>() {
202
203 @Override
204 public int getTotalSamples() {
205 return points.size();
206 }
207
208 @Override
209 public int getSubsetSize() {
210 return CircleRobustEstimator.MINIMUM_SIZE;
211 }
212
213 @Override
214 public void estimatePreliminarSolutions(final int[] samplesIndices, final List<Circle> solutions) {
215 final var point1 = points.get(samplesIndices[0]);
216 final var point2 = points.get(samplesIndices[1]);
217 final var point3 = points.get(samplesIndices[2]);
218
219 try {
220 final var circle = new Circle(point1, point2, point3);
221 solutions.add(circle);
222 } catch (final ColinearPointsException e) {
223 // if points are coincident, no solution is added
224 }
225 }
226
227 @Override
228 public double computeResidual(final Circle currentEstimation, int i) {
229 return residual(currentEstimation, points.get(i));
230 }
231
232 @Override
233 public boolean isReady() {
234 return LMedSCircleRobustEstimator.this.isReady();
235 }
236
237 @Override
238 public void onEstimateStart(final RobustEstimator<Circle> estimator) {
239 if (listener != null) {
240 listener.onEstimateStart(LMedSCircleRobustEstimator.this);
241 }
242 }
243
244 @Override
245 public void onEstimateEnd(final RobustEstimator<Circle> estimator) {
246 if (listener != null) {
247 listener.onEstimateEnd(LMedSCircleRobustEstimator.this);
248 }
249 }
250
251 @Override
252 public void onEstimateNextIteration(final RobustEstimator<Circle> estimator, final int iteration) {
253 if (listener != null) {
254 listener.onEstimateNextIteration(LMedSCircleRobustEstimator.this, iteration);
255 }
256 }
257
258 @Override
259 public void onEstimateProgressChange(final RobustEstimator<Circle> estimator, final float progress) {
260 if (listener != null) {
261 listener.onEstimateProgressChange(LMedSCircleRobustEstimator.this, progress);
262 }
263 }
264 });
265
266 try {
267 locked = true;
268 innerEstimator.setConfidence(confidence);
269 innerEstimator.setMaxIterations(maxIterations);
270 innerEstimator.setProgressDelta(progressDelta);
271 innerEstimator.setStopThreshold(stopThreshold);
272 return innerEstimator.estimate();
273 } catch (final com.irurueta.numerical.LockedException e) {
274 throw new LockedException(e);
275 } catch (final com.irurueta.numerical.NotReadyException e) {
276 throw new NotReadyException(e);
277 } finally {
278 locked = false;
279 }
280 }
281
282 /**
283 * Returns method being used for robust estimation.
284 *
285 * @return method being used for robust estimation.
286 */
287 @Override
288 public RobustEstimatorMethod getMethod() {
289 return RobustEstimatorMethod.LMEDS;
290 }
291 }