colon.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "colon.h"
  9. #include <cstdio>
  10. template <typename L,typename S,typename H,typename T>
  11. IGL_INLINE void igl::colon(
  12. const L low,
  13. const S step,
  14. const H hi,
  15. Eigen::Matrix<T,Eigen::Dynamic,1> & I)
  16. {
  17. if(low < hi)
  18. {
  19. if(step < 0)
  20. {
  21. I.resize(0);
  22. //fprintf(stderr,"WARNING: colon() low(%g)<hi(%g) but step(%g)<0\n",
  23. // (double)low,
  24. // (double)hi,
  25. // (double)step);
  26. //assert(false && "low<hi but step<0");
  27. return;
  28. }
  29. }
  30. if(low > hi)
  31. {
  32. if(step > 0)
  33. {
  34. I.resize(0);
  35. //fprintf(stderr,"Error: colon() low(%g)>hi(%g) but step(%g)>0\n",
  36. // (double)low,
  37. // (double)hi,
  38. // (double)step);
  39. //assert(false && "low>hi but step<0");
  40. return;
  41. }
  42. }
  43. // resize output
  44. int n = std::floor(double((hi-low)/step))+1;
  45. I.resize(n);
  46. int i = 0;
  47. T v = (T)low;
  48. while((low==hi && (H)v==hi) || (low<hi && (H)v<=hi) || (low>hi && (H)v>=hi))
  49. {
  50. I(i) = v;
  51. v = v + (T)step;
  52. i++;
  53. }
  54. assert(i==n);
  55. }
  56. template <typename L,typename H,typename T>
  57. IGL_INLINE void igl::colon(
  58. const L low,
  59. const H hi,
  60. Eigen::Matrix<T,Eigen::Dynamic,1> & I)
  61. {
  62. return igl::colon(low,(T)1,hi,I);
  63. }
  64. template <typename T,typename L,typename H>
  65. IGL_INLINE Eigen::Matrix<T,Eigen::Dynamic,1> igl::colon(
  66. const L low,
  67. const H hi)
  68. {
  69. Eigen::Matrix<T,Eigen::Dynamic,1> I;
  70. igl::colon(low,hi,I);
  71. return I;
  72. }
  73. #ifdef IGL_STATIC_LIBRARY
  74. // Explicit template specialization
  75. // generated by autoexplicit.sh
  76. template Eigen::Matrix<int, -1, 1, 0, -1, 1> igl::colon<int, int, int>(int, int);
  77. // generated by autoexplicit.sh
  78. template Eigen::Matrix<int, -1, 1, 0, -1, 1> igl::colon<int, int, long>(int, long);
  79. template void igl::colon<int, long, int, int>(int, long, int, Eigen::Matrix<int, -1, 1, 0, -1, 1>&);
  80. template void igl::colon<int, int, long, int>(int, int, long, Eigen::Matrix<int, -1, 1, 0, -1, 1>&);
  81. template void igl::colon<int, long, int>(int, long, Eigen::Matrix<int, -1, 1, 0, -1, 1>&);
  82. template void igl::colon<int, int, int>(int, int, Eigen::Matrix<int, -1, 1, 0, -1, 1>&);
  83. #endif