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.CoincidentPointsException;
19 import com.irurueta.geometry.Line2D;
20 import com.irurueta.geometry.Point2D;
21 import com.irurueta.numerical.robust.MSACRobustEstimator;
22 import com.irurueta.numerical.robust.MSACRobustEstimatorListener;
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 2D line for provided collection of 2D points using MSAC
31 * algorithm.
32 */
33 @SuppressWarnings("DuplicatedCode")
34 public class MSACLine2DRobustEstimator extends Line2DRobustEstimator {
35 /**
36 * Constant defining default threshold to determine whether points are
37 * inliers or not.
38 * Because typical resolution for points is 1 pixel, then default threshold
39 * is defined as 1.
40 */
41 public static final double DEFAULT_THRESHOLD = 1;
42
43 /**
44 * Minimum value that can be set as threshold.
45 * Threshold must be strictly greater than 0.0.
46 */
47 public static final double MIN_THRESHOLD = 0.0;
48
49 /**
50 * Threshold to determine whether points are inliers or not when testing
51 * possible estimation solutions.
52 * The threshold refers to the amount of error (i.e. distance) a possible
53 * solution has on a sampled line.
54 */
55 private double threshold;
56
57 /**
58 * Constructor.
59 */
60 public MSACLine2DRobustEstimator() {
61 super();
62 threshold = DEFAULT_THRESHOLD;
63 }
64
65 /**
66 * Constructor with points.
67 *
68 * @param points 2D points to estimate a 2D line.
69 * @throws IllegalArgumentException if provided list of points doesn't have
70 * a size greater or equal than MINIMUM_SIZE.
71 */
72 public MSACLine2DRobustEstimator(final List<Point2D> points) {
73 super(points);
74 threshold = DEFAULT_THRESHOLD;
75 }
76
77 /**
78 * Constructor.
79 *
80 * @param listener listener to be notified of events such as when estimation
81 * starts, ends or its progress significantly changes.
82 */
83 public MSACLine2DRobustEstimator(final Line2DRobustEstimatorListener listener) {
84 super(listener);
85 threshold = DEFAULT_THRESHOLD;
86 }
87
88
89 /**
90 * Constructor.
91 *
92 * @param listener listener to be notified of events such as when estimation
93 * starts, ends or its progress significantly changes.
94 * @param points 2D points to estimate a 2D line.
95 * @throws IllegalArgumentException if provided list of points doesn't have
96 * a size greater or equal than MINIMUM_SIZE.
97 */
98 public MSACLine2DRobustEstimator(final Line2DRobustEstimatorListener listener, final List<Point2D> points) {
99 super(listener, points);
100 threshold = DEFAULT_THRESHOLD;
101 }
102
103 /**
104 * Returns threshold to determine whether points are inliers or not when
105 * testing possible estimation solutions.
106 * The threshold refers to the amount of error a possible solution has on a
107 * given point.
108 *
109 * @return threshold to determine whether points are inliers or not when
110 * testing possible estimation solutions.
111 */
112 public double getThreshold() {
113 return threshold;
114 }
115
116 /**
117 * Sets threshold to determine whether points are inliers or not when
118 * testing possible estimation solutions.
119 * The threshold refers to the amount of error a possible solution has on
120 * a given point.
121 *
122 * @param threshold threshold to be set.
123 * @throws IllegalArgumentException if provided value is equal or less than
124 * zero.
125 * @throws LockedException if robust estimator is locked because an
126 * estimation is already in progress.
127 */
128 public void setThreshold(final double threshold) throws LockedException {
129 if (isLocked()) {
130 throw new LockedException();
131 }
132 if (threshold <= MIN_THRESHOLD) {
133 throw new IllegalArgumentException();
134 }
135 this.threshold = threshold;
136 }
137
138
139 /**
140 * Estimates a 2D line using a robust estimator and the best set of 2D
141 * points that pass through the estimated 2D line (i.e. belong to its locus).
142 *
143 * @return a 2D line.
144 * @throws LockedException if robust estimator is locked because an
145 * estimation is already in progress.
146 * @throws NotReadyException if provided input data is not enough to start
147 * the estimation.
148 * @throws RobustEstimatorException if estimation fails for any reason
149 * (i.e. numerical instability, no solution available, etc).
150 */
151 @Override
152 public Line2D estimate() throws LockedException, NotReadyException, RobustEstimatorException {
153 if (isLocked()) {
154 throw new LockedException();
155 }
156 if (!isReady()) {
157 throw new NotReadyException();
158 }
159
160 final var innerEstimator = new MSACRobustEstimator<>(new MSACRobustEstimatorListener<Line2D>() {
161
162 @Override
163 public double getThreshold() {
164 return threshold;
165 }
166
167 @Override
168 public int getTotalSamples() {
169 return points.size();
170 }
171
172 @Override
173 public int getSubsetSize() {
174 return Line2DRobustEstimator.MINIMUM_SIZE;
175 }
176
177 @Override
178 public void estimatePreliminarSolutions(final int[] samplesIndices, final List<Line2D> solutions) {
179 final var point1 = points.get(samplesIndices[0]);
180 final var point2 = points.get(samplesIndices[1]);
181
182 try {
183 final var line = new Line2D(point1, point2, false);
184 solutions.add(line);
185 } catch (final CoincidentPointsException e) {
186 // if points are coincident, no solution is added
187 }
188 }
189
190 @Override
191 public double computeResidual(final Line2D currentEstimation, final int i) {
192 return residual(currentEstimation, points.get(i));
193 }
194
195 @Override
196 public boolean isReady() {
197 return MSACLine2DRobustEstimator.this.isReady();
198 }
199
200 @Override
201 public void onEstimateStart(final RobustEstimator<Line2D> estimator) {
202 if (listener != null) {
203 listener.onEstimateStart(MSACLine2DRobustEstimator.this);
204 }
205 }
206
207 @Override
208 public void onEstimateEnd(final RobustEstimator<Line2D> estimator) {
209 if (listener != null) {
210 listener.onEstimateEnd(MSACLine2DRobustEstimator.this);
211 }
212 }
213
214 @Override
215 public void onEstimateNextIteration(final RobustEstimator<Line2D> estimator, final int iteration) {
216 if (listener != null) {
217 listener.onEstimateNextIteration(MSACLine2DRobustEstimator.this, iteration);
218 }
219 }
220
221 @Override
222 public void onEstimateProgressChange(final RobustEstimator<Line2D> estimator, final float progress) {
223 if (listener != null) {
224 listener.onEstimateProgressChange(MSACLine2DRobustEstimator.this, progress);
225 }
226 }
227 });
228
229 try {
230 locked = true;
231 innerEstimator.setConfidence(confidence);
232 innerEstimator.setMaxIterations(maxIterations);
233 innerEstimator.setProgressDelta(progressDelta);
234 return innerEstimator.estimate();
235 } catch (final com.irurueta.numerical.LockedException e) {
236 throw new LockedException(e);
237 } catch (final com.irurueta.numerical.NotReadyException e) {
238 throw new NotReadyException(e);
239 } finally {
240 locked = false;
241 }
242 }
243
244 /**
245 * Returns method being used for robust estimation.
246 *
247 * @return method being used for robust estimation.
248 */
249 @Override
250 public RobustEstimatorMethod getMethod() {
251 return RobustEstimatorMethod.MSAC;
252 }
253 }