BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
geometry.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.cpp
6 //! @brief Implements 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 
16 #include "Base/Utils/Assert.h"
17 #include "GUI/ba3d/model/model.h"
18 
19 namespace RealSpace {
20 //------------------------------------------------------------------------------
21 
22 Geometry::Vert_Normal::Vert_Normal(const Vector3D& v_, const Vector3D& n_) : v(v_), n(n_) {}
23 
25 {
26  for (int i = 0; i < n; ++i)
27  append(v);
28 }
29 
30 void Geometry::Vertices::addTriangle(const Vector3D& v1, const Vector3D& v2, const Vector3D& v3)
31 {
32  append(v1);
33  append(v2);
34  append(v3);
35 }
36 
37 void Geometry::Vertices::addQuad(const Vector3D& v1, const Vector3D& v2, const Vector3D& v3,
38  const Vector3D& v4)
39 {
40  addTriangle(v1, v2, v3);
41  addTriangle(v3, v4, v1);
42 }
43 
44 void Geometry::Vertices::addQuad(const Vertices& vs, unsigned i1, unsigned i2, unsigned i3,
45  unsigned i4)
46 {
47  addQuad(vs.at(i1), vs.at(i2), vs.at(i3), vs.at(i4));
48 }
49 
50 void Geometry::Vertices::addStrip(const Vertices& vs, const Indices& is)
51 {
52  ASSERT(is.size() >= 3); // at least one triangle
53  for (unsigned i = 0; i + 2 < is.size(); ++i)
54  if (i % 2)
55  addTriangle(vs.at(is.at(i)), vs.at(is.at(1 + i)), vs.at(is.at(2 + i)));
56  else
57  addTriangle(vs.at(is.at(i)), vs.at(is.at(2 + i)), vs.at(is.at(1 + i)));
58 }
59 
60 void Geometry::Vertices::addFan(const Vertices& vs, const Indices& is)
61 {
62  ASSERT(is.size() >= 3); // at least one triangle
63  auto& ctr = vs.at(is.at(0));
64  for (unsigned i = 0; i + 2 < is.size(); ++i)
65  addTriangle(ctr, vs.at(is.at(1 + i)), vs.at(is.at(2 + i)));
66 }
67 
68 //------------------------------------------------------------------------------
69 
71 {
72  using namespace GeometricID;
73 
74  switch (m_key.id) {
75  case BaseShape::Plane:
76  m_mesh = meshPlane();
77  break;
78  case BaseShape::Box:
79  m_mesh = meshBox();
80  break;
81  case BaseShape::Sphere:
83  break;
84  case BaseShape::Column:
86  break;
87  case BaseShape::Icosahedron:
89  break;
90  case BaseShape::Dodecahedron:
92  break;
93  case BaseShape::TruncatedBox:
95  break;
96  case BaseShape::Cuboctahedron:
98  break;
99  case BaseShape::Ripple:
101  break;
102  }
103 }
104 
106 {
107  // remove self from the store
109 }
110 
112 {
113  int nv = vs.count();
114  ASSERT(0 == nv % 3);
115  ASSERT(!ns || nv == ns->count()); // if normals not given, will be computed
116 
117  Mesh mesh(nv);
118 
119  for (int i = 0; i < nv; i += 3) {
120  const Vector3D &v0 = vs.at(0 + i), v1 = vs.at(1 + i), v2 = vs.at(2 + i);
121  const Vector3D *n0, *n1, *n2;
122  Vector3D nm;
123 
124  if (ns) {
125  n0 = &ns->at(0 + i);
126  n1 = &ns->at(1 + i);
127  n2 = &ns->at(2 + i);
128  } else {
129  nm = cross((v1 - v0), (v2 - v0));
130  n0 = n1 = n2 = &nm;
131  }
132 
133  mesh.append(Vert_Normal(v0, *n0));
134  mesh.append(Vert_Normal(v1, *n1));
135  mesh.append(Vert_Normal(v2, *n2));
136  }
137 
138  return mesh;
139 }
140 
142 {
143  return makeMesh(vs, &ns);
144 }
145 
146 //------------------------------------------------------------------------------
147 
149 {
150  auto it = m_geometries.find(key);
151  if (m_geometries.end() != it) {
152  if (auto g = it->second.lock())
153  return g;
154  }
155  GeometryHandle g = GeometryHandle(new Geometry(key));
156  m_geometries[key] = GeometryRef(g);
157  return g;
158 }
159 
161 {
162  emit deletingGeometry(&g);
163  m_geometries.erase(g.m_key);
164 }
165 
167 {
168  static GeometryStore gs;
169  return gs;
170 }
171 
172 //------------------------------------------------------------------------------
173 } // namespace RealSpace
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:31
void geometryDeleted(Geometry const &)
Definition: geometry.cpp:160
GeometryHandle getGeometry(GeometricID::Key)
Definition: geometry.cpp:148
Geometry(GeometricID::Key)
Definition: geometry.cpp:70
static Mesh meshTruncBox(float tD)
Definition: truncbox.cpp:20
static Mesh meshSphere(float cut, float baseShift=0.0f, float removedTop=0.0f)
Definition: sphere.cpp:23
static Mesh meshBox()
Definition: box.cpp:20
static Mesh meshCuboctahedron(float rH, float alpha, float H)
std::vector< unsigned > Indices
Definition: geometry.h:42
static Mesh meshIcosahedron()
Definition: icosahedron.cpp:22
GeometricID::Key m_key
Definition: geometry.h:72
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
static Mesh meshDodecahedron()
QVector< Vert_Normal > Mesh
Definition: geometry.h:66
static Mesh meshPlane()
Definition: plane.cpp:20
virtual ~Geometry()
Definition: geometry.cpp:105
static Mesh meshRipple(float numSides, float ratio_asymmetry_W)
Definition: ripple.cpp:21
Defines Geometry class.
Defines Model class.
std::shared_ptr< Geometry > GeometryHandle
Definition: geometry_inc.h:26
std::weak_ptr< Geometry > GeometryRef
Definition: geometry_inc.h:29
Vector3D cross(const Vector3D &v1, const Vector3D &v2)
Definition: def.cpp:54
GeometryStore & geometryStore()
Definition: geometry.cpp:166
static constexpr double nm
Definition: Units.h:39
void addVertex(const Vector3D &, int n=1)
Definition: geometry.cpp:24
void addStrip(const Vertices &, const Indices &)
Definition: geometry.cpp:50
void addTriangle(const Vector3D &, const Vector3D &, const Vector3D &)
Definition: geometry.cpp:30
void addFan(const Vertices &, const Indices &)
Definition: geometry.cpp:60
void addQuad(const Vector3D &, const Vector3D &, const Vector3D &, const Vector3D &)
Definition: geometry.cpp:37