#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);
    //����ij����תһ���ǶȺ��λ��
    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);
};