View Javadoc
1   /*
2    * Copyright (C) 2019 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.navigation.indoor.position;
17  
18  import com.irurueta.geometry.Point;
19  import com.irurueta.navigation.LockedException;
20  import com.irurueta.navigation.NotReadyException;
21  import com.irurueta.navigation.indoor.Fingerprint;
22  import com.irurueta.navigation.indoor.RadioSource;
23  import com.irurueta.navigation.indoor.RadioSourceLocated;
24  import com.irurueta.navigation.indoor.Reading;
25  import com.irurueta.navigation.lateration.HomogeneousLinearLeastSquaresLaterationSolver;
26  import com.irurueta.navigation.lateration.InhomogeneousLinearLeastSquaresLaterationSolver;
27  import com.irurueta.navigation.lateration.LaterationException;
28  import com.irurueta.navigation.lateration.LaterationSolver;
29  import com.irurueta.navigation.lateration.LaterationSolverListener;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * Linearly estimates position using located radio sources and any kind of readings at
36   * unknown locations.
37   * These kind of estimators can be used to determine the position of a given device by
38   * getting any kind of readings at an unknown location of different radio sources whose
39   * locations are known.
40   *
41   * @param <P> a {@link Point} type.
42   */
43  public abstract class LinearMixedPositionEstimator<P extends Point<P>> extends MixedPositionEstimator<P> {
44  
45      /**
46       * Indicates that by default an homogeneous linear solver is used to estimate
47       * position.
48       */
49      public static final boolean DEFAULT_USE_HOMOGENEOUS_LINEAR_SOLVER = true;
50  
51      /**
52       * An homogeneous linear lateration solver to solve position.
53       */
54      protected HomogeneousLinearLeastSquaresLaterationSolver<P> homogeneousTrilaterationSolver;
55  
56      /**
57       * An inhomogeneous linear lateration solver to solve position.
58       */
59      protected InhomogeneousLinearLeastSquaresLaterationSolver<P> inhomogeneousTrilaterationSolver;
60  
61      /**
62       * Listener for the lateration solver.
63       */
64      protected LaterationSolverListener<P> laterationSolverListener;
65  
66      /**
67       * Indicates whether an homogeneous linear solver is used to estimate position.
68       */
69      protected boolean useHomogeneousLinearSolver = DEFAULT_USE_HOMOGENEOUS_LINEAR_SOLVER;
70  
71      /**
72       * Constructor.
73       */
74      protected LinearMixedPositionEstimator() {
75          super();
76          init();
77      }
78  
79      /**
80       * Constructor.
81       *
82       * @param listener listener in charge of handling events.
83       */
84      protected LinearMixedPositionEstimator(final MixedPositionEstimatorListener<P> listener) {
85          super(listener);
86          init();
87      }
88  
89      /**
90       * Indicates whether an homogeneous linear solver is used to estimate position.
91       *
92       * @return true if homogeneous linear solver is used, false if an inhomogeneous
93       * linear one is used instead.
94       */
95      public boolean isHomogeneousLinearSolverUsed() {
96          return useHomogeneousLinearSolver;
97      }
98  
99      /**
100      * Specifies whether an homogeneous linear solver is used to estimate position.
101      *
102      * @param useHomogeneousLinearSolver true if homogeneous linear solver is used, false
103      *                                   if an inhomogeneous linear one is used instead.
104      * @throws LockedException if estimator is locked.
105      */
106     public void setHomogeneousLinearSolverUsed(final boolean useHomogeneousLinearSolver) throws LockedException {
107         if (isLocked()) {
108             throw new LockedException();
109         }
110 
111         this.useHomogeneousLinearSolver = useHomogeneousLinearSolver;
112     }
113 
114     /**
115      * Gets minimum required number of located radio sources to perform lateration.
116      *
117      * @return minimum required number of located radio sources to perform lateration.
118      */
119     @Override
120     public int getMinRequiredSources() {
121         return useHomogeneousLinearSolver
122                 ? homogeneousTrilaterationSolver.getMinRequiredPositionsAndDistances()
123                 : inhomogeneousTrilaterationSolver.getMinRequiredPositionsAndDistances();
124     }
125 
126     /**
127      * Indicates whether estimator is ready to find a solution.
128      *
129      * @return true if estimator is ready, false otherwise.
130      */
131     @Override
132     public boolean isReady() {
133         return (!useHomogeneousLinearSolver && inhomogeneousTrilaterationSolver.isReady())
134                 || (useHomogeneousLinearSolver && homogeneousTrilaterationSolver.isReady());
135     }
136 
137     /**
138      * Returns boolean indicating whether this estimator is locked because an estimation is already in progress.
139      *
140      * @return true if estimator is locked, false otherwise.
141      */
142     @Override
143     public boolean isLocked() {
144         return inhomogeneousTrilaterationSolver.isLocked() || homogeneousTrilaterationSolver.isLocked();
145     }
146 
147     /**
148      * Estimates position based on provided located radio sources and RSSI readings of
149      * such radio sources at an unknown location.
150      *
151      * @throws LockedException             if estimator is locked.
152      * @throws NotReadyException           if estimator is not ready.
153      * @throws PositionEstimationException if estimation fails for some other reason.
154      */
155     @SuppressWarnings("Duplicates")
156     @Override
157     public void estimate() throws LockedException, NotReadyException, PositionEstimationException {
158         try {
159             if (useHomogeneousLinearSolver) {
160                 homogeneousTrilaterationSolver.solve();
161                 estimatedPositionCoordinates = homogeneousTrilaterationSolver.getEstimatedPositionCoordinates();
162             } else {
163                 inhomogeneousTrilaterationSolver.solve();
164                 estimatedPositionCoordinates = inhomogeneousTrilaterationSolver.getEstimatedPositionCoordinates();
165             }
166         } catch (LaterationException e) {
167             throw new PositionEstimationException(e);
168         }
169     }
170 
171     /**
172      * Gets known positions of radio sources used internally to solve lateration.
173      *
174      * @return known positions used internally.
175      */
176     @Override
177     public P[] getPositions() {
178         return useHomogeneousLinearSolver
179                 ? homogeneousTrilaterationSolver.getPositions() : inhomogeneousTrilaterationSolver.getPositions();
180     }
181 
182     /**
183      * Gets Euclidean distances from known located radio sources to
184      * the location of provided readings in a fingerprint.
185      * Distance values are used internally to solve lateration.
186      *
187      * @return Euclidean distances used internally.
188      */
189     @Override
190     public double[] getDistances() {
191         return useHomogeneousLinearSolver
192                 ? homogeneousTrilaterationSolver.getDistances() : inhomogeneousTrilaterationSolver.getDistances();
193     }
194 
195     /**
196      * Internally sets located radio sources used for lateration.
197      *
198      * @param sources located radio sources used for lateration.
199      * @throws IllegalArgumentException if provided value is null or the number of
200      *                                  provided sources is less than the required
201      *                                  minimum.
202      */
203     @Override
204     protected void internalSetSources(final List<? extends RadioSourceLocated<P>> sources) {
205         super.internalSetSources(sources);
206         buildPositionsAndDistances();
207     }
208 
209     /**
210      * Internally sets fingerprint containing readings at an unknown location for provided
211      * located radio sources.
212      *
213      * @param fingerprint fingerprint containing readings at an unknown location for
214      *                    provided located radio sources.
215      * @throws IllegalArgumentException if provided value is null.
216      */
217     @Override
218     protected void internalSetFingerprint(
219             final Fingerprint<? extends RadioSource, ? extends Reading<? extends RadioSource>> fingerprint) {
220         super.internalSetFingerprint(fingerprint);
221         buildPositionsAndDistances();
222     }
223 
224     /**
225      * Sets positions and distances on internal lateration solver.
226      *
227      * @param positions positions to be set.
228      * @param distances distances to be set.
229      */
230     protected abstract void setPositionsAndDistances(final List<P> positions, final List<Double> distances);
231 
232     /**
233      * Initializes lateration solver listener.
234      */
235     @SuppressWarnings("Duplicates")
236     private void init() {
237         laterationSolverListener = new LaterationSolverListener<>() {
238             @Override
239             public void onSolveStart(final LaterationSolver<P> solver) {
240                 if (listener != null) {
241                     listener.onEstimateStart(LinearMixedPositionEstimator.this);
242                 }
243             }
244 
245             @Override
246             public void onSolveEnd(final LaterationSolver<P> solver) {
247                 if (listener != null) {
248                     listener.onEstimateEnd(LinearMixedPositionEstimator.this);
249                 }
250             }
251         };
252     }
253 
254     /**
255      * Builds positions and distances for the internal lateration solver.
256      */
257     @SuppressWarnings("Duplicates")
258     private void buildPositionsAndDistances() {
259         if ((useHomogeneousLinearSolver && homogeneousTrilaterationSolver == null)
260                 || (!useHomogeneousLinearSolver && inhomogeneousTrilaterationSolver == null)) {
261             return;
262         }
263 
264         final var min = getMinRequiredSources();
265         if (sources == null || fingerprint == null || sources.size() < min || fingerprint.getReadings() == null
266                 || fingerprint.getReadings().size() < min) {
267             return;
268         }
269 
270         final var positions = new ArrayList<P>();
271         final var distances = new ArrayList<Double>();
272         PositionEstimatorHelper.buildPositionsAndDistances(sources, fingerprint, positions, distances);
273 
274         setPositionsAndDistances(positions, distances);
275     }
276 }