Debian Codegraph @

debian/external_deps/fetk/gamer/tools/GenerateMesh/GenerateMesh.C

raw
1 /*
2 * ***************************************************************************
3 * GAMER = < Geometry-preserving Adaptive MeshER >
4 * Copyright (C) 1994-- Michael Holst and Zeyun Yu
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * ***************************************************************************
20 */
21
22 /*
23 * ***************************************************************************
24 * File: GenerateMesh.C < ... >
25 *
26 * Author: Johan Hake ([email protected])
27 *
28 * Purpose: The 'main' function of GenerateMesh
29 * ***************************************************************************
30 */
31
32 #include <gamer/gamer.h>
33
34 const char* supported_formats[] = {"mcsf", "dolfin"};
35 const char default_mesh_format[] = "mcsf";
36 const unsigned int num_supported_formats = 2;
37 const char tetgen_default_params[] = "nnepAAY";
38 const char tetgen_default_quality_params[] = "q1.333";
39
40 /*
41 * ***************************************************************************
42 * Routine: < ... >
43 *
44 * Author: Zeyun Yu ([email protected])
45 *
46 * Purpose: Assign active sites from a list of user-defined spheres
47 * ***************************************************************************
48 */
49 //void SetAreaConstraint(SurfaceMesh* surfmesh, ATOM* sphere_list,
50 // unsigned int* marker_list, int num_spheres,
51 // float* area_constraint_list,
52 // int& num_facet_area_constraints,
53 // double* facet_area_constraints)
54 //{
55 // unsigned int v, sphere, face, a, b, c, i;
56 // unsigned int* vertex_spheres, *vertex_markers;
57 //
58 // float x, y, z;
59 // float center_x, center_y, center_z;
60 // float dist, radius;
61 //
62 // double* tmp_face_area_constraints;
63 // unsigned char face_marker;
64 //
65 // // Initalize vertex marker and face marker list memory
66 // vertex_spheres = (unsigned int*)malloc(sizeof(unsigned int)*surfmesh->nv);
67 // vertex_markers = (unsigned int*)malloc(sizeof(unsigned int)*surfmesh->nv);
68 // tmp_face_area_constraints = (double*)malloc(sizeof(double)*surfmesh->nf*2);
69 //
70 // // Reset data
71 // for (v = 0; v < surfmesh->nv; v++)
72 // {
73 // vertex_spheres[v] = 0;
74 // vertex_markers[v] = 0;
75 // }
76 //
77 // num_facet_area_constraints = 0;
78 //
79 // // Walk through all spheres and mark the vertices
80 // for (sphere = 0; sphere < num_spheres; sphere++) {
81 // center_x = sphere_list[sphere].x;
82 // center_y = sphere_list[sphere].y;
83 // center_z = sphere_list[sphere].z;
84 // radius = sphere_list[sphere].radius;
85 //
86 // for (v = 0; v < surfmesh->nv; v++) {
87 // x = surfmesh->vertex[v].x;
88 // y = surfmesh->vertex[v].y;
89 // z = surfmesh->vertex[v].z;
90 // dist = sqrt((x-center_x)*(x-center_x)+
91 // (y-center_y)*(y-center_y)+
92 // (z-center_z)*(z-center_z));
93 // if (dist < radius){
94 // vertex_markers[v] = marker_list[sphere];
95 // vertex_spheres[v] = sphere;
96 // }
97 // }
98 // }
99 //
100 // // Walk through the facets and collect area constraint info
101 // for (face = 0; face < surfmesh->nf; face++){
102 //
103 // // Get the vertices
104 // a = surfmesh->face[face].a;
105 // b = surfmesh->face[face].b;
106 // c = surfmesh->face[face].c;
107 //
108 // // Get the marker of the vertices and check that all are the same
109 // face_marker = vertex_markers[a];
110 // if (face_marker != 0 || face_marker != vertex_markers[b])
111 // continue;
112 // if (face_marker != vertex_markers[c])
113 // continue;
114 //
115 // // If all vertices on a face have the same marker,
116 // // then is the area contraint applied to that face
117 // tmp_face_area_constraints[num_facet_area_constraints*2] = face;
118 // tmp_face_area_constraints[num_facet_area_constraints*2+1] = \
119 // area_constraint_list[vertex_spheres[a]];
120 //
121 // num_facet_area_constraints++;
122 // }
123 //
124 // // Create return array
125 // facet_area_constraints = new double[num_facet_area_constraints*2];
126 //
127 // // Copy tmp data to return array
128 // for (i = 0; i < num_facet_area_constraints*2; i++)
129 // facet_area_constraints[i] = tmp_face_area_constraints[i];
130 //
131 // // Free memory
132 // free(vertex_markers);
133 // free(tmp_face_area_constraints);
134 //
135 //}
136
137 /*
138 * ***************************************************************************
139 * Routine: GenerateMeshFromSurfaceMesh < ... >
140 *
141 * Author: Johan Hake ([email protected]), Zeyun Yu ([email protected])
142 *
143 * Purpose: Tetrahedralize a surface mesh
144 * ***************************************************************************
145 */
146 void GenerateMeshFromSurfaceMesh(char* input_name, char* input_site,
147 char* mesh_format, char* tetgen_params)
148 {
149 FILE *fout;
150
151 time_t t0, t1, t2;
152 SurfaceMesh* surfmesh = NULL;
153 char filename[256], basename[256];
154 char* basename_end_pointer;
155 char* suffix_end_pointer;
156 unsigned int basename_index;
157
158 unsigned int num_spheres = 0;
159 ATOM *sphere_list = NULL;
160 unsigned int* marker_list = NULL;
161 unsigned char* active_sites = NULL;
162 float* area_constraint_list = NULL;
163 int dummy_count = -1;
164 bool use_facemarkers = false;
165
166 tetgenio in, out;
167 tetgenio::facet *f;
168 tetgenio::polygon *p;
169
170 unsigned int i, j;
171
172 // Read any active sites from file
173 if (input_site != NULL)
174 ReadActiveSiteFile(input_site, num_spheres, sphere_list, marker_list);
175 // area_constraint_list);
176
177 // Read surface mesh file together with basename
178 strcpy(filename, input_name);
179
180 // Check if input is a .off file
181 basename_end_pointer = strstr(filename, ".off");
182 if (basename_end_pointer == NULL){
183 printf("Provide a mesh in \'.off\' format.\n");
184 exit(1);
185 }
186
187 // Get the basename
188 basename_index = basename_end_pointer-filename;
189 strncpy (basename, filename, basename_index);
190 basename[basename_index] = '\0';
191
192 // Load surface meshes in OFF format
193 surfmesh = SurfaceMesh_readOFF(input_name);
194
195 printf("Begin generating tetrahedral mesh.\n");
196 (void)time(&t0);
197
198 // Convert to a generalized tetrahedral mesh structure
199 GemMesh* gem_out = GemMesh_fromSurfaceMesh(surfmesh, tetgen_params);//GemMesh_fromTetgen(out);
200
201 // Save output mesh in given format
202 if (strcmp(mesh_format, "mcsf")==0)
203 {
204 fflush(stdout);
205 sprintf(filename, "%s.m", basename);
206 printf("Writing results to: %s\n", filename);
207 GemMesh_writeMcsf(gem_out, filename);
208 }
209
210 // Release memory
211 GemMesh_dtor(gem_out);
212 SurfaceMesh_dtor(surfmesh);
213 }
214
215
216 bool check_format(char* format)
217 {
218 for (unsigned int i=0; i<num_supported_formats; i++)
219 if (strcmp(format, supported_formats[i]) == 0)
220 return true;
221 return false;
222 }
223
224 void get_supported_formats(char* formats)
225 {
226 for (unsigned int i=0; i<num_supported_formats; i++)
227 if (i == 0)
228 sprintf(formats, "\'%s\'", supported_formats[i]);
229 else if (i < num_supported_formats-1)
230 sprintf(formats, "%s, \'%s\'", formats, supported_formats[i]);
231 else
232 sprintf(formats, "%s and \'%s\'", formats, supported_formats[i]);
233 }
234
235 void long_usage()
236 {
237 char mesh_formats[1024];
238 get_supported_formats(mesh_formats);
239 printf("Usage: GenerateMesh [OPTION] FILE\n");
240 printf("Generates a tetrahedral mesh from a surface mesh given in FILE");
241 printf("\n");
242 printf(" FILE - A surface mesh given in .off format\n");
243 printf("\n");
244 printf("Options:\n");
245 printf(" --tetgen-params PAR Quality parameters which are passed to tetgen:\n");
246 printf(" Default: \'%s\'\n", tetgen_default_quality_params);
247 printf(" Example: \'q2a0.5\'\n");
248 printf(" In addition are these parameters automatically used:\n");
249 printf(" \'%s\'\n", tetgen_default_params);
250 printf(" --format FORMAT The output mesh format. Supported formats:\n");
251 printf(" %s\n", mesh_formats);
252 printf(" Default: \'%s\'\n", default_mesh_format);
253 printf(" --active-sites FILE A file with active sites which will not be coarsed\n"
254 " during quality improvement. \n"
255 " See README for specifications.\n");
256 printf(" --help Print this help.\n");
257 }
258
259 void option_error()
260 {
261 printf("Error in parsing command line:\nSee \'GenerateMesh --help\' for more information\n");
262 exit(1);
263 }
264
265 void parse_comand_line(int argc, char* argv[], char*& input_filename,
266 char*& input_sites, char*& mesh_format, char*& tetgen_params)
267 {
268 int i = 1;
269
270 if (argc == 1)
271 {
272 printf("Usage: GenerateMesh [OPTION] FILE\n");
273 exit(0);
274 }
275
276 // Parse command line
277 while (i < argc)
278 {
279 // Parse --help
280 if (strcmp (argv[i], "--help") == 0)
281 {
282 long_usage();
283 exit(0);
284 }
285
286 // Parse --format option
287 if (strcmp (argv[i], "--format") == 0)
288 {
289 i++;
290 if (i == argc)
291 option_error();
292 mesh_format = argv[i];
293 if (check_format(mesh_format))
294 option_error();
295 i++;
296 if (i == argc)
297 option_error();
298
299 continue;
300 }
301
302 // Parse --format option
303 if (strcmp (argv[i], "--tetgen-params") == 0)
304 {
305 i++;
306 if (i == argc)
307 option_error();
308 sprintf(tetgen_params, "%s%s", tetgen_default_params, argv[i]);
309 i++;
310 if (i == argc)
311 option_error();
312
313 continue;
314 }
315
316 // Parse --active-sites option
317 if (strcmp (argv[i], "--active-sites") == 0)
318 {
319 i++;
320 if (i == argc)
321 option_error();
322 input_sites = argv[i];
323 i++;
324 if (i == argc)
325 option_error();
326
327 continue;
328 }
329
330 // Parse filename
331 input_filename = argv[i];
332 i++;
333 if (i != argc)
334 option_error();
335
336 }
337
338 }
339
340 int main(int argc, char *argv[])
341 {
342 char* input_sites = NULL;
343 char* input_filename = NULL;
344 char* mesh_format = NULL;
345 char* passed_mesh_format = (char*)malloc(sizeof(char)*16);;
346 char* tetgen_params = (char*)malloc(sizeof(char)*16);
347
348 // Parse command line
349 parse_comand_line(argc, argv, input_filename, input_sites,
350 mesh_format, tetgen_params);
351
352 // Set default tetgen params
353 sprintf(tetgen_params, "%s%s", tetgen_default_params, tetgen_default_quality_params);
354 if (mesh_format == NULL)
355 sprintf(passed_mesh_format, "%s", default_mesh_format);
356 else
357 sprintf(passed_mesh_format, "%s", mesh_format);
358
359 // Call GenerateMesh routine
360 GenerateMeshFromSurfaceMesh(input_filename, input_sites,
361 passed_mesh_format, tetgen_params);
362
363 // Free memory
364 free(tetgen_params);
365 free(passed_mesh_format);
366
367 return(0);
368 }