Visual Servoing Platform  version 3.6.1 under development (2024-02-13)
testJsonCamera.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Test vpCameraParameters JSON parse / save.
33  *
34 *****************************************************************************/
35 
42 #include <visp3/core/vpCameraParameters.h>
43 #include <visp3/core/vpIoTools.h>
44 
45 #if defined(VISP_HAVE_NLOHMANN_JSON) && defined(VISP_HAVE_CATCH2)
46 #include <nlohmann/json.hpp>
47 using json = nlohmann::json;
48 
49 #define CATCH_CONFIG_RUNNER
50 #include <catch.hpp>
51 
52 #include <random>
53 namespace
54 {
55 // This class shows how to implement a simple generator for Catch tests
56 class vpRandomCamGenerator : public Catch::Generators::IGenerator<vpCameraParameters>
57 {
58 private:
59  std::minstd_rand m_rand;
60  std::uniform_int_distribution<> m_count_dist;
61  std::uniform_int_distribution<> m_type_dist;
62  std::uniform_real_distribution<> m_dist;
63 
64  vpCameraParameters current;
65 
66 public:
67  vpRandomCamGenerator(double low, double high)
68  : m_rand(std::random_device {}()), m_count_dist(1, 5), m_type_dist(0, 2), m_dist(low, high)
69  {
70  static_cast<void>(next());
71  }
72 
73  const vpCameraParameters &get() const vp_override { return current; }
74  bool next() vp_override
75  {
76  const double px = m_dist(m_rand);
77  const double py = m_dist(m_rand);
78  const double u0 = m_dist(m_rand);
79  const double v0 = m_dist(m_rand);
80 
81  const int type = m_type_dist(m_rand);
82  switch (type) {
83  case 0: {
84  current.initPersProjWithoutDistortion(px, py, u0, v0);
85  break;
86  }
87  case 1: {
88  current.initPersProjWithDistortion(px, py, u0, v0, m_dist(m_rand), m_dist(m_rand));
89  break;
90  }
91  case 2: {
92  std::vector<double> coeffs;
93  const int count = m_count_dist(m_rand);
94  for (int i = 0; i < count; ++i) {
95  coeffs.push_back(m_dist(m_rand));
96  }
97  current.initProjWithKannalaBrandtDistortion(px, py, u0, v0, coeffs);
98  break;
99  }
100  default: {
101  throw vpException(vpException::badValue, "Shouldn't happen");
102  }
103  }
104  return true;
105  }
106 };
107 
108 Catch::Generators::GeneratorWrapper<vpCameraParameters> randomCam(double low, double high)
109 {
110  return Catch::Generators::GeneratorWrapper<vpCameraParameters>(
111  std::unique_ptr<Catch::Generators::IGenerator<vpCameraParameters> >(new vpRandomCamGenerator(low, high)));
112 }
113 } // namespace
114 
115 SCENARIO("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 
129 SCENARIO("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 
152 int 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 
163 int 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)
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:85