main.cpp 163 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

Kirill Terekhov's avatar
Kirill Terekhov committed
45
46
47
48
49
50
51
52
53
54
55
#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
56
ElementArray<Element> boundary_faces;
Kirill Terekhov's avatar
Kirill Terekhov committed
57
58
59
ElementArray<Edge> added_edges;
std::vector<double> harmonic_points, dual_harmonic_points, conormals;

60
61


Kirill Terekhov's avatar
Kirill Terekhov committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

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
91

Kirill Terekhov's avatar
Kirill Terekhov committed
92
93
94
double amplitude = 10;
double radius = 25;
char visualization_prompt[8192];
Kirill Terekhov's avatar
Kirill Terekhov committed
95
int visualization_prompt_active = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
96
97
Tag visualization_tag;
ElementType visualization_type;
98
bool visualization_smooth = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
99

Kirill Terekhov's avatar
Kirill Terekhov committed
100
101
102
103
void printtext(const char * fmt, ... )
{
	
	unsigned int i;
Kirill Terekhov's avatar
Kirill Terekhov committed
104
	char stext[4096];
Kirill Terekhov's avatar
Kirill Terekhov committed
105
106
107
108
109
110
111
112
113
114
115
116
	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
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
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

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
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
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
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
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
654
  const int tiles = 4;
Kirill Terekhov's avatar
Kirill Terekhov committed
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
  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
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
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
741
742
743
	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
744
745
746
747
748
749
750
751
};

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;
752
	unsigned texture;
753
	int samples;
Kirill Terekhov's avatar
Kirill Terekhov committed
754
755
756
757
758
759
public:
	color_bar()
	{
		min = 0;
		max = 1;
		comment = "";
760
761

		/*
Kirill Terekhov's avatar
Kirill Terekhov committed
762
763
764
765
766
767
		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
768

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

800
801
802
		samples = 2048;

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

Kirill Terekhov's avatar
Kirill Terekhov committed
804
805
		
		
806
		for(int q = 0; q < samples+2; ++q)
807
		{
808
			float t = 1.0f*q/static_cast<float>(samples+1);
809
			color_t c = pick_color(t);
810
811
812
813
			//countour lines
			//if( ((q+1) % 64 == 0 || (q+1) % 64 == 63) && (q+1) < samples ) 
			//	c = pick_color(1-t) + color_t(0,2*t*(1-t),0); 

814
815
816
817
818
819
820
821
822
823
824
			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;

825
826
827
828
		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;
829
830
831
832
833
834
835
836
837

		
		
		glEnable(GL_TEXTURE);
		glEnable(GL_TEXTURE_1D);
		glGenTextures(1,&texture);
		glBindTexture(GL_TEXTURE_1D,texture);
		glPixelStorei(GL_UNPACK_ALIGNMENT,1);
		
838
		glTexImage1D(GL_TEXTURE_1D,0,4,samples+2,1,GL_RGBA,GL_FLOAT,pixel_array);
839
840
841
842
843
844
845
846
847
848
849
850

		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
851
852
853
854
855
		//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;}
856
857
	float get_min() { return min;}
	float get_max() { return max;}
Kirill Terekhov's avatar
Kirill Terekhov committed
858
	color_t pick_color(float value) const
Kirill Terekhov's avatar
Kirill Terekhov committed
859
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
860
861
862
863
		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
864
		float t = (value-min)/(max-min);
Kirill Terekhov's avatar
Kirill Terekhov committed
865
		std::vector<float>::const_iterator it = std::lower_bound(ticks.begin(),ticks.end(),t);
Kirill Terekhov's avatar
Kirill Terekhov committed
866
867
868
869
870
871
872
873
874
875
876
877
		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));
	}
878
879
880
881
882
883
884
885
886
887
888
	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
889
	double pick_texture(double value) const
890
	{
891
		double eps = 1.0/static_cast<double>(samples);
892
893
894
		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
895
896
897
898
899
900
901
	void Draw()
	{
		float text_pos = -0.89;
		float left = -0.95;
		float right = -0.9;
		float bottom = -0.75;
		float top = 0.75;
902
		BindTexture();
Kirill Terekhov's avatar
Kirill Terekhov committed
903
		glBegin(GL_QUADS);
904
905
906
907
908
909
910
911

		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
912
913
914
915
916
917
918
919
920
		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));
		}
921
		*/
Kirill Terekhov's avatar
Kirill Terekhov committed
922
		glEnd();
923
		UnbindTexture();
924
925

		int tickmarks = 11;
Kirill Terekhov's avatar
Kirill Terekhov committed
926
927
		
		glColor4f(0,0,0,1);
928
		for(int i = 0; i < tickmarks; ++i)
Kirill Terekhov's avatar
Kirill Terekhov committed
929
		{
930
931
932
			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
933
934
935
936
937
938
		}
		if( comment != "")
		{
			glRasterPos2f(left,bottom-0.04);
			printtext("%s",comment.c_str());
		}
939
940
941
942
943
944
945
946
947

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

		glBegin(GL_LINES);
948
		for(int i = 0; i < tickmarks; ++i)
949
		{
950
951
			float t = 1.0f*i/static_cast<float>(tickmarks-1);
			float pos = bottom+t*(top-bottom);
952
953
954
955
956
957
958
			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
959
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016

	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;
	}
	
1017
} * CommonColorBar;
Kirill Terekhov's avatar
Kirill Terekhov committed
1018

Kirill Terekhov's avatar
Kirill Terekhov committed
1019
1020
1021

class face2gl
{
Kirill Terekhov's avatar
Kirill Terekhov committed
1022
	float dist;
Kirill Terekhov's avatar
Kirill Terekhov committed
1023
1024
1025
1026
1027
1028
	double c[4];
	double cnt[3];
	bool flag;
	ElementType etype;
	Storage::integer id;
	std::vector<double> verts;
1029
	std::vector<double> texcoords;
Kirill Terekhov's avatar
Kirill Terekhov committed
1030
1031
	std::vector<color_t> colors;
	color_t cntcolor;
1032
	double cnttexcoord;
Kirill Terekhov's avatar
Kirill Terekhov committed
1033
public:
Kirill Terekhov's avatar
Kirill Terekhov committed
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
	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];
	}
1063
	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
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
	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
1077
		colors = other.colors;
1078
		texcoords = other.texcoords;
Kirill Terekhov's avatar
Kirill Terekhov committed
1079
		cntcolor = other.cntcolor;
1080
		cnttexcoord = other.cnttexcoord;
Kirill Terekhov's avatar
Kirill Terekhov committed
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
	}
	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
1096
1097
		colors = other.colors;
		cntcolor = other.cntcolor;
1098
1099
		texcoords = other.texcoords;
		cnttexcoord = other.cnttexcoord;
Kirill Terekhov's avatar
Kirill Terekhov committed
1100
1101
1102
1103
1104
		return *this;
	}
	~face2gl() {}
	void draw_colour() const
	{
1105
		if( texcoords.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1106
		{
1107
			if( colors.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1108
			{
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
				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
1128
1129
1130
1131
1132
1133
			}
		}
		else
		{
			for(unsigned k = 0; k < verts.size(); k+=3) 
			{
1134
				glTexCoord1d(cnttexcoord);
Kirill Terekhov's avatar
Kirill Terekhov committed
1135
				glVertex3dv(cnt);
1136
				glTexCoord1d(texcoords[k/3]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1137
				glVertex3dv(&verts[k]);
1138
				glTexCoord1d(texcoords[(k/3+1)%texcoords.size()]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1139
1140
1141
1142
				glVertex3dv(&verts[(k+3)%verts.size()]);
			}
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
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
	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=\"";
			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
1294
1295
	void draw_colour_alpha(double alpha) const
	{
1296
		if( texcoords.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1297
		{
1298
			if( colors.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1299
			{
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
				//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
1326
1327
1328
1329
1330
1331
			}
		}
		else
		{
			for(unsigned k = 0; k < verts.size(); k+=3) 
			{
1332
				glTexCoord1d(cnttexcoord);
Kirill Terekhov's avatar
Kirill Terekhov committed
1333
				glVertex3dv(cnt);
1334
				glTexCoord1d(texcoords[k/3]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1335
				glVertex3dv(&verts[k]);
1336
				glTexCoord1d(texcoords[(k/3+1)%texcoords.size()]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1337
1338
				glVertex3dv(&verts[(k+3)%verts.size()]);
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
		}
	}
	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
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
	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
1378
1379
1380
1381
1382
1383
1384
1385
	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
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
	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
1397
1398
	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
1399
	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
1400
	void add_vert(double v[3]) {verts.insert(verts.end(),v,v+3);}
Kirill Terekhov's avatar
Kirill Terekhov committed
1401
	void add_color(color_t c) {colors.push_back(c);}
1402
	void add_texcoord(double val) {texcoords.push_back(val);}
Kirill Terekhov's avatar
Kirill Terekhov committed
1403
	double * get_vert(int k) {return &verts[k*3];}
Kirill Terekhov's avatar
Kirill Terekhov committed
1404
	unsigned size() {return (unsigned)verts.size()/3;}
Kirill Terekhov's avatar
Kirill Terekhov committed
1405
	void set_center(double _cnt[3], color_t c = color_t(0,0,0,0))
Kirill Terekhov's avatar
Kirill Terekhov committed
1406
1407
1408
1409
	{
		cnt[0] = _cnt[0];
		cnt[1] = _cnt[1];
		cnt[2] = _cnt[2];
Kirill Terekhov's avatar
Kirill Terekhov committed
1410
		cntcolor = c;
Kirill Terekhov's avatar
Kirill Terekhov committed
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
	}
	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
1428
		for(INMOST_DATA_ENUM_TYPE k = 0; k < verts.size(); k+=3)
Kirill Terekhov's avatar
Kirill Terekhov committed
1429
1430
1431
1432
1433
1434
1435
1436
		{
			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
1437
		compute_center_color();
1438
		compute_center_texcoord();
Kirill Terekhov's avatar
Kirill Terekhov committed
1439
1440
1441
	}
	void compute_center_color()
	{
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
		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
1462
1463
1464
1465
1466
1467
1468
1469
	}
	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
1470
	Element get_elem(Mesh * m) {return m->ElementByLocalID(etype,id);}
Kirill Terekhov's avatar
Kirill Terekhov committed
1471
};
Kirill Terekhov's avatar
Kirill Terekhov committed
1472
face2gl DrawFace(Element f);
Kirill Terekhov's avatar
Kirill Terekhov committed
1473
1474

std::vector<face2gl> all_boundary;
Kirill Terekhov's avatar
Kirill Terekhov committed
1475
std::vector<face2gl> added_faces;
Kirill Terekhov's avatar
Kirill Terekhov committed
1476
1477
std::vector<face2gl> clip_boundary;

Kirill Terekhov's avatar
Kirill Terekhov committed
1478
1479
void draw_faces_nc(std::vector<face2gl> & set, int highlight = -1)
{
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1480
1481
	if( drawedges == 2 || drawedges == 3 ) return;
	glEnable(GL_POLYGON_OFFSET_FILL);
1482
	glPolygonOffset(1.0, 1.0);
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1483
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
	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
1495
	glDisable(GL_POLYGON_OFFSET_FILL);
Kirill Terekhov's avatar
Kirill Terekhov committed
1496
1497
}

Kirill Terekhov's avatar
Kirill Terekhov committed
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
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
1513
1514
void draw_faces(std::vector<face2gl> & set, int highlight = -1)
{
1515
	if( drawedges == 2 || drawedges == 3) return;
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1516
	glEnable(GL_POLYGON_OFFSET_FILL);
1517
	glPolygonOffset(1.0, 1.0);
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1518
	
1519
	if( visualization_tag.isValid() ) CommonColorBar->BindTexture();
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1520
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1521
	glBegin(GL_TRIANGLES);
Kirill Terekhov's avatar
Kirill Terekhov committed
1522
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) set[q].draw_colour();
Kirill Terekhov's avatar
Kirill Terekhov committed
1523
	glEnd();
1524
	if( visualization_tag.isValid() ) CommonColorBar->UnbindTexture();
Kirill Terekhov's avatar
Kirill Terekhov committed
1525
1526
1527
1528
1529
1530
1531
	if( highlight != -1 )
	{
		glColor4f(1,0,0,1);
		glBegin(GL_TRIANGLES);
		set[highlight].draw();
		glEnd();
	}
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1532
1533
1534
	
	glDisable(GL_POLYGON_OFFSET_FILL);

Kirill Terekhov's avatar
Kirill Terekhov committed
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
}

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
1554
1555
}

Kirill Terekhov's avatar
Kirill Terekhov committed
1556
1557
void draw_faces_alpha(std::vector<face2gl> & set, double alpha)
{
1558
	if( drawedges == 2 || drawedges==3) return;
1559
	if( visualization_tag.isValid() ) CommonColorBar->BindTexture();
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1560
1561
	
	glEnable(GL_POLYGON_OFFSET_FILL);
1562
	glPolygonOffset(1.0, 1.0);
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1563
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1564
1565
1566
	glBegin(GL_TRIANGLES);
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) set[q].draw_colour_alpha(alpha);
	glEnd();
1567
	if( visualization_tag.isValid() ) CommonColorBar->UnbindTexture();
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1568
1569
	
	glDisable(GL_POLYGON_OFFSET_FILL);
Kirill Terekhov's avatar
Kirill Terekhov committed
1570
1571
}