Mmg
Simplicial remeshers (mesh adaptation, isovalue discretization, lagrangian movement)
genericIO.c
Go to the documentation of this file.
1/* =============================================================================
2** This file is part of the mmg software package for the tetrahedral
3** mesh modification.
4** Copyright (c) Bx INP/Inria/UBordeaux/UPMC, 2004- .
5**
6** mmg is free software: you can redistribute it and/or modify it
7** under the terms of the GNU Lesser General Public License as published
8** by the Free Software Foundation, either version 3 of the License, or
9** (at your option) any later version.
10**
11** mmg is distributed in the hope that it will be useful, but WITHOUT
12** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13** FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14** License for more details.
15**
16** You should have received a copy of the GNU Lesser General Public
17** License and of the GNU General Public License along with mmg (in
18** files COPYING.LESSER and COPYING). If not, see
19** <http://www.gnu.org/licenses/>. Please read their terms carefully and
20** use this copy of the mmg distribution only if you accept them.
21** =============================================================================
22*/
23
37#include <assert.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <signal.h>
41#include <string.h>
42#include <ctype.h>
43#include <math.h>
44#include <float.h>
45
47// if the header file is in the "include" directory
48// #include "libmmg3d.h"
49// if the header file is in "include/mmg/mmg3d"
50#include "mmg/mmg3d/libmmg3d.h"
51
52int main(int argc,char *argv[]) {
53 MMG5_pMesh mmgMesh;
54 int ier,silent=0;
55 /* To save final mesh in a file */
56 FILE* inm;
57 char *fileout_generic,*fileout_medit,*filein;
58
59
60 fprintf(stdout," -- TEST LOAD AND GET MESH DATA \n");
61
62 if ( argc != 4 ) {
63 printf(" Usage: %s filein fileout silent_mode\n",argv[0]);
64 return(EXIT_FAILURE);
65 }
66
67 /* Name and path of the input mesh file */
68 filein = (char *) calloc(strlen(argv[1]) + 1, sizeof(char));
69 if ( filein == NULL ) {
70 perror(" ## Memory problem: calloc");
71 exit(EXIT_FAILURE);
72 }
73 strcpy(filein,argv[1]);
74
75 fileout_generic = (char *) calloc(strlen(argv[2])+1, sizeof(char));
76 if ( fileout_generic == NULL ) {
77 perror(" ## Memory problem: calloc");
78 exit(EXIT_FAILURE);
79 }
80 strcpy(fileout_generic,argv[2]);
81
82 fileout_medit = (char *) calloc(strlen(argv[2]) + 6, sizeof(char));
83 if ( fileout_medit == NULL ) {
84 perror(" ## Memory problem: calloc");
85 exit(EXIT_FAILURE);
86 }
87 strcpy(fileout_medit,argv[2]);
88 strcat(fileout_medit,".mesh");
89
90 silent = atoi(argv[3]);
91
94 /* args of InitMesh:
95 * MMG5_ARG_start: we start to give the args of a variadic func
96 * MMG5_ARG_ppMesh: next arg will be a pointer over a MMG5_pMesh
97 * &mmgMesh: pointer to your MMG5_pMesh (that stores your mesh)
98 * MMG5_ARG_ppMet: next arg will be a pointer over a MMG5_pSol storing a metric
99 * &mmgSol: pointer to your MMG5_pSol (that stores your metric) */
100 mmgMesh = NULL;
101
103 MMG5_ARG_ppMesh,&mmgMesh,
105
106
108 ier = MMG3D_loadGenericMesh(mmgMesh,NULL,NULL,filein);
109
110 if ( ier<1 ) {
111 if ( ier==0 ) {
112 fprintf(stderr," ** %s NOT FOUND.\n",filein);
113 fprintf(stderr," ** UNABLE TO OPEN INPUT FILE.\n");
114 return EXIT_FAILURE;
115 }
116 else {
117 assert ( ier == -1 );
118 fprintf(stderr," ** UNABLE TO READ INPUT FILE.\n");
119 return EXIT_FAILURE;
120 }
121 }
122
127 MMG5_int np, ne, nt, na;
128 if ( MMG3D_Get_meshSize(mmgMesh,&np,&ne,NULL,&nt,NULL,&na) !=1 ) {
129 fprintf(stderr," ERROR: Unable to get mesh size.\n");
130 return EXIT_FAILURE;
131 }
132
135 double *points=NULL; // point coordinates
136 MMG5_int *ref=NULL; // point references (==gmsh tags, ==colors)
137
138 points = (double *)malloc(3*np*sizeof(double));
139 assert(points);
140 ref = (MMG5_int *)malloc(np*sizeof(MMG5_int));
141 assert(ref);
142
143 ier = MMG3D_Get_vertices(mmgMesh,points, ref,NULL,NULL);
144 if ( !ier ) {
145 fprintf(stderr," ERROR: Unable to get mesh vertices.\n");
146 return EXIT_FAILURE;
147 }
148
151 MMG5_int *tetref=NULL, *tetra=NULL;
152
153 tetra = (MMG5_int *)malloc(4*ne*sizeof(MMG5_int));
154 assert(tetra);
155 tetref = (MMG5_int *)malloc(ne*sizeof(MMG5_int));
156 assert(tetref);
157
158 ier = MMG3D_Get_tetrahedra(mmgMesh,tetra, tetref,NULL);
159 if ( !ier ) {
160 fprintf(stderr," ERROR: Unable to get mesh tetra.\n");
161 return EXIT_FAILURE;
162 }
163
167 MMG5_int k;
168
169 if( !(inm = fopen(fileout_medit,"w")) ) {
170 fprintf(stderr," ** UNABLE TO OPEN OUTPUT MESH FILE.\n");
171 exit(EXIT_FAILURE);
172 }
173 /* Header */
174 fprintf(inm,"MeshVersionFormatted 2\n");
175 fprintf(inm,"\nDimension 3\n");
176
177 /* Mesh vertices */
178 fprintf(inm,"\nVertices\n%"MMG5_PRId"\n",np);
179 for(k=1; k<=np; k++) {
180 MMG5_int address = 3*(k-1);
181 fprintf(inm,"%.15lg %.15lg %.15lg %"MMG5_PRId" \n",
182 points[address],points[address+1],points[address+2],ref[k-1]);
183 }
184
185 /* Mesh tetra */
186 fprintf(inm,"\nTetrahedra\n%"MMG5_PRId"\n",ne);
187 for(k=1; k<=ne; k++) {
188 MMG5_int address = 4*(k-1);
189 fprintf(inm,"%"MMG5_PRId" %"MMG5_PRId" %"MMG5_PRId" %"MMG5_PRId" %"
190 MMG5_PRId" \n",tetra[address],tetra[address+1],
191 tetra[address+2],tetra[address+3],tetref[k-1]);
192 }
193
194 fprintf(inm,"\nEnd\n");
195 fclose(inm);
196
197 /* Memory release */
198 free(points); free(ref);
199 free(tetra); free(tetref);
200 free(fileout_medit);
201 fileout_medit = NULL;
202
204 for ( k=1; k<=ne; ++k ) {
205 MMG5_int adja[4];
206 ier = MMG3D_Get_adjaTet(mmgMesh,k,adja);
207 if ( !ier ) {
208 fprintf(stderr," ERROR: Unable to get adjacents of tetra %"MMG5_PRId".\n",k);
209 return EXIT_FAILURE;
210 }
211
212 if ( !silent ) {
213 fprintf(stdout,"Tetra %"MMG5_PRId" is adjacent to tetras %"MMG5_PRId" %"
214 MMG5_PRId" %"MMG5_PRId" %"MMG5_PRId"\n",
215 k,adja[0],adja[1],adja[2],adja[3]);
216 }
217 }
218
220 ier = MMG3D_saveGenericMesh(mmgMesh,NULL,fileout_generic);
221
222 if ( ier<1 ) {
223 if ( ier==0 ) {
224 fprintf(stderr," ** %s NOT FOUND.\n",fileout_generic);
225 fprintf(stderr," ** UNABLE TO OPEN INPUT FILE.\n");
226 return EXIT_FAILURE;
227 }
228 }
229 free(fileout_generic);
230 fileout_generic = NULL;
231
234 MMG5_ARG_ppMesh,&mmgMesh,
236
237 return(EXIT_SUCCESS);
238}
int MMG3D_Init_mesh(const int starter,...)
Initialize a mesh structure and optionally the associated solution and metric structures.
int MMG3D_Get_vertices(MMG5_pMesh mesh, double *vertices, MMG5_int *refs, int *areCorners, int *areRequired)
Get the coordinates and references of all vertices in the mesh.
int MMG3D_Get_tetrahedra(MMG5_pMesh mesh, MMG5_int *tetra, MMG5_int *refs, int *areRequired)
Get the vertices and reference of all tetrahedra in the mesh.
int MMG3D_Get_meshSize(MMG5_pMesh mesh, MMG5_int *np, MMG5_int *ne, MMG5_int *nprism, MMG5_int *nt, MMG5_int *nquad, MMG5_int *na)
Get the number of vertices, tetrahedra, prisms, triangles, quadrilaterals and edges of the mesh.
int MMG3D_Free_all(const int starter,...)
Deallocations before return.
int ier
program main
Example for using mmglib (basic use)
Definition: main.F90:6
int MMG3D_saveGenericMesh(MMG5_pMesh mesh, MMG5_pSol sol, const char *filename)
Save mesh data in a file whose format depends on the filename extension.
Definition: inout_3d.c:2062
int MMG3D_loadGenericMesh(MMG5_pMesh mesh, MMG5_pSol met, MMG5_pSol sol, const char *filename)
Read mesh data in a format determined by the filename extension.
Definition: inout_3d.c:1190
LIBMMG3D_EXPORT int MMG3D_Get_adjaTet(MMG5_pMesh mesh, MMG5_int kel, MMG5_int listet[4])
Return adjacent elements of a tetrahedron.
#define MMG5_ARG_ppMesh
Definition: libmmgtypes.h:102
#define MMG5_ARG_end
Definition: libmmgtypes.h:179
#define MMG5_ARG_start
Definition: libmmgtypes.h:93
MMG mesh structure.
Definition: libmmgtypes.h:613