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