diff --git a/AirTransmission-API-Doc.pdf b/AirTransmission-API-Doc.pdf deleted file mode 100644 index dba961d..0000000 Binary files a/AirTransmission-API-Doc.pdf and /dev/null differ diff --git a/docs/usage_examples.md b/docs/usage_examples.md index 8a41a32..40f8535 100644 --- a/docs/usage_examples.md +++ b/docs/usage_examples.md @@ -1,10 +1,18 @@ # 使用示例 -本文档提供了使用大气传输计算库的 C# 和 C++ 示例代码。 +本文档提供了在 C# 和 C++ 项目中使用大气传输计算库的示例。 -## C# 示例 +## C# 调用示例 -### 基本使用 +### 项目配置 + +1. 在 Visual Studio 中创建新的 .NET 项目 +2. 将 AirTransmission.dll 复制到项目目录 +3. 在项目中添加 DLL 引用: + - 右键点击项目 -> 添加 -> 引用 + - 浏览 -> 选择 AirTransmission.dll + +### 代码示例 ```csharp using AirTransmission; @@ -56,94 +64,87 @@ var turbulence = calculator.CalculateAtmosphericTurbulence(wavelength, distance, Console.WriteLine($"大气湍流强度: {turbulence:E2}"); ``` -## C++ 调用说明 +## C++ 调用示例 -由于 AirTransmission 是 .NET 动态库,C++ 程序需要通过 C++/CLI 包装器来调用。 - -### C++/CLI 包装器使用方法 - -1. 创建包装器项目(由库作者提供): - - ```cpp - // 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++ 代码中使用包装器: - - ```cpp - #include - #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 包: - -```bash -dotnet add package AirTransmission -``` - -### C++ 项目 +### 项目配置 1. 在 Visual Studio 中创建新的 C++ 项目 -2. 项目配置: - - 包含目录:添加 AirTransmissionWrapper.h 所在目录 - - 库目录:添加 AirTransmissionWrapper.lib 所在目录 - - 链接器输入:添加 AirTransmissionWrapper.lib -3. 确保以下 DLL 在可执行文件的同一目录下: - - AirTransmissionWrapper.dll - - AirTransmission.dll +2. 将 AirTransmission.dll 复制到项目的输出目录(或可执行文件所在目录) +3. 在代码中使用 LoadLibrary 和 GetProcAddress 动态加载 DLL 函数 + +### 代码示例 + +```cpp +#include +#include + +// 定义函数指针类型 +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; +} +``` ## 注意事项 @@ -160,6 +161,10 @@ dotnet add package AirTransmission - 相对湿度:0-1 - 压力:80-120 kPa 8. C++ 调用注意事项: - - 使用 `^` 符号声明托管对象 - - 使用 `gcnew` 创建托管对象 - - 包装器类中的所有对象都需要使用托管引用 + - 确保 DLL 文件在正确的路径下 + - 所有导出函数使用 __cdecl 调用约定 + - 处理好错误情况(DLL 加载失败、函数获取失败等) + - 天气类型枚举值: + - 0 = 晴天 (Clear) + - 1 = 多云 (Cloudy) + - 2 = 雨天 (Rain) diff --git a/release/AirTransmission-API-Doc.pdf b/release/AirTransmission-API-Doc.pdf new file mode 100644 index 0000000..79d101b Binary files /dev/null and b/release/AirTransmission-API-Doc.pdf differ diff --git a/release/AirTransmission-Library.zip b/release/AirTransmission-Library.zip new file mode 100644 index 0000000..f86e223 Binary files /dev/null and b/release/AirTransmission-Library.zip differ diff --git a/src/AirTransmission/AirTransmission.csproj b/src/AirTransmission/AirTransmission.csproj index 0956b39..5ae2303 100644 --- a/src/AirTransmission/AirTransmission.csproj +++ b/src/AirTransmission/AirTransmission.csproj @@ -22,4 +22,9 @@ false + + + + + \ No newline at end of file diff --git a/src/AirTransmission/AirTransmission.xml b/src/AirTransmission/AirTransmission.xml index b5ab102..04dc784 100644 --- a/src/AirTransmission/AirTransmission.xml +++ b/src/AirTransmission/AirTransmission.xml @@ -100,6 +100,16 @@ 烟幕厚度(米) 烟幕透过率(0到1之间的值) + + + 导出函数:计算大气透过率 + + + + + 导出函数:计算大气湍流影响 + + 大气湍流模型类,用于计算大气湍流对光传输的影响 diff --git a/src/AirTransmission/AtmosphericTransmittanceCalculator.cs b/src/AirTransmission/AtmosphericTransmittanceCalculator.cs index 3da905d..bebd4b7 100644 --- a/src/AirTransmission/AtmosphericTransmittanceCalculator.cs +++ b/src/AirTransmission/AtmosphericTransmittanceCalculator.cs @@ -4,6 +4,10 @@ * 1.1.0 (2024-10-18): 改进版本,考虑目标位置关系和大气参数分布。作者:田建勇 */ +using System; +using System.Runtime.InteropServices; +using RGiesecke.DllExport; + namespace AirTransmission { /// @@ -189,5 +193,51 @@ namespace AirTransmission return transmittance; } + + /// + /// 导出函数:计算大气透过率 + /// + [DllExport("CalculateTransmittance", CallingConvention = CallingConvention.Cdecl)] + public static double CalculateTransmittanceExport( + double wavelength, + double distance, + double temperature, + double relativeHumidity, + double pressure, + double visibility, + int weatherType) + { + var weather = new WeatherCondition( + type: (WeatherType)weatherType, + temperature: temperature, + relativeHumidity: relativeHumidity, + visibility: visibility + ); + + return CalcLaser(weather, distance); + } + + /// + /// 导出函数:计算大气湍流影响 + /// + [DllExport("CalculateAtmosphericTurbulence", CallingConvention = CallingConvention.Cdecl)] + public static double CalculateAtmosphericTurbulenceExport( + double wavelength, + double distance, + double temperature, + double relativeHumidity, + double pressure, + double visibility, + int weatherType) + { + var weather = new WeatherCondition( + type: (WeatherType)weatherType, + temperature: temperature, + relativeHumidity: relativeHumidity, + visibility: visibility + ); + + return CalcTurbulenceEffect(weather, distance); + } } } diff --git a/test/cpp/test.cpp b/test/cpp/test.cpp new file mode 100644 index 0000000..6feb767 --- /dev/null +++ b/test/cpp/test.cpp @@ -0,0 +1,69 @@ +#include +#include + +// 定义函数指针类型 +typedef double (__cdecl *CalculateTransmittanceFunc)( + double wavelength, + double distance, + double temperature, + double relativeHumidity, + double pressure, + double visibility, + int weatherType +); + +typedef double (__cdecl *CalculateAtmosphericTurbulenceFunc)( + double wavelength, + double distance, + double temperature, + double relativeHumidity, + 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 relativeHumidity = 0.65; // 相对湿度(0-1) + double pressure = 101.325; // 大气压力(kPa) + double visibility = 10000.0; // 能见度(米) + int weatherType = 0; // 天气类型(0=晴天) + + // 计算透过率 + double transmittance = calculateTransmittance( + wavelength, distance, temperature, relativeHumidity, + pressure, visibility, weatherType + ); + std::cout << "透过率: " << transmittance * 100.0 << "%" << std::endl; + + // 计算湍流影响 + double turbulence = calculateTurbulence( + wavelength, distance, temperature, relativeHumidity, + pressure, visibility, weatherType + ); + std::cout << "湍流强度: " << turbulence << std::endl; + + // 卸载 DLL + FreeLibrary(hDll); + return 0; +} \ No newline at end of file