colon.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #ifndef IGL_COLON_H
  2. #define IGL_COLON_H
  3. #include <Eigen/Dense>
  4. namespace igl
  5. {
  6. // Colon operator like matlab's colon operator. Enumerats values between low
  7. // and hi with step step.
  8. // Templates:
  9. // L should be a eigen matrix primitive type like int or double
  10. // S should be a eigen matrix primitive type like int or double
  11. // H should be a eigen matrix primitive type like int or double
  12. // T should be a eigen matrix primitive type like int or double
  13. // Inputs:
  14. // low starting value if step is valid then this is *always* the first
  15. // element of I
  16. // step step difference between sequential elements returned in I,
  17. // remember this will be cast to template T at compile time. If low<hi
  18. // then step must be positive. If low>hi then step must be negative.
  19. // Otherwise I will be set to empty.
  20. // hi ending value, if (hi-low)%step is zero then this will be the last
  21. // element in I. If step is positive there will be no elements greater
  22. // than hi, vice versa if hi<low
  23. // Output:
  24. // I list of values from low to hi with step size step
  25. template <typename L,typename S,typename H,typename T>
  26. inline void colon(
  27. const L low,
  28. const S step,
  29. const H hi,
  30. Eigen::Matrix<T,Eigen::Dynamic,1> & I);
  31. // Same as above but step == (T)1
  32. template <typename L,typename H,typename T>
  33. inline void colon(
  34. const L low,
  35. const H hi,
  36. Eigen::Matrix<T,Eigen::Dynamic,1> & I);
  37. // Return output rather than set in reference
  38. template <typename T,typename L,typename H>
  39. inline Eigen::Matrix<T,Eigen::Dynamic,1> colon(
  40. const L low,
  41. const H hi);
  42. }
  43. // Implementation
  44. #include <cstdio>
  45. template <typename L,typename S,typename H,typename T>
  46. inline void igl::colon(
  47. const L low,
  48. const S step,
  49. const H hi,
  50. Eigen::Matrix<T,Eigen::Dynamic,1> & I)
  51. {
  52. if(low < hi)
  53. {
  54. if(step < 0)
  55. {
  56. I.resize(0);
  57. fprintf(stderr,"Error: colon() low(%g)<hi(%g) but step(%g)<0\n",
  58. (double)low,
  59. (double)hi,
  60. (double)step);
  61. return;
  62. }
  63. }
  64. if(low > hi)
  65. {
  66. if(step > 0)
  67. {
  68. I.resize(0);
  69. fprintf(stderr,"Error: colon() low(%g)>hi(%g) but step(%g)<0\n",
  70. (double)low,
  71. (double)hi,
  72. (double)step);
  73. return;
  74. }
  75. }
  76. // resize output
  77. int n = floor((hi-low)/step)+1;
  78. I.resize(n);
  79. int i = 0;
  80. T v = (T)low;
  81. while((low<hi && (H)v<=hi) || (low>hi && (H)v>=hi))
  82. {
  83. I(i) = v;
  84. v = v + (T)step;
  85. i++;
  86. }
  87. assert(i==n);
  88. }
  89. template <typename L,typename H,typename T>
  90. inline void igl::colon(
  91. const L low,
  92. const H hi,
  93. Eigen::Matrix<T,Eigen::Dynamic,1> & I)
  94. {
  95. return igl::colon(low,(T)1,hi,I);
  96. }
  97. template <typename T,typename L,typename H>
  98. inline Eigen::Matrix<T,Eigen::Dynamic,1> igl::colon(
  99. const L low,
  100. const H hi)
  101. {
  102. Eigen::Matrix<T,Eigen::Dynamic,1> I;
  103. igl::colon(low,hi,I);
  104. return I;
  105. }
  106. #endif