BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
truncbox.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/ba3d/mesh/truncbox.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 
18 namespace GUI::RealSpace {
19 
21 { // t/D
22  if (tD <= 0)
23  return meshBox();
24 
25  float const D = .5f, t = D * (1 - qMin(tD, 1.f));
26  Vertices vs;
27  vs.reserve(150);
28 
29  QVector<float> as({+D, +t, -t, -D, -D, -t, +t, +D});
30  QVector<float> bs({+t, +D, +D, +t, -t, -D, -D, -t});
31 
32  auto side = [&](int ax, int ay, int az, int bx, int by, int bz, const Vector3D& d, bool rev) {
33  Vertices vs_(8);
34  for (int i = 0; i < 8; ++i)
35  vs_[rev ? 7 - i : i] =
36  Vector3D(ax * as.at(i) + bx * bs.at(i), ay * as.at(i) + by * bs.at(i),
37  az * as.at(i) + bz * bs.at(i))
38  + d;
39  vs.addFan(vs_, {0, 1, 2, 3, 4, 5, 6, 7, 0});
40  };
41 
42  // +0.5f is required to shift the bottom of the Truncated Box to the z=0 plane
43 
44  auto corner = [&](int x, int y, int z) {
45  Vertices vs_({{D * x, D * y, t * z + 0.5f},
46  {D * x, t * y, D * z + 0.5f},
47  {t * x, D * y, D * z + 0.5f}});
48  if (x * y * z > 0)
49  vs.addTriangle(vs_.at(0), vs_.at(2), vs_.at(1));
50  else
51  vs.addTriangle(vs_.at(0), vs_.at(1), vs_.at(2));
52  };
53 
54  side(0, 1, 0, 0, 0, 1, Vector3D(+D, 0, +0.5f), false);
55  side(0, 1, 0, 0, 0, 1, Vector3D(-D, 0, +0.5f), true);
56  side(1, 0, 0, 0, 0, 1, Vector3D(0, +D, +0.5f), true);
57  side(1, 0, 0, 0, 0, 1, Vector3D(0, -D, +0.5f), false);
58  side(1, 0, 0, 0, 1, 0, Vector3D(0, 0, +D + 0.5f), false);
59  side(1, 0, 0, 0, 1, 0, Vector3D(0, 0, -D + 0.5f), true);
60 
61  for (int x : {-1, +1})
62  for (int y : {-1, +1})
63  for (int z : {-1, +1})
64  corner(x, y, z);
65 
66  ASSERT(150 == vs.count());
67 
68  return makeMesh(vs);
69 }
70 
71 } // namespace GUI::RealSpace
static Mesh meshBox()
Definition: box.cpp:20
static Mesh makeMesh(const Vertices &vs, Vertices const *ns=nullptr)
Definition: geometry.cpp:117
QVector< Vert_Normal > Mesh
Definition: geometry.h:66
static Mesh meshTruncBox(float tD)
Definition: truncbox.cpp:20
Defines Geometry class.
void addTriangle(const Vector3D &, const Vector3D &, const Vector3D &)
Definition: geometry.cpp:35