Alec Jacobson 6 жил өмнө
parent
commit
0eb3fa2acb

+ 54 - 0
include/igl/triangulated_grid.cpp

@@ -0,0 +1,54 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+// 
+// Copyright (C) 2016 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 "triangulated_grid.h"
+#include "grid.h"
+#include <cassert>
+
+template <
+  typename XType,
+  typename YType,
+  typename DerivedGV,
+  typename DerivedGF>
+IGL_INLINE void igl::triangulated_grid(
+  const XType & nx,
+  const YType & ny,
+  Eigen::PlainObjectBase<DerivedGV> & GV,
+  Eigen::PlainObjectBase<DerivedGF> & GF)
+{
+  using namespace Eigen;
+  assert(res.size() == 2 && "Only 2D grids supported");
+  Eigen::Matrix<XType,2,1> res(nx,ny);
+  igl::grid(res,GV);
+  GF.resize((nx-1)*(ny-1)*2,3);
+  for(int y = 0;y<ny-1;y++)
+  {
+    for(int x = 0;x<nx-1;x++)
+    {
+      // index of southwest corner
+      const XType sw = (x  +nx*(y+0));
+      const XType se = (x+1+nx*(y+0));
+      const XType ne = (x+1+nx*(y+1));
+      const XType nw = (x  +nx*(y+1));
+      // Index of first triangle in this square
+      const XType gf = 2*(x+(nx-1)*y);
+      GF(gf+0,0) = sw;
+      GF(gf+0,1) = se;
+      GF(gf+0,2) = nw;
+      GF(gf+1,0) = se;
+      GF(gf+1,1) = ne;
+      GF(gf+1,2) = nw;
+    }
+  }
+}
+
+
+#ifdef IGL_STATIC_LIBRARY
+// Explicit template instantiation
+// generated by autoexplicit.sh
+template void igl::triangulated_grid<int, int, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(int const&, int const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
+#endif

+ 38 - 0
include/igl/triangulated_grid.h

@@ -0,0 +1,38 @@
+// 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_TRIANGULATEGRID_H
+#define IGL_TRIANGULATEGRID_H
+#include "igl_inline.h"
+#include <Eigen/Core>
+namespace igl
+{
+  // Create a regular grid of elements (only 2D supported, currently)
+  // Vertex position order is compatible with `igl::grid`
+  //
+  // Inputs:
+  //   nx  number of vertices in the x direction
+  //   ny  number of vertices in the y direction
+  // Outputs:
+  //   GV  nx*ny by 2 list of mesh vertex positions.
+  //   GF  2*(nx-1)*(ny-1) by 3  list of triangle indices
+  template <
+    typename XType,
+    typename YType,
+    typename DerivedGV,
+    typename DerivedGF>
+  IGL_INLINE void triangulated_grid(
+    const XType & nx,
+    const YType & ny,
+    Eigen::PlainObjectBase<DerivedGV> & GV,
+    Eigen::PlainObjectBase<DerivedGF> & GF);
+}
+#ifndef IGL_STATIC_LIBRARY
+#  include "triangulated_grid.cpp"
+#endif
+#endif 
+

+ 23 - 0
tests/include/igl/triangulated_grid.cpp

@@ -0,0 +1,23 @@
+#include <test_common.h>
+#include <igl/triangulated_grid.h>
+#include <igl/doublearea.h>
+
+TEST(triangulated_grid,area)
+{
+  Eigen::MatrixXd V;
+  Eigen::MatrixXi F;
+  const int nx = 4;
+  const int ny = 7;
+  igl::triangulated_grid(nx,ny,V,F);
+  ASSERT_EQ(V.rows(),nx*ny);
+  ASSERT_EQ(F.rows(),2*(nx-1)*(ny-1));
+  Eigen::VectorXd dblA;
+  igl::doublearea(V,F,dblA);
+  ASSERT_NEAR(dblA.array().sum(),2.0,1e-10);
+  const Eigen::VectorXd dblAgt = 
+    Eigen::VectorXd::Constant(
+      2*(nx-1)*(ny-1),
+      1,
+      2.0/double(2*(nx-1)*(ny-1)));
+  test_common::assert_near(dblA,dblAgt,1e-10);
+}