View Javadoc
1   /*
2    * Copyright (C) 2020 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.inertial.calibration;
17  
18  import com.irurueta.navigation.inertial.BodyMagneticFluxDensity;
19  
20  import java.io.Serial;
21  import java.io.Serializable;
22  import java.util.Objects;
23  
24  /**
25   * Contains a body magnetic flux density along with its
26   * corresponding standard deviation.
27   */
28  public class StandardDeviationBodyMagneticFluxDensity implements Serializable, Cloneable {
29  
30      /**
31       * Serialization version. This is used to ensure compatibility of deserialization of permanently stored serialized
32       * instances.
33       */
34      @Serial
35      private static final long serialVersionUID = 0L;
36  
37      /**
38       * Current body magnetic flux density. Contains magnetometer measurements.
39       */
40      private BodyMagneticFluxDensity magneticFluxDensity;
41  
42      /**
43       * Standard deviation of measured magnetic flux density expressed in Teslas
44       * (T).
45       */
46      private double magneticFluxDensityStandardDeviation;
47  
48      /**
49       * Constructor.
50       */
51      public StandardDeviationBodyMagneticFluxDensity() {
52      }
53  
54      /**
55       * Constructor.
56       *
57       * @param magneticFluxDensity current body magnetic flux density.
58       */
59      public StandardDeviationBodyMagneticFluxDensity(final BodyMagneticFluxDensity magneticFluxDensity) {
60          this.magneticFluxDensity = magneticFluxDensity;
61      }
62  
63      /**
64       * Constructor.
65       *
66       * @param magneticFluxDensityStandardDeviation standard deviation of measured
67       *                                             magnetic flux density.
68       * @throws IllegalArgumentException if provided standard deviation is
69       *                                  negative.
70       */
71      public StandardDeviationBodyMagneticFluxDensity(final double magneticFluxDensityStandardDeviation) {
72          setMagneticFluxDensityStandardDeviation(magneticFluxDensityStandardDeviation);
73      }
74  
75      /**
76       * Constructor.
77       *
78       * @param magneticFluxDensity                  current body magnetic flux
79       *                                             density.
80       * @param magneticFluxDensityStandardDeviation standard deviation of measured
81       *                                             magnetic flux density.
82       * @throws IllegalArgumentException if provided standard deviation is
83       *                                  negative.
84       */
85      public StandardDeviationBodyMagneticFluxDensity(
86              final BodyMagneticFluxDensity magneticFluxDensity, final double magneticFluxDensityStandardDeviation) {
87          this(magneticFluxDensityStandardDeviation);
88          this.magneticFluxDensity = magneticFluxDensity;
89      }
90  
91      /**
92       * Constructor.
93       *
94       * @param input instance to copy data from.
95       */
96      public StandardDeviationBodyMagneticFluxDensity(final StandardDeviationBodyMagneticFluxDensity input) {
97          copyFrom(input);
98      }
99  
100     /**
101      * Gets current body magnetic flux density. Contains magnetometer
102      * measurements.
103      *
104      * @return current body magnetic flux density.
105      */
106     public BodyMagneticFluxDensity getMagneticFluxDensity() {
107         return magneticFluxDensity;
108     }
109 
110     /**
111      * Sets current body magnetic flux density. Contains magnetometer
112      * measurements.
113      *
114      * @param magneticFluxDensity current body magnetic flux density.
115      */
116     public void setMagneticFluxDensity(final BodyMagneticFluxDensity magneticFluxDensity) {
117         this.magneticFluxDensity = magneticFluxDensity;
118     }
119 
120     /**
121      * Gets standard deviation of measured magnetic flux density expressed in
122      * Teslas (T).
123      *
124      * @return standard deviation of measured magnetic flux density.
125      */
126     public double getMagneticFluxDensityStandardDeviation() {
127         return magneticFluxDensityStandardDeviation;
128     }
129 
130     /**
131      * Sets standard deviation of measured magnetic flux density expressed in
132      * Teslas (T).
133      *
134      * @param magneticFluxDensityStandardDeviation standard deviation of
135      *                                             measured magnetic flux
136      *                                             density.
137      * @throws IllegalArgumentException if provided value is negative.
138      */
139     public void setMagneticFluxDensityStandardDeviation(final double magneticFluxDensityStandardDeviation) {
140         if (magneticFluxDensityStandardDeviation < 0.0) {
141             throw new IllegalArgumentException();
142         }
143 
144         this.magneticFluxDensityStandardDeviation = magneticFluxDensityStandardDeviation;
145     }
146 
147     /**
148      * Copies data of provided instance into this instance.
149      *
150      * @param input instance to copy data from.
151      */
152     public void copyFrom(final StandardDeviationBodyMagneticFluxDensity input) {
153         if (input.magneticFluxDensity != null) {
154             if (magneticFluxDensity == null) {
155                 magneticFluxDensity = new BodyMagneticFluxDensity(input.magneticFluxDensity);
156             } else {
157                 magneticFluxDensity.copyFrom(input.magneticFluxDensity);
158             }
159         } else {
160             magneticFluxDensity = null;
161         }
162 
163         magneticFluxDensityStandardDeviation = input.magneticFluxDensityStandardDeviation;
164     }
165 
166     /**
167      * Copies this instance data into provided instance.
168      *
169      * @param output destination instance where data will be copied to.
170      */
171     public void copyTo(final StandardDeviationBodyMagneticFluxDensity output) {
172         output.copyFrom(this);
173     }
174 
175     /**
176      * Computes and returns hash code for this instance. Hash codes are almost
177      * unique values that are useful for fast classification and storage of
178      * objects in collections.
179      *
180      * @return Hash code.
181      */
182     @Override
183     public int hashCode() {
184         return Objects.hash(magneticFluxDensity, magneticFluxDensityStandardDeviation);
185     }
186 
187     /**
188      * Checks if provided instance has exactly the same contents as this instance.
189      *
190      * @param other instance to be compared.
191      * @return true if both instances are considered to be equal, false otherwise.
192      */
193     public boolean equals(final StandardDeviationBodyMagneticFluxDensity other) {
194         return equals(other, 0.0);
195     }
196 
197     /**
198      * Checks if provided instance has contents similar to this instance up to
199      * provided threshold value.
200      *
201      * @param other     instance to be compared.
202      * @param threshold maximum allowed difference between magnetic flux density
203      *                  and standard deviation value.
204      * @return true if both instances are considered to be equal (up to provided
205      * threshold, false otherwise).
206      */
207     public boolean equals(final StandardDeviationBodyMagneticFluxDensity other, final double threshold) {
208         if (other == null) {
209             return false;
210         }
211 
212         return ((other.magneticFluxDensity == null && magneticFluxDensity == null)
213                 || (magneticFluxDensity != null
214                 && magneticFluxDensity.equals(other.magneticFluxDensity, threshold)))
215                 && Math.abs(magneticFluxDensityStandardDeviation
216                 - other.magneticFluxDensityStandardDeviation) <= threshold;
217     }
218 
219     /**
220      * Checks if provided object is a StandardDeviationBodyMagneticFluxDensity
221      * instance having exactly the same contents as this instance.
222      *
223      * @param obj object to be compared.
224      * @return true if both objects are considered to be equal, false otherwise.
225      */
226     public boolean equals(final Object obj) {
227         if (this == obj) {
228             return true;
229         }
230         if (obj == null || getClass() != obj.getClass()) {
231             return false;
232         }
233         final var other = (StandardDeviationBodyMagneticFluxDensity) obj;
234         return equals(other);
235     }
236 
237     /**
238      * Makes a copy of this instance.
239      *
240      * @return a copy of this instance.
241      * @throws CloneNotSupportedException if clone fails for some reason.
242      */
243     @Override
244     protected Object clone() throws CloneNotSupportedException {
245         final var result = (StandardDeviationBodyMagneticFluxDensity) super.clone();
246         copyTo(result);
247         return super.clone();
248     }
249 }