BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
Line.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
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/Utils/Algorithms.h"
18 
19 #include <boost/geometry.hpp>
20 #include <boost/geometry/geometries/linestring.hpp>
21 #include <boost/geometry/geometries/point_xy.hpp>
22 #include <limits>
23 
24 using namespace boost::geometry;
25 typedef model::d2::point_xy<double> point_t;
26 typedef model::box<point_t> box_t;
27 typedef model::linestring<point_t> line_t;
28 
29 Line::Line(double x1, double y1, double x2, double y2)
30  : IShape2D("Line"), m_x1(x1), m_y1(y1), m_x2(x2), 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.push_back(point_t(m_x1, m_y1));
39  line.push_back(point_t(m_x2, m_y2));
40 
41  double d = 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.push_back(point_t(binx.m_lower, biny.m_lower));
52  box_points.push_back(point_t(binx.m_lower, biny.m_upper));
53  box_points.push_back(point_t(binx.m_upper, biny.m_upper));
54  box_points.push_back(point_t(binx.m_upper, biny.m_lower));
55  box_points.push_back(point_t(binx.m_lower, biny.m_lower));
56 
57  std::vector<point_t> line_points;
58  line_points.push_back(point_t(m_x1, m_y1));
59  line_points.push_back(point_t(m_x2, m_y2));
60 
61  return 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
68 VerticalLine::VerticalLine(double x) : IShape2D("VerticalLine"), m_x(x) {}
69 
70 bool VerticalLine::contains(double x, double /*y*/) const
71 {
72  return algo::almostEqual(x, m_x);
73 }
74 
75 bool VerticalLine::contains(const Bin1D& binx, const Bin1D& /*biny*/) const
76 {
77  return m_x >= binx.m_lower && m_x <= binx.m_upper;
78 }
79 
80 // ------------------------------------------------------------------------- //
81 
82 //! @param y The value at which it crosses y-axes
83 HorizontalLine::HorizontalLine(double y) : IShape2D("HorizontalLine"), m_y(y) {}
84 
85 bool HorizontalLine::contains(double /*x*/, double y) const
86 {
87  return algo::almostEqual(y, m_y);
88 }
89 
90 bool HorizontalLine::contains(const Bin1D& /*binx*/, const Bin1D& biny) const
91 {
92  return m_y >= biny.m_lower && m_y <= biny.m_upper;
93 }
Defines and implements namespace algo with some algorithms.
Defines structs Bin1D, Bin1DCVector.
Defines class Line.
bool contains(double x, double y) const
Returns true if point with given coordinates is inside or on border of the shape.
Definition: Line.cpp:85
HorizontalLine(double y)
Definition: Line.cpp:83
Basic class for all shapes in 2D.
Definition: IShape2D.h:27
bool contains(double x, double y) const
Returns true if point with given coordinates is inside or on border of the shape.
Definition: Line.cpp:34
bool contains(double x, double y) const
Returns true if point with given coordinates is inside or on border of the shape.
Definition: Line.cpp:70
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:30
Definition: Bin.h:20
double m_upper
upper bound of the bin
Definition: Bin.h:24
double m_lower
lower bound of the bin
Definition: Bin.h:23