cgma
BridgeManager.cpp
Go to the documentation of this file.
00001 //-------------------------------------------------------------------------
00002 // Filename      : BridgeManager.cpp
00003 //
00004 // Purpose       : Manages the TopologyBridges being used by
00005 //                 a single TopologyEntity.  Encapsulates the
00006 //                 merging/unmerging of TopologyBridges into/from
00007 //                 a TopologyEntity.
00008 //
00009 // Creator       : Darryl Melander
00010 //
00011 // Creation Date : 02/20/99
00012 //
00013 // Owner         : Darryl Melander
00014 //-------------------------------------------------------------------------
00015 #include "BridgeManager.hpp"
00016 #include "TopologyBridge.hpp"
00017 #include "TopologyEntity.hpp"
00018 
00019 #include <algorithm>
00020 
00021 // Constructor.
00022 // Attaches this BridgeManager to 'owner'.
00023 BridgeManager::BridgeManager()
00024     : topologyEntity(NULL)
00025 {
00026 }
00027 
00028 // Destructor
00029 BridgeManager::~BridgeManager()
00030 {
00031   std::vector<TopologyBridge*>::iterator iter;
00032   for (iter=mergeList.begin(); iter!=mergeList.end(); iter++)
00033   {
00034     delete (*iter);
00035   }
00036   mergeList.clear();
00037 }
00038 
00039 void BridgeManager::set_entity(TopologyEntity* ent)
00040 {
00041   topologyEntity = ent;
00042 }
00043 
00044 // Adds this TB to the child list.  Tells 'bridge'
00045 // that it belongs to this BridgeManager.  If bridge is
00046 // already in this BridgeManager, we return FAILURE.
00047 // This doesn't mean 'bridge' won't be in this manager,
00048 // it just means it won't be in the list twice.
00049 CubitStatus BridgeManager::add_bridge(TopologyBridge* bridge)
00050 {
00051   if( !bridge || bridge->owner() )
00052   {
00053     assert( bridge && !bridge->owner() );
00054     return CUBIT_FAILURE;
00055   }
00056 
00057   mergeList.push_back(bridge);
00058   
00059   bridge->owner(this);
00060   return CUBIT_SUCCESS;
00061 }
00062 
00063 // This function sets the 
00064 CubitStatus BridgeManager::add_bridge_as_representation(TopologyBridge* bridge)
00065 {
00066     // Make sure bridge isn't already owned by someone else
00067   if( !bridge || bridge->owner() ) 
00068   {
00069     assert( bridge && !bridge->owner() ); 
00070     return CUBIT_FAILURE;
00071   }
00072 
00073   // Add the bridge to the beginning of the list
00074   mergeList.insert(mergeList.begin(), bridge );
00075     
00076   bridge->owner(this);
00077   return CUBIT_SUCCESS;
00078 }  
00079 
00080 // Removes 'bridge' from this.  If it wasn't in this,
00081 // then return FAILURE.
00082 CubitStatus BridgeManager::remove_bridge(TopologyBridge* bridge )
00083 {
00084   std::vector<TopologyBridge*>::iterator iter;
00085   iter = std::find(mergeList.begin(), mergeList.end(), bridge);
00086   if (iter != mergeList.end())
00087     mergeList.erase(iter);
00088   else
00089     return CUBIT_FAILURE;
00090   
00091   bridge->owner(0);
00092   return CUBIT_SUCCESS;
00093 }
00094 
00095 CubitStatus BridgeManager::bridge_destroyed( TopologyBridge* bridge )
00096 {
00097 //  if( firstBridge == bridge &&
00098 //      bridge->next_bridge() &&
00099 //      bridge->next_bridge()->bridge_sense() != bridge->bridge_sense() )
00100 //    topologyEntity->reverse_topology();
00101 
00102   return remove_bridge( bridge );
00103 }
00104   
00105 CubitStatus BridgeManager::remove_all_bridges()
00106 {
00107   if (mergeList.empty())
00108     return CUBIT_SUCCESS;
00109 
00110   std::vector<TopologyBridge*>::iterator iter;
00111   std::vector<TopologyBridge*> temp_list;
00112 
00113   // make a copy of the list so we can modify the real one
00114   temp_list = mergeList;
00115 
00116   for (iter=temp_list.begin(); iter!=temp_list.end(); iter++)
00117   {
00118     if (!remove_bridge( *iter ))
00119       break;
00120   }
00121 
00122   return mergeList.empty() ? CUBIT_SUCCESS : CUBIT_FAILURE;
00123 }
00124 
00125 // Transfer all bridges from 'dead' to 'this'.
00126 // Set correct pointers.
00127 CubitStatus BridgeManager::merge(BridgeManager* dead_manager, 
00128                                  CubitSense relative_sense )
00129 {
00130   if (dead_manager->mergeList.empty())
00131     return CUBIT_SUCCESS;
00132   
00133     // Remove bridge list from dead BridgeManager
00134   std::vector<TopologyBridge*> dead_list = dead_manager->mergeList;
00135   dead_manager->mergeList.clear();
00136 
00137     // Set owner and reverse sense if necessary
00138     // for all bridges in merge list.
00139   std::vector<TopologyBridge*>::iterator iter;
00140   for (iter = dead_list.begin(); iter != dead_list.end(); iter++)
00141   {
00142     (*iter)->owner(this);
00143     if( relative_sense == CUBIT_REVERSED )
00144       (*iter)->reverse_bridge_sense();
00145   }
00146   
00147     // append merge list to end of this manager's bridge list
00148   mergeList.insert(mergeList.end(), dead_list.begin(), dead_list.end());
00149   
00150   return CUBIT_SUCCESS;
00151 }
00152 
00153 // Indicates whether this bridge is in this manager.
00154 CubitBoolean BridgeManager::contains_bridge(TopologyBridge* bridge) const
00155 {
00156   return bridge->owner() == (TBOwner*)this;
00157 }
00158 
00159 // Appends all children TBs to bridge_list.
00160 // There is no check for duplicates in the list.
00161 // The number of TBs added is returned.
00162 int BridgeManager::get_bridge_list(
00163   DLIList<TopologyBridge*>& bridge_list) const
00164 {
00165   std::vector<TopologyBridge*>::const_iterator iter;
00166   for (iter = mergeList.begin(); iter != mergeList.end(); iter++)
00167   {
00168     bridge_list.append(*iter);
00169   }
00170   return mergeList.size();
00171 }
00172 
00173 
00174 // Get all the child TB's that are owned by the passed
00175 // BridgeManager.
00176 int BridgeManager::get_bridge_list( 
00177     DLIList<TopologyBridge*>& bridge_list,
00178   GeometryQueryEngine* gqe_ptr ) const
00179 {
00180   std::vector<TopologyBridge*>::const_iterator iter;
00181   for (iter=mergeList.begin(); iter!=mergeList.end(); iter++)
00182   {
00183     if ( (*iter)->get_geometry_query_engine() == gqe_ptr )
00184       bridge_list.append(*iter);
00185   }
00186   return bridge_list.size();
00187   // TODO - BWH - return on this function should be consistent with other bridge list function
00188 }
00189 
00190 TopologyBridge* BridgeManager::topology_bridge(
00191   GeometryQueryEngine* gqe_ptr ) const
00192 {
00193   std::vector<TopologyBridge*>::const_iterator iter;
00194   for (iter=mergeList.begin(); iter!=mergeList.end(); iter++)
00195   {
00196     if ( (*iter)->get_geometry_query_engine() == gqe_ptr )
00197       return *iter;
00198   }
00199   
00200   return 0;
00201 }
00202 
00203 
00204 void BridgeManager::reverse_bridge_senses ()
00205 {
00206   std::vector<TopologyBridge*>::iterator iter;
00207   for (iter=mergeList.begin(); iter!=mergeList.end(); iter++)
00208   {
00209     (*iter)->reverse_bridge_sense();
00210   }
00211 }  
00212 
00213 CubitStatus BridgeManager::swap_bridge( TopologyBridge* old_tb,
00214                                         TopologyBridge* new_tb,
00215                                         bool reversed )
00216 {
00217     // make sure new_tb isn't already owned by someone else
00218   if( new_tb->owner() != NULL )
00219   {
00220     assert( new_tb->owner() == 0 );
00221     return CUBIT_FAILURE;
00222   }
00223 
00224     // Replace old bridge with new bridge at same location
00225     // in linked list.
00226 
00227   std::vector<TopologyBridge*>::iterator iter;
00228   iter = std::find(mergeList.begin(), mergeList.end(), old_tb);
00229   if (iter != mergeList.end())
00230   {
00231     iter = mergeList.erase(iter);
00232     mergeList.insert(iter, new_tb);
00233   }
00234   else
00235   {
00236     assert(false);
00237     return CUBIT_FAILURE;
00238   }
00239   
00240     // Update owner pointers
00241   
00242   new_tb->owner(this);
00243   old_tb->owner(0);
00244   
00245     // If 'reversed == true' then make sure bridges have
00246     // opposite sense.
00247   
00248   bool same_sense = new_tb->bridge_sense() == old_tb->bridge_sense();
00249   if (reversed == same_sense) // if (reversed XOR same_sense) 
00250     new_tb->reverse_bridge_sense();
00251     
00252   return CUBIT_SUCCESS;
00253 }
00254 
00255 int BridgeManager::number_of_bridges() const
00256 {
00257   return mergeList.size();
00258 }
00259 
00260 void BridgeManager::notify_reversed( TopologyBridge* bridge )
00261 {
00262   bridge->reverse_bridge_sense();
00263 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines