4.9 KiB
4.9 KiB
使用示例
本文档提供了使用大气传输计算库的 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 包装器使用方法
-
创建包装器项目(由库作者提供):
// 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); }; } -
在 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# 项目
- 在 Visual Studio 中创建新的 .NET 项目
- 通过 NuGet 包管理器添加 AirTransmission 包:
dotnet add package AirTransmission
C++ 项目
- 在 Visual Studio 中创建新的 C++ 项目
- 项目配置:
- 包含目录:添加 AirTransmissionWrapper.h 所在目录
- 库目录:添加 AirTransmissionWrapper.lib 所在目录
- 链接器输入:添加 AirTransmissionWrapper.lib
- 确保以下 DLL 在可执行文件的同一目录下:
- AirTransmissionWrapper.dll
- AirTransmission.dll
注意事项
- 所有浮点数输入都使用 double 类型
- 波长单位为微米(μm)
- 距离单位为米(m)
- 温度单位为摄氏度(℃)
- 压力单位为千帕(kPa)
- 相对湿度范围为 0-1
- 确保输入参数在合理范围内:
- 波长:0.2-20 μm
- 距离:> 0 m
- 温度:-40 到 +60 ℃
- 相对湿度:0-1
- 压力:80-120 kPa
- C++ 调用注意事项:
- 使用
^符号声明托管对象 - 使用
gcnew创建托管对象 - 包装器类中的所有对象都需要使用托管引用
- 使用