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  
17  package com.irurueta.ar.sfm;
18  
19  import com.irurueta.algebra.AlgebraException;
20  import com.irurueta.geometry.MetricTransformation3D;
21  import com.irurueta.geometry.Point3D;
22  
23  import java.util.ArrayList;
24  
25  /**
26   * Class in charge of estimating pairs of cameras and 3D reconstruction points from
27   * sparse image point correspondences.
28   */
29  public class PairedViewsSparseReconstructor extends BasePairedViewsSparseReconstructor<
30          PairedViewsSparseReconstructorConfiguration, PairedViewsSparseReconstructor,
31          PairedViewsSparseReconstructorListener> {
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 provided.
39       */
40      public PairedViewsSparseReconstructor(
41              final PairedViewsSparseReconstructorConfiguration configuration,
42              final PairedViewsSparseReconstructorListener listener) {
43          super(configuration, listener);
44      }
45  
46      /**
47       * Constructor with default configuration.
48       *
49       * @param listener listener in charge of handling events.
50       * @throws NullPointerException if listener or configuration is not provided.
51       */
52      public PairedViewsSparseReconstructor(final PairedViewsSparseReconstructorListener listener) {
53          this(new PairedViewsSparseReconstructorConfiguration(), listener);
54      }
55  
56      /**
57       * Indicates whether implementations of a re-constructor uses absolute orientation or
58       * not.
59       *
60       * @return true if absolute orientation is used, false, otherwise.
61       */
62      @Override
63      protected boolean hasAbsoluteOrientation() {
64          return false;
65      }
66  
67      /**
68       * Transforms metric cameras on current pair of views so that they are referred to
69       * last kept location and rotation.
70       *
71       * @param isInitialPairOfViews   true if initial pair of views is being processed, false otherwise.
72       * @param hasAbsoluteOrientation true if absolute orientation is required, false otherwise.
73       * @return true if cameras were successfully transformed.
74       */
75      @SuppressWarnings("DuplicatedCode")
76      @Override
77      protected boolean transformPairOfCamerasAndPoints(
78              final boolean isInitialPairOfViews, final boolean hasAbsoluteOrientation) {
79          final var previousMetricCamera = previousMetricEstimatedCamera.getCamera();
80          final var currentMetricCamera = currentMetricEstimatedCamera.getCamera();
81          if (previousMetricCamera == null || currentMetricCamera == null) {
82              return false;
83          }
84  
85          currentScale = listener.onBaselineRequested(this, previousViewId, currentViewId,
86                  previousMetricEstimatedCamera, currentMetricEstimatedCamera);
87          final var sqrScale = currentScale * currentScale;
88  
89          final var scaleTransformation = new MetricTransformation3D(currentScale);
90  
91          if (isInitialPairOfViews) {
92              // the first pair of views does not require setting translation and rotation
93              referenceEuclideanTransformation = scaleTransformation;
94          } else {
95              // additional pairs also need to translate and rotate
96              final var invRot = lastEuclideanCameraRotation.inverseRotationAndReturnNew();
97              final var translation = new double[Point3D.POINT3D_INHOMOGENEOUS_COORDINATES_LENGTH];
98              translation[0] = lastEuclideanCameraCenter.getInhomX();
99              translation[1] = lastEuclideanCameraCenter.getInhomY();
100             translation[2] = lastEuclideanCameraCenter.getInhomZ();
101             referenceEuclideanTransformation = scaleTransformation.combineAndReturnNew(
102                     new MetricTransformation3D(invRot, translation, 1.0));
103             referenceEuclideanTransformation.setRotation(invRot);
104             referenceEuclideanTransformation.setTranslation(lastEuclideanCameraCenter);
105         }
106 
107         try {
108             // transform cameras
109             final var previousEuclideanCamera = referenceEuclideanTransformation.transformAndReturnNew(
110                     previousMetricCamera);
111             final var currentEuclideanCamera = referenceEuclideanTransformation.transformAndReturnNew(
112                     currentMetricCamera);
113 
114             previousEuclideanEstimatedCamera = new EstimatedCamera();
115             previousEuclideanEstimatedCamera.setCamera(previousEuclideanCamera);
116             previousEuclideanEstimatedCamera.setViewId(previousMetricEstimatedCamera.getViewId());
117             previousEuclideanEstimatedCamera.setQualityScore(previousMetricEstimatedCamera.getQualityScore());
118             if (previousMetricEstimatedCamera.getCovariance() != null) {
119                 previousEuclideanEstimatedCamera.setCovariance(
120                         previousMetricEstimatedCamera.getCovariance().multiplyByScalarAndReturnNew(sqrScale));
121             }
122 
123             currentEuclideanEstimatedCamera = new EstimatedCamera();
124             currentEuclideanEstimatedCamera.setCamera(currentEuclideanCamera);
125             currentEuclideanEstimatedCamera.setViewId(currentMetricEstimatedCamera.getViewId());
126             currentEuclideanEstimatedCamera.setQualityScore(currentMetricEstimatedCamera.getQualityScore());
127             if (currentMetricEstimatedCamera.getCovariance() != null) {
128                 currentEuclideanEstimatedCamera.setCovariance(
129                         currentMetricEstimatedCamera.getCovariance().multiplyByScalarAndReturnNew(sqrScale));
130             }
131 
132             // transform points
133             euclideanReconstructedPoints = new ArrayList<>();
134             for (final var metricReconstructedPoint : metricReconstructedPoints) {
135                 final var metricPoint = metricReconstructedPoint.getPoint();
136                 final var euclideanPoint = referenceEuclideanTransformation.transformAndReturnNew(metricPoint);
137                 final var euclideanReconstructedPoint = new ReconstructedPoint3D();
138                 euclideanReconstructedPoint.setPoint(euclideanPoint);
139                 euclideanReconstructedPoint.setInlier(metricReconstructedPoint.isInlier());
140                 euclideanReconstructedPoint.setId(metricReconstructedPoint.getId());
141                 euclideanReconstructedPoint.setColorData(metricReconstructedPoint.getColorData());
142                 if (metricReconstructedPoint.getCovariance() != null) {
143                     euclideanReconstructedPoint.setCovariance(
144                             metricReconstructedPoint.getCovariance().multiplyByScalarAndReturnNew(sqrScale));
145                 }
146                 euclideanReconstructedPoint.setQualityScore(metricReconstructedPoint.getQualityScore());
147                 euclideanReconstructedPoints.add(euclideanReconstructedPoint);
148             }
149 
150         } catch (final AlgebraException e) {
151             return false;
152         }
153 
154         return super.transformPairOfCamerasAndPoints(isInitialPairOfViews, hasAbsoluteOrientation);
155     }
156 }