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