list_to_matrix.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "list_to_matrix.h"
  9. #include <cassert>
  10. #include <cstdio>
  11. #include <Eigen/Dense>
  12. #include "max_size.h"
  13. #include "min_size.h"
  14. template <typename T, typename Derived>
  15. IGL_INLINE bool igl::list_to_matrix(const std::vector<std::vector<T > > & V,Eigen::PlainObjectBase<Derived>& M)
  16. {
  17. // number of rows
  18. int m = V.size();
  19. if(m == 0)
  20. {
  21. M.resize(0,0);
  22. return true;
  23. }
  24. // number of columns
  25. int n = igl::min_size(V);
  26. if(n != igl::max_size(V))
  27. {
  28. return false;
  29. }
  30. assert(n != -1);
  31. // Resize output
  32. M.resize(m,n);
  33. // Loop over rows
  34. for(int i = 0;i<m;i++)
  35. {
  36. // Loop over cols
  37. for(int j = 0;j<n;j++)
  38. {
  39. M(i,j) = V[i][j];
  40. }
  41. }
  42. return true;
  43. }
  44. template <typename T, typename Derived>
  45. IGL_INLINE bool igl::list_to_matrix(
  46. const std::vector<std::vector<T > > & V,
  47. const int n,
  48. const T & padding,
  49. Eigen::PlainObjectBase<Derived>& M)
  50. {
  51. const int m = V.size();
  52. M.resize(m,n);
  53. for(int i = 0;i<m;i++)
  54. {
  55. const auto & row = V[i];
  56. if(row.size()>n)
  57. {
  58. return false;
  59. }
  60. int j = 0;
  61. for(;j<row.size();j++)
  62. {
  63. M(i,j) = row[j];
  64. }
  65. for(;j<n;j++)
  66. {
  67. M(i,j) = padding;
  68. }
  69. }
  70. return true;
  71. }
  72. template <typename T, typename Derived>
  73. IGL_INLINE bool igl::list_to_matrix(const std::vector<T > & V,Eigen::PlainObjectBase<Derived>& M)
  74. {
  75. // number of rows
  76. int m = V.size();
  77. if(m == 0)
  78. {
  79. //fprintf(stderr,"Error: list_to_matrix() list is empty()\n");
  80. //return false;
  81. if(Eigen::PlainObjectBase<Derived>::ColsAtCompileTime == 1)
  82. {
  83. M.resize(0,1);
  84. }else if(Eigen::PlainObjectBase<Derived>::RowsAtCompileTime == 1)
  85. {
  86. M.resize(1,0);
  87. }else
  88. {
  89. M.resize(0,0);
  90. }
  91. return true;
  92. }
  93. // Resize output
  94. if(Eigen::PlainObjectBase<Derived>::RowsAtCompileTime == 1)
  95. {
  96. M.resize(1,m);
  97. }else
  98. {
  99. M.resize(m,1);
  100. }
  101. // Loop over rows
  102. for(int i = 0;i<m;i++)
  103. {
  104. M(i) = V[i];
  105. }
  106. return true;
  107. }
  108. #ifdef IGL_STATIC_LIBRARY
  109. // Explicit template specialization
  110. template bool igl::list_to_matrix<double, Eigen::Matrix<double, -1, 3, 0, -1, 3> >(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&);
  111. template bool igl::list_to_matrix<double, Eigen::Matrix<double, -1, 3, 1, -1, 3> >(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> >&);
  112. template bool igl::list_to_matrix<double, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  113. template bool igl::list_to_matrix<int, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  114. template bool igl::list_to_matrix<int, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  115. template bool igl::list_to_matrix<int, Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> >(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> >&);
  116. template bool igl::list_to_matrix<double, Eigen::Matrix<float, -1, 3, 1, -1, 3> >(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> >&);
  117. template bool igl::list_to_matrix<float, Eigen::Matrix<float, -1, 3, 1, -1, 3> >(std::vector<std::vector<float, std::allocator<float> >, std::allocator<std::vector<float, std::allocator<float> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> >&);
  118. template bool igl::list_to_matrix<unsigned int, Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3> >(std::vector<std::vector<unsigned int, std::allocator<unsigned int> >, std::allocator<std::vector<unsigned int, std::allocator<unsigned int> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3> >&);
  119. template bool igl::list_to_matrix<int, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(std::vector<int, std::allocator<int> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  120. template bool igl::list_to_matrix<unsigned long, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(std::vector<unsigned long, std::allocator<unsigned long> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  121. template bool igl::list_to_matrix<float, Eigen::Matrix<float, -1, -1, 0, -1, -1> >(std::vector<std::vector<float, std::allocator<float> >, std::allocator<std::vector<float, std::allocator<float> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >&);
  122. template bool igl::list_to_matrix<bool, Eigen::Array<bool, -1, 1, 0, -1, 1> >(std::vector<bool, std::allocator<bool> > const&, Eigen::PlainObjectBase<Eigen::Array<bool, -1, 1, 0, -1, 1> >&);
  123. template bool igl::list_to_matrix<bool, Eigen::Array<bool, -1, -1, 0, -1, -1> >(std::vector<std::vector<bool, std::allocator<bool> >, std::allocator<std::vector<bool, std::allocator<bool> > > > const&, Eigen::PlainObjectBase<Eigen::Array<bool, -1, -1, 0, -1, -1> >&);
  124. template bool igl::list_to_matrix<bool, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::vector<bool, std::allocator<bool> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  125. template bool igl::list_to_matrix<bool, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::vector<std::vector<bool, std::allocator<bool> >, std::allocator<std::vector<bool, std::allocator<bool> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  126. template bool igl::list_to_matrix<double, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(std::__1::vector<double, std::__1::allocator<double> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  127. #endif