#include <cmath>
#include <limits>
#include <vector>
#include <visp3/core/vpTranslationVector.h>
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;
}
int main()
{
{
std::vector<double> bench;
if (test("A", A, bench) == false)
return EXIT_FAILURE;
}
{
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);
}
}
if (test("A", A, bench) == false)
return EXIT_FAILURE;
if (test("B", B, bench) == false)
return EXIT_FAILURE;
std::cout << "Min/Max: " << B.getMinValue() << " " << B.getMaxValue() << std::endl;
}
{
std::vector<double> bench1(12, 2);
if (test("A", A, bench1) == false)
return EXIT_FAILURE;
std::vector<double> bench2(30, 0);
if (test("A", A, bench2) == false)
return EXIT_FAILURE;
A = -2.;
std::vector<double> bench3(30, -2);
if (test("A", A, bench3) == false)
return EXIT_FAILURE;
}
{
std::vector<float> bench;
if (test("A", A, bench) == false)
return EXIT_FAILURE;
}
{
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);
}
}
if (test("A", A, bench) == false)
return EXIT_FAILURE;
if (test("B", B, bench) == false)
return EXIT_FAILURE;
std::cout << "Min/Max: " << B.getMinValue() << " " << B.getMaxValue() << std::endl;
}
{
std::vector<float> bench1(12, 2);
if (test("A", A, bench1) == false)
return EXIT_FAILURE;
std::vector<float> bench2(30, 0);
if (test("A", A, bench2) == false)
return EXIT_FAILURE;
A = -2.;
std::vector<float> bench3(30, -2);
if (test("A", A, bench3) == false)
return EXIT_FAILURE;
}
{
std::cout << "\nTest Hadamard product" << std::endl;
for (unsigned int i = 0; i < A1.size(); i++) {
A1.data[i] = 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;
A2 = A1.hadamard(A2);
std::cout << "\nRes:\n" << A2 << std::endl;
std::cout << "\nR1:\n" << R1 << std::endl;
std::cout << "\nR2:\n" << R2 << std::endl;
R2 = R1.hadamard(R2);
std::cout << "\nRes:\n" << R2 << std::endl;
std::cout << "\nC1:\n" << C1 << std::endl;
std::cout << "\nC2:\n" << C2 << std::endl;
C2 = C1.hadamard(C2);
std::cout << "\nRes:\n" << C2 << std::endl;
}
std::cout << "All tests succeed" << std::endl;
return EXIT_SUCCESS;
}
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
Implementation of column vector and the associated operations.
Implementation of row vector and the associated operations.