瀏覽代碼

Merge pull request #997 from wucm1994/master

cylinder with caps
Francis Williams 6 年之前
父節點
當前提交
b47c6b5472
共有 2 個文件被更改,包括 101 次插入0 次删除
  1. 68 0
      include/igl/cylinder_with_caps.cpp
  2. 33 0
      include/igl/cylinder_with_caps.h

+ 68 - 0
include/igl/cylinder_with_caps.cpp

@@ -0,0 +1,68 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+//
+// Copyright (C) 2018 Alec Jacobson <alecjacobson@gmail.com>
+//
+// This Source Code Form is subject to the terms of the Mozilla Public License
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
+// obtain one at http://mozilla.org/MPL/2.0/.
+#include "cylinder_with_caps.h"
+#include "PI.h"
+#include <cassert>
+#include <cmath>
+
+template <typename DerivedV, typename DerivedF>
+IGL_INLINE void igl::cylinder_with_caps(
+    const int axis_devisions,
+    const int height_devisions,
+    Eigen::PlainObjectBase<DerivedV> &V,
+    Eigen::PlainObjectBase<DerivedF> &F)
+{
+  V.resize(axis_devisions * height_devisions, 3);
+  F.resize(2 * (axis_devisions * height_devisions) - 4, 3);
+  int f = 0;
+  typedef typename DerivedV::Scalar Scalar;
+  for (int th = 0; th < axis_devisions; th++)
+  {
+    Scalar x = cos(2. * igl::PI * Scalar(th) / Scalar(axis_devisions));
+    Scalar y = sin(2. * igl::PI * Scalar(th) / Scalar(axis_devisions));
+    for (int h = 0; h < height_devisions; h++)
+    {
+      Scalar z = Scalar(h) / Scalar(height_devisions - 1);
+      V(th + h * axis_devisions, 0) = x;
+      V(th + h * axis_devisions, 1) = y;
+      V(th + h * axis_devisions, 2) = z;
+      if (h > 0)
+      {
+        F(f, 0) = ((th + 0) % axis_devisions) + (h - 1) * axis_devisions;
+        F(f, 1) = ((th + 1) % axis_devisions) + (h - 1) * axis_devisions;
+        F(f, 2) = ((th + 0) % axis_devisions) + (h + 0) * axis_devisions;
+        f++;
+        F(f, 0) = ((th + 1) % axis_devisions) + (h - 1) * axis_devisions;
+        F(f, 1) = ((th + 1) % axis_devisions) + (h + 0) * axis_devisions;
+        F(f, 2) = ((th + 0) % axis_devisions) + (h + 0) * axis_devisions;
+        f++;
+      }
+    }
+  }
+
+  for (int i = 0; i < axis_devisions - 2; ++i)
+  {
+    F(f, 2) = 0;
+    F(f, 1) = ((i + 1) % axis_devisions);
+    F(f, 0) = ((i + 2) % axis_devisions);
+    f++;
+  }
+
+  for (int i = 0; i < axis_devisions - 2; ++i)
+  {
+    F(f, 0) = (height_devisions - 1) * axis_devisions;
+    F(f, 1) = ((i + 1) % axis_devisions) + (height_devisions - 1) * axis_devisions;
+    F(f, 2) = ((i + 2) % axis_devisions) + (height_devisions - 1) * axis_devisions;
+    f++;
+  }
+  assert(f == F.rows());
+}
+
+#ifdef IGL_STATIC_LIBRARY
+template void igl::cylinder_with_caps<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>> &);
+#endif

+ 33 - 0
include/igl/cylinder_with_caps.h

@@ -0,0 +1,33 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+// 
+// Copyright (C) 2018 Alec Jacobson <alecjacobson@gmail.com>
+// 
+// This Source Code Form is subject to the terms of the Mozilla Public License 
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can 
+// obtain one at http://mozilla.org/MPL/2.0/.
+#ifndef IGL_CYLINDER_WITH_CAPS_H
+#define IGL_CYLINDER_WITH_CAPS_H
+#include "igl_inline.h"
+#include <Eigen/Core>
+namespace igl
+{
+  // Construct a triangle mesh of a cylinder (with caps)
+  //
+  // Inputs:
+  //   axis_devisions  number of vertices _around the cylinder_
+  //   height_devisions  number of vertices _up the cylinder_
+  // Outputs:
+  //   V  #V by 3 list of mesh vertex positions
+  //   F  #F by 3 list of triangle indices into V
+  //
+  template <typename DerivedV, typename DerivedF>
+  IGL_INLINE void cylinder_with_caps(
+    const int axis_devisions,
+    const int height_devisions,
+    Eigen::PlainObjectBase<DerivedV> & V,
+    Eigen::PlainObjectBase<DerivedF> & F);
+}
+#ifndef IGL_STATIC_LIBRARY
+#  include "cylinder_with_caps.cpp"
+#endif
+#endif