cylinder.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 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 "cylinder.h"
  9. #include "PI.h"
  10. #include <cassert>
  11. #include <cmath>
  12. template <typename DerivedV, typename DerivedF>
  13. IGL_INLINE void igl::cylinder(
  14. const int axis_devisions,
  15. const int height_devisions,
  16. Eigen::PlainObjectBase<DerivedV> & V,
  17. Eigen::PlainObjectBase<DerivedF> & F)
  18. {
  19. V.resize(axis_devisions*height_devisions,3);
  20. F.resize(2*(axis_devisions*(height_devisions-1)),3);
  21. int f = 0;
  22. typedef typename DerivedV::Scalar Scalar;
  23. for(int th = 0;th<axis_devisions;th++)
  24. {
  25. Scalar x = cos(2.*igl::PI*Scalar(th)/Scalar(axis_devisions));
  26. Scalar y = sin(2.*igl::PI*Scalar(th)/Scalar(axis_devisions));
  27. for(int h = 0;h<height_devisions;h++)
  28. {
  29. Scalar z = Scalar(h)/Scalar(height_devisions-1);
  30. V(th+h*axis_devisions,0) = x;
  31. V(th+h*axis_devisions,1) = y;
  32. V(th+h*axis_devisions,2) = z;
  33. if(h > 0)
  34. {
  35. F(f,0) = ((th+0)%axis_devisions)+(h-1)*axis_devisions;
  36. F(f,1) = ((th+1)%axis_devisions)+(h-1)*axis_devisions;
  37. F(f,2) = ((th+0)%axis_devisions)+(h+0)*axis_devisions;
  38. f++;
  39. F(f,0) = ((th+1)%axis_devisions)+(h-1)*axis_devisions;
  40. F(f,1) = ((th+1)%axis_devisions)+(h+0)*axis_devisions;
  41. F(f,2) = ((th+0)%axis_devisions)+(h+0)*axis_devisions;
  42. f++;
  43. }
  44. }
  45. }
  46. assert(f == F.rows());
  47. }
  48. #ifdef IGL_STATIC_LIBRARY
  49. template void igl::cylinder<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(int, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  50. #endif