BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
geometry.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/ba3d/model/geometry.h
6 //! @brief Defines Geometry class
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 #ifndef BORNAGAIN_GUI_BA3D_MODEL_GEOMETRY_H
16 #define BORNAGAIN_GUI_BA3D_MODEL_GEOMETRY_H
17 
19 #include <QObject>
20 #include <QVector>
21 #include <unordered_map>
22 #include <vector>
23 
24 namespace GUI::RealSpace {
25 
26 class Buffer;
27 class GeometryStore;
28 
29 class Geometry {
30  friend class Buffer;
31  friend class GeometryStore;
32 
33 public:
34  // vertex + normal pair
35  struct Vert_Normal {
37  Vert_Normal() = default;
38  Vert_Normal(const Vector3D& v, const Vector3D& n);
39  };
40 
41  // vertex indices (for GL)
42  using Indices = std::vector<unsigned>;
43 
44  // vertices (for GL)
45  struct Vertices : private QVector<Vector3D> {
46  using QVector::append;
47  using QVector::QVector;
48  using QVector::reserve;
49  using QVector::resize;
50  using QVector::operator[];
51  using QVector::at;
52  using QVector::begin;
53  using QVector::count;
54  using QVector::end;
55 
56  void addVertex(const Vector3D&, int n = 1); // add a vertex, possibly multiple copies
57  void addTriangle(const Vector3D&, const Vector3D&, const Vector3D&); // triangle
58  void addQuad(const Vector3D&, const Vector3D&, const Vector3D&,
59  const Vector3D&); // quad as 2 triangles
60  void addQuad(const Vertices&, unsigned, unsigned, unsigned, unsigned); // quad by indices
61  void addStrip(const Vertices&, const Indices&); // triangle strip
62  void addFan(const Vertices&, const Indices&); // triangle fan
63  };
64 
65  // vertex/normal mesh
66  using Mesh = QVector<Vert_Normal>;
67 
69  virtual ~Geometry();
70 
71 private:
73 
75  // make a mesh from vectors of vertices and (optionally) normals
76  static Mesh makeMesh(const Vertices& vs, Vertices const* ns = nullptr);
77  static Mesh makeMesh(const Vertices& vs, const Vertices& ns);
78 
79  static Mesh meshPlane();
80  static Mesh meshBox();
81  static Mesh meshSphere(float cut, float baseShift = 0.0f, float removedTop = 0.0f);
82  static Mesh meshColumn(float ratio_Rt_Rb, float numSides);
83  static Mesh meshIcosahedron();
84  static Mesh meshDodecahedron();
85  static Mesh meshTruncBox(float tD);
86  static Mesh meshBipyramid4(float rH, float alpha, float H);
87  static Mesh meshRipple(float numSides, float ratio_asymmetry_W);
88 
89  // mesh params for round shapes
90  static int const RINGS = 12, SLICES = 24;
91 };
92 
93 // a single store keeps existing geometries for sharing
94 
95 class GeometryStore : public QObject {
96  Q_OBJECT
97  friend class Geometry;
98 
99 public:
101 
102 signals:
103  void deletingGeometry(Geometry const*); // signal to canvases
104 
105 private:
106  std::unordered_map<GeometricID::Key, GeometryRef, GeometricID::KeyHash> m_geometries;
107  void geometryDeleted(Geometry const&); // ~Geometry() calls this
108 };
109 
110 GeometryStore& geometryStore(); // simpleton
111 
112 } // namespace GUI::RealSpace
113 
114 #endif // BORNAGAIN_GUI_BA3D_MODEL_GEOMETRY_H
GL buffer.
Definition: buffer.h:28
std::unordered_map< GeometricID::Key, GeometryRef, GeometricID::KeyHash > m_geometries
Definition: geometry.h:106
GeometryHandle getGeometry(GeometricID::Key)
Definition: geometry.cpp:158
void deletingGeometry(Geometry const *)
void geometryDeleted(Geometry const &)
Definition: geometry.cpp:170
static Mesh meshPlane()
Definition: plane.cpp:20
static Mesh meshBox()
Definition: box.cpp:20
static Mesh makeMesh(const Vertices &vs, Vertices const *ns=nullptr)
Definition: geometry.cpp:117
static int const SLICES
Definition: geometry.h:90
GeometricID::Key m_key
Definition: geometry.h:72
QVector< Vert_Normal > Mesh
Definition: geometry.h:66
static Mesh meshColumn(float ratio_Rt_Rb, float numSides)
Definition: column.cpp:22
static Mesh meshBipyramid4(float rH, float alpha, float H)
static Mesh meshTruncBox(float tD)
Definition: truncbox.cpp:20
Geometry(GeometricID::Key)
Definition: geometry.cpp:75
static int const RINGS
Definition: geometry.h:90
static Mesh meshDodecahedron()
static Mesh meshIcosahedron()
Definition: icosahedron.cpp:22
static Mesh meshSphere(float cut, float baseShift=0.0f, float removedTop=0.0f)
Definition: sphere.cpp:24
std::vector< unsigned > Indices
Definition: geometry.h:42
static Mesh meshRipple(float numSides, float ratio_asymmetry_W)
Definition: ripple.cpp:21
Defines namespace GUI::RealSpace::GeometricID.
std::shared_ptr< Geometry > GeometryHandle
Definition: geometry_inc.h:27
GeometryStore & geometryStore()
Definition: geometry.cpp:176
Real shapes will be parameterized by BaseShape enum and possibly two floats.
Definition: geometry_inc.h:51
void addTriangle(const Vector3D &, const Vector3D &, const Vector3D &)
Definition: geometry.cpp:35
void addStrip(const Vertices &, const Indices &)
Definition: geometry.cpp:55
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
void addFan(const Vertices &, const Indices &)
Definition: geometry.cpp:65