LinSpaced.h 2.0 KB

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