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

Kirill Terekhov's avatar
Kirill Terekhov committed
5
6
7
8
9
10
11
//g++ main.cpp rotate.cpp -L/usr/X11R6/lib -lX11 -lXi -lXmu -lGL -lglut -lGLU ../../INMOST.a -O5
// press space - explode mesh to see connection 
#include "../../inmost.h"
#include "rotate.h"
#include <iostream>
#include <sstream>
#include <algorithm>
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
12
#include <stdarg.h>
Kirill Terekhov's avatar
Kirill Terekhov committed
13
#include "my_glut.h"
Kirill Terekhov's avatar
Kirill Terekhov committed
14
15
16
17
18
19
20
21
22
23
24
#include <iomanip>

inline static unsigned int flip(const unsigned int * fp)
{
	unsigned int mask = -((int)(*fp >> 31)) | 0x80000000;
	return *fp ^ mask;
}
#define _0(x)	(x & 0x7FF)
#define _1(x)	(x >> 11 & 0x7FF)
#define _2(x)	(x >> 22 )

Kirill Terekhov's avatar
Kirill Terekhov committed
25
26
27
28
29
30
31


using namespace INMOST;
Mesh * mesh;
int interactive = 0;
double zoom = 1;
int width = 800, height = 800;
Kirill Terekhov's avatar
Kirill Terekhov committed
32
33
34
double sleft = 1e20, sright = -1e20, sbottom = 1e20, stop = -1e20, sfar = -1e20, snear = 1e20;
double shift[3] = {0,0,0};
bool perspective = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
35
bool boundary = true, planecontrol = false, clipupdate = false, bndupdate = true, clipboxupdate = false, draw_volumetric = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
36

Kirill Terekhov's avatar
Kirill Terekhov committed
37
Mesh::GeomParam table;
Kirill Terekhov's avatar
Kirill Terekhov committed
38

Kirill Terekhov's avatar
Kirill Terekhov committed
39
40
41
42
43
44
45
46
47
48
49
#define CLIP_NONE 0
#define CLIP_NODE 1
#define CLIP_FULL 2
#define CLIP_ENDP 3

#define CLIP_FACE_NONE      0
#define CLIP_FACE_INSIDE    1
#define CLIP_FACE_OUTSIDE   2
#define CLIP_FACE_INTERSECT 3

Storage::real p[3] = {0,0,0}, n[3] = {0,0,1};
Kirill Terekhov's avatar
Kirill Terekhov committed
50
ElementArray<Element> boundary_faces;
Kirill Terekhov's avatar
Kirill Terekhov committed
51

Kirill Terekhov's avatar
Kirill Terekhov committed
52
53
54
55
56
57
58
double amplitude = 10;
double radius = 25;
char visualization_prompt[8192];
bool visualization_prompt_active = false;
Tag visualization_tag;
ElementType visualization_type;

Kirill Terekhov's avatar
Kirill Terekhov committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
void printtext(const char * fmt, ... )
{
	
	unsigned int i;
	char stext[1024];
	va_list ap;
	if ( fmt == NULL ) return;
	va_start(ap,fmt);
	vsprintf(stext,fmt,ap);
	va_end(ap);
	for(i=0;i<strlen(stext);i++)
	{
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, 
							stext[i]);
	}
}

Kirill Terekhov's avatar
Kirill Terekhov committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const int name_width = 32;
const int type_width = 14;
const int elems_width = 10;
const int sparse_width = 10;
const int length_width = 10;

void PrintTag(Tag t)
{
	std::cout << std::setw(name_width) << t.GetTagName() << std::setw(type_width) << DataTypeName(t.GetDataType());
	int num = 0;
	char elems[7] = "NEFCSM";
	std::string print = "";
	for(ElementType etype = NODE; etype <= MESH; etype = etype << 1)
	{
		if( t.isDefined(etype) )
		{
			print = print + elems[ElementNum(etype)];
			num++;
		}
	}
	std::cout << std::setw(elems_width) << print;
	print = "";
	for(ElementType etype = NODE; etype <= MESH; etype = etype << 1)
	{
		if( t.isSparse(etype) )
		{
			print = print + elems[ElementNum(etype)];
			num++;
		}
	}
	std::cout << std::setw(sparse_width) << print;
	if( t.GetSize() == ENUMUNDEF )
		std::cout << std::setw(length_width) << "VAR" << std::endl;
	else
		std::cout << std::setw(length_width) << t.GetSize() << std::endl;
}

void PrintTags(Mesh * m, ElementType etypes)
{
	std::cout << std::setw(name_width) << "Name" << std::setw(type_width) << "Type" << std::setw(elems_width) << "Element" << std::setw(sparse_width) << "Sparse" << std::setw(length_width) << "Length" << std::endl;
	for(Mesh::iteratorTag t = m->BeginTag(); t != m->EndTag(); ++t )
	{
		bool print = false;
		for(ElementType etype = NODE; etype <= MESH; etype = etype << 1) if( (etype&etypes) && t->isDefined(etype) ) {print = true; break;}
		if( print ) PrintTag(*t);
	}
}

struct color_t
{
	float c[4];
	color_t() {memset(c,0,sizeof(float)*4);}
	color_t(float r, float g, float b)
	{
		c[0] = r;
		c[1] = g;
		c[2] = b;
		c[3] = 1.0;
	}
	color_t(float r, float g, float b, float a)
	{
		c[0] = r;
		c[1] = g;
		c[2] = b;
		c[3] = a;
	}
	color_t(const color_t & other) {memcpy(c,other.c,sizeof(float)*4);}
	color_t & operator =(color_t const & other)
	{
		memmove(c,other.c,sizeof(float)*4);
		return *this;
	}
	void set_color() const {glColor4fv(c);}
	float & r() {return c[0];}
	float & g() {return c[1];}
	float & b() {return c[2];}
	float & a() {return c[3];}
	float r() const {return c[0];}
	float g() const {return c[1];}
	float b() const {return c[2];}
	float a() const {return c[3];}
	color_t operator *(float mult){return color_t(c[0]*mult,c[1]*mult,c[2]*mult,c[3]*mult);}
	color_t operator +(color_t other){return color_t(c[0]+other.c[0],c[1]+other.c[1],c[2]+other.c[2],c[3]+other.c[3]);}
};

class color_bar
{
	float min, max;
	std::vector<float> ticks; //ticks from 0 to 1 for each color
	std::vector<color_t> colors; //4 floats for each tick
	std::string comment;
public:
	color_bar()
	{
		min = 0;
		max = 1;
		comment = "";
		ticks.push_back(0.f);
		ticks.push_back(0.2f);
		ticks.push_back(0.4f);
		ticks.push_back(0.6f);
		ticks.push_back(0.8f);
		ticks.push_back(1.f);
		colors.push_back(color_t(1,0,0));
		colors.push_back(color_t(1,1,0));
		colors.push_back(color_t(0,1,0));
		colors.push_back(color_t(0,1,1));
		colors.push_back(color_t(0,0,1));
		colors.push_back(color_t(1,0,1));
		
		
		
		
		//colors.push_back(color_t(1,0,0));
	}
	void set_comment(std::string text) {comment = text;}
	void set_min(float newmin) { min = newmin;}
	void set_max(float newmax) { max = newmax;}
	color_t pick_color(float value) 
	{
		float t = (value-min)/(max-min);
		std::vector<float>::iterator it = std::lower_bound(ticks.begin(),ticks.end(),t);
		size_t pos = it-ticks.begin();
		if( it == ticks.end() || pos >= ticks.size() ) 
		{
			return colors.back();
		}
		if( pos == 0 ) 
		{
			return colors[0];
		}
		float interp = (t-ticks[pos-1])/(ticks[pos]-ticks[pos-1]);
		return (colors[pos]*interp+colors[pos-1]*(1-interp));
	}
	void Draw()
	{
		float text_pos = -0.89;
		float left = -0.95;
		float right = -0.9;
		float bottom = -0.75;
		float top = 0.75;
		glBegin(GL_QUADS);
		for(int i = 0; i < ticks.size()-1; ++i)
		{
			colors[i].set_color();
			glVertex2f(left,bottom+ticks[i]*(top-bottom));
			glVertex2f(right,bottom+ticks[i]*(top-bottom));
			colors[i+1].set_color();
			glVertex2f(right,bottom+ticks[(i+1)]*(top-bottom));
			glVertex2f(left,bottom+ticks[(i+1)]*(top-bottom));
		}
		glEnd();
		
		glColor4f(0,0,0,1);
		for(int i = 0; i < ticks.size(); ++i)
		{
			glRasterPos2f(text_pos,bottom+ticks[i]*(top-bottom));
			printtext("%f",min+ticks[i]*(max-min));
		}
		if( comment != "")
		{
			glRasterPos2f(left,bottom-0.04);
			printtext("%s",comment.c_str());
		}
	}
} CommonColorBar;

Kirill Terekhov's avatar
Kirill Terekhov committed
243
244
245

class face2gl
{
Kirill Terekhov's avatar
Kirill Terekhov committed
246
	float dist;
Kirill Terekhov's avatar
Kirill Terekhov committed
247
248
249
250
251
252
	double c[4];
	double cnt[3];
	bool flag;
	ElementType etype;
	Storage::integer id;
	std::vector<double> verts;
Kirill Terekhov's avatar
Kirill Terekhov committed
253
254
	std::vector<color_t> colors;
	color_t cntcolor;
Kirill Terekhov's avatar
Kirill Terekhov committed
255
public:
Kirill Terekhov's avatar
Kirill Terekhov committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
	static void radix_sort_dist(std::vector<face2gl> & set)
	{
		static std::vector<face2gl> tmp;
		tmp.resize(set.size());
		unsigned int i;
		const unsigned int kHist = 2048;
		unsigned int  b0[kHist * 3];
		unsigned int *b1 = b0 + kHist;
		unsigned int *b2 = b1 + kHist;
		memset(b0,0,sizeof(unsigned int)*kHist*3);
		for (i = 0; i < set.size(); i++) 
		{
			unsigned int fi = flip((unsigned int *)&set[i].dist);
			++b0[_0(fi)]; ++b1[_1(fi)]; ++b2[_2(fi)];
		}
		{
			unsigned int sum0 = 0, sum1 = 0, sum2 = 0;
			for (i = 0; i < kHist; i++) 
			{
				b0[kHist-1] = b0[i] + sum0; b0[i] = sum0 - 1; sum0 = b0[kHist-1];
				b1[kHist-1] = b1[i] + sum1; b1[i] = sum1 - 1; sum1 = b1[kHist-1];
				b2[kHist-1] = b2[i] + sum2; b2[i] = sum2 - 1; sum2 = b2[kHist-1];
			}
		}
		for (i = 0; i < set.size(); i++) tmp[++b0[_0(flip((unsigned int *)&set[i].dist))]] = set[i];
		for (i = 0; i < set.size(); i++) set[++b1[_1(flip((unsigned int *)&tmp[i].dist))]] = tmp[i];
		for (i = 0; i < set.size(); i++) tmp[++b2[_2(flip((unsigned int *)&set[i].dist))]] = set[i];
		for (i = 0; i < set.size(); i++) set[i] = tmp[set.size()-1-i];
	}
	face2gl():verts(),colors() {etype = NONE; id = 0; dist = 0; flag = false; memset(c,0,sizeof(double)*4);}
Kirill Terekhov's avatar
Kirill Terekhov committed
286
287
288
289
290
291
292
293
294
295
296
297
298
	face2gl(const face2gl & other) :verts(other.verts) 
	{
		etype = other.etype;
		id = other.id;
		dist = other.dist; 
		cnt[0] = other.cnt[0];
		cnt[1] = other.cnt[1];
		cnt[2] = other.cnt[2];
		c[0] = other.c[0];
		c[1] = other.c[1];
		c[2] = other.c[2];
		c[3] = other.c[3];
		flag = other.flag;
Kirill Terekhov's avatar
Kirill Terekhov committed
299
300
		colors = other.colors;
		cntcolor = other.cntcolor;
Kirill Terekhov's avatar
Kirill Terekhov committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
	}
	face2gl & operator =(face2gl const & other) 
	{ 
		etype = other.etype;
		id = other.id;
		verts = other.verts; 
		dist = other.dist; 
		cnt[0] = other.cnt[0];
		cnt[1] = other.cnt[1];
		cnt[2] = other.cnt[2];
		c[0] = other.c[0];
		c[1] = other.c[1];
		c[2] = other.c[2];
		c[3] = other.c[3];
		flag = other.flag;
Kirill Terekhov's avatar
Kirill Terekhov committed
316
317
		colors = other.colors;
		cntcolor = other.cntcolor;
Kirill Terekhov's avatar
Kirill Terekhov committed
318
319
320
321
322
		return *this;
	}
	~face2gl() {}
	void draw_colour() const
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
323
		if( colors.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
324
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
			glColor4dv(c); 
			for(unsigned k = 0; k < verts.size(); k+=3) 
			{
				glVertex3dv(cnt);
				glVertex3dv(&verts[k]);
				glVertex3dv(&verts[(k+3)%verts.size()]);
			}
		}
		else
		{
			for(unsigned k = 0; k < verts.size(); k+=3) 
			{
				cntcolor.set_color();
				glVertex3dv(cnt);
				colors[k/3].set_color();
				glVertex3dv(&verts[k]);
				colors[(k/3+1)%colors.size()].set_color();
				glVertex3dv(&verts[(k+3)%verts.size()]);
			}
		}
	}
	void draw_colour_alpha(double alpha) const
	{
		if( colors.empty() )
		{
			double cc[4] = {c[0],c[1],c[2],alpha};
			glColor4dv(c); 
			for(unsigned k = 0; k < verts.size(); k+=3) 
			{
				glVertex3dv(cnt);
				glVertex3dv(&verts[k]);
				glVertex3dv(&verts[(k+3)%verts.size()]);
			}
		}
		else
		{
			for(unsigned k = 0; k < verts.size(); k+=3) 
			{
				color_t t = cntcolor;
				t.a() = alpha;
				t.set_color();
				glVertex3dv(cnt);
				t = colors[k/3];
				t.a() = alpha;
				t.set_color();
				glVertex3dv(&verts[k]);
				t = colors[(k/3+1)%colors.size()];
				t.a() = alpha;
				t.set_color();
				glVertex3dv(&verts[(k+3)%verts.size()]);
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
		}
	}
	void draw() const
	{
		for(unsigned k = 0; k < verts.size(); k+=3) 
		{
			glVertex3dv(cnt);
			glVertex3dv(&verts[k]);
			glVertex3dv(&verts[(k+3)%verts.size()]);
		}
	}
	void drawedges() const
	{
		for(unsigned k = 0; k < verts.size(); k+=3) 
		{
			glVertex3dv(&verts[k]); 
			glVertex3dv(&verts[(k+3)%verts.size()]); 
		}
	}
	bool operator <(const face2gl & other) const {return dist < other.dist;}
	void set_color(double r, double g, double b, double a) {c[0] = r; c[1] = g; c[2] = b; c[3] = a;}
Kirill Terekhov's avatar
Kirill Terekhov committed
397
	void add_vert(double x, double y, double z) {unsigned s = (unsigned)verts.size(); verts.resize(s+3); verts[s] = x; verts[s+1] = y; verts[s+2] = z;}
Kirill Terekhov's avatar
Kirill Terekhov committed
398
	void add_vert(double v[3]) {verts.insert(verts.end(),v,v+3);}
Kirill Terekhov's avatar
Kirill Terekhov committed
399
	void add_color(color_t c) {colors.push_back(c);}
Kirill Terekhov's avatar
Kirill Terekhov committed
400
	double * get_vert(int k) {return &verts[k*3];}
Kirill Terekhov's avatar
Kirill Terekhov committed
401
	unsigned size() {return (unsigned)verts.size()/3;}
Kirill Terekhov's avatar
Kirill Terekhov committed
402
	void set_center(double _cnt[3], color_t c = color_t(0,0,0,0))
Kirill Terekhov's avatar
Kirill Terekhov committed
403
404
405
406
	{
		cnt[0] = _cnt[0];
		cnt[1] = _cnt[1];
		cnt[2] = _cnt[2];
Kirill Terekhov's avatar
Kirill Terekhov committed
407
		cntcolor = c;
Kirill Terekhov's avatar
Kirill Terekhov committed
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
	}
	void get_center(float _cnt[3])
	{
		_cnt[0] = cnt[0];
		_cnt[1] = cnt[1];
		_cnt[2] = cnt[2];
	}
	void get_center(double _cnt[3])
	{
		_cnt[0] = cnt[0];
		_cnt[1] = cnt[1];
		_cnt[2] = cnt[2];
	}
	double * get_center() {return cnt;}
	void compute_center()
	{
		cnt[0] = cnt[1] = cnt[2] = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
425
		for(INMOST_DATA_ENUM_TYPE k = 0; k < verts.size(); k+=3)
Kirill Terekhov's avatar
Kirill Terekhov committed
426
427
428
429
430
431
432
433
		{
			cnt[0] += verts[k+0];
			cnt[1] += verts[k+1];
			cnt[2] += verts[k+2];
		}
		cnt[0] /= (verts.size()/3)*1.0;
		cnt[1] /= (verts.size()/3)*1.0;
		cnt[2] /= (verts.size()/3)*1.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
434
435
436
437
438
439
440
441
442
443
444
		if( !colors.empty() ) compute_center_color();
	}
	void compute_center_color()
	{
		cntcolor.r() = 0;
		cntcolor.g() = 0;
		cntcolor.b() = 0;
		cntcolor.a() = 0;
		for(INMOST_DATA_ENUM_TYPE k = 0; k < colors.size(); k++)
			cntcolor = cntcolor + colors[k];
		cntcolor =  cntcolor*(1.0f/static_cast<float>(colors.size()));
Kirill Terekhov's avatar
Kirill Terekhov committed
445
446
447
448
449
450
451
452
	}
	void compute_dist(double cam[3])
	{
		dist = sqrt((cnt[0]-cam[0])*(cnt[0]-cam[0])+(cnt[1]-cam[1])*(cnt[1]-cam[1])+(cnt[2]-cam[2])*(cnt[2]-cam[2]));
	}
	void set_flag(bool set) { flag = set;}
	bool get_flag() {return flag;}
	void set_elem(ElementType _etype, Storage::integer _id) {etype = _etype; id = _id;}
Kirill Terekhov's avatar
Kirill Terekhov committed
453
	Element get_elem(Mesh * m) {return m->ElementByLocalID(etype,id);}
Kirill Terekhov's avatar
Kirill Terekhov committed
454
};
Kirill Terekhov's avatar
Kirill Terekhov committed
455
face2gl DrawFace(Element f);
Kirill Terekhov's avatar
Kirill Terekhov committed
456
457
458
459

std::vector<face2gl> all_boundary;
std::vector<face2gl> clip_boundary;

Kirill Terekhov's avatar
Kirill Terekhov committed
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
void draw_faces_nc(std::vector<face2gl> & set, int highlight = -1)
{
	glColor4f(0,1,0,0.1);
	glBegin(GL_TRIANGLES);
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) set[q].draw();
	glEnd();
	if( highlight != -1 )
	{
		glColor4f(1,0,0,1);
		glBegin(GL_TRIANGLES);
		set[highlight].draw();
		glEnd();
	}
}

Kirill Terekhov's avatar
Kirill Terekhov committed
475
476
477
void draw_faces(std::vector<face2gl> & set, int highlight = -1)
{
	glBegin(GL_TRIANGLES);
Kirill Terekhov's avatar
Kirill Terekhov committed
478
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) set[q].draw_colour();
Kirill Terekhov's avatar
Kirill Terekhov committed
479
480
481
482
483
484
485
486
487
488
	glEnd();
	if( highlight != -1 )
	{
		glColor4f(1,0,0,1);
		glBegin(GL_TRIANGLES);
		set[highlight].draw();
		glEnd();
	}
}

Kirill Terekhov's avatar
Kirill Terekhov committed
489
490
491
492
493
494
495
void draw_faces_alpha(std::vector<face2gl> & set, double alpha)
{
	glBegin(GL_TRIANGLES);
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) set[q].draw_colour_alpha(alpha);
	glEnd();
}

Kirill Terekhov's avatar
Kirill Terekhov committed
496
497
498
void draw_edges(std::vector<face2gl> & set, int highlight = -1)
{
	glBegin(GL_LINES);
Kirill Terekhov's avatar
Kirill Terekhov committed
499
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) set[q].drawedges();
Kirill Terekhov's avatar
Kirill Terekhov committed
500
501
502
503
504
505
506
507
508
509
510
	glEnd();
	if( highlight != -1 )
	{
		glColor4f(0,1,0,1);
		glBegin(GL_LINES);
		set[highlight].drawedges();
		glEnd();
	}
	
}

Kirill Terekhov's avatar
Kirill Terekhov committed
511
512
513
514
515
516
517
518
void draw_faces_interactive_nc(std::vector<face2gl> & set)
{
	glColor4f(0,1,0,0.1);
	glBegin(GL_TRIANGLES);
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) if( set[q].get_flag() ) set[q].draw();
	glEnd();
}

Kirill Terekhov's avatar
Kirill Terekhov committed
519
520
521
void draw_faces_interactive(std::vector<face2gl> & set)
{
	glBegin(GL_TRIANGLES);
Kirill Terekhov's avatar
Kirill Terekhov committed
522
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) if( set[q].get_flag() ) set[q].draw_colour();
Kirill Terekhov's avatar
Kirill Terekhov committed
523
524
525
	glEnd();
}

Kirill Terekhov's avatar
Kirill Terekhov committed
526
527
528
529
530
531
532
void draw_faces_interactive_alpha(std::vector<face2gl> & set, double alpha)
{
	glBegin(GL_TRIANGLES);
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) if( set[q].get_flag() ) set[q].draw_colour_alpha(alpha);
	glEnd();
}

Kirill Terekhov's avatar
Kirill Terekhov committed
533
534
535
void draw_edges_interactive(std::vector<face2gl> & set)
{
	glBegin(GL_LINES);
Kirill Terekhov's avatar
Kirill Terekhov committed
536
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) if( set[q].get_flag() ) set[q].drawedges();
Kirill Terekhov's avatar
Kirill Terekhov committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
	glEnd();
}

Storage::integer clip_plane_edge(double sp0[3], double sp1[3], double p[3], double n[3], double node[3])
{
	Storage::real u[3], w[3], D, N, sI;
	u[0] = sp1[0] - sp0[0]; u[1] = sp1[1] - sp0[1]; u[2] = sp1[2] - sp0[2];
	w[0] = sp0[0] - p[0];   w[1] = sp0[1] - p[1];   w[2] = sp0[2] - p[2];
	D =  (n[0]*u[0] + n[1]*u[1] + n[2]*u[2]);
	N = -(n[0]*w[0] + n[1]*w[1] + n[2]*w[2]);
	if( fabs(D) < 1.0e-9 )
	{
		if( fabs(N) < 1.0e-9 ) return CLIP_FULL;
		else return CLIP_NONE;
	}
	else
	{
		sI = N/D;
		if( sI < 0-1.0e-9 || sI > 1+1.0e-9 ) return CLIP_NONE;
		else
		{
			node[0] = sp0[0] + sI * u[0];
			node[1] = sp0[1] + sI * u[1];
			node[2] = sp0[2] + sI * u[2];
			return (sI > 1.0e-9 && sI < 1.0-1.0e-9) ? CLIP_NODE : CLIP_ENDP;
		}
	}
}

Kirill Terekhov's avatar
Kirill Terekhov committed
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
typedef struct point
{
	float coords[3];
	float diam;
	float dist;
	int id;
	/*
	point & operator = (point const & b)
	{
		coords[0] = b.coords[0];
		coords[1] = b.coords[1];
		coords[2] = b.coords[2];
		diam = b.diam;
		dist = b.dist;
		id = b.id;
	}

	point(const point & b)
	{
		coords[0] = b.coords[0];
		coords[1] = b.coords[1];
		coords[2] = b.coords[2];
		diam = b.diam;
		dist = b.dist;
		id = b.id;
	}
	*/
} point_t;

bool operator <(point_t & a, point_t & b) {return a.dist < b.dist;}




class volumetric
{
	std::vector<point_t> points;
	Mesh * m;

	void radix_sort_dist(std::vector<point_t> & set)
	{
		static std::vector<point_t> tmp;
		tmp.resize(set.size());
		unsigned int i;
		const unsigned int kHist = 2048;
		unsigned int  b0[kHist * 3];
		unsigned int *b1 = b0 + kHist;
		unsigned int *b2 = b1 + kHist;
		memset(b0,0,sizeof(unsigned int)*kHist*3);
		for (i = 0; i < set.size(); i++) 
		{
			unsigned int fi = flip((unsigned int *)&set[i].dist);
			++b0[_0(fi)]; ++b1[_1(fi)]; ++b2[_2(fi)];
		}
		{
			unsigned int sum0 = 0, sum1 = 0, sum2 = 0;
			for (i = 0; i < kHist; i++) 
			{
				b0[kHist-1] = b0[i] + sum0; b0[i] = sum0 - 1; sum0 = b0[kHist-1];
				b1[kHist-1] = b1[i] + sum1; b1[i] = sum1 - 1; sum1 = b1[kHist-1];
				b2[kHist-1] = b2[i] + sum2; b2[i] = sum2 - 1; sum2 = b2[kHist-1];
			}
		}
		for (i = 0; i < set.size(); i++) tmp[++b0[_0(flip((unsigned int *)&set[i].dist))]] = set[i];
		for (i = 0; i < set.size(); i++) set[++b1[_1(flip((unsigned int *)&tmp[i].dist))]] = tmp[i];
		for (i = 0; i < set.size(); i++) tmp[++b2[_2(flip((unsigned int *)&set[i].dist))]] = set[i];
		for (i = 0; i < set.size(); i++) set[i] = tmp[set.size()-1-i];
	}
public:
	
	volumetric(Mesh * _m)
	{
		m = _m;
		
		points.resize(m->NumberOfCells());
		int q = 0;
		for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); ++it)
		{
			Storage::real cnt[3], cntf[3];
			it->Centroid(cnt);
			points[q].coords[0] = cnt[0];
			points[q].coords[1] = cnt[1];
			points[q].coords[2] = cnt[2];
			points[q].id = it->LocalID();
			points[q].diam = 0.f;
			ElementArray<Face> faces = it->getFaces();
			for(ElementArray<Face>::iterator f = faces.begin(); f != faces.end(); ++f)
			{
				f->Centroid(cntf);
				Storage::real d = sqrt((cnt[0]-cntf[0])*(cnt[0]-cntf[0])+(cnt[1]-cntf[1])*(cnt[1]-cntf[1])+(cnt[2]-cntf[2])*(cnt[2]-cntf[2]));
				if( points[q].diam < d ) points[q].diam = d;
			}
			++q;
		}
		/*
		points.reserve(m->NumberOfNodes()*20);
		int q = 0;
		for(Mesh::iteratorNode it = m->BeginNode(); it != m->EndNode(); ++it)
		{
			point_t pnt;
			Storage::real_array cnt = it->Coords();
			pnt.coords[0] = cnt[0];
			pnt.coords[1] = cnt[1];
			pnt.coords[2] = cnt[2];
			pnt.id = it->LocalID();
			points.push_back(pnt);
			
			ElementArray<Element> adj = it->getAdjElements(CELL);
			for(ElementArray<Element>::iterator jt = adj.begin(); jt != adj.end(); ++jt)
			{
				Storage::real cnt2[3];
				jt->Centroid(cnt2);
				const int ncoefs = 1;
				const Storage::real coefs[ncoefs] = {0.82};
				for(int j = 0; j < ncoefs; ++j)
				{
					pnt.coords[0] = cnt[0]*(1-coefs[j]) + cnt2[0]*coefs[j];
					pnt.coords[1] = cnt[1]*(1-coefs[j]) + cnt2[1]*coefs[j];
					pnt.coords[2] = cnt[2]*(1-coefs[j]) + cnt2[2]*coefs[j];
					pnt.id = it->LocalID();
					points.push_back(pnt);
				}
			}
			
		}
		*/
		printf("number of points %d\n",points.size());
	}
	void camera(double pos[3], int interactive)
	{
		if( interactive ) return;
		float posf[3];
		posf[0] = pos[0];
		posf[1] = pos[1];
		posf[2] = pos[2];
		for(int k = 0; k < points.size(); ++k)
		{
			points[k].dist = sqrtf(
				(posf[0]-points[k].coords[0])*(posf[0]-points[k].coords[0])+
				(posf[1]-points[k].coords[1])*(posf[1]-points[k].coords[1])+
				(posf[2]-points[k].coords[2])*(posf[2]-points[k].coords[2]));
		}
		double t = Timer();
		radix_sort_dist(points);
		//std::sort(points.rbegin(),points.rend());
		printf("Time to sort %lf\n",Timer()-t);
	}
	void draw(int interactive)
	{
		double origin[3], right[3], up[3];
		GLdouble modelview[16],projection[16];
		GLint viewport[4] = {0,0,1,1};
		glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
		glGetDoublev(GL_PROJECTION_MATRIX, projection);
		GLdouble outx, outy, outz;  // Var's to save the answer in
		gluUnProject(0.5, 0.5, 0.,
               modelview, projection, viewport,
               &outx, &outy, &outz);
		origin[0] = outx;
		origin[1] = outy;
		origin[2] = outz;
		gluUnProject(1.0, 0.5, 0.,
               modelview, projection, viewport,
               &outx, &outy, &outz);
		right[0] = outx;
		right[1] = outy;
		right[2] = outz;
		gluUnProject(0.5, 1.0, 0.,
               modelview, projection, viewport,
               &outx, &outy, &outz);
		up[0] = outx;
		up[1] = outy;
		up[2] = outz;
		right[0] -= origin[0];
		right[1] -= origin[1];
		right[2] -= origin[2];
		up[0] -= origin[0];
		up[1] -= origin[1];
		up[2] -= origin[2];
		double l = sqrt(right[0]*right[0]+right[1]*right[1]+right[2]*right[2]);
		if( l )
		{
			right[0] /= l;
			right[1] /= l;
			right[2] /= l;
		}
		l = sqrt(up[0]*up[0]+up[1]*up[1]+up[2]*up[2]);
		if( l )
		{
			up[0] /= l;
			up[1] /= l;
			up[2] /= l;
		}
		const float alpha = 0.0075f;
		const float mult = 2.5f;
		const float rmult = 0.7f;
		//glPointSize(5.0);
		glColor4f(0.5f,0.5f,0.5f,alpha);
		glEnable(GL_BLEND);
		//glBegin(GL_TRIANGLES);
		//glBegin(GL_QUADS);
		if( interactive )
		{
			for(int k = 0; k < points.size(); ++k) if( k % 100 == 0 )
			{
				if( visualization_tag.isValid() )
				{
					color_t c = CommonColorBar.pick_color(m->CellByLocalID(points[k].id)->RealDF(visualization_tag));
					c.a() = alpha;
					c.set_color();
				}
				
				glBegin(GL_TRIANGLE_FAN);
				glVertex3f(points[k].coords[0],points[k].coords[1],points[k].coords[2]);
				glVertex3f(points[k].coords[0]+(right[0]*rmult+up[0])*points[k].diam*mult,points[k].coords[1]+(right[1]*rmult+up[1])*points[k].diam*mult,points[k].coords[2]+(right[2]*rmult+up[2])*points[k].diam*mult);
				glVertex3f(points[k].coords[0]+(right[0]*rmult*2)*points[k].diam*mult,points[k].coords[1]+(right[1]*rmult*2)*points[k].diam*mult,points[k].coords[2]+(right[2]*rmult*2)*points[k].diam*mult);
				glVertex3f(points[k].coords[0]+(right[0]*rmult-up[0])*points[k].diam*mult,points[k].coords[1]+(right[1]*rmult-up[1])*points[k].diam*mult,points[k].coords[2]+(right[2]*rmult-up[2])*points[k].diam*mult);
				glVertex3f(points[k].coords[0]+(-right[0]*rmult-up[0])*points[k].diam*mult,points[k].coords[1]+(-right[1]*rmult-up[1])*points[k].diam*mult,points[k].coords[2]+(-right[2]*rmult-up[2])*points[k].diam*mult);
				glVertex3f(points[k].coords[0]+(-right[0]*rmult*2)*points[k].diam*mult,points[k].coords[1]+(-right[1]*rmult*2)*points[k].diam*mult,points[k].coords[2]+(-right[2]*rmult*2)*points[k].diam*mult);
				glVertex3f(points[k].coords[0]+(-right[0]*rmult+up[0])*points[k].diam*mult,points[k].coords[1]+(-right[1]*rmult+up[1])*points[k].diam*mult,points[k].coords[2]+(-right[2]*rmult+up[2])*points[k].diam*mult);
				glVertex3f(points[k].coords[0]+(right[0]*rmult+up[0])*points[k].diam*mult,points[k].coords[1]+(right[1]*rmult+up[1])*points[k].diam*mult,points[k].coords[2]+(right[2]*rmult+up[2])*points[k].diam*mult);
				glEnd();
				
				/*
				glVertex3f(points[k].coords[0]+(right[0]+up[0])*points[k].diam*mult,points[k].coords[1]+(right[1]+up[1])*points[k].diam*mult,points[k].coords[2]+(right[2]+up[2])*points[k].diam*mult);
				glVertex3f(points[k].coords[0]+(-up[0]*2)*points[k].diam*mult,points[k].coords[1]+(-up[1]*2)*points[k].diam*mult,points[k].coords[2]+(-up[2]*2)*points[k].diam*mult);
				glVertex3f(points[k].coords[0]+(-right[0]+up[0])*points[k].diam*mult,points[k].coords[1]+(-right[1]+up[1])*points[k].diam*mult,points[k].coords[2]+(-right[2]+up[2])*points[k].diam*mult);
				*/
				/*
				glVertex3f(points[k].coords[0]+(right[0]+up[0])*points[k].diam*mult,points[k].coords[1]+(right[1]+up[1])*points[k].diam*mult,points[k].coords[2]+(right[2]+up[2])*points[k].diam*mult);
				glVertex3f(points[k].coords[0]+(right[0]-up[0])*points[k].diam*mult,points[k].coords[1]+(right[1]-up[1])*points[k].diam*mult,points[k].coords[2]+(right[2]-up[2])*points[k].diam*mult);
				glVertex3f(points[k].coords[0]+(-right[0]-up[0])*points[k].diam*mult,points[k].coords[1]+(-right[1]-up[1])*points[k].diam*mult,points[k].coords[2]+(-right[2]-up[2])*points[k].diam*mult);
				glVertex3f(points[k].coords[0]+(-right[0]+up[0])*points[k].diam*mult,points[k].coords[1]+(-right[1]+up[1])*points[k].diam*mult,points[k].coords[2]+(-right[2]+up[2])*points[k].diam*mult);
				*/
				//glVertex3f(points[k].coords[0]+right[0]*points[k].diam*mult,points[k].coords[1]+right[1]*points[k].diam*mult,points[k].coords[2]+right[2]*points[k].diam*mult);
				//glVertex3f(points[k].coords[0]+up[0]*points[k].diam*mult,points[k].coords[1]+up[1]*points[k].diam*mult,points[k].coords[2]+up[2]*points[k].diam*mult);
				//glVertex3f(points[k].coords[0]-right[0]*points[k].diam*mult,points[k].coords[1]-right[1]*points[k].diam*mult,points[k].coords[2]-right[2]*points[k].diam*mult);
				//glVertex3f(points[k].coords[0]-up[0]*points[k].diam*mult,points[k].coords[1]-up[1]*points[k].diam*mult,points[k].coords[2]-up[2]*points[k].diam*mult);
			}
		}
		else
		for(int k = 0; k < points.size(); ++k)
		{
			if( visualization_tag.isValid() )
			{
				color_t c = CommonColorBar.pick_color(m->CellByLocalID(points[k].id)->RealDF(visualization_tag));
				c.a() = alpha;
				c.set_color();
			}
			
			glBegin(GL_TRIANGLE_FAN);
			glVertex3f(points[k].coords[0],points[k].coords[1],points[k].coords[2]);
			glVertex3f(points[k].coords[0]+(right[0]+up[0])*points[k].diam*mult,points[k].coords[1]+(right[1]+up[1])*points[k].diam*mult,points[k].coords[2]+(right[2]+up[2])*points[k].diam*mult);
			glVertex3f(points[k].coords[0]+(right[0]*2)*points[k].diam*mult,points[k].coords[1]+(right[1]*2)*points[k].diam*mult,points[k].coords[2]+(right[2]*2)*points[k].diam*mult);
			glVertex3f(points[k].coords[0]+(right[0]-up[0])*points[k].diam*mult,points[k].coords[1]+(right[1]-up[1])*points[k].diam*mult,points[k].coords[2]+(right[2]-up[2])*points[k].diam*mult);
			glVertex3f(points[k].coords[0]+(-right[0]-up[0])*points[k].diam*mult,points[k].coords[1]+(-right[1]-up[1])*points[k].diam*mult,points[k].coords[2]+(-right[2]-up[2])*points[k].diam*mult);
			glVertex3f(points[k].coords[0]+(-right[0]*2)*points[k].diam*mult,points[k].coords[1]+(-right[1]*2)*points[k].diam*mult,points[k].coords[2]+(-right[2]*2)*points[k].diam*mult);
			glVertex3f(points[k].coords[0]+(-right[0]+up[0])*points[k].diam*mult,points[k].coords[1]+(-right[1]+up[1])*points[k].diam*mult,points[k].coords[2]+(-right[2]+up[2])*points[k].diam*mult);
			glVertex3f(points[k].coords[0]+(right[0]+up[0])*points[k].diam*mult,points[k].coords[1]+(right[1]+up[1])*points[k].diam*mult,points[k].coords[2]+(right[2]+up[2])*points[k].diam*mult);
			glEnd();	
			
			/*
			glVertex3f(points[k].coords[0]+(right[0]+up[0])*points[k].diam*mult,points[k].coords[1]+(right[1]+up[1])*points[k].diam*mult,points[k].coords[2]+(right[2]+up[2])*points[k].diam*mult);
			glVertex3f(points[k].coords[0]+(-up[0]*2)*points[k].diam*mult,points[k].coords[1]+(-up[1]*2)*points[k].diam*mult,points[k].coords[2]+(-up[2]*2)*points[k].diam*mult);
			glVertex3f(points[k].coords[0]+(-right[0]+up[0])*points[k].diam*mult,points[k].coords[1]+(-right[1]+up[1])*points[k].diam*mult,points[k].coords[2]+(-right[2]+up[2])*points[k].diam*mult);
			*/
			/*
			glVertex3f(points[k].coords[0]+(right[0]+up[0])*points[k].diam*mult,points[k].coords[1]+(right[1]+up[1])*points[k].diam*mult,points[k].coords[2]+(right[2]+up[2])*points[k].diam*mult);
			glVertex3f(points[k].coords[0]+(right[0]-up[0])*points[k].diam*mult,points[k].coords[1]+(right[1]-up[1])*points[k].diam*mult,points[k].coords[2]+(right[2]-up[2])*points[k].diam*mult);
			glVertex3f(points[k].coords[0]+(-right[0]-up[0])*points[k].diam*mult,points[k].coords[1]+(-right[1]-up[1])*points[k].diam*mult,points[k].coords[2]+(-right[2]-up[2])*points[k].diam*mult);
			glVertex3f(points[k].coords[0]+(-right[0]+up[0])*points[k].diam*mult,points[k].coords[1]+(-right[1]+up[1])*points[k].diam*mult,points[k].coords[2]+(-right[2]+up[2])*points[k].diam*mult);
			*/
			//glVertex3f(points[k].coords[0]+right[0]*points[k].diam*mult,points[k].coords[1]+right[1]*points[k].diam*mult,points[k].coords[2]+right[2]*points[k].diam*mult);
			//glVertex3f(points[k].coords[0]+up[0]*points[k].diam*mult,points[k].coords[1]+up[1]*points[k].diam*mult,points[k].coords[2]+up[2]*points[k].diam*mult);
			//glVertex3f(points[k].coords[0]-right[0]*points[k].diam*mult,points[k].coords[1]-right[1]*points[k].diam*mult,points[k].coords[2]-right[2]*points[k].diam*mult);
			//glVertex3f(points[k].coords[0]-up[0]*points[k].diam*mult,points[k].coords[1]-up[1]*points[k].diam*mult,points[k].coords[2]-up[2]*points[k].diam*mult);
		}
		//glEnd();
		glDisable(GL_BLEND);
		//glPointSize(1.0);
	}
} * CommonVolumetricView;


/*
class volumetric2
{
	std::vector<face2gl> faces;
	Mesh * m;
public:
	volumetric2(Mesh * _m)
	{
		m = _m;
		
		faces.reserve(m->NumberOfFaces());
		int q = 0;
		INMOST_DATA_ENUM_TYPE pace = std::max<INMOST_DATA_ENUM_TYPE>(1,std::min<INMOST_DATA_ENUM_TYPE>(15,(unsigned)m->NumberOfFaces()/100));
		for(Mesh::iteratorFace it = m->BeginFace(); it != m->EndFace(); ++it)
		{
			faces.push_back(DrawFace(it->self()));
			if( q%pace == 0 ) faces.back().set_flag(true);
			q++;
		}
		printf("number of faces %d\n",faces.size());
	}
	void camera(double pos[3], int interactive)
	{
		if( interactive ) return;
		for(int k = 0; k < faces.size(); ++k)
			faces[k].compute_dist(pos);
		//face2gl::radix_sort_dist(faces);
		std::sort(faces.rbegin(),faces.rend());
	}
	void draw(int interactive)
	{
		if( interactive ) draw_faces_interactive_alpha(faces,0.05);
		else draw_faces_alpha(faces,0.05);
	}
}* CommonVolumetricView;
*/
Kirill Terekhov's avatar
Kirill Terekhov committed
886
887
888
889
890
class kdtree
{
	int marked;
	struct entry
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
891
		HandleType e;
Kirill Terekhov's avatar
Kirill Terekhov committed
892
893
894
895
896
897
898
899
900
901
		float xyz[3];
		struct entry & operator =(const struct entry & other)
		{
			e = other.e;
			xyz[0] = other.xyz[0];
			xyz[1] = other.xyz[1];
			xyz[2] = other.xyz[2];
			return *this;
		}
	} * set;
Kirill Terekhov's avatar
Kirill Terekhov committed
902
	Mesh * m;
Kirill Terekhov's avatar
Kirill Terekhov committed
903
	INMOST_DATA_ENUM_TYPE size;
Kirill Terekhov's avatar
Kirill Terekhov committed
904
	float bbox[6];
Kirill Terekhov's avatar
Kirill Terekhov committed
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
	kdtree * children;
	static int cmpElements0(const void * a,const void * b) 
	{
		const entry * ea = ((const entry *)a);
		const entry * eb = ((const entry *)b);
		float ad = ea->xyz[0];
		float bd = eb->xyz[0];
		return (ad > bd) - (ad < bd);
	}
	static int cmpElements1(const void * a,const void * b) 
	{
		const entry * ea = ((const entry *)a);
		const entry * eb = ((const entry *)b);
		float ad = ea->xyz[1];
		float bd = eb->xyz[1];
		return (ad > bd) - (ad < bd);
	}
	static int cmpElements2(const void * a,const void * b) 
	{
		const entry * ea = ((const entry *)a);
		const entry * eb = ((const entry *)b);
		float ad = ea->xyz[2];
		float bd = eb->xyz[2];
		return (ad > bd) - (ad < bd);
	}
	void radix_sort(int dim, struct entry * temp)
	{
		unsigned int i;
		const unsigned int kHist = 2048;
		unsigned int  b0[kHist * 3];
		unsigned int *b1 = b0 + kHist;
		unsigned int *b2 = b1 + kHist;
		memset(b0,0,sizeof(unsigned int)*kHist*3);
		for (i = 0; i < size; i++) 
		{
			unsigned int fi = flip((unsigned int *)&set[i].xyz[dim]);
			++b0[_0(fi)]; ++b1[_1(fi)]; ++b2[_2(fi)];
		}
		{
			unsigned int sum0 = 0, sum1 = 0, sum2 = 0;
			for (i = 0; i < kHist; i++) 
			{
				b0[kHist-1] = b0[i] + sum0; b0[i] = sum0 - 1; sum0 = b0[kHist-1];
				b1[kHist-1] = b1[i] + sum1; b1[i] = sum1 - 1; sum1 = b1[kHist-1];
				b2[kHist-1] = b2[i] + sum2; b2[i] = sum2 - 1; sum2 = b2[kHist-1];
			}
		}
		for (i = 0; i < size; i++) temp[++b0[_0(flip((unsigned int *)&set[i].xyz[dim]))]] = set[i];
		for (i = 0; i < size; i++) set[++b1[_1(flip((unsigned int *)&temp[i].xyz[dim]))]] = temp[i];
		for (i = 0; i < size; i++) temp[++b2[_2(flip((unsigned int *)&set[i].xyz[dim]))]] = set[i];
		for (i = 0; i < size; i++) set[i] = temp[i];
	}
	void kdtree_build(int dim, int & done, int total, struct entry * temp)
	{
		if( size > 1 )
		{
			if( size > 128 ) radix_sort(dim,temp); else 
			switch(dim)
			{
			case 0: qsort(set,size,sizeof(entry),cmpElements0);break;
			case 1: qsort(set,size,sizeof(entry),cmpElements1);break;
			case 2: qsort(set,size,sizeof(entry),cmpElements2);break;
			}
			children = static_cast<kdtree *>(malloc(sizeof(kdtree)*2));//new kdtree[2];
			children[0].marked = 0;
			children[0].children = NULL;
			children[0].set = set;
			children[0].size = size/2;
Kirill Terekhov's avatar
Kirill Terekhov committed
973
			children[0].m = m;
Kirill Terekhov's avatar
Kirill Terekhov committed
974
975
976
977
			children[1].marked = 0;
			children[1].children = NULL;
			children[1].set = set+size/2;
			children[1].size = size - size/2;
Kirill Terekhov's avatar
Kirill Terekhov committed
978
			children[1].m = m;
Kirill Terekhov's avatar
Kirill Terekhov committed
979
980
981
982
983
984
985
986
987
988
989
			children[0].kdtree_build((dim+1)%3,done,total,temp);
			children[1].kdtree_build((dim+1)%3,done,total,temp);
			for(int k = 0; k < 3; k++)
			{
				bbox[0+2*k] = std::min(children[0].bbox[0+2*k],children[1].bbox[0+2*k]);
				bbox[1+2*k] = std::max(children[0].bbox[1+2*k],children[1].bbox[1+2*k]);
			}
		}
		else 
		{
			assert(size == 1);
Kirill Terekhov's avatar
Kirill Terekhov committed
990
			if( GetHandleElementType(set[0].e) == EDGE )
Kirill Terekhov's avatar
Kirill Terekhov committed
991
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
992
993
				Storage::real_array n1 = Edge(m,set[0].e)->getBeg()->Coords();
				Storage::real_array n2 = Edge(m,set[0].e)->getEnd()->Coords();
Kirill Terekhov's avatar
Kirill Terekhov committed
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
				for(int k = 0; k < 3; k++)
				{
					bbox[0+2*k] = std::min(n1[k],n2[k]);
					bbox[1+2*k] = std::max(n1[k],n2[k]);
				}
				done++;
				if( done%150 == 0 )
				{
					printf("%3.1f%%\r",(done*100.0)/(total*1.0));
					fflush(stdout);
				}
			}
			else
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1008
				ElementArray<Node> nodes = Element(m,set[0].e)->getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
1009
1010
				bbox[0] = bbox[2] = bbox[4] = 1.0e20;
				bbox[1] = bbox[3] = bbox[5] = -1.0e20;
Kirill Terekhov's avatar
Kirill Terekhov committed
1011
				for(INMOST_DATA_ENUM_TYPE k = 0; k < nodes.size(); ++k)
Kirill Terekhov's avatar
Kirill Terekhov committed
1012
1013
				{
					Storage::real_array coords = nodes[k].Coords();
Kirill Terekhov's avatar
Kirill Terekhov committed
1014
					for(INMOST_DATA_ENUM_TYPE q = 0; q < 3; q++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1015
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1016
1017
						bbox[q*2+0] = std::min<float>(bbox[q*2+0],coords[q]);
						bbox[q*2+1] = std::max<float>(bbox[q*2+1],coords[q]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1018
1019
1020
1021
1022
					}
				}
			}
		}
	}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1023
	kdtree() : marked(0), set(NULL), size(0), children(NULL) {}
Kirill Terekhov's avatar
Kirill Terekhov committed
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
	inline int plane_bbox(double p[3], double n[3])
	{
		Storage::real pv[3], nv[3];
		for(int k = 0; k < 3; ++k)
		{
			if( n[k] >= 0 ) 
			{ 
				pv[k] = bbox[1+2*k]; //max
				nv[k] = bbox[0+2*k]; //min
			} 
			else 
			{ 
				pv[k] = bbox[0+2*k]; //min
				nv[k] = bbox[1+2*k]; //max
			}
		}
		Storage::real pvD, nvD;
		pvD = n[0]*(pv[0]-p[0])+n[1]*(pv[1]-p[1])+n[2]*(pv[2]-p[2]);
		nvD = n[0]*(nv[0]-p[0])+n[1]*(nv[1]-p[1])+n[2]*(nv[2]-p[2]);
		if( nvD*pvD <= 0.0 )
			return 2;
		else if( nvD < 0.0 )
			return 1;
		else return 0;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1049
	bool sub_intersect_plane_edge(Tag clip_point, Tag clip_state, ElementArray<Cell> & cells, MarkerType mrk, double p[3], double n[3])
Kirill Terekhov's avatar
Kirill Terekhov committed
1050
1051
1052
	{
		if( size == 1 )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1053
1054
1055
1056
1057
1058
			assert( GetHandleElementType(set[0].e) == EDGE );
			Edge ee = Edge(m,set[0].e);
			Storage::real_array sp0 = ee->getBeg()->Coords();
			Storage::real_array sp1 = ee->getEnd()->Coords();
			Storage::integer & clip = m->IntegerDF(set[0].e,clip_state);
			clip = clip_plane_edge(&sp0[0],&sp1[0],p,n,&ee->RealArrayDF(clip_point)[0]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1059
1060
			if( clip )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1061
				ElementArray<Cell> ecells = ee->getCells();
Kirill Terekhov's avatar
Kirill Terekhov committed
1062
				for(INMOST_DATA_ENUM_TYPE k = 0; k < ecells.size(); ++k) if( !ecells[k].GetMarker(mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
1063
1064
				{
					ecells[k].SetMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1065
					cells.push_back(ecells[k]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
				}
				marked = 1;
			}
		}
		else if( plane_bbox(p,n) == 2 )
		{
			bool test1 = children[0].sub_intersect_plane_edge(clip_point,clip_state,cells,mrk,p,n);
			bool test2 = children[1].sub_intersect_plane_edge(clip_point,clip_state,cells,mrk,p,n);
			if( test1 || test2 ) marked = 1;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1076
		return marked != 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1077
1078
1079
1080
1081
1082
	}
	void sub_intersect_plane_faces(Tag clip_state, double p[3], double n[3])
	{
		if( size == 1 )
		{
			Storage::integer state;
Kirill Terekhov's avatar
Kirill Terekhov committed
1083
1084
1085
			Element ee(m,set[0].e);
			assert( ee->GetElementDimension() == 2 );
			ElementArray<Node> nodes = ee->getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
1086
1087
1088
			Storage::real_array coords = nodes[0].Coords();
			Storage::real dot0 = n[0]*(coords[0]-p[0])+n[1]*(coords[1]-p[1])+n[2]*(coords[2]-p[2]);
			if( dot0 <= 0.0 ) state = CLIP_FACE_INSIDE; else state = CLIP_FACE_OUTSIDE;
Kirill Terekhov's avatar
Kirill Terekhov committed
1089
			for(INMOST_DATA_ENUM_TYPE k = 1; k < nodes.size(); k++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1090
1091
1092
1093
1094
1095
1096
1097
1098
			{
				coords = nodes[k].Coords();
				Storage::real dot = n[0]*(coords[0]-p[0])+n[1]*(coords[1]-p[1])+n[2]*(coords[2]-p[2]);
				if( dot*dot0 <= 0.0 ) 
				{
					state = CLIP_FACE_INTERSECT;
					break;
				}
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1099
			m->IntegerDF(set[0].e,clip_state) = state;
Kirill Terekhov's avatar
Kirill Terekhov committed
1100
1101
1102
1103
1104
1105
		}
		else
		{
			marked = plane_bbox(p,n);
			if( marked == 0 )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1106
				for(INMOST_DATA_ENUM_TYPE k = 0; k < size; k++) m->IntegerDF(set[k].e,clip_state) = CLIP_FACE_OUTSIDE;
Kirill Terekhov's avatar
Kirill Terekhov committed
1107
1108
1109
			}
			else if( marked == 1 )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1110
				for(INMOST_DATA_ENUM_TYPE k = 0; k < size; k++) m->IntegerDF(set[k].e,clip_state) = CLIP_FACE_INSIDE;
Kirill Terekhov's avatar
Kirill Terekhov committed
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
			}
			else
			{
				children[0].sub_intersect_plane_faces(clip_state,p,n);
				children[1].sub_intersect_plane_faces(clip_state,p,n);
			}
		}
	}
	void unmark_old_edges(Tag clip_state)
	{
		if( size == 1 )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1123
			assert(GetHandleElementType(set[0].e) == EDGE);
Kirill Terekhov's avatar
Kirill Terekhov committed
1124
			marked = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1125
1126
1127
1128
			if( GetHandleElementType(set[0].e) == EDGE )
				m->IntegerDF(set[0].e,clip_state) = CLIP_NONE;
			else if( GetHandleElementType(set[0].e) == FACE )
				m->IntegerDF(set[0].e,clip_state) = CLIP_FACE_NONE;
Kirill Terekhov's avatar
Kirill Terekhov committed
1129
1130
1131
1132
1133
1134
1135
1136
1137
		}
		else if( children )
		{
			if(children[0].marked) {children[0].unmark_old_edges(clip_state); marked = 0;}
			if(children[1].marked) {children[1].unmark_old_edges(clip_state); marked = 0;}
		}
	}
	void clear_children() { if( children ) {children[0].clear_children(); children[1].clear_children(); free(children);}}
public:
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1138
	kdtree(Mesh * m) :  marked(0),m(m),children(NULL)
Kirill Terekhov's avatar
Kirill Terekhov committed
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
	{
		double tt;
		size = m->NumberOfEdges();
		assert(size > 1);
		set = new entry[size];
		INMOST_DATA_ENUM_TYPE k = 0;
		tt = Timer();
		printf("Prepearing edge set.\n");
		for(Mesh::iteratorEdge it = m->BeginEdge(); it != m->EndEdge(); ++it) 
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1149
			set[k].e = *it;
Kirill Terekhov's avatar
Kirill Terekhov committed
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
			set[k].xyz[0] = (it->getBeg()->Coords()[0] + it->getEnd()->Coords()[0])*0.5;
			set[k].xyz[1] = (it->getBeg()->Coords()[1] + it->getEnd()->Coords()[1])*0.5;
			set[k].xyz[2] = (it->getBeg()->Coords()[2] + it->getEnd()->Coords()[2])*0.5;
			k++;
			if( k%150 == 0 ) 
			{
				printf("%3.1f%%\r",(k*100.0)/(size*1.0));
				fflush(stdout);
			}
		}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1160
		printf("Done. Time %lg\n",Timer()-tt);
Kirill Terekhov's avatar
Kirill Terekhov committed
1161
1162
1163
1164
1165
1166
		int done = 0, total = size;
		printf("Building KD-tree.\n");
		tt = Timer();
		struct entry *  temp = new entry[size];
		kdtree_build(0,done,total,temp);
		delete [] temp;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1167
		printf("Done. Time %lg\n",Timer()-tt);
Kirill Terekhov's avatar
Kirill Terekhov committed
1168
1169
1170
1171
1172
1173
		for(int k = 0; k < 3; k++)
		{
			bbox[0+2*k] = std::min(children[0].bbox[0+2*k],children[1].bbox[0+2*k]);
			bbox[1+2*k] = std::max(children[0].bbox[1+2*k],children[1].bbox[1+2*k]);
		}
	}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1174
	kdtree(Mesh * m, HandleType * eset, INMOST_DATA_ENUM_TYPE size) : marked(0), m(m),size(size),children(NULL)
Kirill Terekhov's avatar
Kirill Terekhov committed
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
	{
		double tt;
		assert(size > 1);
		set = new entry[size];
		tt = Timer();
		printf("Prepearing elements set.\n");
		for(INMOST_DATA_ENUM_TYPE k = 0; k < size; k++) 
		{
			set[k].e = eset[k];
			Storage::real cnt[3];
Kirill Terekhov's avatar
Kirill Terekhov committed
1185
			m->GetGeometricData(set[k].e,CENTROID,cnt);
Kirill Terekhov's avatar
Kirill Terekhov committed
1186
1187
1188
1189
1190
1191
1192
1193
1194
			set[k].xyz[0] = cnt[0];
			set[k].xyz[1] = cnt[1];
			set[k].xyz[2] = cnt[2];
			if( k%150 == 0 ) 
			{
				printf("%3.1f%%\r",(k*100.0)/(size*1.0));
				fflush(stdout);
			}
		}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1195
		printf("Done. Time %lg\n",Timer()-tt);
Kirill Terekhov's avatar
Kirill Terekhov committed
1196
1197
1198
1199
1200
1201
		int done = 0, total = size;
		printf("Building KD-tree.\n");
		tt = Timer();
		struct entry *  temp = new entry[size];
		kdtree_build(0,done,total,temp);
		delete [] temp;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1202
		printf("Done. Time %lg\n",Timer()-tt);
Kirill Terekhov's avatar
Kirill Terekhov committed
1203
1204
1205
1206
1207
1208
		for(int k = 0; k < 3; k++)
		{
			bbox[0+2*k] = std::min(children[0].bbox[0+2*k],children[1].bbox[0+2*k]);
			bbox[1+2*k] = std::max(children[0].bbox[1+2*k],children[1].bbox[1+2*k]);
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1209
	void intersect_plane_edge(Tag clip_point, Tag clip_state, ElementArray<Cell> & cells, MarkerType mark_cells, double p[3], double n[3])
Kirill Terekhov's avatar
Kirill Terekhov committed
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
	{
		if( marked ) 
		{
			unmark_old_edges(clip_state);
			cells.clear();
		}
		sub_intersect_plane_edge(clip_point, clip_state, cells,mark_cells,p,n);
	}
	void intersect_plane_face(Tag clip_state, double p[3], double n[3])
	{
		sub_intersect_plane_faces(clip_state, p,n);
	}
	~kdtree()
	{
		delete [] set;
		clear_children();
	}
};

class clipper
{
	struct edge_point
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1233
		double val;
Kirill Terekhov's avatar
Kirill Terekhov committed
1234
1235
1236
		Storage::real xyz[3];
		Storage::integer edge;
		edge_point(){}
Kirill Terekhov's avatar
Kirill Terekhov committed
1237
		edge_point(Storage::real _xyz[3], Storage::integer n, float v)
Kirill Terekhov's avatar
Kirill Terekhov committed
1238
1239
1240
1241
1242
		{
			xyz[0] = _xyz[0];
			xyz[1] = _xyz[1];
			xyz[2] = _xyz[2];
			edge = n;
Kirill Terekhov's avatar
Kirill Terekhov committed
1243
			val = v;
Kirill Terekhov's avatar
Kirill Terekhov committed
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
		}
		bool operator ==(const edge_point& b) const
		{
			Storage::real temp = 0.0;
			for(int k = 0; k < 3; k++) temp += (xyz[k]-b.xyz[k])*(xyz[k]-b.xyz[k]);
			if( temp < 1.0e-8 ) return true; else return false;
		}
		bool operator !=(const edge_point& b) const {return !(operator ==(b));}
		void print() {printf("%g %g %g e %d\n",xyz[0],xyz[1],xyz[2],edge);}
	};
	Tag clip_point, clip_state;
	kdtree * tree;
Kirill Terekhov's avatar
Kirill Terekhov committed
1256
	Tag clips, clipsv;
Kirill Terekhov's avatar
Kirill Terekhov committed
1257
	MarkerType marker;
Kirill Terekhov's avatar
Kirill Terekhov committed
1258
	ElementArray<Cell> cells;
Kirill Terekhov's avatar
Kirill Terekhov committed
1259
1260
1261
1262
1263
	Mesh * mm;
public:
	~clipper() 
	{
		delete tree; 
Kirill Terekhov's avatar
Kirill Terekhov committed
1264
		for(INMOST_DATA_ENUM_TYPE k = 0; k < cells.size(); k++) cells[k]->RemMarker(marker);
Kirill Terekhov's avatar
Kirill Terekhov committed
1265
1266
		mm->ReleaseMarker(marker); 
		mm->DeleteTag(clips); 
Kirill Terekhov's avatar
Kirill Terekhov committed
1267
		mm->DeleteTag(clipsv); 
Kirill Terekhov's avatar
Kirill Terekhov committed
1268
1269
1270
1271
1272
1273
		mm->DeleteTag(clip_point); 
		mm->DeleteTag(clip_state);
	}
	clipper(Mesh * m)
	{
		mm = m;
Kirill Terekhov's avatar
Kirill Terekhov committed
1274
		cells.SetMeshLink(mm);
Kirill Terekhov's avatar
Kirill Terekhov committed
1275
1276
1277
		tree = new kdtree(m);
		marker = m->CreateMarker();
		clips = m->CreateTag("CLIPS",DATA_REAL,CELL,CELL);
Kirill Terekhov's avatar
Kirill Terekhov committed
1278
		clipsv = m->CreateTag("CLIPS_VAL",DATA_REAL,CELL,CELL);
Kirill Terekhov's avatar
Kirill Terekhov committed
1279
1280
1281
		clip_point = m->CreateTag("CLIP_POINT",DATA_REAL,EDGE,NONE,3);
		clip_state = m->CreateTag("CLIP_STATE",DATA_INTEGER,EDGE,NONE,1);
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
	double compute_value(Edge e, Storage::real * pnt)
	{
		if( visualization_tag.isValid() )
		{
			Storage::real_array c1 = e->getBeg()->Coords();
			Storage::real_array c2 = e->getEnd()->Coords();
			Storage::real d1,d2,t;
			d1 = sqrt((pnt[0]-c1[0])*(pnt[0]-c1[0])+(pnt[1]-c1[1])*(pnt[1]-c1[1])+(pnt[2]-c1[2])*(pnt[2]-c1[2]));
			d2 = sqrt((c2[0]-c1[0])*(c2[0]-c1[0])+(c2[1]-c1[1])*(c2[1]-c1[1])+(c2[2]-c1[2])*(c2[2]-c1[2]));
			t = d1/d2; //(pnt == c2, t = 1 : pnt == c1, t = 0)
			return e->getBeg()->RealDF(visualization_tag)*(1-t)+e->getEnd()->RealDF(visualization_tag)*t;
		}
		else return 0.f;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1296
1297
1298
1299
	void clip_plane(Storage::real p[3], Storage::real n[3])
	{
		const bool print = false;

Kirill Terekhov's avatar
Kirill Terekhov committed
1300
		for(INMOST_DATA_ENUM_TYPE k = 0; k < cells.size(); ++k) 
Kirill Terekhov's avatar
Kirill Terekhov committed
1301
1302
1303
			if( cells[k]->GetMarker(marker) )
			{
				cells[k]->RealArray(clips).clear();
Kirill Terekhov's avatar
Kirill Terekhov committed
1304
				cells[k]->RealArray(clipsv).clear();
Kirill Terekhov's avatar
Kirill Terekhov committed
1305
1306
1307
1308
1309
				cells[k]->RemMarker(marker);
			}
		tree->intersect_plane_edge(clip_point,clip_state,cells,marker,p,n);
		dynarray<edge_point,128> clipcoords, loopcoords;
		std::vector<bool> closed;
Kirill Terekhov's avatar
Kirill Terekhov committed
1310
		for(INMOST_DATA_ENUM_TYPE k = 0; k < cells.size(); ++k)
Kirill Terekhov's avatar
Kirill Terekhov committed
1311
1312
1313
1314
1315
1316
		{
			//assuming faces are convex we will have at most one clipping edge per polygon
			//otherwise every pair of clipping nodes forming edge should appear consequently
			//as long as we go through face's edges in ordered way
			clipcoords.clear();
			//we will gather all the pairs of nodes, then form closed loop
Kirill Terekhov's avatar
Kirill Terekhov committed
1317
1318
			ElementArray<Face> faces = cells[k]->getFaces();
			Face full_face;
Kirill Terekhov's avatar
Kirill Terekhov committed
1319
			int ntotpoints = 0, ntotedges = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1320
			for(INMOST_DATA_ENUM_TYPE q = 0; q < faces.size(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
1321
1322
1323
			{
				int last_edge_type = CLIP_NONE;
				int nfulledges = 0, npoints = 0, nstartedge = ntotedges;
Kirill Terekhov's avatar
Kirill Terekhov committed
1324
				ElementArray<Edge> edges = faces[q].getEdges();
Kirill Terekhov's avatar
Kirill Terekhov committed
1325
				for(INMOST_DATA_ENUM_TYPE r = 0; r < edges.size(); ++r)
Kirill Terekhov's avatar
Kirill Terekhov committed
1326
1327
1328
1329
1330
				{
					Storage::integer state = edges[r].IntegerDF(clip_state);
					if( state == CLIP_FULL )
					{
						nfulledges++;
Kirill Terekhov's avatar
Kirill Terekhov committed
1331
1332
						edge_point n1 = edge_point(&edges[r].getBeg()->Coords()[0],ntotedges,visualization_tag.isValid() ? edges[r].getBeg()->RealDF(visualization_tag) : 0.f);
						edge_point n2 = edge_point(&edges[r].getEnd()->Coords()[0],ntotedges,visualization_tag.isValid() ? edges[r].getEnd()->RealDF(visualization_tag) : 0.f);
Kirill Terekhov's avatar
Kirill Terekhov committed
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
						if( npoints % 2 == 0 ) //all privious edges are closed, just add this one
						{
							clipcoords.push_back(n1);
							clipcoords.push_back(n2);
							npoints+=2;
							ntotedges++;
							last_edge_type = CLIP_FULL;
						} 
						else if(n1 == clipcoords.back()) //this may be prolongation of one point that hit one edge
						{
							clipcoords.push_back(n2);
							npoints++;
							ntotedges++;
							last_edge_type = CLIP_FULL;
						}
						else if( n2 == clipcoords.back() )
						{
							clipcoords.push_back(n1);
							npoints++;
							ntotedges++;
							last_edge_type = CLIP_FULL;
						}
						else printf("%s:%d strange orphan node before me\n",__FILE__,__LINE__);
					}
					else if( state == CLIP_ENDP )
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1359
						edge_point n = edge_point(&edges[r].RealArrayDF(clip_point)[0],ntotedges,compute_value(edges[r],&edges[r].RealArrayDF(clip_point)[0]));
Kirill Terekhov's avatar
Kirill Terekhov committed
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
						bool add = true;
						if( last_edge_type == CLIP_ENDP )
						{
							if( n == clipcoords.back() )
								add = false;
						}
						else if( last_edge_type == CLIP_FULL )
						{
							if( n == clipcoords.back() || n == clipcoords[clipcoords.size()-2])
								add = false;
						}
						if( add ) //this one node should be prolongation of privious edge
						{
							if( print )
							{
								printf("added: ");
								n.print();
							}
							clipcoords.push_back(n);
							npoints++;
							if( npoints % 2 == 0 ) 
							{
								if( print ) printf("edge %d accepted\n",ntotedges);
								ntotedges++;
							}
							last_edge_type = CLIP_ENDP;
						}
						else if( print )
						{
							printf("ignored: ");
							n.print();
						}
					}
					else if( state == CLIP_NODE )
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1395
						edge_point n = edge_point(&edges[r].RealArrayDF(clip_point)[0],ntotedges,compute_value(edges[r],&edges[r].RealArrayDF(clip_point)[0]));
Kirill Terekhov's avatar
Kirill Terekhov committed
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
						if( print )
						{
							printf("added: ");
							n.print();
						}
						clipcoords.push_back(n);
						npoints++;
						if( npoints % 2 == 0 ) 
						{
							if( print ) printf("edge %d accepted\n",ntotedges);
							ntotedges++;
						}
						last_edge_type = CLIP_NODE;
					}
				}
				if( npoints % 2 != 0 ) 
				{
					if( print ) printf("edge %d not closed - remove\n",ntotedges);
					clipcoords.pop_back();
					npoints--;
					//printf("%s:%d this should not happen!\n",__FILE__,__LINE__);
				}
				
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1419
				if( nfulledges == static_cast<int>(edges.size()) )
Kirill Terekhov's avatar
Kirill Terekhov committed
1420
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1421
					full_face = faces[q];
Kirill Terekhov's avatar
Kirill Terekhov committed
1422
1423
1424
1425
1426
					break;
				}
				if( print )
				{
					printf("nodes on face %d\n",faces[q].LocalID());
Kirill Terekhov's avatar
Kirill Terekhov committed
1427
					for(int m = nstartedge*2; m < static_cast<int>(clipcoords.size()); m++) clipcoords[m].print();
Kirill Terekhov's avatar
Kirill Terekhov committed
1428
1429
1430
				}
				ntotpoints += npoints;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1431
			if( full_face.isValid() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1432
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1433
				ElementArray<Node> nodes = full_face->getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
1434
				Storage::real_array cl = cells[k]->RealArray(clips);
Kirill Terekhov's avatar
Kirill Terekhov committed
1435
				Storage::real_array clv = cells[k]->RealArray(clipsv);
Kirill Terekhov's avatar
Kirill Terekhov committed
1436
				cl.resize(static_cast<Storage::real_array::size_type>(3*nodes.size()));
Kirill Terekhov's avatar
Kirill Terekhov committed
1437
				clv.resize(static_cast<Storage::real_array::size_type>(nodes.size()));
Kirill Terekhov's avatar
Kirill Terekhov committed
1438
				for(INMOST_DATA_ENUM_TYPE r = 0; r < nodes.size(); r++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1439
1440
1441
1442
1443
				{
					Storage::real_array p = nodes[r].Coords();
					cl[0+3*r] = p[0];
					cl[1+3*r] = p[1];
					cl[2+3*r] = p[2];
Kirill Terekhov's avatar
Kirill Terekhov committed
1444
					clv[r] = visualization_tag.isValid() ? nodes[r].RealDF(visualization_tag) : 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1445
1446
1447
1448
1449
1450
1451
1452
				}
				cells[k]->SetMarker(marker);
			}
			else if( ntotedges > 2 )
			{
				if( print )
				{
					printf("coords on cell %d\n",cells[k]->LocalID());
Kirill Terekhov's avatar
Kirill Terekhov committed
1453
					for(int m = 0; m < static_cast<int>(clipcoords.size()); m++) clipcoords[m].print();
Kirill Terekhov's avatar
Kirill Terekhov committed
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
				}
				//Can make this faster using hash
				closed.resize(ntotedges);
				std::fill(closed.begin(),closed.end(),false);
				loopcoords.push_back(clipcoords[0]); //this is starting point
				loopcoords.push_back(clipcoords[1]); //this is next
				closed[0] = true;
				for(int r = 0; r < ntotedges-2; ++r) //we need to add this number of points
				{
					bool hit = false;
					for(int q = 0; q < ntotedges; ++q) if( !closed[q] )
					{
						//some end of q-th edge connects to current end point - connect it
						if( clipcoords[q*2+0] == loopcoords.back() )
						{
							loopcoords.push_back(clipcoords[q*2+1]);
							closed[q] = true;
							hit = true;
							break;
						}
						else if( clipcoords[q*2+1] == loopcoords.back() )
						{
							loopcoords.push_back(clipcoords[q*2+0]);
							closed[q] = true;
							hit = true;
							break;
						}
					}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1482
					if( !hit ) printf("%s:%d cannot find end for edge! total edges %d current loop size %ld\n",
Kirill Terekhov's avatar
Kirill Terekhov committed
1483
1484
1485
						__FILE__,__LINE__,ntotedges,loopcoords.size());
				}
				Storage::real_array cl = cells[k]->RealArray(clips);
Kirill Terekhov's avatar
Kirill Terekhov committed
1486
				Storage::real_array clv = cells[k]->RealArray(clipsv);
Kirill Terekhov's avatar
Kirill Terekhov committed
1487
				cl.resize(static_cast<Storage::real_array::size_type>(3*loopcoords.size()));
Kirill Terekhov's avatar
Kirill Terekhov committed
1488
				clv.resize(static_cast<Storage::real_array::size_type>(loopcoords.size()));
Kirill Terekhov's avatar
Kirill Terekhov committed
1489
				for(INMOST_DATA_ENUM_TYPE r = 0; r < loopcoords.size(); ++r)
Kirill Terekhov's avatar
Kirill Terekhov committed
1490
1491
1492
1493
				{
					cl[r*3+0] = loopcoords[r].xyz[0];
					cl[r*3+1] = loopcoords[r].xyz[1];
					cl[r*3+2] = loopcoords[r].xyz[2];
Kirill Terekhov's avatar
Kirill Terekhov committed
1494
					clv[r] = loopcoords[r].val;
Kirill Terekhov's avatar
Kirill Terekhov committed
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
				}

				loopcoords.clear();
				clipcoords.clear();

				cells[k]->SetMarker(marker);
			}
		}
	}
	void gen_clip(std::vector<face2gl> & out)
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1506
1507
		INMOST_DATA_ENUM_TYPE pace = std::max<INMOST_DATA_ENUM_TYPE>(1,std::min<INMOST_DATA_ENUM_TYPE>(15,size()/100));
		for(INMOST_DATA_ENUM_TYPE k = 0; k < cells.size(); k++) if( cells[k]->GetMarker(marker))
Kirill Terekhov's avatar
Kirill Terekhov committed
1508
1509
1510
1511
		{
			face2gl f;
			f.set_color(0.6,0.6,0.6,1);
			Storage::real_array cl = cells[k]->RealArray(clips);
Kirill Terekhov's avatar
Kirill Terekhov committed
1512
			Storage::real_array clv = cells[k]->RealArray(clipsv);
Kirill Terekhov's avatar
Kirill Terekhov committed
1513
			for(INMOST_DATA_ENUM_TYPE q = 0; q < cl.size(); q+=3) f.add_vert(&cl[q]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1514
1515
1516
1517
			if( visualization_tag.isValid() )
			{
				for(INMOST_DATA_ENUM_TYPE q = 0; q < clv.size(); q++) f.add_color(CommonColorBar.pick_color(clv[q]));
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1518
1519
1520
1521
1522
1523
			f.compute_center();
			f.set_elem(cells[k]->GetElementType(),cells[k]->LocalID());
			if( k%pace == 0 ) f.set_flag(true);
			out.push_back(f);
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1524
	void draw_clip(INMOST_DATA_ENUM_TYPE pace)
Kirill Terekhov's avatar
Kirill Terekhov committed
1525
1526
	{
		glBegin(GL_TRIANGLES);
Kirill Terekhov's avatar
Kirill Terekhov committed
1527
		for(INMOST_DATA_ENUM_TYPE k = 0; k < cells.size(); k+=pace) if( cells[k]->GetMarker(marker))
Kirill Terekhov's avatar
Kirill Terekhov committed
1528
1529
		{
			Storage::real_array cl = cells[k]->RealArray(clips);
Kirill Terekhov's avatar
Kirill Terekhov committed
1530
			Storage::real_array clv = cells[k]->RealArray(clipsv);
Kirill Terekhov's avatar
Kirill Terekhov committed
1531
			Storage::real cnt[3] = {0,0,0};
Kirill Terekhov's avatar
Kirill Terekhov committed
1532
			Storage::real cntv = 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1533
			for(INMOST_DATA_ENUM_TYPE q = 0; q < cl.size(); q+=3)