Skip to content

Commit

Permalink
more testing of json serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
SamFlt committed Sep 24, 2024
1 parent d4ecd40 commit 86681cf
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,17 @@ class VISP_EXPORT vpSilhouettePointsExtractionSettings
~vpSilhouettePointsExtractionSettings() = default;
const vpSilhouettePointsExtractionSettings &operator=(const vpSilhouettePointsExtractionSettings &rend);

void setThreshold(double lambda) { m_depthThreshold = lambda; }
double getThreshold() const { return m_depthThreshold; }
void setThresholdIsRelative(bool isRelative) { m_thresholdIsRelative = isRelative; }
void setThreshold(double lambda) { m_depthThreshold = lambda; }
bool thresholdIsRelative() const { return m_thresholdIsRelative; }
void setThresholdIsRelative(bool isRelative) { m_thresholdIsRelative = isRelative; }
bool preferPreviousPoints() const { return m_preferPreviousPoints; }
void setPreferPreviousPoints(bool prefer) { m_preferPreviousPoints = prefer; }


int getMaxCandidates() const { return m_maxNumPoints; }
void setMaxCandidates(int maxCandidates) { m_maxNumPoints = maxCandidates; }
unsigned int getSampleStep() const { return m_sampleStep; }
void setSampleStep(unsigned int a)
{
if (m_sampleStep == 0) {
Expand All @@ -125,13 +130,12 @@ inline void from_json(const nlohmann::json &j, vpSilhouettePointsExtractionSetti
nlohmann::json thresholdSettings = j.at("threshold");
std::string thresholdType = thresholdSettings.at("type");
settings.m_thresholdIsRelative = thresholdType == "relative";
settings.m_preferPreviousPoints = thresholdSettings.at("reusePreviousPoints");

settings.m_depthThreshold = thresholdSettings.at("value");

nlohmann::json samplingSettings = j.at("sampling");
settings.m_preferPreviousPoints = samplingSettings.at("reusePreviousPoints");
settings.m_maxNumPoints = samplingSettings.at("numPoints");
settings.m_sampleStep = samplingSettings.at("samplingRate");
settings.setSampleStep(samplingSettings.at("samplingRate"));

}
#endif
Expand Down
1 change: 1 addition & 0 deletions modules/tracker/rbt/include/visp3/rbt/vpRBTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class VISP_EXPORT vpRBTracker
*/
void addTracker(std::shared_ptr<vpRBFeatureTracker> tracker);
void setupRenderer(const std::string &file);
std::string getModelPath() const { return m_modelPath; }
void setModelPath(const std::string &path);

vpCameraParameters getCameraParameters() const;
Expand Down
129 changes: 116 additions & 13 deletions modules/tracker/rbt/test/testRBT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,52 @@
#define CATCH_CONFIG_RUNNER
#include <catch.hpp>

const char *objCube = R"obj(
o Cube
v -0.050000 -0.050000 0.050000
v -0.050000 0.050000 0.050000
v -0.050000 -0.050000 -0.050000
v -0.050000 0.050000 -0.050000
v 0.050000 -0.050000 0.050000
v 0.050000 0.050000 0.050000
v 0.050000 -0.050000 -0.050000
v 0.050000 0.050000 -0.050000
vn -1.0000 -0.0000 -0.0000
vn -0.0000 -0.0000 -1.0000
vn 1.0000 -0.0000 -0.0000
vn -0.0000 -0.0000 1.0000
vn -0.0000 -1.0000 -0.0000
vn -0.0000 1.0000 -0.0000
vt 0.375000 0.000000
vt 0.375000 1.000000
vt 0.125000 0.750000
vt 0.625000 0.000000
vt 0.625000 1.000000
vt 0.875000 0.750000
vt 0.125000 0.500000
vt 0.375000 0.250000
vt 0.625000 0.250000
vt 0.875000 0.500000
vt 0.375000 0.750000
vt 0.625000 0.750000
vt 0.375000 0.500000
vt 0.625000 0.500000
s 0
f 2/4/1 3/8/1 1/1/1
f 4/9/2 7/13/2 3/8/2
f 8/14/3 5/11/3 7/13/3
f 6/12/4 1/2/4 5/11/4
f 7/13/5 1/3/5 3/7/5
f 4/10/6 6/12/6 8/14/6
f 2/4/1 4/9/1 3/8/1
f 4/9/2 8/14/2 7/13/2
f 8/14/3 6/12/3 5/11/3
f 6/12/4 2/5/4 1/2/4
f 7/13/5 5/11/5 1/3/5
f 4/10/6 2/6/6 6/12/6
)obj";


SCENARIO("Instanciating a silhouette me tracker", "[rbt]")
{
GIVEN("A base me tracker")
Expand Down Expand Up @@ -484,21 +530,26 @@ SCENARIO("Instanciating a render-based tracker", "[rbt]")
},
"vvs": {
"gain": 1.0,
"maxIterations" : 10
"maxIterations" : 10,
"mu": 0.5,
"muIterFactor": 0.1
},
"model" : "/home/sfelton/Downloads/sinatrack-data/data/cutting_guide/cutting_guide.obj",
"model" : "path/to/model.obj",
"silhouetteExtractionSettings" : {
"threshold": {
"type": "relative",
"value" : 0.1
},
"sampling" : {
"type": "fixed",
"numPoints" : 400
"samplingRate": 2,
"numPoints" : 128,
"reusePreviousPoints": true
}
},
"features": {
"silhouetteGeometry": {
"features": [
{
"type": "silhouetteMe",
"weight" : 0.5,
"numCandidates" : 3,
"convergencePixelThreshold" : 3,
Expand All @@ -521,28 +572,80 @@ SCENARIO("Instanciating a render-based tracker", "[rbt]")
"threshold" : 20.0
}
},
"silhouetteColor" : {
{
"type": "silhouetteColor",
"weight" : 0.5,
"convergenceThreshold" : 0.1,
"temporalSmoothing" : 0.1,
"ccd" : {
"h": 4,
"delta_h" : 1
"h": 4,
"delta_h" : 1
}
}
}
}
]
})JSON";
const auto verifyBase = [&tracker]() {
REQUIRE((tracker.getImageHeight() == 240 && tracker.getImageWidth() == 320));
REQUIRE((tracker.getOptimizationGain() == 1.0 && tracker.getMaxOptimizationIters() == 10));
vpSilhouettePointsExtractionSettings silset = tracker.getSilhouetteExtractionParameters();
REQUIRE((silset.thresholdIsRelative() && silset.getThreshold() == 0.1));
REQUIRE((silset.getSampleStep() == 2 && silset.getMaxCandidates() == 128));
REQUIRE((silset.preferPreviousPoints()));

REQUIRE((tracker.getOptimizationGain() == 1.0 && tracker.getMaxOptimizationIters() == 10));
REQUIRE((tracker.getOptimizationInitialMu() == 0.5 && tracker.getOptimizationMuIterFactor() == 0.1));

};
nlohmann::json j = nlohmann::json::parse(jsonLiteral);
THEN("Loading configuration with trackers and a 3D model works")
THEN("Loading configuration with trackers")
{
tracker.loadConfiguration(j);
REQUIRE(...);
verifyBase();
REQUIRE(tracker.getModelPath() == "path/to/model.obj");
AND_THEN("Initializing tracking fails since object does not exist")
{
REQUIRE_THROWS(tracker.startTracking());
}
}
THEN("Loading configuration without model also works")
{
j.erase("model");
tracker.loadConfiguration(j);
REQUIRE(...);
verifyBase();
REQUIRE(tracker.getModelPath() == "");
AND_THEN("Initializing tracking fails since path is not specified")
{
REQUIRE_THROWS(tracker.startTracking());
}
}
THEN("Loading configuration with real 3D model also works")
{
const std::string tempDir = vpIoTools::makeTempDirectory("visp_test_rbt_obj");
const std::string objFile = vpIoTools::createFilePath(tempDir, "cube.obj");
std::ofstream f(objFile);
f << objCube;
f.close();
j["model"] = objFile;
tracker.loadConfiguration(j);
verifyBase();
REQUIRE(tracker.getModelPath() == objFile);
AND_THEN("Initializing tracker works")
{
REQUIRE_NOTHROW(tracker.startTracking());
}
}
}

WHEN("Adding trackers")
{
THEN("Adding nullptr is not allowed")
{
REQUIRE_THROWS(tracker.addTracker(nullptr));
}
THEN("Adding a tracker works")
{
auto ccdTracker = std::make_shared<vpRBSilhouetteCCDTracker>();
tracker.addTracker(ccdTracker);
}
}
#endif
Expand Down

0 comments on commit 86681cf

Please sign in to comment.