128 lines
4.7 KiB
C++
128 lines
4.7 KiB
C++
/**
|
||
* 注意:这是一个C++/CLI示例,展示如何在C++项目中使用ThreatSource库
|
||
* 实际项目中需要:
|
||
* 1. 创建C++/CLI项目
|
||
* 2. 引用ThreatSource.dll
|
||
* 3. 配置正确的目标框架
|
||
*/
|
||
|
||
using namespace System;
|
||
using namespace ThreatSource::Simulation;
|
||
using namespace ThreatSource::Guidance;
|
||
using namespace ThreatSource::Sensor;
|
||
|
||
namespace ThreatSourceWrapper {
|
||
|
||
// 包装类,用于在C++中使用仿真系统
|
||
public ref class IRMissileSimulationWrapper
|
||
{
|
||
private:
|
||
TestSimulationAdapter^ adapter;
|
||
SimulationEntity^ missile;
|
||
SimulationEntity^ target;
|
||
InfraredImagingGuidanceSystem^ guidanceSystem;
|
||
|
||
public:
|
||
IRMissileSimulationWrapper()
|
||
{
|
||
adapter = gcnew TestSimulationAdapter();
|
||
}
|
||
|
||
void RunSimulationExample()
|
||
{
|
||
try {
|
||
// 步骤1:创建导弹实体
|
||
missile = gcnew SimulationEntity();
|
||
missile->Id = "missile_001";
|
||
missile->Position = Vector3(0, 0, 0);
|
||
missile->Velocity = Vector3(0, 0, 0);
|
||
missile->Mass = 100.0f; // kg
|
||
|
||
// 步骤2:配置红外成像制导系统
|
||
guidanceSystem = gcnew InfraredImagingGuidanceSystem();
|
||
guidanceSystem->FieldOfView = 60.0f; // 视场角(度)
|
||
guidanceSystem->TrackingRange = 20000.0f; // 最大跟踪距离(米)
|
||
guidanceSystem->UpdateRate = 50; // 制导更新频率(Hz)
|
||
|
||
missile->GuidanceSystem = guidanceSystem;
|
||
|
||
// 步骤3:设置目标
|
||
target = gcnew SimulationEntity();
|
||
target->Id = "target_001";
|
||
target->Position = Vector3(10000.0f, 1000.0f, 1000.0f); // 目标在10km外
|
||
target->Velocity = Vector3(-100.0f, 0.0f, 0.0f); // 目标以100m/s速度移动
|
||
|
||
InfraredSignature^ signature = gcnew InfraredSignature();
|
||
signature->Temperature = 400.0f; // 目标温度(开尔文)
|
||
signature->EmissivityFactor = 0.8f;
|
||
target->Signature = signature;
|
||
|
||
// 步骤4:配置仿真参数
|
||
SimulationConfig^ config = gcnew SimulationConfig();
|
||
config->TimeStep = 0.02f; // 仿真步长(秒)
|
||
config->MaxSimulationTime = 60.0f; // 最大仿真时间(秒)
|
||
|
||
EnvironmentConditions^ env = gcnew EnvironmentConditions();
|
||
env->Temperature = 288.15f; // 环境温度(开尔文)
|
||
env->Pressure = 101325.0f; // 大气压力(帕)
|
||
env->Humidity = 0.5f; // 相对湿度
|
||
config->EnvironmentConditions = env;
|
||
|
||
// 步骤5:注册事件处理
|
||
adapter->EntityUpdated += gcnew EntityUpdateHandler(this, &IRMissileSimulationWrapper::OnEntityUpdated);
|
||
adapter->GuidanceUpdate += gcnew GuidanceUpdateHandler(this, &IRMissileSimulationWrapper::OnGuidanceUpdate);
|
||
|
||
// 步骤6:初始化仿真
|
||
adapter->Initialize(config);
|
||
adapter->AddEntity(missile);
|
||
adapter->AddEntity(target);
|
||
|
||
// 步骤7:启动仿真
|
||
adapter->StartSimulation();
|
||
|
||
// 步骤8:等待仿真结束
|
||
while (adapter->IsRunning)
|
||
{
|
||
System::Threading::Thread::Sleep(100);
|
||
}
|
||
|
||
// 步骤9:获取仿真结果
|
||
SimulationResults^ results = adapter->GetSimulationResults();
|
||
Console::WriteLine(String::Format("仿真结束,命中精度: {0:F2}米", results->MissDistance));
|
||
|
||
}
|
||
catch (Exception^ e) {
|
||
Console::WriteLine(String::Format("仿真过程出错: {0}", e->Message));
|
||
}
|
||
}
|
||
|
||
private:
|
||
void OnEntityUpdated(SimulationEntity^ entity)
|
||
{
|
||
Console::WriteLine(String::Format("实体 {0} 位置更新: ({1:F2}, {2:F2}, {3:F2})",
|
||
entity->Id,
|
||
entity->Position.X,
|
||
entity->Position.Y,
|
||
entity->Position.Z));
|
||
}
|
||
|
||
void OnGuidanceUpdate(GuidanceInfo^ info)
|
||
{
|
||
Console::WriteLine(String::Format("制导更新: 距离目标 {0:F2}米", info->DistanceToTarget));
|
||
}
|
||
};
|
||
}
|
||
|
||
// 示例:如何在原生C++代码中使用包装类
|
||
int main()
|
||
{
|
||
try {
|
||
auto simulation = gcnew ThreatSourceWrapper::IRMissileSimulationWrapper();
|
||
simulation->RunSimulationExample();
|
||
return 0;
|
||
}
|
||
catch (Exception^ e) {
|
||
Console::WriteLine(String::Format("错误: {0}", e->Message));
|
||
return 1;
|
||
}
|
||
}
|