LinSpaced.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 LinSpaced(
  33. typename Derived::Index size,
  34. const typename Derived::Scalar & low,
  35. const typename Derived::Scalar & high);
  36. }
  37. // Implementation
  38. template <typename Derived>
  39. inline typename Eigen::DenseBase< Derived >::RandomAccessLinSpacedReturnType
  40. igl::LinSpaced(
  41. typename Derived::Index size,
  42. const typename Derived::Scalar & low,
  43. const typename Derived::Scalar & high)
  44. {
  45. if(size == 0)
  46. {
  47. // Force empty vector with correct "RandomAccessLinSpacedReturnType" type.
  48. return Derived::LinSpaced(0,0,1);
  49. }else
  50. {
  51. return Derived::LinSpaced(size,low,high);
  52. }
  53. }
  54. #endif