Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
catchJsonMe.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 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
39
40#include <visp3/core/vpConfig.h>
41
42#if defined(VISP_HAVE_NLOHMANN_JSON) && defined(VISP_HAVE_CATCH2)
43
44#include <random>
45#include <visp3/core/vpIoTools.h>
46#include <visp3/me/vpMe.h>
47
48#include VISP_NLOHMANN_JSON(json.hpp)
49using json = nlohmann::json;
50
51#include <catch_amalgamated.hpp>
52
53#ifdef ENABLE_VISP_NAMESPACE
54using namespace VISP_NAMESPACE_NAME;
55#endif
56
57template <typename T, typename C> void checkProperties(const T &t1, const T &t2, C fn, const std::string &message)
58{
59 THEN(message) { REQUIRE((t1.*fn)() == (t2.*fn)()); }
60}
61
62template <typename T, typename C, typename... Fns>
63void checkProperties(const T &t1, const T &t2, C fn, const std::string &message, Fns... fns)
64{
65 checkProperties(t1, t2, fn, message);
66 checkProperties(t1, t2, fns...);
67}
68
69template <typename C>
70void testOptionalProperty(json &j, const std::vector<std::string> &keys, vpMe &me,
71 std::function<void(vpMe *, C)> setter, std::function<C(vpMe *)> getter,
72 std::function<C(C)> valueFn)
73{
74 THEN("Removing keys does not modify the value")
75 {
76 const C v = valueFn(getter(&me));
77 setter(&me, v);
78 for (const std::string &k : keys) {
79 if (!j.contains(k)) {
80 FAIL();
81 }
82 j.erase(k);
83 }
84 from_json(j, me);
85 REQUIRE(getter(&me) == v);
86 }
87}
88
89namespace
90{
91class RandomMeGenerator : public Catch::Generators::IGenerator<vpMe>
92{
93private:
94 std::minstd_rand m_rand;
95 std::uniform_real_distribution<> m_dist;
96 std::uniform_int_distribution<> m_int_dist;
97
98 vpMe current;
99
100public:
101 RandomMeGenerator() : m_rand(std::random_device {}()), m_dist(0.0, 1.0), m_int_dist(1, 10)
102 {
103 static_cast<void>(next());
104 }
105
106 vpMe const &get() const VP_OVERRIDE { return current; }
107 bool next() VP_OVERRIDE
108 {
109 current.setThreshold(m_dist(m_rand) * 255);
110 current.setMaskNumber(m_int_dist(m_rand) * 10);
111 current.setMaskSign(m_int_dist(m_rand) > 5 ? 1 : 0);
112 current.setMu1(m_dist(m_rand));
113 current.setMu2(current.getMu1() + m_dist(m_rand));
114 current.setNbTotalSample(m_int_dist(m_rand) * 2);
115 current.setPointsToTrack(m_int_dist(m_rand));
116 current.setRange(m_int_dist(m_rand));
117 current.setStrip(m_int_dist(m_rand));
118 return true;
119 }
120};
121
122Catch::Generators::GeneratorWrapper<vpMe> randomMe()
123{
124 return Catch::Generators::GeneratorWrapper<vpMe>(Catch::Detail::make_unique<RandomMeGenerator>());
125}
126} // namespace
127
128SCENARIO("Serializing and deserializing a single vpMe", "[json]")
129{
130 GIVEN("Some random vpMe object")
131 {
132 vpMe me = GENERATE(take(10, randomMe()));
133 WHEN("Serializing and deserializing an object")
134 {
135 const json j = me;
136 const vpMe otherMe = j;
137 THEN("The object's properties are the same")
138 {
139 checkProperties(me, otherMe, &vpMe::getThreshold, "Threshold should be equal", &vpMe::getAngleStep,
140 "Angle step should be equal", &vpMe::getMaskNumber, "Mask number should be equal",
141 &vpMe::getMaskSign, "Mask sign should be equal", &vpMe::getMinSampleStep,
142 "Min sample step should be equal", &vpMe::getSampleStep, "Sample step should be equal",
143 &vpMe::getMu1, "Mu 1 should be equal", &vpMe::getMu2, "Mu 2 should be equal",
144 &vpMe::getNbTotalSample, "Nb total sample should be equal", &vpMe::getPointsToTrack,
145 "Number of points to track should be equal", &vpMe::getRange, "Range should be equal",
146 &vpMe::getStrip, "Strip should be equal");
147 }
148 }
149 WHEN("Removing optional properties in JSON object")
150 {
151 json j = me;
152
153 const auto testInt = [&j, &me](const std::string &key, std::function<void(vpMe *, int)> setter,
154 std::function<int(vpMe *)> getter) -> void {
155 testOptionalProperty<int>(j, { key }, me, setter, getter, [](int v) -> int { return v - 1; });
156 };
157 const auto testDouble = [&j, &me](const std::string &key, std::function<void(vpMe *, double)> setter,
158 std::function<double(vpMe *)> getter) -> void {
159 testOptionalProperty<double>(j, { key }, me, setter, getter, [](double v) -> double { return v + 1.0; });
160 };
161
162 WHEN("Removing threshold") { testDouble("threshold", &vpMe::setThreshold, &vpMe::getThreshold); }
163 WHEN("Removing mu1 and mu2")
164 {
165 testDouble("mu", &vpMe::setMu1, &vpMe::getMu1);
166 testDouble("mu", &vpMe::setMu2, &vpMe::getMu2);
167 }
168 WHEN("Removing nMask") { testInt("nMask", &vpMe::setMaskNumber, &vpMe::getMaskNumber); }
169 WHEN("Removing maskSize") { testInt("maskSize", &vpMe::setMaskSize, &vpMe::getMaskSize); }
170 WHEN("Removing minSampleStep") { testDouble("minSampleStep", &vpMe::setMinSampleStep, &vpMe::getMinSampleStep); }
171 WHEN("Removing sampleStep") { testDouble("sampleStep", &vpMe::setSampleStep, &vpMe::getSampleStep); }
172 WHEN("Removing maskSign") { testInt("maskSign", &vpMe::setMaskSign, &vpMe::getMaskSign); }
173 WHEN("Removing ntotalSample") { testInt("ntotalSample", &vpMe::setNbTotalSample, &vpMe::getNbTotalSample); }
174 WHEN("Removing pointsToTrack") { testInt("pointsToTrack", &vpMe::setPointsToTrack, &vpMe::getPointsToTrack); }
175 WHEN("Removing range") { testInt("range", &vpMe::setRange, &vpMe::getRange); }
176 WHEN("Removing strip") { testInt("strip", &vpMe::setStrip, &vpMe::getStrip); }
177 }
178 }
179}
180
181int main(int argc, char *argv[])
182{
183 Catch::Session session; // There must be exactly one instance
184 session.applyCommandLine(argc, argv);
185
186 int numFailed = session.run();
187 return numFailed;
188}
189
190#else
191
192int main() { return EXIT_SUCCESS; }
193
194#endif
Definition vpMe.h:143
int getMaskSign() const
Definition vpMe.h:231
void setMu1(const double &mu_1)
Definition vpMe.h:408
void setPointsToTrack(const int &points_to_track)
Definition vpMe.h:431
double getMinSampleStep() const
Definition vpMe.h:248
void setMaskSign(const int &mask_sign)
Definition vpMe.h:384
void setRange(const unsigned int &range)
Definition vpMe.h:438
void setNbTotalSample(const int &ntotal_sample)
Definition vpMe.h:422
void setMaskNumber(const unsigned int &mask_number)
Definition vpMe.cpp:555
int getNbTotalSample() const
Definition vpMe.h:269
void setThreshold(const double &threshold)
Definition vpMe.h:489
void setStrip(const int &strip)
Definition vpMe.h:452
unsigned int getAngleStep() const
Definition vpMe.h:202
void setMinSampleStep(const double &min_samplestep)
Definition vpMe.h:401
double getMu1() const
Definition vpMe.h:255
unsigned int getMaskNumber() const
Definition vpMe.h:224
int getPointsToTrack() const
Definition vpMe.h:276
int getStrip() const
Definition vpMe.h:297
void setSampleStep(const double &sample_step)
Definition vpMe.h:445
double getMu2() const
Definition vpMe.h:262
double getThreshold() const
Definition vpMe.h:306
unsigned int getMaskSize() const
Definition vpMe.h:240
void setMaskSize(const unsigned int &mask_size)
Definition vpMe.cpp:563
void setMu2(const double &mu_2)
Definition vpMe.h:415
double getSampleStep() const
Definition vpMe.h:290
unsigned int getRange() const
Definition vpMe.h:283