Back to index

salome-paravis  6.5.0
TestMedReadDescendingPolyhedron.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  * TestMedReadDescendingPolyhedron.cxx
00022  *
00023  *  Created on: 3 mars 2011
00024  *      Author: alejandro
00025  */
00026 
00027 #include <med.h>
00028 #define MESGERR 1
00029 #include <med_utils.h>
00030 
00031 #include <string.h>
00032 
00033 int main (int argc, char **argv) {
00034   med_idt fid;
00035   const char meshname[MED_NAME_SIZE+1] = "3D unstructured mesh";
00036   char meshdescription[MED_COMMENT_SIZE+1];
00037   med_int meshdim;
00038   med_int spacedim;
00039   med_sorting_type sortingtype;
00040   med_int nstep;
00041   med_mesh_type meshtype;
00042   med_axis_type axistype;
00043   char axisname[3*MED_SNAME_SIZE+1];
00044   char unitname[3*MED_SNAME_SIZE+1];
00045   char dtunit[MED_SNAME_SIZE+1];
00046   med_float *coordinates = NULL;
00047   med_int nnodes = 0;
00048   med_int npoly = 0;
00049   med_int indexsize;
00050   med_int faceIndexSize;
00051   med_int *index = NULL;
00052   med_int *faceindex = NULL;
00053   med_int *connectivity = NULL;
00054   med_int connectivitysize;
00055   med_int *triaconnectivity = NULL;
00056   med_int ntria3 = 0;
00057   med_bool coordinatechangement;
00058   med_bool geotransformation;
00059   int i;
00060   int k,ind1,ind2;
00061   int j,jind1,jind2;
00062 
00063   /* open MED file with READ ONLY access mode */
00064   fid = MEDfileOpen("./UsesCase_MEDmesh_17.med",MED_ACC_RDONLY);
00065   if (fid < 0) {
00066     MESSAGE("ERROR : open file in READ ONLY ACCESS mode ...");
00067     return -1;
00068   }
00069 
00070   /*
00071    * ... we know that the MED file has only one mesh,
00072    * a real code working would check ...
00073    */
00074 
00075   /* read mesh informations : mesh dimension, space dimension ... */
00076   if (MEDmeshInfoByName(fid, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
00077       dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
00078     MESSAGE("ERROR : mesh info ...");
00079     return -1;
00080   }
00081 
00082   /* read how many nodes in the mesh */
00083   if ((nnodes = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_POINT1,
00084              MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
00085              &geotransformation)) < 0) {
00086     MESSAGE("ERROR : number of nodes ...");
00087     return -1;
00088   }
00089 
00090   /* read how many triangular cells in the mesh */
00091   if ((ntria3 = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_DESCENDING_FACE,MED_TRIA3,
00092              MED_CONNECTIVITY, MED_NODAL,&coordinatechangement,
00093              &geotransformation)) < 0) {
00094     MESSAGE("ERROR : number of MED_TRIA3 ...");
00095     return -1;
00096   }
00097   ISCRUTE(ntria3);
00098 
00099   /* read cells connectivity in the mesh */
00100   if ((triaconnectivity = (med_int *) malloc(sizeof(med_int)*ntria3*3)) == NULL) {
00101     MESSAGE("ERROR : memory allocation ...");
00102     return -1;
00103   }
00104   if (MEDmeshElementConnectivityRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_DESCENDING_FACE,
00105            MED_TRIA3, MED_NODAL, MED_FULL_INTERLACE, triaconnectivity) < 0) {
00106     MESSAGE("ERROR : MED_TRIA3 connectivity ...");
00107     return -1;
00108   }
00109   for (i=0;i<ntria3*3;i++)
00110     printf("%d - ",*(triaconnectivity+i));
00111   printf("\n");
00112   /*
00113    * ... we know that we only have MED_POLYHEDRON cells in the mesh,
00114    * a real code working would check all MED geometry cell types ...
00115    */
00116 
00117   /* How many polygon in the mesh in nodal connectivity mode */
00118   /* For the polygons, we get the size of array index */
00119   if ((faceIndexSize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
00120           MED_CELL,MED_POLYHEDRON,MED_INDEX_FACE,MED_DESCENDING,
00121           &coordinatechangement,
00122           &geotransformation)) < 0) {
00123     MESSAGE("ERROR : read number of polyedron ...");
00124     return -1;
00125   }
00126   npoly = faceIndexSize - 1;
00127   ISCRUTE(npoly);
00128 
00129   if ((indexsize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
00130           MED_CELL,MED_POLYHEDRON,MED_INDEX_NODE,MED_DESCENDING,
00131           &coordinatechangement,
00132           &geotransformation)) < 0) {
00133     MESSAGE("ERROR : read number of polyedron ...");
00134     return -1;
00135   }
00136   ISCRUTE(indexsize);
00137 
00138   /* how many nodes for the polyhedron connectivity ? */
00139   if ((connectivitysize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
00140            MED_CELL,MED_POLYHEDRON,MED_CONNECTIVITY,MED_DESCENDING,
00141            &coordinatechangement,
00142            &geotransformation)) < 0) {
00143     MESSAGE("ERROR : read connevity size ...");
00144     return -1;
00145     }
00146   ISCRUTE(connectivitysize);
00147 
00148   /* read mesh nodes coordinates */
00149   if ((coordinates = (med_float*) malloc(sizeof(med_float)*nnodes*spacedim)) == NULL) {
00150     MESSAGE("ERROR : memory allocation ...");
00151     return -1;
00152   }
00153 
00154   if (MEDmeshNodeCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
00155             coordinates) < 0) {
00156     MESSAGE("ERROR : nodes coordinates ...");
00157     return -1;
00158   }
00159   for (i=0;i<nnodes*spacedim;i++)
00160     printf("%f - ",*(coordinates+i));
00161   printf("\n");
00162 
00163   /* read polygons connectivity */
00164   if ((index = (med_int *) malloc(sizeof(med_int)*indexsize)) == NULL) {
00165      MESSAGE("ERROR : memory allocation ...");
00166      return -1;
00167    }
00168 
00169   if ((faceindex = (med_int *) malloc(sizeof(med_int)*faceIndexSize)) == NULL) {
00170      MESSAGE("ERROR : memory allocation ...");
00171      return -1;
00172    }
00173 
00174   if ((connectivity = (med_int *) malloc(sizeof(med_int)*connectivitysize)) == NULL) {
00175     MESSAGE("ERROR : memory allocation ...");
00176     return -1;
00177   }
00178 
00179   if (MEDmeshPolyhedronRd(fid,meshname,MED_NO_DT,MED_NO_IT,MED_CELL,MED_DESCENDING,
00180         faceindex, index, connectivity) < 0) {
00181     MESSAGE("ERROR : read polygon connectivity ...");
00182     return -1;
00183   }
00184 
00185   for (i=0;i<npoly;i++)
00186     {
00187     printf(">> MED_POLYHEDRON "IFORMAT" : \n",i+1);
00188     printf("---- Face Index         ----- : [ ");
00189     ind1 = *(index+i)-1;
00190     ind2 = *(index+i+1)-1;
00191     for (k=ind1;k<ind2;k++)
00192       printf(IFORMAT" ",*(faceindex+k));
00193     printf(" ] \n");
00194     printf("---- Connectivity       ----- : [ ");
00195     for (k=0;k<connectivitysize;k++)
00196       {
00197       for (j=0;j<3;j++)
00198         {
00199         printf(IFORMAT" ",triaconnectivity[connectivity[k]+j]);
00200         }
00201       printf("\n");
00202       }
00203     printf(" ] \n");
00204     }
00205 
00206   /*
00207    * ... we know that the family number of nodes and elements is 0, a real working would check ...
00208    */
00209 
00210   /* close MED file */
00211   if (MEDfileClose(fid) < 0) {
00212     MESSAGE("ERROR : close file");
00213     return -1;
00214   }
00215 
00216   /* memory deallocation */
00217   printf("Before free(coordinates)\n");
00218   if (coordinates)
00219     free(coordinates);
00220 
00221   printf("Before free(connectivity)\n");
00222   if (connectivity)
00223     free(connectivity);
00224 
00225   printf("Before free(triaconnectivity)\n");
00226   if (triaconnectivity)
00227     free(triaconnectivity);
00228 
00229   printf("Before free(index)\n");
00230   if (index)
00231     free(index);
00232 
00233   return 0;
00234 }