BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ripple.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/ba3d/mesh/ripple.cpp
6 //! @brief Implements utility functions in ba3d namespace
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 "Base/Util/Assert.h"
17 #include <qmath.h>
18 
19 namespace GUI::RealSpace {
20 
21 Geometry::Mesh Geometry::meshRipple(float numSides, float ratio_asymmetry_W)
22 {
23  int const sides = qRound(numSides);
24  bool const smooth = (0 == sides); // sides = 0 implies smooth -> e.g. cosine ripple
25  int const slices = smooth ? 4 * SLICES : sides;
26 
27  // Values are chosen such that length, width and height are 1
28  float const R = .5f;
29  float const H = 2 * R; // L = W = H = 2*R
30  float const asymmetry = ratio_asymmetry_W * 2 * R; // for CosineRipple asymmetry is inherently 0
31 
32  Vertices vfront(slices), vback(slices);
33 
34  if (numSides == 3) {
35  // SawtoothRipple: saw-tooth (3 rectangular sides and 2 triangular front and back)
36  vfront[0] = Vector3D(-R, -R, 0);
37  vfront[1] = Vector3D(-R, asymmetry, H);
38  vfront[2] = Vector3D(-R, R, 0);
39 
40  vback[0] = Vector3D(R, -R, 0);
41  vback[1] = Vector3D(R, asymmetry, H);
42  vback[2] = Vector3D(R, R, 0);
43  } else if (numSides == 0) {
44  // CosineRipple: cosine ripple
45  for (int s = 0; s < slices; ++s) {
46  auto th = static_cast<float>(M_PI * s / (slices + 1));
47  float y = -R * cosf(th);
48  float z = R * (1.0f + cosf(2 * static_cast<float>(M_PI) * y / (2 * R)));
49  vfront[s] = Vector3D(-R, y, z);
50  vback[s] = Vector3D(R, y, z);
51  }
52  }
53 
54  int const nv = (3 + 3 + 6) * slices; // 3 for part of front face, 3 for part of back face
55  // 6 for side face
56 
57  Vertices vs;
58  vs.reserve(nv);
59 
60  for (int s = 0; s < slices; ++s) {
61  int s1 = s, s2 = (s + 1) % slices;
62 
63  // clockwise ordering of vertices
64  vs.addTriangle(vfront.at(s1), vfront.at(s2), Vector3D(-R, asymmetry, H / 2)); // front
65 
66  // counter-clockwise ordering of vertices
67  vs.addTriangle(vback.at(s1), Vector3D(R, asymmetry, H / 2), vback.at(s2)); // back
68 
69  // counter-clockwise ordering of vertices
70  vs.addQuad(vfront.at(s2), vfront.at(s1), vback.at(s1), vback.at(s2)); // side
71  }
72 
73  ASSERT(vs.count() == nv);
74 
75  return makeMesh(vs, nullptr); // normals not calculated here and left for makeMesh to calculate
76 }
77 
78 } // namespace GUI::RealSpace
static Mesh makeMesh(const Vertices &vs, Vertices const *ns=nullptr)
Definition: geometry.cpp:117
static int const SLICES
Definition: geometry.h:90
QVector< Vert_Normal > Mesh
Definition: geometry.h:66
static Mesh meshRipple(float numSides, float ratio_asymmetry_W)
Definition: ripple.cpp:21
Defines Geometry class.
void addTriangle(const Vector3D &, const Vector3D &, const Vector3D &)
Definition: geometry.cpp:35
void addQuad(const Vector3D &, const Vector3D &, const Vector3D &, const Vector3D &)
Definition: geometry.cpp:42