46 #include <visp3/core/vpConfig.h>
47 #include <visp3/core/vpException.h>
49 #ifdef VISP_HAVE_NLOHMANN_JSON
50 #include VISP_NLOHMANN_JSON(json.hpp)
161 #if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L)))
179 #if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L)))
197 #if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L)))
203 resize(r, c,
false,
false);
218 vpArray2D(
const std::vector<Type> &vec,
unsigned int r = 0,
unsigned int c = 0)
220 #if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L)))
226 if ((r > 0) && (c > 0)) {
227 if ((r * c) != vec.size()) {
229 "Cannot initialize vpArray(%d, %d) from std::vector(%d). Wrong dimension", r, c, vec.size()));
231 resize(r, c,
false,
false);
233 else if ((c == 0) && (r == 0)) {
235 "Cannot initialize vpArray(%d, %d) from std::vector(%d). Using rows = 0 and cols = 0 is ambiguous", r, c, vec.size()));
238 if (r != vec.size()) {
240 "Cannot initialize vpArray(%d, %d) from std::vector(%d). Wrong dimension", r, c, vec.size()));
242 resize(
static_cast<unsigned int>(vec.size()), 1,
false,
false);
245 if (c != vec.size()) {
247 "Cannot initialize vpArray(%d, %d) from std::vector(%d). Wrong dimension", r, c, vec.size()));
249 resize(1,
static_cast<unsigned int>(vec.size()),
false,
false);
252 #if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L)))
253 std::copy(vec.begin(), vec.end(),
data);
255 memcpy(
data, vec.data(), vec.size() *
sizeof(Type));
259 #if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L)))
277 resize(1,
static_cast<unsigned int>(list.size()),
false,
false);
278 std::copy(list.begin(), list.end(),
data);
281 VP_EXPLICIT
vpArray2D(
unsigned int nrows,
unsigned int ncols,
const std::initializer_list<Type> &list)
284 if ((nrows * ncols) !=
static_cast<unsigned int>(list.size())) {
285 std::ostringstream oss;
286 oss <<
"Cannot create a vpArray2D of size (" << nrows <<
", " << ncols <<
") with a list of size " << list.size();
290 resize(nrows, ncols,
false,
false);
291 std::copy(list.begin(), list.end(),
data);
294 VP_EXPLICIT
vpArray2D(
const std::initializer_list<std::initializer_list<Type> > &lists) :
vpArray2D()
296 unsigned int nrows =
static_cast<unsigned int>(lists.size()), ncols = 0;
297 for (
auto &l : lists) {
298 if (
static_cast<unsigned int>(l.size()) > ncols) {
299 ncols =
static_cast<unsigned int>(l.size());
303 resize(nrows, ncols,
false,
false);
304 auto it = lists.begin();
305 for (
unsigned int i = 0; i <
rowNum; ++i, ++it) {
306 std::copy(it->begin(), it->end(),
rowPtrs[i]);
316 if (
data !=
nullptr) {
362 void resize(
unsigned int nrows,
unsigned int ncols,
bool flagNullify =
true,
bool recopy_ =
true)
365 if (flagNullify && (this->data !=
nullptr)) {
366 memset(this->data, 0, this->
dsize *
sizeof(Type));
370 bool recopy = (!flagNullify) && recopy_;
371 bool colcond = (ncols != this->
colNum) && (this->
colNum > 0) && (ncols > 0);
372 const bool recopyNeeded = colcond && ((!flagNullify) || recopy);
373 Type *copyTmp =
nullptr;
374 unsigned int rowTmp = 0, colTmp = 0;
378 if (recopyNeeded && (this->data !=
nullptr)) {
379 copyTmp =
new Type[this->
dsize];
380 memcpy(copyTmp, this->data,
sizeof(Type) * this->
dsize);
386 this->
dsize = nrows * ncols;
387 Type *tmp_data =
reinterpret_cast<Type *
>(realloc(this->data, this->
dsize *
sizeof(Type)));
389 this->data = tmp_data;
392 this->data =
nullptr;
395 if ((
nullptr == this->data) && (0 != this->
dsize)) {
396 if (copyTmp !=
nullptr) {
402 Type **tmp_rowPtrs =
reinterpret_cast<Type **
>(realloc(this->
rowPtrs, nrows *
sizeof(Type *)));
410 if (copyTmp !=
nullptr) {
414 "Memory allocation error when allocating 2D array rowPtrs"));
420 for (
unsigned int i = 0; i <
dsize; i += ncols) {
421 *t_++ = this->data + i;
430 if ((
nullptr != this->data) && (0 != this->
dsize)) {
431 memset(this->data, 0,
static_cast<size_t>(this->dsize) *
sizeof(Type));
434 else if (recopyNeeded && (this->
rowPtrs !=
nullptr)) {
436 unsigned int minRow = (this->
rowNum < rowTmp) ? this->
rowNum : rowTmp;
437 unsigned int minCol = (this->
colNum < colTmp) ? this->
colNum : colTmp;
438 for (
unsigned int i = 0; i < this->
rowNum; ++i) {
439 for (
unsigned int j = 0; j < this->
colNum; ++j) {
440 if ((minRow > i) && (minCol > j)) {
441 (*this)[i][j] = copyTmp[(i * colTmp) + j];
450 if (copyTmp !=
nullptr) {
456 void reshape(
unsigned int nrows,
unsigned int ncols)
463 if ((nrows * ncols) !=
dsize) {
464 std::ostringstream oss;
465 oss <<
"Cannot reshape array of total size " <<
dsize <<
" into shape (" << nrows <<
", " << ncols <<
")";
472 Type **tmp =
reinterpret_cast<Type **
>(realloc(
rowPtrs, nrows *
sizeof(Type *)));
480 for (
unsigned int i = 0; i <
dsize; i += ncols) {
504 unsigned int a_rows = A.
getRows();
505 for (
unsigned int i = r; i < (r + a_rows); ++i) {
544 #if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L)))
547 if (
this != &other) {
563 other.rowPtrs =
nullptr;
565 other.data =
nullptr;
573 if (
dsize !=
static_cast<unsigned int>(list.size())) {
574 resize(1,
static_cast<unsigned int>(list.size()),
false,
false);
576 std::copy(list.begin(), list.end(),
data);
583 unsigned int nrows =
static_cast<unsigned int>(lists.size()), ncols = 0;
584 for (
auto &l : lists) {
585 if (
static_cast<unsigned int>(l.size()) > ncols) {
586 ncols =
static_cast<unsigned int>(l.size());
590 resize(nrows, ncols,
false,
false);
591 auto it = lists.begin();
592 for (
unsigned int i = 0; i <
rowNum; ++i, ++it) {
593 std::copy(it->begin(), it->end(),
rowPtrs[i]);
599 #ifdef VISP_HAVE_NLOHMANN_JSON
616 if ((A.
data ==
nullptr) || (A.
size() == 0)) {
619 std::ios_base::fmtflags original_flags = s.flags();
620 const unsigned int precision = 10;
621 s.precision(precision);
622 unsigned int a_rows = A.
getRows();
623 unsigned int a_cols = A.
getCols();
624 for (
unsigned int i = 0; i < a_rows; ++i) {
625 for (
unsigned int j = 0; j < (a_cols - 1); ++j) {
629 s << A[i][a_cols - 1];
631 if (i < (a_rows - 1)) {
636 s.flags(original_flags);
669 static bool load(
const std::string &filename,
vpArray2D<Type> &A,
bool binary =
false,
char *header =
nullptr)
674 file.open(filename.c_str(), std::fstream::in);
677 file.open(filename.c_str(), std::fstream::in | std::fstream::binary);
687 bool headerIsDecoded =
false;
689 std::streampos pos = file.tellg();
690 char line[FILENAME_MAX];
691 file.getline(line, FILENAME_MAX);
692 std::string prefix(
"# ");
693 std::string line_(line);
694 if (line_.compare(0, prefix.size(), prefix.c_str()) == 0) {
701 h += line_.substr(prefix.size());
705 file.seekg(pos, file.beg);
706 headerIsDecoded =
true;
708 }
while (!headerIsDecoded);
710 if (header !=
nullptr) {
711 #if defined(__MINGW32__) || \
712 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
713 snprintf(header, h.size() + 1,
"%s", h.c_str());
715 _snprintf_s(header, h.size() + 1, _TRUNCATE,
"%s", h.c_str());
719 unsigned int rows, cols;
723 if ((rows >= std::numeric_limits<unsigned int>::max()) || (cols >= std::numeric_limits<unsigned int>::max())) {
730 for (
unsigned int i = 0; i < rows; ++i) {
731 for (
unsigned int j = 0; j < cols; ++j) {
745 if (header !=
nullptr) {
746 #if defined(__MINGW32__) || \
747 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
748 snprintf(header, h.size() + 1,
"%s", h.c_str());
750 _snprintf_s(header, h.size() + 1, _TRUNCATE,
"%s", h.c_str());
754 unsigned int rows, cols;
755 file.read(
reinterpret_cast<char *
>(&rows),
sizeof(
unsigned int));
756 file.read(
reinterpret_cast<char *
>(&cols),
sizeof(
unsigned int));
760 for (
unsigned int i = 0; i < rows; ++i) {
761 for (
unsigned int j = 0; j < cols; ++j) {
762 file.read(
reinterpret_cast<char *
>(&value),
sizeof(Type));
787 file.open(filename.c_str(), std::fstream::in);
794 unsigned int rows = 0, cols = 0;
796 std::string line, subs;
797 bool inheader =
true;
798 unsigned int i = 0, j;
799 unsigned int lineStart = 0;
801 while (getline(file, line)) {
803 const std::string str_rows(
"rows:");
804 const std::string str_cols(
"cols:");
805 const std::string str_data(
"data:");
806 if ((rows == 0) && (line.compare(0, str_rows.size(), str_rows.c_str()) == 0)) {
807 std::stringstream ss(line);
811 else if ((cols == 0) && (line.compare(0, str_cols.size(), str_cols.c_str()) == 0)) {
812 std::stringstream ss(line);
816 else if (line.compare(0, str_data.size(), str_data.c_str()) == 0) {
827 if ((rows == 0) || (cols == 0)) {
833 lineStart =
static_cast<unsigned int>(line.find(
"[")) + 1;
835 std::stringstream ss(line.substr(lineStart, line.find(
"]") - lineStart));
837 while (getline(ss, subs,
',')) {
838 A[i][j++] = atof(subs.c_str());
844 if (header !=
nullptr) {
845 std::string h_ = h.substr(0, h.size() - 1);
846 #if defined(__MINGW32__) || \
847 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
848 snprintf(header, h_.size() + 1,
"%s", h_.c_str());
850 _snprintf_s(header, h_.size() + 1, _TRUNCATE,
"%s", h_.c_str());
874 static bool save(
const std::string &filename,
const vpArray2D<Type> &A,
bool binary =
false,
const char *header =
"")
879 file.open(filename.c_str(), std::fstream::out);
882 file.open(filename.c_str(), std::fstream::out | std::fstream::binary);
893 while (header[i] !=
'\0') {
895 if (header[i] ==
'\n') {
902 file << A << std::endl;
906 while (header[headerSize] !=
'\0') {
909 file.write(header,
static_cast<size_t>(headerSize)+
static_cast<size_t>(1));
910 unsigned int matrixSize;
912 file.write(
reinterpret_cast<char *
>(&matrixSize),
sizeof(
unsigned int));
914 file.write(
reinterpret_cast<char *
>(&matrixSize),
sizeof(
unsigned int));
916 unsigned int a_rows = A.
getRows();
917 unsigned int a_cols = A.
getCols();
918 for (
unsigned int i = 0; i < a_rows; ++i) {
919 for (
unsigned int j = 0; j < a_cols; ++j) {
921 file.write(
reinterpret_cast<char *
>(&value),
sizeof(Type));
976 file.open(filename.c_str(), std::fstream::out);
984 bool inIndent =
false;
985 std::string indent =
"";
986 bool checkIndent =
true;
987 while (header[i] !=
'\0') {
991 if (header[i] ==
' ') {
994 else if (indent.length() > 0) {
998 if ((header[i] ==
'\n') || (inIndent && (header[i] ==
' '))) {
1011 file <<
"rows: " << A.
getRows() << std::endl;
1012 file <<
"cols: " << A.
getCols() << std::endl;
1014 if (indent.length() == 0) {
1018 file <<
"data: " << std::endl;
1020 unsigned int a_rows = A.
getRows();
1021 unsigned int a_cols = A.
getCols();
1022 for (i = 0; i < a_rows; ++i) {
1023 file << indent <<
"- [";
1024 for (j = 0; j < (a_cols - 1); ++j) {
1025 file << A[i][j] <<
", ";
1027 file << A[i][j] <<
"]" << std::endl;
1033 #ifdef VISP_HAVE_NLOHMANN_JSON
1115 Type *dataptr = data;
1116 Type min = *dataptr;
1118 for (
unsigned int i = 0; i < (dsize - 1); ++i) {
1119 if (*dataptr < min) {
1132 Type *dataptr = data;
1133 Type max = *dataptr;
1135 for (
unsigned int i = 0; i < (dsize - 1); ++i) {
1136 if (*dataptr > max) {
1157 out.
resize(rowNum, colNum,
false);
1159 for (
unsigned int i = 0; i < dsize; ++i) {
1169 for (
unsigned int i = 0; i < rowNum; ++i) {
1170 for (
unsigned int j = 0; j < colNum; ++j) {
1171 At[j][i] = (*this)[i][j];
1180 conv2(M, kernel, res, mode);
1190 if (mode ==
"valid") {
1198 if ((mode ==
"full") || (mode ==
"same")) {
1199 const unsigned int pad_x = kernel.
getCols() - 1;
1200 const unsigned int pad_y = kernel.
getRows() - 1;
1201 const unsigned int pad = 2;
1203 M_padded.
insert(M, pad_y, pad_x);
1205 if (mode ==
"same") {
1213 else if (mode ==
"valid") {
1221 if (mode ==
"same") {
1222 unsigned int res_same_rows = res_same.
getRows();
1223 unsigned int res_same_cols = res_same.
getCols();
1224 unsigned int kernel_rows = kernel.
getRows();
1225 unsigned int kernel_cols = kernel.
getCols();
1226 for (
unsigned int i = 0; i < res_same_rows; ++i) {
1227 for (
unsigned int j = 0; j < res_same_cols; ++j) {
1228 for (
unsigned int k = 0; k < kernel_rows; ++k) {
1229 for (
unsigned int l = 0; l < kernel_cols; ++l) {
1230 res_same[i][j] += M_padded[i + k][j + l] * kernel[kernel.
getRows() - k - 1][kernel.
getCols() - l - 1];
1236 const unsigned int start_i = kernel.
getRows() / 2;
1237 const unsigned int start_j = kernel.
getCols() / 2;
1238 unsigned int m_rows = M.
getRows();
1239 for (
unsigned int i = 0; i < m_rows; ++i) {
1245 unsigned int res_rows = res.
getRows();
1246 unsigned int res_cols = res.
getCols();
1247 unsigned int kernel_rows = kernel.
getRows();
1248 unsigned int kernel_cols = kernel.
getCols();
1249 for (
unsigned int i = 0; i < res_rows; ++i) {
1250 for (
unsigned int j = 0; j < res_cols; ++j) {
1251 for (
unsigned int k = 0; k < kernel_rows; ++k) {
1252 for (
unsigned int l = 0; l < kernel_cols; ++l) {
1253 res[i][j] += M_padded[i + k][j + l] * kernel[kernel.
getRows() - k - 1][kernel.
getCols() - l - 1];
1275 unsigned int a_rows = A.
getRows();
1276 unsigned int a_cols = A.
getCols();
1277 for (
unsigned int i = 0; i < a_rows; ++i) {
1278 for (
unsigned int j = 0; j < a_cols; ++j) {
1279 if ((i >= r) && (i < (r + B.
getRows())) && (j >= c) && (j < (c + B.
getCols()))) {
1280 C[i][j] = B[i - r][j - c];
1300 unsigned int a_size = A.
size();
1301 for (
unsigned int i = 0; i < a_size; ++i) {
1302 if (data[i] != A.
data[i]) {
1319 unsigned int a_size = A.
size();
1320 for (
unsigned int i = 0; i < a_size; ++i) {
1321 if (fabs(
data[i] - A.
data[i]) > std::numeric_limits<double>::epsilon()) {
1338 unsigned int a_size = A.
size();
1339 for (
unsigned int i = 0; i < a_size; ++i) {
1340 if (fabsf(
data[i] - A.
data[i]) > std::numeric_limits<float>::epsilon()) {
1353 #ifdef VISP_HAVE_NLOHMANN_JSON
1354 template <
class Type>
1355 inline void from_json(
const nlohmann::json &j,
vpArray2D<Type> &array)
1358 const unsigned int nrows =
static_cast<unsigned int>(j.size());
1363 unsigned int ncols = 0;
1365 for (
const auto &item : j) {
1366 if (!item.is_array()) {
1371 ncols =
static_cast<unsigned int>(item.size());
1373 else if (ncols != item.size()) {
1377 array.
resize(nrows, ncols);
1379 for (
const auto &item : j) {
1380 std::vector<Type> row = item;
1381 std::copy(row.begin(), row.end(), array.
rowPtrs[i]);
1385 else if (j.is_object()) {
1386 const unsigned ncols = j.at(
"cols");
1387 const unsigned nrows = j.at(
"rows");
1388 array.
resize(nrows, ncols);
1389 const nlohmann::json jData = j.at(
"data");
1390 if (!jData.is_array() || jData.size() != nrows * ncols) {
1391 std::stringstream ss;
1392 ss <<
"JSON \"data\" field must be an array of size " << nrows * ncols;
1396 for (
const auto &jValue : jData) {
1397 array.
data[i] = jValue;
1407 template <
class Type>
1413 {
"type",
"vpArray2D"}
1416 nlohmann::json::array_t data;
1417 data.reserve(array.
size());
1418 for (
unsigned i = 0; i < array.
size(); ++i) {
1419 data.push_back(array.
data[i]);
Implementation of a generic 2D array used as base class for matrices and vectors.
unsigned int getCols() const
vpArray2D< Type > insert(const vpArray2D< Type > &A, const vpArray2D< Type > &B, unsigned int r, unsigned int c)
static void conv2(const vpArray2D< Type > &M, const vpArray2D< Type > &kernel, vpArray2D< Type > &res, const std::string &mode)
Type * data
Address of the first element of the data array.
vpArray2D(const vpArray2D< Type > &A)
static bool loadYAML(const std::string &filename, vpArray2D< Type > &A, char *header=nullptr)
Type ** rowPtrs
Address of the first element of each rows.
vpArray2D(const std::vector< Type > &vec, unsigned int r=0, unsigned int c=0)
void insert(const vpArray2D< Type > &A, unsigned int r, unsigned int c)
vpArray2D< Type > & operator=(const vpArray2D< Type > &A)
Type * operator[](unsigned int i)
Set element using A[i][j] = x.
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
static void insert(const vpArray2D< Type > &A, const vpArray2D< Type > &B, vpArray2D< Type > &C, unsigned int r, unsigned int c)
static bool saveYAML(const std::string &filename, const vpArray2D< Type > &A, const char *header="")
unsigned int rowNum
Number of rows in the array.
friend void to_json(nlohmann::json &j, const vpArray2D< T > &array)
static vpArray2D< Type > conv2(const vpArray2D< Type > &M, const vpArray2D< Type > &kernel, const std::string &mode)
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
unsigned int dsize
Current array size (rowNum * colNum)
unsigned int size() const
Return the number of elements of the 2D array.
static bool load(const std::string &filename, vpArray2D< Type > &A, bool binary=false, char *header=nullptr)
vpArray2D< Type > t() const
Compute the transpose of the array.
unsigned int getRows() const
bool operator!=(const vpArray2D< Type > &A) const
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
vpArray2D(unsigned int r, unsigned int c, Type val)
vpArray2D(unsigned int r, unsigned int c)
vpArray2D< Type > hadamard(const vpArray2D< Type > &m) const
static bool save(const std::string &filename, const vpArray2D< Type > &A, bool binary=false, const char *header="")
friend void from_json(const nlohmann::json &j, vpArray2D< T > &array)
void reshape(unsigned int nrows, unsigned int ncols)
Type * operator[](unsigned int i) const
Get element using x = A[i][j].
unsigned int colNum
Number of columns in the array.
bool operator==(const vpArray2D< Type > &A) const
error that can be emitted by ViSP classes.
@ badValue
Used to indicate that a value is not in the allowed range.
@ dimensionError
Bad dimension.
@ memoryAllocationError
Memory allocation error.