Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several changes to Shape class #280

Merged
merged 2 commits into from
Jan 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions PlayRho/Collision/Shapes/ChainShapeConf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ namespace {
#endif
} // anonymous namespace

ChainShapeConf::ChainShapeConf():
ShapeBuilder{ShapeConf{ShapeConf{}.UseVertexRadius(GetDefaultVertexRadius())}}
{
// Intentionally empty.
}
ChainShapeConf::ChainShapeConf() = default;

ChainShapeConf& ChainShapeConf::Set(std::vector<Length2> vertices)
{
Expand Down Expand Up @@ -104,7 +100,6 @@ MassData ChainShapeConf::GetMassData() const noexcept
const auto density = this->density;
if (density > AreaDensity(0))
{
const auto vertexRadius = this->vertexRadius;
const auto vertexCount = GetVertexCount();
if (vertexCount > 1)
{
Expand Down Expand Up @@ -147,7 +142,6 @@ DistanceProxy ChainShapeConf::GetChild(ChildCounter index) const
{
throw InvalidArgument("index out of range");
}
const auto vertexRadius = this->vertexRadius;
const auto vertexCount = GetVertexCount();
if (vertexCount > 1)
{
Expand Down
36 changes: 35 additions & 1 deletion PlayRho/Collision/Shapes/ChainShapeConf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ChainShapeConf: public ShapeBuilder<ChainShapeConf>
/// @brief Gets the default vertex radius.
static PLAYRHO_CONSTEXPR inline NonNegative<Length> GetDefaultVertexRadius() noexcept
{
return DefaultLinearSlop * 2;
return NonNegative<Length>{DefaultLinearSlop * Real{2}};
}

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

/// @brief Uses the given vertex radius.
inline ChainShapeConf& UseVertexRadius(NonNegative<Length> value) noexcept;

/// @brief Gets the vertex count.
ChildCounter GetVertexCount() const noexcept
{
Expand Down Expand Up @@ -112,11 +115,30 @@ class ChainShapeConf: public ShapeBuilder<ChainShapeConf>
return !(lhs == rhs);
}

/// @brief Vertex radius.
///
/// @details This is the radius from the vertex that the shape's "skin" should
/// extend outward by. While any edges &mdash; line segments between multiple
/// vertices &mdash; are straight, corners between them (the vertices) are
/// rounded and treated as rounded. Shapes with larger vertex radiuses compared
/// to edge lengths therefore will be more prone to rolling or having other
/// shapes more prone to roll off of them.
///
/// @note This should be a non-negative value.
///
NonNegative<Length> vertexRadius = GetDefaultVertexRadius();

private:
std::vector<Length2> m_vertices; ///< Vertices.
std::vector<UnitVec> m_normals; ///< Normals.
};

inline ChainShapeConf& ChainShapeConf::UseVertexRadius(NonNegative<Length> value) noexcept
{
vertexRadius = value;
return *this;
}

// Free functions...

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

/// @brief Gets the vertex radius of the given shape configuration.
inline NonNegative<Length> GetVertexRadius(const ChainShapeConf& arg)
{
return arg.vertexRadius;
}

/// @brief Gets the vertex radius of the given shape configuration.
inline NonNegative<Length> GetVertexRadius(const ChainShapeConf& arg, ChildCounter)
{
return GetVertexRadius(arg);
}

} // namespace d2
} // namespace playrho

Expand Down
41 changes: 32 additions & 9 deletions PlayRho/Collision/Shapes/DiskShapeConf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,15 @@ namespace d2 {
struct DiskShapeConf: ShapeBuilder<DiskShapeConf>
{
/// @brief Gets the default radius.
static PLAYRHO_CONSTEXPR inline Length GetDefaultRadius() noexcept
static PLAYRHO_CONSTEXPR inline NonNegative<Length> GetDefaultRadius() noexcept
{
return DefaultLinearSlop * 2;
return NonNegative<Length>{DefaultLinearSlop * 2};
}

PLAYRHO_CONSTEXPR inline DiskShapeConf(): ShapeBuilder{ShapeConf{}.UseVertexRadius(GetDefaultRadius())}
{
// Intentionally empty.
}
PLAYRHO_CONSTEXPR DiskShapeConf() = default;

/// @brief Initializing constructor.
PLAYRHO_CONSTEXPR inline DiskShapeConf(Length radius): ShapeBuilder{ShapeConf{}.UseVertexRadius(radius)}
PLAYRHO_CONSTEXPR inline DiskShapeConf(NonNegative<Length> r): vertexRadius{r}
{
// Intentionally empty.
}
Expand All @@ -65,9 +62,9 @@ struct DiskShapeConf: ShapeBuilder<DiskShapeConf>
}

/// @brief Uses the given value as the radius.
PLAYRHO_CONSTEXPR inline DiskShapeConf& UseRadius(Length radius) noexcept
PLAYRHO_CONSTEXPR inline DiskShapeConf& UseRadius(NonNegative<Length> r) noexcept
{
vertexRadius = radius;
vertexRadius = r;
return *this;
}

Expand All @@ -83,6 +80,19 @@ struct DiskShapeConf: ShapeBuilder<DiskShapeConf>
return location;
}

/// @brief Vertex radius.
///
/// @details This is the radius from the vertex that the shape's "skin" should
/// extend outward by. While any edges &mdash; line segments between multiple
/// vertices &mdash; are straight, corners between them (the vertices) are
/// rounded and treated as rounded. Shapes with larger vertex radiuses compared
/// to edge lengths therefore will be more prone to rolling or having other
/// shapes more prone to roll off of them.
///
/// @note This should be a non-negative value.
///
NonNegative<Length> vertexRadius = GetDefaultRadius();

/// @brief Location for the disk shape to be centered at.
Length2 location = Length2{};
};
Expand Down Expand Up @@ -119,6 +129,19 @@ inline DistanceProxy GetChild(const DiskShapeConf& arg, ChildCounter index)
return DistanceProxy{arg.vertexRadius, 1, &arg.location, nullptr};
}

/// @brief Gets the vertex radius of the given shape configuration.
PLAYRHO_CONSTEXPR inline NonNegative<Length> GetVertexRadius(const DiskShapeConf& arg) noexcept
{
return arg.vertexRadius;
}

/// @brief Gets the vertex radius of the given shape configuration.
PLAYRHO_CONSTEXPR inline NonNegative<Length> GetVertexRadius(const DiskShapeConf& arg,
ChildCounter) noexcept
{
return GetVertexRadius(arg);
}

/// @brief Gets the mass data of the given disk shape configuration.
inline MassData GetMassData(const DiskShapeConf& arg) noexcept
{
Expand Down
8 changes: 1 addition & 7 deletions PlayRho/Collision/Shapes/EdgeShapeConf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@
namespace playrho {
namespace d2 {

EdgeShapeConf::EdgeShapeConf():
ShapeBuilder{ShapeConf{}.UseVertexRadius(GetDefaultVertexRadius())}
{
// Intentionally empty.
}

EdgeShapeConf::EdgeShapeConf(Length2 vA, Length2 vB, const EdgeShapeConf& conf) noexcept:
ShapeBuilder{conf}, m_vertices{vA, vB}
ShapeBuilder{conf}, vertexRadius{conf.vertexRadius}, m_vertices{vA, vB}
{
const auto normal = GetUnitVector(GetFwdPerpendicular(vB - vA));
m_normals[0] = normal;
Expand Down
40 changes: 37 additions & 3 deletions PlayRho/Collision/Shapes/EdgeShapeConf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class EdgeShapeConf: public ShapeBuilder<EdgeShapeConf>
{
public:
/// @brief Gets the default vertex radius.
static PLAYRHO_CONSTEXPR inline Length GetDefaultVertexRadius() noexcept
static PLAYRHO_CONSTEXPR inline NonNegative<Length> GetDefaultVertexRadius() noexcept
{
return DefaultLinearSlop * Real{2};
return NonNegative<Length>{DefaultLinearSlop * Real{2}};
}

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

EdgeShapeConf();
EdgeShapeConf() = default;

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

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

/// @brief Uses the given vertex radius.
EdgeShapeConf& UseVertexRadius(NonNegative<Length> value) noexcept;

/// @brief Gets vertex A.
Length2 GetVertexA() const noexcept
{
Expand All @@ -79,11 +82,30 @@ class EdgeShapeConf: public ShapeBuilder<EdgeShapeConf>
return DistanceProxy{vertexRadius, 2, m_vertices, m_normals};
}

/// @brief Vertex radius.
///
/// @details This is the radius from the vertex that the shape's "skin" should
/// extend outward by. While any edges &mdash; line segments between multiple
/// vertices &mdash; are straight, corners between them (the vertices) are
/// rounded and treated as rounded. Shapes with larger vertex radiuses compared
/// to edge lengths therefore will be more prone to rolling or having other
/// shapes more prone to roll off of them.
///
/// @note This should be a non-negative value.
///
NonNegative<Length> vertexRadius = GetDefaultVertexRadius();

private:
Length2 m_vertices[2] = {Length2{}, Length2{}}; ///< Vertices
UnitVec m_normals[2] = {UnitVec{}, UnitVec{}}; ///< Normals.
};

inline EdgeShapeConf& EdgeShapeConf::UseVertexRadius(NonNegative<Length> value) noexcept
{
vertexRadius = value;
return *this;
}

// Free functions...

/// @brief Equality operator.
Expand Down Expand Up @@ -117,6 +139,18 @@ inline DistanceProxy GetChild(const EdgeShapeConf& arg, ChildCounter index)
return arg.GetChild();
}

/// @brief Gets the vertex radius of the given shape configuration.
inline NonNegative<Length> GetVertexRadius(const EdgeShapeConf& arg) noexcept
{
return arg.vertexRadius;
}

/// @brief Gets the vertex radius of the given shape configuration.
inline NonNegative<Length> GetVertexRadius(const EdgeShapeConf& arg, ChildCounter) noexcept
{
return GetVertexRadius(arg);
}

/// @brief Gets the mass data for the given shape configuration.
inline MassData GetMassData(const EdgeShapeConf& arg) noexcept
{
Expand Down
14 changes: 7 additions & 7 deletions PlayRho/Collision/Shapes/MultiShapeConf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ MassData GetMassData(const MultiShapeConf& arg) noexcept
const auto origin = Length2{};
auto weightedCenter = origin * Kilogram;
auto I = RotInertia(0);
const auto vertexRadius = arg.vertexRadius;
const auto density = arg.density;

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

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

return ConvexHull{vertices, normals};
return ConvexHull{vertices, normals, vertexRadius};
}

MultiShapeConf& MultiShapeConf::AddConvexHull(const VertexSet& pointSet) noexcept
MultiShapeConf& MultiShapeConf::AddConvexHull(const VertexSet& pointSet,
NonNegative<Length> vertexRadius) noexcept
{
children.emplace_back(ConvexHull::Get(pointSet));
children.emplace_back(ConvexHull::Get(pointSet, vertexRadius));
return *this;
}

Expand Down
Loading