Skip to content
Snippets Groups Projects
Commit d26d4677 authored by Pospelov, Gennady's avatar Pospelov, Gennady
Browse files

Rectangle mask and tests

parent 04ff90e6
No related branches found
No related tags found
No related merge requests found
......@@ -20,8 +20,6 @@
// InfinitePlane, Line, VerticalLine, HorizontalLine, Ellipse, Rectangle
//
// Polygon stdvector<stdvector<double>> constructor
DetectorMask::DetectorMask()
{
......@@ -42,7 +40,6 @@ DetectorMask &DetectorMask::operator=(const DetectorMask &other)
tmp.swapContent(*this);
}
return *this;
}
void DetectorMask::addMask(const Geometry::IShape2D &shape, bool mask_value)
......
// ************************************************************************** //
//
// BornAgain: simulate and fit scattering at grazing incidence
//
//! @file Geometry/inc/Rectangle.h
//! @brief Defines class Rectangle.
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2015
//! @authors Scientific Computing Group at MLZ Garching
//! @authors C. Durniak, M. Ganeva, G. Pospelov, W. Van Herck, J. Wuttke
//
// ************************************************************************** //
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include "IShape2D.h"
namespace Geometry {
//! @class Rectangle
//! @ingroup tools
//! @brief The non-rotatable rectangle.
class Rectangle : public IShape2D {
public:
//! Rectangle constructor with lower left and upper right coordinates
//! @param xlow x-coordinate of lower left corner
//! @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(double xlow, double ylow, double xup, double yup);
Rectangle *clone() const;
//! Returns true if given point is inside or on border of polygon
bool contains(double x, double y) const;
//! Returns true if area defined by two bins is inside or on border of polygon.
//! More precisely, if mid point of two bins satisfy this condition.
bool contains(const Bin1D &binx, const Bin1D &biny) const;
double getArea() const;
private:
Rectangle(const Rectangle& other);
double m_xlow, m_ylow, m_xup, m_yup;
};
} // namespace Geometry
#endif
// ************************************************************************** //
//
// BornAgain: simulate and fit scattering at grazing incidence
//
//! @file Geometry/src/Rectangle.cpp
//! @brief Implements class Rectangle.
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2015
//! @authors Scientific Computing Group at MLZ Garching
//! @authors C. Durniak, M. Ganeva, G. Pospelov, W. Van Herck, J. Wuttke
//
// ************************************************************************** //
#include "Rectangle.h"
#include "Bin.h"
namespace Geometry {
Rectangle::Rectangle(double xlow, double ylow, double xup, double yup)
{
if(xup <= xlow) {
std::ostringstream message;
message << "Rectangle(double xlow, double ylow, double xup, double yup) -> Error. ";
message << " xup <= xlow" << std::endl;
throw LogicErrorException(message.str());
}
if(yup <= ylow) {
std::ostringstream message;
message << "Rectangle(double xlow, double ylow, double xup, double yup) -> Error. ";
message << " yup <= ylow" << std::endl;
throw LogicErrorException(message.str());
}
m_xlow = xlow;
m_ylow = ylow;
m_xup = xup;
m_yup = yup;
}
Rectangle *Rectangle::clone() const
{
return new Rectangle(*this);
}
bool Rectangle::contains(double x, double y) const
{
if(x <= m_xup && x >= m_xlow && y <= m_yup && y >= m_ylow) return true;
return false;
}
bool Rectangle::contains(const Bin1D &binx, const Bin1D &biny) const
{
return contains(binx.getMidPoint(), biny.getMidPoint());
}
double Rectangle::getArea() const
{
return (m_xup-m_xlow)*(m_yup-m_ylow);
}
Rectangle::Rectangle(const Rectangle &other)
: m_xlow(other.m_xlow)
, m_ylow(other.m_ylow)
, m_xup(other.m_xup)
, m_yup(other.m_yup)
{
}
} // namespace Geometry
#ifndef SHAPE2DTEST_H
#define SHAPE2DTEST_H
#include "Rectangle.h"
#include "gtest/gtest.h"
#include <boost/scoped_ptr.hpp>
#include <boost/assign/list_of.hpp>
class Shape2DTest : public ::testing::Test
{
public:
};
TEST_F(Shape2DTest, Rectangle)
{
Geometry::Rectangle rect(-4.0, -2.0, 4.0, 2.0);
EXPECT_DOUBLE_EQ(32.0, rect.getArea());
EXPECT_TRUE(rect.contains(0.0, 0.0));
EXPECT_TRUE(rect.contains(4.0, 2.0));
EXPECT_TRUE(rect.contains(-4.0, -2.0));
EXPECT_TRUE(rect.contains(-4.0, -2.0));
EXPECT_FALSE(rect.contains(0.0, 2.01));
EXPECT_FALSE(rect.contains(4.0, -2.01));
Bin1D binx1(3.5, 4.5);
Bin1D biny1(1.5, 2.5);
EXPECT_TRUE(rect.contains(binx1, biny1));
Bin1D binx2(3.5, 4.6);
Bin1D biny2(1.5, 2.6);
EXPECT_FALSE(rect.contains(binx2, biny2));
boost::scoped_ptr<Geometry::Rectangle> clone(rect.clone());
EXPECT_DOUBLE_EQ(32.0, clone->getArea());
EXPECT_TRUE(clone->contains(0.0, 0.0));
EXPECT_TRUE(clone->contains(4.0, 2.0));
EXPECT_TRUE(clone->contains(-4.0, -2.0));
EXPECT_TRUE(clone->contains(-4.0, -2.0));
EXPECT_FALSE(clone->contains(0.0, 2.01));
EXPECT_FALSE(clone->contains(4.0, -2.01));
EXPECT_TRUE(clone->contains(binx1, biny1));
EXPECT_FALSE(clone->contains(binx2, biny2));
}
#endif
......@@ -55,6 +55,7 @@
#include "Histogram2DTest.h"
#include "PolygonTest.h"
#include "DetectorMaskTest.h"
#include "Shape2DTest.h"
......
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