planeExtractor.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "math/mMath.h"
  24. #include "console/console.h"
  25. #include "collision/planeExtractor.h"
  26. //----------------------------------------------------------------------------
  27. // Plane matching parameters
  28. static F32 NormalEpsilon = 0.93969f; // 20 deg.
  29. static F32 DistanceEpsilon = 100.0f;
  30. //----------------------------------------------------------------------------
  31. PlaneExtractorPolyList::PlaneExtractorPolyList()
  32. {
  33. VECTOR_SET_ASSOCIATION(mVertexList);
  34. VECTOR_SET_ASSOCIATION(mPolyPlaneList);
  35. mPlaneList = NULL;
  36. }
  37. PlaneExtractorPolyList::~PlaneExtractorPolyList()
  38. {
  39. }
  40. //----------------------------------------------------------------------------
  41. void PlaneExtractorPolyList::clear()
  42. {
  43. mVertexList.clear();
  44. mPolyPlaneList.clear();
  45. }
  46. U32 PlaneExtractorPolyList::addPoint(const Point3F& p)
  47. {
  48. mVertexList.increment();
  49. Point3F& v = mVertexList.last();
  50. v.x = p.x * mScale.x;
  51. v.y = p.y * mScale.y;
  52. v.z = p.z * mScale.z;
  53. mMatrix.mulP(v);
  54. return mVertexList.size() - 1;
  55. }
  56. U32 PlaneExtractorPolyList::addPlane(const PlaneF& plane)
  57. {
  58. mPolyPlaneList.increment();
  59. mPlaneTransformer.transform(plane, mPolyPlaneList.last());
  60. return mPolyPlaneList.size() - 1;
  61. }
  62. //----------------------------------------------------------------------------
  63. void PlaneExtractorPolyList::plane(U32 v1,U32 v2,U32 v3)
  64. {
  65. mPlaneList->last().set(mVertexList[v1],
  66. mVertexList[v2],mVertexList[v3]);
  67. }
  68. void PlaneExtractorPolyList::plane(const PlaneF& p)
  69. {
  70. mPlaneTransformer.transform(p, mPlaneList->last());
  71. }
  72. void PlaneExtractorPolyList::plane(const U32 index)
  73. {
  74. AssertFatal(index < mPolyPlaneList.size(), "Out of bounds index!");
  75. mPlaneList->last() = mPolyPlaneList[index];
  76. }
  77. const PlaneF& PlaneExtractorPolyList::getIndexedPlane(const U32 index)
  78. {
  79. AssertFatal(index < mPolyPlaneList.size(), "Out of bounds index!");
  80. return mPolyPlaneList[index];
  81. }
  82. //----------------------------------------------------------------------------
  83. bool PlaneExtractorPolyList::isEmpty() const
  84. {
  85. return true;
  86. }
  87. void PlaneExtractorPolyList::begin(BaseMatInstance*,U32)
  88. {
  89. mPlaneList->increment();
  90. }
  91. void PlaneExtractorPolyList::end()
  92. {
  93. // See if there are any duplicate planes
  94. PlaneF &plane = mPlaneList->last();
  95. PlaneF *ptr = mPlaneList->begin();
  96. for (; ptr != &plane; ptr++)
  97. if (mFabs(ptr->d - plane.d) < DistanceEpsilon &&
  98. mDot(*ptr,plane) > NormalEpsilon) {
  99. mPlaneList->decrement();
  100. return;
  101. }
  102. }
  103. void PlaneExtractorPolyList::vertex(U32)
  104. {
  105. }