PolyUtils.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source distribution.
  16. */
  17. namespace DotRecast.Detour.Extras
  18. {
  19. public static class PolyUtils
  20. {
  21. /**
  22. * Find edge shared by 2 polygons within the same tile
  23. */
  24. public static int FindEdge(DtPoly node, DtPoly neighbour, DtMeshData tile, DtMeshData neighbourTile)
  25. {
  26. // Compare indices first assuming there are no duplicate vertices
  27. for (int i = 0; i < node.vertCount; i++)
  28. {
  29. int j = (i + 1) % node.vertCount;
  30. for (int k = 0; k < neighbour.vertCount; k++)
  31. {
  32. int l = (k + 1) % neighbour.vertCount;
  33. if ((node.verts[i] == neighbour.verts[l] && node.verts[j] == neighbour.verts[k])
  34. || (node.verts[i] == neighbour.verts[k] && node.verts[j] == neighbour.verts[l]))
  35. {
  36. return i;
  37. }
  38. }
  39. }
  40. // Fall back to comparing actual positions in case of duplicate vertices
  41. for (int i = 0; i < node.vertCount; i++)
  42. {
  43. int j = (i + 1) % node.vertCount;
  44. for (int k = 0; k < neighbour.vertCount; k++)
  45. {
  46. int l = (k + 1) % neighbour.vertCount;
  47. if ((SamePosition(tile.verts, node.verts[i], neighbourTile.verts, neighbour.verts[l])
  48. && SamePosition(tile.verts, node.verts[j], neighbourTile.verts, neighbour.verts[k]))
  49. || (SamePosition(tile.verts, node.verts[i], neighbourTile.verts, neighbour.verts[k])
  50. && SamePosition(tile.verts, node.verts[j], neighbourTile.verts, neighbour.verts[l])))
  51. {
  52. return i;
  53. }
  54. }
  55. }
  56. return -1;
  57. }
  58. private static bool SamePosition(float[] verts, int v, float[] verts2, int v2)
  59. {
  60. for (int i = 0; i < 3; i++)
  61. {
  62. if (verts[3 * v + i] != verts2[3 * v2 + 1])
  63. {
  64. return false;
  65. }
  66. }
  67. return true;
  68. }
  69. /**
  70. * Find edge closest to the given coordinate
  71. */
  72. public static int FindEdge(DtPoly node, DtMeshData tile, float value, int comp)
  73. {
  74. float error = float.MaxValue;
  75. int edge = 0;
  76. for (int i = 0; i < node.vertCount; i++)
  77. {
  78. int j = (i + 1) % node.vertCount;
  79. float v1 = tile.verts[3 * node.verts[i] + comp] - value;
  80. float v2 = tile.verts[3 * node.verts[j] + comp] - value;
  81. float d = v1 * v1 + v2 * v2;
  82. if (d < error)
  83. {
  84. error = d;
  85. edge = i;
  86. }
  87. }
  88. return edge;
  89. }
  90. }
  91. }