1 /*
2 * Copyright (C) 2018 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.geometry;
17
18 import com.irurueta.algebra.ArrayUtils;
19 import com.irurueta.algebra.Matrix;
20 import com.irurueta.algebra.NonSymmetricPositiveDefiniteMatrixException;
21
22 /**
23 * Contains methods to convert covariance matrices into ellipses representing accuracy
24 * with requested confidence.
25 */
26 public class Accuracy2D extends Accuracy {
27
28 /**
29 * Constructor.
30 */
31 public Accuracy2D() {
32 super();
33 }
34
35 /**
36 * Constructor.
37 *
38 * @param covarianceMatrix covariance matrix to be set. Must be 2x2 and positive
39 * definite.
40 * @throws IllegalArgumentException if provided matrix is not square (it must also be
41 * positive definite to be properly converted to an ellipse).
42 * @throws NonSymmetricPositiveDefiniteMatrixException if provided matrix is not symmetric and
43 * positive definite.
44 */
45 public Accuracy2D(final Matrix covarianceMatrix) throws NonSymmetricPositiveDefiniteMatrixException {
46 super(covarianceMatrix);
47 }
48
49 /**
50 * Constructor.
51 *
52 * @param confidence confidence of provided accuracy of an estimated position.
53 * @throws IllegalArgumentException if provided value is not within 0 and 1.
54 */
55 public Accuracy2D(final double confidence) {
56 super(confidence);
57 }
58
59 /**
60 * Constructor.
61 *
62 * @param covarianceMatrix covariance matrix to be set. Must be 2x2 and positive
63 * definite.
64 * @param confidence confidence of provided accuracy of an estimated position.
65 * @throws IllegalArgumentException if provided matrix is not square (it must also be
66 * positive definite to be properly converted to an ellipse), or
67 * if provided confidence value is not within 0 and 1.
68 * @throws NonSymmetricPositiveDefiniteMatrixException if provided matrix is not symmetric and
69 * positive definite.
70 */
71 public Accuracy2D(final Matrix covarianceMatrix, final double confidence)
72 throws NonSymmetricPositiveDefiniteMatrixException {
73 super(covarianceMatrix, confidence);
74 }
75
76 /**
77 * Gets number of dimensions.
78 *
79 * @return always returns 2.
80 */
81 @Override
82 public int getNumberOfDimensions() {
83 return Point2D.POINT2D_INHOMOGENEOUS_COORDINATES_LENGTH;
84 }
85
86 /**
87 * Converts provided covariance matrix into a 2D ellipse taking into account current
88 * confidence and standard deviation factor.
89 *
90 * @return ellipse representing accuracy of covariance matrix with current confidence and
91 * standard deviation factor.
92 * @throws NullPointerException if covariance matrix has not been provided yet.
93 * @throws InvalidRotationMatrixException if rotation cannot be properly determined.
94 */
95 public Ellipse toEllipse() throws InvalidRotationMatrixException {
96 final var semiAxesLengths = ArrayUtils.multiplyByScalarAndReturnNew(sqrtSingularValues,
97 standardDeviationFactor);
98 final var rotation = new Rotation2D(u);
99 return new Ellipse(Point2D.create(), semiAxesLengths[0], semiAxesLengths[1], rotation);
100 }
101 }