Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testJsonMe.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Test vpCameraParameters JSON parse / save.
32 */
33
40#include <visp3/core/vpConfig.h>
41
42#if defined(VISP_HAVE_NLOHMANN_JSON) && defined(VISP_HAVE_CATCH2) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
43
44#include <random>
45#include <visp3/core/vpIoTools.h>
46#include <visp3/me/vpMe.h>
47
48#include <nlohmann/json.hpp>
49using json = nlohmann::json;
50
51#define CATCH_CONFIG_RUNNER
52#include <catch.hpp>
53
54template <typename T, typename C> void checkProperties(const T &t1, const T &t2, C fn, const std::string &message)
55{
56 THEN(message) { REQUIRE((t1.*fn)() == (t2.*fn)()); }
57}
58
59template <typename T, typename C, typename... Fns>
60void checkProperties(const T &t1, const T &t2, C fn, const std::string &message, Fns... fns)
61{
62 checkProperties(t1, t2, fn, message);
63 checkProperties(t1, t2, fns...);
64}
65
66template <typename C>
67void testOptionalProperty(json &j, const std::vector<std::string> &keys, vpMe &me,
68 std::function<void(vpMe *, C)> setter, std::function<C(vpMe *)> getter,
69 std::function<C(C)> valueFn)
70{
71 THEN("Removing keys does not modify the value")
72 {
73 const C v = valueFn(getter(&me));
74 setter(&me, v);
75 for (const std::string &k : keys) {
76 if (!j.contains(k)) {
77 FAIL();
78 }
79 j.erase(k);
80 }
81 from_json(j, me);
82 REQUIRE(getter(&me) == v);
83 }
84}
85
86namespace
87{
88class RandomMeGenerator : public Catch::Generators::IGenerator<vpMe>
89{
90private:
91 std::minstd_rand m_rand;
92 std::uniform_real_distribution<> m_dist;
93 std::uniform_int_distribution<> m_int_dist;
94
95 vpMe current;
96
97public:
98 RandomMeGenerator() : m_rand(std::random_device{}()), m_dist(0.0, 1.0), m_int_dist(1, 10)
99 {
100 static_cast<void>(next());
101 }
102
103 vpMe const &get() const override { return current; }
104 bool next() override
105 {
106 current.setThreshold(m_dist(m_rand) * 255);
107 current.setMaskNumber(m_int_dist(m_rand) * 10);
108 current.setMaskSign(m_int_dist(m_rand) > 5 ? 1 : 0);
109 current.setMu1(m_dist(m_rand));
110 current.setMu2(current.getMu1() + m_dist(m_rand));
111 current.setNbTotalSample(m_int_dist(m_rand) * 2);
112 current.setPointsToTrack(m_int_dist(m_rand));
113 current.setRange(m_int_dist(m_rand));
114 current.setStrip(m_int_dist(m_rand));
115 return true;
116 }
117};
118Catch::Generators::GeneratorWrapper<vpMe> randomMe()
119{
120 return Catch::Generators::GeneratorWrapper<vpMe>(
121 std::unique_ptr<Catch::Generators::IGenerator<vpMe> >(new RandomMeGenerator()));
122}
123} // namespace
124
125SCENARIO("Serializing and deserializing a single vpMe", "[json]")
126{
127 GIVEN("Some random vpMe object")
128 {
129 vpMe me = GENERATE(take(10, randomMe()));
130 WHEN("Serializing and deserializing an object")
131 {
132 const json j = me;
133 const vpMe otherMe = j;
134 THEN("The object's properties are the same")
135 {
136 checkProperties(me, otherMe, &vpMe::getThreshold, "Threshold should be equal", &vpMe::getAngleStep,
137 "Angle step should be equal", &vpMe::getMaskNumber, "Mask number should be equal",
138 &vpMe::getMaskSign, "Mask sign should be equal", &vpMe::getMinSampleStep,
139 "Min sample step should be equal", &vpMe::getSampleStep, "Sample step should be equal",
140 &vpMe::getMu1, "Mu 1 should be equal", &vpMe::getMu2, "Mu 2 should be equal",
141 &vpMe::getNbTotalSample, "Nb total sample should be equal", &vpMe::getPointsToTrack,
142 "Number of points to track should be equal", &vpMe::getRange, "Range should be equal",
143 &vpMe::getStrip, "Strip should be equal");
144 }
145 }
146 WHEN("Removing optional properties in JSON object")
147 {
148 json j = me;
149
150 const auto testInt = [&j, &me](const std::string &key, std::function<void(vpMe *, int)> setter,
151 std::function<int(vpMe *)> getter) -> void {
152 testOptionalProperty<int>(j, { key }, me, setter, getter, [](int v) -> int { return v - 1; });
153 };
154 const auto testDouble = [&j, &me](const std::string &key, std::function<void(vpMe *, double)> setter,
155 std::function<double(vpMe *)> getter) -> void {
156 testOptionalProperty<double>(j, { key }, me, setter, getter, [](double v) -> double { return v + 1.0; });
157 };
158
159 WHEN("Removing threshold") { testDouble("threshold", &vpMe::setThreshold, &vpMe::getThreshold); }
160 WHEN("Removing mu1 and mu2")
161 {
162 testDouble("mu", &vpMe::setMu1, &vpMe::getMu1);
163 testDouble("mu", &vpMe::setMu2, &vpMe::getMu2);
164 }
165 WHEN("Removing nMask") { testInt("nMask", &vpMe::setMaskNumber, &vpMe::getMaskNumber); }
166 WHEN("Removing maskSize") { testInt("maskSize", &vpMe::setMaskSize, &vpMe::getMaskSize); }
167 WHEN("Removing minSampleStep") { testDouble("minSampleStep", &vpMe::setMinSampleStep, &vpMe::getMinSampleStep); }
168 WHEN("Removing sampleStep") { testDouble("sampleStep", &vpMe::setSampleStep, &vpMe::getSampleStep); }
169 WHEN("Removing maskSign") { testInt("maskSign", &vpMe::setMaskSign, &vpMe::getMaskSign); }
170 WHEN("Removing ntotalSample") { testInt("ntotalSample", &vpMe::setNbTotalSample, &vpMe::getNbTotalSample); }
171 WHEN("Removing pointsToTrack") { testInt("pointsToTrack", &vpMe::setPointsToTrack, &vpMe::getPointsToTrack); }
172 WHEN("Removing range") { testInt("range", &vpMe::setRange, &vpMe::getRange); }
173 WHEN("Removing strip") { testInt("strip", &vpMe::setStrip, &vpMe::getStrip); }
174 }
175 }
176}
177
178int main(int argc, char *argv [])
179{
180 Catch::Session session; // There must be exactly one instance
181 session.applyCommandLine(argc, argv);
182
183 int numFailed = session.run();
184 return numFailed;
185}
186
187#else
188
189int main() { return EXIT_SUCCESS; }
190
191#endif
Definition vpMe.h:122
int getMaskSign() const
Definition vpMe.h:228
void setMu1(const double &mu_1)
Definition vpMe.h:353
void setSampleStep(const double &s)
Definition vpMe.h:390
void setMaskSign(const int &a)
Definition vpMe.h:330
void setRange(const unsigned int &r)
Definition vpMe.h:383
void setStrip(const int &a)
Definition vpMe.h:404
double getMinSampleStep() const
Definition vpMe.h:243
void setMinSampleStep(const double &min)
Definition vpMe.h:346
void setMaskSize(const unsigned int &a)
Definition vpMe.cpp:452
int getNbTotalSample() const
Definition vpMe.h:261
unsigned int getAngleStep() const
Definition vpMe.h:208
double getMu1() const
Definition vpMe.h:249
unsigned int getMaskNumber() const
Definition vpMe.h:222
int getPointsToTrack() const
Definition vpMe.h:267
int getStrip() const
Definition vpMe.h:279
void setNbTotalSample(const int &nb)
Definition vpMe.h:367
double getMu2() const
Definition vpMe.h:255
double getThreshold() const
Definition vpMe.h:288
void setPointsToTrack(const int &n)
Definition vpMe.h:376
unsigned int getMaskSize() const
Definition vpMe.h:236
void setMu2(const double &mu_2)
Definition vpMe.h:360
double getSampleStep() const
Definition vpMe.h:397
unsigned int getRange() const
Definition vpMe.h:273
void setMaskNumber(const unsigned int &a)
Definition vpMe.cpp:445
void setThreshold(const double &t)
Definition vpMe.h:435