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.ar.sfm;
17  
18  import com.irurueta.geometry.MetricTransformation3D;
19  import com.irurueta.geometry.Point3D;
20  
21  import java.util.ArrayList;
22  
23  /**
24   * Class in charge of estimating cameras and 3D reconstructed points from sparse
25   * image point correspondences in two views and known camera baseline (camera
26   * separation), so that both cameras and reconstructed points are obtained with
27   * exact scale.
28   */
29  public class KnownBaselineTwoViewsSparseReconstructor extends
30          BaseTwoViewsSparseReconstructor<KnownBaselineTwoViewsSparseReconstructorConfiguration,
31                  KnownBaselineTwoViewsSparseReconstructor, KnownBaselineTwoViewsSparseReconstructorListener> {
32  
33      /**
34       * Constructor.
35       *
36       * @param configuration configuration for this re-constructor.
37       * @param listener      listener in charge of handling events.
38       * @throws NullPointerException if listener or configuration is not
39       *                              provided.
40       */
41      public KnownBaselineTwoViewsSparseReconstructor(
42              final KnownBaselineTwoViewsSparseReconstructorConfiguration configuration,
43              final KnownBaselineTwoViewsSparseReconstructorListener listener) {
44          super(configuration, listener);
45      }
46  
47      /**
48       * Constructor with default configuration.
49       *
50       * @param listener listener in charge of handling events.
51       * @throws NullPointerException if listener is not provided.
52       */
53      public KnownBaselineTwoViewsSparseReconstructor(final KnownBaselineTwoViewsSparseReconstructorListener listener) {
54          this(new KnownBaselineTwoViewsSparseReconstructorConfiguration(), listener);
55      }
56  
57      /**
58       * Called when processing one frame is successfully finished. This can be done to estimate scale on
59       * those implementations where scale can be measured or is already known.
60       *
61       * @return true if post-processing succeeded, false otherwise.
62       */
63      @SuppressWarnings("DuplicatedCode")
64      @Override
65      protected boolean postProcessOne() {
66          try {
67              // reconstruction succeeded, so we update scale of cameras and
68              // reconstructed points
69              final var baseline = configuration.getBaseline();
70  
71              final var camera1 = estimatedCamera1.getCamera();
72              final var camera2 = estimatedCamera2.getCamera();
73  
74              camera1.decompose();
75              camera2.decompose();
76  
77              final var center1 = camera1.getCameraCenter();
78              final var center2 = camera2.getCameraCenter();
79  
80              final var estimatedBaseline = center1.distanceTo(center2);
81  
82              final var scale = baseline / estimatedBaseline;
83  
84              final var scaleTransformation = new MetricTransformation3D(scale);
85  
86              // update scale of cameras
87              scaleTransformation.transform(camera1);
88              scaleTransformation.transform(camera2);
89  
90              estimatedCamera1.setCamera(camera1);
91              estimatedCamera2.setCamera(camera2);
92  
93              // update scale of reconstructed points
94              final var numPoints = reconstructedPoints.size();
95              final var reconstructedPoints3D = new ArrayList<Point3D>();
96              for (final var reconstructedPoint : reconstructedPoints) {
97                  reconstructedPoints3D.add(reconstructedPoint.getPoint());
98              }
99  
100             scaleTransformation.transformAndOverwritePoints(reconstructedPoints3D);
101 
102             // set scaled points into result
103             for (var i = 0; i < numPoints; i++) {
104                 reconstructedPoints.get(i).setPoint(reconstructedPoints3D.get(i));
105             }
106 
107             return true;
108         } catch (final Exception e) {
109             failed = true;
110             listener.onFail(this);
111 
112             return false;
113         }
114     }
115 }