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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937 | /**
* MOAB, a Mesh-Oriented datABase, is a software component for creating,
* storing and accessing finite element mesh data.
*
* Copyright 2004 Sandia Corporation. Under the terms of Contract
* DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
* retains certain rights in this software.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
*/
#ifdef WIN32
#pragma warning( disable : 4786 )
#endif
#include "WriteUtil.hpp"
#include "moab/Core.hpp"
#include "moab/Error.hpp"
#include "SequenceManager.hpp"
#include "ElementSequence.hpp"
#include "VertexSequence.hpp"
#include "AEntityFactory.hpp"
#include "MBTagConventions.hpp"
#include "RangeSeqIntersectIter.hpp"
#include "MeshSetSequence.hpp"
#include <sys/types.h>
#include <sys/stat.h>
#include <cerrno>
#include <cassert>
#include <iostream>
#ifdef WIN32
#define stat _stat
#else
#include <unistd.h>
#endif
namespace moab
{
WriteUtil::WriteUtil( Core* mdb ) : WriteUtilIface(), mMB( mdb ) {}
//! Check if the specified file already exists.
//! Returns MB_SUCCESS if file does not exist, MB_ALREADY_ALLOCATED
//! if file does exist, or MB_FAILURE for some other error condition.
ErrorCode WriteUtil::check_doesnt_exist( const char* file_name )
{
struct stat s;
if( 0 == stat( file_name, &s ) )
{
MB_SET_ERR( MB_ALREADY_ALLOCATED, file_name << ": file already exists" );
}
else if( errno == ENOENT )
return MB_SUCCESS;
else
return MB_FAILURE;
}
//! Gather all entities in the mesh, or in the sets specified
ErrorCode WriteUtil::gather_entities( Range& all_ents,
/**< range in which entities are returned */
const EntityHandle* ent_sets,
/**< entity sets whose contents are to be gathered */
const int num_sets
/**< number of sets in list */
)
{
ErrorCode rval = MB_SUCCESS;
if( !ent_sets || num_sets == 0 )
{
rval = mMB->get_entities_by_handle( 0, all_ents );
}
else
{
for( int i = 0; i < num_sets; i++ )
{
ErrorCode tmp_rval = mMB->get_entities_by_handle( ent_sets[i], all_ents );
if( MB_SUCCESS != tmp_rval ) rval = tmp_rval;
}
}
return rval;
}
ErrorCode WriteUtil::get_node_coords( const int num_arrays,
const int num_nodes,
const Range& entities,
Tag node_id_tag,
const int start_node_id,
std::vector< double* >& arrays )
{
// Check the data coming into the function
// Dimension should be proper
if( num_arrays < 1 || num_arrays > 3 ) return MB_FAILURE;
// There should be some entities
// if (entities.empty())
// return MB_FAILURE;
// The above necessitates annoying special cases for files
// w/out vertices (e.g. a kD-tree). Return NULL array
// pointers instead. - kraftcheck, 3-14-08
if( entities.empty() )
{
arrays.clear();
arrays.resize( num_arrays, NULL );
return MB_SUCCESS;
}
// Memory should already be allocated for us
int tmp_num_arrays = 0;
for( unsigned int i = 0; i < 3; i++ )
if( i + 1 <= arrays.size() && NULL != arrays[i] ) tmp_num_arrays++;
if( 0 == tmp_num_arrays ) return MB_FAILURE;
// Get coordinate data
ErrorCode result = mMB->get_coords( entities, num_arrays < 1 || arrays.size() < 1 ? NULL : arrays[0],
num_arrays < 2 || arrays.size() < 2 ? NULL : arrays[1],
num_arrays < 3 || arrays.size() < 3 ? NULL : arrays[2] );
if( 0 == node_id_tag || MB_SUCCESS != result ) return result;
// Now assign tags
std::vector< int > ids( num_nodes );
int node_id = start_node_id;
for( int i = 0; i < num_nodes; i++ )
ids[i] = node_id++;
result = mMB->tag_set_data( node_id_tag, entities, &ids[0] );
return result;
}
ErrorCode WriteUtil::get_node_coords( const int which_array, /* 0->X, 1->Y, 2->Z, -1->all */
Range::const_iterator iter,
const Range::const_iterator& end,
const size_t output_array_len,
double* const output_array )
{
// Check the data coming into the function
// Dimension should be proper
if( which_array < -1 || which_array > 2 ) return MB_FAILURE;
// There should be some entities
if( iter == end ) return MB_FAILURE;
// Memory should already be allocated for us
if( NULL == output_array || 0 == output_array_len ) return MB_FAILURE;
// Sequence iterators
TypeSequenceManager::iterator seq_iter, seq_end;
seq_iter = mMB->sequence_manager()->entity_map( MBVERTEX ).begin();
seq_end = mMB->sequence_manager()->entity_map( MBVERTEX ).end();
// Loop over range, getting coordinate value
double* output_iter = output_array;
double* const output_end = output_array + output_array_len;
while( iter != end )
{
// Find the sequence containing the current handle
while( seq_iter != seq_end && ( *seq_iter )->end_handle() < *iter )
++seq_iter;
if( seq_iter == seq_end || *iter < ( *seq_iter )->start_handle() ) return MB_FAILURE;
// Determine how much of the sequence we want.
Range::pair_iterator pair( iter );
Range::const_iterator prev( end );
--prev;
EntityHandle range_end = pair->second;
EntityHandle sequence_end = ( *seq_iter )->end_handle();
EntityHandle end_handle = range_end > sequence_end ? sequence_end : range_end;
if( end_handle > *prev ) end_handle = *prev;
EntityHandle count = end_handle - *iter + 1;
// Get offset in sequence to start at
assert( *iter >= ( *seq_iter )->start_handle() );
EntityHandle offset = *iter - ( *seq_iter )->start_handle();
// Get coordinate arrays from sequence
double* coord_array[3];
static_cast< VertexSequence* >( *seq_iter )
->get_coordinate_arrays( coord_array[0], coord_array[1], coord_array[2] );
// Copy data to output buffer
if( -1 != which_array )
{
if( output_iter + count > output_end ) return MB_FAILURE;
memcpy( output_iter, coord_array[which_array] + offset, count * sizeof( double ) );
output_iter += count;
}
else
{
if( output_iter + 3 * count > output_end ) return MB_FAILURE;
for( unsigned int i = 0; i < count; i++ )
{
*output_iter = coord_array[0][i + offset];
output_iter++;
*output_iter = coord_array[1][i + offset];
output_iter++;
*output_iter = coord_array[2][i + offset];
output_iter++;
}
}
// Iterate
iter += count;
}
return MB_SUCCESS;
}
ErrorCode WriteUtil::get_element_connect( const int num_elements,
const int verts_per_element,
Tag node_id_tag,
const Range& elements,
Tag element_id_tag,
int start_element_id,
int* element_array,
bool add_sizes )
{
// Check the data we got
if( num_elements < 1 ) return MB_FAILURE;
if( verts_per_element < 1 ) return MB_FAILURE;
if( elements.empty() ) return MB_FAILURE;
if( !element_array ) return MB_FAILURE;
Range::const_iterator range_iter = elements.begin();
Range::const_iterator range_iter_end = elements.end();
TypeSequenceManager::iterator seq_iter, seq_iter_end;
EntityType current_type = TYPE_FROM_HANDLE( *range_iter );
seq_iter = mMB->sequence_manager()->entity_map( current_type ).begin();
seq_iter_end = mMB->sequence_manager()->entity_map( current_type ).end();
// Let's find the entity sequence which holds the first entity
TypeSequenceManager::iterator seq_iter_lookahead = seq_iter;
++seq_iter_lookahead;
for( ; seq_iter_lookahead != seq_iter_end && ( *seq_iter_lookahead )->start_handle() < *range_iter; )
{
++seq_iter;
++seq_iter_lookahead;
}
// A look ahead iterator
Range::const_iterator range_iter_lookahead = range_iter;
// Our main loop
for( ; range_iter != range_iter_end && seq_iter != seq_iter_end; /* ++ is handled in loop*/ )
{
// Find a range that fits in the current entity sequence
for( ; range_iter_lookahead != range_iter_end && *range_iter_lookahead <= ( *seq_iter )->end_handle();
++range_iter_lookahead )
{
}
if( current_type != TYPE_FROM_HANDLE( *range_iter ) )
{
current_type = TYPE_FROM_HANDLE( *range_iter );
seq_iter = mMB->sequence_manager()->entity_map( current_type ).begin();
seq_iter_end = mMB->sequence_manager()->entity_map( current_type ).end();
// Let's find the entity sequence which holds the first entity of this type
TypeSequenceManager::const_iterator seq_iter_lookahead2 = seq_iter;
++seq_iter_lookahead2;
for( ; seq_iter_lookahead2 != seq_iter_end && ( *seq_iter_lookahead2 )->start_handle() < *range_iter; )
{
++seq_iter;
++seq_iter_lookahead2;
}
}
int i = static_cast< ElementSequence* >( *seq_iter )->nodes_per_element();
// Get the connectivity array
EntityHandle* conn_array = static_cast< ElementSequence* >( *seq_iter )->get_connectivity_array();
EntityHandle start_handle = ( *seq_iter )->start_handle();
for( Range::const_iterator tmp_iter = range_iter; tmp_iter != range_iter_lookahead; ++tmp_iter )
{
// Set the element id tag
mMB->tag_set_data( element_id_tag, &*tmp_iter, 1, &start_element_id );
++start_element_id;
if( add_sizes ) *element_array++ = i;
// For each node
for( int j = 0; j < i; j++ )
{
EntityHandle node = *( conn_array + j + i * ( *tmp_iter - start_handle ) );
mMB->tag_get_data( node_id_tag, &node, 1, element_array );
element_array++;
}
}
// Go to the next entity sequence
++seq_iter;
// Start with the next entities
range_iter = range_iter_lookahead;
}
return MB_SUCCESS;
}
ErrorCode WriteUtil::get_element_connect( Range::const_iterator iter,
const Range::const_iterator& end,
const int vertices_per_elem,
Tag node_id_tag,
const size_t elem_array_size,
int* const element_array,
bool add_sizes )
{
// Check the data we got
if( iter == end ) return MB_FAILURE;
if( vertices_per_elem < 1 ) return MB_FAILURE;
if( !element_array || elem_array_size < (unsigned)vertices_per_elem ) return MB_FAILURE;
// Sequence iterators
TypeSequenceManager::const_iterator seq_iter, seq_end;
// loop over range, getting coordinate value
EntityType current_type = MBMAXTYPE;
int* output_iter = element_array;
int* const output_end = element_array + elem_array_size;
while( iter != end )
{
// Make sure we have the right sequence list (and get the sequence
// list for the first iteration.)
EntityType type = TYPE_FROM_HANDLE( *iter );
if( type != current_type )
{
if( type >= MBENTITYSET || type < MBEDGE ) return MB_FAILURE;
seq_iter = mMB->sequence_manager()->entity_map( type ).begin();
seq_end = mMB->sequence_manager()->entity_map( type ).end();
current_type = type;
}
// Find the sequence containing the current handle
while( seq_iter != seq_end && ( *seq_iter )->end_handle() < *iter )
++seq_iter;
if( seq_iter == seq_end || *iter < ( *seq_iter )->start_handle() ) return MB_FAILURE;
// Get the connectivity array
EntityHandle* conn_array = NULL;
int conn_size = static_cast< ElementSequence* >( *seq_iter )->nodes_per_element();
conn_array = static_cast< ElementSequence* >( *seq_iter )->get_connectivity_array();
// Determine how much of the sequence we want.
Range::pair_iterator pair( iter );
Range::const_iterator prev( end );
--prev;
EntityHandle range_end = pair->second;
EntityHandle sequence_end = ( *seq_iter )->end_handle();
EntityHandle end_handle = range_end > sequence_end ? sequence_end : range_end;
if( end_handle > *prev ) end_handle = *prev;
EntityHandle count = end_handle - *iter + 1;
// Get offset in sequence to start at
assert( *iter >= ( *seq_iter )->start_handle() );
EntityHandle offset = *iter - ( *seq_iter )->start_handle();
// Make sure sufficient space in output array
if( ( !add_sizes && output_iter + ( count * conn_size ) > output_end ) ||
( add_sizes && output_iter + ( count * ( conn_size + 1 ) ) > output_end ) )
return MB_FAILURE;
// If the nodes per element match, do in one call
conn_array += ( conn_size * offset );
if( vertices_per_elem == conn_size && !add_sizes )
{
ErrorCode rval = mMB->tag_get_data( node_id_tag, conn_array, count * conn_size, output_iter );
if( MB_SUCCESS != rval ) return rval;
output_iter += count * conn_size;
}
// Otherwise need to do one at a time
else
{
int min = vertices_per_elem > conn_size ? conn_size : vertices_per_elem;
for( EntityHandle i = 0; i < count; ++i )
{
*output_iter++ = min;
ErrorCode rval = mMB->tag_get_data( node_id_tag, conn_array, min, output_iter );
if( MB_SUCCESS != rval ) return rval;
output_iter += min;
conn_array += conn_size;
if( vertices_per_elem > conn_size )
{ // Need to pad
memset( output_iter, 0, sizeof( int ) * ( vertices_per_elem - conn_size ) );
output_iter += ( vertices_per_elem - conn_size );
}
}
}
iter += count;
}
return MB_SUCCESS;
}
ErrorCode WriteUtil::get_element_connect( Range::const_iterator iter,
const Range::const_iterator& end,
const int vertices_per_elem,
const size_t elem_array_size,
EntityHandle* const element_array )
{
// Check the data we got
if( iter == end ) return MB_FAILURE;
if( vertices_per_elem < 1 ) return MB_FAILURE;
if( !element_array || elem_array_size < (unsigned)vertices_per_elem ) return MB_FAILURE;
// Sequence iterators
TypeSequenceManager::const_iterator seq_iter, seq_end;
// Loop over range, getting coordinate value
EntityType current_type = MBMAXTYPE;
EntityHandle* output_iter = element_array;
EntityHandle* const output_end = element_array + elem_array_size;
while( iter != end )
{
// Make sure we have the right sequence list (and get the sequence
// list for the first iteration.)
EntityType type = TYPE_FROM_HANDLE( *iter );
if( type != current_type )
{
if( type >= MBENTITYSET || type < MBEDGE ) return MB_FAILURE;
seq_iter = mMB->sequence_manager()->entity_map( type ).begin();
seq_end = mMB->sequence_manager()->entity_map( type ).end();
current_type = type;
}
// Find the sequence containing the current handle
while( seq_iter != seq_end && ( *seq_iter )->end_handle() < *iter )
++seq_iter;
if( seq_iter == seq_end || *iter < ( *seq_iter )->start_handle() ) return MB_FAILURE;
// Get the connectivity array
EntityHandle* conn_array = NULL;
int conn_size = static_cast< ElementSequence* >( *seq_iter )->nodes_per_element();
if( conn_size != vertices_per_elem ) return MB_FAILURE;
conn_array = static_cast< ElementSequence* >( *seq_iter )->get_connectivity_array();
// Determine how much of the sequence we want.
Range::pair_iterator pair( iter );
Range::const_iterator prev( end );
--prev;
EntityHandle range_end = pair->second;
EntityHandle sequence_end = ( *seq_iter )->end_handle();
EntityHandle end_handle = range_end > sequence_end ? sequence_end : range_end;
if( end_handle > *prev ) end_handle = *prev;
EntityHandle count = end_handle - *iter + 1;
// Get offset in sequence to start at
assert( *iter >= ( *seq_iter )->start_handle() );
EntityHandle offset = *iter - ( *seq_iter )->start_handle();
// Make sure sufficient space in output array
if( output_iter + ( count * conn_size ) > output_end ) return MB_FAILURE;
if( conn_array == NULL )
{ // If it is structured mesh
ErrorCode rval;
int temp_buff_size = conn_size * sizeof( EntityHandle );
for( unsigned i = 0; i < count; i++ )
{ // Copy connectivity element by element
std::vector< EntityHandle > connect;
rval = static_cast< ElementSequence* >( *seq_iter )->get_connectivity( *iter, connect );
if( MB_SUCCESS != rval )
{
return rval;
}
memcpy( output_iter, &connect[0], temp_buff_size );
output_iter += conn_size;
++iter;
}
}
else
{
// Copy connectivity into output array
conn_array += ( conn_size * offset );
memcpy( output_iter, conn_array, count * conn_size * sizeof( EntityHandle ) );
output_iter += count * conn_size;
iter += count;
}
}
return MB_SUCCESS;
}
ErrorCode WriteUtil::get_poly_connect_size( Range::const_iterator /* begin */,<--- The function 'get_poly_connect_size' is never used.
const Range::const_iterator& /* end */,
int& /* connectivity_size */ )
{
return MB_NOT_IMPLEMENTED;
}
ErrorCode WriteUtil::get_poly_connect( Range::const_iterator& /* iter */,
const Range::const_iterator& /* end */,
const Tag /* node_id_tag */,
size_t& /* handle_array_len */,
int* const /* handle_array */,
size_t& /* index_array_len */,
int* const /* index_array */,
int& /* index_offset */ )
{
return MB_NOT_IMPLEMENTED;
}
ErrorCode WriteUtil::gather_nodes_from_elements( const Range& elements, const Tag node_bit_mark_tag, Range& nodes )
{
bool printed_warning = false;
if( elements.empty() ) return MB_SUCCESS;
if( TYPE_FROM_HANDLE( elements.front() ) <= MBVERTEX || TYPE_FROM_HANDLE( elements.back() ) >= MBENTITYSET )
return MB_TYPE_OUT_OF_RANGE;
// See if we need to use our own marking tag
Tag exporting_nodes_tag = 0;
if( node_bit_mark_tag )
exporting_nodes_tag = node_bit_mark_tag;
else
{
mMB->tag_get_handle( "__MBWriteUtil::exporting_nodes", 1, MB_TYPE_BIT, exporting_nodes_tag, MB_TAG_CREAT );
}
// The x,y,z tag handles we need
EntityHandle lower_bound = ~0, upper_bound = 0;
std::vector< EntityHandle > tmp_conn;
RangeSeqIntersectIter iter( mMB->sequence_manager() );
for( ErrorCode rval = iter.init( elements.begin(), elements.end() ); MB_FAILURE != rval; rval = iter.step() )
{
if( MB_ENTITY_NOT_FOUND == rval )
{
if( !printed_warning )
{
std::cerr << "Warning: ignoring invalid element handle(s) in gather_nodes_from_elements" << std::endl;
printed_warning = true;
}
continue;
}
ElementSequence* seq = static_cast< ElementSequence* >( iter.get_sequence() );
// Get the connectivity array
const EntityHandle* conn_array = seq->get_connectivity_array();
// If unstructured mesh
if( conn_array && mMB->type_from_handle( iter.get_start_handle() ) != MBPOLYHEDRON )
{
assert( iter.get_start_handle() >= seq->start_handle() );
assert( iter.get_end_handle() <= seq->end_handle() );
const EntityHandle offset = iter.get_start_handle() - seq->start_handle();
const EntityHandle num_elem = iter.get_end_handle() - iter.get_start_handle() + 1;
conn_array += offset * seq->nodes_per_element();
const EntityHandle num_node = num_elem * seq->nodes_per_element();
// For each node
for( EntityHandle j = 0; j < num_node; j++ )
{
EntityHandle node = conn_array[j];
if( node < lower_bound ) lower_bound = node;
if( node > upper_bound ) upper_bound = node;
unsigned char bit = 0x1;
rval = mMB->tag_set_data( exporting_nodes_tag, &node, 1, &bit );
assert( MB_SUCCESS == rval );
if( MB_SUCCESS != rval ) return rval;
}
}
// Polyhedra
else if( conn_array && mMB->type_from_handle( iter.get_start_handle() ) == MBPOLYHEDRON )
{
assert( iter.get_start_handle() >= seq->start_handle() );
assert( iter.get_end_handle() <= seq->end_handle() );
const EntityHandle offset = iter.get_start_handle() - seq->start_handle();
const EntityHandle num_elem = iter.get_end_handle() - iter.get_start_handle() + 1;
conn_array += offset * seq->nodes_per_element();
int num_face = num_elem * seq->nodes_per_element();
// For each node
for( int j = 0; j < num_face; j++ )
{
const EntityHandle* face_conn = NULL;
int face_num_conn = 0;
rval = mMB->get_connectivity( conn_array[j], face_conn, face_num_conn, false );
if( MB_SUCCESS != rval ) return rval;
for( int k = 0; k < face_num_conn; k++ )
{
EntityHandle node = face_conn[k];
if( node < lower_bound ) lower_bound = node;
if( node > upper_bound ) upper_bound = node;
unsigned char bit = 0x1;
rval = mMB->tag_set_data( exporting_nodes_tag, &node, 1, &bit );
assert( MB_SUCCESS == rval );
if( MB_SUCCESS != rval ) return rval;
}
}
}
// Structured mesh
else
{
EntityHandle end_h = iter.get_end_handle() + 1;
for( EntityHandle h = iter.get_start_handle(); h < end_h; ++h )
{
tmp_conn.clear();
rval = seq->get_connectivity( h, tmp_conn, false );
if( MB_SUCCESS != rval )
{
if( node_bit_mark_tag == 0 ) mMB->tag_delete( exporting_nodes_tag );
return rval;
}
// For each node
for( size_t j = 0; j < tmp_conn.size(); j++ )
{
EntityHandle node = tmp_conn[j];
if( node < lower_bound ) lower_bound = node;
if( node > upper_bound ) upper_bound = node;
unsigned char bit = 0x1;
mMB->tag_set_data( exporting_nodes_tag, &node, 1, &bit );
}
}
}
}
// We can get a REALLY long loop if lower_bound is zero
assert( lower_bound != 0 );
// Gather up all the nodes
for( ; upper_bound >= lower_bound; --upper_bound )
{
unsigned char node_marked = 0;
mMB->tag_get_data( exporting_nodes_tag, &upper_bound, 1, &node_marked );
if( node_marked == 0x1 ) nodes.insert( upper_bound );
}
// Clean up our own marking tag
if( node_bit_mark_tag == 0 ) mMB->tag_delete( exporting_nodes_tag );
return MB_SUCCESS;
}
//! Assign ids to input elements starting with start_id, written to id_tag
//! if zero, assigns to GLOBAL_ID_TAG_NAME
ErrorCode WriteUtil::assign_ids( Range& elements, Tag id_tag, const int start_id )
{
ErrorCode result;
if( 0 == id_tag )
{
// Get the global id tag
id_tag = mMB->globalId_tag();
}
// Now assign the ids
int i;
Range::iterator rit;
ErrorCode tmp_result;
result = MB_SUCCESS;
for( i = start_id, rit = elements.begin(); rit != elements.end(); ++rit, i++ )
{
tmp_result = mMB->tag_set_data( id_tag, &( *rit ), 1, &i );
if( MB_SUCCESS != tmp_result ) result = tmp_result;
}
return result;
}
ErrorCode WriteUtil::get_adjacencies( EntityHandle entity, Tag id_tag, std::vector< int >& adj )
{
ErrorCode rval;
const EntityHandle* adj_array;
int num_adj, id;
// Get handles of adjacent entities
rval = mMB->a_entity_factory()->get_adjacencies( entity, adj_array, num_adj );
if( MB_SUCCESS != rval )
{
adj.clear();
return rval;
}
// Append IDs of adjacent entities -- skip meshsets
adj.resize( num_adj ); // Pre-allocate space
adj.clear(); // Clear used space
const EntityHandle* const end = adj_array + num_adj;
for( const EntityHandle* iter = adj_array; iter != end; ++iter )
{
if( TYPE_FROM_HANDLE( *iter ) != MBENTITYSET )
{
rval = mMB->tag_get_data( id_tag, iter, 1, &id );
if( MB_SUCCESS != rval ) return rval;
adj.push_back( id );
}
}
return MB_SUCCESS;
}
ErrorCode WriteUtil::get_adjacencies( EntityHandle entity, const EntityHandle*& adj_array, int& num_adj )
{
return mMB->a_entity_factory()->get_adjacencies( entity, adj_array, num_adj );
}
ErrorCode WriteUtil::get_tag_list( std::vector< Tag >& result_list,
const Tag* user_tag_list,
int user_tag_list_length,
bool include_variable_length_tags )
{
ErrorCode rval;
if( user_tag_list )
{
result_list.clear();
result_list.reserve( user_tag_list_length );
for( int i = 0; i < user_tag_list_length; ++i )
{
std::string name;
rval = mMB->tag_get_name( user_tag_list[i], name );MB_CHK_SET_ERR( rval, "Error " << (int)rval << " getting name for tag (Invalid input tag handle?)" );
if( name.empty() )
{
MB_SET_ERR( MB_TAG_NOT_FOUND, "Explicit request to save anonymous tag" );
}
int size;
if( !include_variable_length_tags &&
MB_VARIABLE_DATA_LENGTH == mMB->tag_get_length( user_tag_list[i], size ) )
{
MB_SET_ERR( MB_TYPE_OUT_OF_RANGE, "File format cannot store variable-length tag: \"" << name << "\"" );
}
result_list.push_back( user_tag_list[i] );
}
}
else
{
std::vector< Tag > temp_list;
rval = mMB->tag_get_tags( temp_list );MB_CHK_SET_ERR( rval, "Interface::tag_get_tags failed" );
result_list.clear();
result_list.reserve( temp_list.size() );
std::vector< Tag >::iterator i;
for( i = temp_list.begin(); i != temp_list.end(); ++i )
{
std::string name;
rval = mMB->tag_get_name( *i, name );MB_CHK_SET_ERR( rval, "Error " << (int)rval << " getting name for tag (Stale tag handle?)" );
// Skip anonymous tags
if( name.empty() ) continue;
// Skip private/internal tags
if( name.size() >= 2 && name[0] == '_' && name[1] == '_' ) continue;
// If requested, skip variable-length tags
int size;
if( !include_variable_length_tags && MB_VARIABLE_DATA_LENGTH == mMB->tag_get_length( *i, size ) ) continue;
result_list.push_back( *i );
}
}
return MB_SUCCESS;
}
ErrorCode WriteUtil::get_entity_list_pointers( Range::const_iterator begin,
Range::const_iterator end,
EntityHandle const** pointers,
EntityListType relation,
int* lengths,
unsigned char* flags )
{
RangeSeqIntersectIter iter( mMB->sequence_manager() );
ErrorCode rval = iter.init( begin, end );
while( MB_SUCCESS == rval )
{
EntityType type = TYPE_FROM_HANDLE( iter.get_start_handle() );
if( MBENTITYSET == type )
{
const MeshSetSequence* seq = reinterpret_cast< MeshSetSequence* >( iter.get_sequence() );
const MeshSet* set;
int len = 0;
size_t clen;
for( EntityHandle h = iter.get_start_handle(); h <= iter.get_end_handle(); ++h )
{
set = seq->get_set( h );
switch( relation )
{
case CONTENTS:
*pointers = set->get_contents( clen );
len = clen;
break;
case CHILDREN:
*pointers = set->get_children( len );
break;
case PARENTS:
*pointers = set->get_parents( len );
break;
}
if( lengths )
{
*lengths = len;
++lengths;
}
if( flags )
{
*flags = (unsigned char)set->flags();
++flags;
}
++pointers;
}
}
else if( MBVERTEX != type )
{
const bool topological = ( relation == TOPOLOGICAL );
int len;
const ElementSequence* seq = reinterpret_cast< ElementSequence* >( iter.get_sequence() );
for( EntityHandle h = iter.get_start_handle(); h <= iter.get_end_handle(); ++h )
{
rval = seq->get_connectivity( h, *pointers, len, topological );
if( MB_SUCCESS != rval ) return rval;
if( lengths )
{
*lengths = len;
++lengths;
}
if( flags )
{
*flags = 0;
++flags;
}
++pointers;
}
}
else
{
return MB_TYPE_OUT_OF_RANGE;
}
rval = iter.step();
}
if( MB_FAILURE == rval )
return MB_SUCCESS; // At end of list
else
return rval;
}
ErrorCode WriteUtil::get_entity_list_pointers( EntityHandle const* entities,
int num_entities,
EntityHandle const** pointers,
EntityListType relation,
int* lengths,
unsigned char* flags )
{
SequenceManager* sm = mMB->sequence_manager();
const EntitySequence* tmp_seq;
ErrorCode rval = MB_SUCCESS;
for( int i = 0; i < num_entities; i++ )
{
rval = sm->find( entities[i], tmp_seq );
if( MB_SUCCESS != rval ) return rval;
EntityType type = TYPE_FROM_HANDLE( entities[i] );
if( MBENTITYSET == type )
{
const MeshSetSequence* seq = reinterpret_cast< const MeshSetSequence* >( tmp_seq );
const MeshSet* set;
int len = 0;
size_t clen;
set = seq->get_set( entities[i] );
switch( relation )
{
case CONTENTS:
*pointers = set->get_contents( clen );
len = clen;
break;
case CHILDREN:
*pointers = set->get_children( len );
break;
case PARENTS:
*pointers = set->get_parents( len );
break;
}
if( lengths )
{
*lengths = len;
++lengths;
}
if( flags )
{
*flags = (unsigned char)set->flags();
++flags;
}
++pointers;
}
else if( MBVERTEX != type )
{
const bool topological = ( relation == TOPOLOGICAL );
int len;
const ElementSequence* seq = reinterpret_cast< const ElementSequence* >( tmp_seq );
rval = seq->get_connectivity( entities[i], *pointers, len, topological );
if( MB_SUCCESS != rval ) return rval;
if( lengths )
{
*lengths = len;
++lengths;
}
if( flags )
{
*flags = 0;
++flags;
}
++pointers;
}
else
{
return MB_TYPE_OUT_OF_RANGE;
}
}
return MB_SUCCESS;
}
} // namespace moab
|