gtcommonkits.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // Created by lloyd on 2020/9/28.
  3. //
  4. #include <iostream>
  5. #include <math.h>
  6. #include <vector>
  7. #include <fstream>
  8. #include "gtcommonkits.h"
  9. /*
  10. ***分割字符串
  11. @str 被分割的字符串
  12. @pattern 指定的分割依赖字符串
  13. */
  14. std::vector<std::string> gtcommonkits::str_Split(std::string str, std::string pattern) {
  15. std::string::size_type pos;
  16. std::vector<std::string> result;
  17. str += pattern;//扩展字符串以方便操作
  18. int size = str.size();
  19. for (int i = 0; i < size; i++) {
  20. pos = str.find(pattern, i);
  21. if (pos < size) {
  22. std::string s = str.substr(i, pos - i);
  23. result.push_back(s);
  24. i = pos + pattern.size() - 1;
  25. }
  26. }
  27. return result;
  28. }
  29. /*
  30. ***写入txt文件
  31. @path物理路径
  32. @aLineText 一行文本内容
  33. */
  34. void gtcommonkits::saveTxt(std::string path, std::string aLineText) {
  35. //std::ifstream myfile(path);
  36. std::ofstream outfile(path, std::ios::app);
  37. //std::string temp;
  38. /*if (!myfile.is_open())
  39. {
  40. std::cout << "未成功打开文件" <<std:: endl;
  41. }*/
  42. /*while (getline(myfile, temp))
  43. {
  44. outfile << temp;
  45. outfile << std::endl;
  46. }*/
  47. //myfile.close();
  48. outfile << aLineText;
  49. outfile << std::endl;
  50. outfile.close();
  51. }
  52. /*
  53. ***读取txt一行数据
  54. @path物理路径
  55. */
  56. std::string gtcommonkits::getTextFromTxt(std::string path) {
  57. std::ifstream myfile(path);
  58. //std::ofstream outfile(path, std::ios::app);
  59. std::string temp;
  60. if (!myfile.is_open()) {
  61. std::cout << "未成功打开文件" << std::endl;
  62. }
  63. getline(myfile, temp);
  64. /*while ()
  65. {
  66. outfile << temp;
  67. outfile << std::endl;
  68. }*/
  69. myfile.close();
  70. //outfile << aLineText;
  71. //outfile << std::endl;
  72. //outfile.close();
  73. return temp;
  74. }
  75. /***
  76. * 贝塞尔插值fuc
  77. * 终将会存在硬件限制,不能真正的无限阶,但是计算功图200阶小case
  78. * @poss 贝塞尔曲线控制点坐标(包含起点终点)
  79. * @precision精度,需要计算的该条贝塞尔曲线上的点的数目
  80. * @dimersion该条贝塞尔曲线上的点维度,坐标轴数(二维坐标,三维坐标...)
  81. */
  82. std::vector<std::vector<double>>
  83. gtcommonkits::BezierInterpolation(std::vector<std::vector<double>> &poss, int precision, int dimersion = 2) {
  84. std::vector<std::vector<double>> bezier_points;
  85. //维度,坐标轴数(二维坐标,三维坐标...)
  86. if (dimersion != 2) return bezier_points;
  87. //贝塞尔曲线控制点数(阶数),此说法不太对,但是便于理解记忆
  88. int number = poss.size();
  89. //控制点数不小于 2 ,至少为二维坐标系
  90. if (number < 2 || dimersion < 2)
  91. return bezier_points;
  92. //double[][] result = new double[precision][];
  93. //计算杨辉三角
  94. double mi[number];
  95. mi[0] = mi[1] = 1;
  96. for (int i = 3; i <= number; i++) {
  97. double t[i - 1];
  98. for (int j = 0; j < i - 1; j++) {
  99. t[j] = mi[j];
  100. }
  101. mi[0] = mi[i - 1] = 1;
  102. for (int j = 0; j < i - 2; j++) {
  103. mi[j + 1] = t[j] + t[j + 1];
  104. }
  105. }
  106. //计算坐标点
  107. for (int i = 0; i < precision; i++) {
  108. double t = (double) i / precision;
  109. std::vector<double> point_unit;
  110. for (int j = 0; j < dimersion; j++) {
  111. double temp = 0.0f;
  112. for (int k = 0; k < number; k++) {
  113. temp += std::pow(1 - t, number - k - 1) * poss[k][j] * std::pow(t, k) * mi[k];
  114. //幂运算改为循环乘,防止溢出???
  115. //var c_temp=poss[k][j]* mi[k];
  116. //for (var c = 1; c <= number - k - 1; c++)
  117. // c_temp *= (1 - t);
  118. //for (var c = 1; c <= k; c++)
  119. // c_temp *= t;
  120. //temp += c_temp;
  121. }
  122. point_unit.push_back(temp);
  123. }
  124. bezier_points.push_back(point_unit);
  125. }
  126. return bezier_points;
  127. }
  128. std::vector<std::vector<double>>
  129. gtcommonkits::FindPeakValue(std::vector<std::vector<double>> data, int StartPoint, int endPoint, int Weight = 1, int EX = 1) {
  130. std::vector<std::vector<double>> r;
  131. for (int i = StartPoint; i < endPoint && i < data.size(); i++) {
  132. if (i - Weight >= 0 && i + Weight < data.size()) {
  133. bool isPeak = true;
  134. for (int j = 1; j <= Weight; j++) {
  135. if (data[i][0] >= data[i - j][0] && data[i][0] <= data[i + j][0]) {
  136. if (EX * data[i][1] > EX * data[i - j][1] && EX * data[i][1] > EX * data[i + j][1])
  137. isPeak = true;
  138. else isPeak = false;
  139. } else isPeak = false;
  140. if (!isPeak) break;
  141. }
  142. if (isPeak) {
  143. std::vector<double> iElem;
  144. iElem.push_back(data[i][0]);
  145. iElem.push_back(data[i][1]);
  146. r.push_back(iElem);
  147. }
  148. }
  149. }
  150. return r;
  151. }
  152. double gtcommonkits::cStandardDeviation(std::vector<double> values) {
  153. double avgValue =0;
  154. double temp = 0;
  155. for (int i = 0; i < values.size(); i++) {
  156. temp += (values[i] - avgValue) * (values[i] - avgValue);
  157. }
  158. temp = temp / values.size();
  159. temp = std::sqrt(temp);
  160. return temp;
  161. }
  162. std::vector<std::vector<double>> gtcommonkits::gt_trans_strtoarray(std::string sgt ){
  163. if (sgt.empty()) return std::vector<std::vector<double>>();
  164. std::string split_str = ",";
  165. std::vector<std::string> ss = gtcommonkits::str_Split(sgt, split_str);
  166. int sgt_point_num = ss.size() / 2;
  167. std::vector<std::vector<double>> gt;//[sgt_point_num];
  168. for (int i = 0; i < sgt_point_num; ++i) {
  169. double s_d = std::atof(ss[i * 2 + 0].c_str()) / 100.0;
  170. double l_d = std::atof(ss[i * 2 + 1].c_str()) / 100.0;
  171. std::vector<double> point;
  172. point.push_back(s_d);
  173. point.push_back(l_d);
  174. gt.push_back(point);
  175. }
  176. return gt;
  177. }
  178. std::string gtcommonkits::gt_trans_arraytostr(std::vector<std::vector<double>> sgt) {
  179. if (sgt.empty() || sgt.size() == 0) return "";
  180. std::string gt_str = "";
  181. for (int i = 0; i < sgt.size(); i++) {
  182. if (i > 0) gt_str += ",";
  183. gt_str += std::to_string((int) std::round(sgt[i][0] * 100)) + "," +
  184. std::to_string((int) std::round(sgt[i][1] * 100));
  185. }
  186. return gt_str;
  187. }