libMesh
mesh_triangle_wrapper.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 #include "libmesh/libmesh_config.h"
20 
21 #ifdef LIBMESH_HAVE_TRIANGLE
22 
23 // Local includes
24 #include "libmesh/mesh_triangle_wrapper.h"
25 #include "libmesh/unstructured_mesh.h"
26 #include "libmesh/point.h"
27 #include "libmesh/face_tri3.h"
28 #include "libmesh/face_tri6.h"
29 #include "libmesh/enum_elem_type.h"
30 
31 namespace libMesh
32 {
33 
34 void TriangleWrapper::init(TriangleWrapper::triangulateio & t)
35 {
36  t.pointlist = static_cast<REAL*>(nullptr);
37  t.pointattributelist = static_cast<REAL*>(nullptr);
38  t.pointmarkerlist = static_cast<int *>(nullptr);
39  t.numberofpoints = 0 ;
40  t.numberofpointattributes = 0 ;
41 
42  t.trianglelist = static_cast<int *>(nullptr);
43  t.triangleattributelist = static_cast<REAL*>(nullptr);
44  t.trianglearealist = static_cast<REAL*>(nullptr);
45  t.neighborlist = static_cast<int *>(nullptr);
46  t.numberoftriangles = 0;
47  t.numberofcorners = 0;
48  t.numberoftriangleattributes = 0;
49 
50  t.segmentlist = static_cast<int *>(nullptr);
51  t.segmentmarkerlist = static_cast<int *>(nullptr);
52  t.numberofsegments = 0;
53 
54  t.holelist = static_cast<REAL*>(nullptr);
55  t.numberofholes = 0;
56 
57  t.regionlist = static_cast<REAL*>(nullptr);
58  t.numberofregions = 0;
59 
60  t.edgelist = static_cast<int *>(nullptr);
61  t.edgemarkerlist = static_cast<int *>(nullptr);
62  t.normlist = static_cast<REAL*>(nullptr);
63  t.numberofedges = 0;
64 }
65 
66 
67 
68 
69 
70 
71 void TriangleWrapper::destroy(TriangleWrapper::triangulateio & t, TriangleWrapper::IO_Type io_type)
72 {
73  std::free (t.pointlist );
74  std::free (t.pointattributelist );
75  std::free (t.pointmarkerlist );
76  std::free (t.trianglelist );
77  std::free (t.triangleattributelist);
78  std::free (t.trianglearealist );
79  std::free (t.neighborlist );
80  std::free (t.segmentlist );
81  std::free (t.segmentmarkerlist );
82 
83  // Only attempt to free these when t was used as an input struct!
84  if (io_type==INPUT)
85  {
86  std::free (t.holelist );
87  std::free (t.regionlist);
88  }
89 
90  std::free (t.edgelist );
91  std::free (t.edgemarkerlist);
92  std::free (t.normlist );
93 
94  // Reset
95  // TriangleWrapper::init(t);
96 }
97 
98 
99 
100 
101 
102 
103 void TriangleWrapper::copy_tri_to_mesh(const triangulateio & triangle_data_input,
104  UnstructuredMesh & mesh_output,
105  const ElemType type,
106  const triangulateio * voronoi)
107 {
108  // Transfer the information into the LibMesh mesh.
109  mesh_output.clear();
110 
111  // Make sure the new Mesh will be 2D
112  mesh_output.set_mesh_dimension(2);
113 
114  // Node information
115  for (int i=0, c=0; c<triangle_data_input.numberofpoints; i+=2, ++c)
116  {
117  // Specify ID when adding point, otherwise, if this is DistributedMesh,
118  // it might add points with a non-sequential numbering...
119  mesh_output.add_point( Point(triangle_data_input.pointlist[i],
120  triangle_data_input.pointlist[i+1]),
121  /*id=*/c);
122  }
123 
124  // Element information
125  for (int i=0; i<triangle_data_input.numberoftriangles; ++i)
126  {
127  switch (type)
128  {
129  case TRI3:
130  {
131  Elem * elem = mesh_output.add_elem (new Tri3);
132 
133  for (unsigned int n=0; n<3; ++n)
134  elem->set_node(n) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*3 + n]);
135 
136  // use the first attribute to set the subdomain ID
137  if (triangle_data_input.triangleattributelist)
138  elem->subdomain_id() =
139  std::round(triangle_data_input.
140  triangleattributelist[i * triangle_data_input.numberoftriangleattributes]);
141  break;
142  }
143 
144  case TRI6:
145  {
146  Elem * elem = mesh_output.add_elem (new Tri6);
147 
148  // Triangle number TRI6 nodes in a different way to libMesh
149  elem->set_node(0) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 0]);
150  elem->set_node(1) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 1]);
151  elem->set_node(2) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 2]);
152  elem->set_node(3) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 5]);
153  elem->set_node(4) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 3]);
154  elem->set_node(5) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 4]);
155 
156  // use the first attribute to set the subdomain ID
157  if (triangle_data_input.triangleattributelist)
158  elem->subdomain_id() =
159  std::round(triangle_data_input.
160  triangleattributelist[i * triangle_data_input.numberoftriangleattributes]);
161  break;
162  }
163 
164  default:
165  libmesh_error_msg("ERROR: Unrecognized triangular element type.");
166  }
167  }
168 
169  // Note: If the input mesh was a parallel one, calling
170  // prepare_for_use() now will re-parallelize it by a call to
171  // delete_remote_elements()... We do not actually want to
172  // reparallelize it here though: the triangulate() function may
173  // still do some Mesh smoothing. The main thing needed (for
174  // smoothing) is the neighbor information, so let's just find
175  // neighbors...
176  //mesh_output.prepare_for_use(/*skip_renumber =*/false);
177  mesh_output.find_neighbors();
178 
179  // set boundary info
180  if (voronoi && triangle_data_input.edgemarkerlist)
181  {
182  BoundaryInfo & boundary_info = mesh_output.get_boundary_info();
183  for (int e=0; e<triangle_data_input.numberofedges; ++e)
184  {
185  if (triangle_data_input.edgemarkerlist[e] != 0)
186  {
187  int p1 = triangle_data_input.edgelist[e + e];
188  int p2 = triangle_data_input.edgelist[e + e + 1];
189  int elem_id = voronoi->edgelist[e + e];
190  unsigned short int s;
191  if (p1 == triangle_data_input.trianglelist[elem_id*3] &&
192  p2 == triangle_data_input.trianglelist[elem_id*3 + 1])
193  s = 0;
194  else if (p1 == triangle_data_input.trianglelist[elem_id*3 + 1] &&
195  p2 == triangle_data_input.trianglelist[elem_id*3 + 2])
196  s = 1;
197  else if (p1 == triangle_data_input.trianglelist[elem_id*3 + 2] &&
198  p2 == triangle_data_input.trianglelist[elem_id*3])
199  s = 2;
200  else
201  libmesh_error_msg("ERROR: finding element errors for boundary edges.");
202 
203  boundary_info.add_side(elem_id, s, triangle_data_input.edgemarkerlist[e]);
204  }
205  }
206  }
207 }
208 
209 
210 }
211 
212 #endif // LIBMESH_HAVE_TRIANGLE
libMesh::BoundaryInfo
The BoundaryInfo class contains information relevant to boundary conditions including storing faces,...
Definition: boundary_info.h:57
libMesh::UnstructuredMesh::find_neighbors
virtual void find_neighbors(const bool reset_remote_elements=false, const bool reset_current_list=true) override
Other functions from MeshBase requiring re-definition.
Definition: unstructured_mesh.C:257
libMesh::MeshBase::get_boundary_info
const BoundaryInfo & get_boundary_info() const
The information about boundary ids on the mesh.
Definition: mesh_base.h:132
libMesh::Tri6
The Tri6 is an element in 2D composed of 6 nodes.
Definition: face_tri6.h:56
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::MeshBase::node_ptr
virtual const Node * node_ptr(const dof_id_type i) const =0
libMesh::TriangleWrapper::init
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
libMesh::TriangleWrapper::copy_tri_to_mesh
void copy_tri_to_mesh(const triangulateio &triangle_data_input, UnstructuredMesh &mesh_output, const ElemType type, const triangulateio *voronoi=nullptr)
Copies triangulation data computed by triangle from a triangulateio object to a LibMesh mesh.
Definition: mesh_triangle_wrapper.C:103
libMesh::TriangleWrapper::destroy
void destroy(triangulateio &t, IO_Type)
Frees any memory which has been dynamically allocated by Triangle.
libMesh::Point
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38
libMesh::TRI3
Definition: enum_elem_type.h:39
libMesh::Elem::set_node
virtual Node *& set_node(const unsigned int i)
Definition: elem.h:2059
libMesh::UnstructuredMesh
The UnstructuredMesh class is derived from the MeshBase class.
Definition: unstructured_mesh.h:48
libMesh::TRI6
Definition: enum_elem_type.h:40
libMesh::MeshBase::add_elem
virtual Elem * add_elem(Elem *e)=0
Add elem e to the end of the element array.
libMesh::TriangleWrapper::INPUT
Definition: mesh_triangle_wrapper.h:66
libMesh::Elem::subdomain_id
subdomain_id_type subdomain_id() const
Definition: elem.h:2069
libMesh::Tri3
The Tri3 is an element in 2D composed of 3 nodes.
Definition: face_tri3.h:56
libMesh::Elem
This is the base class from which all geometric element types are derived.
Definition: elem.h:100
libMesh::TriangleWrapper::IO_Type
IO_Type
Definition: mesh_triangle_wrapper.h:65
libMesh::MeshBase::add_point
virtual Node * add_point(const Point &p, const dof_id_type id=DofObject::invalid_id, const processor_id_type proc_id=DofObject::invalid_processor_id)=0
Add a new Node at Point p to the end of the vertex array, with processor_id procid.
libMesh::MeshBase::clear
virtual void clear()
Deletes all the element and node data that is currently stored.
Definition: mesh_base.C:429
libMesh::MeshBase::set_mesh_dimension
void set_mesh_dimension(unsigned char d)
Resets the logical dimension of the mesh.
Definition: mesh_base.h:218
libMesh::BoundaryInfo::add_side
void add_side(const dof_id_type elem, const unsigned short int side, const boundary_id_type id)
Add side side of element number elem with boundary id id to the boundary information data structure.
Definition: boundary_info.C:886
libMesh::ElemType
ElemType
Defines an enum for geometric element types.
Definition: enum_elem_type.h:33