Salome HOME
Copyright update: 2016
[modules/paravis.git] / src / Plugins / MEDReader / IO / Testing / Cxx / TestMedReadDescendingPolyhedron.cxx
1 // Copyright (C) 2010-2016  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 /*
21  * TestMedReadDescendingPolyhedron.cxx
22  *
23  *  Created on: 3 mars 2011
24  *      Author: alejandro
25  */
26
27 #include <med.h>
28 #define MESGERR 1
29 #include <med_utils.h>
30
31 #include <string.h>
32
33 int main (int argc, char **argv) {
34   med_idt fid;
35   const char meshname[MED_NAME_SIZE+1] = "3D unstructured mesh";
36   char meshdescription[MED_COMMENT_SIZE+1];
37   med_int meshdim;
38   med_int spacedim;
39   med_sorting_type sortingtype;
40   med_int nstep;
41   med_mesh_type meshtype;
42   med_axis_type axistype;
43   char axisname[3*MED_SNAME_SIZE+1];
44   char unitname[3*MED_SNAME_SIZE+1];
45   char dtunit[MED_SNAME_SIZE+1];
46   med_float *coordinates = NULL;
47   med_int nnodes = 0;
48   med_int npoly = 0;
49   med_int indexsize;
50   med_int faceIndexSize;
51   med_int *index = NULL;
52   med_int *faceindex = NULL;
53   med_int *connectivity = NULL;
54   med_int connectivitysize;
55   med_int *triaconnectivity = NULL;
56   med_int ntria3 = 0;
57   med_bool coordinatechangement;
58   med_bool geotransformation;
59   int i;
60   int k,ind1,ind2;
61   int j,jind1,jind2;
62
63   /* open MED file with READ ONLY access mode */
64   fid = MEDfileOpen("./UsesCase_MEDmesh_17.med",MED_ACC_RDONLY);
65   if (fid < 0) {
66     MESSAGE("ERROR : open file in READ ONLY ACCESS mode ...");
67     return -1;
68   }
69
70   /*
71    * ... we know that the MED file has only one mesh,
72    * a real code working would check ...
73    */
74
75   /* read mesh informations : mesh dimension, space dimension ... */
76   if (MEDmeshInfoByName(fid, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
77       dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
78     MESSAGE("ERROR : mesh info ...");
79     return -1;
80   }
81
82   /* read how many nodes in the mesh */
83   if ((nnodes = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_POINT1,
84              MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
85              &geotransformation)) < 0) {
86     MESSAGE("ERROR : number of nodes ...");
87     return -1;
88   }
89
90   /* read how many triangular cells in the mesh */
91   if ((ntria3 = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_DESCENDING_FACE,MED_TRIA3,
92              MED_CONNECTIVITY, MED_NODAL,&coordinatechangement,
93              &geotransformation)) < 0) {
94     MESSAGE("ERROR : number of MED_TRIA3 ...");
95     return -1;
96   }
97   ISCRUTE(ntria3);
98
99   /* read cells connectivity in the mesh */
100   if ((triaconnectivity = (med_int *) malloc(sizeof(med_int)*ntria3*3)) == NULL) {
101     MESSAGE("ERROR : memory allocation ...");
102     return -1;
103   }
104   if (MEDmeshElementConnectivityRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_DESCENDING_FACE,
105            MED_TRIA3, MED_NODAL, MED_FULL_INTERLACE, triaconnectivity) < 0) {
106     MESSAGE("ERROR : MED_TRIA3 connectivity ...");
107     return -1;
108   }
109   for (i=0;i<ntria3*3;i++)
110     printf("%d - ",*(triaconnectivity+i));
111   printf("\n");
112   /*
113    * ... we know that we only have MED_POLYHEDRON cells in the mesh,
114    * a real code working would check all MED geometry cell types ...
115    */
116
117   /* How many polygon in the mesh in nodal connectivity mode */
118   /* For the polygons, we get the size of array index */
119   if ((faceIndexSize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
120           MED_CELL,MED_POLYHEDRON,MED_INDEX_FACE,MED_DESCENDING,
121           &coordinatechangement,
122           &geotransformation)) < 0) {
123     MESSAGE("ERROR : read number of polyedron ...");
124     return -1;
125   }
126   npoly = faceIndexSize - 1;
127   ISCRUTE(npoly);
128
129   if ((indexsize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
130           MED_CELL,MED_POLYHEDRON,MED_INDEX_NODE,MED_DESCENDING,
131           &coordinatechangement,
132           &geotransformation)) < 0) {
133     MESSAGE("ERROR : read number of polyedron ...");
134     return -1;
135   }
136   ISCRUTE(indexsize);
137
138   /* how many nodes for the polyhedron connectivity ? */
139   if ((connectivitysize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
140            MED_CELL,MED_POLYHEDRON,MED_CONNECTIVITY,MED_DESCENDING,
141            &coordinatechangement,
142            &geotransformation)) < 0) {
143     MESSAGE("ERROR : read connevity size ...");
144     return -1;
145     }
146   ISCRUTE(connectivitysize);
147
148   /* read mesh nodes coordinates */
149   if ((coordinates = (med_float*) malloc(sizeof(med_float)*nnodes*spacedim)) == NULL) {
150     MESSAGE("ERROR : memory allocation ...");
151     return -1;
152   }
153
154   if (MEDmeshNodeCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
155             coordinates) < 0) {
156     MESSAGE("ERROR : nodes coordinates ...");
157     return -1;
158   }
159   for (i=0;i<nnodes*spacedim;i++)
160     printf("%f - ",*(coordinates+i));
161   printf("\n");
162
163   /* read polygons connectivity */
164   if ((index = (med_int *) malloc(sizeof(med_int)*indexsize)) == NULL) {
165      MESSAGE("ERROR : memory allocation ...");
166      return -1;
167    }
168
169   if ((faceindex = (med_int *) malloc(sizeof(med_int)*faceIndexSize)) == NULL) {
170      MESSAGE("ERROR : memory allocation ...");
171      return -1;
172    }
173
174   if ((connectivity = (med_int *) malloc(sizeof(med_int)*connectivitysize)) == NULL) {
175     MESSAGE("ERROR : memory allocation ...");
176     return -1;
177   }
178
179   if (MEDmeshPolyhedronRd(fid,meshname,MED_NO_DT,MED_NO_IT,MED_CELL,MED_DESCENDING,
180         faceindex, index, connectivity) < 0) {
181     MESSAGE("ERROR : read polygon connectivity ...");
182     return -1;
183   }
184
185   for (i=0;i<npoly;i++)
186     {
187     printf(">> MED_POLYHEDRON "IFORMAT" : \n",i+1);
188     printf("---- Face Index         ----- : [ ");
189     ind1 = *(index+i)-1;
190     ind2 = *(index+i+1)-1;
191     for (k=ind1;k<ind2;k++)
192       printf(IFORMAT" ",*(faceindex+k));
193     printf(" ] \n");
194     printf("---- Connectivity       ----- : [ ");
195     for (k=0;k<connectivitysize;k++)
196       {
197       for (j=0;j<3;j++)
198         {
199         printf(IFORMAT" ",triaconnectivity[connectivity[k]+j]);
200         }
201       printf("\n");
202       }
203     printf(" ] \n");
204     }
205
206   /*
207    * ... we know that the family number of nodes and elements is 0, a real working would check ...
208    */
209
210   /* close MED file */
211   if (MEDfileClose(fid) < 0) {
212     MESSAGE("ERROR : close file");
213     return -1;
214   }
215
216   /* memory deallocation */
217   printf("Before free(coordinates)\n");
218   if (coordinates)
219     free(coordinates);
220
221   printf("Before free(connectivity)\n");
222   if (connectivity)
223     free(connectivity);
224
225   printf("Before free(triaconnectivity)\n");
226   if (triaconnectivity)
227     free(triaconnectivity);
228
229   printf("Before free(index)\n");
230   if (index)
231     free(index);
232
233   return 0;
234 }