Skip to content
Snippets Groups Projects
Commit f711d6cd authored by Matthias Puchner's avatar Matthias Puchner
Browse files

add inversion possibility to Rectangle

add unit tests as well
parent b12a6be8
No related branches found
No related tags found
1 merge request!81Refactorings no. 2 for the upcoming removing of "region of interest" (ROI)
......@@ -19,7 +19,8 @@
//! @param ylow y-coordinate of lower left corner
//! @param xup x-coordinate of upper right corner
//! @param yup y-coordinate of upper right corner
Rectangle::Rectangle(double xlow, double ylow, double xup, double yup) : IShape2D("Rectangle")
Rectangle::Rectangle(double xlow, double ylow, double xup, double yup, bool inverted)
: IShape2D("Rectangle"), m_inverted(inverted)
{
if (xup <= xlow) {
std::ostringstream message;
......@@ -39,9 +40,15 @@ Rectangle::Rectangle(double xlow, double ylow, double xup, double yup) : IShape2
m_yup = yup;
}
void Rectangle::setInverted(bool inverted /*= true*/)
{
m_inverted = inverted;
}
bool Rectangle::contains(double x, double y) const
{
return x <= m_xup && x >= m_xlow && y <= m_yup && y >= m_ylow;
const bool inRect = x <= m_xup && x >= m_xlow && y <= m_yup && y >= m_ylow;
return m_inverted ? !inRect : inRect;
}
bool Rectangle::contains(const Bin1D& binx, const Bin1D& biny) const
......
......@@ -24,8 +24,10 @@
class Rectangle : public IShape2D {
public:
Rectangle(double xlow, double ylow, double xup, double yup);
Rectangle* clone() const { return new Rectangle(m_xlow, m_ylow, m_xup, m_yup); }
Rectangle(double xlow, double ylow, double xup, double yup, bool inverted = false);
Rectangle* clone() const { return new Rectangle(m_xlow, m_ylow, m_xup, m_yup, m_inverted); }
void setInverted(bool inverted = true);
bool contains(double x, double y) const;
bool contains(const Bin1D& binx, const Bin1D& biny) const;
......@@ -39,6 +41,7 @@ public:
private:
double m_xlow, m_ylow, m_xup, m_yup;
bool m_inverted;
};
#endif // BORNAGAIN_DEVICE_MASK_RECTANGLE_H
......@@ -41,6 +41,37 @@ TEST_F(Shape2DTest, Rectangle)
EXPECT_FALSE(clone->contains(binx2, biny2));
}
TEST_F(Shape2DTest, Rectangle_inverted)
{
Rectangle rect(-4.0, -2.0, 4.0, 2.0, true);
EXPECT_DOUBLE_EQ(32.0, rect.getArea());
EXPECT_FALSE(rect.contains(0.0, 0.0));
EXPECT_FALSE(rect.contains(4.0, 2.0));
EXPECT_FALSE(rect.contains(-4.0, -2.0));
EXPECT_FALSE(rect.contains(-4.0, -2.0));
EXPECT_TRUE(rect.contains(0.0, 2.01));
EXPECT_TRUE(rect.contains(4.0, -2.01));
Bin1D binx1(3.5, 4.5);
Bin1D biny1(1.5, 2.5);
EXPECT_FALSE(rect.contains(binx1, biny1));
Bin1D binx2(3.5, 4.6);
Bin1D biny2(1.5, 2.6);
EXPECT_TRUE(rect.contains(binx2, biny2));
std::unique_ptr<Rectangle> clone(rect.clone());
EXPECT_DOUBLE_EQ(32.0, clone->getArea());
EXPECT_FALSE(clone->contains(0.0, 0.0));
EXPECT_FALSE(clone->contains(4.0, 2.0));
EXPECT_FALSE(clone->contains(-4.0, -2.0));
EXPECT_FALSE(clone->contains(-4.0, -2.0));
EXPECT_TRUE(clone->contains(0.0, 2.01));
EXPECT_TRUE(clone->contains(4.0, -2.01));
EXPECT_FALSE(clone->contains(binx1, biny1));
EXPECT_TRUE(clone->contains(binx2, biny2));
}
TEST_F(Shape2DTest, Ellipse)
{
Ellipse ellipse(10.0, 1.0, 8.0, 4.0);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment