38 #ifndef __vpArray2D_h_ 39 #define __vpArray2D_h_ 50 #include <visp3/core/vpConfig.h> 51 #include <visp3/core/vpException.h> 97 resize(A.rowNum, A.colNum,
false,
false);
98 memcpy(data, A.data, rowNum * colNum *
sizeof(Type));
119 resize(r, c,
false,
false);
132 if (rowPtrs != NULL) {
136 rowNum = colNum = dsize = 0;
171 void resize(
const unsigned int nrows,
const unsigned int ncols,
const bool flagNullify =
true,
172 const bool recopy_ =
true)
174 if ((nrows == rowNum) && (ncols == colNum)) {
175 if (flagNullify && this->data != NULL) {
176 memset(this->data, 0, this->dsize *
sizeof(Type));
179 bool recopy = !flagNullify && recopy_;
180 const bool recopyNeeded = (ncols != this->colNum && this->colNum > 0 && ncols > 0 && (!flagNullify || recopy));
181 Type *copyTmp = NULL;
182 unsigned int rowTmp = 0, colTmp = 0;
186 if (recopyNeeded && this->data != NULL) {
187 copyTmp =
new Type[this->
dsize];
188 memcpy(copyTmp, this->data,
sizeof(Type) * this->dsize);
194 this->dsize = nrows * ncols;
195 this->data = (Type *)realloc(this->data, this->dsize *
sizeof(Type));
196 if ((NULL == this->data) && (0 != this->
dsize)) {
202 this->rowPtrs = (Type **)realloc(this->rowPtrs, nrows *
sizeof(Type *));
203 if ((NULL == this->rowPtrs) && (0 != this->
dsize)) {
207 "Memory allocation error when allocating 2D array rowPtrs"));
213 for (
unsigned int i = 0; i <
dsize; i += ncols) {
214 *t_++ = this->data + i;
218 this->rowNum = nrows;
219 this->colNum = ncols;
223 memset(this->data, 0, this->dsize *
sizeof(Type));
224 }
else if (recopyNeeded && this->rowPtrs != NULL) {
226 const unsigned int minRow = (this->rowNum < rowTmp) ? this->rowNum : rowTmp;
227 const unsigned int minCol = (this->colNum < colTmp) ? this->colNum : colTmp;
228 for (
unsigned int i = 0; i < this->
rowNum; ++i) {
229 for (
unsigned int j = 0; j < this->
colNum; ++j) {
230 if ((minRow > i) && (minCol > j)) {
231 (*this)[i][j] = copyTmp[i * colTmp + j];
246 for (
unsigned int i = 0; i <
rowNum; i++)
247 for (
unsigned int j = 0; j <
colNum; j++)
259 memcpy(data, A.
data, rowNum * colNum *
sizeof(Type));
264 inline Type *
operator[](
unsigned int i) {
return rowPtrs[i]; }
266 inline Type *
operator[](
unsigned int i)
const {
return rowPtrs[i]; }
273 friend std::ostream &operator<<(std::ostream &s, const vpArray2D<Type> &A)
275 if (A.data == NULL || A.size() == 0)
277 std::ios_base::fmtflags original_flags = s.flags();
280 for (
unsigned int i = 0; i < A.getRows(); i++) {
281 for (
unsigned int j = 0; j < A.getCols() - 1; j++) {
285 s << A[i][A.getCols() - 1];
287 if (i < A.getRows() - 1)
291 s.flags(original_flags);
318 static bool load(
const std::string &filename,
vpArray2D<Type> &A,
const bool binary =
false,
char *header = NULL)
323 file.open(filename.c_str(), std::fstream::in);
325 file.open(filename.c_str(), std::fstream::in | std::fstream::binary);
334 bool headerIsDecoded =
false;
336 std::streampos pos = file.tellg();
338 file.getline(line, 256);
339 std::string prefix(
"# ");
340 std::string line_(line);
341 if (line_.compare(0, 2, prefix.c_str()) == 0) {
347 h += line_.substr(2);
350 file.seekg(pos, file.beg);
351 headerIsDecoded =
true;
353 }
while (!headerIsDecoded);
355 if (header != NULL) {
356 #if defined(__MINGW32__) || \ 357 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 358 sprintf(header,
"%s", h.c_str());
360 _snprintf_s(header, h.size() + 1, _TRUNCATE,
"%s", h.c_str());
364 unsigned int rows, cols;
368 if (rows >= (std::numeric_limits<unsigned int>::max)() || cols >= (std::numeric_limits<unsigned int>::max)())
374 for (
unsigned int i = 0; i < rows; i++) {
375 for (
unsigned int j = 0; j < cols; j++) {
384 while ((c !=
'\0')) {
388 if (header != NULL) {
389 #if defined(__MINGW32__) || \ 390 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 391 sprintf(header,
"%s", h.c_str());
393 _snprintf_s(header, h.size() + 1, _TRUNCATE,
"%s", h.c_str());
397 unsigned int rows, cols;
398 file.read((
char *)&rows,
sizeof(
unsigned int));
399 file.read((
char *)&cols,
sizeof(
unsigned int));
403 for (
unsigned int i = 0; i < rows; i++) {
404 for (
unsigned int j = 0; j < cols; j++) {
405 file.read((
char *)&value,
sizeof(Type));
430 file.open(filename.c_str(), std::fstream::in);
437 unsigned int rows = 0, cols = 0;
439 std::string line, subs;
440 bool inheader =
true;
441 unsigned int i = 0, j;
442 unsigned int lineStart = 0;
444 while (getline(file, line)) {
446 if (rows == 0 && line.compare(0, 5,
"rows:") == 0) {
447 std::stringstream ss(line);
450 }
else if (cols == 0 && line.compare(0, 5,
"cols:") == 0) {
451 std::stringstream ss(line);
454 }
else if (line.compare(0, 5,
"data:") == 0)
462 if (rows == 0 || cols == 0) {
468 lineStart = (
unsigned int)line.find(
"[") + 1;
470 std::stringstream ss(line.substr(lineStart, line.find(
"]") - lineStart));
472 while (getline(ss, subs,
','))
473 A[i][j++] = atof(subs.c_str());
478 if (header != NULL) {
479 std::string h_ = h.substr(0, h.size() - 1);
480 #if defined(__MINGW32__) || \ 481 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 482 sprintf(header,
"%s", h_.c_str());
484 _snprintf_s(header, h_.size() + 1, _TRUNCATE,
"%s", h_.c_str());
509 const char *header =
"")
514 file.open(filename.c_str(), std::fstream::out);
516 file.open(filename.c_str(), std::fstream::out | std::fstream::binary);
526 while (header[i] !=
'\0') {
528 if (header[i] ==
'\n')
534 file << A << std::endl;
537 while (header[headerSize] !=
'\0')
539 file.write(header, headerSize + 1);
540 unsigned int matrixSize;
542 file.write((
char *)&matrixSize,
sizeof(
unsigned int));
544 file.write((
char *)&matrixSize,
sizeof(
unsigned int));
546 for (
unsigned int i = 0; i < A.
getRows(); i++) {
547 for (
unsigned int j = 0; j < A.
getCols(); j++) {
549 file.write((
char *)&value,
sizeof(Type));
601 file.open(filename.c_str(), std::fstream::out);
609 bool inIndent =
false;
610 std::string indent =
"";
611 bool checkIndent =
true;
612 while (header[i] !=
'\0') {
616 if (header[i] ==
' ')
618 else if (indent.length() > 0)
621 if (header[i] ==
'\n' || (inIndent && header[i] ==
' '))
631 file <<
"rows: " << A.
getRows() << std::endl;
632 file <<
"cols: " << A.
getCols() << std::endl;
634 if (indent.length() == 0)
637 file <<
"data: " << std::endl;
639 for (i = 0; i < A.
getRows(); ++i) {
640 file << indent <<
"- [";
641 for (j = 0; j < A.
getCols() - 1; ++j)
642 file << A[i][j] <<
", ";
643 file << A[i][j] <<
"]" << std::endl;
657 Type *dataptr =
data;
660 for (
unsigned int i = 0; i <
dsize - 1; i++) {
673 Type *dataptr =
data;
676 for (
unsigned int i = 0; i <
dsize - 1; i++) {
699 for (
unsigned int i = 0; i <
dsize; i++) {
static bool save(const std::string &filename, const vpArray2D< Type > &A, const bool binary=false, const char *header="")
static bool saveYAML(const std::string &filename, const vpArray2D< Type > &A, const char *header="")
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
static bool loadYAML(const std::string &filename, vpArray2D< Type > &A, char *header=NULL)
Type * operator[](unsigned int i) const
Get element using x = A[i][j].
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true, const bool recopy_=true)
error that can be emited by ViSP classes.
unsigned int getRows() const
Type * data
Address of the first element of the data array.
Implementation of a generic 2D array used as vase class of matrices and vectors.
unsigned int size() const
Return the number of elements of the 2D array.
unsigned int getCols() const
unsigned int rowNum
Number of rows in the array.
vpArray2D< Type > & operator=(const vpArray2D< Type > &A)
static bool load(const std::string &filename, vpArray2D< Type > &A, const bool binary=false, char *header=NULL)
vpArray2D< Type > hadamard(const vpArray2D< Type > &m) const
unsigned int colNum
Number of columns in the array.
Type * operator[](unsigned int i)
Set element using A[i][j] = x.
unsigned int dsize
Current array size (rowNum * colNum)
Type ** rowPtrs
Address of the first element of each rows.