PolygonShapeConf.hpp
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  * 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  * 1. The origin of this software must not be misrepresented; you must not
12  * claim that you wrote the original software. If you use this software
13  * in a product, an acknowledgment in the product documentation would be
14  * appreciated but is not required.
15  * 2. Altered source versions must be plainly marked as such, and must not be
16  * misrepresented as being the original software.
17  * 3. This notice may not be removed or altered from any source distribution.
18  */
19 
20 #ifndef PLAYRHO_COLLISION_SHAPES_POLYGONSHAPECONF_HPP
21 #define PLAYRHO_COLLISION_SHAPES_POLYGONSHAPECONF_HPP
22 
23 #include <PlayRho/Common/Math.hpp>
28 #include <type_traits>
29 #include <vector>
30 
31 namespace playrho {
32 namespace d2 {
33 
41 class PolygonShapeConf: public ShapeBuilder<PolygonShapeConf>
42 {
43 public:
46  {
48  }
49 
51  static inline PolygonShapeConf GetDefaultConf() noexcept
52  {
53  return PolygonShapeConf{};
54  }
55 
57 
60  const PolygonShapeConf& conf = GetDefaultConf()) noexcept;
61 
67  explicit PolygonShapeConf(Span<const Length2> points,
68  const PolygonShapeConf& conf = GetDefaultConf()) noexcept;
69 
72 
74  PolygonShapeConf& UseVertices(const std::vector<Length2>& verts) noexcept;
75 
79  PolygonShapeConf& SetAsBox(Length hx, Length hy) noexcept;
80 
82  PolygonShapeConf& SetAsBox(Length hx, Length hy, Length2 center, Angle angle) noexcept;
83 
89  PolygonShapeConf& Set(Span<const Length2> verts) noexcept;
90 
96  PolygonShapeConf& Set(const VertexSet& points) noexcept;
97 
100 
103  PolygonShapeConf& Transform(const Mat22& m) noexcept;
104 
106  friend bool operator== (const PolygonShapeConf& lhs, const PolygonShapeConf& rhs) noexcept
107  {
108  // Don't need to check normals nor centroid since they based on vertices.
109  return lhs.vertexRadius == rhs.vertexRadius && lhs.friction == rhs.friction
110  && lhs.restitution == rhs.restitution && lhs.density == rhs.density
111  && lhs.m_vertices == rhs.m_vertices;
112  }
113 
115  friend bool operator!= (const PolygonShapeConf& lhs, const PolygonShapeConf& rhs) noexcept
116  {
117  return !(lhs == rhs);
118  }
119 
123  VertexCounter GetVertexCount() const noexcept
124  {
125  return static_cast<VertexCounter>(size(m_vertices));
126  }
127 
131  {
132  assert(0 <= index && index < GetVertexCount());
133  return m_vertices[index];
134  }
135 
143  {
144  assert(0 <= index && index < GetVertexCount());
145  return m_normals[index];
146  }
147 
151  {
152  return Span<const Length2>(&m_vertices[0], GetVertexCount());
153  }
154 
157  {
158  return Span<const UnitVec>(&m_normals[0], GetVertexCount());
159  }
160 
162  Length2 GetCentroid() const noexcept { return m_centroid; }
163 
176 
177 private:
180  std::vector<Length2> m_vertices;
181 
185  std::vector<UnitVec> m_normals;
186 
188  Length2 m_centroid = GetInvalid<Length2>();
189 };
190 
192 {
193  vertexRadius = value;
194  return *this;
195 }
196 
197 // Free functions...
198 
202 {
203  return 1;
204 }
205 
208 {
209  if (index != 0)
210  {
211  throw InvalidArgument("only index of 0 is supported");
212  }
213  return DistanceProxy{arg.vertexRadius, arg.GetVertexCount(),
214  data(arg.GetVertices()), data(arg.GetNormals())};
215 }
216 
219 {
220  return arg.vertexRadius;
221 }
222 
225 {
226  return GetVertexRadius(arg);
227 }
228 
230 inline MassData GetMassData(const PolygonShapeConf& arg) noexcept
231 {
232  return playrho::d2::GetMassData(arg.vertexRadius, arg.density, arg.GetVertices());
233 }
234 
239 Length2 GetEdge(const PolygonShapeConf& shape, VertexCounter index);
240 
244 inline void Transform(PolygonShapeConf& arg, const Mat22& m) noexcept
245 {
246  arg.Transform(m);
247 }
248 
253 bool Validate(const Span<const Length2> verts);
254 
255 } // namespace d2
256 } // namespace playrho
257 
258 #endif // PLAYRHO_COLLISION_SHAPES_POLYGONSHAPECONF_HPP