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)) {
197 if (copyTmp != NULL) {
203 this->rowPtrs = (Type **)realloc(this->rowPtrs, nrows *
sizeof(Type *));
204 if ((NULL == this->rowPtrs) && (0 != this->
dsize)) {
205 if (copyTmp != NULL) {
209 "Memory allocation error when allocating 2D array rowPtrs"));
215 for (
unsigned int i = 0; i <
dsize; i += ncols) {
216 *t_++ = this->data + i;
220 this->rowNum = nrows;
221 this->colNum = ncols;
225 memset(this->data, 0, this->dsize *
sizeof(Type));
226 }
else if (recopyNeeded && this->rowPtrs != NULL) {
228 const unsigned int minRow = (this->rowNum < rowTmp) ? this->rowNum : rowTmp;
229 const unsigned int minCol = (this->colNum < colTmp) ? this->colNum : colTmp;
230 for (
unsigned int i = 0; i < this->
rowNum; ++i) {
231 for (
unsigned int j = 0; j < this->
colNum; ++j) {
232 if ((minRow > i) && (minCol > j)) {
233 (*this)[i][j] = copyTmp[i * colTmp + j];
241 if (copyTmp != NULL) {
249 std::fill(data, data + dsize, x);
259 if (data != NULL && A.
data != NULL && data != A.
data) {
260 memcpy(data, A.
data, rowNum * colNum *
sizeof(Type));
266 inline Type *
operator[](
unsigned int i) {
return rowPtrs[i]; }
268 inline Type *
operator[](
unsigned int i)
const {
return rowPtrs[i]; }
275 friend std::ostream &operator<<(std::ostream &s, const vpArray2D<Type> &A)
277 if (A.data == NULL || A.size() == 0) {
280 std::ios_base::fmtflags original_flags = s.flags();
283 for (
unsigned int i = 0; i < A.getRows(); i++) {
284 for (
unsigned int j = 0; j < A.getCols() - 1; j++) {
288 s << A[i][A.getCols() - 1];
290 if (i < A.getRows() - 1) {
295 s.flags(original_flags);
322 static bool load(
const std::string &filename,
vpArray2D<Type> &A,
const bool binary =
false,
char *header = NULL)
327 file.open(filename.c_str(), std::fstream::in);
330 file.open(filename.c_str(), std::fstream::in | std::fstream::binary);
340 bool headerIsDecoded =
false;
342 std::streampos pos = file.tellg();
344 file.getline(line, 256);
345 std::string prefix(
"# ");
346 std::string line_(line);
347 if (line_.compare(0, 2, prefix.c_str()) == 0) {
354 h += line_.substr(2);
357 file.seekg(pos, file.beg);
358 headerIsDecoded =
true;
360 }
while (!headerIsDecoded);
362 if (header != NULL) {
363 #if defined(__MINGW32__) || \ 364 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 365 sprintf(header,
"%s", h.c_str());
367 _snprintf_s(header, h.size() + 1, _TRUNCATE,
"%s", h.c_str());
371 unsigned int rows, cols;
375 if (rows >= (std::numeric_limits<unsigned int>::max)() || cols >= (std::numeric_limits<unsigned int>::max)()) {
382 for (
unsigned int i = 0; i < rows; i++) {
383 for (
unsigned int j = 0; j < cols; j++) {
396 if (header != NULL) {
397 #if defined(__MINGW32__) || \ 398 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 399 sprintf(header,
"%s", h.c_str());
401 _snprintf_s(header, h.size() + 1, _TRUNCATE,
"%s", h.c_str());
405 unsigned int rows, cols;
406 file.read((
char *)&rows,
sizeof(
unsigned int));
407 file.read((
char *)&cols,
sizeof(
unsigned int));
411 for (
unsigned int i = 0; i < rows; i++) {
412 for (
unsigned int j = 0; j < cols; j++) {
413 file.read((
char *)&value,
sizeof(Type));
438 file.open(filename.c_str(), std::fstream::in);
445 unsigned int rows = 0, cols = 0;
447 std::string line, subs;
448 bool inheader =
true;
449 unsigned int i = 0, j;
450 unsigned int lineStart = 0;
452 while (getline(file, line)) {
454 if (rows == 0 && line.compare(0, 5,
"rows:") == 0) {
455 std::stringstream ss(line);
458 }
else if (cols == 0 && line.compare(0, 5,
"cols:") == 0) {
459 std::stringstream ss(line);
462 }
else if (line.compare(0, 5,
"data:") == 0) {
472 if (rows == 0 || cols == 0) {
478 lineStart = (
unsigned int)line.find(
"[") + 1;
480 std::stringstream ss(line.substr(lineStart, line.find(
"]") - lineStart));
482 while (getline(ss, subs,
',')) {
483 A[i][j++] = atof(subs.c_str());
489 if (header != NULL) {
490 std::string h_ = h.substr(0, h.size() - 1);
491 #if defined(__MINGW32__) || \ 492 !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 493 sprintf(header,
"%s", h_.c_str());
495 _snprintf_s(header, h_.size() + 1, _TRUNCATE,
"%s", h_.c_str());
520 const char *header =
"")
525 file.open(filename.c_str(), std::fstream::out);
528 file.open(filename.c_str(), std::fstream::out | std::fstream::binary);
539 while (header[i] !=
'\0') {
541 if (header[i] ==
'\n') {
548 file << A << std::endl;
551 while (header[headerSize] !=
'\0') {
554 file.write(header, headerSize + 1);
555 unsigned int matrixSize;
557 file.write((
char *)&matrixSize,
sizeof(
unsigned int));
559 file.write((
char *)&matrixSize,
sizeof(
unsigned int));
561 for (
unsigned int i = 0; i < A.
getRows(); i++) {
562 for (
unsigned int j = 0; j < A.
getCols(); j++) {
564 file.write((
char *)&value,
sizeof(Type));
616 file.open(filename.c_str(), std::fstream::out);
624 bool inIndent =
false;
625 std::string indent =
"";
626 bool checkIndent =
true;
627 while (header[i] !=
'\0') {
631 if (header[i] ==
' ') {
634 else if (indent.length() > 0) {
638 if (header[i] ==
'\n' || (inIndent && header[i] ==
' ')) {
651 file <<
"rows: " << A.
getRows() << std::endl;
652 file <<
"cols: " << A.
getCols() << std::endl;
654 if (indent.length() == 0) {
658 file <<
"data: " << std::endl;
660 for (i = 0; i < A.
getRows(); ++i) {
661 file << indent <<
"- [";
662 for (j = 0; j < A.
getCols() - 1; ++j) {
663 file << A[i][j] <<
", ";
665 file << A[i][j] <<
"]" << std::endl;
679 Type *dataptr =
data;
682 for (
unsigned int i = 0; i <
dsize - 1; i++) {
683 if (*dataptr < min) {
696 Type *dataptr =
data;
699 for (
unsigned int i = 0; i <
dsize - 1; i++) {
700 if (*dataptr > max) {
723 for (
unsigned int i = 0; i <
dsize; i++) {
Used to indicate that a value is not in the allowed range.
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)
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.
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)
unsigned int getRows() const
unsigned int colNum
Number of columns in the array.
vpArray2D< Type > hadamard(const vpArray2D< Type > &m) const
Type * operator[](unsigned int i)
Set element using A[i][j] = x.
Type * operator[](unsigned int i) const
Get element using x = A[i][j].
unsigned int dsize
Current array size (rowNum * colNum)
Type ** rowPtrs
Address of the first element of each rows.