main.cpp 141 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
29
30
31
32

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

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

57
58


Kirill Terekhov's avatar
Kirill Terekhov committed
59
60
61
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

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
88

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

Kirill Terekhov's avatar
Kirill Terekhov committed
97
98
99
100
void printtext(const char * fmt, ... )
{
	
	unsigned int i;
Kirill Terekhov's avatar
Kirill Terekhov committed
101
	char stext[4096];
Kirill Terekhov's avatar
Kirill Terekhov committed
102
103
104
105
106
107
108
109
110
111
112
113
	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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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

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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
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()
{
  const int tiles = 8;
  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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
struct color_t
{
	float c[4];
	color_t() {memset(c,0,sizeof(float)*4);}
	color_t(float r, float g, float b)
	{
		c[0] = r;
		c[1] = g;
		c[2] = b;
		c[3] = 1.0;
	}
	color_t(float r, float g, float b, float a)
	{
		c[0] = r;
		c[1] = g;
		c[2] = b;
		c[3] = a;
	}
	color_t(const color_t & other) {memcpy(c,other.c,sizeof(float)*4);}
	color_t & operator =(color_t const & other)
	{
		memmove(c,other.c,sizeof(float)*4);
		return *this;
	}
	void set_color() const {glColor4fv(c);}
	float & r() {return c[0];}
	float & g() {return c[1];}
	float & b() {return c[2];}
	float & a() {return c[3];}
	float r() const {return c[0];}
	float g() const {return c[1];}
	float b() const {return c[2];}
	float a() const {return c[3];}
	color_t operator *(float mult){return color_t(c[0]*mult,c[1]*mult,c[2]*mult,c[3]*mult);}
	color_t operator +(color_t other){return color_t(c[0]+other.c[0],c[1]+other.c[1],c[2]+other.c[2],c[3]+other.c[3]);}
740
	color_t operator -(color_t other){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
741
742
743
744
745
746
747
748
};

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

		/*
Kirill Terekhov's avatar
Kirill Terekhov committed
759
760
761
762
763
764
		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
765

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

797
798
799
		samples = 2048;

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

Kirill Terekhov's avatar
Kirill Terekhov committed
801
802
		
		
803
		for(int q = 0; q < samples+2; ++q)
804
		{
805
			float t = 1.0f*q/static_cast<float>(samples+1);
806
			color_t c = pick_color(t);
807
808
809
810
			//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); 

811
812
813
814
815
816
817
818
819
820
821
			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;

822
823
824
825
		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;
826
827
828
829
830
831
832
833
834

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

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

		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
905
906
907
908
909
910
911
912
913
		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));
		}
914
		*/
Kirill Terekhov's avatar
Kirill Terekhov committed
915
		glEnd();
916
		UnbindTexture();
917
918

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

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

		glBegin(GL_LINES);
941
		for(int i = 0; i < tickmarks; ++i)
942
		{
943
944
			float t = 1.0f*i/static_cast<float>(tickmarks-1);
			float pos = bottom+t*(top-bottom);
945
946
947
948
949
950
951
			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
952
	}
953
} * CommonColorBar;
Kirill Terekhov's avatar
Kirill Terekhov committed
954

Kirill Terekhov's avatar
Kirill Terekhov committed
955
956
957

class face2gl
{
Kirill Terekhov's avatar
Kirill Terekhov committed
958
	float dist;
Kirill Terekhov's avatar
Kirill Terekhov committed
959
960
961
962
963
964
	double c[4];
	double cnt[3];
	bool flag;
	ElementType etype;
	Storage::integer id;
	std::vector<double> verts;
965
	std::vector<double> texcoords;
Kirill Terekhov's avatar
Kirill Terekhov committed
966
967
	std::vector<color_t> colors;
	color_t cntcolor;
968
	double cnttexcoord;
Kirill Terekhov's avatar
Kirill Terekhov committed
969
public:
Kirill Terekhov's avatar
Kirill Terekhov committed
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
	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];
	}
999
	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
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
	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
1013
		colors = other.colors;
1014
		texcoords = other.texcoords;
Kirill Terekhov's avatar
Kirill Terekhov committed
1015
		cntcolor = other.cntcolor;
1016
		cnttexcoord = other.cnttexcoord;
Kirill Terekhov's avatar
Kirill Terekhov committed
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
	}
	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
1032
1033
		colors = other.colors;
		cntcolor = other.cntcolor;
1034
1035
		texcoords = other.texcoords;
		cnttexcoord = other.cnttexcoord;
Kirill Terekhov's avatar
Kirill Terekhov committed
1036
1037
1038
1039
1040
		return *this;
	}
	~face2gl() {}
	void draw_colour() const
	{
1041
		if( texcoords.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1042
		{
1043
			if( colors.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1044
			{
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
				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
1064
1065
1066
1067
1068
1069
			}
		}
		else
		{
			for(unsigned k = 0; k < verts.size(); k+=3) 
			{
1070
				glTexCoord1d(cnttexcoord);
Kirill Terekhov's avatar
Kirill Terekhov committed
1071
				glVertex3dv(cnt);
1072
				glTexCoord1d(texcoords[k/3]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1073
				glVertex3dv(&verts[k]);
1074
				glTexCoord1d(texcoords[(k/3+1)%texcoords.size()]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1075
1076
1077
1078
1079
1080
				glVertex3dv(&verts[(k+3)%verts.size()]);
			}
		}
	}
	void draw_colour_alpha(double alpha) const
	{
1081
		if( texcoords.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1082
		{
1083
			if( colors.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1084
			{
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
				//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
1111
1112
1113
1114
1115
1116
			}
		}
		else
		{
			for(unsigned k = 0; k < verts.size(); k+=3) 
			{
1117
				glTexCoord1d(cnttexcoord);
Kirill Terekhov's avatar
Kirill Terekhov committed
1118
				glVertex3dv(cnt);
1119
				glTexCoord1d(texcoords[k/3]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1120
				glVertex3dv(&verts[k]);
1121
				glTexCoord1d(texcoords[(k/3+1)%texcoords.size()]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1122
1123
				glVertex3dv(&verts[(k+3)%verts.size()]);
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
		}
	}
	void draw() const
	{
		for(unsigned k = 0; k < verts.size(); k+=3) 
		{
			glVertex3dv(cnt);
			glVertex3dv(&verts[k]);
			glVertex3dv(&verts[(k+3)%verts.size()]);
		}
	}
	void drawedges() const
	{
		for(unsigned k = 0; k < verts.size(); k+=3) 
		{
			glVertex3dv(&verts[k]); 
			glVertex3dv(&verts[(k+3)%verts.size()]); 
		}
	}
	bool operator <(const face2gl & other) const {return dist < other.dist;}
	void set_color(double r, double g, double b, double a) {c[0] = r; c[1] = g; c[2] = b; c[3] = a;}
Kirill Terekhov's avatar
Kirill Terekhov committed
1145
	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
1146
	void add_vert(double v[3]) {verts.insert(verts.end(),v,v+3);}
Kirill Terekhov's avatar
Kirill Terekhov committed
1147
	void add_color(color_t c) {colors.push_back(c);}
1148
	void add_texcoord(double val) {texcoords.push_back(val);}
Kirill Terekhov's avatar
Kirill Terekhov committed
1149
	double * get_vert(int k) {return &verts[k*3];}
Kirill Terekhov's avatar
Kirill Terekhov committed
1150
	unsigned size() {return (unsigned)verts.size()/3;}
Kirill Terekhov's avatar
Kirill Terekhov committed
1151
	void set_center(double _cnt[3], color_t c = color_t(0,0,0,0))
Kirill Terekhov's avatar
Kirill Terekhov committed
1152
1153
1154
1155
	{
		cnt[0] = _cnt[0];
		cnt[1] = _cnt[1];
		cnt[2] = _cnt[2];
Kirill Terekhov's avatar
Kirill Terekhov committed
1156
		cntcolor = c;
Kirill Terekhov's avatar
Kirill Terekhov committed
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
	}
	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
1174
		for(INMOST_DATA_ENUM_TYPE k = 0; k < verts.size(); k+=3)
Kirill Terekhov's avatar
Kirill Terekhov committed
1175
1176
1177
1178
1179
1180
1181
1182
		{
			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
1183
		compute_center_color();
1184
		compute_center_texcoord();
Kirill Terekhov's avatar
Kirill Terekhov committed
1185
1186
1187
	}
	void compute_center_color()
	{
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
		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
1208
1209
1210
1211
1212
1213
1214
1215
	}
	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
1216
	Element get_elem(Mesh * m) {return m->ElementByLocalID(etype,id);}
Kirill Terekhov's avatar
Kirill Terekhov committed
1217
};
Kirill Terekhov's avatar
Kirill Terekhov committed
1218
face2gl DrawFace(Element f);
Kirill Terekhov's avatar
Kirill Terekhov committed
1219
1220

std::vector<face2gl> all_boundary;
Kirill Terekhov's avatar
Kirill Terekhov committed
1221
std::vector<face2gl> added_faces;
Kirill Terekhov's avatar
Kirill Terekhov committed
1222
1223
std::vector<face2gl> clip_boundary;

Kirill Terekhov's avatar
Kirill Terekhov committed
1224
1225
void draw_faces_nc(std::vector<face2gl> & set, int highlight = -1)
{
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1226
1227
	if( drawedges == 2 || drawedges == 3 ) return;
	glEnable(GL_POLYGON_OFFSET_FILL);
1228
	glPolygonOffset(1.0, 1.0);
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1229
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
	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
1241
	glDisable(GL_POLYGON_OFFSET_FILL);
Kirill Terekhov's avatar
Kirill Terekhov committed
1242
1243
}

Kirill Terekhov's avatar
Kirill Terekhov committed
1244
1245
void draw_faces(std::vector<face2gl> & set, int highlight = -1)
{
1246
	if( drawedges == 2 || drawedges == 3) return;
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1247
	glEnable(GL_POLYGON_OFFSET_FILL);
1248
	glPolygonOffset(1.0, 1.0);
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1249
	
1250
	if( visualization_tag.isValid() ) CommonColorBar->BindTexture();
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1251
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1252
	glBegin(GL_TRIANGLES);
Kirill Terekhov's avatar
Kirill Terekhov committed
1253
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) set[q].draw_colour();
Kirill Terekhov's avatar
Kirill Terekhov committed
1254
	glEnd();
1255
	if( visualization_tag.isValid() ) CommonColorBar->UnbindTexture();
Kirill Terekhov's avatar
Kirill Terekhov committed
1256
1257
1258
1259
1260
1261
1262
	if( highlight != -1 )
	{
		glColor4f(1,0,0,1);
		glBegin(GL_TRIANGLES);
		set[highlight].draw();
		glEnd();
	}
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1263
1264
1265
	
	glDisable(GL_POLYGON_OFFSET_FILL);

Kirill Terekhov's avatar
Kirill Terekhov committed
1266
1267
}

Kirill Terekhov's avatar
Kirill Terekhov committed
1268
1269
void draw_faces_alpha(std::vector<face2gl> & set, double alpha)
{
1270
	if( drawedges == 2 || drawedges==3) return;
1271
	if( visualization_tag.isValid() ) CommonColorBar->BindTexture();
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1272
1273
	
	glEnable(GL_POLYGON_OFFSET_FILL);
1274
	glPolygonOffset(1.0, 1.0);
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1275
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1276
1277
1278
	glBegin(GL_TRIANGLES);
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) set[q].draw_colour_alpha(alpha);
	glEnd();
1279
	if( visualization_tag.isValid() ) CommonColorBar->UnbindTexture();
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1280
1281
	
	glDisable(GL_POLYGON_OFFSET_FILL);
Kirill Terekhov's avatar
Kirill Terekhov committed
1282
1283
}

Kirill Terekhov's avatar
Kirill Terekhov committed
1284
1285
void draw_edges(std::vector<face2gl> & set, int highlight = -1)
{
Kirill Terekhov's avatar
Kirill Terekhov committed
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
  if( drawedges && drawedges != 2 )
  {
	  glBegin(GL_LINES);
	  for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) set[q].drawedges();
	  glEnd();
	  if( highlight != -1 )
	  {
		  glColor4f(0,1,0,1);
		  glBegin(GL_LINES);
		  set[highlight].drawedges();
		  glEnd();
	  }
  }
Kirill Terekhov's avatar
Kirill Terekhov committed
1299
1300
}

Kirill Terekhov's avatar
Kirill Terekhov committed
1301
1302
void draw_faces_interactive_nc(std::vector<face2gl> & set)
{
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1303
	if( drawedges == 2 || drawedges==3 ) return;
Kirill Terekhov's avatar
Kirill Terekhov committed
1304
	glColor4f(0,1,0,0.1);
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1305
1306
	
	glEnable(GL_POLYGON_OFFSET_FILL);
1307
	glPolygonOffset(1.0, 1.0);
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1308
1309
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1310
1311
1312
	glBegin(GL_TRIANGLES);
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) if( set[q].get_flag() ) set[q].draw();
	glEnd();
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1313
1314
	
	glDisable(GL_POLYGON_OFFSET_FILL);
Kirill Terekhov's avatar
Kirill Terekhov committed
1315
1316
}

Kirill Terekhov's avatar
Kirill Terekhov committed
1317
1318
void draw_faces_interactive(std::vector<face2gl> & set)
{
1319
  if( drawedges == 2 || drawedges==3 ) return;
1320
  if( visualization_tag.isValid() ) CommonColorBar->BindTexture();
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1321
1322
	
	glEnable(GL_POLYGON_OFFSET_FILL);
1323
	glPolygonOffset(1.0, 1.0);
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1324
1325
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1326
	glBegin(GL_TRIANGLES);
Kirill Terekhov's avatar
Kirill Terekhov committed
1327
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) if( set[q].get_flag() ) set[q].draw_colour();
Kirill Terekhov's avatar
Kirill Terekhov committed
1328
	glEnd();
1329
	if( visualization_tag.isValid() ) CommonColorBar->UnbindTexture();
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1330
1331
	
	glDisable(GL_POLYGON_OFFSET_FILL);
Kirill Terekhov's avatar
Kirill Terekhov committed
1332
1333
}

Kirill Terekhov's avatar
Kirill Terekhov committed
1334
1335
void draw_faces_interactive_alpha(std::vector<face2gl> & set, double alpha)
{
1336
  if( drawedges == 2 || drawedges==3 ) return;
1337
  if( visualization_tag.isValid() ) CommonColorBar->BindTexture();
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1338
1339
	
	glEnable(GL_POLYGON_OFFSET_FILL);
1340
	glPolygonOffset(1.0, 1.0);
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1341
1342
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1343
1344
1345
	glBegin(GL_TRIANGLES);
	for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) if( set[q].get_flag() ) set[q].draw_colour_alpha(alpha);
	glEnd();
1346
	if( visualization_tag.isValid() ) CommonColorBar->UnbindTexture();
Kirill Terekhov's avatar
Updates    
Kirill Terekhov committed
1347
1348
1349
	
	glDisable(GL_POLYGON_OFFSET_FILL);

Kirill Terekhov's avatar
Kirill Terekhov committed
1350
1351
}

Kirill Terekhov's avatar
Kirill Terekhov committed
1352
1353
void draw_edges_interactive(std::vector<face2gl> & set)
{
Kirill Terekhov's avatar
Kirill Terekhov committed
1354
1355
1356
1357
1358
1359
  if( drawedges && drawedges != 2 )
  {
	  glBegin(GL_LINES);
	  for(INMOST_DATA_ENUM_TYPE q = 0; q < set.size() ; q++) if( set[q].get_flag() ) set[q].drawedges();
	  glEnd();
  }
Kirill Terekhov's avatar
Kirill Terekhov committed
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
}

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

Kirill Terekhov's avatar
Kirill Terekhov committed
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
typedef struct point
{
	float coords[3];
	float diam;
	float dist;
	int id;
	/*
	point & operator = (point const & b)
	{
		coords[0] = b.coords[0];
		coords[1] = b.coords[1];
		coords[2] = b.coords[2];
		diam = b.diam;
		dist = b.dist;
		id = b.id;
	}

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

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




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

	void radix_sort_dist(std::vector<point_t> & set)
	{
		static std::vector<point_t> tmp;
		tmp.resize(set.size());
		unsigned int i;
		const unsigned int kHist = 2048;
		unsigned int  b0[kHist * 3];
		unsigned int *b1 = b0 + kHist;
		unsigned int *b2 = b1 + kHist;
		memset(b0,0,sizeof(unsigned int)*kHist*3);
		for (i = 0; i < set.size(); i++) 
		{
			unsigned int fi = flip((unsigned int *)&set[i].dist);
			++b0[_0(fi)]; ++b1[_1(fi)]; ++b2[_2(fi)];
		}
		{
			unsigned int sum0 = 0, sum1 = 0, sum2 = 0;
			for (i = 0; i < kHist; i++) 
			{
				b0[kHist-1] = b0[i] + sum0; b0[i] = sum0 - 1; sum0 = b0[kHist-1];
				b1[kHist-1] = b1[i] + sum1; b1[i] = sum1 - 1; sum1 = b1[kHist-1];
				b2[kHist-1] = b2[i] + sum2; b2[i] = sum2 - 1; sum2 = b2[kHist-1];
			}
		}
		for (i = 0; i < set.size(); i++) tmp[++b0[_0(flip((unsigned int *)&set[i].dist))]] = set[i];
		for (i = 0; i < set.size(); i++) set[++b1[_1(flip((unsigned int *)&tmp[i].dist))]] = tmp[i];
		for (i = 0; i < set.size(); i++) tmp[++b2[_2(flip((unsigned int *)&set[i].dist))]] = set[i];
		for (i = 0; i < set.size(); i++) set[i] = tmp[set.size()-1-i];
	}
public:
	
	volumetric(Mesh * _m)
	{
		m = _m;
		
		points.resize(m->NumberOfCells());
		int q = 0;
		for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); ++it)
		{
			Storage::real cnt[3], cntf[3];
			it->Centroid(cnt);
			points[q].coords[0] = cnt[0];
			points[q].coords[1] = cnt[1];
			points[q].coords[2] = cnt[2];
			points[q].id = it->LocalID();
			points[q].diam = 0.f;
			ElementArray<Face> faces = it->getFaces();
			for(ElementArray<Face>::iterator f = faces.begin(); f != faces.end(); ++f)
			{
				f->Centroid(cntf);
				Storage::real d = sqrt((cnt[0]-cntf[0])*(cnt[0]-cntf[0])+(cnt[1]-cntf[1])*(cnt[1]-cntf[1])+(cnt[2]-cntf[2])*(cnt[2]-cntf[2]));
				if( points[q].diam < d ) points[q].diam = d;
			}
			++q;
		}
		/*
		points.reserve(m->NumberOfNodes()*20);
		int q = 0;
		for(Mesh::iteratorNode it = m->BeginNode(); it != m->EndNode(); ++it)
		{
			point_t pnt;
			Storage::real_array cnt = it->Coords();
			pnt.coords[0] = cnt[0];
			pnt.coords[1] = cnt[1];
			pnt.coords[2] = cnt[2];
			pnt.id = it->LocalID();
			points.push_back(pnt);
			
			ElementArray<Element> adj = it->getAdjElements(CELL);
			for(ElementArray<Element>::iterator jt = adj.begin(); jt != adj.end(); ++jt)
			{
				Storage::real cnt2[3];
				jt->Centroid(cnt2);
				const int ncoefs = 1;
				const Storage::real coefs[ncoefs] = {0.82};
				for(int j = 0; j < ncoefs; ++j)
				{
					pnt.coords[0] = cnt[0]*(1-coefs[j]) + cnt2[0]*coefs[j];
					pnt.coords[1] = cnt[1]*(1-coefs[j]) + cnt2[1]*coefs[j];
					pnt.coords[2] = cnt[2]*(1-coefs[j]) + cnt2[2]*coefs[j];
					pnt.id = it->LocalID();
					points.push_back(pnt);
				}
			}
			
		}
		*/
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1514
		printf("number of points %d\n",(int)points.size());
Kirill Terekhov's avatar
Kirill Terekhov committed
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
	}
	void camera(double pos[3], int interactive)
	{
		if( interactive ) return;
		float posf[3];
		posf[0] = pos[0];
		posf[1] = pos[1];
		posf[2] = pos[2];
		for(int k = 0; k < points.size(); ++k)
		{
			points[k].dist = sqrtf(
				(posf[0]-points[k].coords[0])*(posf[0]-points[k].coords[0])+
				(posf[1]-points[k].coords[1])*(posf[1]-points[k].coords[1])+
				(posf[2]-points[k].coords[2])*(posf[2]-points[k].coords[2]));
		}
		double t = Timer();
		radix_sort_dist(points);
		//std::sort(points.rbegin(),points.rend());
		printf("Time to sort %lf\n",Timer()-t);
	}
	void draw(int interactive)
	{
		double origin[3], right[3], up[3];
		GLdouble modelview[16],projection[16];
		GLint viewport[4] = {0,0,1,1};
		glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
		glGetDoublev(GL_PROJECTION_MATRIX, projection);
		GLdouble outx, outy, outz;  // Var's to save the answer in
		gluUnProject(0.5, 0.5, 0.,
               modelview, projection, viewport,
               &outx, &outy, &outz);
		origin[0] = outx;
		origin[1] = outy;
		origin[2] = outz;
		gluUnProject(1.0, 0.5, 0.,
               modelview, projection, viewport,
               &outx, &outy, &outz);
		right[0] = outx;
		right[1] = outy;
		right[2] = outz;
		gluUnProject(0.5, 1.0, 0.,
               modelview, projection, viewport,
               &outx, &outy, &outz);
		up[0] = outx;
		up[1] = outy;
		up[2] = outz;
		right[0] -= origin[0];
		right[1] -= origin[1];
		right[2] -= origin[2];
		up[0] -= origin[0];
		up[1] -= origin[1];
		up[2] -= origin[2];
Kirill Terekhov's avatar
Kirill Terekhov committed
1567
    
Kirill Terekhov's avatar
Kirill Terekhov committed
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
		double l = sqrt(right[0]*right[0]+right[1]*right[1]+right[2]*right[2]);
		if( l )
		{
			right[0] /= l;
			right[1] /= l;
			right[2] /= l;
		}
		l = sqrt(up[0]*up[0]+up[1]*up[1]+up[2]*up[2]);
		if( l )
		{
			up[0] /= l;
			up[1] /= l;
			up[