// // Created by lloyd on 2020/9/28. // #include #include #include #include #include "gtcommonkits.h" /* ***分割字符串 @str 被分割的字符串 @pattern 指定的分割依赖字符串 */ std::vector gtcommonkits::str_Split(std::string str, std::string pattern) { std::string::size_type pos; std::vector result; str += pattern;//扩展字符串以方便操作 int size = str.size(); for (int i = 0; i < size; i++) { pos = str.find(pattern, i); if (pos < size) { std::string s = str.substr(i, pos - i); result.push_back(s); i = pos + pattern.size() - 1; } } return result; } /* ***写入txt文件 @path物理路径 @aLineText 一行文本内容 */ void gtcommonkits::saveTxt(std::string path, std::string aLineText) { //std::ifstream myfile(path); std::ofstream outfile(path, std::ios::app); //std::string temp; /*if (!myfile.is_open()) { std::cout << "未成功打开文件" <> gtcommonkits::BezierInterpolation(std::vector> &poss, int precision, int dimersion = 2) { std::vector> bezier_points; //维度,坐标轴数(二维坐标,三维坐标...) if (dimersion != 2) return bezier_points; //贝塞尔曲线控制点数(阶数),此说法不太对,但是便于理解记忆 int number = poss.size(); //控制点数不小于 2 ,至少为二维坐标系 if (number < 2 || dimersion < 2) return bezier_points; //double[][] result = new double[precision][]; //计算杨辉三角 double mi[number]; mi[0] = mi[1] = 1; for (int i = 3; i <= number; i++) { double t[i - 1]; for (int j = 0; j < i - 1; j++) { t[j] = mi[j]; } mi[0] = mi[i - 1] = 1; for (int j = 0; j < i - 2; j++) { mi[j + 1] = t[j] + t[j + 1]; } } //计算坐标点 for (int i = 0; i < precision; i++) { double t = (double) i / precision; std::vector point_unit; for (int j = 0; j < dimersion; j++) { double temp = 0.0f; for (int k = 0; k < number; k++) { temp += std::pow(1 - t, number - k - 1) * poss[k][j] * std::pow(t, k) * mi[k]; //幂运算改为循环乘,防止溢出??? //var c_temp=poss[k][j]* mi[k]; //for (var c = 1; c <= number - k - 1; c++) // c_temp *= (1 - t); //for (var c = 1; c <= k; c++) // c_temp *= t; //temp += c_temp; } point_unit.push_back(temp); } bezier_points.push_back(point_unit); } return bezier_points; } std::vector> gtcommonkits::FindPeakValue(std::vector> data, int StartPoint, int endPoint, int Weight = 1, int EX = 1) { std::vector> r; for (int i = StartPoint; i < endPoint && i < data.size(); i++) { if (i - Weight >= 0 && i + Weight < data.size()) { bool isPeak = true; for (int j = 1; j <= Weight; j++) { if (data[i][0] >= data[i - j][0] && data[i][0] <= data[i + j][0]) { if (EX * data[i][1] > EX * data[i - j][1] && EX * data[i][1] > EX * data[i + j][1]) isPeak = true; else isPeak = false; } else isPeak = false; if (!isPeak) break; } if (isPeak) { std::vector iElem; iElem.push_back(data[i][0]); iElem.push_back(data[i][1]); r.push_back(iElem); } } } return r; } double gtcommonkits::cStandardDeviation(std::vector values) { double avgValue =0; double temp = 0; for (int i = 0; i < values.size(); i++) { temp += (values[i] - avgValue) * (values[i] - avgValue); } temp = temp / values.size(); temp = std::sqrt(temp); return temp; } std::vector> gtcommonkits::gt_trans_strtoarray(std::string sgt ){ if (sgt.empty()) return std::vector>(); std::string split_str = ","; std::vector ss = gtcommonkits::str_Split(sgt, split_str); int sgt_point_num = ss.size() / 2; std::vector> gt;//[sgt_point_num]; for (int i = 0; i < sgt_point_num; ++i) { double s_d = std::atof(ss[i * 2 + 0].c_str()) / 100.0; double l_d = std::atof(ss[i * 2 + 1].c_str()) / 100.0; std::vector point; point.push_back(s_d); point.push_back(l_d); gt.push_back(point); } return gt; } std::string gtcommonkits::gt_trans_arraytostr(std::vector> sgt) { if (sgt.empty() || sgt.size() == 0) return ""; std::string gt_str = ""; for (int i = 0; i < sgt.size(); i++) { if (i > 0) gt_str += ","; gt_str += std::to_string((int) std::round(sgt[i][0] * 100)) + "," + std::to_string((int) std::round(sgt[i][1] * 100)); } return gt_str; }