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  const std::vector<float> expected_mean = {-1.0, 2.0, -3.0};
126  const std::vector<float> expected_covariance =
127  {
128  1.0, 0.3, 0.7, //
129  0.3, 2.0, 0.4, //
130  0.7, 0.4, 1.0, //
131  };
132 
133  const MultivariateNoiseGenerator<float> gen(expected_mean, expected_covariance);
134  testNoiseGeneratorResults(expected_mean, expected_covariance, gen, 0.01);
135 }
136 
137 TEST(NoiseGenerator, MultivariateNoiseGeneratorForState6Dof)
138 {
139  const std::vector<float> expected_mean = {5.0, -6.0, 7.0, -0.3, 0.2, 0.1};
140  const std::vector<float> expected_covariance =
141  {
142  2.000, 0.500, 0.600, 0.050, 0.040, 0.000, //
143  0.500, 2.500, 0.400, 0.060, 0.070, 0.080, //
144  0.600, 0.400, 3.000, 0.090, 0.020, 0.110, //
145  0.050, 0.060, 0.090, 0.200, 0.045, 0.035, //
146  0.040, 0.070, 0.020, 0.045, 0.150, 0.015, //
147  0.000, 0.080, 0.110, 0.035, 0.015, 0.100, //
148  };
149 
150  const mcl_3dl::Vec3 mean_pos(expected_mean[0], expected_mean[1], expected_mean[2]);
151  const mcl_3dl::Quat mean_rot(mcl_3dl::Vec3(expected_mean[3], expected_mean[4], expected_mean[5]));
152  const State6DOF mean(mean_pos, mean_rot);
153 
154  const MultivariateNoiseGenerator<float> gen(mean, expected_covariance);
155  testNoiseGeneratorResults(expected_mean, expected_covariance, gen, 0.01);
156 }
157 
158 } // namespace mcl_3dl
159 
160 int main(int argc, char** argv)
161 {
162  testing::InitGoogleTest(&argc, argv);
163 
164  return RUN_ALL_TESTS();
165 }
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 Tue Jul 4 2023 02:11:07