1
0

line_fit.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #ifndef _LINE_FIT_
  2. #define _LINE_FIT_
  3. #include <math.h>
  4. #include <vector>
  5. #include "zlist.h"
  6. struct fit_item
  7. {
  8. double x,y,xx,xy;
  9. fit_item();
  10. fit_item&set(double x,double y);
  11. void log()const
  12. {
  13. printf("x=%.3lf,y=%.3lf,xx=%.3lf,xy=%.3lf\n",x,y,xx,xy);
  14. }
  15. };
  16. struct card_fit;
  17. struct fit_batch:zlist<fit_item,64>
  18. {
  19. double xo=0,yo=0;
  20. std::vector<card_fit*> tool;
  21. public:
  22. fit_batch();
  23. ~fit_batch();
  24. int tool_size()const
  25. {
  26. return tool.size();
  27. }
  28. card_fit&operator[](int i) { return *tool[i]; }
  29. const card_fit&operator[](int i) const { return *tool[i]; }
  30. void add_tool(double max_x_span,int min_point);
  31. void add_tool(double max_x_span,int min_point,int max_point);
  32. void reset_data();
  33. double add(double x,double y);
  34. void replace(double x,double y);
  35. void remove_bad(int tool_id);
  36. double x(int index)const;
  37. double y(int index)const;
  38. void rlog(int i)const
  39. {
  40. rat(i).log();
  41. }
  42. void log();
  43. };
  44. struct fit_result
  45. {
  46. double k,kb,ke,ka;
  47. double xo,yo;
  48. int num_point;
  49. int min_point;
  50. double max_x_span;
  51. fit_result();
  52. fit_result(double max_x_span,int min_point);
  53. void reset();
  54. int size()const
  55. {
  56. return num_point;
  57. }
  58. bool is_valid()const
  59. {
  60. return num_point>=min_point;
  61. }
  62. void log()
  63. {
  64. printf("%5s, %d, k=(%.3f,%.3f,%.3f)\n",is_valid()?"true":"false", size(),k,kb,ke);
  65. }
  66. double testk(double x,double y)const;
  67. double differ_k(double x,double y)const;
  68. };
  69. struct card_fit:fit_result
  70. {
  71. const fit_batch&d;
  72. double A = 0, B = 0, C = 0, D = 0;
  73. card_fit(const fit_batch&d,double _max_x_span,int min_point);
  74. virtual ~card_fit();
  75. void reset();
  76. void dec_(const fit_item&i);
  77. void add_(const fit_item&i);
  78. virtual void add_last();
  79. virtual bool check_x_span();
  80. virtual bool fit();
  81. };
  82. struct card_fit_stop:card_fit
  83. {
  84. card_fit_stop(const fit_batch&d_,double _max_x_span,int min_point)
  85. :card_fit(d_,_max_x_span,min_point)
  86. {
  87. }
  88. virtual void add_last()
  89. {
  90. }
  91. virtual bool fit()
  92. {
  93. return false;
  94. }
  95. };
  96. struct card_fit_lastn:card_fit
  97. {
  98. int max_point;
  99. card_fit_lastn(const fit_batch&d_,double _max_x_span,int min_point,int max_point_)
  100. :card_fit(d_,_max_x_span,min_point)
  101. ,max_point(max_point_)
  102. {
  103. }
  104. bool check_x_span()
  105. {
  106. int i=size()-1;
  107. for(;i>0;i--)
  108. {
  109. double off=d(0).x-d(i).x;
  110. if(off>max_x_span+0.5 || i>=max_point)
  111. {
  112. --num_point;
  113. dec_(d(i));
  114. continue;
  115. }
  116. break;
  117. }
  118. return num_point>=min_point;
  119. }
  120. };
  121. #endif