1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//- Class:          CAMergePartner
//- Owner:          Greg Nielson
//- Description:    Cubit Attribute for merge partners.
//- Checked By:
//- Version:

#include "CAMergePartner.hpp"
#include "DLIList.hpp"
#include "SDLCAMergePartnerList.hpp"
#include "RefEntity.hpp"
#include "RefFace.hpp"
#include "RefEdge.hpp"
#include "RefVertex.hpp"
#include "TDCompare.hpp"
#include "TDUniqueId.hpp"
#include "MergeTool.hpp"
#include "CastTo.hpp"
#include "CubitAttribUser.hpp"
#include "GeometryQueryTool.hpp"
#include "RefVolume.hpp"
#include "TopologyBridge.hpp"

#include <stdlib.h>
#include <time.h>

// factory function
CubitAttrib* CAMergePartner_creator(RefEntity* entity, const CubitSimpleAttrib &p_csa)
{
  return new CAMergePartner(entity, p_csa);
}

CAMergePartner::CAMergePartner(RefEntity* new_attrib_owner,
                               const CubitSimpleAttrib &csa_ptr)
        : CubitAttrib(new_attrib_owner)
{
  initialize();

  if(!csa_ptr.isEmpty())
  {
    int i_temp = csa_ptr.int_data_list()[0];
    mergeID = i_temp;
    if( csa_ptr.int_data_list().size() > 1 )
    {
      i_temp = csa_ptr.int_data_list()[1];
      switch( i_temp ) {
        case -1: bridge_sense_ = CUBIT_REVERSED; break;
        case  1: bridge_sense_ = CUBIT_FORWARD;  break;
        case  0: bridge_sense_ = CUBIT_UNKNOWN; break;
        default: bridge_sense_ = CUBIT_UNKNOWN; assert(0);
      }
    }
  }
}

CAMergePartner::~CAMergePartner() 
{
    //
  mergeID = 0;
}


void CAMergePartner::initialize()
{
  mergeID = -1;
  bridge_sense_ = CUBIT_UNKNOWN;
  isSurvivor = 0;
}


CubitStatus CAMergePartner::actuate()
{
  hasActuated = CUBIT_TRUE;
  return CUBIT_SUCCESS;
  //We don't need to actuate this attribute here since the merging
  //happens in GeometryQueryTool::check_mergeable_refentity.
  
  /*
  if (hasActuated == CUBIT_TRUE) return CUBIT_SUCCESS;
  
  BasicTopologyEntity * bte_ptr = CAST_TO(attribOwnerEntity,BasicTopologyEntity); 
  if (bte_ptr == NULL || bte_ptr->deactivated() == CUBIT_TRUE)
    return CUBIT_FAILURE;
  
  DLIList<RefEntity*> merge_list;
  //merge_prepare(merge_list);
  merge_list.append_unique(attribOwnerEntity);

  CubitStatus result = CUBIT_SUCCESS;

  if (merge_list.size() > 1)
    result = (NULL == MergeTool::instance()->force_merge(merge_list))
            ? CUBIT_FAILURE : CUBIT_SUCCESS;

  if( result )
  {
    hasActuated = CUBIT_TRUE;
  }        

  return result; */
}

void CAMergePartner::merge_prepare(DLIList<RefEntity*> &merge_list)
{
  DLIList<CubitAttrib*> my_ca_list;
  CAMergePartner *my_camp_ptr;
  RefEntity* re_ptr;

    // get all the merge partner attributes that are on my owner
  attribOwnerEntity->find_cubit_attrib_type(CA_MERGE_PARTNER, my_ca_list);
  merge_list.clean_out();
  DLIList<ToolDataUser*> td_list, temp_td_list;
  int i;
  for (i = my_ca_list.size(); i > 0; i--)
  {
    my_camp_ptr = CAST_TO(my_ca_list.get(),CAMergePartner);
    my_ca_list.step();
    td_list.clean_out();
      // get all the objects with this unique id (which is also the merge id)
    TDUniqueId::find_td_unique_id(my_camp_ptr->merge_id(), temp_td_list);
    td_list += temp_td_list;
  }
  
    // now put those entities into the merge_list
  for (i = td_list.size(); i > 0; i--) 
  {
    re_ptr = CAST_TO(td_list.get(), RefEntity);
    if (re_ptr) 
    {
      CubitAttrib *tmp_attrib = re_ptr->get_cubit_attrib( CA_MERGE_PARTNER, CUBIT_FALSE );
      if( tmp_attrib )
        merge_list.append(re_ptr);
    }
    td_list.step();
  }
  
    // Now get bridge sense for each entity in list.
    // Add this entity to list, too.
  merge_list.append( attribOwnerEntity );
  for( i = merge_list.size(); i--; )
  {
    RefEntity* ent = merge_list.get_and_step();
    TopologyEntity* te = dynamic_cast<TopologyEntity*>(ent);
    if( te->bridge_manager()->number_of_bridges() != 1 )
      continue;
    
    my_ca_list.clean_out();
    ent->find_cubit_attrib_type(CA_MERGE_PARTNER, my_ca_list);
    assert( my_ca_list.size() < 2);
    if( !my_ca_list.size() )
      continue;
    
    my_camp_ptr = dynamic_cast<CAMergePartner*>(my_ca_list.get());
    if( my_camp_ptr->bridge_sense() == CUBIT_UNKNOWN )
      continue;
  }  
  merge_list.pop(); // take attribOwnerEntity back off list
    
  return;
}
  
CubitStatus CAMergePartner::actuate_list(DLIList<RefEntity*> entity_list)
{

    // given a list of ref entities (usually all entities of a given type),
    // actuate the camp's on those entities
  RefEntity *ref_ent, *keeper;
  DLIList<CubitAttrib*> ca_list;
  DLIList<TopologyBridge*> bridge_list(entity_list.size());
  SDLCAMergePartnerList sorted_camp_list;
  int i;
  for(i = entity_list.size(); i > 0; i--)
  {
    ref_ent = entity_list.get_and_step();
    ca_list.clean_out();
    ref_ent->find_cubit_attrib_type(CA_MERGE_PARTNER, ca_list);
    assert(ca_list.size() < 2); // There should only be one
                                //  merge partner per entity
    if(ca_list.size() > 0)
    {
      CAMergePartner* attrib = dynamic_cast<CAMergePartner*>(ca_list.get());
      sorted_camp_list.append( attrib );
      TopologyEntity* te = dynamic_cast<TopologyEntity*>(ref_ent);
      TopologyBridge* bridge = te->bridge_manager()->topology_bridge();
      bridge_list.append(bridge);
    }
  }
  sorted_camp_list.sort();
  sorted_camp_list.reset();

  if (DEBUG_FLAG(90)) {
    for (i = sorted_camp_list.size(); i > 0; i--) {
      CAMergePartner *camp_ptr = sorted_camp_list.get_and_step();
      ref_ent = camp_ptr->attrib_owner();
      PRINT_DEBUG_90("%s %d, unique id = %d\n", ref_ent->class_name(),
                     ref_ent->id(), camp_ptr->merge_id());
    }
  }
  
    // now go through all the camp's for the entity list; peel off
    // camp's with the same id, and merge the associated entities together
  while (sorted_camp_list.size() > 0) 
  {
    DLIList<RefEntity*> refent_list;
    DLIList<CubitAttrib*> camp_list;
    keeper = NULL;

      // get the next list of entities with the same camp id
    sorted_camp_list.last();
    CAMergePartner *camp_ptr = sorted_camp_list.remove();
    sorted_camp_list.back();
    camp_list.append(camp_ptr);
    int current_id = camp_ptr->merge_id();
    while (sorted_camp_list.size() > 0 &&
           sorted_camp_list.get()->merge_id() == current_id) {
      camp_list.append(sorted_camp_list.remove());
      sorted_camp_list.back();
    }
    
    if (camp_list.size() == 1) continue;
    
    CubitBoolean has_actuated = camp_list.get()->has_actuated();

      // check the has actuated flag; if one is set, they all should be;
      // also, compile list of ref entities while we're at it
    for (current_id = camp_list.size(); current_id > 0; current_id--) {
      ref_ent = camp_list.get()->attrib_owner();
      refent_list.append(ref_ent);
      if (!keeper || ref_ent->id() < keeper->id()) keeper = ref_ent;
      assert(camp_list.get()->has_actuated() == has_actuated);
      camp_list.step();
    }

      // if they have already actuated, go on to next ones
    if (has_actuated == CUBIT_TRUE) continue;

      // otherwise merge
    if(refent_list.size() > 1)
      MergeTool::instance()->force_merge(refent_list);

        // remove the cubit attribute from the surviving parent
    keeper->remove_cubit_attrib(CA_MERGE_PARTNER);

  } // loop over existing camp's
  
  return CUBIT_SUCCESS;
}

CubitStatus CAMergePartner::update()
{
  if (hasUpdated) return CUBIT_SUCCESS;
  
    // set the updated flag
  hasUpdated = CUBIT_TRUE;
  BasicTopologyEntity* bte_ptr = CAST_TO(attribOwnerEntity, BasicTopologyEntity);

  if( (bte_ptr == NULL) || (dynamic_cast<RefVolume*>(bte_ptr) != NULL))
  {
    delete_attrib(CUBIT_TRUE);
  }
  else if( (bte_ptr->bridge_manager()->number_of_bridges() == 1) &&
           (bte_ptr->bridge_manager()->topology_bridge()->bridge_sense() != CUBIT_REVERSED) )
  {
    delete_attrib(CUBIT_TRUE);
  }
  else
  {
      // get the merge id from the TDUniqueId for the owner entity
    mergeID = TDUniqueId::get_unique_id(attribOwnerEntity);
    bridge_sense_ = CUBIT_UNKNOWN;
    isSurvivor = 0;
  }

  return CUBIT_SUCCESS;
}

CubitSimpleAttrib CAMergePartner::cubit_simple_attrib()
{
  std::vector<CubitString> cs_list;
  std::vector<double> d_list;
  std::vector<int> i_list;

  i_list.push_back(mergeID);
  cs_list.push_back(att_internal_name());

  return CubitSimpleAttrib(&cs_list, &d_list, &i_list);
}

void CAMergePartner::set_survivor( CubitSimpleAttrib& csa, int is_survivor )
{
  //get the list we want to modify from the CSA
  std::vector<int>& data = csa.int_data_list();
  
  //change or append?
  if( data.size() >= 4 )
  {
    data[3] = is_survivor;
  }
  else 
  {
    assert( data.size() == 3 );
    data.push_back(is_survivor);
  }
}

void CAMergePartner::set_bridge_sense( CubitSimpleAttrib& csa, CubitSense sense )
{
  //encode/decode sense as:
  // CUBIT_FORWARD :  1
  // CUBIT_REVERSE : -1
  // CUBIT_UNKNOWN :  0
  int i = 0;
  switch( sense ) { 
    case CUBIT_FORWARD  : i =  1; break;
    case CUBIT_REVERSED : i = -1; break;
    case CUBIT_UNKNOWN  : i =  0; break;
    default: assert(0);
  }
  
  //get the list we want to modify from the CSA
  std::vector<int>& data = csa.int_data_list();
  
  //change or append?
  if( data.size() >= 2 )
  {
    data[1] = i;
  }
  else 
  {
    assert( data.size() == 1 );
    data.push_back(i);
  }
}
CubitBoolean CAMergePartner::is_survivor( const CubitSimpleAttrib& csa )
{
  const std::vector<int>& data = csa.int_data_list();
  if( data.size() >= 4)
  {
    int i = data[3];
    if( i == 1 )
      return true;
  }
  return false;
}


CubitSense CAMergePartner::get_bridge_sense( const CubitSimpleAttrib& csa )
{
  const std::vector<int>& data = csa.int_data_list();
  if( data.size() >= 2)
  {
    int i = data[1];
    switch(i) {
      case  1: return CUBIT_FORWARD;
      case -1: return CUBIT_REVERSED;
      default: assert(0);  // if -DNDEBUG, fall through to unknown
      case  0: return CUBIT_UNKNOWN;
    }
  }

  return CUBIT_UNKNOWN;
}

void CAMergePartner::set_saved_id( CubitSimpleAttrib& csa, int id )
{
   //get the list we want to modify from the CSA
  std::vector<int>& data = csa.int_data_list();

    // ID goes after bridge sense, so save bridge sense first
  if (data.size() == 1)
    set_bridge_sense( csa, CUBIT_UNKNOWN );
  
    // change?
  if (data.size() > 2 )
  {
    data[2] = id;
  }
    // set?
  else
  {
    assert(data.size() == 2);
    data.push_back(id);
  }
}

int CAMergePartner::get_saved_id( const CubitSimpleAttrib& csa )
{
   //get the list we want to modify from the CSA
  const std::vector<int>& data = csa.int_data_list();

  if (data.size() < 3)
    return 0;
  
  return data[2];
}
    
  

void CAMergePartner::print() 
{
  PRINT_INFO("Attribute MERGE_PARTNER, %s %d: mergeId = %d, sense = %d.\n",
             attribOwnerEntity->class_name(), attribOwnerEntity->id(),
             mergeID, bridge_sense_);
}