1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- #pragma once
- #include <vector>
- #include "dt.h"
- #include <cmath>
- /*
- 1、数学计算相关函数
- */
- typedef XY_DOUBLE_STRUCT POINTF;
- typedef X_Y_Z_STRUCT POINTF_3D;
- //struct POINTF {
- // double x, y;
- // POINTF() : x(0), y(0) {};
- // POINTF(double x, double y) : x(x), y(y) {};
- //};
- //struct POINTF_3D {
- // double x, y, z;
- // POINTF_3D(double x, double y, double z) : x(x), y(y), z(z) {}
- //};
- struct Vector {
- double x, y, z;
- Vector(double x, double y, double z) : x(x), y(y), z(z) {}
- // 向量叉乘
- Vector cross(const Vector& other) const {
- return Vector(
- y * other.z - z * other.y,
- z * other.x - x * other.z,
- x * other.y - y * other.x
- );
- }
- // 向量点乘
- double dot(const Vector& other) const {
- return x * other.x + y * other.y + z * other.z;
- }
- // 向量的模
- double magnitude() const {
- return std::sqrt(x * x + y * y + z * z);
- }
- // 判断向量是否近似为零向量
- bool isZero(double tolerance = 1e-9) const {
- return magnitude() < tolerance;
- }
- };
- class __declspec(dllexport) CMathCalc
- {
- private:
- double CalculateSlope(XY_DOUBLE_STRUCT point1, XY_DOUBLE_STRUCT point2);
- static double ClampCosValue(double value);
- static bool IsValidPoint(const POINTF_3D& point);
- static bool IsValidPoint(const POINTF& point);
- public:
- // 计算两个点的位置和角度偏差的函数
- X_Y_ANGLE_STRUCT CalculateDeviation(const POINTF& p1, const POINTF& p2);
- //计算某点旋转一定角度后的位置
- bool CalRotationPoint(XY_DOUBLE_STRUCT prePoint, double angle, XY_DOUBLE_STRUCT& pointResult);
- //计算两条线的夹角,小于、等于90度的角
- double CalculateAngle(XY_DOUBLE_STRUCT line1_point1, XY_DOUBLE_STRUCT line1_point2, XY_DOUBLE_STRUCT line2_point1, XY_DOUBLE_STRUCT line2_point2);
- //计算一条线与X轴的夹角,小于、等于90度的角
- double CalculateAngle_X(XY_DOUBLE_STRUCT point1, XY_DOUBLE_STRUCT point2);
- //计算一条线与Y轴夹角,小于、等于90度的角
- double CalculateAngle_Y(XY_DOUBLE_STRUCT point1, XY_DOUBLE_STRUCT point2);
- //计算两条线的交点,返回false时,表示两条线平行,没有交点
- bool CalculateCross(XY_DOUBLE_STRUCT line1_point1, XY_DOUBLE_STRUCT line1_point2,
- XY_DOUBLE_STRUCT line2_point1, XY_DOUBLE_STRUCT line2_point2, XY_DOUBLE_STRUCT& out_point);
- //计算直线与X轴的交点,返回false时,表示与X轴平行,没有交点
- bool CalculateCross_X(XY_DOUBLE_STRUCT line_point1, XY_DOUBLE_STRUCT line_point2, XY_DOUBLE_STRUCT& out_point);
- //计算直线与Y轴的交点,返回false时,表示与Y轴平行,没有交点
- bool CalculateCross_Y(XY_DOUBLE_STRUCT line_point1, XY_DOUBLE_STRUCT line_point2, XY_DOUBLE_STRUCT& out_point);
- //计算一个点绕另外一个点旋转
- bool rotationPos(const XY_DOUBLE_STRUCT point, const double rotationRad, XY_DOUBLE_STRUCT center, XY_DOUBLE_STRUCT& rotatedPoint);
- //最小二乘法拟合直线, y = a * x + b
- static bool LSCFitLine(const std::vector<POINTF>& pointSet, double& slope, double& intercept);
- //最小二乘法拟合曲线 y=a*x2 + bx + c
- static bool LSCFitCurve(const std::vector<POINTF>& pointSet, std::vector<double>& result);
- // 判断三点是否共线
- static bool IsCollinear(const POINTF_3D& A, const POINTF_3D& B, const POINTF_3D& C);
- // 计算平面与水平面的夹角
- static double CalHorizAngle(const POINTF_3D& A, const POINTF_3D& B, const POINTF_3D& C);
- // 计算向量与x轴的夹角
- static double CalAngleWithXAxis(const POINTF_3D& A, const POINTF_3D& B, const POINTF_3D& C);
- // 计算向量与y轴的夹角
- static double CalAngleWithYAxis(const POINTF_3D& A, const POINTF_3D& B, const POINTF_3D& C);
- };
|