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.CoincidentPlanesException;
19 import com.irurueta.geometry.DualQuadric;
20 import com.irurueta.geometry.Plane;
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 dual quadric for provided collection of 3D planes using MSAC
31 * algorithm.
32 */
33 @SuppressWarnings("DuplicatedCode")
34 public class MSACDualQuadricRobustEstimator extends DualQuadricRobustEstimator {
35 /**
36 * Constant defining default threshold to determine whether planes are
37 * inliers or not.
38 * Threshold is defined by the equation abs(trans(P) * dQ * P) < t, where
39 * trans is the transposition, P is a plane, dQ is a dual quadric and t is a
40 * threshold.
41 * This equation determines the planes P belonging to the locus of a dual
42 * quadric dQ 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 planes 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 MSACDualQuadricRobustEstimator() {
64 super();
65 threshold = DEFAULT_THRESHOLD;
66 }
67
68 /**
69 * Constructor with points.
70 *
71 * @param planes 3D planes to estimate a dual quadric.
72 * @throws IllegalArgumentException if provided list of planes don't have
73 * a size greater or equal than MINIMUM_SIZE.
74 */
75 public MSACDualQuadricRobustEstimator(final List<Plane> planes) {
76 super(planes);
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 MSACDualQuadricRobustEstimator(final DualQuadricRobustEstimatorListener 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 planes 3D planes to estimate a dual quadric.
98 * @throws IllegalArgumentException if provided list of planes don't have
99 * a size greater or equal than MINIMUM_SIZE.
100 */
101 public MSACDualQuadricRobustEstimator(
102 final DualQuadricRobustEstimatorListener listener, final List<Plane> planes) {
103 super(listener, planes);
104 threshold = DEFAULT_THRESHOLD;
105 }
106
107 /**
108 * Returns threshold to determine whether planes 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 plane.
112 *
113 * @return threshold to determine whether planes 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 planes 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 plane.
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 quadric using a robust estimator and the best set of 3D
145 * planes that fit into the locus of the estimated dual quadric found using
146 * the robust estimator.
147 *
148 * @return a dual quadric.
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 DualQuadric 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 MSACRobustEstimator<>(new MSACRobustEstimatorListener<DualQuadric>() {
166
167 @Override
168 public double getThreshold() {
169 return threshold;
170 }
171
172 @Override
173 public int getTotalSamples() {
174 return planes.size();
175 }
176
177 @Override
178 public int getSubsetSize() {
179 return DualQuadricRobustEstimator.MINIMUM_SIZE;
180 }
181
182 @Override
183 public void estimatePreliminarSolutions(final int[] samplesIndices, final List<DualQuadric> solutions) {
184 final var plane1 = planes.get(samplesIndices[0]);
185 final var plane2 = planes.get(samplesIndices[1]);
186 final var plane3 = planes.get(samplesIndices[2]);
187 final var plane4 = planes.get(samplesIndices[3]);
188 final var plane5 = planes.get(samplesIndices[4]);
189 final var plane6 = planes.get(samplesIndices[5]);
190 final var plane7 = planes.get(samplesIndices[6]);
191 final var plane8 = planes.get(samplesIndices[7]);
192 final var plane9 = planes.get(samplesIndices[8]);
193
194 try {
195 final var dualQuadric = new DualQuadric(plane1, plane2, plane3, plane4, plane5, plane6, plane7,
196 plane8, plane9);
197 solutions.add(dualQuadric);
198 } catch (final CoincidentPlanesException e) {
199 // if points are coincident, no solution is added
200 }
201 }
202
203 @Override
204 public double computeResidual(final DualQuadric currentEstimation, final int i) {
205 return residual(currentEstimation, planes.get(i));
206 }
207
208 @Override
209 public boolean isReady() {
210 return MSACDualQuadricRobustEstimator.this.isReady();
211 }
212
213 @Override
214 public void onEstimateStart(final RobustEstimator<DualQuadric> estimator) {
215 if (listener != null) {
216 listener.onEstimateStart(MSACDualQuadricRobustEstimator.this);
217 }
218 }
219
220 @Override
221 public void onEstimateEnd(final RobustEstimator<DualQuadric> estimator) {
222 if (listener != null) {
223 listener.onEstimateEnd(MSACDualQuadricRobustEstimator.this);
224 }
225 }
226
227 @Override
228 public void onEstimateNextIteration(final RobustEstimator<DualQuadric> estimator, final int iteration) {
229 if (listener != null) {
230 listener.onEstimateNextIteration(MSACDualQuadricRobustEstimator.this, iteration);
231 }
232 }
233
234 @Override
235 public void onEstimateProgressChange(final RobustEstimator<DualQuadric> estimator, final float progress) {
236 if (listener != null) {
237 listener.onEstimateProgressChange(MSACDualQuadricRobustEstimator.this, progress);
238 }
239 }
240 });
241
242 try {
243 locked = true;
244 innerEstimator.setConfidence(confidence);
245 innerEstimator.setMaxIterations(maxIterations);
246 innerEstimator.setProgressDelta(progressDelta);
247 return innerEstimator.estimate();
248 } catch (final com.irurueta.numerical.LockedException e) {
249 throw new LockedException(e);
250 } catch (final com.irurueta.numerical.NotReadyException e) {
251 throw new NotReadyException(e);
252 } finally {
253 locked = false;
254 }
255 }
256
257 /**
258 * Returns method being used for robust estimation.
259 *
260 * @return method being used for robust estimation.
261 */
262 @Override
263 public RobustEstimatorMethod getMethod() {
264 return RobustEstimatorMethod.MSAC;
265 }
266 }