Back to index

salome-paravis  6.5.0
TestMedReadPolyhedron.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2010-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 /*
00021  *  How to create an unstructured mesh with polygons
00022  *
00023  *  Use case 16 : read a 2D unstructured mesh with 2 polyhedrons
00024  */
00025 
00026 #include <med.h>
00027 #define MESGERR 1
00028 #include <med_utils.h>
00029 
00030 #include <string.h>
00031 
00032 int main (int argc, char **argv) {
00033   med_idt fid;
00034   const char meshname[MED_NAME_SIZE+1] = "3D unstructured mesh";
00035   char meshdescription[MED_COMMENT_SIZE+1];
00036   med_int meshdim;
00037   med_int spacedim;
00038   med_sorting_type sortingtype;
00039   med_int nstep;
00040   med_mesh_type meshtype;
00041   med_axis_type axistype;
00042   char axisname[3*MED_SNAME_SIZE+1];
00043   char unitname[3*MED_SNAME_SIZE+1];
00044   char dtunit[MED_SNAME_SIZE+1];
00045   med_float *coordinates = NULL;
00046   med_int nnodes = 0;
00047   med_int npoly = 0;
00048   med_int indexsize;
00049   med_int faceIndexSize;
00050   med_int *index = NULL;
00051   med_int *faceindex = NULL;
00052   med_int *connectivity = NULL;
00053   med_int connectivitysize;
00054   med_bool coordinatechangement;
00055   med_bool geotransformation;
00056   int i;
00057   int k,ind1,ind2;
00058   int j, jind1,jind2;
00059 
00060   /* open MED file with READ ONLY access mode */
00061   fid = MEDfileOpen("./UsesCase_MEDmesh_15.med",MED_ACC_RDONLY);
00062   if (fid < 0) {
00063     MESSAGE("ERROR : open file in READ ONLY ACCESS mode ...");
00064     return -1;
00065   }
00066 
00067   /*
00068    * ... we know that the MED file has only one mesh,
00069    * a real code working would check ...
00070    */
00071 
00072   /* read mesh informations : mesh dimension, space dimension ... */
00073   if (MEDmeshInfoByName(fid, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
00074       dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
00075     MESSAGE("ERROR : mesh info ...");
00076     return -1;
00077   }
00078 
00079   /* read how many nodes in the mesh */
00080   if ((nnodes = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_POINT1,
00081              MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
00082              &geotransformation)) < 0) {
00083     MESSAGE("ERROR : number of nodes ...");
00084     return -1;
00085   }
00086 
00087   /*
00088    * ... we know that we only have MED_POLYHEDRON cells in the mesh,
00089    * a real code working would check all MED geometry cell types ...
00090    */
00091 
00092   /* How many polygon in the mesh in nodal connectivity mode */
00093   /* For the polygons, we get the size of array index */
00094   if ((indexsize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
00095           MED_CELL,MED_POLYHEDRON,MED_INDEX_FACE,MED_NODAL,
00096           &coordinatechangement,
00097           &geotransformation)) < 0) {
00098     MESSAGE("ERROR : read number of polyedron ...");
00099     return -1;
00100   }
00101   npoly = indexsize-1;
00102   ISCRUTE(npoly);
00103 
00104   if ((faceIndexSize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
00105           MED_CELL,MED_POLYHEDRON,MED_INDEX_NODE,MED_NODAL,
00106           &coordinatechangement,
00107           &geotransformation)) < 0) {
00108     MESSAGE("ERROR : read number of polyedron ...");
00109     return -1;
00110   }
00111   ISCRUTE(faceIndexSize);
00112 
00113   /* how many nodes for the polyhedron connectivity ? */
00114   if ((connectivitysize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
00115            MED_CELL,MED_POLYHEDRON,MED_CONNECTIVITY,MED_NODAL,
00116            &coordinatechangement,
00117            &geotransformation)) < 0) {
00118     MESSAGE("ERROR : read connevity size ...");
00119     return -1;
00120     }
00121   ISCRUTE(connectivitysize);
00122 
00123   /* read mesh nodes coordinates */
00124   if ((coordinates = (med_float*) malloc(sizeof(med_float)*nnodes*spacedim)) == NULL) {
00125     MESSAGE("ERROR : memory allocation ...");
00126     return -1;
00127   }
00128 
00129   if (MEDmeshNodeCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
00130             coordinates) < 0) {
00131     MESSAGE("ERROR : nodes coordinates ...");
00132     return -1;
00133   }
00134   for (i=0;i<nnodes*spacedim;i++)
00135     printf("%f - ",*(coordinates+i));
00136   printf("\n");
00137 
00138   /* read polygons connectivity */
00139   index = (med_int *) malloc(sizeof(med_int)*indexsize);
00140   faceindex = (med_int *) malloc(sizeof(med_int)*faceIndexSize);
00141   connectivity = (med_int *) malloc(sizeof(med_int)*connectivitysize);
00142 
00143   if (MEDmeshPolyhedronRd(fid,meshname,MED_NO_DT,MED_NO_IT,MED_CELL,MED_NODAL,
00144         index,faceindex,connectivity) < 0) {
00145     MESSAGE("ERROR : read polygon connectivity ...");
00146     return -1;
00147   }
00148 
00149   for (i=0;i<npoly;i++)
00150     {
00151     printf(">> MED_POLYHEDRON "IFORMAT" : \n",i+1);
00152     printf("---- Face Index         ----- : [ ");
00153     ind1 = *(index+i)-1;
00154     ind2 = *(index+i+1)-1;
00155     for (k=ind1;k<ind2;k++)
00156       printf(IFORMAT" ",*(faceindex+k));
00157     printf(" ] \n");
00158     printf("---- Connectivity       ----- : [ ");
00159     for (k=ind1;k<ind2;k++)
00160       {
00161       jind1 = *(faceindex+k)-1;
00162       jind2 = *(faceindex+k+1)-1;
00163       for (j=jind1;j<jind2;j++)
00164         printf(IFORMAT" ",*(connectivity+j));
00165       printf(" \n");
00166       }
00167     printf(" ] \n");
00168     }
00169 
00170   /*
00171    * ... we know that the family number of nodes and elements is 0, a real working would check ...
00172    */
00173 
00174   /* close MED file */
00175   if (MEDfileClose(fid) < 0) {
00176     MESSAGE("ERROR : close file");
00177     return -1;
00178   }
00179 
00180   /* memory deallocation */
00181   if (coordinates)
00182     free(coordinates);
00183 
00184   if (index)
00185     free(index);
00186 
00187   if (connectivity)
00188     free(connectivity);
00189 
00190   return 0;
00191 }