BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
Line.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Device/Mask/Line.cpp
6 //! @brief Implements class Line.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "Device/Mask/Line.h"
16 #include "Base/Axis/Bin.h"
17 #include "Base/Util/Algorithms.h"
18 
19 #include <boost/geometry.hpp>
20 #include <limits>
21 
22 using point_t = boost::geometry::model::d2::point_xy<double>;
23 using line_t = boost::geometry::model::linestring<point_t>;
24 
25 Line::Line(double x1, double y1, double x2, double y2)
26  : IShape2D("Line")
27  , m_x1(x1)
28  , m_y1(y1)
29  , m_x2(x2)
30  , m_y2(y2)
31 {
32 }
33 
34 bool Line::contains(double x, double y) const
35 {
36  point_t p(x, y);
37  line_t line;
38  line.emplace_back(m_x1, m_y1);
39  line.emplace_back(m_x2, m_y2);
40 
41  double d = boost::geometry::distance(p, line);
42 
43  return d < std::numeric_limits<double>::epsilon();
44 }
45 
46 // Calculates if line crosses the box made out of our bins.
47 // Ugly implementation, see discussion at http://stackoverflow.com/questions/21408977
48 bool Line::contains(const Bin1D& binx, const Bin1D& biny) const
49 {
50  std::vector<point_t> box_points;
51  box_points.emplace_back(binx.m_lower, biny.m_lower);
52  box_points.emplace_back(binx.m_lower, biny.m_upper);
53  box_points.emplace_back(binx.m_upper, biny.m_upper);
54  box_points.emplace_back(binx.m_upper, biny.m_lower);
55  box_points.emplace_back(binx.m_lower, biny.m_lower);
56 
57  std::vector<point_t> line_points;
58  line_points.emplace_back(m_x1, m_y1);
59  line_points.emplace_back(m_x2, m_y2);
60 
61  return boost::geometry::intersects(line_t(box_points.begin(), box_points.end()),
62  line_t(line_points.begin(), line_points.end()));
63 }
64 
65 // ------------------------------------------------------------------------- //
66 
67 //! @param x The value at which it crosses x-axes
69  : IShape2D("VerticalLine")
70  , m_x(x)
71 {
72 }
73 
74 bool VerticalLine::contains(double x, double /*y*/) const
75 {
77 }
78 
79 bool VerticalLine::contains(const Bin1D& binx, const Bin1D& /*biny*/) const
80 {
81  return m_x >= binx.m_lower && m_x <= binx.m_upper;
82 }
83 
84 // ------------------------------------------------------------------------- //
85 
86 //! @param y The value at which it crosses y-axes
88  : IShape2D("HorizontalLine")
89  , m_y(y)
90 {
91 }
92 
93 bool HorizontalLine::contains(double /*x*/, double y) const
94 {
96 }
97 
98 bool HorizontalLine::contains(const Bin1D& /*binx*/, const Bin1D& biny) const
99 {
100  return m_y >= biny.m_lower && m_y <= biny.m_upper;
101 }
Defines and implements namespace BaseUtils::algo with some algorithms.
Defines structs Bin1D, Bin1DCVector.
boost::geometry::model::linestring< point_t > line_t
Definition: Line.cpp:23
boost::geometry::model::d2::point_xy< double > point_t
Definition: Line.cpp:22
Defines class Line.
Definition: Bin.h:20
double m_upper
upper bound of the bin
Definition: Bin.h:29
double m_lower
lower bound of the bin
Definition: Bin.h:28
bool contains(double x, double y) const override
Returns true if point with given coordinates is inside or on border of the shape.
Definition: Line.cpp:93
double m_y
Definition: Line.h:66
HorizontalLine(double y)
Definition: Line.cpp:87
Basic class for all shapes in 2D.
Definition: IShape2D.h:26
bool contains(double x, double y) const override
Returns true if point with given coordinates is inside or on border of the shape.
Definition: Line.cpp:34
double m_x2
Definition: Line.h:32
double m_x1
Definition: Line.h:32
Line(double x1, double y1, double x2, double y2)
Definition: Line.cpp:25
double m_y1
Definition: Line.h:32
double m_y2
Definition: Line.h:32
bool contains(double x, double y) const override
Returns true if point with given coordinates is inside or on border of the shape.
Definition: Line.cpp:74
double m_x
Definition: Line.h:49
VerticalLine(double x)
Definition: Line.cpp:68
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Definition: Algorithms.h:33