View Javadoc
1   /*
2    * Copyright (C) 2017 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.refiners;
17  
18  import com.irurueta.geometry.estimators.LockedException;
19  import com.irurueta.numerical.robust.InliersData;
20  
21  import java.util.BitSet;
22  import java.util.List;
23  
24  /**
25   * Refines an instance of type T by taking into account an initial estimation,
26   * inlier matches, their residuals and pairs of matches samples.
27   * This class can be used to find a solution that minimizes error of inliers in
28   * LMSE terms.
29   * Typically, a refiner is used by a robust estimator, however it can also be
30   * useful in some other situations.
31   *
32   * @param <T>  type of instance to be refined.
33   * @param <S1> type of matched samples in 1st set.
34   * @param <S2> type of matched samples in 2nd set.
35   */
36  public abstract class PairMatchesAndInliersDataRefiner<T, S1, S2> extends InliersDataRefiner<T> {
37  
38      /**
39       * 1st set of paired samples.
40       */
41      protected List<S1> samples1;
42  
43      /**
44       * 2nd set of paired samples.
45       */
46      protected List<S2> samples2;
47  
48      /**
49       * Constructor.
50       */
51      protected PairMatchesAndInliersDataRefiner() {
52      }
53  
54      /**
55       * Constructor.
56       *
57       * @param initialEstimation initial estimation to be set.
58       * @param keepCovariance    true if covariance of estimation must be kept after
59       *                          refinement, false otherwise.
60       * @param inliers           set indicating which of the provided matches are inliers.
61       * @param residuals         residuals for matched samples.
62       * @param numInliers        number of inliers on initial estimation.
63       * @param samples1          1st set of paired samples.
64       * @param samples2          2nd set of paired samples.
65       */
66      protected PairMatchesAndInliersDataRefiner(
67              final T initialEstimation, final boolean keepCovariance, final BitSet inliers,
68              final double[] residuals, final int numInliers, final List<S1> samples1, final List<S2> samples2) {
69          super(initialEstimation, keepCovariance, inliers, residuals, numInliers);
70          this.samples1 = samples1;
71          this.samples2 = samples2;
72      }
73  
74      /**
75       * Constructor.
76       *
77       * @param initialEstimation initial estimation to be set.
78       * @param keepCovariance    true if covariance of estimation must be kept after
79       *                          refinement, false otherwise.
80       * @param inliersData       inlier data, typically obtained from a robust
81       *                          estimator.
82       * @param samples1          1st set of paired samples.
83       * @param samples2          2nd set of paired samples.
84       */
85      protected PairMatchesAndInliersDataRefiner(
86              final T initialEstimation, final boolean keepCovariance, final InliersData inliersData,
87              final List<S1> samples1, final List<S2> samples2) {
88          super(initialEstimation, keepCovariance, inliersData);
89          this.samples1 = samples1;
90          this.samples2 = samples2;
91      }
92  
93      /**
94       * Gets 1st set of paired samples.
95       *
96       * @return 1st set of paired samples.
97       */
98      public List<S1> getSamples1() {
99          return samples1;
100     }
101 
102     /**
103      * Sets 1st set of paired samples.
104      *
105      * @param samples1 1st set of paired samples.
106      * @throws LockedException if estimator is locked.
107      */
108     public void setSamples1(final List<S1> samples1) throws LockedException {
109         if (isLocked()) {
110             throw new LockedException();
111         }
112         this.samples1 = samples1;
113     }
114 
115     /**
116      * Gets 2nd set of paired samples.
117      *
118      * @return 2nd set of paired samples.
119      */
120     public List<S2> getSamples2() {
121         return samples2;
122     }
123 
124     /**
125      * Sets 2nd set of paired samples.
126      *
127      * @param samples2 2nd set of paired samples.
128      * @throws LockedException if estimator is locked.
129      */
130     public void setSamples2(final List<S2> samples2) throws LockedException {
131         if (isLocked()) {
132             throw new LockedException();
133         }
134         this.samples2 = samples2;
135     }
136 
137     /**
138      * Indicates whether this refiner is ready to start refinement computation.
139      *
140      * @return true if refiner is ready, false otherwise.
141      */
142     @Override
143     public boolean isReady() {
144         return initialEstimation != null && inliers != null && residuals != null && samples1 != null
145                 && samples2 != null && residuals.length == samples1.size() && samples1.size() == samples2.size()
146                 && numInliers > 0;
147     }
148 }