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.Point2D;
19  import com.irurueta.navigation.LockedException;
20  import com.irurueta.navigation.indoor.Fingerprint;
21  import com.irurueta.navigation.indoor.RadioSource;
22  import com.irurueta.navigation.indoor.RadioSourceLocated;
23  import com.irurueta.navigation.indoor.Reading;
24  import com.irurueta.navigation.lateration.MSACRobustLateration2DSolver;
25  import com.irurueta.numerical.robust.RobustEstimatorMethod;
26  
27  import java.util.List;
28  
29  /**
30   * Robustly estimates 2D position using located radio sources and their readings at
31   * unknown locations and using MSAC algorithm to discard outliers.
32   * This kind of estimator can be used to robustly determine the 2D position of a given
33   * device by getting readings at an unknown location of different radio
34   * sources whose 2D locations are known.
35   */
36  public class MSACRobustMixedPositionEstimator2D extends RobustMixedPositionEstimator2D {
37  
38      /**
39       * Constructor.
40       */
41      public MSACRobustMixedPositionEstimator2D() {
42          super();
43          init();
44      }
45  
46      /**
47       * Constructor.
48       *
49       * @param sources located radio sources used for lateration.
50       * @throws IllegalArgumentException if provided sources is null or the number of
51       *                                  provided sources is less than the required minimum.
52       */
53      public MSACRobustMixedPositionEstimator2D(final List<? extends RadioSourceLocated<Point2D>> sources) {
54          super();
55          init();
56          internalSetSources(sources);
57      }
58  
59      /**
60       * Constructor.
61       *
62       * @param fingerprint fingerprint containing readings at an unknown
63       *                    location for provided located radio sources.
64       * @throws IllegalArgumentException if provided fingerprint is null.
65       */
66      public MSACRobustMixedPositionEstimator2D(
67              final Fingerprint<? extends RadioSource, ? extends Reading<? extends RadioSource>> fingerprint) {
68          super();
69          init();
70          internalSetFingerprint(fingerprint);
71      }
72  
73      /**
74       * Constructor
75       *
76       * @param sources     located radio sources used for lateration.
77       * @param fingerprint fingerprint containing readings at an unknown
78       *                    location for provided located radio sources.
79       * @throws IllegalArgumentException if either provided sources or fingerprint is null
80       *                                  or the number of provided sources is less than the required minimum.
81       */
82      public MSACRobustMixedPositionEstimator2D(
83              final List<? extends RadioSourceLocated<Point2D>> sources,
84              final Fingerprint<? extends RadioSource, ? extends Reading<? extends RadioSource>> fingerprint) {
85          super();
86          init();
87          internalSetSources(sources);
88          internalSetFingerprint(fingerprint);
89      }
90  
91      /**
92       * Constructor.
93       *
94       * @param listener listener in charge of handling events.
95       */
96      public MSACRobustMixedPositionEstimator2D(final RobustMixedPositionEstimatorListener<Point2D> listener) {
97          super(listener);
98          init();
99      }
100 
101     /**
102      * Constructor.
103      *
104      * @param sources  located radio sources used for lateration.
105      * @param listener listener in charge of handling events.
106      * @throws IllegalArgumentException if provided sources is null or the number of
107      *                                  provided sources is less than the required minimum.
108      */
109     public MSACRobustMixedPositionEstimator2D(
110             final List<? extends RadioSourceLocated<Point2D>> sources,
111             final RobustMixedPositionEstimatorListener<Point2D> listener) {
112         super(listener);
113         init();
114         internalSetSources(sources);
115     }
116 
117     /**
118      * Constructor.
119      *
120      * @param fingerprint fingerprint containing readings at an unknown
121      *                    location for provided location radio sources.
122      * @param listener    listener in charge of handling events.
123      * @throws IllegalArgumentException if provided fingerprint is null.
124      */
125     public MSACRobustMixedPositionEstimator2D(
126             final Fingerprint<? extends RadioSource, ? extends Reading<? extends RadioSource>> fingerprint,
127             final RobustMixedPositionEstimatorListener<Point2D> listener) {
128         super(listener);
129         init();
130         internalSetFingerprint(fingerprint);
131     }
132 
133     /**
134      * Constructor.
135      *
136      * @param sources     located radio sources used for lateration.
137      * @param fingerprint fingerprint containing readings at an unknown
138      *                    location for provided located radio sources.
139      * @param listener    listener in charge of handling events.
140      */
141     public MSACRobustMixedPositionEstimator2D(
142             final List<? extends RadioSourceLocated<Point2D>> sources,
143             final Fingerprint<? extends RadioSource, ? extends Reading<? extends RadioSource>> fingerprint,
144             final RobustMixedPositionEstimatorListener<Point2D> listener) {
145         super(listener);
146         init();
147         internalSetSources(sources);
148         internalSetFingerprint(fingerprint);
149     }
150 
151     /**
152      * Gets threshold to determine whether samples are inliers or not when testing possible solutions.
153      * The threshold refers to the amount of error on distance between estimated position and distances
154      * provided for each sample.
155      *
156      * @return threshold to determine whether samples are inliers or not.
157      */
158     public double getThreshold() {
159         return ((MSACRobustLateration2DSolver) laterationSolver).getThreshold();
160     }
161 
162     /**
163      * Sets threshold to determine whether samples are inliers or not when testing possible solutions.
164      * The threshold refers to the amount of error on distance between estimated position and distances
165      * provided for each sample.
166      *
167      * @param threshold threshold to determine whether samples are inliers or not.
168      * @throws IllegalArgumentException if provided value is equal or less than zero.
169      * @throws LockedException          if this solver is locked.
170      */
171     public void setThreshold(final double threshold) throws LockedException {
172         ((MSACRobustLateration2DSolver) laterationSolver).setThreshold(threshold);
173     }
174 
175     /**
176      * Returns method being used for robust estimation.
177      *
178      * @return method being used for robust estimation.
179      */
180     @Override
181     public RobustEstimatorMethod getMethod() {
182         return RobustEstimatorMethod.MSAC;
183     }
184 
185     /**
186      * Initializes robust lateration solver.
187      */
188     private void init() {
189         laterationSolver = new MSACRobustLateration2DSolver(trilaterationSolverListener);
190     }
191 }
192