mesh_vtk_file.cpp 66.7 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

tgaripov's avatar
tgaripov committed
30
static int __isnan__(double x) { return x != x; }
31
//static int isinf(double x) { return !isnan(x) && isnan(x - x); }
tgaripov's avatar
tgaripov committed
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

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
{
54
55
56
57
58
59
60
61
62
63

	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
64
65
66
67
68
  
	int VtkElementType(ElementType t)
	{
		switch(t)
		{
69
			case Element::Line: return 3;
Kirill Terekhov's avatar
Kirill Terekhov committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
			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)
		{
89
			case Element::Line: return 2;
Kirill Terekhov's avatar
Kirill Terekhov committed
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
			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 )
		{
			printf("VTK file supports 3 dimensions max\n");
			return;
		}
		FILE * f = fopen(File.c_str(),"w");
		if( !f ) throw BadFileName;
		fprintf(f,"# vtk DataFile Version 3.0\n");
117
118
		if (this->GetFileOption("VTK_OUTPUT_FACES") == "1") fprintf(f, "VTK_OUTPUT_FACES file is written by INMOST\n");
		else fprintf(f,"file is written by INMOST\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
119
120
		fprintf(f,"ASCII\n");
		fprintf(f,"DATASET UNSTRUCTURED_GRID\n");
121
122
123
124
125
126
127
128
129
130

		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;
			}
		}

Kirill Terekhov's avatar
Kirill Terekhov committed
131
		//ReorderEmpty(CELL | NODE);
132
		Tag set_id = CreateTag("TEMPORARY_ELEMENT_ID",DATA_INTEGER,CELL |FACE| NODE,NONE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
133
134
135
136
		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++;
137
		cur_num = 0;
138
139
140

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

141
		fprintf(f, "POINTS %u double\n", NumberOfNodes());
Kirill Terekhov's avatar
Kirill Terekhov committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
		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];
				fprintf(f,"%.10f ",temp);
			}
			for(integer i = dim; i < 3; i++)
				fprintf(f,"0 ");
			fprintf(f,"\n");
		}
		{
			dynarray<int,64> values;
			for(Mesh::iteratorCell it = BeginCell(); it != EndCell(); it++)
			{	
				switch(it->GetGeometricType())
				{
					case Element::Tri:
					case Element::Quad:
					case Element::MultiLine:
					case Element::Polygon:
					case Element::Tet:
165
166
167
					case Element::Hex:
					case Element::Pyramid:
					case Element::Prism:
Kirill Terekhov's avatar
Kirill Terekhov committed
168
169
					{
						ElementArray<Node> nodes = it->getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
170
						if( nodes.size() != VtkElementNodes(it->GetGeometricType()) ) goto safe_output;
Kirill Terekhov's avatar
Kirill Terekhov committed
171
172
173
174
175
						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;
					}
176
						/*
Kirill Terekhov's avatar
Kirill Terekhov committed
177
178
179
180
181
182
183
184
185
186
187
188
189
					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;
					}
190
191
						 */
					/*
Kirill Terekhov's avatar
Kirill Terekhov committed
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
					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;
					}
219
					 */
Kirill Terekhov's avatar
Kirill Terekhov committed
220
221
222
223
224
225
					case Element::Polyhedron:
					case Element::MultiPolygon:
					{
safe_output:
						//printf("polyhedron!!!\n");
						ElementArray<Face> faces = it->getFaces();
Kirill Terekhov's avatar
Kirill Terekhov committed
226
227
228
229
						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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
						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;
					}
					default: printf("This should not happen %s\n",Element::GeometricTypeName(it->GetGeometricType()));
				}
			}
247
			if (output_faces)
248
			{
249
				for (Mesh::iteratorFace it = BeginFace(); it != EndFace(); it++)
250
				{
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
					switch (it->GetGeometricType())
					{
					case Element::Line:
					{
										  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;
					}
					case Element::Tri:
					case Element::Quad:
					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;
					}
					default: printf("This should not happen %s\n", Element::GeometricTypeName(it->GetGeometricType()));
					}
274
				}
275

276
				fprintf(f, "CELLS %u %d\n", NumberOfCells() + NumberOfFaces(), (int)values.size());
277
			}
278
			else
279
				fprintf(f, "CELLS %u %d\n", NumberOfCells(), (int)values.size());
280

Kirill Terekhov's avatar
Kirill Terekhov committed
281
282
283
284
285
286
287
			for(dynarray<Storage::integer,64>::size_type i = 0; i < values.size(); i++)
			{
				fprintf(f,"%d ",values[i]);
				if( (i+1) % 20 == 0) fprintf(f,"\n");
			}
			fprintf(f,"\n");
		}
288
289
290
291
292
293
		if (output_faces){
			fprintf(f, "CELL_TYPES %u\n", NumberOfCells() + NumberOfFaces());
		}
		else 			
			fprintf(f, "CELL_TYPES %u\n", NumberOfCells());

Kirill Terekhov's avatar
Kirill Terekhov committed
294
295
296
297
298
299
300
301
		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
				fprintf(f,"%d\n",VtkElementType(it->GetGeometricType()));
			else //number of nodes mismatch with expected - some topology checks must be off
				fprintf(f,"%d\n",VtkElementType(Element::MultiPolygon));
		}
302
303
304
305
306
307
308
309
310
		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
					fprintf(f, "%d\n", VtkElementType(it->GetGeometricType()));
				else //number of nodes mismatch with expected - some topology checks must be off
					fprintf(f, "%d\n", VtkElementType(Element::MultiPolygon));
			}
311
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
312
313
314
315
316
317
318
319
320
		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]);
				//printf("%s %d %d %d\n",tag_names[i].c_str(),t.isDefined(CELL),!t.isSparse(CELL),t.GetDataType() != DATA_BULK);
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
321
				if (((t.isDefined(CELL) && !t.isSparse(CELL))
322
					|| (t.isDefined(FACE) && output_faces)) &&
323
324
325
326
327
328
329
            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
330
331
332
333
334
335
				{
					//printf("added!\n");
					tags.push_back(t);
				}
			}
				
336
337
338
339
340
341
			if (!tags.empty() && output_faces) 
			{ 
				fprintf(f, "CELL_DATA %u\n", NumberOfCells() + NumberOfFaces()); 
			}
			else if (!tags.empty())fprintf(f, "CELL_DATA %u\n", NumberOfCells());

Kirill Terekhov's avatar
Kirill Terekhov committed
342
343
344
345
346
347
348
349
350
351
352
			for(unsigned int i = 0; i < tags.size(); i++)
			{
				unsigned int comps = tags[i].GetSize();
				if( comps == ENUMUNDEF )
				{
					//printf("Warning: vtk don't support arrays of variable size (tag name: %s)\n",tags[i].GetTagName().c_str());
					continue;
				}
				else
				{
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
353
354
355
356
357
358
            std::string type_str = "int";
            if(  tags[i].GetDataType() == DATA_REAL
#if defined(USE_AUTODIFF)
              || tags[i].GetDataType() == DATA_VARIABLE
#endif
              ) type_str = "double";
359
						fprintf(f,"SCALARS %s %s %d\n",Space2Underscore(tags[i].GetTagName()).c_str(),type_str.c_str(),comps);
Kirill Terekhov's avatar
Kirill Terekhov committed
360
						fprintf(f,"LOOKUP_TABLE default\n");
361
						for (Mesh::iteratorCell it = BeginCell(); it != EndCell(); it++)
Kirill Terekhov's avatar
Kirill Terekhov committed
362
						{
363
364
365
366
367
368
369
370
371
372
							switch (tags[i].GetDataType())
							{
							case DATA_REAL:
							{
											  if (tags[i].isDefined(CELL))
											  {
												  Storage::real_array arr = it->RealArray(tags[i]);
												  for (unsigned int m = 0; m < comps; m++)
												  {
													  double val = static_cast<double>(arr[m]);
tgaripov's avatar
tgaripov committed
373
													  fprintf(f, "%14e ", __isbad(val) ? -0.9999E30 : val);
374
375
												  }
											  }
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
376
											  else for (unsigned int m = 0; m < comps; m++) fprintf(f, "%14e ", -0.9999E30);
377
378
379
380
											  fprintf(f, "\n");
							}
								break;
							case DATA_INTEGER:
Kirill Terekhov's avatar
Kirill Terekhov committed
381
							{
382
383
384
385
386
												 if (tags[i].isDefined(CELL))
												 {
													 Storage::integer_array arr = it->IntegerArray(tags[i]);
													 for (unsigned int m = 0; m < comps; m++) fprintf(f, "%d ", arr[m]);
												 }
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
387
												 else for (unsigned int m = 0; m < comps; m++) fprintf(f, "%d ",INT_MIN);
388
389
												 fprintf(f, "\n");
							}
Kirill Terekhov's avatar
Kirill Terekhov committed
390
								break;
391
392
393
394
395
396
397
398
399
#if defined(USE_AUTODIFF)
							case DATA_VARIABLE:
							{
												  if (tags[i].isDefined(CELL))
												  {
													  Storage::var_array arr = it->VariableArray(tags[i]);
													  for (unsigned int m = 0; m < comps; m++)
													  {
														  double val = static_cast<double>(arr[m].GetValue());
tgaripov's avatar
tgaripov committed
400
														  fprintf(f, "%14e ", __isbad(val) ? -0.9999E30 : val);
401
402
													  }
												  }
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
403
												  else for (unsigned int m = 0; m < comps; m++) fprintf(f, "%14e ", -0.9999E30);
404
405
												  fprintf(f, "\n");
							}
Kirill Terekhov's avatar
Kirill Terekhov committed
406
								break;
407
408
409
410
411
#endif
							default: continue;
							}
						}
							
412
413
414
415
416
417
418
419
420
421
422
423
424
425


						if (output_faces){
							for (Mesh::iteratorFace it = BeginFace(); it != EndFace(); it++)
							{
								switch (tags[i].GetDataType())
								{
								case DATA_REAL:
								{
												  if (tags[i].isDefined(FACE))
												  {
													  Storage::real_array arr = it->RealArray(tags[i]);
													  for (unsigned int m = 0; m < comps; m++) fprintf(f, "%14e ", arr[m]);
												  }
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
426
												  else for (unsigned int m = 0; m < comps; m++) fprintf(f, "%14e ", -0.9999E30);
427
428
429
430
431
432
433
434
435
436
												  fprintf(f, "\n");
								}
									break;
								case DATA_INTEGER:
								{
													 if (tags[i].isDefined(FACE))
													 {
														 Storage::integer_array arr = it->IntegerArray(tags[i]);
														 for (unsigned int m = 0; m < comps; m++) fprintf(f, "%d ", arr[m]);
													 }
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
437
													 else for (unsigned int m = 0; m < comps; m++) fprintf(f, "%d ",INT_MIN);
438
439
440
													 fprintf(f, "\n");
								}
									break;
Kirill Terekhov's avatar
Kirill Terekhov committed
441
#if defined(USE_AUTODIFF)
442
443
444
445
446
447
448
449
								case DATA_VARIABLE:
								{
													  if (tags[i].isDefined(FACE))
													  {
														  Storage::var_array arr = it->VariableArray(tags[i]);
														  for (unsigned int m = 0; m < comps; m++)
														  {
															  double val = static_cast<double>(arr[m].GetValue());
tgaripov's avatar
tgaripov committed
450
															  fprintf(f, "%14e ", __isbad(val) ? -0.9999E30 : val);
451
452
														  }
													  }
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
453
													  else for (unsigned int m = 0; m < comps; m++) fprintf(f, "%14e ", -0.9999E30);
454
455
456
													  fprintf(f, "\n");
								}
									break;
Kirill Terekhov's avatar
Kirill Terekhov committed
457
#endif
458
459
								default: continue;
								}
Kirill Terekhov's avatar
Kirill Terekhov committed
460
461
462
463
464
465
466
467
468
469
470
471
472
							}
						}
					}
				}
			}
		}
		{
			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]);
473
474
475
476
477
478
479
480
481
				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
482
483
484
485
486
487
488
489
490
					tags.push_back(t);
			}
				
			if( !tags.empty() ) fprintf(f,"POINT_DATA %u\n",NumberOfNodes());
			for(unsigned int i = 0; i < tags.size(); i++)
			{
				unsigned int comps = tags[i].GetSize();
				if( comps == ENUMUNDEF )
				{
491
					//printf("Warning: vtk don't support arrays of variable size (tag name: %s)\n",tags[i].\().c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
492
493
494
495
496
					continue;
				}
				else
				{
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
497
498
499
500
501
502
            std::string type_str = "int";
            if(  tags[i].GetDataType() == DATA_REAL
#if defined(USE_AUTODIFF)
              || tags[i].GetDataType() == DATA_VARIABLE
#endif
              ) type_str = "double";
503
						fprintf(f,"SCALARS %s %s %d\n",Space2Underscore(tags[i].GetTagName()).c_str(),type_str.c_str(),comps);
Kirill Terekhov's avatar
Kirill Terekhov committed
504
505
506
507
508
509
510
511
						fprintf(f,"LOOKUP_TABLE default\n");
						for(Mesh::iteratorNode it = BeginNode(); it != EndNode(); it++)
						{
							switch( tags[i].GetDataType() )
							{
								case DATA_REAL:
								{
									Storage::real_array arr = it->RealArray(tags[i]);
Kirill Terekhov's avatar
Kirill Terekhov committed
512
513
514
									for(unsigned int m = 0; m < comps; m++) 
									{
										double val = static_cast<double>(arr[m]);
tgaripov's avatar
tgaripov committed
515
										fprintf(f,"%14e ",(__isbad(val) ? -0.9999E30 : val));
Kirill Terekhov's avatar
Kirill Terekhov committed
516
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
517
518
519
520
521
522
523
524
525
526
									fprintf(f,"\n");
								}
								break;
								case DATA_INTEGER:
								{
									Storage::integer_array arr = it->IntegerArray(tags[i]);
									for(unsigned int m = 0; m < comps; m++) fprintf(f,"%d ",arr[m]);
									fprintf(f,"\n");
								}
								break;
Kirill Terekhov's avatar
Kirill Terekhov committed
527
528
529
530
#if defined(USE_AUTODIFF)
                case DATA_VARIABLE:
								{
									Storage::var_array arr = it->VariableArray(tags[i]);
Kirill Terekhov's avatar
Kirill Terekhov committed
531
532
533
									for(unsigned int m = 0; m < comps; m++) 
									{
										double val = static_cast<double>(arr[m].GetValue());
tgaripov's avatar
tgaripov committed
534
										fprintf(f,"%14e ",(__isbad(val) ? -0.9999E30 : val));
Kirill Terekhov's avatar
Kirill Terekhov committed
535
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
536
537
538
539
									fprintf(f,"\n");
								}
								break;
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
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
								default: continue;
							}
						}
					}
				}
			}
		}
		fclose(f);	
	}

  void Mesh::LoadVTK(std::string File)
  {
    int verbosity = 0;
		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 )
				{
					printf("%s:%d Unknown verbosity option: %s\n",__FILE__,__LINE__,file_options[k].second.c_str());
					verbosity = 1;
				}
			}
		}

		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;
		while( state != R_QUIT )
		{
				
			if( fgets(readline,2048,f) == NULL )
			{
				state = R_QUIT;
				continue;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
631
632
633
			//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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
				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:
				{
					//printf("Attached info: %s\n",readline);
654
					if (!strncmp(readline, "VTK_OUTPUT_FACES", 16)) this->SetFileOption("VTK_OUTPUT_FACES", "1");
Kirill Terekhov's avatar
Kirill Terekhov committed
655
656
657
658
659
660
					state = R_DATATYPE;
					break;
				}
				case R_DATATYPE:
				{
					if( !strncmp(readline,"BINARY",6) )
Kirill Terekhov's avatar
Kirill Terekhov committed
661
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
662
						binary = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
663
664
						state = R_WAITDATA;
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
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
					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);
						if( verbosity > 0 ) printf("Reading attribute %s.\n",attrname);
						if( filled < 2 ) 
						{
							printf("%s:%d found %d arguments to SCALARS field, must be >= 2\nline:\n%s\n",__FILE__,__LINE__,filled,readline); 
							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
						{
705
							std::cout << __FILE__ << ":" << __LINE__ << " unexpected data type " << attrtype << " in " << File << " for " << attrname << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
							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 )
						{
							Tag attr = CreateTag(attrname,t,read_into_cell,read_into_cell & ESET,nentries);
							unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newcells.size()/250),1);
							for(unsigned int it = 0; it < newcells.size(); it++)
							{

								if( t == DATA_INTEGER )
								{
									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)
								{
									printf("data %3.1f%%\r",(it*100.0)/(1.0*newcells.size()));
									fflush(stdout);
								}
							}
							filled = fscanf(f,"\n");
							datatags.push_back(attr);
						}
						if( read_into == 1 )
						{
							Tag attr = CreateTag(attrname,t,NODE,NONE,nentries);
							unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newnodes.size())/250,1);
							for(unsigned int it = 0; it < newnodes.size(); it++)
							{
								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)
								{
									printf("data %3.1f%%\r",(it*100.0)/(1.0*newnodes.size()));
									fflush(stdout);
								}
							}
							filled = fscanf(f,"\n");
						}
						break;
					}
					else if( !strcmp(dataname,"COLOR_SCALARS") )
					{	
						char attrname[1024];
						filled = sscanf(readline," COLOR_SCALARS %s %d",attrname,&nentries);
						if( verbosity > 0 ) printf("Reading attribute %s.\n",attrname);
						if( read_into == 2 )
						{
							Tag attr = CreateTag(attrname,DATA_REAL,read_into_cell,read_into_cell & ESET,nentries);
							unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newcells.size()/250),1);
							for(unsigned int it = 0; it < newcells.size(); it++)
							{
								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%100 == 0)
								{
									printf("data %3.1f%%\r",(it*report_pace)/(1.0*newcells.size()));
									fflush(stdout);
								}
							}
							filled = fscanf(f,"\n");
							datatags.push_back(attr);
						}
						if( read_into == 1 )
						{
							Tag attr = CreateTag(attrname,DATA_REAL,NODE,NONE,nentries);
							unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newnodes.size()/250),1);
							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)
								{
									printf("data %3.1f%%\r",(it*100.0)/(1.0*newnodes.size()));
									fflush(stdout);
								}
							}
							filled = fscanf(f,"\n");
						}
						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);
						if( verbosity > 0 ) printf("Reading attribute %s.\n",attrname);
						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 )
						{
							Tag attr = CreateTag(attrname,t,read_into_cell,read_into_cell & ESET,nentries);
							unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newcells.size()/250),1);
							for(unsigned int it = 0; it < newcells.size(); it++)
							{
								if( t == DATA_INTEGER )
								{
									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)
								{
									printf("data %3.1f%%\r",(it*100.0)/(1.0*newcells.size()));
									fflush(stdout);
								}
							}
							filled = fscanf(f,"\n");
							datatags.push_back(attr);
						}
						if( read_into == 1 )
						{
							Tag attr = CreateTag(attrname,t,NODE,NONE,nentries);
							unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newnodes.size()/250),1);
							for(unsigned int it = 0; it < newnodes.size(); it++)
							{
								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)
								{
									printf("data %3.1f%%\r",(it*100.0)/(1.0*newnodes.size()));
									fflush(stdout);
								}
							}
							filled = fscanf(f,"\n");
						}
						break;
					}
					else if( !strcmp(dataname,"TEXTURE_COORDINATES") )
					{
						char attrname[1024];
						char attrtype[1024];
						int nentries;
						DataType t = DATA_BULK;
						filled = sscanf(readline," TEXTURE_COORDINATES %s %d %s",attrname,&nentries,attrtype);
						if( filled != 3 ) 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 )
						{
							Tag attr = CreateTag(attrname,t,read_into_cell,read_into_cell & ESET,nentries);
							unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newcells.size()/250),1);
							for(unsigned int it = 0; it < newcells.size(); it++)
							{
								if( t == DATA_INTEGER )
								{
									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)
								{
									printf("data %3.1f%%\r",(it*100.0)/(1.0*newcells.size()));
									fflush(stdout);
								}
							}
							filled = fscanf(f,"\n");
							datatags.push_back(attr);
						}
						if( read_into == 1 )
						{
							Tag attr = CreateTag(attrname,t,NODE,NONE,nentries);
							unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newnodes.size()/250),1);
							for(unsigned int it = 0; it < newnodes.size(); it++)
							{
								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)
								{
									printf("data %3.1f%%\r",(it*100.0)/(1.0*newnodes.size()));
									fflush(stdout);
								}
							}
							filled = fscanf(f,"\n");
						}
						break;
					}
					else if( !strcmp(dataname,"FIELD") )
					{
						int nfields = 0;
						filled = sscanf(readline," FIELD %*s %d",&nfields);
						if( filled != 1 ) throw BadFile;
						for(int i = 0; i < nfields; i++)
						{
							char attrname[1024];
							char attrtype[1024];
							unsigned int nentries, ntuples;
							DataType t = DATA_BULK;
							filled = fscanf(f," %s %u %u %s\n",attrname,&nentries,&ntuples,attrtype);
							if( filled != 4 ) 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( read_into == 2 )
							{
								Tag attr  = CreateTag(attrname,t,read_into_cell,read_into_cell & ESET,nentries);
								if( ntuples != newcells.size() ) printf("number of tuples in field is not equal to number of cells\n");
								unsigned report_pace = std::max<unsigned>(ntuples/250,1);
								for(unsigned int it = 0; it < ntuples; it++)
								{
									if( t == DATA_INTEGER )
									{
										if( newcells[it] != InvalidHandle() )
										{
											Storage::integer_array attrdata = IntegerArray(newcells[it],attr);
											for(unsigned int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										} else for(unsigned 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(unsigned int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										} else for(unsigned 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)
									{
										printf("data %3.1f%%\r",(it*100.0)/(1.0*ntuples));
										fflush(stdout);
									}
								}
								filled = fscanf(f,"\n");
								datatags.push_back(attr);
							}
							if( read_into == 1 )
							{
								Tag attr = CreateTag(attrname,t,NODE,NONE,nentries);
								if( ntuples != newnodes.size() ) printf("number of tuples in field is not equal to number of nodes\n");
								unsigned report_pace = std::max<unsigned>(ntuples/250,1);
								for(unsigned int it = 0; it < ntuples; it++)
								{
									if( t == DATA_INTEGER )
									{
										Storage::integer_array attrdata = IntegerArray(newnodes[it],attr);
										for(unsigned 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(unsigned 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)
									{
										printf("data %3.1f%%\r",(it*100.0)/(1.0*ntuples));
										fflush(stdout);
									}
								}
								filled = fscanf(f,"\n");
							}
						}
						break;
					}
					else
					{
						state = R_ATTRIBUTES;
					}
				}
				case R_ATTRIBUTES:
				{
					char datatype[1024];
					unsigned int npoints;
					filled = sscanf(readline," %s %u",datatype,&npoints);
					if( filled != 2 ) throw BadFile;
					if( !strcmp(datatype,"CELL_DATA") )
					{
						if( npoints != newcells.size() ) printf("number of attributes is not equal to number of cells\n");
						state = R_ATTRDATA;
						read_into = 2;
						if( verbosity > 0 ) printf("Reading data for cells.\n");
						break;
					}
					else if( !strcmp(datatype,"POINT_DATA") )
					{
						if( npoints != newnodes.size() ) printf("number of attributes is not equal to number of nodes\n");
						state = R_ATTRDATA;
						read_into = 1;
						if( verbosity > 0 ) printf("Reading data for nodes.\n");
						break;
					}
					else
					{
						printf("%s\n",readline);
						printf("Unknown type of attributes\n");
						state = R_WAITDATA;
						read_into = 0;
					}
				}
				case R_WAITDATA:
				{
					char check[1024];
					filled = sscanf(readline," DATASET %s",check);
					if( filled != 1 ) throw BadFile;
					if( !strcmp(check,"STRUCTURED_POINTS") )
						state = R_SPOINTS;
					else if( !strcmp(check,"STRUCTURED_GRID" ) )
						state = R_SGRID;
					else if( !strcmp(check,"UNSTRUCTURED_GRID") )
						state = R_UGRID;
					else if( !strcmp(check,"POLYDATA") )
						state = R_POLYDATA;
					else if( !strcmp(check,"RECTLINEAR_GRID") )
						state = R_RGRID;
					else if( !strcmp(check,"FIELD") )
						state = R_FIELD;
					else throw BadFile;
					break;
				}
				case R_UGRID:
				{
					INMOST_DATA_REAL_TYPE coords[3] = {0,0,0};
					int npoints = 0, i = 0, ncells = 0, ncells2 = 0, nints = 0;
					char datatype[1024];
Kirill Terekhov's avatar
Kirill Terekhov committed
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
					
					while( !strncmp(readline,"FIELD",5) )
					{
						char dataname[1024], fieldname[1024];
						int nfields, ntuples, ncomps, nfields_done = 0;
						filled = sscanf(readline,"%*s %s %d",fieldname,&nfields);
						
						while( nfields_done < nfields )
						{
							if( fgets(readline,2048,f) == NULL )
							{
								state = R_QUIT;
								continue;
							}
							while( strlen(readline) != 0 && isspace(readline[strlen(readline)-1]) )
								readline[strlen(readline)-1] = '\0';
							if( strlen(readline) == 0 ) continue;
							
							sscanf(readline,"%s %d %d %s",dataname,&ncomps,&ntuples,datatype);
							DataType t;
							for(unsigned int i = 0; i < strlen(datatype); i++) datatype[i] = tolower(datatype[i]);
							if( !strcmp(datatype,"bit") || !strcmp(datatype,"unsigned_char") || !strcmp(datatype,"char") ||
								  !strcmp(datatype,"unsigned_short") || !strcmp(datatype,"short") || !strcmp(datatype,"unsigned_int") ||
								  !strcmp(datatype,"int") || !strcmp(datatype,"unsigned_long") || !strcmp(datatype,"long"))
								t = DATA_INTEGER;
							else if( !strcmp(datatype,"float") || !strcmp(datatype,"double") )
								t = DATA_REAL;
							else
								throw BadFile;
							Tag field_data = CreateTag(std::string(fieldname)+"_"+std::string(dataname),t,MESH,NONE,ncomps*ntuples);
							if( t == DATA_REAL )
							{
								for(int q = 0; q < ncomps*ntuples; ++q)
									filled = fscanf(f,"%lf ", &RealArray(GetHandle(),field_data)[q]);
							}
							else
							{
								for(int q = 0; q < ncomps*ntuples; ++q)
									filled = fscanf(f,"%d ", &IntegerArray(GetHandle(),field_data)[q]);
							}

							
							nfields_done++;
						}
						do
						{
							if( fgets(readline,2048,f) == NULL )
							{
								state = R_QUIT;
								continue;
							}
							while( strlen(readline) != 0 && isspace(readline[strlen(readline)-1]) )
								readline[strlen(readline)-1] = '\0';
						}
						while( strlen(readline) == 0 );
					}
					
					
Kirill Terekhov's avatar
Kirill Terekhov committed
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
					filled = sscanf(readline,"%*s %d %s",&npoints,datatype);
					newnodes.resize(npoints);
					//printf("number of nodes: %d\n",npoints);
					if( verbosity > 0 ) printf("Reading %d nodes.\n",npoints);
					if( filled != 2 ) throw BadFile;
					unsigned report_pace = std::max<unsigned>(npoints/250,1);
					if( binary )
					{
						i = 0;
						while(i != npoints)
						{
							if( !strcmp(datatype,"double") ) ReadCoords<double>(f,coords);
							else if( !strcmp(datatype,"float") ) ReadCoords<float>(f,coords);
							else if( !strcmp(datatype,"unsigned char") ) ReadCoords<unsigned char>(f,coords);
							else if( !strcmp(datatype,"char") ) ReadCoords<char>(f,coords);
							else if( !strcmp(datatype,"unsigned short") ) ReadCoords<unsigned short>(f,coords);
							else if( !strcmp(datatype,"short") ) ReadCoords<short>(f,coords);
							else if( !strcmp(datatype,"unsigned int") ) ReadCoords<unsigned int>(f,coords);
							else if( !strcmp(datatype,"int") ) ReadCoords<int>(f,coords);
							else if( !strcmp(datatype,"unsigned long") ) ReadCoords<unsigned long>(f,coords);
							else if( !strcmp(datatype,"long") ) ReadCoords<long>(f,coords);
							else throw BadFile;
							int find = -1;
							if( !old_nodes.empty() )
							{
								std::vector<HandleType>::iterator it = std::lower_bound(old_nodes.begin(),old_nodes.end(),coords,CentroidComparator(this));
								if( it != old_nodes.end() ) 
								{
									Storage::real_array c = RealArrayDF(*it,CoordsTag());
									if( CentroidComparator(this).Compare(c.data(),coords) == 0 )
										find = static_cast<int>(it - old_nodes.begin());
								}
							}
							if( find == -1 ) 
							{
								newnodes[i] = CreateNode(coords)->GetHandle();
								SetMarker(newnodes[i],unused_marker);
							}
							else 
								newnodes[i] = old_nodes[find];
								
							if( verbosity > 1 && i%report_pace == 0)
							{
								printf("nodes %3.1f%%\r",(i*100.0)/(1.0*npoints));
								fflush(stdout);
							}
							i++;
						}
					}
					else
					{
						i = 0;
						while(i != npoints)
						{
							double c[3];
							filled = fscanf(f," %lg %lg %lg",&c[0],&c[1],&c[2]);
							coords[0] = c[0];
							coords[1] = c[1];
							coords[2] = c[2];
							if( filled != 3 ) throw BadFile;
							int find = -1;
							if( !old_nodes.empty() )
							{
								//REPORT_VAL("look up",coords[0] << " " << coords[1] << " " << coords[2]);
								std::vector<HandleType>::iterator it = std::lower_bound(old_nodes.begin(),old_nodes.end(),coords,CentroidComparator(this));
								if( it != old_nodes.end() ) 
								{
									Storage::real_array c = RealArrayDF(*it,CoordsTag());
									//REPORT_VAL("found",c[0] << " " << c[1] << " " << c[2]);
									if( CentroidComparator(this).Compare(c.data(),coords) == 0 )
									{
										find = static_cast<int>(it - old_nodes.begin());
										//REPORT_VAL("match",find << " " << old_nodes[find]);
									}
								}
							}
							if( find == -1 )
							{
								newnodes[i] = CreateNode(coords)->GetHandle();
								SetMarker(newnodes[i],unused_marker);
							}
							else newnodes[i] = old_nodes[find];
								
							if( verbosity > 1 && i%report_pace == 0)
							{
								printf("nodes %3.1f%%\r",(i*100.0)/(1.0*npoints));
								fflush(stdout);
							}
							i++;
						}
						filled = fscanf(f,"\n");
					}
					if( fgets(readline,2048,f) == NULL ) throw BadFile;
					filled = sscanf(readline,"%*s %d %d\n",&ncells,&nints);
					//printf("number of cells: %d\nnumber of ints: %d\n",ncells,nints);
					if( filled != 2 ) throw BadFile;
					cp.resize(nints);
					ct.resize(ncells);
						
					if( binary )
						filled = static_cast<int>(fread(&cp[0],sizeof(int),nints,f));
					else
					{
						i = 0;
						while(i != nints)
						{
							filled = fscanf(f," %d",&cp[i]);
							if( filled != 1 ) throw BadFile;
							i++;
						}
						filled = fscanf(f,"\n");
					}
					if( fgets(readline,2048,f) == NULL ) throw BadFile;
					filled = sscanf(readline,"%*s %d\n",&ncells2);
					if( filled != 1 ) throw BadFile;
					if( ncells2 != ncells ) throw BadFile;
					if( binary )
						filled = static_cast<int>(fread(&ct[0],sizeof(int),nints,f));
					else
					{
						i = 0;
						while(i != ncells)
						{
							filled = fscanf(f," %d",&ct[i]);
							if( filled != 1 ) throw BadFile;
							i++;
						}
						filled = fscanf(f,"\n");
					}

1314
					if( grid_is_2d == 2 && ncells )
Kirill Terekhov's avatar
Kirill Terekhov committed
1315
1316
1317
					{
						for(i = 0; i < ncells && grid_is_2d == 2; i++)
							if( ct[i] > 9 ) grid_is_2d = 0;
1318
1319
						//this will create all 2d elements as cells
						//grid_is_2d = 1; 
1320
						if (grid_is_2d == 2) grid_is_2d = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
					}

					if( verbosity > 0 )
					{
						switch( grid_is_2d )
						{
						case 0: std::cout << "Grid has three dimensions" << std::endl; break;
						case 1: std::cout << "Grid has two dimensions" << std::endl; break;
						case 2: std::cout << "Grid has undetermined dimension" << std::endl; break;
						}
					}

					{
						if( verbosity > 0 ) printf("Reading %d cells.\n",ncells);
						int j = 0;
						ElementArray<Node> c_nodes(this);
						ElementArray<Node> e_nodes(this);
						ElementArray<Edge> f_edges(this);
						ElementArray<Face> c_faces(this);
						ElementArray<Edge> v_edges(this);
						unsigned report_pace = std::max<unsigned>(ncells/250,1);
						newcells.resize(ncells);
						for(i = 0; i < ncells; i++)
						{
							//printf("load progress: %20.2f%%\r",(float)(i+1)/(float)ncells*100.0f);
							//fflush(stdin);
							e_nodes.clear();
							c_nodes.clear();
							f_edges.clear();
							c_faces.clear();
							v_edges.clear();
							switch(ct[i])
							{
								case 1: // VTK_VERTEX
								{
									for(int k = j+1; k < j+1+cp[j]; k++)
									{
										c_nodes.push_back(newnodes[cp[k]]);
										RemMarker(newnodes[cp[k]],unused_marker);
									}
									j = j + 1 + cp[j];
									assert(c_nodes.size() == 1);
									newcells[i] = c_nodes.at(0);
									break;
								}
								case 2: //VTK_POLY_VERTEX
								{
									{
										std::stringstream setname;
										setname << "VTK_POLY_VERTEX_" << rand();
										ElementSet eset = CreateSet(setname.str()).first;
										for(int k = j+1; k < j+1+cp[j]; k++)
										{
											eset->PutElement(newnodes[cp[k]]);
											RemMarker(newnodes[cp[k]],unused_marker);
										}
										newcells[i] = eset->GetHandle();
									}
									j = j + 1 + cp[j];
									break;
								}
								case 3: //VTK_LINE
								{
									for(int k = j+1; k < j+1+cp[j]; k++)
									{
										c_nodes.push_back(newnodes[cp[k]]);
										RemMarker(newnodes[cp[k]],unused_marker);
									}
									j = j + 1 + cp[j];
									assert(c_nodes.size() == 2);
									newcells[i] = CreateEdge(c_nodes).first->GetHandle();
									break;
								}
								case 4: //VTK_POLY_LINE
								{
									/*
#if defined(VTK_DEFINE_CELLS)
									for(int k = j+1; k < j+1+cp[j]; k++)
									{
										c_nodes.push_back(newnodes[cp[k]]);
										newnodes[cp[k]]->RemMarker(unused_marker);
									}
									j = j + 1 + cp[j];
									if( c_nodes.size() < 2 ) throw BadFile;
									f_edges.resize(2);
									for(unsigned int k = 0; k < c_nodes.size()-1; k++)
									{
										e_nodes[0] = c_nodes[k];
										f_edges[0] = CreateEdge(e_nodes, 1).first;
										e_nodes[0] = c_nodes[(k+1)];
										f_edges[1] = CreateEdge(e_nodes, 1).first;
										c_faces.push_back(CreateFace(&f_edges[0],2).first);
									}
									Cell * c = CreateCell(&c_faces[0],c_faces.size(),&c_nodes[0],c_nodes.size()).first;
									newcells[i] = c;
#else
									*/
									{
										std::stringstream setname;
										setname << "VTK_POLY_LINE_" << rand();
										ElementSet eset = CreateSet(setname.str()).first;
										e_nodes.resize(2);
										for(int k = j+1; k < j+cp[j]; k++)
										{
											e_nodes.at(0) = newnodes[cp[k]];
											e_nodes.at(1) = newnodes[cp[k+1]];
											eset->PutElement(CreateEdge(e_nodes).first);
											RemMarker(newnodes[cp[k]],unused_marker);
										}
										RemMarker(newnodes[cp[j+cp[j]]],unused_marker);
										j = j + 1 + cp[j];
										newcells[i] = eset->GetHandle();
									}
//#endif
									break;
								}
								case 5: //VTK_TRIANGLE
								{
									for(int k = j+1; k < j+1+cp[j]; k++)
									{
										c_nodes.push_back(newnodes[cp[k]]);
										RemMarker(newnodes[cp[k]],unused_marker);
									}
									j = j + 1 + cp[j];
									if( c_nodes.size() != 3 ) throw BadFile;
									if( grid_is_2d == 1 )
									{
										e_nodes.resize(1);
										f_edges.resize(2);
										for(unsigned int k = 0; k < 3; k++)
										{
											e_nodes.at(0) = c_nodes.at(k);
											f_edges.at(0) = CreateEdge(e_nodes).first->GetHandle();
											e_nodes.at(0) = c_nodes.at((k+1)%3);
											f_edges.at(1) = CreateEdge(e_nodes).first->GetHandle();
											c_faces.push_back(CreateFace(f_edges).first);
										}
										Cell c = CreateCell(c_faces).first;
										newcells[i] = c->GetHandle();
									}
									else newcells[i] = CreateFace(c_nodes).first->GetHandle();
									break;
								}
								case 6: //VTK_TRIANGLE_STRIP
								{
									for(int k = j+1; k < j+1+cp[j]; k++)
									{
										c_nodes.push_back(newnodes[cp[k]]);
										RemMarker(newnodes[cp[k]],unused_marker);
									}
									j = j + 1 + cp[j];
									if( c_nodes.size() < 3 ) throw BadFile;
									/*
#if defined(VTK_DEFINE_CELLS)
									for(INMOST_DATA_ENUM_TYPE l = c_nodes.size(); l > 2; l--)
											c_faces.push_back(CreateFace(&c_nodes[l-3],3).first);
									Cell * c = CreateCell(&c_faces[0],c_faces.size(),&c_nodes[0],c_nodes.size()).first;
									newcells[i] = c;
#else //treat it as a set of faces
									*/
									{
										std::stringstream setname;
										setname << "VTK_TRIANGLE_STRIP_" << rand();
										ElementSet eset = CreateSet(setname.str()).first;
										for(ElementArray<Node>::size_type l = c_nodes.size(); l > 2; l--)
										{
											Face f = CreateFace(ElementArray<Node>(this,c_nodes.data()+l-3,c_nodes.data()+l)).first;
											eset->PutElement(f);
										}
									}
//#endif
									break;
								}
								case 7: //VTK_POLYGON
								{
									if( grid_is_2d == 1 )
									{
										for(int k = j+1; k < j+1+cp[j]; k++)
										{
											c_nodes.push_back(newnodes[cp[k]]);
											RemMarker(newnodes[cp[k]],unused_marker);
										}
										j = j + 1 + cp[j];
										e_nodes.resize(1);
										f_edges.resize(2);
										for(ElementArray<Node>::size_type k = 0; k < c_nodes.size(); k++)
										{
											e_nodes.at(0) = c_nodes.at(k);
											f_edges.at(0) = CreateEdge(e_nodes).first->GetHandle();
											e_nodes.at(0) = c_nodes.at((k+1)%c_nodes.size());
											f_edges.at(1) = CreateEdge(e_nodes).first->GetHandle();
											c_faces.push_back(CreateFace(f_edges).first);
										}
										Cell c = CreateCell(c_faces,c_nodes).first;
										newcells[i] = c->GetHandle();
									}
									else
									{
										for(int k = j+1; k < j+1+cp[j]; k++)
										{
											c_nodes.push_back(newnodes[cp[k]]);
											RemMarker(newnodes[cp[k]],unused_marker);
										}
										j = j + 1 + cp[j];
										newcells[i] = CreateFace(c_nodes).first->GetHandle();
									}
									break;
								}
								case 8: //VTK_PIXEL
								{
									for(int k = j+1; k < j+1+cp[j]; k++)
									{
										c_nodes.push_back(newnodes[cp[k]]);
										RemMarker(newnodes[cp[k]],unused_marker);
									}
									j = j + 1 + cp[j];
									if( c_nodes.size() != 4 ) throw BadFile;
									HandleType temp = c_nodes.at(2);
									c_nodes.at(2) = c_nodes.at(3);
									c_nodes.at(3) = temp;
									if( grid_is_2d == 1 )
									{
										e_nodes.resize(1);
										f_edges.resize(2);
										for(int k = 0; k < 4; k++)
										{
											e_nodes.at(0) = c_nodes.at(k);
											f_edges.at(0) = CreateEdge(e_nodes).first->GetHandle();
											e_nodes.at(0) = c_nodes.at((k+1)%4);
											f_edges.at(1) = CreateEdge(e_nodes).first->GetHandle();
											c_faces.push_back(CreateFace(f_edges).first);
										}
										Cell c = CreateCell(c_faces,c_nodes).first;
										newcells[i] = c->GetHandle();
									}
									else newcells[i] = CreateFace(c_nodes).first->GetHandle();
									break;
								}
								case 9: //VTK_QUAD
								{
									for(int k = j+1; k < j+1+cp[j]; k++)
									{
										c_nodes.push_back(newnodes[cp[k]]);
										RemMarker(newnodes[cp[k]],unused_marker);
									}
									j = j + 1 + cp[j];
									if( c_nodes.size() != 4 ) throw BadFile;
									if( grid_is_2d == 1 )
									{
										e_nodes.resize(1);
										f_edges.resize(2);
										for(int k = 0; k < 4; k++)
										{
											e_nodes.at(0) = c_nodes.at(k);
											f_edges.at(0) = CreateEdge(e_nodes).first->GetHandle();
											e_nodes.at(0) = c_nodes.at((k+1)%4);
											f_edges.at(1) = CreateEdge(e_nodes).first->GetHandle();
											c_faces.push_back(CreateFace(f_edges).first);
										}
										Cell c = CreateCell(c_faces,c_nodes).first;
										newcells[i] = c->GetHandle();
									}
									else newcells[i] = CreateFace(c_nodes).first->GetHandle();
									break;
								}
								case 10: //VTK_TETRA
								{
									for(int k = j+1; k < j+1+cp[j]; k++)
									{
										c_nodes.push_back(newnodes[cp[k]]);
										RemMarker(newnodes[cp[k]],unused_marker);
									}
									j = j + 1 + cp[j];
									if( c_nodes.size() != 4 ) throw BadFile;
									const integer nodesnum[12] = {0,2,1,0,1,3,1,2,3,0,3,2};
									const integer sizes[4] = {3,3,3,3};
									Cell c = CreateCell(c_nodes,nodesnum,sizes,4).first;
									newcells[i] = c->GetHandle();
									break;
								}
								case 11: //VTK_VOXEL
								{
									for(int k = j+1; k < j+1+cp[j]; k++)
									{
										c_nodes.push_back(newnodes[cp[k]]);
										RemMarker(newnodes[cp[k]],unused_marker);
									}
									j = j + 1 + cp[j];
									if (c_nodes.size() != 8) throw BadFile;
									{
										HandleType temp;
										temp = c_nodes.at(2);
										c_nodes.at(2) = c_nodes.at(3);
										c_nodes.at(3) = temp;
										temp = c_nodes.at(6);
										c_nodes.at(6) = c_nodes.at(7);
										c_nodes.at(7) = temp;
									}
									//INMOST_DATA_ENUM_TYPE nodesnum[24] = {0,4,6,2,1,3,7,5,2,6,7,3,0,1,5,4,0,2,3,1,4,5,7,6};
									const integer nodesnum[24] = {0,4,7,3,1,2,6,5,0,1,5,4,3,7,6,2,0,3,2,1,4,5,6,7};
									const integer sizes[6] = {4,4,4,4,4,4};
									Cell c = CreateCell(c_nodes,nodesnum,sizes,6).first;
									newcells[i] = c->GetHandle();
									break;
								}
								case 12: //VTK_HEXAHEDRON
								{

									for(int k = j+1; k < j+1+cp[j]; k++)
									{
										c_nodes.push_back(newnodes[cp[k]]);
										RemMarker(newnodes[cp[k]],unused_marker);
									}
									j = j + 1 + cp[j];
									if( c_nodes.size() != 8 ) throw BadFile;
									const integer nodesnum[24] = {0,4,7,3,1,2,6,5,0,1,5,4,3,7,6,2,0,3,2,1,4,5,6,7};
									const integer sizes[6] = {4,4,4,4,4,4};
									Cell c = CreateCell(c_nodes,nodesnum,sizes,6).first;
									newcells[i] = c->GetHandle();
									break;
								}
								case 13: //VTK_WEDGE
								{
									for(int k = j+1; k < j+1+cp[j]; k++)
									{
										c_nodes.push_back(newnodes[cp[k]]);
										RemMarker(newnodes[cp[k]],unused_marker);
									}
									j = j + 1 + cp[j];
									if( c_nodes.size() != 6 ) throw BadFile;
									const integer nodesnum[18] = {0,2,5,3,1,4,5,2,0,3,4,1,3,5,4,0,1,2};
Kirill Terekhov's avatar
Kirill Terekhov committed
1652
									//const integer nodesnum[18] = { 0, 3, 5, 2, 0, 1, 4, 3, 1, 4, 5, 2, 3, 4, 5, 0, 2, 1 };
Kirill Terekhov's avatar
Kirill Terekhov committed
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
									const integer sizes[5] = {4,4,4,3,3};
									Cell c = CreateCell(c_nodes,nodesnum,sizes,5).first;
									newcells[i] = c->GetHandle();
									break;
								}
								case 14: //VTK_PYRAMID
								{
									for(int k = j+1; k < j+1+cp[j]; k++)
									{
										c_nodes.push_back(newnodes[cp[k]]);
										RemMarker(newnodes[cp[k]],unused_marker);
									}
									j = j + 1 + cp[j];
									if( c_nodes.size() != 5 ) throw BadFile;
									const integer nodesnum[16] = {0,4,3,0,1,4,1,2,4,3,4,2,0,3,2,1};
									const integer sizes[5] = {3,3,3,3,4};
									Cell c = CreateCell(c_nodes,nodesnum,sizes,5).first;
									newcells[i] = c->GetHandle();
									break;
								}
								case 15: //VTK_PENTAGONAL_PRISM
								{
Kirill Terekhov's avatar
Kirill Terekhov committed
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
									for (int k = j + 1; k < j + 1 + cp[j]; k++)
									{
										c_nodes.push_back(newnodes[cp[k]]);
										RemMarker(newnodes[cp[k]], unused_marker);
									}
									j = j + 1 + cp[j];
									if (c_nodes.size() != 10) throw BadFile;
									const integer nodesnum[30] =
									{
										0, 1, 6, 5,
										1, 2, 7, 6,
										2, 3, 8, 7,
										3, 4, 9, 8,
										4, 0, 5, 9,
										5, 6, 7, 8, 9,
										4, 3, 2, 1, 0
									};
									//5, 6, 7, 8, 9
									//0, 1, 2, 3, 4
									const integer sizes[7] = { 4, 4, 4, 4, 4, 5, 5 };
									Cell c = CreateCell(c_nodes, nodesnum, sizes, 7).first;
									newcells[i] = c->GetHandle();
									//printf("no order description for VTK_PENTAGONAL_PRISM\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
1698
1699
1700
1701
									break;
								}
								case 16: //VTK_HEXAGONAL_PRISM
								{
Kirill Terekhov's avatar
Kirill Terekhov committed
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
									for (int k = j + 1; k < j + 1 + cp[j]; k++)
									{
										c_nodes.push_back(newnodes[cp[k]]);
										RemMarker(newnodes[cp[k]], unused_marker);
									}
									j = j + 1 + cp[j];
									if (c_nodes.size() != 12) throw BadFile;
									const integer nodesnum[36] =
									{
										0, 1, 7, 6,
										1, 2, 8, 7,
										2, 3, 9, 8,
										3, 4, 10, 9,
										4, 5, 11, 10,
										5, 0, 6, 11,
										6, 7, 8, 9, 10, 11,
										5, 4, 3, 2, 1, 0
									};
									//6, 7, 8, 9, 10, 11
									//0, 1, 2, 3, 4, 5
									const integer sizes[8] = { 4, 4, 4, 4, 4, 4, 6, 6 };
									Cell c = CreateCell(c_nodes, nodesnum, sizes, 8).first;
									newcells[i] = c->GetHandle();
									//printf("no order description for VTK_HEXAGONAL_PRISM\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
									break;
								}
								case 21: //VTK_QUADRATIC_EDGE
								{
									printf("no order description for VTK_QUADRATIC_EDGE\n");
									break;
								}
								case 22: //VTK_QUADRATIC_TRIANGLE
								{
									printf("no order description for VTK_QUADRATIC_TRIANGLE\n");
									break;
								}
								case 23: //VTK_QUADRATIC_QUAD
								{
									printf("no order description for VTK_QUADRATIC_QUAD\n");
									break;
								}
								case 24: //VTK_QUADRATIC_TETRA
								{
									printf("no order description for VTK_QUADRATIC_TETRA\n");
									break;
								}
								case 25: //VTK_QUADRATIC_HEXAHEDRON
								{
									printf("no order description for VTK_QUADRATIC_HEXAHEDRON\n");
									break;
								}
								case 26: //VTK_QUADRATIC_WEDGE
								{
									printf("no order description for VTK_QUADRATIC_WEDGE\n");
									break;
								}
								case 27: //VTK_QUADRATIC_PYRAMID
								{
									printf("no order description for VTK_QUADRATIC_PYRAMID\n");
									break;
								}
								case 28: //VTK_BIQUADRATIC_QUAD
								{
									printf("no order description for VTK_BIQUADRATIC_QUAD\n");
									break;
								}
								case 29: //VTK_TRIQUADRATIC_HEXAHEDRON
								{
									printf("no order description for VTK_TRIQUADRATIC_HEXAHEDRON\n");
									break;
								}
								case 30: //VTK_QUADRATIC_LINEAR_QUAD
								{
									printf("no order description for VTK_QUADRATIC_LINEAR_QUAD\n");
									break;
								}
								case 31: //VTK_QUADRATIC_LINEAR_WEDGE
								{
									printf("no order description for VTK_QUADRATIC_LINEAR_WEDGE\n");
									break;
								}
								case 32: //VTK_BIQUADRATIC_QUADRATIC_WEDGE
								{
									printf("no order description for VTK_BIQUADRATIC_QUADRATIC_WEDGE\n");
									break;
								}
								case 33: //VTK_BIQUADRATIC_QUADRATIC_HEXAHEDRON
								{
									printf("no order description for VTK_BIQUADRATIC_QUADRATIC_HEXAHEDRON\n");
									break;
								}
								case 34: //VTK_BIQUADRATIC_TRIANGLE
								{
									printf("no order description for VTK_BIQUADRATIC_TRIANGLE\n");
									break;
								}
								case 35: //VTK_CUBIC_LINE
								{
									printf("no order description for VTK_CUBIC_LINE\n");
									break;
								}
								case 41: //VTK_CONVEX_POINT_SET
								{
									printf("no algorithm for VTK_CONVEX_POINT_SET yet\n");
									break;
								}
								case 42: //VTK_POLYHEDRON
								{
									int k = j;
									k++; //skip number of total number of integers
									dynarray<integer,64> sizes(cp[k++]);
									for(dynarray<integer,64>::size_type m = 0; m < sizes.size(); m++)
									{
										sizes[m] = cp[k++];
										for (integer l = 0; l < sizes[m]; l++)
										{
											c_nodes.push_back(newnodes[cp[k++]]);
											c_nodes.back()->RemMarker(unused_marker);
										}
									}
									j = k;
									Cell c = CreateCell(c_nodes,sizes.data(),static_cast<integer>(sizes.size())).first;
									newcells[i] = c->GetHandle();
									break;
								}
								case 51: //VTK_PARAMETRIC_CURVE
								{
									printf("no order description for VTK_PARAMETRIC_CURVE\n");
									break;
								}
								case 52: //VTK_PARAMETRIC_SURFACE
								{
									printf("no order description for VTK_PARAMETRIC_SURFACE\n");
									break;
								}
								case 53: //VTK_PARAMETRIC_TRI_SURFACE
								{
									printf("no order description for VTK_PARAMETRIC_TRI_SURFACE\n");
									break;
								}
								case 54: //VTK_PARAMETRIC_QUAD_SURFACE
								{
									printf("no order description for VTK_PARAMETRIC_QUAD_SURFACE\n"