import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from sklearn.metrics import r2_score
import warnings
import math
warnings.filterwarnings('ignore')

# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False

# 度数到弧度的转换常数
DEG_TO_RAD = math.pi / 180.0

# 读取CSV文件
csv_path = 'csv/各关节质量与运动角度相关数据表格.csv'
df = pd.read_csv(csv_path, encoding='utf-8')

# 提取数据（度数）
mcp_angles_deg = df.iloc[:, 0].values
finger_names = ['食指', '中指', '无名指', '尾指']
pip_angles_deg = {}
for i, name in enumerate(finger_names):
    pip_angles_deg[name] = df.iloc[:, i+1].values

print("="*60)
print("主动关节(MCP)与被动关节(PIP)函数关系拟合分析")
print("="*60)
print(f"\n数据点数量: {len(mcp_angles_deg)}")
print(f"MCP角度范围: {mcp_angles_deg.min():.2f}° - {mcp_angles_deg.max():.2f}°\n")

# 检查初始值
print("初始值检查（MCP=0°时的PIP值）:")
for name in finger_names:
    pip_at_zero = pip_angles_deg[name][0]
    print(f"  {name}: PIP = {pip_at_zero:.2f}° (当MCP=0°时)")
print()

# 转换为弧度进行拟合（MuJoCo使用弧度）
mcp_angles_rad = mcp_angles_deg * DEG_TO_RAD
pip_angles_rad = {}
for name in finger_names:
    pip_angles_rad[name] = pip_angles_deg[name] * DEG_TO_RAD

# 定义拟合函数（使用弧度）
def linear_func(x, a, b):
    """线性函数: y = a*x + b"""
    return a * x + b

def quadratic_func(x, a, b, c):
    """二次函数: y = a*x^2 + b*x + c"""
    return a * x**2 + b * x + c

# 存储拟合结果
fitting_results = {}

# 对每个手指进行拟合（直接使用弧度数据）
for finger_name in finger_names:
    print(f"\n{'='*60}")
    print(f"分析: {finger_name}")
    print(f"{'='*60}")
    
    y_data_rad = pip_angles_rad[finger_name]
    x_data_rad = mcp_angles_rad
    
    # 尝试不同的拟合方法
    fits = {}
    
    # 1. 线性拟合（弧度）
    try:
        popt_linear, _ = curve_fit(linear_func, x_data_rad, y_data_rad)
        y_pred_linear = linear_func(x_data_rad, *popt_linear)
        r2_linear = r2_score(y_data_rad, y_pred_linear)
        mae_linear = np.mean(np.abs(y_data_rad - y_pred_linear))
        
        fits['线性'] = {
            'params': popt_linear,
            'r2': r2_linear,
            'mae': mae_linear,
            'func': linear_func,
            'formula_rad': f"PIP(rad) = {popt_linear[0]:.6f} * MCP(rad) + {popt_linear[1]:.6f}"
        }
        print(f"线性拟合: {fits['线性']['formula_rad']}")
        print(f"  R² = {r2_linear:.6f}, MAE = {mae_linear:.6f} rad")
    except:
        pass
    
    # 2. 二次拟合（弧度）
    try:
        popt_quad, _ = curve_fit(quadratic_func, x_data_rad, y_data_rad)
        y_pred_quad = quadratic_func(x_data_rad, *popt_quad)
        r2_quad = r2_score(y_data_rad, y_pred_quad)
        mae_quad = np.mean(np.abs(y_data_rad - y_pred_quad))
        
        fits['二次'] = {
            'params': popt_quad,
            'r2': r2_quad,
            'mae': mae_quad,
            'func': quadratic_func,
            'formula_rad': f"PIP(rad) = {popt_quad[0]:.6f} * MCP(rad)² + {popt_quad[1]:.6f} * MCP(rad) + {popt_quad[2]:.6f}"
        }
        print(f"二次拟合: {fits['二次']['formula_rad']}")
        print(f"  R² = {r2_quad:.6f}, MAE = {mae_quad:.6f} rad")
    except:
        pass
    
    # 选择最佳拟合（优先线性，除非二次明显更好）
    if fits:
        best_fit = None
        best_name = None
        
        if '线性' in fits:
            best_fit = fits['线性']
            best_name = '线性'
        
        if '二次' in fits:
            # 如果二次拟合的R²提升超过0.0001，且MAE更小，使用二次
            if best_fit is None or (fits['二次']['r2'] > best_fit['r2'] + 0.0001 and 
                                   fits['二次']['mae'] < best_fit['mae']):
                best_fit = fits['二次']
                best_name = '二次'
        
        if best_fit:
            fitting_results[finger_name] = {
                'best': best_name,
                'best_fit': best_fit
            }
            print(f"\n最佳拟合: {best_name} (R² = {best_fit['r2']:.6f}, MAE = {best_fit['mae']:.6f} rad)")

# 可视化结果
fig, axes = plt.subplots(2, 2, figsize=(15, 12))
axes = axes.flatten()

for idx, finger_name in enumerate(finger_names):
    ax = axes[idx]
    
    # 绘制原始数据点（度数显示）
    ax.scatter(mcp_angles_deg, pip_angles_deg[finger_name], 
              alpha=0.6, s=30, label='原始数据', color='blue')
    
    # 绘制拟合曲线（转换为度数显示）
    if finger_name in fitting_results:
        result = fitting_results[finger_name]
        best_fit = result['best_fit']
        
        # 生成平滑曲线（使用弧度计算，转换为度数显示）
        x_smooth_rad = np.linspace(mcp_angles_rad.min(), mcp_angles_rad.max(), 200)
        y_smooth_rad = best_fit['func'](x_smooth_rad, *best_fit['params'])
        x_smooth_deg = x_smooth_rad / DEG_TO_RAD
        y_smooth_deg = y_smooth_rad / DEG_TO_RAD
        ax.plot(x_smooth_deg, y_smooth_deg, 'r-', linewidth=2, 
               label=f"{result['best']}拟合 (R²={best_fit['r2']:.4f})")
    
    ax.set_xlabel('MCP角度 (°)', fontsize=12)
    ax.set_ylabel('PIP角度 (°)', fontsize=12)
    ax.set_title(f'{finger_name} - MCP vs PIP', fontsize=14, fontweight='bold')
    ax.legend(fontsize=9)
    ax.grid(True, alpha=0.3)

plt.tight_layout()
plt.savefig('scripts/fitting_results.png', dpi=300, bbox_inches='tight')
print(f"\n\n可视化结果已保存到: scripts/fitting_results.png")

# 生成用于MuJoCo XML的polycoef格式
print("\n" + "="*60)
print("MuJoCo XML格式的polycoef系数")
print("="*60)
print("MuJoCo公式: PIP - PIP₀ = a₀ + a₁(MCP - MCP₀) + a₂(MCP - MCP₀)² + a₃(MCP - MCP₀)³ + a₄(MCP - MCP₀)⁴")
print("假设初始配置: MCP₀ = 0, PIP₀ = 0 (弧度)")
print("因此: PIP = a₀ + a₁·MCP + a₂·MCP² + a₃·MCP³ + a₄·MCP⁴")
print("polycoef格式: [a₀, a₁, a₂, a₃, a₄] (5个系数，弧度)")
print("\n注意: 拟合直接在弧度空间进行，系数可直接使用\n")

for finger_name in finger_names:
    if finger_name in fitting_results:
        result = fitting_results[finger_name]
        best_fit = result['best_fit']
        params_rad = best_fit['params']  # 已经是弧度空间的参数
        
        # 根据拟合类型生成polycoef（5个系数）
        if result['best'] == '线性':
            # 线性: PIP(rad) = a*MCP(rad) + b
            # polycoef = [b, a, 0, 0, 0]
            polycoef = [params_rad[1], params_rad[0], 0.0, 0.0, 0.0]
            
        elif result['best'] == '二次':
            # 二次: PIP(rad) = a*MCP(rad)² + b*MCP(rad) + c
            # polycoef = [c, b, a, 0, 0]
            polycoef = [params_rad[2], params_rad[1], params_rad[0], 0.0, 0.0]
        else:
            polycoef = [0.0, 1.0, 0.0, 0.0, 0.0]
        
        # 格式化输出
        polycoef_str = ' '.join([f"{coef:.10f}" for coef in polycoef])
        print(f"\n{finger_name}:")
        print(f"  拟合方法: {result['best']}")
        print(f"  弧度公式: {best_fit['formula_rad']}")
        print(f"  polycoef=\"{polycoef_str}\"")
        print(f"  验证: 当MCP=0时, PIP = {polycoef[0]:.6f} rad = {polycoef[0]/DEG_TO_RAD:.2f}°")

# 输出XML格式的代码片段
print("\n" + "="*60)
print("XML代码片段（可直接复制）")
print("="*60)
print("<equality>")
print("  <!--  mimic distal -> proximal  -->")
print("  <!-- 基于实际数据拟合的函数关系（弧度）: PIP = a₀ + a₁*MCP + a₂*MCP² + ... -->")
print("  <!-- 注意: a₀是MCP=0时PIP的初始值（弧度） -->")

for finger_name in finger_names:
    if finger_name in fitting_results:
        result = fitting_results[finger_name]
        best_fit = result['best_fit']
        params_rad = best_fit['params']
        
        # 生成polycoef（已经是弧度）
        if result['best'] == '线性':
            polycoef = [params_rad[1], params_rad[0], 0.0, 0.0, 0.0]
        elif result['best'] == '二次':
            polycoef = [params_rad[2], params_rad[1], params_rad[0], 0.0, 0.0]
        else:
            polycoef = [0.0, 1.0, 0.0, 0.0, 0.0]
        
        polycoef_str = ' '.join([f"{coef:.10f}" for coef in polycoef])
        
        # 映射手指名称到关节名称
        joint_map = {
            '食指': 'index',
            '中指': 'middle',
            '无名指': 'ring',
            '尾指': 'little'
        }
        joint_name = joint_map.get(finger_name, finger_name)
        print(f"  <joint joint1=\"{joint_name}MCP\" joint2=\"{joint_name}PIP\" polycoef=\"{polycoef_str}\"/>")
print("</equality>")

print("\n分析完成！")