test_noise_generator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, the mcl_3dl authors
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <algorithm>
31 #include <cmath>
32 #include <cstddef>
33 #include <vector>
34 
35 #include <gtest/gtest.h>
36 
39 #include <mcl_3dl/state_6dof.h>
40 
41 namespace mcl_3dl
42 {
43 template <typename NOISE_GEN>
44 void testNoiseGeneratorResults(const std::vector<float>& expected_means,
45  const std::vector<float>& expected_covariances,
46  const NOISE_GEN& gen,
47  const float allowable_error_of_zero_cov)
48 {
49  const size_t dim = expected_means.size();
50  ASSERT_EQ(dim, gen.getDimension());
51  ASSERT_EQ(dim * dim, expected_covariances.size());
52 
53  std::mt19937 mt(123);
54  std::vector<std::vector<float>> results(dim);
55  for (size_t i = 0; i < 10000; ++i)
56  {
57  const auto result = gen(mt);
58  ASSERT_EQ(dim, result.size());
59  for (size_t j = 0; j < dim; ++j)
60  {
61  results[j].push_back(result[j]);
62  }
63  }
64  std::vector<float> averages;
65  for (size_t i = 0; i < dim; ++i)
66  {
67  const float average = std::accumulate(results[i].begin(), results[i].end(), 0.0) / results[i].size();
68  EXPECT_NEAR(average, expected_means[i], 0.1);
69  averages.push_back(average);
70  }
71  for (size_t i = 0; i < dim; ++i)
72  {
73  for (size_t j = i; j < dim; ++j)
74  {
75  float covar = 0;
76  for (size_t n = 0; n < results[i].size(); ++n)
77  {
78  covar += (results[i][n] - averages[i]) * (results[j][n] - averages[j]);
79  }
80  covar /= results[i].size();
81  const float expected_covar = expected_covariances[i + j * dim];
82  if (expected_covar == 0.0)
83  {
84  EXPECT_NEAR(covar, expected_covar, allowable_error_of_zero_cov) << "Error at " << i << "," << j;
85  }
86  else
87  {
88  EXPECT_GE(covar, expected_covariances[i + j * dim] * 0.9 * 0.9) << "Error at " << i << "," << j;
89  EXPECT_LE(covar, expected_covariances[i + j * dim] * 1.1 * 1.1) << "Error at " << i << "," << j;
90  }
91  }
92  }
93 }
94 
95 std::vector<float> toCovarianceMatrix(const std::vector<float>& sigma)
96 {
97  const size_t dim = sigma.size();
98 
99  std::vector<float> result(dim * dim, 0.0);
100  for (size_t i = 0; i < sigma.size(); ++i)
101  {
102  result[i + i * dim] = sigma[i] * sigma[i];
103  }
104  return result;
105 }
106 
107 TEST(NoiseGenerator, DiagonalNoiseGenerator)
108 {
109  std::vector<float> expected_mean;
110  expected_mean.push_back(7.0);
111  expected_mean.push_back(8.0);
112  expected_mean.push_back(9.0);
113 
114  std::vector<float> expected_sigma;
115  expected_sigma.push_back(1.0);
116  expected_sigma.push_back(2.0);
117  expected_sigma.push_back(3.0);
118 
119  const DiagonalNoiseGenerator<float> gen(expected_mean, expected_sigma);
120  testNoiseGeneratorResults(expected_mean, toCovarianceMatrix(expected_sigma), gen, 0.1);
121 }
122 
124 {
125  // clang-format off
126  const std::vector<float> expected_mean =
127  {
128  -1.0, 2.0, -3.0,
129  };
130  const std::vector<float> expected_covariance =
131  {
132  1.0, 0.3, 0.7,
133  0.3, 2.0, 0.4,
134  0.7, 0.4, 1.0
135  };
136  // clang-format on
137 
138  const MultivariateNoiseGenerator<float> gen(expected_mean, expected_covariance);
139  testNoiseGeneratorResults(expected_mean, expected_covariance, gen, 0.01);
140 }
141 
142 TEST(NoiseGenerator, MultivariateNoiseGeneratorForState6Dof)
143 {
144  // clang-format off
145  const std::vector<float> expected_mean =
146  {
147  5.0, -6.0, 7.0, -0.3, 0.2, 0.1
148  };
149  const std::vector<float> expected_covariance =
150  {
151  2.0, 0.5, 0.6, 0.05, 0.04, 0.0,
152  0.5, 2.5, 0.4, 0.06, 0.07, 0.08,
153  0.6, 0.4, 3.0, 0.09, 0.02, 0.11,
154  0.05, 0.06, 0.09, 0.2, 0.045, 0.035,
155  0.04, 0.07, 0.02, 0.045, 0.15, 0.015,
156  0.0, 0.08, 0.11, 0.035, 0.015, 0.1
157  };
158  // clang-format on
159 
160  const mcl_3dl::Vec3 mean_pos(expected_mean[0], expected_mean[1], expected_mean[2]);
161  const mcl_3dl::Quat mean_rot(mcl_3dl::Vec3(expected_mean[3], expected_mean[4], expected_mean[5]));
162  const State6DOF mean(mean_pos, mean_rot);
163 
164  const MultivariateNoiseGenerator<float> gen(mean, expected_covariance);
165  testNoiseGeneratorResults(expected_mean, expected_covariance, gen, 0.01);
166 }
167 
168 } // namespace mcl_3dl
169 
170 int main(int argc, char** argv)
171 {
172  testing::InitGoogleTest(&argc, argv);
173 
174  return RUN_ALL_TESTS();
175 }
TEST(NoiseGenerator, DiagonalNoiseGenerator)
int main(int argc, char **argv)
std::vector< float > toCovarianceMatrix(const std::vector< float > &sigma)
void testNoiseGeneratorResults(const std::vector< float > &expected_means, const std::vector< float > &expected_covariances, const NOISE_GEN &gen, const float allowable_error_of_zero_cov)


mcl_3dl
Author(s): Atsushi Watanabe
autogenerated on Wed May 12 2021 02:16:29