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 samples, their residuals and a collection of 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 <S> type of samples.
34 */
35 public abstract class SamplesAndInliersDataRefiner<T, S> extends InliersDataRefiner<T> {
36
37 /**
38 * Collection of samples.
39 */
40 protected List<S> samples;
41
42 /**
43 * Constructor.
44 */
45 protected SamplesAndInliersDataRefiner() {
46 }
47
48 /**
49 * Constructor.
50 *
51 * @param initialEstimation initial estimation to be set.
52 * @param keepCovariance true if covariance of estimation must be kept after
53 * refinement, false otherwise.
54 * @param inliers set indicating which of the provided matches are inliers.
55 * @param residuals residuals for matched samples.
56 * @param numInliers number of inliers on initial estimation.
57 * @param samples collection of samples.
58 */
59 protected SamplesAndInliersDataRefiner(
60 final T initialEstimation, final boolean keepCovariance, final BitSet inliers, final double[] residuals,
61 final int numInliers, final List<S> samples) {
62 super(initialEstimation, keepCovariance, inliers, residuals, numInliers);
63 this.samples = samples;
64 }
65
66 /**
67 * Constructor.
68 *
69 * @param initialEstimation initial estimation to be set.
70 * @param keepCovariance true if covariance of estimation must be kept after
71 * refinement, false otherwise.
72 * @param inliersData inlier data, typically obtained from a robust
73 * estimator.
74 * @param samples collection of samples.
75 */
76 protected SamplesAndInliersDataRefiner(
77 final T initialEstimation, final boolean keepCovariance, final InliersData inliersData,
78 final List<S> samples) {
79 super(initialEstimation, keepCovariance, inliersData);
80 this.samples = samples;
81 }
82
83 /**
84 * Gets collection of samples.
85 *
86 * @return collection of samples.
87 */
88 public List<S> getSamples() {
89 return samples;
90 }
91
92 /**
93 * Sets collection of samples.
94 *
95 * @param samples collection of samples.
96 * @throws LockedException if estimator is locked.
97 */
98 public void setSamples(final List<S> samples) throws LockedException {
99 if (isLocked()) {
100 throw new LockedException();
101 }
102 this.samples = samples;
103 }
104
105 /**
106 * Indicates whether this refiner is ready to start refinement computation.
107 *
108 * @return true if refiner is ready, false otherwise.
109 */
110 @Override
111 public boolean isReady() {
112 return initialEstimation != null && inliers != null && residuals != null && samples != null
113 && residuals.length == samples.size() && numInliers > 0;
114 }
115 }