#include <visp3/core/vpConfig.h>
#if defined(VISP_HAVE_CATCH2)
#include <catch_amalgamated.hpp>
#include <cmath>
#include <limits>
#include <vector>
#include <visp3/core/vpTranslationVector.h>
#ifdef ENABLE_VISP_NAMESPACE
#endif
template <
typename Type>
bool test(
const std::string &s,
const vpArray2D<Type> &A,
const std::vector<Type> &bench)
{
static unsigned int cpt = 0;
std::cout << "** Test " << ++cpt << std::endl;
std::cout << s <<
"(" << A.
getRows() <<
"," << A.
getCols() <<
") = \n" << A << std::endl;
if (bench.size() != A.
size()) {
std::cout << "Test fails: bad size wrt bench" << std::endl;
return false;
}
for (
unsigned int i = 0; i < A.
size(); i++) {
if (std::fabs(A.
data[i] - bench[i]) > std::fabs(A.
data[i]) * std::numeric_limits<double>::epsilon()) {
std::cout << "Test fails: bad content" << std::endl;
return false;
}
}
return true;
}
template <typename Type>
{
std::cout << "Test fails: bad columns size" << std::endl;
return false;
}
std::cout << "Test fails: bad rows size" << std::endl;
return false;
}
for (
unsigned int i = 0; i < A.
size(); i++) {
if (std::fabs((A.
data[i] * B.
data[i]) - H.
data[i]) > std::numeric_limits<double>::epsilon()) {
std::cout << "Test fails: bad content" << std::endl;
return false;
}
}
return true;
}
TEST_CASE("Test constructors with double", "[constructors]")
{
SECTION("Default constructor")
{
std::vector<double> bench;
CHECK(test("A", A, bench));
}
SECTION("Copy constructor")
{
std::vector<double> bench(12);
for (unsigned int i = 0; i < 3; i++) {
for (unsigned int j = 0; j < 4; j++) {
A[i][j] = (double)(i + j);
bench[i * 4 + j] = (double)(i + j);
}
}
CHECK(test("A", A, bench));
CHECK(test("B", B, bench));
}
SECTION("Constructor with initial value")
{
std::vector<double> bench1(12, 2);
CHECK(test("A", A, bench1));
std::vector<double> bench2(30, 0);
CHECK(test("A", A, bench2));
A = -2.;
std::vector<double> bench3(30, -2);
CHECK(test("A", A, bench3));
}
SECTION("Constructor from std::vector")
{
std::vector<double> bench(12);
for (unsigned int i = 0; i < 3; i++) {
for (unsigned int j = 0; j < 4; j++) {
bench[i * 4 + j] = (double)(i + j);
}
}
SECTION("Keep default size (r=0, c=0)")
{
std::cout << "A with default size (r=0, c=0):\n" << std::endl;
}
SECTION("Keep row size to 0")
{
unsigned int size = static_cast<unsigned int>(bench.size());
std::cout << "A with row size to 0:\n" << A << std::endl;
CHECK(test("A", A, bench));
CHECK(A.
getCols() == bench.size());
}
SECTION("Keep col size to 0")
{
unsigned int size = static_cast<unsigned int>(bench.size());
std::cout << "A with col size to 0:\n" << A << std::endl;
CHECK(test("A", A, bench));
CHECK(A.
getRows() == bench.size());
}
SECTION("Set r=3 and c=4")
{
std::cout << "A with r=3 and c=4:\n" << A << std::endl;
CHECK(test("A", A, bench));
}
}
}
TEST_CASE("Test constructors with float", "[constructors]")
{
SECTION("Default constructor")
{
std::vector<float> bench;
CHECK(test("A", A, bench));
}
SECTION("Copy constructor")
{
std::vector<float> bench(12);
for (unsigned int i = 0; i < 3; i++) {
for (unsigned int j = 0; j < 4; j++) {
A[i][j] = (float)(i + j);
bench[i * 4 + j] = (float)(i + j);
}
}
CHECK(test("A", A, bench));
CHECK(test("B", B, bench));
}
SECTION("Constructor with initial value")
{
std::vector<float> bench1(12, 2);
CHECK(test("A", A, bench1));
std::vector<float> bench2(30, 0);
CHECK(test("A", A, bench2));
A = -2.;
std::vector<float> bench3(30, -2);
CHECK(test("A", A, bench3));
}
SECTION("Constructor from std::vector")
{
std::vector<float> bench(12);
for (unsigned int i = 0; i < 3; i++) {
for (unsigned int j = 0; j < 4; j++) {
bench[i * 4 + j] = (float)(i + j);
}
}
SECTION("Keep default size (r=0, c=0)")
{
std::cout << "A with default size (r=0, c=0):\n" << std::endl;
}
SECTION("Keep row size to 0")
{
unsigned int size = static_cast<unsigned int>(bench.size());
std::cout << "A with row size to 0:\n" << A << std::endl;
CHECK(test("A", A, bench));
CHECK(A.
getCols() == bench.size());
}
SECTION("Keep col size to 0")
{
unsigned int size = static_cast<unsigned int>(bench.size());
std::cout << "A with col size to 0:\n" << A << std::endl;
CHECK(test("A", A, bench));
CHECK(A.
getRows() == bench.size());
}
SECTION("Set r=3 and c=4")
{
std::cout << "A with r=3 and c=4:\n" << A << std::endl;
CHECK(test("A", A, bench));
}
}
}
TEST_CASE("Test Hadamar product", "[hadamar]")
{
for (unsigned int i = 0; i < A1.size(); i++) {
A2.data[i] = i + 2;
R1.data[i] = i;
R2.data[i] = i + 2;
C1.data[i] = i;
C2.data[i] = i + 2;
}
std::cout << "A1:\n" << A1 << std::endl;
std::cout << "\nA2:\n" << A2 << std::endl;
CHECK(test_hadamar(A1, A2, A3));
std::cout << "\nRes hadamar(A1, A2):\n" << A3 << std::endl;
std::cout << "\nR1:\n" << R1 << std::endl;
std::cout << "\nR2:\n" << R2 << std::endl;
CHECK(test_hadamar(R1, R2, R3));
std::cout << "\nRes hadamar(R1, R2):\n" << R3 << std::endl;
std::cout << "\nC1:\n" << C1 << std::endl;
std::cout << "\nC2:\n" << C2 << std::endl;
CHECK(test_hadamar(C1, C2, C3));
std::cout << "\nRes hadamar(C1, C2):\n" << C3 << std::endl;
}
int main(int argc, char *argv[])
{
Catch::Session session;
session.applyCommandLine(argc, argv);
int numFailed = session.run();
std::cout << (numFailed ? "Test failed" : "Test succeed") << std::endl;
return numFailed;
}
#else
int main() { return EXIT_SUCCESS; }
#endif
Implementation of a generic 2D array used as base class for matrices and vectors.
unsigned int getCols() const
Type * data
Address of the first element of the data array.
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
unsigned int size() const
Return the number of elements of the 2D array.
unsigned int getRows() const
vpArray2D< Type > hadamard(const vpArray2D< Type > &m) const
Implementation of column vector and the associated operations.
vpColVector hadamard(const vpColVector &v) const
Implementation of row vector and the associated operations.
vpRowVector hadamard(const vpRowVector &v) const