ChainShapeConf.hpp
Go to the documentation of this file.
1 /*
2  * Original work Copyright (c) 2006-2010 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 
22 #ifndef PLAYRHO_COLLISION_SHAPES_CHAINSHAPECONF_HPP
23 #define PLAYRHO_COLLISION_SHAPES_CHAINSHAPECONF_HPP
24 
25 #include <PlayRho/Common/Math.hpp>
30 #include <vector>
31 
32 namespace playrho {
33 namespace d2 {
34 
49 class ChainShapeConf: public ShapeBuilder<ChainShapeConf>
50 {
51 public:
54  {
56  }
57 
60 
62  ChainShapeConf& Set(std::vector<Length2> arg);
63 
65  ChainShapeConf& Add(Length2 vertex);
66 
70  ChainShapeConf& Transform(const Mat22& m) noexcept;
71 
73  ChildCounter GetChildCount() const noexcept
74  {
75  // edge count = vertex count - 1
76  const auto count = GetVertexCount();
77  return (count > 1)? count - 1: count;
78  }
79 
81  DistanceProxy GetChild(ChildCounter index) const;
82 
84  MassData GetMassData() const noexcept;
85 
88 
90  ChildCounter GetVertexCount() const noexcept
91  {
92  return static_cast<ChildCounter>(size(m_vertices));
93  }
94 
97  {
98  assert((0 <= index) && (index < GetVertexCount()));
99  return m_vertices[index];
100  }
101 
104  {
105  assert((0 <= index) && (index < GetVertexCount()));
106  return m_normals[index];
107  }
108 
110  friend bool operator== (const ChainShapeConf& lhs, const ChainShapeConf& rhs) noexcept
111  {
112  // Don't need to check normals since normals based on vertices.
113  return lhs.vertexRadius == rhs.vertexRadius && lhs.friction == rhs.friction
114  && lhs.restitution == rhs.restitution && lhs.density == rhs.density
115  && lhs.m_vertices == rhs.m_vertices;
116  }
117 
119  friend bool operator!= (const ChainShapeConf& lhs, const ChainShapeConf& rhs) noexcept
120  {
121  return !(lhs == rhs);
122  }
123 
136 
137 private:
139  void ResetNormals();
140 
141  std::vector<Length2> m_vertices;
142  std::vector<UnitVec> m_normals;
143 };
144 
146 {
147  vertexRadius = value;
148  return *this;
149 }
150 
151 // Free functions...
152 
154 inline ChildCounter GetChildCount(const ChainShapeConf& arg) noexcept
155 {
156  return arg.GetChildCount();
157 }
158 
161 {
162  return arg.GetChild(index);
163 }
164 
166 inline MassData GetMassData(const ChainShapeConf& arg) noexcept
167 {
168  return arg.GetMassData();
169 }
170 
172 inline bool IsLooped(const ChainShapeConf& shape) noexcept
173 {
174  const auto count = shape.GetVertexCount();
175  return (count > 1)? (shape.GetVertex(count - 1) == shape.GetVertex(0)): false;
176 }
177 
179 inline ChildCounter GetNextIndex(const ChainShapeConf& shape, ChildCounter index) noexcept
180 {
181  return GetModuloNext(index, shape.GetVertexCount());
182 }
183 
186 {
187  return arg.vertexRadius;
188 }
189 
192 {
193  return GetVertexRadius(arg);
194 }
195 
199 inline void Transform(ChainShapeConf& arg, const Mat22& m) noexcept
200 {
201  arg.Transform(m);
202 }
203 
206 ChainShapeConf GetChainShapeConf(Length2 dimensions);
207 
211 {
212  return GetChainShapeConf(Length2{dimension, dimension});
213 }
214 
216 ChainShapeConf GetChainShapeConf(const AABB& arg);
217 
218 } // namespace d2
219 } // namespace playrho
220 
221 #endif // PLAYRHO_COLLISION_SHAPES_CHAINSHAPECONF_HPP