b3ConvexHullComputer.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright (c) 2011 Ole Kniemeyer, MAXON, www.maxon.net
  3. This software is provided 'as-is', without any express or implied warranty.
  4. In no event will the authors be held liable for any damages arising from the use of this software.
  5. Permission is granted to anyone to use this software for any purpose,
  6. including commercial applications, and to alter it and redistribute it freely,
  7. subject to the following restrictions:
  8. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  9. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  10. 3. This notice may not be removed or altered from any source distribution.
  11. */
  12. #ifndef B3_CONVEX_HULL_COMPUTER_H
  13. #define B3_CONVEX_HULL_COMPUTER_H
  14. #include "Bullet3Common/b3Vector3.h"
  15. #include "Bullet3Common/b3AlignedObjectArray.h"
  16. /// Convex hull implementation based on Preparata and Hong
  17. /// See http://code.google.com/p/bullet/issues/detail?id=275
  18. /// Ole Kniemeyer, MAXON Computer GmbH
  19. class b3ConvexHullComputer
  20. {
  21. private:
  22. b3Scalar compute(const void* coords, bool doubleCoords, int stride, int count, b3Scalar shrink, b3Scalar shrinkClamp);
  23. public:
  24. class Edge
  25. {
  26. private:
  27. int next;
  28. int reverse;
  29. int targetVertex;
  30. friend class b3ConvexHullComputer;
  31. public:
  32. int getSourceVertex() const
  33. {
  34. return (this + reverse)->targetVertex;
  35. }
  36. int getTargetVertex() const
  37. {
  38. return targetVertex;
  39. }
  40. const Edge* getNextEdgeOfVertex() const // clockwise list of all edges of a vertex
  41. {
  42. return this + next;
  43. }
  44. const Edge* getNextEdgeOfFace() const // counter-clockwise list of all edges of a face
  45. {
  46. return (this + reverse)->getNextEdgeOfVertex();
  47. }
  48. const Edge* getReverseEdge() const
  49. {
  50. return this + reverse;
  51. }
  52. };
  53. // Vertices of the output hull
  54. b3AlignedObjectArray<b3Vector3> vertices;
  55. // Edges of the output hull
  56. b3AlignedObjectArray<Edge> edges;
  57. // Faces of the convex hull. Each entry is an index into the "edges" array pointing to an edge of the face. Faces are planar n-gons
  58. b3AlignedObjectArray<int> faces;
  59. /*
  60. Compute convex hull of "count" vertices stored in "coords". "stride" is the difference in bytes
  61. between the addresses of consecutive vertices. If "shrink" is positive, the convex hull is shrunken
  62. by that amount (each face is moved by "shrink" length units towards the center along its normal).
  63. If "shrinkClamp" is positive, "shrink" is clamped to not exceed "shrinkClamp * innerRadius", where "innerRadius"
  64. is the minimum distance of a face to the center of the convex hull.
  65. The returned value is the amount by which the hull has been shrunken. If it is negative, the amount was so large
  66. that the resulting convex hull is empty.
  67. The output convex hull can be found in the member variables "vertices", "edges", "faces".
  68. */
  69. b3Scalar compute(const float* coords, int stride, int count, b3Scalar shrink, b3Scalar shrinkClamp)
  70. {
  71. return compute(coords, false, stride, count, shrink, shrinkClamp);
  72. }
  73. // same as above, but double precision
  74. b3Scalar compute(const double* coords, int stride, int count, b3Scalar shrink, b3Scalar shrinkClamp)
  75. {
  76. return compute(coords, true, stride, count, shrink, shrinkClamp);
  77. }
  78. };
  79. #endif //B3_CONVEX_HULL_COMPUTER_H