rows_to_matrix.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #ifndef IGL_ROWS_TO_MATRIX_H
  2. #define IGL_ROWS_TO_MATRIX_H
  3. #include <vector>
  4. namespace igl
  5. {
  6. // Convert a list (std::vector) of row vectors of the same length to a matrix
  7. // Template:
  8. // Row row vector type, must implement:
  9. // .size()
  10. // Mat Matrix type, must implement:
  11. // .resize(m,n)
  12. // .row(i) = Row
  13. // Inputs:
  14. // V a m-long list of vectors of size n
  15. // Outputs:
  16. // M an m by n matrix
  17. // Returns true on success, false on errors
  18. template <class Row, class Mat>
  19. inline bool rows_to_matrix(const std::vector<Row> & V,Mat & M);
  20. }
  21. // Implementation
  22. #include <cassert>
  23. #include <cstdio>
  24. #include "max_size.h"
  25. #include "min_size.h"
  26. template <class Row, class Mat>
  27. inline bool igl::rows_to_matrix(const std::vector<Row> & V,Mat & M)
  28. {
  29. // number of columns
  30. int m = V.size();
  31. if(m == 0)
  32. {
  33. fprintf(stderr,"Error: rows_to_matrix() list is empty()\n");
  34. return false;
  35. }
  36. // number of rows
  37. int n = igl::min_size(V);
  38. if(n != igl::max_size(V))
  39. {
  40. fprintf(stderr,"Error: rows_to_matrix()"
  41. " list elements are not all the same size\n");
  42. return false;
  43. }
  44. assert(n != -1);
  45. // Resize output
  46. M.resize(m,n);
  47. // Loop over rows
  48. int i = 0;
  49. typename std::vector<Row>::const_iterator iter = V.begin();
  50. while(iter != V.end())
  51. {
  52. M.row(i) = V[i];
  53. // increment index and iterator
  54. i++;
  55. iter++;
  56. }
  57. return true;
  58. }
  59. #endif