MyTest.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /////////////////////////////////////////////////
  2. //
  3. // 使用RecastNavigation库进行寻路的测试程序
  4. // Written by: Liu Gang. July.11.2020.
  5. //
  6. /////////////////////////////////////////////////
  7. #include <iostream>
  8. #include <ostream>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <string>
  12. #include "tools.h"
  13. #include "../RecastNavDll/RecastDll.h"
  14. std::string _strLastError;
  15. int DoRecast(std::string mapPathName, std::string posFrom, std::string posTo);
  16. int main(int argc, char* argv[])
  17. {
  18. std::cout << "共有" << argc << "个参数" << std::endl;
  19. for (int i = 0; i < argc; i++)
  20. {
  21. std::cout << argv[i] << std::endl;
  22. }
  23. if (argc == 2 && _stricmp(argv[1], "/?") == 0)
  24. {
  25. printf_s("Recast工具 参数说明: ");
  26. printf_s(" Recast [寻路文件名, 含路径] [(起点坐标:) x,y] [(终点坐标:) x,y]");
  27. printf_s(" 举例:");
  28. printf_s(" Recast solo_navmesh.bin 33.07,13.46 1103.89,478.21");
  29. return 0;
  30. }
  31. if (argc == 5) //第一个参数是可执行文件自身,第二个参数是Recast标志,第三个参数是文件名,第四个参数是起点,第五个参数是终点
  32. {
  33. int ret = DoRecast(argv[2], argv[3], argv[4]);
  34. if (ret == 0)
  35. {
  36. return 0;
  37. }
  38. else
  39. {
  40. std::string strRet = std::to_string(ret);
  41. std::string error = "Recast工具 错误(" + strRet + ") - " + _strLastError;
  42. printf_s(error.c_str());
  43. return ret;
  44. }
  45. }
  46. printf_s("Recast工具 参数错误(-1)!请使用 /? 查看参数说明。");
  47. return -1;
  48. }
  49. int Find2(int id, std::string mapPathName, const std::string posFrom, const std::string posTo);
  50. int DoRecast(const std::string mapPathName, const std::string posFrom, const std::string posTo)
  51. {
  52. // 1,初始化
  53. if (!recast_init())
  54. {
  55. _strLastError = "Recast 初始化失败!";
  56. return -11;
  57. }
  58. // 2,加载地图101
  59. int id1 = 101;
  60. if (!recast_loadmap(id1, mapPathName.c_str()))
  61. {
  62. _strLastError = "地图创建失败!- " + mapPathName;
  63. return -12;
  64. }
  65. // 2,加载地图102
  66. int id2 = 102;
  67. if (!recast_loadmap(id2, mapPathName.c_str()))
  68. {
  69. _strLastError = "地图创建失败!- " + mapPathName;
  70. return -12;
  71. }
  72. // 3,地图101开始寻路
  73. printf_s("寻路开始...\n");
  74. int ret = Find2(id1, mapPathName, posFrom, posTo);
  75. if (ret < 0)
  76. {
  77. printf_s(_strLastError.c_str());
  78. }
  79. printf_s("...寻路结束 - ret:%d\n\n", ret);
  80. // 3,地图102开始寻路
  81. printf_s("寻路开始...\n");
  82. ret = Find2(id2, mapPathName, posFrom, posTo);
  83. if (ret < 0)
  84. {
  85. printf_s(_strLastError.c_str());
  86. }
  87. printf_s("...寻路结束 - ret:%d\n\n", ret);
  88. // 4,释放地图
  89. recast_freemap(id1);
  90. recast_freemap(id2);
  91. // 5,释放Recast引擎
  92. recast_fini();
  93. printf_s("Recast 寻路成功!");
  94. return 0;
  95. }
  96. int Find2(int id, std::string mapPathName, const std::string posFrom, const std::string posTo)
  97. {
  98. Tools::dtStatus status;
  99. std::vector<std::string> strsFrom = Tools::split(posFrom, ",");
  100. std::vector<std::string> strsTo = Tools::split(posTo, ",");
  101. //注意在Unity中因为坐标系原因,x需要为相反数
  102. float spos[3] = {stof(strsFrom[0]), stof(strsFrom[1]), stof(strsFrom[2])};
  103. float epos[3] = {stof(strsTo[0]), stof(strsTo[1]), stof(strsTo[2])};
  104. // 1,寻路
  105. status = recast_findpath(id, spos, epos);
  106. if (Tools::dtStatusFailed(status))
  107. {
  108. int statusDetail = status & Tools::DT_STATUS_DETAIL_MASK;
  109. std::string strDetail = std::to_string(statusDetail);
  110. _strLastError = "寻路失败!错误码<" + strDetail + ">";
  111. if (statusDetail == Tools::DT_COORD_INVALID)
  112. {
  113. char szFrom[256], szTo[256];
  114. sprintf_s(szFrom, sizeof(szFrom), "%f, %f, %f", spos[0], spos[1], spos[2]);
  115. sprintf_s(szTo, sizeof(szTo), "%f, %f, %f", epos[0], epos[1], epos[2]);
  116. std::string strFrom = szFrom, strTo = szTo;
  117. _strLastError += " - 坐标非法!From<" + strFrom + "> To<" + strTo + ">";
  118. }
  119. return -13;
  120. }
  121. else if (Tools::dtStatusInProgress(status))
  122. {
  123. return -14;
  124. }
  125. float* fixPos = new float[3];
  126. memcpy(fixPos, recast_getfixposition(id, spos), sizeof(float) * 3);
  127. float* fixPos2 = new float[3];
  128. memcpy(fixPos2, recast_getfixposition(id, epos), sizeof(float) * 3);
  129. // 2,得到实际(平滑)路径
  130. recast_smooth(id, 2, 0.5);
  131. // 寻路成功!
  132. // 3,得到凸多边形id序列
  133. std::string format = " 起点<%f,%f,%f> 终点<%f,%f,%f>\n";
  134. printf_s(format.c_str(), spos[0], spos[1], spos[2], epos[0], epos[1], epos[2]);
  135. int m_npolys = recast_getcountpoly(id);
  136. unsigned int* m_polys = recast_getpathpoly(id);
  137. std::string outputPoly = " 路线地块序号 (" + std::to_string(m_npolys) + "): \n ";
  138. for (int i = 0; i < m_npolys; ++i)
  139. {
  140. std::string strPoly = std::to_string((m_polys[i]));
  141. outputPoly += strPoly + ", ";
  142. }
  143. outputPoly += "\n";
  144. printf_s(outputPoly.c_str());
  145. // 4,得到寻路路径的坐标序列
  146. int m_nsmoothPath = recast_getcountsmooth(id);
  147. float* m_smoothPath = recast_getpathsmooth(id);
  148. std::string outputSmooth = " 路线 (" + std::to_string(m_nsmoothPath) + "): \n";
  149. for (int i = 0; i < m_nsmoothPath; ++i)
  150. {
  151. std::string strSmooth = std::to_string(m_smoothPath[i * 3]) + "," + std::to_string(m_smoothPath[i * 3 + 1]) +
  152. "," + std::to_string(m_smoothPath[i * 3 + 2]);
  153. outputSmooth += " <" + strSmooth + "> \n";
  154. }
  155. printf_s(outputSmooth.c_str());
  156. return 1;
  157. }