cgma
testgeom.cc
Go to the documentation of this file.
00001 
00018 #include "iGeom.h"
00019 #include <iostream>
00020 #include <set>
00021 #include <algorithm>
00022 #include <vector>
00023 #include <iterator>
00024 #include <algorithm>
00025 #include <iomanip>
00026 #include <assert.h>
00027 #include <string.h>
00028 #include <math.h>
00029 #define CHECK( STR ) if (err != iBase_SUCCESS) return print_error( STR, err, geom, __FILE__, __LINE__ )
00030 
00031 #define STRINGIFY(S) XSTRINGIFY(S)
00032 #define XSTRINGIFY(S) #S
00033 
00034 static bool print_error( const char* desc, 
00035                          int err,
00036                          iGeom_Instance geom,
00037                          const char* file,
00038                          int line )
00039 {
00040   char buffer[1024];
00041   iGeom_getDescription( geom, buffer, sizeof(buffer) );
00042   buffer[sizeof(buffer)-1] = '\0';
00043   
00044   std::cerr << "ERROR: " << desc << std::endl
00045             << "  Error code: " << err << std::endl
00046             << "  Error desc: " << buffer << std::endl
00047             << "  At        : " << file << ':' << line << std::endl
00048             ;
00049   
00050   return false; // must always return false or CHECK macro will break
00051 }
00052 
00053 typedef iBase_TagHandle TagHandle;
00054 typedef iBase_EntityHandle GentityHandle;
00055 typedef iBase_EntitySetHandle GentitysetHandle;
00056 
00057 /* Frees allocated arrays for us */
00058 template <typename T> class SimpleArray
00059 {
00060   private:
00061     T* arr;
00062     int arrSize;
00063     int arrAllocated;
00064      
00065   public:
00066     SimpleArray() : arr(0) , arrSize(0), arrAllocated(0) {}
00067     SimpleArray( unsigned s ) :arrSize(s), arrAllocated(s) {
00068       arr = (T*)malloc(s*sizeof(T));
00069       for (unsigned i = 0; i < s; ++i)
00070         new (arr+i) T();
00071     }
00072     
00073     ~SimpleArray() {
00074       for (int i = 0; i < size(); ++i)
00075         arr[i].~T();
00076       free(arr);
00077     }
00078 
00079     T**  ptr()            { return &arr; }
00080     int& size()           { return arrSize; }
00081     int  size()     const { return arrSize; }
00082     int& capacity()       { return arrAllocated; }
00083     int  capacity() const { return arrAllocated; }
00084     
00085     typedef T* iterator;
00086     typedef const T* const_iterator;
00087     iterator       begin()       { return arr; }
00088     const_iterator begin() const { return arr; }
00089     iterator         end()       { return arr + arrSize; }
00090     const_iterator   end() const { return arr + arrSize; }
00091     
00092     
00093     T& operator[]( unsigned idx )       { return arr[idx]; }
00094     T  operator[]( unsigned idx ) const { return arr[idx]; }
00095 };
00096 
00097 #define ARRAY_INOUT( A ) A.ptr(), &A.capacity(), &A.size()
00098 #define ARRAY_IN( A ) &A[0], A.size()
00099 
00100 bool gLoad_test(const std::string filename, iGeom_Instance);
00101 
00102 bool tags_test(iGeom_Instance geom);
00103 bool tag_get_set_test(iGeom_Instance geom);
00104 bool tag_info_test(iGeom_Instance geom);
00105 bool gentityset_test(iGeom_Instance geom, bool /*multiset*/, bool /*ordered*/);
00106 bool topology_adjacencies_test(iGeom_Instance geom);
00107 bool construct_test(iGeom_Instance geom);
00108 bool primitives_test(iGeom_Instance geom);
00109 bool transforms_test(iGeom_Instance geom);
00110 bool booleans_test(iGeom_Instance geom);
00111 bool faceting_test(iGeom_Instance geom);
00112 bool shutdown_test(iGeom_Instance geom, std::string &engine_opt);
00113 bool save_entset_test(iGeom_Instance geom);
00114 bool mesh_size_test(iGeom_Instance geom);
00115 
00116 void handle_error_code(const bool result,
00117                        int &number_failed,
00118                        int &/*number_not_implemented*/,
00119                        int &number_successful)
00120 {
00121   if (result) {
00122     std::cout << "Success";
00123     number_successful++;
00124   }
00125   else {
00126     std::cout << "Failure";    
00127     number_failed++;
00128   }
00129 }
00130 
00131 int main( int argc, char *argv[] )
00132 {
00133     // Check command line arg
00134   #ifdef FORCE_OCC
00135     #ifndef HAVE_OCC
00136       #error "Cannot force use of OCC w/out OCC support"
00137     #endif
00138     std::string filename = STRINGIFY(SRCDIR) "/../test/LeverArm.brep";
00139     std::string engine_opt = ";engine=OCC";
00140   #elif defined(HAVE_OCC)
00141     std::string filename = STRINGIFY(SRCDIR) "/../test/LeverArm.brep";
00142     std::string engine_opt = ";engine=OCC";
00143   #else
00144     std::string filename = STRINGIFY(SRCDIR) "/brick.facet";
00145     std::string engine_opt = ";engine=FACET";
00146   #endif
00147   if (argc == 1) {
00148     std::cout << "Using default input file: " << filename << std::endl;
00149   }
00150   else if (argc == 2) {
00151     filename = argv[1];
00152   }
00153   else {
00154     std::cerr << "Usage: " << argv[0] << " [geom_filename]" << std::endl;
00155     return 1;
00156   }
00157 
00158   bool result;
00159   int number_tests = 0;
00160   int number_tests_successful = 0;
00161   int number_tests_not_implemented = 0;
00162   int number_tests_failed = 0;
00163 
00164     // initialize the Mesh
00165   int err;
00166   iGeom_Instance geom;
00167   iGeom_newGeom( engine_opt.c_str(), &geom, &err, engine_opt.length() );
00168 
00169     // Print out Header information
00170   std::cout << "\n\nITAPS GEOMETRY INTERFACE TEST PROGRAM:\n\n";
00171     // gLoad test
00172   std::cout << "   gLoad: ";
00173   result = gLoad_test(filename, geom);
00174   handle_error_code(result, number_tests_failed,
00175                     number_tests_not_implemented,
00176                     number_tests_successful);
00177   number_tests++;
00178   std::cout << "\n";
00179 
00180     // tags test
00181   std::cout << "   tags: ";
00182   result = tags_test(geom);
00183   handle_error_code(result, number_tests_failed,
00184                     number_tests_not_implemented,
00185                     number_tests_successful);
00186   number_tests++;
00187   std::cout << "\n";
00188 
00189     // gentitysets test
00190   std::cout << "   gentity sets: ";
00191   result = gentityset_test(geom, false, false);
00192   handle_error_code(result, number_tests_failed,
00193                     number_tests_not_implemented,
00194                     number_tests_successful);
00195   number_tests++;
00196   std::cout << "\n";
00197 
00198     // topology adjacencies test
00199   std::cout << "   topology adjacencies: ";
00200   result = topology_adjacencies_test(geom);
00201   handle_error_code(result, number_tests_failed,
00202                     number_tests_not_implemented,
00203                     number_tests_successful);
00204   number_tests++;
00205   std::cout << "\n";
00206 #if defined(HAVE_OCC)
00207     // construct test
00208   std::cout << "   construct: ";
00209   result = construct_test(geom);
00210   handle_error_code(result, number_tests_failed,
00211                     number_tests_not_implemented,
00212                     number_tests_successful);
00213   number_tests++;
00214   std::cout << "\n";
00215 
00216     // primitives test
00217   std::cout << "   primitives: ";
00218   result = primitives_test(geom);
00219   handle_error_code(result, number_tests_failed,
00220                     number_tests_not_implemented,
00221                     number_tests_successful);
00222   number_tests++;
00223   std::cout << "\n";
00224 
00225     // transforms test
00226   std::cout << "   transforms: ";
00227   result = transforms_test(geom);
00228   handle_error_code(result, number_tests_failed,
00229                     number_tests_not_implemented,
00230                     number_tests_successful);
00231   number_tests++;
00232   std::cout << "\n";
00233 
00234     // booleans test
00235   std::cout << "   booleans: ";
00236   result = booleans_test(geom);
00237   handle_error_code(result, number_tests_failed,
00238                     number_tests_not_implemented,
00239                     number_tests_successful);
00240   number_tests++;
00241   std::cout << "\n";
00242 #endif
00243   
00244   // this is not run for occ or facet; keep it for the time being
00245 #if 0
00246 
00247     // save entset test
00248   std::cout << "   save entset: ";
00249   result = save_entset_test(geom);
00250   handle_error_code(result, number_tests_failed,
00251                     number_tests_not_implemented,
00252                     number_tests_successful);
00253   number_tests++;
00254   std::cout << "\n";
00255 #endif  
00256 
00257 //not working for cubit13.1 cgm at iGeom_CGMA.cc, line 7428, get_graphics for 
00258 //faces, comment out
00259     // facet tests
00260 #ifdef HAVE_OCC
00261   std::cout << "   faceting: ";
00262   result = faceting_test(geom);
00263   handle_error_code(result, number_tests_failed,
00264                     number_tests_not_implemented,
00265                     number_tests_successful);
00266   number_tests++;
00267   std::cout << "\n";
00268 #endif
00269 
00270     // shutdown test
00271   std::cout << "   shutdown: ";
00272   result = shutdown_test(geom, engine_opt);
00273   handle_error_code(result, number_tests_failed,
00274                     number_tests_not_implemented,
00275                     number_tests_successful);
00276   number_tests++;
00277   std::cout << "\n";
00278   
00279 
00280     // summary
00281 
00282   std::cout << "\nTSTT TEST SUMMARY: \n"
00283             << "   Number Tests:           " << number_tests << "\n"
00284             << "   Number Successful:      " << number_tests_successful << "\n"
00285             << "   Number Not Implemented: " << number_tests_not_implemented << "\n"
00286             << "   Number Failed:          " << number_tests_failed 
00287             << "\n\n" << std::endl;
00288   
00289   return number_tests_failed;
00290 }
00291 
00292 
00298 bool gLoad_test(const std::string filename, iGeom_Instance geom)
00299 {
00300   int err;
00301   iGeom_load( geom, &filename[0], 0, &err, filename.length(), 0 );
00302   CHECK( "ERROR : can not load a geometry" );
00303   
00304   iBase_EntitySetHandle root_set;
00305   iGeom_getRootSet( geom, &root_set, &err );
00306   CHECK( "ERROR : getRootSet failed!" );
00307   
00308     // print out the number of entities
00309   std::cout << "Model contents: " << std::endl;
00310   const char *gtype[] = {"vertices: ", "edges: ", "faces: ", "regions: "};
00311   for (int i = 0; i <= 3; ++i) {
00312     int count;
00313     iGeom_getNumOfType( geom, root_set, i, &count, &err );
00314     CHECK( "Error: problem getting entities after gLoad." );
00315     std::cout << gtype[i] << count << std::endl;
00316   }
00317 
00318   return true;
00319 }
00320 
00326 bool tags_test(iGeom_Instance geom)
00327 {
00328   bool success = true;
00329 
00330   success = tag_info_test(geom);
00331   if (!success) return success;
00332   
00333   success = tag_get_set_test(geom);
00334   if (!success) return success;
00335 
00336   return true;
00337 }
00338 
00339 bool tag_info_test(iGeom_Instance geom) 
00340 {
00341   int err;
00342   
00343   iBase_EntitySetHandle root_set;
00344   iGeom_getRootSet( geom, &root_set, &err );
00345   CHECK( "ERROR : getRootSet failed!" );
00346 
00347     // create an arbitrary tag, size 4
00348   iBase_TagHandle this_tag, tmp_handle;
00349   std::string tag_name("tag_info tag"), tmp_name;
00350   iGeom_createTag( geom, &tag_name[0], 4, iBase_BYTES, &this_tag, &err, tag_name.length() );
00351   CHECK( "ERROR : can not create a tag." );
00352 
00353     // get information on the tag
00354   
00355   char name_buffer[256];
00356   iGeom_getTagName( geom, this_tag, name_buffer, &err, sizeof(name_buffer) );
00357   CHECK( "ERROR : Couldn't get tag name." );
00358   if (tag_name != name_buffer) {
00359     std::cerr << "ERROR: getTagName returned '" << name_buffer 
00360               << "' for tag created as '" << tag_name << "'" << std::endl;
00361     return false;
00362   }
00363   
00364   
00365   iGeom_getTagHandle( geom, &tag_name[0], &tmp_handle, &err, tag_name.length() );
00366   CHECK( "ERROR : Couldn't get tag handle." );
00367   if (tmp_handle != this_tag) {
00368     std::cerr << "ERROR: getTagHandle didn't return consistent result." << std::endl;
00369     return false;
00370   }  
00371 
00372   int tag_size;
00373   iGeom_getTagSizeBytes( geom, this_tag, &tag_size, &err );
00374   CHECK( "ERROR : Couldn't get tag size." );
00375   if (tag_size != 4) {
00376     std::cerr << "ERROR: getTagSizeBytes: expected 4, got " << tag_size << std::endl;
00377     return false;
00378   }
00379 
00380   iGeom_getTagSizeValues( geom, this_tag, &tag_size, &err );
00381   CHECK( "ERROR : Couldn't get tag size." );
00382   if (tag_size != 4) {
00383     std::cerr << "ERROR: getTagSizeValues: expected 4, got " << tag_size << std::endl;
00384     return false;
00385   }
00386 
00387   int tag_type;
00388   iGeom_getTagType( geom, this_tag, &tag_type, &err );
00389   CHECK( "ERROR : Couldn't get tag type." );
00390   if (tag_type != iBase_BYTES) {
00391     std::cerr << "ERROR: getTagType: expected " << iBase_BYTES 
00392               << ", got " << tag_type << std::endl;
00393     return false;
00394   }
00395   
00396   iGeom_destroyTag( geom, this_tag, true, &err );
00397   CHECK( "ERROR : Couldn't delete a tag." );
00398 
00399     // print information about all the tags in the model
00400 
00401   std::set<iBase_TagHandle> tags;
00402   SimpleArray<iBase_EntityHandle> entities;
00403   iGeom_getEntities( geom, root_set, iBase_ALL_TYPES, 
00404                      ARRAY_INOUT(entities),  &err );
00405   CHECK( "getEntities( ..., iBase_ALL_TYPES, ... ) failed." );
00406   for (int i = 0; i < entities.size(); ++i) {
00407     SimpleArray<iBase_TagHandle> tag_arr;
00408     iGeom_getAllTags( geom, entities[i], ARRAY_INOUT(tag_arr), &err);
00409     CHECK( "getAllTags failed." );
00410     std::copy( tag_arr.begin(), tag_arr.end(), std::inserter( tags, tags.begin() ) );
00411   }
00412   
00413   std::cout << "Tags defined on model: ";
00414   bool first = true;
00415   for (std::set<iBase_TagHandle>::iterator sit = tags.begin(); sit != tags.end(); ++sit) {
00416     iGeom_getTagName( geom, *sit, name_buffer, &err, sizeof(name_buffer) );
00417     name_buffer[sizeof(name_buffer)-1] = '\0'; // mnake sure of NUL termination
00418     CHECK( "getTagName failed." );
00419     
00420     if (!first) std::cout << ", ";
00421     std::cout << name_buffer;
00422     first = false;
00423   }
00424   if (first) std::cout << "<none>";
00425   std::cout << std::endl;
00426 
00427   return true;
00428 }
00429 
00430 bool tag_get_set_test(iGeom_Instance geom) 
00431 {
00432   int err;
00433   
00434     // create an arbitrary tag, size 4
00435   iBase_TagHandle this_tag;
00436   std::string tag_name("tag_get_set tag");
00437   iGeom_createTag( geom, &tag_name[0], sizeof(int), iBase_BYTES, &this_tag, &err, tag_name.length() );
00438   CHECK( "ERROR : can not create a tag for get_set test." );
00439   
00440   iBase_EntitySetHandle root_set;
00441   iGeom_getRootSet( geom, &root_set, &err );
00442   CHECK( "ERROR : getRootSet failed!" );
00443   
00444     // set this tag to an integer on each entity; keep track of total sum
00445   int sum = 0, num = 0, dim;
00446   for (dim = 0; dim <= 3; dim++) {
00447     SimpleArray<iBase_EntityHandle> gentity_handles;
00448     iGeom_getEntities( geom, root_set, dim, ARRAY_INOUT( gentity_handles ), &err );
00449     int num_ents = gentity_handles.size();
00450     std::vector<int> tag_vals( num_ents );
00451     for (int i = 0; i < num_ents; ++i) {
00452       tag_vals[i] = num;
00453       sum += num;
00454       ++num;
00455     }
00456     
00457     iGeom_setArrData( geom, ARRAY_IN( gentity_handles ),
00458                       this_tag, 
00459                       &tag_vals[0], tag_vals.size()*sizeof(int),
00460                       &err );
00461     CHECK( "ERROR : can't set tag on entities" );
00462   }
00463   
00464     // check tag values for entities now
00465   int get_sum = 0;
00466   for (dim = 0; dim <= 3; dim++) {
00467     SimpleArray<iBase_EntityHandle> gentity_handles;
00468     iGeom_getEntities( geom, root_set, dim, ARRAY_INOUT( gentity_handles ), &err );
00469     int num_ents = gentity_handles.size();
00470     
00471     std::vector<int> tag_vals( num_ents );
00472     void* vals_ptr = &tag_vals[0];
00473     int vals_size = tag_vals.size(), vals_alloc = tag_vals.size()*sizeof(int);
00474 
00475     iGeom_getArrData( geom, ARRAY_IN( gentity_handles ), this_tag, 
00476                       &vals_ptr, &vals_alloc, &vals_size, &err );
00477     CHECK( "ERROR : can't get tag on entities" );
00478     
00479     for (int i = 0; i < num_ents; ++i)
00480       get_sum += tag_vals[i];
00481   }
00482   
00483   if (get_sum != sum) {
00484     std::cerr << "ERROR: getData didn't return consistent results." << std::endl;
00485     return false;
00486   }
00487   
00488   iGeom_destroyTag( geom, this_tag, true, &err );
00489   CHECK( "ERROR : couldn't delete tag." );
00490 
00491   return true;
00492 }
00493 
00499 bool gentityset_test(iGeom_Instance geom, bool /*multiset*/, bool /*ordered*/)
00500 {
00501   int num_type = 4;
00502   iBase_EntitySetHandle ges_array[4];
00503   int number_array[4];
00504   int num_all_gentities_super = 0;
00505   int ent_type = iBase_VERTEX;
00506 
00507   int err;
00508   iBase_EntitySetHandle root_set;
00509   iGeom_getRootSet( geom, &root_set, &err );
00510   CHECK( "ERROR : getRootSet failed!" );
00511   
00512     // get the number of sets in the whole model
00513   int all_sets = 0;
00514   iGeom_getNumEntSets( geom, root_set, 0, &all_sets, &err );
00515   CHECK( "Problem getting the number of all gentity sets in whole model." );
00516 
00517     // add gentities to entitysets by type
00518   for (; ent_type < num_type; ent_type++) {
00519       // initialize the entityset
00520     iGeom_createEntSet( geom, true, &ges_array[ent_type], &err );
00521     CHECK( "Problem creating entityset." );
00522 
00523       // get entities by type in total "mesh"
00524     SimpleArray<iBase_EntityHandle> gentities;
00525     iGeom_getEntities( geom, root_set, ent_type, ARRAY_INOUT(gentities), &err );
00526     CHECK( "Failed to get gentities by type in gentityset_test." );
00527     
00528       // add gentities into gentity set
00529     iGeom_addEntArrToSet( geom, ARRAY_IN( gentities ), ges_array[ent_type], &err );
00530     CHECK( "Failed to add gentities in entityset_test." );
00531     
00532       // Check to make sure entity set really has correct number of entities in it
00533     iGeom_getNumOfType( geom, ges_array[ent_type], ent_type, &number_array[ent_type], &err );
00534     CHECK( "Failed to get number of gentities by type in entityset_test." );
00535 
00536       // compare the number of entities by type
00537     int num_type_gentity = gentities.size();
00538 
00539     if (number_array[ent_type] != num_type_gentity)
00540     {
00541       std::cerr << "Number of gentities by type is not correct"
00542                 << std::endl;
00543       return false;
00544     }
00545 
00546       // add to number of all entities in super set
00547     num_all_gentities_super += num_type_gentity;
00548   }
00549 
00550     // make a super set having all entitysets
00551   iBase_EntitySetHandle super_set;
00552   iGeom_createEntSet( geom, true, &super_set, &err );
00553   CHECK( "Failed to create a super set in gentityset_test." );
00554 
00555   for (int i = 0; i < num_type; i++) {
00556     iGeom_addEntSet( geom, ges_array[i], super_set, &err );
00557     CHECK( "Failed to create a super set in gentityset_test." );
00558   }
00559 
00560     //----------TEST BOOLEAN OPERATIONS----------------//
00561 
00562   iBase_EntitySetHandle temp_ges1;
00563   iGeom_createEntSet( geom, true, &temp_ges1, &err );
00564   CHECK( "Failed to create a super set in gentityset_test." );
00565 
00566     // Subtract
00567     // add all EDGEs and FACEs to temp_es1
00568     // get all EDGE entities
00569   SimpleArray<iBase_EntityHandle> gedges, gfaces, temp_gentities1;
00570   iGeom_getEntities( geom, ges_array[iBase_EDGE], iBase_EDGE, ARRAY_INOUT(gedges), &err );
00571   CHECK( "Failed to get gedge gentities in gentityset_test." );
00572 
00573     // add EDGEs to ges1
00574   iGeom_addEntArrToSet( geom, ARRAY_IN(gedges), temp_ges1, &err );
00575   CHECK( "Failed to add gedge gentities in gentityset_test." );
00576 
00577     // get all FACE gentities
00578   iGeom_getEntities( geom, ges_array[iBase_FACE], iBase_FACE, ARRAY_INOUT(gfaces), &err );
00579   CHECK( "Failed to get gface gentities in gentityset_test." );
00580 
00581     // add FACEs to es1
00582   iGeom_addEntArrToSet( geom, ARRAY_IN(gfaces), temp_ges1, &err );
00583   CHECK( "Failed to add gface gentities in gentityset_test." );
00584 
00585     // subtract EDGEs
00586   iGeom_subtract( geom, temp_ges1, ges_array[iBase_EDGE], &temp_ges1, &err );
00587   CHECK( "Failed to subtract gentitysets in gentityset_test." );
00588   
00589   iGeom_getEntities( geom, temp_ges1, iBase_FACE, ARRAY_INOUT(temp_gentities1), &err );
00590   CHECK( "Failed to get gface gentities in gentityset_test." );
00591 
00592   if (gfaces.size() != temp_gentities1.size()) {
00593     std::cerr << "Number of entitysets after subtraction not correct \
00594              in gentityset_test." << std::endl;
00595     return false;
00596   }
00597 
00598     // check there's nothing but gfaces in temp_ges1
00599   int num_gents;
00600   iGeom_getNumOfType( geom, temp_ges1, iBase_EDGE, &num_gents, &err );
00601   CHECK( "Failed to get dimensions of gentities in gentityset_test." );
00602   if (0 != num_gents) {
00603     std::cerr << "Subtraction failed to remove all edges" << std::endl;
00604     return false;
00605   }
00606 
00607     //------------Intersect------------
00608     //
00609 
00610     // clean out the temp_ges1
00611   iGeom_rmvEntArrFromSet( geom, ARRAY_IN(gfaces), temp_ges1, &err );
00612   CHECK( "Failed to remove gface gentities in gentityset_test." );
00613 
00614     // check if it is really cleaned out
00615   iGeom_getNumOfType( geom, temp_ges1, iBase_FACE, &num_gents, &err );
00616   CHECK( "Failed to get number of gentities by type in gentityset_test." );
00617 
00618   if (num_gents != 0) {
00619     std::cerr << "failed to remove correctly." << std::endl;
00620     return false;
00621   }
00622   
00623     // add EDGEs to temp ges1
00624   iGeom_addEntArrToSet( geom, ARRAY_IN(gedges), temp_ges1, &err );
00625   CHECK( "Failed to add gedge gentities in gentityset_test." );
00626 
00627     // add FACEs to temp ges1
00628   iGeom_addEntArrToSet( geom, ARRAY_IN(gfaces), temp_ges1, &err );
00629   CHECK( "Failed to add gface gentities in gentityset_test." );
00630 
00631     // intersect temp_ges1 with gedges set 
00632     // temp_ges1 entityset is altered
00633   iGeom_intersect( geom, temp_ges1, ges_array[iBase_EDGE], &temp_ges1, &err );
00634   CHECK( "Failed to intersect in gentityset_test." );
00635   
00636     // try to get FACEs, but there should be nothing but EDGE
00637   iGeom_getNumOfType( geom, temp_ges1, iBase_FACE, &num_gents, &err );
00638   CHECK( "Failed to get gface gentities in gentityset_test." );
00639 
00640   if (num_gents != 0) {
00641     std::cerr << "wrong number of gfaces." << std::endl;
00642     return false;
00643   }
00644 
00645 
00646     //-------------Unite--------------
00647 
00648     // get all regions
00649   iBase_EntitySetHandle temp_ges2;
00650   SimpleArray<iBase_EntityHandle> gregions;
00651 
00652   iGeom_createEntSet( geom, true, &temp_ges2, &err );
00653   CHECK( "Failed to create a temp gentityset in gentityset_test." );
00654   
00655   iGeom_getEntities( geom, ges_array[iBase_REGION], iBase_REGION, ARRAY_INOUT(gregions), &err );
00656   CHECK( "Failed to get gregion gentities in gentityset_test." );
00657   
00658     // add REGIONs to temp es2
00659   iGeom_addEntArrToSet( geom, ARRAY_IN(gregions), temp_ges2, &err );
00660   CHECK( "Failed to add gregion gentities in gentityset_test." );
00661 
00662     // unite temp_ges1 and temp_ges2
00663     // temp_ges1 gentityset is altered
00664   iGeom_unite( geom, temp_ges1, temp_ges2, &temp_ges1, &err );
00665   CHECK( "Failed to unite in gentityset_test." );
00666 
00667     // perform the check
00668   iGeom_getNumOfType( geom, temp_ges1, iBase_REGION, &num_gents, &err );
00669   CHECK( "Failed to get number of gregion gentities by type in gentityset_test." );
00670   
00671   if (num_gents != number_array[iBase_REGION]) {
00672     std::cerr << "different number of gregions in gentityset_test." << std::endl;
00673     return false;
00674   }
00675 
00676 
00677     //--------Test parent/child stuff in entiysets-----------
00678 
00679     // Add 2 sets as children to another
00680   iBase_EntitySetHandle parent_child;
00681   iGeom_createEntSet( geom, true, &parent_child, &err );
00682   CHECK( "Problem creating gentityset in gentityset_test." );
00683 
00684   iGeom_addPrntChld( geom, ges_array[iBase_VERTEX], parent_child, &err );
00685   CHECK( "Problem add parent in gentityset_test." );
00686 
00687     // check if parent is really added
00688   SimpleArray<iBase_EntitySetHandle> parents;
00689   iGeom_getPrnts( geom, parent_child, 1, ARRAY_INOUT(parents), &err );
00690   CHECK( "Problem getting parents in gentityset_test." );
00691 
00692   if (parents.size() != 1) {
00693     std::cerr << "number of parents is not correct in gentityset_test."
00694               << std::endl;
00695     return false;
00696   }
00697 
00698     // add parent and child
00699   //sidl::array<void*> parent_child_array = sidl::array<void*>::create1d(1);
00700   //int num_parent_child_array;
00701   //sidl::array<void*> temp_gedge_array = sidl::array<void*>::create1d(1);
00702   //int num_temp_gedge_array;
00703   //parent_child_array.set(0, parent_child);
00704   //temp_gedge_array.set(0, ges_array[TSTTG::EntityType_EDGE]);
00705   iGeom_addPrntChld( geom, ges_array[iBase_EDGE], parent_child, &err );
00706   CHECK( "Problem adding parent and child in gentityset_test." );
00707 
00708   //sidl::array<void*> temp_gface_array = sidl::array<void*>::create1d(1);
00709   //int num_temp_gface_array;
00710   //temp_gface_array.set(0, ges_array[TSTTG::EntityType_FACE]);
00711   iGeom_addPrntChld( geom, parent_child, ges_array[iBase_FACE], &err );
00712   CHECK( "Problem adding parent and child in gentityset_test." );
00713 
00714     // add child
00715   iGeom_addPrntChld( geom, parent_child, ges_array[iBase_REGION], &err );
00716   CHECK( "Problem adding child in gentityset_test." );
00717 
00718     // get the number of parent gentitysets
00719   num_gents = -1;
00720   iGeom_getNumPrnt( geom, parent_child, 0, &num_gents, &err );
00721   CHECK( "Problem getting number of parents in gentityset_test." );
00722 
00723   if (num_gents != 2) {
00724     std::cerr << "number of parents is not correct in gentityset_test."
00725               << std::endl;
00726     return false;
00727   }
00728 
00729     // get the number of child gentitysets
00730   num_gents = -1;
00731   iGeom_getNumChld( geom, parent_child, 0, &num_gents, &err );
00732   CHECK( "Problem getting number of children in gentityset_test." );
00733 
00734   if (num_gents != 2) {
00735     std::cerr << "number of children is not correct in gentityset_test."
00736               << std::endl;
00737     return false;
00738   }
00739 
00740   SimpleArray<iBase_EntitySetHandle> children;
00741   iGeom_getChldn( geom, parent_child, 0, ARRAY_INOUT(children), &err );
00742   CHECK( "Problem getting children in gentityset_test." );
00743 
00744   if (children.size() != 2) {
00745     std::cerr << "number of children is not correct in gentityset_test."
00746               << std::endl;
00747     return false;
00748   }
00749 
00750     // remove children
00751   iGeom_rmvPrntChld( geom, parent_child, ges_array[iBase_FACE], &err );
00752   CHECK( "Problem removing parent child in gentityset_test." );
00753 
00754     // get the number of child gentitysets
00755   iGeom_getNumChld( geom, parent_child, 0, &num_gents, &err );
00756   CHECK( "Problem getting number of children in gentityset_test." );
00757 
00758   if (num_gents != 1) {
00759     std::cerr << "number of children is not correct in gentityset_test."
00760               << std::endl;
00761     return false;
00762   }
00763 
00764     // parent_child and ges_array[TSTTG::EntityType_EDGE] should be related
00765   int result = 0;
00766   iGeom_isChildOf( geom, ges_array[iBase_EDGE], parent_child, &result, &err );
00767   CHECK( "Problem checking relation in gentityset_test." );
00768   if (!result) {
00769     std::cerr << "parent_child and ges_array[TSTTG::EntityType_EDGE] should be related" << std::endl;
00770     return false;
00771   }
00772 
00773     // ges_array[TSTTG::EntityType_FACE] and ges_array[TSTTG::REGION] are not related
00774   result = 2;
00775   iGeom_isChildOf( geom, ges_array[iBase_FACE], ges_array[iBase_REGION], &result, &err );
00776   if (result) {
00777     std::cerr << "ges_array[TSTTG::REGION] and ges_array[TSTTG::EntityType_FACE] should not be related" << std::endl;
00778     return false;
00779   }
00780   
00781 
00782     //--------test modify and query functions-----------------------------
00783   
00784     // check the number of gentity sets in whole mesh
00785   SimpleArray<iBase_EntitySetHandle> gentity_sets;
00786   iGeom_getEntSets( geom, root_set, 0, ARRAY_INOUT( gentity_sets ), &err );
00787   CHECK( "Problem to get all gentity sets in mesh." );
00788   
00789   if (gentity_sets.size() != all_sets + 8) {
00790     std::cerr << "the number of gentity sets in whole mesh should be 8 times of num_iter."
00791               << std::endl;
00792     return false;
00793   }
00794 
00795     // get all gentity sets in super set
00796   SimpleArray<iBase_EntitySetHandle> ges_array1;
00797   iGeom_getEntSets( geom, super_set, 0, ARRAY_INOUT( ges_array1 ), &err );
00798   CHECK( "Problem to get gentity sets in super set." );
00799 
00800     // get the number of gentity sets in super set
00801   int num_super;
00802   iGeom_getNumEntSets( geom, super_set, 0, &num_super, &err );
00803   CHECK( "Problem to get the number of all gentity sets in super set." );
00804   
00805     // the number of gentity sets in super set should be same
00806   if (num_super != ges_array1.size()) {
00807     std::cerr << "the number of gentity sets in super set should be same." << std::endl;
00808     return false;
00809   }
00810 
00811     // get all entities in super set
00812   SimpleArray<iBase_EntitySetHandle> all_gentities;
00813   iGeom_getEntSets( geom, super_set, 0, ARRAY_INOUT( all_gentities ), &err );
00814   CHECK( "Problem to get all gentities in super set." );
00815   
00816     // compare the number of all gentities in super set
00817   // HJK : num_hops is not implemented
00818   //if (num_all_gentities_super != ARRAY_SIZE(all_gentities)) {
00819   //std::cerr << "number of all gentities in super set should be same." << std::endl;
00820   //success = false;
00821   //}
00822 
00823   return true;
00824 }
00825   
00832 // make each topological entity vectors, check their topology
00833 // types, get interior and exterior faces of model
00834 bool topology_adjacencies_test(iGeom_Instance geom)
00835 {
00836   int i, err;
00837   iBase_EntitySetHandle root_set;
00838   iGeom_getRootSet( geom, &root_set, &err );
00839   CHECK( "ERROR : getRootSet failed!" );
00840 
00841   int top = iBase_VERTEX;
00842   int num_test_top = iBase_ALL_TYPES;
00843   std::vector< std::vector<iBase_EntityHandle> > gentity_vectors(num_test_top);
00844 
00845   // fill the vectors of each topology entities
00846   // like lines vector, polygon vector, triangle vector,
00847   // quadrilateral, polyhedrron, tet, hex, prism, pyramid,
00848   // septahedron vectors
00849   for (i = top; i < num_test_top; i++) {
00850     SimpleArray<iBase_EntityHandle> gentities;
00851     iGeom_getEntities( geom, root_set, i, ARRAY_INOUT( gentities ), &err );
00852     CHECK("Failed to get gentities in adjacencies_test.");
00853   
00854     gentity_vectors[i].resize( gentities.size() );
00855     std::copy( gentities.begin(), gentities.end(), gentity_vectors[i].begin() );
00856   }
00857 
00858   // check number of entities for each topology
00859   for (i = top; i < num_test_top; i++) {
00860     int num_tops = 0;
00861     iGeom_getNumOfType( geom, root_set, i, &num_tops, &err );
00862     CHECK( "Failed to get number of gentities in adjacencies_test." );
00863     
00864     if (static_cast<int>(gentity_vectors[i].size()) != num_tops) {
00865       std::cerr << "Number of gentities doesn't agree with number returned for dimension " 
00866                 << i << std::endl;
00867       return false;
00868     }
00869   }
00870 
00871   // check adjacencies in both directions
00872   std::vector<iBase_EntityHandle>::iterator vit;
00873   for (i = iBase_REGION; i >= iBase_VERTEX; i--) {
00874     for (vit = gentity_vectors[i].begin(); vit != gentity_vectors[i].end(); vit++) {
00875       iBase_EntityHandle this_gent = *vit;
00876 
00877         // check downward adjacencies
00878       for (int j = iBase_VERTEX; j < i; j++) {
00879 
00880         SimpleArray<iBase_EntityHandle> lower_ents;
00881         iGeom_getEntAdj( geom, this_gent, j, ARRAY_INOUT(lower_ents), &err );
00882         CHECK( "Bi-directional adjacencies test failed." );
00883 
00884           // for each of them, make sure they are adjacent to the upward ones
00885         int num_lower = lower_ents.size();
00886         for (int k = 0; k < num_lower; k++) {
00887           SimpleArray<iBase_EntityHandle> upper_ents;
00888           iGeom_getEntAdj( geom, lower_ents[k], i, ARRAY_INOUT(upper_ents), &err );
00889           CHECK( "Bi-directional adjacencies test failed." );
00890           if (std::find(upper_ents.begin(),upper_ents.end(), this_gent) ==
00891               upper_ents.end()) {
00892             std::cerr << "Didn't find lower-upper adjacency which was supposed to be there, dims = "
00893                  << i << ", " << j << std::endl;
00894             return false;
00895           }
00896         }
00897       }
00898     }
00899   }
00900 
00901   return true;
00902 }
00903 
00909 bool construct_test(iGeom_Instance geom)
00910 {
00911   int err;
00912   iBase_EntityHandle new_body = 0;
00913 
00914     // construct a cylinder, sweep it about an axis, and delete the result
00915   iBase_EntityHandle cyl = 0;
00916   iGeom_createCylinder( geom, 1.0, 1.0, 0.0, &cyl, &err );
00917     // Is the minor radius really supposed to be zero??? - JK
00918   CHECK( "Creating cylinder failed." );
00919   
00920     // move it onto the y axis
00921   iGeom_moveEnt( geom, cyl, 0.0, 1.0, -0.5, &err );
00922   CHECK( "Problems moving surface." );
00923 
00924       // get the surface with max z
00925   iBase_EntityHandle max_surf = 0;
00926   SimpleArray<iBase_EntityHandle> surfs;
00927   iGeom_getEntAdj( geom, cyl, iBase_FACE, ARRAY_INOUT(surfs), &err );
00928   CHECK( "Problems getting max surf for rotation." );
00929   
00930   SimpleArray<double> max_corn, min_corn;
00931   iGeom_getArrBoundBox( geom, ARRAY_IN(surfs), iBase_INTERLEAVED, 
00932                         ARRAY_INOUT( min_corn ),
00933                         ARRAY_INOUT( max_corn ),
00934                         &err );
00935   CHECK( "Problems getting max surf for rotation." );
00936   double dtol = 1.0e-6;
00937   for (int i = 0; i < surfs.size(); ++i) {
00938     if ((max_corn[3*i+2]) <= dtol && (max_corn[3*i+2]) >= -dtol &&
00939         (min_corn[3*i+2]) <= dtol && (min_corn[3*i+2]) >= -dtol) {
00940       max_surf = surfs[i];
00941       break;
00942     }
00943   }  
00944   
00945   if (0 == max_surf) {
00946     std::cerr << "Couldn't find max surf for rotation." << std::endl;
00947     return false;
00948   }
00949   
00950     // sweep it around the x axis
00951   iGeom_moveEnt( geom, cyl, 0.0, 1.0, 0.0, &err );
00952   CHECK( "Problems moving surface." );
00953 
00954   iGeom_sweepEntAboutAxis( geom, max_surf, 360.0, 1.0, 0.0, 0.0, &new_body, &err );
00955   CHECK( "Problems sweeping surface about axis." );
00956   
00957     // now delete
00958   iGeom_deleteEnt( geom, new_body, &err );
00959   CHECK( "Problems deleting cylinder or swept surface body." );
00960   
00961     // if we got here, we were successful
00962   return true;
00963 }
00964 
00965 static bool compare_box( const double* expected_min,
00966                          const double* expected_max,
00967                          const double* actual_min,
00968                          const double* actual_max )
00969 {
00970   bool same = true;
00971   double dtol = 1.0e-6;
00972   
00973   for (int i = 0; i < 3; ++i)
00974   {
00975     if (expected_min[i] < actual_min[i] - dtol || 
00976         expected_min[i]*10 >  actual_min[i] ||
00977         expected_max[i] > actual_max[i] + dtol ||
00978         expected_max[i]*10 < actual_max[i])  
00979       same = false;
00980   } 
00981   return same;
00982 }
00983 
00984 bool primitives_test(iGeom_Instance geom) 
00985 {
00986   int err;
00987   SimpleArray<iBase_EntityHandle> prims(3);
00988   iBase_EntityHandle prim;
00989  
00990   iGeom_createBrick( geom, 1.0, 2.0, 3.0, &prim, &err );
00991   CHECK( "createBrick failed." );
00992   prims[0] = prim;
00993   
00994   iGeom_createCylinder( geom, 1.0, 4.0, 2.0, &prim, &err );
00995   CHECK( "createCylinder failed." );
00996   prims[1] = prim;
00997   
00998   iGeom_createTorus( geom, 2.0, 1.0, &prim, &err );
00999   CHECK( "createTorus failed." );
01000   prims[2] = prim;
01001   
01002     // verify the bounding boxes forentities
01003   SimpleArray<double> max_corn, min_corn;
01004   iGeom_getArrBoundBox( geom, ARRAY_IN(prims), iBase_INTERLEAVED, 
01005                         ARRAY_INOUT(min_corn), ARRAY_INOUT(max_corn), &err );
01006 
01007   double preset_min_corn[] = 
01008       // min brick corner xyz
01009     {-0.5, -1.0, -1.5, 
01010       // min cyl corner xyz
01011      -4.0, -2.0, -0.5,
01012       // min torus corner xyz
01013      -3.0, -3.0, -1.0
01014     };
01015   
01016   double preset_max_corn[] = 
01017       // max brick corner xyz
01018     {0.5, 1.0, 1.5, 
01019       // max cyl corner xyz
01020      4.0, 2.0, 0.5,
01021       // max torus corner xyz
01022      3.0, 3.0, 1.0
01023     };
01024   
01025   if (!compare_box( preset_min_corn, preset_max_corn,
01026                     &min_corn[0], &max_corn[0] )) {
01027     std::cerr << "Box check failed for brick" << std::endl;
01028     return false;
01029   }
01030   
01031   if (!compare_box( preset_min_corn+3, preset_max_corn+3,
01032                     &min_corn[3], &max_corn[3] )) {
01033     std::cerr << "Box check failed for cylinder" << std::endl;
01034     return false;
01035   }
01036   
01037   if (!compare_box( preset_min_corn+6, preset_max_corn+6,
01038                     &min_corn[6], &max_corn[6] )) {
01039     std::cerr << "Box check failed for torus" << std::endl;
01040     return false;
01041   }
01042     // must have worked; delete the entities then return
01043   for (int i = 0; i < 3; ++i) {
01044     iGeom_deleteEnt( geom, prims[i], &err );
01045     CHECK( "Problems deleting primitive after boolean check." );
01046   }
01047   
01048   return true;
01049 }
01050   
01051 bool transforms_test(iGeom_Instance geom) 
01052 {
01053   int err;
01054   
01055     // construct a brick
01056   iBase_EntityHandle brick = 0;
01057   iGeom_createBrick( geom, 1.0, 2.0, 3.0, &brick, &err );
01058   CHECK( "Problems creating brick for transforms test." );
01059   
01060     // move it, then test bounding box
01061   iGeom_moveEnt( geom, brick, 0.5, 1.0, 1.5, &err );
01062   CHECK( "Problems moving brick for transforms test." );
01063   
01064   double bb_min[3], bb_max[3];
01065   iGeom_getEntBoundBox( geom, brick, bb_min, bb_min+1, bb_min+2, bb_max, bb_max+1, bb_max+2, &err );
01066   CHECK( "Problems getting bounding box after move." );
01067 
01068   double dtol = 1.0e-6;
01069   if ((bb_min[0]) >= dtol || (bb_min[0]) <= -dtol || 
01070       (bb_min[1]) >= dtol || (bb_min[1]) <= -dtol || 
01071       (bb_min[2]) >= dtol || (bb_min[2]) <= -dtol ||
01072       (bb_max[0]-1) >= dtol || 1-bb_max[0] >=dtol ||
01073       (bb_max[1]-2) >= dtol || 2 - bb_max[1] >=dtol||
01074       (bb_max[2]-3) >= dtol || 3 - bb_max[2] >= dtol) {
01075     std::cerr << "Wrong bounding box after move." << std::endl;
01076     return false;
01077   }
01078   
01079     // now rotate it about +x, then test bounding box
01080   iGeom_rotateEnt( geom, brick, 90, 1.0, 0.0, 0.0, &err );
01081   CHECK( "Problems rotating brick for transforms test." );
01082   
01083   iGeom_getEntBoundBox( geom, brick, bb_min, bb_min+1, bb_min+2, bb_max, bb_max+1, bb_max+2, &err );
01084   CHECK( "Problems getting bounding box after rotate." );
01085 
01086   if ((bb_min[0]) >= dtol || -bb_min[0] >= dtol ||
01087       (bb_min[1]+3) >= dtol || -(bb_min[1]+3) >= dtol ||
01088       (bb_min[2]) >= dtol || -(bb_min[2]) >= dtol ||
01089       (bb_max[0]-1) >= dtol || 1-bb_max[0] >= dtol ||
01090       (bb_max[1]) >= dtol || -(bb_max[1]) >= dtol ||
01091       (bb_max[2]-2) >= dtol || 2-bb_max[2] >=dtol) {
01092     std::cerr << "Wrong bounding box after rotate." << std::endl;
01093     return false;
01094   }
01095   
01096     // now reflect through y plane; should recover original bb
01097   iGeom_reflectEnt( geom, brick, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, &err );
01098   CHECK( "Problems reflecting brick for transforms test." );
01099   
01100   iGeom_getEntBoundBox( geom, brick, bb_min, bb_min+1, bb_min+2, bb_max, bb_max+1, bb_max+2, &err );
01101   CHECK( "Problems getting bounding box after reflect." );
01102   
01103   if ((bb_min[0]) >= dtol || -(bb_min[0]) >= dtol ||(bb_min[1]) >= dtol || 
01104       (bb_min[2]) >= dtol || -(bb_min[1]) >= dtol || -(bb_min[2]) >= dtol ||
01105       (bb_max[0]-1) >= dtol || 1- bb_max[0] >= dtol ||
01106       (bb_max[1]-3) >= dtol || 3 - bb_max[1] >= dtol ||
01107       (bb_max[2]-2) >= dtol || 2 - bb_max[2] >= dtol) {
01108     std::cerr << "Wrong bounding box after reflect." << std::endl;
01109     return false;
01110   }
01111 
01112     // must have worked; delete the entities then return
01113   iGeom_deleteEnt( geom, brick, &err );
01114   CHECK( "Problems deleting brick after transforms check." );
01115   return true;
01116 }
01117 
01118 
01119 bool booleans_test(iGeom_Instance geom) 
01120 {
01121   int err;
01122 
01123     // construct a brick size 1, and a cylinder rad 0.25 height 2
01124   iBase_EntityHandle brick = 0, cyl = 0;
01125   iGeom_createBrick( geom, 1.0, 0.0, 0.0, &brick, &err );
01126   CHECK( "Problems creating brick for booleans test." );
01127   iGeom_createCylinder( geom, 1.0, 0.25, 0.0, &cyl, &err );
01128   CHECK( "Problems creating cylinder for booleans test." );
01129 
01130     // subtract the cylinder from the brick
01131   iBase_EntityHandle subtract_result = 0;
01132   iGeom_subtractEnts( geom, brick, cyl, &subtract_result, &err );
01133   CHECK( "Problems subtracting for booleans subtract test." );
01134 
01135     // section the brick
01136   iBase_EntityHandle section_result = 0;
01137   iGeom_sectionEnt( geom, subtract_result, 1.0, 0.0, 0.0, 0.25, true, &section_result, &err );
01138   CHECK( "Problems sectioning for booleans section test." );
01139 
01140     // unite the section result with a new cylinder
01141   iGeom_createCylinder( geom, 1.0, 0.25, 0.0, &cyl, &err );
01142   CHECK( "Problems creating cylinder for unite test." );
01143   iBase_EntityHandle unite_results;
01144   iBase_EntityHandle unite_input[] = { section_result, cyl };
01145   iGeom_uniteEnts( geom, unite_input, 2, &unite_results, &err );
01146   CHECK( "Problems uniting for booleans unite test." );
01147   
01148   iGeom_deleteEnt( geom, unite_results, &err );
01149   CHECK( "Problems deleting for booleans unite test." );
01150   return true;
01151 }
01152 
01153 static int get_entities( iGeom_Instance geom, int entity_type,
01154                           std::vector<iBase_EntityHandle>& entities_out,
01155                           iBase_TagHandle id_tag = 0,
01156                           std::vector<int>* ids_out = 0 )
01157 {
01158   int err, num;
01159   iBase_EntitySetHandle root;
01160   iGeom_getRootSet( geom, &root, &err ); 
01161   if (iBase_SUCCESS != err)
01162     return err;
01163   iGeom_getNumOfType( geom, root, entity_type, &num, &err ); 
01164   if (iBase_SUCCESS != err)
01165     return err;
01166   
01167   entities_out.resize(num);
01168   int junk1 = entities_out.size(), junk2;
01169   iBase_EntityHandle* junk_ptr = &entities_out[0];;
01170   iGeom_getEntities( geom, root, entity_type, &junk_ptr, &junk1, &junk2, &err );
01171   if (iBase_SUCCESS != err)
01172     return err;
01173   assert( num == junk1 && num == junk2 );
01174   
01175   if (!ids_out)
01176     return iBase_SUCCESS;
01177   
01178   ids_out->resize(num);
01179   int* int_ptr = &(*ids_out)[0];
01180   iGeom_getIntArrData( geom, &entities_out[0], num, id_tag, &int_ptr, &junk1, &junk2, &err );
01181   if (iBase_SUCCESS != err)
01182     return err;
01183   assert( num == junk1 && num == junk2 );
01184   
01185   return iBase_SUCCESS;
01186 }
01187 
01188 //static int check_firmness( iGeom_Instance geom,
01189 //                           const std::vector<iBase_EntityHandle>& entities,
01190 //                           const std::vector<int>& ids,
01191 //                           iBase_TagHandle firmness_tag,
01192 //                           const char* expected_value,
01193 //                           const char* ent_type_str )
01194 //{
01195 //  const int firmness_size = 4;
01196 //  std::vector<char> firmness(firmness_size * entities.size());
01197 
01198 //  void* byte_ptr = &firmness[0];
01199 //  int err, junk1 = firmness.size(), junk2 = entities.size()*firmness_size;
01200 //  iGeom_getArrData( geom, &entities[0], entities.size(), firmness_tag, (void**)&byte_ptr, &junk1, &junk2, &err );
01201 //  if (iBase_SUCCESS != err)
01202 //    return err;
01203   
01204 //  bool all_correct = true;
01205 //  for (unsigned i = 0; i < entities.size(); ++i)
01206 //    if (std::string(&firmness[firmness_size*i],firmness_size) != expected_value)
01207 //      all_correct = false;
01208 //  if (!all_correct) {
01209 //    std::cout << "ERROR: Expected \"" << expected_value << "\" firmness "
01210 //              << "for all " << ent_type_str << "." << std::endl;
01211 //    std::cout << "ID  Actual  " << std::endl;
01212 //    for (unsigned i = 0; i < entities.size(); ++i)
01213 //      std::cout << std::setw(2) << ids[i] << "  "
01214 //                << std::string(&firmness[firmness_size*i],firmness_size)
01215 //                << std::endl;
01216 //    return iBase_FAILURE;
01217 //  }
01218   
01219 //  return iBase_SUCCESS;
01220 //}
01221 
01222 static int count_num_with_tag( iGeom_Instance geom,
01223                                const std::vector<iBase_EntityHandle>& ents,
01224                                iBase_TagHandle tag )
01225 {
01226   int err, bytes;
01227   iGeom_getTagSizeBytes( geom, tag, &bytes, &err );
01228   if (iBase_SUCCESS != err)
01229     return -1;
01230   std::vector<char> data(bytes);
01231   
01232   int success_count = 0;
01233   for (size_t i = 0; i < ents.size(); ++i) {
01234     void* ptr = &data[0];
01235     int junk1 = bytes, junk2;
01236     iGeom_getData( geom, ents[i], tag, (void**)&ptr, &junk1, &junk2, &err );
01237     if (iBase_TAG_NOT_FOUND == err)
01238       continue;
01239     if (iBase_SUCCESS != err)
01240       return -1;
01241     ++success_count;
01242   }
01243   
01244   return success_count;
01245 }
01246   
01247 
01248 bool mesh_size_test(iGeom_Instance geom)
01249 {
01250   const char* filename = STRINGIFY(SRCDIR) "/size.sat";
01251   int err;
01252   
01253   iGeom_deleteAll( geom, &err ); CHECK("");
01254   iGeom_load( geom, filename, 0, &err, strlen(filename), 0 );
01255   CHECK( "Failed to load input file: 'size.sat'" );
01256   
01257     // get tag handles
01258   iBase_TagHandle interval, size, firmness, id;
01259   iGeom_getTagHandle( geom, "MESH_INTERVAL", &interval, &err, strlen("MESH_INTERVAL") );
01260   CHECK( "iGeom_getTagHandle(\"MESH_INTERVAL\")" );
01261   iGeom_getTagHandle( geom, "MESH_SIZE", &size, &err, strlen("MESH_SIZE") );
01262   CHECK( "iGeom_getTagHandle(\"MESH_SIZE\")" );
01263   iGeom_getTagHandle( geom, "SIZE_FIRMNESS", &firmness, &err, strlen("SIZE_FIRMNESS") );
01264   CHECK( "iGeom_getTagHandle(\"SIZE_FIRMNESS\")" );
01265   iGeom_getTagHandle( geom, "GLOBAL_ID", &id, &err, strlen("GLOBAL_ID") );
01266   CHECK( "iGeom_getTagHandle(\"GLOBAL_ID\")" );
01267   
01268     // get entity lists
01269   std::vector<iBase_EntityHandle> verts, curves, surfs, vols;
01270   std::vector<int> vert_ids, curve_ids, surf_ids, vol_ids;
01271   err = get_entities( geom, iBase_VERTEX, verts,  id, &vert_ids  ); CHECK("");
01272   err = get_entities( geom, iBase_EDGE,   curves, id, &curve_ids ); CHECK("");
01273   err = get_entities( geom, iBase_FACE,   surfs,  id, &surf_ids  ); CHECK("");
01274   err = get_entities( geom, iBase_REGION, vols,   id, &vol_ids   ); CHECK("");
01275   
01276     // expect interval count to be the same as ID for every curve
01277 /* testgeom didn't pass with the following call, uncomment it when fixed.
01278   std::vector<int> intervals(curves.size());
01279   int *int_ptr = &intervals[0];
01280   junk1 = junk2 = curves.size();
01281   iGeom_getIntArrData( geom, &curves[0], curves.size(), interval, &int_ptr, &junk1, &junk2, &err ); 
01282   CHECK("Failed to get intervals for curves");
01283   if (intervals != curve_ids) {
01284     std::cout << "ERROR: Incorrect curve intervals for one or more curves." << std::endl;
01285     std::cout << "ID  Expected  Actual" << std::endl;
01286     for (unsigned i = 0; i < curves.size(); ++i)
01287       std::cout << std::setw(2) << curve_ids[i] << "  "
01288                 << std::setw(8) << curve_ids[i] << "  "
01289                 << std::setw(6) << intervals[i] << std::endl;
01290     result = false;
01291   }
01292   
01293     // expect size to be the same as ID for every surface
01294   std::vector<double> sizes(surfs.size());
01295   double* dbl_ptr = &sizes[0];
01296   junk1 = junk2 = surfs.size();
01297   iGeom_getDblArrData( geom, &surfs[0], surfs.size(), size, &dbl_ptr, &junk1, &junk2, &err ); 
01298   CHECK("Failed to get sizes for surfaces");
01299   bool all_correct = true;
01300   for (unsigned i = 0; i < surfs.size(); ++i)
01301     if (fabs(sizes[i] - (double)surf_ids[i] ) > 1e-8)
01302       all_correct = false;
01303   if (!all_correct) {
01304     std::cout << "ERROR: Incorrect mesh size for one or more surfaces." << std::endl;
01305     std::cout << "ID  Expected  Actual  " << std::endl;
01306     for (unsigned i = 0; i < surfs.size(); ++i)
01307       std::cout << std::setw(2) << surf_ids[i] << "  "
01308                 << std::setw(8) << (double)surf_ids[i] << "  "
01309                 << std::setw(8) << sizes[i] << std::endl;
01310     result = false;
01311   }
01312   
01313   
01314   err = result ? iBase_SUCCESS : iBase_FAILURE;
01315   CHECK("Invalid size or interval data");
01316     
01317     // expect "HARD" firmness on all curves
01318   err = check_firmness( geom, curves, curve_ids, firmness, "HARD", "curves" );
01319   CHECK("Invalid curve firmness");
01320     // expect "SOFT" firmness on all surfaces
01321   err = check_firmness( geom, surfs, surf_ids, firmness, "SOFT", "surfaces" );
01322   CHECK("Invalid surface firmness");
01323 */  
01324     // expect no firmnes on other entities
01325   err = count_num_with_tag( geom, verts, firmness ) ? iBase_FAILURE : iBase_SUCCESS;
01326   CHECK("Got firmness for vertex.");
01327   err = count_num_with_tag( geom, vols, firmness ) ? iBase_FAILURE : iBase_SUCCESS;
01328   CHECK("Got firmness for volume.");
01329 
01330     // expect no interval tag on any entities except curves
01331   err = count_num_with_tag( geom, verts, interval ) ? iBase_FAILURE : iBase_SUCCESS;
01332   CHECK("Got interval count for vertex.");
01333   err = count_num_with_tag( geom, vols, interval ) ? iBase_FAILURE : iBase_SUCCESS;
01334   CHECK("Got interval count for volume.");
01335 
01336     // expect no size tag on any entities except surfaces
01337     // curves should have size of one of their parent surfaces
01338   err = count_num_with_tag( geom, verts, size ) ? iBase_FAILURE : iBase_SUCCESS;
01339   CHECK("Got mesh size for vertex.");
01340   err = count_num_with_tag( geom, vols, size ) ? iBase_FAILURE : iBase_SUCCESS;
01341   CHECK("Got mesh size for volume.");
01342 
01343   return true;
01344 }
01345 
01346 bool faceting_test(iGeom_Instance geom)
01347 {
01348 
01349   const char* filename = STRINGIFY(SRCDIR) "/size.step";
01350 
01351   int err;
01352   double dist_tolerance=0.001;
01353   
01354   iGeom_deleteAll( geom, &err ); CHECK("");
01355   iGeom_load( geom, filename, 0, &err, strlen(filename), 0 );
01356   CHECK( "Failed to load input file: 'size.step'" );
01357 
01358   // get entity lists
01359   iBase_TagHandle id;
01360   iGeom_getTagHandle( geom, "GLOBAL_ID", &id, &err, strlen("GLOBAL_ID") );
01361   CHECK( "iGeom_getTagHandle(\"GLOBAL_ID\")" );
01362   std::vector<iBase_EntityHandle> verts, curves, surfs, vols;
01363   std::vector<int> vert_ids, curve_ids, surf_ids, vol_ids;
01364   err = get_entities( geom, iBase_VERTEX, verts,  id, &vert_ids  ); CHECK("");
01365   err = get_entities( geom, iBase_EDGE,   curves, id, &curve_ids ); CHECK("");
01366   err = get_entities( geom, iBase_FACE,   surfs,  id, &surf_ids  ); CHECK("");
01367   err = get_entities( geom, iBase_REGION, vols,   id, &vol_ids   ); CHECK("");
01368   
01369   unsigned int ent;
01370   std::vector<double> points;
01371   double *p_ptr = &points[0];
01372   int p_size = 0, p_alloc=points.size();
01373   std::vector<int> facets;
01374   int *f_ptr = &facets[0];
01375   int f_size = 0, f_alloc=facets.size();
01376 
01377   for (ent=0; ent < verts.size(); ent++)
01378     {
01379       iGeom_getFacets(geom, verts[ent], dist_tolerance, 
01380               &p_ptr, &p_alloc, &p_size, 
01381               &f_ptr, &f_alloc, &f_size, 
01382               &err);
01383       if (iBase_INVALID_ENTITY_TYPE != err)
01384     {
01385       err = iBase_FAILURE;
01386       CHECK("getFacets should fail for vertices.");
01387     }
01388     }
01389    for (ent=0; ent < curves.size(); ent++)
01390     {
01391       iGeom_getFacets(geom, curves[ent], dist_tolerance, 
01392               &p_ptr, &p_alloc, &p_size, 
01393               &f_ptr, &f_alloc, &f_size, 
01394               &err);
01395       points.resize(p_size);
01396       facets.resize(f_size);
01397 
01398       if ( iBase_BAD_ARRAY_DIMENSION == err || iBase_BAD_ARRAY_SIZE == err)
01399     {
01400       p_ptr = &points[0];
01401       p_alloc = points.size();
01402       f_ptr = &facets[0];
01403       f_alloc = facets.size();
01404       iGeom_getFacets(geom, curves[ent], dist_tolerance, 
01405               &p_ptr, &p_alloc, &p_size, 
01406               &f_ptr, &f_alloc, &f_size, 
01407               &err);
01408     }
01409 
01410       CHECK("getFacets failed for a curve.");
01411 
01412       err = (6 != p_size); CHECK("wrong number of points in curve");
01413       err = (0 != f_size); CHECK("Non-zero number of facets in curve");
01414 
01415       // should check for known sizes and values of these facets & points
01416     }
01417    for (ent=0; ent < surfs.size(); ent++)
01418     {
01419       iGeom_getFacets(geom, surfs[ent], dist_tolerance, 
01420               &p_ptr, &p_alloc, &p_size, 
01421               &f_ptr, &f_alloc, &f_size, 
01422               &err);
01423       points.resize(p_size);
01424       facets.resize(f_size);
01425 
01426       if ( iBase_BAD_ARRAY_DIMENSION == err || iBase_BAD_ARRAY_SIZE == err)
01427     {
01428       p_ptr = &points[0];
01429       p_alloc = points.size();
01430       f_ptr = &facets[0];
01431       f_alloc = facets.size();
01432       iGeom_getFacets(geom, surfs[ent], dist_tolerance, 
01433               &p_ptr, &p_alloc, &p_size, 
01434               &f_ptr, &f_alloc, &f_size, 
01435               &err);
01436     }
01437       std::cerr << "surface " << ent << ": " << p_size << " points and " << f_size << " facets." << std::endl;
01438       CHECK("getFacets failed for a surface.");
01439 
01440       err = (12 != p_size); CHECK("wrong number of points in curve");
01441       err = (6 != f_size); CHECK("wrong number of facets in curve");
01442       // should check for known sizes and values of these facets & points
01443     }
01444   for (ent=0; ent < vols.size(); ent++)
01445     {
01446       iGeom_getFacets(geom, vols[ent], dist_tolerance, 
01447               &p_ptr, &p_alloc, &p_size, 
01448               &f_ptr, &f_alloc, &f_size, 
01449               &err);
01450       if (iBase_INVALID_ENTITY_TYPE != err)
01451     {
01452       err = iBase_FAILURE;
01453       CHECK("getFacets should fail for volumes.");
01454     }
01455     }
01456     
01457    return true;
01458 }
01459 
01460 bool shutdown_test(iGeom_Instance geom, std::string &engine_opt) 
01461 {
01462   int err;
01463 
01464     // test shutdown & startup of interface
01465   iGeom_dtor(geom, &err);
01466   CHECK( "Interface destruction didn't work properly." );
01467   
01468   iGeom_newGeom(engine_opt.c_str(), &geom, &err, engine_opt.length());
01469   CHECK( "Interface re-construction didn't work properly." );
01470   
01471   iGeom_dtor(geom, &err);
01472   CHECK( "2nd Interface destruction didn't work properly." );
01473   
01474   return true;
01475 }
01476 
01477 bool save_entset_test(iGeom_Instance geom) 
01478 {
01479   int err;
01480 
01481   std::string filename = "testout.brep";
01482 
01483     // initialize number of ents and sets to compare with later
01484   int num_ents_bef, num_sets_bef;
01485   iBase_EntitySetHandle root;
01486   iGeom_getRootSet( geom, &root, &err ); 
01487   CHECK("Failed to get root set.");
01488   iGeom_getNumEntSets(geom, root, 0, &num_sets_bef, &err);
01489   CHECK("Failed to get number of ent sets.");
01490   iGeom_getNumOfType(geom, root, iBase_REGION, &num_ents_bef, &err);
01491   CHECK("Failed to get number of entities.");
01492 
01493     // create set, and entity to add to set
01494   iBase_EntityHandle cyl;
01495   iGeom_createCylinder( geom, 1.0, 0.25, 0.0, &cyl, &err );
01496   CHECK( "Problems creating cylinder for save entset test." );
01497   iBase_EntitySetHandle seth;
01498   iGeom_createEntSet(geom, true, &seth, &err);
01499   CHECK( "Problems creating entity set for save entset test." );
01500 
01501     // add the entity
01502   iGeom_addEntToSet(geom, cyl, seth, &err);
01503   CHECK( "Problems adding entity to set for save entset test." );
01504 
01505     // save/restore the model, and see if the entity is there
01506   iGeom_save(geom, filename.c_str(), NULL, &err, filename.length(), 0);
01507   CHECK( "Problems saving file for save entset test." );
01508 
01509   iGeom_destroyEntSet(geom, seth, &err);
01510   CHECK("Failed to destroy entity set.");
01511   iGeom_deleteEnt(geom, cyl, &err);
01512   CHECK("Failed to destroy entity.");
01513   
01514     // read the file back in
01515   iGeom_load(geom, filename.c_str(), NULL, &err,
01516              filename.length(), 0);
01517   CHECK( "Problems reading file for save entset test." );
01518 
01519     // check number of sets and entities
01520   int num_ents_aft, num_sets_aft;
01521   iGeom_getNumEntSets(geom, root, 0, &num_sets_aft, &err);
01522   CHECK("Failed to get number of ent sets.");
01523   iGeom_getNumOfType(geom, root, iBase_REGION, &num_ents_aft, &err);
01524   CHECK("Failed to get number of entities.");
01525   bool success = true;
01526   if (num_ents_aft != 2*num_ents_bef + 1) {
01527     print_error("Failed to get the right number of entities.",
01528                 iBase_FAILURE, geom, __FILE__, __LINE__);
01529     success = false;
01530   }
01531   else if (num_sets_aft != 2*num_sets_bef + 1) {
01532     print_error("Failed to get the right number of entity sets.",
01533                 iBase_FAILURE, geom, __FILE__, __LINE__);
01534     success = false;
01535   }
01536 
01537     // otherwise, we succeeded
01538   return success;
01539 }
01540 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines