CMathCalc.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #pragma once
  2. #include <vector>
  3. #include "dt.h"
  4. #include <cmath>
  5. /*
  6. 1、数学计算相关函数
  7. */
  8. typedef XY_DOUBLE_STRUCT POINTF;
  9. typedef X_Y_Z_STRUCT POINTF_3D;
  10. //struct POINTF {
  11. // double x, y;
  12. // POINTF() : x(0), y(0) {};
  13. // POINTF(double x, double y) : x(x), y(y) {};
  14. //};
  15. //struct POINTF_3D {
  16. // double x, y, z;
  17. // POINTF_3D(double x, double y, double z) : x(x), y(y), z(z) {}
  18. //};
  19. struct Vector {
  20. double x, y, z;
  21. Vector(double x, double y, double z) : x(x), y(y), z(z) {}
  22. // 向量叉乘
  23. Vector cross(const Vector& other) const {
  24. return Vector(
  25. y * other.z - z * other.y,
  26. z * other.x - x * other.z,
  27. x * other.y - y * other.x
  28. );
  29. }
  30. // 向量点乘
  31. double dot(const Vector& other) const {
  32. return x * other.x + y * other.y + z * other.z;
  33. }
  34. // 向量的模
  35. double magnitude() const {
  36. return std::sqrt(x * x + y * y + z * z);
  37. }
  38. // 判断向量是否近似为零向量
  39. bool isZero(double tolerance = 1e-9) const {
  40. return magnitude() < tolerance;
  41. }
  42. };
  43. class __declspec(dllexport) CMathCalc
  44. {
  45. private:
  46. double CalculateSlope(XY_DOUBLE_STRUCT point1, XY_DOUBLE_STRUCT point2);
  47. static double ClampCosValue(double value);
  48. static bool IsValidPoint(const POINTF_3D& point);
  49. static bool IsValidPoint(const POINTF& point);
  50. public:
  51. // 计算两个点的位置和角度偏差的函数
  52. X_Y_ANGLE_STRUCT CalculateDeviation(const POINTF& p1, const POINTF& p2);
  53. //计算某点旋转一定角度后的位置
  54. bool CalRotationPoint(XY_DOUBLE_STRUCT prePoint, double angle, XY_DOUBLE_STRUCT& pointResult);
  55. //计算两条线的夹角,小于、等于90度的角
  56. double CalculateAngle(XY_DOUBLE_STRUCT line1_point1, XY_DOUBLE_STRUCT line1_point2, XY_DOUBLE_STRUCT line2_point1, XY_DOUBLE_STRUCT line2_point2);
  57. //计算一条线与X轴的夹角,小于、等于90度的角
  58. double CalculateAngle_X(XY_DOUBLE_STRUCT point1, XY_DOUBLE_STRUCT point2);
  59. //计算一条线与Y轴夹角,小于、等于90度的角
  60. double CalculateAngle_Y(XY_DOUBLE_STRUCT point1, XY_DOUBLE_STRUCT point2);
  61. //计算两条线的交点,返回false时,表示两条线平行,没有交点
  62. bool CalculateCross(XY_DOUBLE_STRUCT line1_point1, XY_DOUBLE_STRUCT line1_point2,
  63. XY_DOUBLE_STRUCT line2_point1, XY_DOUBLE_STRUCT line2_point2, XY_DOUBLE_STRUCT& out_point);
  64. //计算直线与X轴的交点,返回false时,表示与X轴平行,没有交点
  65. bool CalculateCross_X(XY_DOUBLE_STRUCT line_point1, XY_DOUBLE_STRUCT line_point2, XY_DOUBLE_STRUCT& out_point);
  66. //计算直线与Y轴的交点,返回false时,表示与Y轴平行,没有交点
  67. bool CalculateCross_Y(XY_DOUBLE_STRUCT line_point1, XY_DOUBLE_STRUCT line_point2, XY_DOUBLE_STRUCT& out_point);
  68. //计算一个点绕另外一个点旋转
  69. bool rotationPos(const XY_DOUBLE_STRUCT point, const double rotationRad, XY_DOUBLE_STRUCT center, XY_DOUBLE_STRUCT& rotatedPoint);
  70. //最小二乘法拟合直线, y = a * x + b
  71. static bool LSCFitLine(const std::vector<POINTF>& pointSet, double& slope, double& intercept);
  72. //最小二乘法拟合曲线 y=a*x2 + bx + c
  73. static bool LSCFitCurve(const std::vector<POINTF>& pointSet, std::vector<double>& result);
  74. // 判断三点是否共线
  75. static bool IsCollinear(const POINTF_3D& A, const POINTF_3D& B, const POINTF_3D& C);
  76. // 计算平面与水平面的夹角
  77. static double CalHorizAngle(const POINTF_3D& A, const POINTF_3D& B, const POINTF_3D& C);
  78. // 计算向量与x轴的夹角
  79. static double CalAngleWithXAxis(const POINTF_3D& A, const POINTF_3D& B, const POINTF_3D& C);
  80. // 计算向量与y轴的夹角
  81. static double CalAngleWithYAxis(const POINTF_3D& A, const POINTF_3D& B, const POINTF_3D& C);
  82. };