GeometryUtils.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "GeometryUtils.h"
  25. #include "Plane.h"
  26. inline void ClipEdge(float* output, float* v0, float* v1, float d0, float d1, unsigned components, unsigned blendWeightsComp)
  27. {
  28. float t = d0 / (d0 - d1);
  29. if (!blendWeightsComp)
  30. {
  31. for (unsigned i = 0; i < components; ++i)
  32. output[i] = v0[i] + t * (v1[i] - v0[i]);
  33. }
  34. else
  35. {
  36. for (unsigned i = 0; i < blendWeightsComp; ++i)
  37. output[i] = v0[i] + t * (v1[i] - v0[i]);
  38. // Blend weights / indices can not be easily interpolated, choose the one that is nearer to the split plane
  39. if (Abs(d0) < Abs(d1))
  40. {
  41. for (unsigned i = blendWeightsComp; i < components; ++i)
  42. output[i] = v0[i];
  43. }
  44. else
  45. {
  46. for (unsigned i = blendWeightsComp; i < components; ++i)
  47. output[i] = v1[i];
  48. }
  49. }
  50. }
  51. inline void CopyVertex(float* output, float* input, unsigned components)
  52. {
  53. for (unsigned i = 0; i < components; ++i)
  54. output[i] = input[i];
  55. }
  56. unsigned ClipPolygon(float* input, float* output, unsigned vertexCount, unsigned vertexSize, const Plane& plane, unsigned blendWeightsOffset, float* clip, unsigned* clipVertexCount)
  57. {
  58. unsigned components = vertexSize / sizeof(float);
  59. unsigned blendWeightsComp = blendWeightsOffset / sizeof(float);
  60. unsigned outVertexCount = 0;
  61. float* lastVertex = input;
  62. float lastDistance = 0.0f;
  63. if (clipVertexCount)
  64. *clipVertexCount = 0;
  65. for (unsigned i = 0; i < vertexCount; ++i)
  66. {
  67. float* vertex = input + i * components;
  68. const Vector3& position = *((Vector3*)vertex);
  69. float distance = plane.Distance(position);
  70. if (distance >= 0.0f)
  71. {
  72. if (lastDistance < 0.0f)
  73. {
  74. ClipEdge(output, lastVertex, vertex, lastDistance, distance, components, blendWeightsComp);
  75. if (clip && clipVertexCount)
  76. {
  77. CopyVertex(clip, output, components);
  78. clip += components;
  79. ++(*clipVertexCount);
  80. }
  81. output += components;
  82. ++outVertexCount;
  83. }
  84. for (unsigned j = 0; j < components; ++j)
  85. output[j] = vertex[j];
  86. output += components;
  87. ++outVertexCount;
  88. }
  89. else
  90. {
  91. if (lastDistance >= 0.0f && i != 0)
  92. {
  93. ClipEdge(output, lastVertex, vertex, lastDistance, distance, components, blendWeightsComp);
  94. if (clip && clipVertexCount)
  95. {
  96. CopyVertex(clip, output, components);
  97. clip += components;
  98. ++(*clipVertexCount);
  99. }
  100. output += components;
  101. ++outVertexCount;
  102. }
  103. }
  104. lastVertex = vertex;
  105. lastDistance = distance;
  106. }
  107. // Recheck the distances of the last and first vertices and add the final clipped vertex if applicable
  108. {
  109. float* vertex = input;
  110. const Vector3& position = *((Vector3*)vertex);
  111. float distance = plane.Distance(position);
  112. if ((lastDistance < 0.0f && distance >= 0.0f) || (lastDistance >= 0.0f && distance < 0.0f))
  113. {
  114. ClipEdge(output, lastVertex, vertex, lastDistance, distance, components, blendWeightsComp);
  115. if (clip && clipVertexCount)
  116. {
  117. CopyVertex(clip, output, components);
  118. clip += components;
  119. ++(*clipVertexCount);
  120. }
  121. output += components;
  122. ++outVertexCount;
  123. }
  124. }
  125. return outVertexCount;
  126. }