45 #include <visp3/core/vpConfig.h>
46 #include <visp3/core/vpException.h>
48 #ifdef VISP_HAVE_NLOHMANN_JSON
49 #include <nlohmann/json.hpp>
158 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
176 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
194 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
200 resize(r, c,
false,
false);
204 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
222 resize(1,
static_cast<unsigned int>(list.size()),
false,
false);
223 std::copy(list.begin(), list.end(),
data);
226 explicit vpArray2D<Type>(
unsigned int nrows,
unsigned int ncols,
const std::initializer_list<Type> &list)
229 if (nrows * ncols !=
static_cast<unsigned int>(list.size())) {
230 std::ostringstream oss;
231 oss <<
"Cannot create a vpArray2D of size (" << nrows <<
", " << ncols <<
") with a list of size " << list.size();
235 resize(nrows, ncols,
false,
false);
236 std::copy(list.begin(), list.end(),
data);
241 unsigned int nrows =
static_cast<unsigned int>(lists.size()), ncols = 0;
242 for (
auto &l : lists) {
243 if (
static_cast<unsigned int>(l.size()) > ncols) {
244 ncols =
static_cast<unsigned int>(l.size());
248 resize(nrows, ncols,
false,
false);
249 auto it = lists.begin();
250 for (
unsigned int i = 0; i <
rowNum; i++, ++it) {
251 std::copy(it->begin(), it->end(),
rowPtrs[i]);
305 void resize(
unsigned int nrows,
unsigned int ncols,
bool flagNullify =
true,
bool recopy_ =
true)
308 if (flagNullify && this->data != NULL) {
309 memset(this->data, 0, this->dsize *
sizeof(Type));
313 bool recopy = !flagNullify && recopy_;
314 const bool recopyNeeded = (ncols != this->colNum && this->colNum > 0 && ncols > 0 && (!flagNullify || recopy));
315 Type *copyTmp = NULL;
316 unsigned int rowTmp = 0, colTmp = 0;
320 if (recopyNeeded && this->data != NULL) {
321 copyTmp =
new Type[this->
dsize];
322 memcpy(copyTmp, this->data,
sizeof(Type) * this->dsize);
328 this->dsize = nrows * ncols;
329 this->data = (Type *)realloc(this->data, this->dsize *
sizeof(Type));
330 if ((NULL == this->data) && (0 != this->
dsize)) {
331 if (copyTmp != NULL) {
337 this->rowPtrs = (Type **)realloc(this->rowPtrs, nrows *
sizeof(Type *));
338 if ((NULL == this->rowPtrs) && (0 != this->
dsize)) {
339 if (copyTmp != NULL) {
343 "Memory allocation error when allocating 2D array rowPtrs"));
349 for (
unsigned int i = 0; i <
dsize; i += ncols) {
350 *t_++ = this->data + i;
354 this->rowNum = nrows;
355 this->colNum = ncols;
359 memset(this->data, 0, (
size_t)(this->dsize) *
sizeof(Type));
361 else if (recopyNeeded && this->rowPtrs != NULL) {
363 unsigned int minRow = (this->rowNum < rowTmp) ? this->rowNum : rowTmp;
364 unsigned int minCol = (this->colNum < colTmp) ? this->colNum : colTmp;
365 for (
unsigned int i = 0; i < this->
rowNum; ++i) {
366 for (
unsigned int j = 0; j < this->
colNum; ++j) {
367 if ((minRow > i) && (minCol > j)) {
368 (*this)[i][j] = copyTmp[i * colTmp + j];
377 if (copyTmp != NULL) {
383 void reshape(
unsigned int nrows,
unsigned int ncols)
390 if (nrows * ncols !=
dsize) {
391 std::ostringstream oss;
392 oss <<
"Cannot reshape array of total size " <<
dsize <<
" into shape (" << nrows <<
", " << ncols <<
")";
398 rowPtrs =
reinterpret_cast<Type **
>(realloc(
rowPtrs, nrows *
sizeof(Type *)));
401 for (
unsigned int i = 0; i <
dsize; i += ncols) {
424 for (
unsigned int i = r; i < (r + A.
getRows()); i++) {
463 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
466 if (
this != &other) {
478 other.rowPtrs = NULL;
488 if (
dsize !=
static_cast<unsigned int>(list.size())) {
489 resize(1,
static_cast<unsigned int>(list.size()),
false,
false);
491 std::copy(list.begin(), list.end(),
data);
498 unsigned int nrows =
static_cast<unsigned int>(lists.size()), ncols = 0;
499 for (
auto &l : lists) {
500 if (
static_cast<unsigned int>(l.size()) > ncols) {
501 ncols =
static_cast<unsigned int>(l.size());
505 resize(nrows, ncols,
false,
false);
506 auto it = lists.begin();
507 for (
unsigned int i = 0; i <
rowNum; i++, ++it) {
508 std::copy(it->begin(), it->end(),
rowPtrs[i]);
514 #ifdef VISP_HAVE_NLOHMANN_JSON
531 if (A.
data == NULL || A.
size() == 0) {
534 std::ios_base::fmtflags original_flags = s.flags();
537 for (
unsigned int i = 0; i < A.
getRows(); i++) {
538 for (
unsigned int j = 0; j < A.
getCols() - 1; j++) {
544 if (i < A.getRows() - 1) {
549 s.flags(original_flags);
582 static bool load(
const std::string &filename,
vpArray2D<Type> &A,
bool binary =
false,
char *header = NULL)
587 file.open(filename.c_str(), std::fstream::in);
590 file.open(filename.c_str(), std::fstream::in | std::fstream::binary);
600 bool headerIsDecoded =
false;
602 std::streampos pos = file.tellg();
604 file.getline(line, 256);
605 std::string prefix(
"# ");
606 std::string line_(line);
607 if (line_.compare(0, 2, prefix.c_str()) == 0) {
614 h += line_.substr(2);
618 file.seekg(pos, file.beg);
619 headerIsDecoded =
true;
621 }
while (!headerIsDecoded);
623 if (header != NULL) {
624 #if defined(__MINGW32__) || \
625 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
626 snprintf(header, h.size() + 1,
"%s", h.c_str());
628 _snprintf_s(header, h.size() + 1, _TRUNCATE,
"%s", h.c_str());
632 unsigned int rows, cols;
636 if (rows >= (std::numeric_limits<unsigned int>::max)() || cols >= (std::numeric_limits<unsigned int>::max)()) {
643 for (
unsigned int i = 0; i < rows; i++) {
644 for (
unsigned int j = 0; j < cols; j++) {
658 if (header != NULL) {
659 #if defined(__MINGW32__) || \
660 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
661 snprintf(header, h.size() + 1,
"%s", h.c_str());
663 _snprintf_s(header, h.size() + 1, _TRUNCATE,
"%s", h.c_str());
667 unsigned int rows, cols;
668 file.read((
char *)&rows,
sizeof(
unsigned int));
669 file.read((
char *)&cols,
sizeof(
unsigned int));
673 for (
unsigned int i = 0; i < rows; i++) {
674 for (
unsigned int j = 0; j < cols; j++) {
675 file.read((
char *)&value,
sizeof(Type));
700 file.open(filename.c_str(), std::fstream::in);
707 unsigned int rows = 0, cols = 0;
709 std::string line, subs;
710 bool inheader =
true;
711 unsigned int i = 0, j;
712 unsigned int lineStart = 0;
714 while (getline(file, line)) {
716 if (rows == 0 && line.compare(0, 5,
"rows:") == 0) {
717 std::stringstream ss(line);
721 else if (cols == 0 && line.compare(0, 5,
"cols:") == 0) {
722 std::stringstream ss(line);
726 else if (line.compare(0, 5,
"data:") == 0) {
737 if (rows == 0 || cols == 0) {
743 lineStart = (
unsigned int)line.find(
"[") + 1;
745 std::stringstream ss(line.substr(lineStart, line.find(
"]") - lineStart));
747 while (getline(ss, subs,
',')) {
748 A[i][j++] = atof(subs.c_str());
754 if (header != NULL) {
755 std::string h_ = h.substr(0, h.size() - 1);
756 #if defined(__MINGW32__) || \
757 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
758 snprintf(header, h_.size() + 1,
"%s", h_.c_str());
760 _snprintf_s(header, h_.size() + 1, _TRUNCATE,
"%s", h_.c_str());
784 static bool save(
const std::string &filename,
const vpArray2D<Type> &A,
bool binary =
false,
const char *header =
"")
789 file.open(filename.c_str(), std::fstream::out);
792 file.open(filename.c_str(), std::fstream::out | std::fstream::binary);
803 while (header[i] !=
'\0') {
805 if (header[i] ==
'\n') {
812 file << A << std::endl;
816 while (header[headerSize] !=
'\0') {
819 file.write(header, (
size_t)headerSize + (
size_t)1);
820 unsigned int matrixSize;
822 file.write((
char *)&matrixSize,
sizeof(
unsigned int));
824 file.write((
char *)&matrixSize,
sizeof(
unsigned int));
826 for (
unsigned int i = 0; i < A.
getRows(); i++) {
827 for (
unsigned int j = 0; j < A.
getCols(); j++) {
829 file.write((
char *)&value,
sizeof(Type));
881 file.open(filename.c_str(), std::fstream::out);
889 bool inIndent =
false;
890 std::string indent =
"";
891 bool checkIndent =
true;
892 while (header[i] !=
'\0') {
896 if (header[i] ==
' ') {
899 else if (indent.length() > 0) {
903 if (header[i] ==
'\n' || (inIndent && header[i] ==
' ')) {
916 file <<
"rows: " << A.
getRows() << std::endl;
917 file <<
"cols: " << A.
getCols() << std::endl;
919 if (indent.length() == 0) {
923 file <<
"data: " << std::endl;
925 for (i = 0; i < A.
getRows(); ++i) {
926 file << indent <<
"- [";
927 for (j = 0; j < A.
getCols() - 1; ++j) {
928 file << A[i][j] <<
", ";
930 file << A[i][j] <<
"]" << std::endl;
936 #ifdef VISP_HAVE_NLOHMANN_JSON
1008 Type *dataptr = data;
1009 Type min = *dataptr;
1011 for (
unsigned int i = 0; i < dsize - 1; i++) {
1012 if (*dataptr < min) {
1025 Type *dataptr = data;
1026 Type max = *dataptr;
1028 for (
unsigned int i = 0; i < dsize - 1; i++) {
1029 if (*dataptr > max) {
1050 out.
resize(rowNum, colNum,
false);
1052 for (
unsigned int i = 0; i < dsize; i++) {
1062 for (
unsigned int i = 0; i < rowNum; i++) {
1063 for (
unsigned int j = 0; j < colNum; j++) {
1064 At[j][i] = (*this)[i][j];
1073 conv2(M, kernel, res, mode);
1082 if (mode ==
"valid") {
1089 if (mode ==
"full" || mode ==
"same") {
1090 const unsigned int pad_x = kernel.
getCols() - 1;
1091 const unsigned int pad_y = kernel.
getRows() - 1;
1093 M_padded.
insert(M, pad_y, pad_x);
1095 if (mode ==
"same") {
1103 else if (mode ==
"valid") {
1111 if (mode ==
"same") {
1112 for (
unsigned int i = 0; i < res_same.
getRows(); i++) {
1113 for (
unsigned int j = 0; j < res_same.
getCols(); j++) {
1114 for (
unsigned int k = 0; k < kernel.
getRows(); k++) {
1115 for (
unsigned int l = 0; l < kernel.
getCols(); l++) {
1116 res_same[i][j] += M_padded[i + k][j + l] * kernel[kernel.
getRows() - k - 1][kernel.
getCols() - l - 1];
1122 const unsigned int start_i = kernel.
getRows() / 2;
1123 const unsigned int start_j = kernel.
getCols() / 2;
1124 for (
unsigned int i = 0; i < M.
getRows(); i++) {
1130 for (
unsigned int i = 0; i < res.
getRows(); i++) {
1131 for (
unsigned int j = 0; j < res.
getCols(); j++) {
1132 for (
unsigned int k = 0; k < kernel.
getRows(); k++) {
1133 for (
unsigned int l = 0; l < kernel.
getCols(); l++) {
1134 res[i][j] += M_padded[i + k][j + l] * kernel[kernel.
getRows() - k - 1][kernel.
getCols() - l - 1];
1156 for (
unsigned int i = 0; i < A.
getRows(); i++) {
1157 for (
unsigned int j = 0; j < A.
getCols(); j++) {
1158 if (i >= r && i < (r + B.
getRows()) && j >= c && j < (c + B.
getCols())) {
1159 C[i][j] = B[i - r][j - c];
1179 for (
unsigned int i = 0; i < A.
size(); i++) {
1180 if (data[i] != A.
data[i]) {
1197 for (
unsigned int i = 0; i < A.
size(); i++) {
1198 if (fabs(
data[i] - A.
data[i]) > std::numeric_limits<double>::epsilon()) {
1215 for (
unsigned int i = 0; i < A.
size(); i++) {
1216 if (fabsf(
data[i] - A.
data[i]) > std::numeric_limits<float>::epsilon()) {
1229 #ifdef VISP_HAVE_NLOHMANN_JSON
1232 template <
class Type>
1233 inline void from_json(
const nlohmann::json &j,
vpArray2D<Type> &array)
1236 const unsigned int nrows =
static_cast<unsigned int>(j.size());
1241 unsigned int ncols = 0;
1243 for (
const auto &item: j) {
1244 if (!item.is_array()) {
1249 ncols =
static_cast<unsigned int>(item.size());
1251 else if (ncols != item.size()) {
1255 array.
resize(nrows, ncols);
1257 for (
const auto &item: j) {
1258 std::vector<Type> row = item;
1259 std::copy(row.begin(), row.end(), array.
rowPtrs[i]);
1263 else if (j.is_object()) {
1264 const unsigned ncols = j.at(
"cols");
1265 const unsigned nrows = j.at(
"rows");
1266 array.
resize(nrows, ncols);
1267 const nlohmann::json jData = j.at(
"data");
1268 if (!jData.is_array() || jData.size() != nrows * ncols) {
1269 std::stringstream ss;
1270 ss <<
"JSON \"data\" field must be an array of size " << nrows * ncols;
1274 for (
const auto &jValue: jData) {
1275 array.
data[i] = jValue;
1285 template <
class Type>
1291 {
"type",
"vpArray2D"}
1294 nlohmann::json::array_t data;
1295 data.reserve(array.
size());
1296 for (
unsigned i = 0; i < array.
size(); ++i) {
1297 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
static bool load(const std::string &filename, vpArray2D< Type > &A, bool binary=false, char *header=NULL)
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.
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.
vpArray2D< Type > t() const
Compute the transpose of the array.
static bool loadYAML(const std::string &filename, vpArray2D< Type > &A, char *header=NULL)
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.