1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.irurueta.ar.slam;
17
18 import com.irurueta.algebra.Matrix;
19 import com.irurueta.algebra.WrongSizeException;
20 import com.irurueta.statistics.InvalidCovarianceMatrixException;
21 import com.irurueta.statistics.MultivariateNormalDist;
22
23 import java.io.Serializable;
24
25
26
27
28
29 public abstract class BaseCalibrationData implements Serializable {
30
31
32
33
34 private final int controlLength;
35
36
37
38
39 private final int stateLength;
40
41
42
43
44 private double[] controlMean;
45
46
47
48
49
50 private Matrix controlCovariance;
51
52
53
54
55 private transient MultivariateNormalDist.JacobianEvaluator evaluator;
56
57
58
59
60
61
62
63
64
65 protected BaseCalibrationData(final int controlLength, final int stateLength) {
66 if (controlLength < 1 || stateLength < 1) {
67 throw new IllegalArgumentException("length must be greater than zero");
68 }
69
70 this.controlLength = controlLength;
71 this.stateLength = stateLength;
72 }
73
74
75
76
77
78
79 public int getControlLength() {
80 return controlLength;
81 }
82
83
84
85
86
87
88 public int getStateLength() {
89 return stateLength;
90 }
91
92
93
94
95
96
97 public double[] getControlMean() {
98 return controlMean;
99 }
100
101
102
103
104
105
106
107
108 public void setControlMean(final double[] controlMean) {
109 if (controlMean.length != controlLength) {
110 throw new IllegalArgumentException("wrong mean length");
111 }
112
113 this.controlMean = controlMean;
114 }
115
116
117
118
119
120
121
122 public Matrix getControlCovariance() {
123 return controlCovariance;
124 }
125
126
127
128
129
130
131
132
133 public void setControlCovariance(final Matrix controlCovariance) {
134 if (controlCovariance.getRows() != controlLength || controlCovariance.getColumns() != controlLength) {
135 throw new IllegalArgumentException("wrong covariance size");
136 }
137
138 this.controlCovariance = controlCovariance;
139 }
140
141
142
143
144
145
146
147
148
149
150
151 public void setControlMeanAndCovariance(final double[] controlMean, final Matrix controlCovariance) {
152 if (controlMean.length != controlLength) {
153 throw new IllegalArgumentException("wrong mean length");
154 }
155 if (controlCovariance.getRows() != controlLength || controlCovariance.getColumns() != controlLength) {
156 throw new IllegalArgumentException("wrong covariance size");
157 }
158
159 this.controlMean = controlMean;
160 this.controlCovariance = controlCovariance;
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174
175 public MultivariateNormalDist propagateWithControlJacobian(final Matrix controlJacobian)
176 throws InvalidCovarianceMatrixException {
177 final var dist = new MultivariateNormalDist();
178 propagateWithControlJacobian(controlJacobian, dist);
179 return dist;
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193
194 public void propagateWithControlJacobian(
195 final Matrix controlJacobian, final MultivariateNormalDist result) throws InvalidCovarianceMatrixException {
196 if (controlJacobian.getRows() != stateLength || controlJacobian.getColumns() != controlLength) {
197 throw new IllegalArgumentException("wrong control jacobian size");
198 }
199
200 if (evaluator == null) {
201 evaluator = new MultivariateNormalDist.JacobianEvaluator() {
202 @Override
203 public void evaluate(final double[] x, final double[] y, final Matrix jacobian) {
204 controlJacobian.copyTo(jacobian);
205 }
206
207 @Override
208 public int getNumberOfVariables() {
209 return stateLength;
210 }
211 };
212 }
213
214 try {
215 MultivariateNormalDist.propagate(evaluator, controlMean, controlCovariance, result);
216 } catch (final WrongSizeException e) {
217 throw new InvalidCovarianceMatrixException(e);
218 }
219 }
220 }