input_view.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "qt/input_view.hpp"
  2. QtInputView::QtInputView(QtInputGeometry* _input_geometry,Geometry* geometry,InitialState* _initial_state,Source* _source):QtView(geometry){
  3. input_geometry=_input_geometry;
  4. initial_state=_initial_state;
  5. source=_source;
  6. selected=3*np;
  7. status=Other;
  8. initPoints();
  9. hbot.setPoints(point,np);
  10. hbot.compute();
  11. hsoil.setPoints(&point[np],np);
  12. hsoil.compute();
  13. hsat.setPoints(&point[2*np],np);
  14. hsat.compute();
  15. initial_state->update(*geometry,hsat);
  16. }
  17. void
  18. QtInputView::initPoints(){
  19. for(size_t i=0;i<np;++i){
  20. double x=double(i)/(np-1);
  21. point[i].x=x;
  22. point[i].y=0.2;
  23. point[np+i].x=x;
  24. point[np+i].y=0.8;
  25. point[2*np+i].x=x;
  26. point[2*np+i].y=0.5;
  27. }
  28. }
  29. void
  30. QtInputView::paintGL(){
  31. if(status==Geom or status==Init){
  32. glBegin(GL_QUADS);
  33. double xs=double(pointSize)/width();
  34. double ys=double(pointSize)/height();
  35. glColor3f(0,0,0);
  36. size_t imin=(status==Geom)?0:2*np;
  37. size_t imax=(status==Geom)?2*np:3*np;
  38. for(size_t i=imin;i<imax;++i){
  39. Point& P=point[i];
  40. double x=P.x;
  41. double y=P.y;
  42. glVertex3f(x-xs,y-ys,1);
  43. glVertex3f(x+xs,y-ys,1);
  44. glVertex3f(x+xs,y+ys,1);
  45. glVertex3f(x-xs,y+ys,1);
  46. }
  47. glEnd();
  48. }
  49. for(auto it=initial_state->tanks.begin();it!=initial_state->tanks.end();++it){
  50. paintTank(*it);
  51. }
  52. for(auto it=source->pumps.begin();it!=source->pumps.end();++it){
  53. paintPump(*it);
  54. }
  55. for(auto it=source->clouds.begin();it!=source->clouds.end();++it){
  56. paintCloud(*it);
  57. }
  58. glColor3f(0.6,0.6,0.6);
  59. drawSpline(hbot);
  60. glColor3f(0.6,0.3,0);
  61. drawSpline(hsoil);
  62. glColor3f(0,1,1);
  63. drawSpline(hsat);
  64. QtView::paintGL();
  65. }
  66. void
  67. QtInputView::paintTank(Tank* tank){
  68. double l=tank->left;
  69. double r=tank->right;
  70. double b=tank->bottom;
  71. double t=tank->top;
  72. glLineWidth(2);
  73. glColor3f(1,0,1);
  74. glBegin(GL_LINE_LOOP);
  75. glVertex2f(l,b);
  76. glVertex2f(r,b);
  77. glVertex2f(r,t);
  78. glVertex2f(l,t);
  79. glEnd();
  80. }
  81. void
  82. QtInputView::paintPump(Pump* pump){
  83. double a=pump->get_amplitude(time);
  84. if(a==0) return;
  85. double l=pump->get_left(time);
  86. double r=pump->get_right(time);
  87. double b=pump->get_bottom(time);
  88. double t=pump->get_top(time);
  89. double dl=pump->get_left_delta(time);
  90. double dr=pump->get_right_delta(time);
  91. double db=pump->get_bottom_delta(time);
  92. double dt=pump->get_top_delta(time);
  93. glLineWidth(2);
  94. if(a>0){
  95. glColor3f(0,0.6,1);
  96. }
  97. else{
  98. glColor3f(1,0.2,0);
  99. }
  100. glLineStipple(3, 0xAAAA);
  101. glBegin(GL_LINE_LOOP);
  102. glVertex2f(l,b);
  103. glVertex2f(r,b);
  104. glVertex2f(r,t);
  105. glVertex2f(l,t);
  106. glEnd();
  107. glLineWidth(1);
  108. glEnable(GL_LINE_STIPPLE);
  109. glBegin(GL_LINE_LOOP);
  110. glVertex2f(l-dl,b-db);
  111. glVertex2f(r+dr,b-db);
  112. glVertex2f(r+dr,t+dt);
  113. glVertex2f(l-dl,t+dt);
  114. glEnd();
  115. glDisable(GL_LINE_STIPPLE);
  116. }
  117. void
  118. QtInputView::paintCloud(Cloud* cloud){
  119. double a=cloud->get_amplitude(time);
  120. double amax=cloud->get_amplitude_max();
  121. if(a==0) return;
  122. double l=cloud->get_left(time);
  123. double r=cloud->get_right(time);
  124. double dl=cloud->get_left_delta(time);
  125. double dr=cloud->get_right_delta(time);
  126. double middle=0.97;
  127. double thick=0.03*a/amax;
  128. double top=middle+thick;
  129. double bottom=middle-thick;
  130. glLineWidth(1);
  131. glColor3f(0.5,0.5,0.5);
  132. glLineWidth(1);
  133. //glEnable(GL_LINE_STIPPLE);
  134. glBegin(GL_POLYGON);
  135. glVertex2f(l-dl,middle);
  136. glVertex2f(l,top);
  137. glVertex2f(r,top);
  138. glVertex2f(r+dr,middle);
  139. glVertex2f(r,bottom);
  140. glVertex2f(l,bottom);
  141. glEnd();
  142. //glDisable(GL_LINE_STIPPLE);
  143. }
  144. void
  145. QtInputView::mousePressEvent(QMouseEvent* event){
  146. if(status==Geom or status==Init){
  147. size_t p=findPoint(event->x(),height()-event->y());
  148. if(status==Geom){
  149. selected=(p<2*np)?p:3*np;
  150. }
  151. else{
  152. selected=(p>=2*np)?p:3*np;
  153. }
  154. }
  155. }
  156. void
  157. QtInputView::mouseMoveEvent(QMouseEvent* event){
  158. if(selected<3*np){
  159. double mx=double(event->x())/width();
  160. double my=1-double(event->y())/height();
  161. moveSelected(mx,my);
  162. }
  163. }
  164. size_t
  165. QtInputView::findPoint(int x,int y){
  166. double w=width();
  167. double h=height();
  168. for(size_t i=0;i<3*np;++i){
  169. int px=point[i].x*w;
  170. int py=point[i].y*h;
  171. if(abs(x-px)<pointSize and abs(y-py)<pointSize) return i;
  172. }
  173. return 3*np;
  174. }
  175. void
  176. QtInputView::updateGeometry(){
  177. geometry->update(input_geometry->get_lX(),input_geometry->get_nX(),input_geometry->get_depth(),input_geometry->get_nZ_max(),hsoil,hbot);
  178. initial_state->update(*geometry,hsat);
  179. update();
  180. }
  181. void
  182. QtInputView::updateInitialState(){
  183. update();
  184. }
  185. void
  186. QtInputView::updateSource(){
  187. update();
  188. }
  189. void
  190. QtInputView::drawSpline(Spline& S){
  191. double w=width();
  192. glLineWidth(3);
  193. glBegin(GL_LINE_STRIP);
  194. for(int i=0;i<=w;i+=2){
  195. double x=i/w;
  196. glVertex2f(x,S(x));
  197. }
  198. glEnd();
  199. }
  200. void
  201. QtInputView::moveSelected(double x,double y){
  202. double xp=point[selected].x;
  203. double yp=point[selected].y;
  204. if((selected%np)!=0 and (selected%np)!=np-1){
  205. float xmin=point[selected-1].x+min_d;
  206. float xmax=point[selected+1].x-min_d;
  207. if(xmin<=x and x<=xmax){
  208. point[selected].x=x;
  209. }
  210. }
  211. if(0<=y and y<=1){
  212. point[selected].y=y;
  213. }
  214. if(selected<np) hbot.compute();
  215. else if(selected<2*np) hsoil.compute();
  216. else hsat.compute();
  217. if(selected<2*np){
  218. double w=width();
  219. bool ok=true;
  220. for(int i=0;i<=w;++i){
  221. double x=i/w;
  222. double ybot=hbot(x);
  223. double ysoil=hsoil(x);
  224. if(ybot<min_d or ybot>1-min_d or ysoil<min_d or ysoil>1-min_d or ybot>ysoil-min_d){
  225. point[selected].x=xp;
  226. point[selected].y=yp;
  227. if(selected<np) hbot.compute();
  228. else hsoil.compute();
  229. update();
  230. return;
  231. }
  232. }
  233. }
  234. update();
  235. }
  236. void
  237. QtInputView::mouseReleaseEvent(QMouseEvent* event){
  238. if(selected<3*np) updateGeometry();
  239. }