colon.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "colon.h"
  2. #include <cstdio>
  3. template <typename L,typename S,typename H,typename T>
  4. IGL_INLINE void igl::colon(
  5. const L low,
  6. const S step,
  7. const H hi,
  8. Eigen::Matrix<T,Eigen::Dynamic,1> & I)
  9. {
  10. if(low < hi)
  11. {
  12. if(step < 0)
  13. {
  14. I.resize(0);
  15. fprintf(stderr,"Error: colon() low(%g)<hi(%g) but step(%g)<0\n",
  16. (double)low,
  17. (double)hi,
  18. (double)step);
  19. return;
  20. }
  21. }
  22. if(low > hi)
  23. {
  24. if(step > 0)
  25. {
  26. I.resize(0);
  27. fprintf(stderr,"Error: colon() low(%g)>hi(%g) but step(%g)<0\n",
  28. (double)low,
  29. (double)hi,
  30. (double)step);
  31. return;
  32. }
  33. }
  34. // resize output
  35. int n = floor(double((hi-low)/step))+1;
  36. I.resize(n);
  37. int i = 0;
  38. T v = (T)low;
  39. while((low<hi && (H)v<=hi) || (low>hi && (H)v>=hi))
  40. {
  41. I(i) = v;
  42. v = v + (T)step;
  43. i++;
  44. }
  45. assert(i==n);
  46. }
  47. template <typename L,typename H,typename T>
  48. IGL_INLINE void igl::colon(
  49. const L low,
  50. const H hi,
  51. Eigen::Matrix<T,Eigen::Dynamic,1> & I)
  52. {
  53. return igl::colon(low,(T)1,hi,I);
  54. }
  55. template <typename T,typename L,typename H>
  56. IGL_INLINE Eigen::Matrix<T,Eigen::Dynamic,1> igl::colon(
  57. const L low,
  58. const H hi)
  59. {
  60. Eigen::Matrix<T,Eigen::Dynamic,1> I;
  61. igl::colon(low,hi,I);
  62. return I;
  63. }
  64. #ifndef IGL_HEADER_ONLY
  65. // Explicit template specialization
  66. // generated by autoexplicit.sh
  67. template Eigen::Matrix<int, -1, 1, 0, -1, 1> igl::colon<int, int, int>(int, int);
  68. // generated by autoexplicit.sh
  69. template Eigen::Matrix<int, -1, 1, 0, -1, 1> igl::colon<int, int, long>(int, long);
  70. #endif