libMesh
mesh_triangle_wrapper.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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 
30 namespace libMesh
31 {
32 
33 void TriangleWrapper::init(TriangleWrapper::triangulateio & t)
34 {
35  t.pointlist = static_cast<REAL*>(libmesh_nullptr);
36  t.pointattributelist = static_cast<REAL*>(libmesh_nullptr);
37  t.pointmarkerlist = static_cast<int *>(libmesh_nullptr);
38  t.numberofpoints = 0 ;
39  t.numberofpointattributes = 0 ;
40 
41  t.trianglelist = static_cast<int *>(libmesh_nullptr);
42  t.triangleattributelist = static_cast<REAL*>(libmesh_nullptr);
43  t.trianglearealist = static_cast<REAL*>(libmesh_nullptr);
44  t.neighborlist = static_cast<int *>(libmesh_nullptr);
45  t.numberoftriangles = 0;
46  t.numberofcorners = 0;
47  t.numberoftriangleattributes = 0;
48 
49  t.segmentlist = static_cast<int *>(libmesh_nullptr);
50  t.segmentmarkerlist = static_cast<int *>(libmesh_nullptr);
51  t.numberofsegments = 0;
52 
53  t.holelist = static_cast<REAL*>(libmesh_nullptr);
54  t.numberofholes = 0;
55 
56  t.regionlist = static_cast<REAL*>(libmesh_nullptr);
57  t.numberofregions = 0;
58 
59  t.edgelist = static_cast<int *>(libmesh_nullptr);
60  t.edgemarkerlist = static_cast<int *>(libmesh_nullptr);
61  t.normlist = static_cast<REAL*>(libmesh_nullptr);
62  t.numberofedges = 0;
63 }
64 
65 
66 
67 
68 
69 
70 void TriangleWrapper::destroy(TriangleWrapper::triangulateio & t, TriangleWrapper::IO_Type io_type)
71 {
72  std::free (t.pointlist );
73  std::free (t.pointattributelist );
74  std::free (t.pointmarkerlist );
75  std::free (t.trianglelist );
76  std::free (t.triangleattributelist);
77  std::free (t.trianglearealist );
78  std::free (t.neighborlist );
79  std::free (t.segmentlist );
80  std::free (t.segmentmarkerlist );
81 
82  // Only attempt to free these when t was used as an input struct!
83  if (io_type==INPUT)
84  {
85  std::free (t.holelist );
86  std::free (t.regionlist);
87  }
88 
89  std::free (t.edgelist );
90  std::free (t.edgemarkerlist);
91  std::free (t.normlist );
92 
93  // Reset
94  // TriangleWrapper::init(t);
95 }
96 
97 
98 
99 
100 
101 
102 void TriangleWrapper::copy_tri_to_mesh(const triangulateio & triangle_data_input,
103  UnstructuredMesh & mesh_output,
104  const ElemType type)
105 {
106  // Transfer the information into the LibMesh mesh.
107  mesh_output.clear();
108 
109  // Make sure the new Mesh will be 2D
110  mesh_output.set_mesh_dimension(2);
111 
112  // Node information
113  for (int i=0, c=0; c<triangle_data_input.numberofpoints; i+=2, ++c)
114  {
115  // Specify ID when adding point, otherwise, if this is DistributedMesh,
116  // it might add points with a non-sequential numbering...
117  mesh_output.add_point( Point(triangle_data_input.pointlist[i],
118  triangle_data_input.pointlist[i+1]),
119  /*id=*/c);
120  }
121 
122  // Element information
123  for (int i=0; i<triangle_data_input.numberoftriangles; ++i)
124  {
125  switch (type)
126  {
127  case TRI3:
128  {
129  Elem * elem = mesh_output.add_elem (new Tri3);
130 
131  for (unsigned int n=0; n<3; ++n)
132  elem->set_node(n) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*3 + n]);
133 
134  break;
135  }
136 
137  case TRI6:
138  {
139  Elem * elem = mesh_output.add_elem (new Tri6);
140 
141  // Triangle number TRI6 nodes in a different way to libMesh
142  elem->set_node(0) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 0]);
143  elem->set_node(1) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 1]);
144  elem->set_node(2) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 2]);
145  elem->set_node(3) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 5]);
146  elem->set_node(4) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 3]);
147  elem->set_node(5) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 4]);
148 
149  break;
150  }
151 
152  default:
153  libmesh_error_msg("ERROR: Unrecognized triangular element type.");
154  }
155  }
156 
157  // Note: If the input mesh was a parallel one, calling
158  // prepare_for_use() now will re-parallelize it by a call to
159  // delete_remote_elements()... We do not actually want to
160  // reparallelize it here though: the triangulate() function may
161  // still do some Mesh smoothing. The main thing needed (for
162  // smoothing) is the neighbor information, so let's just find
163  // neighbors...
164  //mesh_output.prepare_for_use(/*skip_renumber =*/false);
165  mesh_output.find_neighbors();
166 }
167 
168 
169 }
170 
171 #endif // LIBMESH_HAVE_TRIANGLE
The Tri3 is an element in 2D composed of 3 nodes.
Definition: face_tri3.h:54
virtual Node *& set_node(const unsigned int i)
Definition: elem.h:1941
ElemType
Defines an enum for geometric element types.
This is the base class from which all geometric element types are derived.
Definition: elem.h:89
const class libmesh_nullptr_t libmesh_nullptr
virtual const Node * node_ptr(const dof_id_type i) const =0
The libMesh namespace provides an interface to certain functionality in the library.
virtual void find_neighbors(const bool reset_remote_elements=false, const bool reset_current_list=true) libmesh_override
Other functions from MeshBase requiring re-definition.
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.
The Tri6 is an element in 2D composed of 6 nodes.
Definition: face_tri6.h:49
The UnstructuredMesh class is derived from the MeshBase class.
virtual Elem * add_elem(Elem *e)=0
Add elem e to the end of the element array.
void copy_tri_to_mesh(const triangulateio &triangle_data_input, UnstructuredMesh &mesh_output, const ElemType type)
Copies triangulation data computed by triangle from a triangulateio object to a LibMesh mesh...
void init(triangulateio &t)
Initializes the fields of t to NULL/0 as necessary.
void set_mesh_dimension(unsigned char d)
Resets the logical dimension of the mesh.
Definition: mesh_base.h:199
virtual void clear()
Deletes all the data that are currently stored.
Definition: mesh_base.C:285
void destroy(triangulateio &t, IO_Type)
Frees any memory which has been dynamically allocated by Triangle.
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38