123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- //
- // Created by lloyd on 2020/9/28.
- //
- #include <iostream>
- #include <math.h>
- #include <vector>
- #include <fstream>
- #include "gtcommonkits.h"
- /*
- ***分割字符串
- @str 被分割的字符串
- @pattern 指定的分割依赖字符串
- */
- std::vector<std::string> gtcommonkits::str_Split(std::string str, std::string pattern) {
- std::string::size_type pos;
- std::vector<std::string> 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 << "未成功打开文件" <<std:: endl;
- }*/
- /*while (getline(myfile, temp))
- {
- outfile << temp;
- outfile << std::endl;
- }*/
- //myfile.close();
- outfile << aLineText;
- outfile << std::endl;
- outfile.close();
- }
- /*
- ***读取txt一行数据
- @path物理路径
- */
- std::string gtcommonkits::getTextFromTxt(std::string path) {
- std::ifstream myfile(path);
- //std::ofstream outfile(path, std::ios::app);
- std::string temp;
- if (!myfile.is_open()) {
- std::cout << "未成功打开文件" << std::endl;
- }
- getline(myfile, temp);
- /*while ()
- {
- outfile << temp;
- outfile << std::endl;
- }*/
- myfile.close();
- //outfile << aLineText;
- //outfile << std::endl;
- //outfile.close();
- return temp;
- }
- /***
- * 贝塞尔插值fuc
- * 终将会存在硬件限制,不能真正的无限阶,但是计算功图200阶小case
- * @poss 贝塞尔曲线控制点坐标(包含起点终点)
- * @precision精度,需要计算的该条贝塞尔曲线上的点的数目
- * @dimersion该条贝塞尔曲线上的点维度,坐标轴数(二维坐标,三维坐标...)
- */
- std::vector<std::vector<double>>
- gtcommonkits::BezierInterpolation(std::vector<std::vector<double>> &poss, int precision, int dimersion = 2) {
- std::vector<std::vector<double>> 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<double> 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<std::vector<double>>
- gtcommonkits::FindPeakValue(std::vector<std::vector<double>> data, int StartPoint, int endPoint, int Weight = 1, int EX = 1) {
- std::vector<std::vector<double>> 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<double> iElem;
- iElem.push_back(data[i][0]);
- iElem.push_back(data[i][1]);
- r.push_back(iElem);
- }
- }
- }
- return r;
- }
- double gtcommonkits::cStandardDeviation(std::vector<double> 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<std::vector<double>> gtcommonkits::gt_trans_strtoarray(std::string sgt ){
- if (sgt.empty()) return std::vector<std::vector<double>>();
- std::string split_str = ",";
- std::vector<std::string> ss = gtcommonkits::str_Split(sgt, split_str);
- int sgt_point_num = ss.size() / 2;
- std::vector<std::vector<double>> 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<double> 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<std::vector<double>> 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;
- }
|