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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//-------------------------------------------------------------------------
// Filename      : RefVolume.cpp
//
// Purpose       : This file contains the implementation of the class 
//                 RefVolume. 
//
// Special Notes :
//
// Creator       : 
//
// Creation Date : 
//
// Owner         : 
//-------------------------------------------------------------------------

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <assert.h>

#include "CubitDefines.h"
#include "CubitMessage.hpp"
#include "CubitBox.hpp"

#include "Body.hpp"
#include "RefVolume.hpp"
#include "RefFace.hpp"
#include "RefEdge.hpp"
#include "RefVertex.hpp"
#include "CoVolume.hpp"
#include "Shell.hpp"

// tools
#include "RefEntityFactory.hpp"
#include "GeometryQueryTool.hpp"
#include "ModelQueryEngine.hpp"

// lists
#include "DLIList.hpp"

#include "Lump.hpp"

//-------------------------------------------------------------------------
// Purpose       : Constructor with a pointer to a Lump 
//
// Special Notes :
//
// Creator       : Xuchen Liu
//
// Creation Date : 07/11/96
//-------------------------------------------------------------------------
RefVolume::RefVolume(Lump* lumpPtr)
{
   // Set the GeometryEntity pointer
   if (lumpPtr != NULL)
   {
      set_geometry_entity_ptr(lumpPtr) ;

      // Commented-out code from Phil Tuchinsky for FUTURE meshing
      //   from a surface mesh with no solid model.
      //
//    hasGeometry = CUBIT_TRUE;
//
      // initialize the surface mesh entity lists exactly once
//    nodes_boundary  ( surfaceNodes );
//    edges_inclusive ( surfaceEdges );
//    faces_inclusive ( surfaceFaces );
   }
   else
   {
      PRINT_ERROR("In the RefVolume(Lump*) constructor\n");
      PRINT_ERROR("       Input Lump pointer is NULL\n");
      assert(CUBIT_FALSE);
   }
   
   // Initialize the member data
   initialize();
}

//-------------------------------------------------------------------------
// Purpose       : The destructor.
//
// Special Notes :
//
// Creator       : Raikanta Sahu
//
// Creation Date : 10/22/96
//-------------------------------------------------------------------------
RefVolume::~RefVolume()
{
}

//-------------------------------------------------------------------------
// Purpose       : Return a pointer to the Lump associated with a volume.
//
// Special Notes :
//
// Creator       : Xuechen Liu
//
// Creation Date : 08/02/96
//-------------------------------------------------------------------------

Lump* RefVolume::get_lump_ptr() 
{
  return CAST_TO(get_geometry_entity_ptr(), Lump);
}

Lump const* RefVolume::get_lump_ptr() const 
{
  return CAST_TO(get_geometry_entity_ptr(), Lump);
}

//-------------------------------------------------------------------------
// Purpose       : Get parent Body
//
// Special Notes : 
//
// Creator       : Jason Kraftcheck
//
// Creation Date : 02/11/03
//-------------------------------------------------------------------------
Body* RefVolume::get_body_ptr()
{
  SenseEntity* co_volume_ptr = get_first_sense_entity_ptr();
  
  if (!co_volume_ptr                  // no covolume 
    || co_volume_ptr->next_on_bte() ) // multiple covolumes )
  {
    PRINT_ERROR("%s:%d RefVolume %d has other than one parent CoVolume.\n"
                "This is a BUG -- please report it.\n",
                __FILE__, __LINE__, id() );
    return 0;
  }
  
  return dynamic_cast<Body*>(co_volume_ptr->get_grouping_entity_ptr());
}

int RefVolume::genus() 
{
  int i;
  DLIList<RefFace*> faces;
  ref_faces(faces);
  int gs = 0;
  for (i = faces.size(); i > 0; i--)
    gs += faces.get_and_step()->genus();
  
  return 1 - (num_ref_vertices() - num_ref_edges() + faces.size() - gs)/2;
}

CubitVector RefVolume::center_point()
{
  return bounding_box().center();
}

int RefVolume::dimension() const
{
  return 3;
}

CubitStatus RefVolume::mass_properties( CubitVector &centroid, double &volume )
{
  DLIList<Body*> bodies;
  this->bodies( bodies );
  if( bodies.get()->is_sheet_body() )
  {
    centroid.set(0,0,0);
    volume = 0;
    return CUBIT_SUCCESS;
  }
  else
  {
    Lump *lump = get_lump_ptr();
    return lump->mass_properties( centroid, volume );
  }
}

CubitStatus RefVolume::mass_properties( CubitVector  principal_axes[3], 
                                        CubitVector &principal_moments, 
                                        CubitVector &centroid, 
                                        double &volume )
{
  DLIList<Body*> bodies;
  this->bodies( bodies );
  if( bodies.get()->is_sheet_body() )
  {
    centroid.set(0,0,0);
    volume = 0;
    return CUBIT_SUCCESS;
  }
  else
  {
    Lump *lump = get_lump_ptr();
    return lump->mass_properties( principal_axes, principal_moments, centroid, volume );
  }
}

CubitString RefVolume::measure_label()
{
  return "volume";
}

// Draws each surface in the volume
/*
void RefVolume::draw_my_faces ( int color )
{
   DLIList<RefFace*> ref_face_list;
   int i;

   if (color == CUBIT_DEFAULT_COLOR_INDEX)
      color = this->color();
   ref_faces ( ref_face_list );
   ref_face_list.reset();
   for (i = 0; i < ref_face_list.size(); i++ )
      ref_face_list.get_and_step()->draw(color);
}
*/

//-------------------------------------------------------------------------
// Purpose       : Initializes member data
//
// Special Notes :
//
// Creator       : Malcolm J. Panthaki
//
// Creation Date : 10/07/96
//-------------------------------------------------------------------------
void RefVolume::initialize()
{
   // Set the Entity and Block IDs for this new RefVolume
   GeometryEntity* geom_ptr = get_geometry_entity_ptr();
   int saved_id = geom_ptr->get_saved_id();
   if ( !saved_id || RefEntityFactory::instance()->get_ref_volume(saved_id) )
   {
     saved_id =  RefEntityFactory::instance()->next_ref_volume_id();
     geom_ptr->set_saved_id(saved_id);
   }
   entityId = saved_id;

     // read and initialize attributes
   auto_read_cubit_attrib();
   auto_actuate_cubit_attrib();

     // Assign a default entity name
   assign_default_name();   
}

// unmark this face, and
// recursively unmark all other marked faces sharing an edge with this face.
void unmark_component( RefFace *face )
{
  assert( face->marked() );
  face->marked( CUBIT_FALSE );
  DLIList<RefEdge*> edges;
  face->ref_edges( edges );
  DLIList<RefFace*> edge_faces;
  RefEdge *edge;
  for ( int e = edges.size(); e--; ) 
  {
    edge = edges.get_and_step();
    edge_faces.clean_out();
    edge->ref_faces( edge_faces );
    for ( int f = edge_faces.size(); f--; ) 
    {
      face = edge_faces.get_and_step();
      if ( face->marked() )
         unmark_component( face );
    }
  }
}

int RefVolume::num_boundary_components() 
{
  // get all the surfaces
  // march from one to the next, using shared-ref_edge connectivity
  
  int num_components = 0;
  DLIList<RefFace*> face_list;
  ref_faces( face_list );

  // mark them as belonging to this volume and not seen yet.
  int i;
  RefFace *face;
  for( i = face_list.size(); i--; )
  {
    face = face_list.get_and_step();
    face->marked( CUBIT_FALSE );
  }
  
  for ( i = face_list.size(); i--; ) 
  {
    face = face_list.get_and_step();
    assert( !face->marked() );
    face->marked( CUBIT_TRUE );
  }

  for ( i = face_list.size(); i--; ) 
  {
    face = face_list.get_and_step();
    if ( face->marked() ) 
    {
      num_components++;
      unmark_component( face );      
    }
  }
  return num_components;
}

CubitBoolean RefVolume::about_spatially_equal( 
    RefVolume* ref_vol_ptr_2,double tolerance_factor)
{
     // Get rid of the trivial case...
   if( this == ref_vol_ptr_2)
   {
      return CUBIT_TRUE;
   }

   DLIList<RefEdge*> ref_edge_list_1, ref_edge_list_2;
   this->ref_edges( ref_edge_list_1 );
   ref_vol_ptr_2->ref_edges( ref_edge_list_2 );
      
        //compare the size of the two lists.
   if ( ref_edge_list_1.size() != ref_edge_list_2.size() )
     return CUBIT_FALSE;
      
   DLIList<RefFace*> ref_face_list_1, ref_face_list_2;
   this->ref_faces( ref_face_list_1 );
   ref_vol_ptr_2->ref_faces( ref_face_list_2 );
      
        //compare the size of the two lists.
   if ( ref_face_list_1.size() != ref_face_list_2.size() )
     return CUBIT_FALSE;
      
   
     //This compare precedure does the following :
     //   1. Test the bounding boxes of the 2 volumes for equality;
        
   CubitBox box_1 = this->bounding_box();
   CubitBox box_2 = ref_vol_ptr_2->bounding_box();

   CubitBoolean passes_b_box = CUBIT_FALSE;

       // This test checks to see that the min and max vectors of the
       // bounding boxes are within 10% of the length of the bbox diagonal.
       // Note that this assumes the default values of resabs=1e-6 and
       // tolerance_factor=500

   CubitVector tol_vect(
      CUBIT_MIN(box_1.x_range(), box_2.x_range()),
      CUBIT_MIN(box_1.y_range(), box_2.y_range()),
      CUBIT_MIN(box_1.z_range(), box_2.z_range()) );
   tol_vect *= 200.0 * tolerance_factor * GEOMETRY_RESABS;
   if( tol_vect.x() < GEOMETRY_RESABS )
      tol_vect.x(GEOMETRY_RESABS);
   if( tol_vect.y() < GEOMETRY_RESABS )
      tol_vect.y(GEOMETRY_RESABS);
   if( tol_vect.z() < GEOMETRY_RESABS )
      tol_vect.z(GEOMETRY_RESABS);
   
   if((fabs(box_1.minimum().x() - box_2.minimum().x()) < tol_vect.x()) &&
      (fabs(box_1.maximum().x() - box_2.maximum().x()) < tol_vect.x()) &&
      (fabs(box_1.minimum().y() - box_2.minimum().y()) < tol_vect.y()) &&
      (fabs(box_1.maximum().y() - box_2.maximum().y()) < tol_vect.y()) &&
      (fabs(box_1.minimum().z() - box_2.minimum().z()) < tol_vect.z()) &&
      (fabs(box_1.maximum().z() - box_2.maximum().z()) < tol_vect.z())) 
   {
     passes_b_box = CUBIT_TRUE;
   }
   
   return passes_b_box;
}    

//-------------------------------------------------------------------------
// Purpose       : Check if all shells are sheets
//
// Special Notes : 
//
// Creator       : Jason Kraftcheck
//
// Creation Date : 01/15/04
//-------------------------------------------------------------------------
CubitBoolean RefVolume::is_sheet()
{
  DLIList<Shell*> shells;
  this->shells(shells);
  while (shells.size())
    if (!shells.pop()->is_sheet())
      return CUBIT_FALSE;
  return CUBIT_TRUE;
}
  
int RefVolume::validate()
{
     //- This function determines whether the entity is valid.
     //- Several types of checks can be done, 
   int error = 0;

     // Perform general RefEntity checks (measure > 0)
   error += RefEntity::validate();

     // Pass through to surface and add in its validation
   Lump *lump = this->get_lump_ptr();

     // check surface ptr
   if (lump != NULL) {
        // Check underlying surface
	   DLIList <TopologyEntity*> bad_entities;
      error += lump->validate(entity_name(), bad_entities);
   } else {
      PRINT_WARNING("\tWARNING: Null underlying volume for %s, (%s %d)\n",
                    entity_name().c_str(), class_name(), id());
      error++;
   }
   return error;
}

void RefVolume::get_parent_ref_entities(DLIList<RefEntity*>& entity_list)
{

  // First get the type of RefEntity that is a child of "this" one
  DagType parent_type = get_parent_ref_entity_type();;

  DLIList<TopologyEntity*> tempList ;

  CubitStatus result = ModelQueryEngine::instance()->
      query_model( *this, parent_type, tempList );
  if (result == CUBIT_FAILURE)
  {
    PRINT_ERROR("In RefEntity::get_parent_ref_entities\n");
    PRINT_ERROR("       Query failed for unknown reason.\n");
    return;
  }

  entity_list.clean_out();
  for(int i=0; i<tempList.size(); i++)
  {
    entity_list.append(static_cast<ParentType*>(tempList[i]));
  }
}