File: File_tetgen.h

package info (click to toggle)
cgal 6.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 144,952 kB
  • sloc: cpp: 811,597; ansic: 208,576; sh: 493; python: 411; makefile: 286; javascript: 174
file content (235 lines) | stat: -rw-r--r-- 6,980 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) 2012  GeometryFactory Sarl (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1.1/SMDS_3/include/CGAL/IO/File_tetgen.h $
// $Id: include/CGAL/IO/File_tetgen.h 08b27d3db14 $
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s)     : Laurent Rineau

#ifndef CGAL_IO_FILE_TETGEN_H
#define CGAL_IO_FILE_TETGEN_H

#include <CGAL/license/SMDS_3.h>

#include <CGAL/Time_stamper.h>
#include <CGAL/IO/File_medit.h>
#include <CGAL/utility.h>

#include <fstream>
#include <iostream>
#include <string>

namespace CGAL {

namespace SMDS_3 {

template <class C3T3, bool rebind, bool no_patch>
void
output_to_tetgen(std::string filename,
                 const C3T3& c3t3)
{
#ifdef CGAL_MESH_3_IO_VERBOSE
  std::cerr << "Output to tetgen:\n";
#endif

  typedef Medit_pmap_generator<C3T3,Renumber_subdomain_indices(rebind),Facet_indices(no_patch)> Generator;
  typedef typename Generator::Cell_pmap Cell_pmap;
  typedef typename Generator::Facet_pmap Facet_pmap;
  typedef typename Generator::Facet_pmap_twice Facet_pmap_twice;
  typedef typename Generator::Vertex_pmap Vertex_pmap;

  Cell_pmap cell_pmap(c3t3);
  Facet_pmap facet_pmap(c3t3,cell_pmap);
  Facet_pmap_twice facet_pmap_twice(c3t3,cell_pmap);
  Vertex_pmap vertex_pmap(c3t3,cell_pmap,facet_pmap);

  output_to_tetgen(filename,
                   c3t3,
                   vertex_pmap,
                   facet_pmap,
                   cell_pmap,
                   facet_pmap_twice,
                   Generator().print_twice());

#ifdef CGAL_MESH_3_IO_VERBOSE
  std::cerr << "done.\n";
#endif
}



template <class C3T3,
          class Vertex_index_property_map,
          class Facet_index_property_map,
          class Facet_index_property_map_twice,
          class Cell_index_property_map>
void
output_to_tetgen(std::string filename,
                 const C3T3& c3t3,
                 const Vertex_index_property_map& /* vertex_pmap */,
                 const Facet_index_property_map& /* facet_pmap */,
                 const Cell_index_property_map& /* cell_pmap */,
                 const Facet_index_property_map_twice& /* facet_twice_pmap */ = Facet_index_property_map_twice(),
                 const bool /* print_each_facet_twice */ = false)
{
  typedef typename C3T3::Triangulation Tr;
  typedef typename C3T3::Facets_in_complex_iterator Facet_iterator;
  typedef typename C3T3::Cells_in_complex_iterator Cell_iterator;

  typedef typename Tr::Finite_vertices_iterator Finite_vertices_iterator;
  typedef typename Tr::Vertex_handle Vertex_handle;
  typedef typename Tr::Cell_handle Cell_handle;
  typedef typename Tr::Weighted_point Weighted_point;
  typedef typename Tr::Facet Facet;

  typedef CGAL::Hash_handles_with_or_without_timestamps Hash_fct;

  const Tr& tr = c3t3.triangulation();

  boost::unordered_map<Vertex_handle, std::size_t, Hash_fct> V;

  //-------------------------------------------------------
  // File output
  //-------------------------------------------------------

  //-------------------------------------------------------
  // nodes
  //-------------------------------------------------------

  std::string node_filename = filename + ".node";
  std::ofstream node_stream(node_filename.c_str());

  node_stream << std::setprecision(17);
  node_stream << tr.number_of_vertices() << " 3 0 0" << std::endl;

  std::size_t vert_counter = 0;
  for(Finite_vertices_iterator
        vit = tr.finite_vertices_begin(),
        end = tr.finite_vertices_end();
      vit != end; ++vit)
  {
    const Weighted_point& p = tr.point(vit);
    const double x = CGAL::to_double(p.x());
    const double y = CGAL::to_double(p.y());
    const double z = CGAL::to_double(p.z());

    V[vit] = ++vert_counter;

    node_stream << vert_counter << " " << x << " " << y << " " << z;
    node_stream << std::endl;
  }
  node_stream.close();


  //-------------------------------------------------------
  // Elements
  //-------------------------------------------------------

  std::string elem_filename = filename + ".ele";
  std::ofstream elem_stream(elem_filename.c_str());

  elem_stream << std::setprecision(17);
  elem_stream << c3t3.number_of_cells_in_complex() << " 4 0" << std::endl;

  std::size_t cell_counter = 0;
  for (Cell_iterator
         cit = c3t3.cells_in_complex_begin(),
         end = c3t3.cells_in_complex_end();
       cit != end; ++cit)
  {
    const Cell_handle ch = cit;

    elem_stream << ++cell_counter;
    for (int i=3; i>=0; i--)
    {
      const Vertex_handle vh = ch->vertex(i);
      elem_stream << " " << V[vh];
    }
    elem_stream << std::endl;
  }
  elem_stream.close();


  //-------------------------------------------------------
  // Face
  //-------------------------------------------------------

  std::string face_filename = filename + ".face";
  std::ofstream face_stream(face_filename.c_str());

  face_stream << std::setprecision(17);
  face_stream << c3t3.number_of_facets_in_complex() << " 0" << std::endl;

  std::size_t facet_counter = 0;
  for(Facet_iterator
        fit = c3t3.facets_in_complex_begin(),
        end = c3t3.facets_in_complex_end();
      fit != end; ++fit )
  {
    const Facet& facet = *fit;

    Vertex_handle vh1 = facet.first->vertex((facet.second+1)%4);
    Vertex_handle vh2 = facet.first->vertex((facet.second+2)%4);
    Vertex_handle vh3 = facet.first->vertex((facet.second+3)%4);

    face_stream << ++facet_counter << " " << V[vh1] << " " << V[vh2] << " " << V[vh3] << std::endl;
  }
  face_stream.close();

  //-------------------------------------------------------
  // End
  //-------------------------------------------------------
} // end output_to_tetgen(...)

} // end namespace SMDS_3

namespace IO {


/**
 * \ingroup PkgSMDS3ExportFunctions
 * @brief exports a mesh complex to tetgen format
 * @param filename the path to the output files, without the extension.
 * @param c3t3 the mesh complex
 * @param rebind if true, labels of cells are rebinded into [1..nb_of_labels]
 * @param show_patches if true, patches are labeled with different labels than
 * cells. If false, each surface facet is written twice, using the label of
 * each adjacent cell.
 * \see \ref IOStreamTetgen
 */
template <class C3T3>
void
output_to_tetgen(std::string filename,
                 const C3T3& c3t3,
                 bool rebind = false,
                 bool show_patches = false)
{
  if ( rebind )
  {
    if ( show_patches )
      SMDS_3::output_to_tetgen<C3T3,true,false>(filename,c3t3);
    else
      SMDS_3::output_to_tetgen<C3T3,true,true>(filename,c3t3);
  }
  else
  {
    if ( show_patches )
      SMDS_3::output_to_tetgen<C3T3,false,false>(filename,c3t3);
    else
      SMDS_3::output_to_tetgen<C3T3,false,true>(filename,c3t3);
  }
}

} // namespace IO

#ifndef CGAL_NO_DEPRECATED_CODE
using IO::output_to_tetgen;
#endif

} // end namespace CGAL

#endif // CGAL_IO_FILE_TETGEN_H