mesh_triangle_wrapper.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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
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 {
107  // Transfer the information into the LibMesh mesh.
108  mesh_output.clear();
109 
110  // Make sure the new Mesh will be 2D
111  mesh_output.set_mesh_dimension(2);
112 
113  // Node information
114  for (int i=0, c=0; c<triangle_data_input.numberofpoints; i+=2, ++c)
115  {
116  // Specify ID when adding point, otherwise, if this is DistributedMesh,
117  // it might add points with a non-sequential numbering...
118  mesh_output.add_point( Point(triangle_data_input.pointlist[i],
119  triangle_data_input.pointlist[i+1]),
120  /*id=*/c);
121  }
122 
123  // Element information
124  for (int i=0; i<triangle_data_input.numberoftriangles; ++i)
125  {
126  switch (type)
127  {
128  case TRI3:
129  {
130  Elem * elem = mesh_output.add_elem (new Tri3);
131 
132  for (unsigned int n=0; n<3; ++n)
133  elem->set_node(n) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*3 + n]);
134 
135  break;
136  }
137 
138  case TRI6:
139  {
140  Elem * elem = mesh_output.add_elem (new Tri6);
141 
142  // Triangle number TRI6 nodes in a different way to libMesh
143  elem->set_node(0) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 0]);
144  elem->set_node(1) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 1]);
145  elem->set_node(2) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 2]);
146  elem->set_node(3) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 5]);
147  elem->set_node(4) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 3]);
148  elem->set_node(5) = mesh_output.node_ptr(triangle_data_input.trianglelist[i*6 + 4]);
149 
150  break;
151  }
152 
153  default:
154  libmesh_error_msg("ERROR: Unrecognized triangular element type.");
155  }
156  }
157 
158  // Note: If the input mesh was a parallel one, calling
159  // prepare_for_use() now will re-parallelize it by a call to
160  // delete_remote_elements()... We do not actually want to
161  // reparallelize it here though: the triangulate() function may
162  // still do some Mesh smoothing. The main thing needed (for
163  // smoothing) is the neighbor information, so let's just find
164  // neighbors...
165  //mesh_output.prepare_for_use(/*skip_renumber =*/false);
166  mesh_output.find_neighbors();
167 }
168 
169 
170 }
171 
172 #endif // LIBMESH_HAVE_TRIANGLE
A 2D triangular element with 3 nodes.
Definition: face_tri3.h:56
virtual Node *& set_node(const unsigned int i)
Definition: elem.h:2024
The base class for all geometric element types.
Definition: elem.h:100
virtual void find_neighbors(const bool reset_remote_elements=false, const bool reset_current_list=true) override
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
A 2D triangular element with 6 nodes.
Definition: face_tri6.h:56
Base class for Replicated and Distributed meshes.
virtual Elem * add_elem(Elem *e)=0
void copy_tri_to_mesh(const triangulateio &triangle_data_input, UnstructuredMesh &mesh_output, const ElemType type)
void init(triangulateio &t)
void set_mesh_dimension(unsigned char d)
Definition: mesh_base.h:213
virtual void clear()
Definition: mesh_base.C:260
void destroy(triangulateio &t, IO_Type)
virtual const Node * node_ptr(const dof_id_type i) const =0
A geometric point in (x,y,z) space.
Definition: point.h:38