32 const std::string& basename,
33 const std::string& suffix,
34 const char* exp_suffix,
37 std::ifstream& file_stream,
40 std::string real_file_name;
45 if( suffix == exp_suffix )
47 real_file_name = filename;
51 real_file_name = basename;
52 real_file_name +=
".";
53 real_file_name += exp_suffix;
57 if( !real_file_name.empty() ) file_stream.open( real_file_name.c_str(), std::ios::in );
58 if( file_required && !file_stream.is_open() )
79 const Tag* file_id_tag )
81 std::ifstream node_file, ele_file, face_file, edge_file;
89 std::string suffix, base, filename( file_name_c );
90 size_t dot_idx = filename.find_last_of(
'.' );
91 if( dot_idx == std::string::npos )
97 suffix = filename.substr( dot_idx + 1 );
98 for(
size_t i = 0; i < suffix.length(); ++i )
99 suffix[i] = (
char)tolower( suffix[i] );
100 if( suffix ==
"node" || suffix ==
"ele" || suffix ==
"face" || suffix ==
"edge" )
102 base = filename.substr( 0, dot_idx );
111 rval =
open_file( filename, base, suffix,
"node",
"NODE_FILE", opts, node_file,
true );
113 rval =
open_file( filename, base, suffix,
"ele",
"ELE_FILE", opts, ele_file );
115 rval =
open_file( filename, base, suffix,
"face",
"FACE_FILE", opts, face_file );
117 rval =
open_file( filename, base, suffix,
"edge",
"EDGE_FILE", opts, edge_file );
120 std::vector< Tag > attr_tags[4];
121 std::vector< int > attr_idx[4];
122 const char* option_names[4] = {
"NODE_ATTR_LIST",
"EDGE_ATTR_LIST",
"TRI_ATTR_LIST",
"TET_ATTR_LIST" };
123 const char* group_names[4] = { 0,
"CURVE_ID",
"SURFACE_ID",
"VOLUME_ID" };
124 for(
int i = 0; i < 4; ++i )
129 rval =
parse_attr_list( opt_str, attr_tags[i], attr_idx[i], group_names[i] );
136 Range tets, tris, edges;
137 std::vector< EntityHandle > nodes;
138 rval =
read_node_file( node_file, &attr_tags[0][0], &attr_idx[0][0], attr_tags[0].
size(), nodes );
152 std::vector< Tag >& tag_list,
153 std::vector< int >& index_list,
154 const char* group_designator )
156 std::vector< std::string > name_list;
158 while( prev_pos != std::string::npos )
160 size_t pos = option_str.find_first_of(
',', prev_pos );
161 name_list.push_back( option_str.substr( prev_pos, pos ) );
165 index_list.resize( name_list.size() );
166 std::map< std::string, int > name_count;
167 for(
size_t i = 0; i < name_list.size(); ++i )
168 index_list[i] = name_count[name_list[i]]++;
170 for(
size_t i = 0; i < name_list.size(); ++i )
172 if( group_designator && name_list[i] == group_designator )
177 else if( name_list.empty() )
203 size_t pos = line.find_first_of(
'#' );
204 if( pos != std::string::npos ) line = line.substr( 0, pos );
206 for( pos = 0; pos < line.length() && isspace( line[pos] ); ++pos )
208 if( pos == line.length() )
211 line = line.substr( pos );
212 }
while( line.empty() );
225 std::stringstream str( line );
226 for(
int i = 0; i < num_values; ++i )
231 MB_SET_ERR( MB_FAILURE,
"Error reading node data at line " << lineno );
238 if( ( str >> junk ) || !str.eof() )
240 MB_SET_ERR( MB_FAILURE,
"Unexpected trailing data for line " << lineno <<
" of node data" );
247 const Tag* attr_tag_list,
248 const int* attr_tag_index,
249 int attr_tag_list_len,
250 std::vector< EntityHandle >& nodes )
255 double header_vals[4];
256 rval =
read_line( file, header_vals, 4, lineno );
259 const int num_vtx = (int)header_vals[0];
260 const int dim = (int)header_vals[1];
261 const int num_attr = (int)header_vals[2];
262 const int bdry_flag = (int)header_vals[3];
263 if( num_vtx < 1 || dim < 2 || dim > 3 || num_attr < 0 || bdry_flag < 0 || bdry_flag > 1 )
265 MB_SET_ERR( MB_FAILURE,
"Invalid header line for node data" );
267 if( attr_tag_list_len > num_attr ) attr_tag_list_len = num_attr;
270 std::map< Tag, int > tag_size;
271 std::map< Tag, std::vector< double > > tag_data;
272 for(
int i = 0; i < attr_tag_list_len; ++i )
274 if( !attr_tag_list[i] || attr_tag_index[i] < 0 )
continue;
275 std::vector< double >& data = tag_data[attr_tag_list[i]];
278 data.resize( data.size() + num_vtx );
279 ++tag_size[attr_tag_list[i]];
281 std::vector< double* > attr_data( attr_tag_list_len );
282 std::vector< int > attr_size( attr_tag_list_len );
283 for(
int i = 0; i < attr_tag_list_len; ++i )
285 if( !attr_tag_list[i] || attr_tag_index[i] < 0 )
292 attr_data[i] = &( tag_data[attr_tag_list[i]] )[0];
293 attr_size[i] = tag_size[attr_tag_list[i]];
298 std::vector< double* > coords;
304 nodes.reserve( num_vtx );
305 std::vector< double > data( 1 +
dim + num_attr + bdry_flag );
306 std::vector< int > ids( num_vtx );
307 for(
int i = 0; i < num_vtx; ++i )
309 rval =
read_line( file, &data[0], data.size(), lineno );
313 ids[i] = (int)data[0];
314 if( ids[i] >= (
int)nodes.size() ) nodes.resize( ids[i] + 1 );
315 nodes[ids[i]] = start_handle + i;
319 for(
int j = 0; j <
dim; ++j )
320 coords[j][i] = data[j + 1];
323 for(
int j = 0; j < attr_tag_list_len; ++j )
324 if( attr_data[j] ) attr_data[j][i * attr_size[j] + attr_tag_index[j]] = data[j + 1 +
dim];
331 node_range.
insert( start_handle, start_handle + num_vtx - 1 );
332 for( std::map<
Tag, std::vector< double > >::iterator i = tag_data.begin(); i != tag_data.end(); ++i )
347 const std::vector< EntityHandle >& nodes,
353 int node_per_elem, have_group_id,
dim;
354 double header_vals[3];
358 rval =
read_line( file, header_vals, 3, lineno );
359 node_per_elem = (int)header_vals[1];
360 have_group_id = (int)header_vals[2];
364 rval =
read_line( file, header_vals, 2, lineno );
366 have_group_id = (int)header_vals[1];
370 rval =
read_line( file, header_vals, 1, lineno );
380 const int num_elem = (int)header_vals[0];
381 if( num_elem < 1 || node_per_elem < 2 || have_group_id < 0 || have_group_id > 1 )
383 MB_SET_ERR( MB_FAILURE,
"Invalid header line for element data" );
387 std::map< double, EntityHandle > groups;
391 const int negone = -1;
400 elems.
insert( start_handle, start_handle + num_elem - 1 );
403 std::vector< double > data( 1 + node_per_elem + have_group_id );
404 std::vector< int > ids( num_elem );
405 for(
int i = 0; i < num_elem; ++i )
407 rval =
read_line( file, &data[0], data.size(), lineno );
411 ids[i] = (int)data[0];
414 for(
int j = 0; j < node_per_elem; ++j )
415 conn_array[node_per_elem * i + j] = nodes[(
int)data[j + 1]];
418 if( have_group_id && 0.0 != data[node_per_elem + 1] )
420 double id = data[node_per_elem + 1];
442 elems2.
insert( start_handle, start_handle + num_elem - 1 );