colon.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. assert(false && "low<hi but step<0");
  20. return;
  21. }
  22. }
  23. if(low > hi)
  24. {
  25. if(step > 0)
  26. {
  27. I.resize(0);
  28. fprintf(stderr,"Error: colon() low(%g)>hi(%g) but step(%g)>0\n",
  29. (double)low,
  30. (double)hi,
  31. (double)step);
  32. assert(false && "low>hi but step<0");
  33. return;
  34. }
  35. }
  36. // resize output
  37. int n = floor(double((hi-low)/step))+1;
  38. I.resize(n);
  39. int i = 0;
  40. T v = (T)low;
  41. while((low<hi && (H)v<=hi) || (low>hi && (H)v>=hi))
  42. {
  43. I(i) = v;
  44. v = v + (T)step;
  45. i++;
  46. }
  47. assert(i==n);
  48. }
  49. template <typename L,typename H,typename T>
  50. IGL_INLINE void igl::colon(
  51. const L low,
  52. const H hi,
  53. Eigen::Matrix<T,Eigen::Dynamic,1> & I)
  54. {
  55. return igl::colon(low,(T)1,hi,I);
  56. }
  57. template <typename T,typename L,typename H>
  58. IGL_INLINE Eigen::Matrix<T,Eigen::Dynamic,1> igl::colon(
  59. const L low,
  60. const H hi)
  61. {
  62. Eigen::Matrix<T,Eigen::Dynamic,1> I;
  63. igl::colon(low,hi,I);
  64. return I;
  65. }
  66. #ifndef IGL_HEADER_ONLY
  67. // Explicit template specialization
  68. // generated by autoexplicit.sh
  69. template Eigen::Matrix<int, -1, 1, 0, -1, 1> igl::colon<int, int, int>(int, int);
  70. // generated by autoexplicit.sh
  71. template Eigen::Matrix<int, -1, 1, 0, -1, 1> igl::colon<int, int, long>(int, long);
  72. template void igl::colon<int, long, int, int>(int, long, int, Eigen::Matrix<int, -1, 1, 0, -1, 1>&);
  73. template void igl::colon<int, int, long, int>(int, int, long, Eigen::Matrix<int, -1, 1, 0, -1, 1>&);
  74. template void igl::colon<int, long, int>(int, long, Eigen::Matrix<int, -1, 1, 0, -1, 1>&);
  75. template void igl::colon<int, int, int>(int, int, Eigen::Matrix<int, -1, 1, 0, -1, 1>&);
  76. #endif