Skip to content

Commit

Permalink
Opt: perform in-place CurvedPolygon::reverseOrientation()
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyweiss committed Nov 25, 2021
1 parent e7b59aa commit 6861e21
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/axom/primal/geometry/CurvedPolygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,23 @@ class CurvedPolygon
void reverseOrientation()
{
const int ngon = numEdges();
std::vector<BezierCurve<T, NDIMS>> old_edges = m_edges;
for(int i = 0; i < ngon; ++i)
const int mid = ngon >> 1;
const bool isOdd = (ngon & 1) != 0;

// Swap matching left/right cases, unmatched center is dealt with below
for(int i = 0; i < mid; ++i)
{
const int left = i;
const int right = ngon - i - 1;
m_edges[left].reverseOrientation();
m_edges[right].reverseOrientation();
axom::utilities::swap(m_edges[left], m_edges[right]);
}

// Handle unmatched center curve, if necessary
if(isOdd)
{
old_edges[ngon - 1 - i].reverseOrientation();
m_edges[i] = old_edges[ngon - 1 - i];
m_edges[mid].reverseOrientation();
}
}

Expand Down
79 changes: 79 additions & 0 deletions src/axom/primal/tests/primal_curved_polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@

#include "gtest/gtest.h"

#include "axom/config.hpp"
#include "axom/slic.hpp"
#include "axom/primal/geometry/Point.hpp"
#include "axom/primal/geometry/Segment.hpp"
#include "axom/primal/geometry/CurvedPolygon.hpp"
#include "axom/primal/geometry/OrientationResult.hpp"
#include "axom/primal/operators/intersect.hpp"
#include "axom/primal/operators/compute_moments.hpp"
#include "axom/primal/operators/orientation.hpp"

#include <numeric>
#include <math.h>

namespace primal = axom::primal;

Expand Down Expand Up @@ -537,6 +542,80 @@ TEST(primal_curvedpolygon, area_intersection_triangle_quadratic)
EXPECT_NEAR(A, expA, 1e-10);
}


//----------------------------------------------------------------------------------
TEST(primal_curvedpolygon, reverseOrientation)
{
const int DIM = 2;
const int order = 1;
using CoordType = double;
using CurvedPolygonType = primal::CurvedPolygon<CoordType, DIM>;
using PointType = primal::Point<CoordType, DIM>;
using SegmentType = primal::Segment<CoordType, DIM>;
using BezierCurveType = primal::BezierCurve<CoordType, DIM>;

// Create a set of line segments on the unit circle
const int MAX_SEG = 10;
const PointType origin;
for(int nseg = 3; nseg < MAX_SEG; ++nseg)
{
CurvedPolygonType poly(nseg);
axom::Array<PointType> pts(nseg + 1);
for(int i = 0; i < nseg; ++i)
{
const double theta = i / static_cast<double>(nseg) * (2. * M_PI);
pts[i] = PointType {cos(theta), sin(theta)};
}
pts[nseg] = pts[0];

for(int i = 0; i < nseg; ++i)
{
poly[i] = BezierCurveType(&pts[i], order);
}

// Perform some checks
for(int i = 0; i < nseg; ++i)
{
// check that the end point of each segment is equal to the start of the next
auto& currentEnd = poly[i][order];
auto& nextStart = poly[(i + 1) % nseg][0];
EXPECT_EQ(currentEnd, nextStart);

// check that the orientation of segment midpoints goes in the same direction
SegmentType seg(poly[i].evaluate(0.5), poly[(i + 1) % nseg].evaluate(0.5));
EXPECT_EQ(primal::ON_NEGATIVE_SIDE, primal::orientation(origin, seg));
}

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

EXPECT_EQ(poly.numEdges(), reversed.numEdges());

// Perform some checks on the reversed polygon
for(int i = 0; i < nseg; ++i)
{
// check that order of each segment stayed the same
EXPECT_EQ(poly[i].getOrder(), reversed[i].getOrder());

// check that the end point of each segment is equal to the start of the next
auto& currentEnd = reversed[i][order];
auto& nextStart = reversed[(i + 1) % nseg][0];
EXPECT_EQ(currentEnd, nextStart);

// check that segment midpoints are oriented in same direction (opposite of origin)
SegmentType seg(reversed[i].evaluate(0.5),
reversed[(i + 1) % nseg].evaluate(0.5));
EXPECT_EQ(primal::ON_POSITIVE_SIDE, primal::orientation(origin, seg));
}

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

//----------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
Expand Down

0 comments on commit 6861e21

Please sign in to comment.