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
#include "CubitPointData.hpp"
#include "CubitFacet.hpp"
#include "CubitFacetEdge.hpp"
#include "ToolData.hpp"
#include "CubitFacetData.hpp"
#include "CubitFacetEdgeData.hpp"
#include "CastTo.hpp"

static int point_counter = 0;

//===========================================================================
// Function Name: CubitPointData
//
// Member Type:  PUBLIC
// Description:  constructor
// Author: 
// Date:
//===========================================================================
CubitPointData::CubitPointData( double x_val, double y_val, double z_val )
  : coords( x_val, y_val, z_val )
{
  attachedFacets = NULL;
  point_counter++;
  entityId = point_counter;
}

//===========================================================================
// Function Name: CubitPointData
//
// Member Type:  PUBLIC
// Description:  constructor
// Author: 
// Date:
//===========================================================================
CubitPointData::CubitPointData(double x_val, double y_val, double z_val,int *)
  : coords(x_val, y_val, z_val)
{
  attachedFacets = NULL;
  point_counter++;
  entityId = point_counter;
}

//===========================================================================
// Function Name: CubitPointData
//
// Member Type:  PUBLIC
// Description:  constructor
// Author: 
// Date:
//===========================================================================
CubitPointData::CubitPointData( const CubitVector &new_point )
  : coords( new_point )
{
  attachedFacets = NULL;
  point_counter++;
  entityId = point_counter;
}

//===========================================================================
// Function Name: ~CubitPoint
//
// Member Type:  PUBLIC
// Description:  destructor
// Author: 
// Date:
//===========================================================================
CubitPointData::~CubitPointData()
{
  delete attachedFacets;
}

//===========================================================================
// Function Name: coordinates
//
// Member Type:  PUBLIC
// Description:  return coordinates of point in form of array
// Author: 
// Date:
//===========================================================================
void CubitPointData::coordinates(double point_array[3])
{
  point_array[0] = coords.x();
  point_array[1] = coords.y();
  point_array[2] = coords.z();
}

//===========================================================================
// Function Name: add_facet
//
// Member Type:  PUBLIC
// Description:  add a facet to the point's adjacency list
// Author: 
// Date:
//===========================================================================
void CubitPointData::add_facet( CubitFacet *facet )
{
  if ( attachedFacets == NULL )
    attachedFacets = new DLIList<CubitFacet*>(8);
  attachedFacets->append(facet);
  return;
}

//===========================================================================
// Function Name: remove_facet
//
// Member Type:  PUBLIC
// Description:  remove a facet to the point's adjacency list
// Author: 
// Date:
//===========================================================================
void CubitPointData::remove_facet( CubitFacet *facet )
{
  if ( attachedFacets == NULL )
    return;
  attachedFacets->remove(facet);
  return;
}

//===========================================================================
// Function Name: edges
//
// Member Type:  PUBLIC
// Description:  return the list of facet-edges attached to this point
// Author: sjowen
// Date:  4/28/01
//===========================================================================
void CubitPointData::edges( DLIList<CubitFacetEdge *> &edge_list )
{
  int ii, jj, kk;
  CubitFacet *facet_ptr;
  CubitFacetEdge *edge_ptr, *check_edge_ptr;
  
  if(attachedFacets == NULL)
    return;

  for (ii=0; ii<attachedFacets->size(); ii++)
  {
    facet_ptr = attachedFacets->get_and_step();
    for (jj=0; jj<3; jj++)
    {
      edge_ptr = facet_ptr->edge( jj );
      if (edge_ptr)
      {
        if (edge_ptr->point(0) == this ||
          edge_ptr->point(1) == this)
        {
          int found = 0;
          for (kk=0; kk<edge_list.size() && !found; kk++)
          {
            check_edge_ptr = edge_list.get_and_step();
            if (check_edge_ptr == edge_ptr)
              found = 1;
          }
          if (!found)
          {
            edge_list.append( edge_ptr );
          }
        }
      }
    }
  }
  
  return;
}


//===========================================================================
// Function Name: num_adj_facets
//
// Member Type:  PUBLIC
// Description:  return the number of facets attached to the point
// Author: 
// Date:
//===========================================================================
int CubitPointData::num_adj_facets()
{ 
  if (attachedFacets == NULL)
    return 0;
  else
    return attachedFacets->size(); 
}

//===========================================================================
// Function Name: merge_points
//
// Member Type:  PUBLIC
// Description:  merge this point with another
// Note:         currently ignores edges - call before defining the edges
// Author: sjowen
// Date: 9/18/01
//===========================================================================
CubitStatus CubitPointData::merge_points( CubitPoint *other_point,
                                          CubitBoolean keep_point /* = CUBIT_FALSE*/)
{
  if( other_point == this )
    return CUBIT_SUCCESS;
  
  DLIList<CubitFacet *>facet_list;
  other_point->facets( facet_list );
  CubitFacet *adj_facet;
  CubitFacetData *afd;
  int ii;
  for (ii=0; ii<facet_list.size(); ii++)
  {
    adj_facet = facet_list.get_and_step();
    other_point->remove_facet( adj_facet );
    afd = CAST_TO(adj_facet, CubitFacetData);
    if (afd->point(0) == other_point)
      afd->set_point( this, 0 );
    else if(afd->point(1) == other_point)
      afd->set_point( this, 1 );
    else if(afd->point(2) == other_point)
      afd->set_point( this, 2 );
    else
    {
      assert(0);
      return CUBIT_FAILURE;
    }

    this->add_facet( adj_facet );
  
      // added by J.Kraftcheck - 2/14/03 - update edges also!!!
    CubitFacetEdgeData* afed;
    for ( int j = 0; j < 3; j++ ) {
      if( adj_facet->edge(j) ) {
        afed = dynamic_cast<CubitFacetEdgeData*>(adj_facet->edge(j));
        if ( afed->point(0) == other_point ) 
          afed->set_point(this, 0);
        else if( afed->point(1) == other_point )
          afed->set_point(this, 1);
      }
    } 
  }
  
  if (!keep_point)
    delete other_point;
  return CUBIT_SUCCESS;
}

CubitStatus CubitPointData::collapse_edge( CubitPointData *dead_point )
{
  int i, j;<--- Shadowed declaration
  
    // Get the list of facets that will be destroyed
  DLIList<CubitFacet*> dead_facets, edge_facets;
  shared_facets( dead_point, dead_facets );

    // Get edges to update
  DLIList<CubitFacetEdge*> adj_edge_list;
  dead_point->edges(adj_edge_list);
  
    // Get edge to collapse
  CubitFacetEdge* collapse = 0;
  for ( i = adj_edge_list.size(); i-- && !collapse; )
    if (adj_edge_list.step_and_get()->other_point(dead_point) == this)
      collapse = adj_edge_list.extract();

    // For each dead facet...
  CubitFacetData *dead_facet, *edge_facet;
  CubitFacetEdgeData *keep_edge, *dead_edge;
  for ( i = dead_facets.size(); i--; )
  {
      // Get dead facet and relevant indices
    dead_facet = dynamic_cast<CubitFacetData*>(dead_facets.get_and_step());
    assert(!!dead_facet);
    int dead_pt_index = dead_facet->point_index(dead_point);
    int this_pt_index = dead_facet->point_index(this);
    int other_pt_index = 3 - dead_pt_index - this_pt_index;
    
      // The get the other point (facet should have 
      // this point, the dead point, and one other).
    CubitPoint* other_pt = dead_facet->point(other_pt_index);
    
      // Get the edges to merge
    dead_edge = dynamic_cast<CubitFacetEdgeData*>(dead_facet->edge(this_pt_index));
    keep_edge = dynamic_cast<CubitFacetEdgeData*>(dead_facet->edge(dead_pt_index));
    edge_facets.clean_out();

    //propagate all the tds from dead edge to keep edge
    DLIList<ToolData*> tds;
    dead_edge->get_all_TDs(&tds);
    for (int i=0; i<tds.size(); i++)<--- Shadow variable
    {
      ToolData* new_td = tds.get_and_step()->propogate(keep_edge);
      if (new_td)
        keep_edge->add_TD(new_td);
    }
    
      // Get the list of adjacent facets to be updated
    dead_point->shared_facets(other_pt, edge_facets);
    
      // Determine relative sense of edges
    int dead_edge_pt = dead_edge->point(0) == other_pt ? 0 : 1;
    int rel_edge_sense = keep_edge->point(dead_edge_pt) == other_pt ? 1 : -1;
    
      // Update edge in each adjacent facet (merge keep_edge with dead_edge)
    for ( j = edge_facets.size(); j--; )
    {
      edge_facet = dynamic_cast<CubitFacetData*>(edge_facets.get_and_step());
      if ( edge_facet == dead_facet )
        continue;
      
      int dead_index = edge_facet->point_index(dead_point);
      int edge_index = (dead_index+1) % 3;
      if ( edge_facet->point(edge_index) == other_pt )
        edge_index = (dead_index+2) % 3;
      
      if ( dead_edge )
      {
        assert(edge_facet->edge(edge_index) == dead_edge);
        dead_edge->remove_facet(edge_facet);
        edge_facet->edge( 0, edge_index );
      }
      if ( keep_edge )
      {
        assert(edge_facet->edge(edge_index) == 0);
        keep_edge->add_facet(edge_facet);
        edge_facet->edge( keep_edge, edge_index );
        int use = rel_edge_sense * edge_facet->edge_use(edge_index);
        edge_facet->edge_use( use, edge_index );
      }
    }
    
      // delete dead entities
    
    delete dead_facet;
    
    if (dead_edge)
    {
      assert(dead_edge->num_adj_facets() == 0);
      
      adj_edge_list.move_to(dead_edge);
      assert(adj_edge_list.get() == dead_edge);
      adj_edge_list.extract();
      
      delete dead_edge;
    }
  }
  
  while (adj_edge_list.size())
  {
    keep_edge = dynamic_cast<CubitFacetEdgeData*>(adj_edge_list.pop());
    assert(!!keep_edge);
    if (keep_edge->point(0) == dead_point)
      keep_edge->set_point(this, 0);
    else if(keep_edge->point(1) == dead_point)
      keep_edge->set_point(this, 1);
    else
      assert(0);
  }
  
  DLIList<CubitFacet*> adj_facet_list(dead_point->num_adj_facets());
  dead_point->facets(adj_facet_list);
  while (adj_facet_list.size())
  {
    CubitFacetData* facet = dynamic_cast<CubitFacetData*>(adj_facet_list.pop());
    assert(!!facet);
    
    int index = facet->point_index(dead_point);
    assert((unsigned)index < (unsigned)3 && facet->point(index) == dead_point);
    
    dead_point->remove_facet(facet);
    this->add_facet(facet);
    facet->set_point( this, index );
  }
  
  
    // delete dead entities
  if ( collapse )
  {
    assert( collapse->num_adj_facets() == 0 );
    delete collapse;
  }

  assert( dead_point->num_adj_facets() == 0 );
  delete dead_point;
  
  return CUBIT_SUCCESS;
}