qv::PolyArea: return 0 for degenerate polygons
This commit is contained in:
parent
d0ea010acb
commit
5c7a490fea
|
|
@ -545,7 +545,8 @@ template<typename Iter, typename T = typename std::iterator_traits<Iter>::value_
|
||||||
template<typename Iter, typename T = typename std::iterator_traits<Iter>::value_type, typename F = typename T::value_type>
|
template<typename Iter, typename T = typename std::iterator_traits<Iter>::value_type, typename F = typename T::value_type>
|
||||||
[[nodiscard]] inline F PolyArea(Iter begin, Iter end)
|
[[nodiscard]] inline F PolyArea(Iter begin, Iter end)
|
||||||
{
|
{
|
||||||
Q_assert((end - begin) >= 3);
|
if ((end - begin) < 3)
|
||||||
|
return static_cast<F>(0);
|
||||||
|
|
||||||
float poly_area = 0;
|
float poly_area = 0;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -196,6 +196,11 @@ TEST(mathlib, PolygonArea)
|
||||||
{0, 64, 0}, {64, 64, 0}, {64, 0, 0}};
|
{0, 64, 0}, {64, 64, 0}, {64, 0, 0}};
|
||||||
|
|
||||||
EXPECT_EQ(64.0f * 64.0f, qv::PolyArea(poly.begin(), poly.end()));
|
EXPECT_EQ(64.0f * 64.0f, qv::PolyArea(poly.begin(), poly.end()));
|
||||||
|
|
||||||
|
// 0, 1, or 2 vertices return 0 area
|
||||||
|
EXPECT_EQ(0.0f, qv::PolyArea(poly.begin(), poly.begin()));
|
||||||
|
EXPECT_EQ(0.0f, qv::PolyArea(poly.begin(), poly.begin() + 1));
|
||||||
|
EXPECT_EQ(0.0f, qv::PolyArea(poly.begin(), poly.begin() + 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(mathlib, BarycentricFromPoint)
|
TEST(mathlib, BarycentricFromPoint)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue