AirTransmissionLibrary/docs/usage_examples.md

4.9 KiB
Raw Blame History

使用示例

本文档提供了使用大气传输计算库的 C# 和 C++ 示例代码。

C# 示例

基本使用

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++ 调用说明

由于 AirTransmission 是 .NET 动态库C++ 程序需要通过 C++/CLI 包装器来调用。

C++/CLI 包装器使用方法

  1. 创建包装器项目(由库作者提供):

    // AirTransmissionWrapper.h
    #pragma once
    
    namespace AirTransmissionWrapper {
        public enum class WeatherType {
            Clear = 0,
            Cloudy = 1,
            Rain = 2
        };
    
        public ref class WeatherCondition {
        public:
            property double Temperature;
            property double Humidity;
            property double Pressure;
            property double Visibility;
            property WeatherType Type;
        };
    
        public ref class Calculator {
        public:
            Calculator();
            double CalculateTransmittance(double wavelength, double distance, WeatherCondition^ weather);
            double CalculateAtmosphericTurbulence(double wavelength, double distance, WeatherCondition^ weather);
        };
    }
    
  2. 在 C++ 代码中使用包装器:

    #include <iostream>
    #include "AirTransmissionWrapper.h"
    
    int main() {
        using namespace AirTransmissionWrapper;
    
        // 创建天气条件
        WeatherCondition^ weather = gcnew WeatherCondition();
        weather->Temperature = 20.0;    // 温度(摄氏度)
        weather->Humidity = 0.65;       // 相对湿度0-1
        weather->Pressure = 101.325;    // 大气压力kPa
        weather->Visibility = 10000.0;  // 能见度(米)
        weather->Type = WeatherType::Clear;
    
        // 创建计算器
        Calculator^ calculator = gcnew Calculator();
    
        // 计算透过率
        double wavelength = 1.064;  // 波长(微米)
        double distance = 1000.0;   // 传输距离(米)
        double transmittance = calculator->CalculateTransmittance(wavelength, distance, weather);
    
        std::cout << "透过率: " << transmittance * 100.0 << "%" << std::endl;
        return 0;
    }
    

项目配置

C# 项目

  1. 在 Visual Studio 中创建新的 .NET 项目
  2. 通过 NuGet 包管理器添加 AirTransmission 包:
dotnet add package AirTransmission

C++ 项目

  1. 在 Visual Studio 中创建新的 C++ 项目
  2. 项目配置:
    • 包含目录:添加 AirTransmissionWrapper.h 所在目录
    • 库目录:添加 AirTransmissionWrapper.lib 所在目录
    • 链接器输入:添加 AirTransmissionWrapper.lib
  3. 确保以下 DLL 在可执行文件的同一目录下:
    • AirTransmissionWrapper.dll
    • AirTransmission.dll

注意事项

  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++ 调用注意事项:
    • 使用 ^ 符号声明托管对象
    • 使用 gcnew 创建托管对象
    • 包装器类中的所有对象都需要使用托管引用