Browse Source

Merge pull request #1369 from libigl/quad-grid

Quad grid
Alec Jacobson 6 years ago
parent
commit
3d05330b68

+ 1 - 0
include/igl/grid.h

@@ -21,6 +21,7 @@ namespace igl
   // Outputs:
   // Outputs:
   //   GV  res.array().prod() by #res list of mesh vertex positions.
   //   GV  res.array().prod() by #res list of mesh vertex positions.
   //   
   //   
+  //   See also: triangulated_grid, quad_grid
   template <
   template <
     typename Derivedres,
     typename Derivedres,
     typename DerivedGV>
     typename DerivedGV>

+ 97 - 0
include/igl/quad_grid.cpp

@@ -0,0 +1,97 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+// 
+// Copyright (C) 2019 Alec Jacobson <[email protected]>
+// 
+// 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 "quad_grid.h"
+#include "grid.h"
+
+template<
+  typename DerivedV,
+  typename DerivedQ,
+  typename DerivedE>
+IGL_INLINE void igl::quad_grid(
+  const int nx,
+  const int ny,
+  Eigen::PlainObjectBase<DerivedV> & V,
+  Eigen::PlainObjectBase<DerivedQ> & Q,
+  Eigen::PlainObjectBase<DerivedE> & E)
+{
+  grid(Eigen::Vector2i(nx,ny),V);
+  return igl::quad_grid(nx,ny,Q,E);
+}
+
+template<
+  typename DerivedQ,
+  typename DerivedE>
+IGL_INLINE void igl::quad_grid(
+  const int nx,
+  const int ny,
+  Eigen::PlainObjectBase<DerivedQ> & Q,
+  Eigen::PlainObjectBase<DerivedE> & E)
+{
+  Eigen::MatrixXi I(nx,ny);
+  Q.resize(  (nx-1)*(ny-1),4);
+  E.resize((nx-1)*ny + (ny-1)*nx,2);
+  {
+    int v = 0;
+    int q = 0;
+    int e = 0;
+    // Ordered to match igl::grid
+    for(int y = 0;y<ny;y++)
+    {
+      for(int x = 0;x<nx;x++)
+      {
+        //// Add a vertex
+        //V(v,0) = (-1.0) + double(x)/double(nx-1) * (1.0 - (-1.0));
+        //V(v,2) = (-1.0) + double(y)/double(ny-1) * (1.0 - (-1.0));
+        I(x,y) = v;
+        v++;
+        // Add a verical edge
+        if(y>0)
+        {
+          E(e,0) = I(x,y);
+          E(e,1) = I(x,y-1);
+          e++;
+        }
+        // Add a horizontal edge
+        if(x>0)
+        {
+          E(e,0) = I(x,y);
+          E(e,1) = I(x-1,y);
+          e++;
+        }
+        // Add two triangles
+        if(x>0 && y>0)
+        {
+          // -1,0----0,0
+          //   |    / |
+          //   |   /  |
+          //   |  /   |
+          //   | /    |
+          // -1,-1---0,-1
+          Q(q,0) = I(x-0,y-0);
+          Q(q,1) = I(x-1,y-0);
+          Q(q,2) = I(x-1,y-1);
+          Q(q,3) = I(x-0,y-1);
+          q++;
+          //F(f,2) = I(x-0,y-0);
+          //F(f,1) = I(x-1,y-0);
+          //F(f,0) = I(x-1,y-1);
+          //f++;
+          //F(f,2) = I(x-0,y-0);
+          //F(f,1) = I(x-1,y-1);
+          //F(f,0) = I(x-0,y-1);
+          //f++;
+        }
+      }
+    }
+  }
+}
+
+#ifdef IGL_STATIC_LIBRARY
+// Explicit template instantiation
+template void igl::quad_grid<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -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> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
+#endif

+ 50 - 0
include/igl/quad_grid.h

@@ -0,0 +1,50 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+// 
+// Copyright (C) 2019 Alec Jacobson <[email protected]>
+// 
+// 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_QUAD_GRID_H
+#define IGL_QUAD_GRID_H
+
+#include <igl/igl_inline.h>
+#include <Eigen/Core>
+
+namespace igl
+{
+  // Generate a quad mesh over a regular grid.
+  //
+  // Inputs:
+  //   nx  number of vertices in the x direction
+  //   ny  number of vertices in the y direction
+  // Outputs:
+  //   V  nx*ny by 2 list of vertex positions
+  //   Q  (nx-1)*(ny-1) by 4 list of quad indices into V
+  //   E  (nx-1)*ny+(ny-1)*nx by 2 list of undirected quad edge indices into V
+  //
+  //   See also: grid, triangulated_grid
+  template<
+    typename DerivedV,
+    typename DerivedQ,
+    typename DerivedE>
+  IGL_INLINE void quad_grid(
+    const int nx,
+    const int ny,
+    Eigen::PlainObjectBase<DerivedV> & V,
+    Eigen::PlainObjectBase<DerivedQ> & Q,
+    Eigen::PlainObjectBase<DerivedE> & E);
+  template<
+    typename DerivedQ,
+    typename DerivedE>
+  IGL_INLINE void quad_grid(
+    const int nx,
+    const int ny,
+    Eigen::PlainObjectBase<DerivedQ> & Q,
+    Eigen::PlainObjectBase<DerivedE> & E);
+}
+
+#ifndef IGL_STATIC_LIBRARY
+#  include "quad_grid.cpp"
+#endif
+#endif

+ 12 - 0
include/igl/triangulated_grid.cpp

@@ -23,6 +23,18 @@ IGL_INLINE void igl::triangulated_grid(
   using namespace Eigen;
   using namespace Eigen;
   Eigen::Matrix<XType,2,1> res(nx,ny);
   Eigen::Matrix<XType,2,1> res(nx,ny);
   igl::grid(res,GV);
   igl::grid(res,GV);
+  return igl::triangulated_grid(nx,ny,GF);
+};
+
+template <
+  typename XType,
+  typename YType,
+  typename DerivedGF>
+IGL_INLINE void igl::triangulated_grid(
+  const XType & nx,
+  const YType & ny,
+  Eigen::PlainObjectBase<DerivedGF> & GF)
+{
   GF.resize((nx-1)*(ny-1)*2,3);
   GF.resize((nx-1)*(ny-1)*2,3);
   for(int y = 0;y<ny-1;y++)
   for(int y = 0;y<ny-1;y++)
   {
   {

+ 10 - 0
include/igl/triangulated_grid.h

@@ -20,6 +20,8 @@ namespace igl
   // Outputs:
   // Outputs:
   //   GV  nx*ny by 2 list of mesh vertex positions.
   //   GV  nx*ny by 2 list of mesh vertex positions.
   //   GF  2*(nx-1)*(ny-1) by 3  list of triangle indices
   //   GF  2*(nx-1)*(ny-1) by 3  list of triangle indices
+  //
+  //   See also: grid, quad_grid
   template <
   template <
     typename XType,
     typename XType,
     typename YType,
     typename YType,
@@ -30,6 +32,14 @@ namespace igl
     const YType & ny,
     const YType & ny,
     Eigen::PlainObjectBase<DerivedGV> & GV,
     Eigen::PlainObjectBase<DerivedGV> & GV,
     Eigen::PlainObjectBase<DerivedGF> & GF);
     Eigen::PlainObjectBase<DerivedGF> & GF);
+  template <
+    typename XType,
+    typename YType,
+    typename DerivedGF>
+  IGL_INLINE void triangulated_grid(
+    const XType & nx,
+    const YType & ny,
+    Eigen::PlainObjectBase<DerivedGF> & GF);
 }
 }
 #ifndef IGL_STATIC_LIBRARY
 #ifndef IGL_STATIC_LIBRARY
 #  include "triangulated_grid.cpp"
 #  include "triangulated_grid.cpp"