46 #include <visp3/core/vpConfig.h>
47 #include <visp3/core/vpException.h>
49 #ifdef VISP_HAVE_NLOHMANN_JSON
50 #include <nlohmann/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<Type>(
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<Type>(
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);
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;
429 memset(this->data, 0,
static_cast<size_t>(this->dsize) *
sizeof(Type));
431 else if (recopyNeeded && (this->
rowPtrs !=
nullptr)) {
433 unsigned int minRow = (this->
rowNum < rowTmp) ? this->
rowNum : rowTmp;
434 unsigned int minCol = (this->
colNum < colTmp) ? this->
colNum : colTmp;
435 for (
unsigned int i = 0; i < this->
rowNum; ++i) {
436 for (
unsigned int j = 0; j < this->
colNum; ++j) {
437 if ((minRow > i) && (minCol > j)) {
438 (*this)[i][j] = copyTmp[(i * colTmp) + j];
447 if (copyTmp !=
nullptr) {
453 void reshape(
unsigned int nrows,
unsigned int ncols)
460 if ((nrows * ncols) !=
dsize) {
461 std::ostringstream oss;
462 oss <<
"Cannot reshape array of total size " <<
dsize <<
" into shape (" << nrows <<
", " << ncols <<
")";
469 Type **tmp =
reinterpret_cast<Type **
>(realloc(
rowPtrs, nrows *
sizeof(Type *)));
477 for (
unsigned int i = 0; i <
dsize; i += ncols) {
501 unsigned int a_rows = A.
getRows();
502 for (
unsigned int i = r; i < (r + a_rows); ++i) {
541 #if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L)))
544 if (
this != &other) {
560 other.rowPtrs =
nullptr;
562 other.data =
nullptr;
570 if (
dsize !=
static_cast<unsigned int>(list.size())) {
571 resize(1,
static_cast<unsigned int>(list.size()),
false,
false);
573 std::copy(list.begin(), list.end(),
data);
580 unsigned int nrows =
static_cast<unsigned int>(lists.size()), ncols = 0;
581 for (
auto &l : lists) {
582 if (
static_cast<unsigned int>(l.size()) > ncols) {
583 ncols =
static_cast<unsigned int>(l.size());
587 resize(nrows, ncols,
false,
false);
588 auto it = lists.begin();
589 for (
unsigned int i = 0; i <
rowNum; ++i, ++it) {
590 std::copy(it->begin(), it->end(),
rowPtrs[i]);
596 #ifdef VISP_HAVE_NLOHMANN_JSON
613 if ((A.
data ==
nullptr) || (A.
size() == 0)) {
616 std::ios_base::fmtflags original_flags = s.flags();
617 const unsigned int precision = 10;
618 s.precision(precision);
619 unsigned int a_rows = A.
getRows();
620 unsigned int a_cols = A.
getCols();
621 for (
unsigned int i = 0; i < a_rows; ++i) {
622 for (
unsigned int j = 0; j < (a_cols - 1); ++j) {
626 s << A[i][a_cols - 1];
628 if (i < (a_rows - 1)) {
633 s.flags(original_flags);
666 static bool load(
const std::string &filename,
vpArray2D<Type> &A,
bool binary =
false,
char *header =
nullptr)
671 file.open(filename.c_str(), std::fstream::in);
674 file.open(filename.c_str(), std::fstream::in | std::fstream::binary);
684 bool headerIsDecoded =
false;
686 std::streampos pos = file.tellg();
687 char line[FILENAME_MAX];
688 file.getline(line, FILENAME_MAX);
689 std::string prefix(
"# ");
690 std::string line_(line);
691 if (line_.compare(0, prefix.size(), prefix.c_str()) == 0) {
698 h += line_.substr(prefix.size());
702 file.seekg(pos, file.beg);
703 headerIsDecoded =
true;
705 }
while (!headerIsDecoded);
707 if (header !=
nullptr) {
708 #if defined(__MINGW32__) || \
709 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
710 snprintf(header, h.size() + 1,
"%s", h.c_str());
712 _snprintf_s(header, h.size() + 1, _TRUNCATE,
"%s", h.c_str());
716 unsigned int rows, cols;
720 if ((rows >= std::numeric_limits<unsigned int>::max()) || (cols >= std::numeric_limits<unsigned int>::max())) {
727 for (
unsigned int i = 0; i < rows; ++i) {
728 for (
unsigned int j = 0; j < cols; ++j) {
742 if (header !=
nullptr) {
743 #if defined(__MINGW32__) || \
744 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
745 snprintf(header, h.size() + 1,
"%s", h.c_str());
747 _snprintf_s(header, h.size() + 1, _TRUNCATE,
"%s", h.c_str());
751 unsigned int rows, cols;
752 file.read(
reinterpret_cast<char *
>(&rows),
sizeof(
unsigned int));
753 file.read(
reinterpret_cast<char *
>(&cols),
sizeof(
unsigned int));
757 for (
unsigned int i = 0; i < rows; ++i) {
758 for (
unsigned int j = 0; j < cols; ++j) {
759 file.read(
reinterpret_cast<char *
>(&value),
sizeof(Type));
784 file.open(filename.c_str(), std::fstream::in);
791 unsigned int rows = 0, cols = 0;
793 std::string line, subs;
794 bool inheader =
true;
795 unsigned int i = 0, j;
796 unsigned int lineStart = 0;
798 while (getline(file, line)) {
800 const std::string str_rows(
"rows:");
801 const std::string str_cols(
"cols:");
802 const std::string str_data(
"data:");
803 if ((rows == 0) && (line.compare(0, str_rows.size(), str_rows.c_str()) == 0)) {
804 std::stringstream ss(line);
808 else if ((cols == 0) && (line.compare(0, str_cols.size(), str_cols.c_str()) == 0)) {
809 std::stringstream ss(line);
813 else if (line.compare(0, str_data.size(), str_data.c_str()) == 0) {
824 if ((rows == 0) || (cols == 0)) {
830 lineStart =
static_cast<unsigned int>(line.find(
"[")) + 1;
832 std::stringstream ss(line.substr(lineStart, line.find(
"]") - lineStart));
834 while (getline(ss, subs,
',')) {
835 A[i][j++] = atof(subs.c_str());
841 if (header !=
nullptr) {
842 std::string h_ = h.substr(0, h.size() - 1);
843 #if defined(__MINGW32__) || \
844 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
845 snprintf(header, h_.size() + 1,
"%s", h_.c_str());
847 _snprintf_s(header, h_.size() + 1, _TRUNCATE,
"%s", h_.c_str());
871 static bool save(
const std::string &filename,
const vpArray2D<Type> &A,
bool binary =
false,
const char *header =
"")
876 file.open(filename.c_str(), std::fstream::out);
879 file.open(filename.c_str(), std::fstream::out | std::fstream::binary);
890 while (header[i] !=
'\0') {
892 if (header[i] ==
'\n') {
899 file << A << std::endl;
903 while (header[headerSize] !=
'\0') {
906 file.write(header,
static_cast<size_t>(headerSize)+
static_cast<size_t>(1));
907 unsigned int matrixSize;
909 file.write(
reinterpret_cast<char *
>(&matrixSize),
sizeof(
unsigned int));
911 file.write(
reinterpret_cast<char *
>(&matrixSize),
sizeof(
unsigned int));
913 unsigned int a_rows = A.
getRows();
914 unsigned int a_cols = A.
getCols();
915 for (
unsigned int i = 0; i < a_rows; ++i) {
916 for (
unsigned int j = 0; j < a_cols; ++j) {
918 file.write(
reinterpret_cast<char *
>(&value),
sizeof(Type));
973 file.open(filename.c_str(), std::fstream::out);
981 bool inIndent =
false;
982 std::string indent =
"";
983 bool checkIndent =
true;
984 while (header[i] !=
'\0') {
988 if (header[i] ==
' ') {
991 else if (indent.length() > 0) {
995 if ((header[i] ==
'\n') || (inIndent && (header[i] ==
' '))) {
1008 file <<
"rows: " << A.
getRows() << std::endl;
1009 file <<
"cols: " << A.
getCols() << std::endl;
1011 if (indent.length() == 0) {
1015 file <<
"data: " << std::endl;
1017 unsigned int a_rows = A.
getRows();
1018 unsigned int a_cols = A.
getCols();
1019 for (i = 0; i < a_rows; ++i) {
1020 file << indent <<
"- [";
1021 for (j = 0; j < (a_cols - 1); ++j) {
1022 file << A[i][j] <<
", ";
1024 file << A[i][j] <<
"]" << std::endl;
1030 #ifdef VISP_HAVE_NLOHMANN_JSON
1112 Type *dataptr = data;
1113 Type min = *dataptr;
1115 for (
unsigned int i = 0; i < (dsize - 1); ++i) {
1116 if (*dataptr < min) {
1129 Type *dataptr = data;
1130 Type max = *dataptr;
1132 for (
unsigned int i = 0; i < (dsize - 1); ++i) {
1133 if (*dataptr > max) {
1154 out.
resize(rowNum, colNum,
false);
1156 for (
unsigned int i = 0; i < dsize; ++i) {
1166 for (
unsigned int i = 0; i < rowNum; ++i) {
1167 for (
unsigned int j = 0; j < colNum; ++j) {
1168 At[j][i] = (*this)[i][j];
1177 conv2(M, kernel, res, mode);
1187 if (mode ==
"valid") {
1195 if ((mode ==
"full") || (mode ==
"same")) {
1196 const unsigned int pad_x = kernel.
getCols() - 1;
1197 const unsigned int pad_y = kernel.
getRows() - 1;
1198 const unsigned int pad = 2;
1200 M_padded.
insert(M, pad_y, pad_x);
1202 if (mode ==
"same") {
1210 else if (mode ==
"valid") {
1218 if (mode ==
"same") {
1219 unsigned int res_same_rows = res_same.
getRows();
1220 unsigned int res_same_cols = res_same.
getCols();
1221 unsigned int kernel_rows = kernel.
getRows();
1222 unsigned int kernel_cols = kernel.
getCols();
1223 for (
unsigned int i = 0; i < res_same_rows; ++i) {
1224 for (
unsigned int j = 0; j < res_same_cols; ++j) {
1225 for (
unsigned int k = 0; k < kernel_rows; ++k) {
1226 for (
unsigned int l = 0; l < kernel_cols; ++l) {
1227 res_same[i][j] += M_padded[i + k][j + l] * kernel[kernel.
getRows() - k - 1][kernel.
getCols() - l - 1];
1233 const unsigned int start_i = kernel.
getRows() / 2;
1234 const unsigned int start_j = kernel.
getCols() / 2;
1235 unsigned int m_rows = M.
getRows();
1236 for (
unsigned int i = 0; i < m_rows; ++i) {
1242 unsigned int res_rows = res.
getRows();
1243 unsigned int res_cols = res.
getCols();
1244 unsigned int kernel_rows = kernel.
getRows();
1245 unsigned int kernel_cols = kernel.
getCols();
1246 for (
unsigned int i = 0; i < res_rows; ++i) {
1247 for (
unsigned int j = 0; j < res_cols; ++j) {
1248 for (
unsigned int k = 0; k < kernel_rows; ++k) {
1249 for (
unsigned int l = 0; l < kernel_cols; ++l) {
1250 res[i][j] += M_padded[i + k][j + l] * kernel[kernel.
getRows() - k - 1][kernel.
getCols() - l - 1];
1272 unsigned int a_rows = A.
getRows();
1273 unsigned int a_cols = A.
getCols();
1274 for (
unsigned int i = 0; i < a_rows; ++i) {
1275 for (
unsigned int j = 0; j < a_cols; ++j) {
1276 if ((i >= r) && (i < (r + B.
getRows())) && (j >= c) && (j < (c + B.
getCols()))) {
1277 C[i][j] = B[i - r][j - c];
1297 unsigned int a_size = A.
size();
1298 for (
unsigned int i = 0; i < a_size; ++i) {
1299 if (data[i] != A.
data[i]) {
1316 unsigned int a_size = A.
size();
1317 for (
unsigned int i = 0; i < a_size; ++i) {
1318 if (fabs(
data[i] - A.
data[i]) > std::numeric_limits<double>::epsilon()) {
1335 unsigned int a_size = A.
size();
1336 for (
unsigned int i = 0; i < a_size; ++i) {
1337 if (fabsf(
data[i] - A.
data[i]) > std::numeric_limits<float>::epsilon()) {
1350 #ifdef VISP_HAVE_NLOHMANN_JSON
1351 template <
class Type>
1352 inline void from_json(
const nlohmann::json &j,
vpArray2D<Type> &array)
1355 const unsigned int nrows =
static_cast<unsigned int>(j.size());
1360 unsigned int ncols = 0;
1362 for (
const auto &item : j) {
1363 if (!item.is_array()) {
1368 ncols =
static_cast<unsigned int>(item.size());
1370 else if (ncols != item.size()) {
1374 array.
resize(nrows, ncols);
1376 for (
const auto &item : j) {
1377 std::vector<Type> row = item;
1378 std::copy(row.begin(), row.end(), array.
rowPtrs[i]);
1382 else if (j.is_object()) {
1383 const unsigned ncols = j.at(
"cols");
1384 const unsigned nrows = j.at(
"rows");
1385 array.
resize(nrows, ncols);
1386 const nlohmann::json jData = j.at(
"data");
1387 if (!jData.is_array() || jData.size() != nrows * ncols) {
1388 std::stringstream ss;
1389 ss <<
"JSON \"data\" field must be an array of size " << nrows * ncols;
1393 for (
const auto &jValue : jData) {
1394 array.
data[i] = jValue;
1404 template <
class Type>
1410 {
"type",
"vpArray2D"}
1413 nlohmann::json::array_t data;
1414 data.reserve(array.
size());
1415 for (
unsigned i = 0; i < array.
size(); ++i) {
1416 data.push_back(array.
data[i]);
Implementation of a generic 2D array used as base class for matrices and vectors.
vpArray2D< Type > & operator=(const nlohmann::json &j)=delete
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)
vpArray2D< Type > & operator=(const std::initializer_list< Type > &list)
Type * data
Address of the first element of the data array.
static bool loadYAML(const std::string &filename, vpArray2D< Type > &A, char *header=nullptr)
Type ** rowPtrs
Address of the first element of each rows.
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)
vpArray2D< Type > & operator=(vpArray2D< Type > &&other) noexcept
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< Type > & operator=(const std::initializer_list< std::initializer_list< Type > > &lists)
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.