Salome HOME
Update copyrights
[modules/paravis.git] / src / Plugins / MEDReader / IO / Testing / Cxx / TestMedReadPolyhedron.cxx
1 // Copyright (C) 2010-2019  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  *  How to create an unstructured mesh with polygons
22  *
23  *  Use case 16 : read a 2D unstructured mesh with 2 polyhedrons
24  */
25
26 #include <med.h>
27 #define MESGERR 1
28 #include <med_utils.h>
29
30 #include <string.h>
31
32 int main (int argc, char **argv) {
33   med_idt fid;
34   const char meshname[MED_NAME_SIZE+1] = "3D unstructured mesh";
35   char meshdescription[MED_COMMENT_SIZE+1];
36   med_int meshdim;
37   med_int spacedim;
38   med_sorting_type sortingtype;
39   med_int nstep;
40   med_mesh_type meshtype;
41   med_axis_type axistype;
42   char axisname[3*MED_SNAME_SIZE+1];
43   char unitname[3*MED_SNAME_SIZE+1];
44   char dtunit[MED_SNAME_SIZE+1];
45   med_float *coordinates = NULL;
46   med_int nnodes = 0;
47   med_int npoly = 0;
48   med_int indexsize;
49   med_int faceIndexSize;
50   med_int *index = NULL;
51   med_int *faceindex = NULL;
52   med_int *connectivity = NULL;
53   med_int connectivitysize;
54   med_bool coordinatechangement;
55   med_bool geotransformation;
56   int i;
57   int k,ind1,ind2;
58   int j, jind1,jind2;
59
60   /* open MED file with READ ONLY access mode */
61   fid = MEDfileOpen("./UsesCase_MEDmesh_15.med",MED_ACC_RDONLY);
62   if (fid < 0) {
63     MESSAGE("ERROR : open file in READ ONLY ACCESS mode ...");
64     return -1;
65   }
66
67   /*
68    * ... we know that the MED file has only one mesh,
69    * a real code working would check ...
70    */
71
72   /* read mesh informations : mesh dimension, space dimension ... */
73   if (MEDmeshInfoByName(fid, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
74       dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
75     MESSAGE("ERROR : mesh info ...");
76     return -1;
77   }
78
79   /* read how many nodes in the mesh */
80   if ((nnodes = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_POINT1,
81              MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
82              &geotransformation)) < 0) {
83     MESSAGE("ERROR : number of nodes ...");
84     return -1;
85   }
86
87   /*
88    * ... we know that we only have MED_POLYHEDRON cells in the mesh,
89    * a real code working would check all MED geometry cell types ...
90    */
91
92   /* How many polygon in the mesh in nodal connectivity mode */
93   /* For the polygons, we get the size of array index */
94   if ((indexsize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
95           MED_CELL,MED_POLYHEDRON,MED_INDEX_FACE,MED_NODAL,
96           &coordinatechangement,
97           &geotransformation)) < 0) {
98     MESSAGE("ERROR : read number of polyedron ...");
99     return -1;
100   }
101   npoly = indexsize-1;
102   ISCRUTE(npoly);
103
104   if ((faceIndexSize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
105           MED_CELL,MED_POLYHEDRON,MED_INDEX_NODE,MED_NODAL,
106           &coordinatechangement,
107           &geotransformation)) < 0) {
108     MESSAGE("ERROR : read number of polyedron ...");
109     return -1;
110   }
111   ISCRUTE(faceIndexSize);
112
113   /* how many nodes for the polyhedron connectivity ? */
114   if ((connectivitysize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
115            MED_CELL,MED_POLYHEDRON,MED_CONNECTIVITY,MED_NODAL,
116            &coordinatechangement,
117            &geotransformation)) < 0) {
118     MESSAGE("ERROR : read connevity size ...");
119     return -1;
120     }
121   ISCRUTE(connectivitysize);
122
123   /* read mesh nodes coordinates */
124   if ((coordinates = (med_float*) malloc(sizeof(med_float)*nnodes*spacedim)) == NULL) {
125     MESSAGE("ERROR : memory allocation ...");
126     return -1;
127   }
128
129   if (MEDmeshNodeCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
130             coordinates) < 0) {
131     MESSAGE("ERROR : nodes coordinates ...");
132     return -1;
133   }
134   for (i=0;i<nnodes*spacedim;i++)
135     printf("%f - ",*(coordinates+i));
136   printf("\n");
137
138   /* read polygons connectivity */
139   index = (med_int *) malloc(sizeof(med_int)*indexsize);
140   faceindex = (med_int *) malloc(sizeof(med_int)*faceIndexSize);
141   connectivity = (med_int *) malloc(sizeof(med_int)*connectivitysize);
142
143   if (MEDmeshPolyhedronRd(fid,meshname,MED_NO_DT,MED_NO_IT,MED_CELL,MED_NODAL,
144         index,faceindex,connectivity) < 0) {
145     MESSAGE("ERROR : read polygon connectivity ...");
146     return -1;
147   }
148
149   for (i=0;i<npoly;i++)
150     {
151     printf(">> MED_POLYHEDRON "IFORMAT" : \n",i+1);
152     printf("---- Face Index         ----- : [ ");
153     ind1 = *(index+i)-1;
154     ind2 = *(index+i+1)-1;
155     for (k=ind1;k<ind2;k++)
156       printf(IFORMAT" ",*(faceindex+k));
157     printf(" ] \n");
158     printf("---- Connectivity       ----- : [ ");
159     for (k=ind1;k<ind2;k++)
160       {
161       jind1 = *(faceindex+k)-1;
162       jind2 = *(faceindex+k+1)-1;
163       for (j=jind1;j<jind2;j++)
164         printf(IFORMAT" ",*(connectivity+j));
165       printf(" \n");
166       }
167     printf(" ] \n");
168     }
169
170   /*
171    * ... we know that the family number of nodes and elements is 0, a real working would check ...
172    */
173
174   /* close MED file */
175   if (MEDfileClose(fid) < 0) {
176     MESSAGE("ERROR : close file");
177     return -1;
178   }
179
180   /* memory deallocation */
181   if (coordinates)
182     free(coordinates);
183
184   if (index)
185     free(index);
186
187   if (connectivity)
188     free(connectivity);
189
190   return 0;
191 }