static_tool.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // Created by zx on 2021/5/11.
  3. //
  4. #pragma once
  5. #include "defines.hpp"
  6. #include <fstream>
  7. #include <fcntl.h>
  8. #include <vector>
  9. #include <pcl/io/pcd_io.h>
  10. static bool string2point(const std::string &str, pcl::PointXYZ &point) {
  11. std::istringstream iss;
  12. iss.str(str);
  13. float value;
  14. float data[3];
  15. for (int i = 0; i < 3; ++i) {
  16. if (iss >> value) {
  17. data[i] = value;
  18. } else {
  19. return false;
  20. }
  21. }
  22. point.x = data[0];
  23. point.y = data[1];
  24. point.z = data[2];
  25. return true;
  26. }
  27. static bool ReadTxtCloud(const std::string &file, pcl::PointCloud<pcl::PointXYZ>::Ptr &cloud) {
  28. DLOG(INFO) << "Load point data form file: " << file;
  29. std::ifstream fin(file.c_str());
  30. if (fin.bad()) {
  31. LOG(WARNING) << "File " << file << " don't exist.";
  32. return false;
  33. }
  34. const int line_length = 64;
  35. char str[line_length] = {0};
  36. while (fin.getline(str, line_length)) {
  37. pcl::PointXYZ point{};
  38. if (string2point(str, point)) {
  39. cloud->push_back(point);
  40. }
  41. }
  42. DLOG(INFO) << "Load point complete, get point number: " << cloud->size();
  43. return true;
  44. }
  45. static bool WriteTxtCloud(const std::string &file, pcl::PointCloud<pcl::PointXYZ>::Ptr &cloud) {
  46. DLOG(INFO) << "begain push point data to " << file;
  47. std::fstream fileTxtCloud;
  48. fileTxtCloud.open(file, std::ios_base::trunc | std::ios::in | std::ios::out);
  49. for (auto &point: *cloud) {
  50. fileTxtCloud << point.x << " " << point.y << " " << point.z << std::endl;
  51. }
  52. fileTxtCloud.close();
  53. DLOG(INFO) << "Push point complete, push point number: " << cloud->size();
  54. return true;
  55. }
  56. static bool WriteTxtCloud(const std::string &file, pcl::PointCloud<pcl::PointXYZI>::Ptr &cloud) {
  57. DLOG(INFO) << "begain push point data to " << file;
  58. std::fstream fileTxtCloud;
  59. fileTxtCloud.open(file, std::ios_base::trunc | std::ios::in | std::ios::out);
  60. for (auto &point: *cloud) {
  61. fileTxtCloud << point.x << " " << point.y << " " << point.z << std::endl;
  62. }
  63. fileTxtCloud.close();
  64. DLOG(INFO) << "Push point complete, push point number: " << cloud->size();
  65. return true;
  66. }
  67. static bool WriteTxtCloud(const std::string &file, pcl::PointCloud<pcl::PointALL>::Ptr &cloud) {
  68. DLOG(INFO) << "begain push point data to " << file;
  69. std::fstream fileTxtCloud;
  70. fileTxtCloud.open(file, std::ios_base::trunc | std::ios::in | std::ios::out);
  71. for (auto &point: *cloud) {
  72. fileTxtCloud << point.x << " " << point.y << " " << point.z << " " << point.intensity << std::endl;
  73. }
  74. fileTxtCloud.close();
  75. DLOG(INFO) << "Push point complete, push point number: " << cloud->size();
  76. return true;
  77. }
  78. template <typename T>
  79. static int WritePCDCloud(const std::string &file, boost::shared_ptr<pcl::PointCloud<T>> &cloud) {
  80. //static int WritePCDCloud(const std::string &file, boost::shared_ptr<pcl::PointCloud<pcl::PointXYZ>> &cloud) {
  81. return pcl::io::savePCDFile(file, *cloud);
  82. }
  83. #include <Eigen/Core>
  84. // 计算均方误差
  85. static bool computer_var(std::vector<double> data, double &var) {
  86. if (data.empty())
  87. return false;
  88. Eigen::VectorXd dis_vec(data.size());
  89. for (int i = 0; i < data.size(); ++i) {
  90. dis_vec[i] = data[i];
  91. }
  92. double mean = dis_vec.mean();
  93. Eigen::VectorXd mean_vec(data.size());
  94. Eigen::VectorXd mat = dis_vec - (mean_vec.setOnes() * mean);
  95. Eigen::MatrixXd result = (mat.transpose()) * mat;
  96. var = sqrt(result(0) / double(data.size()));
  97. return true;
  98. }
  99. #include <opencv2/opencv.hpp>
  100. static double distance(cv::Point2f p1, cv::Point2f p2) {
  101. return sqrt(pow(p1.x - p2.x, 2.0) + pow(p1.y - p2.y, 2.0));
  102. }
  103. #include <google/protobuf/message.h>
  104. #include <google/protobuf/text_format.h>
  105. #include <google/protobuf/io/zero_copy_stream_impl.h>
  106. //读取protobuf 配置文件,转化为protobuf参数形式
  107. //input: prototxt_path :prototxt文件路径
  108. //ouput: parameter: protobuf参数,这里是消息基类,实际调用时传入对应的子类即可。
  109. static bool read_proto_param(std::string prototxt_path, ::google::protobuf::Message &protobuf_parameter) {
  110. int fd = open(prototxt_path.c_str(), O_RDONLY);
  111. if (fd == -1) return false;
  112. google::protobuf::io::FileInputStream *input = new google::protobuf::io::FileInputStream(fd);
  113. bool ret = google::protobuf::TextFormat::Parse(input, &protobuf_parameter);
  114. delete input;
  115. close(fd);
  116. return ret;
  117. }
  118. template <typename T>
  119. static T mean(std::vector<T> &values, int erase = 0) {
  120. T m = 0;
  121. if (erase) {
  122. if (erase * 2 > values.size()) {
  123. return 0;
  124. }
  125. sort(values.begin(), values.end());
  126. for (int i = 0; i < erase; i++) {
  127. values.erase(values.begin());
  128. values.erase(values.end() - 1);
  129. }
  130. }
  131. for (auto &value: values) {
  132. m += value;
  133. }
  134. return m / values.size();
  135. }
  136. template <typename T>
  137. static T variance(const T &mean, std::vector<T> &values) {
  138. T var = 0;
  139. for (auto &value: values) {
  140. var += sqrt((value - mean) * (value - mean));
  141. }
  142. return var / values.size();
  143. }
  144. template <typename T>
  145. static T MSE(std::vector<T> &values, int erase = 1, float radio = 1) {
  146. if (erase) {
  147. if (erase * 2 > values.size()) {
  148. return 0;
  149. }
  150. sort(values.begin(), values.end());
  151. for (int i = 0; i < erase; i++) {
  152. values.erase(values.begin());
  153. values.erase(values.end() - 1);
  154. }
  155. }
  156. T mse = mean(values);
  157. T var = variance(mse, values);
  158. int mse_num = 0;
  159. mse = 0;
  160. for (auto &value: values) {
  161. if (fabs(value - var) < radio * var) {
  162. mse += value;
  163. mse_num++;
  164. }
  165. }
  166. return mse / mse_num;
  167. }