42#include <visp3/core/vpIoTools.h>
43#include <visp3/mbt/vpMbGenericTracker.h>
45#if defined(VISP_HAVE_NLOHMANN_JSON) && defined(VISP_HAVE_CATCH2)
46#include <nlohmann/json.hpp>
47using json = nlohmann::json;
49#define CATCH_CONFIG_RUNNER
54 const std::vector<std::string> names = {
"C1",
"C2" };
55 std::vector<int> featureTypes;
56#if defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
69 std::map<std::string, vpCameraParameters> cams;
70 cams[names[0]] = cam1;
71 cams[names[1]] = cam2;
81template<
typename T,
typename C>
82void checkProperties(
const T &t1,
const T &t2, C fn,
const std::string &message)
86 REQUIRE((t1.*fn)() == (t2.*fn)());
90template<
typename T,
typename C,
typename... Fns>
91void checkProperties(
const T &t1,
const T &t2, C fn,
const std::string &message, Fns... fns)
93 checkProperties(t1, t2, fn, message);
94 checkProperties(t1, t2, fns...);
105 std::map<std::string, vpCameraParameters> c1, c2;
112json loadJson(
const std::string &path)
114 std::ifstream json_file(path);
115 if (!json_file.good()) {
118 json j = json::parse(json_file);
123void saveJson(
const json &j,
const std::string &path)
125 std::ofstream json_file(path);
126 if (!json_file.good()) {
129 json_file << j.dump();
133SCENARIO(
"MBT JSON Serialization",
"[json]")
140 GIVEN(
"A generic tracker with two cameras, one with edge and KLT features, the other with depth features")
143 WHEN(
"Saving to a JSON settings file")
145 const std::string jsonPath = tmp_dir +
"/" +
"tracker_save.json";
147 const auto modifyJson = [&jsonPath](std::function<void(json &)> modify) ->
void {
148 json j = loadJson(jsonPath);
150 saveJson(j, jsonPath);
154 THEN(
"Reloading this tracker has the same basic properties")
158 compareNamesAndTypes(t1, t2);
159 compareCameraParameters(t1, t2);
162 THEN(
"Reloading this tracker has the same basic properties")
166 checkProperties(t1, t2,
172 THEN(
"Reloaded edge tracker parameters should be the same")
174 std::map<std::string, vpMe> oldvpMe, newvpMe;
179 for (
const auto &it : oldvpMe) {
182 REQUIRE_NOTHROW(n = newvpMe[it.first]);
183 checkProperties(o, n,
198#if defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV)
199 THEN(
"Reloaded KLT tracker parameters should be the same")
201 std::map<std::string, vpKltOpencv> oldvpKlt, newvpKlt;
206 for (
const auto &it : oldvpKlt) {
209 REQUIRE_NOTHROW(n = newvpKlt[it.first]);
210 checkProperties(o, n,
223 THEN(
"Clipping properties should be the same")
230 std::map<std::string, unsigned int> oldFlags, newFlags;
233 for (
const auto &it : oldFlags) {
234 unsigned int o = it.second;
236 REQUIRE_NOTHROW(n = newFlags[it.first]);
237 THEN(
"Clipping flags for camera " + it.first +
" should be the same")
242 checkProperties(t1, t2,
248 WHEN(
"Modifying JSON file/Using a custom JSON file")
250 THEN(
"Removing version from file generates an error on load")
252 modifyJson([](json &j) ->
void {
258 THEN(
"Using an unsupported version generates an error on load")
260 modifyJson([](json &j) ->
void {
261 j[
"version"] =
"0.0.0";
266 THEN(
"Using an undefined reference camera generates an error")
268 modifyJson([](json &j) ->
void {
269 j[
"referenceCameraName"] =
"C3";
274 THEN(
"Not defining a transformation matrix for the reference camera is valid")
276 modifyJson([&t1](json &j) ->
void {
282 THEN(
"Not defining a transformation from a non-reference camera to the reference camera generates an error")
284 modifyJson([&t1](json &j) ->
void {
286 j[
"trackers"][otherCamName].erase(
"camTref");
291 THEN(
"The full clipping config is optional")
294 const double clipping_near = 0.21;
295 const double clipping_far = 5.2;
300 modifyJson([&t1](json &j) ->
void {
302 j[
"trackers"][c].erase(
"clipping");
311 THEN(
"Each clipping param is optional on its own")
314 const double clipping_near = 0.21;
315 const double clipping_far = 5.2;
320 THEN(
"Near clipping is optional")
322 modifyJson([&t1](json &j) ->
void {
324 j[
"trackers"][c][
"clipping"].erase(
"near");
332 THEN(
"Far clipping is optional")
334 modifyJson([&t1](json &j) ->
void {
336 j[
"trackers"][c][
"clipping"].erase(
"far");
344 THEN(
"Clipping flags are optional")
346 modifyJson([&t1](json &j) ->
void {
348 j[
"trackers"][c][
"clipping"].erase(
"flags");
361int main(
int argc,
char *argv [])
363 Catch::Session session;
364 session.applyCommandLine(argc, argv);
366 int numFailed = session.run();
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
error that can be emitted by ViSP classes.
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
double getQuality() const
int getMaxFeatures() const
Get the list of lost feature.
int getWindowSize() const
Get the window size used to refine the corner locations.
double getHarrisFreeParameter() const
Get the free parameter of the Harris detector.
double getMinDistance() const
int getBlockSize() const
Get the size of the averaging block used to track the features.
int getPyramidLevels() const
Get the list of features id.
static double rad(double deg)
Real-time 6D object pose tracking using its CAD model.
virtual void setCameraParameters(const vpCameraParameters &camera)
virtual void setLod(bool useLod, const std::string &name="")
virtual void setAngleAppear(const double &a)
virtual void saveConfigFile(const std::string &settingsFile) const
virtual std::string getReferenceCameraName() const
virtual std::map< std::string, int > getCameraTrackerTypes() const
virtual void setNearClippingDistance(const double &dist)
virtual void getCameraParameters(vpCameraParameters &camera) const
virtual void setAngleDisappear(const double &a)
virtual std::vector< std::string > getCameraNames() const
virtual void getClipping(unsigned int &clippingFlag1, unsigned int &clippingFlag2) const
virtual vpKltOpencv getKltOpencv() const
virtual vpMe getMovingEdge() const
virtual void setFarClippingDistance(const double &dist)
virtual void setClipping(const unsigned int &flags)
virtual void loadConfigFile(const std::string &configFile, bool verbose=true)
virtual double getNearClippingDistance() const
virtual double getAngleAppear() const
virtual double getAngleDisappear() const
virtual double getFarClippingDistance() const
double getMinSampleStep() const
int getNbTotalSample() const
unsigned int getAngleStep() const
unsigned int getMaskNumber() const
int getPointsToTrack() const
unsigned int getRange() const