BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
column.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/ba3d/mesh/column.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/Math/Constants.h"
16 #include "Base/Util/Assert.h"
18 #include <qmath.h>
19 
20 namespace GUI::RealSpace {
21 
22 Geometry::Mesh Geometry::meshColumn(float ratio_Rt_Rb, float numSides)
23 {
24  int const sides = qRound(numSides);
25  bool const smooth = (0 == sides); // sides = 0 implies smooth -> e.g. cylinder
26  int const slices = smooth ? SLICES : sides;
27 
28  // Rt is the top radius, Rb is the bottom radius, H is the height
29  // Values are chosen such that diameter and height are 1
30  float const R = .5f;
31  float Rb = R, Rt = Rb * ratio_Rt_Rb, H = 2 * R;
32 
33  // mesh of vertices (bottom and top) and normals
34  Vertices vb(slices), vt(slices), nbt(slices);
35  float const nz = (1 - Rt / Rb) * H;
36 
37  for (int s = 0; s < slices; ++s) {
38  float th = float(M_TWOPI * s / slices), st = sinf(th), ct = cosf(th);
39 
40  Vector3D vb_(Rb * ct, Rb * st, 0), vt_(Rt * ct, Rt * st, H);
41  vb[s] = vb_;
42  vt[s] = vt_;
43  if (smooth)
44  nbt[s] = Vector3D(vb_.x, vb_.y, nz).normalized();
45  }
46 
47  // make into triangles
48  int const nv = 6 * 2 * slices;
49  Vertices vs;
50  vs.reserve(nv);
51  Vertices ns;
52  if (smooth)
53  ns.reserve(nv);
54 
55  for (int s = 0; s < slices; ++s) {
56  int s1 = s, s2 = (s + 1) % slices;
57 
58  vs.addTriangle(vb.at(s1), Vector3D(0, 0, 0), vb.at(s2)); // bottom
59  if (smooth)
60  ns.addVertex(-Vector3D::_z, 3);
61 
62  vs.addTriangle(Vector3D(0, 0, H), vt.at(s1), vt.at(s2)); // top
63  if (smooth)
64  ns.addVertex(+Vector3D::_z, 3);
65 
66  vs.addQuad(vb.at(s1), vb.at(s2), vt.at(s2), vt.at(s1)); // side
67  if (smooth) {
68  auto &n1 = nbt.at(s1), &n2 = nbt.at(s2);
69  ns.addQuad(n1, n2, n2, n1);
70  }
71  }
72 
73  ASSERT(vs.count() == nv);
74  ASSERT(!smooth || ns.count() == nv);
75 
76  return makeMesh(vs, smooth ? &ns : nullptr);
77 }
78 
79 } // 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 meshColumn(float ratio_Rt_Rb, float numSides)
Definition: column.cpp:22
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
void addVertex(const Vector3D &, int n=1)
Definition: geometry.cpp:29
static Vector3D const _z
Definition: def.h:46
Vector3D normalized() const
Definition: def.cpp:49