Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
catchJsonCamera.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/vpCameraParameters.h>
41#include <visp3/core/vpIoTools.h>
42
43#if defined(VISP_HAVE_NLOHMANN_JSON) && defined(VISP_HAVE_CATCH2)
44#include VISP_NLOHMANN_JSON(json.hpp)
45using json = nlohmann::json;
46
47#include <catch_amalgamated.hpp>
48
49#ifdef ENABLE_VISP_NAMESPACE
50using namespace VISP_NAMESPACE_NAME;
51#endif
52
53#include <random>
54namespace
55{
56// This class shows how to implement a simple generator for Catch tests
57class vpRandomCamGenerator : public Catch::Generators::IGenerator<vpCameraParameters>
58{
59private:
60 std::minstd_rand m_rand;
61 std::uniform_int_distribution<> m_count_dist;
62 std::uniform_int_distribution<> m_type_dist;
63 std::uniform_real_distribution<> m_dist;
64
65 vpCameraParameters current;
66
67public:
68 vpRandomCamGenerator(double low, double high)
69 : m_rand(std::random_device {}()), m_count_dist(1, 5), m_type_dist(0, 2), m_dist(low, high)
70 {
71 static_cast<void>(next());
72 }
73
74 const vpCameraParameters &get() const VP_OVERRIDE { return current; }
75 bool next() VP_OVERRIDE
76 {
77 const double px = m_dist(m_rand);
78 const double py = m_dist(m_rand);
79 const double u0 = m_dist(m_rand);
80 const double v0 = m_dist(m_rand);
81
82 const int type = m_type_dist(m_rand);
83 switch (type) {
84 case 0: {
85 current.initPersProjWithoutDistortion(px, py, u0, v0);
86 break;
87 }
88 case 1: {
89 current.initPersProjWithDistortion(px, py, u0, v0, m_dist(m_rand), m_dist(m_rand));
90 break;
91 }
92 case 2: {
93 std::vector<double> coeffs;
94 const int count = m_count_dist(m_rand);
95 for (int i = 0; i < count; ++i) {
96 coeffs.push_back(m_dist(m_rand));
97 }
98 current.initProjWithKannalaBrandtDistortion(px, py, u0, v0, coeffs);
99 break;
100 }
101 default: {
102 throw vpException(vpException::badValue, "Shouldn't happen");
103 }
104 }
105 return true;
106 }
107};
108
109Catch::Generators::GeneratorWrapper<vpCameraParameters> randomCam(double low, double high)
110{
111 return Catch::Generators::GeneratorWrapper<vpCameraParameters>(Catch::Detail::make_unique<vpRandomCamGenerator>(low, high));
112}
113} // namespace
114
115SCENARIO("Serializing and deserializing a single vpCameraParameters", "[json]")
116{
117 GIVEN("Some camera intrinsics")
118 {
119 vpCameraParameters cam = GENERATE(take(100, randomCam(50.0, 500.0)));
120 THEN("Serializing and deserializing does not modify the object")
121 {
122 const json j = cam;
123 const vpCameraParameters otherCam = j;
124 REQUIRE(cam == otherCam);
125 }
126 }
127}
128
129SCENARIO("Serializing two cameras", "[json]")
130{
131 GIVEN("Some camera intrinsics")
132 {
133 vpCameraParameters cam = GENERATE(take(10, randomCam(50.0, 500.0)));
134 vpCameraParameters cam2 = GENERATE(take(10, randomCam(50.0, 500.0)));
135 if (cam != cam2) {
136 WHEN("serializing and deserializing two different cameras")
137 {
138 THEN("The deserialized cams are still different")
139 {
140 const json j1 = cam;
141 const json j2 = cam2;
142
143 const vpCameraParameters resCam = j1;
144 const vpCameraParameters resCam2 = j2;
145 REQUIRE(resCam != resCam2);
146 }
147 }
148 }
149 }
150}
151
152int main(int argc, char *argv[])
153{
154 Catch::Session session; // There must be exactly one instance
155 session.applyCommandLine(argc, argv);
156
157 int numFailed = session.run();
158 return numFailed;
159}
160
161#else
162
163int main() { return EXIT_SUCCESS; }
164
165#endif
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
void initPersProjWithDistortion(double px, double py, double u0, double v0, double kud, double kdu)
void initProjWithKannalaBrandtDistortion(double px, double py, double u0, double v0, const std::vector< double > &distortion_coefficients)
@ badValue
Used to indicate that a value is not in the allowed range.
Definition vpException.h:73