LCOV - code coverage report
Current view: top level - geom - SurfaceOverlapTool.cpp (source / functions) Hit Total Coverage
Test: coverage_sk.info Lines: 16 1304 1.2 %
Date: 2020-06-30 00:58:45 Functions: 3 46 6.5 %
Branches: 2 3226 0.1 %

           Branch data     Line data    Source code
       1                 :            : //- Class: SurfaceOverlapTool
       2                 :            : //- Description: Utilities to debug imprinting/merging problems
       3                 :            : //- Owner: Steve Storm
       4                 :            : //- Created: 22 October 1999
       5                 :            : //- Overhauled: January 2003
       6                 :            : 
       7                 :            : #include "RefEntityFactory.hpp"
       8                 :            : #include "SurfaceOverlapTool.hpp"
       9                 :            : #include "RefVertex.hpp"
      10                 :            : #include "RefEdge.hpp"
      11                 :            : #include "RefFace.hpp"
      12                 :            : #include "Body.hpp"
      13                 :            : #include "CoEdge.hpp"
      14                 :            : #include "BodySM.hpp"
      15                 :            : #include "RefGroup.hpp"
      16                 :            : #include "GeometryModifyTool.hpp"
      17                 :            : #include "GeometryQueryTool.hpp"
      18                 :            : #include "TopologyEntity.hpp"
      19                 :            : #include "Surface.hpp"
      20                 :            : #include "Curve.hpp"
      21                 :            : 
      22                 :            : #include "CubitBox.hpp"
      23                 :            : #include "CubitUtil.hpp"
      24                 :            : 
      25                 :            : #include "DLIList.hpp"
      26                 :            : #include "ProgressTool.hpp"
      27                 :            : #include "AppUtil.hpp"
      28                 :            : #include "SurfaceOverlapFacet.hpp"
      29                 :            : #include "CurveOverlapFacet.hpp"
      30                 :            : #include "TDSurfaceOverlap.hpp"
      31                 :            : #include "RTree.hpp"
      32                 :            : #include "AbstractTree.hpp"
      33                 :            : 
      34                 :            : #include "GMem.hpp"
      35                 :            : #include "SettingHandler.hpp"
      36                 :            : 
      37                 :            : #include "GfxPreview.hpp"
      38                 :            : #include "GfxDebug.hpp"
      39                 :            : #include "CpuTimer.hpp"
      40                 :            : 
      41                 :            : #define NO_FACETS_FOR_ABSTRACTTREE 10
      42                 :            : 
      43                 :            : SurfaceOverlapTool* SurfaceOverlapTool::instance_ = 0;
      44                 :            : double SurfaceOverlapTool::gapMin = 0.0;
      45                 :            : double SurfaceOverlapTool::gapMax = 0.01;
      46                 :            : double SurfaceOverlapTool::angleMin = 0.0;
      47                 :            : double SurfaceOverlapTool::angleMax = 5.0;
      48                 :            : int SurfaceOverlapTool::normalType = 1; // 1=any, 2=opposite, 3=same
      49                 :            : double SurfaceOverlapTool::overlapTolerance = .001;
      50                 :            : CubitBoolean SurfaceOverlapTool::groupResults = CUBIT_TRUE;
      51                 :            : CubitBoolean SurfaceOverlapTool::listPairs = CUBIT_TRUE;
      52                 :            : CubitBoolean SurfaceOverlapTool::displayPairs = CUBIT_TRUE;
      53                 :            : CubitBoolean SurfaceOverlapTool::imprintResults = CUBIT_FALSE;
      54                 :            : double SurfaceOverlapTool::facetAbsTol = 0.0; // Use default
      55                 :            : unsigned short SurfaceOverlapTool::facetAngTol = 15; // Seems to work pretty good
      56                 :            : CubitBoolean SurfaceOverlapTool::checkWithinBodies = CUBIT_FALSE;
      57                 :            : CubitBoolean SurfaceOverlapTool::checkAcrossBodies = CUBIT_TRUE;
      58                 :            : bool SurfaceOverlapTool::skipFacingSurfaces = CUBIT_FALSE; // skip the pair if the normals pass through eachother (normalType must == 2)
      59                 :            : 
      60                 :            : // Constructor
      61                 :          0 : SurfaceOverlapTool* SurfaceOverlapTool::instance()
      62                 :            : {
      63         [ #  # ]:          0 :    if( instance_ == NULL )
      64         [ #  # ]:          0 :        instance_ = new SurfaceOverlapTool;
      65                 :          0 :    return instance_;
      66                 :            : }
      67                 :            : 
      68                 :          0 : SurfaceOverlapTool::SurfaceOverlapTool()
      69                 :            : {
      70                 :          0 :   facetAbsTol = 0.0; // Use default
      71                 :          0 :   facetAngTol = 10;  // Seems to work pretty good
      72                 :          0 :   gapMin = 0.0;
      73                 :          0 :   gapMax = 0.01;
      74                 :          0 :   angleMin = 0.0;
      75                 :          0 :   angleMax = 5.0;
      76                 :          0 :   normalType = 1; // 1=any, 2=opposite, 3=same
      77                 :          0 :   groupResults = CUBIT_TRUE;
      78                 :          0 :   listPairs = CUBIT_TRUE;
      79                 :          0 :   displayPairs = CUBIT_TRUE;
      80                 :          0 :   overlapTolerance = .001;
      81                 :          0 :   imprintResults = CUBIT_FALSE;
      82                 :          0 :   checkWithinBodies = CUBIT_FALSE;
      83                 :          0 :   checkAcrossBodies = CUBIT_TRUE;
      84                 :          0 :   skipFacingSurfaces = CUBIT_FALSE;
      85                 :          0 : }
      86                 :            : 
      87                 :            : // Destructor
      88                 :          0 : SurfaceOverlapTool::~SurfaceOverlapTool()
      89                 :            : {
      90                 :          0 : }
      91                 :            : 
      92                 :          0 : CubitBoolean SurfaceOverlapTool::draw_overlapping_surface_pair( RefFace *ref_face_1,
      93                 :            :                                                         RefFace *ref_face_2)
      94                 :            : {
      95                 :          0 :   CubitBoolean abort = CUBIT_FALSE;
      96                 :          0 :   CubitBoolean draw_overlap = CUBIT_TRUE;
      97                 :            :   CubitBoolean overlap = check_overlap( ref_face_1, ref_face_2, 
      98                 :          0 :                                         abort, draw_overlap );
      99                 :            : 
     100                 :          0 :   return overlap;
     101                 :            : }
     102                 :            : 
     103                 :            : CubitStatus 
     104                 :          0 : SurfaceOverlapTool::find_overlapping_surfaces( DLIList<RefFace*> &ref_face_list, 
     105                 :            :                                                DLIList<RefEntity*> &faces_to_draw,
     106                 :            :                                                bool filter_slivers)
     107                 :            : {
     108 [ #  # ][ #  # ]:          0 :    DLIList<RefFace*> list1, list2;
                 [ #  # ]
     109                 :            :    return find_overlapping_surfaces( ref_face_list, list1, list2, faces_to_draw, CUBIT_TRUE,
     110 [ #  # ][ #  # ]:          0 :      filter_slivers);
     111                 :            : }
     112                 :            : 
     113                 :            : CubitStatus
     114                 :          0 : SurfaceOverlapTool::find_overlapping_surfaces( DLIList<Body*> &body_list,
     115                 :            :                                                DLIList<RefEntity*> &faces_to_draw,
     116                 :            :                                                bool filter_slivers)
     117                 :            : {
     118 [ #  # ][ #  # ]:          0 :   DLIList<RefFace*> list1, list2;
                 [ #  # ]
     119                 :            :   return find_overlapping_surfaces( body_list, list1, list2, faces_to_draw, CUBIT_TRUE,
     120 [ #  # ][ #  # ]:          0 :     filter_slivers);
     121                 :            : }
     122                 :            : 
     123                 :            : 
     124                 :            : CubitStatus
     125                 :          0 : SurfaceOverlapTool::find_candidate_surfaces_for_imprinting( DLIList<BodySM*> &body_list,
     126                 :            :                                               DLIList<Surface*> &surface_list1,
     127                 :            :                                               DLIList<Surface*> &surface_list2,
     128                 :            :                                               double overlap_tol,
     129                 :            :                                               bool filter_slivers)
     130                 :            : {
     131                 :            :   //collect all the surfaces
     132         [ #  # ]:          0 :   DLIList<Surface*> surface_list;
     133                 :            :   int i;
     134 [ #  # ][ #  # ]:          0 :   for( i=body_list.size(); i--; )
     135                 :            :   {
     136         [ #  # ]:          0 :     BodySM *body_sm = body_list.get_and_step();
     137         [ #  # ]:          0 :     DLIList<Surface*> surfs;
     138         [ #  # ]:          0 :     body_sm->surfaces_ignore_virtual( surfs, false );
     139                 :            : //    body_sm->surfaces( surfs );
     140         [ #  # ]:          0 :     surface_list.merge_unique( surfs );
     141         [ #  # ]:          0 :   }
     142                 :            : 
     143         [ #  # ]:          0 :   double tolerance = GeometryQueryTool::get_geometry_factor()*GEOMETRY_RESABS;
     144         [ #  # ]:          0 :   if(overlap_tol > 0.0)
     145                 :          0 :     tolerance = overlap_tol;
     146                 :            : 
     147                 :            :   // Populate the Surface AbstractTree
     148 [ #  # ][ #  # ]:          0 :   AbstractTree<Surface*> *a_tree = new RTree<Surface*>( tolerance );
     149         [ #  # ]:          0 :   surface_list.reset();
     150 [ #  # ][ #  # ]:          0 :   for( i=surface_list.size(); i--; )
     151                 :            :   {
     152         [ #  # ]:          0 :     Surface *surface = surface_list.get_and_step();
     153         [ #  # ]:          0 :     a_tree->add( surface );
     154                 :            :   }
     155                 :            : 
     156 [ #  # ][ #  # ]:          0 :   std::map<Surface*, DLIList<SurfaceOverlapFacet*>* > surface_facet_map;
     157 [ #  # ][ #  # ]:          0 :   std::map<Surface*, double > surface_to_area_map;
     158 [ #  # ][ #  # ]:          0 :   std::map<Surface*, AbstractTree<SurfaceOverlapFacet*>* > a_tree_map;
     159                 :            : 
     160         [ #  # ]:          0 :   surface_list.reset();
     161 [ #  # ][ #  # ]:          0 :   for( i=surface_list.size(); i--; )
     162                 :            :   {
     163         [ #  # ]:          0 :     Surface *surf1 = surface_list.get_and_step();
     164                 :            : 
     165         [ #  # ]:          0 :     BodySM *surf1_body = surf1->bodysm();
     166                 :            : 
     167                 :            :     // Remove this surface from AbstractTree so it is not found and never
     168                 :            :     // found again
     169         [ #  # ]:          0 :     a_tree->remove( surf1 );
     170                 :            : 
     171                 :            :     // Find RefFaces from AbstractTree that are within range of this surface
     172         [ #  # ]:          0 :     CubitBox surf1_box = surf1->bounding_box();
     173 [ #  # ][ #  # ]:          0 :     DLIList<Surface*> close_surfaces;
     174         [ #  # ]:          0 :     a_tree->find( surf1_box, close_surfaces );
     175                 :            : 
     176                 :            :     int j;
     177 [ #  # ][ #  # ]:          0 :     for( j=close_surfaces.size(); j--; )
     178                 :            :     {
     179         [ #  # ]:          0 :       Surface *surf2 = close_surfaces.get_and_step();
     180         [ #  # ]:          0 :       BodySM *surf2_body = surf2->bodysm();
     181                 :            : 
     182                 :            :       //don't check for overlapping surfaces within bodies
     183         [ #  # ]:          0 :       if( surf1_body == surf2_body )
     184                 :          0 :         continue;
     185                 :            :       
     186                 :            :       // check for overlap, boundary contact, int contact, penetration, etc.
     187 [ #  # ][ #  # ]:          0 :       if( check_surfs_for_imprinting( surf1, surf2, 
     188                 :            :                          &surface_facet_map, 
     189                 :            :                          &surface_to_area_map, 
     190                 :            :                          &a_tree_map,
     191                 :          0 :                          overlap_tol ) == CUBIT_TRUE )
     192                 :            :       {
     193         [ #  # ]:          0 :         surface_list1.append( surf1 );
     194         [ #  # ]:          0 :         surface_list2.append( surf2 );
     195                 :            :       }
     196                 :            : 
     197                 :            :     }
     198         [ #  # ]:          0 :   }
     199                 :            : 
     200                 :            :   //clean up maps;
     201         [ #  # ]:          0 :   std::map<Surface*, AbstractTree<SurfaceOverlapFacet*>* >::iterator tree_iter; 
     202         [ #  # ]:          0 :   tree_iter = a_tree_map.begin();
     203 [ #  # ][ #  # ]:          0 :   for(; tree_iter != a_tree_map.end(); tree_iter++ )
         [ #  # ][ #  # ]
     204 [ #  # ][ #  # ]:          0 :     delete tree_iter->second;
                 [ #  # ]
     205                 :            : 
     206         [ #  # ]:          0 :   std::map<Surface*, DLIList<SurfaceOverlapFacet*>* >::iterator sof_iter; 
     207         [ #  # ]:          0 :   sof_iter = surface_facet_map.begin();
     208 [ #  # ][ #  # ]:          0 :   for(; sof_iter != surface_facet_map.end(); sof_iter++)
         [ #  # ][ #  # ]
     209                 :            :   {
     210         [ #  # ]:          0 :     DLIList<SurfaceOverlapFacet*> *tmp_list = sof_iter->second;
     211                 :            : 
     212                 :            :     //delete contents of list
     213 [ #  # ][ #  # ]:          0 :     for( i=tmp_list->size(); i--; )
     214 [ #  # ][ #  # ]:          0 :       delete tmp_list->get_and_step();
                 [ #  # ]
     215                 :            : 
     216                 :            :     //delete the list
     217 [ #  # ][ #  # ]:          0 :     delete tmp_list;
     218                 :            :   }
     219                 :            : 
     220 [ #  # ][ #  # ]:          0 :   delete a_tree;
     221                 :            : 
     222         [ #  # ]:          0 :   return CUBIT_SUCCESS;
     223                 :            : }
     224                 :            : 
     225                 :            : CubitStatus
     226                 :          0 : SurfaceOverlapTool::find_overlapping_surfaces( DLIList<Body*> &body_list,
     227                 :            :                                               DLIList<RefFace*> &ref_face_list1,
     228                 :            :                                               DLIList<RefFace*> &ref_face_list2,
     229                 :            :                                               DLIList<RefEntity*> &faces_to_draw,
     230                 :            :                                               CubitBoolean show_messages,
     231                 :            :                                               bool filter_slivers)
     232                 :            : {
     233                 :          0 :   CubitStatus status = CUBIT_SUCCESS;
     234                 :            : 
     235                 :          0 :   CubitBoolean group_results = CUBIT_FALSE;
     236                 :          0 :   CubitBoolean list_pairs = CUBIT_FALSE;
     237                 :          0 :   CubitBoolean imprint_results = CUBIT_FALSE;
     238                 :            : 
     239         [ #  # ]:          0 :   if( show_messages == CUBIT_TRUE )
     240                 :            :   {
     241                 :          0 :      group_results = groupResults;
     242                 :          0 :      list_pairs = listPairs;
     243                 :          0 :      imprint_results = imprintResults;
     244                 :            :   }
     245                 :            : 
     246                 :            :   // Handle the special case of finding overlapping surfaces within a given 
     247                 :            :   // body - we can do this MUCH faster than the general case (this is a 
     248                 :            :   // rare case in general but at Cat we have an application for this!).
     249                 :            : 
     250                 :            :   // The usual case
     251 [ #  # ][ #  # ]:          0 :   if( checkWithinBodies == CUBIT_FALSE ||
     252         [ #  # ]:          0 :       (checkWithinBodies == CUBIT_TRUE && checkAcrossBodies == CUBIT_TRUE) )
     253                 :            :   {
     254                 :            :     // Utilize a straight surface list
     255         [ #  # ]:          0 :     DLIList<RefFace*> ref_face_list;
     256         [ #  # ]:          0 :     body_list.reset();
     257                 :            :     int i;
     258 [ #  # ][ #  # ]:          0 :     for( i=body_list.size(); i--; )
     259                 :            :     {
     260         [ #  # ]:          0 :       Body* body_ptr = body_list.get_and_step();
     261         [ #  # ]:          0 :       DLIList<RefFace*> body_face_list;
     262         [ #  # ]:          0 :       body_ptr->ref_faces( body_face_list );
     263         [ #  # ]:          0 :       ref_face_list.merge_unique( body_face_list );
     264         [ #  # ]:          0 :     }
     265                 :            : 
     266                 :          0 :     int prog_step = 0;
     267         [ #  # ]:          0 :     if( show_messages )
     268                 :            :     {
     269                 :          0 :       prog_step = 10;
     270 [ #  # ][ #  # ]:          0 :       PRINT_INFO( "Finding surface overlap...\n" );
         [ #  # ][ #  # ]
     271 [ #  # ][ #  # ]:          0 :       if( ref_face_list.size() > prog_step )
     272                 :            :       {
     273                 :            :         char message[128];
     274         [ #  # ]:          0 :         sprintf(message, "Finding Surface Overlap On %d Surfaces", ref_face_list.size() );
     275 [ #  # ][ #  # ]:          0 :         AppUtil::instance()->progress_tool()->start(0, ref_face_list.size(),
     276 [ #  # ][ #  # ]:          0 :           "Progress", message, TRUE, TRUE );
     277                 :            :       }
     278                 :            :     }
     279                 :            : 
     280                 :            :     status = find_overlapping_surfaces( ref_face_list, ref_face_list1,
     281         [ #  # ]:          0 :       ref_face_list2, faces_to_draw, list_pairs, prog_step, filter_slivers );
     282                 :            : 
     283 [ #  # ][ #  # ]:          0 :     if( show_messages && ref_face_list.size() > prog_step )
         [ #  # ][ #  # ]
     284 [ #  # ][ #  # ]:          0 :       AppUtil::instance()->progress_tool()->end();
         [ #  # ][ #  # ]
     285                 :            :   }
     286                 :            : 
     287                 :            :   // Special case - checking within bodies
     288                 :            :   else
     289                 :            :   {
     290                 :          0 :     int prog_step = 5;
     291         [ #  # ]:          0 :     if( show_messages )
     292                 :            :     {
     293 [ #  # ][ #  # ]:          0 :       PRINT_INFO( "Finding surface overlap...\n" );
     294         [ #  # ]:          0 :       if( body_list.size() > prog_step )
     295                 :            :       {
     296                 :            :         char message[128];
     297         [ #  # ]:          0 :         sprintf(message, "Finding Surface Overlap On %d Bodies", body_list.size() );
     298 [ #  # ][ #  # ]:          0 :         AppUtil::instance()->progress_tool()->start(0, body_list.size(),
     299 [ #  # ][ #  # ]:          0 :           "Progress", message, TRUE, TRUE );
     300                 :            :       }
     301                 :            :     }
     302                 :            : 
     303                 :            :     int i;
     304                 :          0 :     body_list.reset();
     305         [ #  # ]:          0 :     for( i=body_list.size(); i--; )
     306                 :            :     {
     307         [ #  # ]:          0 :       Body* body_ptr = body_list.get_and_step();
     308         [ #  # ]:          0 :       DLIList<RefFace*> body_face_list;
     309         [ #  # ]:          0 :       body_ptr->ref_faces( body_face_list );
     310                 :            : 
     311                 :            :       status = find_overlapping_surfaces( body_face_list, ref_face_list1,
     312         [ #  # ]:          0 :         ref_face_list2, faces_to_draw, list_pairs, -1, filter_slivers );
     313                 :            : 
     314 [ #  # ][ #  # ]:          0 :       if( show_messages && body_list.size() > prog_step )
         [ #  # ][ #  # ]
     315 [ #  # ][ #  # ]:          0 :         AppUtil::instance()->progress_tool()->step();
                 [ #  # ]
     316                 :            : 
     317         [ #  # ]:          0 :       if( status == CUBIT_FAILURE )
     318 [ #  # ][ #  # ]:          0 :         break;
     319                 :          0 :     }
     320                 :            : 
     321 [ #  # ][ #  # ]:          0 :     if( show_messages && body_list.size() > prog_step )
                 [ #  # ]
     322                 :          0 :         AppUtil::instance()->progress_tool()->end();
     323                 :            :   }
     324                 :            : 
     325         [ #  # ]:          0 :   if( faces_to_draw.size() )
     326                 :            :   {
     327         [ #  # ]:          0 :     if( group_results == CUBIT_TRUE )
     328                 :            :     {
     329 [ #  # ][ #  # ]:          0 :       RefGroup *new_group = RefEntityFactory::instance()->construct_RefGroup( "surf_overlap" );
     330         [ #  # ]:          0 :       new_group->add_ref_entity( faces_to_draw );
     331         [ #  # ]:          0 :       CubitString name = new_group->entity_name();
     332 [ #  # ][ #  # ]:          0 :       PRINT_INFO( "Found %d overlapping surface pairs (added to group '%s')\n", 
         [ #  # ][ #  # ]
                 [ #  # ]
     333 [ #  # ][ #  # ]:          0 :         ref_face_list1.size(), name.c_str() );
     334                 :            :     }
     335         [ #  # ]:          0 :     else if( show_messages )
     336 [ #  # ][ #  # ]:          0 :       PRINT_INFO( "Found %d overlapping surface pairs\n", ref_face_list1.size() );
     337                 :            : 
     338         [ #  # ]:          0 :     if ( imprint_results )
     339                 :            :     {
     340                 :          0 :       CubitStatus stat = imprint(ref_face_list1, ref_face_list2);
     341         [ #  # ]:          0 :       if ( stat != CUBIT_SUCCESS )
     342                 :            :       {
     343 [ #  # ][ #  # ]:          0 :         PRINT_WARNING("Imprinting overlaps was unsuccessful\n");
     344                 :            :       }
     345                 :            :     }
     346                 :            :   }
     347         [ #  # ]:          0 :   else if (show_messages )
     348 [ #  # ][ #  # ]:          0 :     PRINT_INFO( "Found 0 overlapping surface pairs\n" );
     349                 :            : 
     350                 :          0 :   return status;
     351                 :            : }
     352                 :            : 
     353                 :            : CubitStatus
     354                 :          0 : SurfaceOverlapTool::find_overlapping_surfaces( DLIList<RefFace*> &ref_face_list,
     355                 :            :                                               DLIList<RefFace*> &ref_face_list1,
     356                 :            :                                               DLIList<RefFace*> &ref_face_list2,
     357                 :            :                                               DLIList<RefEntity*> &faces_to_draw,
     358                 :            :                                               CubitBoolean show_messages, 
     359                 :            :                                               bool filter_slivers)
     360                 :            : {
     361                 :          0 :   CubitStatus status = CUBIT_SUCCESS;
     362                 :            : 
     363                 :          0 :   CubitBoolean group_results = CUBIT_FALSE;
     364                 :          0 :   CubitBoolean list_pairs = CUBIT_FALSE;
     365                 :          0 :   CubitBoolean imprint_results = CUBIT_FALSE;
     366                 :            : 
     367         [ #  # ]:          0 :   if( show_messages == CUBIT_TRUE )
     368                 :            :   {
     369                 :          0 :      group_results = groupResults;
     370                 :          0 :      list_pairs = listPairs;
     371                 :          0 :      imprint_results = imprintResults;
     372                 :            :   }
     373                 :            : 
     374                 :          0 :   int prog_step = 10;
     375                 :            : 
     376         [ #  # ]:          0 :   if( show_messages )
     377                 :            :   {
     378 [ #  # ][ #  # ]:          0 :     PRINT_INFO( "Finding surface overlap...\n" );
     379         [ #  # ]:          0 :     if( ref_face_list.size() > prog_step )
     380                 :            :     {
     381                 :            :       char message[128];
     382         [ #  # ]:          0 :       sprintf(message, "Finding Surface Overlap On %d Surfaces", ref_face_list.size() );
     383 [ #  # ][ #  # ]:          0 :       AppUtil::instance()->progress_tool()->start(0, ref_face_list.size(),
     384 [ #  # ][ #  # ]:          0 :         "Progress", message, TRUE, TRUE );
     385                 :            :     }
     386                 :            :   }
     387                 :            :   else
     388                 :          0 :     prog_step = -1;
     389                 :            : 
     390                 :            :   status = find_overlapping_surfaces( ref_face_list, ref_face_list1,
     391                 :          0 :     ref_face_list2, faces_to_draw, list_pairs, prog_step, filter_slivers );
     392                 :            : 
     393 [ #  # ][ #  # ]:          0 :   if( show_messages && ref_face_list.size() > prog_step )
                 [ #  # ]
     394                 :          0 :     AppUtil::instance()->progress_tool()->end();
     395                 :            : 
     396         [ #  # ]:          0 :   if( faces_to_draw.size() )
     397                 :            :   {
     398         [ #  # ]:          0 :     if( group_results == CUBIT_TRUE )
     399                 :            :     {
     400 [ #  # ][ #  # ]:          0 :       RefGroup *new_group = RefEntityFactory::instance()->construct_RefGroup( "surf_overlap" );
     401         [ #  # ]:          0 :       new_group->add_ref_entity( faces_to_draw );
     402         [ #  # ]:          0 :       CubitString name = new_group->entity_name();
     403 [ #  # ][ #  # ]:          0 :       PRINT_INFO( "Found %d overlapping surface pairs (added to group '%s')\n", 
         [ #  # ][ #  # ]
                 [ #  # ]
     404 [ #  # ][ #  # ]:          0 :         ref_face_list1.size(), name.c_str() );
     405                 :            :     }
     406         [ #  # ]:          0 :     else if( show_messages )
     407 [ #  # ][ #  # ]:          0 :       PRINT_INFO( "Found %d overlapping surface pairs\n", ref_face_list1.size() );
     408                 :            : 
     409         [ #  # ]:          0 :     if ( imprint_results )
     410                 :            :     {
     411                 :          0 :       CubitStatus stat = imprint(ref_face_list1, ref_face_list2);
     412         [ #  # ]:          0 :       if ( stat != CUBIT_SUCCESS )
     413                 :            :       {
     414 [ #  # ][ #  # ]:          0 :         PRINT_WARNING("Imprinting overlaps was unsuccessful\n");
     415                 :            :       }
     416                 :            :     }
     417                 :            :   }
     418         [ #  # ]:          0 :   else if (show_messages )
     419 [ #  # ][ #  # ]:          0 :     PRINT_INFO( "Found 0 overlapping surface pairs\n" );
     420                 :            : 
     421                 :          0 :   return status;
     422                 :            : }
     423                 :            : 
     424                 :            : CubitStatus
     425                 :          0 : SurfaceOverlapTool::find_overlapping_surfaces( DLIList<RefFace*> &ref_face_list,
     426                 :            :                                               DLIList<RefFace*> &ref_face_list1,
     427                 :            :                                               DLIList<RefFace*> &ref_face_list2,
     428                 :            :                                               DLIList<RefEntity*> &pair_list,
     429                 :            :                                               CubitBoolean list_pairs,
     430                 :            :                                               int prog_step,
     431                 :            :                                               bool filter_slivers)
     432                 :            : {
     433                 :          0 :   int number_pairs = 0;
     434                 :          0 :   CubitBoolean abort = CUBIT_FALSE;
     435                 :            :   RefEntity* ref_entity;
     436                 :            : 
     437                 :            :   // Check each surface with each one later in the list
     438                 :            :   RefFace *ref_face_ptr1, *ref_face_ptr2;
     439                 :            : 
     440                 :            :   // Populate the RefFace AbstractTree
     441 [ #  # ][ #  # ]:          0 :   AbstractTree<RefFace*> *a_tree = new RTree<RefFace*>( gapMax );
     442                 :            :   int i;
     443         [ #  # ]:          0 :   ref_face_list.reset();
     444 [ #  # ][ #  # ]:          0 :   for( i=ref_face_list.size(); i--; )
     445                 :            :   {
     446         [ #  # ]:          0 :     RefFace *ref_face_ptr = ref_face_list.get_and_step();
     447         [ #  # ]:          0 :     a_tree->add( ref_face_ptr );
     448                 :            :   }
     449                 :            : 
     450                 :            :   // Main loop for finding overlapping surfaces
     451         [ #  # ]:          0 :   ref_face_list.reset();
     452 [ #  # ][ #  # ]:          0 :   for( i=ref_face_list.size(); i--; )
     453                 :            :   {
     454                 :            :     // Cancel button pushed or cntrl-C
     455 [ #  # ][ #  # ]:          0 :     if (AppUtil::instance()->interrupt()) 
                 [ #  # ]
     456                 :            :     {
     457 [ #  # ][ #  # ]:          0 :       PRINT_INFO("Find overlap operation aborted.\n");
         [ #  # ][ #  # ]
     458                 :          0 :       goto done;
     459                 :            :     }
     460                 :            : 
     461         [ #  # ]:          0 :     ref_face_ptr1 = ref_face_list.get_and_step();
     462                 :            : 
     463                 :            :     // Remove this surface from AbstractTree so it is not found and never
     464                 :            :     // found again
     465         [ #  # ]:          0 :     a_tree->remove( ref_face_ptr1 );
     466                 :            : 
     467                 :            :     // Find RefFaces from AbstractTree that are within range of this surface
     468         [ #  # ]:          0 :     CubitBox ref_face1_box = ref_face_ptr1->bounding_box();
     469 [ #  # ][ #  # ]:          0 :     DLIList<RefFace*> close_ref_faces;
                 [ #  # ]
     470         [ #  # ]:          0 :     a_tree->find( ref_face1_box, close_ref_faces );
     471                 :            : 
     472                 :            :     int j;
     473 [ #  # ][ #  # ]:          0 :     for( j=close_ref_faces.size(); j--; )
     474                 :            :     {
     475                 :            :       // Cancel button pushed or cntrl-C
     476 [ #  # ][ #  # ]:          0 :       if (AppUtil::instance()->interrupt()) 
                 [ #  # ]
     477                 :            :       {
     478 [ #  # ][ #  # ]:          0 :          PRINT_INFO("Find overlap operation aborted.\n");
         [ #  # ][ #  # ]
     479                 :          0 :          goto done;
     480                 :            :       }
     481                 :            : 
     482         [ #  # ]:          0 :       ref_face_ptr2 = close_ref_faces.get_and_step();
     483                 :            : 
     484         [ #  # ]:          0 :       bool overlap = check_overlap( ref_face_ptr1, ref_face_ptr2, abort );
     485 [ #  # ][ #  # ]:          0 :       if(overlap == CUBIT_TRUE && filter_slivers)
     486                 :            :       {
     487                 :            :         RefFace *f1, *f2;
     488 [ #  # ][ #  # ]:          0 :         DLIList<RefEdge*> f1_edges, f2_edges, *f1_edge_list/* , *f2_edge_list */;
                 [ #  # ]
     489         [ #  # ]:          0 :         ref_face_ptr1->ref_edges(f1_edges);
     490         [ #  # ]:          0 :         ref_face_ptr2->ref_edges(f2_edges);
     491 [ #  # ][ #  # ]:          0 :         if(f1_edges.size() > f2_edges.size())
                 [ #  # ]
     492                 :            :         {
     493                 :          0 :           f1 = ref_face_ptr2;
     494                 :          0 :           f2 = ref_face_ptr1;
     495                 :          0 :           f1_edge_list = &f2_edges;
     496                 :            :         }
     497                 :            :         else
     498                 :            :         {
     499                 :          0 :           f1 = ref_face_ptr1;
     500                 :          0 :           f2 = ref_face_ptr2;
     501                 :          0 :           f1_edge_list = &f1_edges;
     502                 :            :         }
     503                 :            :         int b;
     504 [ #  # ][ #  # ]:          0 :         for(b=f1_edge_list->size(); b>0 && overlap; b--)
                 [ #  # ]
     505                 :            :         {
     506         [ #  # ]:          0 :           RefEdge *cur_edge = f1_edge_list->get_and_step();
     507 [ #  # ][ #  # ]:          0 :           if(cur_edge->is_merged())
     508                 :            :           {
     509         [ #  # ]:          0 :             DLIList<RefFace*> face_list;
     510         [ #  # ]:          0 :             cur_edge->ref_faces(face_list);
     511 [ #  # ][ #  # ]:          0 :             if(face_list.is_in_list(f2))
     512                 :            :             {
     513         [ #  # ]:          0 :               CubitVector mid_pt;
     514         [ #  # ]:          0 :               cur_edge->mid_point(mid_pt);
     515         [ #  # ]:          0 :               CubitVector f1_norm = f1->normal_at(mid_pt); 
     516         [ #  # ]:          0 :               CubitVector f2_norm = f2->normal_at(mid_pt); 
     517 [ #  # ][ #  # ]:          0 :               DLIList<CoEdge*> f1_coedges, f2_coedges;
                 [ #  # ]
     518         [ #  # ]:          0 :               cur_edge->get_co_edges(f1_coedges, f1);
     519         [ #  # ]:          0 :               cur_edge->get_co_edges(f2_coedges, f2);
     520 [ #  # ][ #  # ]:          0 :               if(f1_coedges.size() == 1 && f2_coedges.size() == 1)
         [ #  # ][ #  # ]
                 [ #  # ]
     521                 :            :               {
     522         [ #  # ]:          0 :                 CoEdge *ce1 = f1_coedges.get();
     523         [ #  # ]:          0 :                 CoEdge *ce2 = f2_coedges.get();
     524         [ #  # ]:          0 :                 CubitVector curve_dir;
     525                 :            : 
     526 [ #  # ][ #  # ]:          0 :                 if( cur_edge->get_curve_ptr()->geometry_type() == STRAIGHT_CURVE_TYPE )
                 [ #  # ]
     527         [ #  # ]:          0 :                   cur_edge->get_point_direction(mid_pt, curve_dir);
     528                 :            :                 else
     529         [ #  # ]:          0 :                   cur_edge->tangent( mid_pt, curve_dir );
     530                 :            : 
     531 [ #  # ][ #  # ]:          0 :                 CubitVector ce1_dir, ce2_dir;
     532 [ #  # ][ #  # ]:          0 :                 if(ce1->get_sense() == CUBIT_REVERSED)
     533 [ #  # ][ #  # ]:          0 :                   ce1_dir = -curve_dir;
     534                 :            :                 else
     535         [ #  # ]:          0 :                   ce1_dir = curve_dir;
     536 [ #  # ][ #  # ]:          0 :                 if(ce2->get_sense() == CUBIT_REVERSED)
     537 [ #  # ][ #  # ]:          0 :                   ce2_dir = -curve_dir;
     538                 :            :                 else
     539         [ #  # ]:          0 :                   ce2_dir = curve_dir;
     540         [ #  # ]:          0 :                 CubitVector in_dir1 = f1_norm * ce1_dir;
     541         [ #  # ]:          0 :                 CubitVector in_dir2 = f2_norm * ce2_dir;
     542 [ #  # ][ #  # ]:          0 :                 if(in_dir1 % in_dir2 < 0.0)
     543                 :          0 :                   overlap = false;
     544         [ #  # ]:          0 :               }
     545         [ #  # ]:          0 :             }
     546                 :            :           }
     547         [ #  # ]:          0 :         }
     548                 :            :       }
     549         [ #  # ]:          0 :       if(overlap)
     550                 :            :       {
     551         [ #  # ]:          0 :         if( abort == CUBIT_TRUE )
     552                 :          0 :           goto done;
     553                 :            : 
     554         [ #  # ]:          0 :         if( list_pairs == CUBIT_TRUE )
     555 [ #  # ][ #  # ]:          0 :           PRINT_INFO( " Surface %d and %d overlap\n", ref_face_ptr1->id(),
         [ #  # ][ #  # ]
                 [ #  # ]
     556         [ #  # ]:          0 :           ref_face_ptr2->id() );
     557                 :            :         
     558                 :          0 :         number_pairs++;
     559                 :            :         
     560         [ #  # ]:          0 :         ref_entity = CAST_TO(ref_face_ptr1,RefEntity);
     561         [ #  # ]:          0 :         pair_list.append_unique( ref_entity );
     562                 :            :         
     563         [ #  # ]:          0 :         ref_entity = CAST_TO(ref_face_ptr2,RefEntity);
     564         [ #  # ]:          0 :         pair_list.append_unique( ref_entity );
     565                 :            :         
     566         [ #  # ]:          0 :         ref_face_list1.append( ref_face_ptr1 );
     567         [ #  # ]:          0 :         ref_face_list2.append( ref_face_ptr2 );
     568                 :            :       }
     569                 :            : 
     570         [ #  # ]:          0 :       if( abort == CUBIT_TRUE )
     571                 :          0 :         goto done;
     572                 :            :     }
     573                 :            : 
     574                 :            :     // Free memory, since this surface will never be accessed again.  This
     575                 :            :     // helps to reduce memory required.
     576         [ #  # ]:          0 :     ref_face_ptr1->delete_TD( &TDSurfaceOverlap::is_surface_overlap );
     577                 :            : 
     578 [ #  # ][ #  # ]:          0 :     if( prog_step>0 && ref_face_list.size() > prog_step )
         [ #  # ][ #  # ]
     579 [ #  # ][ #  # ]:          0 :       AppUtil::instance()->progress_tool()->step();
         [ #  # ][ #  # ]
                 [ #  # ]
     580                 :          0 :   }
     581                 :            : 
     582                 :            : done:  
     583                 :            : 
     584                 :            :   // Make sure all tool datas are deleted
     585 [ #  # ][ #  # ]:          0 :   for( i=ref_face_list.size(); i--; )
     586 [ #  # ][ #  # ]:          0 :     ref_face_list.get_and_step()->delete_TD( &TDSurfaceOverlap::is_surface_overlap );
     587                 :            : 
     588 [ #  # ][ #  # ]:          0 :   delete a_tree;
     589                 :            : 
     590                 :          0 :   return CUBIT_SUCCESS;
     591                 :            : }
     592                 :            : 
     593                 :            : 
     594                 :          0 : double SurfaceOverlapTool::find_area_overlap( SurfaceOverlapFacet *facet1, SurfaceOverlapFacet *facet2, const double facet_compare_tol )
     595                 :            : {
     596                 :          0 :   double local_overlap_area = 0.0;
     597                 :          0 :   double opp_low = 180.0 - angleMax;
     598                 :          0 :   double opp_high = 180.0 - angleMin;
     599                 :            : 
     600                 :            :   // Check angle between triangles, must be within criteria
     601                 :          0 :   double ang = facet1->angle( *facet2 ); 
     602                 :            : 
     603                 :            :   // Allow overlap for angles close to 180 and 0 degrees
     604                 :            : 
     605                 :            :   // normalType - 1=any, 2=opposite, 3=same
     606                 :            :   //ang>=180.0-angt || ang<angt
     607 [ #  # ][ #  # ]:          0 :   if( ((normalType==1 && ang>=opp_low && ang<=opp_high) ||
         [ #  # ][ #  # ]
     608 [ #  # ][ #  # ]:          0 :       (normalType==1 && ang>=angleMin && ang<=angleMax) ||
                 [ #  # ]
     609 [ #  # ][ #  # ]:          0 :       (normalType==2 && ang>=opp_low && ang<=opp_high) ||
                 [ #  # ]
     610 [ #  # ][ #  # ]:          0 :       (normalType==3 && ang>=angleMin && ang<=angleMax)) && 
         [ #  # ][ #  # ]
     611 [ #  # ][ #  # ]:          0 :       (normalType != 2 || !skipFacingSurfaces || !facet1->facing( *facet2 )))// check to make sure the surfaces are not facing 
     612                 :            :   {
     613                 :            :       // If triangle bounding boxes don't intersect - no overlap
     614         [ #  # ]:          0 :       if( facet1->bbox_overlap( facet_compare_tol, *facet2 ) )
     615                 :            :       {
     616                 :            :           // Check distance between triangles, must be within criteria
     617                 :          0 :           double dist = facet1->distance( *facet2 );
     618 [ #  # ][ #  # ]:          0 :           if( dist >= gapMin && dist <= facet_compare_tol )
     619                 :            :           {
     620                 :            :               // Check for projected overlap
     621                 :            :               // We want sum of area of ALL overlapping facets
     622                 :          0 :               local_overlap_area = facet1->projected_overlap( *facet2 );
     623                 :          0 :               return local_overlap_area;               
     624                 :            :           }
     625                 :            :       }
     626                 :            :    }
     627                 :          0 :    return local_overlap_area;
     628                 :            : }
     629                 :            : 
     630                 :          0 : CubitBoolean SurfaceOverlapTool::extract_surf_facets( 
     631                 :            :                                                      Surface *surface1, 
     632                 :            :                                                      Surface *surface2, 
     633                 :            :                                                      std::map<Surface*, DLIList<SurfaceOverlapFacet*>* > *facet_map,
     634                 :            :                                                      const double tolerance,
     635                 :            :                                                      const double facet_tol,
     636                 :            :                                                      DLIList<SurfaceOverlapFacet*> *&facet_list1,
     637                 :            :                                                      DLIList<SurfaceOverlapFacet*> *&facet_list2                                                      
     638                 :            :                                                      )
     639                 :            : {
     640                 :            :   int i;
     641         [ #  # ]:          0 :   AnalyticGeometryTool::instance();
     642                 :            :   
     643                 :            : 
     644         [ #  # ]:          0 :   std::map<Surface*, DLIList<SurfaceOverlapFacet*>* >::iterator facet_iterator;
     645                 :            : 
     646                 :            : 
     647                 :            :   //see if surface is in map...if not we have to create faceting for it.
     648         [ #  # ]:          0 :   if( facet_map )
     649         [ #  # ]:          0 :     facet_iterator = facet_map->find( surface1 );
     650                 :            : 
     651 [ #  # ][ #  # ]:          0 :   if( facet_map == NULL || facet_iterator == facet_map->end() )
         [ #  # ][ #  # ]
                 [ #  # ]
           [ #  #  #  # ]
     652                 :            :   {
     653                 :            :     //for non-planar surfaces, facet wrt the smallest curve of the surface 
     654                 :          0 :     double min_edge_length = 0.0;
     655                 :            : 
     656 [ #  # ][ #  # ]:          0 :     if( surface1->geometry_type() != PLANE_SURFACE_TYPE )
     657                 :            :     {
     658         [ #  # ]:          0 :       DLIList<Curve*> tmp_curves;
     659         [ #  # ]:          0 :       surface1->curves( tmp_curves );
     660 [ #  # ][ #  # ]:          0 :       CubitBox surface_box = surface1->bounding_box();
     661                 :            : 
     662                 :            :       //ignore curves that are really small
     663 [ #  # ][ #  # ]:          0 :       double min_length_threshold = (surface_box.diagonal().length())*0.01;
     664 [ #  # ][ #  # ]:          0 :       if( tmp_curves.size() )
     665                 :            :       {
     666                 :          0 :         min_edge_length = CUBIT_DBL_MAX; 
     667                 :            :         double tmp_length; 
     668 [ #  # ][ #  # ]:          0 :         for( i=tmp_curves.size(); i--; )
     669                 :            :         {
     670 [ #  # ][ #  # ]:          0 :           tmp_length = tmp_curves.get_and_step()->measure();
     671 [ #  # ][ #  # ]:          0 :           if( tmp_length > min_length_threshold && tmp_length < min_edge_length )
     672                 :          0 :             min_edge_length = tmp_length;
     673                 :            :         }
     674                 :            :       }
     675         [ #  # ]:          0 :       if( min_edge_length == CUBIT_DBL_MAX )
     676                 :          0 :         min_edge_length = 0.0;
     677                 :            :       else
     678         [ #  # ]:          0 :         min_edge_length *= 2;
     679                 :            :     }
     680                 :            : 
     681 [ #  # ][ #  # ]:          0 :     facet_list1 = new DLIList<SurfaceOverlapFacet*>;
     682         [ #  # ]:          0 :     GMem surface_graphics;
     683         [ #  # ]:          0 :     surface1->get_geometry_query_engine()->get_graphics( surface1, &surface_graphics,
     684         [ #  # ]:          0 :                                 0, facet_tol, 0 );                               
     685                 :            : 
     686         [ #  # ]:          0 :     GPoint* plist = surface_graphics.point_list();
     687         [ #  # ]:          0 :     int* facet_list = surface_graphics.facet_list();
     688                 :            : 
     689                 :            :     GPoint p[3];
     690         [ #  # ]:          0 :     for (i = 0; i < surface_graphics.fListCount; )
     691                 :            :     {
     692                 :          0 :       int sides = facet_list[i++];
     693         [ #  # ]:          0 :       if (sides != 3)
     694                 :            :       {
     695 [ #  # ][ #  # ]:          0 :         PRINT_WARNING("Skipping n-sided polygone in triangle list"
                 [ #  # ]
     696         [ #  # ]:          0 :                       " in TDSurfaceOverlap.\n");
     697                 :          0 :         i += sides;
     698                 :            :       }
     699                 :            :       else
     700                 :            :       {
     701                 :          0 :         p[0] = plist[facet_list[i++]];
     702                 :          0 :         p[1] = plist[facet_list[i++]];
     703                 :          0 :         p[2] = plist[facet_list[i++]];
     704                 :            :      
     705 [ #  # ][ #  # ]:          0 :         SurfaceOverlapFacet *facet = new SurfaceOverlapFacet( p );        
     706         [ #  # ]:          0 :         facet_list1->append( facet );
     707                 :            :       }
     708                 :            :     }
     709                 :            : 
     710         [ #  # ]:          0 :     if( facet_map )
     711                 :            :       facet_map->insert( std::map<Surface*, 
     712 [ #  # ][ #  # ]:          0 :         DLIList<SurfaceOverlapFacet*>*>::value_type( surface1, facet_list1 )); 
                 [ #  # ]
     713                 :            :   }
     714                 :            :   else
     715         [ #  # ]:          0 :     facet_list1 = facet_iterator->second;
     716                 :            : 
     717                 :            :   //see if surface is in map...if not we have to create faceting for it.
     718         [ #  # ]:          0 :   if( facet_map )
     719         [ #  # ]:          0 :     facet_iterator = facet_map->find( surface2 );
     720                 :            : 
     721 [ #  # ][ #  # ]:          0 :   if( facet_map == NULL || facet_iterator == facet_map->end() )
         [ #  # ][ #  # ]
                 [ #  # ]
           [ #  #  #  # ]
     722                 :            :   {
     723                 :            :     //for non-planar surfaces, facet wrt the smallest curve of the surface 
     724                 :          0 :     double min_edge_length = 0.0;
     725                 :            : 
     726 [ #  # ][ #  # ]:          0 :     if( surface2->geometry_type() != PLANE_SURFACE_TYPE )
     727                 :            :     {
     728         [ #  # ]:          0 :       DLIList<Curve*> tmp_curves;
     729         [ #  # ]:          0 :       surface2->curves( tmp_curves );
     730 [ #  # ][ #  # ]:          0 :       CubitBox surface_box = surface2->bounding_box();
     731                 :            : 
     732                 :            :       //ignore curves that are really small
     733 [ #  # ][ #  # ]:          0 :       double min_length_threshold = (surface_box.diagonal().length())*0.01;
     734 [ #  # ][ #  # ]:          0 :       if( tmp_curves.size() )
     735                 :            :       {
     736                 :          0 :         min_edge_length = CUBIT_DBL_MAX; 
     737                 :            :         double tmp_length; 
     738 [ #  # ][ #  # ]:          0 :         for( i=tmp_curves.size(); i--; )
     739                 :            :         {
     740 [ #  # ][ #  # ]:          0 :           tmp_length = tmp_curves.get_and_step()->measure();
     741 [ #  # ][ #  # ]:          0 :           if( tmp_length > min_length_threshold && tmp_length < min_edge_length )
     742                 :          0 :             min_edge_length = tmp_length;
     743                 :            :         }
     744                 :            :       }
     745         [ #  # ]:          0 :       if( min_edge_length == CUBIT_DBL_MAX )
     746                 :          0 :         min_edge_length = 0.0;
     747                 :            :       else
     748         [ #  # ]:          0 :         min_edge_length *= 2;
     749                 :            :     }
     750                 :            : 
     751 [ #  # ][ #  # ]:          0 :     facet_list2 = new DLIList<SurfaceOverlapFacet*>;
     752         [ #  # ]:          0 :     GMem surface_graphics;
     753         [ #  # ]:          0 :     surface2->get_geometry_query_engine()->get_graphics( surface2, &surface_graphics, 
     754         [ #  # ]:          0 :                                           0, facet_tol, 0 );                                
     755                 :            : 
     756         [ #  # ]:          0 :     GPoint* plist = surface_graphics.point_list();
     757         [ #  # ]:          0 :     int* facet_list = surface_graphics.facet_list();
     758                 :            : 
     759                 :            :     GPoint p[3];
     760         [ #  # ]:          0 :     for (i = 0; i < surface_graphics.fListCount; )
     761                 :            :     {
     762                 :          0 :       int sides = facet_list[i++];
     763         [ #  # ]:          0 :       if (sides != 3)
     764                 :            :       {
     765 [ #  # ][ #  # ]:          0 :         PRINT_WARNING("Skipping n-sided polygone in triangle list"
                 [ #  # ]
     766         [ #  # ]:          0 :                       " in TDSurfaceOverlap.\n");
     767                 :          0 :         i += sides;
     768                 :            :       }
     769                 :            :       else
     770                 :            :       {
     771                 :          0 :         p[0] = plist[facet_list[i++]];
     772                 :          0 :         p[1] = plist[facet_list[i++]];
     773                 :          0 :         p[2] = plist[facet_list[i++]];
     774                 :            :      
     775 [ #  # ][ #  # ]:          0 :         SurfaceOverlapFacet *facet = new SurfaceOverlapFacet( p );            
     776         [ #  # ]:          0 :         facet_list2->append( facet );
     777                 :            :       }
     778                 :            :     }
     779                 :            : 
     780         [ #  # ]:          0 :     if( facet_map )
     781                 :            :       facet_map->insert( std::map<Surface*, 
     782 [ #  # ][ #  # ]:          0 :         DLIList<SurfaceOverlapFacet*>*>::value_type( surface2, facet_list2 )); 
                 [ #  # ]
     783                 :            :   }           
     784                 :            :   else
     785         [ #  # ]:          0 :     facet_list2 = facet_iterator->second;
     786                 :            : 
     787                 :          0 :   return CUBIT_TRUE;
     788                 :            : }
     789                 :            : 
     790                 :          0 : CubitBoolean SurfaceOverlapTool::check_size_and_swap_surfs( 
     791                 :            :                                                     Surface *&tmp_surf1, 
     792                 :            :                                                     Surface *&tmp_surf2, 
     793                 :            :                                                     const double tolerance,
     794                 :            :                                                     const double facet_tol,
     795                 :            :                                                     DLIList<SurfaceOverlapFacet*> *&facet_list1, 
     796                 :            :                                                     DLIList<SurfaceOverlapFacet*> *&facet_list2, 
     797                 :            :                                                     std::map<Surface*, AbstractTree<SurfaceOverlapFacet*>* > *&a_tree_map,
     798                 :            :                                                     AbstractTree<SurfaceOverlapFacet*> *&a_tree2 )
     799                 :            : {
     800                 :            :   
     801                 :            :   // Compare facets
     802                 :          0 :   int num_tri1 = facet_list1->size();
     803         [ #  # ]:          0 :   if( !num_tri1 )
     804                 :            :   {
     805 [ #  # ][ #  # ]:          0 :     PRINT_WARNING( "Unable to facet surface\n" );
     806                 :          0 :     return CUBIT_FALSE;
     807                 :            :   }
     808                 :            :   
     809                 :          0 :   int num_tri2 = facet_list2->size();
     810         [ #  # ]:          0 :   if( !num_tri2 )
     811                 :            :   {
     812 [ #  # ][ #  # ]:          0 :     PRINT_WARNING( "Unable to facet surface\n" );
     813                 :          0 :     return CUBIT_FALSE;
     814                 :            :   }
     815                 :            :    
     816                 :            :   // Compare least to most - possibly switch the lists
     817                 :            :   Surface *temp_surf;
     818         [ #  # ]:          0 :   if( facet_list1->size() > facet_list2->size() )
     819                 :            :   {
     820                 :          0 :     DLIList<SurfaceOverlapFacet*> *temp_list = facet_list1;
     821                 :          0 :     facet_list1 = facet_list2;
     822                 :          0 :     facet_list2 = temp_list;
     823                 :          0 :     temp_surf = tmp_surf1;
     824                 :          0 :     tmp_surf1 = tmp_surf2;
     825                 :          0 :     tmp_surf2 = temp_surf;    
     826                 :            :   }
     827                 :            : 
     828                 :            :   // Possibly use an AbstractTree for facet_list2
     829                 :            :   int i;
     830         [ #  # ]:          0 :   if( facet_list2->size() > NO_FACETS_FOR_ABSTRACTTREE )
     831                 :            :   { 
     832 [ #  # ][ #  # ]:          0 :     std::map<Surface*, AbstractTree<SurfaceOverlapFacet*>* >::iterator iter1, iter2; 
     833                 :            :     // If the same size, use the existing AbstractTree if one has
     834                 :            :     // one and the other doesn't.  This probably won't 
     835                 :            :     // save time, but there is a miniscule chance it might.
     836 [ #  # ][ #  # ]:          0 :     if( facet_list1->size() == facet_list2->size() )
                 [ #  # ]
     837                 :            :     {
     838                 :            :       //see if both are in the a_tree_map
     839         [ #  # ]:          0 :       iter1 = a_tree_map->find( tmp_surf1 );  
     840         [ #  # ]:          0 :       iter2 = a_tree_map->find( tmp_surf2 );  
     841                 :            : 
     842 [ #  # ][ #  # ]:          0 :       if( !(iter2 != a_tree_map->end()) && (iter1 != a_tree_map->end()) ) 
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
           [ #  #  #  #  
                   #  # ]
     843                 :            :       {
     844                 :            :         // Switch them, just to use the existing AbstractTree
     845                 :          0 :         DLIList<SurfaceOverlapFacet*> *temp_list = facet_list1;
     846                 :          0 :         facet_list1 = facet_list2;
     847                 :          0 :         facet_list2 = temp_list;
     848                 :          0 :         Surface *tmp_surf = tmp_surf1;
     849                 :          0 :         tmp_surf1 = tmp_surf2;
     850                 :          0 :         tmp_surf2 = tmp_surf;
     851         [ #  # ]:          0 :         std::map<Surface*, AbstractTree<SurfaceOverlapFacet*>* >::iterator tmp_iter;
     852                 :          0 :         tmp_iter = iter1;
     853                 :          0 :         iter1 = iter2;
     854                 :          0 :         iter2 = tmp_iter; 
     855                 :            :       }
     856                 :            :     }
     857                 :            :   
     858                 :            :     //populate tree if necessary
     859         [ #  # ]:          0 :     iter2 = a_tree_map->find( tmp_surf2 ); 
     860 [ #  # ][ #  # ]:          0 :     if( iter2 == a_tree_map->end() ) 
                 [ #  # ]
     861                 :            :     {
     862                 :            :       //a_tree = new RTree<SurfaceOverlapFacet*>( gapMax );
     863 [ #  # ][ #  # ]:          0 :       a_tree2 = new RTree<SurfaceOverlapFacet*>( tolerance+facet_tol );
     864                 :            : 
     865 [ #  # ][ #  # ]:          0 :       for( i=facet_list2->size(); i--; )
     866 [ #  # ][ #  # ]:          0 :         a_tree2->add( facet_list2->get_and_step() ); 
     867                 :            :       
     868 [ #  # ][ #  # ]:          0 :       a_tree_map->insert( std::map<Surface*, AbstractTree<SurfaceOverlapFacet*>*>::value_type( tmp_surf2, a_tree2 ));
     869                 :            :     }
     870                 :            :     else
     871         [ #  # ]:          0 :       a_tree2 = iter2->second;
     872                 :            :   }
     873                 :          0 :   return CUBIT_TRUE;
     874                 :            : }
     875                 :            : 
     876                 :            : //Currently this function is only called when using tolerant imprinting.
     877                 :            : //It does not use the settings controlled by the user for this tool
     878                 :            : 
     879                 :          0 : CubitBoolean SurfaceOverlapTool::check_surfs_for_imprinting( Surface *surface1, Surface *surface2,
     880                 :            :               std::map<Surface*, DLIList<SurfaceOverlapFacet*>* > *facet_map,
     881                 :            :               std::map<Surface*, double > *area_map,
     882                 :            :               std::map<Surface*, AbstractTree<SurfaceOverlapFacet*>* > *a_tree_map,
     883                 :            :               double overlap_tol)
     884                 :            : {
     885         [ #  # ]:          0 :   if( surface1 == surface2 )
     886                 :          0 :     return CUBIT_FALSE;
     887                 :            : 
     888                 :            :   // Initialize toelrances
     889         [ #  # ]:          0 :   double tolerance = GeometryQueryTool::get_geometry_factor()*GEOMETRY_RESABS;
     890         [ #  # ]:          0 :   if(overlap_tol > 0.0)
     891                 :          0 :     tolerance = overlap_tol;
     892                 :          0 :   double facet_tol = tolerance*10;
     893                 :            :   //double facet_tol = 0.00025;
     894                 :            :   
     895                 :            :   // Extract graphics facets if not available in map
     896                 :          0 :   DLIList<SurfaceOverlapFacet*> *facet_list1 = NULL;
     897                 :          0 :   DLIList<SurfaceOverlapFacet*> *facet_list2 = NULL;
     898         [ #  # ]:          0 :   extract_surf_facets( surface1, surface2, facet_map, tolerance, facet_tol, facet_list1, facet_list2 ); 
     899                 :            : 
     900                 :            :   // Swap first and second surfaces/facets to set smaller surface as first
     901                 :          0 :   Surface *tmp_surf1 = surface1;
     902                 :          0 :   Surface *tmp_surf2 = surface2;
     903                 :          0 :   AbstractTree<SurfaceOverlapFacet*> *a_tree2 = NULL;
     904 [ #  # ][ #  # ]:          0 :   if( CUBIT_FALSE == check_size_and_swap_surfs( tmp_surf1, tmp_surf2, tolerance, facet_tol, facet_list1, facet_list2, a_tree_map, a_tree2 ) )
     905                 :            :   { 
     906                 :          0 :     return CUBIT_FALSE;
     907                 :            :   }
     908                 :            : 
     909                 :            :   // Calculate the tolerances for dist and area overlap comparision
     910                 :          0 :   double facet_compare_tol = facet_tol+tolerance; // tolerance used in evaluating dist betwen facets
     911                 :          0 :   double overlap_tolerance = tolerance * tolerance; // tolerance used in overlaping facets
     912                 :            :   //calculate_tolerances_for_surf_intersection( tmp_surf1, tmp_surf2, facet_list1, facet_list2, area_map, facet_compare_tol, overlap_tolerance );
     913                 :            :  
     914                 :            :   // Check 1: check for boundary contact (boundary entities (curves & verts) of one surface touches other surface ) 
     915 [ #  # ][ #  # ]:          0 :   if( check_boundary_contact( facet_list1, facet_list2, a_tree2, facet_compare_tol, overlap_tolerance ) )
     916                 :            :   {
     917                 :          0 :     return CUBIT_TRUE;
     918                 :            :   }
     919                 :            : 
     920                 :            :   //if surfaces are not splines and are not of the same type, they won't overlap
     921 [ #  # ][ #  # ]:          0 :   if( (surface1->geometry_type() != SPLINE_SURFACE_TYPE  &&
                 [ #  # ]
     922 [ #  # ][ #  # ]:          0 :        surface2->geometry_type() != SPLINE_SURFACE_TYPE) &&
                 [ #  # ]
     923 [ #  # ][ #  # ]:          0 :       (surface1->geometry_type() != surface2->geometry_type() )) 
     924                 :          0 :     return CUBIT_FALSE;
     925                 :            : 
     926                 :            :   // Check 2: check for overlap between surfaces
     927 [ #  # ][ #  # ]:          0 :   if( check_overlap( facet_list1, facet_list2, a_tree2, facet_compare_tol, overlap_tolerance ) )
     928                 :            :   {
     929                 :          0 :     return CUBIT_TRUE;
     930                 :            :   }
     931                 :            : 
     932                 :            : 
     933                 :            : 
     934                 :            : 
     935                 :            :   // Check 3: check for interior contact (interior of surface touches interior of other surface )
     936                 :            : 
     937                 :            :   // Check 4: check for peneration ( one surface penetrates other surface )
     938                 :            : 
     939                 :          0 :   return CUBIT_FALSE;
     940                 :            : }
     941                 :            : 
     942                 :          0 : CubitBoolean SurfaceOverlapTool::calculate_tolerances_for_surf_intersection( 
     943                 :            :                                                                             Surface *tmp_surf1, 
     944                 :            :                                                                             Surface *tmp_surf2, 
     945                 :            :                                                                             DLIList<SurfaceOverlapFacet*> *facet_list1, 
     946                 :            :                                                                             DLIList<SurfaceOverlapFacet*> *facet_list2, 
     947                 :            :                                                                             std::map<Surface*, double > *area_map, 
     948                 :            :                                                                             double &facet_compare_tol, 
     949                 :            :                                                                             double &overlap_tolerance )
     950                 :            : {
     951                 :            : 
     952                 :            :   
     953                 :            :   //set ovelap tolerance to one thousandth of the smaller area.
     954                 :            :   double face1_area, face2_area;
     955         [ #  # ]:          0 :   if( area_map )
     956                 :            :   {
     957         [ #  # ]:          0 :     std::map<Surface*, double>::iterator area_iter;
     958         [ #  # ]:          0 :     area_iter = area_map->find( tmp_surf1);
     959 [ #  # ][ #  # ]:          0 :     if( area_iter == area_map->end() )
                 [ #  # ]
     960                 :            :     {
     961         [ #  # ]:          0 :       face1_area = tmp_surf1->measure();
     962 [ #  # ][ #  # ]:          0 :       area_map->insert( std::map<Surface*, double>::value_type( tmp_surf1, face1_area ) ); 
     963                 :            :     }
     964                 :            :     else
     965         [ #  # ]:          0 :       face1_area = area_iter->second; 
     966                 :            : 
     967         [ #  # ]:          0 :     area_iter = area_map->find( tmp_surf2);
     968 [ #  # ][ #  # ]:          0 :     if( area_iter == area_map->end() )
                 [ #  # ]
     969                 :            :     {
     970         [ #  # ]:          0 :       face2_area = tmp_surf2->measure();
     971 [ #  # ][ #  # ]:          0 :       area_map->insert( std::map<Surface*, double>::value_type( tmp_surf2, face2_area )); 
     972                 :            :     }
     973                 :            :     else
     974         [ #  # ]:          0 :       face2_area = area_iter->second; 
     975                 :            :   }
     976                 :            :   else
     977                 :            :   {
     978         [ #  # ]:          0 :     face1_area = tmp_surf1->measure();
     979         [ #  # ]:          0 :     face2_area = tmp_surf2->measure();
     980                 :            :   }
     981                 :            : 
     982                 :          0 :   bool sample_deviation = false; 
     983                 :          0 :   Surface *larger_surface = NULL;
     984                 :            :   //if the surfaces are non planar and one is much bigger than the other one...
     985                 :            :   //adjust the tolerance some
     986 [ #  # ][ #  # ]:          0 :   if( (tmp_surf1->geometry_type() != PLANE_SURFACE_TYPE  &&
         [ #  # ][ #  # ]
     987         [ #  # ]:          0 :        tmp_surf2->geometry_type() != PLANE_SURFACE_TYPE) )
     988                 :            :   {
     989 [ #  # ][ #  # ]:          0 :     double length1 = tmp_surf1->bounding_box().diagonal().length();
         [ #  # ][ #  # ]
     990 [ #  # ][ #  # ]:          0 :     double length2 = tmp_surf2->bounding_box().diagonal().length();
         [ #  # ][ #  # ]
     991                 :            :     
     992                 :          0 :     double ratio = 0;
     993         [ #  # ]:          0 :     if(length1 > length2)
     994                 :            :     {
     995                 :          0 :       ratio = length1/length2;
     996                 :          0 :       larger_surface = tmp_surf1;
     997                 :            :     }
     998                 :            :     else
     999                 :            :     {
    1000                 :          0 :       ratio = length2/length1;
    1001                 :          0 :       larger_surface = tmp_surf2;
    1002                 :            :     }
    1003                 :            :    
    1004         [ #  # ]:          0 :     if( ratio > 50 )
    1005                 :          0 :       sample_deviation = true;
    1006                 :            :   }
    1007                 :            : 
    1008         [ #  # ]:          0 :   if( face1_area < face2_area )
    1009                 :          0 :     overlap_tolerance = face1_area * 0.001;
    1010                 :            :   else
    1011                 :          0 :     overlap_tolerance = face2_area * 0.001;
    1012                 :            : 
    1013                 :            : 
    1014                 :            :   //If you're comparing overlap between a large surface and a very small one, 
    1015                 :            :   //you might have to adjust tolerances because of the faceting.  Here we try 
    1016                 :            :   //to determine the deviation of the faceting of the larger surface.  I 
    1017                 :            :   //take the mid point of the smallest edge on the facet, thinking the smallest 
    1018                 :            :   //edge is approximating high curvature so it would deviate the most and thus
    1019                 :            :   //provide the safest tolerance.
    1020                 :            :   int i;
    1021         [ #  # ]:          0 :   if( sample_deviation )
    1022                 :            :   {
    1023                 :          0 :     DLIList<SurfaceOverlapFacet*> *tmp_facet_list = NULL;
    1024         [ #  # ]:          0 :     if( larger_surface == tmp_surf1 )
    1025                 :          0 :       tmp_facet_list = facet_list1;
    1026                 :            :     else
    1027                 :          0 :       tmp_facet_list = facet_list2;
    1028                 :            : 
    1029 [ #  # ][ #  # ]:          0 :     for( i=tmp_facet_list->size(); i--; )
    1030                 :            :     {
    1031         [ #  # ]:          0 :       SurfaceOverlapFacet *tmp_facet = tmp_facet_list->get_and_step();
    1032                 :            :         //we used to be lucky, where one seventh of the facets would give us a 
    1033                 :            :         //decent tolerance...this doesn't work now....not robust enough
    1034                 :            : //      if( (i%7) == 0)  // commenting this out....sometimes you haveto 
    1035                 :            : //      {
    1036         [ #  # ]:          0 :         CubitVector point = tmp_facet->smallest_edge_midpoint(); 
    1037                 :            :         //determine distance between surface and centroid 
    1038                 :            : 
    1039         [ #  # ]:          0 :         CubitVector tmp_point;
    1040 [ #  # ][ #  # ]:          0 :         larger_surface->closest_point_trimmed( point, tmp_point );  
    1041         [ #  # ]:          0 :         double tmp_distance = point.distance_between( tmp_point ); 
    1042         [ #  # ]:          0 :         if( tmp_distance > facet_compare_tol )
    1043                 :            :         {
    1044                 :          0 :           facet_compare_tol = tmp_distance;
    1045                 :            :         }
    1046                 :            : //      }
    1047                 :            :     }
    1048                 :            :   }
    1049                 :            :   else
    1050                 :            :   {
    1051 [ #  # ][ #  # ]:          0 :     for( i=facet_list1->size(); i--; )
    1052 [ #  # ][ #  # ]:          0 :       facet_compare_tol += facet_list1->get_and_step()->bounding_box().diagonal().length();
         [ #  # ][ #  # ]
                 [ #  # ]
    1053                 :            : 
    1054 [ #  # ][ #  # ]:          0 :     for( i=facet_list2->size(); i--; )
    1055 [ #  # ][ #  # ]:          0 :       facet_compare_tol += facet_list2->get_and_step()->bounding_box().diagonal().length();
         [ #  # ][ #  # ]
                 [ #  # ]
    1056                 :            : 
    1057 [ #  # ][ #  # ]:          0 :     if( facet_list1->size() || facet_list2->size() )
         [ #  # ][ #  # ]
                 [ #  # ]
    1058                 :            :     {
    1059 [ #  # ][ #  # ]:          0 :       facet_compare_tol /= (facet_list1->size() + facet_list2->size() );
    1060                 :          0 :       facet_compare_tol *= 0.0025;
    1061                 :            :     }
    1062                 :            :   }
    1063                 :            :   
    1064                 :            :   //if the merge tolerance is a larger, use it
    1065         [ #  # ]:          0 :   double tolerance = GeometryQueryTool::get_geometry_factor()*GEOMETRY_RESABS;
    1066         [ #  # ]:          0 :   if( tolerance > facet_compare_tol )
    1067                 :          0 :     facet_compare_tol = tolerance;
    1068                 :            : 
    1069                 :          0 :   return CUBIT_TRUE;
    1070                 :            : }
    1071                 :            : 
    1072                 :          0 : CubitBoolean SurfaceOverlapTool::check_overlap(  
    1073                 :            :   DLIList<SurfaceOverlapFacet*> *facet_list1, 
    1074                 :            :   DLIList<SurfaceOverlapFacet*> *facet_list2, 
    1075                 :            :   AbstractTree<SurfaceOverlapFacet*> *a_tree, 
    1076                 :            :   const double facet_tol, 
    1077                 :            :   const double tolerance )
    1078                 :            : {
    1079                 :            :   int i, j;
    1080                 :          0 :   double area = 0;
    1081                 :          0 :   facet_list1->reset();
    1082                 :          0 :   double overlap_tol = tolerance * tolerance;
    1083                 :            : 
    1084         [ #  # ]:          0 :   for( i=facet_list1->size(); i--; )
    1085                 :            :   {
    1086         [ #  # ]:          0 :     SurfaceOverlapFacet *facet1 = facet_list1->get_and_step();
    1087                 :            :     
    1088         [ #  # ]:          0 :     CubitBox facet1_bbox = facet1->bounding_box();
    1089 [ #  # ][ #  # ]:          0 :     DLIList<SurfaceOverlapFacet*> close_facets;
                 [ #  # ]
    1090         [ #  # ]:          0 :     if( a_tree )
    1091                 :            :     {
    1092         [ #  # ]:          0 :       a_tree->find( facet1_bbox, close_facets );
    1093                 :          0 :       facet_list2 = &close_facets;
    1094                 :            :     }
    1095                 :            : 
    1096         [ #  # ]:          0 :     double facet1_perimeter = facet1->perimeter();
    1097                 :            :     
    1098         [ #  # ]:          0 :     facet_list2->reset();
    1099 [ #  # ][ #  # ]:          0 :     for( j=facet_list2->size(); j--; )
         [ #  # ][ #  # ]
    1100                 :            :     {
    1101         [ #  # ]:          0 :       SurfaceOverlapFacet *facet2 = facet_list2->get_and_step();       
    1102                 :            :       
    1103                 :            :       // Check angle between triangles, must be within criteria
    1104         [ #  # ]:          0 :       double ang = facet1->angle( *facet2 );               
    1105                 :            :       
    1106                 :            :       //rough estimate...must be less than half of 45
    1107 [ #  # ][ #  # ]:          0 :       if( fabs( 180-ang) < 23.5 || ang < 23.5 )       
    1108                 :            :       {
    1109                 :            :         //make sure midpoint and one other point are within facet_tol+tolerance of plane 
    1110                 :            :         //defined by larger facet
    1111                 :          0 :         bool within_parallel_tol = false;
    1112                 :            : 
    1113 [ #  # ][ #  # ]:          0 :         if( facet1_perimeter > facet2->perimeter() )        
    1114         [ #  # ]:          0 :           within_parallel_tol = facet1->facet_points_within_tol( facet2, (facet_tol+tolerance) );       
    1115                 :            :         else
    1116         [ #  # ]:          0 :           within_parallel_tol = facet2->facet_points_within_tol( facet1, (facet_tol+tolerance) );
    1117                 :            :         
    1118         [ #  # ]:          0 :         if( within_parallel_tol )
    1119                 :            :         {
    1120                 :            :           // Check distance between triangles, must be within criteria
    1121         [ #  # ]:          0 :           double dist = facet1->distance( *facet2 );
    1122         [ #  # ]:          0 :           if( dist <= facet_tol+tolerance )
    1123                 :            :           {
    1124                 :            :             // Check for projected overlap
    1125                 :            :             // We want sum of area of ALL overlapping facets
    1126         [ #  # ]:          0 :             area += facet1->projected_overlap( *facet2 );
    1127         [ #  # ]:          0 :             if( area > overlap_tol )                  
    1128                 :            :             {
    1129                 :          0 :               return CUBIT_TRUE;
    1130                 :            :             }                
    1131                 :            :           }
    1132                 :            :         }
    1133                 :            :       }
    1134                 :            :     }
    1135                 :          0 :   }
    1136                 :            : 
    1137                 :          0 :   return CUBIT_FALSE;
    1138                 :            : }
    1139                 :            : 
    1140                 :          0 : CubitBoolean SurfaceOverlapTool::check_boundary_contact( 
    1141                 :            :   DLIList<SurfaceOverlapFacet*> *facet_list1, 
    1142                 :            :   DLIList<SurfaceOverlapFacet*> *facet_list2, 
    1143                 :            :   AbstractTree<SurfaceOverlapFacet*> *a_tree, 
    1144                 :            :   const double facet_compare_tol, 
    1145                 :            :   const double tmp_overlap_tol )
    1146                 :            : {
    1147                 :            :   int i, j;
    1148                 :          0 :   facet_list1->reset();
    1149         [ #  # ]:          0 :   for( i=facet_list1->size(); i--; )
    1150                 :            :   {
    1151         [ #  # ]:          0 :     SurfaceOverlapFacet *facet1 = facet_list1->get_and_step();
    1152                 :            :     
    1153         [ #  # ]:          0 :     CubitBox facet1_bbox = facet1->bounding_box();
    1154 [ #  # ][ #  # ]:          0 :     DLIList<SurfaceOverlapFacet*> close_facets;
                 [ #  # ]
    1155         [ #  # ]:          0 :     if( a_tree )
    1156                 :            :     {
    1157         [ #  # ]:          0 :       a_tree->find( facet1_bbox, close_facets );
    1158                 :          0 :       facet_list2 = &close_facets;
    1159                 :            :     }
    1160                 :            :     
    1161         [ #  # ]:          0 :     facet_list2->reset();
    1162 [ #  # ][ #  # ]:          0 :     for( j=facet_list2->size(); j--; )
         [ #  # ][ #  # ]
    1163                 :            :     {
    1164         [ #  # ]:          0 :       SurfaceOverlapFacet *facet2 = facet_list2->get_and_step();
    1165                 :            :            
    1166                 :            :      // Check boundary contact
    1167                 :            :      // If triangle bounding boxes don't intersect - no overlap
    1168 [ #  # ][ #  # ]:          0 :      if( facet1->bbox_overlap( facet_compare_tol, *facet2 ) )
    1169                 :            :      {
    1170                 :            :         // Check distance between triangles, must be within criteria
    1171         [ #  # ]:          0 :         double dist = facet1->distance( *facet2 );
    1172 [ #  # ][ #  # ]:          0 :         if( dist >= gapMin && dist <= facet_compare_tol )
    1173                 :            :         {
    1174                 :          0 :            return CUBIT_TRUE;
    1175                 :            :         }
    1176                 :            :       }   
    1177                 :            :     }
    1178                 :          0 :   }
    1179                 :            : 
    1180                 :          0 :   return CUBIT_FALSE;
    1181                 :            : }
    1182                 :            : 
    1183                 :            : 
    1184                 :            : CubitBoolean 
    1185                 :          0 : SurfaceOverlapTool::check_overlap( DLIList<SurfaceOverlapFacet*> *facet_list1,
    1186                 :            :                                    DLIList<SurfaceOverlapFacet*> *facet_list2,
    1187                 :            :                                    AbstractTree<SurfaceOverlapFacet*> *a_tree, 
    1188                 :            :                                    CubitBoolean abort, 
    1189                 :            :                                    CubitBoolean draw_overlap,
    1190                 :            :                                    double *overlap_area ) 
    1191                 :            : {
    1192                 :          0 :   double area = 0;
    1193                 :          0 :   double opp_low = 180.0 - angleMax;
    1194                 :          0 :   double opp_high = 180.0 - angleMin;
    1195                 :            : 
    1196                 :          0 :   facet_list1->reset();
    1197                 :          0 :   int num_tri1 = facet_list1->size();
    1198                 :            :   int i,j;
    1199         [ #  # ]:          0 :   for( i=num_tri1; i--; )
    1200                 :            :   {
    1201         [ #  # ]:          0 :     if(i%20 == 0)
    1202                 :            :     {
    1203                 :            :       // Cancel button pushed or cntrl-C
    1204 [ #  # ][ #  # ]:          0 :       if (AppUtil::instance()->interrupt()) 
                 [ #  # ]
    1205                 :            :       {
    1206 [ #  # ][ #  # ]:          0 :         PRINT_INFO("Find overlap operation aborted.\n");
         [ #  # ][ #  # ]
    1207                 :          0 :         abort = CUBIT_TRUE;
    1208                 :          0 :         return CUBIT_FALSE;
    1209                 :            :       }
    1210                 :            :     }
    1211                 :            : 
    1212         [ #  # ]:          0 :     SurfaceOverlapFacet *facet1 = facet_list1->get_and_step();
    1213                 :            : 
    1214         [ #  # ]:          0 :     DLIList<SurfaceOverlapFacet*> close_facets;
    1215         [ #  # ]:          0 :     if( a_tree )
    1216                 :            :     {
    1217 [ #  # ][ #  # ]:          0 :       a_tree->find( facet1->bounding_box(), close_facets );
                 [ #  # ]
    1218                 :          0 :       facet_list2 = &close_facets;
    1219                 :            :     }
    1220                 :            :     
    1221         [ #  # ]:          0 :     facet_list2->reset();
    1222 [ #  # ][ #  # ]:          0 :     for( j=facet_list2->size(); j--; )
         [ #  # ][ #  # ]
    1223                 :            :     {
    1224         [ #  # ]:          0 :       if(j%20 == 0)
    1225                 :            :       {
    1226                 :            :         // Cancel button pushed or cntrl-C
    1227 [ #  # ][ #  # ]:          0 :         if (AppUtil::instance()->interrupt()) 
                 [ #  # ]
    1228                 :            :         {
    1229 [ #  # ][ #  # ]:          0 :           PRINT_INFO("Find overlap operation aborted.\n");
         [ #  # ][ #  # ]
    1230                 :          0 :           abort = CUBIT_TRUE;
    1231                 :          0 :           return CUBIT_FALSE;
    1232                 :            :         }
    1233                 :            :       }
    1234                 :            : 
    1235         [ #  # ]:          0 :       SurfaceOverlapFacet *facet2 = facet_list2->get_and_step();
    1236                 :            :       
    1237                 :            :       // Check angle between triangles, must be within criteria
    1238         [ #  # ]:          0 :       double ang = facet1->angle( *facet2 ); 
    1239                 :            : 
    1240                 :            :       // Allow overlap for angles close to 180 and 0 degrees
    1241                 :            :       
    1242                 :            :       // normalType - 1=any, 2=opposite, 3=same
    1243                 :            :       //ang>=180.0-angt || ang<angt
    1244 [ #  # ][ #  # ]:          0 :       if( ((normalType==1 && ang>=opp_low && ang<=opp_high) ||
         [ #  # ][ #  # ]
    1245 [ #  # ][ #  # ]:          0 :         (normalType==1 && ang>=angleMin && ang<=angleMax) ||
                 [ #  # ]
    1246 [ #  # ][ #  # ]:          0 :         (normalType==2 && ang>=opp_low && ang<=opp_high) ||
                 [ #  # ]
    1247 [ #  # ][ #  # ]:          0 :         (normalType==3 && ang>=angleMin && ang<=angleMax) ) &&
         [ #  # ][ #  # ]
    1248 [ #  # ][ #  # ]:          0 :         (normalType != 2 || !skipFacingSurfaces || !facet1->facing( *facet2 )))// check to make sure the surfaces are not facing )
                 [ #  # ]
    1249                 :            :       {
    1250                 :            :         // If triangle bounding boxes don't intersect - no overlap
    1251 [ #  # ][ #  # ]:          0 :         if( facet1->bbox_overlap( gapMax, *facet2 ) )
    1252                 :            :         {
    1253                 :            :           // Check distance between triangles, must be within criteria
    1254         [ #  # ]:          0 :           double dist = facet1->distance( *facet2 );
    1255                 :            : 
    1256 [ #  # ][ #  # ]:          0 :           if( dist >= gapMin && dist <= gapMax )
    1257                 :            :           {
    1258                 :            :             // Check for projected overlap
    1259                 :            :             // We want sum of area of ALL overlapping facets
    1260         [ #  # ]:          0 :             area += facet1->projected_overlap( *facet2, draw_overlap );
    1261 [ #  # ][ #  # ]:          0 :             if( area > overlapTolerance &&
    1262         [ #  # ]:          0 :                (draw_overlap == CUBIT_FALSE && overlap_area == NULL ) )
    1263                 :            :             {
    1264                 :          0 :               return CUBIT_TRUE;
    1265                 :            :             }                
    1266                 :            :           }
    1267                 :            :         }
    1268                 :            :       }
    1269                 :            :     }
    1270                 :          0 :   }  
    1271         [ #  # ]:          0 :   if( draw_overlap == CUBIT_TRUE ) 
    1272                 :            :   {
    1273 [ #  # ][ #  # ]:          0 :     PRINT_INFO("Total overlapping area = %f\n", area );
    1274                 :            : 
    1275         [ #  # ]:          0 :     if( area > 0.0 )
    1276                 :          0 :       return CUBIT_TRUE;
    1277                 :            :   }
    1278                 :            : 
    1279         [ #  # ]:          0 :   if( overlap_area )
    1280                 :            :   {
    1281         [ #  # ]:          0 :     if( area > 0.0 )
    1282                 :            :     {
    1283                 :          0 :       *overlap_area = area;
    1284                 :          0 :       return CUBIT_TRUE;
    1285                 :            :     }
    1286                 :            :   }
    1287                 :            : 
    1288                 :          0 :   return CUBIT_FALSE;
    1289                 :            : }
    1290                 :            : 
    1291                 :            : CubitBoolean
    1292                 :          0 : SurfaceOverlapTool::check_overlap( RefFace *ref_face_ptr1, RefFace *ref_face_ptr2, 
    1293                 :            :                                    CubitBoolean abort, 
    1294                 :            :                                    CubitBoolean draw_overlap,
    1295                 :            :                                    double *overlap_area ) 
    1296                 :            : {
    1297         [ #  # ]:          0 :   if( ref_face_ptr1 == ref_face_ptr2 )
    1298                 :          0 :     return CUBIT_FALSE;
    1299                 :            : 
    1300                 :            :   //if surfaces are not splines and are not of the same type, 
    1301                 :            :   //they won't overlap
    1302                 :            :   /*
    1303                 :            :   I am commenting this out because it filters out some cases we need to find with 
    1304                 :            :   this overlap check.
    1305                 :            :   if( (ref_face_ptr1->get_surface_ptr()->geometry_type() != SPLINE_SURFACE_TYPE  &&
    1306                 :            :        ref_face_ptr2->get_surface_ptr()->geometry_type() != SPLINE_SURFACE_TYPE) &&
    1307                 :            :       (ref_face_ptr1->get_surface_ptr()->geometry_type() != 
    1308                 :            :        ref_face_ptr2->get_surface_ptr()->geometry_type() )) 
    1309                 :            :     return CUBIT_FALSE;
    1310                 :            :     */
    1311                 :            :  
    1312                 :          0 :   AnalyticGeometryTool::instance();
    1313                 :          0 :   abort = CUBIT_FALSE;
    1314                 :            : 
    1315                 :            :   // Check for overlap between the found surfaces using the facets
    1316                 :            :   TDSurfaceOverlap *tdso_1;
    1317                 :          0 :   tdso_1 = (TDSurfaceOverlap *)ref_face_ptr1->get_TD(&TDSurfaceOverlap::is_surface_overlap);
    1318         [ #  # ]:          0 :   if( !tdso_1 )
    1319                 :            :   {
    1320                 :            :     ref_face_ptr1->add_TD( new TDSurfaceOverlap( ref_face_ptr1, facetAngTol, facetAbsTol,
    1321         [ #  # ]:          0 :                                                  gapMax ) );
    1322                 :          0 :     tdso_1 = (TDSurfaceOverlap *)ref_face_ptr1->get_TD(&TDSurfaceOverlap::is_surface_overlap);
    1323                 :            :   }
    1324                 :            : 
    1325                 :            :   TDSurfaceOverlap *tdso_2;
    1326                 :          0 :   tdso_2 = (TDSurfaceOverlap *)ref_face_ptr2->get_TD(&TDSurfaceOverlap::is_surface_overlap);
    1327         [ #  # ]:          0 :   if( !tdso_2 )
    1328                 :            :   {
    1329                 :            :     ref_face_ptr2->add_TD( new TDSurfaceOverlap( ref_face_ptr2, facetAngTol, facetAbsTol,
    1330         [ #  # ]:          0 :                                                  gapMax ) );
    1331                 :          0 :     tdso_2 = (TDSurfaceOverlap *)ref_face_ptr2->get_TD(&TDSurfaceOverlap::is_surface_overlap);
    1332                 :            :   }
    1333                 :            :   
    1334                 :            :   // Check if within the same body - maybe we don't need to consider this
    1335         [ #  # ]:          0 :   if( checkWithinBodies == CUBIT_FALSE )
    1336                 :            :   {
    1337                 :            :     DLIList<Body*> *body_list_ptr1, *body_list_ptr2;
    1338         [ #  # ]:          0 :     body_list_ptr1 = tdso_1->get_body_list();
    1339         [ #  # ]:          0 :     body_list_ptr2 = tdso_2->get_body_list();
    1340         [ #  # ]:          0 :     DLIList<Body*> shared_body_list = *body_list_ptr1;
    1341         [ #  # ]:          0 :     shared_body_list.intersect( *body_list_ptr2 );
    1342 [ #  # ][ #  # ]:          0 :     if( shared_body_list.size() )
    1343 [ #  # ][ #  # ]:          0 :       return CUBIT_FALSE;
    1344                 :            :   }
    1345                 :            : 
    1346                 :            :   // Check if in different bodies - maybe we don't need to consider this
    1347         [ #  # ]:          0 :   if( checkAcrossBodies == CUBIT_FALSE )
    1348                 :            :   {
    1349                 :            :     DLIList<Body*> *body_list_ptr1, *body_list_ptr2;
    1350         [ #  # ]:          0 :     body_list_ptr1 = tdso_1->get_body_list();
    1351         [ #  # ]:          0 :     body_list_ptr2 = tdso_2->get_body_list();
    1352         [ #  # ]:          0 :     DLIList<Body*> shared_body_list = *body_list_ptr1;
    1353         [ #  # ]:          0 :     shared_body_list.intersect( *body_list_ptr2 );
    1354 [ #  # ][ #  # ]:          0 :     if( shared_body_list.size() == 0 )
    1355 [ #  # ][ #  # ]:          0 :       return CUBIT_FALSE;
    1356                 :            :   }
    1357                 :            :   
    1358                 :            :   // Compare facets
    1359                 :          0 :   DLIList<SurfaceOverlapFacet*> *facet_list1 = tdso_1->get_facet_list();
    1360                 :          0 :   int num_tri1 = 0;
    1361         [ #  # ]:          0 :   if(facet_list1)
    1362                 :          0 :     num_tri1 = facet_list1->size();
    1363         [ #  # ]:          0 :   if( !num_tri1 )
    1364                 :            :   {
    1365 [ #  # ][ #  # ]:          0 :     PRINT_WARNING( "Unable to facet surface %d\n", ref_face_ptr1->id() );
    1366                 :          0 :     return CUBIT_FALSE;
    1367                 :            :   }
    1368                 :            :   
    1369                 :          0 :   DLIList<SurfaceOverlapFacet*> *facet_list2 = tdso_2->get_facet_list();
    1370                 :          0 :   int num_tri2 = 0;
    1371         [ #  # ]:          0 :   if(facet_list2)
    1372                 :          0 :     num_tri2 = facet_list2->size();
    1373         [ #  # ]:          0 :   if( !num_tri2 )
    1374                 :            :   {
    1375 [ #  # ][ #  # ]:          0 :     PRINT_WARNING( "Unable to facet surface %d\n", ref_face_ptr2->id() );
    1376                 :          0 :     return CUBIT_FALSE;
    1377                 :            :   }
    1378                 :            :   
    1379 [ #  # ][ #  # ]:          0 :   PRINT_DEBUG_102( " Comparing %d facets in Surface %d with %d facets in Surface %d...\n",
    1380         [ #  # ]:          0 :     num_tri1, ref_face_ptr1->id(), num_tri2, ref_face_ptr2->id() );
    1381                 :            : 
    1382                 :            :   // Compare least to most - possibly switch the lists
    1383         [ #  # ]:          0 :   if( facet_list1->size() > facet_list2->size() )
    1384                 :            :   {
    1385                 :          0 :     DLIList<SurfaceOverlapFacet*> *temp_list = facet_list1;
    1386                 :          0 :     facet_list1 = facet_list2;
    1387                 :          0 :     facet_list2 = temp_list;
    1388                 :          0 :     TDSurfaceOverlap *temp_tdso = tdso_1;
    1389                 :          0 :     tdso_2 = tdso_1;
    1390                 :          0 :     tdso_1 = temp_tdso;
    1391                 :            :   }
    1392                 :            : 
    1393                 :            :   // Possibly use an AbstractTree for facet_list2
    1394                 :          0 :   AbstractTree<SurfaceOverlapFacet*> *a_tree = NULL;
    1395         [ #  # ]:          0 :   if( facet_list2->size() > NO_FACETS_FOR_ABSTRACTTREE )
    1396                 :            :   { 
    1397                 :            :     // If the same size, use the existing AbstractTree if one has
    1398                 :            :     // one and the other doesn't.  This probably won't 
    1399                 :            :     // save time, but there is a miniscule chance it might.
    1400         [ #  # ]:          0 :     if( facet_list1->size() == facet_list2->size() )
    1401                 :            :     {
    1402 [ #  # ][ #  # ]:          0 :       if( !tdso_2->has_rtree() && tdso_1->has_rtree() )
                 [ #  # ]
    1403                 :            :       {
    1404                 :            :         // Switch them, just to use the existing AbstractTree
    1405                 :          0 :         DLIList<SurfaceOverlapFacet*> *temp_list = facet_list1;
    1406                 :          0 :         facet_list1 = facet_list2;
    1407                 :          0 :         facet_list2 = temp_list;
    1408                 :          0 :         TDSurfaceOverlap *temp_tdso = tdso_1;
    1409                 :          0 :         tdso_2 = tdso_1;
    1410                 :          0 :         tdso_1 = temp_tdso;
    1411                 :            :       }
    1412                 :            :     }
    1413                 :          0 :     a_tree = tdso_2->get_facet_rtree();
    1414                 :            :   }
    1415                 :            : 
    1416                 :            :   bool surfs_overlap = check_overlap( facet_list1, facet_list2, a_tree,
    1417                 :          0 :                                       abort, draw_overlap, overlap_area );
    1418                 :            : 
    1419                 :          0 :   return surfs_overlap;
    1420                 :            : }
    1421                 :            : 
    1422                 :          0 : void SurfaceOverlapTool::list_settings()
    1423                 :            : {
    1424                 :            :   char on_off[2][4];
    1425                 :          0 :   strcpy( on_off[0], "Off" );
    1426                 :          0 :   strcpy( on_off[1], "On" );
    1427                 :            : 
    1428 [ #  # ][ #  # ]:          0 :   PRINT_INFO( "Surface Overlap Algorithm Settings:\n" );
         [ #  # ][ #  # ]
    1429         [ #  # ]:          0 :   if( facetAbsTol == 0.0 )
    1430 [ #  # ][ #  # ]:          0 :     PRINT_INFO( " Facetting Absolute Tolerance: 0.0 (using default solid modeler setting)\n" );
         [ #  # ][ #  # ]
    1431                 :            :   else
    1432 [ #  # ][ #  # ]:          0 :     PRINT_INFO( " Facetting Absolute Tolerance: %f\n", facetAbsTol );
         [ #  # ][ #  # ]
    1433                 :            : 
    1434         [ #  # ]:          0 :   if( facetAngTol == 0 )
    1435 [ #  # ][ #  # ]:          0 :     PRINT_INFO( " Facetting Angle Tolerance: 0 (using default solid modeler setting)\n" );
         [ #  # ][ #  # ]
    1436                 :            :   else
    1437 [ #  # ][ #  # ]:          0 :     PRINT_INFO( " Facetting Angle Tolerance: %d\n", facetAngTol );
         [ #  # ][ #  # ]
    1438                 :            : 
    1439 [ #  # ][ #  # ]:          0 :   PRINT_INFO( " Gap Range: %f to %f\n", gapMin, gapMax );
         [ #  # ][ #  # ]
    1440 [ #  # ][ #  # ]:          0 :   PRINT_INFO( " Angle Range: %.1f to %.1f degrees\n", angleMin, angleMax );
         [ #  # ][ #  # ]
    1441 [ #  # ][ #  # ]:          0 :   PRINT_INFO( " Overlap Area Tolerance: %f\n", overlapTolerance );
         [ #  # ][ #  # ]
    1442                 :            : 
    1443   [ #  #  #  # ]:          0 :   switch( normalType )
    1444                 :            :   {
    1445                 :            :   case 1:
    1446 [ #  # ][ #  # ]:          0 :     PRINT_INFO( " Pair Normals Allowed: Any\n" );
         [ #  # ][ #  # ]
    1447                 :          0 :     break;
    1448                 :            :   case 2:
    1449 [ #  # ][ #  # ]:          0 :     PRINT_INFO( " Pair Normals Allowed: Opposite\n" );
         [ #  # ][ #  # ]
    1450                 :          0 :     break;
    1451                 :            :   case 3:
    1452 [ #  # ][ #  # ]:          0 :     PRINT_INFO( " Pair Normals Allowed: Same\n" );
         [ #  # ][ #  # ]
    1453                 :          0 :     break;
    1454                 :            :   }
    1455                 :            : 
    1456 [ #  # ][ #  # ]:          0 :   PRINT_INFO( " Display Pairs: %s\n", on_off[displayPairs] );
         [ #  # ][ #  # ]
    1457 [ #  # ][ #  # ]:          0 :   PRINT_INFO( " List Pairs: %s\n", on_off[listPairs] );
         [ #  # ][ #  # ]
    1458 [ #  # ][ #  # ]:          0 :   PRINT_INFO( " Group Results: %s\n", on_off[groupResults] );
         [ #  # ][ #  # ]
    1459 [ #  # ][ #  # ]:          0 :   PRINT_INFO( " Across Body|Volume: %s\n", on_off[checkAcrossBodies] );
         [ #  # ][ #  # ]
    1460 [ #  # ][ #  # ]:          0 :   PRINT_INFO( " Within Body|Volume: %s\n", on_off[checkWithinBodies] );
         [ #  # ][ #  # ]
    1461 [ #  # ][ #  # ]:          0 :   PRINT_INFO( " Imprint: %s\n", on_off[imprintResults] );
         [ #  # ][ #  # ]
    1462                 :          0 : }
    1463                 :            : 
    1464                 :          0 : CubitString SurfaceOverlapTool::get_normal_type_setting()
    1465                 :            : {
    1466         [ #  # ]:          0 :   if (normalType == 2) {
    1467                 :          0 :     return CubitString("opposite");
    1468                 :            :   }
    1469         [ #  # ]:          0 :   else if (normalType == 3) {
    1470                 :          0 :     return CubitString("same");
    1471                 :            :   }
    1472                 :            :   else {
    1473                 :          0 :     return CubitString("any");
    1474                 :            :   }
    1475                 :            : }
    1476                 :            : 
    1477                 :          0 : void SurfaceOverlapTool::set_normal_type_setting(CubitString type )
    1478                 :            : {
    1479         [ #  # ]:          0 :   if (CubitUtil::compare(type.c_str(), "opposite")) {
    1480                 :          0 :     normalType = 2;
    1481                 :            :   }
    1482         [ #  # ]:          0 :   else if (CubitUtil::compare(type.c_str(), "same")) {
    1483                 :          0 :     normalType = 3;
    1484                 :            :   }
    1485                 :            :   else {
    1486                 :          0 :     normalType = 1;
    1487                 :            :   }
    1488                 :          0 : }
    1489                 :            : 
    1490                 :          0 : int SurfaceOverlapTool::get_group_results_setting()
    1491                 :          0 : {return groupResults;}
    1492                 :            : 
    1493                 :          0 : void SurfaceOverlapTool::set_group_results_setting( int setting )
    1494         [ #  # ]:          0 : {groupResults = (setting) ? CUBIT_TRUE : CUBIT_FALSE;}
    1495                 :            : 
    1496                 :          0 : int SurfaceOverlapTool::get_list_pairs_setting()
    1497                 :          0 : {return listPairs;}
    1498                 :            : 
    1499                 :          0 : void SurfaceOverlapTool::set_list_pairs_setting( int setting )
    1500         [ #  # ]:          0 : {listPairs = (setting) ? CUBIT_TRUE : CUBIT_FALSE;}
    1501                 :            : 
    1502                 :          0 : int SurfaceOverlapTool::get_display_pairs_setting()
    1503                 :          0 : {return displayPairs;}
    1504                 :            : 
    1505                 :          0 : void SurfaceOverlapTool::set_display_pairs_setting( int setting )
    1506         [ #  # ]:          0 : {displayPairs = (setting) ? CUBIT_TRUE : CUBIT_FALSE;}
    1507                 :            : 
    1508                 :          0 : int SurfaceOverlapTool::get_facet_ang_tol_setting()
    1509                 :          0 : {return facetAngTol;}
    1510                 :            : 
    1511                 :          0 : void SurfaceOverlapTool::set_facet_ang_tol_setting( int val )
    1512                 :          0 : {facetAngTol=(unsigned short)val;}
    1513                 :            : 
    1514                 :            : //Imprints the bodies containing the lists of surfaces.  Also
    1515                 :            : //uses the edges imprinting rather than the normal imprint.
    1516                 :          0 : CubitStatus SurfaceOverlapTool::imprint(DLIList<RefFace*> &ref_face_list1,
    1517                 :            :                                         DLIList<RefFace*> &ref_face_list2)
    1518                 :            : {
    1519                 :            :   //This is a very tricky goal.  The goal is to imprint each pair for the
    1520                 :            :   //two lists.  The problem is that we need to imprint the bodies of the
    1521                 :            :   //surfaces not just the surfaces since this isn't as robust.  The problem
    1522                 :            :   //is that more than one of these surfaces may reference the same body.  So
    1523                 :            :   //in fact as we imprint one of the surfaces, the other ones could get deleted and
    1524                 :            :   //create a memory leak.
    1525                 :            :   
    1526                 :            :   //The only thing I can think of is to use the id's of the bodies as the constant.
    1527                 :            :   //On the imprint, the body id should not change.  So lets create an array of bodies,
    1528                 :            :   //and update the array as they get changed.  The array will allow constant access time.
    1529 [ #  # ][ #  # ]:          0 :   DLIList <Body*> body_list, temp_bodies;
                 [ #  # ]
    1530 [ #  # ][ #  # ]:          0 :   DLIList <int> body_ids1, body_ids2, modified_list;
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
    1531 [ #  # ][ #  # ]:          0 :   DLIList <DLIList<RefEdge*>*> edges_list1, edges_list2;
         [ #  # ][ #  # ]
    1532 [ #  # ][ #  # ]:          0 :   DLIList <RefEdge*> *ref_edges1, *ref_edges2, tmp_edges;
    1533                 :            :   
    1534                 :          0 :   CubitBoolean print = CUBIT_TRUE;
    1535                 :            :   Body *tmp_body;
    1536                 :          0 :   int i, max_id = -1;
    1537                 :            :   
    1538                 :            :     //first get the max id of the bodies here to help us
    1539                 :            :     //get the size of the array.
    1540 [ #  # ][ #  # ]:          0 :   for ( i = ref_face_list1.size(); i > 0; i-- )
    1541                 :            :   {
    1542         [ #  # ]:          0 :     temp_bodies.clean_out();
    1543         [ #  # ]:          0 :     RefFace *ref_face1 = ref_face_list1.get_and_step();
    1544         [ #  # ]:          0 :     if ( ref_face1 == NULL )
    1545                 :            :     {
    1546 [ #  # ][ #  # ]:          0 :       PRINT_ERROR("Bad data sent to imprint overlaps.\n");
         [ #  # ][ #  # ]
    1547                 :          0 :       return CUBIT_FAILURE;
    1548                 :            :     }
    1549         [ #  # ]:          0 :     ref_face1->bodies(temp_bodies);
    1550 [ #  # ][ #  # ]:          0 :     if ( temp_bodies.size() != 1 )
    1551                 :            :     {
    1552 [ #  # ][ #  # ]:          0 :       PRINT_WARNING("Entities must be in one body\n");
         [ #  # ][ #  # ]
    1553 [ #  # ][ #  # ]:          0 :       PRINT_WARNING("Skipping that entity\n");
         [ #  # ][ #  # ]
    1554         [ #  # ]:          0 :       ref_face_list2.step();
    1555                 :          0 :       continue;
    1556                 :            :     }
    1557         [ #  # ]:          0 :     tmp_body = temp_bodies.get();
    1558         [ #  # ]:          0 :     temp_bodies.clean_out();
    1559         [ #  # ]:          0 :     RefFace *ref_face2 = ref_face_list2.get_and_step();
    1560         [ #  # ]:          0 :     if ( ref_face2 == NULL )
    1561                 :            :     {
    1562 [ #  # ][ #  # ]:          0 :       PRINT_ERROR("Bad data sent to imprint overlaps.\n");
         [ #  # ][ #  # ]
    1563                 :          0 :       return CUBIT_FAILURE;
    1564                 :            :     }
    1565         [ #  # ]:          0 :     ref_face2->bodies(temp_bodies);
    1566 [ #  # ][ #  # ]:          0 :     if ( temp_bodies.size() != 1 )
    1567                 :            :     {
    1568 [ #  # ][ #  # ]:          0 :       PRINT_WARNING("Entities must be in one body\n");
         [ #  # ][ #  # ]
    1569 [ #  # ][ #  # ]:          0 :       PRINT_WARNING("Skipping that entity\n");
         [ #  # ][ #  # ]
    1570                 :          0 :       continue;
    1571                 :            :     }
    1572 [ #  # ][ #  # ]:          0 :     if ( tmp_body->id() > max_id )
    1573         [ #  # ]:          0 :       max_id = tmp_body->id();
    1574         [ #  # ]:          0 :     body_list.append(tmp_body);
    1575 [ #  # ][ #  # ]:          0 :     body_ids1.append(tmp_body->id());
    1576 [ #  # ][ #  # ]:          0 :     body_list.append(temp_bodies.get());
    1577 [ #  # ][ #  # ]:          0 :     body_ids2.append(temp_bodies.get()->id());
                 [ #  # ]
    1578 [ #  # ][ #  # ]:          0 :     if ( temp_bodies.get()->id() > max_id )
                 [ #  # ]
    1579 [ #  # ][ #  # ]:          0 :       max_id = temp_bodies.get()->id();
    1580                 :            :       //Now set up the edge lists.
    1581 [ #  # ][ #  # ]:          0 :     ref_edges1 = new DLIList<RefEdge*>;
    1582         [ #  # ]:          0 :     ref_face1->ref_edges(tmp_edges);
    1583         [ #  # ]:          0 :     CubitStatus stat = copy_edges_in_list(tmp_edges, *ref_edges1);
    1584         [ #  # ]:          0 :     if ( stat != CUBIT_SUCCESS )
    1585                 :          0 :       return CUBIT_FAILURE;
    1586 [ #  # ][ #  # ]:          0 :     ref_edges2 = new DLIList<RefEdge*>;
    1587         [ #  # ]:          0 :     tmp_edges.clean_out();
    1588         [ #  # ]:          0 :     ref_face2->ref_edges(tmp_edges);
    1589         [ #  # ]:          0 :     stat = copy_edges_in_list(tmp_edges, *ref_edges2);
    1590         [ #  # ]:          0 :     if ( stat != CUBIT_SUCCESS )
    1591                 :          0 :       return CUBIT_FAILURE;
    1592         [ #  # ]:          0 :     edges_list1.append(ref_edges1);
    1593         [ #  # ]:          0 :     edges_list2.append(ref_edges2);
    1594                 :            :   }
    1595                 :            :     //Now create an array and store all of the bodies.
    1596 [ #  # ][ #  # ]:          0 :   Body **body_array = new Body* [2*max_id+1];
    1597                 :            :     //initialize the array to null.
    1598         [ #  # ]:          0 :   for ( i = 0; i < 2*max_id+1; i++ )
    1599                 :            :   {
    1600                 :          0 :     body_array[i] = (Body*)NULL;
    1601                 :            :   }
    1602                 :            :     //Now put the bodies into the array.
    1603 [ #  # ][ #  # ]:          0 :   for ( i = body_list.size(); i > 0; i-- )
    1604                 :            :   {
    1605         [ #  # ]:          0 :     tmp_body = body_list.get_and_step();
    1606         [ #  # ]:          0 :     body_array[tmp_body->id()] = tmp_body;
    1607                 :            :   }
    1608                 :            :     //Now go through and pairwise imprint the bodies.
    1609 [ #  # ][ #  # ]:          0 :   DLIList <Body*> bodies, tmp_modified_bodies;
         [ #  # ][ #  # ]
    1610 [ #  # ][ #  # ]:          0 :   for( i = body_ids1.size(); i > 0; i-- )
    1611                 :            :   {
    1612         [ #  # ]:          0 :     tmp_modified_bodies.clean_out();
    1613         [ #  # ]:          0 :     int id1 = body_ids1.get_and_step();
    1614         [ #  # ]:          0 :     int id2 = body_ids2.get_and_step();
    1615         [ #  # ]:          0 :     ref_edges1 = edges_list1.get_and_step();
    1616         [ #  # ]:          0 :     ref_edges2 = edges_list2.get_and_step();
    1617                 :            :     
    1618                 :          0 :     Body *body1 = body_array[id1];
    1619                 :          0 :     Body *body2 = body_array[id2];
    1620         [ #  # ]:          0 :     int body1_num = num_descendants(body1);
    1621         [ #  # ]:          0 :     int body2_num = num_descendants(body2);
    1622         [ #  # ]:          0 :     bodies.clean_out();
    1623         [ #  # ]:          0 :     bodies.append(body2);
    1624                 :            :     CubitStatus stat = GeometryModifyTool::instance()->
    1625 [ #  # ][ #  # ]:          0 :       imprint(bodies, *ref_edges1, tmp_modified_bodies, CUBIT_FALSE, CUBIT_FALSE);
    1626 [ #  # ][ #  # ]:          0 :     if ( stat == CUBIT_SUCCESS && tmp_modified_bodies.size() == 1 )
         [ #  # ][ #  # ]
    1627                 :            :     {
    1628         [ #  # ]:          0 :       body2 = tmp_modified_bodies.get();
    1629                 :            :     }
    1630         [ #  # ]:          0 :     tmp_modified_bodies.clean_out();
    1631         [ #  # ]:          0 :     bodies.clean_out();
    1632         [ #  # ]:          0 :     bodies.append(body1);
    1633                 :            :     stat = GeometryModifyTool::instance()->
    1634 [ #  # ][ #  # ]:          0 :       imprint(bodies, *ref_edges2, tmp_modified_bodies, CUBIT_FALSE, CUBIT_FALSE);
    1635 [ #  # ][ #  # ]:          0 :     if ( stat == CUBIT_SUCCESS && tmp_modified_bodies.size() == 1 )
         [ #  # ][ #  # ]
    1636                 :            :     {
    1637         [ #  # ]:          0 :       body1 = tmp_modified_bodies.get();
    1638                 :            :     }
    1639                 :          0 :     body_array[id1] = body1;
    1640         [ #  # ]:          0 :     int temp_num = num_descendants(body1);
    1641         [ #  # ]:          0 :     if ( temp_num != body1_num )
    1642                 :            :     {
    1643         [ #  # ]:          0 :       modified_list.append_unique(id1);
    1644                 :            :     }
    1645                 :          0 :     body_array[id2] = body2;
    1646         [ #  # ]:          0 :     temp_num = num_descendants(body2);
    1647         [ #  # ]:          0 :     if ( temp_num != body2_num )
    1648                 :            :     {
    1649         [ #  # ]:          0 :       modified_list.append_unique(id2);
    1650                 :            :     }
    1651                 :            :   }
    1652 [ #  # ][ #  # ]:          0 :   if ( modified_list.size() && print )
         [ #  # ][ #  # ]
    1653                 :            :   {
    1654 [ #  # ][ #  # ]:          0 :     if ( modified_list.size() != 1 )
    1655 [ #  # ][ #  # ]:          0 :       PRINT_INFO("Imprinting Modified Bodies: ");
         [ #  # ][ #  # ]
    1656                 :            :     else
    1657 [ #  # ][ #  # ]:          0 :       PRINT_INFO("Imprinting Modified Body: ");
         [ #  # ][ #  # ]
    1658 [ #  # ][ #  # ]:          0 :     for ( i = 0; i < modified_list.size(); i++ )
    1659                 :            :     {
    1660 [ #  # ][ #  # ]:          0 :       if ( i != modified_list.size()-1 )
    1661                 :            :       {
    1662 [ #  # ][ #  # ]:          0 :         if (i%10 == 0 && i != 0 )
    1663 [ #  # ][ #  # ]:          0 :           PRINT_INFO("%d,\n", modified_list.get_and_step());
         [ #  # ][ #  # ]
                 [ #  # ]
    1664                 :            :         else
    1665 [ #  # ][ #  # ]:          0 :           PRINT_INFO("%d, ", modified_list.get_and_step());
         [ #  # ][ #  # ]
                 [ #  # ]
    1666                 :            :       }
    1667                 :            :       else
    1668 [ #  # ][ #  # ]:          0 :         PRINT_INFO("%d.\n", modified_list.get_and_step());
         [ #  # ][ #  # ]
                 [ #  # ]
    1669                 :            :     }
    1670                 :            :   }
    1671 [ #  # ][ #  # ]:          0 :   else if ( modified_list.size() == 0 && print )
         [ #  # ][ #  # ]
    1672 [ #  # ][ #  # ]:          0 :     PRINT_INFO("Imprinting overlaps resulted in no modifications.\n");
         [ #  # ][ #  # ]
    1673                 :            : 
    1674                 :            :     //clean up our memory.
    1675 [ #  # ][ #  # ]:          0 :   for ( i = edges_list1.size(); i > 0; i-- )
    1676                 :            :   {
    1677         [ #  # ]:          0 :     ref_edges1 =  edges_list1.remove();
    1678         [ #  # ]:          0 :     ref_edges2 = edges_list2.remove();
    1679         [ #  # ]:          0 :     delete_edges_in_list(*ref_edges1);
    1680         [ #  # ]:          0 :     delete_edges_in_list(*ref_edges2);
    1681 [ #  # ][ #  # ]:          0 :     delete ref_edges1;
    1682 [ #  # ][ #  # ]:          0 :     delete ref_edges2;
    1683                 :            :   }
    1684         [ #  # ]:          0 :   delete [] body_array;
    1685         [ #  # ]:          0 :   return CUBIT_SUCCESS;
    1686                 :            : }
    1687                 :            : 
    1688                 :          0 : int SurfaceOverlapTool::num_descendants(Body *body)
    1689                 :            : {
    1690                 :          0 :   int counter = 0;
    1691                 :          0 :   counter += body->num_ref_volumes();
    1692                 :          0 :   counter += body->num_ref_faces();
    1693                 :          0 :   counter += body->num_ref_edges();
    1694                 :          0 :   counter += body->num_ref_vertices();
    1695                 :          0 :   return counter;
    1696                 :            : }
    1697                 :            : 
    1698                 :            : //---------------------------------------------------------
    1699                 :            : // copy_edges_in_list:
    1700                 :            : // copy's each of the edges in the old list, and puts the
    1701                 :            : // stand alone edges in the new list.
    1702                 :            : // NOTE: THESE ARE NEWLY CREATED EDGES AND SHOULD BE DELETED
    1703                 :            : //       BY THE CALLING FUNCTION.
    1704                 :            : // Author: David R. White
    1705                 :            : // Date: 1/11/02
    1706                 :            : //---------------------------------------------------------
    1707                 :          0 : CubitStatus SurfaceOverlapTool::copy_edges_in_list(DLIList<RefEdge*> &old_list,
    1708                 :            :                                                    DLIList<RefEdge*> &new_list )
    1709                 :            : {
    1710                 :            :   int i;
    1711                 :            :   RefEdge *curr_edge, *new_edge;
    1712                 :            :   RefEntity *curr_entity, *new_entity;
    1713                 :            :   
    1714 [ #  # ][ #  # ]:          0 :   for ( i = old_list.size(); i > 0; i-- )
    1715                 :            :   {
    1716         [ #  # ]:          0 :     curr_edge = old_list.get_and_step();
    1717         [ #  # ]:          0 :     curr_entity = CAST_TO(curr_edge, RefEntity);
    1718         [ #  # ]:          0 :     assert(curr_entity != NULL );
    1719 [ #  # ][ #  # ]:          0 :     new_entity = GeometryModifyTool::instance()->copy_refentity(curr_entity);
    1720         [ #  # ]:          0 :     new_edge = CAST_TO(new_entity, RefEdge);
    1721         [ #  # ]:          0 :     if ( new_edge == NULL )
    1722                 :            :     {
    1723 [ #  # ][ #  # ]:          0 :       PRINT_ERROR("Problems copying edges for imprinting overlap.\n");
         [ #  # ][ #  # ]
    1724         [ #  # ]:          0 :       assert(new_edge != NULL );
    1725                 :          0 :       return CUBIT_FAILURE;
    1726                 :            :     }
    1727         [ #  # ]:          0 :     new_list.append(new_edge);
    1728                 :            :   }
    1729                 :          0 :   return CUBIT_SUCCESS;
    1730                 :            : }
    1731                 :            : 
    1732                 :            : //Initialize all settings in this class
    1733                 :        874 : void SurfaceOverlapTool::initialize_settings()
    1734                 :            : {
    1735                 :            :   SettingHandler::instance()->add_setting("Overlap Facet BBox Absolute", 
    1736                 :            :     SurfaceOverlapTool::set_facet_abs_tol, 
    1737                 :        874 :     SurfaceOverlapTool::get_facet_abs_tol); 
    1738                 :            :   
    1739                 :            :   SettingHandler::instance()->add_setting("Overlap Facet BBox Angle", 
    1740                 :            :     SurfaceOverlapTool::set_facet_ang_tol_setting, 
    1741                 :        874 :     SurfaceOverlapTool::get_facet_ang_tol_setting);
    1742                 :            :   
    1743                 :            :   SettingHandler::instance()->add_setting("Overlap Minimum Gap", 
    1744                 :            :     SurfaceOverlapTool::set_gap_min, 
    1745                 :        874 :     SurfaceOverlapTool::get_gap_min);  
    1746                 :            :   
    1747                 :            :   SettingHandler::instance()->add_setting("Overlap Maximum Gap",
    1748                 :            :     SurfaceOverlapTool::set_gap_max,
    1749                 :        874 :     SurfaceOverlapTool::get_gap_max);
    1750                 :            :   
    1751                 :            :   SettingHandler::instance()->add_setting("Overlap Minimum Angle", 
    1752                 :            :     SurfaceOverlapTool::set_angle_min, 
    1753                 :        874 :     SurfaceOverlapTool::get_angle_min); 
    1754                 :            :   
    1755                 :            :   SettingHandler::instance()->add_setting("Overlap Maximum Angle", 
    1756                 :            :     SurfaceOverlapTool::set_angle_max, 
    1757                 :        874 :     SurfaceOverlapTool::get_angle_max);
    1758                 :            :   
    1759                 :            :   SettingHandler::instance()->add_setting("Overlap Normal", 
    1760                 :            :     SurfaceOverlapTool::set_normal_type_setting, 
    1761                 :        874 :     SurfaceOverlapTool::get_normal_type_setting);  
    1762                 :            :   
    1763                 :            :   SettingHandler::instance()->add_setting("Overlap Tolerance",
    1764                 :            :     SurfaceOverlapTool::set_overlap_tolerance,
    1765                 :        874 :     SurfaceOverlapTool::get_overlap_tolerance);
    1766                 :            :   
    1767                 :            :   SettingHandler::instance()->add_setting("Overlap Group", 
    1768                 :            :     SurfaceOverlapTool::set_group_results_setting, 
    1769                 :        874 :     SurfaceOverlapTool::get_group_results_setting); 
    1770                 :            :   
    1771                 :            :   SettingHandler::instance()->add_setting("Overlap List", 
    1772                 :            :     SurfaceOverlapTool::set_list_pairs_setting, 
    1773                 :        874 :     SurfaceOverlapTool::get_list_pairs_setting);
    1774                 :            :   
    1775                 :            :   SettingHandler::instance()->add_setting("Overlap Display", 
    1776                 :            :     SurfaceOverlapTool::set_display_pairs_setting, 
    1777                 :        874 :     SurfaceOverlapTool::get_display_pairs_setting);
    1778                 :            :   
    1779                 :            :   SettingHandler::instance()->add_setting("Overlap Within Bodies", 
    1780                 :            :     SurfaceOverlapTool::set_check_within_bodies, 
    1781                 :        874 :     SurfaceOverlapTool::get_check_within_bodies);
    1782                 :            : 
    1783                 :            :   SettingHandler::instance()->add_setting("Overlap Across Bodies", 
    1784                 :            :     SurfaceOverlapTool::set_check_across_bodies, 
    1785                 :        874 :     SurfaceOverlapTool::get_check_across_bodies);
    1786                 :        874 : }
    1787                 :            : 
    1788                 :          0 : CubitStatus SurfaceOverlapTool::delete_edges_in_list(DLIList<RefEdge*> &edge_list )
    1789                 :            : {
    1790                 :            :   int i;
    1791                 :            :   RefEdge *curr_edge;
    1792                 :            :   RefEntity *curr_entity;
    1793                 :            :   
    1794         [ #  # ]:          0 :   for ( i = edge_list.size(); i > 0; i-- )
    1795                 :            :   {
    1796                 :          0 :     curr_edge = edge_list.remove();
    1797         [ #  # ]:          0 :     curr_entity = CAST_TO(curr_edge, RefEntity);
    1798         [ #  # ]:          0 :     assert(curr_entity != NULL );
    1799                 :          0 :     CubitStatus stat = GeometryQueryTool::instance()->delete_RefEntity(curr_entity);
    1800         [ #  # ]:          0 :     if ( stat != CUBIT_SUCCESS )
    1801                 :            :     {
    1802 [ #  # ][ #  # ]:          0 :       PRINT_ERROR("Problems deleting edges after imprinting overlaps\n");
    1803                 :          0 :       assert(0);
    1804                 :            :       return CUBIT_FAILURE;
    1805                 :            :     }
    1806                 :            :   }
    1807                 :          0 :   return CUBIT_SUCCESS;
    1808                 :            : }
    1809                 :            : 
    1810                 :          0 : CubitStatus SurfaceOverlapTool::find_overlapping_curves( DLIList<Curve*> &curve_list,
    1811                 :            :                                 DLIList< DLIList<Curve*> *> &overlapping_curve_lists,
    1812                 :            :                                 std::map<Curve*, DLIList<Curve*>* > &curve_to_list_map,
    1813                 :            :                                 std::multimap<BodySM*, CubitVector> &body_point_imprint_map,
    1814                 :            :                                 double overlap_tol)
    1815                 :            : {
    1816                 :            :   int i;
    1817                 :            : 
    1818                 :            :   //put all the curves into a tree 
    1819         [ #  # ]:          0 :   double tolerance = GeometryQueryTool::get_geometry_factor()*GEOMETRY_RESABS;
    1820         [ #  # ]:          0 :   if(overlap_tol > 0.0)
    1821                 :          0 :     tolerance = overlap_tol;
    1822                 :            : 
    1823                 :            :   // Populate the Surface AbstractTree
    1824 [ #  # ][ #  # ]:          0 :   AbstractTree<Curve*> *a_tree = new RTree<Curve*>( tolerance );
    1825         [ #  # ]:          0 :   curve_list.reset();
    1826 [ #  # ][ #  # ]:          0 :   for( i=curve_list.size(); i--; )
    1827                 :            :   {
    1828         [ #  # ]:          0 :     Curve *curve = curve_list.get_and_step();
    1829         [ #  # ]:          0 :     a_tree->add( curve );
    1830                 :            :   }
    1831                 :            : 
    1832         [ #  # ]:          0 :   std::map<Curve*, DLIList<CurveOverlapFacet*>* > facet_map;
    1833         [ #  # ]:          0 :   std::map<Curve*, DLIList<Curve*>* >::iterator list_iter; 
    1834                 :            : 
    1835         [ #  # ]:          0 :   curve_list.reset();
    1836 [ #  # ][ #  # ]:          0 :   for( i=curve_list.size(); i--; )
    1837                 :            :   {
    1838         [ #  # ]:          0 :     Curve *curve1= curve_list.get_and_step();
    1839                 :            : 
    1840         [ #  # ]:          0 :     BodySM *curve1_body = curve1->bodysm();
    1841                 :            : 
    1842                 :            :     // Remove this surface from AbstractTree so it is not found and never
    1843                 :            :     // found again
    1844         [ #  # ]:          0 :     a_tree->remove( curve1 );
    1845                 :            : 
    1846                 :            :     // Find RefFaces from AbstractTree that are within range of this surface
    1847         [ #  # ]:          0 :     CubitBox curve1_box = curve1->bounding_box();
    1848 [ #  # ][ #  # ]:          0 :     DLIList<Curve*> close_curves;
    1849         [ #  # ]:          0 :     a_tree->find( curve1_box, close_curves );
    1850                 :            : 
    1851                 :            :     int j;
    1852 [ #  # ][ #  # ]:          0 :     for( j=close_curves.size(); j--; )
    1853                 :            :     {
    1854         [ #  # ]:          0 :       Curve *curve2 = close_curves.get_and_step();
    1855         [ #  # ]:          0 :       BodySM *curve2_body = curve2->bodysm();
    1856                 :            :       
    1857         [ #  # ]:          0 :       if( curve2_body == curve1_body )
    1858                 :          0 :         continue;
    1859                 :            : 
    1860         [ #  # ]:          0 :       std::multimap<BodySM*, CubitVector> tmp_body_point_imprint_map;
    1861                 :            : 
    1862 [ #  # ][ #  # ]:          0 :       if( check_overlap( curve1, curve2, &facet_map, &tmp_body_point_imprint_map, overlap_tol  ) ) 
    1863                 :            :       {
    1864                 :            :         //check to see if the curve1 is already overlapping with another curve 
    1865         [ #  # ]:          0 :         list_iter = curve_to_list_map.find( curve1 );
    1866 [ #  # ][ #  # ]:          0 :         if( list_iter != curve_to_list_map.end() )
                 [ #  # ]
    1867                 :            :         {
    1868         [ #  # ]:          0 :           DLIList<Curve*> *tmp_curve_list = list_iter->second;
    1869         [ #  # ]:          0 :           tmp_curve_list->append( curve2 );
    1870                 :            :         }
    1871                 :            :         else
    1872                 :            :         {
    1873                 :            :           //list for curve 1 does not exist....make a new one
    1874 [ #  # ][ #  # ]:          0 :           DLIList<Curve*> *tmp_curve_list = new DLIList<Curve*>;
    1875         [ #  # ]:          0 :           overlapping_curve_lists.append( tmp_curve_list );
    1876         [ #  # ]:          0 :           tmp_curve_list->append( curve1 );
    1877         [ #  # ]:          0 :           tmp_curve_list->append( curve2 );
    1878                 :            :           curve_to_list_map.insert( std::map<Curve*,
    1879 [ #  # ][ #  # ]:          0 :                DLIList<Curve*>*>::value_type( curve1, tmp_curve_list ));
    1880                 :            :         }
    1881                 :            :       }
    1882                 :            :       else
    1883                 :            :       {
    1884                 :            :         //append what's in body_point_imprint_map to one passed in
    1885                 :            :         body_point_imprint_map.insert( tmp_body_point_imprint_map.begin(), 
    1886 [ #  # ][ #  # ]:          0 :                                        tmp_body_point_imprint_map.end() ); 
                 [ #  # ]
    1887                 :            :       }
    1888         [ #  # ]:          0 :     }
    1889         [ #  # ]:          0 :   }
    1890                 :            : 
    1891                 :            :   //clean up facet map
    1892         [ #  # ]:          0 :   std::map<Curve*, DLIList<CurveOverlapFacet*>* >::iterator facet_iter;
    1893         [ #  # ]:          0 :   facet_iter=facet_map.begin(); 
    1894 [ #  # ][ #  # ]:          0 :   for(; facet_iter != facet_map.end(); facet_iter++ )
         [ #  # ][ #  # ]
    1895                 :            :   {
    1896         [ #  # ]:          0 :     DLIList<CurveOverlapFacet*> *co_facet_list = facet_iter->second;
    1897                 :            : 
    1898                 :            :     //delete all the facets in the list
    1899 [ #  # ][ #  # ]:          0 :     for( i=co_facet_list->size(); i--; )
    1900 [ #  # ][ #  # ]:          0 :       delete co_facet_list->get_and_step();
                 [ #  # ]
    1901 [ #  # ][ #  # ]:          0 :     delete co_facet_list;
    1902                 :            :   }
    1903                 :            : 
    1904 [ #  # ][ #  # ]:          0 :   delete a_tree;
    1905                 :            : 
    1906         [ #  # ]:          0 :   return CUBIT_SUCCESS;
    1907                 :            : }
    1908                 :            : 
    1909                 :          0 : CubitStatus SurfaceOverlapTool::find_overlapping_curves( DLIList<Surface*> &surf_list,
    1910                 :            :                                 DLIList< DLIList<Curve*> *> &overlapping_curve_lists,
    1911                 :            :                                 std::map<Curve*, DLIList<Curve*>* > &curve_to_list_map,
    1912                 :            :                                 std::multimap<BodySM*, CubitVector> &body_point_imprint_map)
    1913                 :            : {
    1914                 :            :   //collect all the surfaces
    1915         [ #  # ]:          0 :   DLIList<Curve*> curve_list;
    1916                 :            :   int i;
    1917 [ #  # ][ #  # ]:          0 :   for( i=surf_list.size(); i--; )
    1918                 :            :   {
    1919         [ #  # ]:          0 :     Surface *surf = surf_list.get_and_step();
    1920         [ #  # ]:          0 :     DLIList<Curve*> curves;
    1921         [ #  # ]:          0 :     surf->curves_ignore_virtual(curves, false);
    1922                 :            : //    body_sm->curves( curves);
    1923         [ #  # ]:          0 :     curve_list.merge_unique( curves );
    1924         [ #  # ]:          0 :   }
    1925                 :            : 
    1926                 :            :   return find_overlapping_curves(curve_list, overlapping_curve_lists, curve_to_list_map,
    1927 [ #  # ][ #  # ]:          0 :     body_point_imprint_map);
    1928                 :            : }
    1929                 :            : 
    1930                 :          0 : CubitStatus SurfaceOverlapTool::find_overlapping_curves( DLIList<BodySM*> &body_list,
    1931                 :            :                                 DLIList< DLIList<Curve*> *> &overlapping_curve_lists,
    1932                 :            :                                 std::map<Curve*, DLIList<Curve*>* > &curve_to_list_map,
    1933                 :            :                                 std::multimap<BodySM*, CubitVector> &body_point_imprint_map,
    1934                 :            :                                 double overlap_tol)
    1935                 :            : {
    1936                 :            :   //collect all the surfaces
    1937         [ #  # ]:          0 :   DLIList<Curve*> curve_list;
    1938                 :            :   int i;
    1939 [ #  # ][ #  # ]:          0 :   for( i=body_list.size(); i--; )
    1940                 :            :   {
    1941         [ #  # ]:          0 :     BodySM *body_sm = body_list.get_and_step();
    1942         [ #  # ]:          0 :     DLIList<Curve*> curves;
    1943         [ #  # ]:          0 :     body_sm->curves_ignore_virtual(curves, false);
    1944                 :            : //    body_sm->curves( curves);
    1945         [ #  # ]:          0 :     curve_list.merge_unique( curves );
    1946         [ #  # ]:          0 :   }
    1947                 :            : 
    1948                 :            :   return find_overlapping_curves(curve_list, overlapping_curve_lists, curve_to_list_map,
    1949 [ #  # ][ #  # ]:          0 :     body_point_imprint_map, overlap_tol);
    1950                 :            : 
    1951                 :            : }
    1952                 :            : 
    1953                 :          0 : CubitStatus SurfaceOverlapTool::find_overlapping_curves( DLIList<Body*> &bodies,
    1954                 :            :                                 std::multimap<RefEdge*, RefEdge*> &overlapping_edge_map,
    1955                 :            :                                 double maxgap /* =-1*/)
    1956                 :            : {
    1957         [ #  # ]:          0 :   std::map<RefEdge*, DLIList<CurveOverlapFacet*>* >::iterator list_iter; 
    1958                 :            : 
    1959         [ #  # ]:          0 :   double tolerance = GeometryQueryTool::get_geometry_factor()*GEOMETRY_RESABS;
    1960 [ #  # ][ #  # ]:          0 :   if (maxgap != -1 && maxgap != 0.0)
    1961                 :          0 :       tolerance = maxgap;
    1962                 :            : 
    1963                 :            :   int i,j;
    1964         [ #  # ]:          0 :   DLIList<Body*> tmp_body_list = bodies;
    1965 [ #  # ][ #  # ]:          0 :   for(i=tmp_body_list.size(); i--; )
    1966                 :            :   {
    1967         [ #  # ]:          0 :     Body *tmp_body1 = tmp_body_list.pop();
    1968                 :            : 
    1969         [ #  # ]:          0 :     tmp_body_list.reset();
    1970 [ #  # ][ #  # ]:          0 :     for(j=tmp_body_list.size(); j--; )
    1971                 :            :     {
    1972         [ #  # ]:          0 :       Body *tmp_body2 = tmp_body_list.get_and_step();
    1973                 :            : 
    1974                 :            :       //determine if bodies are close enough to have overlapping curves
    1975         [ #  # ]:          0 :       CubitBox body_box1 = tmp_body1->bounding_box();
    1976 [ #  # ][ #  # ]:          0 :       CubitBox body_box2 = tmp_body2->bounding_box();
                 [ #  # ]
    1977                 :            : 
    1978 [ #  # ][ #  # ]:          0 :       if( body_box1.overlap( tolerance, body_box2 ) )
    1979                 :            :       {
    1980                 :            :         // create list of edges on each body.
    1981                 :            :             // put the edges in body2 in edges1 so edges1 contains
    1982                 :            :                 // the list of curves contained in the body listed first in the list
    1983 [ #  # ][ #  # ]:          0 :         DLIList<RefEdge*> edges1, edges2;
         [ #  # ][ #  # ]
    1984         [ #  # ]:          0 :         tmp_body1->ref_edges( edges2 );
    1985         [ #  # ]:          0 :         tmp_body2->ref_edges( edges1 );
    1986                 :            :                 
    1987                 :            :                 //check individual curves for bounding box interference
    1988 [ #  # ][ #  # ]:          0 :         if (!find_overlapping_curves(edges1, edges2, overlapping_edge_map, tolerance))
    1989 [ #  # ][ #  # ]:          0 :                         return CUBIT_FAILURE;
         [ #  # ][ #  # ]
    1990                 :            :       }
    1991                 :          0 :     }
    1992                 :            :   }
    1993                 :            : 
    1994         [ #  # ]:          0 :   return CUBIT_SUCCESS;
    1995                 :            : }
    1996                 :            : 
    1997                 :          0 : CubitStatus SurfaceOverlapTool::find_overlapping_curves( DLIList<RefFace*> &faces,
    1998                 :            :                         std::multimap<RefEdge*, RefEdge*> &overlapping_edge_map,
    1999                 :            :                         double maxgap /*=-1*/)
    2000                 :            : {
    2001                 :          0 :   double tolerance = GeometryQueryTool::get_geometry_factor()*GEOMETRY_RESABS;
    2002                 :            : 
    2003 [ #  # ][ #  # ]:          0 :   if (maxgap != -1 && maxgap != 0.0)
    2004                 :          0 :       tolerance = maxgap;
    2005                 :            : 
    2006                 :            :   // Populate the Surface AbstractTree
    2007         [ #  # ]:          0 :   AbstractTree<RefFace*> *a_tree = new RTree<RefFace*>( tolerance );
    2008                 :          0 :   faces.reset();
    2009                 :            :   int i;
    2010         [ #  # ]:          0 :   for( i=faces.size(); i--; )
    2011                 :            :   {
    2012                 :          0 :     RefFace* face = faces.get_and_step();
    2013                 :          0 :     a_tree->add( face );
    2014                 :            :   }
    2015                 :            : 
    2016                 :          0 :   faces.reset();
    2017         [ #  # ]:          0 :   for( i=faces.size(); i--; )
    2018                 :            :   {
    2019         [ #  # ]:          0 :     RefFace* face1 = faces.get_and_step();
    2020                 :            : 
    2021         [ #  # ]:          0 :     Body *face1_body = face1->body();
    2022                 :            : 
    2023                 :            :     // Remove this face from AbstractTree so it is not found and never
    2024                 :            :     // found again
    2025         [ #  # ]:          0 :     a_tree->remove( face1 );
    2026                 :            : 
    2027                 :            :     // Find RefFaces from AbstractTree that are within range of this surface
    2028         [ #  # ]:          0 :     CubitBox face1_box = face1->bounding_box();
    2029 [ #  # ][ #  # ]:          0 :     DLIList<RefFace*> close_faces;
                 [ #  # ]
    2030         [ #  # ]:          0 :     a_tree->find( face1_box, close_faces );
    2031                 :            : 
    2032                 :            :     int j;
    2033 [ #  # ][ #  # ]:          0 :     for( j=close_faces.size(); j--; )
         [ #  # ][ #  # ]
    2034                 :            :     {
    2035         [ #  # ]:          0 :       RefFace *face2 = close_faces.get_and_step();
    2036         [ #  # ]:          0 :       Body *face2_body = face2->body();
    2037                 :            : 
    2038                 :            :       //don't check for overlapping faces within bodies
    2039         [ #  # ]:          0 :       if( face1_body == face2_body )
    2040                 :          0 :               continue;
    2041                 :            : 
    2042 [ #  # ][ #  # ]:          0 :       DLIList<RefEdge*> edges1, edges2;
         [ #  # ][ #  # ]
    2043         [ #  # ]:          0 :       face1->ref_edges( edges1 );
    2044         [ #  # ]:          0 :       face2->ref_edges( edges2 );
    2045                 :            : 
    2046 [ #  # ][ #  # ]:          0 :       if (!find_overlapping_curves(edges1, edges2, overlapping_edge_map, tolerance))
    2047 [ #  # ][ #  # ]:          0 :               return CUBIT_FAILURE;
    2048                 :            :             
    2049                 :          0 :     }
    2050                 :          0 :   }
    2051                 :          0 :   return CUBIT_SUCCESS;
    2052                 :            : }
    2053                 :            : 
    2054                 :          0 : CubitStatus SurfaceOverlapTool::find_overlapping_curves( DLIList<RefEdge*> &edgelist,
    2055                 :            :                                 std::multimap<RefEdge*, RefEdge*> &overlapping_edge_map, double maxgap /*=-1*/ )
    2056                 :            : {
    2057                 :            :   // for now, check every edge against every other edge
    2058                 :            :   // (the helper function will skip edges if they are in the same body)
    2059                 :            : 
    2060         [ #  # ]:          0 :   DLIList<RefEdge*> edgelist2 = edgelist;
    2061 [ #  # ][ #  # ]:          0 :   return find_overlapping_curves(edgelist, edgelist2, overlapping_edge_map, maxgap);
    2062                 :            : }
    2063                 :            : 
    2064                 :          0 : CubitStatus SurfaceOverlapTool::find_overlapping_curves( DLIList<RefEdge*> &edges1, DLIList<RefEdge*> &edges2,
    2065                 :            :                                               std::multimap<RefEdge*, RefEdge*> &overlapping_edge_map,
    2066                 :            :                                               double maxgap /*=-1*/)
    2067                 :            : {
    2068                 :            :   int i;
    2069         [ #  # ]:          0 :   std::map<RefEdge*, DLIList<CurveOverlapFacet*>* > facet_map; 
    2070         [ #  # ]:          0 :   std::map<RefEdge*, DLIList<CurveOverlapFacet*>* >::iterator list_iter; 
    2071                 :            : 
    2072         [ #  # ]:          0 :   double tolerance = GeometryQueryTool::get_geometry_factor()*GEOMETRY_RESABS;
    2073                 :            : 
    2074 [ #  # ][ #  # ]:          0 :   if (maxgap != -1 && maxgap != 0.0)
    2075                 :          0 :       tolerance = maxgap;
    2076                 :            : 
    2077 [ #  # ][ #  # ]:          0 :   for( i=edges1.size(); i--; )
    2078                 :            :   {
    2079         [ #  # ]:          0 :     RefEdge *edge1 = edges1.get_and_step(); 
    2080                 :            : 
    2081                 :            :     int j;
    2082 [ #  # ][ #  # ]:          0 :     for( j=edges2.size(); j--; )
    2083                 :            :     {
    2084         [ #  # ]:          0 :       RefEdge *edge2 = edges2.get_and_step();
    2085                 :            :       
    2086         [ #  # ]:          0 :       if( edge1 == edge2 )
    2087                 :          0 :         continue;
    2088                 :            : 
    2089         [ #  # ]:          0 :       std::multimap<RefEdge*, RefEdge*>::iterator it;
    2090         [ #  # ]:          0 :       it = overlapping_edge_map.find( edge2 );
    2091 [ #  # ][ #  # ]:          0 :       if( it != overlapping_edge_map.end() )
                 [ #  # ]
    2092                 :          0 :         continue;
    2093                 :            : 
    2094                 :            :       // check to see if this pair is already in the overlap map.
    2095                 :            :       //  If it is, don't need to bother checking it for overlap
    2096                 :            :      /* 
    2097                 :            :       std::multimap<RefEdge*, RefEdge*>::iterator it;
    2098                 :            :       std::pair< std::multimap<RefEdge*,RefEdge*>::iterator, std::multimap<RefEdge*,RefEdge*>::iterator > range;
    2099                 :            : 
    2100                 :            :       range = overlapping_edge_map.equal_range(edge1);
    2101                 :            : 
    2102                 :            :       for (it=range.first; it!=range.second; it++)
    2103                 :            :       {
    2104                 :            :         if ( (it->second == edge2) && (it->first == edge1) )
    2105                 :            :                 pair_already_in_map = true;
    2106                 :            :       }
    2107                 :            :       if (pair_already_in_map)
    2108                 :            :         continue; 
    2109                 :            : 
    2110                 :            :       */
    2111                 :            : 
    2112 [ #  # ][ #  # ]:          0 :       if( check_overlap( edge1, edge2, &facet_map, &tolerance ) ) 
    2113                 :            :       {
    2114                 :            :         overlapping_edge_map.insert( std::multimap<RefEdge*, RefEdge*>
    2115 [ #  # ][ #  # ]:          0 :                       ::value_type( edge1, edge2));
    2116                 :            :         //PRINT_INFO("Curve %d and %d overlap.\n", edge1->id(), edge2->id());
    2117                 :            :         //PRINT_INFO("Insert Curve %d, Curve %d.\n", edge1->id(), edge2->id());
    2118                 :            :       }
    2119                 :            :     }
    2120                 :            :   }
    2121                 :            :   //clean up facet map
    2122         [ #  # ]:          0 :   list_iter=facet_map.begin(); 
    2123 [ #  # ][ #  # ]:          0 :   for(; list_iter != facet_map.end(); list_iter++ )
         [ #  # ][ #  # ]
    2124                 :            :   {
    2125         [ #  # ]:          0 :     DLIList<CurveOverlapFacet*> *co_facet_list = list_iter->second;
    2126                 :            :     //delete all the facets in the list
    2127 [ #  # ][ #  # ]:          0 :     for( i=co_facet_list->size(); i--; )
    2128 [ #  # ][ #  # ]:          0 :             delete co_facet_list->get_and_step();
                 [ #  # ]
    2129 [ #  # ][ #  # ]:          0 :     delete co_facet_list;
    2130                 :            :   }
    2131                 :            : 
    2132         [ #  # ]:          0 :   return CUBIT_SUCCESS;
    2133                 :            : }
    2134                 :            : 
    2135                 :          0 : CubitBoolean SurfaceOverlapTool::check_overlap( Curve *curve1, Curve *curve2, 
    2136                 :            :   std::map<Curve*, DLIList<CurveOverlapFacet*>* > *facet_map,
    2137                 :            :   std::multimap<BodySM*,CubitVector> *body_point_imprint_map,
    2138                 :            :   double overlap_tol) 
    2139                 :            : {
    2140                 :            :   //if surfaces are not splines and are not of the same type, 
    2141                 :            :   //they won't overlap
    2142         [ #  # ]:          0 :   GeometryType curve1_type = curve1->geometry_type();
    2143         [ #  # ]:          0 :   GeometryType curve2_type = curve2->geometry_type();
    2144                 :            : 
    2145         [ #  # ]:          0 :   double tolerance = GeometryQueryTool::get_geometry_factor()*GEOMETRY_RESABS;
    2146         [ #  # ]:          0 :   if(overlap_tol > 0.0)
    2147                 :          0 :     tolerance = overlap_tol;
    2148                 :            : 
    2149                 :          0 :   double facet_tol = tolerance*10;
    2150                 :            : 
    2151         [ #  # ]:          0 :   std::map<Curve*, DLIList<CurveOverlapFacet*>* >::iterator facet_iterator;
    2152                 :            : 
    2153         [ #  # ]:          0 :   CubitBox curve_box1 = curve1->bounding_box(); 
    2154 [ #  # ][ #  # ]:          0 :   CubitBox curve_box2 = curve2->bounding_box();
    2155                 :            : 
    2156                 :          0 :   DLIList<CurveOverlapFacet*> *facet_list1 = NULL;
    2157                 :          0 :   DLIList<CurveOverlapFacet*> *facet_list2 = NULL;
    2158                 :            : 
    2159                 :            :   //see if curve is in map...if not we have to create faceting for it.
    2160         [ #  # ]:          0 :   if( facet_map )
    2161         [ #  # ]:          0 :     facet_iterator = facet_map->find( curve1 );
    2162                 :            : 
    2163 [ #  # ][ #  # ]:          0 :   if( facet_map == NULL || facet_iterator == facet_map->end() )
         [ #  # ][ #  # ]
                 [ #  # ]
           [ #  #  #  # ]
    2164                 :            :   {
    2165 [ #  # ][ #  # ]:          0 :     facet_list1 = new DLIList<CurveOverlapFacet*>;
    2166                 :            : 
    2167         [ #  # ]:          0 :     GMem curve_graphics;
    2168         [ #  # ]:          0 :     curve1->get_geometry_query_engine()->get_graphics( curve1, 
    2169         [ #  # ]:          0 :       &curve_graphics, 0, facet_tol );
    2170                 :            : 
    2171         [ #  # ]:          0 :     GPoint *points = curve_graphics.point_list();
    2172                 :          0 :     int num_points = curve_graphics.pointListCount;
    2173                 :            : 
    2174                 :            :     int kk;
    2175         [ #  # ]:          0 :     for( kk=0; kk<num_points-1; kk++ )
    2176                 :            :     {
    2177                 :            :       //create a new CurveOverlapFacets
    2178                 :            :       GPoint gpoints[2];
    2179                 :          0 :       gpoints[0] = points[kk];
    2180                 :          0 :       gpoints[1] = points[kk + 1];
    2181                 :            : 
    2182 [ #  # ][ #  # ]:          0 :       CurveOverlapFacet *tmp_facet = new CurveOverlapFacet( gpoints ); 
    2183         [ #  # ]:          0 :       facet_list1->append( tmp_facet );
    2184                 :            :     }
    2185                 :            : 
    2186         [ #  # ]:          0 :     if( facet_map )
    2187                 :            :       facet_map->insert( std::map<Curve*, 
    2188 [ #  # ][ #  # ]:          0 :       DLIList<CurveOverlapFacet*>*>::value_type( curve1, facet_list1 )); 
                 [ #  # ]
    2189                 :            : 
    2190                 :            :   }
    2191                 :            :   else
    2192         [ #  # ]:          0 :     facet_list1 = (*facet_iterator).second;
    2193                 :            : 
    2194                 :            :   //see if edge is in map...if not we have to create facet for it.
    2195         [ #  # ]:          0 :   if( facet_map )
    2196         [ #  # ]:          0 :     facet_iterator = facet_map->find( curve2 );
    2197                 :            : 
    2198 [ #  # ][ #  # ]:          0 :   if( facet_map == NULL || facet_iterator == facet_map->end() )
         [ #  # ][ #  # ]
                 [ #  # ]
           [ #  #  #  # ]
    2199                 :            :   {
    2200 [ #  # ][ #  # ]:          0 :     facet_list2 = new DLIList<CurveOverlapFacet*>;
    2201                 :            : 
    2202         [ #  # ]:          0 :     GMem curve_graphics;
    2203         [ #  # ]:          0 :     curve2->get_geometry_query_engine()->get_graphics( curve2, 
    2204         [ #  # ]:          0 :       &curve_graphics, 0, facet_tol );    
    2205                 :            : 
    2206         [ #  # ]:          0 :     GPoint *points = curve_graphics.point_list();
    2207                 :          0 :     int num_points = curve_graphics.pointListCount;
    2208                 :            : 
    2209                 :            :     int kk;
    2210         [ #  # ]:          0 :     for( kk=0; kk<num_points-1; kk++ )
    2211                 :            :     {
    2212                 :            :       //create a new CurveOverlapFacets
    2213                 :            :       GPoint gpoints[2];
    2214                 :          0 :       gpoints[0] = points[kk];
    2215                 :          0 :       gpoints[1] = points[kk + 1];
    2216                 :            : 
    2217 [ #  # ][ #  # ]:          0 :       CurveOverlapFacet *tmp_facet = new CurveOverlapFacet( gpoints ); 
    2218         [ #  # ]:          0 :       facet_list2->append( tmp_facet );
    2219                 :            :     }
    2220                 :            : 
    2221         [ #  # ]:          0 :     if( facet_map )
    2222                 :            :       facet_map->insert( std::map<Curve*, 
    2223 [ #  # ][ #  # ]:          0 :       DLIList<CurveOverlapFacet*>*>::value_type( curve2, facet_list2 )); 
                 [ #  # ]
    2224                 :            :   }
    2225                 :            :   else
    2226         [ #  # ]:          0 :     facet_list2 = (*facet_iterator).second;
    2227                 :            : 
    2228                 :          0 :   bool overlap = false;
    2229                 :            :   int kk;
    2230 [ #  # ][ #  # ]:          0 :   if(facet_list1->size() > 0 && facet_list2->size() > 0)
         [ #  # ][ #  # ]
                 [ #  # ]
    2231                 :            :   {
    2232                 :            :     //compare smaller list to larger list...want list1 to be smaller
    2233 [ #  # ][ #  # ]:          0 :     if( facet_list1->size() > facet_list2->size() )
                 [ #  # ]
    2234                 :            :     {
    2235                 :          0 :       DLIList<CurveOverlapFacet*> *tmp_list = facet_list1;
    2236                 :          0 :       facet_list1 = facet_list2;
    2237                 :          0 :       facet_list2 = tmp_list;
    2238                 :          0 :       Curve *tmp_curve = curve1;
    2239                 :          0 :       curve1 = curve2;
    2240                 :          0 :       curve2 = tmp_curve;
    2241                 :            :     }
    2242                 :            : 
    2243         [ #  # ]:          0 :     CubitVector curv1_start_pt;
    2244         [ #  # ]:          0 :     CubitVector curv2_start_pt;
    2245         [ #  # ]:          0 :     CubitVector curv1_end_pt;
    2246         [ #  # ]:          0 :     CubitVector curv2_end_pt;
    2247                 :            : 
    2248         [ #  # ]:          0 :     facet_list1->reset();
    2249         [ #  # ]:          0 :     facet_list2->reset();
    2250                 :            :     //get start/end facet points on curves
    2251         [ #  # ]:          0 :     if( body_point_imprint_map )
    2252                 :            :     {
    2253 [ #  # ][ #  # ]:          0 :       curv1_start_pt = facet_list1->get()->start_point();
                 [ #  # ]
    2254 [ #  # ][ #  # ]:          0 :       curv2_start_pt = facet_list2->get()->start_point();
                 [ #  # ]
    2255         [ #  # ]:          0 :       facet_list1->last();
    2256         [ #  # ]:          0 :       facet_list2->last();
    2257 [ #  # ][ #  # ]:          0 :       curv1_end_pt = facet_list1->get()->end_point();
                 [ #  # ]
    2258 [ #  # ][ #  # ]:          0 :       curv2_end_pt = facet_list2->get()->end_point();
                 [ #  # ]
    2259                 :            :     }
    2260                 :            : 
    2261         [ #  # ]:          0 :     facet_list1->reset();
    2262         [ #  # ]:          0 :     facet_list2->reset();
    2263                 :          0 :     double total_overlap = 0.0;
    2264                 :            :     //now determine if enough curve facets overlap
    2265                 :            : 
    2266         [ #  # ]:          0 :     int list_size1 = facet_list1->size();
    2267         [ #  # ]:          0 :     for( kk=list_size1; kk--; )
    2268                 :            :     {
    2269         [ #  # ]:          0 :       CurveOverlapFacet *curr_facet = facet_list1->get_and_step();
    2270 [ #  # ][ #  # ]:          0 :       if( curr_facet->length() < GEOMETRY_RESABS )
    2271                 :          0 :         continue;
    2272                 :            : 
    2273                 :            :       //if 'curr_facet' is the first or last facet in curve1,
    2274                 :            :       //check to see if the start/end point is on curve2, and not coincident
    2275                 :            :       //with the start/end point of curve2.  If so, it's a case where you need
    2276                 :            :       //to imprint this start/end point onto the body of curve2.
    2277         [ #  # ]:          0 :       if( body_point_imprint_map )
    2278                 :            :       {
    2279         [ #  # ]:          0 :         if( kk == list_size1-1 ) //first facet in curve1
    2280                 :            :         {
    2281         [ #  # ]:          0 :           CubitVector closest_point;
    2282 [ #  # ][ #  # ]:          0 :           curve2->closest_point_trimmed( curr_facet->start_point(), closest_point );
    2283 [ #  # ][ #  # ]:          0 :           if( curv2_start_pt.distance_between( curr_facet->start_point() ) > tolerance &&  
         [ #  # ][ #  # ]
           [ #  #  #  # ]
    2284 [ #  # ][ #  # ]:          0 :               curv2_end_pt.distance_between( curr_facet->start_point() ) > tolerance &&  
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
    2285 [ #  # ][ #  # ]:          0 :               closest_point.distance_between( curr_facet->start_point() ) < tolerance )
         [ #  # ][ #  # ]
    2286                 :            :           {
    2287         [ #  # ]:          0 :             BodySM *tmp_body_sm = curve2->bodysm();
    2288                 :            :             body_point_imprint_map->insert( std::multimap<BodySM*, 
    2289                 :            :               CubitVector>::value_type(
    2290 [ #  # ][ #  # ]:          0 :               tmp_body_sm, closest_point)); 
    2291                 :            :           }
    2292                 :            :         }
    2293         [ #  # ]:          0 :         if( kk == 0 ) //last facet in curve1 
    2294                 :            :         {
    2295         [ #  # ]:          0 :           CubitVector closest_point;
    2296 [ #  # ][ #  # ]:          0 :           curve2->closest_point_trimmed( curr_facet->end_point(), closest_point );
    2297 [ #  # ][ #  # ]:          0 :           if( curv2_start_pt.distance_between( curr_facet->end_point() ) > tolerance &&  
         [ #  # ][ #  # ]
           [ #  #  #  # ]
    2298 [ #  # ][ #  # ]:          0 :               curv2_end_pt.distance_between( curr_facet->end_point() ) > tolerance &&  
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
    2299 [ #  # ][ #  # ]:          0 :               closest_point.distance_between( curr_facet->end_point() ) < tolerance ) 
         [ #  # ][ #  # ]
    2300                 :            :           {
    2301                 :            :             //insert into vertex-volume imprint map
    2302         [ #  # ]:          0 :             BodySM *tmp_body_sm = curve2->bodysm();
    2303                 :            :             body_point_imprint_map->insert( std::multimap<BodySM*, 
    2304                 :            :               CubitVector>::value_type(
    2305 [ #  # ][ #  # ]:          0 :               tmp_body_sm, closest_point)); 
    2306                 :            :           }
    2307                 :            :         }
    2308                 :            :       } 
    2309                 :            : 
    2310                 :            :       //do bounding boxes of facets overlap?
    2311                 :            :       int jj;
    2312         [ #  # ]:          0 :       int list_size2 = facet_list2->size();
    2313         [ #  # ]:          0 :       for( jj=list_size2; jj--; )
    2314                 :            :       {
    2315         [ #  # ]:          0 :         CurveOverlapFacet *other_facet = facet_list2->get_and_step();
    2316                 :            : 
    2317 [ #  # ][ #  # ]:          0 :         if( curr_facet->bbox_overlap( tolerance, other_facet ) )
    2318                 :            :         {
    2319         [ #  # ]:          0 :           double distance_between_facets = curr_facet->facet_to_facet_distance( other_facet );
    2320         [ #  # ]:          0 :           if( distance_between_facets < tolerance+facet_tol )
    2321                 :            :           {
    2322 [ #  # ][ #  # ]:          0 :             if( other_facet->length() < GEOMETRY_RESABS ) 
    2323                 :          0 :               continue;
    2324                 :            : 
    2325                 :            :             //if 'other_facet' is the first or last facet in curve1,
    2326                 :            :             //check to see if the start/end point is on curve1, and not coincident
    2327                 :            :             //with the start/end point of curve1.  If so, it's a case where you need
    2328                 :            :             //to imprint this start/end point onto the body of curve1.
    2329         [ #  # ]:          0 :             if( body_point_imprint_map  )
    2330                 :            :             {
    2331         [ #  # ]:          0 :               if( jj == list_size2-1 ) //first facet in curve1
    2332                 :            :               {
    2333         [ #  # ]:          0 :                 CubitVector closest_point;
    2334 [ #  # ][ #  # ]:          0 :                 curve1->closest_point_trimmed( other_facet->start_point(), closest_point );
    2335                 :            : 
    2336 [ #  # ][ #  # ]:          0 :                 if( curv1_start_pt.distance_between( other_facet->start_point() ) > tolerance &&  
         [ #  # ][ #  # ]
           [ #  #  #  # ]
    2337 [ #  # ][ #  # ]:          0 :                     curv1_end_pt.distance_between( other_facet->start_point() ) > tolerance &&  
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
    2338 [ #  # ][ #  # ]:          0 :                     closest_point.distance_between( other_facet->start_point() ) < tolerance )
         [ #  # ][ #  # ]
    2339                 :            :                 {
    2340                 :            :                   //insert into vertex-volume imprint map
    2341         [ #  # ]:          0 :                   BodySM *tmp_body_sm = curve1->bodysm();
    2342                 :            :                   body_point_imprint_map->insert( std::multimap<BodySM*, 
    2343                 :            :                     CubitVector>::value_type(
    2344 [ #  # ][ #  # ]:          0 :                     tmp_body_sm, closest_point )); 
    2345                 :            :                 }
    2346                 :            :               }
    2347         [ #  # ]:          0 :               if( jj == 0 ) //last facet in curve1 
    2348                 :            :               {
    2349         [ #  # ]:          0 :                 CubitVector closest_point;
    2350 [ #  # ][ #  # ]:          0 :                 curve1->closest_point_trimmed( other_facet->end_point(), closest_point );
    2351 [ #  # ][ #  # ]:          0 :                 if( curv1_start_pt.distance_between( other_facet->end_point() ) > tolerance &&  
         [ #  # ][ #  # ]
           [ #  #  #  # ]
    2352 [ #  # ][ #  # ]:          0 :                     curv1_end_pt.distance_between( other_facet->end_point() ) > tolerance &&  
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
    2353 [ #  # ][ #  # ]:          0 :                     closest_point.distance_between( other_facet->end_point() ) < tolerance )
         [ #  # ][ #  # ]
    2354                 :            :                 {
    2355                 :            :                   //insert into vertex-volume imprint map
    2356         [ #  # ]:          0 :                   BodySM *tmp_body_sm = curve1->bodysm();
    2357                 :            :                   body_point_imprint_map->insert( std::multimap<BodySM*, 
    2358                 :            :                     CubitVector>::value_type(
    2359 [ #  # ][ #  # ]:          0 :                     tmp_body_sm, closest_point )); 
    2360                 :            :                 }
    2361                 :            :               }
    2362                 :            :             }
    2363                 :            : 
    2364                 :            :             //get the long and short facet edge
    2365         [ #  # ]:          0 :             double curr_facet_length = curr_facet->length();
    2366         [ #  # ]:          0 :             double other_facet_length = other_facet->length();
    2367                 :            :             CurveOverlapFacet *long_facet = ( curr_facet_length > other_facet_length ?
    2368         [ #  # ]:          0 :                                                curr_facet : other_facet );
    2369                 :          0 :             CurveOverlapFacet *short_facet = curr_facet;
    2370         [ #  # ]:          0 :             if( long_facet == curr_facet )
    2371                 :          0 :                 short_facet = other_facet;
    2372                 :            :                 
    2373                 :            :             //make sure both endpoints or one endpoint and the midpoint 
    2374                 :            :             // of the smaller facet edge lie within a radius
    2375                 :            :             //of merge tolerance + facet tolerance to an infinite line defined 
    2376                 :            :             // by longer facet edge
    2377 [ #  # ][ #  # ]:          0 :             CubitVector direction = long_facet->end_point() - long_facet->start_point();
                 [ #  # ]
    2378                 :            :             double dist1 = short_facet->start_point().distance_from_infinite_line(
    2379 [ #  # ][ #  # ]:          0 :               long_facet->start_point(), direction );
                 [ #  # ]
    2380                 :            :             double dist2 = short_facet->end_point().distance_from_infinite_line(
    2381 [ #  # ][ #  # ]:          0 :               long_facet->start_point(), direction );
                 [ #  # ]
    2382                 :            : 
    2383 [ #  # ][ #  # ]:          0 :             if( dist2 > tolerance+facet_tol && dist1 > tolerance+facet_tol )
    2384                 :          0 :               continue;
    2385                 :            : 
    2386 [ #  # ][ #  # ]:          0 :             CubitVector facet_mid_point = (short_facet->end_point() + short_facet->start_point()) * 0.5;
         [ #  # ][ #  # ]
    2387                 :            : 
    2388                 :            :             double dist_to_midpoint = facet_mid_point.distance_from_infinite_line(
    2389 [ #  # ][ #  # ]:          0 :               long_facet->start_point(), direction );               
    2390                 :            :       
    2391 [ #  # ][ #  # ]:          0 :             if( dist_to_midpoint <= tolerance+facet_tol && (dist2 <= tolerance+facet_tol || dist1 <= tolerance+facet_tol ))
                 [ #  # ]
    2392                 :            :             {              
    2393                 :            :               double overlap_tolerance = 
    2394 [ #  # ][ #  # ]:          0 :                 curr_facet->length() < other_facet->length() ? curr_facet->length():
    2395 [ #  # ][ #  # ]:          0 :                 other_facet->length();
                 [ #  # ]
    2396                 :          0 :               overlap_tolerance *= 0.01;
    2397                 :            : 
    2398                 :            :               //how much of the facet overlaps?
    2399         [ #  # ]:          0 :               double tmp_overlap = curr_facet->distance_overlapping( other_facet ) ;
    2400         [ #  # ]:          0 :               if( tmp_overlap > overlap_tolerance )
    2401                 :          0 :                 total_overlap += tmp_overlap;
    2402                 :            : 
    2403         [ #  # ]:          0 :               if( total_overlap > tolerance )
    2404                 :            :               {
    2405                 :          0 :                 overlap = true;
    2406                 :          0 :                 break;
    2407                 :            :               }
    2408                 :            :             }
    2409                 :            :           }
    2410                 :            :         }
    2411                 :            :       }
    2412         [ #  # ]:          0 :       if( overlap == true )
    2413                 :          0 :         break;
    2414                 :            :     } 
    2415                 :            :   }
    2416                 :            : 
    2417         [ #  # ]:          0 :   if( facet_map == NULL )
    2418                 :            :   {
    2419                 :            :     //clean up facets and list
    2420 [ #  # ][ #  # ]:          0 :     for( kk=facet_list1->size(); kk--; )
    2421 [ #  # ][ #  # ]:          0 :       delete facet_list1->get_and_step();
                 [ #  # ]
    2422 [ #  # ][ #  # ]:          0 :     for( kk=facet_list2->size(); kk--; )
    2423 [ #  # ][ #  # ]:          0 :       delete facet_list2->get_and_step();
                 [ #  # ]
    2424                 :            : 
    2425 [ #  # ][ #  # ]:          0 :     delete facet_list1; 
    2426 [ #  # ][ #  # ]:          0 :     delete facet_list2; 
    2427                 :            :   }
    2428                 :            : 
    2429         [ #  # ]:          0 :   if( overlap == false )
    2430                 :          0 :     return CUBIT_FALSE;
    2431                 :            : 
    2432 [ #  # ][ #  # ]:          0 :   if( curve1_type == SPLINE_CURVE_TYPE  || 
    2433                 :            :       curve2_type == SPLINE_CURVE_TYPE ) 
    2434                 :            :   {
    2435                 :            :     //measure between the 2 curves
    2436                 :          0 :     double dist_between_curves = 0;
    2437 [ #  # ][ #  # ]:          0 :     CubitVector point_on_curve1, point_on_curve2;
    2438                 :            :     GeometryQueryTool::instance()->entity_entity_distance(curve1, curve2,
    2439 [ #  # ][ #  # ]:          0 :       point_on_curve1, point_on_curve2, dist_between_curves);
    2440                 :            : 
    2441         [ #  # ]:          0 :     if( dist_between_curves > tolerance )
    2442                 :          0 :         return CUBIT_FALSE;
    2443                 :            : 
    2444                 :            :     //the curvature at the same spot on the curve could be almost the same
    2445                 :            :     //if curvatures at midpoint are not the same, they don't overlap
    2446 [ #  # ][ #  # ]:          0 :     CubitVector curvature1, curvature2;
    2447 [ #  # ][ #  # ]:          0 :     CubitVector tangent, closest_location;
    2448         [ #  # ]:          0 :     curve1->closest_point( point_on_curve1, closest_location, &tangent, &curvature1 ); 
    2449         [ #  # ]:          0 :     curve2->closest_point( point_on_curve2, closest_location, &tangent, &curvature2 ); 
    2450                 :            : 
    2451         [ #  # ]:          0 :     double rad1 = curvature1.length();
    2452         [ #  # ]:          0 :     double rad2 = curvature2.length();
    2453                 :            :       
    2454                 :            :     //if curvatures are more than 10% off 
    2455                 :          0 :     double curvature_diff = fabs( rad1 - rad2 );
    2456                 :            : 
    2457 [ #  # ][ #  # ]:          0 :     if( rad1 > GEOMETRY_RESABS || rad2 > GEOMETRY_RESABS ) 
    2458                 :            :     {
    2459         [ #  # ]:          0 :       if( curvature_diff/( (fabs(rad1) + fabs(rad2))/2 ) > 0.1 )
    2460                 :          0 :         return CUBIT_FALSE;
    2461                 :          0 :     }
    2462                 :            :   }
    2463 [ #  # ][ #  # ]:          0 :   else if( curve1_type == ARC_CURVE_TYPE && 
    2464                 :            :            curve2_type == ARC_CURVE_TYPE ) 
    2465                 :            :   {
    2466                 :            :     //if both curves are arcs, make sure radii are almost the same
    2467 [ #  # ][ #  # ]:          0 :     CubitVector mid_point1, mid_point2;
    2468         [ #  # ]:          0 :     curve1->mid_point( mid_point1 );
    2469         [ #  # ]:          0 :     curve2->mid_point( mid_point2 );
    2470                 :            :     
    2471         [ #  # ]:          0 :     CubitVector dummy_vec;
    2472         [ #  # ]:          0 :     CubitVector curvature1;
    2473         [ #  # ]:          0 :     CubitVector curvature2;
    2474         [ #  # ]:          0 :     curve1->closest_point( mid_point1, dummy_vec, NULL, &curvature1 );
    2475         [ #  # ]:          0 :     curve2->closest_point( mid_point2, dummy_vec, NULL, &curvature2 );
    2476                 :            :     
    2477         [ #  # ]:          0 :     double rad1 = 1/curvature1.length();
    2478         [ #  # ]:          0 :     double rad2 = 1/curvature2.length();
    2479                 :            : 
    2480         [ #  # ]:          0 :     if( fabs( rad1 - rad2 ) > tolerance )
    2481                 :          0 :       return CUBIT_FALSE; 
    2482                 :            :   }
    2483                 :            :   else
    2484                 :          0 :     return CUBIT_TRUE;
    2485                 :            :   
    2486         [ #  # ]:          0 :   return CUBIT_TRUE;
    2487                 :            : }
    2488                 :            : 
    2489                 :          0 : CubitBoolean SurfaceOverlapTool::check_overlap( RefEdge *edge1, RefEdge *edge2, 
    2490                 :            :   std::map<RefEdge*, DLIList<CurveOverlapFacet*>* > *facet_map,
    2491                 :            :   double *overlap_tol ) 
    2492                 :            : {
    2493                 :            :   //if edges are not splines and are not of the same type, 
    2494                 :            :   //they won't overlap
    2495 [ #  # ][ #  # ]:          0 :   if( (edge1->get_curve_ptr()->geometry_type() != SPLINE_CURVE_TYPE  &&
         [ #  # ][ #  # ]
    2496 [ #  # ][ #  # ]:          0 :        edge2->get_curve_ptr()->geometry_type() != SPLINE_CURVE_TYPE) &&
         [ #  # ][ #  # ]
    2497 [ #  # ][ #  # ]:          0 :       (edge1->get_curve_ptr()->geometry_type() != 
    2498 [ #  # ][ #  # ]:          0 :        edge2->get_curve_ptr()->geometry_type() )) 
    2499                 :          0 :     return CUBIT_FALSE;
    2500                 :            : 
    2501                 :            :   // we don't want to consider edges from the same body
    2502 [ #  # ][ #  # ]:          0 :   if (edge1->body() == edge2->body())
                 [ #  # ]
    2503                 :          0 :           return CUBIT_FALSE;
    2504                 :            : 
    2505                 :            : 
    2506         [ #  # ]:          0 :   double tolerance = GeometryQueryTool::get_geometry_factor()*GEOMETRY_RESABS;
    2507         [ #  # ]:          0 :   if( overlap_tol )
    2508                 :          0 :     tolerance = *overlap_tol;
    2509                 :            :     
    2510                 :            : 
    2511         [ #  # ]:          0 :   std::map<RefEdge*, DLIList<CurveOverlapFacet*>* >::iterator facet_iterator;
    2512         [ #  # ]:          0 :   CubitBox edge_box1 = edge1->bounding_box(); 
    2513 [ #  # ][ #  # ]:          0 :   CubitBox edge_box2 = edge2->bounding_box();
    2514                 :            : 
    2515                 :            :   //do bounding boxes overlap
    2516 [ #  # ][ #  # ]:          0 :   if( edge_box1.overlap( tolerance, edge_box2 ) )
    2517                 :            :   {
    2518                 :            :     //curves must overlap at least by 100th of the smaller curve's length
    2519                 :          0 :     double min_overlap = 0.0;
    2520         [ #  # ]:          0 :     double edge1_length = edge1->measure();
    2521         [ #  # ]:          0 :     double edge2_length = edge2->measure();
    2522         [ #  # ]:          0 :     if( edge1_length < edge2_length )
    2523         [ #  # ]:          0 :       min_overlap = edge1->measure() * 0.01;
    2524                 :            :     else 
    2525         [ #  # ]:          0 :       min_overlap = edge2->measure() * 0.01;
    2526                 :            : 
    2527                 :          0 :     DLIList<CurveOverlapFacet*> *facet_list1 = NULL;
    2528                 :          0 :     DLIList<CurveOverlapFacet*> *facet_list2 = NULL;
    2529                 :            : 
    2530                 :            :     //see if edge is in map...if not we have to create faceting for it.
    2531         [ #  # ]:          0 :     if( facet_map )
    2532         [ #  # ]:          0 :       facet_iterator = facet_map->find( edge1 );
    2533                 :            : 
    2534 [ #  # ][ #  # ]:          0 :     if( facet_map == NULL || facet_iterator == facet_map->end() )
         [ #  # ][ #  # ]
                 [ #  # ]
           [ #  #  #  # ]
    2535                 :            :     {
    2536 [ #  # ][ #  # ]:          0 :       facet_list1 = new DLIList<CurveOverlapFacet*>;
    2537                 :            : 
    2538         [ #  # ]:          0 :       GMem curve_graphics;
    2539         [ #  # ]:          0 :       edge1->get_graphics( curve_graphics );
    2540                 :            : 
    2541         [ #  # ]:          0 :       GPoint *points = curve_graphics.point_list();
    2542                 :          0 :       int num_points = curve_graphics.pointListCount;
    2543                 :            : 
    2544                 :            :       int kk;
    2545         [ #  # ]:          0 :       for( kk=0; kk<num_points-1; kk++ )
    2546                 :            :       {
    2547                 :            :         //create a new CurveOverlapFacets
    2548                 :            :         GPoint gpoints[2];
    2549                 :          0 :         gpoints[0] = points[kk];
    2550                 :          0 :         gpoints[1] = points[kk + 1];
    2551                 :            : 
    2552 [ #  # ][ #  # ]:          0 :         CurveOverlapFacet *tmp_facet = new CurveOverlapFacet( gpoints ); 
    2553         [ #  # ]:          0 :         facet_list1->append( tmp_facet );
    2554                 :            :       }
    2555                 :            :       
    2556         [ #  # ]:          0 :       if( facet_map )
    2557                 :            :         facet_map->insert( std::map<RefEdge*, 
    2558 [ #  # ][ #  # ]:          0 :           DLIList<CurveOverlapFacet*>*>::value_type( edge1, facet_list1 )); 
                 [ #  # ]
    2559                 :            :     }
    2560                 :            :     else
    2561         [ #  # ]:          0 :       facet_list1 = (*facet_iterator).second;
    2562                 :            : 
    2563                 :            :     //see if edge is in map...if not we have to create facet for it.
    2564         [ #  # ]:          0 :     if( facet_map )
    2565         [ #  # ]:          0 :       facet_iterator = facet_map->find( edge2 );
    2566                 :            : 
    2567 [ #  # ][ #  # ]:          0 :     if( facet_map == NULL || facet_iterator == facet_map->end() )
         [ #  # ][ #  # ]
                 [ #  # ]
           [ #  #  #  # ]
    2568                 :            :     {
    2569 [ #  # ][ #  # ]:          0 :       facet_list2 = new DLIList<CurveOverlapFacet*>;
    2570                 :            : 
    2571         [ #  # ]:          0 :       GMem curve_graphics;
    2572         [ #  # ]:          0 :       edge2->get_graphics( curve_graphics );
    2573                 :            : 
    2574         [ #  # ]:          0 :       GPoint *points = curve_graphics.point_list();
    2575                 :          0 :       int num_points = curve_graphics.pointListCount;
    2576                 :            : 
    2577                 :            :       int kk;
    2578         [ #  # ]:          0 :       for( kk=0; kk<num_points-1; kk++ )
    2579                 :            :       {
    2580                 :            :         //create a new CurveOverlapFacets
    2581                 :            :         GPoint gpoints[2];
    2582                 :          0 :         gpoints[0] = points[kk];
    2583                 :          0 :         gpoints[1] = points[kk + 1];
    2584                 :            : 
    2585 [ #  # ][ #  # ]:          0 :         CurveOverlapFacet *tmp_facet = new CurveOverlapFacet( gpoints ); 
    2586         [ #  # ]:          0 :         facet_list2->append( tmp_facet );
    2587                 :            :       }
    2588                 :            :       
    2589         [ #  # ]:          0 :       if( facet_map )
    2590                 :            :         facet_map->insert( std::map<RefEdge*, 
    2591 [ #  # ][ #  # ]:          0 :           DLIList<CurveOverlapFacet*>*>::value_type( edge2, facet_list2 )); 
                 [ #  # ]
    2592                 :            :     }
    2593                 :            :     else
    2594         [ #  # ]:          0 :       facet_list2 = (*facet_iterator).second;
    2595                 :            :     
    2596                 :            :     //compare smaller list to larger list...want list1 to be smaller
    2597 [ #  # ][ #  # ]:          0 :     if( facet_list1->size() > facet_list2->size() )
                 [ #  # ]
    2598                 :            :     {
    2599                 :          0 :       DLIList<CurveOverlapFacet*> *tmp_list = facet_list1;
    2600                 :          0 :       facet_list1 = facet_list2;
    2601                 :          0 :       facet_list2 = tmp_list;
    2602                 :            :     }
    2603                 :            : 
    2604         [ #  # ]:          0 :     facet_list1->reset();
    2605         [ #  # ]:          0 :     facet_list2->reset();
    2606                 :            :     int kk;
    2607                 :          0 :     double total_overlap = 0.0;
    2608                 :          0 :     bool overlap = false;
    2609                 :            :     //now determine if enough curve facets overlap
    2610 [ #  # ][ #  # ]:          0 :     for( kk=facet_list1->size(); kk--; )
    2611                 :            :     {
    2612         [ #  # ]:          0 :       CurveOverlapFacet *curr_facet = facet_list1->get_and_step();
    2613                 :            : 
    2614                 :            :       //do bounding boxes of facets overlap?
    2615                 :            :       int jj;
    2616 [ #  # ][ #  # ]:          0 :       for( jj=facet_list2->size(); jj--; )
    2617                 :            :       {
    2618         [ #  # ]:          0 :         CurveOverlapFacet *other_facet = facet_list2->get_and_step();
    2619                 :            : 
    2620 [ #  # ][ #  # ]:          0 :         if( curr_facet->bbox_overlap( tolerance, other_facet ) )
    2621                 :            :         {
    2622 [ #  # ][ #  # ]:          0 :           if( curr_facet->facet_to_facet_distance( other_facet ) < tolerance )
    2623                 :            :           {
    2624                 :            : 
    2625                 :            :             //are facets parallel within some tolerance angle?
    2626         [ #  # ]:          0 :             double angle = curr_facet->angle( other_facet ); 
    2627 [ #  # ][ #  # ]:          0 :             if( angle < 1 || fabs(180-angle ) < 1 )
    2628                 :            :             {
    2629                 :            :               double overlap_tolerance = 
    2630 [ #  # ][ #  # ]:          0 :                 curr_facet->length() < other_facet->length() ? curr_facet->length():
    2631 [ #  # ][ #  # ]:          0 :                 other_facet->length();
                 [ #  # ]
    2632                 :          0 :               overlap_tolerance *= 0.01;
    2633                 :            :               
    2634                 :            :               //how much of the facet overlaps?
    2635         [ #  # ]:          0 :               double tmp_overlap = curr_facet->distance_overlapping( other_facet ) ;
    2636         [ #  # ]:          0 :               if( tmp_overlap > overlap_tolerance )
    2637                 :          0 :                 total_overlap += tmp_overlap;
    2638                 :            : 
    2639         [ #  # ]:          0 :               if( total_overlap > min_overlap )
    2640                 :            :               {
    2641         [ #  # ]:          0 :                 if( facet_map == NULL )
    2642                 :            :                 {
    2643                 :            :                   //clean up facets and list
    2644                 :            :                   int i;
    2645 [ #  # ][ #  # ]:          0 :                   for( i=facet_list1->size(); i--; )
    2646 [ #  # ][ #  # ]:          0 :                     delete facet_list1->get_and_step();
                 [ #  # ]
    2647 [ #  # ][ #  # ]:          0 :                   for( i=facet_list2->size(); i--; )
    2648 [ #  # ][ #  # ]:          0 :                     delete facet_list2->get_and_step();
                 [ #  # ]
    2649                 :            : 
    2650 [ #  # ][ #  # ]:          0 :                   delete facet_list1; 
    2651 [ #  # ][ #  # ]:          0 :                   delete facet_list2; 
    2652                 :            :                 }
    2653                 :          0 :                 overlap = true;
    2654                 :          0 :                 break;
    2655                 :            :               }
    2656                 :            :             }
    2657                 :            :           }
    2658                 :            :         }
    2659                 :            :       }
    2660         [ #  # ]:          0 :       if( overlap == true )
    2661                 :          0 :         break;
    2662                 :            :     }
    2663         [ #  # ]:          0 :     if( overlap == false )
    2664                 :          0 :       return CUBIT_FALSE;
    2665                 :            : 
    2666 [ #  # ][ #  # ]:          0 :     if( edge1->get_curve_ptr()->geometry_type() == SPLINE_CURVE_TYPE  || 
         [ #  # ][ #  # ]
                 [ #  # ]
    2667 [ #  # ][ #  # ]:          0 :         edge2->get_curve_ptr()->geometry_type() == SPLINE_CURVE_TYPE ) 
    2668                 :            :     {
    2669                 :            :       //measure between the 2 curves
    2670                 :          0 :       double dist_between_edges = 0;
    2671 [ #  # ][ #  # ]:          0 :       CubitVector point_on_edge1, point_on_edge2;
    2672                 :            :       GeometryQueryTool::instance()->entity_entity_distance( edge1, edge2, 
    2673 [ #  # ][ #  # ]:          0 :                                                 point_on_edge1, point_on_edge2, dist_between_edges );
         [ #  # ][ #  # ]
    2674                 :            :     
    2675         [ #  # ]:          0 :       if( dist_between_edges > tolerance )
    2676                 :          0 :         return CUBIT_FALSE;
    2677                 :            : 
    2678                 :            :       //the curvature at the same spot on the curve could be almost the same
    2679                 :            :       //if curvature's at midpoint are not the same, they don't overlap
    2680 [ #  # ][ #  # ]:          0 :       CubitVector curvature1, curvature2;
    2681 [ #  # ][ #  # ]:          0 :       CubitVector tangent, closest_location;
    2682         [ #  # ]:          0 :       edge1->closest_point( point_on_edge1, closest_location, &tangent, &curvature1 ); 
    2683         [ #  # ]:          0 :       edge2->closest_point( point_on_edge2, closest_location, &tangent, &curvature2 ); 
    2684                 :            : 
    2685         [ #  # ]:          0 :       double rad1 = curvature1.length();
    2686         [ #  # ]:          0 :       double rad2 = curvature2.length();
    2687                 :            :         
    2688                 :            :       //if curvatures are more than 10% off 
    2689                 :          0 :       double curvature_diff = fabs( rad1 - rad2 );
    2690                 :            : 
    2691 [ #  # ][ #  # ]:          0 :       if( (rad1 || rad2 ) && 
                 [ #  # ]
    2692         [ #  # ]:          0 :          (rad1 > GEOMETRY_RESABS ||   //radii must be of significance even look at  
    2693                 :            :           rad2 > GEOMETRY_RESABS ))
    2694                 :            :       {
    2695         [ #  # ]:          0 :         if( curvature_diff/( (fabs(rad1) + fabs(rad2))/2 ) > 0.1 )
    2696                 :          0 :           return CUBIT_FALSE;
    2697                 :            :       }
    2698                 :            :     }
    2699                 :            :     else
    2700                 :          0 :       return CUBIT_TRUE;
    2701                 :            :   }
    2702                 :            :   else
    2703                 :          0 :     return CUBIT_FALSE;
    2704                 :            :   
    2705         [ #  # ]:          0 :   return CUBIT_TRUE;
    2706 [ +  - ][ +  - ]:       6540 : }

Generated by: LCOV version 1.11