AABB.cpp
Go to the documentation of this file.
1 /*
2  * Original work Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
3  * Modified work Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 
29 
32 
33 namespace playrho {
34 namespace d2 {
35 
36 AABB ComputeAABB(const DistanceProxy& proxy, const Transformation& xf) noexcept
37 {
38  assert(IsValid(xf));
39  auto result = AABB{};
40  for (const auto& vertex: proxy.GetVertices())
41  {
42  Include(result, Transform(vertex, xf));
43  }
44  return GetFattenedAABB(result, proxy.GetVertexRadius());
45 }
46 
48  const Transformation& xfm0, const Transformation& xfm1) noexcept
49 {
50  assert(IsValid(xfm0));
51  assert(IsValid(xfm1));
52  auto result = AABB{};
53  for (const auto& vertex: proxy.GetVertices())
54  {
55  Include(result, Transform(vertex, xfm0));
56  Include(result, Transform(vertex, xfm1));
57  }
58  return GetFattenedAABB(result, proxy.GetVertexRadius());
59 }
60 
61 AABB ComputeAABB(const Shape& shape, const Transformation& xf) noexcept
62 {
63  auto sum = AABB{};
64  const auto childCount = GetChildCount(shape);
65  for (auto i = decltype(childCount){0}; i < childCount; ++i)
66  {
67  Include(sum, ComputeAABB(GetChild(shape, i), xf));
68  }
69  return sum;
70 }
71 
72 AABB ComputeAABB(const Fixture& fixture) noexcept
73 {
74  return ComputeAABB(fixture.GetShape(), fixture.GetBody()->GetTransformation());
75 }
76 
77 AABB ComputeAABB(const Body& body)
78 {
79  auto sum = AABB{};
80  const auto xf = body.GetTransformation();
81  for (auto&& f: body.GetFixtures())
82  {
83  Include(sum, ComputeAABB((GetRef(f).GetShape()), xf));
84  }
85  return sum;
86 }
87 
89  const Fixture& fB, ChildCounter iB) noexcept
90 {
91  const auto xA = fA.GetBody()->GetTransformation();
92  const auto xB = fB.GetBody()->GetTransformation();
93  const auto childA = GetChild(fA.GetShape(), iA);
94  const auto childB = GetChild(fB.GetShape(), iB);
95  const auto aabbA = ComputeAABB(childA, xA);
96  const auto aabbB = ComputeAABB(childB, xB);
97  return GetIntersectingAABB(aabbA, aabbB);
98 }
99 
101 {
102  return ComputeIntersectingAABB(*contact.GetFixtureA(), contact.GetChildIndexA(),
103  *contact.GetFixtureB(), contact.GetChildIndexB());
104 }
105 
106 AABB GetAABB(const RayCastInput& input) noexcept
107 {
108  const auto totalDelta = input.p2 - input.p1;
109  const auto fractDelta = input.maxFraction * totalDelta;
110  return AABB{input.p1, input.p1 + fractDelta};
111 }
112 
113 } // namespace d2
114 } // namespace playrho