mesh_vtk_file.cpp 82.2 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1
2
3
4
5
6
#ifdef _MSC_VER //kill some warnings
#define _CRT_SECURE_NO_WARNINGS
#endif


#include "inmost.h"
Kirill Terekhov's avatar
Kirill Terekhov committed
7
#include <cfloat>
Kirill Terekhov's avatar
Kirill Terekhov committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#if defined(USE_MESH)

//vtk states

#define R_VERSION     0
#define R_USERDATA    1
#define R_DATATYPE    2
#define R_WAITDATA    3

#define R_SGRID       4
#define R_RGRID       5
#define R_UGRID       6
#define R_POLYDATA    7
#define R_FIELD       8
#define R_SPOINTS     9

#define R_ATTRIBUTES  10
#define R_ATTRDATA    11

#define R_QUIT		  100

30
//static int __isnan__(double x) { return x != x; }
31
//static int isinf(double x) { return !isnan(x) && isnan(x - x); }
32
33
//static int __isinf__(double x) { return fabs(x) > DBL_MAX; }
//static int __isbad(double x) { return __isnan__(x) || __isinf__(x); }
Kirill Terekhov's avatar
Kirill Terekhov committed
34

35
36


Kirill Terekhov's avatar
Kirill Terekhov committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
template<typename T>
void ReadCoords(FILE * f,INMOST_DATA_REAL_TYPE c[3])
{
	T temp[3];
	if( fread(temp,sizeof(T),3,f) == 3 )
	{
		c[0] = temp[0];
		c[1] = temp[1];
		c[2] = temp[2];
	}
	else 
	{
		std::cout << __FILE__ << ":" << __LINE__ << " Cannot read 3 coordinates from file " << std::endl;
		throw INMOST::BadFile;
	}
}

namespace INMOST
{
56

57
58
59
60
61
62

	void skip_empty(char * readline,FILE * f)
	{
		bool empty = true;
		do
		{
63
			for(size_t k = 0; k < strlen(readline); ++k)
64
65
66
67
68
69
70
71
72
73
74
75
76
				if( !isspace(readline[k]) ) empty = false;
			if( empty )
			{
				if( fgets(readline,2048,f) == NULL ) 
					break;
			}
		} while(empty);
		
	}

	void skip_metadata(int comps, char * readline,FILE * f)
	{
		int dcnt,filled, scnt;
77
		//char ntype[256];
78
79
80
81
82
83
84
85
86
87
88
89
90
		if( !strncmp(readline,"METADATA",8) )
		{
			for(int k = 0; k < 2; ++k)
			{
				if( fgets(readline,2048,f) == NULL ) throw BadFile;
				if( !strncmp(readline,"INFORMATION",11) )
				{
					filled = sscanf(readline,"%*s %d",&dcnt); //INFORMATION %d
					if( filled != 1 ) throw BadFile;
					for(int k = 0; k < dcnt; ++k) //2 lines per data
					{
						if( fgets(readline,2048,f) == NULL )  //NAME
							throw BadFile;
91
						filled = fscanf(f,"%*s"); //DATA
92
93
						filled = fscanf(f,"%d",&scnt); //vector size
						if( filled != 1 ) throw BadFile;
94
						for(int q = 0; q < scnt; ++q) filled = fscanf(f,"%*s\n");
95
96
97
98
99
100
101
102
103
104
105
106
107
108
					}
				}
				else if( !strncmp(readline,"COMPONENT_NAMES",15) )
				{
					for(int k = 0; k < comps; ++k) //2 lines per data
					{
						if( fgets(readline,2048,f) == NULL ) 
							throw BadFile;
					}
				}
			}
		}
	}

109
110
111
112
113
114
115
116
117
	std::string Space2Underscore(const std::string & inp)
	{
		std::string ret = inp;
		for (size_t k = 0; k < ret.size(); ++k)
		{
			if (ret[k] == ' ') ret[k] = '_';
		}
		return ret;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
118
119
120
121
122
  
	int VtkElementType(ElementType t)
	{
		switch(t)
		{
123
			case Element::Line: return 3;
Kirill Terekhov's avatar
Kirill Terekhov committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
			case Element::Tri: return 5;
			case Element::Quad: return 9;
			case Element::MultiLine: return 4;
			case Element::Polygon: return 7;
			case Element::Tet: return 10;
			case Element::Hex: return 12;
			case Element::Prism: return 13;
			case Element::Pyramid: return 14;
			case Element::Polyhedron: return 42;
			case Element::MultiPolygon: return 42;
		}
		assert(false);
		return -1;
	}
	
	INMOST_DATA_ENUM_TYPE VtkElementNodes(ElementType t)
	{
		switch(t)
		{
143
			case Element::Line: return 2;
Kirill Terekhov's avatar
Kirill Terekhov committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
			case Element::Tri: return 3;
			case Element::Quad: return 4;
			case Element::MultiLine: return ENUMUNDEF;
			case Element::Polygon: return ENUMUNDEF;
			case Element::Tet: return 4;
			case Element::Hex: return 8;
			case Element::Prism: return 6;
			case Element::Pyramid: return 5;
			case Element::Polyhedron: return ENUMUNDEF;
			case Element::MultiPolygon: return ENUMUNDEF;
		}
		assert(false);
		return ENUMUNDEF;
	}
	

  void Mesh::SaveVTK(std::string File)
  {
		integer dim = GetDimensions();
		if( dim > 3 )
		{
165
			std::cout << "VTK file supports 3 dimensions max" << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
166
167
			return;
		}
168
169
170
171
172
173
174
175
176
		std::ofstream f(File.c_str());
		//~ FILE * f = fopen(File.c_str(),"w");
		//~ if( !f ) throw BadFileName;
		if( f.fail() ) throw BadFileName;
		f << "# vtk DataFile Version 3.0\n";
		if (this->GetFileOption("VTK_OUTPUT_FACES") == "1") f << "VTK_OUTPUT_FACES file is written by INMOST\n";
		else f << "file is written by INMOST\n";
		f << "ASCII\n";
		f << "DATASET UNSTRUCTURED_GRID\n";
177
178
179
180
181
182
183
184
185

		bool output_faces = false;
		for (INMOST_DATA_ENUM_TYPE k = 0; k < file_options.size(); ++k)
		{
			if (file_options[k].first == "VTK_OUTPUT_FACES")
			{
				output_faces = true;
			}
		}
186
187
188
189
190
191
		
		std::set< std::string > nosave, saveonly;
		
		nosave = TagOptions("nosave");
		saveonly = TagOptions("saveonly");
		
Kirill Terekhov's avatar
Kirill Terekhov committed
192
		//ReorderEmpty(CELL | NODE);
193
		Tag set_id = CreateTag("TEMPORARY_ELEMENT_ID",DATA_INTEGER,CELL |FACE| NODE,NONE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
194
195
196
197
		Storage::integer cur_num = 0;
		for(Mesh::iteratorNode it = BeginNode(); it != EndNode(); ++it) it->IntegerDF(set_id) = cur_num++;
		cur_num = 0;
		for(Mesh::iteratorCell it = BeginCell(); it != EndCell(); ++it) it->IntegerDF(set_id) = cur_num++;
198
		cur_num = 0;
199
200
201

		if (output_faces) for (Mesh::iteratorFace it = BeginFace(); it != EndFace(); ++it) it->IntegerDF(set_id) = cur_num++;

202
		f << "POINTS " << NumberOfNodes() << " double\n";
Kirill Terekhov's avatar
Kirill Terekhov committed
203
204
205
206
207
208
		for(Mesh::iteratorNode it = BeginNode(); it != EndNode(); it++)
		{
			Storage::real_array coords = it->RealArray(CoordsTag());
			for(integer i = 0; i < dim; i++) 
			{
				double temp = coords[i];
209
				f << temp << " ";
Kirill Terekhov's avatar
Kirill Terekhov committed
210
211
			}
			for(integer i = dim; i < 3; i++)
212
213
				f << "0 ";
			f << "\n";
Kirill Terekhov's avatar
Kirill Terekhov committed
214
215
		}
		{
216
			std::vector<int> values;
Kirill Terekhov's avatar
Kirill Terekhov committed
217
218
219
220
221
222
			for(Mesh::iteratorCell it = BeginCell(); it != EndCell(); it++)
			{	
				switch(it->GetGeometricType())
				{
					case Element::Tri:
					case Element::Quad:
223
224
					//case Element::MultiLine:
					//case Element::Polygon:
Kirill Terekhov's avatar
Kirill Terekhov committed
225
					case Element::Tet:
226
227
228
					case Element::Hex:
					case Element::Pyramid:
					case Element::Prism:
Kirill Terekhov's avatar
Kirill Terekhov committed
229
230
					{
						ElementArray<Node> nodes = it->getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
231
						if( nodes.size() != VtkElementNodes(it->GetGeometricType()) ) goto safe_output;
Kirill Terekhov's avatar
Kirill Terekhov committed
232
233
234
235
						values.push_back(static_cast<integer>(nodes.size()));
						for(ElementArray<Node>::iterator jt = nodes.begin(); jt != nodes.end(); jt++)
							values.push_back(jt->IntegerDF(set_id));
						break;
236
237
238
239
240
241
242
243
244
					}
					case Element::MultiLine:
					case Element::Polygon:
					{
						ElementArray<Node> nodes = it->getNodes();
						values.push_back(static_cast<integer>(nodes.size()));
						for(ElementArray<Node>::iterator jt = nodes.begin(); jt != nodes.end(); jt++)
							values.push_back(jt->IntegerDF(set_id));
						break;
Kirill Terekhov's avatar
Kirill Terekhov committed
245
					}
246
						/*
Kirill Terekhov's avatar
Kirill Terekhov committed
247
248
249
250
251
252
253
254
255
256
257
258
259
					case Element::Prism:
					{
						ElementArray<Node> nodes = it->getNodes();
						if( nodes.size() != 6 ) goto safe_output;
						values.push_back(static_cast<integer>(nodes.size()));
						values.push_back(nodes[0].IntegerDF(set_id));
						values.push_back(nodes[2].IntegerDF(set_id));
						values.push_back(nodes[1].IntegerDF(set_id));
						values.push_back(nodes[3].IntegerDF(set_id));
						values.push_back(nodes[5].IntegerDF(set_id));
						values.push_back(nodes[4].IntegerDF(set_id));
						break;
					}
260
261
						 */
					/*
Kirill Terekhov's avatar
Kirill Terekhov committed
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
					case Element::Hex:
					{
						ElementArray<Node> nodes = it->getNodes();
						if( nodes.size() != 8 ) goto safe_output;
						values.push_back(static_cast<integer>(nodes.size()));
						values.push_back(nodes[0].IntegerDF(set_id));
						values.push_back(nodes[3].IntegerDF(set_id));
						values.push_back(nodes[2].IntegerDF(set_id));
						values.push_back(nodes[1].IntegerDF(set_id));
						values.push_back(nodes[4].IntegerDF(set_id));
						values.push_back(nodes[7].IntegerDF(set_id));
						values.push_back(nodes[6].IntegerDF(set_id));
						values.push_back(nodes[5].IntegerDF(set_id));
						break;
					}
					case Element::Pyramid:
					{
						ElementArray<Node> nodes = it->getNodes();
						if( nodes.size() != 5 ) goto safe_output;
						values.push_back(static_cast<integer>(nodes.size()));
						values.push_back(nodes[0].IntegerDF(set_id));
						values.push_back(nodes[3].IntegerDF(set_id));
						values.push_back(nodes[2].IntegerDF(set_id));
						values.push_back(nodes[1].IntegerDF(set_id));
						values.push_back(nodes[4].IntegerDF(set_id));
						break;
					}
289
					 */
Kirill Terekhov's avatar
Kirill Terekhov committed
290
291
292
293
					case Element::Polyhedron:
					case Element::MultiPolygon:
					{
safe_output:
294
						
Kirill Terekhov's avatar
Kirill Terekhov committed
295
						ElementArray<Face> faces = it->getFaces();
Kirill Terekhov's avatar
Kirill Terekhov committed
296
297
298
299
						integer totalNum = 1 + static_cast<integer>(faces.size());
                        for(ElementArray<Face>::iterator jt = faces.begin(); jt != faces.end(); jt++)
							totalNum += jt->nbAdjElements(NODE);
						values.push_back(totalNum);
Kirill Terekhov's avatar
Kirill Terekhov committed
300
301
302
303
304
305
306
307
308
309
310
311
312
313
						values.push_back(static_cast<integer>(faces.size()));
						for(ElementArray<Face>::iterator jt = faces.begin(); jt != faces.end(); jt++)
						{
							ElementArray<Node> nodes = jt->getNodes();
							values.push_back(static_cast<integer>(nodes.size()));
							if( jt->FaceOrientedOutside(it->self()) )
								for(ElementArray<Node>::iterator kt = nodes.begin(); kt != nodes.end(); kt++)
									values.push_back(kt->IntegerDF(set_id));
							else
								for(ElementArray<Node>::reverse_iterator kt = nodes.rbegin(); kt != nodes.rend(); kt++)
									values.push_back(kt->IntegerDF(set_id));
						}
						break;
					}
314
					default: std::cout << __FILE__ << ":" << __LINE__ << " This should not happen " << Element::GeometricTypeName(it->GetGeometricType()) << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
315
316
				}
			}
317
			if (output_faces)
318
			{
319
				for (Mesh::iteratorFace it = BeginFace(); it != EndFace(); it++)
320
				{
321
322
323
324
					switch (it->GetGeometricType())
					{
					case Element::Line:
					{
325
326
327
328
329
						ElementArray<Node> nodes = it->getNodes();
						values.push_back(static_cast<integer>(nodes.size()));
						for (ElementArray<Node>::iterator jt = nodes.begin(); jt != nodes.end(); jt++)
							values.push_back(jt->IntegerDF(set_id));
						break;
330
331
332
333
334
335
					}
					case Element::Tri:
					case Element::Quad:
					case Element::MultiLine:
					case Element::Polygon:
					{
336
337
338
339
340
						ElementArray<Node> nodes = it->getNodes();
						values.push_back(static_cast<integer>(nodes.size()));
						for (ElementArray<Node>::iterator jt = nodes.begin(); jt != nodes.end(); jt++)
							values.push_back(jt->IntegerDF(set_id));
						break;
341
					}
342
					default: std::cout << __FILE__ << ":" << __LINE__ << " This should not happen " << Element::GeometricTypeName(it->GetGeometricType()) << std::endl;
343
					}
344
				}
345
				f << "CELLS " << NumberOfCells() + NumberOfFaces() << " " << values.size() << "\n";
346
			}
347
			else
348
				f << "CELLS " << NumberOfCells() << " " << values.size() << "\n";
349

350
			for(size_t i = 0; i < values.size(); i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
351
			{
352
353
				f << values[i] << " ";
				if( (i+1) % 20 == 0) f << "\n";
Kirill Terekhov's avatar
Kirill Terekhov committed
354
			}
355
			f << "\n";
Kirill Terekhov's avatar
Kirill Terekhov committed
356
		}
357
		if (output_faces)
358
			f << "CELL_TYPES " << NumberOfCells() + NumberOfFaces() << "\n";
359
		else 			
360
			f << "CELL_TYPES " << NumberOfCells() << "\n";
361

Kirill Terekhov's avatar
Kirill Terekhov committed
362
363
364
365
		for(Mesh::iteratorCell it = BeginCell(); it != EndCell(); it++)
		{
			INMOST_DATA_ENUM_TYPE nnodes = VtkElementNodes(it->GetGeometricType());
			if( nnodes == ENUMUNDEF || nnodes == it->nbAdjElements(NODE) ) //nodes match - output correct type
366
				f << VtkElementType(it->GetGeometricType()) << "\n";
Kirill Terekhov's avatar
Kirill Terekhov committed
367
			else //number of nodes mismatch with expected - some topology checks must be off
368
				f << VtkElementType(Element::MultiPolygon) << "\n";
Kirill Terekhov's avatar
Kirill Terekhov committed
369
		}
370
371
372
373
374
		if (output_faces){
			for (Mesh::iteratorFace it = BeginFace(); it != EndFace(); it++)
			{
				INMOST_DATA_ENUM_TYPE nnodes = VtkElementNodes(it->GetGeometricType());
				if (nnodes == ENUMUNDEF || nnodes == it->nbAdjElements(NODE)) //nodes match - output correct type
375
					f << VtkElementType(it->GetGeometricType()) << "\n";
376
				else //number of nodes mismatch with expected - some topology checks must be off
377
					f << VtkElementType(Element::MultiPolygon) << "\n";
378
			}
379
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
380
381
382
383
384
385
386
387
		DeleteTag(set_id);
		{
			std::vector<std::string> tag_names;
			std::vector<Tag> tags;
			ListTagNames(tag_names);
			for(unsigned int i = 0; i < tag_names.size(); i++)
			{
				Tag t = GetTag(tag_names[i]);
388
				if (((t.isDefined(CELL) /*&& !t.isSparse(CELL)*/)
389
					|| (t.isDefined(FACE) && output_faces)) &&
390
391
392
393
394
395
396
397
						t.GetPrint() && //Temporary solution: @see Mesh::file_option
						t.GetDataType() != DATA_BULK && 
						t.GetDataType() != DATA_REFERENCE &&
						t.GetDataType() != DATA_REMOTE_REFERENCE &&
						t != CoordsTag() && 
						t != SharedTag() && 
						t != SendtoTag() && 
						t != ProcessorsTag())
Kirill Terekhov's avatar
Kirill Terekhov committed
398
				{
399
400
401
					bool skip = CheckSaveSkip(tag_names[i],nosave,saveonly);
					if( !skip )
						tags.push_back(t);
Kirill Terekhov's avatar
Kirill Terekhov committed
402
403
404
				}
			}
				
405
406
			if (!tags.empty() && output_faces) 
			{ 
407
				f << "CELL_DATA " << NumberOfCells() + NumberOfFaces() << "\n"; 
408
			}
409
			else if (!tags.empty()) f << "CELL_DATA " << NumberOfCells() << "\n";
410

411
			for(INMOST_DATA_ENUM_TYPE i = 0; i < tags.size(); i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
412
			{
413
				INMOST_DATA_ENUM_TYPE comps = tags[i].GetSize();
Kirill Terekhov's avatar
Kirill Terekhov committed
414
415
416
417
418
				if( comps == ENUMUNDEF )
					continue;
				else
				{
					{
419
420
						std::string type_str = "int";
						if(  tags[i].GetDataType() == DATA_REAL
Kirill Terekhov's avatar
Kirill Terekhov committed
421
#if defined(USE_AUTODIFF)
422
						   || tags[i].GetDataType() == DATA_VARIABLE
Kirill Terekhov's avatar
Kirill Terekhov committed
423
#endif
424
						   ) type_str = "double";
425
426
						f << "SCALARS " << Space2Underscore(tags[i].GetTagName()) << " " << type_str << " " << comps << "\n";
						f << "LOOKUP_TABLE default\n";
427
						for (Mesh::iteratorCell it = BeginCell(); it != EndCell(); it++)
Kirill Terekhov's avatar
Kirill Terekhov committed
428
						{
429
430
431
432
							switch (tags[i].GetDataType())
							{
							case DATA_REAL:
							{
433
434
435
436
437
438
								if (tags[i].isDefined(CELL) && it->HaveData(tags[i]))
								{
									Storage::real_array arr = it->RealArray(tags[i]);
									for (unsigned int m = 0; m < comps; m++)
									{
										double val = static_cast<double>(arr[m]);
439
										f << (__isbad(val) ? -0.9999E30 : val) << " ";
440
441
									}
								}
442
443
								else for (unsigned int m = 0; m < comps; m++) f << -0.9999E30 << " ";
								f << "\n";
444
							}
445
							break;
446
							case DATA_INTEGER:
Kirill Terekhov's avatar
Kirill Terekhov committed
447
							{
448
449
450
								if (tags[i].isDefined(CELL) && it->HaveData(tags[i]))
								{
									Storage::integer_array arr = it->IntegerArray(tags[i]);
451
									for (unsigned int m = 0; m < comps; m++) f << arr[m] << " ";
452
								}
453
454
								else for (unsigned int m = 0; m < comps; m++) f << INT_MIN << " ";
								f << "\n";
455
							}
456
							break;
457
458
459
#if defined(USE_AUTODIFF)
							case DATA_VARIABLE:
							{
460
461
462
463
464
465
								if (tags[i].isDefined(CELL) && it->HaveData(tags[i]))
								{
									Storage::var_array arr = it->VariableArray(tags[i]);
									for (unsigned int m = 0; m < comps; m++)
									{
										double val = static_cast<double>(arr[m].GetValue());
466
										f << (__isbad(val) ? -0.9999E30 : val) << " ";
467
468
									}
								}
469
470
								else for (unsigned int m = 0; m < comps; m++) f << -0.9999E30 << " ";
								f << "\n";
471
							}
472
							break;
473
474
475
476
477
#endif
							default: continue;
							}
						}
							
478
479


480
481
						if (output_faces)
						{
482
483
484
485
486
487
							for (Mesh::iteratorFace it = BeginFace(); it != EndFace(); it++)
							{
								switch (tags[i].GetDataType())
								{
								case DATA_REAL:
								{
488
489
490
									if (tags[i].isDefined(FACE) && it->HaveData(tags[i]))
									{
										Storage::real_array arr = it->RealArray(tags[i]);
491
										for (unsigned int m = 0; m < comps; m++) f << arr[m] << " ";
492
									}
493
494
									else for (unsigned int m = 0; m < comps; m++) f << -0.9999E30 << " ";
									f << "\n";
495
								}
496
								break;
497
498
								case DATA_INTEGER:
								{
499
500
501
									if (tags[i].isDefined(FACE) && it->HaveData(tags[i]))
									{
										Storage::integer_array arr = it->IntegerArray(tags[i]);
502
										for (unsigned int m = 0; m < comps; m++) f << arr[m] << " ";
503
									}
504
505
									else for (unsigned int m = 0; m < comps; m++) f << INT_MIN << " ";
									f << "\n";
506
								}
507
								break;
Kirill Terekhov's avatar
Kirill Terekhov committed
508
#if defined(USE_AUTODIFF)
509
510
								case DATA_VARIABLE:
								{
511
512
513
514
515
516
									if (tags[i].isDefined(FACE) && it->HaveData(tags[i]))
									{
										Storage::var_array arr = it->VariableArray(tags[i]);
										for (unsigned int m = 0; m < comps; m++)
										{
											double val = static_cast<double>(arr[m].GetValue());
517
											f << (__isbad(val) ? -0.9999E30 : val) << " ";
518
519
										}
									}
520
521
									else for (unsigned int m = 0; m < comps; m++) f << -0.9999E30 << " ";
									f << "\n";
522
								}
523
								break;
Kirill Terekhov's avatar
Kirill Terekhov committed
524
#endif
525
526
								default: continue;
								}
Kirill Terekhov's avatar
Kirill Terekhov committed
527
528
529
530
531
532
533
534
535
536
537
538
539
							}
						}
					}
				}
			}
		}
		{
			std::vector<std::string> tag_names;
			std::vector<Tag> tags;
			ListTagNames(tag_names);
			for(unsigned int i = 0; i < tag_names.size(); i++)
			{
				Tag t = GetTag(tag_names[i]);
540
541
542
543
544
545
546
547
				if( t.isDefined(NODE) && /*!t.isSparse(NODE) &&*/
				    t.GetDataType() != DATA_BULK &&
				    t.GetDataType() != DATA_REFERENCE &&
				    t.GetDataType() != DATA_REMOTE_REFERENCE &&
					t != CoordsTag() &&
				    t != SharedTag() &&
				    t != SendtoTag() &&
				    t != ProcessorsTag())
Kirill Terekhov's avatar
Kirill Terekhov committed
548
549
550
					tags.push_back(t);
			}
				
551
552
			if( !tags.empty() ) f << "POINT_DATA " << NumberOfNodes() << "\n";
			for(INMOST_DATA_ENUM_TYPE i = 0; i < tags.size(); i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
553
			{
554
				INMOST_DATA_ENUM_TYPE comps = tags[i].GetSize();
Kirill Terekhov's avatar
Kirill Terekhov committed
555
556
557
558
559
				if( comps == ENUMUNDEF )
					continue;
				else
				{
					{
560
561
						std::string type_str = "int";
						if(  tags[i].GetDataType() == DATA_REAL
Kirill Terekhov's avatar
Kirill Terekhov committed
562
#if defined(USE_AUTODIFF)
563
						   || tags[i].GetDataType() == DATA_VARIABLE
Kirill Terekhov's avatar
Kirill Terekhov committed
564
#endif
565
						   ) type_str = "double";
566
567
						f << "SCALARS " << Space2Underscore(tags[i].GetTagName()) << " " << type_str << " " << comps << "\n";
						f << "LOOKUP_TABLE default\n";
Kirill Terekhov's avatar
Kirill Terekhov committed
568
569
570
571
572
573
						for(Mesh::iteratorNode it = BeginNode(); it != EndNode(); it++)
						{
							switch( tags[i].GetDataType() )
							{
								case DATA_REAL:
								{
574
									if (it->HaveData(tags[i]))
Kirill Terekhov's avatar
Kirill Terekhov committed
575
									{
576
577
578
579
										Storage::real_array arr = it->RealArray(tags[i]);
										for(unsigned int m = 0; m < comps; m++)
										{
											double val = static_cast<double>(arr[m]);
580
											f << (__isbad(val) ? -0.9999E30 : val) << " ";
581
										}
582
										f << "\n";
Kirill Terekhov's avatar
Kirill Terekhov committed
583
									}
584
									else for(unsigned int m = 0; m < comps; m++) f << -0.9999E30 << " ";
Kirill Terekhov's avatar
Kirill Terekhov committed
585
586
587
588
								}
								break;
								case DATA_INTEGER:
								{
589
590
591
									if (it->HaveData(tags[i]))
									{
										Storage::integer_array arr = it->IntegerArray(tags[i]);
592
593
										for(unsigned int m = 0; m < comps; m++) f << arr[m] << " ";
										f << "\n";
594
									}
595
									else for (unsigned int m = 0; m < comps; m++) f << INT_MIN << " ";
Kirill Terekhov's avatar
Kirill Terekhov committed
596
597
								}
								break;
Kirill Terekhov's avatar
Kirill Terekhov committed
598
#if defined(USE_AUTODIFF)
599
								case DATA_VARIABLE:
Kirill Terekhov's avatar
Kirill Terekhov committed
600
								{
601
									if (it->HaveData(tags[i]))
Kirill Terekhov's avatar
Kirill Terekhov committed
602
									{
603
604
605
606
										Storage::var_array arr = it->VariableArray(tags[i]);
										for(unsigned int m = 0; m < comps; m++)
										{
											double val = static_cast<double>(arr[m].GetValue());
607
											f << (__isbad(val) ? -0.9999E30 : val) << " ";
608
										}
609
										f << "\n";
Kirill Terekhov's avatar
Kirill Terekhov committed
610
									}
611
									else for(unsigned int m = 0; m < comps; m++) f << -0.9999E30 << " ";
Kirill Terekhov's avatar
Kirill Terekhov committed
612
613
614
								}
								break;
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
615
616
617
618
619
620
621
622
623
								default: continue;
							}
						}
					}
				}
			}
		}
	}

624
625
626
	void Mesh::LoadVTK(std::string File)
	{
		int verbosity = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
627
628
629
630
631
632
633
		for(INMOST_DATA_ENUM_TYPE k = 0; k < file_options.size(); ++k)
		{
			if( file_options[k].first == "VERBOSITY" )
			{
				verbosity = atoi(file_options[k].second.c_str());
				if( verbosity < 0 || verbosity > 2 )
				{
634
					std::cout << __FILE__ << ":" << __LINE__ << " Unknown verbosity option: " << file_options[k].second << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
635
636
637
638
					verbosity = 1;
				}
			}
		}
639
640
641
642
643
644
		
		std::set< std::string > noload, loadonly;
		
		noload = TagOptions("noload");
		loadonly = TagOptions("loadonly");
		
Kirill Terekhov's avatar
Kirill Terekhov committed
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

		MarkerType unused_marker = CreateMarker();
		int grid_is_2d = 2;
		for(INMOST_DATA_ENUM_TYPE k = 0; k < file_options.size(); ++k)
		{
			if( file_options[k].first == "VTK_GRID_DIMS" )
			{
				if( file_options[k].second == "AUTO" )
					grid_is_2d = 2;
				if( atoi(file_options[k].second.c_str()) == 2 )
					grid_is_2d = 1;
				else if( atoi(file_options[k].second.c_str()) == 3 )
					grid_is_2d = 0;
			}
		}

		//Determine whether there are already 3d elements so that the grid is 3d
		if( grid_is_2d == 2 && NumberOfCells() )
		{
			for(Mesh::iteratorCell it = BeginCell(); it != EndCell() && grid_is_2d == 2; ++it)
				if( it->GetElementDimension() == 3 )
					grid_is_2d = 0;
		}


		std::vector<HandleType> old_nodes(NumberOfNodes());
		{
			unsigned qq = 0;
			for(Mesh::iteratorNode it = BeginNode(); it != EndNode(); ++it)
				old_nodes[qq++] = *it;
		}
		if( !old_nodes.empty() ) 
		{
			std::sort(old_nodes.begin(),old_nodes.end(),CentroidComparator(this));
			//for(std::vector<HandleType>::iterator it = old_nodes.begin(); it != old_nodes.end(); ++it)
			//{
			//	Storage::real_array c = RealArrayDF(*it,CoordsTag());
			//	REPORT_VAL("coord: ",c[0] << " " << c[1] << " " << c[2]);
			//}
		}

			
		FILE * f = fopen(File.c_str(),"r");
		if( !f ) 
		{
			std::cout << __FILE__ << ":" << __LINE__ << " cannot open " << File << std::endl;
			throw BadFileName;
		}
		std::vector<Tag> datatags;
		std::vector<HandleType> newnodes;
		std::vector<HandleType> newcells;
		std::vector<int> cp;
		std::vector<int> ct;
		unsigned int state = R_VERSION;
		char readline[2048];
		int filled;
		bool binary = false;
		int read_into = NONE, read_into_cell = NONE;
703
		bool read_next_line = true;
Kirill Terekhov's avatar
Kirill Terekhov committed
704
705
		while( state != R_QUIT )
		{
706
707
			
			if( read_next_line )
Kirill Terekhov's avatar
Kirill Terekhov committed
708
			{
709
710
711
712
713
714
				if( fgets(readline,2048,f) == NULL )
				{
					state = R_QUIT;
					continue;
				}
			} else read_next_line = true;
Kirill Terekhov's avatar
Kirill Terekhov committed
715
716
717
			//if( readline[strlen(readline)-1] == '\n' )
			//	readline[strlen(readline)-1] = '\0';
			while( strlen(readline) != 0 && isspace(readline[strlen(readline)-1]) )
Kirill Terekhov's avatar
Kirill Terekhov committed
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
				readline[strlen(readline)-1] = '\0';
			if( strlen(readline) == 0 ) continue;
			switch( state )
			{
				case R_VERSION:
				{
					int h,l;
					filled = sscanf(readline,"# vtk DataFile Version %1d.%1d",&h,&l);
					if( filled == 0 ) 
					{
						std::cout << __FILE__ << ":" << __LINE__ << " version information not found in " << File << std::endl;
						throw BadFile;
					}
					//file version checker
					state = R_USERDATA;
					break;
				}
				case R_USERDATA:
				{
737
					if (!strncmp(readline, "VTK_OUTPUT_FACES", 16)) this->SetFileOption("VTK_OUTPUT_FACES", "1");
Kirill Terekhov's avatar
Kirill Terekhov committed
738
739
740
741
742
743
					state = R_DATATYPE;
					break;
				}
				case R_DATATYPE:
				{
					if( !strncmp(readline,"BINARY",6) )
Kirill Terekhov's avatar
Kirill Terekhov committed
744
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
745
						binary = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
746
747
						state = R_WAITDATA;
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
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
					else if( !strncmp(readline,"ASCII",5) )
						state = R_WAITDATA;
					else 
					{
						std::cout << __FILE__ << ":" << __LINE__ << " unexpected data type " << readline << " in " << File << " expected BINARY or ASCII instead " << std::endl;
						throw BadFile;
					}
					break;
				}
				case R_ATTRDATA:
				{
					char dataname[1024];
					char attrname[1024];
					char attrtype[1024];
					int nentries = 1;
					filled = sscanf(readline," %s ",dataname);
					if( filled != 1 ) 
					{
						std::cout << __FILE__ << ":" << __LINE__ << " cannot read attribute name in " << File << std::endl;
						throw BadFile;
					}
					if( !strcmp(dataname,"SCALARS") )
					{
						DataType t = DATA_BULK;
						filled = sscanf(readline," SCALARS %s %s %d",attrname,attrtype,&nentries);
773
						bool skip = CheckLoadSkip(attrname,noload,loadonly);
774
						if( verbosity > 0 ) std::cout << (skip?"Skipping":"Reading") << " scalar attribute " << attrname << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
775
776
						if( filled < 2 ) 
						{
777
							std::cout << __FILE__ << ":" << __LINE__ << " found " << filled << " arguments to SCALARS field, must be >= 2\nline:\n" << readline << "\n";
Kirill Terekhov's avatar
Kirill Terekhov committed
778
779
780
781
782
783
784
785
786
787
788
							throw BadFile;
						}
						for(unsigned int i = 0; i < strlen(attrtype); i++) attrtype[i] = tolower(attrtype[i]);
						if( !strcmp(attrtype,"bit") || !strcmp(attrtype,"unsigned_char") || !strcmp(attrtype,"char") ||
							  !strcmp(attrtype,"unsigned_short") || !strcmp(attrtype,"short") || !strcmp(attrtype,"unsigned_int") ||
							  !strcmp(attrtype,"int") || !strcmp(attrtype,"unsigned_long") || !strcmp(attrtype,"long"))
							t = DATA_INTEGER;
						else if( !strcmp(attrtype,"float") || !strcmp(attrtype,"double") )
							t = DATA_REAL;
						else
						{
789
							std::cout << __FILE__ << ":" << __LINE__ << " unexpected data type " << attrtype << " in " << File << " for " << attrname << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
790
791
792
793
794
795
796
797
798
799
							std::cout << "expected one of: bit, unsigned_char, char, unsigned_short, short, unsigned_int, int, unsigned_long, long, float, double" << std::endl;
							throw BadFile;
						}
						if( fgets(readline,2048,f) == NULL ) 
						{
							std::cout << __FILE__ << ":" << __LINE__ << " expected LOOKUP_TABLE in " << File << std::endl;
							throw BadFile; //LOOK_UP TABLE
						}
						if( read_into == 2 )
						{
800
							bool sparse = (std::string(attrname).substr(0,9) != "PROTECTED");
Kirill Terekhov's avatar
Kirill Terekhov committed
801
							unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newcells.size()/250),1);
802
							if( skip )
Kirill Terekhov's avatar
Kirill Terekhov committed
803
							{
804
								for(unsigned int it = 0; it < newcells.size(); it++)
Kirill Terekhov's avatar
Kirill Terekhov committed
805
								{
806
807
808
809
810
811

									if( t == DATA_INTEGER )
										for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile;}
									if( t == DATA_REAL )
										for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; }
									if( verbosity > 1 && it%report_pace == 0)
Kirill Terekhov's avatar
Kirill Terekhov committed
812
									{
813
814
815
816
										std::ios save(NULL);
										save.copyfmt(std::cout);
										std::cout << "data " << std::setw(5) << std::fixed << std::setprecision(1) << (it*100.0)/(1.0*newcells.size()) << "%\r" << std::flush;
										std::cout.copyfmt(save);
Kirill Terekhov's avatar
Kirill Terekhov committed
817
818
									}
								}
819
820
821
822
823
							}
							else
							{
								Tag attr = CreateTag(attrname,t,read_into_cell,sparse ? read_into_cell & ~CELL : NONE,nentries);
								for(unsigned int it = 0; it < newcells.size(); it++)
Kirill Terekhov's avatar
Kirill Terekhov committed
824
								{
825
826

									if( t == DATA_INTEGER )
Kirill Terekhov's avatar
Kirill Terekhov committed
827
									{
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
										if( newcells[it] != InvalidHandle() )
										{
											Storage::integer_array attrdata = IntegerArray(newcells[it],attr);
											for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										}
										else for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile;}
									}
									if( t == DATA_REAL )
									{
										if( newcells[it] != InvalidHandle() )
										{
											Storage::real_array attrdata = RealArray(newcells[it],attr);
											for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										}
										else for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; }
									}
									if( verbosity > 1 && it%report_pace == 0)
									{
846
847
848
849
										std::ios save(NULL);
										save.copyfmt(std::cout);
										std::cout << "data " << std::setw(5) << std::fixed << std::setprecision(1) << (it*100.0)/(1.0*newcells.size()) << "%\r" << std::flush;
										std::cout.copyfmt(save);
Kirill Terekhov's avatar
Kirill Terekhov committed
850
851
									}
								}
852
								datatags.push_back(attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
853
854
855
856
857
							}
						}
						if( read_into == 1 )
						{
							unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newnodes.size())/250,1);
858
							if( skip )
Kirill Terekhov's avatar
Kirill Terekhov committed
859
							{
860
								for(unsigned int it = 0; it < newnodes.size(); it++)
Kirill Terekhov's avatar
Kirill Terekhov committed
861
								{
862
863
864
865
866
867
									if( t == DATA_INTEGER )
										for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile;}
									if( t == DATA_REAL )
										for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile;}
									if( verbosity > 1 && it%report_pace == 0)
									{
868
869
870
871
										std::ios save(NULL);
										save.copyfmt(std::cout);
										std::cout << "data " << std::setw(5) << std::fixed << std::setprecision(1) << (it*100.0)/(1.0*newnodes.size()) << "%\r" << std::flush;
										std::cout.copyfmt(save);
872
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
873
								}
874
875
876
877
878
							}
							else
							{
								Tag attr = CreateTag(attrname,t,NODE,NONE,nentries);
								for(unsigned int it = 0; it < newnodes.size(); it++)
Kirill Terekhov's avatar
Kirill Terekhov committed
879
								{
880
881
882
883
884
885
886
887
888
889
890
891
									if( t == DATA_INTEGER )
									{
										Storage::integer_array attrdata = IntegerArray(newnodes[it],attr);
										for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									}
									if( t == DATA_REAL )
									{
										Storage::real_array attrdata = RealArray(newnodes[it],attr);
										for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									}
									if( verbosity > 1 && it%report_pace == 0)
									{
892
893
894
895
										std::ios save(NULL);
										save.copyfmt(std::cout);
										std::cout << "data " << std::setw(5) << std::fixed << std::setprecision(1) << (it*100.0)/(1.0*newnodes.size()) << "%\r" << std::flush;
										std::cout.copyfmt(save);
896
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
897
898
								}
							}
899
							//filled = fscanf(f,"\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
900
						}
901
902
903
904
905
906
						if( fgets(readline,2048,f) == NULL ) throw BadFile;
						skip_empty(readline,f);
						skip_metadata(3,readline,f);
						skip_empty(readline,f);
						read_next_line = false;
						
Kirill Terekhov's avatar
Kirill Terekhov committed
907
908
909
910
911
912
						break;
					}
					else if( !strcmp(dataname,"COLOR_SCALARS") )
					{	
						char attrname[1024];
						filled = sscanf(readline," COLOR_SCALARS %s %d",attrname,&nentries);
913
						bool skip = CheckLoadSkip(attrname,noload,loadonly);
914
						if( verbosity > 0 ) std::cout << (skip?"Skipping":"Reading") << " color attribute " << attrname << "\n";
Kirill Terekhov's avatar
Kirill Terekhov committed
915
916
						if( read_into == 2 )
						{
917
							bool sparse = (std::string(attrname).substr(0,9) != "PROTECTED");
Kirill Terekhov's avatar
Kirill Terekhov committed
918
							unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newcells.size()/250),1);
919
							if( skip )
Kirill Terekhov's avatar
Kirill Terekhov committed
920
							{
921
								for(unsigned int it = 0; it < newcells.size(); it++)
Kirill Terekhov's avatar
Kirill Terekhov committed
922
								{
923
									for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile;}
924
									if( verbosity > 1 && it%report_pace == 0)
925
									{
926
927
928
929
										std::ios save(NULL);
										save.copyfmt(std::cout);
										std::cout << "data " << std::setw(5) << std::fixed << std::setprecision(1) << (it*100.0)/(1.0*newcells.size()) << "%\r" << std::flush;
										std::cout.copyfmt(save);
930
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
931
								}
932
933
934
935
936
							}
							else
							{
								Tag attr = CreateTag(attrname,DATA_REAL,read_into_cell,sparse ? read_into_cell & ~CELL : NONE,nentries);
								for(unsigned int it = 0; it < newcells.size(); it++)
Kirill Terekhov's avatar
Kirill Terekhov committed
937
								{
938
939
940
941
942
943
									if( newcells[it] != InvalidHandle() )
									{
										Storage::real_array attrdata = RealArray(newcells[it],attr);
										for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									}
									else for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile;}
944
									if( verbosity > 1 && it%report_pace == 0)
945
									{
946
947
948
949
										std::ios save(NULL);
										save.copyfmt(std::cout);
										std::cout << "data " << std::setw(5) << std::fixed << std::setprecision(1) << (it*100.0)/(1.0*newcells.size()) << "%\r" << std::flush;
										std::cout.copyfmt(save);
950
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
951
								}
952
								datatags.push_back(attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
953
954
955
956
957
							}
						}
						if( read_into == 1 )
						{
							unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newnodes.size()/250),1);
958
							if( skip ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
959
							{
960
								for(unsigned int it = 0; it < newnodes.size(); it++)
Kirill Terekhov's avatar
Kirill Terekhov committed
961
								{
962
963
964
									for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile;}
									if( verbosity > 1 && it%report_pace == 0)
									{
965
966
967
968
										std::ios save(NULL);
										save.copyfmt(std::cout);
										std::cout << "data " << std::setw(5) << std::fixed << std::setprecision(1) << (it*100.0)/(1.0*newnodes.size()) << "%\r" << std::flush;
										std::cout.copyfmt(save);
969
970
971
972
973
974
975
976
977
978
979
980
									}
								}
							}
							else
							{
								Tag attr = CreateTag(attrname,DATA_REAL,NODE,NONE,nentries);
								for(unsigned int it = 0; it < newnodes.size(); it++)
								{
									Storage::real_array attrdata = RealArray(newnodes[it],attr);
									for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									if( verbosity > 1 && it%report_pace == 0)
									{
981
982
983
984
										std::ios save(NULL);
										save.copyfmt(std::cout);
										std::cout << "data " << std::setw(5) << std::fixed << std::setprecision(1) << (it*100.0)/(1.0*newnodes.size()) << "%\r" << std::flush;
										std::cout.copyfmt(save);
985
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
986
987
								}
							}
988
							//filled = fscanf(f,"\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
989
						}
990
991
992
993
994
995
						if( fgets(readline,2048,f) == NULL ) throw BadFile;
						skip_empty(readline,f);
						skip_metadata(3,readline,f);
						skip_empty(readline,f);
						read_next_line = false;
					
Kirill Terekhov's avatar
Kirill Terekhov committed
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
						break;
					}
					else if( !strcmp(dataname," LOOKUP_TABLE") )
					{
						int nentries;
						filled = sscanf(readline," LOOKUP_TABLE %*s %d",&nentries);
						if( filled != 1 ) throw BadFile;
						for(int i = 0; i < nentries; i++)
						{
							filled = fscanf(f," %*f %*f %*f %*f");
						}
						filled = fscanf(f,"\n");
						break;
					}
					else if( !strcmp(dataname,"NORMALS") || !strcmp(dataname,"VECTORS") || !strcmp(dataname,"TENSORS"))
					{
						char attrname[1024];
						char attrtype[1024];
						int nentries = 3;
						if( !strcmp(dataname,"TENSORS") ) nentries = 9;
						DataType t = DATA_BULK;
						filled = sscanf(readline,"%*s %s %s",attrname,attrtype);
1018
						bool skip = CheckLoadSkip(attrname,noload,loadonly);
1019
						if( verbosity > 0 ) std::cout << (skip? "Skipping":"Reading") << " " << dataname << " attribute " << attrname << "n";
Kirill Terekhov's avatar
Kirill Terekhov committed
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
						if( filled != 2 ) throw BadFile;
						for(unsigned int i = 0; i < strlen(attrtype); i++) attrtype[i] = tolower(attrtype[i]);
						if( !strcmp(attrtype,"bit") || !strcmp(attrtype,"unsigned_char") || !strcmp(attrtype,"char") ||
							  !strcmp(attrtype,"unsigned_short") || !strcmp(attrtype,"short") || !strcmp(attrtype,"unsigned_int") ||
							  !strcmp(attrtype,"int") || !strcmp(attrtype,"unsigned_long") || !strcmp(attrtype,"long"))
							t = DATA_INTEGER;
						else if( !strcmp(attrtype,"float") || !strcmp(attrtype,"double") )
							t = DATA_REAL;
						else
							throw BadFile;
						if( fgets(readline,2048,f) == NULL ) throw BadFile; //LOOK_UP TABLE
						if( read_into == 2 )
						{
1033
							bool sparse = (std::string(attrname).substr(0,9) != "PROTECTED");
Kirill Terekhov's avatar
Kirill Terekhov committed
1034
							unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newcells.size()/250),1);
1035
							if( skip )
Kirill Terekhov's avatar
Kirill Terekhov committed
1036
							{
1037
								for(unsigned int it = 0; it < newcells.size(); it++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1038
								{
1039
1040
1041
1042
1043
									if( t == DATA_INTEGER )
										for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile;}
									if( t == DATA_REAL )
										for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile;}
									if( verbosity > 1 && it%report_pace == 0)
Kirill Terekhov's avatar
Kirill Terekhov committed
1044
									{
1045
1046
1047
1048
										std::ios save(NULL);
										save.copyfmt(std::cout);
										std::cout << "data " << std::setw(5) << std::fixed << std::setprecision(1) << (it*100.0)/(1.0*newcells.size()) << "%\r" << std::flush;
										std::cout.copyfmt(save);
Kirill Terekhov's avatar
Kirill Terekhov committed
1049
1050
									}
								}
1051
1052
1053
1054
1055
							}
							else
							{
								Tag attr = CreateTag(attrname,t,read_into_cell,sparse ? read_into_cell & ~CELL : NONE,nentries);
								for(unsigned int it = 0; it < newcells.size(); it++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1056
								{
1057
									if( t == DATA_INTEGER )
Kirill Terekhov's avatar
Kirill Terekhov committed
1058
									{
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
										if( newcells[it] != InvalidHandle() )
										{
											Storage::integer_array attrdata = IntegerArray(newcells[it],attr);
											for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										}
										else for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile;}
									}
									if( t == DATA_REAL )
									{
										if( newcells[it] != InvalidHandle() )
										{
											Storage::real_array attrdata = RealArray(newcells[it],attr);
											for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										}
										else for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile;}
									}
									if( verbosity > 1 && it%report_pace == 0)
									{
1077
1078
1079
1080
										std::ios save(NULL);
										save.copyfmt(std::cout);
										std::cout << "data " << std::setw(5) << std::fixed << std::setprecision(1) << (it*100.0)/(1.0*newcells.size()) << "%\r" << std::flush;
										std::cout.copyfmt(save);
Kirill Terekhov's avatar
Kirill Terekhov committed
1081
1082
									}
								}
1083
								datatags.push_back(attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1084
1085
1086
1087
1088
							}
						}
						if( read_into == 1 )
						{
							unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newnodes.size()/250),1);
1089
							if( skip )
Kirill Terekhov's avatar
Kirill Terekhov committed
1090
							{
1091
								for(unsigned int it = 0; it < newnodes.size(); it++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1092
								{
1093
1094
1095
1096
1097
1098
									if( t == DATA_INTEGER )
										for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; }
									if( t == DATA_REAL )
										for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; }
									if( verbosity > 1 && it%report_pace == 0)
									{
1099
1100
1101
1102
										std::ios save(NULL);
										save.copyfmt(std::cout);
										std::cout << "data " << std::setw(5) << std::fixed << std::setprecision(1) << (it*100.0)/(1.0*newnodes.size()) << "%\r" << std::flush;
										std::cout.copyfmt(save);
1103
									}