main.cpp 168 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
//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 
Kirill Terekhov's avatar
Kirill Terekhov committed
7
#include "inmost.h"
Kirill Terekhov's avatar
Kirill Terekhov committed
8
9
10
11
#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
#include <iomanip>
15
#include "clipboard.h"
Kirill Terekhov's avatar
Kirill Terekhov committed
16
17
18
19
20
21
22
23
24
25

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
26
void draw_screen();
Kirill Terekhov's avatar
Kirill Terekhov committed
27
28
void svg_draw(std::ostream & file);
void svg_line(std::ostream & file, double x1, double y1, double z1, double x2, double y2, double z2, double modelview[16], double projection[16], int viewport[4]);
Kirill Terekhov's avatar
Kirill Terekhov committed
29
30
31
32
33
34

using namespace INMOST;
Mesh * mesh;
int interactive = 0;
double zoom = 1;
int width = 800, height = 800;
Kirill Terekhov's avatar
Kirill Terekhov committed
35
36
37
double sleft = 1e20, sright = -1e20, sbottom = 1e20, stop = -1e20, sfar = -1e20, snear = 1e20;
double shift[3] = {0,0,0};
bool perspective = false;
38
int drawedges = 0, draw_orphan = true;
39
bool boundary = true, planecontrol = false, clipupdate = false, bndupdate = true, clipboxupdate = false, draw_volumetric = false, elevation = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
40
bool drawaxis = true, drawcolorbar = true;
41
Element disp_e;
Kirill Terekhov's avatar
Kirill Terekhov committed
42
Mesh::GeomParam table;
43
ElementArray<Element> orphans;
Kirill Terekhov's avatar
Kirill Terekhov committed
44
int is_material_defined = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
45

Kirill Terekhov's avatar
Kirill Terekhov committed
46
47
48
49
50
51
52
53
54
55
56
#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
57
ElementArray<Element> boundary_faces;
Kirill Terekhov's avatar
Kirill Terekhov committed
58
ElementArray<Edge> added_edges;
Kirill Terekhov's avatar
Kirill Terekhov committed
59
60
61
62
std::map<int,std::vector<HandleType> > material_faces;
std::map<int,double> material_x;
std::map<int,double> material_y;
std::map<int,double> material_z;
Kirill Terekhov's avatar
Kirill Terekhov committed
63
64
std::vector<double> harmonic_points, dual_harmonic_points, conormals;

65
66


Kirill Terekhov's avatar
Kirill Terekhov committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

static void GetBox(Element e, Storage::real min[3], Storage::real max[3])
{
  min[0] = min[1] = min[2] = 1.0e20;
  max[0] = max[1] = max[2] = -1.0e20;
	ElementArray<Node> nodes = e->getNodes();
	for (ElementArray<Node>::iterator it = nodes.begin(); it != nodes.end(); it++)
	{
		Storage::real_array c = it->Coords();
		for (int i = 0; i < (int)c.size(); i++) 
    {
			if (max[i] < c[i]) max[i] = c[i]; //max
			if (min[i] > c[i]) min[i] = c[i]; //min
		}
	}
  for(int i = 0; i < 3; ++i)
  {
    if( max[i] < min[i] )
    {
      max[i] = 0.0001;
      min[i] = -0.0001;
    }
    else if( max[i] == min[i] )
    {
      max[i] += 0.0001;
      min[i] += -0.0001;
    }
  }
}
Kirill Terekhov's avatar
Kirill Terekhov committed
96

Kirill Terekhov's avatar
Kirill Terekhov committed
97
98
99
double amplitude = 10;
double radius = 25;
char visualization_prompt[8192];
Kirill Terekhov's avatar
Kirill Terekhov committed
100
int visualization_prompt_active = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
101
102
Tag visualization_tag;
ElementType visualization_type;
103
bool visualization_smooth = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
104

Kirill Terekhov's avatar
Kirill Terekhov committed
105
106
107
108
void printtext(const char * fmt, ... )
{
	
	unsigned int i;
Kirill Terekhov's avatar
Kirill Terekhov committed
109
	char stext[4096];
Kirill Terekhov's avatar
Kirill Terekhov committed
110
111
112
113
114
115
116
117
118
119
120
121
	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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583

class coord
{
	double p[3];
public:
	coord() { p[0] = p[1] = p[2] = 0; }
  coord(double xyz[3]) {p[0] = xyz[0]; p[1] = xyz[1]; p[2] = xyz[2];}
	coord(double x, double y, double z) {p[0] = x; p[1] = y; p[2] = z;}
	coord(const coord & other) {p[0] = other.p[0]; p[1] = other.p[1]; p[2] = other.p[2];}
	coord & operator = (coord const & other) {p[0] = other.p[0]; p[1] = other.p[1]; p[2] = other.p[2]; return *this;}
  coord & operator +=(const coord & other) {p[0] += other.p[0]; p[1] += other.p[1]; p[2] += other.p[2]; return *this;}
  coord & operator -=(const coord & other) {p[0] -= other.p[0]; p[1] -= other.p[1]; p[2] -= other.p[2]; return *this;}
  coord & operator *=(const coord & other) 
  {
    double tmp[3] = {p[1]*other.p[2] - p[2]*other.p[1], p[2]*other.p[0] - p[0]*other.p[2], p[0]*other.p[1] - p[1]*other.p[0]};
    p[0] = tmp[0]; p[1] = tmp[1]; p[2] = tmp[2];
    return *this;
  }
  coord & operator *=(double other) { p[0] *= other; p[1] *= other; p[2] *= other; return *this;}
  coord & operator /=(double other) { p[0] /= other; p[1] /= other; p[2] /= other; return *this;}
	coord operator -(const coord & other) const {return coord(p[0]-other.p[0],p[1]-other.p[1],p[2]-other.p[2]);}
	coord operator +(const coord & other) const {return coord(p[0]+other.p[0],p[1]+other.p[1],p[2]+other.p[2]);}
	coord operator *(const coord & other) const {return coord(p[1]*other.p[2] - p[2]*other.p[1],p[2]*other.p[0] - p[0]*other.p[2],p[0]*other.p[1] - p[1]*other.p[0]);}
	coord operator /(double other) const {return coord(p[0]/other,p[1]/other,p[2]/other);}
  coord operator *(double other) const {return coord(p[0]*other,p[1]*other,p[2]*other);}
	double operator ^(const coord & other) const {return p[0]*other.p[0]+p[1]*other.p[1]+p[2]*other.p[2];}
	~coord() {}
  double length() const {return sqrt((*this)^(*this));}
	double & operator [](int i) {return p[i];}
  double * data() {return p;}
};

double abs(const coord & p)
{
  return sqrt(p^p);
}


void get_matrix(const coord & a, const coord & b, double matrix[16])
{
	double d;
	coord z = (b-a)/sqrt((b-a)^(b-a));
	coord y;
	coord x;
	y = coord(z[1],-z[2],0);
	d = sqrt(y^y);
	if( d < 1e-5 )
	{
		y = coord(-z[2],0,z[0]);
		d = sqrt(y^y);
	}
	y = y / d;
	x = y*z;
	x = x / sqrt(x^x);
	y = x*z;
	matrix[0] = x[0];
	matrix[1] = x[1];
	matrix[2] = x[2];
	matrix[3] = 0;
	matrix[4] = y[0];
	matrix[5] = y[1];
	matrix[6] = y[2];
	matrix[7] = 0;
	matrix[8] = z[0];
	matrix[9] = z[1];
	matrix[10] = z[2];
	matrix[11] = 0;
	matrix[12] = 0;
	matrix[13] = 0;
	matrix[14] = 0;
	matrix[15] = 1;
}

GLUquadric * cylqs = NULL;
void drawcylinder(coord a,coord b, double width)
{
	double matrix[16];
	if( cylqs == NULL )
	{
		cylqs = gluNewQuadric();
		gluQuadricNormals(cylqs, GLU_SMOOTH);
		gluQuadricOrientation(cylqs, GLU_OUTSIDE);
		gluQuadricDrawStyle(cylqs,GLU_FILL);//GLU_SILHOUETTE
	}
	glPushMatrix();
	glTranslated(a[0],a[1],a[2]);
	get_matrix(a,b,matrix);
	glMultMatrixd(matrix);
	gluCylinder(cylqs, width,width,sqrt((b-a)^(b-a)), 4, 2);
	glPopMatrix();
}

class Octree : public ElementSet
{
  Tag save_center_tag;
  bool save_quad_tree;
  void SubConstruct(const Tag & child_tag, const Tag & center_tag, HandleType * cells, HandleType * temp, int size, bool quad_tree)
  {
    Storage::real_array center = RealArray(center_tag);
    //create 8 nodes
    Storage::real cell_center[3];
    int offsets[8], sizes[8];
    int dims = 3 - (quad_tree ? 1 : 0);
    int numchildren = (1 << dims);
    for(int k = 0; k < numchildren; ++k)
    {
      offsets[k] = 0;
      sizes[k] = 0;
    }
    for(int r = 0; r < size; ++r)
    {
      Element c = Element(GetMeshLink(),cells[r]);
      c->Centroid(cell_center);
      int child_num = 0;
      for(int k = 0; k < dims; ++k)
      {
        if( cell_center[k] > center[k] )
        {
          int m = 1<<k;
          child_num += m;
        }
      }
      c->IntegerDF(child_tag) = child_num;
      sizes[child_num]++;
    }
    for(int k = 1; k < numchildren; ++k)
    {
      offsets[k] = offsets[k-1]+sizes[k-1];
    }
    for(int k = 0; k < numchildren; ++k)
    {
      std::stringstream name;
      name << GetName() << "chld" << k;
      ElementSet child = GetMeshLink()->CreateSetUnique(name.str()).first;
      Storage::real_array child_center = child->RealArray(center_tag);
      for(int r = 0; r < dims; ++r)
      {
        int l = 1 << r;
        int m = k & l;
        child_center[r] = center[r] + ((m ? 1.0 : -1.0) * center[r+3] * 0.25);
        child_center[r+3] = center[r+3]*0.5;
      }
      int m = 0;
      for(int r = 0; r < size; ++r)
      {
        Element c = Element(GetMeshLink(),cells[r]);
        int q = c->IntegerDF(child_tag);
        if( q == k ) (temp+offsets[k])[m++] = cells[r];
      }
      AddChild(child);
      if( sizes[k] <= 16 && sizes[k] > 0 )
        child->PutElements(temp+offsets[k],sizes[k]);
    }
    // cells array is not needed anymore
    ElementSet child = GetChild();
    for(int k = 0; k < numchildren; ++k)
    {
      if( sizes[k] > 16 )
        Octree(child).SubConstruct(child_tag,center_tag,temp+offsets[k],cells+offsets[k],sizes[k], quad_tree);
      child = child->GetSibling();
    }
  }
  Cell SubFindCell(const Tag & center_tag, Storage::real pnt[3], bool quad_tree) const
  {
    if( HaveChild() )
    {
      Storage::real_array center = RealArray(center_tag);
      int child_num = 0, q;
      int dims = 3 - (quad_tree ? 1 : 0);
      for(int k = 0; k < dims; ++k)
      {
        if( pnt[k] > center[k] )
          child_num += (1 << k);
      }
      q = 0;
      ElementSet set = GetChild();
      while(q != child_num) {set = set->GetSibling(); q++;}
      return Octree(set).SubFindCell(center_tag,pnt,quad_tree);
    }
    else
    {
      HandleType * cells = getHandles();
      int ncells = (int)nbHandles();
      Node closest = InvalidNode();
      Storage::real mindist = 1.0e20, dist;
      for(int k = 0; k < ncells; ++k)
      {
        Node c = Node(GetMeshLink(),cells[k]);
        Storage::real_array cnt = c->Coords();
        dist = sqrt((cnt[0]-pnt[0])*(cnt[0]-pnt[0])+(cnt[1]-pnt[1])*(cnt[1]-pnt[1])+(cnt[2]-pnt[2])*(cnt[2]-pnt[2]));
        if( mindist > dist )
        {
          mindist = dist;
          closest = c;
        }
      }
      if( closest.isValid() )
      {
        ElementArray<Cell> cells = closest->getCells();
        for(ElementArray<Cell>::iterator c = cells.begin(); c != cells.end(); ++c)
          if( c->Inside(pnt) ) return c->self();
      }
      return InvalidCell();
    }
  }
  bool Inside(const Storage::real_array & center, Storage::real pnt[3], bool quad_tree) const
  {
    bool inside = true;
    int dims = 3 - (quad_tree ? 1 : 0);
    for(int i = 0; i < dims; ++i) 
      inside &= (pnt[i] >= center[i] - center[3+i]*0.5 && pnt[i] <= center[i] + center[3+i]*0.5);
    return inside;
  }
  Node SubFindNode(const Tag & center_tag, Storage::real pnt[3], bool quad_tree) const
  {
    if( HaveChild() )
    {
      Storage::real_array center = RealArray(center_tag);
      if( !Inside(center,pnt,quad_tree) ) return InvalidNode();
      int child_num = 0, q;
      int dims = 3 - (quad_tree ? 1 : 0);
      for(int k = 0; k < dims; ++k)
      {
        if( pnt[k] > center[k] )
          child_num += (1 << k);
      }
      q = 0;
      ElementSet set = GetChild();
      while(q != child_num) {set = set->GetSibling(); q++;}
      return Octree(set).SubFindNode(center_tag,pnt,quad_tree);
    }
    else
    {
      HandleType * cells = getHandles();
      int ncells = (int)nbHandles();
      Node closest = InvalidNode();
      Storage::real mindist = 1.0e20, dist;
      for(int k = 0; k < ncells; ++k)
      {
        Node c = Node(GetMeshLink(),cells[k]);
        Storage::real_array cnt = c->Coords();
        dist = sqrt((cnt[0]-pnt[0])*(cnt[0]-pnt[0])+(cnt[1]-pnt[1])*(cnt[1]-pnt[1])+(cnt[2]-pnt[2])*(cnt[2]-pnt[2]));
        if( mindist > dist )
        {
          mindist = dist;
          closest = c;
        }
      }
      return closest;
    }
  }
  void SubDestroy()
  {
    if( HaveChild() )
    {
      ElementSet set = GetChild(), next;
      while(set->isValid())
      {
        next = set->GetSibling();
        Octree(set).SubDestroy();
        set = next;
      }
    }
    DeleteSet();
    handle = InvalidHandle();
    handle_link = NULL;
  }
public:
  Octree() : ElementSet(InvalidElementSet()) {}
  Octree(const Octree & other) : ElementSet(other) {}
  Octree(const ElementSet & eset) : ElementSet(eset) {}
  void Construct(ElementType elem, bool quad_tree = false)
  {
    save_quad_tree = quad_tree;
    int dims = 3 - (quad_tree ? 1 : 0);
    Tag child_tag = GetMeshLink()->CreateTag("OCTREE_CHILD_NUM_"+GetName(),DATA_INTEGER,elem,NONE,1);
    save_center_tag = GetMeshLink()->CreateTag("OCTREE_CENTER_"+GetName(),DATA_REAL,ESET,ESET,6);
    Storage::real bounds[3][2];
    for(int k = 0; k < dims; ++k)
    {
      bounds[k][0] = 1.0e20;
      bounds[k][1] =-1.0e20;
    }
    //calculate bounds
    for(Mesh::iteratorNode node = GetMeshLink()->BeginNode(); node != GetMeshLink()->EndNode(); ++node)
    {
      Storage::real_array coord = node->Coords();
      for(int k = 0; k < dims; ++k)
      {
        if( coord[k] < bounds[k][0] ) bounds[k][0] = coord[k];
        if( coord[k] > bounds[k][1] ) bounds[k][1] = coord[k];
      }
    }
    Storage::real_array center_data = RealArray(save_center_tag);
    for(int k = 0; k < dims; ++k)
    {
      center_data[k] = (bounds[k][0]+bounds[k][1])*0.5; //central position
      center_data[k+3] = bounds[k][1]-bounds[k][0]; //length
    }
    //copy cells
    int size = GetMeshLink()->NumberOf(elem), k = 0;
    HandleType * cells = new HandleType[size*2];
    HandleType * temp = cells+size;
    for(Mesh::iteratorElement cell = GetMeshLink()->BeginElement(elem); cell != GetMeshLink()->EndElement(); ++cell)
      cells[k++] = *cell;
    SubConstruct(child_tag,save_center_tag,cells,temp,size,quad_tree);
    GetMeshLink()->DeleteTag(child_tag);
  }
  Cell FindCell(Storage::real pnt[3]) const
  {
    return SubFindCell(save_center_tag,pnt,save_quad_tree);
  }
  Node FindNode(Storage::real pnt[3]) const
  {
    return SubFindNode(save_center_tag,pnt,save_quad_tree);
  }
  void Destroy()
  {
    if( save_center_tag.isValid() )
      GetMeshLink()->DeleteTag(save_center_tag);
    SubDestroy();
  }
  ~Octree() { }
};



void GetVelocity(Cell c, const Tag & velocity_tag, coord pnt, coord & ret)
{
  ElementArray<Cell> adj = c->NeighbouringCells();
  adj.push_back(c);
  coord cnt;
  const Storage::real eps = 1.0e-8;
  Storage::real dist = 0;
  ret[0] = ret[1] = ret[2] = 0;
  for(ElementArray<Cell>::iterator it = adj.begin(); it != adj.end(); ++it)
  {
    it->Centroid(cnt.data());
    coord vel = coord(it->RealArray(velocity_tag).data());
    Storage::real l = (cnt-pnt).length() + eps;
    Storage::real omega = 1.0/(l*l);
    ret += vel*omega;
    dist += omega;
  }
  ret /= dist;
}

void GetVelocity(Node c, const Tag & velocity_tag, coord pnt, coord & ret)
{
  ElementArray<Cell> adj = c->getCells();
  coord cnt;
  const Storage::real eps = 1.0e-8;
  Storage::real dist = 0;
  ret[0] = ret[1] = ret[2] = 0;
  for(ElementArray<Cell>::iterator it = adj.begin(); it != adj.end(); ++it)
  {
    it->Centroid(cnt.data());
    coord vel = coord(it->RealArray(velocity_tag).data());
    Storage::real l = (cnt-pnt).length() + eps;
    Storage::real omega = 1.0/(l*l);
    ret += vel*omega;
    dist += omega;
  }
  ret /= dist;
}


Storage::real GetSize(Cell c)
{
  Storage::real bounds[3][2] = {{1.0e20,-1.0e20},{1.0e20,-1.0e20},{1.0e20,-1.0e20}};
  ElementArray<Node> nodes = c->getNodes();
  for(ElementArray<Node>::iterator n = nodes.begin(); n != nodes.end(); ++n)
  {
    Storage::real_array cnt = n->Coords();
    for(int k = 0; k < 3; ++k)
    {
      if( bounds[k][0] > cnt[k] ) bounds[k][0] = cnt[k];
      if( bounds[k][1] < cnt[k] ) bounds[k][1] = cnt[k];
    }
  }
  Storage::real ret = bounds[0][1]-bounds[0][0];
  ret = std::min(ret,bounds[1][1]-bounds[1][0]);
  ret = std::min(ret,bounds[2][1]-bounds[2][0]);
  return ret;
}


Storage::real GetSize(Node n, const Tag & size_tag)
{
  ElementArray<Cell> cells = n->getCells();
  Storage::real minsize = 1.0e+20, size;
  for(ElementArray<Cell>::iterator c = cells.begin(); c != cells.end(); ++c)
  {
    size = c->RealDF(size_tag);
    if( minsize > size ) minsize = size;
  }
  if( minsize > 1.0e+19 ) std::cout << __FILE__ << ":" << __LINE__ << " oops" << std::endl;
  return minsize;
}


class Streamline
{
private:
	std::vector<coord> points;
	std::vector<double> velarr;
public:
  Streamline() {}
	Streamline(const Octree & octsearch, coord pos, Tag velocity_tag, Tag cell_size, Storage::real velocity_min, Storage::real velocity_max, Storage::real sign, MarkerType visited)
	{
		Storage::real coef, len, size;
    coord next = pos, vel;
    Node c;
    const int maxsteps = 4000;
    points.reserve(maxsteps/2);
    velarr.reserve(maxsteps/2);
		points.push_back(pos);
		velarr.push_back(0);
	  while( points.size() < maxsteps )
		{
      c = octsearch.FindNode(next.data());
      if( !c.isValid() ) break;
      //c.SetMarker(visited);
      GetVelocity(c,velocity_tag,next,vel);
      len = vel.length();
      if( len < 1.0e-4 ) break;
      size = GetSize(c,cell_size);// c->RealDF(cell_size);
      coef = 0.35*size/len;
      next += vel*coef*sign;
      points.push_back(next);
      velarr.push_back((log(len+1.0e-25)-velocity_min)/(velocity_max-velocity_min));
		}
		//printf("%ld %ld\n",points.size(),velarr.size());
	}
	Streamline(const Streamline & other) { points = other.points; velarr = other.velarr; }
	Streamline & operator =(Streamline const & other) {points = other.points; velarr = other.velarr; return *this;}
	~Streamline() { points.clear(); velarr.clear(); }
	void Draw(int reduced)
	{
		
		if( reduced )
		{
			glBegin(GL_LINE_STRIP);
			for(unsigned int i = 0; i < points.size()-1; i++)
			{
				glColor3f(velarr[i+1]*0.65,0.65*(velarr[i+1] < 0.5 ? velarr[i] : 1.0-velarr[i]),0.65*(1-velarr[i+1]));
				glVertex3d(points[i][0],points[i][1],points[i][2]);
			}
			glEnd();
		}
		else for(unsigned int i = 0; i < points.size()-1; i++)
		{
			glColor3f(velarr[i+1]*0.65,0.65*(velarr[i+1] < 0.5 ? velarr[i] : 1.0-velarr[i]),0.65*(1-velarr[i+1]));
			drawcylinder(points[i],points[i+1],0.25*abs(points[i+1]-points[i]));
		}
		
	}
};

std::vector<Streamline> streamlines;


Kirill Terekhov's avatar
Kirill Terekhov committed
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
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);
	}
}

Kirill Terekhov's avatar
Kirill Terekhov committed
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
bool write_tga( const char *filename, int w, int h, char *buffer ) 
{ 
  FILE *f = fopen( filename, "wb" ); 
  if ( !f ) 
    return false;
  putc(0,f);
  putc(0,f);
  putc(2,f);
  putc(0,f);putc(0,f);
  putc(0,f);putc(0,f);
  putc(0,f);
  putc(0,f);putc(0,f);
  putc(0,f);putc(0,f);
  putc((w & 0x00ff),f);
  putc((w & 0xff00)/256,f);
  putc((h & 0x00ff),f);
  putc((h & 0xff00)/256,f);
  putc(24,f);
  putc(0,f);
  size_t buflen = w * h * 3; 
  fwrite( buffer, 1, buflen, f ); 
  fclose(f); 
  return true; 
}

void screenshot()
{
Kirill Terekhov's avatar
Kirill Terekhov committed
659
  const int tiles = 4;
Kirill Terekhov's avatar
Kirill Terekhov committed
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
  int oldwidth = width;
  int oldheight = height;
  width *= tiles;
  height *= tiles;
  
  char * pixelbuffer = new char[width*height*3+oldwidth*oldheight*3];
  char * tempbuffer = pixelbuffer + width*height*3;

  
  
  
  for(int i = 0; i < tiles; ++i)
  {
    for(int j = 0; j < tiles; ++j )
    {
      glViewport(-oldwidth*i,-oldheight*j,width,height);
      draw_screen();
      glReadBuffer(GL_BACK);
      glReadPixels(0,0,oldwidth,oldheight,GL_BGR_EXT,GL_UNSIGNED_BYTE,tempbuffer);

      int koff = oldwidth*(i);
      int loff = oldheight*(j);
      
      for(int l = 0; l < oldheight; ++l)
      for(int k = 0; k < oldwidth; ++k)
      for(int m = 0; m < 3; ++m)
        pixelbuffer[((koff+k) + (loff+l)*width)*3+m] = tempbuffer[(k + l*oldwidth)*3+m];

      //filename[0] += i;
      //filename[1] += j;
      //write_tga(filename,width,height,pixelbuffer);
      //filename[0] = filename[1] = '0';
    }
  }
  
  /*
  glViewport(-oldwidth*3,-oldheight*3,width,height);
  draw_screen();
  glReadBuffer(GL_BACK);
  glReadPixels(0,0,width,height,GL_BGR_EXT,GL_UNSIGNED_BYTE,pixelbuffer);
  */


  
  
  
  write_tga("screenshot.tga",width,height,pixelbuffer);
  delete [] pixelbuffer;
  width = oldwidth;
  height = oldheight;
  glViewport(0,0,width,height);
}

Kirill Terekhov's avatar
Kirill Terekhov committed
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
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];}
Kirill Terekhov's avatar
Kirill Terekhov committed
746
747
748
	color_t operator *(float mult)const {return color_t(c[0]*mult,c[1]*mult,c[2]*mult,c[3]*mult);}
	color_t operator +(color_t other)const{return color_t(c[0]+other.c[0],c[1]+other.c[1],c[2]+other.c[2],c[3]+other.c[3]);}
	color_t operator -(color_t other)const {return color_t(c[0]-other.c[0],c[1]-other.c[1],c[2]-other.c[2],other.c[3]);}
Kirill Terekhov's avatar
Kirill Terekhov committed
749
750
751
752
753
754
755
756
};

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;
757
	unsigned texture;
758
	int samples;
Kirill Terekhov's avatar
Kirill Terekhov committed
759
760
761
762
763
764
public:
	color_bar()
	{
		min = 0;
		max = 1;
		comment = "";
765
766

		/*
Kirill Terekhov's avatar
Kirill Terekhov committed
767
768
769
770
771
772
		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);
Kirill Terekhov's avatar
Kirill Terekhov committed
773

774
775
776
777
778
779
780
781
		 
		//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));
		
Kirill Terekhov's avatar
Kirill Terekhov committed
782
		colors.push_back(color_t(1,0,1));
783
784
		colors.push_back(color_t(0,0,1));
		colors.push_back(color_t(0,1,1));
Kirill Terekhov's avatar
Kirill Terekhov committed
785
		colors.push_back(color_t(0,1,0));
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
		colors.push_back(color_t(1,1,0));
		colors.push_back(color_t(1,0,0));
		*/

		//inversed gnuplot color scheme
		ticks.push_back(0.f);
		ticks.push_back(0.05f);
		ticks.push_back(0.5f);
		ticks.push_back(0.75f);
		ticks.push_back(0.95f);
		ticks.push_back(1.f);

		colors.push_back(color_t(1,1,1));
		colors.push_back(color_t(1,1,0));
		colors.push_back(color_t(0.85,0,0));
		colors.push_back(color_t(0.65,0.25,0.85));
		colors.push_back(color_t(0.45,0,0.55));
		colors.push_back(color_t(0,0,0));

Kirill Terekhov's avatar
Kirill Terekhov committed
805
		samples = 4096;
806
807

		float * pixel_array = new float[(samples+2)*4];
808

Kirill Terekhov's avatar
Kirill Terekhov committed
809
810
		
		
811
		for(int q = 0; q < samples+2; ++q)
812
		{
813
			float t = 1.0f*q/static_cast<float>(samples+1);
814
			color_t c = pick_color(t);
815
			//countour lines
Kirill Terekhov's avatar
Kirill Terekhov committed
816
817
			//if( ((q+1) % 128 == 0 || (q+1) % 128 == 127) && (q+1) < samples )
			//	c = pick_color(1-t) + color_t(0,2*t*(1-t),0);
818

819
820
821
822
823
824
825
826
827
828
829
			pixel_array[(q)*4+0] = c.r();
			pixel_array[(q)*4+1] = c.g();
			pixel_array[(q)*4+2] = c.b();
			pixel_array[(q)*4+3] = c.a();
		}

		pixel_array[0] = 0;
		pixel_array[1] = 1;
		pixel_array[2] = 0;
		pixel_array[3] = 1;

830
831
832
833
		pixel_array[(samples+1)*4+0] = 0;
		pixel_array[(samples+1)*4+1] = 1;
		pixel_array[(samples+1)*4+2] = 0;
		pixel_array[(samples+1)*4+3] = 1;
834
835
836
837
838
839
840
841
842

		
		
		glEnable(GL_TEXTURE);
		glEnable(GL_TEXTURE_1D);
		glGenTextures(1,&texture);
		glBindTexture(GL_TEXTURE_1D,texture);
		glPixelStorei(GL_UNPACK_ALIGNMENT,1);
		
843
		glTexImage1D(GL_TEXTURE_1D,0,4,samples+2,1,GL_RGBA,GL_FLOAT,pixel_array);
844
845
846
847
848
849
850
851
852
853
854
855

		std::cout << "Created texture " << texture << std::endl;
		
		glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_WRAP_S,GL_CLAMP);
		glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

		UnbindTexture();

		delete [] pixel_array;
Kirill Terekhov's avatar
Kirill Terekhov committed
856
857
858
859
860
		//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;}
861
862
	float get_min() { return min;}
	float get_max() { return max;}
Kirill Terekhov's avatar
Kirill Terekhov committed
863
	color_t pick_color(float value) const
Kirill Terekhov's avatar
Kirill Terekhov committed
864
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
865
866
867
868
		if( value < min ) 
			return color_t(0.4,1.0,0.4);
		if( value > max )
			return color_t(0,0.6,0);
Kirill Terekhov's avatar
Kirill Terekhov committed
869
		float t = (value-min)/(max-min);
Kirill Terekhov's avatar
Kirill Terekhov committed
870
		std::vector<float>::const_iterator it = std::lower_bound(ticks.begin(),ticks.end(),t);
Kirill Terekhov's avatar
Kirill Terekhov committed
871
872
873
874
875
876
877
878
879
880
881
882
		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));
	}
883
884
885
886
887
888
889
890
891
892
893
	void BindTexture()
	{
		//glDisable( GL_TEXTURE_GEN_S ); 
		glDisable(GL_TEXTURE_2D);
		glEnable( GL_TEXTURE_1D );
		glBindTexture(GL_TEXTURE_1D, texture);
	}
	void UnbindTexture()
	{
		glDisable( GL_TEXTURE_1D );
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
894
	double pick_texture(double value) const
895
	{
896
		double eps = 1.0/static_cast<double>(samples);
897
898
899
		return (value-min)/(max-min)*(1-2*eps) + eps;
		//return std::max(std::min((value-min)/(max-min),0.99),0.01);
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
900
901
902
903
904
905
906
	void Draw()
	{
		float text_pos = -0.89;
		float left = -0.95;
		float right = -0.9;
		float bottom = -0.75;
		float top = 0.75;
907
		BindTexture();
Kirill Terekhov's avatar
Kirill Terekhov committed
908
		glBegin(GL_QUADS);
909
910
911
912
913
914
915
916

		glTexCoord1d(1.0/1024.0);
		glVertex2f(left,bottom);
		glVertex2f(right,bottom);
		glTexCoord1d(1.0);
		glVertex2f(right,top);
		glVertex2f(left,top);
		/*
Kirill Terekhov's avatar
Kirill Terekhov committed
917
918
919
920
921
922
923
924
925
		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));
		}
926
		*/
Kirill Terekhov's avatar
Kirill Terekhov committed
927
		glEnd();
928
		UnbindTexture();
929
930

		int tickmarks = 11;
Kirill Terekhov's avatar
Kirill Terekhov committed
931
932
		
		glColor4f(0,0,0,1);
933
		for(int i = 0; i < tickmarks; ++i)
Kirill Terekhov's avatar
Kirill Terekhov committed
934
		{
935
936
937
			float t = 1.0f*i/static_cast<float>(tickmarks-1);
			glRasterPos2f(text_pos,bottom+t*(top-bottom));
			printtext("%g",min+t*(max-min));
Kirill Terekhov's avatar
Kirill Terekhov committed
938
939
940
941
942
943
		}
		if( comment != "")
		{
			glRasterPos2f(left,bottom-0.04);
			printtext("%s",comment.c_str());
		}
944
945
946
947
948
949
950
951
952

		glBegin(GL_LINE_LOOP);
		glVertex2f(left,bottom);
		glVertex2f(right,bottom);
		glVertex2f(right,top);
		glVertex2f(left,top);
		glEnd();

		glBegin(GL_LINES);
953
		for(int i = 0; i < tickmarks; ++i)
954
		{
955
956
			float t = 1.0f*i/static_cast<float>(tickmarks-1);
			float pos = bottom+t*(top-bottom);
957
958
959
960
961
962
963
			glVertex2f(left,pos);
			glVertex2f(left+(right-left)*0.2,pos);

			glVertex2f(right+(left-right)*0.25,pos);
			glVertex2f(right,pos);
		}
		glEnd();
Kirill Terekhov's avatar
Kirill Terekhov committed
964
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
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

	void DrawSVG(std::ostream & file, double modelview[16], double projection[16], int viewport[4])
	{
		float text_pos = -0.89;
		float left = -0.95;
		float right = -0.9;
		float bottom = -0.75;
		float top = 0.75;
		double px,py;
		double px1, py1, z;
		double px2, py2;
		file << "<g>"<<std::endl;
		for(int i = 0; i < ticks.size()-1; ++i)
		{
			colors[i].set_color();
			gluProject(left,bottom+ticks[i]*(top-bottom),0,modelview,projection,viewport,&px1,&py1,&z); py1 = height - py1;
			gluProject(right,bottom+ticks[i+1]*(top-bottom),0,modelview,projection,viewport,&px2,&py2,&z); py2 = height - py2;
			file << "<def>" << std::endl;
			file << "<linearGradient id=\"grad"<<i<<"\" gradientUnits=\"userSpaceOnUse\" x1=\"" << px1 << "\" y1=\""<<py1<<"\" x2=\""<<px1<<"\" y2=\""<<py2<<"\">" << std::endl;
			file << "<stop offset=\"0%\" stop-color=\"rgb("<<floor(colors[i].r()*255)<<","<<floor(colors[i].g()*255)<<","<<floor(colors[i].b()*255)<<")\"/>" << std::endl;
			file << "<stop offset=\"100%\" stop-color=\"rgb("<<floor(colors[i+1].r()*255)<<","<<floor(colors[i+1].g()*255)<<","<<floor(colors[i+1].b()*255)<<")\"/>" << std::endl;
        	file << "</linearGradient>" << std::endl;
			file << "</def>" << std::endl;
			file << "<rect stroke=\"none\" x=\""<<px1<<"\" y=\""<<py2<<"\" width=\""<< px2-px1 <<"\" height=\""<<py1-py2<<"\" fill=\"url(#grad"<<i<<")\"/>" << std::endl;
		}
		
		int tickmarks = 11;
		
		for(int i = 0; i < tickmarks; ++i)
		{
			float t = 1.0f*i/static_cast<float>(tickmarks-1);
			glRasterPos2f(text_pos,bottom+t*(top-bottom));
			gluProject(text_pos,bottom+t*(top-bottom),0,modelview,projection,viewport,&px,&py,&z); py = height - py;
			file << "<text x=\""<<px<<"\" y=\""<<py<<"\">" << min+t*(max-min) << "</text>" << std::endl;
		}
		if( comment != "")
		{
			gluProject(left,bottom-0.04,0,modelview,projection,viewport,&px,&py,&z); py = height - py;
			file << "<text x=\""<<px<<"\" y=\""<<py<<"\">" << comment.c_str() << "</text>" << std::endl;
		}

		gluProject(left,bottom,0,modelview,projection,viewport,&px1,&py1,&z); py1 = height - py1;
		gluProject(right,top,0,modelview,projection,viewport,&px2,&py2,&z); py2 = height - py2;
		file << "<rect stroke=\"black\" fill=\"none\" x=\""<<px1<<"\" y=\""<<py2<<"\" width=\""<<px2-px1<<"\" height=\""<<py1-py2<<"\"/>" << std::endl;
		
		file << "<g stroke=\"black\">" << std::endl;
		for(int i = 0; i < tickmarks; ++i)
		{
			float t = 1.0f*i/static_cast<float>(tickmarks-1);
			float pos = bottom+t*(top-bottom);
			svg_line(file,left,pos,0,left+(right-left)*0.2,pos,0,modelview,projection,viewport);
			svg_line(file,right+(left-right)*0.25,pos,0,right,pos,0,modelview,projection,viewport);
		}
		file << "</g>" << std::endl;
		file << "</g>" << std::endl;
	}
	
1022
} * CommonColorBar;
Kirill Terekhov's avatar
Kirill Terekhov committed
1023

Kirill Terekhov's avatar
Kirill Terekhov committed
1024
1025
1026

class face2gl
{
Kirill Terekhov's avatar
Kirill Terekhov committed
1027
	float dist;
Kirill Terekhov's avatar
Kirill Terekhov committed
1028
1029
1030
1031
1032
1033
	double c[4];
	double cnt[3];
	bool flag;
	ElementType etype;
	Storage::integer id;
	std::vector<double> verts;
1034
	std::vector<double> texcoords;
Kirill Terekhov's avatar
Kirill Terekhov committed
1035
1036
	std::vector<color_t> colors;
	color_t cntcolor;
1037
	double cnttexcoord;
Kirill Terekhov's avatar
Kirill Terekhov committed
1038
public:
Kirill Terekhov's avatar
Kirill Terekhov committed
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
	void shift(double x, double y, double z)
	{
		cnt[0] += x;
		cnt[1] += y;
		cnt[2] += z;
		for(size_t k = 0; k < verts.size(); k+=3)
		{
			verts[k+0] += x;
			verts[k+1] += y;
			verts[k+2] += z;
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
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
	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];
	}
1080
	face2gl():verts(),colors(),texcoords() {etype = NONE; id = 0; dist = 0; flag = false; memset(c,0,sizeof(double)*4);}
Kirill Terekhov's avatar
Kirill Terekhov committed
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
	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
1094
		colors = other.colors;
1095
		texcoords = other.texcoords;
Kirill Terekhov's avatar
Kirill Terekhov committed
1096
		cntcolor = other.cntcolor;
1097
		cnttexcoord = other.cnttexcoord;
Kirill Terekhov's avatar
Kirill Terekhov committed
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
	}
	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
1113
1114
		colors = other.colors;
		cntcolor = other.cntcolor;
1115
1116
		texcoords = other.texcoords;
		cnttexcoord = other.cnttexcoord;
Kirill Terekhov's avatar
Kirill Terekhov committed
1117
1118
1119
1120
1121
		return *this;
	}
	~face2gl() {}
	void draw_colour() const
	{
1122
		if( texcoords.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1123
		{
1124
			if( colors.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1125
			{
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
				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()]);
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
1145
1146
1147
1148
1149
1150
			}
		}
		else
		{
			for(unsigned k = 0; k < verts.size(); k+=3) 
			{
1151
				glTexCoord1d(cnttexcoord);
Kirill Terekhov's avatar
Kirill Terekhov committed
1152
				glVertex3dv(cnt);
1153
				glTexCoord1d(texcoords[k/3]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1154
				glVertex3dv(&verts[k]);
1155
				glTexCoord1d(texcoords[(k/3+1)%texcoords.size()]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1156
1157
1158
1159
				glVertex3dv(&verts[(k+3)%verts.size()]);
			}
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
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
	void svg_draw_colour(std::ostream & file, double modelview[16], double projection[16], int viewport[4]) const
	{
		static int shape_id = 0;
		if( colors.empty() && texcoords.empty() )
		{
			double pcntx,pcnty,z;
			double pverts1x,pverts1y;
			double pverts2x,pverts2y;
			gluProject(cnt[0],cnt[1],cnt[2],modelview,projection,viewport,&pcntx,&pcnty,&z); pcnty = height-pcnty;
			//file << "<g stroke=\"none\" fill=\"rgb("<<floor(c[0]*255)<<","<<floor(c[1]*255)<<","<<floor(c[2]*255)<<")\" fill-opacity=\"" << c[3] << "\">" << std::endl;
			file << "<g stroke=\"none\" fill=\"white\" fill-opacity=\"" << c[3] << "\">" << std::endl;
			for(unsigned k = 0; k < verts.size(); k+=3) 
			{
				gluProject(verts[k+0],verts[k+1],verts[k+2],modelview,projection,viewport,&pverts1x,&pverts1y,&z); pverts1y = height-pverts1y;
				gluProject(verts[(k+3)%verts.size()+0],verts[(k+3)%verts.size()+1],verts[(k+3)%verts.size()+2],modelview,projection,viewport,&pverts2x,&pverts2y,&z); pverts2y = height - pverts2y;
				file << "<polygon points=\"" << pcntx <<"," << pcnty << " " << pverts1x << "," << pverts1y << " " << pverts2x << "," << pverts2y << "\"/>" << std::endl;
			}
			file << "</g>" << std::endl;
		}
		
		else if( !colors.empty() )
		{
			double pcntx,pcnty,z;
			double pverts1x,pverts1y;
			double pverts2x,pverts2y;
			gluProject(cnt[0],cnt[1],cnt[2],modelview,projection,viewport,&pcntx,&pcnty,&z); pcnty = height-pcnty;
			file << "<g stroke=\"none\" fill=\"rgb("<<floor(cntcolor.r()*255)<<","<<floor(cntcolor.g()*255)<<","<<floor(cntcolor.b()*255)<<")\">" << std::endl;
			for(unsigned k = 0; k < verts.size(); k+=3) 
			{
				gluProject(verts[k+0],verts[k+1],verts[k+2],modelview,projection,viewport,&pverts1x,&pverts1y,&z); pverts1y = height-pverts1y;
				gluProject(verts[(k+3)%verts.size()+0],verts[(k+3)%verts.size()+1],verts[(k+3)%verts.size()+2],modelview,projection,viewport,&pverts2x,&pverts2y,&z); pverts2y = height-pverts2y;
				file << "<polygon points=\"" << pcntx <<"," << pcnty << " " << pverts1x << "," << pverts1y << " " << pverts2x << "," << pverts2y << "\"/>" << std::endl;
			}
			file << "</g>" << std::endl;
		}
		else if( !texcoords.empty() )
		{
			double pcntx,pcnty,z;
			double pverts1x,pverts1y;
			double pverts2x,pverts2y;
			color_t c0 = CommonColorBar->pick_color(cnttexcoord*(CommonColorBar->get_max()-CommonColorBar->get_min())+CommonColorBar->get_min());
			gluProject(cnt[0],cnt[1],cnt[2],modelview,projection,viewport,&pcntx,&pcnty,&z); pcnty = height-pcnty;
			//file << "<!-- " << cnttexcoord << " color " << c0.r() << " " << c0.g() << " " << c0.b() << " " << c0.a() << " -->" << std::endl;
			//file << "<g stroke=\"none\" fill=\"rgb("<<floor(c0.r()*255)<<","<<floor(c0.g()*255)<<","<<floor(c0.b()*255)<<")\" fill-opacity=\"" << c[3] << "\">" << std::endl;
			file << "<g stroke=\"none\" fill=\"rgb("<<floor(c0.r()*255)<<","<<floor(c0.g()*255)<<","<<floor(c0.b()*255)<<")\">" << std::endl;
			for(unsigned k = 0; k < verts.size(); k+=3) 
			{
				gluProject(verts[k+0],verts[k+1],verts[k+2],modelview,projection,viewport,&pverts1x,&pverts1y,&z); pverts1y = height - pverts1y;
				gluProject(verts[(k+3)%verts.size()+0],verts[(k+3)%verts.size()+1],verts[(k+3)%verts.size()+2],modelview,projection,viewport,&pverts2x,&pverts2y,&z); pverts2y = height - pverts2y;
				file << "<polygon points=\"" << pcntx <<"," << pcnty << " " << pverts1x << "," << pverts1y << " " << pverts2x << "," << pverts2y << "\"/>" << std::endl;
			}
			file << "</g>" << std::endl;
		}
		/*
		
		else if( !texcoords.empty() )
		{
			double pverts0x,pverts0y,z;
			double pverts1x,pverts1y;
			double pverts2x,pverts2y;
			color_t c0 = CommonColorBar->pick_color(cnttexcoord*(CommonColorBar->get_max()-CommonColorBar->get_min())+CommonColorBar->get_min()),c1,c2;
			gluProject(cnt[0],cnt[1],cnt[2],modelview,projection,viewport,&pverts0x,&pverts0y,&z); pverts0y = height-pverts0y;
			for(unsigned k = 0; k < verts.size(); k+=3) 
			{
				c1 = CommonColorBar->pick_color(texcoords[k/3]*(CommonColorBar->get_max()-CommonColorBar->get_min())+CommonColorBar->get_min());
				c2 = CommonColorBar->pick_color(texcoords[(k/3+1)%texcoords.size()]*(CommonColorBar->get_max()-CommonColorBar->get_min())+CommonColorBar->get_min());
				gluProject(verts[k+0],verts[k+1],verts[k+2],modelview,projection,viewport,&pverts1x,&pverts1y,&z); pverts1y = height-pverts1y;
				gluProject(verts[(k+3)%verts.size()+0],verts[(k+3)%verts.size()+1],verts[(k+3)%verts.size()+2],modelview,projection,viewport,&pverts2x,&pverts2y,&z); pverts2y = height-pverts2y;
				file << "<defs>" << std::endl;
				file << "<linearGradient id=\"fadeA"<<shape_id<<"\" gradientUnits=\"userSpaceOnUse\" x1=\"" << pverts0x << "\" y1=\"" << pverts0y << "\" x2=\"" << (pverts1x+pverts2x)*0.5 << "\" y2=\"" << (pverts1y+pverts2y)*0.5 << "\">" << std::endl;
				file << "<stop offset=\"0%\" stop-color=\"rgb("<<floor(c0.r()*255)<<","<<floor(c0.g()*255)<<","<<floor(c0.b()*255)<<")\"/>" <<std::endl;
				file << "<stop offset=\"100%\" stop-color=\"rgb("<<floor((c1.r()+c2.r())*0.5*255)<<","<<floor((c1.g()+c2.g())*0.5*255)<<","<<floor((c1.b()+c2.b())*0.5*255)<<")\"/>" <<std::endl;
				file << "</linearGradient>"<<std::endl;
				file << "<linearGradient id=\"fadeB"<<shape_id<<"\" gradientUnits=\"userSpaceOnUse\" x1=\"" << pverts1x << "\" y1=\"" << pverts1y << "\" x2=\"" << (pverts0x+pverts2x)*0.5 << "\" y2=\"" << (pverts0y+pverts2y)*0.5 << "\">" << std::endl;
				file << "<stop offset=\"0%\" stop-color=\"rgb("<<floor(c1.r()*255)<<","<<floor(c1.g()*255)<<","<<floor(c1.b()*255)<<")\"/>" <<std::endl;
				file << "<stop offset=\"100%\" stop-color=\"rgb("<<floor((c0.r()+c2.r())*0.5*255)<<","<<floor((c0.g()+c2.g())*0.5*255)<<","<<floor((c0.b()+c2.b())*0.5*255)<<")\"/>" <<std::endl;
				file << "</linearGradient>"<<std::endl;
				file << "<linearGradient id=\"fadeC"<<shape_id<<"\" gradientUnits=\"userSpaceOnUse\" x1=\"" << pverts2x << "\" y1=\"" << pverts2y << "\" x2=\"" << (pverts0x+pverts1x)*0.5 << "\" y2=\"" << (pverts0y+pverts1y)*0.5 << "\">" << std::endl;
				file << "<stop offset=\"0%\" stop-color=\"rgb("<<floor(c2.r()*255)<<","<<floor(c2.g()*255)<<","<<floor(c2.b()*255)<<")\"/>" <<std::endl;
				file << "<stop offset=\"100%\" stop-color=\"rgb("<<floor((c1.r()+c0.r())*0.5*255)<<","<<floor((c1.g()+c0.g())*0.5*255)<<","<<floor((c1.b()+c0.b())*0.5*255)<<")\"/>" <<std::endl;
				file << "</linearGradient>"<<std::endl;
				file << "<path id=\"pathA"<<shape_id<<"\" d=\"M "<<pverts0x<<","<<pverts0y<<" L "<<pverts1x<<","<<pverts1y<<" "<<pverts2x<<","<<pverts2y<<" Z\" fill=\"url(#fadeA"<<shape_id<<")\"/>" <<std::endl;
				file << "<path id=\"pathB"<<shape_id<<"\" d=\"M "<<pverts0x<<","<<pverts0y<<" L "<<pverts1x<<","<<pverts1y<<" "<<pverts2x<<","<<pverts2y<<" Z\" fill=\"url(#fadeB"<<shape_id<<")\"/>" <<std::endl;
				file << "<filter id=\"filter"<<shape_id<<"\">"<<std::endl;
				file << "<feImage xlink:href=\"#pathA"<<shape_id<<"\" result=\"layerA"<<shape_id<<"\" x=\"0\" y=\"0\"/>" <<std::endl;
				file << "<feImage xlink:href=\"#pathB"<<shape_id<<"\" result=\"layerB"<<shape_id<<"\" x=\"0\" y=\"0\"/>" <<std::endl;
				file << "<feComposite in=\"layerA"<<shape_id<<"\" in2=\"layerB"<<shape_id<<"\" operator=\"arithmetic\" k1=\"0\" k2=\"1\" k3=\"1\" k4=\"0\" result=\"temp"<<shape_id<<"\"/>"<<std::endl;
				file << "<feComposite in=\"temp"<<shape_id<<"\" in2=\"graphic"<<shape_id<<"\" operator=\"arithmetic\" k1=\"0\" k2=\"1\" k3=\"1\" k4=\"0\"/>"<<std::endl;
				file << "</filter>"<<std::endl;
				file << "</defs>" << std::endl;
				file << "<g stroke=\"none\">" << std::endl;
				file << "<path d=\"M "<<pverts0x<<","<<pverts0y<<" L "<<pverts1x<<","<<pverts1y<<" "<<pverts2x<<","<<pverts2y<<" Z\" fill=\"url(#fadeC"<<shape_id<<")\" filter=\"url(#filter"<<shape_id<<")\"/>" <<std::endl;
				file << "</g>" << std::endl;
				shape_id++;
			}
		}
		else if( !colors.empty() )
		{
			double pverts0x,pverts0y,z;
			double pverts1x,pverts1y;
			double pverts2x,pverts2y;
			gluProject(cnt[0],cnt[1],cnt[2],modelview,projection,viewport,&pverts0x,&pverts0y,&z); pverts0y = height-pverts0y;
			for(unsigned k = 0; k < verts.size(); k+=3) 
			{
				gluProject(verts[k+0],verts[k+1],verts[k+2],modelview,projection,viewport,&pverts1x,&pverts1y,&z); pverts1y = height-pverts1y;
				gluProject(verts[(k+3)%verts.size()+0],verts[(k+3)%verts.size()+1],verts[(k+3)%verts.size()+2],modelview,projection,viewport,&pverts2x,&pverts2y,&z); pverts2y = height-pverts2y;
				file << "<defs>" << std::endl;
				file << "<linearGradient id=\"fadeA"<<shape_id<<"\" gradientUnits=\"userSpaceOnUse\" x1=\"" << pverts0x << "\" y1=\"" << pverts0y << "\" x2=\"" << (pverts1x+pverts2x)*0.5 << "\" y2=\"" << (pverts1y+pverts2y)*0.5 << "\">" << std::endl;
				file << "<stop offset=\"0%\" stop-color=\"rgb("<<floor(cntcolor.r()*255)<<","<<floor(cntcolor.g()*255)<<","<<floor(cntcolor.b()*255)<<")\"/>" <<std::endl;
				file << "<stop offset=\"100%\" stop-color=\"#000000\"/>" <<std::endl;
				file << "</linearGradient>"<<std::endl;
				file << "<linearGradient id=\"fadeB"<<shape_id<<"\" gradientUnits=\"userSpaceOnUse\" x1=\"" << pverts1x << "\" y1=\"" << pverts1y << "\" x2=\"" << (pverts0x+pverts2x)*0.5 << "\" y2=\"" << (pverts0y+pverts2y)*0.5 << "\">" << std::endl;
				file << "<stop offset=\"0%\" stop-color=\"rgb("<<floor(colors[k/3].r()*255)<<","<<floor(colors[k/3].g()*255)<<","<<floor(colors[k/3].b()*255)<<")\"/>" <<std::endl;
				file << "<stop offset=\"100%\" stop-color=\"#000000\"/>" <<std::endl;
				file << "</linearGradient>"<<std::endl;
				file << "<linearGradient id=\"fadeC"<<shape_id<<"\" gradientUnits=\"userSpaceOnUse\" x1=\"" << pverts2x << "\" y1=\"" << pverts2y << "\" x2=\"" << (pverts0x+pverts1x)*0.5 << "\" y2=\"" << (pverts0y+pverts1y)*0.5 << "\">" << std::endl;
				file << "<stop offset=\"0%\" stop-color=\"rgb("<<floor(colors[(k/3+1)%colors.size()].r()*255)<<","<<floor(colors[(k/3+1)%colors.size()].g()*255)<<","<<floor(colors[(k/3+1)%colors.size()].b()*255)<<")\"/>" <<std::endl;
				file << "<stop offset=\"100%\" stop-color=\"#000000\"/>" <<std::endl;
				file << "</linearGradient>"<<std::endl;
				file << "<path id=\"pathA"<<shape_id<<"\" d=\"M "<<pverts0x<<","<<pverts0y<<" L "<<pverts1x<<","<<pverts1y<<" "<<pverts2x<<","<<pverts2y<<" Z\" fill=\"url(#fadeA"<<shape_id<<")\"/>" <<std::endl;
				file << "<path id=\"pathB"<<shape_id<<"\" d=\"M "<<pverts0x<<","<<pverts0y<<" L "<<pverts1x<<","<<pverts1y<<" "<<pverts2x<<","<<pverts2y<<" Z\" fill=\"url(#fadeB"<<shape_id<<")\"/>" <<std::endl;
				file << "<filter id=\"filter"<<shape_id<<"\">"<<std::endl;
				file << "<feImage xlink:href=\"#pathA"<<shape_id<<"\" result=\"layerA"<<shape_id<<"\" x=\"0\" y=\"0\"/>" <<std::endl;
				file << "<feImage xlink:href=\"#pathB"<<shape_id<<"\" result=\"layerB"<<shape_id<<"\" x=\"0\" y=\"0\"/>" <<std::endl;
				file << "<feComposite in=\"layerA"<<shape_id<<"\" in2=\"layerB"<<shape_id<<"\" operator=\"arithmetic\" k1=\"0\" k2=\"1.0\" k4=\"0\" result=\"temp"<<shape_id<<"\"/>"<<std::endl;
				file << "<feComposite in=\"temp"<<shape_id<<"\" in2=\"graphic"<<shape_id<<"\" operator=\"arithmetic\" k1=\"0\" k2=\"1.0\" k4=\"0\"/>"<<std::endl;
				file << "</filter>"<<std::endl;
				file << "</defs>" << std::endl;
				file << "<g stroke=\"none\" stroke-width=\"0\" shape-rendering=\"crispEdges\">" << std::endl;
				file << "<path d=\"M "<<pverts0x<<","<<pverts0y<<" L "<<pverts1x<<","<<pverts1y<<" "<<pverts2x<<","<<pverts2y<<" Z\" fill=\"url(#fadeC"<<shape_id<<")\" filter=\"url(#filter"<<shape_id<<")\"/>" <<std::endl;
				file << "</g>" << std::endl;
				shape_id++;
			}
		}
		*/

		if( ::drawedges && ::drawedges != 2 )
		{
			double px,py,z;
			file << "<g stroke=\"black\">" << std::endl;
			file << "<polyline fill=\"none\" points=\"";
Kirill Terekhov's avatar
Kirill Terekhov committed
1301
			for(unsigned k = 0; k < verts.size()+3; k+=3)
Kirill Terekhov's avatar
Kirill Terekhov committed
1302
1303
1304
1305
1306
1307
1308
1309
1310
			{
				gluProject(verts[k%verts.size()+0],verts[k%verts.size()+1],verts[k%verts.size()+2],modelview,projection,viewport,&px,&py,&z); py = height-py;
				file << px << "," << py << " ";
			}
			file << "\" />" << std::endl;
			file << "</g>" << std::endl;
		}
		
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1311
1312
	void draw_colour_alpha(double alpha) const
	{
1313
		if( texcoords.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1314
		{
1315
			if( colors.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1316
			{
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
				//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
1343
1344
1345
1346
1347
1348
			}
		}
		else
		{
			for(unsigned k = 0; k < verts.size(); k+=3) 
			{
1349
				glTexCoord1d(cnttexcoord);
Kirill Terekhov's avatar
Kirill Terekhov committed
1350
				glVertex3dv(cnt);
1351
				glTexCoord1d(texcoords[k/3]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1352
				glVertex3dv(&verts[k]);
1353
				glTexCoord1d(texcoords[(k/3+1)%texcoords.size()]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1354
1355
				glVertex3dv(&verts[(k+3)%verts.size()]);
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
		}
	}
	void draw() const
	{
		for(unsigned k = 0; k < verts.size(); k+=3) 
		{
			glVertex3dv(cnt);
			glVertex3dv(&verts[k]);
			glVertex3dv(&verts[(k+3)%verts.size()]);
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
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
	void svg_draw(std::ostream & file, double modelview[16],double projection[16],int viewport[4]) const
	{
		double pcntx, pcnty, z;
		double pverts1x, pverts1y;
		double pverts2x, pverts2y;
		gluProject(cnt[0],cnt[1],cnt[2],modelview,projection,viewport,&pcntx,&pcnty,&z); pcnty = height-pcnty;
		file << "<g stroke=\"none\">" << std::endl;
		for(unsigned k = 0; k < verts.size(); k+=3) 
		{
			gluProject(verts[k+0],verts[k+1],verts[k+2],modelview,projection,viewport,&pverts1x,&pverts1y,&z); pverts1y = height-pverts1y;
			gluProject(verts[(k+3)%verts.size()+0],verts[(k+3)%verts.size()+1],verts[(k+3)%verts.size()+2],modelview,projection,viewport,&pverts2x,&pverts2y,&z); pverts2y = height-pverts2y;
			file << "<polygon points=\"" << pcntx <<"," << pcnty << " " << pverts1x << "," << pverts1y << " " << pverts2x << "," << pverts2y << "\"/>" << std::endl;
		}
		file << "</g>" << std::endl;
		if( ::drawedges && ::drawedges != 2 )
		{
			double px,py,z;
			file << "<g stroke=\"black\">" << std::endl;
			file << "<polyline fill=\"none\" points=\"";
			for(unsigned k = 0; k < verts.size(); k+=3) 
			{
				gluProject(verts[k%verts.size()+0],verts[k%verts.size()+1],verts[k%verts.size()+2],modelview,projection,viewport,&px,&py,&z); py = height-py;
				file << px << "," << py << " ";
			}
			file << "\" />" << std::endl;
			file << "</g>" << std::endl;
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1395
1396
1397
1398
1399
1400
1401
1402
	void drawedges() const
	{
		for(unsigned k = 0; k < verts.size(); k+=3) 
		{
			glVertex3dv(&verts[k]); 
			glVertex3dv(&verts[(k+3)%verts.size()]); 
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
	void svg_drawedges(std::ostream & file, double modelview[16],double projection[16],int viewport[4]) const
	{
		double px, py, z;
		file << "<polyline fill=\"none\" points=\"";
		for(unsigned k = 0; k < verts.size(); k+=3) 
		{
			gluProject(verts[k%verts.size()+0],verts[k%verts.size()+1],verts[k%verts.size()+2],modelview,projection,viewport,&px,&py,&z); py = height-py;
			file << px << "," << py << " ";
		}
		file << "\" />" << std::endl;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1414
1415
	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
1416
	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
1417
	void add_vert(double v[3]) {verts.insert(verts.end(),v,v+3);}
Kirill Terekhov's avatar
Kirill Terekhov committed
1418
	void add_color(color_t c) {colors.push_back(c);}
1419
	void add_texcoord(double val) {texcoords.push_back(val);}
Kirill Terekhov's avatar
Kirill Terekhov committed
1420
	double * get_vert(int k) {return &verts[k*3];}
Kirill Terekhov's avatar
Kirill Terekhov committed
1421
	unsigned size() {return (unsigned)verts.size()/3;}
Kirill Terekhov's avatar
Kirill Terekhov committed
1422
	void set_center(double _cnt[3], color_t c = color_t(0,0,0,0))
Kirill Terekhov's avatar
Kirill Terekhov committed
1423
1424
1425
1426
	{
		cnt[0] = _cnt[0];
		cnt[1] = _cnt[1];
		cnt[2] = _cnt[2];
Kirill Terekhov's avatar
Kirill Terekhov committed
1427
		cntcolor = c;
Kirill Terekhov's avatar
Kirill Terekhov committed
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
	}
	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
1445
		for(INMOST_DATA_ENUM_TYPE k = 0; k < verts.size(); k+=3)
Kirill Terekhov's avatar
Kirill Terekhov committed
1446
1447
1448
1449
1450
1451
1452
1453
		{
			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
1454
		compute_center_color();
1455
		compute_center_texcoord();
Kirill Terekhov's avatar
Kirill Terekhov committed
1456
1457
1458
	}
	void compute_center_color()
	{
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
		if( !colors.empty() )
		{
			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()));
		}
	}
	void compute_center_texcoord()
	{
		if( !texcoords.empty() )
		{
			cnttexcoord = 0.0;
			for(INMOST_DATA_ENUM_TYPE k = 0; k < texcoords.size(); k++)
				cnttexcoord += texcoords[k];
			cnttexcoord /= static_cast<double>(texcoords.size());
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1479
1480
1481
1482
1483
1484
1485
1486
	}
	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
1487
	Element get_elem(Mesh * m) {return m->ElementByLocalID(etype,id);}
Kirill Terekhov's avatar
Kirill Terekhov committed
1488
};
Kirill Terekhov's avatar
Kirill Terekhov committed
1489
face2gl DrawFace(Element f);
Kirill Terekhov's avatar
Kirill Terekhov committed
1490
1491

std::vector<face2gl> all_boundary;
Kirill Terekhov's avatar
Kirill Terekhov committed
1492
std::vector<face2gl> added_faces;
Kirill Terekhov's avatar
Kirill Terekhov committed
1493
1494
std::vector<face2gl> clip_boundary;

Kirill Terekhov's avatar
Kirill Terekhov committed
1495
1496
void draw_faces_nc(std::vector<face2gl> & set, int highlight = -1)
{
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1497
1498
	if( drawedges == 2 || drawedges == 3 ) return;
	glEnable(GL_POLYGON_OFFSET_FILL);
1499
	glPolygonOffset(1.0, 1.0);
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1500
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
	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
Updates    
Kirill Terekhov committed
1512
	glDisable(GL_POLYGON_OFFSET_FILL);
Kirill Terekhov's avatar
Kirill Terekhov committed
1513
1514
}

Kirill Terekhov's avatar
Kirill Terekhov committed
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
void svg_draw_faces_nc(std::ostream & file, std::vector<face2gl> & set, double modelview[16], double projection[16], int viewport[4], int highlight = -1)
{
	if( drawedges == 2 || drawedges == 3 ) return;
	
	file << "<g stroke=\"none\" fill=\"green\" fill-opacity=\"0.1\">" << std::endl;
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) set[q].svg_draw(file,modelview,projection,viewport);
	file << "</g>" << std::endl;
	if( highlight != -1 )
	{
		file << "<g fill=\"red\" fill-opacity=\"1\">" << std::endl;
		set[highlight].svg_draw(file,modelview,projection,viewport);
		file << "</g>" << std::endl;
	}
}

Kirill Terekhov's avatar
Kirill Terekhov committed
1530
1531
void draw_faces(std::vector<face2gl> & set, int highlight = -1)
{
1532
	if( drawedges == 2 || drawedges == 3) return;
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1533
	glEnable(GL_POLYGON_OFFSET_FILL);
1534
	glPolygonOffset(1.0, 1.0);
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1535
	
1536
	if( visualization_tag.isValid() ) CommonColorBar->BindTexture();
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1537
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1538
	glBegin(GL_TRIANGLES);
Kirill Terekhov's avatar
Kirill Terekhov committed
1539
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) set[q].draw_colour();
Kirill Terekhov's avatar
Kirill Terekhov committed
1540
	glEnd();
1541
	if( visualization_tag.isValid() ) CommonColorBar->UnbindTexture();
Kirill Terekhov's avatar
Kirill Terekhov committed
1542
1543
1544
1545
1546
1547
1548
	if( highlight != -1 )
	{
		glColor4f(1,0,0,1);
		glBegin(GL_TRIANGLES);
		set[highlight].draw();
		glEnd();
	}
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1549
1550
1551
	
	glDisable(GL_POLYGON_OFFSET_FILL);

Kirill Terekhov's avatar
Kirill Terekhov committed
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
}

void svg_draw_faces(std::ostream & file, std::vector<face2gl> & set, double modelview[16], double projection[16], int viewport[4], int highlight = -1)
{
	if( drawedges == 2 || drawedges == 3) return;
	
	
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) set[q].svg_draw_colour(file,modelview,projection,viewport);
	
	
	if( highlight != -1 )
	{
		file << "<g color=\"red\" fill-opacity=\"1\">" << std::endl;
		set[highlight].svg_draw(file,modelview,projection,viewport);
		file << "</g>" << std::endl;
	}
	
	

Kirill Terekhov's avatar
Kirill Terekhov committed
1571
1572
}