input_geometry_curves.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "input_geometry_curves.hpp"
  2. QtInputGeometryCurves::QtInputGeometryCurves(){
  3. margin=10;
  4. radius=8;
  5. min_d=0.01;
  6. initPoints();
  7. hbot.setPoints(point,np);
  8. hbot.compute();
  9. hsoil.setPoints(&point[np],np);
  10. hsoil.compute();
  11. }
  12. void
  13. QtInputGeometryCurves::initPoints(){
  14. for(size_t i=0;i<np;++i){
  15. double x=double(i)/(np-1);
  16. point[i].x=x;
  17. point[i].y=0.7;
  18. point[i+np].x=x;
  19. point[i+np].y=0.3;
  20. }
  21. }
  22. int
  23. QtInputGeometryCurves::get_x(float x,int w){
  24. return margin+x*w;
  25. }
  26. int
  27. QtInputGeometryCurves::get_y(float y,int h){
  28. return y*h;
  29. }
  30. void
  31. QtInputGeometryCurves::paintEvent(QPaintEvent* event){
  32. QPainter painter(this);
  33. painter.setRenderHint(QPainter::Antialiasing);
  34. int w=width()-2*margin;
  35. int h=height();
  36. // Hbot
  37. painter.setBrush(Qt::SolidPattern);
  38. painter.setPen(QColor(102,102,102));
  39. drawSpline(hbot,painter);
  40. painter.setPen(QColor(154,77,0));
  41. drawSpline(hsoil,painter);
  42. painter.setPen(Qt::black);
  43. //Control points
  44. for(size_t i=0;i<2*np;++i){
  45. Point& P=point[i];
  46. painter.drawEllipse(get_x(P.x,w)-radius,get_y(P.y,h)-radius,2*radius,2*radius);
  47. }
  48. }
  49. void
  50. QtInputGeometryCurves::mousePressEvent(QMouseEvent* event){
  51. int mx=event->x();
  52. int my=event->y();
  53. selected=findPoint(mx,my);
  54. }
  55. void
  56. QtInputGeometryCurves::mouseMoveEvent(QMouseEvent* event){
  57. if(selected<2*np){
  58. int w=width()-2*margin;
  59. int h=height();
  60. float mx=event->x();
  61. float my=event->y();
  62. float x=(mx-margin)/w;
  63. float y=my/h;
  64. moveSelected(x,y);
  65. update();
  66. }
  67. }
  68. void
  69. QtInputGeometryCurves::mouseReleaseEvent(QMouseEvent* event){
  70. selected=2*np;
  71. }
  72. size_t
  73. QtInputGeometryCurves::findPoint(int x,int y){
  74. int w=width()-2*margin;
  75. int h=height();
  76. for(size_t i=0;i<2*np;++i){
  77. Point& P=point[i];
  78. int px=get_x(P.x,w);
  79. int py=get_y(P.y,h);
  80. int dx=x-px;
  81. int dy=y-py;
  82. int d=dx*dx+dy*dy;
  83. if(d<=radius*radius) return i;
  84. }
  85. return 2*np;
  86. }
  87. void
  88. QtInputGeometryCurves::moveSelected(float x,float y){
  89. double xp=point[selected].x;
  90. double yp=point[selected].y;
  91. if(selected!=0 and selected!=np-1){
  92. float xmin=point[selected-1].x+min_d;
  93. float xmax=point[selected+1].x-min_d;
  94. if(xmin<=x and x<=xmax){
  95. point[selected].x=x;
  96. }
  97. }
  98. if(0<=y and y<=1){
  99. point[selected].y=y;
  100. }
  101. if(selected<np) hbot.compute();
  102. else hsoil.compute();
  103. double w=width()-2*margin;
  104. bool ok=true;
  105. for(int i=1;i<=w;++i){
  106. double x=i/w;
  107. double ybot=hbot(x);
  108. double ysoil=hsoil(x);
  109. if(ybot<min_d or ybot>1-min_d or ysoil<min_d or ysoil>1-min_d or ybot<ysoil+min_d){
  110. point[selected].x=xp;
  111. point[selected].y=yp;
  112. if(selected<np) hbot.compute();
  113. else hsoil.compute();
  114. return;
  115. }
  116. }
  117. }
  118. void
  119. QtInputGeometryCurves::drawSpline(Spline& S,QPainter& painter){
  120. double w=width()-2*margin;
  121. double h=height();
  122. double xp=0;
  123. double yp=S(0);
  124. for(int i=1;i<=w;i+=2){
  125. double x=i/w;
  126. double y=S(x);
  127. painter.drawLine(margin+xp*w,yp*h,margin+x*w,y*h);
  128. xp=x;
  129. yp=y;
  130. }
  131. }
  132. void
  133. QtInputGeometryCurves::save(fstream& file){
  134. for(size_t i=0;i<2*np;++i){
  135. file.write((char*)&point[i].x,sizeof(double));
  136. file.write((char*)&point[i].y,sizeof(double));
  137. }
  138. }
  139. void
  140. QtInputGeometryCurves::load(fstream& file){
  141. for(size_t i=0;i<2*np;++i){
  142. file.read((char*)&point[i].x,sizeof(double));
  143. file.read((char*)&point[i].y,sizeof(double));
  144. }
  145. hbot.compute();
  146. hsoil.compute();
  147. update();
  148. }