output_view.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #include "qt/output/output_view.hpp"
  2. QtOutputView::QtOutputView(QtSolver* k){
  3. solver=k;
  4. nX=solver->get_geometry().nX;
  5. dX=solver->get_geometry().dX;
  6. lX=solver->get_geometry().lX;
  7. factor=solver->get_vertical_factor();
  8. };
  9. void
  10. QtOutputView::initializeGL(){
  11. glClearColor(1,1,1,1);
  12. glEnable(GL_DEPTH_TEST);
  13. glEnable(GL_LIGHT0);
  14. glEnable(GL_LIGHTING);
  15. glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
  16. glEnable(GL_COLOR_MATERIAL);
  17. }
  18. void
  19. QtOutputView::paintGL(){
  20. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  21. if(step>=solver->get_computed_solutions_number()) return;
  22. double h=height();
  23. double w=width();
  24. glBegin(GL_TRIANGLES);
  25. for(size_t i=0;i<nX-1;++i){
  26. size_t max_left=solver->get_geometry().nZ[i];
  27. size_t max_right=solver->get_geometry().nZ[i+1];
  28. size_t left=0;
  29. size_t right=0;
  30. while(left<max_left-1 and right<max_right-1){
  31. if(solver->get_geometry().Z[i][left+1]<=solver->get_geometry().Z[i+1][right+1]){
  32. drawTriangle(i,left,i+1,right,i,left+1);
  33. ++left;
  34. }
  35. else{
  36. drawTriangle(i,left,i+1,right,i+1,right+1);
  37. ++right;
  38. }
  39. }
  40. if(left==max_left-1){
  41. while(right<max_right-1){
  42. drawTriangle(i,left,i+1,right,i+1,right+1);
  43. ++right;
  44. }
  45. }
  46. if(right==max_right-1){
  47. while(left<max_left-1){
  48. drawTriangle(i,left,i+1,right,i,left+1);
  49. ++left;
  50. }
  51. }
  52. }
  53. glEnd();
  54. //Draw area under hbot
  55. glColor3f(0.6,0.6,0.6);
  56. glLineWidth(3);
  57. glBegin(GL_QUADS);
  58. for(size_t ix=0;ix<nX-1;ix+=1){
  59. double x=ix*dX;
  60. glVertex3f(x,0,1);
  61. glVertex3f(x,solver->get_geometry().hbot[ix],0);
  62. glVertex3f(x+dX,solver->get_geometry().hbot[ix+1],0);
  63. glVertex3f(x+dX,0,0);
  64. }
  65. glEnd();
  66. //Draw hsoil
  67. glColor3f(0.6,0.3,0);
  68. glLineWidth(1);
  69. glBegin(GL_LINE_STRIP);
  70. for(size_t ix=0;ix<nX;ix+=1){
  71. double x=ix*dX;
  72. glVertex3f(x,solver->get_geometry().hsoil[ix],0.8);
  73. }
  74. glEnd();
  75. //Draw hydr
  76. glColor3f(1,0,0);
  77. glLineWidth(3);
  78. glBegin(GL_LINE_STRIP);
  79. for(size_t ix=0;ix<nX;ix+=1){
  80. double x=ix*dX;
  81. glVertex3f(x,solver->get_solution(step)->hydr[ix]-Psat/(Physics::rho*Physics::g),1);
  82. }
  83. glEnd();
  84. //Draw hov
  85. glColor3f(0,0,0);
  86. glLineWidth(1);
  87. glBegin(GL_LINE_STRIP);
  88. for(size_t ix=0;ix<nX;ix+=1){
  89. double x=ix*dX;
  90. double y=solver->get_solution(step)->hov[ix];
  91. glVertex3f(x,y,1);
  92. }
  93. glEnd();
  94. //Draw hsat
  95. glColor3f(0,1,1);
  96. glLineWidth(1);
  97. glBegin(GL_LINE_STRIP);
  98. for(size_t ix=0;ix<nX;ix+=1){
  99. double x=ix*dX;
  100. double y=solver->get_solution(step)->hsat[ix];
  101. glVertex3f(x,y,1);
  102. }
  103. glEnd();
  104. //Draw pumps
  105. for(auto it=solver->get_source().pumps.begin();it!=solver->get_source().pumps.end();++it){
  106. drawPump(*it);
  107. }
  108. drawOverland();
  109. drawVectors();
  110. }
  111. void
  112. QtOutputView::resizeGL(int w,int h){
  113. glViewport(0,0,w,h);
  114. glMatrixMode(GL_PROJECTION);
  115. glLoadIdentity();
  116. gluOrtho2D(0,lX,0,factor);
  117. glMatrixMode(GL_MODELVIEW);
  118. glLoadIdentity();
  119. }
  120. void
  121. QtOutputView::drawTriangle(size_t ix1,size_t iz1,size_t ix2,size_t iz2,size_t ix3,size_t iz3){
  122. double factor_inv=1;//1/solver->solution.factor;
  123. double h=height();
  124. double w=width();
  125. double x1=ix1*dX;
  126. double x2=ix2*dX;
  127. double x3=ix3*dX;
  128. double y1=solver->get_geometry().Z[ix1][iz1]*factor_inv;;
  129. double y2=solver->get_geometry().Z[ix2][iz2]*factor_inv;;
  130. double y3=solver->get_geometry().Z[ix3][iz3]*factor_inv;;
  131. setColor(ix1,iz1);
  132. glVertex3f(x1,y1,0);
  133. setColor(ix2,iz2);
  134. glVertex3f(x2,y2,0);
  135. setColor(ix3,iz3);
  136. glVertex3f(x3,y3,0);
  137. }
  138. void
  139. QtOutputView::draw(size_t _step){
  140. step=_step;
  141. update();
  142. }
  143. void
  144. QtOutputView::mousePressEvent(QMouseEvent* event){
  145. double x=double(event->x())/width()*lX;
  146. double y=1-(double(event->y())/height());
  147. displayInfos(x,y*factor);
  148. }
  149. void
  150. QtOutputView::setColor(size_t ix,size_t iz){
  151. double p=getP(ix,iz);
  152. double s=Physics::s(p);
  153. double r=(1-s);
  154. double g=0.7*(1-s);
  155. double b=0.8*s+0.4*(1-s);
  156. glColor3f(r,g,b);
  157. }
  158. void
  159. QtOutputView::drawVector(size_t ix,size_t iz,double u,double v){
  160. double h=height();
  161. double w=width();
  162. double x1=ix*dX;
  163. double y1=solver->get_geometry().Z[ix][iz];
  164. double x2=x1+u;
  165. double y2=y1+v;
  166. double x3=x2+(-0.866*u/lX*w+0.5*v/factor*h)*scale_arrow_head*lX/w;//*lX;
  167. double y3=y2+(-0.5*u/lX*w-0.866*v/factor*h)*scale_arrow_head*factor/h;
  168. double x4=x2+(-0.866*u/lX*w-0.5*v/factor*h)*scale_arrow_head*lX/w;//*lX;
  169. double y4=y2+(0.5*u/lX*w-0.866*v/factor*h)*scale_arrow_head*factor/h;
  170. glColor3f(1,1,0);
  171. glBegin(GL_LINE_STRIP);
  172. glVertex3f(x1,y1,0.8);
  173. glVertex3f(x2,y2,0.8);
  174. glEnd();
  175. glBegin(GL_LINE_STRIP);
  176. glVertex3f(x3,y3,0.8);
  177. glVertex3f(x2,y2,0.8);
  178. glVertex3f(x4,y4,0.8);
  179. glEnd();
  180. }
  181. void
  182. QtOutputView::drawVectors(){
  183. for(size_t ix=1;ix<nX-1;ix+=arrow_step_x){
  184. //Case iz=0
  185. double p1=getP(ix,0);
  186. double p2=getP(ix,1);
  187. double u=-Physics::k0*Physics::kr(getPl(ix)+Physics::rho*Physics::g*(getl(ix)-solver->get_geometry().Z[ix][0]))*(getHydr(ix+1)-getHydr(ix-1))/(2*dX);
  188. double v=-Physics::k0*Physics::kr(p1)*((p2-p1)/(Physics::rho*Physics::g*solver->get_geometry().dZ[ix])+1);
  189. drawVector(ix,0,scale_arrow*u,scale_arrow*v);
  190. for(size_t iz=arrow_step_z;iz<solver->get_geometry().nZ[ix]-1;iz+=arrow_step_z){
  191. double p0=getP(ix,iz-1);
  192. double p1=getP(ix,iz);
  193. double p2=getP(ix,iz+1);
  194. double u=-Physics::k0*Physics::kr(getPl(ix)+Physics::rho*Physics::g*(getl(ix)-solver->get_geometry().Z[ix][iz]))*(getHydr(ix+1)-getHydr(ix-1))/(2*dX);
  195. double v=-Physics::k0*Physics::kr(p1)*((p2-p0)/(2*Physics::rho*Physics::g*solver->get_geometry().dZ[ix])+1);
  196. drawVector(ix,iz,scale_arrow*u,scale_arrow*v);
  197. }
  198. }
  199. }
  200. void
  201. QtOutputView::drawPump(Pump* pump){
  202. double time=(step*Time::dT)/Time::T;
  203. double a=pump->get_amplitude(time);
  204. if(a==0) return;
  205. double l=pump->get_left(time);
  206. double r=pump->get_right(time);
  207. double b=pump->get_bottom(time);//*factor_inv;
  208. double t=pump->get_top(time);//*factor_inv;
  209. double dl=pump->get_left_delta(time);
  210. double dr=pump->get_right_delta(time);
  211. double db=pump->get_bottom_delta(time);//*factor_inv;
  212. double dt=pump->get_top_delta(time);//*factor_inv;
  213. glLineWidth(2);
  214. if(a>0) glColor3f(0,0.6,1);
  215. else glColor3f(1,0.2,0);
  216. glLineStipple(3, 0xAAAA);
  217. glBegin(GL_LINE_LOOP);
  218. glVertex3f(l,b,1);
  219. glVertex3f(r,b,1);
  220. glVertex3f(r,t,1);
  221. glVertex3f(l,t,1);
  222. glEnd();
  223. glLineWidth(1);
  224. glEnable(GL_LINE_STIPPLE);
  225. glBegin(GL_LINE_LOOP);
  226. glVertex3f(l-dl,b-db,1);
  227. glVertex3f(r+dr,b-db,1);
  228. glVertex3f(r+dr,t+dt,1);
  229. glVertex3f(l-dl,t+dt,1);
  230. glEnd();
  231. glDisable(GL_LINE_STIPPLE);
  232. }
  233. void
  234. QtOutputView::drawOverland(){
  235. const Geometry& G=solver->get_geometry();
  236. double dX=G.dX;
  237. glColor3f(0.5,0.5,1);
  238. glBegin(GL_QUADS);
  239. for(size_t i=0;i<G.nX-1;++i){
  240. glVertex3f(i*dX,G.hsoil[i],0);
  241. glVertex3f(i*dX,solver->get_solution(step)->hov[i],0);
  242. glVertex3f(i*dX+dX,solver->get_solution(step)->hov[i+1],0);
  243. glVertex3f(i*dX+dX,G.hsoil[i+1],0);
  244. }
  245. glEnd();
  246. }
  247. void
  248. QtOutputView::displayInfos(double x,double z){
  249. cout<<"---------------------------------"<<endl;
  250. cout<<"step = "<<step<<endl;
  251. cout<<"x = "<<x<<endl;
  252. cout<<"z = "<<z<<endl;
  253. size_t ix=floor(x/dX+0.5);
  254. if(ix==nX) --ix;
  255. cout<<"ix = "<<ix<<endl;
  256. double hbot=solver->get_geometry().hbot[ix];
  257. cout<<"hbot = "<<hbot<<endl;
  258. double hsoil=solver->get_geometry().hsoil[ix];
  259. cout<<"hsoil = "<<hsoil<<endl;
  260. cout<<"l = "<<getl(ix)<<endl;
  261. if(z>hbot and z<hsoil){
  262. double dZ=solver->get_geometry().dZ[ix];
  263. cout<<"dZ = "<<dZ<<endl;
  264. size_t nz=solver->get_geometry().nZ[ix];
  265. cout<<"nZ = "<<nz<<endl;
  266. size_t iz=floor((z-hbot)/dZ+0.5);
  267. if(iz==nz) --iz;
  268. cout<<"iz = "<<iz<<endl;
  269. cout<<"P = "<<getP(ix,iz)<<endl;
  270. }
  271. }