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