AirTransmissionLibrary/docs/usage_examples.md

5.0 KiB
Raw Blame History

使用示例

本文档提供了在 C# 和 C++ 项目中使用大气传输计算库的示例。

C# 调用示例

项目配置

  1. 在 Visual Studio 中创建新的 .NET 项目
  2. 将 AirTransmission.dll 复制到项目目录
  3. 在项目中添加 DLL 引用:
    • 右键点击项目 -> 添加 -> 引用
    • 浏览 -> 选择 AirTransmission.dll

代码示例

using AirTransmission;

// 创建天气条件
var weather = new WeatherCondition
{
    Temperature = 20,      // 温度(摄氏度)
    Humidity = 0.65,      // 相对湿度0-1
    Pressure = 101.325,   // 大气压力kPa
    Visibility = 10000,   // 能见度(米)
    WeatherType = WeatherType.Clear  // 天气类型
};

// 创建大气透过率计算器
var calculator = new AtmosphericTransmittanceCalculator();

// 计算激光透过率
double wavelength = 1.064;  // 波长(微米)
double distance = 1000;     // 传输距离(米)
double transmittance = calculator.CalculateTransmittance(wavelength, distance, weather);

Console.WriteLine($"激光透过率: {transmittance:P2}");

高级用法

// 计算不同天气条件下的透过率
var weatherConditions = new[]
{
    new WeatherCondition { WeatherType = WeatherType.Clear, Temperature = 20, Humidity = 0.65 },
    new WeatherCondition { WeatherType = WeatherType.Cloudy, Temperature = 18, Humidity = 0.8 },
    new WeatherCondition { WeatherType = WeatherType.Rain, Temperature = 15, Humidity = 0.95 }
};

var calculator = new AtmosphericTransmittanceCalculator();
double wavelength = 1.064;  // 微米
double distance = 1000;     // 米

foreach (var weather in weatherConditions)
{
    var transmittance = calculator.CalculateTransmittance(wavelength, distance, weather);
    Console.WriteLine($"天气: {weather.WeatherType}, 透过率: {transmittance:P2}");
}

// 计算大气湍流影响
var turbulence = calculator.CalculateAtmosphericTurbulence(wavelength, distance, weather);
Console.WriteLine($"大气湍流强度: {turbulence:E2}");

C++ 调用示例

项目配置

  1. 在 Visual Studio 中创建新的 C++ 项目
  2. 将 AirTransmission.dll 复制到项目的输出目录(或可执行文件所在目录)
  3. 在代码中使用 LoadLibrary 和 GetProcAddress 动态加载 DLL 函数

代码示例

#include <windows.h>
#include <iostream>

// 定义函数指针类型
typedef double (__cdecl *CalculateTransmittanceFunc)(
    double wavelength, 
    double distance,
    double temperature,
    double humidity,
    double pressure,
    double visibility,
    int weatherType
);

typedef double (__cdecl *CalculateAtmosphericTurbulenceFunc)(
    double wavelength, 
    double distance,
    double temperature,
    double humidity,
    double pressure,
    double visibility,
    int weatherType
);

int main() {
    // 加载 DLL
    HMODULE hDll = LoadLibrary(L"AirTransmission.dll");
    if (hDll == NULL) {
        std::cout << "无法加载 DLL" << std::endl;
        return 1;
    }

    // 获取函数地址
    auto calculateTransmittance = (CalculateTransmittanceFunc)GetProcAddress(hDll, "CalculateTransmittance");
    auto calculateTurbulence = (CalculateAtmosphericTurbulenceFunc)GetProcAddress(hDll, "CalculateAtmosphericTurbulence");
    
    if (calculateTransmittance == NULL || calculateTurbulence == NULL) {
        std::cout << "无法获取函数地址" << std::endl;
        FreeLibrary(hDll);
        return 1;
    }

    // 设置参数
    double wavelength = 1.064;   // 波长(微米)
    double distance = 1000.0;    // 距离(米)
    double temperature = 20.0;   // 温度(摄氏度)
    double humidity = 0.65;      // 相对湿度0-1
    double pressure = 101.325;   // 大气压力kPa
    double visibility = 10000.0; // 能见度(米)
    int weatherType = 0;         // 天气类型0=晴天)

    // 计算透过率
    double transmittance = calculateTransmittance(
        wavelength, distance, temperature, humidity, 
        pressure, visibility, weatherType
    );
    std::cout << "透过率: " << transmittance * 100.0 << "%" << std::endl;

    // 计算湍流影响
    double turbulence = calculateTurbulence(
        wavelength, distance, temperature, humidity, 
        pressure, visibility, weatherType
    );
    std::cout << "湍流强度: " << turbulence << std::endl;

    // 卸载 DLL
    FreeLibrary(hDll);
    return 0;
}

注意事项

  1. 所有浮点数输入都使用 double 类型
  2. 波长单位为微米μm
  3. 距离单位为米m
  4. 温度单位为摄氏度(℃)
  5. 压力单位为千帕kPa
  6. 相对湿度范围为 0-1
  7. 确保输入参数在合理范围内:
    • 波长0.2-20 μm
    • 距离:> 0 m
    • 温度:-40 到 +60 ℃
    • 相对湿度0-1
    • 压力80-120 kPa
  8. C++ 调用注意事项:
    • 确保 DLL 文件在正确的路径下
    • 所有导出函数使用 __cdecl 调用约定
    • 处理好错误情况DLL 加载失败、函数获取失败等)
    • 天气类型枚举值:
      • 0 = 晴天 (Clear)
      • 1 = 多云 (Cloudy)
      • 2 = 雨天 (Rain)