45 #include <visp3/core/vpConfig.h>
46 #include <visp3/core/vpException.h>
48 #ifdef VISP_HAVE_NLOHMANN_JSON
49 #include <nlohmann/json.hpp>
152 #if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L)))
170 #if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L)))
188 #if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L)))
194 resize(r, c,
false,
false);
198 #if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L)))
216 resize(1,
static_cast<unsigned int>(list.size()),
false,
false);
217 std::copy(list.begin(), list.end(),
data);
220 explicit vpArray2D<Type>(
unsigned int nrows,
unsigned int ncols,
const std::initializer_list<Type> &list)
223 if (nrows * ncols !=
static_cast<unsigned int>(list.size())) {
224 std::ostringstream oss;
225 oss <<
"Cannot create a vpArray2D of size (" << nrows <<
", " << ncols <<
") with a list of size " << list.size();
229 resize(nrows, ncols,
false,
false);
230 std::copy(list.begin(), list.end(),
data);
235 unsigned int nrows =
static_cast<unsigned int>(lists.size()), ncols = 0;
236 for (
auto &l : lists) {
237 if (
static_cast<unsigned int>(l.size()) > ncols) {
238 ncols =
static_cast<unsigned int>(l.size());
242 resize(nrows, ncols,
false,
false);
243 auto it = lists.begin();
244 for (
unsigned int i = 0; i <
rowNum; i++, ++it) {
245 std::copy(it->begin(), it->end(),
rowPtrs[i]);
255 if (
data !=
nullptr) {
299 void resize(
unsigned int nrows,
unsigned int ncols,
bool flagNullify =
true,
bool recopy_ =
true)
302 if (flagNullify && this->data !=
nullptr) {
303 memset(this->data, 0, this->dsize *
sizeof(Type));
307 bool recopy = !flagNullify && recopy_;
308 const bool recopyNeeded = (ncols != this->colNum && this->colNum > 0 && ncols > 0 && (!flagNullify || recopy));
309 Type *copyTmp =
nullptr;
310 unsigned int rowTmp = 0, colTmp = 0;
314 if (recopyNeeded && this->data !=
nullptr) {
315 copyTmp =
new Type[this->
dsize];
316 memcpy(copyTmp, this->data,
sizeof(Type) * this->dsize);
322 this->dsize = nrows * ncols;
323 Type *tmp_data =
reinterpret_cast<Type *
>(realloc(this->data, this->dsize *
sizeof(Type)));
325 this->data = tmp_data;
328 this->data =
nullptr;
331 if ((
nullptr == this->data) && (0 != this->
dsize)) {
332 if (copyTmp !=
nullptr) {
338 Type **tmp_rowPtrs =
reinterpret_cast<Type **
>(realloc(this->rowPtrs, nrows *
sizeof(Type *)));
340 this->rowPtrs = tmp_rowPtrs;
343 this->rowPtrs =
nullptr;
345 if ((
nullptr == this->rowPtrs) && (0 != this->
dsize)) {
346 if (copyTmp !=
nullptr) {
350 "Memory allocation error when allocating 2D array rowPtrs"));
356 for (
unsigned int i = 0; i <
dsize; i += ncols) {
357 *t_++ = this->data + i;
361 this->rowNum = nrows;
362 this->colNum = ncols;
366 memset(this->data, 0, (
size_t)(this->dsize) *
sizeof(Type));
368 else if (recopyNeeded && this->rowPtrs !=
nullptr) {
370 unsigned int minRow = (this->rowNum < rowTmp) ? this->rowNum : rowTmp;
371 unsigned int minCol = (this->colNum < colTmp) ? this->colNum : colTmp;
372 for (
unsigned int i = 0; i < this->
rowNum; ++i) {
373 for (
unsigned int j = 0; j < this->
colNum; ++j) {
374 if ((minRow > i) && (minCol > j)) {
375 (*this)[i][j] = copyTmp[i * colTmp + j];
384 if (copyTmp !=
nullptr) {
390 void reshape(
unsigned int nrows,
unsigned int ncols)
397 if (nrows * ncols !=
dsize) {
398 std::ostringstream oss;
399 oss <<
"Cannot reshape array of total size " <<
dsize <<
" into shape (" << nrows <<
", " << ncols <<
")";
406 Type **tmp =
reinterpret_cast<Type **
>(realloc(
rowPtrs, nrows *
sizeof(Type *)));
414 for (
unsigned int i = 0; i <
dsize; i += ncols) {
438 for (
unsigned int i = r; i < (r + A.
getRows()); i++) {
477 #if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L)))
480 if (
this != &other) {
496 other.rowPtrs =
nullptr;
498 other.data =
nullptr;
506 if (
dsize !=
static_cast<unsigned int>(list.size())) {
507 resize(1,
static_cast<unsigned int>(list.size()),
false,
false);
509 std::copy(list.begin(), list.end(),
data);
516 unsigned int nrows =
static_cast<unsigned int>(lists.size()), ncols = 0;
517 for (
auto &l : lists) {
518 if (
static_cast<unsigned int>(l.size()) > ncols) {
519 ncols =
static_cast<unsigned int>(l.size());
523 resize(nrows, ncols,
false,
false);
524 auto it = lists.begin();
525 for (
unsigned int i = 0; i <
rowNum; i++, ++it) {
526 std::copy(it->begin(), it->end(),
rowPtrs[i]);
532 #ifdef VISP_HAVE_NLOHMANN_JSON
549 if (A.
data ==
nullptr || A.
size() == 0) {
552 std::ios_base::fmtflags original_flags = s.flags();
555 for (
unsigned int i = 0; i < A.
getRows(); i++) {
556 for (
unsigned int j = 0; j < A.
getCols() - 1; j++) {
562 if (i < A.getRows() - 1) {
567 s.flags(original_flags);
600 static bool load(
const std::string &filename,
vpArray2D<Type> &A,
bool binary =
false,
char *header =
nullptr)
605 file.open(filename.c_str(), std::fstream::in);
608 file.open(filename.c_str(), std::fstream::in | std::fstream::binary);
618 bool headerIsDecoded =
false;
620 std::streampos pos = file.tellg();
622 file.getline(line, 256);
623 std::string prefix(
"# ");
624 std::string line_(line);
625 if (line_.compare(0, 2, prefix.c_str()) == 0) {
632 h += line_.substr(2);
636 file.seekg(pos, file.beg);
637 headerIsDecoded =
true;
639 }
while (!headerIsDecoded);
641 if (header !=
nullptr) {
642 #if defined(__MINGW32__) || \
643 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
644 snprintf(header, h.size() + 1,
"%s", h.c_str());
646 _snprintf_s(header, h.size() + 1, _TRUNCATE,
"%s", h.c_str());
650 unsigned int rows, cols;
654 if (rows >= (std::numeric_limits<unsigned int>::max)() || cols >= (std::numeric_limits<unsigned int>::max)()) {
661 for (
unsigned int i = 0; i < rows; i++) {
662 for (
unsigned int j = 0; j < cols; j++) {
676 if (header !=
nullptr) {
677 #if defined(__MINGW32__) || \
678 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
679 snprintf(header, h.size() + 1,
"%s", h.c_str());
681 _snprintf_s(header, h.size() + 1, _TRUNCATE,
"%s", h.c_str());
685 unsigned int rows, cols;
686 file.read((
char *)&rows,
sizeof(
unsigned int));
687 file.read((
char *)&cols,
sizeof(
unsigned int));
691 for (
unsigned int i = 0; i < rows; i++) {
692 for (
unsigned int j = 0; j < cols; j++) {
693 file.read((
char *)&value,
sizeof(Type));
718 file.open(filename.c_str(), std::fstream::in);
725 unsigned int rows = 0, cols = 0;
727 std::string line, subs;
728 bool inheader =
true;
729 unsigned int i = 0, j;
730 unsigned int lineStart = 0;
732 while (getline(file, line)) {
734 if (rows == 0 && line.compare(0, 5,
"rows:") == 0) {
735 std::stringstream ss(line);
739 else if (cols == 0 && line.compare(0, 5,
"cols:") == 0) {
740 std::stringstream ss(line);
744 else if (line.compare(0, 5,
"data:") == 0) {
755 if (rows == 0 || cols == 0) {
761 lineStart = (
unsigned int)line.find(
"[") + 1;
763 std::stringstream ss(line.substr(lineStart, line.find(
"]") - lineStart));
765 while (getline(ss, subs,
',')) {
766 A[i][j++] = atof(subs.c_str());
772 if (header !=
nullptr) {
773 std::string h_ = h.substr(0, h.size() - 1);
774 #if defined(__MINGW32__) || \
775 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
776 snprintf(header, h_.size() + 1,
"%s", h_.c_str());
778 _snprintf_s(header, h_.size() + 1, _TRUNCATE,
"%s", h_.c_str());
802 static bool save(
const std::string &filename,
const vpArray2D<Type> &A,
bool binary =
false,
const char *header =
"")
807 file.open(filename.c_str(), std::fstream::out);
810 file.open(filename.c_str(), std::fstream::out | std::fstream::binary);
821 while (header[i] !=
'\0') {
823 if (header[i] ==
'\n') {
830 file << A << std::endl;
834 while (header[headerSize] !=
'\0') {
837 file.write(header, (
size_t)headerSize + (
size_t)1);
838 unsigned int matrixSize;
840 file.write((
char *)&matrixSize,
sizeof(
unsigned int));
842 file.write((
char *)&matrixSize,
sizeof(
unsigned int));
844 for (
unsigned int i = 0; i < A.
getRows(); i++) {
845 for (
unsigned int j = 0; j < A.
getCols(); j++) {
847 file.write((
char *)&value,
sizeof(Type));
901 file.open(filename.c_str(), std::fstream::out);
909 bool inIndent =
false;
910 std::string indent =
"";
911 bool checkIndent =
true;
912 while (header[i] !=
'\0') {
916 if (header[i] ==
' ') {
919 else if (indent.length() > 0) {
923 if (header[i] ==
'\n' || (inIndent && header[i] ==
' ')) {
936 file <<
"rows: " << A.
getRows() << std::endl;
937 file <<
"cols: " << A.
getCols() << std::endl;
939 if (indent.length() == 0) {
943 file <<
"data: " << std::endl;
945 for (i = 0; i < A.
getRows(); ++i) {
946 file << indent <<
"- [";
947 for (j = 0; j < A.
getCols() - 1; ++j) {
948 file << A[i][j] <<
", ";
950 file << A[i][j] <<
"]" << std::endl;
956 #ifdef VISP_HAVE_NLOHMANN_JSON
1028 Type *dataptr = data;
1029 Type min = *dataptr;
1031 for (
unsigned int i = 0; i < dsize - 1; i++) {
1032 if (*dataptr < min) {
1045 Type *dataptr = data;
1046 Type max = *dataptr;
1048 for (
unsigned int i = 0; i < dsize - 1; i++) {
1049 if (*dataptr > max) {
1070 out.
resize(rowNum, colNum,
false);
1072 for (
unsigned int i = 0; i < dsize; i++) {
1082 for (
unsigned int i = 0; i < rowNum; i++) {
1083 for (
unsigned int j = 0; j < colNum; j++) {
1084 At[j][i] = (*this)[i][j];
1093 conv2(M, kernel, res, mode);
1102 if (mode ==
"valid") {
1109 if (mode ==
"full" || mode ==
"same") {
1110 const unsigned int pad_x = kernel.
getCols() - 1;
1111 const unsigned int pad_y = kernel.
getRows() - 1;
1113 M_padded.
insert(M, pad_y, pad_x);
1115 if (mode ==
"same") {
1123 else if (mode ==
"valid") {
1131 if (mode ==
"same") {
1132 for (
unsigned int i = 0; i < res_same.
getRows(); i++) {
1133 for (
unsigned int j = 0; j < res_same.
getCols(); j++) {
1134 for (
unsigned int k = 0; k < kernel.
getRows(); k++) {
1135 for (
unsigned int l = 0; l < kernel.
getCols(); l++) {
1136 res_same[i][j] += M_padded[i + k][j + l] * kernel[kernel.
getRows() - k - 1][kernel.
getCols() - l - 1];
1142 const unsigned int start_i = kernel.
getRows() / 2;
1143 const unsigned int start_j = kernel.
getCols() / 2;
1144 for (
unsigned int i = 0; i < M.
getRows(); i++) {
1150 for (
unsigned int i = 0; i < res.
getRows(); i++) {
1151 for (
unsigned int j = 0; j < res.
getCols(); j++) {
1152 for (
unsigned int k = 0; k < kernel.
getRows(); k++) {
1153 for (
unsigned int l = 0; l < kernel.
getCols(); l++) {
1154 res[i][j] += M_padded[i + k][j + l] * kernel[kernel.
getRows() - k - 1][kernel.
getCols() - l - 1];
1176 for (
unsigned int i = 0; i < A.
getRows(); i++) {
1177 for (
unsigned int j = 0; j < A.
getCols(); j++) {
1178 if (i >= r && i < (r + B.
getRows()) && j >= c && j < (c + B.
getCols())) {
1179 C[i][j] = B[i - r][j - c];
1199 for (
unsigned int i = 0; i < A.
size(); i++) {
1200 if (data[i] != A.
data[i]) {
1217 for (
unsigned int i = 0; i < A.
size(); i++) {
1218 if (fabs(
data[i] - A.
data[i]) > std::numeric_limits<double>::epsilon()) {
1235 for (
unsigned int i = 0; i < A.
size(); i++) {
1236 if (fabsf(
data[i] - A.
data[i]) > std::numeric_limits<float>::epsilon()) {
1249 #ifdef VISP_HAVE_NLOHMANN_JSON
1252 template <
class Type>
1253 inline void from_json(
const nlohmann::json &j,
vpArray2D<Type> &array)
1256 const unsigned int nrows =
static_cast<unsigned int>(j.size());
1261 unsigned int ncols = 0;
1263 for (
const auto &item : j) {
1264 if (!item.is_array()) {
1269 ncols =
static_cast<unsigned int>(item.size());
1271 else if (ncols != item.size()) {
1275 array.
resize(nrows, ncols);
1277 for (
const auto &item : j) {
1278 std::vector<Type> row = item;
1279 std::copy(row.begin(), row.end(), array.
rowPtrs[i]);
1283 else if (j.is_object()) {
1284 const unsigned ncols = j.at(
"cols");
1285 const unsigned nrows = j.at(
"rows");
1286 array.
resize(nrows, ncols);
1287 const nlohmann::json jData = j.at(
"data");
1288 if (!jData.is_array() || jData.size() != nrows * ncols) {
1289 std::stringstream ss;
1290 ss <<
"JSON \"data\" field must be an array of size " << nrows * ncols;
1294 for (
const auto &jValue : jData) {
1295 array.
data[i] = jValue;
1305 template <
class Type>
1311 {
"type",
"vpArray2D"}
1314 nlohmann::json::array_t data;
1315 data.reserve(array.
size());
1316 for (
unsigned i = 0; i < array.
size(); ++i) {
1317 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.
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)
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 > 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.