View Javadoc
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.algebra.AlgebraException;
19  import com.irurueta.geometry.AffineTransformation3D;
20  import com.irurueta.geometry.CoincidentPlanesException;
21  import com.irurueta.geometry.Plane;
22  import com.irurueta.numerical.robust.MSACRobustEstimator;
23  import com.irurueta.numerical.robust.MSACRobustEstimatorListener;
24  import com.irurueta.numerical.robust.RobustEstimator;
25  import com.irurueta.numerical.robust.RobustEstimatorException;
26  import com.irurueta.numerical.robust.RobustEstimatorMethod;
27  
28  import java.util.List;
29  
30  /**
31   * Finds the best affine 3D transformation for provided collections of matched
32   * planes using MSAC algorithm.
33   */
34  @SuppressWarnings("DuplicatedCode")
35  public class MSACPlaneCorrespondenceAffineTransformation3DRobustEstimator
36          extends PlaneCorrespondenceAffineTransformation3DRobustEstimator {
37  
38      /**
39       * Constant defining default threshold to determine whether planes are
40       * inliers or not.
41       * Residuals to determine whether planes are inliers or not are computed by
42       * comparing two planes algebraically (e.g. doing the dot product of their
43       * parameters).
44       * A residual of 0 indicates that dot product was 1 or -1 and planes were
45       * equal.
46       * A residual of 1 indicates that dot product was 0 and planes were
47       * orthogonal.
48       * If dot product between planes is -1, then although their director vectors
49       * are opposed, planes are considered equal, since sign changes are not
50       * taken into account and their residuals will be 0.
51       */
52      public static final double DEFAULT_THRESHOLD = 1e-6;
53  
54      /**
55       * Minimum value that can be set as threshold.
56       * Threshold must be strictly greater than 0.0.
57       */
58      public static final double MIN_THRESHOLD = 0.0;
59  
60      /**
61       * Threshold to determine whether planes are inliers or not when testing
62       * possible estimation solutions.
63       * The threshold refers to the amount of error (i.e. distance and director
64       * vector angle difference) a possible solution has on a matched pair of
65       * planes.
66       */
67      private double threshold;
68  
69      /**
70       * Constructor.
71       */
72      public MSACPlaneCorrespondenceAffineTransformation3DRobustEstimator() {
73          super();
74          threshold = DEFAULT_THRESHOLD;
75      }
76  
77      /**
78       * Constructor with lists of planes to be used to estimate an affine 2D
79       * transformation.
80       * Planes in the list located at the same position are considered to be
81       * matched. Hence, both lists must have the same size, and their size must
82       * be greater or equal than MINIMUM_SIZE.
83       *
84       * @param inputPlanes  list of input planes to be used to estimate an affine
85       *                     3D transformation.
86       * @param outputPlanes list of output planes to be used to estimate an affine
87       *                     3D transformation.
88       * @throws IllegalArgumentException if provided lists of planes don't have
89       *                                  the same size or their size is smaller than MINIMUM_SIZE.
90       */
91      public MSACPlaneCorrespondenceAffineTransformation3DRobustEstimator(
92              final List<Plane> inputPlanes, final List<Plane> outputPlanes) {
93          super(inputPlanes, outputPlanes);
94          threshold = DEFAULT_THRESHOLD;
95      }
96  
97      /**
98       * Constructor.
99       *
100      * @param listener listener to be notified of events such as when estimation
101      *                 starts, ends or its progress significantly changes.
102      */
103     public MSACPlaneCorrespondenceAffineTransformation3DRobustEstimator(
104             final AffineTransformation3DRobustEstimatorListener listener) {
105         super(listener);
106         threshold = DEFAULT_THRESHOLD;
107     }
108 
109     /**
110      * Constructor with listener and lists of planes to be used to estimate an
111      * affine 3D transformation.
112      * Planes in the list located at the same position are considered to be
113      * matched. Hence, both lists must have the same size, and their size must
114      * be greater or equal than MINIMUM_SIZE.
115      *
116      * @param listener     listener to be notified of events such as when estimation
117      *                     starts, ends or its progress significantly changes.
118      * @param inputPlanes  list of input planes to be used to estimate an affine
119      *                     3D transformation.
120      * @param outputPlanes list of output planes to be used to estimate an
121      *                     affine 3D transformation.
122      * @throws IllegalArgumentException if provided lists of planes don't have
123      *                                  the same size or their size is smaller than MINIMUM_SIZE.
124      */
125     public MSACPlaneCorrespondenceAffineTransformation3DRobustEstimator(
126             final AffineTransformation3DRobustEstimatorListener listener,
127             final List<Plane> inputPlanes, final List<Plane> outputPlanes) {
128         super(listener, inputPlanes, outputPlanes);
129         threshold = DEFAULT_THRESHOLD;
130     }
131 
132     /**
133      * Returns threshold to determine whether planes are inliers or not when
134      * testing possible estimation solutions.
135      * Residuals to determine whether planes are inliers or not are computed by
136      * comparing two planes algebraically (e.g. doing the dot product of their
137      * parameters).
138      * A residual of 0 indicates that dot product was 1 or -1 and planes were
139      * equal.
140      * A residual of 1 indicates that dot product was 0 and planes were
141      * orthogonal.
142      * If dot product between lines is -1, then although their director vectors
143      * are opposed, planes are considered equal, since sign changes are not
144      * taken into account and their residuals will be 0.
145      *
146      * @return threshold to determine whether matched planes are inliers or not.
147      */
148     public double getThreshold() {
149         return threshold;
150     }
151 
152     /**
153      * Sets threshold to determine whether planes are inliers or not when
154      * testing possible estimation solutions.
155      * Residuals to determine whether planes are inliers or not are computed by
156      * comparing two planes algebraically (e.g. doing the dot product of their
157      * parameters).
158      * A residual of 0 indicates that dot product was 1 or -1 and planes were
159      * equal.
160      * A residual of 1 indicates that dot product was 0 and planes were
161      * orthogonal.
162      * If dot product between planes is -1, then although their director vectors
163      * are opposed, planes are considered equal, since sign changes are not
164      * taken into account and their residuals will be 0.
165      *
166      * @param threshold threshold to determine whether matched planes are
167      *                  inliers or not.
168      * @throws IllegalArgumentException if provided value is equal or less than
169      *                                  zero.
170      * @throws LockedException          if robust estimator is locked because an
171      *                                  estimation is already in progress.
172      */
173     public void setThreshold(final double threshold) throws LockedException {
174         if (isLocked()) {
175             throw new LockedException();
176         }
177         if (threshold <= MIN_THRESHOLD) {
178             throw new IllegalArgumentException();
179         }
180         this.threshold = threshold;
181     }
182 
183     /**
184      * Estimates an affine 2D transformation using a robust estimator and
185      * the best set of matched 2D planes correspondences found using the robust
186      * estimator.
187      *
188      * @return an affine 2D transformation.
189      * @throws LockedException          if robust estimator is locked because an
190      *                                  estimation is already in progress.
191      * @throws NotReadyException        if provided input data is not enough to start
192      *                                  the estimation.
193      * @throws RobustEstimatorException if estimation fails for any reason
194      *                                  (i.e. numerical instability, no solution available, etc).
195      */
196     @Override
197     public AffineTransformation3D estimate() throws LockedException, NotReadyException, RobustEstimatorException {
198         if (isLocked()) {
199             throw new LockedException();
200         }
201         if (!isReady()) {
202             throw new NotReadyException();
203         }
204 
205         final var innerEstimator = new MSACRobustEstimator<>(new MSACRobustEstimatorListener<AffineTransformation3D>() {
206 
207             // plane to be reused when computing residuals
208             private final Plane testPlane = new Plane();
209 
210             @Override
211             public double getThreshold() {
212                 return threshold;
213             }
214 
215             @Override
216             public int getTotalSamples() {
217                 return inputPlanes.size();
218             }
219 
220             @Override
221             public int getSubsetSize() {
222                 return AffineTransformation3DRobustEstimator.MINIMUM_SIZE;
223             }
224 
225             @Override
226             public void estimatePreliminarSolutions(
227                     final int[] samplesIndices, final List<AffineTransformation3D> solutions) {
228                 final var inputPlane1 = inputPlanes.get(samplesIndices[0]);
229                 final var inputPlane2 = inputPlanes.get(samplesIndices[1]);
230                 final var inputPlane3 = inputPlanes.get(samplesIndices[2]);
231                 final var inputPlane4 = inputPlanes.get(samplesIndices[3]);
232 
233                 final var outputPlane1 = outputPlanes.get(samplesIndices[0]);
234                 final var outputPlane2 = outputPlanes.get(samplesIndices[1]);
235                 final var outputPlane3 = outputPlanes.get(samplesIndices[2]);
236                 final var outputPlane4 = outputPlanes.get(samplesIndices[3]);
237 
238                 try {
239                     final var transformation = new AffineTransformation3D(inputPlane1, inputPlane2, inputPlane3,
240                             inputPlane4, outputPlane1, outputPlane2, outputPlane3, outputPlane4);
241                     solutions.add(transformation);
242                 } catch (final CoincidentPlanesException e) {
243                     // if lines are coincident, no solution is added
244                 }
245             }
246 
247             @Override
248             public double computeResidual(final AffineTransformation3D currentEstimation, final int i) {
249                 final var inputPlane = inputPlanes.get(i);
250                 final var outputPlane = outputPlanes.get(i);
251 
252                 // transform input line and store result in mTestLine
253                 try {
254                     currentEstimation.transform(inputPlane, testPlane);
255 
256                     return getResidual(outputPlane, testPlane);
257                 } catch (final AlgebraException e) {
258                     // this happens when internal matrix of affine transformation
259                     // cannot be reverse (i.e. transformation is not well-defined,
260                     // numerical instabilities, etc.)
261                     return Double.MAX_VALUE;
262                 }
263             }
264 
265             @Override
266             public boolean isReady() {
267                 return MSACPlaneCorrespondenceAffineTransformation3DRobustEstimator.this.isReady();
268             }
269 
270             @Override
271             public void onEstimateStart(final RobustEstimator<AffineTransformation3D> estimator) {
272                 if (listener != null) {
273                     listener.onEstimateStart(
274                             MSACPlaneCorrespondenceAffineTransformation3DRobustEstimator.this);
275                 }
276             }
277 
278             @Override
279             public void onEstimateEnd(final RobustEstimator<AffineTransformation3D> estimator) {
280                 if (listener != null) {
281                     listener.onEstimateEnd(MSACPlaneCorrespondenceAffineTransformation3DRobustEstimator.this);
282                 }
283             }
284 
285             @Override
286             public void onEstimateNextIteration(
287                     final RobustEstimator<AffineTransformation3D> estimator, final int iteration) {
288                 if (listener != null) {
289                     listener.onEstimateNextIteration(
290                             MSACPlaneCorrespondenceAffineTransformation3DRobustEstimator.this, iteration);
291                 }
292             }
293 
294             @Override
295             public void onEstimateProgressChange(
296                     final RobustEstimator<AffineTransformation3D> estimator, final float progress) {
297                 if (listener != null) {
298                     listener.onEstimateProgressChange(
299                             MSACPlaneCorrespondenceAffineTransformation3DRobustEstimator.this, progress);
300                 }
301             }
302         });
303 
304         try {
305             locked = true;
306             inliersData = null;
307             innerEstimator.setConfidence(confidence);
308             innerEstimator.setMaxIterations(maxIterations);
309             innerEstimator.setProgressDelta(progressDelta);
310             final var transformation = innerEstimator.estimate();
311             inliersData = innerEstimator.getInliersData();
312             return attemptRefine(transformation);
313         } catch (final com.irurueta.numerical.LockedException e) {
314             throw new LockedException(e);
315         } catch (final com.irurueta.numerical.NotReadyException e) {
316             throw new NotReadyException(e);
317         } finally {
318             locked = false;
319         }
320     }
321 
322     /**
323      * Returns method being used for robust estimation.
324      *
325      * @return method being used for robust estimation.
326      */
327     @Override
328     public RobustEstimatorMethod getMethod() {
329         return RobustEstimatorMethod.MSAC;
330     }
331 
332     /**
333      * Gets standard deviation used for Levenberg-Marquardt fitting during
334      * refinement.
335      * Returned value gives an indication of how much variance each residual
336      * has.
337      * Typically, this value is related to the threshold used on each robust
338      * estimation, since residuals of found inliers are within the range of
339      * such threshold.
340      *
341      * @return standard deviation used for refinement.
342      */
343     @Override
344     protected double getRefinementStandardDeviation() {
345         return threshold;
346     }
347 }