Skip to content

Commit a0ecc4a

Browse files
Merge pull request #280 from louis-langholtz/shape-changes-20180118
Several changes to Shape class
2 parents 706269f + b244d8d commit a0ecc4a

29 files changed

+430
-199
lines changed

PlayRho/Collision/Shapes/ChainShapeConf.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ namespace {
4343
#endif
4444
} // anonymous namespace
4545

46-
ChainShapeConf::ChainShapeConf():
47-
ShapeBuilder{ShapeConf{ShapeConf{}.UseVertexRadius(GetDefaultVertexRadius())}}
48-
{
49-
// Intentionally empty.
50-
}
46+
ChainShapeConf::ChainShapeConf() = default;
5147

5248
ChainShapeConf& ChainShapeConf::Set(std::vector<Length2> vertices)
5349
{
@@ -104,7 +100,6 @@ MassData ChainShapeConf::GetMassData() const noexcept
104100
const auto density = this->density;
105101
if (density > AreaDensity(0))
106102
{
107-
const auto vertexRadius = this->vertexRadius;
108103
const auto vertexCount = GetVertexCount();
109104
if (vertexCount > 1)
110105
{
@@ -147,7 +142,6 @@ DistanceProxy ChainShapeConf::GetChild(ChildCounter index) const
147142
{
148143
throw InvalidArgument("index out of range");
149144
}
150-
const auto vertexRadius = this->vertexRadius;
151145
const auto vertexCount = GetVertexCount();
152146
if (vertexCount > 1)
153147
{

PlayRho/Collision/Shapes/ChainShapeConf.hpp

+35-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ChainShapeConf: public ShapeBuilder<ChainShapeConf>
5151
/// @brief Gets the default vertex radius.
5252
static PLAYRHO_CONSTEXPR inline NonNegative<Length> GetDefaultVertexRadius() noexcept
5353
{
54-
return DefaultLinearSlop * 2;
54+
return NonNegative<Length>{DefaultLinearSlop * Real{2}};
5555
}
5656

5757
/// @brief Default constructor.
@@ -77,6 +77,9 @@ class ChainShapeConf: public ShapeBuilder<ChainShapeConf>
7777
/// @brief Gets the mass data.
7878
MassData GetMassData() const noexcept;
7979

80+
/// @brief Uses the given vertex radius.
81+
inline ChainShapeConf& UseVertexRadius(NonNegative<Length> value) noexcept;
82+
8083
/// @brief Gets the vertex count.
8184
ChildCounter GetVertexCount() const noexcept
8285
{
@@ -112,11 +115,30 @@ class ChainShapeConf: public ShapeBuilder<ChainShapeConf>
112115
return !(lhs == rhs);
113116
}
114117

118+
/// @brief Vertex radius.
119+
///
120+
/// @details This is the radius from the vertex that the shape's "skin" should
121+
/// extend outward by. While any edges &mdash; line segments between multiple
122+
/// vertices &mdash; are straight, corners between them (the vertices) are
123+
/// rounded and treated as rounded. Shapes with larger vertex radiuses compared
124+
/// to edge lengths therefore will be more prone to rolling or having other
125+
/// shapes more prone to roll off of them.
126+
///
127+
/// @note This should be a non-negative value.
128+
///
129+
NonNegative<Length> vertexRadius = GetDefaultVertexRadius();
130+
115131
private:
116132
std::vector<Length2> m_vertices; ///< Vertices.
117133
std::vector<UnitVec> m_normals; ///< Normals.
118134
};
119135

136+
inline ChainShapeConf& ChainShapeConf::UseVertexRadius(NonNegative<Length> value) noexcept
137+
{
138+
vertexRadius = value;
139+
return *this;
140+
}
141+
120142
// Free functions...
121143

122144
/// @brief Gets the child count for a given chain shape configuration.
@@ -150,6 +172,18 @@ inline ChildCounter GetNextIndex(const ChainShapeConf& shape, ChildCounter index
150172
return GetModuloNext(index, shape.GetVertexCount());
151173
}
152174

175+
/// @brief Gets the vertex radius of the given shape configuration.
176+
inline NonNegative<Length> GetVertexRadius(const ChainShapeConf& arg)
177+
{
178+
return arg.vertexRadius;
179+
}
180+
181+
/// @brief Gets the vertex radius of the given shape configuration.
182+
inline NonNegative<Length> GetVertexRadius(const ChainShapeConf& arg, ChildCounter)
183+
{
184+
return GetVertexRadius(arg);
185+
}
186+
153187
} // namespace d2
154188
} // namespace playrho
155189

PlayRho/Collision/Shapes/DiskShapeConf.hpp

+32-9
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,15 @@ namespace d2 {
4141
struct DiskShapeConf: ShapeBuilder<DiskShapeConf>
4242
{
4343
/// @brief Gets the default radius.
44-
static PLAYRHO_CONSTEXPR inline Length GetDefaultRadius() noexcept
44+
static PLAYRHO_CONSTEXPR inline NonNegative<Length> GetDefaultRadius() noexcept
4545
{
46-
return DefaultLinearSlop * 2;
46+
return NonNegative<Length>{DefaultLinearSlop * 2};
4747
}
4848

49-
PLAYRHO_CONSTEXPR inline DiskShapeConf(): ShapeBuilder{ShapeConf{}.UseVertexRadius(GetDefaultRadius())}
50-
{
51-
// Intentionally empty.
52-
}
49+
PLAYRHO_CONSTEXPR DiskShapeConf() = default;
5350

5451
/// @brief Initializing constructor.
55-
PLAYRHO_CONSTEXPR inline DiskShapeConf(Length radius): ShapeBuilder{ShapeConf{}.UseVertexRadius(radius)}
52+
PLAYRHO_CONSTEXPR inline DiskShapeConf(NonNegative<Length> r): vertexRadius{r}
5653
{
5754
// Intentionally empty.
5855
}
@@ -65,9 +62,9 @@ struct DiskShapeConf: ShapeBuilder<DiskShapeConf>
6562
}
6663

6764
/// @brief Uses the given value as the radius.
68-
PLAYRHO_CONSTEXPR inline DiskShapeConf& UseRadius(Length radius) noexcept
65+
PLAYRHO_CONSTEXPR inline DiskShapeConf& UseRadius(NonNegative<Length> r) noexcept
6966
{
70-
vertexRadius = radius;
67+
vertexRadius = r;
7168
return *this;
7269
}
7370

@@ -83,6 +80,19 @@ struct DiskShapeConf: ShapeBuilder<DiskShapeConf>
8380
return location;
8481
}
8582

83+
/// @brief Vertex radius.
84+
///
85+
/// @details This is the radius from the vertex that the shape's "skin" should
86+
/// extend outward by. While any edges &mdash; line segments between multiple
87+
/// vertices &mdash; are straight, corners between them (the vertices) are
88+
/// rounded and treated as rounded. Shapes with larger vertex radiuses compared
89+
/// to edge lengths therefore will be more prone to rolling or having other
90+
/// shapes more prone to roll off of them.
91+
///
92+
/// @note This should be a non-negative value.
93+
///
94+
NonNegative<Length> vertexRadius = GetDefaultRadius();
95+
8696
/// @brief Location for the disk shape to be centered at.
8797
Length2 location = Length2{};
8898
};
@@ -119,6 +129,19 @@ inline DistanceProxy GetChild(const DiskShapeConf& arg, ChildCounter index)
119129
return DistanceProxy{arg.vertexRadius, 1, &arg.location, nullptr};
120130
}
121131

132+
/// @brief Gets the vertex radius of the given shape configuration.
133+
PLAYRHO_CONSTEXPR inline NonNegative<Length> GetVertexRadius(const DiskShapeConf& arg) noexcept
134+
{
135+
return arg.vertexRadius;
136+
}
137+
138+
/// @brief Gets the vertex radius of the given shape configuration.
139+
PLAYRHO_CONSTEXPR inline NonNegative<Length> GetVertexRadius(const DiskShapeConf& arg,
140+
ChildCounter) noexcept
141+
{
142+
return GetVertexRadius(arg);
143+
}
144+
122145
/// @brief Gets the mass data of the given disk shape configuration.
123146
inline MassData GetMassData(const DiskShapeConf& arg) noexcept
124147
{

PlayRho/Collision/Shapes/EdgeShapeConf.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,8 @@
2222
namespace playrho {
2323
namespace d2 {
2424

25-
EdgeShapeConf::EdgeShapeConf():
26-
ShapeBuilder{ShapeConf{}.UseVertexRadius(GetDefaultVertexRadius())}
27-
{
28-
// Intentionally empty.
29-
}
30-
3125
EdgeShapeConf::EdgeShapeConf(Length2 vA, Length2 vB, const EdgeShapeConf& conf) noexcept:
32-
ShapeBuilder{conf}, m_vertices{vA, vB}
26+
ShapeBuilder{conf}, vertexRadius{conf.vertexRadius}, m_vertices{vA, vB}
3327
{
3428
const auto normal = GetUnitVector(GetFwdPerpendicular(vB - vA));
3529
m_normals[0] = normal;

PlayRho/Collision/Shapes/EdgeShapeConf.hpp

+37-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class EdgeShapeConf: public ShapeBuilder<EdgeShapeConf>
4242
{
4343
public:
4444
/// @brief Gets the default vertex radius.
45-
static PLAYRHO_CONSTEXPR inline Length GetDefaultVertexRadius() noexcept
45+
static PLAYRHO_CONSTEXPR inline NonNegative<Length> GetDefaultVertexRadius() noexcept
4646
{
47-
return DefaultLinearSlop * Real{2};
47+
return NonNegative<Length>{DefaultLinearSlop * Real{2}};
4848
}
4949

5050
/// @brief Gets the default configuration.
@@ -53,14 +53,17 @@ class EdgeShapeConf: public ShapeBuilder<EdgeShapeConf>
5353
return EdgeShapeConf{};
5454
}
5555

56-
EdgeShapeConf();
56+
EdgeShapeConf() = default;
5757

5858
/// @brief Initializing constructor.
5959
EdgeShapeConf(Length2 vA, Length2 vB, const EdgeShapeConf& conf = GetDefaultConf()) noexcept;
6060

6161
/// @brief Sets both vertices in one call.
6262
EdgeShapeConf& Set(Length2 vA, Length2 vB) noexcept;
6363

64+
/// @brief Uses the given vertex radius.
65+
EdgeShapeConf& UseVertexRadius(NonNegative<Length> value) noexcept;
66+
6467
/// @brief Gets vertex A.
6568
Length2 GetVertexA() const noexcept
6669
{
@@ -79,11 +82,30 @@ class EdgeShapeConf: public ShapeBuilder<EdgeShapeConf>
7982
return DistanceProxy{vertexRadius, 2, m_vertices, m_normals};
8083
}
8184

85+
/// @brief Vertex radius.
86+
///
87+
/// @details This is the radius from the vertex that the shape's "skin" should
88+
/// extend outward by. While any edges &mdash; line segments between multiple
89+
/// vertices &mdash; are straight, corners between them (the vertices) are
90+
/// rounded and treated as rounded. Shapes with larger vertex radiuses compared
91+
/// to edge lengths therefore will be more prone to rolling or having other
92+
/// shapes more prone to roll off of them.
93+
///
94+
/// @note This should be a non-negative value.
95+
///
96+
NonNegative<Length> vertexRadius = GetDefaultVertexRadius();
97+
8298
private:
8399
Length2 m_vertices[2] = {Length2{}, Length2{}}; ///< Vertices
84100
UnitVec m_normals[2] = {UnitVec{}, UnitVec{}}; ///< Normals.
85101
};
86102

103+
inline EdgeShapeConf& EdgeShapeConf::UseVertexRadius(NonNegative<Length> value) noexcept
104+
{
105+
vertexRadius = value;
106+
return *this;
107+
}
108+
87109
// Free functions...
88110

89111
/// @brief Equality operator.
@@ -117,6 +139,18 @@ inline DistanceProxy GetChild(const EdgeShapeConf& arg, ChildCounter index)
117139
return arg.GetChild();
118140
}
119141

142+
/// @brief Gets the vertex radius of the given shape configuration.
143+
inline NonNegative<Length> GetVertexRadius(const EdgeShapeConf& arg) noexcept
144+
{
145+
return arg.vertexRadius;
146+
}
147+
148+
/// @brief Gets the vertex radius of the given shape configuration.
149+
inline NonNegative<Length> GetVertexRadius(const EdgeShapeConf& arg, ChildCounter) noexcept
150+
{
151+
return GetVertexRadius(arg);
152+
}
153+
120154
/// @brief Gets the mass data for the given shape configuration.
121155
inline MassData GetMassData(const EdgeShapeConf& arg) noexcept
122156
{

PlayRho/Collision/Shapes/MultiShapeConf.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,12 @@ MassData GetMassData(const MultiShapeConf& arg) noexcept
3434
const auto origin = Length2{};
3535
auto weightedCenter = origin * Kilogram;
3636
auto I = RotInertia(0);
37-
const auto vertexRadius = arg.vertexRadius;
3837
const auto density = arg.density;
3938

4039
std::for_each(std::begin(arg.children), std::end(arg.children),
4140
[&](const ConvexHull& ch) {
42-
const auto dp = ch.GetDistanceProxy(vertexRadius);
43-
const auto md = playrho::d2::GetMassData(vertexRadius, density,
41+
const auto dp = ch.GetDistanceProxy();
42+
const auto md = playrho::d2::GetMassData(ch.GetVertexRadius(), density,
4443
Span<const Length2>(dp.GetVertices().begin(), dp.GetVertexCount()));
4544
mass += Mass{md.mass};
4645
weightedCenter += md.center * Mass{md.mass};
@@ -51,7 +50,7 @@ MassData GetMassData(const MultiShapeConf& arg) noexcept
5150
return MassData{center, mass, I};
5251
}
5352

54-
ConvexHull ConvexHull::Get(const VertexSet& pointSet)
53+
ConvexHull ConvexHull::Get(const VertexSet& pointSet, NonNegative<Length> vertexRadius)
5554
{
5655
auto vertices = GetConvexHullAsVector(pointSet);
5756
assert(!vertices.empty() && vertices.size() < std::numeric_limits<VertexCounter>::max());
@@ -74,12 +73,13 @@ ConvexHull ConvexHull::Get(const VertexSet& pointSet)
7473
normals.push_back(UnitVec{});
7574
}
7675

77-
return ConvexHull{vertices, normals};
76+
return ConvexHull{vertices, normals, vertexRadius};
7877
}
7978

80-
MultiShapeConf& MultiShapeConf::AddConvexHull(const VertexSet& pointSet) noexcept
79+
MultiShapeConf& MultiShapeConf::AddConvexHull(const VertexSet& pointSet,
80+
NonNegative<Length> vertexRadius) noexcept
8181
{
82-
children.emplace_back(ConvexHull::Get(pointSet));
82+
children.emplace_back(ConvexHull::Get(pointSet, vertexRadius));
8383
return *this;
8484
}
8585

0 commit comments

Comments
 (0)