Wildmeshing Toolkit
Grid2Options.cpp
Go to the documentation of this file.
1 #include "Grid2Options.hpp"
2 #include <wmtk/TriMesh.hpp>
4 #include "grid_utils.hpp"
5 
6 
8 const std::array<std::string, 2> Grid2Options::tiling_names = {{"bcc", "diagonal"}};
9 
10 namespace {
11 std::shared_ptr<TriMesh> make_diagonal_mesh(const Grid2Options& opt)
12 {
13  const auto d = opt.dimensions;
14  auto vertex_dimensions = opt.dimensions;
15  for (size_t j = 0; j < d.size(); ++j) {
16  int64_t& v = vertex_dimensions[j];
17  if (!opt.cycles[j]) {
18  v++;
19  }
20  }
21  using CoordType = std::array<int64_t, 2>;
22 
23  Eigen::Matrix<int64_t, Eigen::Dynamic, 3> FV(2 * d[0] * d[1], 3);
24 
25  CoordType i;
26  // auto& [j, k] = i;
27  int64_t& j = i[0];
28  int64_t& k = i[1];
29  for (j = 0; j < d[0]; ++j) {
30  for (k = 0; k < d[1]; ++k) {
31  auto f = [&](int64_t a, int64_t b) {
32  CoordType coord{{j + a, k + b}};
33  for (size_t e = 0; e < 2; ++e) {
34  auto& v = coord[e];
35  v = v % vertex_dimensions[e];
36  }
37  return procedural::grid_index(vertex_dimensions, coord);
38  };
39  int64_t c[2][2] = {
40  {
41  f(0, 0),
42  f(1, 0),
43  },
44  {
45  f(0, 1),
46  f(1, 1),
47  }};
48  int64_t f0_index = 2 * procedural::grid_index(d, i);
49  int64_t f1_index = f0_index + 1;
50 
51  FV.row(f0_index) << c[0][0], c[0][1], c[1][0];
52  FV.row(f1_index) << c[1][1], c[1][0], c[0][1];
53  }
54  }
55 
56  auto m = std::make_shared<TriMesh>();
57  m->initialize(FV);
58  if (opt.coordinates.has_value()) {
59  int vertex_size = vertex_dimensions[0] * vertex_dimensions[1];
60 
61  const auto& spacing = opt.coordinates->spacing;
62  Eigen::Matrix<double, Eigen::Dynamic, 2> P(vertex_size, 2);
63  for (j = 0; j < vertex_dimensions[0]; ++j) {
64  double x = spacing[0] * j;
65  for (k = 0; k < vertex_dimensions[1]; ++k) {
66  double y = spacing[1] * k;
67  int64_t index = procedural::grid_index(vertex_dimensions, i);
68 
69  P.row(index) << x, y;
70  }
71  }
72  const auto& name = opt.coordinates->name;
74  }
75  return m;
76 }
77 } // namespace
78 
79 std::shared_ptr<TriMesh> make_mesh(const Grid2Options& opt)
80 {
81  switch (opt.tiling_type) {
82  case Grid2Options::TilingType::Diagonal: return make_diagonal_mesh(opt);
84  throw std::runtime_error("bcc lattice not implemented yet");
85  [[fallthrough]];
86  default: break;
87  }
88  throw std::runtime_error("failed to select a tiling type");
89  return nullptr;
90 }
91 } // namespace wmtk::components::procedural
static const std::array< std::string, 2 > tiling_names
std::shared_ptr< TriMesh > make_mesh(const DiskOptions &opt)
Definition: DiskOptions.cpp:10
int64_t grid_index(const std::array< int64_t, 3 > &d, const std::array< int64_t, 3 > &i)
Definition: grid_utils.cpp:16
attribute::MeshAttributeHandle set_matrix_attribute(const Mat &data, const std::string &name, const PrimitiveType &type, Mesh &mesh)
Definition: mesh_utils.hpp:9