Skip to content

Commit

Permalink
Slight clean up of primal::CurvedPolygon::isClosed()
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyweiss committed Nov 25, 2021
1 parent 6861e21 commit 8115436
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
17 changes: 7 additions & 10 deletions src/axom/primal/geometry/CurvedPolygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,22 +184,19 @@ class CurvedPolygon
return false;
}

// foreach edge: check last vertex of current edge against first vertex of next edge
for(int i = 1; i < nEdges; ++i)
// foreach edge: check last vertex of previous edge against first vertex of current edge
for(int cur = 0, prev = nEdges - 1; cur < nEdges; prev = cur++)
{
const auto ord = m_edges[i - 1].getOrder();
const auto& lastPrev = m_edges[i - 1][ord];
const auto& firstCur = m_edges[i][0];
const auto ord = m_edges[prev].getOrder();
const auto& lastPrev = m_edges[prev][ord];
const auto& firstCur = m_edges[cur][0];
if(!isNearlyEqual(squared_distance(lastPrev, firstCur), 0., sq_tol))
{
return false;
}
}
// check last edge against first
const auto ord = m_edges[nEdges - 1].getOrder();
const auto& lastPrev = m_edges[nEdges - 1][ord];
const auto& firstCur = m_edges[0][0];
return isNearlyEqual(squared_distance(lastPrev, firstCur), 0., sq_tol);

return true;
}

/// \brief Reverses orientation of a CurvedPolygon
Expand Down
48 changes: 31 additions & 17 deletions src/axom/primal/tests/primal_curved_polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,28 +158,40 @@ TEST(primal_curvedpolygon, isClosed)

SLIC_INFO("Test checking if CurvedPolygon is closed.");

CurvedPolygonType bPolygon;
EXPECT_EQ(0, bPolygon.numEdges());
EXPECT_EQ(false, bPolygon.isClosed());
{
CurvedPolygonType bPolygon;
EXPECT_EQ(0, bPolygon.numEdges());
EXPECT_FALSE(bPolygon.isClosed());
}

std::vector<PointType> CP = {PointType {0.6, 1.2},
PointType {0.3, 2.0},
PointType {0.0, 1.6},
PointType {0.6, 1.2}};
std::vector<int> orders = {1, 1, 1};

std::vector<PointType> subCP = {PointType {0.6, 1.2}, PointType {0.3, 2.0}};
std::vector<int> suborders = {1};
CurvedPolygonType subPolygon = createPolygon(subCP, suborders);
EXPECT_EQ(false, subPolygon.isClosed());
{
std::vector<PointType> subCP = {PointType {0.6, 1.2}, PointType {0.3, 2.0}};
std::vector<int> suborders = {1};
CurvedPolygonType subPolygon = createPolygon(subCP, suborders);
EXPECT_FALSE(subPolygon.isClosed());
}

{
CurvedPolygonType bPolygon = createPolygon(CP, orders);
EXPECT_EQ(3, bPolygon.numEdges());
EXPECT_TRUE(bPolygon.isClosed());

bPolygon = createPolygon(CP, orders);
bPolygon[2][1][0] -= 2e-15;
EXPECT_FALSE(bPolygon.isClosed(1e-15));
}

EXPECT_EQ(3, bPolygon.numEdges());
EXPECT_EQ(true, bPolygon.isClosed());
{
CurvedPolygonType bPolygon = createPolygon(CP, orders);

bPolygon[2][1][0] -= 2e-15;
EXPECT_EQ(false, bPolygon.isClosed(1e-15));
bPolygon[1][0][0] = 5;
EXPECT_FALSE(bPolygon.isClosed(1e-15));
}
}

//----------------------------------------------------------------------------------
Expand All @@ -194,7 +206,7 @@ TEST(primal_curvedpolygon, isClosed_BiGon)

CurvedPolygonType bPolygon;
EXPECT_EQ(0, bPolygon.numEdges());
EXPECT_EQ(false, bPolygon.isClosed());
EXPECT_FALSE(bPolygon.isClosed());

// Bi-gon defined by a quadratic edge and a straight line
std::vector<PointType> CP = {PointType {0.8, .25},
Expand Down Expand Up @@ -554,11 +566,12 @@ TEST(primal_curvedpolygon, reverseOrientation)
using SegmentType = primal::Segment<CoordType, DIM>;
using BezierCurveType = primal::BezierCurve<CoordType, DIM>;

// Create a set of line segments on the unit circle
// Test several n-gons discretizing the unit circle
const int MAX_SEG = 10;
const PointType origin;
for(int nseg = 3; nseg < MAX_SEG; ++nseg)
{
// Create an n-gon with line segments going CCW along the unit circle
CurvedPolygonType poly(nseg);
axom::Array<PointType> pts(nseg + 1);
for(int i = 0; i < nseg; ++i)
Expand All @@ -572,8 +585,9 @@ TEST(primal_curvedpolygon, reverseOrientation)
{
poly[i] = BezierCurveType(&pts[i], order);
}
EXPECT_TRUE(poly.isClosed());

// Perform some checks
// Perform some checks on the polygon
for(int i = 0; i < nseg; ++i)
{
// check that the end point of each segment is equal to the start of the next
Expand All @@ -586,7 +600,7 @@ TEST(primal_curvedpolygon, reverseOrientation)
EXPECT_EQ(primal::ON_NEGATIVE_SIDE, primal::orientation(origin, seg));
}

// Create a polygon of reversed segments;
// Create a polygon with reversed orientation
CurvedPolygonType reversed = poly;
reversed.reverseOrientation();

Expand All @@ -609,7 +623,7 @@ TEST(primal_curvedpolygon, reverseOrientation)
EXPECT_EQ(primal::ON_POSITIVE_SIDE, primal::orientation(origin, seg));
}

// Check that reversing twice yields the original;
// Check that reversing twice yields the original
CurvedPolygonType reversedAgain = reversed;
reversedAgain.reverseOrientation();
EXPECT_EQ(poly, reversedAgain);
Expand Down

0 comments on commit 8115436

Please sign in to comment.