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 
13 template <typename DerivedV, typename DerivedF>
cylinder(const int axis_devisions,const int height_devisions,Eigen::PlainObjectBase<DerivedV> & V,Eigen::PlainObjectBase<DerivedF> & F)14 IGL_INLINE void igl::cylinder(
15   const int axis_devisions,
16   const int height_devisions,
17   Eigen::PlainObjectBase<DerivedV> & V,
18   Eigen::PlainObjectBase<DerivedF> & F)
19 {
20   V.resize(axis_devisions*height_devisions,3);
21   F.resize(2*(axis_devisions*(height_devisions-1)),3);
22   int f = 0;
23   typedef typename DerivedV::Scalar Scalar;
24   for(int th = 0;th<axis_devisions;th++)
25   {
26     Scalar x = cos(2.*igl::PI*Scalar(th)/Scalar(axis_devisions));
27     Scalar y = sin(2.*igl::PI*Scalar(th)/Scalar(axis_devisions));
28     for(int h = 0;h<height_devisions;h++)
29     {
30       Scalar z = Scalar(h)/Scalar(height_devisions-1);
31       V(th+h*axis_devisions,0) = x;
32       V(th+h*axis_devisions,1) = y;
33       V(th+h*axis_devisions,2) = z;
34       if(h > 0)
35       {
36         F(f,0) = ((th+0)%axis_devisions)+(h-1)*axis_devisions;
37         F(f,1) = ((th+1)%axis_devisions)+(h-1)*axis_devisions;
38         F(f,2) = ((th+0)%axis_devisions)+(h+0)*axis_devisions;
39         f++;
40         F(f,0) = ((th+1)%axis_devisions)+(h-1)*axis_devisions;
41         F(f,1) = ((th+1)%axis_devisions)+(h+0)*axis_devisions;
42         F(f,2) = ((th+0)%axis_devisions)+(h+0)*axis_devisions;
43         f++;
44       }
45     }
46   }
47   assert(f == F.rows());
48 }
49 
50 #ifdef IGL_STATIC_LIBRARY
51 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> >&);
52 #endif
53