287 lines
9.8 KiB
C++
287 lines
9.8 KiB
C++
#ifndef SPC_CORE_H
|
|
#define SPC_CORE_H
|
|
|
|
#include <iostream>
|
|
#include <vector>
|
|
#include <map>
|
|
#include <cmath>
|
|
#include <string>
|
|
#include <stdexcept>
|
|
#include <algorithm>
|
|
#include <memory>
|
|
#include <chrono>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
#include <fstream>
|
|
|
|
// 使用 nlohmann/json 库
|
|
#include "nlohmann/json.hpp"
|
|
using json = nlohmann::json;
|
|
|
|
// ==================== 前向声明 ====================
|
|
class SpcDataXR;
|
|
class SpcDataXS;
|
|
class SpcDataCpk;
|
|
class SpcTestData;
|
|
// class ApiResponseSpc;
|
|
class SpcRequestParam;
|
|
|
|
// ==================== 常量定义 ====================
|
|
const int MAX_SUBGROUP_SIZE = 25;
|
|
const int MIN_SUBGROUP_SIZE = 2;
|
|
const int DEFAULT_DECIMALS = 3;
|
|
|
|
// ==================== SPC 系数结构 ====================
|
|
struct SpcCoefficient
|
|
{
|
|
int ID;
|
|
int NUM;
|
|
double A;
|
|
double A2;
|
|
double A3;
|
|
double B3;
|
|
double B4;
|
|
double B5;
|
|
double B6;
|
|
double D1;
|
|
double D2;
|
|
double D3;
|
|
double D4;
|
|
double C4;
|
|
double C4_1;
|
|
double L_D1;
|
|
double L_D1_1;
|
|
|
|
// 添加默认构造函数
|
|
SpcCoefficient() : ID(0), NUM(0), A(0), A2(0), A3(0), B3(0),
|
|
B4(0), B5(0), B6(0), D1(0), D2(0), D3(0),
|
|
D4(0), C4(0), C4_1(0), L_D1(0), L_D1_1(0) {}
|
|
|
|
SpcCoefficient(int id, int num, double a, double a2, double a3, double b3,
|
|
double b4, double b5, double b6, double d1, double d2,
|
|
double d3, double d4, double c4, double c4_1, double l_d1, double l_d1_1);
|
|
};
|
|
// ==================== SPC 系数表类 ====================
|
|
class SpcCoefficients
|
|
{
|
|
private:
|
|
static std::map<int, SpcCoefficient> coefficientDict;
|
|
static void Initialize();
|
|
|
|
public:
|
|
static const SpcCoefficient &GetBySubgroupSize(int subgroupSize);
|
|
static double GetA2(int subgroupSize);
|
|
static double GetA3(int subgroupSize);
|
|
static double GetD3(int subgroupSize);
|
|
static double GetD4(int subgroupSize);
|
|
static double GetB3(int subgroupSize);
|
|
static double GetB4(int subgroupSize);
|
|
static double GetC4(int subgroupSize);
|
|
static double GetL_D1(int subgroupSize);
|
|
};
|
|
|
|
// ==================== 工具函数 ====================
|
|
namespace SpcUtils
|
|
{
|
|
double Round(double value, int decimals = DEFAULT_DECIMALS);
|
|
double Mean(const std::vector<double> &values);
|
|
double Max(const std::vector<double> &values);
|
|
double Min(const std::vector<double> &values);
|
|
double Range(const std::vector<double> &values);
|
|
double StandardDeviation(const std::vector<double> &values);
|
|
double StandardDeviationPopulation(const std::vector<double> &values);
|
|
std::string GetCurrentTimestamp();
|
|
}
|
|
|
|
// ==================== SPC 数据类 ====================
|
|
class SpcDataXR
|
|
{
|
|
public:
|
|
int n; // 子组大小
|
|
int k; // 子组个数
|
|
double CL_X; // X图中心线
|
|
double UCL_X; // X图上控制限
|
|
double LCL_X; // X图下控制限
|
|
double CL_R; // R图中心线
|
|
double UCL_R; // R图上控制限
|
|
double LCL_R; // R图下控制限
|
|
std::vector<double> CL_Xk; // 各子组平均值
|
|
std::vector<double> CL_Rk; // 各子组极差
|
|
|
|
SpcDataXR();
|
|
json ToJson() const;
|
|
static SpcDataXR FromJson(const json &j);
|
|
};
|
|
|
|
class SpcDataXS
|
|
{
|
|
public:
|
|
int n; // 子组大小
|
|
int k; // 子组个数
|
|
double CL_X; // X图中心线
|
|
double UCL_X; // X图上控制限
|
|
double LCL_X; // X图下控制限
|
|
double CL_S; // S图中心线
|
|
double UCL_S; // S图上控制限
|
|
double LCL_S; // S图下控制限
|
|
std::vector<double> CL_Xk; // 各子组平均值
|
|
std::vector<double> CL_Sk; // 各子组标准差
|
|
|
|
SpcDataXS();
|
|
json ToJson() const;
|
|
static SpcDataXS FromJson(const json &j);
|
|
};
|
|
|
|
class SpcDataCpk
|
|
{
|
|
public:
|
|
int n; // 子组大小
|
|
int k; // 子组个数
|
|
double SL; // 特征值中值
|
|
double USL; // 规格上限
|
|
double LSL; // 规格下限
|
|
double Singma; // 组内过程标准差的估计值
|
|
double SingmaS; // 子组标准差的平均值
|
|
double Ca; // 过程准确度
|
|
double Cp; // 过程精密度
|
|
double CPU; // 能力指数上限
|
|
double CPL; // 能力指数下限
|
|
double CR; // 稳定过程的能力比值
|
|
double Cpk; // 过程能力指数
|
|
double Pp; // 性能指数
|
|
double PPU; // 性能指数上限
|
|
double PPL; // 性能指数下限
|
|
double PR; // 性能比率
|
|
double Ppk; // 性能指数
|
|
double ProcessSpread; // 全距
|
|
double GroupWidth; // 组距
|
|
int GroupCount; // 分组数
|
|
double ValueMax; // 最大值
|
|
double ValueMin; // 最小值
|
|
std::vector<double> Xk; // 组中点
|
|
std::vector<double> XkUp; // 组上界
|
|
std::vector<double> XkDown; // 组下界
|
|
std::vector<double> Yk; // 组频率百分比
|
|
std::vector<double> YkCount; // 组频数
|
|
std::vector<double> NormalDistributionX; // 正态分布X坐标
|
|
std::vector<double> NormalDistributionY; // 正态分布Y坐标
|
|
|
|
SpcDataCpk();
|
|
json ToJson() const;
|
|
static SpcDataCpk FromJson(const json &j);
|
|
};
|
|
|
|
// ==================== 统一的 SPC 计算结果 JSON 结构 ====================
|
|
class SpcResultJson
|
|
{
|
|
public:
|
|
SpcDataXR XR;
|
|
SpcDataXS XS;
|
|
SpcDataCpk Cpk;
|
|
std::string Timestamp;
|
|
std::string Version;
|
|
|
|
SpcResultJson();
|
|
json ToJson() const;
|
|
static SpcResultJson FromJson(const json &j);
|
|
};
|
|
|
|
// ==================== SPC 计算器 ====================
|
|
class SpcCalculator
|
|
{
|
|
private:
|
|
static void CalculateHistogramData(const std::vector<double> &data, SpcDataCpk &result);
|
|
static void CalculateNormalDistributionCurve(double mean, double sigma, SpcDataCpk &result);
|
|
|
|
public:
|
|
static json Spc(const json ¶m);
|
|
|
|
static SpcDataXR CalculateXR(const std::vector<double> &data, int subgroupSize);
|
|
static SpcDataXS CalculateXS(const std::vector<double> &data, int subgroupSize);
|
|
static SpcDataCpk CalculateCpk(const std::vector<double> &data, int subgroupSize, double usl, double lsl);
|
|
static json CalculateAllToJson(const std::vector<double> &data, int subgroupSize, double usl, double lsl);
|
|
|
|
static void RoundSpcData(SpcDataXR &xr, SpcDataXS &xs, SpcDataCpk &cpk, int decimals = DEFAULT_DECIMALS);
|
|
static void RoundSpcDataXR(SpcDataXR &data, int decimals = DEFAULT_DECIMALS);
|
|
static void RoundSpcDataXS(SpcDataXS &data, int decimals = DEFAULT_DECIMALS);
|
|
static void RoundSpcDataCpk(SpcDataCpk &data, int decimals = DEFAULT_DECIMALS);
|
|
|
|
static double CalculateCp(const std::vector<double> &data, int subgroupSize, double usl, double lsl);
|
|
static double CalculatePp(const std::vector<double> &data, int subgroupSize, double usl, double lsl);
|
|
static double CalculateCpu(const std::vector<double> &data, int subgroupSize, double usl);
|
|
static double CalculateCpl(const std::vector<double> &data, int subgroupSize, double lsl);
|
|
static double CalculateXbarbar(const std::vector<double> &data, int subgroupSize);
|
|
static double CalculateRbar(const std::vector<double> &data, int subgroupSize);
|
|
static double CalculateSbar(const std::vector<double> &data, int subgroupSize);
|
|
};
|
|
|
|
// ==================== 直方图计算器 ====================
|
|
class HistogramCalculator
|
|
{
|
|
public:
|
|
static void CalculateHistogram(const std::vector<double> &data, int numBins,
|
|
std::vector<int> &frequencies, std::vector<double> &cumulativePercentages);
|
|
static double CalculateBinWidth(const std::vector<double> &data, int numBins);
|
|
};
|
|
|
|
// ==================== API 响应和请求类 ====================
|
|
class SpcTestData
|
|
{
|
|
public:
|
|
int n; // 子组大小
|
|
int k; // 子组个数
|
|
double usl; // 特征值上限
|
|
double lsl; // 特征值下限
|
|
std::vector<double> x; // 测量数据数组
|
|
|
|
SpcTestData();
|
|
json ToJson() const;
|
|
static SpcTestData FromJson(const json &j);
|
|
};
|
|
|
|
class SpcRequestParam : public SpcTestData
|
|
{
|
|
// 可以添加额外的请求参数
|
|
};
|
|
|
|
// class ApiResponseSpc
|
|
// {
|
|
// public:
|
|
// bool success;
|
|
// int code;
|
|
// std::string msg;
|
|
// std::string req_code;
|
|
// std::string req_from;
|
|
// std::string req_cmd;
|
|
// std::shared_ptr<SpcRequestParam> req_param;
|
|
|
|
// ApiResponseSpc();
|
|
// json ToJson() const;
|
|
// static ApiResponseSpc FromJson(const json &j);
|
|
// };
|
|
|
|
// ==================== SPC 数据处理工具 ====================
|
|
|
|
// class SpcDataProcessor
|
|
// {
|
|
// public:
|
|
// static ApiResponseSpc DeserializeApiResponseSpc(const std::string &jsonStr);
|
|
// static SpcTestData ExtractSpcTestData(const ApiResponseSpc &response);
|
|
// static ApiResponseSpc ValidateResponse(const ApiResponseSpc &response); // 添加这行
|
|
// };
|
|
|
|
// ==================== 示例数据 ====================
|
|
namespace Spc_Data_TestData
|
|
{
|
|
extern const int n;
|
|
extern const int k;
|
|
extern const double USL;
|
|
extern const double LSL;
|
|
extern const std::vector<double> X;
|
|
}
|
|
|
|
// ==================== 示例函数 ====================
|
|
void RunJsonExample_5_30();
|
|
void RunSpcExample();
|
|
|
|
#endif // SPC_CORE_H
|