NiHu  2.0
mesh_building.mex.cpp
1 // This file is a part of NiHu, a C++ BEM template library.
2 //
3 // Copyright (C) 2012-2014 Peter Fiala <fiala@hit.bme.hu>
4 // Copyright (C) 2012-2014 Peter Rucz <rucz@hit.bme.hu>
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 #include <boost/math/constants/constants.hpp>
20 
21 #include "core/mesh.hpp"
22 #include "library/lib_element.hpp"
23 
25 typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> dMatrix;
26 typedef Eigen::Matrix<unsigned, Eigen::Dynamic, Eigen::Dynamic> uMatrix;
28 
29 void test1()
30 {
32  dMatrix nodes(6,3);
33  nodes << // 3 4 5
34  0.0, 0.0, 0.0, // +---+---+
35  1.0, 0.0, 0.0, // | |2 /|
36  2.0, 0.0, 0.0, // | 0 | / |
37  0.0, 1.0, 0.0, // | |/ 1|
38  1.0, 1.0, 0.0, // +---+---+
39  2.0, 1.0, 0.0; // 0 1 2
40 
41  uMatrix elements(3,5);
42  elements <<
43  NiHu::quad_1_elem::id, 0, 1, 4, 3,
44  NiHu::tria_1_elem::id, 1, 2, 5, 0,
45  NiHu::tria_1_elem::id, 1, 5, 4, 0;
47 
49  auto my_mesh = NiHu::create_mesh(nodes, elements, NiHu::quad_1_tag(), NiHu::tria_1_tag());
51 }
52 
53 void test2()
54 {
55  using namespace boost::math::double_constants;
56 
58  double R = 1.0; // radius
59  unsigned N = 180; // number of elements
60 
61  dMatrix nodes(N, 2); // nodal locations
62  for (unsigned i = 0; i < N; ++i)
63  {
64  double phi = i * (two_pi / N);
65  nodes(i,0) = R * cos(phi);
66  nodes(i,1) = R * sin(phi);
67  }
68 
69  uMatrix elements(N,3); // element connections
70  for (unsigned i = 0; i < N; ++i)
71  {
72  elements(i,0) = NiHu::line_1_elem::id;
73  elements(i,1) = i;
74  elements(i,2) = (i+1)%N;
75  }
76 
77  auto mesh = NiHu::create_mesh(nodes, elements, NiHu::line_1_tag()); // homogeneous mesh
79 }
80 
81 
82 
84 #include "util/mex_matrix.hpp"
85 
86 // the MATLAB entry point
87 void mexFunction(int nlhs, mxArray *lhs[], int nrhs, mxArray const *rhs[])
88 {
89  // matrix import
90  NiHu::mex::real_matrix<double> nodes(rhs[0]), elements(rhs[1]);
91  // create the mesh from Matlab
92  auto surf_mesh = NiHu::create_mesh(nodes, elements, NiHu::tria_1_tag(), NiHu::quad_1_tag());
93 }
95 
96 
97 int main()
98 {
99  test1();
100  test2();
101 }
102 
NiHu::type2tag
Metafunction assigning a tag to a type.
Definition: type2tag.hpp:17
NiHu::create_mesh
mesh< tmp::vector< typename tag2type< Args >::type... > > create_mesh(nodes_t const &nodes, elements_t const &elements, Args...)
factory function to create a mesh from nodes and elements matrices
Definition: mesh.hpp:298
mex_matrix.hpp
A Matlab mex matrix interface.
NiHu::mex::real_matrix
Definition: mex_matrix_separate.hpp:133
lib_element.hpp
mesh.hpp
Declaration of class Mesh.