libdart_openmvg/src/tests/test_frame.cxx

130 lines
3.3 KiB
C++

#include <sys/stat.h>
#include <archives/binary.hpp>
#include <CUnit/Basic.h>
#include <errno.h>
#include <ios>
#include "../image.h"
#include "../streamingview.hpp"
#include "../util.h"
#include <cwalk.h>
#ifdef __cplusplus
#include <iostream>
#endif // __cplusplus
#define STRLEN 2048
#define FRAME_COUNT 200
typedef Frame CFrame;
char *framePaths[FRAME_COUNT];
#define HERE __FILE__
int setUp(void)
{
char framesDir[STRLEN];
size_t length;
char framePathTpl[STRLEN];
cwk_path_get_dirname(HERE, &length); // archimedes_mobile_lib/src/tests
cwk_path_join(HERE, "../../../assets/test/frames", framesDir, STRLEN);
cwk_path_join(framesDir, "%04d.png", framePathTpl, STRLEN);
// Set the length to be the length of the "template" plus enough room
// for the sequence number.
size_t pathLen = strlen(framePathTpl) + 5;
for (size_t i = 0; i < FRAME_COUNT; ++i)
{
framePaths[i] = (char *)calloc(pathLen, sizeof(char *));
snprintf(framePaths[i], pathLen, framePathTpl, i+1, 4);
printf("Load frame %s\n", framePaths[i]);
}
return 0;
}
CFrame *get_nth_frame(size_t frame_num)
{
struct stat statbuf;
if (stat(framePaths[frame_num], &statbuf) < 0)
{
// std::cerr << "ERROR reading \"" << framePaths << "\"" << std::endl;
std::cerr << std::string("Reading \"") << std::string(framePaths[frame_num]) << std::string("\"") << std::endl;
throw new std::ios_base::failure(std::string(strerror(errno)));
}
FILE *handle = fopen(framePaths[frame_num], "r");
CFrame *f = new_frame_from_handle(
handle,
768,
768,
64);
return f;
}
int tearDown(void)
{
for (int i = 0; i < FRAME_COUNT; ++i)
{
free(framePaths[i]);
}
return 0;
}
void test_calculate_fetures(void)
{
Frame *f = get_nth_frame(0);
DartOpenMvg::Frame frame(f);
frame.calculateFeatures();
CU_ASSERT_TRUE(frame.mRegions.get()->RegionCount() > 0);
}
void test_compute_matches(void)
{
Frame *f1 = get_nth_frame(0);
Frame *f2 = get_nth_frame(1);
DartOpenMvg::Frame frame1(f1), frame2(f2);
openMVG::sfm::Bundle_Adjustment_Ceres::BA_Ceres_options opts;
DartOpenMvg::Frames frames(opts);
frame1.calculateFeatures();
frame2.calculateFeatures();
frames.mFrames.push_back(frame1);
frames.mFrames.push_back(frame2);
CU_ASSERT_EQUAL(frames.mFeatureMap.size(), 0)
frames.computeMatches();
CU_ASSERT_TRUE(frames.mFeatureMap.size() > 0);
}
int main()
{
CU_pSuite pSuite = NULL;
/* initialize the CUnit test registry */
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();
/* add a suite to the registry */
pSuite = CU_add_suite("FrameSuite", setUp, tearDown);
if (NULL == pSuite)
{
CU_cleanup_registry();
return CU_get_error();
}
/* add the tests to the suite */
if ((NULL == CU_add_test(pSuite, "frame features can be calculated", test_calculate_fetures)) ||
(NULL == CU_add_test(pSuite, "matches can be computed", test_compute_matches)))
{
CU_cleanup_registry();
return CU_get_error();
}
/* Run all tests using the CUnit Basic interface */
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_error();
}