MultiShapeConf.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  * claim that you wrote the original software. If you use this software
14  * in a product, an acknowledgment in the product documentation would be
15  * appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  * misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  */
20 
21 #ifndef PLAYRHO_COLLISION_SHAPES_MULTISHAPECONF_HPP
22 #define PLAYRHO_COLLISION_SHAPES_MULTISHAPECONF_HPP
23 
24 #include <PlayRho/Common/Math.hpp>
28 #include <vector>
29 
30 namespace playrho {
31 namespace d2 {
32 
33 class VertexSet;
34 
37 {
38 public:
39 
41  static ConvexHull Get(const VertexSet& pointSet, NonNegative<Length> vertexRadius =
43 
46  {
47  return DistanceProxy{
48  vertexRadius, static_cast<VertexCounter>(size(vertices)),
49  data(vertices), data(normals)
50  };
51  }
52 
56  {
57  return vertexRadius;
58  }
59 
62  ConvexHull& Transform(const Mat22& m) noexcept;
63 
65  friend bool operator== (const ConvexHull& lhs, const ConvexHull& rhs) noexcept
66  {
67  // No need to check normals - they're based on vertices.
68  return lhs.vertexRadius == rhs.vertexRadius && lhs.vertices == rhs.vertices;
69  }
70 
72  friend bool operator!= (const ConvexHull& lhs, const ConvexHull& rhs) noexcept
73  {
74  return !(lhs == rhs);
75  }
76 
77 private:
79  ConvexHull(std::vector<Length2> verts, std::vector<UnitVec> norms,
81  vertices{verts}, normals{norms}, vertexRadius{vr}
82  {}
83 
86  std::vector<Length2> vertices;
87 
92  std::vector<UnitVec> normals;
93 
105  NonNegative<Length> vertexRadius;
106 };
107 
111 struct MultiShapeConf: public ShapeBuilder<MultiShapeConf>
112 {
115  {
117  }
118 
120  static inline MultiShapeConf GetDefaultConf() noexcept
121  {
122  return MultiShapeConf{};
123  }
124 
125  inline MultiShapeConf():
127  {
128  // Intentionally empty.
129  }
130 
136  MultiShapeConf& AddConvexHull(const VertexSet& pointSet, NonNegative<Length> vertexRadius =
137  GetDefaultVertexRadius()) noexcept;
138 
141  MultiShapeConf& Transform(const Mat22& m) noexcept;
142 
143  std::vector<ConvexHull> children;
144 };
145 
146 // Free functions...
147 
149 inline bool operator== (const MultiShapeConf& lhs, const MultiShapeConf& rhs) noexcept
150 {
151  return lhs.friction == rhs.friction && lhs.restitution == rhs.restitution
152  && lhs.density == rhs.density && lhs.children == rhs.children;
153 }
154 
156 inline bool operator!= (const MultiShapeConf& lhs, const MultiShapeConf& rhs) noexcept
157 {
158  return !(lhs == rhs);
159 }
160 
162 inline ChildCounter GetChildCount(const MultiShapeConf& arg) noexcept
163 {
164  return static_cast<ChildCounter>(size(arg.children));
165 }
166 
169 {
170  if (index >= GetChildCount(arg))
171  {
172  throw InvalidArgument("index out of range");
173  }
174  return arg.children[index].GetDistanceProxy();
175 }
176 
178 MassData GetMassData(const MultiShapeConf& arg) noexcept;
179 
182 {
183  if (index >= GetChildCount(arg))
184  {
185  throw InvalidArgument("index out of range");
186  }
187  return arg.children[index].GetVertexRadius();
188 }
189 
193 inline void Transform(MultiShapeConf& arg, const Mat22& m) noexcept
194 {
195  arg.Transform(m);
196 }
197 
198 } // namespace d2
199 } // namespace playrho
200 
201 #endif // PLAYRHO_COLLISION_SHAPES_MULTISHAPECONF_HPP