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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
#include "SphereDecomp.hpp"
#include "moab/MeshTopoUtil.hpp"
#include "moab/Range.hpp"
#include "moab/CN.hpp"
#include <cmath>
#include <cassert>
#include <iostream>

#define RR \
    if( MB_SUCCESS != result ) return result

const char* SUBDIV_VERTICES_TAG_NAME = "subdiv_vertices";

using namespace moab;

SphereDecomp::SphereDecomp( Interface* impl )
{
    mbImpl = impl;
}

ErrorCode SphereDecomp::build_sphere_mesh( const char* sphere_radii_tag_name, EntityHandle* hex_set )
{
    ErrorCode result = mbImpl->tag_get_handle( sphere_radii_tag_name, 1, MB_TYPE_DOUBLE, sphereRadiiTag );RR;

    // need to make sure all interior edges and faces are created
    Range all_verts;
    result = mbImpl->get_entities_by_type( 0, MBVERTEX, all_verts );RR;
    MeshTopoUtil mtu( mbImpl );
    result = mtu.construct_aentities( all_verts );RR;

    // create tag to hold vertices
    result = mbImpl->tag_get_handle( SUBDIV_VERTICES_TAG_NAME, 9, MB_TYPE_HANDLE, subdivVerticesTag,
                                     MB_TAG_DENSE | MB_TAG_EXCL );RR;

    // compute nodal positions for each dimension element
    result = compute_nodes( 1 );RR;
    result = compute_nodes( 2 );RR;
    result = compute_nodes( 3 );RR;

    // build hex elements
    std::vector< EntityHandle > sphere_hexes, interstic_hexes;
    result = build_hexes( sphere_hexes, interstic_hexes );RR;

    result = mbImpl->tag_delete( subdivVerticesTag );RR;

    if( NULL != hex_set )
    {
        if( 0 == *hex_set )
        {
            EntityHandle this_set;
            // make a new set
            result = mbImpl->create_meshset( MESHSET_SET, this_set );RR;
            *hex_set = this_set;
        }

        // save all the hexes to this set
        result = mbImpl->add_entities( *hex_set, &sphere_hexes[0], sphere_hexes.size() );RR;
        result = mbImpl->add_entities( *hex_set, &interstic_hexes[0], interstic_hexes.size() );RR;
    }

    return result;
}

ErrorCode SphereDecomp::compute_nodes( const int dim )
{
    // get facets of that dimension
    Range these_ents;
    const EntityType the_types[4] = { MBVERTEX, MBEDGE, MBTRI, MBTET };

    ErrorCode result = mbImpl->get_entities_by_dimension( 0, dim, these_ents );RR;
    assert( mbImpl->type_from_handle( *these_ents.begin() ) == the_types[dim] &&
            mbImpl->type_from_handle( *these_ents.rbegin() ) == the_types[dim] );

    EntityHandle subdiv_vertices[9];
    MeshTopoUtil mtu( mbImpl );
    double avg_pos[3], vert_pos[12], new_vert_pos[12], new_new_vert_pos[3];
    double radii[4], unitv[3];
    int num_verts = CN::VerticesPerEntity( the_types[dim] );

    for( Range::iterator rit = these_ents.begin(); rit != these_ents.end(); ++rit )
    {

        // get vertices
        const EntityHandle* connect;
        int num_connect;
        result = mbImpl->get_connectivity( *rit, connect, num_connect );RR;

        // compute center
        result = mtu.get_average_position( connect, num_connect, avg_pos );RR;

        // create center vertex
        result = mbImpl->create_vertex( avg_pos, subdiv_vertices[num_verts] );RR;

        // get coords of other vertices
        result = mbImpl->get_coords( connect, num_connect, vert_pos );RR;

        // get radii associated with each vertex
        result = mbImpl->tag_get_data( sphereRadiiTag, connect, num_connect, radii );RR;

        // compute subdiv vertex position for each vertex
        for( int i = 0; i < num_verts; i++ )
        {
            for( int j = 0; j < 3; j++ )
                unitv[j] = avg_pos[j] - vert_pos[3 * i + j];
            double vlength = sqrt( unitv[0] * unitv[0] + unitv[1] * unitv[1] + unitv[2] * unitv[2] );
            if( vlength < radii[i] )
            {
                std::cout << "Radius too large at vertex " << i << std::endl;
                result = MB_FAILURE;
                continue;
            }

            for( int j = 0; j < 3; j++ )
                unitv[j] /= vlength;

            for( int j = 0; j < 3; j++ )
                new_vert_pos[3 * i + j] = vert_pos[3 * i + j] + radii[i] * unitv[j];

            // create vertex at this position
            ErrorCode tmp_result = mbImpl->create_vertex( &new_vert_pos[3 * i], subdiv_vertices[i] );
            if( MB_SUCCESS != tmp_result ) result = tmp_result;
        }

        if( MB_SUCCESS != result ) return result;

        // compute subdiv vertex positions for vertices inside spheres; just mid-pt between
        // previous subdiv vertex and corner vertex
        for( int i = 0; i < num_verts; i++ )
        {
            for( int j = 0; j < 3; j++ )
                new_new_vert_pos[j] = .5 * ( vert_pos[3 * i + j] + new_vert_pos[3 * i + j] );

            result = mbImpl->create_vertex( new_new_vert_pos, subdiv_vertices[num_verts + 1 + i] );<--- result is assigned
        }

        // set the tag
        result = mbImpl->tag_set_data( subdivVerticesTag, &( *rit ), 1, subdiv_vertices );RR;<--- result is overwritten
    }

    return result;
}

ErrorCode SphereDecomp::build_hexes( std::vector< EntityHandle >& sphere_hexes,
                                     std::vector< EntityHandle >& interstic_hexes )
{
    // build hexes inside each tet element separately
    Range tets;
    ErrorCode result = mbImpl->get_entities_by_type( 0, MBTET, tets );RR;

    for( Range::iterator vit = tets.begin(); vit != tets.end(); ++vit )
    {
        result = subdivide_tet( *vit, sphere_hexes, interstic_hexes );RR;
    }

    return MB_SUCCESS;
}

ErrorCode SphereDecomp::subdivide_tet( EntityHandle tet,
                                       std::vector< EntityHandle >& sphere_hexes,
                                       std::vector< EntityHandle >& interstic_hexes )
{
    // 99: (#subdiv_verts/entity=9) * (#edges=6 + #faces=4 + 1=tet)
    EntityHandle subdiv_verts[99];

    // get tet connectivity
    std::vector< EntityHandle > tet_conn;
    ErrorCode result = mbImpl->get_connectivity( &tet, 1, tet_conn );RR;

    for( int dim = 1; dim <= 3; dim++ )
    {
        // get entities of this dimension
        std::vector< EntityHandle > ents;
        if( dim != 3 )
        {
            result = mbImpl->get_adjacencies( &tet, 1, dim, false, ents );RR;
        }
        else
            ents.push_back( tet );

        // for each, get subdiv verts & put into vector
        for( std::vector< EntityHandle >::iterator vit = ents.begin(); vit != ents.end(); ++vit )
        {
            result = retrieve_subdiv_verts( tet, *vit, &tet_conn[0], dim, subdiv_verts );RR;
        }
    }

    // ok, subdiv_verts are in canonical order; now create the hexes, using pre-computed templates

    // Templates are specified in terms of the vertices making up each hex; vertices are specified
    // by specifying the facet index and type they resolve, and the index of that vertex in that
    // facet's subdivision vertices list.

    // Each facet is subdivided into:
    // - a mid vertex
    // - one vertex for each corner vertex on the facet (located on a line between the mid vertex
    // and
    //   the corresponding corner vertex, a distance equal to the sphere radius away from the corner
    //   vertex)
    // - one vertex midway between each corner vertex and the corresponding "sphere surface" vertex
    // For edges, tris and tets this gives 5, 7 and 9 subdivision vertices, respectively.
    // Subdivision vertices appear in the list in the order: sphere surface vertices, mid vertex,
    // sphere interior vertices.  In each of those sub lists, vertices are listed in the canonical
    // order of the corresponding corner vertices for that facet.

    // Subdivision vertices for facetes are indexed by listing the facet type they resolve (EDGE,
    // FACE, TET), the index of that facet (integer = 0..5, 0..3, 0 for edges, tris, tet, resp), and
    // subdivision index (AINDEX..EINDEX for edges, AINDEX..GINDEX for tris, AINDEX..IINDEX for
    // tets).

    // Subdivision vertices for all facets of a tet are stored in one subdivision vertex vector, in
    // order of increasing facet dimension and index (index varies fastest).  The ESV, FSV, and TSV
    // macros are used to compute the indices into that vector for various parameters.  The CV macro
    // is used to index into the tet connectivity vector.

    // Subdivision templates for splitting the tet into 28 hexes were derived by hand, and are
    // listed below (using the indexing scheme described above).

#define EDGE        0
#define FACE        1
#define TET         2
#define AINDEX      0
#define BINDEX      1
#define CINDEX      2
#define DINDEX      3
#define EINDEX      4
#define FINDEX      5
#define GINDEX      6
#define HINDEX      7
#define IINDEX      8
#define V0INDEX     0
#define V1INDEX     1
#define V2INDEX     2
#define V3INDEX     3
#define CV( a )     tet_conn[a]
#define ESV( a, b ) subdiv_verts[(a)*9 + ( b )]
#define FSV( a, b ) subdiv_verts[54 + (a)*9 + ( b )]
#define TSV( a, b ) subdiv_verts[90 + (a)*9 + ( b )]

    EntityHandle this_connect[8], this_hex;

    // first, interstices hexes, three per vertex/spherical surface
    // V0:
    int i             = 0;
    this_connect[i++] = ESV( 0, AINDEX );
    this_connect[i++] = ESV( 0, CINDEX );
    this_connect[i++] = FSV( 3, DINDEX );
    this_connect[i++] = FSV( 3, AINDEX );
    this_connect[i++] = FSV( 0, AINDEX );
    this_connect[i++] = FSV( 0, DINDEX );
    this_connect[i++] = TSV( 0, EINDEX );
    this_connect[i++] = TSV( 0, AINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    interstic_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = FSV( 0, AINDEX );
    this_connect[i++] = FSV( 0, DINDEX );
    this_connect[i++] = TSV( 0, EINDEX );
    this_connect[i++] = TSV( 0, AINDEX );
    this_connect[i++] = ESV( 3, AINDEX );
    this_connect[i++] = ESV( 3, CINDEX );
    this_connect[i++] = FSV( 2, DINDEX );
    this_connect[i++] = FSV( 2, AINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    interstic_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = FSV( 3, AINDEX );
    this_connect[i++] = FSV( 3, DINDEX );
    this_connect[i++] = ESV( 2, CINDEX );
    this_connect[i++] = ESV( 2, BINDEX );
    this_connect[i++] = TSV( 0, AINDEX );
    this_connect[i++] = TSV( 0, EINDEX );
    this_connect[i++] = FSV( 2, DINDEX );
    this_connect[i++] = FSV( 2, AINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    interstic_hexes.push_back( this_hex );

    // V1:
    i                 = 0;
    this_connect[i++] = ESV( 0, CINDEX );
    this_connect[i++] = ESV( 0, BINDEX );
    this_connect[i++] = FSV( 3, CINDEX );
    this_connect[i++] = FSV( 3, DINDEX );
    this_connect[i++] = FSV( 0, DINDEX );
    this_connect[i++] = FSV( 0, BINDEX );
    this_connect[i++] = TSV( 0, BINDEX );
    this_connect[i++] = TSV( 0, EINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    interstic_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = FSV( 0, DINDEX );
    this_connect[i++] = FSV( 0, BINDEX );
    this_connect[i++] = TSV( 0, BINDEX );
    this_connect[i++] = TSV( 0, EINDEX );
    this_connect[i++] = ESV( 4, CINDEX );
    this_connect[i++] = ESV( 4, AINDEX );
    this_connect[i++] = FSV( 1, AINDEX );
    this_connect[i++] = FSV( 1, DINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    interstic_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = FSV( 1, DINDEX );
    this_connect[i++] = FSV( 1, AINDEX );
    this_connect[i++] = TSV( 0, BINDEX );
    this_connect[i++] = TSV( 0, EINDEX );
    this_connect[i++] = ESV( 1, CINDEX );
    this_connect[i++] = ESV( 1, AINDEX );
    this_connect[i++] = FSV( 3, CINDEX );
    this_connect[i++] = FSV( 3, DINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    interstic_hexes.push_back( this_hex );

    // V2:
    i                 = 0;
    this_connect[i++] = FSV( 3, DINDEX );
    this_connect[i++] = ESV( 1, CINDEX );
    this_connect[i++] = ESV( 1, BINDEX );
    this_connect[i++] = FSV( 3, BINDEX );
    this_connect[i++] = TSV( 0, EINDEX );
    this_connect[i++] = FSV( 1, DINDEX );
    this_connect[i++] = FSV( 1, BINDEX );
    this_connect[i++] = TSV( 0, CINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    interstic_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = TSV( 0, EINDEX );
    this_connect[i++] = FSV( 1, DINDEX );
    this_connect[i++] = FSV( 1, BINDEX );
    this_connect[i++] = TSV( 0, CINDEX );
    this_connect[i++] = FSV( 2, DINDEX );
    this_connect[i++] = ESV( 5, CINDEX );
    this_connect[i++] = ESV( 5, AINDEX );
    this_connect[i++] = FSV( 2, CINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    interstic_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = TSV( 0, CINDEX );
    this_connect[i++] = FSV( 2, CINDEX );
    this_connect[i++] = ESV( 2, AINDEX );
    this_connect[i++] = FSV( 3, BINDEX );
    this_connect[i++] = TSV( 0, EINDEX );
    this_connect[i++] = FSV( 2, DINDEX );
    this_connect[i++] = ESV( 2, CINDEX );
    this_connect[i++] = FSV( 3, DINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    interstic_hexes.push_back( this_hex );

    // V3:
    i                 = 0;
    this_connect[i++] = TSV( 0, EINDEX );
    this_connect[i++] = FSV( 1, DINDEX );
    this_connect[i++] = ESV( 5, CINDEX );
    this_connect[i++] = FSV( 2, DINDEX );
    this_connect[i++] = TSV( 0, DINDEX );
    this_connect[i++] = FSV( 1, CINDEX );
    this_connect[i++] = ESV( 5, BINDEX );
    this_connect[i++] = FSV( 2, BINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    interstic_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = FSV( 0, DINDEX );
    this_connect[i++] = ESV( 4, CINDEX );
    this_connect[i++] = FSV( 1, DINDEX );
    this_connect[i++] = TSV( 0, EINDEX );
    this_connect[i++] = FSV( 0, CINDEX );
    this_connect[i++] = ESV( 4, BINDEX );
    this_connect[i++] = FSV( 1, CINDEX );
    this_connect[i++] = TSV( 0, DINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    interstic_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = ESV( 3, CINDEX );
    this_connect[i++] = FSV( 0, DINDEX );
    this_connect[i++] = TSV( 0, EINDEX );
    this_connect[i++] = FSV( 2, DINDEX );
    this_connect[i++] = ESV( 3, BINDEX );
    this_connect[i++] = FSV( 0, CINDEX );
    this_connect[i++] = TSV( 0, DINDEX );
    this_connect[i++] = FSV( 2, BINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    interstic_hexes.push_back( this_hex );

    // now, the sphere interiors, four hexes per vertex sphere

    // V0:
    i                 = 0;
    this_connect[i++] = CV( V0INDEX );
    this_connect[i++] = ESV( 0, DINDEX );
    this_connect[i++] = FSV( 3, EINDEX );
    this_connect[i++] = ESV( 2, EINDEX );
    this_connect[i++] = ESV( 3, DINDEX );
    this_connect[i++] = FSV( 0, EINDEX );
    this_connect[i++] = TSV( 0, FINDEX );
    this_connect[i++] = FSV( 2, EINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    sphere_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = ESV( 0, DINDEX );
    this_connect[i++] = ESV( 0, AINDEX );
    this_connect[i++] = FSV( 3, AINDEX );
    this_connect[i++] = FSV( 3, EINDEX );
    this_connect[i++] = FSV( 0, EINDEX );
    this_connect[i++] = FSV( 0, AINDEX );
    this_connect[i++] = TSV( 0, AINDEX );
    this_connect[i++] = TSV( 0, FINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    sphere_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = FSV( 3, EINDEX );
    this_connect[i++] = FSV( 3, AINDEX );
    this_connect[i++] = ESV( 2, BINDEX );
    this_connect[i++] = ESV( 2, EINDEX );
    this_connect[i++] = TSV( 0, FINDEX );
    this_connect[i++] = TSV( 0, AINDEX );
    this_connect[i++] = FSV( 2, AINDEX );
    this_connect[i++] = FSV( 2, EINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    sphere_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = TSV( 0, FINDEX );
    this_connect[i++] = TSV( 0, AINDEX );
    this_connect[i++] = FSV( 2, AINDEX );
    this_connect[i++] = FSV( 2, EINDEX );
    this_connect[i++] = FSV( 0, EINDEX );
    this_connect[i++] = FSV( 0, AINDEX );
    this_connect[i++] = ESV( 3, AINDEX );
    this_connect[i++] = ESV( 3, DINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    sphere_hexes.push_back( this_hex );

    // V1:
    i                 = 0;
    this_connect[i++] = CV( V1INDEX );
    this_connect[i++] = ESV( 1, DINDEX );
    this_connect[i++] = FSV( 3, GINDEX );
    this_connect[i++] = ESV( 0, EINDEX );
    this_connect[i++] = ESV( 4, DINDEX );
    this_connect[i++] = FSV( 1, EINDEX );
    this_connect[i++] = TSV( 0, GINDEX );
    this_connect[i++] = FSV( 0, FINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    sphere_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = FSV( 3, GINDEX );
    this_connect[i++] = ESV( 1, DINDEX );
    this_connect[i++] = ESV( 1, AINDEX );
    this_connect[i++] = FSV( 3, CINDEX );
    this_connect[i++] = TSV( 0, GINDEX );
    this_connect[i++] = FSV( 1, EINDEX );
    this_connect[i++] = FSV( 1, AINDEX );
    this_connect[i++] = TSV( 0, BINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    sphere_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = TSV( 0, GINDEX );
    this_connect[i++] = FSV( 1, EINDEX );
    this_connect[i++] = FSV( 1, AINDEX );
    this_connect[i++] = TSV( 0, BINDEX );
    this_connect[i++] = FSV( 0, FINDEX );
    this_connect[i++] = ESV( 4, DINDEX );
    this_connect[i++] = ESV( 4, AINDEX );
    this_connect[i++] = FSV( 0, BINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    sphere_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = ESV( 0, BINDEX );
    this_connect[i++] = ESV( 0, EINDEX );
    this_connect[i++] = FSV( 3, GINDEX );
    this_connect[i++] = FSV( 3, CINDEX );
    this_connect[i++] = FSV( 0, BINDEX );
    this_connect[i++] = FSV( 0, FINDEX );
    this_connect[i++] = TSV( 0, GINDEX );
    this_connect[i++] = TSV( 0, BINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    sphere_hexes.push_back( this_hex );

    // V2:
    i                 = 0;
    this_connect[i++] = ESV( 1, BINDEX );
    this_connect[i++] = ESV( 1, EINDEX );
    this_connect[i++] = FSV( 3, FINDEX );
    this_connect[i++] = FSV( 3, BINDEX );
    this_connect[i++] = FSV( 1, BINDEX );
    this_connect[i++] = FSV( 1, FINDEX );
    this_connect[i++] = TSV( 0, HINDEX );
    this_connect[i++] = TSV( 0, CINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    sphere_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = FSV( 3, FINDEX );
    this_connect[i++] = ESV( 1, EINDEX );
    this_connect[i++] = CV( V2INDEX );
    this_connect[i++] = ESV( 2, DINDEX );
    this_connect[i++] = TSV( 0, HINDEX );
    this_connect[i++] = FSV( 1, FINDEX );
    this_connect[i++] = ESV( 5, DINDEX );
    this_connect[i++] = FSV( 2, GINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    sphere_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = TSV( 0, HINDEX );
    this_connect[i++] = FSV( 1, FINDEX );
    this_connect[i++] = ESV( 5, DINDEX );
    this_connect[i++] = FSV( 2, GINDEX );
    this_connect[i++] = TSV( 0, CINDEX );
    this_connect[i++] = FSV( 1, BINDEX );
    this_connect[i++] = ESV( 5, AINDEX );
    this_connect[i++] = FSV( 2, CINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    sphere_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = FSV( 3, BINDEX );
    this_connect[i++] = FSV( 3, FINDEX );
    this_connect[i++] = ESV( 2, DINDEX );
    this_connect[i++] = ESV( 2, AINDEX );
    this_connect[i++] = TSV( 0, CINDEX );
    this_connect[i++] = TSV( 0, HINDEX );
    this_connect[i++] = FSV( 2, GINDEX );
    this_connect[i++] = FSV( 2, CINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    sphere_hexes.push_back( this_hex );

    // V3:
    i                 = 0;
    this_connect[i++] = FSV( 0, CINDEX );
    this_connect[i++] = ESV( 4, BINDEX );
    this_connect[i++] = FSV( 1, CINDEX );
    this_connect[i++] = TSV( 0, DINDEX );
    this_connect[i++] = FSV( 0, GINDEX );
    this_connect[i++] = ESV( 4, EINDEX );
    this_connect[i++] = FSV( 1, GINDEX );
    this_connect[i++] = TSV( 0, IINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    sphere_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = ESV( 3, BINDEX );
    this_connect[i++] = FSV( 0, CINDEX );
    this_connect[i++] = TSV( 0, DINDEX );
    this_connect[i++] = FSV( 2, BINDEX );
    this_connect[i++] = ESV( 3, EINDEX );
    this_connect[i++] = FSV( 0, GINDEX );
    this_connect[i++] = TSV( 0, IINDEX );
    this_connect[i++] = FSV( 2, FINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    sphere_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = TSV( 0, DINDEX );
    this_connect[i++] = FSV( 1, CINDEX );
    this_connect[i++] = ESV( 5, BINDEX );
    this_connect[i++] = FSV( 2, BINDEX );
    this_connect[i++] = TSV( 0, IINDEX );
    this_connect[i++] = FSV( 1, GINDEX );
    this_connect[i++] = ESV( 5, EINDEX );
    this_connect[i++] = FSV( 2, FINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    sphere_hexes.push_back( this_hex );

    i                 = 0;
    this_connect[i++] = FSV( 0, GINDEX );
    this_connect[i++] = ESV( 4, EINDEX );
    this_connect[i++] = FSV( 1, GINDEX );
    this_connect[i++] = TSV( 0, IINDEX );
    this_connect[i++] = ESV( 3, EINDEX );
    this_connect[i++] = CV( V3INDEX );
    this_connect[i++] = ESV( 5, EINDEX );
    this_connect[i++] = FSV( 2, FINDEX );
    result            = mbImpl->create_element( MBHEX, this_connect, 8, this_hex );RR;
    sphere_hexes.push_back( this_hex );

    return result;
}

ErrorCode SphereDecomp::retrieve_subdiv_verts( EntityHandle tet,
                                               EntityHandle this_ent,
                                               const EntityHandle* tet_conn,
                                               const int dim,
                                               EntityHandle* subdiv_verts )
{
    // get the subdiv verts for this entity
    ErrorCode result;

    // if it's a tet, just put them on the end & return
    if( tet == this_ent )
    {
        result = mbImpl->tag_get_data( subdivVerticesTag, &this_ent, 1, &subdiv_verts[90] );<--- Variable 'result' is assigned a value that is never used.
        return MB_SUCCESS;
    }

    // if it's a sub-entity, need to find index, relative orientation, and offset
    // get connectivity of sub-entity
    std::vector< EntityHandle > this_conn;
    result = mbImpl->get_connectivity( &this_ent, 1, this_conn );RR;

    // get relative orientation
    std::vector< int > conn_tet_indices( this_conn.size() );
    for( size_t i = 0; i < this_conn.size(); ++i )
        conn_tet_indices[i] = std::find( tet_conn, tet_conn + 4, this_conn[i] ) - tet_conn;
    int sense, side_no, offset;
    int success = CN::SideNumber( MBTET, &conn_tet_indices[0], this_conn.size(), dim, side_no, sense, offset );
    if( -1 == success ) return MB_FAILURE;

    // start of this entity's subdiv_verts; edges go first, then preceding sides, then this one;
    // this assumes 6 edges/tet
    EntityHandle* subdiv_start = &subdiv_verts[( ( dim - 1 ) * 6 + side_no ) * 9];

    // get subdiv_verts and put them into proper place
    result = mbImpl->tag_get_data( subdivVerticesTag, &this_ent, 1, subdiv_start );

    // could probably do this more elegantly, but isn't worth it
#define SWITCH( a, b )                        \
    {                                         \
        EntityHandle tmp_handle = a;          \
        ( a )                   = b;          \
        ( b )                   = tmp_handle; \
    }
    switch( dim )
    {
        case 1:
            if( offset != 0 || sense == -1 )
            {
                SWITCH( subdiv_start[0], subdiv_start[1] );
                SWITCH( subdiv_start[3], subdiv_start[4] );
            }
            break;
        case 2:
            // rotate first
            if( 0 != offset )
            {
                std::rotate( subdiv_start, subdiv_start + offset, subdiv_start + 3 );
                std::rotate( subdiv_start + 4, subdiv_start + 4 + offset, subdiv_start + 7 );
            }
            // now flip, if necessary
            if( -1 == sense )
            {
                SWITCH( subdiv_start[1], subdiv_start[2] );
                SWITCH( subdiv_start[5], subdiv_start[6] );
            }
            break;
        default:
            return MB_FAILURE;
    }

    // ok, we're done
    return MB_SUCCESS;
}