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

Kirill Terekhov's avatar
Kirill Terekhov committed
5 6
//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>
13
#include "inc_glut.h"
Kirill Terekhov's avatar
Kirill Terekhov committed
14
#include <iomanip>
Kirill Terekhov's avatar
Kirill Terekhov committed
15

Kirill Terekhov's avatar
Kirill Terekhov committed
16 17 18
#include "color.h"
#include "coord.h"
#include "octree.h"
19 20 21 22 23 24 25
#include "streamline.h"
#include "svg_line.h"
#include "face2gl.h"
#include "color_bar.h"
#include "tga.h"
#include "screenshot.h"
#include "volumetric.h"
26
#include "vector.h"
27 28 29
#include "input.h"
#include "picker.h"
#include "clipper.h"
Kirill Terekhov's avatar
Kirill Terekhov committed
30 31 32 33 34 35 36 37 38 39

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
40
void draw_screen();
Kirill Terekhov's avatar
Kirill Terekhov committed
41
void svg_draw(std::ostream & file);
Kirill Terekhov's avatar
Kirill Terekhov committed
42 43 44 45 46 47

using namespace INMOST;
Mesh * mesh;
int interactive = 0;
double zoom = 1;
int width = 800, height = 800;
Kirill Terekhov's avatar
Kirill Terekhov committed
48 49 50
double sleft = 1e20, sright = -1e20, sbottom = 1e20, stop = -1e20, sfar = -1e20, snear = 1e20;
double shift[3] = {0,0,0};
bool perspective = false;
51
int drawedges = 0, draw_orphan = true;
52
bool boundary = true, planecontrol = false, clipupdate = false, bndupdate = true, clipboxupdate = false, draw_volumetric = false, elevation = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
53
bool drawaxis = true, drawcolorbar = true;
54
Element disp_e;
Kirill Terekhov's avatar
Kirill Terekhov committed
55
Mesh::GeomParam table;
56
ElementArray<Element> orphans;
Kirill Terekhov's avatar
Kirill Terekhov committed
57
int is_material_defined = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
58

Kirill Terekhov's avatar
Kirill Terekhov committed
59 60

Storage::real p[3] = {0,0,0}, n[3] = {0,0,1};
Kirill Terekhov's avatar
Kirill Terekhov committed
61
ElementArray<Element> boundary_faces;
Kirill Terekhov's avatar
Kirill Terekhov committed
62
ElementArray<Edge> added_edges;
Kirill Terekhov's avatar
Kirill Terekhov committed
63 64 65 66
std::map<int,std::vector<HandleType> > material_faces;
std::map<int,double> material_x;
std::map<int,double> material_y;
std::map<int,double> material_z;
Kirill Terekhov's avatar
Kirill Terekhov committed
67 68
std::vector<double> harmonic_points, dual_harmonic_points, conormals;

69 70


Kirill Terekhov's avatar
Kirill Terekhov committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

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
100

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

Kirill Terekhov's avatar
Kirill Terekhov committed
109 110 111 112

std::vector<Streamline> streamlines;


Kirill Terekhov's avatar
Kirill Terekhov committed
113 114 115 116 117 118 119 120 121 122 123
class segment
{
	coord v[2];
public:
	segment(coord a, coord b) 
	{
		v[0] = a; 
		v[1] = b;
	}
	segment(const segment & b)
	{
124 125
		v[0] = b.v[0];
		v[1] = b.v[1];
Kirill Terekhov's avatar
Kirill Terekhov committed
126
	}
127
	segment & operator =(segment const & b)
Kirill Terekhov's avatar
Kirill Terekhov committed
128
	{
129 130 131
		v[0] = b.v[0];
		v[1] = b.v[1];
		return *this;
Kirill Terekhov's avatar
Kirill Terekhov committed
132
	}
133
	void Draw()
Kirill Terekhov's avatar
Kirill Terekhov committed
134
	{
135 136
		glVertex3dv(v[0].data());
		glVertex3dv(v[1].data());
Kirill Terekhov's avatar
Kirill Terekhov committed
137
	}
138
	void SVGDraw(std::ostream & file, double modelview[16], double projection[16], int viewport[4])
Kirill Terekhov's avatar
Kirill Terekhov committed
139
	{
140 141 142
		double * v0 = v[0].data();
		double * v1 = v[1].data();
		svg_line(file, v0[0], v0[1], v0[2], v1[0], v1[1], v1[2], modelview, projection, viewport);
Kirill Terekhov's avatar
Kirill Terekhov committed
143
	}
144
};
Kirill Terekhov's avatar
Kirill Terekhov committed
145

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
std::vector<segment> segments;



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) )
Kirill Terekhov's avatar
Kirill Terekhov committed
165
		{
166 167
			print = print + elems[ElementNum(etype)];
			num++;
Kirill Terekhov's avatar
Kirill Terekhov committed
168 169
		}
	}
170 171 172
	std::cout << std::setw(elems_width) << print;
	print = "";
	for(ElementType etype = NODE; etype <= MESH; etype = etype << 1)
Kirill Terekhov's avatar
Kirill Terekhov committed
173
	{
174
		if( t.isSparse(etype) )
Kirill Terekhov's avatar
Kirill Terekhov committed
175
		{
176 177
			print = print + elems[ElementNum(etype)];
			num++;
Kirill Terekhov's avatar
Kirill Terekhov committed
178 179
		}
	}
180 181 182 183 184 185
	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;
}
Kirill Terekhov's avatar
Kirill Terekhov committed
186

187
void PrintTags(Mesh * m, ElementType etypes)
Kirill Terekhov's avatar
Kirill Terekhov committed
188
{
189 190
	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 )
Kirill Terekhov's avatar
Kirill Terekhov committed
191
	{
192 193 194
		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
195
	}
196 197 198 199 200 201 202 203 204 205 206 207 208 209
}






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



volumetric * CommonVolumetricView;
210
Vectors * CommonVectors = NULL;
211 212 213 214 215 216



clipper * oclipper = NULL;
bnd_clipper * bclipper = NULL;
picker * current_picker = NULL;
Kirill Terekhov's avatar
Kirill Terekhov committed
217 218


Kirill Terekhov's avatar
Kirill Terekhov committed
219
void set_matrix3d()
Kirill Terekhov's avatar
Kirill Terekhov committed
220
{
Kirill Terekhov's avatar
Kirill Terekhov committed
221 222 223
	double aspect = (double)width/(double)height;
	double side = std::max(std::max( sright-sleft, stop-sbottom ), sfar-snear)*0.5;
	//double center[3] = { (sleft+sright)*0.5, (sbottom+stop)*0.5, (sfar+snear)*0.5};
Kirill Terekhov's avatar
Kirill Terekhov committed
224 225 226
	const double sc = 2;
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
Kirill Terekhov's avatar
Kirill Terekhov committed
227 228 229 230 231 232 233 234 235 236
	//~ glOrtho(center[0]-sc*side*zoom*aspect,center[0]+sc*zoom*side*aspect,
		//~ center[1]-sc*side*zoom,center[1]+sc*zoom*side,
		//~ center[2]-sc*side*100,center[2]+sc*side*100);
	if( !perspective )
	{
		glOrtho(-sc*side*zoom*aspect,sc*side*zoom*aspect,
				-sc*side*zoom,sc*side*zoom,
				-sc*side*100,sc*side*100);
	}
	else
Kirill Terekhov's avatar
Kirill Terekhov committed
237 238
	{
		const double pi = 3.1415926535897932384626433832795;
Kirill Terekhov's avatar
Kirill Terekhov committed
239
		const double znear = zoom*2*std::max(std::max( sright-sleft, stop-sbottom ), sfar-snear)*0.1;
Kirill Terekhov's avatar
Kirill Terekhov committed
240
		const double zfar  = 1000000.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
241 242 243 244
		const double fH = znear * tan( 60.0 / 360.0 * pi);
		double fW = fH * aspect;
		glFrustum(-fW,fW,-fH,fH,znear,zfar);
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
245
	glMatrixMode (GL_MODELVIEW);
Kirill Terekhov's avatar
Kirill Terekhov committed
246
	glLoadIdentity();
Kirill Terekhov's avatar
Kirill Terekhov committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
}

void set_matrix2d()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-1,1,-1,1,-1,1);
	glMatrixMode(GL_MODELVIEW);
}

void reshape(int w, int h)
{
	width = w;
	height = h;
	set_matrix3d();
Kirill Terekhov's avatar
Kirill Terekhov committed
262 263 264
	glViewport(0, 0, w, h);
}

Kirill Terekhov's avatar
Kirill Terekhov committed
265 266 267 268 269 270 271 272

int actionstate  = 0;
double mymx = 0;
double mymy = 0;

void myclickmotion(int nmx, int nmy) // Mouse
{
	double lmx = 2.*(nmx/(double)width - 0.5),lmy = 2.*(0.5 - nmy/(double)height), dmx = lmx-mymx, dmy = lmy - mymy;
Kirill Terekhov's avatar
Kirill Terekhov committed
273
	if( actionstate == 1 ) //middle button
Kirill Terekhov's avatar
Kirill Terekhov committed
274 275
	{
		double shiftmod[3] = {0,0,0};
Kirill Terekhov's avatar
Kirill Terekhov committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
		shiftmod[0] += dmx*zoom*std::max(std::max( sright-sleft, stop-sbottom ), sfar-snear);
		shiftmod[1] += dmy*zoom*std::max(std::max( sright-sleft, stop-sbottom ), sfar-snear);
		if( planecontrol )
		{
			
			rotatevector_from_stack((double*)shiftmod);
			p[0] += shiftmod[0];
			p[1] += shiftmod[1];
			p[2] += shiftmod[2];
			clipupdate = true;
		}
		else
		{
			//shiftmod[0] += dmx*zoom*std::max(std::max( sright-sleft, stop-sbottom ), sfar-snear)*0.5;
			//shiftmod[1] += dmy*zoom*std::max(std::max( sright-sleft, stop-sbottom ), sfar-snear)*0.5;
			rotatevector((double*)shiftmod);
			shift[0] += shiftmod[0];
			shift[1] += shiftmod[1];
			shift[2] += shiftmod[2];
			bndupdate = true;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
297 298 299 300
		glutPostRedisplay();
		mymx = lmx;
		mymy = lmy;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
301
	else if( actionstate == 2 ) //right button
Kirill Terekhov's avatar
Kirill Terekhov committed
302
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
		if( planecontrol )
		{
			double shiftmod[3] = {0,0,0};
			shiftmod[2] -= dmy*zoom*std::max(std::max( sright-sleft, stop-sbottom ), sfar-snear);
			rotatevector_from_stack((double*)shiftmod);
			p[0] += shiftmod[0];
			p[1] += shiftmod[1];
			p[2] += shiftmod[2];
			clipupdate = true;
		}
		else
		{
			zoom *= expf(-dmy);
			reshape(width,height);
			double shiftmod[3] = {0,0,0};
			shiftmod[2] += dmx*zoom*std::max(std::max( sright-sleft, stop-sbottom ), sfar-snear);
			rotatevector((double*)shiftmod);
			shift[0] += shiftmod[0];
			shift[1] += shiftmod[1];
			shift[2] += shiftmod[2];
			bndupdate = true;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
325 326 327 328
		glutPostRedisplay();
		mymx = lmx;
		mymy = lmy;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
329 330
	else if( actionstate == 3 ) //left buttion
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
331
		clickmotion(nmx,nmy);
Kirill Terekhov's avatar
Kirill Terekhov committed
332 333
		if( planecontrol )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
334 335 336 337
			//quatget(n);
			reverse_rotatevector_from_stack((double *)n);
			reverse_rotatevector((double *)n);
			rotatevector_from_stack((double *)n);
Kirill Terekhov's avatar
Kirill Terekhov committed
338 339 340 341 342
			clipupdate = true;
			quatinit();
		}
		else bndupdate = true;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
343 344 345 346 347 348
}
void mymotion(int nmx, int nmy) // Mouse
{
	motion(nmx,nmy);
	mymx = 2.*(nmx/(double)width - 0.5);
	mymy = 2.*(0.5 - nmy/(double)height);
Kirill Terekhov's avatar
Kirill Terekhov committed
349
	if( current_picker != NULL ) glutPostRedisplay();
Kirill Terekhov's avatar
Kirill Terekhov committed
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
}

void myclick(int b, int s, int nmx, int nmy) // Mouse
{
	if( b == GLUT_LEFT_BUTTON )
	{
		if( s == GLUT_DOWN )
		{
			actionstate = 3;
		}
		else
		{
			actionstate = 0;
		}
		click(b,s,nmx,nmy);
	}
	else if( b == GLUT_MIDDLE_BUTTON )
	{
		if( s == GLUT_DOWN )
		{
			actionstate = 1;
			interactive = true;
		}
		else
		{
			actionstate = 0;
			interactive = false;
		}
		mymx = 2.*(nmx/(double)width - 0.5);
		mymy = 2.*(0.5 - nmy/(double)height);
	}
	else if( b == GLUT_RIGHT_BUTTON )
	{
		if( s == GLUT_DOWN )
		{
			actionstate = 2;
			interactive = true;
		}
		else
		{
			actionstate = 0;
			interactive = false;
		}
		mymx = 2.*(nmx/(double)width - 0.5);
		mymy = 2.*(0.5 - nmy/(double)height);
	}
396 397
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
398 399 400 401
	glutPostRedisplay();
}


402
Input * CommonInput = NULL;
Kirill Terekhov's avatar
Kirill Terekhov committed
403 404


Kirill Terekhov's avatar
Kirill Terekhov committed
405 406 407

void keyboard(unsigned char key, int x, int y)
{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
408 409
	(void) x;
	(void) y;
Kirill Terekhov's avatar
Kirill Terekhov committed
410
	printf("%d %d\n",(int)key,(int)glutGetModifiers());
411 412
	if( glutGetModifiers() & (GLUT_ACTIVE_CTRL) )
		std::cout << "pressed " << ((char)(key)) << " int " << ((int)key) << " ctrl " << (glutGetModifiers() & GLUT_ACTIVE_CTRL ? "yes" : "no") << " shift " << (glutGetModifiers() & GLUT_ACTIVE_SHIFT ? "yes" : "no") << " alt " << (glutGetModifiers() & GLUT_ACTIVE_ALT ? "yes" : "no") << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
413 414
	if( CommonInput != NULL )
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
415
		CommonInput->KeyPress(key);
Kirill Terekhov's avatar
Kirill Terekhov committed
416 417
		return;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
418 419
	if( key == 27 )
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
420 421 422
		if( oclipper ) delete oclipper;
		if( bclipper ) delete bclipper;
		if( current_picker ) delete current_picker;
Kirill Terekhov's avatar
Kirill Terekhov committed
423 424 425
		delete mesh;
		exit(-1);
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
426
	else if( key == '=' || key == '+')
Kirill Terekhov's avatar
Kirill Terekhov committed
427 428 429 430
	{
		zoom /= 1.1;
		reshape(width,height);
		glutPostRedisplay();
Kirill Terekhov's avatar
Kirill Terekhov committed
431 432
		interactive = true;
		//reset_timer = Timer();
Kirill Terekhov's avatar
Kirill Terekhov committed
433
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
434
	else if( key == '_' || key == '-')
Kirill Terekhov's avatar
Kirill Terekhov committed
435 436 437 438
	{
		zoom *= 1.1;	
		reshape(width,height);
		glutPostRedisplay();
Kirill Terekhov's avatar
Kirill Terekhov committed
439 440
		interactive = true;
		//reset_timer = Timer();
Kirill Terekhov's avatar
Kirill Terekhov committed
441
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
442 443
  else if( key == 'e' )
  {
Kirill Terekhov's avatar
Kirill Terekhov committed
444
    drawedges = (drawedges+1)%(4+is_material_defined);
Kirill Terekhov's avatar
Kirill Terekhov committed
445 446
    glutPostRedisplay();
  }
Kirill Terekhov's avatar
Kirill Terekhov committed
447
	else if( key == 'w' )
Kirill Terekhov's avatar
Kirill Terekhov committed
448
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
449 450
		double shiftmod[3] = {0,0,0};
		shiftmod[1] -= 0.03f*expf(zoom-1)*std::max(std::max( sright-sleft, stop-sbottom ), sfar-snear)*0.5;
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
		if( !planecontrol )
		{
			rotatevector((double*)shiftmod);
			shift[0] += shiftmod[0];
			shift[1] += shiftmod[1];
			shift[2] += shiftmod[2];
			interactive = true;
			bndupdate = true;
		}
		else
		{
			rotatevector_from_stack((double*)shiftmod);
			p[0] -= shiftmod[0];
			p[1] -= shiftmod[1];
			p[2] -= shiftmod[2];
			clipupdate = true;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
468 469 470 471
		glutPostRedisplay();
	}
	else if( key == 's' )
	{
472 473
		double shiftmod[3] = {0,0,0};
		shiftmod[1] += 0.03f*expf(zoom-1)*std::max(std::max( sright-sleft, stop-sbottom ), sfar-snear)*0.5;
Kirill Terekhov's avatar
Kirill Terekhov committed
474 475 476 477 478 479 480 481 482
		if( !planecontrol )
		{
			rotatevector((double*)shiftmod);
			shift[0] += shiftmod[0];
			shift[1] += shiftmod[1];
			shift[2] += shiftmod[2];
			interactive = true;
			bndupdate = true;
		}
483 484 485 486 487 488 489 490 491
		else
		{
			rotatevector_from_stack((double*)shiftmod);
			p[0] -= shiftmod[0];
			p[1] -= shiftmod[1];
			p[2] -= shiftmod[2];
			clipupdate = true;
		}
		glutPostRedisplay();
Kirill Terekhov's avatar
Kirill Terekhov committed
492 493 494
	}
	else if( key == 'a' )
	{
495 496
		double shiftmod[3] = {0,0,0};
		shiftmod[0] += 0.03f*expf(zoom-1)*std::max(std::max( sright-sleft, stop-sbottom ), sfar-snear)*0.5;
Kirill Terekhov's avatar
Kirill Terekhov committed
497 498 499 500 501 502 503 504 505
		if( !planecontrol )
		{
			rotatevector((double*)shiftmod);
			shift[0] += shiftmod[0];
			shift[1] += shiftmod[1];
			shift[2] += shiftmod[2];
			interactive = true;
			bndupdate = true;
		}
506 507 508 509 510 511 512 513 514
		else
		{
			rotatevector_from_stack((double*)shiftmod);
			p[0] -= shiftmod[0];
			p[1] -= shiftmod[1];
			p[2] -= shiftmod[2];
			clipupdate = true;
		}
		glutPostRedisplay();
Kirill Terekhov's avatar
Kirill Terekhov committed
515 516 517
	}
	else if( key == 'd' )
	{
518 519 520
		double shiftmod[3] = {0,0,0};
		shiftmod[0] -= 0.03f*expf(zoom-1)*std::max(std::max( sright-sleft, stop-sbottom ), sfar-snear)*0.5;
		if(!planecontrol)
Kirill Terekhov's avatar
Kirill Terekhov committed
521 522 523 524 525 526 527 528
		{
			rotatevector((double*)shiftmod);
			shift[0] += shiftmod[0];
			shift[1] += shiftmod[1];
			shift[2] += shiftmod[2];
			interactive = true;
			bndupdate = true;
		}
529 530 531 532 533 534 535 536
		else
		{	rotatevector_from_stack((double*)shiftmod);
			p[0] -= shiftmod[0];
			p[1] -= shiftmod[1];
			p[2] -= shiftmod[2];
			clipupdate = true;
		}
		glutPostRedisplay();
Kirill Terekhov's avatar
Kirill Terekhov committed
537 538 539
	}
	else if( key == 'r' )
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
		if( planecontrol )
		{
			p[0] = (sleft+sright)*0.5;
			p[1] = (sbottom+stop)*0.5;
			p[2] = (sfar+snear)*0.5;
			n[0] = 0;
			n[1] = 0;
			n[2] = 1;
			quatinit();
			clipupdate = true;
		}
		else
		{
			shift[0] = 0.0f;
			shift[1] = 0.0f;
			shift[2] = 0.0f;
			zoom = 1;
			quatinit();
			bndupdate = true;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
560 561
		glutPostRedisplay();
		interactive = true;
Kirill Terekhov's avatar
Kirill Terekhov committed
562
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
563 564
	else if( key == 'z' )
	{
565
		int ret;
Kirill Terekhov's avatar
Kirill Terekhov committed
566
		printf("Enter point of plane (x,y,z):\n");
567
		ret = scanf("%lf %lf %lf",p,p+1,p+2);
Kirill Terekhov's avatar
Kirill Terekhov committed
568
		printf("Enter normal of plane (x,y,z):\n");
569
		ret = scanf("%lf %lf %lf",n,n+1,n+2);
Kirill Terekhov's avatar
Kirill Terekhov committed
570 571 572 573 574 575 576 577 578 579
		Storage::real l = sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]);
		if( l )
		{
			n[0] /= l;
			n[1] /= l;
			n[2] /= l;
		}
		if( oclipper ) 
		{
			oclipper->clip_plane(p,n);
580 581 582 583
			clipboxupdate = true;
		}
		if( bclipper )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
584 585 586 587 588 589
			bclipper->clip_plane(p,n);
			clipboxupdate = true;
		}
			
		glutPostRedisplay();
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
590
	else if( key == 'x' )
Kirill Terekhov's avatar
Kirill Terekhov committed
591 592 593 594
	{
		draw_volumetric = !draw_volumetric;
		glutPostRedisplay();
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
595
	else if( key == 'b' )
Kirill Terekhov's avatar
Kirill Terekhov committed
596
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
597 598
		boundary = !boundary;
		glutPostRedisplay();
Kirill Terekhov's avatar
Kirill Terekhov committed
599
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
600
	else if( key == 'l' )
Kirill Terekhov's avatar
Kirill Terekhov committed
601
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
602 603 604 605 606 607 608
		planecontrol = !planecontrol;
		if( planecontrol ) 
		{
			quatpush();
			quatinit();
		}
		else quatpop();
Kirill Terekhov's avatar
Kirill Terekhov committed
609
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
610 611 612 613 614 615 616 617 618 619
	else if( key == 'n' )
	{
		if( CommonInput == NULL ) CommonInput = new Input(&amplitude, "Amplitude");
		glutPostRedisplay();
	}
	else if( key == 'm' )
	{
		if( CommonInput == NULL ) CommonInput = new Input(&radius, "Radius");
		glutPostRedisplay();
	}
620 621 622 623 624
	else if( key == 'h' )
	{
		draw_orphan = !draw_orphan;
		glutPostRedisplay();
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
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
	else if( key == ',' || key == '<' )
	{
		std::cout << "positive shift" << std::endl;
		Storage::real radius2 = radius*radius;
		for(Mesh::iteratorNode it = mesh->BeginNode(); it != mesh->EndNode(); ++it)
		{
			Storage::real_array coords = it->Coords();
			Storage::real proj[3] = {0,0,0};
			Storage::real d = -(n[0]*(coords[0]-p[0]) + n[1]*(coords[1]-p[1]) + n[2]*(coords[2]-p[2]));
			proj[0] = coords[0] + d*n[0];
			proj[1] = coords[1] + d*n[1];
			proj[2] = coords[2] + d*n[2];
			d = (n[0]*(proj[0]-p[0]) + n[1]*(proj[1]-p[1]) + n[2]*(proj[2]-p[2]));
			assert(fabs(d) < 1.0e-7); // check that it is indeed a projection
			Storage::real dist = sqrt((p[0]-proj[0])*(p[0]-proj[0])+(p[1]-proj[1])*(p[1]-proj[1])+(p[2]-proj[2])*(p[2]-proj[2]));
			Storage::real delta = exp(-dist*dist/radius2);
			coords[0] += n[0]*delta*amplitude;
			coords[1] += n[1]*delta*amplitude;
			coords[2] += n[2]*delta*amplitude;
		}
		all_boundary.clear();
		INMOST_DATA_ENUM_TYPE pace = std::max<INMOST_DATA_ENUM_TYPE>(1,std::min<INMOST_DATA_ENUM_TYPE>(15,(unsigned)boundary_faces.size()/100));
		for(INMOST_DATA_ENUM_TYPE k = 0; k < boundary_faces.size(); k++) 
		{
			all_boundary.push_back(DrawFace(boundary_faces[k]));
			if( k%pace == 0 ) all_boundary.back().set_flag(true);
		}
		if( oclipper ) delete oclipper;
		if( bclipper ) delete bclipper;
		bclipper = new bnd_clipper(mesh,boundary_faces.data(),(int)boundary_faces.size());
655 656
		if( mesh->GetDimensions() != 2 )
			oclipper = new clipper(mesh);
Kirill Terekhov's avatar
Kirill Terekhov committed
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
		clipupdate = true;
		glutPostRedisplay();
	}
	else if( key == '.' || key == '>' )
	{
		std::cout << "negative shift" << std::endl;
		Storage::real radius2 = radius*radius;
		for(Mesh::iteratorNode it = mesh->BeginNode(); it != mesh->EndNode(); ++it)
		{
			Storage::real_array coords = it->Coords();
			Storage::real proj[3] = {0,0,0};
			Storage::real d = -(n[0]*(coords[0]-p[0]) + n[1]*(coords[1]-p[1]) + n[2]*(coords[2]-p[2]));
			proj[0] = coords[0] + d*n[0];
			proj[1] = coords[1] + d*n[1];
			proj[2] = coords[2] + d*n[2];
			d = (n[0]*(proj[0]-p[0]) + n[1]*(proj[1]-p[1]) + n[2]*(proj[2]-p[2]));
			assert(fabs(d) < 1.0e-7); // check that it is indeed a projection
			Storage::real dist = sqrt((p[0]-proj[0])*(p[0]-proj[0])+(p[1]-proj[1])*(p[1]-proj[1])+(p[2]-proj[2])*(p[2]-proj[2]));
			Storage::real delta = exp(-dist*dist/radius2);
			coords[0] -= n[0]*delta*amplitude;
			coords[1] -= n[1]*delta*amplitude;
			coords[2] -= n[2]*delta*amplitude;
		}
		all_boundary.clear();
		INMOST_DATA_ENUM_TYPE pace = std::max<INMOST_DATA_ENUM_TYPE>(1,std::min<INMOST_DATA_ENUM_TYPE>(15,(unsigned)boundary_faces.size()/100));
		for(INMOST_DATA_ENUM_TYPE k = 0; k < boundary_faces.size(); k++) 
		{
			all_boundary.push_back(DrawFace(boundary_faces[k]));
			if( k%pace == 0 ) all_boundary.back().set_flag(true);
		}
		if( oclipper ) delete oclipper;
		if( bclipper ) delete bclipper;
		bclipper = new bnd_clipper(mesh,boundary_faces.data(),(unsigned)boundary_faces.size());
690 691
		if( mesh->GetDimensions() != 2 )
			oclipper = new clipper(mesh);
Kirill Terekhov's avatar
Kirill Terekhov committed
692 693 694 695 696 697 698 699
		clipupdate = true;
		glutPostRedisplay();
	}
	else if( key == 'p' )
	{
		perspective = !perspective;
		glutPostRedisplay();
	}
700 701 702 703 704 705
	else if( key == 'o' )
	{
		elevation = !elevation;
		clipupdate = true;
		glutPostRedisplay();
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
706 707 708 709 710 711
	else if( key == 'v' )
	{
		if( CommonInput == NULL ) 
		{
			PrintTags(mesh,CELL|FACE|EDGE|NODE);

712
			CommonInput = new Input(visualization_prompt, "Enter data for visualization as Element:Name:Component or ElementType:Number");
Kirill Terekhov's avatar
Kirill Terekhov committed
713
			visualization_prompt_active = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
714
			clipupdate = true;
Kirill Terekhov's avatar
Kirill Terekhov committed
715
			//if( visualization_tag.isValid() ) visualization_tag =  mesh->DeleteTag(visualization_tag);
716
			//if( disp_e.isValid() ) disp_e = InvalidElement();
Kirill Terekhov's avatar
Kirill Terekhov committed
717 718 719
		}
		glutPostRedisplay();
	}
720 721 722
	else if( key == 'c' )
	{
		if( CommonInput == NULL ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
723 724 725 726
		{
			CommonInput = new Input(visualization_prompt, "Enter data for color bounds as min:max");
			visualization_prompt_active = 2;
		}
727 728
		glutPostRedisplay();
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
729 730 731 732
	else if( key == 'q' )
	{
		mesh->Save("mesh.vtk");
		mesh->Save("mesh.pmf");
733
		mesh->Save("mesh.xml");
Kirill Terekhov's avatar
Kirill Terekhov committed
734
		mesh->Save("mesh.gmv");
Kirill Terekhov's avatar
Kirill Terekhov committed
735
	}
736
	else if( key == 't' )
Kirill Terekhov's avatar
Kirill Terekhov committed
737
	{
738
		screenshot(4);
Kirill Terekhov's avatar
Kirill Terekhov committed
739 740 741 742
		std::fstream fout("screenshot.svg",std::ios::out);
		svg_draw(fout);
		fout.close();
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
743 744
}

Kirill Terekhov's avatar
Kirill Terekhov committed
745 746
void keyboard2(unsigned char key, int x, int y)
{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
747 748
	(void) x;
	(void) y;
Kirill Terekhov's avatar
Kirill Terekhov committed
749 750
	if( key == '=' || key == '+' ||  key == '_' || key == '-' || key == 'w' || key == 's' || key == 'a' || key == 'd' || key == 'r' || key == 'p' || key == 'z')
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
751
		//printf("depressed\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
752 753 754 755 756 757
		interactive = false;
		glutPostRedisplay();
	}
	
}

Kirill Terekhov's avatar
Kirill Terekhov committed
758 759
Tag face_center;

760
void DrawElement(Element e, color_t face, color_t edge, color_t node, bool print_adj)
761
{
762
	glPointSize(4);
763 764 765 766 767
	if( e.GetElementType() == NODE )
	{
		node.set_color();
		glColor3f(1,1,0);
		glBegin(GL_POINTS);
768 769 770 771 772 773 774 775 776
		glVertexNdv(e->getAsNode()->Coords().data(),e.GetMeshLink()->GetDimensions());
		glEnd();
	}
	if( e.GetElementType() == EDGE && e.GetElementDimension() == 0)
	{
		node.set_color();
		glColor3f(1,1,0);
		glBegin(GL_POINTS);
		glVertexNdv(e->getNodes()[0]->Coords().data(),e.GetMeshLink()->GetDimensions());
777 778
		glEnd();
	}
779
	else if( e.GetElementType() == EDGE && e.GetElementDimension() == 1 )
780 781 782 783
	{
		edge.set_color();
		glLineWidth(4);
		glBegin(GL_LINES);
784 785
		glVertexNdv(e->getAsEdge()->getBeg()->Coords().data(),e.GetMeshLink()->GetDimensions());
		glVertexNdv(e->getAsEdge()->getEnd()->Coords().data(),e.GetMeshLink()->GetDimensions());
786 787 788
		glEnd();
		node.set_color();
		glBegin(GL_POINTS);
789 790
		glVertexNdv(e->getAsEdge()->getBeg()->Coords().data(),e.GetMeshLink()->GetDimensions());
		glVertexNdv(e->getAsEdge()->getEnd()->Coords().data(),e.GetMeshLink()->GetDimensions());
791 792
		glEnd();
		glLineWidth(1);
793 794 795 796 797 798 799 800

		if (print_adj)
		{
			glRasterPos3dv(e->getAsEdge().getBeg().Coords().data());
			printtext("%d", e->getAsEdge().getBeg().LocalID());
			glRasterPos3dv(e->getAsEdge().getEnd().Coords().data());
			printtext("%d", e->getAsEdge().getEnd().LocalID());
		}
801
	}
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
	else if(  e.GetElementType() == FACE && e.GetElementDimension() == 1 )
	{
		edge.set_color();
		glLineWidth(4);
		glBegin(GL_LINES);
		glVertexNdv(e->getAsFace()->getBeg()->Coords().data(),e.GetMeshLink()->GetDimensions());
		glVertexNdv(e->getAsFace()->getEnd()->Coords().data(),e.GetMeshLink()->GetDimensions());
		glEnd();
		node.set_color();
		glBegin(GL_POINTS);
		glVertexNdv(e->getAsFace()->getBeg()->Coords().data(),e.GetMeshLink()->GetDimensions());
		glVertexNdv(e->getAsFace()->getEnd()->Coords().data(),e.GetMeshLink()->GetDimensions());
		glEnd();
		glLineWidth(1);

		if (print_adj)
		{
			glRasterPos3dv(e->getAsFace().getBeg().Coords().data());
			printtext("%d", e->getAsFace().getBeg().LocalID());
			glRasterPos3dv(e->getAsFace().getEnd().Coords().data());
			printtext("%d", e->getAsFace().getEnd().LocalID());
		}
	}
	else if( e.GetElementType() == FACE || (e.GetElementType() == CELL && e.GetElementDimension() == 2) )
826 827 828 829 830 831 832 833 834 835 836 837
	{
		face2gl f = DrawFace(e);
		face.set_color();
		glBegin(GL_TRIANGLES);
		f.draw();
		glEnd();
		edge.set_color();
		glBegin(GL_LINES);
		f.drawedges();
		glEnd();
		node.set_color();
		ElementArray<Node> nodes = e->getNodes();
Kirill Terekhov's avatar
Kirill Terekhov committed
838
		ElementArray<Edge> edges = e->getEdges();
839 840
		glBegin(GL_POINTS);
		for(ElementArray<Node>::iterator it = nodes.begin(); it != nodes.end(); ++it)
841
			glVertexNdv(it->Coords().data(),it->GetMeshLink()->GetDimensions());
842
		glEnd();
Kirill Terekhov's avatar
Kirill Terekhov committed
843 844
		glColor3f(0, 0, 0);
		//glDisable(GL_DEPTH_TEST);
845
		if (print_adj)
Kirill Terekhov's avatar
Kirill Terekhov committed
846
		{
847 848 849 850 851 852 853 854
			for (ElementArray<Edge>::iterator it = edges.begin(); it != edges.end(); ++it)
			{
				double cnt[3];
				it->Centroid(cnt);
				//for (int k = 0; k < 3; ++k) cnt[k] = cnt[k] * 0.99 + campos[k] * 0.01;
				glRasterPos3dv(cnt);
				printtext("%d", it->LocalID());
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
855 856
		}
		//glEnable(GL_DEPTH_TEST);
857 858 859 860 861 862
	}
	else if( e.GetElementType() == CELL )
	{
		ElementArray<Face> dfaces = e.getFaces();
		face.set_color();
		glBegin(GL_TRIANGLES);
Kirill Terekhov's avatar
Kirill Terekhov committed
863
		for(ElementArray<Face>::iterator it = dfaces.begin(); it != dfaces.end(); ++it)
864 865 866 867 868
		{
			face2gl f = DrawFace(it->self());
			f.draw();
		}
		glEnd();
Kirill Terekhov's avatar
Kirill Terekhov committed
869
		glColor3f(0, 0, 0);
870 871
		edge.set_color();
		glBegin(GL_LINES);
Kirill Terekhov's avatar
Kirill Terekhov committed
872
		for(ElementArray<Face>::iterator it = dfaces.begin(); it != dfaces.end(); ++it)
873 874 875 876 877 878 879 880 881
		{
			face2gl f = DrawFace(it->self());
			f.drawedges();
		}
		glEnd();
		node.set_color();
		ElementArray<Node> nodes = e->getNodes();
		glBegin(GL_POINTS);
		for(ElementArray<Node>::iterator it = nodes.begin(); it != nodes.end(); ++it)
882
			glVertexNdv(it->Coords().data(),it->GetMeshLink()->GetDimensions());
883
		glEnd();
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915

		if (print_adj)
		{
			for (ElementArray<Face>::iterator it = dfaces.begin(); it != dfaces.end(); ++it)
			{
				double cnt[3];
				it->Centroid(cnt);
				//for (int k = 0; k < 3; ++k) cnt[k] = cnt[k] * 0.99 + campos[k] * 0.01;
				glRasterPos3dv(cnt);
				printtext("%d", it->LocalID());
			}
		}
	}
	glPointSize(1);
	if (e.GetElementType() == ESET)
	{
		ElementSet s = e.getAsSet();
		for(ElementSet::iterator it = s.Begin(); it != s.End(); ++it)
			DrawElement(it->self(),face,edge,node,false);

		if (print_adj)
		{
			glDisable(GL_DEPTH_TEST);
			for (ElementSet::iterator it = s.Begin(); it != s.End(); ++it)
			{
				double cnt[3];
				it->Centroid(cnt);
				glRasterPos3dv(cnt);
				printtext("%d", it->LocalID());
			}
			glEnable(GL_DEPTH_TEST);
		}
916
	}
917
	
918 919
}

Kirill Terekhov's avatar
Kirill Terekhov committed
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
void whereami(double & cx, double & cy, double & cz)
{
   // Get the viewing matrix
   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);

   // Return the result.
   cx = outx;
   cy = outy;
   cz = outz;
}


void pick_mouse(double origin[3], double direction[3])
{
   // Get the viewing matrix
   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((mymx+1.0)/2.0, (mymy+1.0)/2.0, 0.,
               modelview, projection, viewport,
               &outx, &outy, &outz);

   // Return the result.
   origin[0] = outx;
   origin[1] = outy;
   origin[2] = outz;
   
   gluUnProject((mymx+1.0)/2.0, (mymy+1.0)/2.0, 1.,
               modelview, projection, viewport,
               &outx, &outy, &outz);
   direction[0] = outx - origin[0];
   direction[1] = outy - origin[1];
   direction[2] = outz - origin[2];
   double l = sqrt(direction[0]*direction[0]+direction[1]*direction[1]+direction[2]*direction[2]);
   if( l ) 
   {
	   direction[0] /= l;
	   direction[1] /= l;
	   direction[2] /= l;
   }
Kirill Terekhov's avatar
Kirill Terekhov committed
973 974
}

975 976 977 978 979 980 981 982 983 984 985 986 987
//returns position of bottom
double display_elem_info(Element e, double top, double left, double interval)
{
	double bottom = top-interval;
	for(Mesh::iteratorTag t = mesh->BeginTag(); t != mesh->EndTag(); ++t) if( t->isDefined(e->GetElementType()) )
		if( e->HaveData(*t) )
			bottom -= interval;
	
	glDisable(GL_DEPTH_TEST);
	glLoadIdentity();
	set_matrix2d();
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
988
	glColor4f(1,1,1,0.65);
Kirill Terekhov's avatar
Kirill Terekhov committed
989
	glEnable(GL_BLEND);
990 991 992 993 994 995
	glBegin(GL_QUADS);
	glVertex2f(left-0.01,bottom-0.01);
	glVertex2f(left-0.01,top-0.01);
	glVertex2f(0.99,top-0.01);
	glVertex2f(0.99,bottom-0.01);
	glEnd();
Kirill Terekhov's avatar
Kirill Terekhov committed
996
	glDisable(GL_BLEND);
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
	glColor3f(0,0,0);
	glBegin(GL_LINE_LOOP);
	glVertex2f(left-0.01,bottom-0.01);
	glVertex2f(left-0.01,top-0.01);
	glVertex2f(0.99,top-0.01);
	glVertex2f(0.99,bottom-0.01);
	glEnd();
	
	
	glColor3f(0.2,0.2,0.2);
	top -= interval;
	glRasterPos2f(left,top);
	printtext("%s %d",ElementTypeName(e->GetElementType()),e->LocalID());
1010
	if( e->GetElementType() == ESET ) printtext(" size %d",e->getAsSet().Size());
1011 1012 1013 1014 1015
	glColor3f(0.2,0.2,0.2);
	for(Mesh::iteratorTag t = mesh->BeginTag(); t != mesh->EndTag(); ++t) if( t->isDefined(e->GetElementType()) )
	{
		if( e->HaveData(*t) )
		{
1016 1017
			char str[131072];
			char temp[131072];
1018
			str[0] = '\0';
Kirill Terekhov's avatar
Kirill Terekhov committed
1019
			int dsize;
1020 1021 1022 1023 1024
			switch(t->GetDataType())
			{
				case DATA_INTEGER:
				{
					Storage::integer_array arr = e->IntegerArray(*t);
Kirill Terekhov's avatar
Kirill Terekhov committed
1025
					dsize = arr.size();
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
					for(INMOST_DATA_ENUM_TYPE k = 0; k < arr.size(); k++)
					{
						sprintf(temp,"%s %d",str,arr[k]);
						strcpy(str,temp);
					}
					break;
				}
				case DATA_REAL:
				{
					Storage::real_array arr = e->RealArray(*t);
Kirill Terekhov's avatar
Kirill Terekhov committed
1036
					dsize = arr.size();
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
					for(INMOST_DATA_ENUM_TYPE k = 0; k < arr.size(); k++)
					{
						sprintf(temp,"%s %lf",str,arr[k]);
						strcpy(str,temp);
					}
					break;
				}
				case DATA_BULK:
				{
					Storage::bulk_array arr = e->BulkArray(*t);
Kirill Terekhov's avatar
Kirill Terekhov committed
1047
					dsize = arr.size();
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
					for(INMOST_DATA_ENUM_TYPE k = 0; k < arr.size(); k++)
					{
						sprintf(temp,"%s %d",str,arr[k]);
						strcpy(str,temp);
					}
					break;
				}
				case DATA_REFERENCE:
				{
					Storage::reference_array arr = e->ReferenceArray(*t);
Kirill Terekhov's avatar
Kirill Terekhov committed
1058
					dsize = arr.size();
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
					for(INMOST_DATA_ENUM_TYPE k = 0; k < arr.size(); k++)
					{
						if(arr.at(k) == InvalidHandle()) sprintf(temp,"%s NULL",str);
						else sprintf(temp,"%s %s:%d",str,ElementTypeName(arr[k]->GetElementType()),arr[k]->LocalID());
						strcpy(str,temp);
					}
					break;
				}
				case DATA_REMOTE_REFERENCE:
				{
					Storage::remote_reference_array arr = e->RemoteReferenceArray(*t);
Kirill Terekhov's avatar
Kirill Terekhov committed
1070
					dsize = arr.size();
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
					for(INMOST_DATA_ENUM_TYPE k = 0; k < arr.size(); k++)
					{
						if(arr.at(k).first == NULL || arr.at(k).second == InvalidHandle()) sprintf(temp,"%s NULL",str);
						else sprintf(temp,"%s %p:%s:%d",str,arr[k]->GetMeshLink(),ElementTypeName(arr[k]->GetElementType()),arr[k]->LocalID());
						strcpy(str,temp);
					}
					break;
				}
#if defined(USE_AUTODIFF)
				case DATA_VARIABLE:
				{
					Storage::var_array arr = e->VariableArray(*t);
Kirill Terekhov's avatar
Kirill Terekhov committed
1083
					dsize = arr.size();
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
					for(INMOST_DATA_ENUM_TYPE k = 0; k < arr.size(); k++)
					{
						std::stringstream stream;
						stream << arr[k].GetValue() << " {[" << arr[k].GetRow().Size() << "] ";
						for(INMOST_DATA_ENUM_TYPE q = 0; q < arr[k].GetRow().Size(); ++q)
						{
							stream << "(" << arr[k].GetRow().GetValue(q) << "," << arr[k].GetRow().GetIndex(q) << ") ";
						}
						stream << "}";
						sprintf(temp,"%s %s",str,stream.str().c_str());
						strcpy(str,temp);
					}
					break;
				}
#endif
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1100
			sprintf(temp,"%s[%d] %s %s",t->GetTagName().c_str(),dsize,DataTypeName(t->GetDataType()),str);
1101 1102 1103 1104 1105 1106 1107 1108
			strcpy(str,temp);
			top -= interval;
			glRasterPos2f(left,top);
			printtext(str);
			
		}
	}
	glEnable(GL_DEPTH_TEST);
Kirill Terekhov's avatar
Kirill Terekhov committed
1109
	return top-2*interval;
1110 1111
}

Kirill Terekhov's avatar
Kirill Terekhov committed
1112
void draw_screen()
Kirill Terekhov's avatar
Kirill Terekhov committed
1113
{
Kirill Terekhov's avatar
Kirill Terekhov committed
1114
	//glDepthMask(GL_TRUE);
Kirill Terekhov's avatar
Kirill Terekhov committed
1115 1116
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
Kirill Terekhov's avatar
Kirill Terekhov committed
1117
	set_matrix3d();
Kirill Terekhov's avatar
Kirill Terekhov committed
1118

Kirill Terekhov's avatar
Kirill Terekhov committed
1119
	Storage::real mult = zoom*std::max(std::max( sright-sleft, stop-sbottom ), sfar-snear)*0.5*0.1;
Kirill Terekhov's avatar
Kirill Terekhov committed
1120 1121
	if( perspective )
		glTranslated(0,0,-zoom*2*std::max(std::max( sright-sleft, stop-sbottom ), sfar-snear)*0.5);
1122 1123 1124
	if( planecontrol )
		rotate_from_stack();
	else
Kirill Terekhov's avatar
Kirill Terekhov committed
1125 1126 1127
		rotate();

	//axis
Kirill Terekhov's avatar
Kirill Terekhov committed
1128
	if( drawaxis )
Kirill Terekhov's avatar
Kirill Terekhov committed
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
	{
		glLineWidth(3.0);
		glBegin(GL_LINES);
		glColor3f(1,0,0);
		glVertex3d(0,0,0);
		glVertex3d(mult,0,0);
		glColor3f(0,1,0);
		glVertex3d(0,0,0);
		glVertex3d(0,mult,0);
		glColor3f(0,0,1);
		glVertex3d(0,0,0);
		glVertex3d(0,0,mult);
		glEnd();
		glLineWidth(1.0);
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1144 1145 1146 1147 1148
	//glPointSize(1);
	
	//glTranslated(-(sleft+sright)*0.5+shift[0],-(sbottom+stop)*0.5 + shift[1],-(snear+sfar)*0.5 + shift[2]);
	
	glTranslated(shift[0],shift[1],shift[2]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1149

Kirill Terekhov's avatar
Kirill Terekhov committed
1150 1151

	//if( planecontrol )
Kirill Terekhov's avatar
Kirill Terekhov committed
1152
	if( drawaxis )
Kirill Terekhov's avatar
Kirill Terekhov committed
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
	{
		glColor3f(0.6,0.4,0.2);
		glPointSize(5);
		glBegin(GL_POINTS);
		glVertex3dv(p);
		glEnd();
		glPointSize(1);

		glColor3f(0.2,0.4,0.6);
		glLineWidth(3.0);
		glBegin(GL_LINES);
		glVertex3dv(p);
		glVertex3d(p[0]+n[0]*mult*2,p[1]+n[1]*mult*2,p[2]+n[2]*mult*2);
		glEnd();
		glLineWidth(1.0);
	}

Kirill Terekhov's avatar
Kirill Terekhov committed
1170
	
Kirill Terekhov's avatar
Kirill Terekhov committed
1171 1172

	double campos[3] = {0.5,0.5,0}, pickp[3], pickd[3];
Kirill Terekhov's avatar
Kirill Terekhov committed
1173
	whereami(campos[0], campos[1], campos[2]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1174 1175
	int picked = -1;

1176 1177 1178 1179 1180 1181 1182 1183
	if (draw_orphan)
	for (int k = 0; k < (int)orphans.size(); ++k)
		DrawElement(orphans[k], color_t(1, 0, 1), color_t(0, 1, 1), color_t(0, 0, 1), true);

	if (disp_e.isValid())
		DrawElement(disp_e, color_t(1, 1, 0), color_t(1, 0, 0), color_t(0, 0, 1), true);


Kirill Terekhov's avatar
Kirill Terekhov committed
1184
	//glTranslated((l+r)*0.5,(b+t)*0.5,(near+far)*0.5);
Kirill Terekhov's avatar
Kirill Terekhov committed
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
	if( drawedges == 4 )
	{
		clip_boundary.clear();
		for(std::map<int,std::vector<HandleType> >::iterator it = material_faces.begin(); it != material_faces.end(); ++it)
		{
			for(std::vector<HandleType>::iterator jt = it->second.begin(); jt != it->second.end(); ++jt)
			{
				clip_boundary.push_back(DrawFace(Face(mesh,*jt)));
				clip_boundary.back().shift(material_x[it->first],material_y[it->first],material_z[it->first]);
				clip_boundary.back().set_color(0.6,0.6,0.6,1);
			}
		}
1197 1198
		if (!(drawedges == 2 || drawedges == 3))
			draw_faces(clip_boundary);
Kirill Terekhov's avatar
Kirill Terekhov committed
1199
		glColor4f(0,0,0,1);
1200 1201
		if (drawedges && drawedges != 2) 
			draw_edges(clip_boundary);
Kirill Terekhov's avatar
Kirill Terekhov committed
1202 1203 1204
		clipboxupdate = true;
	}
	else
Kirill Terekhov's avatar
Kirill Terekhov committed
1205
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1206
		
1207
		if( oclipper || bclipper )
Kirill Terekhov's avatar
Kirill Terekhov committed
1208
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1209 1210 1211 1212
			
			if( clipupdate ) 
			{
				if( current_picker != NULL ) {delete current_picker; current_picker = NULL;}
1213
				if( oclipper ) oclipper->clip_plane(p,n);
Kirill Terekhov's avatar
Kirill Terekhov committed
1214 1215 1216
				clipupdate = false;
				clipboxupdate = true;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1217
		
Kirill Terekhov's avatar
Kirill Terekhov committed
1218
			if( !interactive && clipboxupdate )
Kirill Terekhov's avatar
Kirill Terekhov committed
1219
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1220
				clip_boundary.clear();
1221
				if( oclipper ) oclipper->gen_clip(clip_boundary,n,elevation);
Kirill Terekhov's avatar
Kirill Terekhov committed
1222
				bclipper->clip_plane(p,n);
1223
				bclipper->gen_clip(clip_boundary,p,n,elevation);
Kirill Terekhov's avatar
Kirill Terekhov committed
1224
				clipboxupdate = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
				for(int k = 0; k < (int)orphans.size(); ++k)
				{
					if( orphans[k]->GetElementType() == FACE )
						clip_boundary.push_back(DrawFace(orphans[k]->getAsFace()));
					else if( orphans[k]->GetElementType() == CELL )
					{
						ElementArray<Face> faces = orphans[k].getFaces();
						for(int q = 0; q < faces.size(); ++q)
							clip_boundary.push_back(DrawFace(faces[q]));
					}
				}

Kirill Terekhov's avatar
Kirill Terekhov committed
1237 1238

				if( current_picker != NULL ) {delete current_picker; current_picker = NULL;}
Kirill Terekhov's avatar
Kirill Terekhov committed
1239
				if( !clip_boundary.empty() ) current_picker = new picker(clip_boundary);
Kirill Terekhov's avatar
Kirill Terekhov committed
1240 1241
			}

Kirill Terekhov's avatar
Kirill Terekhov committed
1242 1243 1244 1245 1246 1247 1248
			

			if( current_picker != NULL )
			{
				pick_mouse(pickp,pickd);
				picked = current_picker->select(pickp,pickd);
			}
1249
			
Kirill Terekhov's avatar
Kirill Terekhov committed
1250 1251 1252 1253
	
			if( interactive && clipboxupdate )
			{
				//printf("draw1 %d %d\n",interactive, clipboxupdate);
Kirill Terekhov's avatar
Kirill Terekhov committed
1254 1255
				INMOST_DATA_ENUM_TYPE opace = !planecontrol ? std::max<INMOST_DATA_ENUM_TYPE>(1,std::min<INMOST_DATA_ENUM_TYPE>(15,oclipper->size()/100)) : 1;
				INMOST_DATA_ENUM_TYPE bpace = std::max<INMOST_DATA_ENUM_TYPE>(1,std::min<INMOST_DATA_ENUM_TYPE>(15,bclipper->size()/100));
Kirill Terekhov's avatar
Kirill Terekhov committed
1256
				glColor4f(0.6,0.6,0.6,1);
1257
				if (isColorBarEnabled()) GetColorBar()->BindTexture();
1258
				if( oclipper ) oclipper->draw_clip(opace,n,elevation);
1259 1260
				bclipper->draw_clip(bpace,p,n);
				if (isColorBarEnabled()) GetColorBar()->UnbindTexture();
Kirill Terekhov's avatar
Kirill Terekhov committed
1261
				glColor4f(0,0,0,1); 
1262 1263
				if (drawedges)
				{
1264
					if( oclipper ) oclipper->draw_clip_edges(opace, n,elevation);
1265 1266
					bclipper->draw_clip_edges(bpace);
				}
1267
				
Kirill Terekhov's avatar
Kirill Terekhov committed
1268 1269
			}
			else
Kirill Terekhov's avatar
Kirill Terekhov committed
1270
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1271 1272
				//printf("draw2 %d %d\n",interactive, clipboxupdate);
				if( interactive )
Kirill Terekhov's avatar
Kirill Terekhov committed
1273
				{
1274 1275
					if (!(drawedges == 2 || drawedges == 3)) 
						draw_faces_interactive(clip_boundary);
Kirill Terekhov's avatar
Kirill Terekhov committed
1276
					glColor4f(0,0,0,1); 
1277 1278
					if (drawedges && drawedges != 2) 
						draw_edges_interactive(clip_boundary);
Kirill Terekhov's avatar
Kirill Terekhov committed
1279
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
1280
				else
Kirill Terekhov's avatar
Kirill Terekhov committed
1281
				{
1282 1283
					if (!(drawedges == 2 || drawedges == 3))
						draw_faces(clip_boundary,picked);
Kirill Terekhov's avatar
Kirill Terekhov committed
1284
					glColor4f(0,0,0,1); 
1285 1286
					if (drawedges && drawedges != 2) 
						draw_edges(clip_boundary, picked);
Kirill Terekhov's avatar
Kirill Terekhov committed
1287 1288
				}
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1289
		}
1290 1291
		
		
Kirill Terekhov's avatar
Kirill Terekhov committed
1292 1293 1294 1295 1296 1297 1298


		if( draw_volumetric )
		{
			if( bndupdate ) CommonVolumetricView->camera(campos,interactive);
			CommonVolumetricView->draw(interactive);
		}
1299 1300 1301
		
		if( CommonVectors )
			CommonVectors->Draw(interactive);
Kirill Terekhov's avatar
Kirill Terekhov committed
1302 1303


Kirill Terekhov's avatar
Kirill Terekhov committed
1304 1305
		for(int k = 0; k < streamlines.size(); ++k)
			streamlines[k].Draw(true);//interactive);
Kirill Terekhov's avatar
Kirill Terekhov committed
1306

Kirill Terekhov's avatar
Kirill Terekhov committed
1307 1308 1309 1310 1311 1312
		glColor4f(0, 0, 0, 1);
		glBegin(GL_LINES);
		for (int k = 0; k < segments.size(); ++k)
			segments[k].Draw();
		glEnd();

Kirill Terekhov's avatar
Kirill Terekhov committed
1313 1314 1315 1316 1317
		if( boundary )
		{
			glEnable(GL_BLEND);
			if( !interactive && bndupdate)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1318
				for(INMOST_DATA_ENUM_TYPE q = 0; q < all_boundary.size() ; q++) 
Kirill Terekhov's avatar
Kirill Terekhov committed
1319
					all_boundary[q].compute_dist(campos);
Kirill Terekhov's avatar
Kirill Terekhov committed
1320 1321
				//std::sort(all_boundary.rbegin(),all_boundary.rend());
				face2gl::radix_sort_dist(all_boundary);
Kirill Terekhov's avatar
Kirill Terekhov committed
1322 1323


Kirill Terekhov's avatar
Kirill Terekhov committed
1324
				for(INMOST_DATA_ENUM_TYPE q = 0; q < added_faces.size() ; q++) 
Kirill Terekhov's avatar
Kirill Terekhov committed
1325 1326 1327
					added_faces[q].compute_dist(campos);
				//std::sort(all_boundary.rbegin(),all_boundary.rend());
				face2gl::radix_sort_dist(added_faces);
Kirill Terekhov's avatar
Kirill Terekhov committed
1328
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1329
			glColor4f(0,0,0,0.25); 
1330 1331 1332 1333 1334