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
#include "GMem.hpp"<--- Skipping configuration 'ACIS' since the value of 'ACIS' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'DBL_MAX' since the value of 'DBL_MAX' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'DBL_MIN' since the value of 'DBL_MIN' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'M_PI' since the value of 'M_PI' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
#include <cstring> // To define NULL
#include "CubitVector.hpp"
#include "DLIList.hpp"
#include "OctTree.hpp"
#include "CubitTransformMatrix.hpp"

GMem::GMem()
{
   ptsSize = 0;
   fListSize = 0;
   pointListCount = 0;
   fListCount = 0;
   pointList = NULL;
   facetList = NULL;
   pointsConsolidated = CUBIT_FALSE;
}

GMem::~GMem()
{
   if (pointList)
      delete [] pointList;
   if (facetList)
      delete [] facetList;
}

void GMem::allocate_tri(int num_tri)
{
   if (num_tri*3 > ptsSize) 
   {
      if (pointList)
         delete [] pointList;
      ptsSize = 3 * num_tri;
      pointList = new GPoint[ptsSize];
   } 
   if (num_tri*4 > fListSize) 
   {
      if (facetList)
         delete [] facetList;
      fListSize = 4 * num_tri;
      facetList = new int[fListSize]; 
   }
}

// Allocates space for a polyline consisting of
// {num_lines} segments.  This is num_lines + 1 pointList.
void GMem::allocate_polylines(int num_lines)
{
   num_lines++;
   if (num_lines > ptsSize)
   {
      if (pointList)
         delete [] pointList;
      ptsSize = num_lines;
      pointList = new GPoint[ptsSize];
   }
}

// Allocates additional space for a bigger polyline.
// The total number of points will now be
// pointListCount + num_additional_segs.
void GMem::allocate_more_polylines(int num_additional_segs)
{
     // Make sure we need to do something
   if (num_additional_segs <= 0)
      return;
     // If there are no points, we need space for one additional point
   if (pointListCount == 0)
      num_additional_segs++;
     // Make sure we need to grow
   if (ptsSize >= pointListCount + num_additional_segs)
      return;

     // Make a big enough array of points
   GPoint *new_points = new GPoint[pointListCount + num_additional_segs];
     // Copy the old data to the new array
   if (pointList)
   {
      memcpy (new_points, pointList, pointListCount*sizeof(GPoint));
      delete []pointList;
   }
     // Store the new array in 'pointList'
   pointList = new_points;
   ptsSize = pointListCount + num_additional_segs;
}

void GMem::clean_out()
{
   pointListCount = 0;
   fListCount = 0;
}


void GMem::replace_point_list(GPoint new_point_list[], int num_valid_points,
                              int array_size)
{
     // First, delete the old memory
   if (pointList)
      delete [] pointList;
     // Now replace it with the new
   pointList = new_point_list;
     // Set the array size
   ptsSize = array_size;
     // Set the number of valid entries
   pointListCount = num_valid_points;
}

void GMem::replace_facet_list(int new_facet_list[], int num_valid_entries,
                              int array_size)
{
     // First, delete the old memory
   if (facetList)
      delete [] facetList;
     // Now replace it with the new
   facetList = new_facet_list;
     // Set the array size
   fListSize = array_size;
     // Set the number of valid entries
   fListCount = num_valid_entries;
}

// copy constructor
GMem::GMem(const GMem& from)
{
     // Set the array pointers to NULL
   pointList = NULL;
   facetList = NULL;
     // Set one equal to the other
   *this = from;
}

// Equality operator
GMem& GMem::operator=(const GMem& from)
{
   if (this != &from)
   {
        // Make a copy of the point array
      GPoint* temp1 = new GPoint[from.ptsSize];
      memcpy (temp1, from.pointList, from.ptsSize*sizeof(GPoint));
        // Put it in the receiving GMem
      replace_point_list(temp1, from.pointListCount, from.ptsSize);
        // Make a copy of the facet array
      int* temp2 = new int[from.fListSize];
      memcpy (temp2, from.facetList, from.fListSize*sizeof(int));
        // Put it in the receiving GMem
      replace_facet_list(temp2, from.fListCount, from.fListSize);
        // Set whether it's consolidated
      pointsConsolidated = from.pointsConsolidated;
   }
   return *this;
}


void GMem::consolidate_points( double tolerance )
{
  if ( pointListCount < 1000 )
    consolidate_few_points( tolerance );
  else 
    consolidate_many_points( tolerance );
}

void GMem::consolidate_few_points( double tolerance )
{
  const double tolsqr = tolerance * tolerance;
  int* index_map = new int[pointListCount];
    
    // Consolidate the point list.  index_map is used
    // to maintain a map between the old index of a point
    // (the index into index_map) and the new index of a
    // point (the value in index_map).
  int write = 0, read, comp;
  for( read = 0; read < pointListCount; read++ )
  {
    const GPoint& pti = pointList[read];
    for( comp = 0; comp < write; comp++ )
    {
      const GPoint& ptj = pointList[comp];
      double x = pti.x - ptj.x;
      double y = pti.y - ptj.y;
      double z = pti.z - ptj.z;
      if( (x*x+y*y+z*z) <= tolsqr )
        break;
    }
    
    index_map[read] = comp;
    if( comp == write )
    {
      pointList[comp] = pointList[read];
      write++;
    }
  }
  pointListCount = write;  

    // Update the facet list using values from index_map.
  int *itor = facetList;
  const int* end = facetList + fListCount;
  while( itor < end )
    for( int count = *(itor++); count--; itor++ )
      *itor = index_map[*itor];

  delete [] index_map;
  pointsConsolidated = CUBIT_TRUE;
}

class GPointOctTreeEval {
  public: static inline CubitVector coordinates(GPoint* p)
    { return CubitVector( p->x, p->y, p->z ); }
};

void GMem::consolidate_many_points( double tolerance )
{
  const double tolsqr = tolerance * tolerance;
  
    // build OctTree
  DLIList<GPoint*> point_list(pointListCount);
  GPoint* p_itor = pointList;
  GPoint* p_end  = p_itor + pointListCount;
  while( p_itor < p_end )
    point_list.append( p_itor++ );
  OctTree<GPoint, GPointOctTreeEval> tree( point_list, tolerance );
  point_list.clean_out();
  
    // Consolidate the point list.  index_map is used
    // to maintain a map between the old index of a point
    // (the index into index_map) and the new index of a
    // point (the value in index_map).
  int* index_map = new int[pointListCount];
  GPoint* new_array = new GPoint[pointListCount];
  int read, write = 0;
  for ( read = 0; read < pointListCount; read++)
  {
    GPoint* pt = pointList + read;
    CubitVector v(pt->x, pt->y, pt->z);
    index_map[read] = write;
    point_list.clean_out();
    tree.nodes_near( v, point_list );
    while ( point_list.size() )
    {
      GPoint* p = point_list.pop();
      int index = p - pointList;
      assert( (index >= 0) && (index < pointListCount) );
      CubitVector v2(p->x, p->y, p->z);
      if ( (index < read) && ((v - v2).length_squared() < tolsqr) )
      {
        index_map[read] = index_map[index];
        break;
      }
    }
    
    if ( index_map[read] == write )
    {
      new_array[write++] = pointList[read];
    }
  }
  pointListCount = write;  
  delete [] pointList;
  pointList = new_array;
  
    // Update the facet list using values from index_map.
  int *itor = facetList;
  const int* end = facetList + fListCount;
  while( itor < end )
    for( int count = *(itor++); count--; itor++ )
      *itor = index_map[*itor];

  delete [] index_map;
  pointsConsolidated = CUBIT_TRUE;
}

void GMem::transform(CubitTransformMatrix &transform)
{
  int i=0;
  CubitVector temp_point;
  for (i=0; i<this->pointListCount; i++)
  {
    temp_point.set(pointList[i].x, pointList[i].y, pointList[i].z);
    temp_point = transform * temp_point;
    pointList[i].x = (float)temp_point.x();
    pointList[i].y = (float)temp_point.y();
    pointList[i].z = (float)temp_point.z();
  }
}