GeometryUtils.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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,
  57. unsigned blendWeightsOffset, float* clip, unsigned* clipVertexCount)
  58. {
  59. unsigned components = vertexSize / sizeof(float);
  60. unsigned blendWeightsComp = blendWeightsOffset / sizeof(float);
  61. unsigned outVertexCount = 0;
  62. float* lastVertex = input;
  63. float lastDistance = 0.0f;
  64. if (clipVertexCount)
  65. *clipVertexCount = 0;
  66. for (unsigned i = 0; i < vertexCount; ++i)
  67. {
  68. float* vertex = input + i * components;
  69. const Vector3& position = *((Vector3*)vertex);
  70. float distance = plane.Distance(position);
  71. if (distance >= 0.0f)
  72. {
  73. if (lastDistance < 0.0f)
  74. {
  75. ClipEdge(output, lastVertex, vertex, lastDistance, distance, components, blendWeightsComp);
  76. if (clip && clipVertexCount)
  77. {
  78. CopyVertex(clip, output, components);
  79. clip += components;
  80. ++(*clipVertexCount);
  81. }
  82. output += components;
  83. ++outVertexCount;
  84. }
  85. for (unsigned j = 0; j < components; ++j)
  86. output[j] = vertex[j];
  87. output += components;
  88. ++outVertexCount;
  89. }
  90. else
  91. {
  92. if (lastDistance >= 0.0f && i != 0)
  93. {
  94. ClipEdge(output, lastVertex, vertex, lastDistance, distance, components, blendWeightsComp);
  95. if (clip && clipVertexCount)
  96. {
  97. CopyVertex(clip, output, components);
  98. clip += components;
  99. ++(*clipVertexCount);
  100. }
  101. output += components;
  102. ++outVertexCount;
  103. }
  104. }
  105. lastVertex = vertex;
  106. lastDistance = distance;
  107. }
  108. // Recheck the distances of the last and first vertices and add the final clipped vertex if applicable
  109. {
  110. float* vertex = input;
  111. const Vector3& position = *((Vector3*)vertex);
  112. float distance = plane.Distance(position);
  113. if ((lastDistance < 0.0f && distance >= 0.0f) || (lastDistance >= 0.0f && distance < 0.0f))
  114. {
  115. ClipEdge(output, lastVertex, vertex, lastDistance, distance, components, blendWeightsComp);
  116. if (clip && clipVertexCount)
  117. {
  118. CopyVertex(clip, output, components);
  119. clip += components;
  120. ++(*clipVertexCount);
  121. }
  122. output += components;
  123. ++outVertexCount;
  124. }
  125. }
  126. return outVertexCount;
  127. }