LinSpaced.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #ifndef IGL_LINSPACED_H
  2. #define IGL_LINSPACED_H
  3. #include <Eigen/Core>
  4. // This function is not intended to be a permanent function of libigl. Rather
  5. // it is a "drop-in" workaround for documented bug in Eigen:
  6. // http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1383
  7. //
  8. // Replace:
  9. //
  10. // Eigen::VectorXi::LinSpaced(size,low,high);
  11. //
  12. // With:
  13. //
  14. // igl::LinSpaced<Eigen::VectorXi>(size,low,high);
  15. //
  16. // Specifcally, this version will _always_ return an empty vector if size==0,
  17. // regardless of the values for low and high. If size != 0, then this simply
  18. // returns the result of Eigen::Derived::LinSpaced.
  19. //
  20. // Until this bug is fixed, we should also avoid calls to the member function
  21. // `.setLinSpaced`. This means replacing:
  22. //
  23. // a.setLinSpaced(size,low,high);
  24. //
  25. // with
  26. //
  27. // a = igl::LinSpaced<decltype(a) >(size,low,high);
  28. //
  29. namespace igl
  30. {
  31. template <typename Derived>
  32. //inline typename Eigen::DenseBase< Derived >::RandomAccessLinSpacedReturnType
  33. inline Derived LinSpaced(
  34. typename Derived::Index size,
  35. const typename Derived::Scalar & low,
  36. const typename Derived::Scalar & high);
  37. }
  38. // Implementation
  39. template <typename Derived>
  40. //inline typename Eigen::DenseBase< Derived >::RandomAccessLinSpacedReturnType
  41. inline Derived
  42. igl::LinSpaced(
  43. typename Derived::Index size,
  44. const typename Derived::Scalar & low,
  45. const typename Derived::Scalar & high)
  46. {
  47. if(size == 0)
  48. {
  49. // Force empty vector with correct "RandomAccessLinSpacedReturnType" type.
  50. return Derived::LinSpaced(0,0,1);
  51. }else if(high < low)
  52. {
  53. return low-Derived::LinSpaced(size,low-low,low-high).array();
  54. }else{
  55. return Derived::LinSpaced(size,low,high);
  56. }
  57. }
  58. #endif