CCPlane.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /****************************************************************************
  2. Copyright (c) 2014-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  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. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "3d/CCPlane.h"
  22. NS_CC_BEGIN
  23. Plane::Plane()
  24. :
  25. _normal(0.f, 0.f, 1.f),
  26. _dist(0.f)
  27. {
  28. }
  29. // create plane from tree point
  30. Plane::Plane(const Vec3& p1, const Vec3& p2, const Vec3& p3)
  31. {
  32. initPlane(p1, p2, p3);
  33. }
  34. // create plane from normal and dist
  35. Plane::Plane(const Vec3& normal, float dist)
  36. {
  37. initPlane(normal, dist);
  38. }
  39. // create plane from normal and a point on plane
  40. Plane::Plane(const Vec3& normal, const Vec3& point)
  41. {
  42. initPlane(normal, point);
  43. }
  44. void Plane::initPlane(const Vec3& p1, const Vec3& p2, const Vec3& p3)
  45. {
  46. Vec3 p21 = p2 - p1;
  47. Vec3 p32 = p3 - p2;
  48. Vec3::cross(p21, p32, &_normal);
  49. _normal.normalize();
  50. _dist = _normal.dot(p1);
  51. }
  52. void Plane::initPlane(const Vec3& normal, float dist)
  53. {
  54. float oneOverLength = 1 / normal.length();
  55. _normal = normal * oneOverLength;
  56. _dist = dist * oneOverLength;
  57. }
  58. void Plane::initPlane(const Vec3& normal, const Vec3& point)
  59. {
  60. _normal = normal;
  61. _normal.normalize();
  62. _dist = _normal.dot(point);
  63. }
  64. float Plane::dist2Plane(const Vec3& p) const
  65. {
  66. return _normal.dot(p) - _dist;
  67. }
  68. PointSide Plane::getSide(const Vec3& point) const
  69. {
  70. float dist = dist2Plane(point);
  71. if (dist > 0)
  72. return PointSide::FRONT_PLANE;
  73. else if (dist < 0)
  74. return PointSide::BEHIND_PLANE;
  75. else
  76. return PointSide::IN_PLANE;
  77. }
  78. NS_CC_END