File: transmesh.c

package info (click to toggle)
libmeshb 7.80-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,848 kB
  • sloc: ansic: 12,810; f90: 1,146; fortran: 406; makefile: 218
file content (300 lines) | stat: -rw-r--r-- 9,325 bytes parent folder | download
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300


/*----------------------------------------------------------------------------*/
/*                                                                            */
/*                               TRANSMESH V 5.2                              */
/*                                                                            */
/*----------------------------------------------------------------------------*/
/*                                                                            */
/* Description:         convert mesh file from/to ascii/bin                   */
/* Author:              Loic MARECHAL                                         */
/* Creation date:       mar 08 2004                                           */
/* Last modification:   aug 05 2021                                           */
/*                                                                            */
/*----------------------------------------------------------------------------*/


/*----------------------------------------------------------------------------*/
/* Defines                                                                    */
/*----------------------------------------------------------------------------*/

#define TRANSMESH 1
#define StrSiz 1024
#define DefMaxRef 1024
#define MaxGrp 1024
#define MaxTok 1024

#ifdef PRINTF_INT64_MODIFIER
#define INT64_T_FMT "%" PRINTF_INT64_MODIFIER "d"
#else
#   ifdef GMF_WINDOWS
#    define INT64_T_FMT "%Id"
#   else
#    include <inttypes.h>
#    define INT64_T_FMT "%" PRId64
#   endif
#endif

/*----------------------------------------------------------------------------*/
/* Includes                                                                   */
/*----------------------------------------------------------------------------*/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "libmeshb7.h"


/*----------------------------------------------------------------------------*/
/* Prototypes of structures                                                   */
/*----------------------------------------------------------------------------*/

typedef struct
{
   int idx, NmbRef, *RefTab, typ, sgn;
   char *NfoStr;
}GrpSct;


/*----------------------------------------------------------------------------*/
/* Return the kwd's integer from its string                                   */
/*----------------------------------------------------------------------------*/

int GetKwdIdx(char *KwdStr)
{
   int KwdIdx;

   for(KwdIdx=1; KwdIdx<=GmfMaxKwd; KwdIdx++)
      if(!strcmp(GmfKwdFmt[ KwdIdx ][0], KwdStr))
         return(KwdIdx);

   return(0);
}


/*----------------------------------------------------------------------------*/
/* Check args and copy each field from in-file to out-file                    */
/*----------------------------------------------------------------------------*/

int main(int ArgCnt, char **ArgVec)
{
   int      NmbTyp, SolSiz, TypTab[ GmfMaxTyp ], FilVer = 0, InpVer, OutVer = 1;
   int      i, j, dim, MaxRef, ArgIdx = 1, NmbGrp = 0, pos, deg, NmbNod;
   int64_t  NmbLin, InpIdx, OutIdx;
   char     *InpNam, *OutNam, *PtrArg, *FlgTab;
   GrpSct   GrpTab[ MaxGrp ], *grp;
#ifndef WIN32
   float    f;
   double   d;
   int      NmbTok, NmbTok2, TokTab[ MaxTok ], TokTab2[ MaxTok ];
   char     *MshNam[ StrSiz ], *TmpStr, *TmpStr2, *TmpStr3;
#endif

   if(ArgCnt == 1)
   {
      puts("\nTRANSMESH v5.2, july 19 2017, Loic MARECHAL / INRIA\n");
      puts(" Usage    : transmesh source_name destination_name (-options)\n");
      puts(" optional : -v output_file_version");
      puts(" version 1: 32 bits integers, 32 bits reals, file size < 2 GigaBytes");
      puts(" version 2: 32 bits integers, 64 bits reals, file size < 2 GigaBytes");
      puts(" version 3: 32 bits integers, 64 bits reals, file size < 8 ExaBytes");
      puts(" version 4: 64 bits integers, 64 bits reals, file size < 8 ExaBytes\n");
#ifndef WIN32
      puts(" optional : -igroup inflow 1 Triangles 3,5,6,9-14");
      puts("            Defines a group named \"inflow\" with index 1 that includes");
      puts("            a set of comma-seprated triangle references.");
      puts("            You may also specify ranges between two dash-separated indices.\n");
      puts(" optional : -egroup wings 3 Quadrilaterals 6-20");
      puts("            Defines a group named \"wings\" with index 3 that excludes");
      puts("            a set of comma-seprated quadrilateral references.");
      puts("            You may also specify ranges between two dash-separated indices.\n");
#endif
      exit(0);
   }

   InpNam = ArgVec[ ArgIdx++ ];
   OutNam = ArgVec[ ArgIdx++ ];

   if(!strcmp(InpNam, OutNam))
   {
      puts("The output mesh cannot overwrite the input mesh.");
      exit(1);
   }

   memset(GmfMaxRefTab, 0, (GmfMaxKwd + 1) * sizeof(int));

   while(ArgIdx < ArgCnt)
   {
      PtrArg = ArgVec[ ArgIdx++ ];

      if(!strcmp(PtrArg, "-v"))
      {
         FilVer = atoi(ArgVec[ ArgIdx++ ]);

         if( (FilVer < 1) || (FilVer > 4) )
         {
            printf("Wrong size type : %d\n", FilVer);
            exit(1);
         }
      }
#ifndef WIN32
      if(!strcmp(PtrArg, "-igroup") || !strcmp(PtrArg, "-egroup"))
      {
         grp = &GrpTab[ NmbGrp++ ];
         grp->NfoStr = ArgVec[ ArgIdx++ ];
         grp->idx = atoi(ArgVec[ ArgIdx++ ]);
         grp->typ = GetKwdIdx(ArgVec[ ArgIdx++ ]);
         grp->NmbRef = 0;
         NmbTok = 0;
         TmpStr = ArgVec[ ArgIdx++ ];

         while( (NmbTok < MaxTok) && ((TmpStr2 = strsep(&TmpStr, ","))) )
         {
            NmbTok2 = 0;

            while( (NmbTok2 < 2) && ((TmpStr3 = strsep(&TmpStr2, "-"))) )
               TokTab2[ NmbTok2++ ] = atoi(TmpStr3);

            if(NmbTok2 == 1)
               TokTab[ NmbTok++ ] = TokTab2[0];
            else if(NmbTok2 == 2)
               for(i=TokTab2[0]; i<=TokTab2[1]; i++)
                  TokTab[ NmbTok++ ] = i;
            else
               exit(1);
         }

         grp->NmbRef = NmbTok;
         grp->RefTab = malloc(grp->NmbRef * sizeof(int));

         for(i=0;i<grp->NmbRef;i++)
            grp->RefTab[i] = TokTab[i];

         if(!strcmp(PtrArg, "-igroup"))
            grp->sgn = 1;
         else
            grp->sgn = -1;
      }
#endif
   }

   if(!(InpIdx = GmfOpenMesh(InpNam, GmfRead, &InpVer, &dim)))
   {
      fprintf(stderr,"Source of error : TRANSMESH / OPEN_MESH\n");
      fprintf(stderr,"Cannot open %s\n", InpNam);
      return(1);
   }

   if(FilVer)
      OutVer = FilVer;
   else
      OutVer = InpVer;

   if(!(OutIdx = GmfOpenMesh(OutNam, GmfWrite, OutVer, dim)))
   {
      fprintf(stderr,"Source of error : TRANSMESH / OPEN_MESH\n");
      fprintf(stderr,"Cannot open %s\n", OutNam);
      return(1);
   }

   for(i=0;i<=GmfMaxKwd;i++)
   {
      if(!strcmp(GmfKwdFmt[i][0], "Reserved") || !strcmp(GmfKwdFmt[i][0], "End"))
         continue;

      if(!GmfGotoKwd(InpIdx, i))
         continue;

      if(strcmp("i", GmfKwdFmt[i][1]))
      {
         if((NmbLin = GmfStatKwd(InpIdx, i)))
            GmfSetKwd(OutIdx, i, 0);
         else
            continue;
      }
      else if(strcmp("sr", GmfKwdFmt[i][2]) && strcmp("hr", GmfKwdFmt[i][2]))
      {
         if((NmbLin = GmfStatKwd(InpIdx, i)))
            GmfSetKwd(OutIdx, i, NmbLin);
         else
            continue;
      }
      else
      {
         if(!strcmp("sr", GmfKwdFmt[i][2]))
         {
            if((NmbLin = GmfStatKwd(InpIdx, i, &NmbTyp, &SolSiz, TypTab)))
               GmfSetKwd(OutIdx, i,  NmbLin, NmbTyp, TypTab);
            else
               continue;
         }
         else if(!strcmp("hr", GmfKwdFmt[i][2]))
         {
            if((NmbLin = GmfStatKwd(InpIdx, i, &NmbTyp, &SolSiz, TypTab, &deg, &NmbNod)))
               GmfSetKwd(OutIdx, i,  NmbLin, NmbTyp, TypTab, deg, NmbNod);
            else
               continue;
         }
      }

      printf("Parsing %s : "INT64_T_FMT" item\n", GmfKwdFmt[i][0], NmbLin);

      for(j=1;j<=NmbLin;j++)
         GmfCpyLin(InpIdx, OutIdx, i);
   }

   if(NmbGrp)
   {
      GmfSetKwd(OutIdx, GmfIRefGroups, NmbGrp);
      pos = 1;

      for(i=0;i<NmbGrp;i++)
      {
         grp = &GrpTab[i];

         if(grp->sgn == -1)
         {
            MaxRef = GmfMaxRefTab[ grp->typ ] ? GmfMaxRefTab[ grp->typ ] : DefMaxRef;
            FlgTab = malloc(MaxRef+1);

            for(i=1;i<=MaxRef;i++)
               FlgTab[i] = 1;

            for(i=0;i<grp->NmbRef;i++)
               FlgTab[ grp->RefTab[i] ] = 0;

            free(grp->RefTab);
            grp->NmbRef = MaxRef - grp->NmbRef;
            grp->RefTab = malloc(grp->NmbRef * sizeof(int));
            pos = 0;

            for(i=1;i<=MaxRef;i++)
               if(FlgTab[i])
                  grp->RefTab[ pos++ ] = i;

            free(FlgTab);
         }

         GmfSetLin(OutIdx, GmfIRefGroups, grp->NfoStr, grp->idx, grp->typ, grp->NmbRef, pos);
         pos += grp->NmbRef;
      }

      GmfSetKwd(OutIdx, GmfDRefGroups, pos - 1);

      for(i=0;i<NmbGrp;i++)
      {
         grp = &GrpTab[i];

         for(j=0;j<grp->NmbRef;j++)
            GmfSetLin(OutIdx, GmfDRefGroups, grp->typ, grp->RefTab[j], grp->idx);
      }
   }

   if(!GmfCloseMesh(InpIdx))
      return(1);

   if(!GmfCloseMesh(OutIdx))
      return(1);

   return(0);
}