BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
IShape3D.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Sample/Shapes/IShape3D.cpp
6 //! @brief Implements default methods of interface IShape3D.
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 "Sample/Shapes/IShape3D.h"
16 
17 #include <cmath>
18 
19 // Value of 24 ensures that real points stick out of the convex hull at most
20 // 1% of the radius
21 const size_t IShape3D::N_Circle = 24;
22 
23 std::vector<R3> IShape3D::vertices() const
24 {
25  return m_vertices;
26 }
27 
28 std::vector<R3> RectangleVertices(double length, double width, double z)
29 {
30  std::vector<R3> result = {{length / 2.0, width / 2.0, z},
31  {-length / 2.0, width / 2.0, z},
32  {-length / 2.0, -width / 2.0, z},
33  {length / 2.0, -width / 2.0, z}};
34  return result;
35 }
36 
37 std::vector<R3> EllipseVerticesZ(double r_x, double r_y, double z)
38 {
39  static constexpr double delta_angle = 2.0 * M_PI / IShape3D::N_Circle;
40  std::vector<R3> result(IShape3D::N_Circle);
41  for (size_t i = 0; i < IShape3D::N_Circle; ++i) {
42  double angle = i * delta_angle;
43  double x = r_x * std::cos(angle);
44  double y = r_y * std::sin(angle);
45  result[i] = R3(x, y, z);
46  }
47  return result;
48 }
49 
50 std::vector<R3> EllipseVerticesXtrunc(double x, double r_y, double r_z, double z_b, double z_t)
51 {
52  static constexpr double delta_angle = 2.0 * M_PI / IShape3D::N_Circle;
53  // for full ellipse
54  // std::vector<R3> result(IShape3D::N_Circle);
55  // for (size_t i = 0; i < IShape3D::N_Circle; ++i) {
56  // double angle = i * delta_angle;
57  // double y = r_y * std::cos(angle);
58  // double z = r_z * std::sin(angle) + r_z; // bottom is at 0, top at 2*r_z
59  // result[i] = R3(x, y, z);
60  // }
61 
62  // for truncated ellipse
63  std::vector<R3> result;
64  result.reserve(IShape3D::N_Circle + 4);
65  for (size_t i = 0; i < IShape3D::N_Circle; ++i) {
66  double angle = i * delta_angle;
67  double y = r_y * std::cos(angle);
68  double z = r_z * std::sin(angle) - z_b; // bottom is at 0, top at z_t-z_b
69 
70  if (0 <= z && z <= (z_t - z_b)) {
71  result.push_back(R3(x, y, z));
72  }
73  }
74  double alpha_t = atan(z_t / sqrt(r_z * r_z - z_t * z_t) * r_z / r_y);
75  double alpha_b = atan(z_b / sqrt(r_z * r_z - z_b * z_b) * r_z / r_y);
76 
77  double y_t = r_y * std::cos(alpha_t);
78  double y_b = r_y * std::cos(alpha_b);
79 
80  result.push_back(R3(x, y_t, z_t - z_b));
81  result.push_back(R3(x, -y_t, z_t - z_b));
82  result.push_back(R3(x, y_b, 0));
83  result.push_back(R3(x, -y_b, 0));
84 
85  return result;
86 }
#define M_PI
Definition: Constants.h:44
std::vector< R3 > EllipseVerticesXtrunc(double x, double r_y, double r_z, double z_b, double z_t)
Generate vertices of centered vertical ellipse with given semi-axes at position x and truncation heig...
Definition: IShape3D.cpp:50
std::vector< R3 > EllipseVerticesZ(double r_x, double r_y, double z)
Generate vertices of centered horizontal ellipse with given semi-axes at height z.
Definition: IShape3D.cpp:37
std::vector< R3 > RectangleVertices(double length, double width, double z)
Helper functions to construct lists of vertices.
Definition: IShape3D.cpp:28
Defines interface IShape3D.
virtual std::vector< R3 > vertices() const
Retrieves a list of the vertices constituting this concrete shape.
Definition: IShape3D.cpp:23
std::vector< R3 > m_vertices
List of vertices initialized during construction.
Definition: IShape3D.h:42
static const size_t N_Circle
Definition: IShape3D.h:38