lasso
test_entset.cpp
Go to the documentation of this file.
00001 #include "iGeom.h"
00002 #include "iMesh.h"
00003 #include "iRel.h"
00004 
00005 #include "stdio.h"
00006 #include "stdlib.h"
00007 #include "string.h"
00008 
00009 #ifdef HAVE_OCC
00010 #define DEFAULT_GEOM_FILE brick.stp
00011 #define DEFAULT_MESH_FILE brick.h5m
00012 #else
00013 #define DEFAULT_GEOM_FILE brick.cub
00014 #define DEFAULT_MESH_FILE brick.cub
00015 #endif
00016 
00017 #define STRINGIFY_(X) #X
00018 #define STRINGIFY(X) STRINGIFY_(X)
00019 #ifdef SRCDIR
00020 #  define DEFAULT_GEOM STRINGIFY(SRCDIR/DEFAULT_GEOM_FILE)
00021 #  define DEFAULT_MESH STRINGIFY(SRCDIR/DEFAULT_MESH_FILE)
00022 #else
00023 #  define DEFAULT_GEOM STRINGIFY(DEFAULT_GEOM_FILE)
00024 #  define DEFAULT_MESH STRINGIFY(DEFAULT_MESH_FILE)
00025 #endif
00026 
00027 #define CHECK_SIZE_C(type, array, allocated_size, size)  \
00028           if (NULL == *array || *allocated_size == 0) {\
00029             *array = (type *) malloc(sizeof(type) * size); \
00030             *allocated_size = size;} \
00031           else if (*allocated_size < size) { \
00032              printf("   Array passed in is non-zero but too short.\n"); }
00033 
00034 typedef void* iRel_EntityHandle;
00035 
00039 void handle_error_code(const int result,
00040                        int *number_failed,
00041                        int *number_not_implemented,
00042                        int *number_successful)
00043 {
00044   if (result) {
00045     printf("Success");
00046     (*number_successful)++;
00047   }
00048   else {
00049     printf("Failure");
00050     (*number_failed)++;
00051   }
00052 }
00053 
00054 int print_geom_info(iGeom_Instance geom, iBase_EntityHandle gent)
00055 {
00056     /* print information about this entity */
00057   int ent_type;
00058   int result;
00059   const char *type_names[] = {"Vertex", "Edge", "Face", "Region"};
00060 
00061   iGeom_getEntType(geom, gent, &ent_type, &result);
00062 
00063   if (iBase_SUCCESS != result) {
00064     printf("Trouble getting entity adjacencies or types.");
00065     return 0;
00066   }
00067 
00068   printf("%s 0x%lx\n", type_names[ent_type], (unsigned long)gent);
00069 
00070   return 1;
00071 }
00072 
00073 int print_mesh_info(iMesh_Instance mesh, iBase_EntityHandle ment)
00074 {
00075     /* print information about this entity */
00076 
00077     /* get adjacencies first; assume not more than 50 */
00078   iBase_EntityHandle adj_ents[50], *adj_ents_ptr = adj_ents;
00079    int ent_types[50], *ent_types_ptr = ent_types;
00080   int adj_ents_alloc = 50, adj_ents_size, ent_types_size,
00081     ent_types_allocated = 50;
00082   int result;
00083 
00084   iBase_TagHandle *ment_tags = NULL;
00085   int ment_tags_size, ment_tags_alloc;
00086 
00087   char **tag_names;
00088   int i;
00089 
00090   const char *type_names[] = {"Vertex", "Edge", "Face", "Region"};
00091 
00092   iMesh_getEntAdj(mesh, ment, iBase_ALL_TYPES,
00093                   &adj_ents_ptr, &adj_ents_alloc, &adj_ents_size,
00094                   &result);
00095 
00096   if (iBase_SUCCESS != result) return 0;
00097 
00098     /* put this ent on the end, then get types */
00099   adj_ents[adj_ents_size] = ment;
00100   iMesh_getEntArrType(mesh, adj_ents, adj_ents_size+1,
00101                       &ent_types_ptr, &ent_types_allocated,
00102                       &ent_types_size, &result);
00103   if (iBase_SUCCESS != result) {
00104     printf("Trouble getting entity adjacencies or types.");
00105     return 0;
00106   }
00107 
00108     /* get tags on ment */
00109   iMesh_getAllTags(mesh, ment,
00110                    &ment_tags, &ment_tags_alloc, &ment_tags_size,
00111                    &result);
00112 
00113   printf("Trouble getting tags on an entity or their names.");
00114 
00115     /* while we're at it, get all the tag names */
00116 
00117   tag_names = (char **) malloc(ment_tags_size * sizeof(char*));
00118 
00119   for (i = 0; i < ment_tags_size; i++) {
00120     tag_names[i] = (char*)malloc(120*sizeof(char));
00121     iMesh_getTagName(mesh, ment_tags[i], tag_names[i], &result, 120);
00122   }
00123 
00124     /* now print the information */
00125   printf("%s %ld:\n", type_names[ent_types[ent_types_size-1]], (long)ment);
00126   printf("Adjacencies:");
00127   for (i = 0; i < adj_ents_size; i++) {
00128     if (i > 0) printf(", ");
00129     printf("%s %ld", type_names[ent_types[i]],
00130            (long)adj_ents[i]);
00131   }
00132   printf("\nTags: \n");
00133   for (i = 0; i < ment_tags_size; i++) {
00134     int tag_type;
00135 
00136     printf("%s ", tag_names[i]);
00137     iMesh_getTagType(mesh, ment_tags[i], &tag_type, &result);
00138     if (iBase_SUCCESS != result)
00139       printf("(trouble getting type...)\n");
00140     else {
00141       char *dum_handle = NULL;
00142       int dum_handle_alloc = 0, dum_handle_size = 0;
00143       int int_data;
00144       double dbl_data;
00145       iBase_EntityHandle eh_data;
00146 
00147       switch (tag_type) {
00148         case iBase_INTEGER:
00149           iMesh_getIntData(mesh, ment, ment_tags[i], &int_data, &result);
00150           printf("(Int value=%d)", int_data);
00151           break;
00152         case iBase_DOUBLE:
00153           iMesh_getDblData(mesh, ment, ment_tags[i], &dbl_data, &result);
00154           printf("(Dbl value=%f)", dbl_data);
00155           break;
00156         case iBase_ENTITY_HANDLE:
00157           iMesh_getEHData(mesh, ment, ment_tags[i], &eh_data, &result);
00158           printf("(EH value=%ld)", (long)eh_data);
00159           break;
00160         case iBase_BYTES:
00161           iMesh_getData(mesh, ment, ment_tags[i],
00162                         (void**)&dum_handle, &dum_handle_alloc,
00163                         &dum_handle_size, &result);
00164           if (NULL != dum_handle && dum_handle_size > 0)
00165             printf("(Opaque value=%c)", dum_handle[0]);
00166           break;
00167       }
00168     }
00169 
00170     printf("\n");
00171   }
00172   printf("(end tags)\n\n");
00173 
00174   return 1;
00175 }
00176 
00182 int load_geom_mesh_test(const char *geom_filename,
00183                         const char *mesh_filename,
00184                         iGeom_Instance geom,
00185                         iMesh_Instance mesh)
00186 {
00187     /* load a geom */
00188   int result;
00189   iGeom_load(geom, geom_filename, 0, &result, strlen(geom_filename), 0);
00190   if (iBase_SUCCESS != result) {
00191     printf("ERROR : can not load a geometry\n");
00192     return 0;
00193   }
00194 
00195     /* load a mesh */
00196   iMesh_load(mesh, 0, mesh_filename, 0, &result, strlen(mesh_filename), 0);
00197   if (iBase_SUCCESS != result) {
00198     printf("ERROR : can not load a mesh\n");
00199     return 0;
00200   }
00201 
00202   return 1;
00203 }
00204 
00210 int create_relation_test(iRel_Instance assoc,
00211                          iGeom_Instance geom,
00212                          iMesh_Instance mesh,
00213                          iRel_PairHandle *pair)
00214 {
00215   int result;
00216   iBase_Instance iface1, iface2;
00217   int type1, type2;
00218   int ent_or_set1, ent_or_set2;
00219   int status1, status2;
00220 
00221   iRel_PairHandle tmp_pair;
00222   iRel_PairHandle *pair_ptr = &tmp_pair;
00223   int pairs_alloc = 1, pairs_size;
00224 
00225     /* create an relation, entity to set */
00226   iRel_createPair(assoc,
00227                   geom, iRel_ENTITY, iRel_IGEOM_IFACE, iRel_ACTIVE,
00228                   mesh, iRel_SET,    iRel_IMESH_IFACE, iRel_ACTIVE,
00229                   pair, &result);
00230   if (iBase_SUCCESS != result) {
00231     printf("Couldn't create a new relation.\n");
00232     return 0;
00233   }
00234 
00235   iRel_getPairInfo(assoc, *pair,
00236                    &iface1, &ent_or_set1, &type1, &status1,
00237                    &iface2, &ent_or_set2, &type2, &status2, &result);
00238   if (iBase_SUCCESS != result) {
00239     printf("Couldn't retrieve relation info.\n");
00240     return 0;
00241   }
00242   if (iface1 != geom || ent_or_set1 != iRel_ENTITY ||
00243       type1 != iRel_IGEOM_IFACE || iface2 != mesh || ent_or_set2 != iRel_SET ||
00244       type2 != iRel_IMESH_IFACE) {
00245     printf("Unexpected relation info returned.\n");
00246     return 0;
00247   }
00248 
00249   iRel_findPairs(assoc, geom, &pair_ptr, &pairs_alloc, &pairs_size, &result);
00250   if (iBase_SUCCESS != result) {
00251     printf("Couldn't find relation pair when querying geom.\n");
00252     return 0;
00253   }
00254   if (pairs_size != 1 || tmp_pair != *pair) {
00255     printf("Unexpected relation pairs returned when querying geom.\n");
00256     return 0;
00257   }
00258 
00259   iRel_findPairs(assoc, mesh, &pair_ptr, &pairs_alloc, &pairs_size, &result);
00260   if (iBase_SUCCESS != result) {
00261     printf("Couldn't find relation pair when querying mesh.\n");
00262     return 0;
00263   }
00264   if (pairs_size != 1 || tmp_pair != *pair) {
00265     printf("Unexpected relation pairs returned when querying mesh.\n");
00266     return 0;
00267   }
00268 
00269   return 1;
00270 }
00271 
00277 int relate_geom_mesh_test(iRel_Instance assoc,
00278                           iGeom_Instance geom,
00279                           iMesh_Instance mesh,
00280                           iRel_PairHandle pair)
00281 {
00282   int result;
00283 
00284   iBase_EntityHandle *gentities = NULL;
00285   int gentities_size = 0, gentities_alloc = 0;
00286 
00287   iBase_EntitySetHandle *mentity_handles = NULL;
00288   int mentity_handles_size = 0, mentity_handles_alloc = 0;
00289 
00290   const char *dim_tag_name = "GEOM_DIMENSION";
00291   iBase_TagHandle dim_tag_mesh;
00292 
00293   iBase_EntitySetHandle *mentities_vec;
00294   int mentities_vec_size = 0;
00295   int i;
00296 
00297   iBase_EntitySetHandle *out_mentities = NULL;
00298   int out_mentities_size = 0, out_mentities_alloc = 0;
00299 
00300   iBase_EntitySetHandle *out_mentities2 = NULL;
00301   int out_mentities2_size = 0, out_mentities2_alloc = 0;
00302 
00303   iBase_EntityHandle *out_gentities = NULL;
00304   int out_gentities_size = 0, out_gentities_alloc = 0;
00305 
00306     /* relate geometry entities with coresponding mesh entity sets */
00307   iGeom_getEntities(geom, NULL,
00308                     iBase_VERTEX,
00309                     &gentities,
00310                     &gentities_alloc,
00311                     &gentities_size, &result);
00312   if (iBase_SUCCESS != result) {
00313     printf("Failed to get gentities by type in relate_geom_mesh_test.\n");
00314     return 0;
00315   }
00316 
00317   iRel_inferEntArrRelations(assoc, pair,
00318                             gentities, gentities_size, 0,
00319                             &result);
00320   if (iBase_SUCCESS != result) {
00321     printf("Failed to relate geom entities in relate_geom_mesh_test.\n");
00322     return 0;
00323   }
00324 
00325     /* relate coresponding mesh entity sets for geometry entities */
00326     /* get 1-dimensional mesh entitysets */
00327   iMesh_getEntSets(mesh, NULL, 1,
00328                    &mentity_handles, &mentity_handles_alloc,
00329                    &mentity_handles_size, &result);
00330   if (iBase_SUCCESS != result) {
00331     printf("Problem to get all entity sets.\n");
00332     return 0;
00333   }
00334 
00335     /* get geom dimension tags for mesh entitysets */
00336   iMesh_createTag(mesh, dim_tag_name, 1, iBase_INTEGER,
00337                   &dim_tag_mesh, &result, 15);
00338   if (iBase_SUCCESS != result && result != iBase_TAG_ALREADY_EXISTS) {
00339     printf("Couldn't create geom dim tag for mesh entities.\n");
00340     return 0;
00341   }
00342 
00343     /* get 1-dimensional mesh entitysets */
00344   mentities_vec = (iBase_EntitySetHandle*)
00345     malloc(mentity_handles_size*sizeof(iBase_EntitySetHandle));
00346   for (i = 0; i < mentity_handles_size; i++) { /* test */
00347     int dim;
00348     iMesh_getEntSetIntData(mesh, mentity_handles[i], dim_tag_mesh,
00349                            &dim, &result);
00350     if (iBase_SUCCESS != result)
00351       continue;
00352 
00353     if (dim == 1)
00354       mentities_vec[mentities_vec_size++] = mentity_handles[i];
00355   }
00356 
00357   iRel_inferSetArrRelations(assoc, pair,
00358                             mentities_vec, mentities_vec_size,
00359                             1, &result);
00360   if (iBase_SUCCESS != result) {
00361     printf("Failed to relate mesh entities in relate_geom_mesh_test.\n");
00362     return 0;
00363   }
00364 
00365     /* relate all geometry and mesh entities */
00366   iRel_inferAllRelations(assoc, pair, &result);
00367   if (iBase_SUCCESS != result) {
00368     printf("Failed to relate all geom and mesh entities in relate_geom_mesh_test.\n");
00369     return 0;
00370   }
00371 
00372     /* reset geom entities list and get all geom entities (prev
00373        only vertices) */
00374   free(gentities);
00375   gentities = NULL;
00376   gentities_alloc = 0;
00377   iGeom_getEntities(geom, NULL,
00378                     iBase_ALL_TYPES,
00379                     &gentities,
00380                     &gentities_alloc,
00381                     &gentities_size, &result);
00382   if (iBase_SUCCESS != result) {
00383     printf("Failed to get gentities by type in relate_geom_mesh_test.\n");
00384     return 0;
00385   }
00386 
00387     /* get related mesh entity sets for geometry entities */
00388   iRel_getEntArrSetArrRelation(assoc, pair,
00389                                gentities, gentities_size, 0,
00390                                &out_mentities, &out_mentities_alloc,
00391                                &out_mentities_size,
00392                                &result);
00393   if (iBase_SUCCESS != result) {
00394     printf("Failed to get geom entities in relate_geom_mesh_test.\n");
00395     return 0;
00396   }
00397 
00398   if (out_mentities_size != gentities_size) {
00399     printf("Number of input geom entities and output mesh entity sets should be same\n");
00400     return 0;
00401   }
00402 
00403     /* now try deleting this relation */
00404   iRel_rmvEntArrRelation(assoc, pair, gentities, gentities_size, 0, &result);
00405   if (iBase_SUCCESS != result) {
00406     printf("Failed to remove relation in relate_geom_mesh_test.\n");
00407     return 0;
00408   }
00409 
00410   iRel_getEntArrSetArrRelation(assoc, pair,
00411                                gentities, gentities_size, 0,
00412                                &out_mentities2, &out_mentities2_alloc,
00413                                &out_mentities2_size,
00414                                &result);
00415   if (iBase_SUCCESS == result) {
00416     printf("Shouldn't have gotten mesh sets in relate_geom_mesh_test.\n");
00417     return 0;
00418   }
00419 
00420     /* restore the relation, since we need it later */
00421   iRel_setEntArrSetArrRelation(assoc, pair, gentities, gentities_size,
00422                                out_mentities, out_mentities_size, &result);
00423   if (iBase_SUCCESS != result) {
00424     printf("Failed to restore relation in relate_geom_mesh_test.\n");
00425     return 0;
00426   }
00427 
00428     /* get related geometry entities for mesh entity sets */
00429   iRel_getSetArrEntArrRelation(assoc, pair,
00430                                out_mentities, out_mentities_size, 1,
00431                                &out_gentities, &out_gentities_alloc,
00432                                &out_gentities_size,
00433                                &result);
00434   if (iBase_SUCCESS != result) {
00435     printf("Failed to get mesh entities in relate_geom_mesh_test.\n");
00436     return 0;
00437   }
00438 
00439   if (out_mentities_size != out_gentities_size) {
00440     printf("Number of input mesh entity sets and output geom entities should be same\n");
00441     return 0;
00442   }
00443 
00444   return 1;
00445 }
00446 
00452 int query_relations_test(iRel_Instance assoc,
00453                          iGeom_Instance geom,
00454                          iMesh_Instance mesh,
00455                          iRel_PairHandle pair)
00456 {
00457   int result;
00458   int i;
00459 
00460   iBase_EntityHandle *gentities = NULL;
00461   int gentities_size = 0, gentities_alloc = 0;
00462 
00463   iBase_EntitySetHandle *out_mentities = NULL;
00464   int out_mentities_size, out_mentities_alloc = 0;
00465 
00466   iBase_EntityHandle *out_gentities = NULL;
00467   int out_gentities_size, out_gentities_alloc = 0;
00468 
00469     /* get all the geom entities, and find relation to some mesh entity */
00470   iGeom_getEntities(geom, NULL, iBase_ALL_TYPES,
00471                     &gentities, &gentities_alloc,
00472                     &gentities_size, &result);
00473   if (iBase_SUCCESS != result) {
00474     printf("Problem getting all geom entities.\n" );
00475     return 0;
00476   }
00477 
00478   iRel_getEntArrSetArrRelation(assoc, pair,
00479                                gentities, gentities_size, 0,
00480                                &out_mentities, &out_mentities_alloc,
00481                                &out_mentities_size,
00482                                &result);
00483     /* might not all be */
00484   if (iBase_SUCCESS != result) {
00485     char descr[120];
00486 
00487     printf("Failed to get mesh entities related to geom entities in query_relations_test.\n");
00488 
00489     iRel_getDescription(assoc, descr, sizeof(descr)-1);
00490     printf("Entities missing relations: %s\n", descr);
00491 
00492     for (i = 0; i < gentities_size; i++) {
00493         print_geom_info(geom, gentities[i]);
00494     }
00495 
00496     return 0;
00497   }
00498 
00499     /* check that they're all non-null */
00500   if (out_mentities_size != gentities_size) {
00501     printf("Number of mesh & related geom entities don't match.\n");
00502     return 0;
00503   }
00504 
00505     /* check to make sure they're mesh sets; how to do that? */
00506   for (i = 0; i < out_mentities_size; i++) {
00507     int is_list;
00508     iMesh_isList(mesh, (iBase_EntitySetHandle)out_mentities[i], &is_list, &result);
00509     if (iBase_SUCCESS != result) {
00510       printf("Entity set returned from classification wasn't valid.\n");
00511       return 0;
00512     }
00513   }
00514 
00515     /* now turn around and check classification of those mesh entities */
00516   iRel_getSetArrEntArrRelation(assoc, pair,
00517                                out_mentities, out_mentities_size, 1,
00518                                &out_gentities, &out_gentities_alloc,
00519                                &out_gentities_size,
00520                                &result);
00521   if (iBase_SUCCESS != result) {
00522     printf("Failed to get geom entities related to mesh entities in query_relations_test.\n");
00523     return 0;
00524   }
00525 
00526     /* check that they're all non-null */
00527   if (out_mentities_size != out_gentities_size) {
00528     printf("Number of geom & related mesh entities don't match.\n" );
00529     return 0;
00530   }
00531 
00532     /* ok, we're done */
00533   return 1;
00534 }
00535 
00536 
00537 int main( int argc, char *argv[] )
00538 {
00539     /* Check command line arg */
00540   const char *geom_filename = DEFAULT_GEOM;
00541   const char *mesh_filename = DEFAULT_MESH;
00542 
00543   int result;
00544   int number_tests = 0;
00545   int number_tests_successful = 0;
00546   int number_tests_not_implemented = 0;
00547   int number_tests_failed = 0;
00548 
00549   iGeom_Instance geom;
00550   iMesh_Instance mesh;
00551   iRel_Instance assoc;
00552   iRel_PairHandle pair;
00553 
00554   if (argc == 2 && !strcmp(argv[1], "-h")) {
00555     printf("Usage: %s <geom_filename> <mesh_filename>\n",
00556            argv[0]);
00557     return 1;
00558   }
00559   else if (argc == 2) {
00560     geom_filename = argv[1];
00561     mesh_filename = argv[1];
00562   }
00563   else if (argc == 3) {
00564     geom_filename = argv[1];
00565     mesh_filename = argv[2];
00566   }
00567 
00568     /* initialize the Geometry */
00569   iGeom_newGeom(0, &geom, &result, 0);
00570 
00571     /* initialize the Mesh */
00572   iMesh_newMesh(0, &mesh, &result, 0);
00573 
00574     /* initialize the Associate */
00575   iRel_create(0, &assoc, &result, 0);
00576 
00577     /* Print out Header information */
00578   printf("\n\niRel TEST PROGRAM:\n\n");
00579 
00580     /* load_geom_mesh test */
00581   printf("   load_geom_mesh: ");
00582   result = load_geom_mesh_test(geom_filename, mesh_filename,
00583                                geom, mesh);
00584   handle_error_code(result, &number_tests_failed,
00585                     &number_tests_not_implemented,
00586                     &number_tests_successful);
00587   number_tests++;
00588   printf("\n");
00589 
00590     /* create_relation test */
00591   printf("   create_relation: ");
00592   result = create_relation_test(assoc, geom, mesh, &pair);
00593   handle_error_code(result, &number_tests_failed,
00594                     &number_tests_not_implemented,
00595                     &number_tests_successful);
00596   number_tests++;
00597   printf("\n");
00598 
00599     /* relate_geom_mesh test */
00600   printf("   relate_geom_mesh: ");
00601   result = relate_geom_mesh_test(assoc, geom, mesh, pair);
00602   handle_error_code(result, &number_tests_failed,
00603                     &number_tests_not_implemented,
00604                     &number_tests_successful);
00605   number_tests++;
00606   printf("\n");
00607 
00608     /* query_relations test */
00609   printf("   query_relations: ");
00610   result = query_relations_test(assoc, geom, mesh, pair);
00611   handle_error_code(result, &number_tests_failed,
00612                     &number_tests_not_implemented,
00613                     &number_tests_successful);
00614   number_tests++;
00615   printf("\n");
00616 
00617     /* summary */
00618 
00619   printf("\niRel TEST SUMMARY: \n");
00620   printf("   Number Tests:           %d\n", number_tests);
00621   printf("   Number Successful:      %d\n", number_tests_successful);
00622   printf("   Number Not Implemented: %d\n", number_tests_not_implemented);
00623   printf("   Number Failed:          %d\n", number_tests_failed);
00624   printf("\n\n");
00625 
00626   iRel_destroy(assoc, &result);
00627 
00628   return number_tests_failed != 0;
00629 }
00630 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines