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.estimators;
17
18 import com.irurueta.geometry.Line2D;
19 import com.irurueta.geometry.Plane;
20 import com.irurueta.numerical.robust.RobustEstimatorMethod;
21
22 import java.util.List;
23
24 /**
25 * Base abstract class for algorithms to robustly find the best pinhole camera
26 * for collections of matched planes and lines using DLT algorithm.
27 * Implementations of this class should be able to detect and discard outliers
28 * in order to find the best solution.
29 */
30 public abstract class DLTLinePlaneCorrespondencePinholeCameraRobustEstimator
31 extends LinePlaneCorrespondencePinholeCameraRobustEstimator {
32
33 /**
34 * Constructor.
35 */
36 protected DLTLinePlaneCorrespondencePinholeCameraRobustEstimator() {
37 super();
38 }
39
40 /**
41 * Constructor with lists of matched planes and 2D lines to estimate a
42 * pinhole camera.
43 * Points and lines in the lists located at the same position are considered
44 * to be matched. Hence, both lists must have the same size, and their size
45 * must be greater or equal than MIN_NUMBER_OF_LINE_PLANE_CORRESPONDENCES
46 * (4 matches).
47 *
48 * @param planes list of planes used to estimate a pinhole camera.
49 * @param lines list of corresponding projected 2D lines used to estimate
50 * a pinhole camera.
51 * @throws IllegalArgumentException if provided lists don't have the same
52 * size or their size is smaller than required minimum size (4 matches).
53 */
54 protected DLTLinePlaneCorrespondencePinholeCameraRobustEstimator(
55 final List<Plane> planes, final List<Line2D> lines) {
56 super(planes, lines);
57 }
58
59 /**
60 * Constructor with listener.
61 *
62 * @param listener listener to be notified of events such as when estimation
63 * starts, ends or its progress significantly changes.
64 */
65 protected DLTLinePlaneCorrespondencePinholeCameraRobustEstimator(
66 final PinholeCameraRobustEstimatorListener listener) {
67 super(listener);
68 }
69
70 /**
71 * Constructor with listener and lists of matched planes and 2D lines to
72 * estimate a pinhole camera.
73 * Points and lines in the lists located at the same position are considered
74 * to be matched. Hence, both lists must have the same size, and their size
75 * must be greater or equal than MIN_NUMBER_OF_LINE_PLANE_CORRESPONDENCES
76 * (4 matches).
77 *
78 * @param listener listener to be notified of events such as when estimation
79 * starts, ends or its progress significantly changes.
80 * @param planes list of planes used to estimate a pinhole camera.
81 * @param lines list of corresponding projected 2D lines used to estimate
82 * a pinhole camera.
83 * @throws IllegalArgumentException if provided lists don't have the same
84 * size or their size is smaller than required minimum size (4 matches).
85 */
86 protected DLTLinePlaneCorrespondencePinholeCameraRobustEstimator(
87 final PinholeCameraRobustEstimatorListener listener, final List<Plane> planes, final List<Line2D> lines) {
88 super(listener, planes, lines);
89 }
90
91 /**
92 * Creates a pinhole camera robust estimator based on plane/line
93 * correspondences and using provided robust estimator method.
94 *
95 * @param method method of a robust estimator algorithm to estimate the best
96 * pinhole camera.
97 * @return an instance of a pinhole camera robust estimator.
98 */
99 public static DLTLinePlaneCorrespondencePinholeCameraRobustEstimator create(final RobustEstimatorMethod method) {
100 return switch (method) {
101 case LMEDS -> new LMedSDLTLinePlaneCorrespondencePinholeCameraRobustEstimator();
102 case MSAC -> new MSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator();
103 case PROSAC -> new PROSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator();
104 case PROMEDS -> new PROMedSDLTLinePlaneCorrespondencePinholeCameraRobustEstimator();
105 default -> new RANSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator();
106 };
107 }
108
109 /**
110 * Creates a pinhole camera robust estimator based on plane/line
111 * correspondences and using provided planes and lines and provided robust
112 * estimator method.
113 *
114 * @param planes list of 3D planes to estimate a pinhole camera.
115 * @param lines list of corresponding projected 2D lines used to estimate a
116 * pinhole camera.
117 * @param method method of a robust estimator algorithm to estimate the best
118 * pinhole camera.
119 * @return an instance of a pinhole camera robust estimator.
120 * @throws IllegalArgumentException if provided lists of planes and lines
121 * don't have the same size or their size is smaller than required minimum
122 * size (4 correspondences).
123 */
124 public static DLTLinePlaneCorrespondencePinholeCameraRobustEstimator create(
125 final List<Plane> planes, final List<Line2D> lines, final RobustEstimatorMethod method) {
126 return switch (method) {
127 case LMEDS -> new LMedSDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(planes, lines);
128 case MSAC -> new MSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(planes, lines);
129 case PROSAC -> new PROSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(planes, lines);
130 case PROMEDS -> new PROMedSDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(planes, lines);
131 default -> new RANSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(planes, lines);
132 };
133 }
134
135 /**
136 * Creates a pinhole camera robust estimator based on plane/line
137 * correspondences and using provided listener and robust estimator method.
138 *
139 * @param listener listener to be notified of events such as when estimation
140 * starts, ends or its progress significantly changes.
141 * @param method method of a robust estimator algorithm to estimate the best
142 * pinhole camera.
143 * @return an instance of a pinhole camera robust estimator.
144 */
145 public static DLTLinePlaneCorrespondencePinholeCameraRobustEstimator create(
146 final PinholeCameraRobustEstimatorListener listener, final RobustEstimatorMethod method) {
147 return switch (method) {
148 case LMEDS -> new LMedSDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener);
149 case MSAC -> new MSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener);
150 case PROSAC -> new PROSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener);
151 case PROMEDS -> new PROMedSDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener);
152 default -> new RANSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener);
153 };
154 }
155
156 /**
157 * Creates a pinhole camera robust estimator based on plane/line
158 * correspondences and using provided listener, planes and lines, and robust
159 * estimator method.
160 *
161 * @param listener listener to be notified of events such as when estimation
162 * starts, ends or its progress significantly changes.
163 * @param planes list of 3D planes to estimate a pinhole camera.
164 * @param lines list of corresponding projected 2D lines used to estimate a
165 * pinhole camera.
166 * @param method method of a robust estimator algorithm to estimate the best
167 * pinhole camera.
168 * @return an instance of a pinhole camera robust estimator.
169 * @throws IllegalArgumentException if provided lists of planes and lines
170 * don't have the same size or their size is smaller than required minimum
171 * size (4 correspondences).
172 */
173 public static DLTLinePlaneCorrespondencePinholeCameraRobustEstimator create(
174 final PinholeCameraRobustEstimatorListener listener, final List<Plane> planes, final List<Line2D> lines,
175 final RobustEstimatorMethod method) {
176 return switch (method) {
177 case LMEDS -> new LMedSDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener, planes, lines);
178 case MSAC -> new MSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener, planes, lines);
179 case PROSAC -> new PROSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener, planes, lines);
180 case PROMEDS -> new PROMedSDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener, planes, lines);
181 default -> new RANSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener, planes, lines);
182 };
183 }
184
185 /**
186 * Creates a pinhole camera robust estimator based on plane/line
187 * correspondences and using provided quality scores and robust estimator
188 * method.
189 *
190 * @param qualityScores quality scores corresponding to each pair of matched
191 * planes/lines.
192 * @param method method of a robust estimator algorithm to estimate the best
193 * pinhole camera.
194 * @return an instance of a pinhole camera robust estimator.
195 * @throws IllegalArgumentException if provided quality scores length is
196 * smaller than required minimum size (4 samples).
197 */
198 public static DLTLinePlaneCorrespondencePinholeCameraRobustEstimator create(
199 final double[] qualityScores, final RobustEstimatorMethod method) {
200 return switch (method) {
201 case LMEDS -> new LMedSDLTLinePlaneCorrespondencePinholeCameraRobustEstimator();
202 case MSAC -> new MSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator();
203 case PROSAC -> new PROSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(qualityScores);
204 case PROMEDS -> new PROMedSDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(qualityScores);
205 default -> new RANSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator();
206 };
207 }
208
209 /**
210 * Creates a pinhole camera robust estimator based on plane/line
211 * correspondences and using provided planes and lines, quality scores and
212 * provided robust estimator method.
213 *
214 * @param planes list of 3D planes to estimate a pinhole camera.
215 * @param lines list of corresponding projected 2D lines used to estimate a
216 * pinhole camera.
217 * @param qualityScores quality scores corresponding to each pair of matched
218 * planes/lines.
219 * @param method method of a robust estimator algorithm to estimate the best
220 * pinhole camera.
221 * @return an instance of a pinhole camera robust estimator.
222 * @throws IllegalArgumentException if provided lists of planes and lines or
223 * quality scores don't have the same size of their size is smaller than
224 * required minimum size ($ correspondences).
225 */
226 public static DLTLinePlaneCorrespondencePinholeCameraRobustEstimator create(
227 final List<Plane> planes, final List<Line2D> lines, final double[] qualityScores,
228 final RobustEstimatorMethod method) {
229 return switch (method) {
230 case LMEDS -> new LMedSDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(planes, lines);
231 case MSAC -> new MSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(planes, lines);
232 case PROSAC -> new PROSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(
233 planes, lines, qualityScores);
234 case PROMEDS -> new PROMedSDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(
235 planes, lines, qualityScores);
236 default -> new RANSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(planes, lines);
237 };
238 }
239
240 /**
241 * Creates a pinhole camera robust estimator based on plane/list
242 * correspondences and using provided listener, quality scores and robust
243 * estimator method.
244 *
245 * @param listener listener to be notified of events such as when estimation
246 * starts, ends or its progress significantly changes.
247 * @param qualityScores quality scores corresponding to each pair of matched
248 * planes/lines.
249 * @param method method of a robust estimator algorithm to estimate the best
250 * pinhole camera.
251 * @return an instance of a pinhole camera robust estimator.
252 * @throws IllegalArgumentException if provided quality scores don't have
253 * the required minimum size (4 samples).
254 */
255 public static DLTLinePlaneCorrespondencePinholeCameraRobustEstimator create(
256 final PinholeCameraRobustEstimatorListener listener, final double[] qualityScores,
257 final RobustEstimatorMethod method) {
258 return switch (method) {
259 case LMEDS -> new LMedSDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener);
260 case MSAC -> new MSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener);
261 case PROSAC -> new PROSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener, qualityScores);
262 case PROMEDS -> new PROMedSDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener, qualityScores);
263 default -> new RANSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener);
264 };
265 }
266
267 /**
268 * Creates a pinhole camera robust estimator based on plane/line
269 * correspondences and using provided listener, planes and lines, and robust
270 * estimator method.
271 *
272 * @param listener listener to be notified of events such as when estimation
273 * starts, ends or its progress significantly changes.
274 * @param planes list of 3D planes to estimate a pinhole camera.
275 * @param lines list of corresponding projected 2D lines used to estimate
276 * a pinhole camera.
277 * @param qualityScores quality scores corresponding to each pair of matched
278 * planes/lines.
279 * @param method method of a robust estimator algorithm to estimate the best
280 * pinhole camera.
281 * @return an instance of a pinhole camera robust estimator.
282 * @throws IllegalArgumentException if provided lists of planes and lines or
283 * quality scores don't have the same size or their size is smaller than
284 * required minimum size (4 correspondences).
285 */
286 public static DLTLinePlaneCorrespondencePinholeCameraRobustEstimator create(
287 final PinholeCameraRobustEstimatorListener listener, final List<Plane> planes, final List<Line2D> lines,
288 final double[] qualityScores, final RobustEstimatorMethod method) {
289 return switch (method) {
290 case LMEDS -> new LMedSDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener, planes, lines);
291 case MSAC -> new MSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener, planes, lines);
292 case PROSAC -> new PROSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener, planes, lines,
293 qualityScores);
294 case PROMEDS -> new PROMedSDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener, planes, lines,
295 qualityScores);
296 default -> new RANSACDLTLinePlaneCorrespondencePinholeCameraRobustEstimator(listener, planes, lines);
297 };
298 }
299
300 /**
301 * Creates a pinhole camera robust estimator based on plane/line
302 * correspondences and using default robust estimator method.
303 *
304 * @return an instance of a pinhole camera robust estimator.
305 */
306 public static DLTLinePlaneCorrespondencePinholeCameraRobustEstimator create() {
307 return create(DEFAULT_ROBUST_METHOD);
308 }
309
310 /**
311 * Creates a pinhole camera robust estimator based on plane/line
312 * correspondences and using provided planes and lines and default robust
313 * estimator method.
314 *
315 * @param planes list of 3D planes to estimate a pinhole camera.
316 * @param lines list of corresponding projected 2D lines used to estimate
317 * a pinhole camera.
318 * @return an instance of a pinhole camera robust estimator.
319 * @throws IllegalArgumentException if provided lists of planes and lines
320 * don't have the same size or their size is smaller than required minimum
321 * size (4 correspondences).
322 */
323 public static DLTLinePlaneCorrespondencePinholeCameraRobustEstimator create(
324 final List<Plane> planes, final List<Line2D> lines) {
325 return create(planes, lines, DEFAULT_ROBUST_METHOD);
326 }
327
328 /**
329 * Creates a pinhole camera robust estimator based on plane/line
330 * correspondences and using provided listener and default estimator method.
331 *
332 * @param listener listener to be notified of events such as when estimation
333 * starts, ends or its progress significantly changes.
334 * @return an instance of a pinhole camera robust estimator.
335 */
336 public static DLTLinePlaneCorrespondencePinholeCameraRobustEstimator create(
337 final PinholeCameraRobustEstimatorListener listener) {
338 return create(listener, DEFAULT_ROBUST_METHOD);
339 }
340
341 /**
342 * Creates a pinhole camera robust estimator based on plane/line
343 * correspondences and using provided listener, planes and lines, and
344 * default robust estimator method.
345 *
346 * @param listener listener to be notified of events such as when estimation
347 * starts, ends or its progress significantly changes.
348 * @param planes list of 3D planes to estimate a pinhole camera.
349 * @param lines list of corresponding projected 2D lines used to estimate a
350 * pinhole camera.
351 * @return an instance of a pinhole camera robust estimator.
352 * @throws IllegalArgumentException if provided lists of planes and lines
353 * don't have the same size or their size is smaller than required minimum
354 * size (4 correspondences).
355 */
356 public static DLTLinePlaneCorrespondencePinholeCameraRobustEstimator create(
357 final PinholeCameraRobustEstimatorListener listener, final List<Plane> planes, final List<Line2D> lines) {
358 return create(listener, planes, lines, DEFAULT_ROBUST_METHOD);
359 }
360
361 /**
362 * Creates a pinhole camera robust estimator based on plane/line
363 * correspondences and using provided quality scores and default robust
364 * estimator method.
365 *
366 * @param qualityScores quality scores corresponding to each pair of matched
367 * planes/lines.
368 * @return an instance of a pinhole camera robust estimator.
369 * @throws IllegalArgumentException if provided quality scores length is
370 * smaller than required minimum size (4 samples).
371 */
372 public static DLTLinePlaneCorrespondencePinholeCameraRobustEstimator create(final double[] qualityScores) {
373 return create(qualityScores, DEFAULT_ROBUST_METHOD);
374 }
375
376 /**
377 * Creates a pinhole camera robust estimator based on plane/line
378 * correspondences and using provided planes and lines, quality scores and
379 * default robust estimator method.
380 *
381 * @param planes list of 3D planes to estimate a pinhole camera.
382 * @param lines list of corresponding projected 2D lines used to estimate
383 * a pinhole camera.
384 * @param qualityScores quality scores corresponding to each pair of matched
385 * planes/lines.
386 * @return an instance of a pinhole camera robust estimator.
387 * @throws IllegalArgumentException if provided lists of planes and lines or
388 * quality scores don't have the same size or their size is smaller than
389 * required minimum size (4 correspondences).
390 */
391 public static DLTLinePlaneCorrespondencePinholeCameraRobustEstimator create(
392 final List<Plane> planes, final List<Line2D> lines, final double[] qualityScores) {
393 return create(planes, lines, qualityScores, DEFAULT_ROBUST_METHOD);
394 }
395
396 /**
397 * Creates a pinhole camera robust estimator based on plane/line
398 * correspondences and using provided listener, quality scores and default
399 * robust estimator method.
400 *
401 * @param listener listener to be notified of events such as when estimation
402 * starts, ends or its progress significantly changes.
403 * @param qualityScores quality scores corresponding to each pair of matched
404 * planes/lines.
405 * @return an instance of a pinhole camera robust estimator.
406 * @throws IllegalArgumentException if provided quality scores don't have
407 * the required minimum size (4 samples).
408 */
409 public static DLTLinePlaneCorrespondencePinholeCameraRobustEstimator create(
410 final PinholeCameraRobustEstimatorListener listener, final double[] qualityScores) {
411 return create(listener, qualityScores, DEFAULT_ROBUST_METHOD);
412 }
413
414 /**
415 * Creates a pinhole camera robust estimator based on plane/line
416 * correspondences and using provided listener, planes and lines, and
417 * default robust estimator method.
418 *
419 * @param listener listener to be notified of events such as when estimation
420 * starts, ends or its progress significantly changes.
421 * @param planes list of 3D planes to estimate a pinhole camera.
422 * @param lines list of corresponding projected 2D lines used to estimate
423 * a pinhole camera.
424 * @param qualityScores quality scores corresponding to each pair of matched
425 * planes/lines.
426 * @return an instance of a pinhole camera robust estimator.
427 * @throws IllegalArgumentException if provided lists of planes and lines or
428 * quality scores don't have the same size or their size is smaller than
429 * required minimum size (4 correspondences).
430 */
431 public static DLTLinePlaneCorrespondencePinholeCameraRobustEstimator create(
432 final PinholeCameraRobustEstimatorListener listener, final List<Plane> planes, final List<Line2D> lines,
433 final double[] qualityScores) {
434 return create(listener, planes, lines, qualityScores, DEFAULT_ROBUST_METHOD);
435 }
436 }