-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an iterator class to the ConvexPolygon
This is just a simple forward iterator that doesn't implement the full iterator interface. Refs #12584
- Loading branch information
1 parent
bce83dd
commit e7eec18
Showing
5 changed files
with
188 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
Code/Mantid/Framework/Geometry/src/Math/ConvexPolygonIterator.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//----------------------------------------------------------------------------- | ||
// Includes | ||
//----------------------------------------------------------------------------- | ||
#include "MantidGeometry/Math/ConvexPolygon.h" | ||
#include "MantidGeometry/Math/PolygonEdge.h" | ||
#include "MantidKernel/Exception.h" | ||
|
||
namespace Mantid { | ||
namespace Geometry { | ||
|
||
//----------------------------------------------------------------------------- | ||
// Public member functions | ||
//----------------------------------------------------------------------------- | ||
/** | ||
* @param polygon A reference to the initializing polygon | ||
*/ | ||
ConvexPolygon::Iterator::Iterator(const ConvexPolygon &polygon) | ||
: m_polygon(polygon), m_index(0) { | ||
if (!polygon.isValid()) { | ||
throw std::invalid_argument("Cannot create iterator for invalid polygon."); | ||
} | ||
} | ||
|
||
/** | ||
* @return Dereference the iterator and return the current value | ||
*/ | ||
const Kernel::V2D &ConvexPolygon::Iterator::operator*() const { | ||
return m_polygon[m_index]; | ||
} | ||
|
||
/** | ||
* Advance the iterator to the next point. When the iterator points to the | ||
* last point the next increment will take it back to the "first" point. | ||
*/ | ||
void ConvexPolygon::Iterator::operator++() { | ||
m_index = nextIndex(); | ||
} | ||
|
||
/** | ||
* @return A PolygonEdge defining the edge with the current point and the next | ||
*/ | ||
PolygonEdge ConvexPolygon::Iterator::edge() const { | ||
return PolygonEdge(**this, m_polygon[nextIndex()]); | ||
} | ||
|
||
//----------------------------------------------------------------------------- | ||
// Private member functions | ||
//----------------------------------------------------------------------------- | ||
/// @return The next index, taking into account cycling back after npoints() | ||
size_t ConvexPolygon::Iterator::nextIndex() const { | ||
return (m_index + 1) % m_polygon.npoints(); | ||
} | ||
|
||
} // namespace Geometry | ||
} // namespace Mantid |
89 changes: 89 additions & 0 deletions
89
Code/Mantid/Framework/Geometry/test/ConvexPolygonIteratorTest.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#ifndef MANTID_GEOMETRY_POLYGONPOINTITERATORTEST_H_ | ||
#define MANTID_GEOMETRY_POLYGONPOINTITERATORTEST_H_ | ||
|
||
#include <cxxtest/TestSuite.h> | ||
|
||
#include "MantidGeometry/Math/ConvexPolygon.h" | ||
#include "MantidGeometry/Math/PolygonEdge.h" | ||
#include "MantidKernel/V2D.h" | ||
|
||
using Mantid::Geometry::ConvexPolygon; | ||
using Mantid::Kernel::V2D; | ||
|
||
class ConvexPolygonIteratorTest : public CxxTest::TestSuite { | ||
public: | ||
// This pair of boilerplate methods prevent the suite being created statically | ||
// This means the constructor isn't called when running other tests | ||
static ConvexPolygonIteratorTest *createSuite() { | ||
return new ConvexPolygonIteratorTest(); | ||
} | ||
static void destroySuite(ConvexPolygonIteratorTest *suite) { delete suite; } | ||
|
||
// --------------------- Success cases -------------------------------------- | ||
|
||
void test_Iterator_Initially_Points_At_Index_Zero() { | ||
auto poly = makeRectangle(); | ||
ConvexPolygon::Iterator iter(poly); | ||
TS_ASSERT_EQUALS(V2D(), *iter); | ||
} | ||
|
||
void test_Increment_Moves_On_One_Point() { | ||
auto poly = makeRectangle(); | ||
ConvexPolygon::Iterator iter(poly); | ||
++iter; | ||
TS_ASSERT_EQUALS(V2D(0.0, 1.0), *iter); | ||
} | ||
|
||
void test_Incrementing_By_Number_Of_Points_Produces_First_Point() { | ||
auto poly = makeRectangle(); | ||
ConvexPolygon::Iterator iter(poly); | ||
for (size_t i = 0; i < poly.npoints(); ++i) { | ||
++iter; | ||
} | ||
TS_ASSERT_EQUALS(V2D(), *iter); | ||
} | ||
|
||
void test_edge_points_from_current_to_next() { | ||
auto poly = makeRectangle(); | ||
ConvexPolygon::Iterator iter(poly); | ||
auto p01 = iter.edge(); | ||
TS_ASSERT_EQUALS(V2D(), p01.start()); | ||
TS_ASSERT_EQUALS(V2D(0.0,1.0), p01.end()); | ||
|
||
++iter; | ||
auto p12 = iter.edge(); | ||
TS_ASSERT_EQUALS(V2D(0.0,1.0), p12.start()); | ||
TS_ASSERT_EQUALS(V2D(2.0,1.0), p12.end()); | ||
|
||
// Check final has first as end point | ||
++iter; | ||
++iter; | ||
auto p40 = iter.edge(); | ||
TS_ASSERT_EQUALS(V2D(2.0,0.0), p40.start()); | ||
TS_ASSERT_EQUALS(V2D(), p40.end()); | ||
} | ||
|
||
// --------------------- Failure cases -------------------------------------- | ||
|
||
void test_Invalid_Polygon_Theows_Error_On_Construction() { | ||
ConvexPolygon invalid; | ||
// TS_ASSERT_THROWS cannot be used if there is no default constructor | ||
// so use a pointer instead | ||
ConvexPolygon::Iterator *iter(NULL); | ||
TS_ASSERT_THROWS(iter = new ConvexPolygon::Iterator(invalid), | ||
std::invalid_argument); | ||
delete iter; | ||
} | ||
|
||
private: | ||
ConvexPolygon makeRectangle() { | ||
ConvexPolygon rectangle; | ||
rectangle.insert(0.0, 0.0); | ||
rectangle.insert(0.0, 1.0); | ||
rectangle.insert(2.0, 1.0); | ||
rectangle.insert(2.0, 0.0); | ||
return rectangle; | ||
} | ||
}; | ||
|
||
#endif /* MANTID_GEOMETRY_POLYGONPOINTITERATORTEST_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters